编译自定义的malloc [英] Compiling a custom malloc

查看:169
本文介绍了编译自定义的malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写它实现了一个自定义库的malloc /释放calloc / realloc的/自由使用标准的C原型,我想通了,如何把它编译成一个如此。我想通过链接反对一个标准的应用程序来测试库?什么是做到这一点的好办法?有一次,我有一个工作库我以为我可以只是LD_ preLOAD加载它,但我怎么得到我的功能与共存而是采取precedence在系统库的呢?我的功能需要进行调用malloc,以获得运行内存,所以我不能只是完全沟STDLIB ...帮助?

I have written a custom library which implements malloc/calloc/realloc/free using the standard C prototypes, and I figured out how to compile it to an so. I want to test the library by linking a standard application against it? What would be a good way to do this? Once I have a working library I assume I can just load it with LD_PRELOAD, but how do I get my functions to co-exist with but take precedence over the system library ones? My functions need to make a call to malloc in order to get memory to run, so I can't just completely ditch stdlib... Help?

推荐答案

这是您要替换功能是标准的C函数,而不是宏,没有系统调用。所以,你必须简单地给你的函数相同的名称,并编译成一个共享库。

Functions that you are trying to replace are standard C functions, not macros, not system calls. So you have to simply give your functions the same names and compile them into a shared library.

然后,使用 LD_ preLOAD 来pre-到您的书架。由于所有的地址都解决了一次,连接器将计算出你的函数地址,并记住他们的名字,并在标准库不会看他们以后。

Then, use LD_PRELOAD to pre-load your library before binary starts. Since all addresses are resolved once, linker will figure out addresses of your functions and remember their names and will not look for them in standard library later.

如果你的程序与标准运行静态链接,此方法可能无法正常工作。此外,它不会在Mac OS X因为有插值另一个API工作。

This approach might not work if your program is linked with the standard runtime statically. Also, it will not work on Mac OS X as there is another API for interpolation.

在Linux中,例如,为了让您的功能并存(即,如果您要使用系统的malloc 在自己的执行的malloc ),您必须使用手动打开标准库的dlopen ,查找你需要有使用功能则dlsym 和地址后给他们打电话。

In Linux, for example, in order for your functions to co-exist (i.e. if you want to use system malloc in your own implementation of malloc), you have to open the standard library manually using dlopen, look up functions you need there using dlsym and call them later by address.

这篇关于编译自定义的malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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