现在的位置: 首页 > 综合 > 正文

重定位类型分析(1)

2013年10月29日 ⁄ 综合 ⁄ 共 30940字 ⁄ 字号 评论关闭
重定位目标文件:位置相关 gcc -c 1.c -o 1.o

PIC重定位目标文件:位置无关 gcc -fPIC -c 1.c -o 1.o

静态库文件:多个重定位目标文件的集合 ar c lib.a 1.o 2.o

共享库:动态链接库 ld -shared 1.o -o 1.so

可执行文件:静态链接 gcc 2.o 1.o -o 3 or gcc 2.o lib.a -o 3

可执行文件:动态链接 gcc 2.o 1.so -o 3

 
重定位分两个步骤

目标文件中的重定位信息,指导ld如何连接时重定位(静态)

可执行文件中的重定位信息,指导/lib/ld-linux.so.2如何执行时重定位(动态)

 
重定位类型解析(1)
由于是重定位目标文件,所以Offset是相对于节的偏移。.rel.text说明要重定位的节是.text.

找节表,.rel.text节的Inf指向1节,即.text.(可发现Addr全0)

 
Info字段分成两部分,第八位为重定位类型,高24位为符号表索引,据此可找到重定位的符号。

这里的符号表是.symtab.

  0000000b  00901 R_386_32              00000000  s         

而009在.symtab索引到的项为   

     9: 00000000     4 OBJECT  GLOBAL DEFAULT    3  s

名字为s,值为全0,大小为4字节(指针变量),节索引为3,即在.data节中.

  00000027  00a01 R_386_32              00000004  t

可见00a在.symtab索引到的项为   

    10: 00000004     4 OBJECT  GLOBAL DEFAULT    3 t     

名字为t,值为4,大小为4字节(指针变量),节索引为3,即在.data节中.

(还可以发现符号表中f和g的大小正好等于其代码长度,f大小25,代码长度0~0x18,...)

这里的值指的是该符号在所在的节中的偏移。
 
[root@proxy ~/3]# objdump -sj .data 1.o
1.o:     file format elf32-i386
Contents of section .data:

 0000 00000000 0e000000                    ........ 

 

可见,s指向的值是0,t指向的值是0xe.这就是变量的s和t的值。

实际上,这两个值是指向.rodata节的偏移。

[root@proxy ~/3]# objdump -sj .rodata 1.o
1.o:     file format elf32-i386
Contents of section .rodata:

 0000 68656c6c 6f20576f 726c6421 0a006162  hello World!..ab

 0010 630a00                               c..          

 

 即s指向hello world!

 t指向abc

 和程序中的意思一样。

                

再看看重定位中的偏移Offset:[该offset指明了程序中需要重定位的位置]

Relocation section '.rel.text' at offset 0x40c contains 4 entries:

  Offset          Info           Type            Symbol's    Value            Symbol's Name

  0000000b  00901       R_386_32       00000000                       s                       

  00000010  00c02        R_386_PC32  00000000                       printf                  

  00000027  00a01        R_386_32       00000004                       t                       

  0000002c  00c02        R_386_PC32   00000000                       printf

可以在objdump -dj .text 1.o中查找对应的位置 

 

假设现在1.o要和另一个目标文件2.o连接成可执行文件,由于ld要合并相同的节,1.o中符号原来的地址

变成无效地址了,因此1.o中的符号必须被重定位

R_386_32重定位方法: .data节被合并到新的地址处,假设为x,则s的值就修正为x+0,t的值被修正为x+4

(此时是地址,而不再是节偏移),然后把该值写入Offset处

参看3.o调用1.o的f和g

R_386_PC32重定位方法: .text节被合并到新的地址处,假设为y,则f的值被修正为y+0,g的值被修正为y+0x1c.

然后把该值写到Offset处。

因此就不难理解下面的意思了

R_386_NONE 0 none      none

R_386_32      1 word32  S + A

R_386_PC32  2 word32  S + A

S是节的新地址,A是Offset
 
附件1
 
1.c.txt

[root@proxy ~/3]# cat 1.c

#include <stdio.h>

char *s="hello World!/n";

char *t="abc/n";

void f()

{

printf(s);

}

void g()

{

printf(t);

}

[root@proxy ~/3]# gcc -c 1.c -o 1.o

[root@proxy ~/3]# objdump -dj .text 1.o

1.o:     file format elf32-i386

Disassembly of section .text:

00000000 <f>:

   0:   55                          push   %ebp

   1:   89 e5                     mov    %esp,%ebp

   3:   83 ec 08                 sub    $0x8,%esp

   6:   83 ec 0c                 sub    $0xc,%esp

   9:   ff 35 00 00 00 00   pushl  0x0

   f:   e8 fc ff ff ff               call   10 <f+0x10>

  14:   83 c4 10                add    $0x10,%esp

  17:   c9                          leave 

  18:   c3                          ret   

  19:   8d 76 00                lea    0x0(%esi),%esi

0000001c <g>:

  1c:   55                         push   %ebp

  1d:   89 e5                    mov    %esp,%ebp

  1f:   83 ec 08                sub    $0x8,%esp

  22:   83 ec 0c                sub    $0xc,%esp

  25:   ff 35 00 00 00 00  pushl  0x0

  2b:   e8 fc ff ff ff             call   2c <g+0x10>

  30:   83 c4 10                add    $0x10,%esp

  33:   c9                          leave 

  34:   c3                          ret   

  35:   8d 76 00                lea    0x0(%esi),%esi

 

[root@proxy ~/3]# readelf -a 1.o

ELF Header:

  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

  Class:                             ELF32

  Data:                              2's complement, little endian

  Version:                           1 (current)

  OS/ABI:                            UNIX - System V

  ABI Version:                       0

  Type:                              REL (Relocatable file)

  Machine:                           Intel 80386

  Version:                           0x1

  Entry point address:               0x0

  Start of program headers:          0 (bytes into file)

  Start of section headers:          296 (bytes into file)

  Flags:                             0x0

  Size of this header:               52 (bytes)

  Size of program headers:           0 (bytes)

  Number of program headers:         0

  Size of section headers:           40 (bytes)

  Number of section headers:         12

  Section header string table index: 9

Section Headers:

  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al

  [ 0]                      NULL            00000000 000000 000000 00      0   0  0

  [ 1] .text              PROGBITS        00000000 000034 000038 00  AX  0   0  4

  [ 2] .rel.text         REL             00000000 00040c 000020 08     10   1  4

  [ 3] .data             PROGBITS        00000000 00006c 000008 00  WA  0   0  4

  [ 4] .rel.data        REL             00000000 00042c 000010 08     10   3  4

  [ 5] .bss               NOBITS          00000000 000074 000000 00  WA  0   0  4

  [ 6] .note              NOTE            00000000 000074 000014 00      0   0  1

  [ 7] .rodata           PROGBITS        00000000 000088 000013 00   A  0   0  1

  [ 8] .comment       PROGBITS        00000000 00009b 000036 00      0   0  1

  [ 9] .shstrtab        STRTAB          00000000 0000d1 000057 00      0   0  1

  [10] .symtab         SYMTAB          00000000 000308 0000e0 10     11   9  4

  [11] .strtab           STRTAB          00000000 0003e8 000023 00      0   0  1

Key to Flags:

  W (write), A (alloc), X (execute), M (merge), S (strings)

  I (info), L (link order), G (group), x (unknown)

  O (extra OS processing required) o (OS specific), p (processor specific)

There are no program headers in this file.

There is no dynamic segment in this file.

Relocation section '.rel.text' at offset 0x40c contains 4 entries:

  Offset          Info  Type                    Symbol's Value  Symbol's Name

  0000000b  00901 R_386_32              00000000       s                       

  00000010  00c02 R_386_PC32          00000000       printf                  

  00000027  00a01 R_386_32              00000004       t                       

  0000002c  00c02 R_386_PC32           00000000       printf                  

Relocation section '.rel.data' at offset 0x42c contains 2 entries:

  Offset           Info  Type                   Symbol's Value  Symbol's Name

  00000000  00601 R_386_32              00000000      .rodata                 

  00000004  00601 R_386_32              00000000      .rodata                 

 

There are no unwind sections in this file.

Symbol table '.symtab' contains 14 entries:

   Num:    Value    Size Type         Bind      Vis         Ndx   Name

     0: 00000000     0  NOTYPE   LOCAL  DEFAULT  UND

     1: 00000000     0  FILE        LOCAL  DEFAULT  ABS   1.c

     2: 00000000     0  SECTION LOCAL  DEFAULT    1

     3: 00000000     0  SECTION LOCAL  DEFAULT    3

     4: 00000000     0  SECTION LOCAL  DEFAULT    5

     5: 00000000     0  NOTYPE   LOCAL  DEFAULT    1     gcc2_compiled.

     6: 00000000     0  SECTION LOCAL  DEFAULT    7

     7: 00000000     0  SECTION  LOCAL  DEFAULT    6

     8: 00000000     0  SECTION LOCAL  DEFAULT     8

     9: 00000000     4  OBJECT   GLOBAL DEFAULT    3     s

    10: 00000004    4  OBJECT   GLOBAL DEFAULT    3      t

    11: 00000000    25 FUNC     GLOBAL DEFAULT    1      f

    12: 00000000     0  NOTYPE  GLOBAL DEFAULT  UND  printf

    13: 0000001c    25 FUNC      GLOBAL DEFAULT    1     g

No version information found in this file.

 

附件2
3.c.txt
[root@proxy ~/3]# cat 3.c

void f();

int main()

{

f();

g();

return 0;

}

[root@proxy ~/3]# gcc -c 3.c -o 3.o

[root@proxy ~/3]# objdump -dj .text 3.o

3.o:     file format elf32-i386
Disassembly of section .text:
00000000 <main>:

   0:   55                      push   %ebp

   1:   89 e5                 mov    %esp,%ebp

   3:   83 ec 08             sub    $0x8,%esp

   6:   e8 fc ff ff ff          call   7 <main+0x7>

   b:   e8 fc ff ff ff          call   c <main+0xc>

  10:   b8 00 00 00 00  mov    $0x0,%eax

  15:   c9                       leave 

  16:   c3                       ret   

  17:   90                       nop  

[root@proxy ~/3]# readelf -a 3.o

ELF Header:

  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

  Class:                             ELF32

  Data:                              2's complement, little endian

  Version:                           1 (current)

  OS/ABI:                            UNIX - System V

  ABI Version:                       0

  Type:                              REL (Relocatable file)

  Machine:                           Intel 80386

  Version:                           0x1

  Entry point address:               0x0

  Start of program headers:          0 (bytes into file)

  Start of section headers:          220 (bytes into file)

  Flags:                             0x0

  Size of this header:               52 (bytes)

  Size of program headers:           0 (bytes)

  Number of program headers:         0

  Size of section headers:           40 (bytes)

  Number of section headers:         10

  Section header string table index: 7

Section Headers:

  [Nr] Name              Type            Addr         Off        Size     ES  Flg  Lk Inf Al

  [ 0]                      NULL            00000000 000000 000000 00        0   0  0

  [ 1] .text              PROGBITS    00000000 000034 000018 00  AX  0   0  4

  [ 2] .rel.text         REL              00000000 00033c 000010 08         8   1  4

  [ 3] .data             PROGBITS    00000000 00004c 000000 00  WA  0   0  4

  [ 4] .bss               NOBITS        00000000 00004c 000000 00  WA  0   0  4

  [ 5] .note             NOTE            00000000 00004c 000014 00         0   0  1

  [ 6] .comment      PROGBITS     00000000 000060 000036 00         0   0  1

  [ 7] .shstrtab       STRTAB         00000000 000096 000045 00         0   0  1

  [ 8] .symtab         SYMTAB         00000000 00026c 0000b0 10         9   8  4

  [ 9] .strtab           STRTAB         00000000 00031c 00001d 00          0   0  1

Key to Flags:

  W (write), A (alloc), X (execute), M (merge), S (strings)

  I (info), L (link order), G (group), x (unknown)

  O (extra OS processing required) o (OS specific), p (processor specific)

There are no program headers in this file.
There is no dynamic segment in this file.
Relocation section '.rel.text' at offset 0x33c contains 2 entries:

  Offset           Info  Type                  Symbol's Value  Symbol's Name

  00000007  00902 R_386_PC32         00000000          f                       

  0000000c  00a02 R_386_PC32          00000000         g                       

There are no unwind sections in this file.
Symbol table '.symtab' contains 11 entries:

   Num:    Value  Size Type    Bind   Vis      Ndx Name

     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND

     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS 3.c

     2: 00000000     0 SECTION LOCAL  DEFAULT    1

     3: 00000000     0 SECTION LOCAL  DEFAULT    3

     4: 00000000     0 SECTION LOCAL  DEFAULT    4

     5: 00000000     0 NOTYPE  LOCAL  DEFAULT    1 gcc2_compiled.

     6: 00000000     0 SECTION LOCAL  DEFAULT    5

     7: 00000000     0 SECTION LOCAL  DEFAULT    6

     8: 00000000    23 FUNC    GLOBAL DEFAULT    1 main

     9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND f

    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND g

No version information found in this file.
 
附件3
4.c.txt
 
[root@proxy ~/3]# gcc 1.o 3.o -o 4

[root@proxy ~/3]# ./4

hello World!

abc

[root@proxy ~/3]# objdump -dj .text 4

4:     file format elf32-i386
Disassembly of section .text:
08048360 <_start>:

 8048360:       31 ed                   xor    %ebp,%ebp

 8048362:       5e                        pop    %esi

 8048363:       89 e1                   mov    %esp,%ecx

 8048365:       83 e4 f0               and    $0xfffffff0,%esp

 8048368:       50                        push   %eax

 8048369:       54                        push   %esp

 804836a:       52                        push   %edx

 804836b:       68 f0 84 04 08     push   $0x80484f0

 8048370:       68 e4 82 04 08    push   $0x80482e4

 8048375:       51                        push   %ecx

 8048376:       56                        push   %esi

 8048377:       68 98 84 04 08    push   $0x8048498

 804837c:       e8 ab ff ff ff          call   804832c <_init+0x48>

 8048381:       f4                         hlt   

 8048382:       89 f6                   mov    %esi,%esi

08048384 <call_gmon_start>:

 8048384:       55                              push   %ebp

 8048385:       89 e5                         mov    %esp,%ebp

 8048387:       53                              push   %ebx

 8048388:       50                              push   %eax

 8048389:       e8 00 00 00 00          call   804838e <call_gmon_start+0xa>

 804838e:       5b                              pop    %ebx

 804838f:       81 c3 ca 11 00 00       add    $0x11ca,%ebx

 8048395:       8b 83 20 00 00 00     mov    0x20(%ebx),%eax

 804839b:       85 c0                          test   %eax,%eax

 804839d:       74 02                          je     80483a1 <call_gmon_start+0x1d>

 804839f:       ff d0                            call   *%eax

 80483a1:       8b 5d fc                      mov    0xfffffffc(%ebp),%ebx

 80483a4:       c9                               leave 

 80483a5:       c3                               ret   

 80483a6:       89 f6                           mov    %esi,%esi

 80483a8:       90                               nop   

 80483a9:       90                               nop   

 80483aa:       90                               nop   

 80483ab:       90                               nop   

 80483ac:       90                                nop   

 80483ad:       90                               nop   

 80483ae:       90                               nop   

 80483af:       90                                nop   

080483b0 <__do_global_dtors_aux>:

 80483b0:       55                            push   %ebp

 80483b1:       89 e5                       mov    %esp,%ebp

 80483b3:       83 ec 08                   sub    $0x8,%esp

 80483b6:       8b 15 38 95 04 08    mov    0x8049538,%edx

 80483bc:       85 d2                         test   %edx,%edx

 80483be:       75 49                         jne    8048409 <__do_global_dtors_aux+0x59>

 80483c0:       8b 15 34 95 04 08      mov    0x8049534,%edx

 80483c6:       8b 02                          mov    (%edx),%eax

 80483c8:       85 c0                          test   %eax,%eax

 80483ca:       74 1a                         je     80483e6 <__do_global_dtors_aux+0x36>

 80483cc:       8d 74 26 00               lea    0x0(%esi,1),%esi

 80483d0:       8d 42 04                   lea    0x4(%edx),%eax

 80483d3:       a3 34 95 04 08          mov    %eax,0x8049534

 80483d8:       ff 12                           call   *(%edx)

 80483da:       8b 15 34 95 04 08     mov    0x8049534,%edx

 80483e0:       8b 0a                         mov    (%edx),%ecx

 80483e2:       85 c9                          test   %ecx,%ecx

 80483e4:       75 ea                         jne    80483d0 <__do_global_dtors_aux+0x20>

 80483e6:       b8 1c 83 04 08          mov    $0x804831c,%eax

 80483eb:       85 c0                          test   %eax,%eax

 80483ed:       74 10                          je     80483ff <__do_global_dtors_aux+0x4f>

 80483ef:       83 ec 0c                      sub    $0xc,%esp

 80483f2:       68 44 95 04 08          push   $0x8049544

 80483f7:       e8 20 ff ff ff                call   804831c <_init+0x38>

 80483fc:       83 c4 10                     add    $0x10,%esp

 80483ff:       b8 01 00 00 00           mov    $0x1,%eax

 8048404:       a3 38 95 04 08          mov    %eax,0x8049538

 8048409:       89 ec                          mov    %ebp,%esp

 804840b:       5d                              pop    %ebp

 804840c:       c3                               ret   

 804840d:       8d 76 00                    lea    0x0(%esi),%esi

08048410 <fini_dummy>:

 8048410:       55                             push   %ebp

 8048411:       89 e5                        mov    %esp,%ebp

 8048413:       83 ec 08                   sub    $0x8,%esp

 8048416:       89 ec                        mov    %ebp,%esp

 8048418:       5d                             pop    %ebp

 8048419:       c3                              ret   

 804841a:       8d b6 00 00 00 00    lea    0x0(%esi),%esi

08048420 <frame_dummy>:

 8048420:       55                             push   %ebp

 8048421:       b8 0c 83 04 08          mov    $0x804830c,%eax

 8048426:       89 e5                         mov    %esp,%ebp

 8048428:       83 ec 08                    sub    $0x8,%esp

 804842b:       85 c0                         test   %eax,%eax

 804842d:       74 15                         je     8048444 <frame_dummy+0x24>

 804842f:       83 ec 08                     sub    $0x8,%esp

 8048432:       68 44 96 04 08          push   $0x8049644

 8048437:       68 44 95 04 08          push   $0x8049544

 804843c:       e8 cb fe ff ff                call   804830c <_init+0x28>

 8048441:       83 c4 10                     add    $0x10,%esp

 8048444:       89 ec                          mov    %ebp,%esp

 8048446:       5d                               pop    %ebp

 8048447:       c3                               ret   

 8048448:       90                               nop   

 8048449:       8d b4 26 00 00 00 00    lea    0x0(%esi,1),%esi

08048450 <init_dummy>:

 8048450:       55                               push   %ebp

 8048451:       89 e5                          mov    %esp,%ebp

 8048453:       83 ec 08                      sub    $0x8,%esp

 8048456:       89 ec                          mov    %ebp,%esp

 8048458:       5d                               pop    %ebp

 8048459:       c3                                ret   

 804845a:       8d b6 00 00 00 00       lea    0x0(%esi),%esi

08048460 <f>:

 8048460:       55                         push   %ebp

 8048461:       89 e5                    mov    %esp,%ebp

 8048463:       83 ec 08                sub    $0x8,%esp

 8048466:       83 ec 0c                 sub    $0xc,%esp

 8048469:       ff 35 3c 95 04 08    pushl  0x804953c

 804846f:       e8 c8 fe ff ff             call   804833c <_init+0x58>

 8048474:       83 c4 10                add    $0x10,%esp

 8048477:       c9                          leave 

 8048478:       c3                          ret   

 8048479:       8d 76 00                lea    0x0(%esi),%esi

0804847c <g>:

 804847c:       55                         push   %ebp

 804847d:       89 e5                   mov    %esp,%ebp

 804847f:       83 ec 08                sub    $0x8,%esp

 8048482:       83 ec 0c                sub    $0xc,%esp

 8048485:       ff 35 40 95 04 08  pushl  0x8049540

 804848b:       e8 ac fe ff ff          call   804833c <_init+0x58>

 8048490:       83 c4 10                add    $0x10,%esp

 8048493:       c9                          leave 

 8048494:       c3                          ret   

 8048495:       8d 76 00                lea    0x0(%esi),%esi

08048498 <main>:

 8048498:       55                        push   %ebp

 8048499:       89 e5                   mov    %esp,%ebp

 804849b:       83 ec 08               sub    $0x8,%esp

 804849e:       e8 bd ff ff ff          call   8048460 <f>

 80484a3:       e8 d4 ff ff ff          call   804847c <g>

 80484a8:       b8 00 00 00 00     mov    $0x0,%eax

 80484ad:       c9                         leave 

 80484ae:       c3                         ret   

 80484af:       90                          nop   

080484b0 <__do_global_ctors_aux>:

 80484b0:       55                      push   %ebp

 80484b1:       89 e5                   mov    %esp,%ebp

 80484b3:       53                      push   %ebx

 80484b4:       83 ec 04                sub    $0x4,%esp

 80484b7:       a1 48 95 04 08          mov    0x8049548,%eax

 80484bc:       bb 48 95 04 08          mov    $0x8049548,%ebx

 80484c1:       83 f8 ff                cmp    $0xffffffff,%eax

 80484c4:       74 16                   je     80484dc <__do_global_ctors_aux+0x2c>

 80484c6:       8d 76 00                lea    0x0(%esi),%esi

 80484c9:       8d bc 27 00 00 00 00    lea    0x0(%edi,1),%edi

 80484d0:       83 eb 04                sub    $0x4,%ebx

 80484d3:       ff d0                   call   *%eax

 80484d5:       8b 03                   mov    (%ebx),%eax

 80484d7:       83 f8 ff                cmp    $0xffffffff,%eax

 80484da:       75 f4                   jne    80484d0 <__do_global_ctors_aux+0x20>

 80484dc:       58                      pop    %eax

 80484dd:       5b                      pop    %ebx

 80484de:       5d                      pop    %ebp

 80484df:       c3                      ret   

080484e0 <init_dummy>:

 80484e0:       55                      push   %ebp

 80484e1:       89 e5                   mov    %esp,%ebp

 80484e3:       83 ec 08                sub    $0x8,%esp

 80484e6:       89 ec                   mov    %ebp,%esp

 80484e8:       5d                      pop    %ebp

 80484e9:       c3                      ret   

 80484ea:       8d b6 00 00 00 00       lea    0x0(%esi),%esi

 [root@proxy ~/3]# readelf -a 4

ELF Header:

  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

  Class:                             ELF32

  Data:                              2's complement, little endian

  Version:                           1 (current)

  OS/ABI:                            UNIX - System V

  ABI Version:                       0

  Type:                              EXEC (Executable file)

  Machine:                           Intel 80386

  Version:                           0x1

  Entry point address:               0x8048360

  Start of program headers:          52 (bytes into file)

  Start of section headers:          10844 (bytes into file)

  Flags:                             0x0

  Size of this header:               52 (bytes)

  Size of program headers:           32 (bytes)

  Number of program headers:         6

  Size of section headers:           40 (bytes)

  Number of section headers:         30

  Section header string table index: 27

Section Headers:

  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al

  [ 0]                   NULL            00000000 000000 000000 00      0   0  0

  [ 1] .interp           PROGBITS        080480f4 0000f4 000013 00   A  0   0  1

  [ 2] .note.ABI-tag     NOTE            08048108 000108 000020 00   A  0   0  4

  [ 3] .hash             HASH            08048128 000128 000034 04   A  4   0  4

  [ 4] .dynsym           DYNSYM          0804815c 00015c 000080 10   A  5   1  4

  [ 5] .dynstr           STRTAB          080481dc 0001dc 000095 00   A  0   0  1

  [ 6] .gnu.version      VERSYM          08048272 000272 000010 02   A  4   0  2

  [ 7] .gnu.version_r    VERNEED         08048284 000284 000030 00   A  5   1  4

  [ 8] .rel.dyn          REL             080482b4 0002b4 000008 08   A  4   0  4

  [ 9] .rel.plt          REL             080482bc 0002bc 000028 08   A  4   b  4

  [10] .init             PROGBITS        080482e4 0002e4 000018 00  AX  0   0  4

  [11] .plt              PROGBITS        080482fc 0002fc 000060 04  AX  0   0  4

  [12] .text             PROGBITS        08048360 000360 000190 00  AX  0   0 16

  [13] .fini             PROGBITS        080484f0 0004f0 00001e 00  AX  0   0  4

  [14] .rodata           PROGBITS        08048510 000510 00001b 00   A  0   0  4

  [15] .data             PROGBITS        0804952c 00052c 000018 00  WA  0   0  4

  [16] .eh_frame         PROGBITS        08049544 000544 000004 00  WA  0   0  4

  [17] .ctors            PROGBITS        08049548 000548 000008 00  WA  0   0  4

  [18] .dtors            PROGBITS        08049550 000550 000008 00  WA  0   0  4

  [19] .got              PROGBITS        08049558 000558 000024 04  WA  0   0  4

  [20] .dynamic          DYNAMIC         0804957c 00057c 0000c8 08  WA  5   0  4

  [21] .sbss             PROGBITS        08049644 000644 000000 00   W  0   0  1

  [22] .bss              NOBITS          08049644 000644 000018 00  WA  0   0  4

  [23] .stab             PROGBITS        00000000 000644 0007a4 0c     24   0  4

  [24] .stabstr          STRTAB          00000000 000de8 001983 00      0   0  1

  [25] .comment          PROGBITS        00000000 00276b 00017a 00      0   0  1

  [26] .note             NOTE            00000000 0028e5 00008c 00      0   0  1

  [27] .shstrtab         STRTAB          00000000 002971 0000e9 00      0   0  1

  [28] .symtab           SYMTAB          00000000 002f0c 000540 10     29  3d  4

  [29] .strtab           STRTAB          00000000 00344c 000234 00      0   0  1

Key to Flags:

  W (write), A (alloc), X (execute), M (merge), S (strings)

  I (info), L (link order), G (group), x (unknown)

  O (extra OS processing required) o (OS specific), p (processor specific)

Program Headers:

  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align

  PHDR           0x000034 0x08048034 0x08048034 0x000c0 0x000c0 R E 0x4

  INTERP         0x0000f4 0x080480f4 0x080480f4 0x00013 0x00013 R   0x1

      [Requesting program interpreter: /lib/ld-linux.so.2]

  LOAD           0x000000 0x08048000 0x08048000 0x0052b 0x0052b R E 0x1000

  LOAD           0x00052c 0x0804952c 0x0804952c 0x00118 0x00130 RW  0x1000

  DYNAMIC        0x00057c 0x0804957c 0x0804957c 0x000c8 0x000c8 RW  0x4

  NOTE           0x000108 0x08048108 0x08048108 0x00020 0x00020 R   0x4

 Section to Segment mapping:

  Segment Sections...

   00    

   01     .interp

  
02     .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version
.gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata

   03     .data .eh_frame .ctors .dtors .got .dynamic .bss

   04     .dynamic

   05     .note.ABI-tag

Dynamic segment at offset 0x57c contains 20 entries:

  Tag        Type                         Name/Value

 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

 0x0000000c (INIT)                       0x80482e4

 0x0000000d (FINI)                       0x80484f0

 0x00000004 (HASH)                       0x8048128

 0x00000005 (STRTAB)                     0x80481dc

 0x00000006 (SYMTAB)                     0x804815c

 0x0000000a (STRSZ)                      127 (bytes)

 0x0000000b (SYMENT)                     16 (bytes)

 0x00000015 (DEBUG)                      0x0

 0x00000003 (PLTGOT)                     0x8049558

 0x00000002 (PLTRELSZ)                   40 (bytes)

 0x00000014 (PLTREL)                     REL

 0x00000017 (JMPREL)                     0x80482bc

 0x00000011 (REL)                        0x80482b4

 0x00000012 (RELSZ)                      8 (bytes)

 0x00000013 (RELENT)                     8 (bytes)

 0x6ffffffe (VERNEED)                    0x8048284

 0x6fffffff (VERNEEDNUM)                 1

 0x6ffffff0 (VERSYM)                     0x8048272

 0x00000000 (NULL)                       0x0

Relocation section '.rel.dyn' at offset 0x2b4 contains 1 entries:

  Offset    Info  Type            Symbol's Value  Symbol's Name

  08049578  00706 R_386_GLOB_DAT        00000000  __gmon_start__          

Relocation section '.rel.plt' at offset 0x2bc contains 5 entries:

  Offset    Info  Type            Symbol's Value  Symbol's Name

  08049564  00107 R_386_JUMP_SLOT       0804830c  __register_frame_info   

  08049568  00207 R_386_JUMP_SLOT       0804831c  __deregister_frame_info 

  0804956c  00307 R_386_JUMP_SLOT       0804832c  __libc_start_main       

  08049570  00407 R_386_JUMP_SLOT       0804833c  printf                  

  08049574  00507 R_386_JUMP_SLOT       0804834c  __cxa_finalize          

There are no unwind sections in this file.
Symbol table '.dynsym' contains 8 entries:

   Num:    Value  Size Type    Bind   Vis      Ndx Name

     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND

     1: 0804830c   129 FUNC    WEAK   DEFAULT  UND __register_frame_info@GLIBC_2.0

(2)

     2: 0804831c   172 FUNC    WEAK   DEFAULT  UND __deregister_frame_info@GLIBC_2.0

(2)

     3: 0804832c   202 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0

(2)

     4: 0804833c    50 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.0

(2)

     5: 0804834c   157 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.1.3

(3)

     6: 08048514     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used

     7: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

Symbol table '.symtab' contains 84 entries:

   Num:    Value  Size Type    Bind   Vis      Ndx Name

     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND

     1: 080480f4     0 SECTION LOCAL  DEFAULT    1

     2: 08048108     0 SECTION LOCAL  DEFAULT    2

     3: 08048128     0 SECTION LOCAL  DEFAULT    3

     4: 0804815c     0 SECTION LOCAL  DEFAULT    4

     5: 080481dc     0 SECTION LOCAL  DEFAULT    5

     6: 08048272     0 SECTION LOCAL  DEFAULT    6

     7: 08048284     0 SECTION LOCAL  DEFAULT    7

     8: 080482b4     0 SECTION LOCAL  DEFAULT    8

     9: 080482bc     0 SECTION LOCAL  DEFAULT    9

    10: 080482e4     0 SECTION LOCAL  DEFAULT   10

    11: 080482fc     0 SECTION LOCAL  DEFAULT   11

    12: 08048360     0 SECTION LOCAL  DEFAULT   12

    13: 080484f0     0 SECTION LOCAL  DEFAULT   13

    14: 08048510     0 SECTION LOCAL  DEFAULT   14

    15: 0804952c     0 SECTION LOCAL  DEFAULT   15

    16: 08049544     0 SECTION LOCAL  DEFAULT   16

    17: 08049548     0 SECTION LOCAL  DEFAULT   17

    18: 08049550     0 SECTION LOCAL  DEFAULT   18

    19: 08049558     0 SECTION LOCAL  DEFAULT   19

    20: 0804957c     0 SECTION LOCAL  DEFAULT   20

    21: 08049644     0 SECTION LOCAL  DEFAULT   21

    22: 08049644     0 SECTION LOCAL  DEFAULT   22

    23: 00000000     0 SECTION LOCAL  DEFAULT   23

    24: 00000000     0 SECTION LOCAL  DEFAULT   24

    25: 00000000     0 SECTION LOCAL  DEFAULT   25

    26: 00000000     0 SECTION LOCAL  DEFAULT   26

    27: 00000000     0 SECTION LOCAL  DEFAULT   27

    28: 00000000     0 SECTION LOCAL  DEFAULT   28

    29: 00000000     0 SECTION LOCAL  DEFAULT   29

    30: 00000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c

    31: 08048384     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    32: 08048384     0 FUNC    LOCAL  DEFAULT   12 call_gmon_start

    33: 00000000     0 FILE    LOCAL  DEFAULT  ABS init.c

    34: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c

    35: 080483b0     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    36: 08049534     0 OBJECT  LOCAL  DEFAULT   15 p.0

    37: 08049550     0 OBJECT  LOCAL  DEFAULT   18 __DTOR_LIST__

    38: 08049538     0 OBJECT  LOCAL  DEFAULT   15 completed.1

    39: 080483b0     0 FUNC    LOCAL  DEFAULT   12 __do_global_dtors_aux

    40: 08049544     0 OBJECT  LOCAL  DEFAULT   16 __EH_FRAME_BEGIN__

    41: 08048410     0 FUNC    LOCAL  DEFAULT   12 fini_dummy

    42: 08049644    24 OBJECT  LOCAL  DEFAULT   22 object.2

    43: 08048420     0 FUNC    LOCAL  DEFAULT   12 frame_dummy

    44: 08048450     0 FUNC    LOCAL  DEFAULT   12 init_dummy

    45: 0804953c     0 OBJECT  LOCAL  DEFAULT   15 force_to_data

    46: 08049548     0 OBJECT  LOCAL  DEFAULT   17 __CTOR_LIST__

    47: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c

    48: 080484b0     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    49: 080484b0     0 FUNC    LOCAL  DEFAULT   12 __do_global_ctors_aux

    50: 0804954c     0 OBJECT  LOCAL  DEFAULT   17 __CTOR_END__

    51: 080484e0     0 FUNC    LOCAL  DEFAULT   12 init_dummy

    52: 08049544     0 OBJECT  LOCAL  DEFAULT   15 force_to_data

    53: 08049554     0 OBJECT  LOCAL  DEFAULT   18 __DTOR_END__

    54: 08049544     0 OBJECT  LOCAL  DEFAULT   16 __FRAME_END__

    55: 00000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c

    56: 080484f0     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    57: 00000000     0 FILE    LOCAL  DEFAULT  ABS 1.c

    58: 08048460     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    59: 00000000     0 FILE    LOCAL  DEFAULT  ABS 3.c

    60: 08048498     0 NOTYPE  LOCAL  DEFAULT   12 gcc2_compiled.

    61: 08049540     4 OBJECT  GLOBAL DEFAULT   15 t

    62: 0804957c     0 OBJECT  GLOBAL DEFAULT   20 _DYNAMIC

    63: 08048460    25 FUNC    GLOBAL DEFAULT   12 f

    64: 0804830c   129 FUNC    WEAK   DEFAULT  UND __register_frame_info@@GLIBC_2.0

    65: 08048510     4 NOTYPE  GLOBAL DEFAULT   14 _fp_hw

    66: 0804847c    25 FUNC    GLOBAL DEFAULT   12 g

    67: 080482e4     0 FUNC    GLOBAL DEFAULT   10 _init

    68: 0804831c   172 FUNC    WEAK   DEFAULT  UND __deregister_frame_info@@GLIBC_2.0

    69: 08048360     0 FUNC    GLOBAL DEFAULT   12 _start

    70: 08049644     0 OBJECT  GLOBAL DEFAULT  ABS __bss_start

    71: 08048498    23 FUNC    GLOBAL DEFAULT   12 main

    72: 0804832c   202 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_2.0

    73: 0804952c     0 NOTYPE  WEAK   DEFAULT   15 data_start

    74: 0804833c    50 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.0

    75: 080484f0     0 FUNC    GLOBAL DEFAULT   13 _fini

    76: 0804953c     4 OBJECT  GLOBAL DEFAULT   15 s

    77: 0804834c   157 FUNC    WEAK   DEFAULT  UND __cxa_finalize@@GLIBC_2.1.3

    78: 08049644     0 OBJECT  GLOBAL DEFAULT  ABS _edata

    79: 08049558     0 OBJECT  GLOBAL DEFAULT   19 _GLOBAL_OFFSET_TABLE_

    80: 0804965c     0 OBJECT  GLOBAL DEFAULT  ABS _end

    81: 08048514     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used

    82: 0804952c     0 NOTYPE  GLOBAL DEFAULT   15 __data_start

    83: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

Histogram for bucket list length (total of 3 buckets):

 Length  Number     % of total  Coverage

      0  0          (  0.0%)

      1  1          ( 33.3%)     14.3%

      2  0          (  0.0%)     14.3%

      3  2          ( 66.7%)    100.0%

Version symbols section '.gnu.version' contains 8 entries:

 Addr: 0000000008048272  Offset: 0x000272  Link: 4 (.dynsym)

  000:   0 (*local*)       2 (GLIBC_2.0)     2 (GLIBC_2.0)     2 (GLIBC_2.0) 

  004:   2 (GLIBC_2.0)     3 (GLIBC_2.1.3)   1 (*global*)      0 (*local*)   

Version needs section '.gnu.version_r' contains 1 entries:

 Addr: 0x0000000008048284  Offset: 0x000284  Link to section: 5 (.dynstr)

  000000: Version: 1  File: libc.so.6  Cnt: 2

  0x0010: Name: GLIBC_2.1.3  Flags: none  Version: 3

  0x0020: Name: GLIBC_2.0  Flags: none  Version: 2

 

补充:

注意1.c.txt中这个重定位节
Relocation section '.rel.data' at offset 0x42c contains 2 entries:
  Offset    Info  Type            Symbol's Value  Symbol's Name
  00000000  00601 R_386_32              00000000  .rodata                 
  00000004  00601 R_386_32              00000000  .rodata

分析一下可以发现,它也是用来重定位s和t的。这又是为什么呢?

原来,s指向的字符串是在.rodata节中,节被合并后,.rodata中数据的

地址也会变动,而s和t处的值正是指向.rodata的地址值,因此也需要重定位。

重定位方法同上。只不过有点不同.

006索引的符号是

     6: 00000000     0 SECTION LOCAL  DEFAULT    7

7节是

  [ 7] .rodata           PROGBITS        00000000 000088 000013 00   A  0   0  1

符号类型是SECTION,说明该符号和节相关,因此重定位时的S就是修正后.rodata的地址.

总结:
   当编译器编译1.c 生成 1.o文件时,是否需要生成 .rel.text 节,我觉得应该由 .text
   节中是否使用了需要重定位的符号决定:
全局变量符号,外部函数符号。 【这个需要实验一下: 局部变量 是否需要定位[根据x86的
函数局部变量都是放在堆栈中可知这是不需要定位的];对静态的局部变量呢?;对于全局的
静态变量呢? 对本编译单元内部中函数的调用是否需要定位】而且由于这种.rel.text 是链
接阶段的重定位,所以只有object文件中才有。

.rel.data 是否需要生成,应该由变量中是否使用了常量来决定。不管这个变量是全局变量
还是局部变量。这个还需要实验一下。

抱歉!评论已关闭.