aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-08-01 00:47:05 -0700
committerGitHub <noreply@github.com>2024-08-01 00:47:05 -0700
commit63aa85e7afb37d0b2b5c48309ef1a88d8fd444b5 (patch)
tree65ae842b0bc28adfa9e81eda7dcde46c753501f6
parent8ea323822ba1fc2f54940841b2cedb3494230a3c (diff)
parent653eb7535514d06aca1eaaf777f82e1ecd968086 (diff)
downloadzig-63aa85e7afb37d0b2b5c48309ef1a88d8fd444b5.tar.gz
zig-63aa85e7afb37d0b2b5c48309ef1a88d8fd444b5.zip
Merge pull request #20872 from alexrp/riscv-gp
start: Initialize `gp` to `__global_pointer$` on riscv.
-rw-r--r--lib/std/start.zig17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/std/start.zig b/lib/std/start.zig
index 61e41370b4..512cc8d4bd 100644
--- a/lib/std/start.zig
+++ b/lib/std/start.zig
@@ -272,13 +272,27 @@ fn _start() callconv(.Naked) noreturn {
\\ bstrins.d $sp, $zero, 3, 0
\\ b %[posixCallMainAndExit]
,
+ // zig fmt: off
.riscv32, .riscv64 =>
+ // 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.)
@@ -293,6 +307,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 +329,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)