Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112662
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-08-12 11:06:04

《链接器与加载器》第1章链接与加载

1.
1.1 main.c
  1. cong@msi:/work/test/test/linkload$ cat m.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. extern void a(char*);
  5. int main (int argc, char *argv[])
  6. {
  7.     static char string[]="hello,world\n";
  8.     a(string);
  9.     return 0;
  10. }
1.2 main.o的objdump
gcc -c -O0 m.c -o m.o    ;这儿只是编译,没有链接
  1. cong@msi:/work/test/test/linkload$ objdump -S ./m.o    --> -S的作用是:
           -S
           --source
               Display source code intermixed with disassembly, if possible.  Implies -d

  2. ./m.o: file format elf64-x86-64


  3. Disassembly of section .text:

  4. 0000000000000000 <main>:
  5.    0:    55                 push %rbp
  6.    1:    48 89 e5           mov %rsp,%rbp
  7.    4:    48 83 ec 10        sub $0x10,%rsp
  8.    8:    89 7d fc           mov %edi,-0x4(%rbp)
  9.    b:    48 89 75 f0        mov %rsi,-0x10(%rbp)
  10.    f:    bf 00 00 00 00     mov $0x0,%edi
  11.   14:    e8 00 00 00 00     callq 19 <main+0x19>        -->《链接器与加载器》上是call 0 
  12.   19:    b8 00 00 00 00     mov $0x0,%eax               -->这儿是下一行的地址
  13.   1e:    c9                 leaveq
  14.   1f:    c3                 retq
这儿为神马是下一行的地址呢?
 我的理解:程序本身就是顺序执行的,这儿的call不知道要call到哪个地址去,索性就顺序执行。
2.
2.1 a.c
  1. cong@msi:/work/test/test/linkload$ cat a.
  2. #include <unistd.h>
  3. #include <string.h>
  4. void a(char* s)
  5. {
  6.     write(1, s, strlen(s));
  7. }
2.2 a.c的objdump
gcc -c -O0 a.c -o a.o    ;这儿只是编译,没有链接
  1. cong@msi:/work/test/test/linkload$ objdump -S ./a.o

  2. ./a.o: file format elf64-x86-64


  3. Disassembly of section .text:

  4. 0000000000000000 <a>:
  5.    0:    55                        push %rbp
  6.    1:    48 89 e5                  mov %rsp,%rbp
  7.    4:    48 83 ec 10               sub $0x10,%rsp
  8.    8:    48 89 7d f8               mov %rdi,-0x8(%rbp)
  9.    c:    48 8b 45 f8               mov -0x8(%rbp),%rax
  10.   10:    48 89 c7                  mov %rax,%rdi
  11.   13:    e8 00 00 00 00            callq 18 <a+0x18>         -->《链接器与加载器》上是call 0 
  12.   18:    48 89 c2                  mov %rax,%rdx             -->这儿是下一行的地址
  13.   1b:    48 8b 45 f8               mov -0x8(%rbp),%rax
  14.   1f:    48 89 c6                  mov %rax,%rsi
  15.   22:    bf 01 00 00 00            mov $0x1,%edi
  16.   27:    e8 00 00 00 00            callq 2c <a+0x2c>
  17.   2c:    c9                        leaveq
  18.   2d:    c3                        retq

3.完整程序的objdump
gcc a.o m.o -o test    链接之后
  1. cong@msi:/work/test/test/linkload$ objdump -S  ./test

  2. ./test: file format elf64-x86-64


  3. Disassembly of section .init:

  4. 0000000000400418 <_init>:
  5.   400418:    48 83 ec 08              sub $0x8,%rsp
  6.   40041c:    48 8b 05 d5 0b 20 00     mov 0x200bd5(%rip),%rax # 600ff8 <_DYNAMIC+0x1d0>
  7.   400423:    48 85 c0                 test %rax,%rax
  8.   400426:    74 05                    je 40042d <_init+0x15>
  9.   400428:    e8 53 00 00 00           callq 400480 <__gmon_start__@plt>
  10.   40042d:    48 83 c4 08              add $0x8,%rsp
  11.   400431:    c3     retq

  12. Disassembly of section .plt:

  13. 0000000000400440 <write@plt-0x10>:
  14.   400440:    ff 35 c2 0b 20 00         pushq 0x200bc2(%rip) # 601008 <_GLOBAL_OFFSET_TABLE_+0x8>
  15.   400446:    ff 25 c4 0b 20 00         jmpq *0x200bc4(%rip) # 601010 <_GLOBAL_OFFSET_TABLE_+0x10>
  16.   40044c:    0f 1f 40 00               nopl 0x0(%rax)

  17. 0000000000400450 <write@plt>:
  18.   400450:    ff 25 c2 0b 20 00         jmpq *0x200bc2(%rip) # 601018 <_GLOBAL_OFFSET_TABLE_+0x18>
  19.   400456:    68 00 00 00 00            pushq $0x0
  20.   40045b:    e9 e0 ff ff ff            jmpq 400440 <_init+0x28>

  21. 0000000000400460 <strlen@plt>:
  22.   400460:    ff 25 ba 0b 20 00         jmpq *0x200bba(%rip) # 601020 <_GLOBAL_OFFSET_TABLE_+0x20>
  23.   400466:    68 01 00 00 00            pushq $0x1
  24.   40046b:    e9 d0 ff ff ff            jmpq 400440 <_init+0x28>

  25. 0000000000400470 <__libc_start_main@plt>:
  26.   400470:    ff 25 b2 0b 20 00         jmpq *0x200bb2(%rip) # 601028 <_GLOBAL_OFFSET_TABLE_+0x28>
  27.   400476:    68 02 00 00 00            pushq $0x2
  28.   40047b:    e9 c0 ff ff ff            jmpq 400440 <_init+0x28>

  29. 0000000000400480 <__gmon_start__@plt>:
  30.   400480:    ff 25 aa 0b 20 00         jmpq *0x200baa(%rip) # 601030 <_GLOBAL_OFFSET_TABLE_+0x30>
  31.   400486:    68 03 00 00 00            pushq $0x3
  32.   40048b:    e9 b0 ff ff ff            jmpq 400440 <_init+0x28>

  33. Disassembly of section .text:

  34. 0000000000400490 <_start>:
  35.   400490:    31 ed                     xor %ebp,%ebp
  36.   400492:    49 89 d1                  mov %rdx,%r9
  37.   400495:    5e                        pop %rsi
  38.   400496:    48 89 e2                  mov %rsp,%rdx
  39.   400499:    48 83 e4 f0               and $0xfffffffffffffff0,%rsp
  40.   40049d:    50                        push %rax
  41.   40049e:    54                        push %rsp
  42.   40049f:    49 c7 c0 40 06 40 00      mov $0x400640,%r8
  43.   4004a6:    48 c7 c1 d0 05 40 00      mov $0x4005d0,%rcx
  44.   4004ad:    48 c7 c7 ab 05 40 00      mov $0x4005ab,%rdi
  45.   4004b4:    e8 b7 ff ff ff            callq 400470 <__libc_start_main@plt>
  46.   4004b9:    f4                        hlt
  47.   4004ba:    66 0f 1f 44 00 00         nopw 0x0(%rax,%rax,1)

  48. 00000000004004c0 <deregister_tm_clones>:
  49.   4004c0:    b8 5f 10 60 00            mov $0x60105f,%eax
  50.   4004c5:    55                        push %rbp
  51.   4004c6:    48 2d 58 10 60 00         sub $0x601058,%rax
  52.   4004cc:    48 83 f8 0e               cmp $0xe,%rax
  53.   4004d0:    48 89 e5                  mov %rsp,%rbp
  54.   4004d3:    77 02                     ja 4004d7 <deregister_tm_clones+0x17>
  55.   4004d5:    5d                        pop %rbp
  56.   4004d6:    c3                        retq
  57.   4004d7:    b8 00 00 00 00            mov $0x0,%eax
  58.   4004dc:    48 85 c0                  test %rax,%rax
  59.   4004df:    74 f4                     je 4004d5 <deregister_tm_clones+0x15>
  60.   4004e1:    5d                        pop %rbp
  61.   4004e2:    bf 58 10 60 00            mov $0x601058,%edi
  62.   4004e7:    ff e0                     jmpq *%rax
  63.   4004e9:    0f 1f 80 00 00 00 00      nopl 0x0(%rax)

  64. 00000000004004f0 <register_tm_clones>:
  65.   4004f0:    b8 58 10 60 00            mov $0x601058,%eax
  66.   4004f5:    55                        push %rbp
  67.   4004f6:    48 2d 58 10 60 00         sub $0x601058,%rax
  68.   4004fc:    48 c1 f8 03               sar $0x3,%rax
  69.   400500:    48 89 e5                  mov %rsp,%rbp
  70.   400503:    48 89 c2                  mov %rax,%rdx
  71.   400506:    48 c1 ea 3f               shr $0x3f,%rdx
  72.   40050a:    48 01 d0                  add %rdx,%rax
  73.   40050d:    48 d1 f8                  sar %rax
  74.   400510:    75 02                     jne 400514 <register_tm_clones+0x24>
  75.   400512:    5d                        pop %rbp
  76.   400513:    c3                        retq
  77.   400514:    ba 00 00 00 00            mov $0x0,%edx
  78.   400519:    48 85 d2                  test %rdx,%rdx
  79.   40051c:    74 f4                     je 400512 <register_tm_clones+0x22>
  80.   40051e:    5d                        pop %rbp
  81.   40051f:    48 89 c6                  mov %rax,%rsi
  82.   400522:    bf 58 10 60 00            mov $0x601058,%edi
  83.   400527:    ff e2                     jmpq *%rdx
  84.   400529:    0f 1f 80 00 00 00 00      nopl 0x0(%rax)

  85. 0000000000400530 <__do_global_dtors_aux>:
  86.   400530:    80 3d 1e 0b 20 00 00      cmpb $0x0,0x200b1e(%rip) # 601055 <_edata>
  87.   400537:    75 11                     jne 40054a <__do_global_dtors_aux+0x1a>
  88.   400539:    55                        push %rbp
  89.   40053a:    48 89 e5                  mov %rsp,%rbp
  90.   40053d:    e8 7e ff ff ff            callq 4004c0 <deregister_tm_clones>
  91.   400542:    5d                        pop %rbp
  92.   400543:    c6 05 0b 0b 20 00 01      movb $0x1,0x200b0b(%rip) # 601055 <_edata>
  93.   40054a:    f3 c3                     repz retq
  94.   40054c:    0f 1f 40 00               nopl 0x0(%rax)

  95. 0000000000400550 <frame_dummy>:
  96.   400550:    48 83 3d c8 08 20 00     cmpq $0x0,0x2008c8(%rip) # 600e20 <__JCR_END__>
  97.   400557:    00
  98.   400558:    74 1e                     je 400578 <frame_dummy+0x28>
  99.   40055a:    b8 00 00 00 00            mov $0x0,%eax
  100.   40055f:    48 85 c0                  test %rax,%rax
  101.   400562:    74 14                     je 400578 <frame_dummy+0x28>
  102.   400564:    55                        push %rbp
  103.   400565:    bf 20 0e 60 00            mov $0x600e20,%edi
  104.   40056a:    48 89 e5                  mov %rsp,%rbp
  105.   40056d:    ff d0                     callq *%rax
  106.   40056f:    5d                        pop %rbp
  107.   400570:    e9 7b ff ff ff            jmpq 4004f0 <register_tm_clones>
  108.   400575:    0f 1f 00                  nopl (%rax)
  109.   400578:    e9 73 ff ff ff            jmpq 4004f0 <register_tm_clones>

  110. 000000000040057d <a>:
  111.   40057d:    55                        push %rbp
  112.   40057e:    48 89 e5                  mov %rsp,%rbp
  113.   400581:    48 83 ec 10               sub $0x10,%rsp
  114.   400585:    48 89 7d f8               mov %rdi,-0x8(%rbp)
  115.   400589:    48 8b 45 f8               mov -0x8(%rbp),%rax
  116.   40058d:    48 89 c7                  mov %rax,%rdi
  117.   400590:    e8 cb fe ff ff            callq 400460 <strlen@plt>     -->在编译时是  callq 18,链接后变成了符号strlen的地址
  118.   400595:    48 89 c2                  mov %rax,%rdx
  119.   400598:    48 8b 45 f8               mov -0x8(%rbp),%rax
  120.   40059c:    48 89 c6                  mov %rax,%rsi
  121.   40059f:    bf 01 00 00 00            mov $0x1,%edi
  122.   4005a4:    e8 a7 fe ff ff            callq 400450 <write@plt>      -->在编译时是  callq 2c,链接后变成了符号write的地址
  123.   4005a9:    c9                        leaveq
  124.   4005aa:    c3                        retq

  125. 00000000004005ab <main>:
  126.   4005ab:    55                        push %rbp
  127.   4005ac:    48 89 e5                  mov %rsp,%rbp
  128.   4005af:    48 83 ec 10               sub $0x10,%rsp
  129.   4005b3:    89 7d fc                  mov %edi,-0x4(%rbp)
  130.   4005b6:    48 89 75 f0               mov %rsi,-0x10(%rbp)
  131.   4005ba:    bf 48 10 60 00            mov $0x601048,%edi
  132.   4005bf:    e8 b9 ff ff ff            callq 40057d <a>                 -->在编译时是  callq 19,链接后变成了符号a的地址
  133.   4005c4:    b8 00 00 00 00            mov $0x0,%eax
  134.   4005c9:    c9                        leaveq
  135.   4005ca:    c3                        retq
  136.   4005cb:    0f 1f 44 00 00            nopl 0x0(%rax,%rax,1)

  137. 00000000004005d0 <__libc_csu_init>:
  138.   4005d0:    41 57                     push %r15
  139.   4005d2:    41 89 ff                  mov %edi,%r15d
  140.   4005d5:    41 56                     push %r14
  141.   4005d7:    49 89 f6                  mov %rsi,%r14
  142.   4005da:    41 55                     push %r13
  143.   4005dc:    49 89 d5                  mov %rdx,%r13
  144.   4005df:    41 54                     push %r12
  145.   4005e1:    4c 8d 25 28 08 20 00      lea 0x200828(%rip),%r12 # 600e10 <__frame_dummy_init_array_entry>
  146.   4005e8:    55                        push %rbp
  147.   4005e9:    48 8d 2d 28 08 20 00      lea 0x200828(%rip),%rbp # 600e18 <__init_array_end>
  148.   4005f0:    53                        push %rbx
  149.   4005f1:    4c 29 e5                  sub %r12,%rbp
  150.   4005f4:    31 db                     xor %ebx,%ebx
  151.   4005f6:    48 c1 fd 03               sar $0x3,%rbp
  152.   4005fa:    48 83 ec 08               sub $0x8,%rsp
  153.   4005fe:    e8 15 fe ff ff            callq 400418 <_init>
  154.   400603:    48 85 ed                  test %rbp,%rbp
  155.   400606:    74 1e                     je 400626 <__libc_csu_init+0x56>
  156.   400608:    0f 1f 84 00 00 00 00      nopl 0x0(%rax,%rax,1)
  157.   40060f:    00
  158.   400610:    4c 89 ea                  mov %r13,%rdx
  159.   400613:    4c 89 f6                  mov %r14,%rsi
  160.   400616:    44 89 ff                  mov %r15d,%edi
  161.   400619:    41 ff 14 dc               callq *(%r12,%rbx,8)
  162.   40061d:    48 83 c3 01               add $0x1,%rbx
  163.   400621:    48 39 eb                  cmp %rbp,%rbx
  164.   400624:    75 ea                     jne 400610 <__libc_csu_init+0x40>
  165.   400626:    48 83 c4 08               add $0x8,%rsp
  166.   40062a:    5b                        pop %rbx
  167.   40062b:    5d                        pop %rbp
  168.   40062c:    41 5c                     pop %r12
  169.   40062e:    41 5d                     pop %r13
  170.   400630:    41 5e                     pop %r14
  171.   400632:    41 5f                     pop %r15
  172.   400634:    c3                        retq
  173.   400635:    66 66 2e 0f 1f 84 00      data32 nopw %cs:0x0(%rax,%rax,1)
  174.   40063c:    00 00 00 00

  175. 0000000000400640 <__libc_csu_fini>:
  176.   400640:    f3 c3                     repz retq

  177. Disassembly of section .fini:

  178. 0000000000400644 <_fini>:
  179.   400644:    48 83 ec 08              sub $0x8,%rsp
  180.   400648:    48 83 c4 08              add $0x8,%rsp
  181.   40064c:    c3     retq





阅读(2023) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~