PIC(职位独立代码)

Raj*_*Raj 8 c compiler-construction linker gcc fpic

有没有办法检查目标文件(.o文件)是否支持PIC?

Mat*_*Mat 7

不确定这是多么便携,但对于x86和x86_64,ELF格式,您可以使用readelf -r并查看重定位类型.

对于32位PIC代码,您应该有一个R_386_GOTPC重定位部分:

Relocation section '.rel.text' at offset 0x38c contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000004  00000902 R_386_PC32        00000000   __i686.get_pc_thunk.cx
0000000a  00000a0a R_386_GOTPC       00000000   _GLOBAL_OFFSET_TABLE_
00000010  00000803 R_386_GOT32       00000000   f
Run Code Online (Sandbox Code Playgroud)

对于非PIC,不应存在此类部分.o.(您还会在readelf -s输出中看到全局偏移表.)

对于64位,同样的东西,但具有R_X86_64_GOTPCREL重定位类型.我很确定所有的重定位类型名称都直接指示代码是否为PIC,但我现在找不到引用.