From 7daf0b6f4660245ac518c663f8971aad7e540da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 12 Dec 2025 20:30:50 +0100 Subject: [PATCH] build: use -ffunction-sections -fdata-sections for the Zig compiler on PowerPC It has been observed in practice on powerpc64(le)-linux that zig.o (in various stages and build modes) is large enough that the +/- 32MB branch range of PowerPC is insufficient to reach from one end of the code section to the other. With LLVM 21, this leads to silent miscompiles that then crash at runtime. With LLVM 22, it will at least lead to branch range errors that fail the compilation, but that gets us no closer to a working compiler. By using these options, we give the linker much greater flexibility to move code and data around to satisfy these range constraints; without them, the linker is not allowed to split up the huge code and data sections of zig.o to do so. Similar issues have also been observed on powerpc-linux (32-bit), hexagon-linux, and some variations of mips(64)(el)-linux. But let's be conservative for now; those other targets can be added to the condition later. As a data point to support this change, it's worth noting that LLD started enabling these options for LTO precisely because the resulting large compilation units ran into these range issues. In some abstract sense, Zig can be seen as doing a limited form of "LTO" in the frontend, so it's not surprising that we would hit the same issues. --- CMakeLists.txt | 5 +++++ build.zig | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4de1a9b99198c178c6f8fbfc57b7802dbc311847..fbd8673ce48b00f22cd95d79200de3f27403614f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -787,6 +787,11 @@ else() set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2") set(ZIG1_COMPILE_FLAGS "-std=c99 -Os") set(ZIG2_COMPILE_FLAGS "-std=c99 -O0 -fno-sanitize=undefined -fno-stack-protector") + # Must match the condition in build.zig. + if(ZIG_HOST_TARGET_ARCH MATCHES "^powerpc(64)?(le)?$") + set(ZIG1_COMPILE_FLAGS "${ZIG1_COMPILE_FLAGS} -ffunction-sections -fdata-sections") + set(ZIG2_COMPILE_FLAGS "${ZIG2_COMPILE_FLAGS} -ffunction-sections -fdata-sections") + endif() if(APPLE) set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000") elseif(MINGW) diff --git a/build.zig b/build.zig index 342c2cfada354e9cccda63484ed9252b7bcafe67..2311f879a42153e79056f180fa0d50b381ac9ba0 100644 --- a/build.zig +++ b/build.zig @@ -838,6 +838,12 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerModOptions) *std.Build.Ste }); exe.stack_size = stack_size; + // Must match the condition in CMakeLists.txt. + const function_data_sections = options.target.result.cpu.arch.isPowerPC(); + + exe.link_function_sections = function_data_sections; + exe.link_data_sections = function_data_sections; + return exe; } -- 2.54.0