有没有办法覆盖C语言中的malloc/free函数? [英] Is there a way to overwrite the malloc/free function in C?

查看:371
本文介绍了有没有办法覆盖C语言中的malloc/free函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以挂接它自身的C应用程序中的malloc/free函数调用?

解决方案

malloc()free()在标准库中定义;链接代码时,链接器将仅在库中搜索尚未遇到的更易遇到的目标代码尚未解析的符号,并且从编译生成的目标文件总是在任何库之前被链接到.

因此,您可以简单地通过在自己的代码中定义任何库函数来覆盖任何库函数,确保其具有正确的签名(相同的名称,相同的参数编号和类型以及相同的返回类型).

Is there a way to hook the malloc/free function call from a C application it self?

解决方案

malloc() and free() are defined in the standard library; when linking code, the linker will search the library only for symbols that are not already resolved by eailier encountered object code, and object files generated from compilation are always linked before any libraries.

So you can override any library function simply by defining it in your own code, ensuring that it has the correct signature (same name, same number and types of parameters and same return type).

这篇关于有没有办法覆盖C语言中的malloc/free函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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