From 78e581b86f11bfc8da277cbad7b7a30941e55e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 30 Jul 2024 04:02:32 +0200 Subject: [PATCH 1/3] start: Copy the mips64 comment about gp to the mips32 code. Also, don't incorrectly claim that it's only needed for dynamic linking/PIC. --- lib/std/start.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/start.zig b/lib/std/start.zig index 2a61be46e4..bca0922fb3 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -293,6 +293,7 @@ fn _start() callconv(.Naked) noreturn { \\ .gpword . \\ .gpword %[posixCallMainAndExit] \\ 1: + // The `gp` register on MIPS serves a similar purpose to `r2` (ToC pointer) on PPC64. \\ lw $gp, 0($ra) \\ subu $gp, $ra, $gp \\ lw $25, 4($ra) @@ -314,8 +315,6 @@ fn _start() callconv(.Naked) noreturn { \\ .gpdword %[posixCallMainAndExit] \\ 1: // The `gp` register on MIPS serves a similar purpose to `r2` (ToC pointer) on PPC64. - // We need to set it up in order for dynamically-linked / position-independent code to - // work. \\ ld $gp, 0($ra) \\ dsubu $gp, $ra, $gp \\ ld $25, 8($ra) From 982510f8d565895cd6b467da092d2feb851aabe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 30 Jul 2024 04:02:49 +0200 Subject: [PATCH 2/3] start: Initialize gp to __global_pointer$ on riscv. --- lib/std/start.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/start.zig b/lib/std/start.zig index bca0922fb3..7b68f99b9f 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -273,6 +273,14 @@ fn _start() callconv(.Naked) noreturn { \\ b %[posixCallMainAndExit] , .riscv32, .riscv64 => + // The RISC-V ELF ABI assumes that `gp` is set to the value of `__global_pointer$` at + // startup in order for GP relaxation to work, even in static builds. + \\ .weak __global_pointer$ + \\ .hidden __global_pointer$ + \\ .option push + \\ .option norelax + \\ lla gp, __global_pointer$ + \\ .option pop \\ li s0, 0 \\ li ra, 0 \\ mv a0, sp From 653eb7535514d06aca1eaaf777f82e1ecd968086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 30 Jul 2024 17:42:11 +0200 Subject: [PATCH 3/3] start: Disable the gp initialization code for the self-hosted riscv64 backend. --- lib/std/start.zig | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/std/start.zig b/lib/std/start.zig index 7b68f99b9f..2f92ded569 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -272,21 +272,27 @@ fn _start() callconv(.Naked) noreturn { \\ bstrins.d $sp, $zero, 3, 0 \\ b %[posixCallMainAndExit] , + // zig fmt: off .riscv32, .riscv64 => - // The RISC-V ELF ABI assumes that `gp` is set to the value of `__global_pointer$` at - // startup in order for GP relaxation to work, even in static builds. - \\ .weak __global_pointer$ - \\ .hidden __global_pointer$ - \\ .option push - \\ .option norelax - \\ lla gp, __global_pointer$ - \\ .option pop + // The self-hosted riscv64 backend is not able to assemble this yet. + if (builtin.zig_backend != .stage2_riscv64) + // The RISC-V ELF ABI assumes that `gp` is set to the value of `__global_pointer$` at + // startup in order for GP relaxation to work, even in static builds. + \\ .weak __global_pointer$ + \\ .hidden __global_pointer$ + \\ .option push + \\ .option norelax + \\ lla gp, __global_pointer$ + \\ .option pop + else "" + ++ \\ li s0, 0 \\ li ra, 0 \\ mv a0, sp \\ andi sp, sp, -16 \\ tail %[posixCallMainAndExit]@plt , + // zig fmt: off .m68k => // Note that the - 8 is needed because pc in the jsr instruction points into the middle // of the jsr instruction. (The lea is 6 bytes, the jsr is 4 bytes.)