PIE程序的入口地址

Entry point address of a PIE program

如何知道 Linux/Android 上 PIE 程序的实际入口点地址?

我可以使用readelf -l读取入口点地址,但对于使用-pie-fPIE编译链接的elf,实际的入口点地址将与其不同。我怎样才能在 运行 时间获得这样的地址?也就是说,知道程序加载到内存中的位置。

程序的入口点总是可用的地址 符号 _start.

main.c

#include <stdio.h>

extern char _start;
int main()
{
    printf("&_start = %p\n",&_start);
    return 0;
}

编译并link -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

编译并link -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.

处进入