链接与装载---elf文件示例

制作elf文件: 使用demo.c编译得到。

  代码demo.c: 一个非常简单的程序

#include <stdio.h>

int func1()
{
	printf("func1\n");
	return 0;
}

int func2()
{
	printf("func2\n");
	return 0;
}

int main()
{
	func1();
	func2();
	return 0;
}

编译

gcc demo.c 

编译后得到可执行elf文件:a.out

执行readelf命令,查看elf文件信息:readelf -a a.out

# readelf -a a.out
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:               0x8048310
  Start of program headers:          52 (bytes into file)
  Start of section headers:          6152 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         31
  Section header string table index: 28

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        08048154 000154 000013 00   A  0   0  1
  [ 2] .note.ABI-tag     NOTE            08048168 000168 000020 00   A  0   0  4
  [ 3] .note.gnu.build-i NOTE            08048188 000188 000024 00   A  0   0  4
  [ 4] .gnu.hash         GNU_HASH        080481ac 0001ac 000020 04   A  5   0  4
  [ 5] .dynsym           DYNSYM          080481cc 0001cc 000050 10   A  6   1  4
  [ 6] .dynstr           STRTAB          0804821c 00021c 00004a 00   A  0   0  1
  [ 7] .gnu.version      VERSYM          08048266 000266 00000a 02   A  5   0  2
  [ 8] .gnu.version_r    VERNEED         08048270 000270 000020 00   A  6   1  4
  [ 9] .rel.dyn          REL             08048290 000290 000008 08   A  5   0  4
  [10] .rel.plt          REL             08048298 000298 000010 08  AI  5  24  4
  [11] .init             PROGBITS        080482a8 0002a8 000023 00  AX  0   0  4
  [12] .plt              PROGBITS        080482d0 0002d0 000030 04  AX  0   0 16
  [13] .plt.got          PROGBITS        08048300 000300 000008 00  AX  0   0  8
  [14] .text             PROGBITS        08048310 000310 0001c2 00  AX  0   0 16
  [15] .fini             PROGBITS        080484d4 0004d4 000014 00  AX  0   0  4
  [16] .rodata           PROGBITS        080484e8 0004e8 000014 00   A  0   0  4
  [17] .eh_frame_hdr     PROGBITS        080484fc 0004fc 00003c 00   A  0   0  4
  [18] .eh_frame         PROGBITS        08048538 000538 00010c 00   A  0   0  4
  [19] .init_array       INIT_ARRAY      08049f08 000f08 000004 00  WA  0   0  4
  [20] .fini_array       FINI_ARRAY      08049f0c 000f0c 000004 00  WA  0   0  4
  [21] .jcr              PROGBITS        08049f10 000f10 000004 00  WA  0   0  4
  [22] .dynamic          DYNAMIC         08049f14 000f14 0000e8 08  WA  6   0  4
  [23] .got              PROGBITS        08049ffc 000ffc 000004 04  WA  0   0  4
  [24] .got.plt          PROGBITS        0804a000 001000 000014 04  WA  0   0  4
  [25] .data             PROGBITS        0804a014 001014 000008 00  WA  0   0  4
  [26] .bss              NOBITS          0804a01c 00101c 000004 00  WA  0   0  1
  [27] .comment          PROGBITS        00000000 00101c 000035 01  MS  0   0  1
  [28] .shstrtab         STRTAB          00000000 0016fc 00010a 00      0   0  1
  [29] .symtab           SYMTAB          00000000 001054 000470 10     30  47  4
  [30] .strtab           STRTAB          00000000 0014c4 000238 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4
  INTERP         0x000154 0x08048154 0x08048154 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]
  LOAD           0x000000 0x08048000 0x08048000 0x00644 0x00644 R E 0x1000
  LOAD           0x000f08 0x08049f08 0x08049f08 0x00114 0x00118 RW  0x1000
  DYNAMIC        0x000f14 0x08049f14 0x08049f14 0x000e8 0x000e8 RW  0x4
  NOTE           0x000168 0x08048168 0x08048168 0x00044 0x00044 R   0x4
  GNU_EH_FRAME   0x0004fc 0x080484fc 0x080484fc 0x0003c 0x0003c R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x000f08 0x08049f08 0x08049f08 0x000f8 0x000f8 R   0x1

 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 .rel.dyn .rel.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .init_array .fini_array .jcr .dynamic .got 

Dynamic section at offset 0xf14 contains 24 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x0000000c (INIT)                       0x80482a8
 0x0000000d (FINI)                       0x80484d4
 0x00000019 (INIT_ARRAY)                 0x8049f08
 0x0000001b (INIT_ARRAYSZ)               4 (bytes)
 0x0000001a (FINI_ARRAY)                 0x8049f0c
 0x0000001c (FINI_ARRAYSZ)               4 (bytes)
 0x6ffffef5 (GNU_HASH)                   0x80481ac
 0x00000005 (STRTAB)                     0x804821c
 0x00000006 (SYMTAB)                     0x80481cc
 0x0000000a (STRSZ)                      74 (bytes)
 0x0000000b (SYMENT)                     16 (bytes)
 0x00000015 (DEBUG)                      0x0
 0x00000003 (PLTGOT)                     0x804a000
 0x00000002 (PLTRELSZ)                   16 (bytes)
 0x00000014 (PLTREL)                     REL
 0x00000017 (JMPREL)                     0x8048298
 0x00000011 (REL)                        0x8048290
 0x00000012 (RELSZ)                      8 (bytes)
 0x00000013 (RELENT)                     8 (bytes)
 0x6ffffffe (VERNEED)                    0x8048270
 0x6fffffff (VERNEEDNUM)                 1
 0x6ffffff0 (VERSYM)                     0x8048266
 0x00000000 (NULL)                       0x0

Relocation section '.rel.dyn' at offset 0x290 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049ffc  00000206 R_386_GLOB_DAT    00000000   __gmon_start__

Relocation section '.rel.plt' at offset 0x298 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0804a00c  00000107 R_386_JUMP_SLOT   00000000   puts@GLIBC_2.0
0804a010  00000307 R_386_JUMP_SLOT   00000000   __libc_start_main@GLIBC_2.0

The decoding of unwind sections for machine type Intel 80386 is not currently supported.

Symbol table '.dynsym' contains 5 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)
     2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0 (2)
     4: 080484ec     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used

Symbol table '.symtab' contains 71 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 08048154     0 SECTION LOCAL  DEFAULT    1 
     2: 08048168     0 SECTION LOCAL  DEFAULT    2 
     3: 08048188     0 SECTION LOCAL  DEFAULT    3 
     4: 080481ac     0 SECTION LOCAL  DEFAULT    4 
     5: 080481cc     0 SECTION LOCAL  DEFAULT    5 
     6: 0804821c     0 SECTION LOCAL  DEFAULT    6 
     7: 08048266     0 SECTION LOCAL  DEFAULT    7 
     8: 08048270     0 SECTION LOCAL  DEFAULT    8 
     9: 08048290     0 SECTION LOCAL  DEFAULT    9 
    10: 08048298     0 SECTION LOCAL  DEFAULT   10 
    11: 080482a8     0 SECTION LOCAL  DEFAULT   11 
    12: 080482d0     0 SECTION LOCAL  DEFAULT   12 
    13: 08048300     0 SECTION LOCAL  DEFAULT   13 
    14: 08048310     0 SECTION LOCAL  DEFAULT   14 
    15: 080484d4     0 SECTION LOCAL  DEFAULT   15 
    16: 080484e8     0 SECTION LOCAL  DEFAULT   16 
    17: 080484fc     0 SECTION LOCAL  DEFAULT   17 
    18: 08048538     0 SECTION LOCAL  DEFAULT   18 
    19: 08049f08     0 SECTION LOCAL  DEFAULT   19 
    20: 08049f0c     0 SECTION LOCAL  DEFAULT   20 
    21: 08049f10     0 SECTION LOCAL  DEFAULT   21 
    22: 08049f14     0 SECTION LOCAL  DEFAULT   22 
    23: 08049ffc     0 SECTION LOCAL  DEFAULT   23 
    24: 0804a000     0 SECTION LOCAL  DEFAULT   24 
    25: 0804a014     0 SECTION LOCAL  DEFAULT   25 
    26: 0804a01c     0 SECTION LOCAL  DEFAULT   26 
    27: 00000000     0 SECTION LOCAL  DEFAULT   27 
    28: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    29: 08049f10     0 OBJECT  LOCAL  DEFAULT   21 __JCR_LIST__
    30: 08048350     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones
    31: 08048380     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones
    32: 080483c0     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux
    33: 0804a01c     1 OBJECT  LOCAL  DEFAULT   26 completed.7209
    34: 08049f0c     0 OBJECT  LOCAL  DEFAULT   20 __do_global_dtors_aux_fin
    35: 080483e0     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
    36: 08049f08     0 OBJECT  LOCAL  DEFAULT   19 __frame_dummy_init_array_
    37: 00000000     0 FILE    LOCAL  DEFAULT  ABS demo.c
    38: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    39: 08048640     0 OBJECT  LOCAL  DEFAULT   18 __FRAME_END__
    40: 08049f10     0 OBJECT  LOCAL  DEFAULT   21 __JCR_END__
    41: 00000000     0 FILE    LOCAL  DEFAULT  ABS 
    42: 08049f0c     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_end
    43: 08049f14     0 OBJECT  LOCAL  DEFAULT   22 _DYNAMIC
    44: 08049f08     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start
    45: 080484fc     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR
    46: 0804a000     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_
    47: 080484d0     2 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
    48: 0804840b    29 FUNC    GLOBAL DEFAULT   14 func1
    49: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
    50: 08048340     4 FUNC    GLOBAL HIDDEN    14 __x86.get_pc_thunk.bx
    51: 0804a014     0 NOTYPE  WEAK   DEFAULT   25 data_start
    52: 0804a01c     0 NOTYPE  GLOBAL DEFAULT   25 _edata
    53: 080484d4     0 FUNC    GLOBAL DEFAULT   15 _fini
    54: 0804a014     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
    55: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.0
    56: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    57: 0804a018     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle
    58: 080484ec     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
    59: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
    60: 08048428    29 FUNC    GLOBAL DEFAULT   14 func2
    61: 08048470    93 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
    62: 0804a020     0 NOTYPE  GLOBAL DEFAULT   26 _end
    63: 08048310     0 FUNC    GLOBAL DEFAULT   14 _start
    64: 080484e8     4 OBJECT  GLOBAL DEFAULT   16 _fp_hw
    65: 0804a01c     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start
    66: 08048445    41 FUNC    GLOBAL DEFAULT   14 main
    67: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    68: 0804a01c     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__
    69: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
    70: 080482a8     0 FUNC    GLOBAL DEFAULT   11 _init

Histogram for `.gnu.hash' bucket list length (total of 2 buckets):
 Length  Number     % of total  Coverage
      0  1          ( 50.0%)
      1  1          ( 50.0%)    100.0%

Version symbols section '.gnu.version' contains 5 entries:
 Addr: 0000000008048266  Offset: 0x000266  Link: 5 (.dynsym)
  000:   0 (*local*)       2 (GLIBC_2.0)     0 (*local*)       2 (GLIBC_2.0)  
  004:   1 (*global*)   

Version needs section '.gnu.version_r' contains 1 entries:
 Addr: 0x0000000008048270  Offset: 0x000270  Link: 6 (.dynstr)
  000000: Version: 1  File: libc.so.6  Cnt: 1
  0x0010:   Name: GLIBC_2.0  Flags: none  Version: 2

Displaying notes found at file offset 0x00000168 with length 0x00000020:
  Owner                 Data size	Description
  GNU                  0x00000010	NT_GNU_ABI_TAG (ABI version tag)
    OS: Linux, ABI: 2.6.32

Displaying notes found at file offset 0x00000188 with length 0x00000024:
  Owner                 Data size	Description
  GNU                  0x00000014	NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: 005e4701f52fa16cdd1db02642fdfc12b6a8ce7c

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值