authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-12 22:09:45+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-15 10:56:32+02:00
logab9628dd2c7441e2cc648874583cc38a357998a1
tree15ca0e7206dfddfde1dee103253a9a85fdce1928
parent5d080e25d5b9b69fda4fd9d340cc9571fc0d0939
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

start: Avoid concatenating strings for inline asm.

For csky, we can just always do the gb initialization. For riscv, the gp code is temporarily pulled above the main switch until the blocking issue is resolved.

1 files changed, 17 insertions(+), 26 deletions(-)

lib/std/start.zig+17-26
......@@ -232,6 +232,16 @@ fn _start() callconv(.Naked) noreturn {
232232 );
233233 }
234234
235 // Move this to the riscv prong below when this is resolved: https://github.com/ziglang/zig/issues/20918
236 if (builtin.cpu.arch.isRISCV() and builtin.zig_backend != .stage2_riscv64) asm volatile (
237 \\ .weak __global_pointer$
238 \\ .hidden __global_pointer$
239 \\ .option push
240 \\ .option norelax
241 \\ lla gp, __global_pointer$
242 \\ .option pop
243 );
244
235245 // Note that we maintain a very low level of trust with regards to ABI guarantees at this point.
236246 // We will redundantly align the stack, clear the link register, etc. While e.g. the Linux
237247 // kernel is usually good about upholding the ABI guarantees, the same cannot be said of dynamic
......@@ -275,24 +285,19 @@ fn _start() callconv(.Naked) noreturn {
275285 \\ and sp, #-16
276286 \\ b %[posixCallMainAndExit]
277287 ,
278 // zig fmt: off
279288 .csky =>
280 if (builtin.position_independent_code)
281 // The CSKY ABI assumes that `gb` is set to the address of the GOT in order for
282 // position-independent code to work. We depend on this in `std.os.linux.start_pie`
283 // to locate `_DYNAMIC` as well.
284 \\ grs t0, 1f
285 \\ 1:
286 \\ lrw gb, 1b@GOTPC
287 \\ addu gb, t0
288 else ""
289 ++
289 // The CSKY ABI assumes that `gb` is set to the address of the GOT in order for
290 // position-independent code to work. We depend on this in `std.os.linux.start_pie`
291 // to locate `_DYNAMIC` as well.
292 \\ grs t0, 1f
293 \\ 1:
294 \\ lrw gb, 1b@GOTPC
295 \\ addu gb, t0
290296 \\ movi lr, 0
291297 \\ mov a0, sp
292298 \\ andi sp, sp, -8
293299 \\ jmpi %[posixCallMainAndExit]
294300 ,
295 // zig fmt: on
296301 .hexagon =>
297302 // r29 = SP, r30 = FP
298303 \\ r30 = #0
......@@ -308,27 +313,13 @@ fn _start() callconv(.Naked) noreturn {
308313 \\ bstrins.d $sp, $zero, 3, 0
309314 \\ b %[posixCallMainAndExit]
310315 ,
311 // zig fmt: off
312316 .riscv32, .riscv64 =>
313 // The self-hosted riscv64 backend is not able to assemble this yet.
314 if (builtin.zig_backend != .stage2_riscv64)
315 // The RISC-V ELF ABI assumes that `gp` is set to the value of `__global_pointer$` at
316 // startup in order for GP relaxation to work, even in static builds.
317 \\ .weak __global_pointer$
318 \\ .hidden __global_pointer$
319 \\ .option push
320 \\ .option norelax
321 \\ lla gp, __global_pointer$
322 \\ .option pop
323 else ""
324 ++
325317 \\ li s0, 0
326318 \\ li ra, 0
327319 \\ mv a0, sp
328320 \\ andi sp, sp, -16
329321 \\ tail %[posixCallMainAndExit]@plt
330322 ,
331 // zig fmt: on
332323 .m68k =>
333324 // Note that the - 8 is needed because pc in the jsr instruction points into the middle
334325 // of the jsr instruction. (The lea is 6 bytes, the jsr is 4 bytes.)