连接器 ELF格式 装载可执行文件与虚拟进程空间

来源:互联网 发布:淘宝网电动葫芦 编辑:程序博客网 时间:2024/06/03 07:23

饭后一个小歇息 把一些个人理解传来,具体可参考我上传的文件(linux格式纯文档,没有颜色标注)

连接器把目标文件中相同属性(可读的执行段.text,可读写段 .data .bss)的段尽可能合并成segment.操作系统装载ELF可执行文件时,把segment映射为虚拟进程空间的域

从section看,ELF文件是链接视图

以下是段表的数据结构,ELF段表是结构提数组,每一各数组元素段条目。每一个结构体成员定义段表条目的属性,对应最上面行的标志。
typedef struct
{
Elf64_Word sh_name; /* Section name (string tbl index) */
Elf64_Word sh_type; /* Section type */
Elf64_Xword sh_flags; /* Section flags */
Elf64_Addr sh_addr; /* Section virtual addr at execution */
Elf64_Off sh_offset; /* Section file offset */
Elf64_Xword sh_size; /* Section size in bytes */
Elf64_Word sh_link; /* Link to another section */
Elf64_Word sh_info; /* Additional section information */
Elf64_Xword sh_addralign; /* Section alignment */
Elf64_Xword sh_entsize; /* Entry size if section holds table */
} Elf64_Shdr;

There are 30 section headers, starting at offset 0x998:

目标文件有30个段
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

TYPE 为蓝色的具有相同的读写属性,连接器把目标文件中有相同属性的段合并为segment段,操作系统

把同一类segment段装载到虚拟进程空间的同一个域。

[ 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
000000000000003e 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
0000000000000010 0000000000000000 A 0 0 8
[16] .eh_frame_hdr PROGBITS 00000000004005d8 000005d8
0000000000000024 0000000000000000 A 0 0 4
[17] .eh_frame PROGBITS 0000000000400600 00000600
000000000000007c 0000000000000000 A 0 0 8
[18] .ctors PROGBITS 0000000000600680 00000680
0000000000000010 0000000000000000 WA 0 0 8
[19] .dtors PROGBITS 0000000000600690 00000690
0000000000000010 0000000000000000 WA 0 0 8
[20] .jcr PROGBITS 00000000006006a0 000006a0
0000000000000008 0000000000000000 WA 0 0 8
[21] .dynamic DYNAMIC 00000000006006a8 000006a8
0000000000000190 0000000000000010 WA 6 0 8
[22] .got PROGBITS 0000000000600838 00000838
0000000000000008 0000000000000008 WA 0 0 8
[23] .got.plt PROGBITS 0000000000600840 00000840
0000000000000028 0000000000000008 WA 0 0 8
[24] .data PROGBITS 0000000000600868 00000868
0000000000000004 0000000000000000 WA 0 0 4
[25] .bss NOBITS 0000000000600870 0000086c
0000000000000010 0000000000000000 WA 0 0 8
[26] .comment PROGBITS 0000000000000000 0000086c
000000000000002d 0000000000000001 MS 0 0 1
[27] .shstrtab STRTAB 0000000000000000 00000899
00000000000000fe 0000000000000000 0 0 1
[28] .symtab SYMTAB 0000000000000000 00001118
0000000000000600 0000000000000018 29 46 8
[29] .strtab STRTAB 0000000000000000 00001718
0000000000000201 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)



从segment角度看,ELF是执行视图
可执行的ELF文件中有一个程序头表,保存ELF中的Segment信息,它也是一个结构体数组一个结构元素提对应一个segment。
LOADE的segment就是装载到虚拟进程空间的VM域的。
/* Special section indices. */

typedef struct
{
Elf64_Word p_type; /* Segment type */
Elf64_Word p_flags; /* Segment flags */
Elf64_Off p_offset; /* Segment file offset */
Elf64_Addr p_vaddr; /* Segment virtual address */
Elf64_Addr p_paddr; /* Segment physical address */
Elf64_Xword p_filesz; /* Segment size in file */
Elf64_Xword p_memsz; /* Segment size in memory */
Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
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
0x000000000000067c 0x000000000000067c R E 200000
LOAD 0x0000000000000680 0x0000000000600680 0x0000000000600680
0x00000000000001ec 0x0000000000000200 RW 200000
DYNAMIC 0x00000000000006a8 0x00000000006006a8 0x00000000006006a8
0x0000000000000190 0x0000000000000190 RW 8
NOTE 0x000000000000021c 0x000000000040021c 0x000000000040021c
0x0000000000000044 0x0000000000000044 R 4
GNU_EH_FRAME 0x00000000000005d8 0x00000000004005d8 0x00000000004005d8
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


0 0
原创粉丝点击