elf 就是一个文件格式。 可以被操作系统识别并执行。把一个elf格式文件可以简单的理解为一本书

 

elf  的 header 相当于 目录索引。 我将它称为称为 ELF_Header,  头部大小为 e_ehsize

elf 的 segment 相当于 书中的章

elf 的 section 相当于书中的节。

section header 相当于节的索引。 我将它称为 SH_Header

segment header 相当于 章的索引,我将它称为 PH_Header. 

 

存放SH_Header的表, 我将它成为SH_Table.

根据这三个值 e_shoff, e_shentsize,e_shnum 可以拿到secion表

 

存放PH_Header的表, 我将它成为PH_Table.

根据这三个值 e_phoff, e_phentsize,e_phnum 可以拿到segment表

 

总结:

一个或多个section组成一个segment

一个 ELF_Header 加上 一个或多个segment组成一个elf文件。

 

有一个命令行工具readelf. 可以查看elf一些信息:

查看ELF_Header:

➜  android-ndk-r16b readelf -h /Volumes/dzqExt/source/public/android_cmd/crack/obj/local/armeabi-v7a/libcocos2djs.so
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:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          143852 (bytes into file)
  Flags:                             0x5000200, Version5 EABI, soft-float ABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         8
  Size of section headers:           40 (bytes)
  Number of section headers:         27
  Section header string table index: 26

 

查看所有PH_Header:

➜  android-ndk-r16b readelf --segments /Volumes/dzqExt/source/public/android_cmd/crack/obj/local/armeabi-v7a/libcocos2djs.so           

Elf file type is DYN (Shared object file)
Entry point 0x0
There are 8 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x00100 0x00100 R   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x218a7 0x218a7 R E 0x1000
  LOAD           0x0224e8 0x000234e8 0x000234e8 0x00b2c 0x00dc9 RW  0x1000
  DYNAMIC        0x022ce4 0x00023ce4 0x00023ce4 0x00118 0x00118 RW  0x4
  NOTE           0x000134 0x00000134 0x00000134 0x000bc 0x000bc R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  EXIDX          0x01ff94 0x0001ff94 0x0001ff94 0x00728 0x00728 R   0x4
  GNU_RELRO      0x0224e8 0x000234e8 0x000234e8 0x00b18 0x00b18 RW  0x4

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .note.android.ident .note.gnu.build-id .dynsym .dynstr .hash .gnu.version .gnu.version_d .gnu.version_r .rel.dyn .rel.plt .plt .text .ARM.extab .ARM.exidx .rodata 
   02     .fini_array .data.rel.ro .init_array .dynamic .got .data .bss 
   03     .dynamic 
   04     .note.android.ident .note.gnu.build-id 
   05     
   06     .ARM.exidx 
   07     .fini_array .data.rel.ro .init_array .dynamic .got 

  

查看所有的SH_header

➜  android-ndk-r16b readelf --sections /Volumes/dzqExt/source/public/android_cmd/crack/obj/local/armeabi-v7a/libcocos2djs.so
There are 27 section headers, starting at offset 0x231ec:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .note.android.ide NOTE            00000134 000134 000098 00   A  0   0  4
  [ 2] .note.gnu.build-i NOTE            000001cc 0001cc 000024 00   A  0   0  4
  [ 3] .dynsym           DYNSYM          000001f0 0001f0 0016f0 10   A  4   1  4
  [ 4] .dynstr           STRTAB          000018e0 0018e0 001c44 00   A  0   0  1
  [ 5] .hash             HASH            00003524 003524 0009e0 04   A  3   0  4
  [ 6] .gnu.version      VERSYM          00003f04 003f04 0002de 02   A  3   0  2
  [ 7] .gnu.version_d    VERDEF          000041e4 0041e4 00001c 00   A  4   1  4
  [ 8] .gnu.version_r    VERNEED         00004200 004200 000050 00   A  4   2  4
  [ 9] .rel.dyn          REL             00004250 004250 000ea8 08   A  3   0  4
  [10] .rel.plt          REL             000050f8 0050f8 000308 08  AI  3  20  4
  [11] .plt              PROGBITS        00005400 005400 0004a0 00  AX  0   0  4
  [12] .text             PROGBITS        000058a0 0058a0 017c80 00  AX  0   0  4
  [13] .ARM.extab        PROGBITS        0001d520 01d520 002a74 00   A  0   0  4
  [14] .ARM.exidx        ARM_EXIDX       0001ff94 01ff94 000728 08  AL 12   0  4
  [15] .rodata           PROGBITS        000206c0 0206c0 0011e7 00   A  0   0 16
  [16] .fini_array       FINI_ARRAY      000234e8 0224e8 000008 04  WA  0   0  4
  [17] .data.rel.ro      PROGBITS        000234f0 0224f0 0007f0 00  WA  0   0  4
  [18] .init_array       INIT_ARRAY      00023ce0 022ce0 000004 04  WA  0   0  1
  [19] .dynamic          DYNAMIC         00023ce4 022ce4 000118 08  WA  4   0  4
  [20] .got              PROGBITS        00023dfc 022dfc 000204 00  WA  0   0  4
  [21] .data             PROGBITS        00024000 023000 000014 00  WA  0   0  4
  [22] .bss              NOBITS          00024020 023014 000291 00  WA  0   0 16
  [23] .comment          PROGBITS        00000000 023014 000065 01  MS  0   0  1
  [24] .note.gnu.gold-ve NOTE            00000000 02307c 00001c 00      0   0  4
  [25] .ARM.attributes   ARM_ATTRIBUTES  00000000 023098 00003b 00      0   0  1
  [26] .shstrtab         STRTAB          00000000 0230d3 000117 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  y (noread), p (processor specific)

 

ELF_Header, PH_Header, SH_Header 一起显示

readelf -e  xxx.so