Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not use -wrap for all linked libraries?

Tags:

c

gcc

linker

My program uses several precompiled static libraries. I wrap malloc and free, but I want one of linked libraries to use 'real' malloc and free. As I run:

gcc [...] -W1, --wrap=malloc -W1, --wrap=free [used libraries]

all libraries would use wrapped functions.

Is partial linking a way here? What if I link this one library dynamically?

Thanks in advance, Jacek

like image 671
jbulatek Avatar asked Nov 26 '25 11:11

jbulatek


1 Answers

You should be able to achieve this with some elf trickery on your binary static libraries.

Let's assume, that your library is called libbar.a. Then you can change all calls to malloc() to __real_malloc() with the help of objcopy:

objcopy libbar.a --redefine-sym malloc=__real_malloc --redefine-sym free=__real_free libbar2.a

Now, if you link the copied (modified) libbar2.a instead of libbar.a the original (not wrapped) malloc() and free() should be called.

like image 99
Ctx Avatar answered Nov 29 '25 08:11

Ctx