musllvm

musl and LLVM cross-toolchain build system
git clone https://git.daat.foo/musllvm.git
Log | Files | Refs | README | LICENSE

commit b3264a212ed1b7bbecd87417f5eb5e52f3eb9399
parent 7838e7676676f4cd0b1dbf2989e6a75c0c562601
Author: cowmonk <cowmonk@based.pt>
Date:   Mon, 19 Oct 2026 16:12:30 -0700

the toolchain uses current sources and chimera fixes

Diffstat:
M.github/workflows/ci.yml | 8++++++--
M.github/workflows/release.yml | 9+++++++--
MREADME.md | 18+++++++++++++-----
Apatches/README.md | 34++++++++++++++++++++++++++++++++++
Apatches/llvm/0001-llvm-always-set-a-larger-stack-size-explicitly.patch | 36++++++++++++++++++++++++++++++++++++
Apatches/llvm/0002-llvm-musl-workarounds.patch | 32++++++++++++++++++++++++++++++++
Apatches/llvm/0011-compiler-rt-HACK-always-compile-in-gcc_personality_v.patch | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/001-6f6bd4a1896ba0be19168abc1346c8c7e3851709.patch | 31+++++++++++++++++++++++++++++++
Apatches/musl/002-0572555dab1d1e10b5f7351a005ec588cab41e25.patch | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/003-40acb04b2c1291f7d3091c61080109da11eea48b.patch | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/004-67219f0130ec7c876ac0b299046460fad31caabf.patch | 320+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/005-d2f20c49dfb556d9096251aa0acd92ca907b3400.patch | 31+++++++++++++++++++++++++++++++
Apatches/musl/006-228da39e38c1cae13cbe637e771412c1984dba5d.patch | 44++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/007-b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7.patch | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/musl/008-5122f9f3c99fee366167c5de98b31546312921ab.patch | 39+++++++++++++++++++++++++++++++++++++++
Apatches/musl/nxthdr.patch | 31+++++++++++++++++++++++++++++++
Mscripts/build.sh | 21+++++++++++++++++----
Mscripts/config.sh | 24++++++++++++++++++++++--
Msources.list | 6+++---
Mtests/test_driver.py | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
20 files changed, 1041 insertions(+), 20 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml @@ -34,9 +34,13 @@ jobs: - uses: actions/checkout@v4 - name: Install host dependencies run: | + curl --fail --location --show-error --retry 3 https://apt.llvm.org/llvm-snapshot.gpg.key | + sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null + echo 'deb [signed-by=/etc/apt/trusted.gpg.d/apt.llvm.org.asc] https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | + sudo tee /etc/apt/sources.list.d/llvm.list sudo apt-get update - sudo apt-get install -y clang-19 lld-19 llvm-19 llvm-19-dev cmake ninja-build curl xz-utils perl rsync pkg-config qemu-user - echo /usr/lib/llvm-19/bin >> "$GITHUB_PATH" + sudo apt-get install -y clang-21 lld-21 llvm-21 llvm-21-dev cmake ninja-build curl xz-utils perl rsync patch pkg-config qemu-user + echo /usr/lib/llvm-21/bin >> "$GITHUB_PATH" - uses: actions/cache@v4 with: path: sources diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml @@ -10,6 +10,7 @@ on: - scripts/** - '!scripts/test.sh' - files/** + - patches/** - tests/hello.* - tests/cmake/** - .github/workflows/release.yml @@ -46,9 +47,13 @@ jobs: - uses: actions/checkout@v4 - name: Install host dependencies run: | + curl --fail --location --show-error --retry 3 https://apt.llvm.org/llvm-snapshot.gpg.key | + sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null + echo 'deb [signed-by=/etc/apt/trusted.gpg.d/apt.llvm.org.asc] https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | + sudo tee /etc/apt/sources.list.d/llvm.list sudo apt-get update - sudo apt-get install -y clang-19 lld-19 llvm-19 llvm-19-dev cmake ninja-build curl xz-utils perl rsync pkg-config qemu-user - echo /usr/lib/llvm-19/bin >> "$GITHUB_PATH" + sudo apt-get install -y clang-21 lld-21 llvm-21 llvm-21-dev cmake ninja-build curl xz-utils perl rsync patch pkg-config qemu-user + echo /usr/lib/llvm-21/bin >> "$GITHUB_PATH" - uses: actions/cache@v4 with: path: sources diff --git a/README.md b/README.md @@ -23,7 +23,7 @@ the archive you downloaded: tar -xJf musllvm-x86_64-linux-musl-host-x86_64-linux.tar.xz eval "$(musllvm-x86_64-linux-musl/bin/x86_64-linux-musl-env)" -Each archive includes its source pins, licenses and build information under +Each archive includes its source pins, patches, licenses and build information under share/musllvm/. pkg-config is optional and uses the host installation. @@ -32,13 +32,14 @@ dependencies Build on a Linux host with: - clang/llvm Clang 19 or newer, clang++, LLD, LLVM utilities and + clang/llvm Clang 21 or newer, clang++, LLD, LLVM utilities and matching llvm-config and LLVM CMake metadata (system mode) make GNU make cmake 3.20 or newer ninja python3 perl + patch GNU patch curl tar gzip and xz support sha256sum @@ -114,6 +115,8 @@ The configuration also enables the Chimera Linux hardening defaults: -Wl,--no-as-needed or -Wl,-z,lazy override them. The pkg-config wrapper searches the target sysroot. +libc++ uses Chimera's fast hardening mode and typeinfo comparison setting. + For CMake: cmake -S . -B out -G Ninja \ @@ -139,11 +142,16 @@ Downloads enter the cache after checksum verification. make show-config Run the same command after a failed build to resume completed stages. -Changes to the target, source pins, compiler, paths, build recipes or +Changes to the target, source pins, patches, compiler, paths, build recipes or library flags require make clean with the same ARCH, PREFIX and BUILD_DIR. Cleaning removes that toolchain and its work directory, but keeps the downloaded and extracted sources. Changing JOBS does not require cleaning. +The build applies [patches](patches/README.md) before configuring each source. +If a patch changes for an archive already extracted, remove that source's +directory under build/src/ before rebuilding. Patch failures leave the +downloaded archive available for another attempt. + A running build holds a lock in BUILD_DIR/lock. After a forced termination, check that the build process has stopped before removing a leftover lock with rmdir. @@ -173,7 +181,7 @@ publishing ========== [The release workflow](.github/workflows/release.yml) runs on pushes to -main that change sources.list, config.mk, Makefile, scripts/, files/ +main that change sources.list, config.mk, Makefile, scripts/, files/, patches/ or the compiler smoke tests. This covers source version updates and new core components added to the build. README edits do not publish a release. The workflow can also be run manually from main. @@ -195,7 +203,7 @@ The first release still needs verification on GitHub Actions. credits ======== -* [Chimera Linux](https://chimera-linux.org) - libatomic and the libc++ typeinfo setting +* [Chimera Linux](https://chimera-linux.org) - patches, libatomic and toolchain defaults * [CMLFS](https://github.com/dslm4515/CMLFS) - build script references * [Mussel](https://github.com/firasuke/mussel) - musl cross compiler references * [musl.cc](https://musl.cc) - cross compiler resources diff --git a/patches/README.md b/patches/README.md @@ -0,0 +1,34 @@ +patches +======= + +These patches come from Chimera Linux's [cports tree](https://github.com/chimera-linux/cports/tree/8c138d9ee1cebf0f16d32b56193044c20a46a5f4/main) +at 8c138d9ee1cebf0f16d32b56193044c20a46a5f4, checked on 2026-10-19. +The build applies each component's .patch files in filename order. + +llvm +==== + +For LLVM 23.1.3: + +* 0001 gives LLVM worker threads an 8 MiB stack, including on musl hosts. + Its context was updated for LLVM 23; the change matches Chimera's patch. +* 0002 removes musl's large file macros before LLVM declares matching names. +* 0011 builds the C exception personality with LLVM's unwind headers during + the compiler-rt bootstrap. + +The Clang configuration and CMake settings cover Chimera's hardening, +libatomic, library layout and typeinfo defaults. The build also uses +Chimera's fast libc++ hardening mode and disables outline atomics when +building libatomic for aarch64. + +musl +==== + +For musl 1.2.6: + +* 001 through 008 are upstream fixes carried by Chimera for DNS, strptime, + regular expressions, GB18030 conversion, iswalnum and qsort. +* nxthdr.patch fixes signedness and alignment warnings in CMSG_NXTHDR. + +The toolchain keeps musl's stock allocator. Chimera's allocator integration, +PowerPC changes and LLVM sanitizer patches do not apply to this build. diff --git a/patches/llvm/0001-llvm-always-set-a-larger-stack-size-explicitly.patch b/patches/llvm/0001-llvm-always-set-a-larger-stack-size-explicitly.patch @@ -0,0 +1,36 @@ +From 9a866f0faba625016c2ee62d2956ba9a4245cc94 Mon Sep 17 00:00:00 2001 +From: q66 <q66@chimera-linux.org> +Date: Sat, 4 Nov 2023 08:44:01 +0100 +Subject: [PATCH 01/28] llvm: always set a larger stack size explicitly + +--- + llvm/lib/Support/Threading.cpp | 14 -------------- + 1 file changed, 14 deletions(-) + +diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp +index 90352d2f21..59e6705f9b 100644 +--- a/llvm/lib/Support/Threading.cpp ++++ b/llvm/lib/Support/Threading.cpp +@@ -78,20 +78,7 @@ + return std::min((unsigned)MaxThreadCount, ThreadsRequested); + } + +-#if defined(__APPLE__) +- // Darwin's default stack size for threads except the main one is only 512KB, +- // which is not enough for some/many normal LLVM compilations. This implements +- // the same interface as std::thread but requests the same stack size as the +- // main thread (8MB) before creation. + const std::optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024; +-#elif defined(_AIX) +- // On AIX, the default pthread stack size limit is ~192k for 64-bit programs. +- // This limit is easily reached when doing link-time thinLTO. AIX library +- // developers have used 4MB, so we'll do the same. +-const std::optional<unsigned> llvm::thread::DefaultStackSize = 4 * 1024 * 1024; +-#else +-const std::optional<unsigned> llvm::thread::DefaultStackSize; +-#endif + + + #endif +-- +2.52.0 diff --git a/patches/llvm/0002-llvm-musl-workarounds.patch b/patches/llvm/0002-llvm-musl-workarounds.patch @@ -0,0 +1,32 @@ +From 3b558fe3a9ea17b2127fa736e870190974225bf4 Mon Sep 17 00:00:00 2001 +From: q66 <q66@chimera-linux.org> +Date: Sat, 4 Nov 2023 08:50:28 +0100 +Subject: [PATCH 02/28] llvm: musl workarounds + +--- + llvm/include/llvm/Analysis/TargetLibraryInfo.h | 9 +++++++++ + 1 file changed, 9 insertions(+) + +diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.h b/llvm/include/llvm/Analysis/TargetLibraryInfo.h +index 0f98af69f..15ad453fe 100644 +--- a/llvm/include/llvm/Analysis/TargetLibraryInfo.h ++++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.h +@@ -22,6 +22,15 @@ + #include <bitset> + #include <optional> + ++#undef fopen64 ++#undef fseeko64 ++#undef fstat64 ++#undef fstatvfs64 ++#undef ftello64 ++#undef lstat64 ++#undef stat64 ++#undef tmpfile64 ++ + namespace llvm { + + template <typename T> class ArrayRef; +-- +2.52.0 + diff --git a/patches/llvm/0011-compiler-rt-HACK-always-compile-in-gcc_personality_v.patch b/patches/llvm/0011-compiler-rt-HACK-always-compile-in-gcc_personality_v.patch @@ -0,0 +1,51 @@ +From 47cb263b59074bf7ab82c0ddcb389149f99cdd7f Mon Sep 17 00:00:00 2001 +From: q66 <q66@chimera-linux.org> +Date: Sat, 4 Nov 2023 09:04:56 +0100 +Subject: [PATCH 11/28] compiler-rt: HACK: always compile in gcc_personality_v0 + +Looks like HAVE_UNWIND_H is not accounted for when doing the +runtimes build, but this is the only place where it's checked. + +Unconditionally compile it in because some things use it. +--- + compiler-rt/lib/builtins/CMakeLists.txt | 10 ++++------ + compiler-rt/lib/builtins/gcc_personality_v0.c | 2 +- + 2 files changed, 5 insertions(+), 7 deletions(-) + +diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt +index 7b04b8d4c..7215ae3b2 100644 +--- a/compiler-rt/lib/builtins/CMakeLists.txt ++++ b/compiler-rt/lib/builtins/CMakeLists.txt +@@ -235,12 +235,10 @@ if(APPLE) + ) + endif() + +-if (HAVE_UNWIND_H) +- set(GENERIC_SOURCES +- ${GENERIC_SOURCES} +- gcc_personality_v0.c +- ) +-endif () ++set(GENERIC_SOURCES ++ ${GENERIC_SOURCES} ++ gcc_personality_v0.c ++) + + if (NOT FUCHSIA) + set(GENERIC_SOURCES +diff --git a/compiler-rt/lib/builtins/gcc_personality_v0.c b/compiler-rt/lib/builtins/gcc_personality_v0.c +index 6d92a7b24..dde17313b 100644 +--- a/compiler-rt/lib/builtins/gcc_personality_v0.c ++++ b/compiler-rt/lib/builtins/gcc_personality_v0.c +@@ -9,7 +9,7 @@ + #include "int_lib.h" + #include <stddef.h> + +-#include <unwind.h> ++#include "../../../libunwind/include/unwind.h" + #if defined(__arm__) && !defined(__ARM_DWARF_EH__) && \ + !defined(__USING_SJLJ_EXCEPTIONS__) + // When building with older compilers (e.g. clang <3.9), it is possible that we +-- +2.52.0 + diff --git a/patches/musl/001-6f6bd4a1896ba0be19168abc1346c8c7e3851709.patch b/patches/musl/001-6f6bd4a1896ba0be19168abc1346c8c7e3851709.patch @@ -0,0 +1,31 @@ +From 6f6bd4a1896ba0be19168abc1346c8c7e3851709 Mon Sep 17 00:00:00 2001 +From: Liam Wachter <liam@asymmetric.re> +Date: Fri, 20 Mar 2026 12:19:40 -0400 +Subject: dns: fix nameserver OOB read in IPv6-disabled fallback + +In __res_msend_rc(), the IPv6-disabled fallback check uses conf->ns[nns] +inside a loop controlled by i, so it tests a fixed slot instead of +walking configured nameservers. This reads one past the array's size. + +Use conf->ns[i] so the loop correctly detects whether all configured +nameservers are IPv6-only. +--- + src/network/res_msend.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/network/res_msend.c b/src/network/res_msend.c +index fcb52513..51d42ecb 100644 +--- a/src/network/res_msend.c ++++ b/src/network/res_msend.c +@@ -124,7 +124,7 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries, + + /* Handle case where system lacks IPv6 support */ + if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) { +- for (i=0; i<nns && conf->ns[nns].family == AF_INET6; i++); ++ for (i=0; i<nns && conf->ns[i].family == AF_INET6; i++); + if (i==nns) { + pthread_setcancelstate(cs, 0); + return -1; +-- +cgit v1.2.1 + diff --git a/patches/musl/002-0572555dab1d1e10b5f7351a005ec588cab41e25.patch b/patches/musl/002-0572555dab1d1e10b5f7351a005ec588cab41e25.patch @@ -0,0 +1,107 @@ +From 0572555dab1d1e10b5f7351a005ec588cab41e25 Mon Sep 17 00:00:00 2001 +From: Rich Felker <dalias@aerifal.cx> +Date: Sun, 22 Mar 2026 21:32:35 -0400 +Subject: fix incorrect access to tzname[] by strptime %Z conversion specifier + +there are three issues here: + +1. if tzset has not been called (explicitly or implicitly), the +tzname[] array will contain null pointers, and the dereference to +compare against them has undefined behavior (and will fault). + +2. access to tzname[] was performed without the timezone lock held. +this resulted in a data race if the timezone is concurrently changed +from another thread. + +3. due to unintended signedness of the types, the open-coded isalpha +in the non-matching case was wrong and would continue past null +termination. + +to fix the first two issues, the body of the %Z conversion is moved to +__tz.c where it has access to locking, and null checks are added. + +there is probably an argument to be made that the equivalent of tzset +should happen here, but POSIX does not specify that to happen, so in +the absence of an interpretation adding such an allowance or +requirement, it is not done. + +the third issue is fixed just by using the existing isalpha macro. +--- + src/time/__tz.c | 19 +++++++++++++++++++ + src/time/strptime.c | 13 +++---------- + src/time/time_impl.h | 1 + + 3 files changed, 23 insertions(+), 10 deletions(-) + +diff --git a/src/time/__tz.c b/src/time/__tz.c +index 54ed4cf6..cfce268e 100644 +--- a/src/time/__tz.c ++++ b/src/time/__tz.c +@@ -436,3 +436,22 @@ const char *__tm_to_tzname(const struct tm *tm) + UNLOCK(lock); + return p; + } ++ ++int __tzname_to_isdst(const char *restrict *s) ++{ ++ size_t len; ++ int isdst = -1; ++ LOCK(lock); ++ if (tzname[0] && !strncmp(*s, tzname[0], len = strlen(tzname[0]))) { ++ isdst = 0; ++ *s += len; ++ } else if (tzname[1] && !strncmp(*s, tzname[1], len=strlen(tzname[1]))) { ++ isdst = 1; ++ *s += len; ++ } else { ++ /* FIXME: is this supposed to be an error? */ ++ while (isalpha(**s)) ++*s; ++ } ++ UNLOCK(lock); ++ return isdst; ++} +diff --git a/src/time/strptime.c b/src/time/strptime.c +index b1147242..40bb37af 100644 +--- a/src/time/strptime.c ++++ b/src/time/strptime.c +@@ -5,6 +5,7 @@ + #include <stddef.h> + #include <string.h> + #include <strings.h> ++#include "time_impl.h" + + char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm) + { +@@ -207,16 +208,8 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri + s += 5; + break; + case 'Z': +- if (!strncmp(s, tzname[0], len = strlen(tzname[0]))) { +- tm->tm_isdst = 0; +- s += len; +- } else if (!strncmp(s, tzname[1], len=strlen(tzname[1]))) { +- tm->tm_isdst = 1; +- s += len; +- } else { +- /* FIXME: is this supposed to be an error? */ +- while ((*s|32)-'a' <= 'z'-'a') s++; +- } ++ i = __tzname_to_isdst(&s); ++ if (i>=0) tm->tm_isdst = i; + break; + case '%': + if (*s++ != '%') return 0; +diff --git a/src/time/time_impl.h b/src/time/time_impl.h +index f26d8005..ffe5050b 100644 +--- a/src/time/time_impl.h ++++ b/src/time/time_impl.h +@@ -5,6 +5,7 @@ hidden int __month_to_secs(int, int); + hidden long long __year_to_secs(long long, int *); + hidden long long __tm_to_secs(const struct tm *); + hidden const char *__tm_to_tzname(const struct tm *); ++hidden int __tzname_to_isdst(const char *restrict *); + hidden int __secs_to_tm(long long, struct tm *); + hidden void __secs_to_zone(long long, int, int *, long *, long *, const char **); + hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const struct tm *, locale_t, int); +-- +cgit v1.2.1 + diff --git a/patches/musl/003-40acb04b2c1291f7d3091c61080109da11eea48b.patch b/patches/musl/003-40acb04b2c1291f7d3091c61080109da11eea48b.patch @@ -0,0 +1,62 @@ +From 40acb04b2c1291f7d3091c61080109da11eea48b Mon Sep 17 00:00:00 2001 +From: Szabolcs Nagy <nsz@port70.net> +Date: Mon, 23 Mar 2026 17:33:20 +0000 +Subject: regex: reject invalid \digit back reference in BRE + +in BRE \n matches the nth subexpression, but regcomp did not check if +the nth subexpression was complete or not, only that there were more +subexpressions overall than the largest backref. + +fix regcomp to error if the referenced subexpression is incomplete. +the bug could cause an infinite loop in regexec: + + regcomp(&re, "\\(^a*\\1\\)*", 0); + regexec(&re, "aa", 0, 0, 0); + +since BRE has backreferences, any application accepting a BRE from +untrusted sources is already vulnerable to an attacker-controlled +near-infinite (exponential-time) loop, but this particular case where +the loop is actually infinite can and should be avoided. + +ERE is not affected since the language an ERE describes is actually +regular. + +Reported-by: Simon Resch <simon.resch@code-intelligence.com> +--- + src/regex/regcomp.c | 6 ++++++ + 1 file changed, 6 insertions(+) + +diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c +index fb24556e..b4b81968 100644 +--- a/src/regex/regcomp.c ++++ b/src/regex/regcomp.c +@@ -409,6 +409,8 @@ typedef struct { + int position; + /* The highest back reference or -1 if none seen so far. */ + int max_backref; ++ /* Bit mask of submatch IDs that can be back referenced. */ ++ int backref_ok; + /* Compilation flags. */ + int cflags; + } tre_parse_ctx_t; +@@ -769,6 +771,8 @@ static reg_errcode_t marksub(tre_parse_ctx_t *ctx, tre_ast_node_t *node, int sub + node->submatch_id = subid; + node->num_submatches++; + ctx->n = node; ++ if (subid < 10) ++ ctx->backref_ok |= 1<<subid; + return REG_OK; + } + +@@ -864,6 +868,8 @@ static reg_errcode_t parse_atom(tre_parse_ctx_t *ctx, const char *s) + if (!ere && (unsigned)*s-'1' < 9) { + /* back reference */ + int val = *s - '0'; ++ if (!(ctx->backref_ok & 1<<val)) ++ return REG_ESUBREG; + node = tre_ast_new_literal(ctx->mem, BACKREF, val, ctx->position++); + ctx->max_backref = MAX(val, ctx->max_backref); + } else { +-- +cgit v1.2.1 + diff --git a/patches/musl/004-67219f0130ec7c876ac0b299046460fad31caabf.patch b/patches/musl/004-67219f0130ec7c876ac0b299046460fad31caabf.patch @@ -0,0 +1,320 @@ +From 67219f0130ec7c876ac0b299046460fad31caabf Mon Sep 17 00:00:00 2001 +From: Rich Felker <dalias@aerifal.cx> +Date: Mon, 30 Mar 2026 16:00:50 -0400 +Subject: fix pathological slowness & incorrect mappings in iconv gb18030 + decoder + +in order to implement the "UTF" aspect of gb18030 (ability to +represent arbitrary unicode characters not present in the 2-byte +mapping), we have to apply the index obtained from the encoded 4-byte +sequence into the set of unmapped characters. this was done by +scanning repeatedly over the table of mapped characters and counting +off mapped characters below a running index by which to adjust the +running index by on each iteration. this iterative process eventually +leaves us with the value of the Nth unmapped character replacing the +index, but depending on which particular character that is, the number +of iterations needed to find it can be in the tens of thousands, and +each iteration traverses the whole 126x190 table in the inner loop. +this can lead to run times exceeding an entire second per character on +moderate-speed machines. + +on top of that, the transformation logic produced wrong results for +BMP characters above the the surrogate range, as a result of not +correctly accounting for it being excluded, and for characters outside +the BMP, as a result of a misunderstanding of how gb18030 encodes +them. + +this patch replaces the unmapped character lookup with a single linear +search of a list of unmapped ranges. there are only 206 such ranges, +and these are permanently assigned and unchangeable as a consequence +of the character encoding having to be stable, so a simple array of +16-bit start/length values for each range consumes only 824 bytes, a +very reasonable size cost here. + +this new table accounts for the previously-incorrect surrogate +handling, and non-BMP characters are handled correctly by a single +offset, without the need for any unmapped-range search. + +there are still a small number of mappings that are incorrect due to +late changes made in the definition of gb18030, swapping PUA +codepoints with proper Unicode characters. correcting these requires a +postprocessing step that will be added later. +--- + src/locale/gb18030utf.h | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ + src/locale/iconv.c | 33 +++++--- + 2 files changed, 230 insertions(+), 9 deletions(-) + create mode 100644 src/locale/gb18030utf.h + +diff --git a/src/locale/gb18030utf.h b/src/locale/gb18030utf.h +new file mode 100644 +index 00000000..322a2440 +--- /dev/null ++++ b/src/locale/gb18030utf.h +@@ -0,0 +1,206 @@ ++{ 0x80, 36 }, ++{ 0xa5, 2 }, ++{ 0xa9, 7 }, ++{ 0xb2, 5 }, ++{ 0xb8, 31 }, ++{ 0xd8, 8 }, ++{ 0xe2, 6 }, ++{ 0xeb, 1 }, ++{ 0xee, 4 }, ++{ 0xf4, 3 }, ++{ 0xf8, 1 }, ++{ 0xfb, 1 }, ++{ 0xfd, 4 }, ++{ 0x102, 17 }, ++{ 0x114, 7 }, ++{ 0x11c, 15 }, ++{ 0x12c, 24 }, ++{ 0x145, 3 }, ++{ 0x149, 4 }, ++{ 0x14e, 29 }, ++{ 0x16c, 98 }, ++{ 0x1cf, 1 }, ++{ 0x1d1, 1 }, ++{ 0x1d3, 1 }, ++{ 0x1d5, 1 }, ++{ 0x1d7, 1 }, ++{ 0x1d9, 1 }, ++{ 0x1db, 1 }, ++{ 0x1dd, 28 }, ++{ 0x1fa, 87 }, ++{ 0x252, 15 }, ++{ 0x262, 101 }, ++{ 0x2c8, 1 }, ++{ 0x2cc, 13 }, ++{ 0x2da, 183 }, ++{ 0x3a2, 1 }, ++{ 0x3aa, 7 }, ++{ 0x3c2, 1 }, ++{ 0x3ca, 55 }, ++{ 0x402, 14 }, ++{ 0x450, 1 }, ++{ 0x452, 7102 }, ++{ 0x2011, 2 }, ++{ 0x2017, 1 }, ++{ 0x201a, 2 }, ++{ 0x201e, 7 }, ++{ 0x2027, 9 }, ++{ 0x2031, 1 }, ++{ 0x2034, 1 }, ++{ 0x2036, 5 }, ++{ 0x203c, 112 }, ++{ 0x20ad, 86 }, ++{ 0x2104, 1 }, ++{ 0x2106, 3 }, ++{ 0x210a, 12 }, ++{ 0x2117, 10 }, ++{ 0x2122, 62 }, ++{ 0x216c, 4 }, ++{ 0x217a, 22 }, ++{ 0x2194, 2 }, ++{ 0x219a, 110 }, ++{ 0x2209, 6 }, ++{ 0x2210, 1 }, ++{ 0x2212, 3 }, ++{ 0x2216, 4 }, ++{ 0x221b, 2 }, ++{ 0x2221, 2 }, ++{ 0x2224, 1 }, ++{ 0x2226, 1 }, ++{ 0x222c, 2 }, ++{ 0x222f, 5 }, ++{ 0x2238, 5 }, ++{ 0x223e, 10 }, ++{ 0x2249, 3 }, ++{ 0x224d, 5 }, ++{ 0x2253, 13 }, ++{ 0x2262, 2 }, ++{ 0x2268, 6 }, ++{ 0x2270, 37 }, ++{ 0x2296, 3 }, ++{ 0x229a, 11 }, ++{ 0x22a6, 25 }, ++{ 0x22c0, 82 }, ++{ 0x2313, 333 }, ++{ 0x246a, 10 }, ++{ 0x249c, 100 }, ++{ 0x254c, 4 }, ++{ 0x2574, 13 }, ++{ 0x2590, 3 }, ++{ 0x2596, 10 }, ++{ 0x25a2, 16 }, ++{ 0x25b4, 8 }, ++{ 0x25be, 8 }, ++{ 0x25c8, 3 }, ++{ 0x25cc, 2 }, ++{ 0x25d0, 18 }, ++{ 0x25e6, 31 }, ++{ 0x2607, 2 }, ++{ 0x260a, 54 }, ++{ 0x2641, 1 }, ++{ 0x2643, 2110 }, ++{ 0x2e82, 2 }, ++{ 0x2e85, 3 }, ++{ 0x2e89, 2 }, ++{ 0x2e8d, 10 }, ++{ 0x2e98, 15 }, ++{ 0x2ea8, 2 }, ++{ 0x2eab, 3 }, ++{ 0x2eaf, 4 }, ++{ 0x2eb4, 2 }, ++{ 0x2eb8, 3 }, ++{ 0x2ebc, 14 }, ++{ 0x2ecb, 293 }, ++{ 0x2ffc, 4 }, ++{ 0x3004, 1 }, ++{ 0x3018, 5 }, ++{ 0x301f, 2 }, ++{ 0x302a, 20 }, ++{ 0x303f, 2 }, ++{ 0x3094, 7 }, ++{ 0x309f, 2 }, ++{ 0x30f7, 5 }, ++{ 0x30ff, 6 }, ++{ 0x312a, 246 }, ++{ 0x322a, 7 }, ++{ 0x3232, 113 }, ++{ 0x32a4, 234 }, ++{ 0x3390, 12 }, ++{ 0x339f, 2 }, ++{ 0x33a2, 34 }, ++{ 0x33c5, 9 }, ++{ 0x33cf, 2 }, ++{ 0x33d3, 2 }, ++{ 0x33d6, 113 }, ++{ 0x3448, 43 }, ++{ 0x3474, 298 }, ++{ 0x359f, 111 }, ++{ 0x360f, 11 }, ++{ 0x361b, 765 }, ++{ 0x3919, 85 }, ++{ 0x396f, 96 }, ++{ 0x39d1, 14 }, ++{ 0x39e0, 147 }, ++{ 0x3a74, 218 }, ++{ 0x3b4f, 287 }, ++{ 0x3c6f, 113 }, ++{ 0x3ce1, 885 }, ++{ 0x4057, 264 }, ++{ 0x4160, 471 }, ++{ 0x4338, 116 }, ++{ 0x43ad, 4 }, ++{ 0x43b2, 43 }, ++{ 0x43de, 248 }, ++{ 0x44d7, 373 }, ++{ 0x464d, 20 }, ++{ 0x4662, 193 }, ++{ 0x4724, 5 }, ++{ 0x472a, 82 }, ++{ 0x477d, 16 }, ++{ 0x478e, 441 }, ++{ 0x4948, 50 }, ++{ 0x497b, 2 }, ++{ 0x497e, 4 }, ++{ 0x4984, 1 }, ++{ 0x4987, 20 }, ++{ 0x499c, 3 }, ++{ 0x49a0, 22 }, ++{ 0x49b8, 703 }, ++{ 0x4c78, 39 }, ++{ 0x4ca4, 111 }, ++{ 0x4d1a, 148 }, ++{ 0x4daf, 81 }, ++{ 0x9fa6, 14426 }, ++{ 0xe76c, 1 }, ++{ 0xe7c8, 1 }, ++{ 0xe7e7, 13 }, ++{ 0xe815, 1 }, ++{ 0xe819, 5 }, ++{ 0xe81f, 7 }, ++{ 0xe827, 4 }, ++{ 0xe82d, 4 }, ++{ 0xe833, 8 }, ++{ 0xe83c, 7 }, ++{ 0xe844, 16 }, ++{ 0xe856, 14 }, ++{ 0xe865, 4295 }, ++{ 0xf92d, 76 }, ++{ 0xf97a, 27 }, ++{ 0xf996, 81 }, ++{ 0xf9e8, 9 }, ++{ 0xf9f2, 26 }, ++{ 0xfa10, 1 }, ++{ 0xfa12, 1 }, ++{ 0xfa15, 3 }, ++{ 0xfa19, 6 }, ++{ 0xfa22, 1 }, ++{ 0xfa25, 2 }, ++{ 0xfa2a, 1030 }, ++{ 0xfe32, 1 }, ++{ 0xfe45, 4 }, ++{ 0xfe53, 1 }, ++{ 0xfe58, 1 }, ++{ 0xfe67, 1 }, ++{ 0xfe6c, 149 }, ++{ 0xff5f, 129 }, ++{ 0xffe6, 26 }, +diff --git a/src/locale/iconv.c b/src/locale/iconv.c +index 52178950..4151411d 100644 +--- a/src/locale/iconv.c ++++ b/src/locale/iconv.c +@@ -74,6 +74,10 @@ static const unsigned short gb18030[126][190] = { + #include "gb18030.h" + }; + ++static const unsigned short gb18030utf[][2] = { ++#include "gb18030utf.h" ++}; ++ + static const unsigned short big5[89][157] = { + #include "big5.h" + }; +@@ -224,6 +228,8 @@ static unsigned uni_to_jis(unsigned c) + } + } + ++#define countof(a) (sizeof (a) / sizeof *(a)) ++ + size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb) + { + size_t x=0; +@@ -430,15 +436,24 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri + d = *((unsigned char *)*in + 3); + if (d-'0'>9) goto ilseq; + c += d-'0'; +- c += 128; +- for (d=0; d<=c; ) { +- k = 0; +- for (int i=0; i<126; i++) +- for (int j=0; j<190; j++) +- if (gb18030[i][j]-d <= c-d) +- k++; +- d = c+1; +- c += k; ++ /* Starting at 90 30 81 30 (189000), mapping is ++ * linear without gaps, to U+10000 and up. */ ++ if (c >= 189000) { ++ c -= 189000; ++ c += 0x10000; ++ if (c >= 0x110000) goto ilseq; ++ break; ++ } ++ /* Otherwise we must process an index into set ++ * of characters unmapped by 2-byte table. */ ++ for (int i=0; ; i++) { ++ if (i==countof(gb18030utf)) ++ goto ilseq; ++ if (c<gb18030utf[i][1]) { ++ c += gb18030utf[i][0]; ++ break; ++ } ++ c -= gb18030utf[i][1]; + } + break; + } +-- +cgit v1.2.1 + diff --git a/patches/musl/005-d2f20c49dfb556d9096251aa0acd92ca907b3400.patch b/patches/musl/005-d2f20c49dfb556d9096251aa0acd92ca907b3400.patch @@ -0,0 +1,31 @@ +From d2f20c49dfb556d9096251aa0acd92ca907b3400 Mon Sep 17 00:00:00 2001 +From: Rich Felker <dalias@aerifal.cx> +Date: Thu, 9 Apr 2026 20:23:44 -0400 +Subject: adjust iswalnum to admit tail call to iswalpha + +use of || forces the caller to boolean-normalize the result of +iswalpha to 0 or 1, requiring code after the call returns and thus +precluding a tail call. + +since this isn't actually needed, don't write it that way. +--- + src/ctype/iswalnum.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/src/ctype/iswalnum.c b/src/ctype/iswalnum.c +index 046c399c..6e245e85 100644 +--- a/src/ctype/iswalnum.c ++++ b/src/ctype/iswalnum.c +@@ -2,7 +2,8 @@ + + int iswalnum(wint_t wc) + { +- return iswdigit(wc) || iswalpha(wc); ++ if (iswdigit(wc)) return 1; ++ return iswalpha(wc); + } + + int __iswalnum_l(wint_t c, locale_t l) +-- +cgit v1.2.1 + diff --git a/patches/musl/006-228da39e38c1cae13cbe637e771412c1984dba5d.patch b/patches/musl/006-228da39e38c1cae13cbe637e771412c1984dba5d.patch @@ -0,0 +1,44 @@ +From 228da39e38c1cae13cbe637e771412c1984dba5d Mon Sep 17 00:00:00 2001 +From: Rich Felker <dalias@aerifal.cx> +Date: Thu, 9 Apr 2026 22:51:30 -0400 +Subject: qsort: fix leonardo heap corruption from bug in doubleword ctz + primitive + +the pntz function, implementing a "count trailing zeros" variant for a +bit vector consisting of two size_t words, erroneously returned zero +rather than the number of bits in the low word when the first bit set +was the low bit of the high word. + +as a result, a loop in the trinkle function which should have a +guaranteed small bound on the number of iterations, could run +unboundedly, thereby overflowing a stack-based working-space array +which was sized for the bound. + +CVE-2026-40200 has been assigned for this issue. +--- + src/stdlib/qsort.c | 8 ++++---- + 1 file changed, 4 insertions(+), 4 deletions(-) + +diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c +index ab79dc6f..13219ab3 100644 +--- a/src/stdlib/qsort.c ++++ b/src/stdlib/qsort.c +@@ -34,11 +34,11 @@ + + typedef int (*cmpfun)(const void *, const void *, void *); + ++/* returns index of first bit set, excluding the low bit assumed to always ++ * be set, starting from low bit of p[0] up through high bit of p[1] */ + static inline int pntz(size_t p[2]) { +- int r = ntz(p[0] - 1); +- if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) { +- return r; +- } ++ if (p[0] != 1) return ntz(p[0] - 1); ++ if (p[1]) return 8*sizeof(size_t) + ntz(p[1]); + return 0; + } + +-- +cgit v1.2.1 + diff --git a/patches/musl/007-b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7.patch b/patches/musl/007-b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7.patch @@ -0,0 +1,92 @@ +From b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7 Mon Sep 17 00:00:00 2001 +From: Rich Felker <dalias@aerifal.cx> +Date: Thu, 9 Apr 2026 23:40:53 -0400 +Subject: qsort: hard-preclude oob array writes independent of any invariants + +while the root cause of CVE-2026-40200 was a faulty ctz primitive, the +fallout of the bug would have been limited to erroneous sorting or +infinite loop if not for the stores to a stack-based array that +depended on trusting invariants in order not to go out of bounds. + +increase the size of the array to a power of two so that we can mask +indices into it to force them into range. in the absence of any +further bug, the masking is a no-op, but it does not have any +measurable performance cost, and it makes spatial memory safety +trivial to prove (and for readers not familiar with the algorithms to +trust). +--- + src/stdlib/qsort.c | 20 +++++++++++++------- + 1 file changed, 13 insertions(+), 7 deletions(-) + +diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c +index 13219ab3..e4bce9f7 100644 +--- a/src/stdlib/qsort.c ++++ b/src/stdlib/qsort.c +@@ -89,10 +89,16 @@ static inline void shr(size_t p[2], int n) + p[1] >>= n; + } + ++/* power-of-two length for working array so that we can mask indices and ++ * not depend on any invariant of the algorithm for spatial memory safety. ++ * the original size was just 14*sizeof(size_t)+1 */ ++#define AR_LEN (16 * sizeof(size_t)) ++#define AR_MASK (AR_LEN - 1) ++ + static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[]) + { + unsigned char *rt, *lf; +- unsigned char *ar[14 * sizeof(size_t) + 1]; ++ unsigned char *ar[AR_LEN]; + int i = 1; + + ar[0] = head; +@@ -104,16 +110,16 @@ static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int p + break; + } + if(cmp(lf, rt, arg) >= 0) { +- ar[i++] = lf; ++ ar[i++ & AR_MASK] = lf; + head = lf; + pshift -= 1; + } else { +- ar[i++] = rt; ++ ar[i++ & AR_MASK] = rt; + head = rt; + pshift -= 2; + } + } +- cycle(width, ar, i); ++ cycle(width, ar, i & AR_MASK); + } + + static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], int pshift, int trusty, size_t lp[]) +@@ -121,7 +127,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si + unsigned char *stepson, + *rt, *lf; + size_t p[2]; +- unsigned char *ar[14 * sizeof(size_t) + 1]; ++ unsigned char *ar[AR_LEN]; + int i = 1; + int trail; + +@@ -142,7 +148,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si + } + } + +- ar[i++] = stepson; ++ ar[i++ & AR_MASK] = stepson; + head = stepson; + trail = pntz(p); + shr(p, trail); +@@ -150,7 +156,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si + trusty = 0; + } + if(!trusty) { +- cycle(width, ar, i); ++ cycle(width, ar, i & AR_MASK); + sift(head, width, cmp, arg, pshift, lp); + } + } +-- +cgit v1.2.1 + diff --git a/patches/musl/008-5122f9f3c99fee366167c5de98b31546312921ab.patch b/patches/musl/008-5122f9f3c99fee366167c5de98b31546312921ab.patch @@ -0,0 +1,39 @@ +From 5122f9f3c99fee366167c5de98b31546312921ab Mon Sep 17 00:00:00 2001 +From: Luca Kellermann <mailto.luca.kellermann@gmail.com> +Date: Fri, 10 Apr 2026 03:03:22 +0200 +Subject: qsort: fix shift UB in shl and shr + +if shl() or shr() are called with n==8*sizeof(size_t), n is adjusted +to 0. the shift by (sizeof(size_t) * 8 - n) that then follows will +consequently shift by the width of size_t, which is UB and in practice +produces an incorrect result. + +return early in this case. the bitvector p was already shifted by the +required amount. +--- + src/stdlib/qsort.c | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c +index e4bce9f7..28607450 100644 +--- a/src/stdlib/qsort.c ++++ b/src/stdlib/qsort.c +@@ -71,6 +71,7 @@ static inline void shl(size_t p[2], int n) + n -= 8 * sizeof(size_t); + p[1] = p[0]; + p[0] = 0; ++ if (!n) return; + } + p[1] <<= n; + p[1] |= p[0] >> (sizeof(size_t) * 8 - n); +@@ -83,6 +84,7 @@ static inline void shr(size_t p[2], int n) + n -= 8 * sizeof(size_t); + p[0] = p[1]; + p[1] = 0; ++ if (!n) return; + } + p[0] >>= n; + p[0] |= p[1] << (sizeof(size_t) * 8 - n); +-- +cgit v1.2.1 + diff --git a/patches/musl/nxthdr.patch b/patches/musl/nxthdr.patch @@ -0,0 +1,31 @@ +commit cbad54c13d4c919d2d9b489de526451b8fa00409 +Author: q66 <q66@chimera-linux.org> +Date: Mon Mar 23 18:18:01 2026 +0100 + + silence CMSG_NXTHDR warnings with higher clang warnlevels + + The first line fixes: + + msg.cc:8:15: warning: comparison of integers of different signs: 'unsigned long' and 'long' [-Wsign-compare] + 8 | auto *x = CMSG_NXTHDR(a, b); + + The second line fixes: + + msg.cc:8:15: warning: cast from 'unsigned char *' to 'struct cmsghdr *' increases required alignment from 1 to 4 [-Wcast-align] + 8 | auto *x = CMSG_NXTHDR(a, b); + +diff --git a/include/sys/socket.h b/include/sys/socket.h +index 6dc1e40..b805f7d 100644 +--- a/include/sys/socket.h ++++ b/include/sys/socket.h +@@ -355,8 +355,8 @@ struct linger { + + #define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) + #define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ +- __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ +- ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) ++ __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= (size_t)(__MHDR_END(mhdr) - (unsigned char *)(cmsg)) \ ++ ? 0 : (struct cmsghdr *)(void *)__CMSG_NEXT(cmsg)) + #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + + #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) diff --git a/scripts/build.sh b/scripts/build.sh @@ -7,7 +7,7 @@ TOOLS='clang clang++ ld.lld llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdum doctor() { - for program in make cmake ninja python3 perl tar xz curl sha256sum rsync; do + for program in make cmake ninja python3 perl tar xz curl sha256sum rsync patch; do need "$program" done need "$HOST_CC" @@ -17,7 +17,7 @@ doctor() die "LLVM_MODE=system requires Clang; set HOST_CC=clang" clang_major=$("$HOST_CC" -dumpversion) clang_major=${clang_major%%.*} - [ "$clang_major" -ge 19 ] 2>/dev/null || die "LLVM_MODE=system requires Clang 19 or newer" + [ "$clang_major" -ge 21 ] 2>/dev/null || die "LLVM_MODE=system requires Clang 21 or newer" for program in $TOOLS llvm-config; do need "$program"; done llvm_cmake=$(llvm-config --cmakedir) [ -f "$llvm_cmake/LLVMConfig.cmake" ] || die "LLVM CMake metadata missing; install the matching LLVM development package" @@ -180,12 +180,16 @@ musl() atomic() { if ! done_stage atomic; then + atomic_cflags=$TARGET_CFLAGS + if [ "$ARCH" = aarch64 ]; then + atomic_cflags="$atomic_cflags -mno-outline-atomics" + fi mkdir -p "$BUILD_DIR/atomic" cp "$ATOMIC_SOURCE/atomic.c" "$ATOMIC_SOURCE/Makefile" "$BUILD_DIR/atomic/" make -C "$BUILD_DIR/atomic" -j "$JOBS" CC="$BUILD_DIR/musl-cc" \ - AR="$PREFIX/libexec/llvm-ar" CFLAGS="$TARGET_CFLAGS" + AR="$PREFIX/libexec/llvm-ar" CFLAGS="$atomic_cflags" make -C "$BUILD_DIR/atomic" CC="$BUILD_DIR/musl-cc" \ - AR="$PREFIX/libexec/llvm-ar" CFLAGS="$TARGET_CFLAGS" PREFIX=/usr DESTDIR="$SYSROOT" install + AR="$PREFIX/libexec/llvm-ar" CFLAGS="$atomic_cflags" PREFIX=/usr DESTDIR="$SYSROOT" install mark_stage atomic fi } @@ -200,6 +204,7 @@ runtimes() -DCMAKE_INSTALL_PREFIX=/usr -DLLVM_ENABLE_RUNTIMES='libunwind;libcxxabi;libcxx' \ -DLIBCXX_HAS_MUSL_LIBC=ON -DLIBCXX_USE_COMPILER_RT=ON \ -DLIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION=2 \ + -DLIBCXX_HARDENING_MODE=fast \ -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_ENABLE_SHARED=ON -DLIBCXX_ENABLE_STATIC=ON \ -DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON \ -DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT=ON \ @@ -236,6 +241,9 @@ wrappers() sed -e "s/@TARGET@/$TARGET/g" -e "s/@RESOURCE_VERSION@/$RESOURCE_VERSION/g" \ "$PROOT/files/toolchain/clang.cfg" > "$PREFIX/share/musllvm/clang.cfg" printf '%s\n' "$RESOURCE_VERSION" > "$PREFIX/share/musllvm/resource-version" + if [ -d "$PROOT/patches" ]; then + cp -R "$PROOT/patches" "$PREFIX/share/musllvm/" + fi printf 'Toolchain ready: %s\n' "$PREFIX" } @@ -278,6 +286,11 @@ config_digest=$({ "$BOOTSTRAP_CXX" --version cat "$SOURCES_FILE" cat "$PROOT/scripts/build.sh" "$PROOT/scripts/config.sh" + for source_patch in "$PROOT"/patches/*/*.patch; do + [ -f "$source_patch" ] || continue + printf '%s\n' "${source_patch#"$PROOT/"}" + cat "$source_patch" + done if [ "$LLVM_MODE" = system ]; then for program in $TOOLS llvm-config; do command -v "$program"; done ld.lld --version diff --git a/scripts/config.sh b/scripts/config.sh @@ -64,9 +64,23 @@ extract_source() { source_info "$1" archive_ok || die "missing or corrupt $archive; run make pull" + set -- "$PROOT/patches/$component/"*.patch + source_digest=$digest + if [ -f "$1" ]; then + need patch + source_digest=$({ + printf '%s\n' "$digest" + for source_patch do + printf '%s\n' "${source_patch##*/}" + cat "$source_patch" + done + } | sha256sum) + source_digest=${source_digest%% *} + fi source_path="$PROOT/build/src/$source_name" if [ -f "$source_path/.musllvm-source" ]; then - [ "$(cat "$source_path/.musllvm-source")" = "$digest" ] || die "source digest changed: $source_path" + [ "$(cat "$source_path/.musllvm-source")" = "$source_digest" ] || + die "source or patches changed; remove $source_path and clean the affected toolchain before rebuilding" return fi [ ! -e "$source_path" ] || die "incomplete source directory: $source_path" @@ -80,7 +94,13 @@ extract_source() mkdir "$source_tmp" printf 'Extracting %s\n' "$archive" tar -xf "$SOURCES_DIR/$archive" --strip-components=1 -C "$source_tmp" || die "could not extract $archive" - printf '%s\n' "$digest" > "$source_tmp/.musllvm-source" + for source_patch do + [ -f "$source_patch" ] || continue + printf 'Applying %s\n' "${source_patch##*/}" + patch --batch --forward --fuzz=0 -p1 -d "$source_tmp" < "$source_patch" || + die "could not apply $source_patch" + done + printf '%s\n' "$source_digest" > "$source_tmp/.musllvm-source" mv "$source_tmp" "$source_path" ) } diff --git a/sources.list b/sources.list @@ -1,5 +1,5 @@ # component archive sha256 url -musl musl-1.2.5.tar.gz a9a118bbe84d8764da0ea0d28b3ab3fae8477fc7e4085d90102b8596fc7c75e4 https://musl.libc.org/releases/musl-1.2.5.tar.gz -linux linux-6.17.7.tar.xz ddf2ea0d4439e1d57136be3623102af9458f601f5b1cb77e83246e88aea09d0e https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.17.7.tar.xz -llvm llvm-project-21.1.8.src.tar.xz 4633a23617fa31a3ea51242586ea7fb1da7140e426bd62fc164261fe036aa142 https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.8/llvm-project-21.1.8.src.tar.xz +musl musl-1.2.6.tar.gz d585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a https://musl.libc.org/releases/musl-1.2.6.tar.gz +linux linux-7.3.tar.xz 87363c24f10e06321eb362765521094e94471f6bff8bce2d6b3e40f39f004953 https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.3.tar.xz +llvm llvm-project-23.1.3.src.tar.xz c44186a7762ed28954be72e5ff6df9808e0779d4f1bf014ecc4e7e211d31ee34 https://github.com/llvm/llvm-project/releases/download/llvmorg-23.1.3/llvm-project-23.1.3.src.tar.xz atomic libatomic-chimera-0.90.0.tar.gz fccbd8c0ef7fd473275f835b3fca9275fb27a0c196cdcdff1f6d14ab12ed3a53 https://github.com/chimera-linux/libatomic-chimera/archive/refs/tags/v0.90.0.tar.gz diff --git a/tests/test_driver.py b/tests/test_driver.py @@ -31,10 +31,11 @@ def write(path, value="fixture"): path.write_text(value) if name in ("clang", "clang++"): + version = os.environ.get("FAKE_CLANG_VERSION", "21.1.8") if "--version" in args: - print("clang version 21.1.8 (fixture)") + print("clang version " + version + " (fixture)") elif "-dumpversion" in args: - print("21.1.8") + print(version) elif "-print-resource-dir" in args: print(os.environ["FAKE_RESOURCE"]) elif "-print-target-triple" in args: @@ -171,6 +172,13 @@ class DriverTests(unittest.TestCase): path.write_text(CAPTURE) path.chmod(0o755) + def source_patch(self, name, before="fixture", after="patched"): + path = self.root / "patches/llvm" / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("--- a/placeholder\n+++ b/placeholder\n@@ -1 +1 @@\n-" + before + + "\n\\ No newline at end of file\n+" + after + "\n\\ No newline at end of file\n") + return path + def install_wrapper(self, name, template, prefix=None): prefix = prefix or Path(self.temporary.name) / "relocated tools' path" (prefix / "bin").mkdir(parents=True, exist_ok=True) @@ -198,6 +206,12 @@ class DriverTests(unittest.TestCase): self.run_make("show-config", *arguments, success=False) self.assertFalse((self.root / "build").exists()) + def test_old_system_clang_is_rejected_before_fetch(self): + result = self.run_make("runtimes", success=False, + env={**self.env, "FAKE_CLANG_VERSION": "20.1.8"}) + self.assertIn("requires Clang 21 or newer", result.stdout) + self.assertFalse((self.root / "sources").exists()) + def test_fetch_verifies_and_skips_downloaded_archives(self): self.run_make("pull") self.assertEqual(sum(item["tool"] == "curl" for item in self.commands()), 4) @@ -242,6 +256,53 @@ class DriverTests(unittest.TestCase): self.assertEqual(os.readlink(prefix / "libexec/clang"), "../host/bin/clang") self.assertTrue((prefix / "bin/x86_64-linux-musl-c++").exists()) + def test_source_patches_apply_once_in_order_and_are_installed(self): + self.source_patch("10-second.patch", "first", "second") + self.source_patch("00-first.patch", "fixture", "first") + result = self.run_make("runtimes") + source = self.root / "build/src/llvm-test" + self.assertEqual((source / "placeholder").read_text(), "second") + self.assertLess(result.stdout.index("Applying 00-first.patch"), result.stdout.index("Applying 10-second.patch")) + installed = self.root / "build/x86_64-linux-musl/share/musllvm/patches/llvm/00-first.patch" + self.assertEqual(installed.read_bytes(), (self.root / "patches/llvm/00-first.patch").read_bytes()) + self.assertNotIn("Applying", self.run_make("runtimes", "JOBS=1").stdout) + + def test_changed_or_removed_patch_rejects_cached_sources(self): + patch = self.source_patch("00-fixture.patch") + self.run_make("runtimes") + commands = sum(item["tool"] in ("make", "cmake") for item in self.commands()) + self.source_patch("00-fixture.patch", after="changed") + result = self.run_make("runtimes", success=False) + self.assertIn("source or patches changed", result.stdout) + patch.unlink() + result = self.run_make("runtimes", success=False) + self.assertIn("source or patches changed", result.stdout) + self.assertEqual(sum(item["tool"] in ("make", "cmake") for item in self.commands()), commands) + self.assertEqual((self.root / "build/src/llvm-test/placeholder").read_text(), "patched") + + def test_failed_source_patch_does_not_publish_extraction(self): + self.source_patch("00-broken.patch", before="missing") + result = self.run_make("runtimes", success=False) + self.assertIn("could not apply", result.stdout) + self.assertFalse((self.root / "build/src/llvm-test").exists()) + self.assertFalse((self.root / "build/src/llvm-test.lock").exists()) + self.assertTrue((self.root / "sources/llvm-test.tar.gz").exists()) + self.assertFalse(any(item["tool"] in ("make", "cmake") for item in self.commands())) + + def test_chimera_runtime_settings_preserve_target_flags(self): + for arch in ("x86_64", "aarch64"): + with self.subTest(arch=arch): + self.run_make("runtimes", "ARCH=" + arch, "TARGET_CFLAGS=-Os") + work = self.root / "build/work" / (arch + "-linux-musl") + options = json.loads((work / "runtimes/fixture-config.json").read_text()) + self.assertIn("-DLIBCXX_HARDENING_MODE=fast", options) + self.assertIn("-DLIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION=2", options) + calls = [item for item in self.commands() if item["tool"] == "make" and str(work / "atomic") in item["args"]] + self.assertEqual(len(calls), 2) + for call in calls: + flags = next(argument for argument in call["args"] if argument.startswith("CFLAGS=")) + self.assertEqual(flags, "CFLAGS=-Os" + (" -mno-outline-atomics" if arch == "aarch64" else "")) + def test_clean_preserves_unmarked_directory(self): prefix = self.root / "unrelated" prefix.mkdir()