LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 621|回复: 9

又一个programming from ground up中出现的一个问题

[复制链接]
发表于 2004-10-9 11:01:37 | 显示全部楼层 |阅读模式
programming from ground up 这本书的第四章上讲
传给一个程序的参数个数放在(%ebp)
第一个参数在4(%ebp)
第一个参数就是程序本身.
但是经过我调试 并不是这样~~

希望斑竹和各位大侠 多多关照
发表于 2004-10-9 11:05:57 | 显示全部楼层
书上说的是C语言的函数参数传递约定,如果是自己写程序,完成可以自己随意决定次序。
不过我觉得你的操作可以有误,能不能详细讲述一下你是如何做的?
 楼主| 发表于 2004-10-9 16:34:47 | 显示全部楼层
sorry 是我没有说清楚~
这样:我是说system传给main的参数而不是自己定义的函数~
我用gdb调试后.
发现参数个数变量的地址在4(%ebp)
而指针数组的头地址在8(%ebp)
而书上说的不符和!
我相知到是不是因为16位 和 32 位的原因!!!

我用的是
讯驰 1.6

同时 感谢 斑斑
 楼主| 发表于 2004-10-9 22:41:01 | 显示全部楼层
顶一下自己
 楼主| 发表于 2004-10-9 22:58:36 | 显示全部楼层
有看过的这本书的马?
看过的来讨论一下阿~
发表于 2004-10-10 17:27:49 | 显示全部楼层
最初由 nuclearweapon 发表
sorry 是我没有说清楚~
这样:我是说system传给main的参数而不是自己定义的函数~
我用gdb调试后.
发现参数个数变量的地址在4(%ebp)
而指针数组的头地址在8(%ebp)
而书上说的不符和!
我相知到是不是因为16位 和 32 位的原因!!!

我用的是
讯驰 1.6

同时 感谢 斑斑

书上的第151页有一张图,你自己先好好看看。
btw: 斑斑是什么意思?
 楼主| 发表于 2004-10-10 22:56:37 | 显示全部楼层
我就问问斑竹,你作实验了马?
没作实验就不要随便说~
书我是看了~~
发表于 2004-10-11 16:09:25 | 显示全部楼层
书看了,不等于就理解了。书上第151页的那张图实际上就是程序运行之前的内存布局。如果它说得不够明白,我可以在这里给你解释一下。在堆栈顶部是系统传递给进程的命令行参数个数,然后往上依次是第一个命令行参数(实际上就是程序的名字),第二个参数。。。直到最后一个参数。再往上就是系统传递给进程的环境变量。
如果系统要象正常的函数调用那样去调用程序的入口函数(通常情况下是main函数),那就应该在堆栈的顶部有一个返回地址。可是事实上进程的启动执行和退出都是由操作系统来处理的,不采用这种函数调用的方式,自然没有这个ret返回地址了。所以你看到参数个数变量的地址在4(%ebp)而不是在8(%ebp),一点也不奇怪呀。这和系统是16位还是32位也毫无关系。
发表于 2004-10-12 09:42:04 | 显示全部楼层
不好意思,今天翻了一下书,发现我原来的理解是不正确的,一个进程的加载过程应该是这样的:
当用户在命令行输入命令执行可执行程序之后,shell调用操作系统中一个叫加载器(loader)的函数,拷贝可执行文件中的代码和数据到内存,然后将控制转移到这个程序的开头。
当加载器运行时,它创建如书上第151页的那张图所示的内存布局。然后根据在可执行文件中段头表的指示,将可执行文件的相关内容复制到代码段和数据段。接下来,加载器跳转到程序的入口点,这个入口点就是符号_start的地址,对可执行文件用objdump -d可以看到这一部分反汇编的内容。
以一个最简单的程序为例:

  1. int main()
  2. {
  3.         exit(0);
  4. }
复制代码

用gcc temp.c编译后,再用objdump -d a.out反汇编得到以下结果:

  1. [kj501@s2023 c]$ objdump -d a.out

  2. a.out:     file format elf32-i386

  3. Disassembly of section .init:

  4. 0804824c <_init>:
  5. 804824c:       55                      push   %ebp
  6. 804824d:       89 e5                   mov    %esp,%ebp
  7. 804824f:       83 ec 08                sub    $0x8,%esp
  8. 8048252:       e8 6d 00 00 00          call   80482c4 <call_gmon_start>
  9. 8048257:       e8 d4 00 00 00          call   8048330 <frame_dummy>
  10. 804825c:       e8 cf 01 00 00          call   8048430 <__do_global_ctors_aux>
  11. 8048261:       c9                      leave
  12. 8048262:       c3                      ret
  13. Disassembly of section .plt:

  14. 08048264 <.plt>:
  15. 8048264:       ff 35 68 95 04 08       pushl  0x8049568
  16. 804826a:       ff 25 6c 95 04 08       jmp    *0x804956c
  17. 8048270:       00 00                   add    %al,(%eax)
  18. 8048272:       00 00                   add    %al,(%eax)
  19. 8048274:       ff 25 70 95 04 08       jmp    *0x8049570
  20. 804827a:       68 00 00 00 00          push   $0x0
  21. 804827f:       e9 e0 ff ff ff          jmp    8048264 <_init+0x18>
  22. 8048284:       ff 25 74 95 04 08       jmp    *0x8049574
  23. 804828a:       68 08 00 00 00          push   $0x8
  24. 804828f:       e9 d0 ff ff ff          jmp    8048264 <_init+0x18>
  25. Disassembly of section .text:

  26. 080482a0 <_start>:
  27. 80482a0:       31 ed                   xor    %ebp,%ebp
  28. 80482a2:       5e                      pop    %esi
  29. 80482a3:       89 e1                   mov    %esp,%ecx
  30. 80482a5:       83 e4 f0                and    $0xfffffff0,%esp
  31. 80482a8:       50                      push   %eax
  32. 80482a9:       54                      push   %esp
  33. 80482aa:       52                      push   %edx
  34. 80482ab:       68 d0 83 04 08          push   $0x80483d0
  35. 80482b0:       68 80 83 04 08          push   $0x8048380
  36. 80482b5:       51                      push   %ecx
  37. 80482b6:       56                      push   %esi
  38. 80482b7:       68 5c 83 04 08          push   $0x804835c
  39. 80482bc:       e8 b3 ff ff ff          call   8048274 <_init+0x28>
  40. 80482c1:       f4                      hlt
  41. 80482c2:       90                      nop
  42. 80482c3:       90                      nop

  43. 080482c4 <call_gmon_start>:
  44. 80482c4:       55                      push   %ebp
  45. 80482c5:       89 e5                   mov    %esp,%ebp
  46. 80482c7:       53                      push   %ebx
  47. 80482c8:       e8 00 00 00 00          call   80482cd <call_gmon_start+0x9>
  48. 80482cd:       5b                      pop    %ebx
  49. 80482ce:       81 c3 97 12 00 00       add    $0x1297,%ebx
  50. 80482d4:       52                      push   %edx
  51. 80482d5:       8b 83 14 00 00 00       mov    0x14(%ebx),%eax
  52. 80482db:       85 c0                   test   %eax,%eax
  53. 80482dd:       74 02                   je     80482e1 <call_gmon_start+0x1d>
  54. 80482df:       ff d0                   call   *%eax
  55. 80482e1:       58                      pop    %eax
  56. 80482e2:       5b                      pop    %ebx
  57. 80482e3:       c9                      leave
  58. 80482e4:       c3                      ret
  59. 80482e5:       90                      nop
  60. 80482e6:       90                      nop
  61. 80482e7:       90                      nop
  62. 80482e8:       90                      nop
  63. 80482e9:       90                      nop
  64. 80482ea:       90                      nop
  65. 80482eb:       90                      nop
  66. 80482ec:       90                      nop
  67. 80482ed:       90                      nop
  68. 80482ee:       90                      nop
  69. 80482ef:       90                      nop

  70. 080482f0 <__do_global_dtors_aux>:
  71. 80482f0:       55                      push   %ebp
  72. 80482f1:       89 e5                   mov    %esp,%ebp
  73. 80482f3:       50                      push   %eax
  74. 80482f4:       50                      push   %eax
  75. 80482f5:       80 3d 7c 95 04 08 00    cmpb   $0x0,0x804957c
  76. 80482fc:       75 2e                   jne    804832c <__do_global_dtors_aux+0x3c>
  77. 80482fe:       a1 84 94 04 08          mov    0x8049484,%eax
  78. 8048303:       8b 10                   mov    (%eax),%edx
  79. 8048305:       85 d2                   test   %edx,%edx
  80. 8048307:       74 1c                   je     8048325 <__do_global_dtors_aux+0x35>
  81. 8048309:       8d b4 26 00 00 00 00    lea    0x0(%esi,1),%esi
  82. 8048310:       83 c0 04                add    $0x4,%eax
  83. 8048313:       a3 84 94 04 08          mov    %eax,0x8049484
  84. 8048318:       ff d2                   call   *%edx
  85. 804831a:       a1 84 94 04 08          mov    0x8049484,%eax
  86. 804831f:       8b 10                   mov    (%eax),%edx
  87. 8048321:       85 d2                   test   %edx,%edx
  88. 8048323:       75 eb                   jne    8048310 <__do_global_dtors_aux+0x20>
  89. 8048325:       c6 05 7c 95 04 08 01    movb   $0x1,0x804957c
  90. 804832c:       c9                      leave
  91. 804832d:       c3                      ret
  92. 804832e:       89 f6                   mov    %esi,%esi

  93. 08048330 <frame_dummy>:
  94. 8048330:       55                      push   %ebp
  95. 8048331:       89 e5                   mov    %esp,%ebp
  96. 8048333:       51                      push   %ecx
  97. 8048334:       51                      push   %ecx
  98. 8048335:       8b 15 60 95 04 08       mov    0x8049560,%edx
  99. 804833b:       85 d2                   test   %edx,%edx
  100. 804833d:       74 19                   je     8048358 <frame_dummy+0x28>
  101. 804833f:       b8 00 00 00 00          mov    $0x0,%eax
  102. 8048344:       85 c0                   test   %eax,%eax
  103. 8048346:       74 10                   je     8048358 <frame_dummy+0x28>
  104. 8048348:       83 ec 0c                sub    $0xc,%esp
  105. 804834b:       68 60 95 04 08          push   $0x8049560
  106. 8048350:       e8 ab 7c fb f7          call   0 <_init-0x804824c>
  107. 8048355:       83 c4 10                add    $0x10,%esp
  108. 8048358:       c9                      leave
  109. 8048359:       c3                      ret
  110. 804835a:       90                      nop
  111. 804835b:       90                      nop

  112. 0804835c <main>:
  113. 804835c:       55                      push   %ebp
  114. 804835d:       89 e5                   mov    %esp,%ebp
  115. 804835f:       83 ec 08                sub    $0x8,%esp
  116. 8048362:       83 e4 f0                and    $0xfffffff0,%esp
  117. 8048365:       b8 00 00 00 00          mov    $0x0,%eax
  118. 804836a:       29 c4                   sub    %eax,%esp
  119. 804836c:       83 ec 0c                sub    $0xc,%esp
  120. 804836f:       6a 00                   push   $0x0
  121. 8048371:       e8 0e ff ff ff          call   8048284 <_init+0x38>
  122. 8048376:       90                      nop
  123. 8048377:       90                      nop
  124. 8048378:       90                      nop
  125. 8048379:       90                      nop
  126. 804837a:       90                      nop
  127. 804837b:       90                      nop
  128. 804837c:       90                      nop
  129. 804837d:       90                      nop
  130. 804837e:       90                      nop
  131. 804837f:       90                      nop

  132. 08048380 <__libc_csu_init>:
  133. 8048380:       55                      push   %ebp
  134. 8048381:       89 e5                   mov    %esp,%ebp
  135. 8048383:       57                      push   %edi
  136. 8048384:       56                      push   %esi
  137. 8048385:       31 f6                   xor    %esi,%esi
  138. 8048387:       53                      push   %ebx
  139. 8048388:       e8 00 00 00 00          call   804838d <__libc_csu_init+0xd>
  140. 804838d:       5b                      pop    %ebx
  141. 804838e:       81 c3 d7 11 00 00       add    $0x11d7,%ebx
  142. 8048394:       83 ec 0c                sub    $0xc,%esp
  143. 8048397:       e8 b0 fe ff ff          call   804824c <_init>
  144. 804839c:       8d 83 18 ff ff ff       lea    0xffffff18(%ebx),%eax
  145. 80483a2:       8d 93 18 ff ff ff       lea    0xffffff18(%ebx),%edx
  146. 80483a8:       29 c2                   sub    %eax,%edx
  147. 80483aa:       c1 fa 02                sar    $0x2,%edx
  148. 80483ad:       39 d6                   cmp    %edx,%esi
  149. 80483af:       73 10                   jae    80483c1 <__libc_csu_init+0x41>
  150. 80483b1:       89 45 f0                mov    %eax,0xfffffff0(%ebp)
  151. 80483b4:       89 d7                   mov    %edx,%edi
  152. 80483b6:       ff 14 b0                call   *(%eax,%esi,4)
  153. 80483b9:       8b 45 f0                mov    0xfffffff0(%ebp),%eax
  154. 80483bc:       46                      inc    %esi
  155. 80483bd:       39 fe                   cmp    %edi,%esi
  156. 80483bf:       72 f5                   jb     80483b6 <__libc_csu_init+0x36>
  157. 80483c1:       83 c4 0c                add    $0xc,%esp
  158. 80483c4:       5b                      pop    %ebx
  159. 80483c5:       5e                      pop    %esi
  160. 80483c6:       5f                      pop    %edi
  161. 80483c7:       5d                      pop    %ebp
  162. 80483c8:       c3                      ret
  163. 80483c9:       8d b4 26 00 00 00 00    lea    0x0(%esi,1),%esi

  164. 080483d0 <__libc_csu_fini>:
  165. 80483d0:       55                      push   %ebp
  166. 80483d1:       89 e5                   mov    %esp,%ebp
  167. 80483d3:       57                      push   %edi
  168. 80483d4:       56                      push   %esi
  169. 80483d5:       53                      push   %ebx
  170. 80483d6:       e8 00 00 00 00          call   80483db <__libc_csu_fini+0xb>
  171. 80483db:       5b                      pop    %ebx
  172. 80483dc:       81 c3 89 11 00 00       add    $0x1189,%ebx
  173. 80483e2:       83 ec 0c                sub    $0xc,%esp
  174. 80483e5:       8d 83 18 ff ff ff       lea    0xffffff18(%ebx),%eax
  175. 80483eb:       8d bb 18 ff ff ff       lea    0xffffff18(%ebx),%edi
  176. 80483f1:       29 f8                   sub    %edi,%eax
  177. 80483f3:       c1 f8 02                sar    $0x2,%eax
  178. 80483f6:       85 c0                   test   %eax,%eax
  179. 80483f8:       8d 70 ff                lea    0xffffffff(%eax),%esi
  180. 80483fb:       75 13                   jne    8048410 <__libc_csu_fini+0x40>
  181. 80483fd:       e8 52 00 00 00          call   8048454 <_fini>
  182. 8048402:       83 c4 0c                add    $0xc,%esp
  183. 8048405:       5b                      pop    %ebx
  184. 8048406:       5e                      pop    %esi
  185. 8048407:       5f                      pop    %edi
  186. 8048408:       5d                      pop    %ebp
  187. 8048409:       c3                      ret
  188. 804840a:       8d b6 00 00 00 00       lea    0x0(%esi),%esi
  189. 8048410:       ff 14 b7                call   *(%edi,%esi,4)
  190. 8048413:       89 f0                   mov    %esi,%eax
  191. 8048415:       4e                      dec    %esi
  192. 8048416:       85 c0                   test   %eax,%eax
  193. 8048418:       75 f6                   jne    8048410 <__libc_csu_fini+0x40>
  194. 804841a:       e8 35 00 00 00          call   8048454 <_fini>
  195. 804841f:       83 c4 0c                add    $0xc,%esp
  196. 8048422:       5b                      pop    %ebx
  197. 8048423:       5e                      pop    %esi
  198. 8048424:       5f                      pop    %edi
  199. 8048425:       5d                      pop    %ebp
  200. 8048426:       c3                      ret
  201. 8048427:       90                      nop
  202. 8048428:       90                      nop
  203. 8048429:       90                      nop
  204. 804842a:       90                      nop
  205. 804842b:       90                      nop
  206. 804842c:       90                      nop
  207. 804842d:       90                      nop
  208. 804842e:       90                      nop
  209. 804842f:       90                      nop

  210. 08048430 <__do_global_ctors_aux>:
  211. 8048430:       55                      push   %ebp
  212. 8048431:       89 e5                   mov    %esp,%ebp
  213. 8048433:       53                      push   %ebx
  214. 8048434:       52                      push   %edx
  215. 8048435:       bb 50 95 04 08          mov    $0x8049550,%ebx
  216. 804843a:       a1 50 95 04 08          mov    0x8049550,%eax
  217. 804843f:       83 f8 ff                cmp    $0xffffffff,%eax
  218. 8048442:       74 0c                   je     8048450 <__do_global_ctors_aux+0x20>
  219. 8048444:       83 eb 04                sub    $0x4,%ebx
  220. 8048447:       ff d0                   call   *%eax
  221. 8048449:       8b 03                   mov    (%ebx),%eax
  222. 804844b:       83 f8 ff                cmp    $0xffffffff,%eax
  223. 804844e:       75 f4                   jne    8048444 <__do_global_ctors_aux+0x14>
  224. 8048450:       58                      pop    %eax
  225. 8048451:       5b                      pop    %ebx
  226. 8048452:       5d                      pop    %ebp
  227. 8048453:       c3                      ret
  228. Disassembly of section .fini:

  229. 08048454 <_fini>:
  230. 8048454:       55                      push   %ebp
  231. 8048455:       89 e5                   mov    %esp,%ebp
  232. 8048457:       53                      push   %ebx
  233. 8048458:       e8 00 00 00 00          call   804845d <_fini+0x9>
  234. 804845d:       5b                      pop    %ebx
  235. 804845e:       81 c3 07 11 00 00       add    $0x1107,%ebx
  236. 8048464:       50                      push   %eax
  237. 8048465:       e8 86 fe ff ff          call   80482f0 <__do_global_dtors_aux>
  238. 804846a:       59                      pop    %ecx
  239. 804846b:       5b                      pop    %ebx
  240. 804846c:       c9                      leave
  241. 804846d:       c3                      ret
  242. [kj501@s2023 c]$
复制代码

你可以看到在进入main之前,还要执行很多初始化的代码。不过要搞清楚这些初始化代码的作用,就得去研究ELF的格式了。
 楼主| 发表于 2004-10-12 22:47:53 | 显示全部楼层
谢谢~
最近也在找点看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表