Commit 7bc78967b4

Alex Rønne Petersen <alex@alexrp.com>
2024-07-22 01:22:47
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().
1 parent ecd459b
Changed files (1)
lib
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)