LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
123
返回列表 发新帖
楼主: macarthor

C必须包含main函数吗?

[复制链接]
发表于 2004-10-8 17:19:26 | 显示全部楼层
看来 C 的编译器对于 main 函数在开始前和结束后都做了一些工作,要想只用 C 来指定特定的函数作为入口点是比较困难的。
发表于 2004-10-8 17:22:52 | 显示全部楼层
第二个程序的段错误我觉得和进程加载时的初始化有关。
以一个最简单的程序为例:

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

然后再用objdump反汇编:

  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函数之前,一个进程是要作很多初始化工作的,进程结束时也一样要做清场工作。而对第二个程序x2的objdump反汇编结果,就能看出要少很多:

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

  2. a.out:     file format elf32-i386

  3. Disassembly of section .plt:

  4. 080481b0 <.plt>:
  5. 80481b0:       ff 35 b0 92 04 08       pushl  0x80492b0
  6. 80481b6:       ff 25 b4 92 04 08       jmp    *0x80492b4
  7. 80481bc:       00 00                   add    %al,(%eax)
  8. 80481be:       00 00                   add    %al,(%eax)
  9. 80481c0:       ff 25 b8 92 04 08       jmp    *0x80492b8
  10. 80481c6:       68 00 00 00 00          push   $0x0
  11. 80481cb:       e9 e0 ff ff ff          jmp    80481b0 <x-0x30>
  12. 80481d0:       ff 25 bc 92 04 08       jmp    *0x80492bc
  13. 80481d6:       68 08 00 00 00          push   $0x8
  14. 80481db:       e9 d0 ff ff ff          jmp    80481b0 <x-0x30>
  15. Disassembly of section .text:

  16. 080481e0 <x>:
  17. 80481e0:       55                      push   %ebp
  18. 80481e1:       89 e5                   mov    %esp,%ebp
  19. 80481e3:       83 ec 08                sub    $0x8,%esp
  20. 80481e6:       83 ec 08                sub    $0x8,%esp
  21. 80481e9:       8b 45 0c                mov    0xc(%ebp),%eax
  22. 80481ec:       ff 30                   pushl  (%eax)
  23. 80481ee:       68 05 82 04 08          push   $0x8048205
  24. 80481f3:       e8 c8 ff ff ff          call   80481c0 <x-0x20>
  25. 80481f8:       83 c4 10                add    $0x10,%esp
  26. 80481fb:       83 ec 0c                sub    $0xc,%esp
  27. 80481fe:       6a 00                   push   $0x0
  28. 8048200:       e8 cb ff ff ff          call   80481d0 <x-0x10>
  29. [kj501@s2023 c]$
复制代码

具体的原因还有等待于进一步研究。
发表于 2004-10-8 17:27:43 | 显示全部楼层
比较了手工和自动的输入后,好像只要能在编译下面的程序时加入初始化和收尾的工作就可以手工指定入口点了:
  1. int user_defined_function(int argc, char *argv[])
  2. {
  3.     asm("andl $-16, %esp\n"
  4.         "movl $0, %eax\n"
  5.         "subl %eax, %esp");
  6.     /* regular codes here */
  7.     return 0;
  8. }
复制代码
还有个问题,就是如果需要第 3 个参数 char *envp[] 的话 _init 是否能够做好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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