可执行程序工作原理

2021-10-07  本文已影响0人  CurryCoder

1 ELF目标文件格式

2 ELF文件的3种类型(可执行文件格式中3种不同类型的目标文件)

3 ELF文件的作用

4 从源文件hello.c------>可执行文件hello的详细过程

[cdl@h3c temp]$ readelf -S hello.o
There are 13 section headers, starting at offset 0x138:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000000000  00000040
       0000000000000015  0000000000000000  AX       0     0     4
  [ 2] .rela.text        RELA             0000000000000000  00000598
       0000000000000030  0000000000000018          11     1     8
  [ 3] .data             PROGBITS         0000000000000000  00000058
       0000000000000000  0000000000000000  WA       0     0     4
  [ 4] .bss              NOBITS           0000000000000000  00000058
       0000000000000000  0000000000000000  WA       0     0     4
  [ 5] .rodata           PROGBITS         0000000000000000  00000058
       000000000000000c  0000000000000000   A       0     0     1
  [ 6] .comment          PROGBITS         0000000000000000  00000064
       000000000000002e  0000000000000001  MS       0     0     1
  [ 7] .note.GNU-stack   PROGBITS         0000000000000000  00000092
       0000000000000000  0000000000000000           0     0     1
  [ 8] .eh_frame         PROGBITS         0000000000000000  00000098
       0000000000000038  0000000000000000   A       0     0     8
  [ 9] .rela.eh_frame    RELA             0000000000000000  000005c8
       0000000000000018  0000000000000018          11     8     8
  [10] .shstrtab         STRTAB           0000000000000000  000000d0
       0000000000000061  0000000000000000           0     0     1
  [11] .symtab           SYMTAB           0000000000000000  00000478
       0000000000000108  0000000000000018          12     9     8
  [12] .strtab           STRTAB           0000000000000000  00000580
       0000000000000013  0000000000000000           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)
[cdl@h3c temp]$ readelf -S hello
There are 39 section headers, starting at offset 0x11c8:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000400200  00000200
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             000000000040021c  0000021c
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             000000000040023c  0000023c
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000400260  00000260
       000000000000001c  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           0000000000400280  00000280
       0000000000000060  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           00000000004002e0  000002e0
       000000000000003d  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           000000000040031e  0000031e
       0000000000000008  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          0000000000400328  00000328
       0000000000000020  0000000000000000   A       6     1     8
  [ 9] .rela.dyn         RELA             0000000000400348  00000348
       0000000000000018  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             0000000000400360  00000360
       0000000000000030  0000000000000018   A       5    12     8
  [11] .init             PROGBITS         0000000000400390  00000390
       0000000000000018  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         00000000004003a8  000003a8
       0000000000000030  0000000000000010  AX       0     0     4
  [13] .text             PROGBITS         00000000004003e0  000003e0
       00000000000001d8  0000000000000000  AX       0     0     16
  [14] .fini             PROGBITS         00000000004005b8  000005b8
       000000000000000e  0000000000000000  AX       0     0     4
  [15] .rodata           PROGBITS         00000000004005c8  000005c8
       000000000000001c  0000000000000000   A       0     0     8
  [16] .eh_frame_hdr     PROGBITS         00000000004005e4  000005e4
       0000000000000024  0000000000000000   A       0     0     4
  [17] .eh_frame         PROGBITS         0000000000400608  00000608
       000000000000007c  0000000000000000   A       0     0     8
  [18] .ctors            PROGBITS         0000000000600688  00000688
       0000000000000010  0000000000000000  WA       0     0     8
  [19] .dtors            PROGBITS         0000000000600698  00000698
       0000000000000010  0000000000000000  WA       0     0     8
  [20] .jcr              PROGBITS         00000000006006a8  000006a8
       0000000000000008  0000000000000000  WA       0     0     8
  [21] .dynamic          DYNAMIC          00000000006006b0  000006b0
       0000000000000190  0000000000000010  WA       6     0     8
  [22] .got              PROGBITS         0000000000600840  00000840
       0000000000000008  0000000000000008  WA       0     0     8
  [23] .got.plt          PROGBITS         0000000000600848  00000848
       0000000000000028  0000000000000008  WA       0     0     8
  [24] .data             PROGBITS         0000000000600870  00000870
       0000000000000004  0000000000000000  WA       0     0     4
  [25] .bss              NOBITS           0000000000600878  00000874
       0000000000000010  0000000000000000  WA       0     0     8
  [26] .comment          PROGBITS         0000000000000000  00000874
       000000000000002d  0000000000000001  MS       0     0     1
  [27] .debug_aranges    PROGBITS         0000000000000000  000008b0
       0000000000000060  0000000000000000           0     0     16
  [28] .debug_pubnames   PROGBITS         0000000000000000  00000910
       000000000000005f  0000000000000000           0     0     1
  [29] .debug_info       PROGBITS         0000000000000000  0000096f
       000000000000023b  0000000000000000           0     0     1
  [30] .debug_abbrev     PROGBITS         0000000000000000  00000baa
       000000000000012a  0000000000000000           0     0     1
  [31] .debug_line       PROGBITS         0000000000000000  00000cd4
       0000000000000120  0000000000000000           0     0     1
  [32] .debug_str        PROGBITS         0000000000000000  00000df4
       0000000000000100  0000000000000001  MS       0     0     1
  [33] .debug_loc        PROGBITS         0000000000000000  00000ef4
       00000000000000d9  0000000000000000           0     0     1
  [34] .debug_pubtypes   PROGBITS         0000000000000000  00000fcd
       000000000000002f  0000000000000000           0     0     1
  [35] .debug_ranges     PROGBITS         0000000000000000  00000ffc
       0000000000000050  0000000000000000           0     0     1
  [36] .shstrtab         STRTAB           0000000000000000  0000104c
       0000000000000177  0000000000000000           0     0     1
  [37] .symtab           SYMTAB           0000000000000000  00001b88
       0000000000000738  0000000000000018          38    59     8
  [38] .strtab           STRTAB           0000000000000000  000022c0
       000000000000020e  0000000000000000           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)
[cdl@h3c temp]$ readelf -l hello

Elf file type is EXEC (Executable file)
Entry point 0x4003e0
There are 8 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001c0 0x00000000000001c0  R E    8
  INTERP         0x0000000000000200 0x0000000000400200 0x0000000000400200
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000000684 0x0000000000000684  R E    200000
  LOAD           0x0000000000000688 0x0000000000600688 0x0000000000600688
                 0x00000000000001ec 0x0000000000000200  RW     200000
  DYNAMIC        0x00000000000006b0 0x00000000006006b0 0x00000000006006b0
                 0x0000000000000190 0x0000000000000190  RW     8
  NOTE           0x000000000000021c 0x000000000040021c 0x000000000040021c
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000005e4 0x00000000004005e4 0x00000000004005e4
                 0x0000000000000024 0x0000000000000024  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     

5 链接与库

[cdl@h3c temp]$ readelf -s hello.o

Symbol table '.symtab' contains 11 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS hello.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    8 
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
     9: 0000000000000000    21 FUNC    GLOBAL DEFAULT    1 main
    10: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND puts
[cdl@h3c temp]$ readelf -s hello

Symbol table '.dynsym' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)

Symbol table '.symtab' contains 77 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000400200     0 SECTION LOCAL  DEFAULT    1 
     2: 000000000040021c     0 SECTION LOCAL  DEFAULT    2 
     3: 000000000040023c     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000400260     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000400280     0 SECTION LOCAL  DEFAULT    5 
     6: 00000000004002e0     0 SECTION LOCAL  DEFAULT    6 
     7: 000000000040031e     0 SECTION LOCAL  DEFAULT    7 
     8: 0000000000400328     0 SECTION LOCAL  DEFAULT    8 
     9: 0000000000400348     0 SECTION LOCAL  DEFAULT    9 
    10: 0000000000400360     0 SECTION LOCAL  DEFAULT   10 
    11: 0000000000400390     0 SECTION LOCAL  DEFAULT   11 
    12: 00000000004003a8     0 SECTION LOCAL  DEFAULT   12 
    13: 00000000004003e0     0 SECTION LOCAL  DEFAULT   13 
    14: 00000000004005b8     0 SECTION LOCAL  DEFAULT   14 
    15: 00000000004005c8     0 SECTION LOCAL  DEFAULT   15 
    16: 00000000004005e4     0 SECTION LOCAL  DEFAULT   16 
    17: 0000000000400608     0 SECTION LOCAL  DEFAULT   17 
    18: 0000000000600688     0 SECTION LOCAL  DEFAULT   18 
    19: 0000000000600698     0 SECTION LOCAL  DEFAULT   19 
    20: 00000000006006a8     0 SECTION LOCAL  DEFAULT   20 
    21: 00000000006006b0     0 SECTION LOCAL  DEFAULT   21 
    22: 0000000000600840     0 SECTION LOCAL  DEFAULT   22 
    23: 0000000000600848     0 SECTION LOCAL  DEFAULT   23 
    24: 0000000000600870     0 SECTION LOCAL  DEFAULT   24 
    25: 0000000000600878     0 SECTION LOCAL  DEFAULT   25 
    26: 0000000000000000     0 SECTION LOCAL  DEFAULT   26 
    27: 0000000000000000     0 SECTION LOCAL  DEFAULT   27 
    28: 0000000000000000     0 SECTION LOCAL  DEFAULT   28 
    29: 0000000000000000     0 SECTION LOCAL  DEFAULT   29 
    30: 0000000000000000     0 SECTION LOCAL  DEFAULT   30 
    31: 0000000000000000     0 SECTION LOCAL  DEFAULT   31 
    32: 0000000000000000     0 SECTION LOCAL  DEFAULT   32 
    33: 0000000000000000     0 SECTION LOCAL  DEFAULT   33 
    34: 0000000000000000     0 SECTION LOCAL  DEFAULT   34 
    35: 0000000000000000     0 SECTION LOCAL  DEFAULT   35 
    36: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS init.c
    37: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c
    38: 000000000040040c     0 FUNC    LOCAL  DEFAULT   13 call_gmon_start
    39: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    40: 0000000000600688     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_LIST__
    41: 0000000000600698     0 OBJECT  LOCAL  DEFAULT   19 __DTOR_LIST__
    42: 00000000006006a8     0 OBJECT  LOCAL  DEFAULT   20 __JCR_LIST__
    43: 0000000000400430     0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux
    44: 0000000000600878     1 OBJECT  LOCAL  DEFAULT   25 completed.6364
    45: 0000000000600880     8 OBJECT  LOCAL  DEFAULT   25 dtor_idx.6366
    46: 00000000004004a0     0 FUNC    LOCAL  DEFAULT   13 frame_dummy
    47: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    48: 0000000000600690     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_END__
    49: 0000000000400680     0 OBJECT  LOCAL  DEFAULT   17 __FRAME_END__
    50: 00000000006006a8     0 OBJECT  LOCAL  DEFAULT   20 __JCR_END__
    51: 0000000000400580     0 FUNC    LOCAL  DEFAULT   13 __do_global_ctors_aux
    52: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c
    53: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS hello.c
    54: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS elf-init.c
    55: 0000000000600848     0 OBJECT  LOCAL  DEFAULT   23 _GLOBAL_OFFSET_TABLE_
    56: 0000000000600684     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_end
    57: 0000000000600684     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_start
    58: 00000000006006b0     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
    59: 0000000000600870     0 NOTYPE  WEAK   DEFAULT   24 data_start
    60: 00000000004004e0     2 FUNC    GLOBAL DEFAULT   13 __libc_csu_fini
    61: 00000000004003e0     0 FUNC    GLOBAL DEFAULT   13 _start
    62: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    63: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    64: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.2.5
    65: 00000000004005b8     0 FUNC    GLOBAL DEFAULT   14 _fini
    66: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
    67: 00000000004005c8     4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used
    68: 0000000000600870     0 NOTYPE  GLOBAL DEFAULT   24 __data_start
    69: 00000000004005d0     0 OBJECT  GLOBAL HIDDEN    15 __dso_handle
    70: 00000000006006a0     0 OBJECT  GLOBAL HIDDEN    19 __DTOR_END__
    71: 00000000004004f0   137 FUNC    GLOBAL DEFAULT   13 __libc_csu_init
    72: 0000000000600874     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    73: 0000000000600888     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    74: 0000000000600874     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    75: 00000000004004c4    21 FUNC    GLOBAL DEFAULT   13 main
    76: 0000000000400390     0 FUNC    GLOBAL DEFAULT   11 _init
[cdl@h3c temp]$ readelf -r hello.o

Relocation section '.rela.text' at offset 0x598 contains 2 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000005  00050000000a R_X86_64_32       0000000000000000 .rodata + 0
00000000000a  000a00000002 R_X86_64_PC32     0000000000000000 puts - 4

Relocation section '.rela.eh_frame' at offset 0x5c8 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
[cdl@h3c temp]$ objdump -d hello.o

hello.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   bf 00 00 00 00          mov    $0x0,%edi
   9:   e8 00 00 00 00          callq  e <main+0xe>
   e:   b8 00 00 00 00          mov    $0x0,%eax
  13:   c9                      leaveq 
  14:   c3                      retq

6 ELF常用命令

上一篇下一篇

猜你喜欢

热点阅读