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

Re: gcc and -fPIC



On Wed, Mar 20, 2002 at 06:00:35PM +0000, Will Newton wrote:
> I understand the need for PIC, but I was asking whether or not compiling 
> whole binaries with PIC unnecesarily might be a bad thing. e.g. if I cannot 
> control the granularity of compiler options easily in a build system and I 
> build an executable with -fPIC in order to build an accompanying library 
> correctly, am I trading off speed or footprint to any significant degree?

Yes, this will slow down your program by (usualy) around 5% on
x86. Probably it will increase the footprint a little as well - I have
never tried and measured that though.

> > 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.
> 
> Unless they are later linked into a dynamic library. :)

Don't do that.

If you feel you'd like to link a static (non-PIC) lib into a shared
library, you should instead ask the users of your shared library to
add the static lib to their link command. Some packages (like sdl)
have a config script that will tell the user what options he should
use to link the library. try sdl-config --libs for an example.

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

I'd like to add that some architectures (including x86) do not actualy
require that you use -fPIC for shared libraries. If you choose to do
that, the benefits are that you dont pay the overhead of -fPIC code,
the drawbacks however are that 1) the resulting shared lib violates
debian policy, 2) programs which use the shared lib will need to do
"fixups" in it at runtime, which results in the code being duplicated
in memory - in effect, the code is not shared anymore, and 3) you have
to be careful in the build system to still use -fPIC for architectures
that really require it (and will segfault if you dont use it). If you
use libtool you can use the -prefer-non-pic option to only use -fPIC
on architectures that require it.

-- 
Michel "Walken" LESPINASSE
Is this the best that god can do ? Then I'm not impressed.



Reply to: