Global Offset Table (Processor-Specific)
Position-independent code cannot, in general, contain absolute virtual addresses.
Global offset tables hold absolute addresses in private data. Addresses are therefore
available without compromising the position-independence and shareability of a program's
text. A program references its GOT
using position-independent
addressing and extracts absolute values. This technique redirects position-independent
references to absolute locations.
Initially, the GOT
holds information as required by its relocation
entries. After the system creates memory segments for a loadable object file, the
runtime linker processes the relocation entries. Some relocations can be of type
R_xxxx_GLOB_DAT
, referring to the GOT
.
The runtime linker determines the associated symbol values, calculates their absolute addresses, and sets the appropriate memory table entries to the proper values. Although the absolute addresses are unknown when the link-editor creates an object file, the runtime linker knows the addresses of all memory segments and can thus calculate the absolute addresses of the symbols contained therein.
If a program requires direct access to the absolute address of a symbol, that symbol
will have a GOT
entry. Because dynamic objects
have a separate GOT
, a symbol's address can appear in several tables.
The runtime linker processes all the GOT
relocations before giving
control to any code in the process image. This processing ensures that absolute
addresses are available during execution.
The table's entry zero is reserved to hold the address of the dynamic structure,
referenced with the symbol _DYNAMIC
. This symbol enables a program,
such as the runtime linker, to find its own dynamic structure without having yet
processed its relocation entries. This method is especially important for the runtime
linker, because it must initialize itself without relying on other programs to relocate
its memory image.
The system can choose different memory segment addresses for the same shared object in different programs. The system can even choose different library addresses for different executions of the same program. Nonetheless, memory segments do not change addresses once the process image is established. As long as a process exists, its memory segments reside at fixed virtual addresses.
A GOT
format and interpretation are processor-specific.
The symbol _GLOBAL_OFFSET_TABLE_
can be used to access the table.
This symbol can reside in the middle of the .got
section, allowing
both negative and nonnegative subscripts into the array of addresses. The symbol type is
an array of Elf32_Addr
for 32-bit code, and an array of
Elf64_Addr
for 64-bit code.
extern Elf32_Addr _GLOBAL_OFFSET_TABLE_[]; extern Elf64_Addr _GLOBAL_OFFSET_TABLE_[];