歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux系統中main函數的執行過程

Linux系統中main函數的執行過程

日期:2017/2/28 16:07:50   编辑:Linux教程

1. 問題:Linux如何執行main函數。

本文使用一個簡單的C程序(simple.c)作為例子講解。代碼如下,

  1. int main()
  2. {
  3. return(0);
  4. }

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 中看到,如下所示:

  1. typedef struct
  2. {
  3. unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
  4. Elf32_Half e_type; /* Object file type */
  5. Elf32_Half e_machine; /* Architecture */
  6. Elf32_Word e_version; /* Object file version */
  7. Elf32_Addr e_entry; /* Entry point virtual address */
  8. Elf32_Off e_phoff; /* Program header table file offset */
  9. Elf32_Off e_shoff; /* Section header table file offset */
  10. Elf32_Word e_flags; /* Processor-specific flags */
  11. Elf32_Half e_ehsize; /* ELF header size in bytes */
  12. Elf32_Half e_phentsize; /* Program header table entry size */
  13. Elf32_Half e_phnum; /* Program header table entry count */
  14. Elf32_Half e_shentsize; /* Section header table entry size */
  15. Elf32_Half e_shnum; /* Section header table entry count */
  16. Elf32_Half e_shstrndx; /* Section header string table index */
  17. } Elf32_Ehdr;
其中,e_entry存儲了該執行文件的起始地址。

5. 關於起始地址

~#objdump -d simple

  1. 80482d0 <_start>:
  2. 80482d0: 31 ed xor %ebp,%ebp
  3. 80482d2: 5e pop %esi
  4. 80482d3: 89 e1 mov %esp,%ecx
  5. 80482d5: 83 e4 f0 and $0xfffffff0,%esp
  6. 80482d8: 50 push %eax
  7. 80482d9: 54 push %esp
  8. 80482da: 52 push %edx
  9. 80482db: 68 20 84 04 08 push $0x8048420
  10. 80482e0: 68 74 82 04 08 push $0x8048274
  11. 80482e5: 51 push %ecx
  12. 80482e6: 56 push %esi
  13. 80482e7: 68 d0 83 04 08 push $0x80483d0
  14. 80482ec: e8 cb ff ff ff call 80482bc <_init+0x48>
  15. 80482f1: f4 hlt
  16. 80482f2: 89 f6 mov %esi,%esi

該命令可以得到simple的反匯編代碼,可以看到,起始地址0x80482d0對應的是_start這個routine。這段代碼所做的事情是,將ebp清0,調整esp的值,然後將一些數據壓棧,最後調用一個函數。

Copyright © Linux教程網 All Rights Reserved