|
发表于 2004-10-8 17:22:52
|
显示全部楼层
第二个程序的段错误我觉得和进程加载时的初始化有关。
以一个最简单的程序为例:
然后再用objdump反汇编:
- [kj501@s2023 c]$ objdump -d a.out
- a.out: file format elf32-i386
- Disassembly of section .init:
- 0804824c <_init>:
- 804824c: 55 push %ebp
- 804824d: 89 e5 mov %esp,%ebp
- 804824f: 83 ec 08 sub $0x8,%esp
- 8048252: e8 6d 00 00 00 call 80482c4 <call_gmon_start>
- 8048257: e8 d4 00 00 00 call 8048330 <frame_dummy>
- 804825c: e8 cf 01 00 00 call 8048430 <__do_global_ctors_aux>
- 8048261: c9 leave
- 8048262: c3 ret
- Disassembly of section .plt:
- 08048264 <.plt>:
- 8048264: ff 35 68 95 04 08 pushl 0x8049568
- 804826a: ff 25 6c 95 04 08 jmp *0x804956c
- 8048270: 00 00 add %al,(%eax)
- 8048272: 00 00 add %al,(%eax)
- 8048274: ff 25 70 95 04 08 jmp *0x8049570
- 804827a: 68 00 00 00 00 push $0x0
- 804827f: e9 e0 ff ff ff jmp 8048264 <_init+0x18>
- 8048284: ff 25 74 95 04 08 jmp *0x8049574
- 804828a: 68 08 00 00 00 push $0x8
- 804828f: e9 d0 ff ff ff jmp 8048264 <_init+0x18>
- Disassembly of section .text:
- 080482a0 <_start>:
- 80482a0: 31 ed xor %ebp,%ebp
- 80482a2: 5e pop %esi
- 80482a3: 89 e1 mov %esp,%ecx
- 80482a5: 83 e4 f0 and $0xfffffff0,%esp
- 80482a8: 50 push %eax
- 80482a9: 54 push %esp
- 80482aa: 52 push %edx
- 80482ab: 68 d0 83 04 08 push $0x80483d0
- 80482b0: 68 80 83 04 08 push $0x8048380
- 80482b5: 51 push %ecx
- 80482b6: 56 push %esi
- 80482b7: 68 5c 83 04 08 push $0x804835c
- 80482bc: e8 b3 ff ff ff call 8048274 <_init+0x28>
- 80482c1: f4 hlt
- 80482c2: 90 nop
- 80482c3: 90 nop
- 080482c4 <call_gmon_start>:
- 80482c4: 55 push %ebp
- 80482c5: 89 e5 mov %esp,%ebp
- 80482c7: 53 push %ebx
- 80482c8: e8 00 00 00 00 call 80482cd <call_gmon_start+0x9>
- 80482cd: 5b pop %ebx
- 80482ce: 81 c3 97 12 00 00 add $0x1297,%ebx
- 80482d4: 52 push %edx
- 80482d5: 8b 83 14 00 00 00 mov 0x14(%ebx),%eax
- 80482db: 85 c0 test %eax,%eax
- 80482dd: 74 02 je 80482e1 <call_gmon_start+0x1d>
- 80482df: ff d0 call *%eax
- 80482e1: 58 pop %eax
- 80482e2: 5b pop %ebx
- 80482e3: c9 leave
- 80482e4: c3 ret
- 80482e5: 90 nop
- 80482e6: 90 nop
- 80482e7: 90 nop
- 80482e8: 90 nop
- 80482e9: 90 nop
- 80482ea: 90 nop
- 80482eb: 90 nop
- 80482ec: 90 nop
- 80482ed: 90 nop
- 80482ee: 90 nop
- 80482ef: 90 nop
- 080482f0 <__do_global_dtors_aux>:
- 80482f0: 55 push %ebp
- 80482f1: 89 e5 mov %esp,%ebp
- 80482f3: 50 push %eax
- 80482f4: 50 push %eax
- 80482f5: 80 3d 7c 95 04 08 00 cmpb $0x0,0x804957c
- 80482fc: 75 2e jne 804832c <__do_global_dtors_aux+0x3c>
- 80482fe: a1 84 94 04 08 mov 0x8049484,%eax
- 8048303: 8b 10 mov (%eax),%edx
- 8048305: 85 d2 test %edx,%edx
- 8048307: 74 1c je 8048325 <__do_global_dtors_aux+0x35>
- 8048309: 8d b4 26 00 00 00 00 lea 0x0(%esi,1),%esi
- 8048310: 83 c0 04 add $0x4,%eax
- 8048313: a3 84 94 04 08 mov %eax,0x8049484
- 8048318: ff d2 call *%edx
- 804831a: a1 84 94 04 08 mov 0x8049484,%eax
- 804831f: 8b 10 mov (%eax),%edx
- 8048321: 85 d2 test %edx,%edx
- 8048323: 75 eb jne 8048310 <__do_global_dtors_aux+0x20>
- 8048325: c6 05 7c 95 04 08 01 movb $0x1,0x804957c
- 804832c: c9 leave
- 804832d: c3 ret
- 804832e: 89 f6 mov %esi,%esi
- 08048330 <frame_dummy>:
- 8048330: 55 push %ebp
- 8048331: 89 e5 mov %esp,%ebp
- 8048333: 51 push %ecx
- 8048334: 51 push %ecx
- 8048335: 8b 15 60 95 04 08 mov 0x8049560,%edx
- 804833b: 85 d2 test %edx,%edx
- 804833d: 74 19 je 8048358 <frame_dummy+0x28>
- 804833f: b8 00 00 00 00 mov $0x0,%eax
- 8048344: 85 c0 test %eax,%eax
- 8048346: 74 10 je 8048358 <frame_dummy+0x28>
- 8048348: 83 ec 0c sub $0xc,%esp
- 804834b: 68 60 95 04 08 push $0x8049560
- 8048350: e8 ab 7c fb f7 call 0 <_init-0x804824c>
- 8048355: 83 c4 10 add $0x10,%esp
- 8048358: c9 leave
- 8048359: c3 ret
- 804835a: 90 nop
- 804835b: 90 nop
- 0804835c <main>:
- 804835c: 55 push %ebp
- 804835d: 89 e5 mov %esp,%ebp
- 804835f: 83 ec 08 sub $0x8,%esp
- 8048362: 83 e4 f0 and $0xfffffff0,%esp
- 8048365: b8 00 00 00 00 mov $0x0,%eax
- 804836a: 29 c4 sub %eax,%esp
- 804836c: 83 ec 0c sub $0xc,%esp
- 804836f: 6a 00 push $0x0
- 8048371: e8 0e ff ff ff call 8048284 <_init+0x38>
- 8048376: 90 nop
- 8048377: 90 nop
- 8048378: 90 nop
- 8048379: 90 nop
- 804837a: 90 nop
- 804837b: 90 nop
- 804837c: 90 nop
- 804837d: 90 nop
- 804837e: 90 nop
- 804837f: 90 nop
- 08048380 <__libc_csu_init>:
- 8048380: 55 push %ebp
- 8048381: 89 e5 mov %esp,%ebp
- 8048383: 57 push %edi
- 8048384: 56 push %esi
- 8048385: 31 f6 xor %esi,%esi
- 8048387: 53 push %ebx
- 8048388: e8 00 00 00 00 call 804838d <__libc_csu_init+0xd>
- 804838d: 5b pop %ebx
- 804838e: 81 c3 d7 11 00 00 add $0x11d7,%ebx
- 8048394: 83 ec 0c sub $0xc,%esp
- 8048397: e8 b0 fe ff ff call 804824c <_init>
- 804839c: 8d 83 18 ff ff ff lea 0xffffff18(%ebx),%eax
- 80483a2: 8d 93 18 ff ff ff lea 0xffffff18(%ebx),%edx
- 80483a8: 29 c2 sub %eax,%edx
- 80483aa: c1 fa 02 sar $0x2,%edx
- 80483ad: 39 d6 cmp %edx,%esi
- 80483af: 73 10 jae 80483c1 <__libc_csu_init+0x41>
- 80483b1: 89 45 f0 mov %eax,0xfffffff0(%ebp)
- 80483b4: 89 d7 mov %edx,%edi
- 80483b6: ff 14 b0 call *(%eax,%esi,4)
- 80483b9: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
- 80483bc: 46 inc %esi
- 80483bd: 39 fe cmp %edi,%esi
- 80483bf: 72 f5 jb 80483b6 <__libc_csu_init+0x36>
- 80483c1: 83 c4 0c add $0xc,%esp
- 80483c4: 5b pop %ebx
- 80483c5: 5e pop %esi
- 80483c6: 5f pop %edi
- 80483c7: 5d pop %ebp
- 80483c8: c3 ret
- 80483c9: 8d b4 26 00 00 00 00 lea 0x0(%esi,1),%esi
- 080483d0 <__libc_csu_fini>:
- 80483d0: 55 push %ebp
- 80483d1: 89 e5 mov %esp,%ebp
- 80483d3: 57 push %edi
- 80483d4: 56 push %esi
- 80483d5: 53 push %ebx
- 80483d6: e8 00 00 00 00 call 80483db <__libc_csu_fini+0xb>
- 80483db: 5b pop %ebx
- 80483dc: 81 c3 89 11 00 00 add $0x1189,%ebx
- 80483e2: 83 ec 0c sub $0xc,%esp
- 80483e5: 8d 83 18 ff ff ff lea 0xffffff18(%ebx),%eax
- 80483eb: 8d bb 18 ff ff ff lea 0xffffff18(%ebx),%edi
- 80483f1: 29 f8 sub %edi,%eax
- 80483f3: c1 f8 02 sar $0x2,%eax
- 80483f6: 85 c0 test %eax,%eax
- 80483f8: 8d 70 ff lea 0xffffffff(%eax),%esi
- 80483fb: 75 13 jne 8048410 <__libc_csu_fini+0x40>
- 80483fd: e8 52 00 00 00 call 8048454 <_fini>
- 8048402: 83 c4 0c add $0xc,%esp
- 8048405: 5b pop %ebx
- 8048406: 5e pop %esi
- 8048407: 5f pop %edi
- 8048408: 5d pop %ebp
- 8048409: c3 ret
- 804840a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
- 8048410: ff 14 b7 call *(%edi,%esi,4)
- 8048413: 89 f0 mov %esi,%eax
- 8048415: 4e dec %esi
- 8048416: 85 c0 test %eax,%eax
- 8048418: 75 f6 jne 8048410 <__libc_csu_fini+0x40>
- 804841a: e8 35 00 00 00 call 8048454 <_fini>
- 804841f: 83 c4 0c add $0xc,%esp
- 8048422: 5b pop %ebx
- 8048423: 5e pop %esi
- 8048424: 5f pop %edi
- 8048425: 5d pop %ebp
- 8048426: c3 ret
- 8048427: 90 nop
- 8048428: 90 nop
- 8048429: 90 nop
- 804842a: 90 nop
- 804842b: 90 nop
- 804842c: 90 nop
- 804842d: 90 nop
- 804842e: 90 nop
- 804842f: 90 nop
- 08048430 <__do_global_ctors_aux>:
- 8048430: 55 push %ebp
- 8048431: 89 e5 mov %esp,%ebp
- 8048433: 53 push %ebx
- 8048434: 52 push %edx
- 8048435: bb 50 95 04 08 mov $0x8049550,%ebx
- 804843a: a1 50 95 04 08 mov 0x8049550,%eax
- 804843f: 83 f8 ff cmp $0xffffffff,%eax
- 8048442: 74 0c je 8048450 <__do_global_ctors_aux+0x20>
- 8048444: 83 eb 04 sub $0x4,%ebx
- 8048447: ff d0 call *%eax
- 8048449: 8b 03 mov (%ebx),%eax
- 804844b: 83 f8 ff cmp $0xffffffff,%eax
- 804844e: 75 f4 jne 8048444 <__do_global_ctors_aux+0x14>
- 8048450: 58 pop %eax
- 8048451: 5b pop %ebx
- 8048452: 5d pop %ebp
- 8048453: c3 ret
- Disassembly of section .fini:
- 08048454 <_fini>:
- 8048454: 55 push %ebp
- 8048455: 89 e5 mov %esp,%ebp
- 8048457: 53 push %ebx
- 8048458: e8 00 00 00 00 call 804845d <_fini+0x9>
- 804845d: 5b pop %ebx
- 804845e: 81 c3 07 11 00 00 add $0x1107,%ebx
- 8048464: 50 push %eax
- 8048465: e8 86 fe ff ff call 80482f0 <__do_global_dtors_aux>
- 804846a: 59 pop %ecx
- 804846b: 5b pop %ebx
- 804846c: c9 leave
- 804846d: c3 ret
- [kj501@s2023 c]$
复制代码
可以看到在main函数之前,一个进程是要作很多初始化工作的,进程结束时也一样要做清场工作。而对第二个程序x2的objdump反汇编结果,就能看出要少很多:
- [kj501@s2023 c]$ objdump -d a.out
- a.out: file format elf32-i386
- Disassembly of section .plt:
- 080481b0 <.plt>:
- 80481b0: ff 35 b0 92 04 08 pushl 0x80492b0
- 80481b6: ff 25 b4 92 04 08 jmp *0x80492b4
- 80481bc: 00 00 add %al,(%eax)
- 80481be: 00 00 add %al,(%eax)
- 80481c0: ff 25 b8 92 04 08 jmp *0x80492b8
- 80481c6: 68 00 00 00 00 push $0x0
- 80481cb: e9 e0 ff ff ff jmp 80481b0 <x-0x30>
- 80481d0: ff 25 bc 92 04 08 jmp *0x80492bc
- 80481d6: 68 08 00 00 00 push $0x8
- 80481db: e9 d0 ff ff ff jmp 80481b0 <x-0x30>
- Disassembly of section .text:
- 080481e0 <x>:
- 80481e0: 55 push %ebp
- 80481e1: 89 e5 mov %esp,%ebp
- 80481e3: 83 ec 08 sub $0x8,%esp
- 80481e6: 83 ec 08 sub $0x8,%esp
- 80481e9: 8b 45 0c mov 0xc(%ebp),%eax
- 80481ec: ff 30 pushl (%eax)
- 80481ee: 68 05 82 04 08 push $0x8048205
- 80481f3: e8 c8 ff ff ff call 80481c0 <x-0x20>
- 80481f8: 83 c4 10 add $0x10,%esp
- 80481fb: 83 ec 0c sub $0xc,%esp
- 80481fe: 6a 00 push $0x0
- 8048200: e8 cb ff ff ff call 80481d0 <x-0x10>
- [kj501@s2023 c]$
复制代码
具体的原因还有等待于进一步研究。 |
|