1. 問題:Linux如何執行main函數。
本文使用一個簡單的C程序(simple.c)作為例子講解。代碼如下,
- int main()
- {
- return(0);
- }
2. 編譯
~#gcc -o simple simple.c
3. 查看可執行文件的基本信息
~#objdump -f simple
simple: file format elf32-i386 architecture: i386, flags 0x00000112: EXEC_P, HAS_SYMS, D_PAGED start address 0x080482d0
借助objdump這個工具,可以獲得可執行文件的一些關鍵信息。
比如,simple文件的格式是“ELF32”,該文件的起始地址是0x80482d0,,等。
4. 什麼是ELF
ELF是Executable and Linking Format的縮寫,是Unix上常見的幾種目標文件格式(及可執行文件格式)之一。
ELF的頭部結構提供了ELF文件的基本信息,其數據結構可以在/usr/include/elf.h 中看到,如下所示:
- 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;
其中,e_entry存儲了該執行文件的起始地址。
5. 關於起始地址
~#objdump -d simple
- 80482d0 <_start>:
- 80482d0: 31 ed xor %ebp,%ebp
- 80482d2: 5e pop %esi
- 80482d3: 89 e1 mov %esp,%ecx
- 80482d5: 83 e4 f0 and $0xfffffff0,%esp
- 80482d8: 50 push %eax
- 80482d9: 54 push %esp
- 80482da: 52 push %edx
- 80482db: 68 20 84 04 08 push $0x8048420
- 80482e0: 68 74 82 04 08 push $0x8048274
- 80482e5: 51 push %ecx
- 80482e6: 56 push %esi
- 80482e7: 68 d0 83 04 08 push $0x80483d0
- 80482ec: e8 cb ff ff ff call 80482bc <_init+0x48>
- 80482f1: f4 hlt
- 80482f2: 89 f6 mov %esi,%esi
該命令可以得到simple的反匯編代碼,可以看到,起始地址0x80482d0對應的是_start這個routine。這段代碼所做的事情是,將ebp清0,調整esp的值,然後將一些數據壓棧,最後調用一個函數。