fPIC code in a static library: Why? What happens?

604 Views Asked by At

What happens when Position Independent Code is placed in a static library? (In particular, on x86-64)

That is to say: Several .o files are generated with PIC and placed in a .a file. This .a is statically linked into a final executable as normal.

It seems to work, but does it ever make sense to do this? Why?

What is the final address calculation done? (link-time? run-time?)

Thank you

1

There are 1 best solutions below

0
On

IIRC in a PIC system all global variables will be accessed through PIC.

The whole idea of PIC is that only a single table needs adjusting (the GOT), not every global access in the code.

The code itself is position independent because it accesses through the GOT, and the position of the GOT (relative to the instruction's address) is encoded during final linking.

So it is a mix. Some is done during the final link (coding GOT offsets), some is done on startup (GOT relocation/fixup), and some is not done (code relocation).