I have 3 sample codes below(all .so files):
1)
int myglob = 42;
int fun(int a, int b)
{
return myglob + a + b;
}
2)
static int myglob = 42;
int fun(int a, int b)
{
return myglob + a + b;
}
3)
extern int myglob;
int fun(int a, int b)
{
return myglob + a + b;
}
How do relocations work in these three cases? How are they different from each other?
My guesses :
Since myglob
in the 3rd case is extern
, it uses the GOT
table.
In the 2nd case, I don't think we need to use the GOT table since myglob
is defined in the object itself and linker knows about the offset between the .text
and .data
sections, so we can add the known offset to the current instruction pointer to access myglob
in the data section.
1st one is declared in the file scope and don't specify a storage class, then it defaults to external linkage. So this should behave similar to the 3rd case.
Am I correct?