Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux shared library: replace symbol runtime

I have a quite large C application consisting of several shared libraries. One of the core libraries has a function

void common_function(const char * arg) { ... }

Which is called by all the other libraries. During testing I would like to use a different test implementation of the common_function.

My plan has been to create a test library containing the alternate implementation of common_function; is it at all possible to replace the default common_function runtime using dlopen() / dlsym() trickery, or alternatively would this link line:

gcc -o test.c -ltest -lcommon

ensure that the common_function implementation in libtest.so was used also in the libcommon.so - altough the latter has it's own implementation of common_function.

like image 698
user422005 Avatar asked May 06 '26 10:05

user422005


1 Answers

Read Drepper's paper: How To Write Shared Libraries and wikipage on dynamic linker.

You probably want to play the LD_PRELOAD trick (assuming all your libraries are shared, not static).

like image 160
Basile Starynkevitch Avatar answered May 08 '26 02:05

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!