| ... | ... | @@ -13,6 +13,49 @@ static void panic(const char *reason) { |
| 13 | 13 | abort(); |
| 14 | 14 | } |
| 15 | 15 | |
| 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 | |
| 16 | 59 | #if defined(__WIN32__) |
| 17 | 60 | #error TODO write the functionality for executing child process into this build script |
| 18 | 61 | #else |
| ... | ... | @@ -102,26 +145,6 @@ int main(int argc, char **argv) { |
| 102 | 145 | const char *cc = get_c_compiler(); |
| 103 | 146 | const char *host_triple = get_host_triple(); |
| 104 | 147 | |
| 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 | | |
| 125 | 148 | { |
| 126 | 149 | const char *child_argv[] = { |
| 127 | 150 | cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL, |
| ... | ... | @@ -214,7 +237,12 @@ int main(int argc, char **argv) { |
| 214 | 237 | "-pthread", |
| 215 | 238 | #endif |
| 216 | 239 | "-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 |
| 218 | 246 | NULL, |
| 219 | 247 | }; |
| 220 | 248 | print_and_run(child_argv); |