From 813cf7b45ec3dc7c0b5c4e3fc681ac9026fb56b6 Mon Sep 17 00:00:00 2001
From: florian <florian@freepascal.org>
Date: Sun, 7 Mar 2021 22:28:18 +0000
Subject: [PATCH]   o RiscV64 glibc startup code fixed:     * there is no
 _init/_fini, call libc_csu_* instead     * fix loading of global_pointer

git-svn-id: trunk@48904 -
---
 rtl/linux/riscv64/si_c.inc | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/rtl/linux/riscv64/si_c.inc b/rtl/linux/riscv64/si_c.inc
index 1dd3de3139..942372e934 100644
--- a/rtl/linux/riscv64/si_c.inc
+++ b/rtl/linux/riscv64/si_c.inc
@@ -22,8 +22,10 @@ var
   BSS_START: record end; external name '__bss_start';
   STACK_PTR: record end; external name '__stkptr';
 
-  libc_init_proc: TProcedure; weakexternal name '_init';
-  libc_fini_proc: TProcedure; weakexternal name '_fini';
+  { as we do not call these procedures directly, calling conventions do not matter and
+    even if we did, we use c calling conventions anyways }
+  procedure __libc_csu_init; external name '__libc_csu_init';
+  procedure __libc_csu_fini; external name '__libc_csu_fini';
 
 procedure libc_start_main(main: TProcedure; argc: ptruint; argv: ppchar; init, fini, rtld_fini: TProcedure; stack_end: pointer); cdecl; external name '__libc_start_main';
 procedure libc_exit(code: ptruint); cdecl; external name 'exit';
@@ -41,7 +43,7 @@ procedure _FPC_rv_enter(at_exit: TProcedure; sp: pptruint);
     operatingsystem_parameter_argv:=argv;
     operatingsystem_parameter_envp:=@sp[argc+2];
 
-    libc_start_main(@PascalMain, argc, argv, libc_init_proc, libc_fini_proc, at_exit, sp);
+    libc_start_main(@PascalMain, argc, argv, @__libc_csu_init, @__libc_csu_fini, at_exit, sp);
   end;
 
 
@@ -51,7 +53,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(BSS_START+0x7f8)
     addi  gp, gp, %pcrel_lo(.L1)
     .option pop
 
@@ -79,3 +81,20 @@ procedure _FPC_proc_haltproc(e:longint); cdecl; public name '_haltproc';
         _FPC_rv_exit(e);
       end;
   end;
+
+
+ procedure initgp; assembler; nostackframe;
+   asm
+   .Linitgp:
+     .option push
+     .option norelax
+   .L1:
+     auipc gp, %pcrel_hi(BSS_START+0x7f8)
+     addi  gp, gp, %pcrel_lo(.L1)
+     .option pop
+     jalr x0, x1
+
+     .section ".preinit_array","aw"
+     .dc.a .Linitgp
+     .text
+ end;