Let's say we have:
f1.c
#include <stdio.h>
static int x = 10;
void f1() {
printf("f1.c : %d\n", x);
}
main.c
extern void f1();
int main(int argc, char **argv) {
f1();
return 0;
}
we will compile and read the two ELF file symboltables (rel. ELF and exec ELF):
$> gcc -c *.c
$> readelf -s f1.o | grep x
Num: Value Size Type Bind Vis Ndx Name
5: 0000000000000000 4 OBJECT LOCAL DEFAULT 3 x
$> gcc *.o
$> readelf -s a.out | grep x
Num: Value Size Type Bind Vis Ndx Name
38: 0000000000601038 4 OBJECT LOCAL DEFAULT 25 x
I can see that the Value
(also known as address) in which the global static variable x
is 0000000000000000
from reading the relocatable object file f1.o
.
Which means we haven't initialized it yet since it is still a rel. ELF object file and the linker will take care of this.
So my question is then, if the linker is the one to set x
to the value of 10 at known address after linking of 0000000000601038
, how does it do so? Where does the linker get the information to set the value to 10 and who gives this information (f1.o
?) ?