Use '__global_pointer$' special linker symbol to set gp,

because its value can be different from __BSS_END__ - 0x800.

Details from binutils-2.40/ld/emulparams/elf32lriscv-defs.sh
// We must cover as much of sdata as possible if it exists.  If sdata+bss is
// smaller than 0x1000 then we should start from bss end to cover as much of
// the program as possible.  But we can't allow gp to cover any of rodata, as
// the address of variables in rodata may change during relaxation, so we start
// from data in that case.
OTHER_END_SYMBOLS="${CREATE_SHLIB-__BSS_END__ = .;
    __global_pointer$ = MIN(__SDATA_BEGIN__ + 0x800,
                            MAX(__DATA_BEGIN__ + 0x800, __BSS_END__ - 0x800));}"
This commit is contained in:
Pierre Muller 2025-01-16 17:01:33 +00:00
parent 4813ab9f31
commit b1a47a5d7d
3 changed files with 8 additions and 8 deletions

View File

@ -16,7 +16,7 @@
******************************************************************************}
var
BSS_START: record end; external name '__bss_start';
global_pointer: record end; external name '__global_pointer$';
procedure ini_dummy;
begin
@ -48,7 +48,7 @@ procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
.option push
.option norelax
.L1:
auipc gp, %pcrel_hi(BSS_START+0x800)
auipc gp, %pcrel_hi(global_pointer)
addi gp, gp, %pcrel_lo(.L1)
.option pop
@ -84,7 +84,7 @@ procedure _FPC_proc_haltproc(e:longint); cdecl; public name '_haltproc';
.option push
.option norelax
.L1:
auipc gp, %pcrel_hi(BSS_START+0x800)
auipc gp, %pcrel_hi(global_pointer)
addi gp, gp, %pcrel_lo(.L1)
.option pop
jalr x0, x1

View File

@ -18,7 +18,7 @@
{$linklib gcc}
var
BSS_START: record end; external name '__bss_start';
global_pointer: record end; external name '__global_pointer$';
_etext: pointer; external name '_etext';
procedure ini_dummy;
@ -58,7 +58,7 @@ procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
.option push
.option norelax
.L1:
auipc gp, %pcrel_hi(BSS_START+0x800)
auipc gp, %pcrel_hi(global_pointer)
addi gp, gp, %pcrel_lo(.L1)
.option pop
@ -94,7 +94,7 @@ procedure _FPC_proc_haltproc(e:longint); cdecl; public name '_haltproc';
.option push
.option norelax
.L1:
auipc gp, %pcrel_hi(BSS_START+0x800)
auipc gp, %pcrel_hi(global_pointer)
addi gp, gp, %pcrel_lo(.L1)
.option pop
jalr x0, x1

View File

@ -19,7 +19,7 @@ var
dlexitproc : pointer;
var
BSS_START: record end; external name '__bss_start';
global_pointer: record end; external name '__global_pointer$';
STACK_PTR: record end; external name '__stkptr';
procedure _FPC_rv_enter(sp: pptruint);
@ -42,7 +42,7 @@ procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
.option push
.option norelax
.L1:
auipc gp, %pcrel_hi(BSS_START+0x800)
auipc gp, %pcrel_hi(global_pointer)
addi gp, gp, %pcrel_lo(.L1)
.option pop