How to override malloc () on Linux for use in C ++ new

I have mem_malloc () and mem_free () defined for me and I want to use them to replace malloc () and free () and therefore C ++ new and delete.

I define them as follows:

extern "C" { extern void *mem_malloc(size_t); extern void mem_free(void *); void * malloc(size_t size) { return mem_malloc(size); } void free(void *memory) { mem_free(memory); } } 

However, I get two link errors:

 [ user@machine test]$ g++ -m32 -pthread main.cpp -static libmemnmf-Oa /usr/lib/../lib/libc.a(malloc.o): In function `free': (.text+0x153c): multiple definition of `free' /tmp/ccD2Mgln.o:main.cpp:(.text+0x842): first defined here /usr/lib/../lib/libc.a(malloc.o): In function `malloc': (.text+0x3084): multiple definition of `malloc' /tmp/ccD2Mgln.o:main.cpp:(.text+0x856): first defined here libmemnmf-Oa(mem_debug.o): In function `mem_init_debug_routines': mem_debug.c:(.text+0x83c): undefined reference to `dlopen' mem_debug.c:(.text+0x89d): undefined reference to `dlsym' mem_debug.c:(.text+0xa03): undefined reference to `dlclose' mem_debug.c:(.text+0xa24): undefined reference to `dlclose' mem_debug.c:(.text+0xa2e): undefined reference to `dlerror' collect2: ld returned 1 exit status 

1) How to get malloc () and free () error errors to leave and just take my definition, not the one built into it?

2) Which library has dlopen () and friends in? I expect this to be inline, but they are undefined.

+4
source share
5 answers

I assume that you define malloc for free in the main.cpp file that you are trying to compile, and that mem_alloc and mem_free are located in libmemnmf-0.a

What is probably happening is that some links in main.cpp require objects from glibc. In particular, something dynamically loads the library (dlopen). This code is included in glibc (to answer question 2). When the linker includes objects from glibc and finds that the malloc / free symbol is required for these objects, it will try to directly enable malloc / free from the glibc library. Because of your -static linker flag, the entire libmemnmf-0.a library is statically included in your executable. This will obviously include another malloc and a free object in your executable.

What you need to do is put malloc and free routines in a separate .o file and add this file somewhere in your link command, preferably at the end (unless you specify a standard library in a special way on this line). The .o file will satisfy all character requests, and the glibc library will find these matches resolved whenever dlopen or other objects are required. The difference is that the libmnef-0.a file is a library, and linkers deal with libraries differently than simple objects (which is due to the number of passes through the library to resolve the characters requested by objects in this library). Alternatively, you can remove the -static flag, which I expect will fix the problem as well, but you probably have a good reason to turn this flag on for a start.

If you want to override the behavior of new and delete, you can also view the overload of the new operator and the delete operator for classes to provide a class allocation method or memory pool.

+4
source

Try:

 #define free mem_free #define malloc mem_alloc 

After turning on stdlib.h .

2.) dlopen () and friends:

 #include <dlfcn.h> 

Linker flags: -ldl

See the dlopen (3) man page .

+2
source

By default, you bind libc, which defines malloc, try renaming C # define. Dlopen etc. Defined in ld .

+1
source

You can give preference to the library using the environment variable LD_LIBRARY_PATH and LD_PRELOAD .

This will allow you to provide custom malloc and free in a dynamic library that will prevail over the provided libc. I believe this is preferable to #define based solutions.

0
source

I was looking for this answer. Finally, I found something that works for me. The linker has the option --wrap = symbol, which you can use.

Run

 man ld 

and search for "wrap" for details.

I posted this in case someone finds this question, but none of the answers work for him, as was the case in my case.

0
source

All Articles