From c2a13bc9c09b6c0588cf4a00ec02b2503f0e84c6 Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 02:34:39 +0100 Subject: [PATCH] Forward the musl SDK sysroot to ld and supply the stack-protector thunk --- musl/PKGBUILD.vile | 18 ++++++--- tests/toolchain/musl-sysroot.v | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 tests/toolchain/musl-sysroot.v diff --git a/musl/PKGBUILD.vile b/musl/PKGBUILD.vile index 96bcf99..522d673 100644 --- a/musl/PKGBUILD.vile +++ b/musl/PKGBUILD.vile @@ -8,7 +8,7 @@ prefix := if string?(cross-prefix) { cross-prefix } else { "" } defpkgbuild(musl, #:pkgname, "musl", #:pkgver, "1.2.6", - #:pkgrel, 1, + #:pkgrel, 2, #:pkgdesc, "Vinix C runtime, development headers and GCC specs", #:arch, [#:x86-64, #:aarch64], #:url, "https://musl.libc.org/", @@ -19,14 +19,16 @@ defpkgbuild(musl, "patches/fix-loongarch64-zero-len-extcontext.patch", "patches/CVE-2026-6042.patch", "patches/CVE-2026-40200.patch", - "patches/malloc-retain.patch"], + "patches/malloc-retain.patch", + "https://raw.githubusercontent.com/alpinelinux/aports/284d827a2793963e6dcf88324bd1eb81adff579c/main/musl/__stack_chk_fail_local.c"], #:sha256sums, ["d585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a", "2a65e03b94217063d72fe998e5fb2e25cd01716078a3ba1b0ab574ab205111e6", "b2067680c66af9eb9f4fdc1fa5d9337a9fd62bdab2c437b416aa97d79eb884ee", "92c8e965798ed516c15cb2a4e7c4ac45ec1eb68b6abff16c73adff2a3bae1b14", "1d0be2e72b9d5bd16546b923aa8af861d271322f01644716a81823bec4065c99", "1ee29f64f9ca8e8ad7c349779d661ff6b52126a27575d3586981357a52c406fb", - "ec459e48e5c3c9946c91e805428149802d0ab1835c64e5e023811ab54a48af1d"], + "ec459e48e5c3c9946c91e805428149802d0ab1835c64e5e023811ab54a48af1d", + "299a7d75a09de3e2e11e7fb4acc3182e4a14e868093d2f30938fce9bfcff13da"], #:prepare, fn() { for-each(fn(patch) { @@ -44,9 +46,12 @@ defpkgbuild(musl, string-append("--target=", target-arch, "-linux-musl"), "--prefix=/usr", "--syslibdir=/lib", "--enable-wrapper=gcc", "--enable-optimize=internal,malloc,malloc/mallocng/*.c,string") run("make", "WRAPCC_GCC=gcc") + run(string-append(prefix, "gcc"), "-Os", "-fPIC", "-fno-stack-protector", "-c", "__stack_chk_fail_local.c", "-o", "__stack_chk_fail_local.o") + run(string-append(prefix, "ar"), "rcsD", "libssp_nonshared.a", "__stack_chk_fail_local.o") }, #:package, fn() { run("make", "install", string-append("DESTDIR=", pkgdir())) + run("install", "-m644", "libssp_nonshared.a", string-append(pkgdir(), "/usr/lib/libssp_nonshared.a")) ; The SONAME, ELF interpreter and development linker name refer to one file. loader-name := string-append("ld-musl-", target-arch, ".so.1") loader := string-append(pkgdir(), "/lib/", loader-name) @@ -55,9 +60,12 @@ defpkgbuild(musl, run("mv", devlib, loader) run("ln", "-s", loader-name, string-append(pkgdir(), "/lib/libc.musl-", target-arch, ".so.1")) run("ln", "-s", string-append("../../lib/", loader-name), devlib) - ; GCC expands %R to its --sysroot. Leave the interpreter path guest-absolute. + ; Pass the sysroot to GNU ld as well, so absolute paths in target linker + ; scripts resolve inside the SDK. Keep the interpreter path guest-absolute. specs := string-append(pkgdir(), "/usr/lib/musl-gcc-sysroot.specs") run("cp", string-append(pkgdir(), "/usr/lib/musl-gcc.specs"), specs) - run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g", specs) + run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g", + "-e", "s|^-dynamic-linker |--sysroot=%R -dynamic-linker |", specs) + run("grep", "-F", "--", string-append("--sysroot=%R -dynamic-linker /lib/", loader-name), specs) run("install", "-Dm644", "musl-1.2.6/COPYRIGHT", string-append(pkgdir(), "/usr/share/licenses/musl/COPYRIGHT")) }) diff --git a/tests/toolchain/musl-sysroot.v b/tests/toolchain/musl-sysroot.v new file mode 100644 index 0000000..337b191 --- /dev/null +++ b/tests/toolchain/musl-sysroot.v @@ -0,0 +1,70 @@ +// Check GNU ld's handling of absolute filenames in a musl SDK linker script. +module main + +import os + +fn checked(argv []string) ! { + result := os.exec(argv) + if result.exit_code != 0 { return error('${argv}: ${result.output}') } +} + +fn check(arch string, sdk string, work string) ! { + if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(sdk) || !os.is_abs_path(work) { + return error('expected a supported architecture and absolute SDK and work paths') + } + os.mkdir(work)! + root := os.join_path(work, 'sysroot') + lib := os.join_path(root, 'usr/lib') + os.mkdir_all(lib)! + checked(['cp', '-a', os.join_path(sdk, 'lib'), root])! + for name in ['musl-gcc-sysroot.specs', 'Scrt1.o', 'crti.o', 'crtn.o', 'libc.so', 'libssp_nonshared.a'] { + checked(['cp', '-a', os.join_path(sdk, 'usr/lib', name), lib])! + } + cc := [os.getenv('CROSS_COMPILE') + 'gcc', '--sysroot=' + root, '-specs=' + + os.join_path(lib, 'musl-gcc-sysroot.specs')] + provider := os.join_path(work, 'provider.c') + consumer := os.join_path(work, 'consumer.c') + os.write_file(provider, 'int vinix_sdk_probe(void) { return 42; }\n')! + os.write_file(consumer, 'extern void __stack_chk_fail_local(void);\nint vinix_sdk_probe(void);\nint main(void) { if (vinix_sdk_probe() != 42) __stack_chk_fail_local(); return 0; }\n')! + soname := 'libvinix-sdk-probe.so.1' + target_dso := os.join_path(lib, soname) + checked([...cc, '-shared', '-fPIC', '-Wl,-soname,' + soname, provider, '-o', target_dso])! + linker_script := os.join_path(lib, 'libvinix-sdk-probe.so') + os.write_file(linker_script, 'GROUP ( /usr/lib/${soname} AS_NEEDED ( -lc ) )\n')! + app := os.join_path(work, 'consumer') + link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app] + checked(link)! + mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path', + lib + ':' + os.join_path(root, 'lib'), app] + if arch != os.uname().machine { run.prepend('qemu-' + arch) } + checked(run)! + map_text := os.read_file(os.join_path(work, 'consumer.map'))! + if !map_text.contains(os.join_path(lib, 'libssp_nonshared.a(__stack_chk_fail_local.o)')) { + return error('expected the packaged libssp_nonshared.a to supply __stack_chk_fail_local') + } + println('The linked stack-protector thunk came from the packaged development archive.') + println('${arch} linked and ran the target library from GROUP(/usr/lib/${soname} AS_NEEDED(-lc)).') + + // The same target-compatible ELF is now accessible only by a host path. + host_dso := os.join_path(work, soname) + os.rename(target_dso, host_dso)! + os.rm(app)! + os.write_file(linker_script, 'GROUP ( "${host_dso}" AS_NEEDED ( -lc ) )\n')! + result := os.exec(link) + if result.exit_code == 0 || !result.output.contains(host_dso) || !result.output.contains(root) { + return error('expected a sysroot lookup failure for the host-only library: ${result.output}') + } + println(result.output.trim_space()) + println('${arch} rejected the same SONAME stored only outside the SDK.') +} + +fn main() { + if os.args.len != 4 { + eprintln('usage: musl-sysroot ARCH INSTALLED_ROOT NEW_WORK_DIR') + exit(2) + } + check(os.args[1], os.args[2], os.args[3]) or { + eprintln(err) + exit(1) + } +} -- 2.56.1