How can I get gcc to add a prefix to all symbol names
Asked Answered
E

2

15

I know that in the past there was an option -fprefix-function-name that would add a prefix to all generated symbols, it doesn't seem to be part of gcc anymore. Is there any other way to do this?

Ephesian answered 14/4, 2012 at 21:53 Comment(3)
Why do you need to do this? Perhaps using a namespace would solve your probem?Aborning
I'm compiling code I got from someone else. The code is multi threaded and it's trying to use an old, non thread safe, C library. The solution found by the person who wrote the app is to have multiple copies of the C library, each of which has a different prefix to it's symbol names. Then each thread of the app calls a different version of the function. So, for instance, if the old library has a funcion oldlib_func, there would be the versions v1_oldlib_func, v2_oldlib_func so that thread number 1 would call v1_oldlib_func and thread number 2 would call v2_oldlib_func.Ephesian
The person who wrote the app had to create these different versions of the old library with these prefixes, the thing is I didn't get that part of the code so I have to take the original library code and find a way to add a prefix to the symbol names.Ephesian
A
19

I believe this answer will give you the solution.

In short, you can 'prefix' symbols in an existing library using objcopy like this:

objcopy --prefix-symbols=foo_ foo.o

Aborning answered 14/4, 2012 at 22:40 Comment(1)
That's great if you use GNU, but what if you use MSVC, IAR, Keil, Green Hills, or any dozen other compilers out there?Bonaventure
S
3

*EDIT: George Skoptsov's solution's better than mine :) The nm trick might come in handy though.


This is not exactly what you are looking for, but I have had to do something similar in the past (renaming the symbols exported by a library)

If you know the names of the symbols you want to redefine you can try using objcopy --redefine-syms old=new . See the man pages of objcopy for more details on the input (objcopy might overwrite your file so be careful with that)

If you do not know the names of the symbols you can trying using nm to get a list of symbols. Again, since I am not sure what kind of symbols you are looking for, the man pages will probably be your best bet.

Subak answered 14/4, 2012 at 22:46 Comment(2)
I "love" how a command called objcopy does not, in fact, produce a copy.Foilsman
@DeadMG: it can produce a copy (and does, internally), but clearly modification in place is too useful a feature not to add it. :)Hardhack

© 2022 - 2024 — McMap. All rights reserved.