How to not use -wrap for all linked libraries?

Viewed 124

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

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.

Related