Overwriting a weak symbol of one shared library by another

282 views Asked by At

I have a program that is linked against a (shared) library libFoo.so. Now I needed to write a patched version of that library. Call it libFooPatch although the name will be libFoo.so too. This one defines an additional weak symbol Init.

First scenario (just for clarification):
Now suppose I would have implemented "Init" in the program. At runtime libFooPatch is loaded through LD_LIBRARY_PATH.
If the program was linked against libFoo calling "Init" from libFooPatch causes it to call only the weak referenced version, not the one from the program. Linking it against libFooPatch fixes that and it works without any problems.
1) But why is that? Shouldn't at runtime the strong referenced "Init" from the program be called?

Now the real problem (2nd scenario):
I cannot change the program itself. It does not define "Init" and is linked against libFoo. I can change libFooPatch which is still instead of libFoo by placing it into LD_LIBRARY_PATH.
I want to write a (shared) library libBar that is used in the program (most probably by dlopen), defines "Init" as a strong symbol and make libFooPatch call this one.
However I can't get it to work. My own Init function is never called and even obtaining a pointer within libBar gets me only the weak one from libFooPatch.

"nm libBar.so" returns "0000000000000eb0 T Init" so it is correctly defined there, isn't it?

Any pointers how I can get this to work?

0

There are 0 answers