Five EmbedDev logo Five EmbedDev

An Embedded RISC-V Blog

Introduction

The global pointer is defined by the ABI.

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.