c++gccfpic

Why not always use fpic (Position Independent Code)?


I read this post on PIC and it seems that it always be good to use PIC (whenever it is exe / static / share llibrary).

So what are the disadvantages?
Are there examples elaborating when not to use PIC?


Solution

  • The accepted answer in the linked question is very simplistic, and only brings up one thing that differs between PIC and non-PIC code, generation of jumps that are relative instead of absolute.

    When you make PIC code, it's not only the code that's position independent, it's the data as well. And not all code or data can be addressed simply by using relative offsets, it has to be resolved at load-time (when the library/program is loaded into memory) or even during run-time.

    Also, using relative addressing means that the CPU has to translate the relative offsets into absolute addresses, instead of it being done by the compiler.

    On system with virtual memory there's often no need to spend load- or run-time on these relative address resolutions when the compiler can do it once and for all.