共享库的结构
##linux:
ELF, Executable and Linking Format, 是一种用于可执行文件、目标文件、共享库和核心转储的标准文件格式。 ELF格式是是UNIX系统实验室作为ABI(Application Binary Interface)而开发和发布的。
这里简单介绍一下相关历史:
- UNIX: 最初采用的格式为a.out,之后被System V中的COFF取代,最后则被SVR4中的ELF格式所取代。
- Windows: 采用的则是COFF格式的变种PE格式
- MAC OS X: 采用的是Mach-O格式
ELF有四种不同的类型:
- 可重定位文件(Relocatable): 编译器和汇编器产生的.o文件,需要被Linker进一步处理
- 可执行文件(Executable): Have all relocation done and all symbol resolved except perhaps shared library symbols that must be resolved at run time
- 共享对象文件(Shared Object): 即动态库文件(.so)
- 核心转储文件(Core File):
TIP:
- A single segment usually consist of several sections.
- Relocatable files have Section header tables. Executable files have Program header tables. Shared object files have both
- Sections are intended for further processing by a linker, while the segments are intended to be mapped into memory
- 只有ELF header是固定在文件的首部, 而Program header和Section header的位置则由ELF header指出
ELF数据表示: 六种数据类型(32-bit)
Name | Size | Alignment | Purpose |
#define EI_NIDENT (16)
typedef struct
{
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
[root@VM_0_9_centos dynamic_lib_text]# ls
add.c add.h add.o add.s a.out libadd.so main.c test
[root@VM_0_9_centos dynamic_lib_text]# readelf -h a.out
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x4008e0
Start of program headers: 64 (bytes into file)
Start of section headers: 7304 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 30
Section header string table index: 27
[root@VM_0_9_centos dynamic_lib_text]#
typedef struct
{
elf32_word sh_name; /* Section name (string tbl index) */
elf32_word sh_type; /* Section type */
elf32_word sh_flags; /* Section flags */
elf32_addr sh_addr; /* Section virtual addr at execution */
elf32_off sh_offset; /* Section file offset */
elf32_word sh_size; /* Section size in bytes */
elf32_word sh_link; /* Link to another section */
elf32_word sh_info; /* Additional section information */
elf32_word sh_addralign; /* Section alignment */
elf32_word sh_entsize; /* Entry size if section holds table */
} elf32_shdr;
[root@VM_0_9_centos dynamic_lib_text]# readelf -s a.out
Symbol table '.dynsym' contains 20 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEi@GLIBCXX_3.4 (2)
2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
3: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitC1Ev@GLIBCXX_3.4 (2)
5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (3)
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit@GLIBC_2.2.5 (3)
7: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab
8: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsISt11char_traitsIcE@GLIBCXX_3.4 (2)
9: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
10: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Z3addii
11: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEPFRSoS_E@GLIBCXX_3.4 (2)
12: 0000000000601198 0 NOTYPE GLOBAL DEFAULT 25 _end
13: 000000000060106c 0 NOTYPE GLOBAL DEFAULT 24 _edata
14: 000000000060106c 0 NOTYPE GLOBAL DEFAULT 25 __bss_start
15: 00000000004008d0 0 FUNC GLOBAL DEFAULT UND _ZSt4endlIcSt11char_trait@GLIBCXX_3.4 (2)
16: 0000000000400810 0 FUNC GLOBAL DEFAULT 11 _init
17: 0000000000400ae4 0 FUNC GLOBAL DEFAULT 14 _fini
18: 0000000000400890 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitD1Ev@GLIBCXX_3.4 (2)
19: 0000000000601080 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cout@GLIBCXX_3.4 (2)
Symbol table '.symtab' contains 76 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000400238 0 SECTION LOCAL DEFAULT 1
2: 0000000000400254 0 SECTION LOCAL DEFAULT 2
3: 0000000000400274 0 SECTION LOCAL DEFAULT 3
4: 0000000000400298 0 SECTION LOCAL DEFAULT 4
5: 00000000004002e0 0 SECTION LOCAL DEFAULT 5
6: 00000000004004c0 0 SECTION LOCAL DEFAULT 6
7: 0000000000400686 0 SECTION LOCAL DEFAULT 7
8: 00000000004006b0 0 SECTION LOCAL DEFAULT 8
9: 00000000004006f0 0 SECTION LOCAL DEFAULT 9
10: 0000000000400720 0 SECTION LOCAL DEFAULT 10
11: 0000000000400810 0 SECTION LOCAL DEFAULT 11
12: 0000000000400830 0 SECTION LOCAL DEFAULT 12
13: 00000000004008e0 0 SECTION LOCAL DEFAULT 13
14: 0000000000400ae4 0 SECTION LOCAL DEFAULT 14
15: 0000000000400af0 0 SECTION LOCAL DEFAULT 15
16: 0000000000400b1c 0 SECTION LOCAL DEFAULT 16
17: 0000000000400b60 0 SECTION LOCAL DEFAULT 17
18: 0000000000600db8 0 SECTION LOCAL DEFAULT 18
19: 0000000000600dc8 0 SECTION LOCAL DEFAULT 19
20: 0000000000600dd0 0 SECTION LOCAL DEFAULT 20
21: 0000000000600dd8 0 SECTION LOCAL DEFAULT 21
22: 0000000000600ff8 0 SECTION LOCAL DEFAULT 22
23: 0000000000601000 0 SECTION LOCAL DEFAULT 23
24: 0000000000601068 0 SECTION LOCAL DEFAULT 24
25: 0000000000601080 0 SECTION LOCAL DEFAULT 25
26: 0000000000000000 0 SECTION LOCAL DEFAULT 26
27: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
28: 0000000000600dd0 0 OBJECT LOCAL DEFAULT 20 __JCR_LIST__
29: 0000000000400910 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones
30: 0000000000400940 0 FUNC LOCAL DEFAULT 13 register_tm_clones
31: 0000000000400980 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux
32: 0000000000601190 1 OBJECT LOCAL DEFAULT 25 completed.6355
33: 0000000000600dc8 0 OBJECT LOCAL DEFAULT 19 __do_global_dtors_aux_fin
34: 00000000004009a0 0 FUNC LOCAL DEFAULT 13 frame_dummy
35: 0000000000600db8 0 OBJECT LOCAL DEFAULT 18 __frame_dummy_init_array_
36: 0000000000000000 0 FILE LOCAL DEFAULT ABS main.c
37: 0000000000601191 1 OBJECT LOCAL DEFAULT 25 _ZStL8__ioinit
38: 0000000000400a17 61 FUNC LOCAL DEFAULT 13 _Z41__static_initializati
39: 0000000000400a54 21 FUNC LOCAL DEFAULT 13 _GLOBAL__sub_I_main
40: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
41: 0000000000400c90 0 OBJECT LOCAL DEFAULT 17 __FRAME_END__
42: 0000000000600dd0 0 OBJECT LOCAL DEFAULT 20 __JCR_END__
43: 0000000000000000 0 FILE LOCAL DEFAULT ABS
44: 0000000000601000 0 OBJECT LOCAL DEFAULT 23 _GLOBAL_OFFSET_TABLE_
45: 0000000000600dc8 0 NOTYPE LOCAL DEFAULT 18 __init_array_end
46: 0000000000600db8 0 NOTYPE LOCAL DEFAULT 18 __init_array_start
47: 0000000000600dd8 0 OBJECT LOCAL DEFAULT 21 _DYNAMIC
48: 0000000000601068 0 NOTYPE WEAK DEFAULT 24 data_start
49: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEi@@GLIBCXX_3.4
50: 0000000000400ae0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
51: 00000000004008e0 0 FUNC GLOBAL DEFAULT 13 _start
52: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
53: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
54: 0000000000400ae4 0 FUNC GLOBAL DEFAULT 14 _fini
55: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitC1Ev@@
56: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
57: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit@@GLIBC_2.2.5
58: 0000000000400890 0 FUNC GLOBAL DEFAULT UND _ZNSt8ios_base4InitD1Ev@@
59: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab
60: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZStlsISt11char_traitsIcE
61: 0000000000400af0 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
62: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
63: 0000000000601068 0 NOTYPE GLOBAL DEFAULT 24 __data_start
64: 0000000000601070 0 OBJECT GLOBAL HIDDEN 24 __TMC_END__
65: 0000000000601080 272 OBJECT GLOBAL DEFAULT 25 _ZSt4cout@@GLIBCXX_3.4
66: 0000000000400af8 0 OBJECT GLOBAL HIDDEN 15 __dso_handle
67: 0000000000400a70 101 FUNC GLOBAL DEFAULT 13 __libc_csu_init
68: 000000000060106c 0 NOTYPE GLOBAL DEFAULT 25 __bss_start
69: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _Z3addii
70: 0000000000601198 0 NOTYPE GLOBAL DEFAULT 25 _end
71: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSolsEPFRSoS_E@@GLIBCXX
72: 00000000004008d0 0 FUNC GLOBAL DEFAULT UND _ZSt4endlIcSt11char_trait
73: 000000000060106c 0 NOTYPE GLOBAL DEFAULT 24 _edata
74: 00000000004009cd 74 FUNC GLOBAL DEFAULT 13 main
75: 0000000000400810 0 FUNC GLOBAL DEFAULT 11 _init
[root@VM_0_9_centos dynamic_lib_text]#
typedef struct
{
Elf32_Word p_type; /* Segment type */
Elf32_Off p_offset; /* Segment file offset */
Elf32_Addr p_vaddr; /* Segment virtual address */
Elf32_Addr p_paddr; /* Segment physical address */
Elf32_Word p_filesz; /* Segment size in file */
Elf32_Word p_memsz; /* Segment size in memory */
Elf32_Word p_flags; /* Segment flags */
Elf32_Word p_align; /* Segment alignment */
} Elf32_Phdr;
[root@VM_0_9_centos dynamic_lib_text]# readelf -l a.out
Elf file type is EXEC (Executable file)
Entry point 0x4008e0
There are 9 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x00000000000001f8 0x00000000000001f8 R E 8
INTERP 0x0000000000000238 0x0000000000400238 0x0000000000400238
0x000000000000001c 0x000000000000001c R 1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000000c94 0x0000000000000c94 R E 200000
LOAD 0x0000000000000db8 0x0000000000600db8 0x0000000000600db8
0x00000000000002b4 0x00000000000003e0 RW 200000
DYNAMIC 0x0000000000000dd8 0x0000000000600dd8 0x0000000000600dd8
0x0000000000000220 0x0000000000000220 RW 8
NOTE 0x0000000000000254 0x0000000000400254 0x0000000000400254
0x0000000000000044 0x0000000000000044 R 4
GNU_EH_FRAME 0x0000000000000b1c 0x0000000000400b1c 0x0000000000400b1c
0x0000000000000044 0x0000000000000044 R 4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 10
GNU_RELRO 0x0000000000000db8 0x0000000000600db8 0x0000000000600db8
0x0000000000000248 0x0000000000000248 R 1
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 .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
[root@VM_0_9_centos dynamic_lib_text]#