Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIC (Position Independedent Code)

Is there any way to check if an object file(.o file) is PIC-enabled?

like image 576
Raj Avatar asked May 05 '11 09:05

Raj


1 Answers

Not sure how portable this is, but for x86 and x86_64, ELF format, you can use readelf -r and look at the relocation types.

For 32bit PIC code, you should have a R_386_GOTPC relocation section:

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

No such section section should exists for non-PIC .os. (You'll also see a global offset table in the readelf -s output.)

For 64bit, same thing but with a R_X86_64_GOTPCREL relocation type. I'm pretty sure all the relocation type names are directly indicative of whether the code is PIC or not, but I can't find a reference right now.

like image 133
Mat Avatar answered Oct 17 '22 01:10

Mat