diff --git a/CMakeLists.txt b/CMakeLists.txt index 06cc1cd8dd..146e507930 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/bootstrap.c b/bootstrap.c index 44ba071476..329a1af10e 100644 --- a/bootstrap.c +++ b/bootstrap.c @@ -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); diff --git a/ci/x86_64-linux-release.sh b/ci/x86_64-linux-release.sh index 21411ab2af..0317ad55fc 100755 --- a/ci/x86_64-linux-release.sh +++ b/ci/x86_64-linux-release.sh @@ -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