1.ELF的三個文件頭
每個ELF文件有三個文件頭,用來索引信息。
(1).EH = ELF file Header 可在此讀到PH,SH在文件中的offset。
(2).PH = Program Header 與load program有關的索引,.o的PH為空。
(3).SH = Section Header 組成此文件的所有section的索引。
2. elf.h
先以32位的.o文件為例。
/usr/include/elf.h中可以查到三個文件頭的組成結構,以及各個值對應的意義。
/* The ELF file header. This appears at the start of every ELF file. */ #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;
3.示例程序
bar.c
int bar0 = 0; int bar1 = 1;
這個.c只有兩個變量定義組成。把它編譯成bar.o。
4.對應關系:
(1)magic number: 給操作系統和編譯器辨別此文件是ELF二進制庫。
0x45 0x4C 0x46為ELF三個字母的ASCII碼。
(2)type: 文件類型
Relocatable file = 1(.o, .a 可重定位文件,靜態庫)
Executable file = 2(可執行文件,a.out,exe,運行庫)
Shared object file = 3(共享目標文件,.so,動態庫)
(3)machine:架構
eg:0x0028=40=ARM, 0x0003=3=Intel_80386
(4)version: ELF 版本,目前均為1
(5)entry:程序的入口地址(虛擬地址),.o文件沒有入口,故為0。
可執行文件應該為_start的虛擬地址。
(6)PH相關: phoff: H在文件中的offset
phentsize: PH的每個entry有多大,Byte
phnum: PH有多少項entry
(7)SH相關: shoff: SH在文件中的offset
shentsize: SH的每個entry有多大
shnum: SH有多少項entry
shstrndx: SH中String Table Section排第幾項。
(8)flags:ABI版本
(9)ehsize:ELF Header的大小:0x0034=52(B),EH大小為固定值52B。