免费注册 查看新帖 |

Chinaunix

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

获取ESP地址的疑惑 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-23 12:22 |只看该作者 |倒序浏览
编个程序获取ESP地址并打印,但是用GDB调试的结果却和单独执行程序的结果不一样。请问这是为什么,测试程序如下:

  1. long get_esp()
  2. {
  3.    __asm__("movl %esp,%eax");
  4. }

  5. main()
  6. {
  7.         long esp;
  8.         esp = get_esp();
  9.         printf("%x\n",esp);


  10. }
  11. (gdb) disassemble main
  12. Dump of assembler code for function main:
  13. 0x080483ab <main+0>:    lea    0x4(%esp),%ecx
  14. 0x080483af <main+4>:    and    $0xfffffff0,%esp
  15. 0x080483b2 <main+7>:    pushl  0xfffffffc(%ecx)
  16. 0x080483b5 <main+10>:   push   %ebp
  17. 0x080483b6 <main+11>:   mov    %esp,%ebp
  18. 0x080483b8 <main+13>:   push   %ecx
  19. 0x080483b9 <main+14>:   sub    $0x24,%esp
  20. 0x080483bc <main+17>:   call   0x80483a4 <get_esp>
  21. 0x080483c1 <main+22>:   mov    %eax,0xfffffff8(%ebp)
  22. 0x080483c4 <main+25>:   mov    0xfffffff8(%ebp),%eax
  23. 0x080483c7 <main+28>:   mov    %eax,0x4(%esp)
  24. 0x080483cb <main+32>:   movl   $0x80484b0,(%esp)
  25. 0x080483d2 <main+39>:   call   0x80482b8 <printf@plt>
  26. 0x080483d7 <main+44>:   add    $0x24,%esp
  27. 0x080483da <main+47>:   pop    %ecx
  28. 0x080483db <main+48>:   pop    %ebp
  29. 0x080483dc <main+49>:   lea    0xfffffffc(%ecx),%esp
  30. 0x080483df <main+52>:   ret
  31. End of assembler dump.
  32. (gdb) r
  33. Starting program: /kernelwork/bats-linux/overflow/muyin/testesp
  34. bfffe358

  35. Program exited with code 011.
复制代码


  1. [root@localhost muyin]# ./testesp
  2. bfffe3a8
复制代码


谢谢~~~

[ 本帖最后由 ruger 于 2008-8-23 16:30 编辑 ]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-08-03 06:20:00
2 [报告]
发表于 2008-08-23 13:55 |只看该作者
每个程序分配情况不一样吧 栈底总在小于0xc0000000 的地方吧

论坛徽章:
0
3 [报告]
发表于 2008-08-23 14:28 |只看该作者
原帖由 317316abcd 于 2008-8-23 13:55 发表
每个程序分配情况不一样吧 栈底总在小于0xc0000000 的地方吧

同一个程序,在同一个终端中运行结果都是一样的,但是在GDB调试的时候执行结果是不同的,你的意思是gdb对程序的地址产生了一定的影响?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2008-08-23 14:39 |只看该作者
这样看栈指针恐怕不行吧。

论坛徽章:
0
5 [报告]
发表于 2008-08-23 15:10 |只看该作者
原帖由 cjaizss 于 2008-8-23 14:39 发表
这样看栈指针恐怕不行吧。

版主教我~~~

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
6 [报告]
发表于 2008-08-23 15:11 |只看该作者
gcc -S 之后对着汇编码自己琢磨琢磨,呵呵
你的get_esp不过是个普通函数,而且是外部的函数,那么别的函数调用这个函数的时候,你觉得会和调用别的函数有什么区别吗?

[ 本帖最后由 cjaizss 于 2008-8-23 15:13 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-08-23 16:20 |只看该作者

回复 #6 cjaizss 的帖子


  1. [root@localhost muyin]# gcc -S testesp.c -o testesp.S
  2. testesp.c: 在函数 ‘main’ 中:
  3. testesp.c:10: 警告:隐式声明与内建函数 ‘printf’ 不兼容
  4. [root@localhost muyin]# cat testesp.S
  5.         .file   "testesp.c"
  6.         .text
  7. .globl get_esp
  8.         .type   get_esp, @function
  9. get_esp:
  10.         pushl   %ebp
  11.         movl    %esp, %ebp
  12. #APP
  13.         movl %esp,%eax
  14. #NO_APP
  15.         popl    %ebp
  16.         ret
  17.         .size   get_esp, .-get_esp
  18.         .section        .rodata
  19. .LC0:
  20.         .string "%x\n"
  21.         .text
  22. .globl main
  23.         .type   main, @function
  24. main:
  25.         leal    4(%esp), %ecx
  26.         andl    $-16, %esp
  27.         pushl   -4(%ecx)
  28.         pushl   %ebp
  29.         movl    %esp, %ebp
  30.         pushl   %ecx
  31.         subl    $36, %esp
  32.         call    get_esp
  33.         movl    %eax, -8(%ebp)
  34.         movl    -8(%ebp), %eax
  35.         movl    %eax, 4(%esp)
  36.         movl    $.LC0, (%esp)
  37.         call    printf
  38.         addl    $36, %esp
  39.         popl    %ecx
  40.         popl    %ebp
  41.         leal    -4(%ecx), %esp
  42.         ret
  43.         .size   main, .-main
  44.         .ident  "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-27)"
  45.         .section        .note.GNU-stack,"",@progbits
复制代码

我查了一下#APP和#NOAPP啥意思,看样子是没有多大关系的:

The “#APP” and “#NO_APP” parts are instructions to the assembler that briefly put it into
normal operating mode, as opposed to the special high-speed “compiler output” mode that
turns off every feature that the compiler doesn’t use as well as a lot of error-checking. For
our purposes, it’s convenient becuase it highlights the part of the code we’re interested in.
版主的意思是这样获取的ESP指针应该是实际想要获得的减少了4?因为get_sp()是在将EBP压栈后的结果,因此少了4个数?但是我用gdb调试的结果确和单独执行差了0x50啊?

[ 本帖最后由 ruger 于 2008-8-23 16:36 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2008-09-05 16:31 |只看该作者
#define get_esp()  ({ int reg;  __asm__("movl %esp,%0\n\t" : "=r"(reg); reg; })
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP