曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 10462|回复: 0
打印 上一主题 下一主题

main 函数与 crt1.o 库

[复制链接]

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
跳转到指定楼层
楼主
发表于 2010-11-11 08:44:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
先用 http://www.groad.net/bbs/read.php?tid-1391-fpage-3.html 中的 cpuid 作为例子来尝试下面的一个编译链接:
$ as -o cpuid.o cpuid.s     # 使用 as 编译出 *.o  目标文件
$ gcc cpuid.o -o cpuid
cpuid.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

上面使用 as 编译时没有问题,但在用 gcc 链接时出错了。提示出错的原因有两个,1 是没有定义 main 函数;2 是重复定义了 _start ,且这个 _start 在 crt1.o 中已有定义。然而,如果 cpuid.o 是一个由 c 文件产生的目标文件(gcc -c cpuid.c -o cpuid.o),那么则不会出现错误。

从上面的错误提示中我们知道,_start 这个入口标号在 crt1.o 中有定义。

实际上,用 gcc 编译一个 C 文件要经过 3 个阶段,第一是生成汇编代码,第二是生成目标文件,第三是生成可执行文件,如:

gcc -S dumpstack.c   # -S 选项生成汇编代码
gcc -c dumpstack.s  # -c 选项生成目标文件
gcc dumpstack.o   # 生成可执行文件,默认为 a.out

上面第三步生成可执行文件,实际上是用 ld 进行了必要的链接。这里,用 ld 来做一个试验,先如下链接:

$ ld dumpstack.o -o dumpstack
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
dumpstack.o: In function `main':
dumpstack.c:(.text+0x26): undefined reference to `printf'
dumpstack.c:(.text+0x3e): undefined reference to `printf'
dumpstack.c:(.text+0x43): undefined reference to `environ'
dumpstack.c:(.text+0x53): undefined reference to `printf'
dumpstack.c:(.text+0x58): undefined reference to `environ'
dumpstack.c:(.text+0x6a): undefined reference to `printf'
dumpstack.c:(.text+0x7d): undefined reference to `printf'
dumpstack.c:(.text+0x90): undefined reference to `printf'
dumpstack.c:(.text+0xa3): undefined reference to `printf'
dumpstack.c:(.text+0xb6): undefined reference to `printf'
dumpstack.c:(.text+0xc2): undefined reference to `puts'
dumpstack.c:(.text+0xe0): undefined reference to `printf'
dumpstack.c:(.text+0x107): undefined reference to `printf'
dumpstack.c:(.text+0x124): undefined reference to `putchar'
dumpstack.c:(.text+0x14a): undefined reference to `puts'
dumpstack.c:(.text+0x179): undefined reference to `printf'
dumpstack.c:(.text+0x1a1): undefined reference to `printf'
dumpstack.c:(.text+0x1be): undefined reference to `printf'
dumpstack.c:(.text+0x20d): undefined reference to `putchar'
dumpstack.c:(.text+0x22a): undefined reference to `putchar'

上面输出,不但提示找不到 _start 符号还缺少了如 printf() 等基本函数。从上面知道,出错的原因实际上是在链接时一些必要的库文件没有加载进来,正确的链接方法是:

$ ld /usr/lib/crt1.o  /usr/lib/crti.o dumpstack.o -o dumpstack -lc -dynamic-linker /lib/ld-linux.so.2

上面链接时用到了 crt1.o 和 crti.o 以及 ld-linux.so.2 动态链接库。其中在 crt1.o 里会提供 _start 入口点的定义,crti.o 里会提供  _init (初始化)和 _fini (完成返回) 这些标号入口,动态链接库 ld-linux.so.2 里会提供诸如 printf(),puts() 等函数的动态链接功能。

如下面是对 dumpstack 程序的反汇编:
$ objdump -d dumpstack
dumpstack:     file format elf32-i386
Disassembly of section .init:
08048324 <_init>:   # 在 /usr/lib/crti.o 中定义
8048324:       55                      push   %ebp
8048325:       89 e5                   mov    %esp,%ebp
8048327:       53                      push   %ebx
8048328:       83 ec 04                sub    $0x4,%esp
804832b:       e8 00 00 00 00          call   8048330 <_init+0xc>
8048330:       5b                      pop    %ebx
8048331:       81 c3 e4 15 00 00       add    $0x15e4,%ebx
8048337:       8b 93 fc ff ff ff       mov    -0x4(%ebx),%edx
804833d:       85 d2                   test   %edx,%edx
804833f:       74 05                   je     8048346 <_init+0x22>
8048341:       e8 1e 00 00 00          call   8048364 <[email=__gmon_start__@plt]__gmon_start__@plt[/email]>
8048346:       e8 05 01 00 00          call   8048450 <frame_dummy>
804834b:       e8 f0 03 00 00          call   8048740 <__do_global_ctors_aux>
8048350:       58                      pop    %eax
8048351:       5b                      pop    %ebx
8048352:       c9                      leave
8048353:       c3                      ret
Disassembly of section .plt:
08048354 <[email=__gmon_start__@plt-0x10]__gmon_start__@plt-0x10[/email]>:
8048354:       ff 35 18 99 04 08       pushl  0x8049918
804835a:       ff 25 1c 99 04 08       jmp    *0x804991c
8048360:       00 00                   add    %al,(%eax)
        ...
08048364 <[email=__gmon_start__@plt]__gmon_start__@plt[/email]>:
8048364:       ff 25 20 99 04 08       jmp    *0x8049920
804836a:       68 00 00 00 00          push   $0x0
804836f:       e9 e0 ff ff ff          jmp    8048354 <_init+0x30>
08048374 <[email=putchar@plt]putchar@plt[/email]>:
8048374:       ff 25 24 99 04 08       jmp    *0x8049924
804837a:       68 08 00 00 00          push   $0x8
804837f:       e9 d0 ff ff ff          jmp    8048354 <_init+0x30>
08048384 <[email=__libc_start_main@plt]__libc_start_main@plt[/email]>:
8048384:       ff 25 28 99 04 08       jmp    *0x8049928
804838a:       68 10 00 00 00          push   $0x10
804838f:       e9 c0 ff ff ff          jmp    8048354 <_init+0x30>
08048394 <[email=printf@plt]printf@plt[/email]>:
8048394:       ff 25 2c 99 04 08       jmp    *0x804992c
804839a:       68 18 00 00 00          push   $0x18
804839f:       e9 b0 ff ff ff          jmp    8048354 <_init+0x30>
080483a4 <[email=puts@plt]puts@plt[/email]>:
80483a4:       ff 25 30 99 04 08       jmp    *0x8049930
80483aa:       68 20 00 00 00          push   $0x20
80483af:       e9 a0 ff ff ff          jmp    8048354 <_init+0x30>
Disassembly of section .text:
080483c0 <_start>:     # 在 /usr/lib/crt1.o 中有定义,程序从这里执行
80483c0:       31 ed                   xor    %ebp,%ebp
80483c2:       5e                      pop    %esi
80483c3:       89 e1                   mov    %esp,%ecx
80483c5:       83 e4 f0                and    $0xfffffff0,%esp
80483c8:       50                      push   %eax
80483c9:       54                      push   %esp
80483ca:       52                      push   %edx
80483cb:       68 d0 86 04 08          push   $0x80486d0
80483d0:       68 e0 86 04 08          push   $0x80486e0
80483d5:       51                      push   %ecx
80483d6:       56                      push   %esi
80483d7:       68 74 84 04 08          push   $0x8048474    # main 函数地址压栈
80483dc:       e8 a3 ff ff ff          call   8048384 <[email=__libc_start_main@plt]__libc_start_main@plt[/email]>
80483e1:       f4                      hlt
80483e2:       90                      nop
80483e3:       90                      nop
80483e4:       90                      nop
80483e5:       90                      nop
80483e6:       90                      nop
80483e7:       90                      nop
80483e8:       90                      nop
80483e9:       90                      nop
80483ea:       90                      nop
80483eb:       90                      nop
80483ec:       90                      nop
80483ed:       90                      nop
80483ee:       90                      nop
80483ef:       90                      nop
080483f0 <__do_global_dtors_aux>:
80483f0:       55                      push   %ebp
80483f1:       89 e5                   mov    %esp,%ebp
80483f3:       53                      push   %ebx
80483f4:       83 ec 04                sub    $0x4,%esp
80483f7:       80 3d 44 99 04 08 00    cmpb   $0x0,0x8049944
80483fe:       75 40                   jne    8048440 <__do_global_dtors_aux+0x50>
8048400:       8b 15 48 99 04 08       mov    0x8049948,%edx
8048406:       b8 38 98 04 08          mov    $0x8049838,%eax
804840b:       2d 34 98 04 08          sub    $0x8049834,%eax
8048410:       c1 f8 02                sar    $0x2,%eax
8048413:       8d 58 ff                lea    -0x1(%eax),%ebx
8048416:       39 da                   cmp    %ebx,%edx
8048418:       73 1f                   jae    8048439 <__do_global_dtors_aux+0x49>
804841a:       8d b6 00 00 00 00       lea    0x0(%esi),%esi
8048420:       8d 42 01                lea    0x1(%edx),%eax
8048423:       a3 48 99 04 08          mov    %eax,0x8049948
8048428:       ff 14 85 34 98 04 08    call   *0x8049834(,%eax,4)
804842f:       8b 15 48 99 04 08       mov    0x8049948,%edx
8048435:       39 da                   cmp    %ebx,%edx
8048437:       72 e7                   jb     8048420 <__do_global_dtors_aux+0x30>
8048439:       c6 05 44 99 04 08 01    movb   $0x1,0x8049944
8048440:       83 c4 04                add    $0x4,%esp
8048443:       5b                      pop    %ebx
8048444:       5d                      pop    %ebp
8048445:       c3                      ret
8048446:       8d 76 00                lea    0x0(%esi),%esi
8048449:       8d bc 27 00 00 00 00    lea    0x0(%edi),%edi
08048450 <frame_dummy>:
8048450:       55                      push   %ebp
8048451:       89 e5                   mov    %esp,%ebp
8048453:       83 ec 08                sub    $0x8,%esp
8048456:       a1 3c 98 04 08          mov    0x804983c,%eax
804845b:       85 c0                   test   %eax,%eax
804845d:       74 12                   je     8048471 <frame_dummy+0x21>
804845f:       b8 00 00 00 00          mov    $0x0,%eax
8048464:       85 c0                   test   %eax,%eax
8048466:       74 09                   je     8048471 <frame_dummy+0x21>
8048468:       c7 04 24 3c 98 04 08    movl   $0x804983c,(%esp)
804846f:       ff d0                   call   *%eax
8048471:       c9                      leave
8048472:       c3                      ret
8048473:       90                      nop
08048474 <main>:           # C 程序中的 main
8048474:       8d 4c 24 04             lea    0x4(%esp),%ecx
8048478:       83 e4 f0                and    $0xfffffff0,%esp
804847b:       ff 71 fc                pushl  -0x4(%ecx)
804847e:       55                      push   %ebp
804847f:       89 e5                   mov    %esp,%ebp
8048481:       51                      push   %ecx
8048482:       83 ec 24                sub    $0x24,%esp
8048485:       89 4d e4                mov    %ecx,-0x1c(%ebp)
8048488:       8b 55 e4                mov    -0x1c(%ebp),%edx
804848b:       8b 42 04                mov    0x4(%edx),%eax
804848e:       89 44 24 04             mov    %eax,0x4(%esp)
8048492:       c7 04 24 90 87 04 08    movl   $0x8048790,(%esp)
8048499:       e8 f6 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
804849e:       8b 4d e4                mov    -0x1c(%ebp),%ecx
80484a1:       8b 41 04                mov    0x4(%ecx),%eax
80484a4:       8b 00                   mov    (%eax),%eax
80484a6:       89 44 24 04             mov    %eax,0x4(%esp)
80484aa:       c7 04 24 9d 87 04 08    movl   $0x804879d,(%esp)
80484b1:       e8 de fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
80484b6:       a1 40 99 04 08          mov    0x8049940,%eax
80484bb:       89 44 24 04             mov    %eax,0x4(%esp)
80484bf:       c7 04 24 ad 87 04 08    movl   $0x80487ad,(%esp)
80484c6:       e8 c9 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
80484cb:       a1 40 99 04 08          mov    0x8049940,%eax
80484d0:       8b 00                   mov    (%eax),%eax
80484d2:       89 44 24 04             mov    %eax,0x4(%esp)
80484d6:       c7 04 24 bd 87 04 08    movl   $0x80487bd,(%esp)
80484dd:       e8 b2 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
80484e2:       8d 45 f8                lea    -0x8(%ebp),%eax
80484e5:       89 44 24 04             mov    %eax,0x4(%esp)
80484e9:       c7 04 24 d0 87 04 08    movl   $0x80487d0,(%esp)
80484f0:       e8 9f fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
80484f5:       8d 45 f4                lea    -0xc(%ebp),%eax
80484f8:       89 44 24 04             mov    %eax,0x4(%esp)
80484fc:       c7 04 24 df 87 04 08    movl   $0x80487df,(%esp)
8048503:       e8 8c fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
8048508:       8d 45 f0                lea    -0x10(%ebp),%eax
804850b:       89 44 24 04             mov    %eax,0x4(%esp)
804850f:       c7 04 24 ee 87 04 08    movl   $0x80487ee,(%esp)
8048516:       e8 79 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
804851b:       8d 45 ec                lea    -0x14(%ebp),%eax
804851e:       89 44 24 04             mov    %eax,0x4(%esp)
8048522:       c7 04 24 fd 87 04 08    movl   $0x80487fd,(%esp)
8048529:       e8 66 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
804852e:       c7 04 24 0f 88 04 08    movl   $0x804880f,(%esp)
8048535:       e8 6a fe ff ff          call   80483a4 <[email=puts@plt]puts@plt[/email]>
804853a:       8d 45 f8                lea    -0x8(%ebp),%eax
804853d:       83 e0 f0                and    $0xfffffff0,%eax
8048540:       89 45 f4                mov    %eax,-0xc(%ebp)
8048543:       eb 60                   jmp    80485a5 <main+0x131>
8048545:       8b 45 f4                mov    -0xc(%ebp),%eax
8048548:       89 44 24 04             mov    %eax,0x4(%esp)
804854c:       c7 04 24 11 88 04 08    movl   $0x8048811,(%esp)
8048553:       e8 3c fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
8048558:       c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%ebp)
804855f:       eb 27                   jmp    8048588 <main+0x114>
8048561:       8b 55 f4                mov    -0xc(%ebp),%edx
8048564:       8b 45 f8                mov    -0x8(%ebp),%eax
8048567:       c1 e0 02                shl    $0x2,%eax
804856a:       8d 04 02                lea    (%edx,%eax,1),%eax
804856d:       8b 00                   mov    (%eax),%eax
804856f:       89 44 24 04             mov    %eax,0x4(%esp)
8048573:       c7 04 24 17 88 04 08    movl   $0x8048817,(%esp)
804857a:       e8 15 fe ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
804857f:       8b 45 f8                mov    -0x8(%ebp),%eax
8048582:       83 c0 01                add    $0x1,%eax
8048585:       89 45 f8                mov    %eax,-0x8(%ebp)
8048588:       8b 45 f8                mov    -0x8(%ebp),%eax
804858b:       83 f8 03                cmp    $0x3,%eax
804858e:       76 d1                   jbe    8048561 <main+0xed>
8048590:       c7 04 24 0a 00 00 00    movl   $0xa,(%esp)
8048597:       e8 d8 fd ff ff          call   8048374 <[email=putchar@plt]putchar@plt[/email]>
804859c:       8b 45 f4                mov    -0xc(%ebp),%eax
804859f:       83 c0 10                add    $0x10,%eax
80485a2:       89 45 f4                mov    %eax,-0xc(%ebp)
80485a5:       8b 45 f4                mov    -0xc(%ebp),%eax
80485a8:       89 c2                   mov    %eax,%edx
80485aa:       8b 4d e4                mov    -0x1c(%ebp),%ecx
80485ad:       8b 41 04                mov    0x4(%ecx),%eax
80485b0:       8b 00                   mov    (%eax),%eax
80485b2:       39 c2                   cmp    %eax,%edx
80485b4:       72 8f                   jb     8048545 <main+0xd1>
80485b6:       c7 04 24 0f 88 04 08    movl   $0x804880f,(%esp)
80485bd:       e8 e2 fd ff ff          call   80483a4 <[email=puts@plt]puts@plt[/email]>
80485c2:       8b 55 e4                mov    -0x1c(%ebp),%edx
80485c5:       8b 42 04                mov    0x4(%edx),%eax
80485c8:       8b 00                   mov    (%eax),%eax
80485ca:       83 e0 f0                and    $0xfffffff0,%eax
80485cd:       83 e8 10                sub    $0x10,%eax
80485d0:       89 45 f0                mov    %eax,-0x10(%ebp)
80485d3:       8b 45 f0                mov    -0x10(%ebp),%eax
80485d6:       89 45 ec                mov    %eax,-0x14(%ebp)
80485d9:       e9 cd 00 00 00          jmp    80486ab <main+0x237>
80485de:       8b 45 f0                mov    -0x10(%ebp),%eax
80485e1:       89 44 24 04             mov    %eax,0x4(%esp)
80485e5:       c7 04 24 11 88 04 08    movl   $0x8048811,(%esp)
80485ec:       e8 a3 fd ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
80485f1:       c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%ebp)
80485f8:       eb 28                   jmp    8048622 <main+0x1ae>
80485fa:       8b 55 f0                mov    -0x10(%ebp),%edx
80485fd:       8b 45 f8                mov    -0x8(%ebp),%eax
8048600:       8d 04 02                lea    (%edx,%eax,1),%eax
8048603:       0f b6 00                movzbl (%eax),%eax
8048606:       0f b6 c0                movzbl %al,%eax
8048609:       89 44 24 04             mov    %eax,0x4(%esp)
804860d:       c7 04 24 1d 88 04 08    movl   $0x804881d,(%esp)
8048614:       e8 7b fd ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
8048619:       8b 45 f8                mov    -0x8(%ebp),%eax
804861c:       83 c0 01                add    $0x1,%eax
804861f:       89 45 f8                mov    %eax,-0x8(%ebp)
8048622:       8b 45 f8                mov    -0x8(%ebp),%eax
8048625:       83 f8 0f                cmp    $0xf,%eax
8048628:       76 d0                   jbe    80485fa <main+0x186>
804862a:       c7 04 24 23 88 04 08    movl   $0x8048823,(%esp)
8048631:       e8 5e fd ff ff          call   8048394 <[email=printf@plt]printf@plt[/email]>
8048636:       c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%ebp)
804863d:       eb 4f                   jmp    804868e <main+0x21a>
804863f:       8b 55 f0                mov    -0x10(%ebp),%edx
8048642:       8b 45 f8                mov    -0x8(%ebp),%eax
8048645:       8d 04 02                lea    (%edx,%eax,1),%eax
8048648:       0f b6 00                movzbl (%eax),%eax
804864b:       3c 20                   cmp    $0x20,%al
804864d:       76 24                   jbe    8048673 <main+0x1ff>
804864f:       8b 55 f0                mov    -0x10(%ebp),%edx
8048652:       8b 45 f8                mov    -0x8(%ebp),%eax
8048655:       8d 04 02                lea    (%edx,%eax,1),%eax
8048658:       0f b6 00                movzbl (%eax),%eax
804865b:       3c 7e                   cmp    $0x7e,%al
804865d:       77 14                   ja     8048673 <main+0x1ff>
804865f:       8b 55 f0                mov    -0x10(%ebp),%edx
8048662:       8b 45 f8                mov    -0x8(%ebp),%eax
8048665:       8d 04 02                lea    (%edx,%eax,1),%eax
8048668:       0f b6 00                movzbl (%eax),%eax
804866b:       0f b6 c0                movzbl %al,%eax
804866e:       89 45 e8                mov    %eax,-0x18(%ebp)
8048671:       eb 07                   jmp    804867a <main+0x206>
8048673:       c7 45 e8 2e 00 00 00    movl   $0x2e,-0x18(%ebp)
804867a:       8b 4d e8                mov    -0x18(%ebp),%ecx
804867d:       89 0c 24                mov    %ecx,(%esp)
8048680:       e8 ef fc ff ff          call   8048374 <[email=putchar@plt]putchar@plt[/email]>
8048685:       8b 45 f8                mov    -0x8(%ebp),%eax
8048688:       83 c0 01                add    $0x1,%eax
804868b:       89 45 f8                mov    %eax,-0x8(%ebp)
804868e:       8b 45 f8                mov    -0x8(%ebp),%eax
8048691:       83 f8 0f                cmp    $0xf,%eax
8048694:       76 a9                   jbe    804863f <main+0x1cb>
8048696:       c7 04 24 0a 00 00 00    movl   $0xa,(%esp)
804869d:       e8 d2 fc ff ff          call   8048374 <[email=putchar@plt]putchar@plt[/email]>
80486a2:       8b 45 f0                mov    -0x10(%ebp),%eax
80486a5:       83 c0 10                add    $0x10,%eax
80486a8:       89 45 f0                mov    %eax,-0x10(%ebp)
80486ab:       8b 45 f0                mov    -0x10(%ebp),%eax
80486ae:       89 c2                   mov    %eax,%edx
80486b0:       8b 45 ec                mov    -0x14(%ebp),%eax
80486b3:       05 c0 05 00 00          add    $0x5c0,%eax
80486b8:       39 c2                   cmp    %eax,%edx
80486ba:       0f 82 1e ff ff ff       jb     80485de <main+0x16a>
80486c0:       b8 00 00 00 00          mov    $0x0,%eax
80486c5:       83 c4 24                add    $0x24,%esp
80486c8:       59                      pop    %ecx
80486c9:       5d                      pop    %ebp
80486ca:       8d 61 fc                lea    -0x4(%ecx),%esp
80486cd:       c3                      ret
80486ce:       90                      nop
80486cf:       90                      nop
080486d0 <__libc_csu_fini>:
80486d0:       55                      push   %ebp
80486d1:       89 e5                   mov    %esp,%ebp
80486d3:       5d                      pop    %ebp
80486d4:       c3                      ret
80486d5:       8d 74 26 00             lea    0x0(%esi),%esi
80486d9:       8d bc 27 00 00 00 00    lea    0x0(%edi),%edi
080486e0 <__libc_csu_init>:
80486e0:       55                      push   %ebp
80486e1:       89 e5                   mov    %esp,%ebp
80486e3:       57                      push   %edi
80486e4:       56                      push   %esi
80486e5:       53                      push   %ebx
80486e6:       e8 4f 00 00 00          call   804873a <__i686.get_pc_thunk.bx>
80486eb:       81 c3 29 12 00 00       add    $0x1229,%ebx
80486f1:       83 ec 0c                sub    $0xc,%esp
80486f4:       e8 2b fc ff ff          call   8048324 <_init>
80486f9:       8d bb 18 ff ff ff       lea    -0xe8(%ebx),%edi
80486ff:       8d 83 18 ff ff ff       lea    -0xe8(%ebx),%eax
8048705:       29 c7                   sub    %eax,%edi
8048707:       c1 ff 02                sar    $0x2,%edi
804870a:       85 ff                   test   %edi,%edi
804870c:       74 24                   je     8048732 <__libc_csu_init+0x52>
804870e:       31 f6                   xor    %esi,%esi
8048710:       8b 45 10                mov    0x10(%ebp),%eax
8048713:       89 44 24 08             mov    %eax,0x8(%esp)
8048717:       8b 45 0c                mov    0xc(%ebp),%eax
804871a:       89 44 24 04             mov    %eax,0x4(%esp)
804871e:       8b 45 08                mov    0x8(%ebp),%eax
8048721:       89 04 24                mov    %eax,(%esp)
8048724:       ff 94 b3 18 ff ff ff    call   *-0xe8(%ebx,%esi,4)
804872b:       83 c6 01                add    $0x1,%esi
804872e:       39 fe                   cmp    %edi,%esi
8048730:       72 de                   jb     8048710 <__libc_csu_init+0x30>
8048732:       83 c4 0c                add    $0xc,%esp
8048735:       5b                      pop    %ebx
8048736:       5e                      pop    %esi
8048737:       5f                      pop    %edi
8048738:       5d                      pop    %ebp
8048739:       c3                      ret
0804873a <__i686.get_pc_thunk.bx>:
804873a:       8b 1c 24                mov    (%esp),%ebx
804873d:       c3                      ret
804873e:       90                      nop
804873f:       90                      nop
08048740 <__do_global_ctors_aux>:
8048740:       55                      push   %ebp
8048741:       89 e5                   mov    %esp,%ebp
8048743:       53                      push   %ebx
8048744:       83 ec 04                sub    $0x4,%esp
8048747:       a1 2c 98 04 08          mov    0x804982c,%eax
804874c:       83 f8 ff                cmp    $0xffffffff,%eax
804874f:       74 13                   je     8048764 <__do_global_ctors_aux+0x24>
8048751:       bb 2c 98 04 08          mov    $0x804982c,%ebx
8048756:       66 90                   xchg   %ax,%ax
8048758:       83 eb 04                sub    $0x4,%ebx
804875b:       ff d0                   call   *%eax
804875d:       8b 03                   mov    (%ebx),%eax
804875f:       83 f8 ff                cmp    $0xffffffff,%eax
8048762:       75 f4                   jne    8048758 <__do_global_ctors_aux+0x
8048764:       83 c4 04                add    $0x4,%esp
8048767:       5b                      pop    %ebx
8048768:       5d                      pop    %ebp
8048769:       c3                      ret
804876a:       90                      nop
804876b:       90                      nop
Disassembly of section .fini:
0804876c <_fini>:             # 在 /usr/lib/crti.o 中定义
804876c:       55                      push   %ebp
804876d:       89 e5                   mov    %esp,%ebp
804876f:       53                      push   %ebx
8048770:       83 ec 04                sub    $0x4,%esp
8048773:       e8 00 00 00 00          call   8048778 <_fini+0xc>
8048778:       5b                      pop    %ebx
8048779:       81 c3 9c 11 00 00       add    $0x119c,%ebx
804877f:       e8 6c fc ff ff          call   80483f0 <__do_global_dtors_aux>
8048784:       59                      pop    %ecx
8048785:       5b                      pop    %ebx
8048786:       c9                      leave
8048787:       c3                      ret

上面,.plt 段用来协助完成动态链接功能。

另外,查看目标文件可以用 readelf 命令,如果只是关心符号信息,那么还可以使用 nm 命令,如下查看 crt1.o 和 crti.o 的符号信息:

C$ nm /usr/lib/crt1.o
00000000 R _IO_stdin_used
00000000 D __data_start
         U __libc_csu_fini
         U __libc_csu_init
         U __libc_start_main
00000000 R _fp_hw
00000000 T _start
00000000 W data_start
         U main

$ nm /usr/lib/crti.o
         U _GLOBAL_OFFSET_TABLE_
         w __gmon_start__
00000000 T _fini
00000000 T _init
上面,大写字母 U 表示未定义,它需要在别处定义出来;T 表示已在此库里有定义。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-4-29 13:58 , Processed in 0.064075 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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