Reputation: 11
I'm a bit curious about the internal of ELF
file. And I have this question:
Why we need to compile a shared library (.so)
with the flag -fPIC
?
While theoretically, we can dynamically link an executable with a statically linked elf
file.
This is because the GOT
and PLT
table in the executable need to be updated while the dynamically link executable can be left unchanged.
Upvotes: 1
Views: 1143
Reputation: 10667
The idea of shared library is that the same code can be shared by several program in memory. However, one cannot ensure that the two running program expect the library to be at the same address of their memory layout (think of the collisions if the program use different shared libraries). So the shared library is only stored once in physical memory but thanks to the Memory Management Unit, the same physical memory is seen as being in two different address by the two programs. Of course for this to work you need the code to be independent of its real address (See eg http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/ for more precise explanations)
This moreover allows you to randomize the address of the shared memory which is good for security reason (http://fr.wikipedia.org/wiki/Return-to-libc_attack)
Upvotes: 3
Reputation: 8487
PIC simply means position independent code. Think of jump addresses in your code, which need to be relative to the location of your shared library code in memory.
Also have a look at:
Upvotes: 0