如何知道二进制文件是否已与“ pie”链接器标志链接?

时间:2018-09-11 13:11:40

标签: gcc linker

我想知道在链接过程中是否已使用位置无关的可执行标志链接了二进制文件。

1 个答案:

答案 0 :(得分:1)

这是一种方法:

main.c

#include <stdio.h>

int main(void)
{
    puts(__func__);
    return 0;
}

编译并链接非PIE:

$ gcc -Wall -c main.c
$ gcc -Wall -no-pie main.o

请参阅程序标头(我的^^^^^^^^^注释):

$ 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