典型的可重定向文件结构:


以下面这段代码为例:
/*
* SimpleSection.c
* gcc -c SimpleSection.c
*/
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 0;
}
gcc -c SimpleSection.c -o SimpleSection.o
typedef struct elf64_hdr {
unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Elf64_Addr e_entry; /* Entry point virtual address */
Elf64_Off e_phoff; /* Program header table file offset */
Elf64_Off e_shoff; /* Section header table file offset */
Elf64_Word e_flags;
Elf64_Half e_ehsize;
Elf64_Half e_phentsize;
Elf64_Half e_phnum;
Elf64_Half e_shentsize;
Elf64_Half e_shnum;
Elf64_Half e_shstrndx;
} Elf64_Ehdr;
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 0100 3e00 0100 0000 :.ELF..............>.....
00000018: 0000 0000 0000 0000 0000 0000 0000 0000 0804 0000 0000 0000 :........................
00000030: 0000 0000 4000 0000 0000 4000 0e00 0d00
| 字段 | 占位(字节) | 示意 | HEX |
|---|---|---|---|
| e_ident | 16 | Magic… | 7f45 4c46 0201 0100 0000 0000 0000 000 |
| e_type | 2 | ELF 文件类型 |
使用 readelf 查看
readelf -h SimpleSection.o
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: REL (Relocatable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 1032 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 14
Section header string table index: 13
通过上面的信息,可以得出该 .o 文件的结构大致如下:
