如何重新定义malloc()在Linux中用于C ++新 [英] How to redefine malloc() in Linux for use in C++ new

查看:602
本文介绍了如何重新定义malloc()在Linux中用于C ++新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mem_malloc()和mem_free()为我定义,我想使用它们来替换malloc()和free(),从而C ++的新和删除。



我将它们定义如下:

  externC{

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);但是,我得到两个链接错误:




$ b >

  [user @ machine test] $ g ++ -m32 -pthread main.cpp -static libmemnmf-Oa 
/ usr / lib / ../lib/libc.a(malloc.o):在函数`free':
(.text + 0x153c):`free'的多重定义
/tmp/ccD2Mgln.o:main。 cpp :( .text + 0x842):首先在这里定义
/usr/lib/../lib/libc.a(malloc.o):在函数`malloc'中:
(.text + 0x3084 ):`malloc'的多重定义
/tmp/ccD2Mgln.o:main.cpp:(.text+0x856):首先在这里定义
libmemnmf-Oa(mem_debug.o):在函数`mem_init_debug_routines ':
mem_debug.c :(。text + 0x83c):未定义的引用`dlopen'
mem_debug.c :(。+ 0x89d):未定义引用`dlsym'
mem_debug。 c:。文本+ 0xa03):未定义的引用`dlclose'
mem_debug.c :(。text + 0xa24):未定义引用`dlclose'
mem_debug.c :(。 :未定义引用`dlerror'
collect2:ld返回1退出状态

1)我会得到乘法定义的malloc()和free()错误消失,只是接受我的定义,而不是内置的一个?



2)什么库提供dlopen () 和朋友?

解决方案

我假设你定义malloc和free在main .cpp文件,并且mem_alloc和mem_free位于libmemnmf-0.a



可能发生的是,main.cpp中的一些引用需要对象从glibc。具体来说,有些是动态加载库(dlopen)。此代码包含在glibc中(回答问题2)。
当链接器包含来自glibc的对象并发现这些对象需要malloc / free符号时,它将尝试直接包含glibc库中的malloc / free。
由于您的-static连接器标志,整个libmemnmf-0.a库静态地包含在您的可执行文件中。这显然会在您的可执行文件中包含另一个malloc和自由对象。



您应该做的是将malloc和自由例程放在一个单独的.o文件中,在你的链接命令中,最好是在结尾(假设你没有在该行上以特殊方式指定标准库)。 .o文件将满足所有符号请求,glibc库将找到这些匹配,只要dlopen或其他对象需要它们。
区别是libmnef-0.a文件是一个库,链接器与库的处理方式不同于简单对象(这与通过库解析符号库中的对象所请求的符号的次数有关) )。
或者,你可以删除-static标志,我期望解决问题,但你可能有一个很好的理由包括该标志开始。



如果要覆盖new和delete的行为,还可以查看重载操作符new和操作符delete以提供类特定的分配方法或内存池。


I have a mem_malloc() and mem_free() defined for me and I want to use them to replace the malloc() and free() and consequently C++'s 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-O.a
/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-O.a(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 do I get the multiply defined malloc() and free() errors to go away and just take my definition and not the built in one?

2) What library provides dlopen() and friends? I'd expect this to be built in, but they are undefined.

解决方案

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

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

What you should do is put the malloc and free routines in a separate .o file and add that file somewhere in your link-command, preferrably on the end (assuming you do not specify the standard library in a special way on that line). The .o file will satisfy all symbol requests and the glibc library will find these matches resolved whenever dlopen or other objects require them. The difference is that the libmnef-0.a file is a library and linkers deal differently with libraries than with simple objects (which has to do with the number of passes through a library to resolve symbols requested by objects in that library). Alternatively, you can drop the -static flag, which I expect to resolve the issue as well, but you probably have a good reason to include that flag to start with.

If you want to override the behaviour of new and delete, you can also look into overloading operator new and operator delete for classes to provide a class-specific allocation method or memory pool.

这篇关于如何重新定义malloc()在Linux中用于C ++新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆