Linux系统中main函数的执行过程

来源:互联网 发布:手机淘宝详情添加视频 编辑:程序博客网 时间:2024/05/01 01:59

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的值,然后将一些数据压栈,最后调用一个函数。


转载于:http://www.linuxidc.com/Linux/2011-08/41869.htm

原创粉丝点击