2

Let's assume that I have the following C code:

extern int f_1();
extern int g_1();

extern int f_2();
extern int g_2();

extern int f_3();
extern int g_3();

int main(int argc, char **argv) {
    // Using f_1, f_2, f_3 and g_1, g_2, g_3 here:
    ...
}

And I want to build it by linking with 3 different libraries: l1, l2, l3 -- assuming each of them exports its own f and g functions -- so that:

  • f_1 and g_1 will be resolved to f and g respectively from l1;
  • f_2 and g_2 will be resolved to f and g respectively from l2;
  • f_3 and g_3 will be resolved to f and g respectively from l3.

Is this possible with gcc and ld:

  1. Is this possible if l1, l2, l3 are shared libraries (.so)?
  2. Is this possible if l1, l2, l3 are archives (.a)?
Sasha
  • 3,599
  • 1
  • 31
  • 52
  • It should be possible with shared libraries (.so) files IMO. Maybe [this](http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html) will help. – rurtle Jun 13 '16 at 18:29

1 Answers1

1

objcopy's --redefine-sym option is your friend:

--redefine-sym old=new

Change the name of a symbol old, to new. This can be useful when one is trying link two things together for which you have no source, and there are name collisions.

--redefine-syms=filename

Apply --redefine-sym to each symbol pair "old new" listed in the file filename. filename is simply a flat file, with one symbol pair per line. Line comments may be introduced by the hash character. This option may be given more than once.

Apply it to you libraries l1, l2, l3. It should work both for .a and .so.

Leon
  • 31,443
  • 4
  • 72
  • 97
  • This is only for **.o** files, isn't it? And it **modifies** (or *creates copy of*) an object file -- so not a way to link to existing .so/.a files, am I right? – Sasha Jun 13 '16 at 18:43
  • That's a reasonable suggestion, but the question was "Is this possible with gcc and ld [...]?". Shall I take your answer as a "no" to that? – John Bollinger Jun 13 '16 at 18:43
  • The danger is that the libraries may still have given the same name to different objects and functions. And `objcopy` change the visibility of public symbols to be hidden, except for the specific functions that the OP wants to rename? – jxh Jun 13 '16 at 18:44
  • @Sasha objcopy handles **.a** and **.so** files too. It can both modify the input file or create a renamed copy. – Leon Jun 13 '16 at 18:53
  • @JohnBollinger I would say that in general case, the answer to OP's original question is "no". – Leon Jun 13 '16 at 18:55
  • @Leon, but it forces me to clone .a/.so file -- not allowing to link directly to existing one. Am I right? (Although, with .a files it probably makes no difference whether I link to original .a or modified.) – Sasha Jun 13 '16 at 19:03
  • @Sasha You got it right. But look out for other possible conflicts between other symbols in the libraries. – Leon Jun 13 '16 at 19:08
  • @Leon, and if it need to link *identically-named* functions from *different* .so files (without modifying or copying them) -- really no way to do that without [dlopen](http://man7.org/linux/man-pages/man3/dlopen.3.html)? – Sasha Jun 13 '16 at 19:47
  • Are they different functions, that happen to have identical names? If so - should both of them be callable? – Leon Jun 13 '16 at 19:52
  • @Sasha That will be a problem. Even dlopen will not help (if the two functions need to be usable at the same time). – Leon Jun 13 '16 at 20:10
  • @Leon, dlopen will surely help. But I don't believe that pure ld cannot do it (it's such a trivial task). – Sasha Jun 14 '16 at 06:21