Introduction
The global pointer is defined by the ABI.
- In the standard ABI and embedded ABI it is register
x3
AKAgp
. - The global pointer is a software convention, it has no implementation in hardware or specific instructions.
- It is implemented in the linker, enabled (default) or disabled by the
--relax
and--no-relax
options. - Global variables are ‘relaxed’ and accessed via a relative immediate offset from the global pointer.
- The global variables need to be within
imm12
reach of the global pointer to be relaxed. (That is +-2048 bytes.)
Initialization
In the bare-metal environments the linker script defines the global pointer as a symbol global_pointer$
, for example in SiFive’s example linker script. The symbol name has special importance to the linker.
.data : ALIGN(8) {
*(.data .data.*)
// ...
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800 );
// ...
} >ram AT>rom :ram_init
The global pointer is initialized before entering the C environment. It is important to temporarily disable relaxed
addressing when writing to the global pointer.
.option push
.option norelax
la gp, __global_pointer$
.option pop
Location
The global pointer should be in the middle of the global memory space. This is as imm12
is signed.