Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't all code compiled position independent?

Tags:

c

compilation

When compiling shared libraries in gcc the -fPIC option compiles the code as position independent. Is there any reason (performance or otherwise) why you would not compile all code position independent?

like image 363
ojblass Avatar asked May 02 '09 01:05

ojblass


People also ask

Why do we need Position Independent Code?

Position-independent code is not tied to a specific address. This independence allows the code to execute efficiently at a different address in each process that uses the code. Position-independent code is recommended for the creation of shared objects.

Is position independent code slower?

Consequently, PIC objects are usually slightly larger and slower at runtime than the equivalent non-PIC object.

What is position-independent Shellcode?

Position-independent code (PIC) is code that uses no hard-coded addresses for either code or data. Shellcode is PIC.


2 Answers

It adds an indirection. With position independent code you have to load the address of your function and then jump to it. Normally the address of the function is already present in the instruction stream.

like image 167
wowest Avatar answered Oct 14 '22 09:10

wowest


Yes there are performance reasons. Some accesses are effectively under another layer of indirection to get the absolute position in memory.

There is also the GOT (Global offset table) which stores offsets of global variables. To me, this just looks like an IAT fixup table, which is classified as position dependent by wikipedia and a few other sources.

http://en.wikipedia.org/wiki/Position_independent_code

like image 20
Unknown Avatar answered Oct 14 '22 08:10

Unknown