参照来源:https://blog.csdn.net/cherisegege/article/details/80297320
ld有多种方法设置进程入口地址, 按一下顺序: (编号越前, 优先级越高)
1, ld命令行的-e选项
2, 连接脚本的ENTRY(SYMBOL)命令
eg. ENTRY(_start) /* Entry point of application*/
3, 如果定义了start符号, 使用start符号值
4, 如果存在.text section, 使用.text section的第一字节的位置值
5, 使用值0
(一)通常例子
#include <stdio.h>
int main()
{
printf("helloworld! \n");
return 0;
}
$ gcc hello.c -o hello
$ ./hello
用户的代码是从main函数开始执行的,还有其它很多函数,比如_start函数。实际上程序真正的入口并不是main函数,我们以下面命令对hello.c代码进行编译:
$ gcc hello.c -nostdlib
/usr/bin/ld: warning: cannot find entrysymbol _start; defaulting to 0000000000400144
-nostdlib命令是指不链接标准库,报错说找不到entry symbol _start,这里是说找不到入口符号_start,也就是说程序的真正入口是_start函数。
实际上main函数只是用户代码的入口,它会由系统库去调用,在main函数之前,系统库会做一些初始化工作,比如分配全局变量的内存,初始化堆、线程等,当main函数执行完后,会通过exit()函数做一些清理工作,用户可以自己实现_start函数:
(二)通过 _start 来实现
#include <stdio.h>
#include <stdlib.h>
int _start(void)
{
printf("hello world!\n");
exit(0);
}
执行如下编译命令并运行:
$ gcc hello_start.c -nostartfiles -o hello_start
$ ./hello_start
hello world!
$ readelf -al a.out
...
Symbol table '.dynsym' contains 3 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@GLIBC_2.2.5 (2)
...
//查看elf文件 参数只有一个,printf 被优化成了puts
$ readelf -al a.out | grep FUN
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@GLIBC_2.2.5 (2)
22: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@@GLIBC_2.2.5
25: 0000000000400390 24 FUNC GLOBAL DEFAULT 10 _start
27: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@@GLIBC_2.2.5
(三)通过链接的时候指定来实现
#include <stdio.h>
#include <stdlib.h>
int mymain()
{
printf("helloworld!\n");
exit(0);
}
$ gcc hello_nomain.c -nostartfiles -e mymain -o hello_mymain
其中-e选项可以指定程序入口符号,查看符号表如下:
$ readelf -s hello_mymain | grep FUNC
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@GLIBC_2.2.5 (2)
22: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@@GLIBC_2.2.5
24: 0000000000400390 24 FUNC GLOBAL DEFAULT 10 mymain
27: 0000000000000000 0 FUNC GLOBAL DEFAULT UND exit@@GLIBC_2.2.5
//elf header 中的 入口 Entry point address 就是 函数的开始地址
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x400390
Start of program headers: 64 (bytes into file)
Start of section headers: 5184 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 20
Section header string table index: 17