如何知道二进制文件是否已与“pie”链接器标志链接?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何知道二进制文件是否已与“pie”链接器标志链接?相关的知识,希望对你有一定的参考价值。
我想知道在链接期间是否使用位置无关的可执行标志链接了二进制文件。
答案
这是一种方式:
main.c中
#include <stdio.h>
int main(void)
{
puts(__func__);
return 0;
}
编译并链接非PIE:
$ gcc -Wall -c main.c
$ gcc -Wall -no-pie main.o
查看程序头文件(我的^^^^^^^^^
-annotations):
$ readelf -l a.out
Elf file type is EXEC (Executable file)
^^^^^^^^^^^^^^^^^^^^^^
Entry point 0x400400
^^^^^^^^
| Absolute entry point
There are 9 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x00000000000001f8 0x00000000000001f8 R 0x8
INTERP 0x0000000000000238 0x0000000000400238 0x0000000000400238
0x000000000000001c 0x000000000000001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
^^^^^^^^^^^^^^^^^^
| Absolute load address
0x00000000000006c8 0x00000000000006c8 R E 0x200000
LOAD 0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
0x0000000000000220 0x0000000000000228 RW 0x200000
DYNAMIC 0x0000000000000e20 0x0000000000600e20 0x0000000000600e20
0x00000000000001d0 0x00000000000001d0 RW 0x8
NOTE 0x0000000000000254 0x0000000000400254 0x0000000000400254
0x0000000000000044 0x0000000000000044 R 0x4
GNU_EH_FRAME 0x000000000000058c 0x000000000040058c 0x000000000040058c
0x000000000000003c 0x000000000000003c R 0x4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
0x00000000000001f0 0x00000000000001f0 R 0x1
...
...
编译并链接PIE:
$ gcc -Wall -fPIC -c main.c
$ gcc -Wall -pie main.o
再次查看程序标题:
$ readelf -l a.out
Elf file type is DYN (Shared object file)
^^^^^^^^^^^^^^^^^^^^^^^^
Entry point 0x530
^^^^^
| Offset from unknown load address
There are 9 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000000040 0x0000000000000040
0x00000000000001f8 0x00000000000001f8 R 0x8
INTERP 0x0000000000000238 0x0000000000000238 0x0000000000000238
0x000000000000001c 0x000000000000001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
^^^^^^^^^^^^^^^^^^
| Unknown load address
0x0000000000000830 0x0000000000000830 R E 0x200000
LOAD 0x0000000000000db8 0x0000000000200db8 0x0000000000200db8
0x0000000000000258 0x0000000000000260 RW 0x200000
DYNAMIC 0x0000000000000dc8 0x0000000000200dc8 0x0000000000200dc8
0x00000000000001f0 0x00000000000001f0 RW 0x8
NOTE 0x0000000000000254 0x0000000000000254 0x0000000000000254
0x0000000000000044 0x0000000000000044 R 0x4
GNU_EH_FRAME 0x00000000000006ec 0x00000000000006ec 0x00000000000006ec
0x000000000000003c 0x000000000000003c R 0x4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x0000000000000db8 0x0000000000200db8 0x0000000000200db8
0x0000000000000248 0x0000000000000248 R 0x1
以上是关于如何知道二进制文件是否已与“pie”链接器标志链接?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用命令行将“其他链接器标志”添加到 xcode 项目?
如何在 Eclipse 中将“其他链接器标志”设置为 Xcode?