Linux中程序如何调用printf等C库函数?

cumirror 2011-11-09 04:12:34

#include <stdio.h>
int main(int argc, char** argv)
{
printf("hello world\nprintf:%x", printf);
return 0;
}

反汇编后,大致如下:

08048320 <printf@plt>:
8048320: ff 25 08 a0 04 08 jmp *0x804a008
8048326: 68 10 00 00 00 push $0x10
804832b: e9 c0 ff ff ff jmp 80482f0 <_init+0x30>

int main(int argc, char** argv)
{
80483e4: 55 push %ebp
80483e5: 89 e5 mov %esp,%ebp
80483e7: 83 e4 f0 and $0xfffffff0,%esp
80483ea: 83 ec 10 sub $0x10,%esp
printf("hello world\nprintf:%x", printf);
80483ed: b8 d0 84 04 08 mov $0x80484d0,%eax
80483f2: c7 44 24 04 20 83 04 movl $0x8048320,0x4(%esp)
80483f9: 08
80483fa: 89 04 24 mov %eax,(%esp)
80483fd: e8 1e ff ff ff call 8048320 <printf@plt>
return 0;
8048402: b8 00 00 00 00 mov $0x0,%eax
8048407: c9 leave
8048408: c3 ret
8048409: 90 nop
...


这里我不太理解的是:
08048320 <printf@plt>:
8048320: ff 25 08 a0 04 08 jmp *0x804a008
8048326: 68 10 00 00 00 push $0x10
804832b: e9 c0 ff ff ff jmp 80482f0 <_init+0x30>

谁能详细解释下吗。

另外程序被加载时,load程序如何链接libc库中的printf函数呢,通过上面的汇编能反映出来不?
...全文
314 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cumirror 2011-11-09
  • 打赏
  • 举报
回复
初次接受plt、got等概念,对link and load不熟悉,我找到了下面这个资料,介绍得比较仔细:
http://blog.chinaunix.net/space.php?uid=21423035&do=blog&id=1826559
zmmkele 2011-11-09
  • 打赏
  • 举报
回复
<print@plt>中的plt是一个函数跳转表,跳到全局偏移表got中。在got中有print函数的绝对地址,该绝对地址是动态链接时候解析获得的。
上面汇编反映不出链接过程,需要看源代码。
cumirror 2011-11-09
  • 打赏
  • 举报
回复
printf函数是在加载时动态链接libc.so库进行调用的,为何编译后(加载前)就能确定它的调用地址是08048320?
另外我写了一些别的函数如malloc进行测试,都是形如:

080483c0 <memcpy@plt>:
80483c0: ff 25 0c a0 04 08 jmp *0x804a00c
80483c6: 68 18 00 00 00 push $0x18
80483cb: e9 b0 ff ff ff jmp 8048380 <_init+0x30>

080483d0 <strlen@plt>:
80483d0: ff 25 10 a0 04 08 jmp *0x804a010
80483d6: 68 20 00 00 00 push $0x20
80483db: e9 a0 ff ff ff jmp 8048380 <_init+0x30>

080483e0 <printf@plt>:
80483e0: ff 25 14 a0 04 08 jmp *0x804a014
80483e6: 68 28 00 00 00 push $0x28
80483eb: e9 90 ff ff ff jmp 8048380 <_init+0x30>

080483f0 <malloc@plt>:
80483f0: ff 25 18 a0 04 08 jmp *0x804a018
80483f6: 68 30 00 00 00 push $0x30
80483fb: e9 80 ff ff ff jmp 8048380 <_init+0x30>


自己不是很明白,希望大家能指点下。

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧