分类: LINUX
2010-05-11 11:51:57
原文如下
-------------------------------------------------------------------
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