ELF文件的格式在我看来分成两种来看,一种是可重定位文件,一种是可执行文件。
首先对于可重定位文件。
int printf(const char* format,...);
int global_init_var=84;
int global_uninit_var;
void func1(int i)
{
printf("%d\n",i);
}
int main(void)
{
static int static_var=85;
static int static_var2;
int a=1;
int b;
func1(static_var+static_var2+a+b);
return a;
}
在UBuntu16.02下,用gcc编译成32位可重定位文件。
gcc -m32 -c SimpleSection.c
编译出SimpleSection.o文件。
通过objdump命令来查看目标文件的内容和结构。objdump -h SimpleSection.o
查看elf头部readelf -h SimpleSection.o
整个程序的结构如下图所示:
这个段描述符其实就是节描述符,就是section。每个section都会有一个段描述符来存储相应section的信息。
例如上面程序,有13个section,则section headers的大小就是1340字节即0x208字节。
32位ELF header格式
typedef struct
{
unsigned char e_ident[EI_NIDENT]; / Magic number和其它信息 /
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;
字符串表
ELF的符号表