Ld build a symbol table and relocate outer modules’s variables addresses. Because static library is combined with main program, so during linking period the executable file get absolute address.
Shared library has two link ways. One is decided when loaded into virtual memory, it’s called load time relocation. Another is via PIC(Positon-independent Code) technology decide address until access it.
First, wo will analyze the former way. The executable file when loaded into virtual memory gets the address from shared library. In other words, when module’s absulote addresses in memory is sured, the system relocates all abosulate addresses.
But there are some issues, if shared library exists instruments are relevance to address, such as when a variable in shared library access other library variable, It may be different in different programs. So it must be different code section in different program’s shared library. It goes against the purpose of the shared library, and system must copy several code to different program using shared library.
PIC technoloy separates code from independent code and dependent-address code. When compiled module, module build .got section, and put .got table’s address corresponding name which is refer to outer module.
So after compiled, dependent-address instruments in module can get address from .got table. When loaded into memory, .got section get absoute address is the same as the former way. As we know, even if shared library, every program must own a data section copy and .got section exists .data section, so different programs own different .got section, there will be non-conflict address in code.
We can conclude that if module load self’s function or variables, it’s easy to understand that module can get abosulate address directly. If access the shared library’s function or variable, it will get address from .got section. The instrument find address in .got section, then indirectly get the value.