I\'m trying to interpose malloc/free/calloc/realloc etc with some interposers via LD_PRELOAD. In my small test, only malloc
seems to be interposed, even though
You are compiling a C++; this means that (by default) functions are name-mangled to fit the C++ ABI. Unfortunately, the functions you are trying to hook are not name-mangled, so you end up hooking the wrong (nonexistent) function.
The fix is to either a) define your wrappers in a extern "C" { }
block or a) make sure you include the header for the function - as in #include <cstdlib>
. This will pull in declarations for malloc
and free
with an extern "C"
wrapper, telling the compiler not to name-mangle.
The reason malloc
works is probably because <stdio.h>
or <dlfcn.h>
pulls in a declaration for malloc
but not free
. Thus, malloc
is unmangled, but free
is mangled.
Also note: If you're using glibc, you should be using malloc hooks to hook memory allocation functions. glibc does some pretty weird stuff with symbol versioning that may interfere with your hooks otherwise. An example of how to use it is in the documentation linked - don't forget extern "C"
s if you're using C++, though!