I made a piece of code which consists in a dynamic library (lib.c
), and a main executable (main.c
).
In both files I define a global variable named:
When you compile with -fPIC
the object in question will determine the address of global symbols using the Global Offset Table. What happens though when part of the code is -fPIC
and part isn't is that one of your int global
s will be using this table to determine the address whilst the other part isn't.
If you had two shared object linked with -fPIC
, but your main program not then you would still have two addresses for int global
, one using the global offset table and one which was just local to the non-PIC code.
There's a really great discussion on PIC vs pic vs non PIC if you want to read further.
By default, when building an executable references to variables are done internally, with a fixed offset and no relocation.
However, you're passing -fPIC
and access to global data are converted to access via GOT and GOT relocations are added.