[Gllug] -fPIC option

Nix nix at esperi.demon.co.uk
Sat Feb 1 22:15:52 UTC 2003


On Sat, 1 Feb 2003, rich at annexia.org stipulated:
> On Sat, Feb 01, 2003 at 05:17:01PM +0000, Tethys wrote:
>> While we're at it, under what circumstances should -fpic be used instead
>> of -fPIC? Surely if you want position independent code, then you want it
>> regardless of branch displacement sizes? Nix?
> 
> I've been reading around this a bit, and it seems like the difference
> between -fpic and -fPIC is the amount of position independence you get -
> so -fPIC will (apparently) produce completely position-independent code
> but with a size penalty. -fpic will produce mostly position-independent
> code, resulting in some relocations at run time (and hence some non-
> shared pages).

Not quite so. Both switches produce completely PIC code; however, on
some platforms, it is possible to use more efficient instruction
sequences if the GOT and PLT are known to be below a certain size. But
this is something that can only be known at link time, so the compiler
can't guess it for you; you have to tell it.

(On SPARC/Linux, the difference is three instructions per GOT/PLT
reference, IIRC. I'm feeling too ill right now to check.)

> This might be the answer to my question too - it looks like my shared
> libraries (compiled without any -fpic/PIC option) will be heavily
> relocated at run time, so not very shared :-(

Quite so. Every reference which (would otherwise have) gone through the
PLT (basically functions) and GOT (basically global variables) will have
a relocation pointing at it, so every page with at least one function
call on it will be relocated.

Basically you'll get almost no sharing at all --- and ld.so'll have to
do so much relocation that startup will be decidedly slow too (try
LD_DEBUG=statistics to see).

On many OSes (e.g. Solaris), ld.so does no such relocation of shared
libraries; on such platforms, *all* shared libraries must be
*completely* PIC --- note that this means that any static libraries that
are linked into your shared libraries must be PIC too.

(Plus, prelinking doesn't work unless your library is compiled PIC ---
to be pedantic, unless it has no DF_TEXTREL relocations.)

-- 
2003-02-01: the day the STS died.

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list