1. 问题:Linux如何执行main函数。本文使用一个简单的C程序(simple.c)作为例子讲解。代码如下,
- int main()
- {
- return(0);
- }
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 中看到,如下所示:
- 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的值,然后将一些数据压栈,最后调用一个函数。
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)