bootstrap: work around GCC bug

GCC bug 119085, which affects versions 13.0--15.1, is being triggered by
the C backend, resulting in a (small but critical) miscompilation in
zig2. Unfortunately, we can't really work around that bug without a
command-line flag, because the bug is sensitive to things like the order
in which `struct` types are defined.

Therefore, I have added an option to `bootstrap.c` which causes it to
pass the appropriate flag to `gcc` to disable the optimization pass in
question. This flag can likely be removed in future, once the affected
GCC versions become less common.

At least one of our x86_64-linux CI machines is using an affected GCC
version, so I also updated the relevant CI script to pass this flag when
testing no-LLVM bootstrap.

CMakeLists also has a workaround, but no user intervention is required
there: the GCC version range is automatically detected by CMakeLists.
This commit is contained in:
Matthew Lugg
2026-03-04 10:52:54 +00:00
parent c73db56b45
commit a5219cd288
3 changed files with 33 additions and 3 deletions
+10 -2
View File
@@ -330,7 +330,6 @@ set(ZIG_STAGE2_SOURCES
src/Air/Liveness.zig
src/Air/Liveness/Verify.zig
src/Air/print.zig
src/Air/types_resolved.zig
src/Builtin.zig
src/Compilation.zig
src/Compilation/Config.zig
@@ -344,6 +343,7 @@ set(ZIG_STAGE2_SOURCES
src/Sema.zig
src/Sema/bitcast.zig
src/Sema/comptime_ptr_access.zig
src/Sema/type_resolution.zig
src/Type.zig
src/Value.zig
src/Zcu.zig
@@ -360,7 +360,8 @@ set(ZIG_STAGE2_SOURCES
src/codegen/aarch64/Mir.zig
src/codegen/aarch64/Select.zig
src/codegen/c.zig
src/codegen/c/Type.zig
src/codegen/c/type.zig
src/codegen/c/type/render_defs.zig
src/codegen/llvm.zig
src/codegen/llvm/bindings.zig
src/crash_report.zig
@@ -375,6 +376,7 @@ set(ZIG_STAGE2_SOURCES
src/libs/libunwind.zig
src/link.zig
src/link/C.zig
src/link/ConstPool.zig
src/link/Coff.zig
src/link/Dwarf.zig
src/link/Elf.zig
@@ -623,6 +625,12 @@ else()
else()
set(ZIG2_LINK_FLAGS "-Wl,-z,stack-size=0x10000000")
endif()
# Prevent GCC from miscompiling 'zig2.c'. See also 'workaround_gcc_sra_miscomp' in 'bootstrap.c'.
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0" AND
CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "15.2")
set(ZIG2_COMPILE_FLAGS "${ZIG2_COMPILE_FLAGS} -fno-tree-sra")
endif()
endif()
set(ZIG1_WASM_MODULE "${PROJECT_SOURCE_DIR}/stage1/zig1.wasm")
+21
View File
@@ -102,6 +102,26 @@ int main(int argc, char **argv) {
const char *cc = get_c_compiler();
const char *host_triple = get_host_triple();
// GCC versions 13.0--14.1 have a miscompilation where some bytes of a union may get clobbered
// depending on the union layout and the order in which types are defined. This miscompilation
// affects the output of the C backend, and thus can affect the bootstrap process. Specifically,
// we observe that using the self-hosted x86_64 backend in 'zig2' will cause all function calls
// to be relocated incorrectly, causing immediate crashes on any binary produced by it.
//
// The only reliable workaround for this bug is to disable the optimization pass containing it,
// so here we check for a CLI flag requesting that workaround.
//
// The upstream bug is fixed in GCC version 15.2 onwards (and was also backported to the 13 and
// 14 branches). Once this bug is no longer widespread, we can remove this CLI flag.
//
// Upstream bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119085
bool workaround_gcc_sra_miscomp = false;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--workaround-gcc-sra-miscomp")) {
workaround_gcc_sra_miscomp = true;
}
}
{
const char *child_argv[] = {
cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
@@ -193,6 +213,7 @@ int main(int argc, char **argv) {
#if defined(__GNUC__)
"-pthread",
#endif
workaround_gcc_sra_miscomp ? "-fno-tree-sra" : NULL,
NULL,
};
print_and_run(child_argv);
+2 -1
View File
@@ -21,7 +21,8 @@ export ZIG_LOCAL_CACHE_DIR="$PWD/zig-local-cache"
# Test building from source without LLVM.
cc -o bootstrap bootstrap.c
./bootstrap
# See comments in bootstrap.c for an explanation of the flag given here.
./bootstrap --workaround-gcc-sra-miscomp
./zig2 build -Dno-lib
./zig-out/bin/zig test test/behavior.zig