[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc and -fPIC



Hi,

on Wed, 20 Mar 2002, Will Newton wrote:

> Is it valid to compile an executable with -fPIC?
> Can anyone tell me which architectures require -fPIC?

In general, you need to compile everything that could be mapped at
arbitrary addresses (in the process's virtual address space) with the
-fPIC option to make it position independent (whatever that means for the
target platform). Programs are loaded at a fixed address (for i386 Linux,
this is somewhere around 0x80000000), so they needn't be compiled with
-fPIC, while shared libraries can be mapped to different addresses in
different processes and thus need -fPIC. Static libraries are collections
of .o files, also called relocatable objects, which are not position
independent, but contain the information how to move them to a specified
position ("relocate", which basically means adding the start address to
all pointers). Thus, they don't need -fPIC either.

-fPIC is implemented in different ways for different architectures:

i386 doesn't have hardware support for PIC:

        JSR label  /* relative */
label:  POP %reg   /* now we have the address of "label" in %reg

m68k can address data relative to the current program counter, but only
for source addresses:

        MOVE.L data(PC),%reg
        [...]
        LEA data(PC),%tmpreg
        MOVE.L %reg,(%tmpreg)

As you can think for yourself, this is a performance penalty, especially
if you only have few registers, like on i386 (that's why i386 doesn't use
libperl in its perl interpreter -- 5% performance loss), however you
cannot avoid it for shared libraries.

For the "which architectures" question: Do not think about that.
Architectures which don't require -fPIC simply have compilers that ignore
it. :-)

   Simon

-- 
GPG public key available from http://phobos.fs.tum.de/pgp/Simon.Richter.asc
 Fingerprint: 040E B5F7 84F1 4FBC CEAD  ADC6 18A0 CC8D 5706 A4B4
Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!



Reply to: