编译自定义 malloc
我编写了一个自定义库,它使用标准 C 原型实现 malloc/calloc/realloc/free,并且我弄清楚了如何将其编译为 so。我想通过链接标准应用程序来测试该库?这样做的好方法是什么?一旦我有了一个工作库,我假设我可以使用 LD_PRELOAD 加载它,但是如何让我的函数与系统库函数共存但优先于系统库函数?我的函数需要调用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试替换的函数是标准 C 函数,而不是宏,也不是系统调用。因此,您必须简单地为您的函数提供相同的名称并将它们编译到共享库中。
然后,在二进制文件启动之前使用
LD_PRELOAD
预加载您的库。由于所有地址都被解析一次,链接器将找出函数的地址并记住它们的名称,并且以后不会在标准库中查找它们。如果您的程序与标准运行时静态链接,则此方法可能不起作用。此外,它不适用于 Mac OS X,因为还有另一个用于插值的 API。
例如,在 Linux 中,为了使您的函数共存(即,如果您想在自己的
malloc
实现中使用系统malloc
),您必须打开使用dlopen
手动打开标准库,使用dlsym
查找您需要的函数,并稍后通过地址调用它们。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.
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.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.
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 ofmalloc
), you have to open the standard library manually usingdlopen
, look up functions you need there usingdlsym
and call them later by address.不要用
malloc()
来编写malloc()
,而是使用sbrk
来编写,它直接从操作系统获取内存。Don't write your
malloc()
in terms ofmalloc()
-- write it usingsbrk
, which gets memory directly from the OS.如果您可以控制使用该库的源代码,那么这是一种可能性。使用不同的函数名称:例如,将其称为 newCoolMalloc,而不是 malloc。此方法有时更简单,并且不依赖于特殊的链接器选项。
然后在您的代码中,使用
#define
使代码调用所需的函数集。你可以#define malloc 做一些不同的事情。例如:但是,如果你这样做,你必须非常非常小心地一致地包含它。否则,您将面临在一个地方使用 stdlib malloc,然后在另一个地方使用自己的 free 的风险,从而导致混乱的错误。帮助缓解这种情况的一种方法是(如果可能)在您自己的代码中为分配和释放函数使用自定义名称。这样就更容易确保调用的是正确的。您可以为自己的 malloc 函数甚至原始 stdlib malloc 函数定义各种自定义名称。
例如,您可以使用 mallocPlaceHolder 作为代码中的实际名称:
那么您的定义将看起来更像这样:
如果实际不存在 mallocPlaceHolder 形式的函数(以及关联的 free),则可以避免混合不同的库。
If you have control of the source code that is to use this library, here is one possibility. Use different function names: Rather than malloc, for example, call it newCoolMalloc. This method is sometimes simpler and doesn't depend on special linker options.
Then in your code, use
#define
to cause the code to call the desired set of functions. You can #define malloc to be something different. For example:If you do that, though, you have to be very very careful to include that consistently. Otherwise you run the risk of using stdlib malloc in one place and then your own free in another leading to messy bugs. One way to help mitigate that situation is to (if possible) in your own code use custom names for the allocation and free functions. Then it is easier to ensure that the correct one is being called. You can define the various custom names to your own malloc functions or even the original stdlib malloc functions.
For example, you might use mallocPlaceHolder as the actual name in the code:
Then your defines would look more like this:
If no function of the form mallocPlaceHolder (and associated free) actually exist, it avoids mixing different libraries.