Welcome 微信登录

首页 / 操作系统 / Linux / Linux系统中main函数的执行过程

1. 问题:Linux如何执行main函数。本文使用一个简单的C程序(simple.c)作为例子讲解。代码如下,
  1. int main()  
  2. {  
  3.     return(0);  
  4. }  
2.  编译~#gcc -o simple simple.c3. 查看可执行文件的基本信息~#objdump -f simplesimple: file format elf32-i386 architecture: i386, flags 0x00000112: EXEC_P, HAS_SYMS, D_PAGED start address 0x080482d0借助objdump这个工具,可以获得可执行文件的一些关键信息。比如,simple文件的格式是“ELF32”,该文件的起始地址是0x80482d0,,等。
4. 什么是ELFELF是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的值,然后将一些数据压栈,最后调用一个函数。
  • 1
  • 2
  • 下一页
Linux下安装配置MemCached(以及libevent)Linux中为什么要随机函数栈的起始地址相关资讯      Linux函数 
  • Linux C语言中gotoxy函数  (04月11日)
  • Linux进程之Fork函数  (04/16/2015 08:48:35)
  • Linux中getrusage的使用  (11/08/2014 07:07:38)
  • Linux内核中min和max的实现  (03月03日)
  • Linux下mmap函数的一个练习  (01/19/2015 21:11:21)
  • Linux下confstr与uname函数_获取C  (10/28/2014 20:23:36)
本文评论 查看全部评论 (0)
表情: 姓名: 字数