Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23178

Weak symbols, shared libraries and dlopen

I have a binary with a weak symbol that I want to be able to link at runtime with a run dependent shared library.

$nm testrun
...         
w basic2.test
...

My first test was using a .o file at static linktime, that worked, but I need it to be shared.

So, my second test was getting a shared library with that symbol defined and link it at compile time with -lmy (libmy.so), and this, actually worked as well.

Third step tried not linking at compile time and use ld_preload trick and this did not work.

nm libmy.so
...
00000550 T basic2.test
...

I have really no idea why this particular one does not work, looks like dynamic loader should have enough information to set testruns weak symbol with the one in libmy.so.

My final objective, which I guess will require more work is to load at start a small function that does check for the appropiate symbol with dlsym and sets it there.

Any hint?

Upvotes: 1

Views: 3341

Answers (1)

iabdalkader
iabdalkader

Reputation: 17312

It seems that you may need to use LD_DYNAMIC_WEAK along with LD_PRELOAD from the man page:

LD_DYNAMIC_WEAK (glibc since 2.1.91) Allow weak symbols to be overridden (reverting to old glibc behavior). For security reasons, since glibc 2.3.4, LD_DYNAMIC_WEAK is ignored for set-user-ID/set-group-ID binaries.

Note: it could be a typo, but you should use -lmylib.so and not -Lmylib.so

Upvotes: 3

Related Questions