Skip to content

Commit 2e8be2f

Browse files
committed
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 parent b00f586 commit 2e8be2f

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

lib/std/start.zig

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,27 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv
224224
}
225225

226226
fn _start() callconv(.Naked) noreturn {
227+
// It's important that all checks here are comptime since having any kind of non-asm code in a
228+
// naked function is asking for trouble with some LLVM backends.
229+
227230
// TODO set Top of Stack on non x86_64-plan9
228-
if (native_os == .plan9 and native_arch == .x86_64) {
231+
if (comptime native_os == .plan9 and native_arch == .x86_64) {
229232
// from /sys/src/libc/amd64/main9.s
230233
std.os.plan9.tos = asm volatile (""
231234
: [tos] "={rax}" (-> *std.os.plan9.Tos),
232235
);
233236
}
234237

238+
// Move this to the riscv prong below when this is resolved: https://github.com/ziglang/zig/issues/20918
239+
if (comptime builtin.cpu.arch.isRISCV() and builtin.zig_backend != .stage2_riscv64) asm volatile (
240+
\\ .weak __global_pointer$
241+
\\ .hidden __global_pointer$
242+
\\ .option push
243+
\\ .option norelax
244+
\\ lla gp, __global_pointer$
245+
\\ .option pop
246+
);
247+
235248
// Note that we maintain a very low level of trust with regards to ABI guarantees at this point.
236249
// We will redundantly align the stack, clear the link register, etc. While e.g. the Linux
237250
// kernel is usually good about upholding the ABI guarantees, the same cannot be said of dynamic
@@ -275,24 +288,19 @@ fn _start() callconv(.Naked) noreturn {
275288
\\ and sp, #-16
276289
\\ b %[posixCallMainAndExit]
277290
,
278-
// zig fmt: off
279291
.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-
++
292+
// The CSKY ABI assumes that `gb` is set to the address of the GOT in order for
293+
// position-independent code to work. We depend on this in `std.os.linux.start_pie`
294+
// to locate `_DYNAMIC` as well.
295+
\\ grs t0, 1f
296+
\\ 1:
297+
\\ lrw gb, 1b@GOTPC
298+
\\ addu gb, t0
290299
\\ movi lr, 0
291300
\\ mov a0, sp
292301
\\ andi sp, sp, -8
293302
\\ jmpi %[posixCallMainAndExit]
294303
,
295-
// zig fmt: on
296304
.hexagon =>
297305
// r29 = SP, r30 = FP
298306
\\ r30 = #0
@@ -308,27 +316,13 @@ fn _start() callconv(.Naked) noreturn {
308316
\\ bstrins.d $sp, $zero, 3, 0
309317
\\ b %[posixCallMainAndExit]
310318
,
311-
// zig fmt: off
312319
.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-
++
325320
\\ li s0, 0
326321
\\ li ra, 0
327322
\\ mv a0, sp
328323
\\ andi sp, sp, -16
329324
\\ tail %[posixCallMainAndExit]@plt
330325
,
331-
// zig fmt: off
332326
.m68k =>
333327
// Note that the - 8 is needed because pc in the jsr instruction points into the middle
334328
// of the jsr instruction. (The lea is 6 bytes, the jsr is 4 bytes.)

0 commit comments

Comments
 (0)