免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9132 | 回复: 12
打印 上一主题 下一主题

重载glibc的malloc()库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-20 16:25 |只看该作者 |倒序浏览
我想在一个C程序中重载glibc的malloc() API函数,或者实现一个hook类似的东西,在调用glibc的malloc()函数时,调用一个我自己编写的函数,这两个问题如何解决?

我考虑的一个可能是,在用ld连接我的程序时候,用我的mylib库覆盖glibc的malloc():

cc -o myprog myprog.c -lmylib

不知道可以不?

论坛徽章:
0
2 [报告]
发表于 2009-01-20 16:33 |只看该作者
这个问题容易解决,ld中有一个选项 --wrap, 专门解决这个问题
当查找某个符号时,它先解析__wrap_symbol, 解析不到才去解析symbol

--wrap symbol
Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol.
This can be used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol. If it wishes to call the system function, it should call __real_symbol.

Here is a trivial example:

          void *
          __wrap_malloc (size_t c)
          {
            printf ("malloc called with %zu\n", c);
            return __real_malloc (c);
          }
     
If you link other code with this file using --wrap malloc, then all calls to malloc will call the function __wrap_malloc instead. The call to __real_malloc in __wrap_malloc will call the real malloc function.

You may wish to provide a __real_malloc function as well, so that links without the --wrap option will succeed. If you do this, you should not put the definition of __real_malloc in the same file as __wrap_malloc; if you do, the assembler may resolve the call before the linker has a chance to wrap it to malloc.

评分

参与人数 1可用积分 +9 收起 理由
MMMIX + 9 very useful

查看全部评分

论坛徽章:
0
3 [报告]
发表于 2009-01-20 17:16 |只看该作者
如果我用cc -o myprog myprog.c -lmylib, 而不想修改缺省的ld的命令行参数或者linker脚本,不知可不可以?

[ 本帖最后由 ljshan 于 2009-1-20 17:19 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-01-20 17:31 |只看该作者
原帖由 ljshan 于 2009-1-20 17:16 发表
如果我用cc -o myprog myprog.c -lmylib, 而不想修改缺省的ld的命令行参数或者linker脚本,不知可不可以?


你自己动手测试一下不就知道了吗

论坛徽章:
0
5 [报告]
发表于 2009-01-20 18:08 |只看该作者
原帖由 zhuhefang2006 于 2009-1-20 17:31 发表


你自己动手测试一下不就知道了吗


关键是我不知道如何编写mylib呀:)什么样的mylib能替换掉glibc中的malloc呢?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2009-01-20 18:35 |只看该作者
原帖由 ljshan 于 2009-1-20 18:08 发表


关键是我不知道如何编写mylib呀:)什么样的mylib能替换掉glibc中的malloc呢?

先写个空实现看看链接会不会出错。

论坛徽章:
0
7 [报告]
发表于 2009-01-20 18:59 |只看该作者

回复 #3 ljshan 的帖子

好像没什么好办法了,你可以确保你的函数不链入C库,那malloc自然就使用你的库中的malloc了

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
8 [报告]
发表于 2009-01-20 19:07 |只看该作者
原帖由 lyl19 于 2009-1-20 18:59 发表
好像没什么好办法了,你可以确保你的函数不链入C库,那malloc自然就使用你的库中的malloc了

这样也没法用c库里的其它函数了。

论坛徽章:
0
9 [报告]
发表于 2009-01-21 16:15 |只看该作者

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
10 [报告]
发表于 2009-01-21 17:32 |只看该作者
原帖由 ljshan 于 2009-1-21 16:15 发表
找到了:
http://www.gnu.org/software/libt ... oks-for-Malloc.html

只有想不到
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP