From 7bc78967b400322a0fc5651f37a1b0428c37fb9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 22 Jul 2024 01:22:47 +0200 Subject: [PATCH] start: Fix _start() to initialize the ToC for powerpc64. The previous version of this function referenced the argc_argv_ptr global variable as an inline asm operand. This caused LLVM to generate prologue code to initialize the ToC so that the global variable can actually be accessed. Ordinarily, there's nothing wrong with that. But _start() is a naked function! This makes it actually super surprising that LLVM did this. It also means that the old version only really worked by accident. Once the reference to the global variable was removed, no ToC was set up, thus violating the calling convention once we got to posixCallMainAndExit(). This then caused any attempt to access global variables here to crash - namely when setting std.os.linux.elf_aux_maybe. The fix is to just initialize the ToC manually in _start(). --- lib/std/start.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/std/start.zig b/lib/std/start.zig index 9da28423e1..28c77fa49e 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -341,6 +341,8 @@ fn _start() callconv(.Naked) noreturn { .powerpc64, .powerpc64le => // Setup the initial stack frame and clear the back chain pointer. // TODO: Support powerpc64 (big endian) on ELFv2. + \\ addis 2, 12, .TOC. - _start@ha + \\ addi 2, 2, .TOC. - _start@l \\ mr 3, 1 \\ li 0, 0 \\ stdu 0, -32(1)