我如何知道 Linux/Android 上 PIE 程序的实际入口点地址?
我可以使用 读取入口点地址,但是对于使用orreadelf -l
编译和链接的精灵,实际的入口点地址将与它不同。如何在运行时获得这样的地址?也就是说,知道程序在哪里加载到内存中。-pie
-fPIE
我如何知道 Linux/Android 上 PIE 程序的实际入口点地址?
我可以使用 读取入口点地址,但是对于使用orreadelf -l
编译和链接的精灵,实际的入口点地址将与它不同。如何在运行时获得这样的地址?也就是说,知道程序在哪里加载到内存中。-pie
-fPIE
程序的入口点始终可以作为符号的地址使用_start
。
主程序
#include <stdio.h>
extern char _start;
int main()
{
printf("&_start = %p\n",&_start);
return 0;
}
编译和链接-no-pie
:
$ gcc -no-pie main.c
然后我们看到:
$ nm a.out | grep '_start'
0000000000601030 B __bss_start
0000000000601020 D __data_start
0000000000601020 W data_start
w __gmon_start__
0000000000600e10 t __init_array_start
U __libc_start_main@@GLIBC_2.2.5
0000000000400400 T _start
^^^^^^^^^^^^^^^
和:
$ readelf -h a.out | grep Entry
Entry point address: 0x400400
和:
$ ./a.out
&_start = 0x400400
编译和链接-pie
:
$ gcc -pie main.c
然后我们看到:
$ nm a.out | grep '_start'
0000000000201010 B __bss_start
0000000000201000 D __data_start
0000000000201000 W data_start
w __gmon_start__
0000000000200db8 t __init_array_start
U __libc_start_main@@GLIBC_2.2.5
0000000000000540 T _start
^^^^^^^^^^^^
和:
$ readelf -h a.out | grep Entry
Entry point address: 0x540
和:
$ ./a.out
&_start = 0x560a8dc5e540
^^^
因此 PIE 程序在其名义入口点0x540
加上0x560a8dc5e000
。