[clue] Linking static libraries into shared objects

marcus hall marcus at tuells.org
Wed Jan 11 15:42:28 MST 2012


On Wed, Jan 11, 2012 at 01:19:30PM -0700, Jeremy Slade wrote:
> The .o's that went into the .a have to be compiled PIC (gcc -fPIC) in 
> order to be linked into a shared library.
> 

This is not strictly true, but it is recommended practice.  If they are
compiled with -fPIC, gcc generates position independent code, which can
be mapped to any address and still work.  If it is not compiled with -fPIC,
the code will likely be slightly smaller, but it will make references to
the address of the code.  When this is placed into a .so, the code
segments from the .so will be mapped into the address space at an arbitrary
address and the linker will have to adjust any address references made in
the code.  This adds some time during linking (dynamic loading), but most
significantly, the adjustments made to the code are made via copy-on-write
which generates a unique page in memory for each process.  This defeats
the point of a shared library somewhat, since these text pages are no longer
shared between the users of the library.

But, things *will* work if you don't specify -fPIC...

marcus hall
marcus at tuells.org


More information about the clue mailing list