Context: I can create a shared object library which is linked to a static library without any problems on 32bit linux. When I attempt the same build on 64bit linux, I see this linker error:
- relocation R_X86_64_32S against `a local symbol’ can not be used when making a shared object; recompile with -fPIC
This error is quite common on the web. The solution is to compile the statically linked library with position independent code (-fPIC).
What I do not understand is why this is not required for the 32bit build. Can anyone help out?
Ok the answer is described in detail here: http://www.technovelty.org/code/c/amd64-pic.html.
The basic gist of the explanation is that the i386 architecture implicitly dereferences the frame pointer for each function (explained on the last paragraph of the linked page). This process incurs some extra overhead so in the new 64-bit architectures, this dereferencing overhead was eliminated as an optimization.
The consequence of this optimization from a linking perspective was that unless 64-bit code is explicitly compiled as position independent code, it will produce code that is hard-coded with offsets for its execution context.
This is an imperfect explanation of the content in the linked page but it suffices for my purposes.