I recently received a:
...relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
error while trying to compile a program as a shared library.
Now the solution to this is not too difficult (recompile all dependencies with -fPIC), but after some research it turns out that this problem is only present on x86-64 platforms. On 32bit any position dependent code can still be relocated by the dynamic loader.
The best answer I could find is:
x86 has support for .text relocations (which is what happens when you have position-dependend code). This support comes at a cost, namely that every page containing such relocation becomes basically unshared, even if it sits in a shared library, thereby spoiling the very concept of shared libs. Hence we decided to disallow this on amd64 (plus it creates problems if the value needs more than 32bit, because all .text relocs only have size 'word32')
But I don't find this quite adequate. If it is the case that relocations spoil the concept of shared libraries, why can it be done on 32bit platforms? Also, if there were changes that needed to be made to the ELF format to support 64bit, then why were not all fields increased in size to accommodate?
This may be a minor point, but it is motivated by the fact that a) the code in question is a scientific code and it would be nice not to have to take a performance hit and b) this information was nye impossible to find in the first place!
[Edit: 'The Answer'
@awoodlands answer is probably the best 'literal answer', @servn added some good information.
In a search to find more about different types of relocations I found this and ultimately an x86_64 ABI reference (see page 68) ]
-fPIC
performance hit is lessened on x86-64 (relative to x86-32) because it has more registers, PC-relative addressing, and an ABI that was designed with PIC in mind. I am not going to say it's gone, but measure it and you might be pleasantly surprised. – Heartstrings-fPIC
can be linked to a .so, while others cannot due to the similar error message. – Coupe