Linux中Main函數的執行過程

來源:互聯網
上載者:User

標籤:

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的值,然後將一些資料壓棧,最後調用一個函數。

Linux中Main函數的執行過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.