authorgravatar for michaldrozd@protonmail.chfardragon <michaldrozd@protonmail.ch> 2026-03-20 04:55:51+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-20 04:55:51+01:00
log4acecad93356fe6397b973b51c8752132ef522d8
tree903741876792c117d789365e7c9e9b58234ed926
parent3c8b96df6d146e6ae820f58ec993510d1eedf2c0

bootstrap.c: work around the gcc sra miscomp using preprocessor checks rather than CLI arg (#31589)

Closes #31577 I've also extended the "good" ranges to fit versions >14.3 and >13.4, since both of these branches have the fix merged so if they ever make a dot release it should be good. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31589 Reviewed-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: fardragon <michaldrozd@protonmail.ch> Co-committed-by: fardragon <michaldrozd@protonmail.ch>

3 files changed, 66 insertions(+), 28 deletions(-)

CMakeLists.txt+16-5
......@@ -625,11 +625,22 @@ else()
625625 else()
626626 set(ZIG2_LINK_FLAGS "-Wl,-z,stack-size=0x10000000")
627627 endif()
628 # Prevent GCC from miscompiling 'zig2.c'. See also 'workaround_gcc_sra_miscomp' in 'bootstrap.c'.
629 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
630 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0" AND
631 CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "15.2")
632 set(ZIG2_COMPILE_FLAGS "${ZIG2_COMPILE_FLAGS} -fno-tree-sra")
628 if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
629 # Prevent GCC from miscompiling 'zig2.c'. See also 'GCC_BUG_119085_PRESENT' workaround details in 'bootstrap.c'.
630 if (
631 (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "10.0" AND
632 CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "13.4") OR
633 (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "14.0" AND
634 CMAKE_C_COMPILER_VERSION VERSION_LESS_EQUAL "14.3") OR
635 (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "15.0" AND
636 CMAKE_C_COMPILER_VERSION VERSION_LESS "15.2")
637 )
638 set(ZIG2_COMPILE_FLAGS "${ZIG2_COMPILE_FLAGS} -fno-tree-sra")
639 endif()
640
641 # Prevent GCC from spamming false positive `-Waddress-of-packed-member` warnings in 'zig2.c'.
642 # See also 'GCC_BUG_94081_PRESENT' workaround details in 'bootstrap.c'.
643 set(ZIG2_COMPILE_FLAGS "${ZIG2_COMPILE_FLAGS} -Wno-address-of-packed-member")
633644 endif()
634645endif()
635646
bootstrap.c+49-21
......@@ -13,6 +13,49 @@ static void panic(const char *reason) {
1313 abort();
1414}
1515
16#if defined(__GNUC__) && !defined(__clang__)
17 #define GCC_VERSION (__GNUC__ * 10000 \
18 + __GNUC_MINOR__ * 100 \
19 + __GNUC_PATCHLEVEL__)
20 // GCC versions 10.0--15.1 have a miscompilation where some bytes of a union may get clobbered
21 // depending on the union layout and the order in which types are defined. This miscompilation
22 // affects the output of the C backend, and thus can affect the bootstrap process. Specifically,
23 // we observe that using the self-hosted x86_64 backend in 'zig2' will cause all function calls
24 // to be relocated incorrectly, causing immediate crashes on any binary produced by it.
25 //
26 // The only reliable workaround for this bug is to disable the optimization pass containing it,
27 // so here we detect whether the compiler being used requires that workaround.
28 //
29 // The upstream bug is fixed in GCC version 15.2 onwards (and was also backported to the 13 and
30 // 14 branches, however there are no 13.x and 14.x releases that contains the fix as of now).
31 // Once this bug is no longer widespread, we can remove this workaround.
32 //
33 // Upstream bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119085
34 #if (GCC_VERSION >= 160000) || \
35 (GCC_VERSION >= 150200 && GCC_VERSION < 160000) || \
36 (GCC_VERSION > 140300 && GCC_VERSION < 150000) || \
37 (GCC_VERSION > 130400 && GCC_VERSION < 140000) || \
38 (GCC_VERSION <= 100000)
39 #define GCC_BUG_119085_PRESENT 0
40 #else
41 #define GCC_BUG_119085_PRESENT 1
42 #endif
43
44
45 // GCC doesn't take into account alignment annotations when --Waddress-of-packed-member is
46 // evaluated, which causes false positive warnings when the C backend generates code that takes
47 // the address of a field in a packed struct.
48 //
49 // There is no fix for this as now, so the only way to get rid of the false positive warnings
50 // is to disable the flag altogether.
51 //
52 // Upstream bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94081
53 #define GCC_BUG_94081_PRESENT 1
54#else
55 #define GCC_BUG_94081_PRESENT 0
56 #define GCC_BUG_119085_PRESENT 0
57#endif
58
1659#if defined(__WIN32__)
1760#error TODO write the functionality for executing child process into this build script
1861#else
......@@ -102,26 +145,6 @@ int main(int argc, char **argv) {
102145 const char *cc = get_c_compiler();
103146 const char *host_triple = get_host_triple();
104147
105 // GCC versions 10.0--15.1 have a miscompilation where some bytes of a union may get clobbered
106 // depending on the union layout and the order in which types are defined. This miscompilation
107 // affects the output of the C backend, and thus can affect the bootstrap process. Specifically,
108 // we observe that using the self-hosted x86_64 backend in 'zig2' will cause all function calls
109 // to be relocated incorrectly, causing immediate crashes on any binary produced by it.
110 //
111 // The only reliable workaround for this bug is to disable the optimization pass containing it,
112 // so here we check for a CLI flag requesting that workaround.
113 //
114 // The upstream bug is fixed in GCC version 15.2 onwards (and was also backported to the 13 and
115 // 14 branches). Once this bug is no longer widespread, we can remove this CLI flag.
116 //
117 // Upstream bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119085
118 bool workaround_gcc_sra_miscomp = false;
119 for (int i = 1; i < argc; ++i) {
120 if (!strcmp(argv[i], "--workaround-gcc-sra-miscomp")) {
121 workaround_gcc_sra_miscomp = true;
122 }
123 }
124
125148 {
126149 const char *child_argv[] = {
127150 cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
......@@ -214,7 +237,12 @@ int main(int argc, char **argv) {
214237 "-pthread",
215238#endif
216239 "-fno-strict-aliasing",
217 workaround_gcc_sra_miscomp ? "-fno-tree-sra" : NULL,
240#if GCC_BUG_119085_PRESENT
241 "-fno-tree-sra",
242#endif
243#if GCC_BUG_94081_PRESENT
244 "-Wno-address-of-packed-member",
245#endif
218246 NULL,
219247 };
220248 print_and_run(child_argv);
ci/x86_64-linux-release.sh+1-2
......@@ -21,8 +21,7 @@ export ZIG_LOCAL_CACHE_DIR="$PWD/zig-local-cache"
2121
2222# Test building from source without LLVM.
2323cc -o bootstrap bootstrap.c
24# See comments in bootstrap.c for an explanation of the flag given here.
25./bootstrap --workaround-gcc-sra-miscomp
24./bootstrap
2625./zig2 build -Dno-lib
2726./zig-out/bin/zig test test/behavior.zig
2827