9

In creating a static object is it possible to rename the symbols at compile time (without changing the code) in a cross platform way? I have recently had objcopy recommended, but linux is not the only target platform it must also work on a mac. I am compiling using gcc, so I was hoping that there was a gcc option of some sort.

I have heard about .def files, but this may have been misleading as the information about them that I have found seems to be for windows.

Edit: I'm trying to change the name of C and Fortran functions, specifically pre-pending them with the word "wrap" in order to avoid symbol conflicts at link time.

V.S.
  • 2,924
  • 4
  • 32
  • 43
  • Can you give an example of what sort of symbols you would want to rename, and from what to what, and why? – John Zwinck Aug 27 '11 at 16:58
  • Essentially because I'm wrapping some code that I'm not supposed to touch, but I want to change some symbols to avoid symbol conflicts. If I can't change the code, I'd like to change the symbols that the compiler outputs. As to what - I want to rename functions, specifically prepending their symbol name with the word "wrap". – V.S. Aug 27 '11 at 17:28
  • Another option in cases like that might be to run the "untouchable" library in its own process, so that there are no conflicts or other ugliness, and just expose its functionality via a well-defined interface, probably via sockets. – John Zwinck Aug 27 '11 at 17:30
  • I'll be honest, I know nothing about sockets, but that seems like it would be a much slower way of doing things, these functions are called incredibly regularly. Currently the best option seems to be editing the code and asking for forgiveness! – V.S. Aug 27 '11 at 17:33

2 Answers2

8

is it possible to rename the symbols at compile time

You might be able to achieve it with preprocessor:

gcc -c foo.c -Dfoo=foo_renamed
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Sorry, I missed the fortran tag in the original question. Assumedly your answer only applies to the C preprocessor? – V.S. Aug 27 '11 at 17:30
  • 1
    Should work for Fortran just as well: http://gcc.gnu.org/onlinedocs/gfortran/Preprocessing-and-conditional-compilation.html Why don't you just *try* it? – Employed Russian Aug 27 '11 at 21:00
  • Yep should work. Just a few implementation hiccoughs that need to be addressed. Firstly I'm changing a large number of symbols, so the -Dfoo=bar syntax is a little ugly, however I've discovered the -include flag for GCC which lets you include a file. ALSO there's a small issue of fortran being case insensitive, whilst the preprocessor isn't. But still, minor problems :). – V.S. Aug 28 '11 at 09:02
  • it seem for this to work `foo` needs to be sufficiently unique that it doesn't appear as a substring anywhere else. Be aware also for fixed form fortran your preprocessor sub risks making lines too long if the new name is longer than the original. – agentp Oct 27 '17 at 19:27
7

You can use the gcc alias attribute to make multiple symbols that point to the same function.

void name1() __attribute__((alias ("name2")));

I'm not sure if the alias attribute works for other types of symbols (e.g. variables).

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Sorry, I previously missed out the fortran tag. And I believe this only applies to C! :( – V.S. Aug 27 '11 at 17:35
  • This should work for Fortran as well, but will not solve the problem: you'll still have the original name1, and will still cause conflict. You could possibly hide name1 by making it static. But both of these require changes to the source code, which the OP is trying to avoid in the first place. – Employed Russian Aug 27 '11 at 21:03
  • actually looking into the documentation of gcc there is also `__attribute__((weak))` which means that if another global symbol exists it will be used instead. – Alexander Oh Oct 10 '13 at 12:12