Forward the musl SDK sysroot to ld and supply the stack-protector thunk
@@ -8,7 +8,7 @@ prefix := if string?(cross-prefix) { cross-prefix } else { "" }
88defpkgbuild(musl,
99 #:pkgname, "musl",
1010 #:pkgver, "1.2.6",
11- #:pkgrel, 1,
11+ #:pkgrel, 2,
1212 #:pkgdesc, "Vinix C runtime, development headers and GCC specs",
1313 #:arch, [#:x86-64, #:aarch64],
1414 #:url, "https://musl.libc.org/",
@@ -19,14 +19,16 @@ defpkgbuild(musl,
1919 "patches/fix-loongarch64-zero-len-extcontext.patch",
2020 "patches/CVE-2026-6042.patch",
2121 "patches/CVE-2026-40200.patch",
22- "patches/malloc-retain.patch"],
22+ "patches/malloc-retain.patch",
23+ "https://raw.githubusercontent.com/alpinelinux/aports/284d827a2793963e6dcf88324bd1eb81adff579c/main/musl/__stack_chk_fail_local.c"],
2324 #:sha256sums, ["d585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a",
2425 "2a65e03b94217063d72fe998e5fb2e25cd01716078a3ba1b0ab574ab205111e6",
2526 "b2067680c66af9eb9f4fdc1fa5d9337a9fd62bdab2c437b416aa97d79eb884ee",
2627 "92c8e965798ed516c15cb2a4e7c4ac45ec1eb68b6abff16c73adff2a3bae1b14",
2728 "1d0be2e72b9d5bd16546b923aa8af861d271322f01644716a81823bec4065c99",
2829 "1ee29f64f9ca8e8ad7c349779d661ff6b52126a27575d3586981357a52c406fb",
29- "ec459e48e5c3c9946c91e805428149802d0ab1835c64e5e023811ab54a48af1d"],
30+ "ec459e48e5c3c9946c91e805428149802d0ab1835c64e5e023811ab54a48af1d",
31+ "299a7d75a09de3e2e11e7fb4acc3182e4a14e868093d2f30938fce9bfcff13da"],
3032
3133 #:prepare, fn() {
3234 for-each(fn(patch) {
@@ -44,9 +46,12 @@ defpkgbuild(musl,
4446 string-append("--target=", target-arch, "-linux-musl"), "--prefix=/usr", "--syslibdir=/lib", "--enable-wrapper=gcc",
4547 "--enable-optimize=internal,malloc,malloc/mallocng/*.c,string")
4648 run("make", "WRAPCC_GCC=gcc")
49+ run(string-append(prefix, "gcc"), "-Os", "-fPIC", "-fno-stack-protector", "-c", "__stack_chk_fail_local.c", "-o", "__stack_chk_fail_local.o")
50+ run(string-append(prefix, "ar"), "rcsD", "libssp_nonshared.a", "__stack_chk_fail_local.o")
4751 },
4852 #:package, fn() {
4953 run("make", "install", string-append("DESTDIR=", pkgdir()))
54+ run("install", "-m644", "libssp_nonshared.a", string-append(pkgdir(), "/usr/lib/libssp_nonshared.a"))
5055 ; The SONAME, ELF interpreter and development linker name refer to one file.
5156 loader-name := string-append("ld-musl-", target-arch, ".so.1")
5257 loader := string-append(pkgdir(), "/lib/", loader-name)
@@ -55,9 +60,12 @@ defpkgbuild(musl,
5560 run("mv", devlib, loader)
5661 run("ln", "-s", loader-name, string-append(pkgdir(), "/lib/libc.musl-", target-arch, ".so.1"))
5762 run("ln", "-s", string-append("../../lib/", loader-name), devlib)
58- ; GCC expands %R to its --sysroot. Leave the interpreter path guest-absolute.
63+ ; Pass the sysroot to GNU ld as well, so absolute paths in target linker
64+ ; scripts resolve inside the SDK. Keep the interpreter path guest-absolute.
5965 specs := string-append(pkgdir(), "/usr/lib/musl-gcc-sysroot.specs")
6066 run("cp", string-append(pkgdir(), "/usr/lib/musl-gcc.specs"), specs)
61- run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g", specs)
67+ run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g",
68+ "-e", "s|^-dynamic-linker |--sysroot=%R -dynamic-linker |", specs)
69+ run("grep", "-F", "--", string-append("--sysroot=%R -dynamic-linker /lib/", loader-name), specs)
6270 run("install", "-Dm644", "musl-1.2.6/COPYRIGHT", string-append(pkgdir(), "/usr/share/licenses/musl/COPYRIGHT"))
6371 })
@@ -0,0 +1,70 @@
1+// Check GNU ld's handling of absolute filenames in a musl SDK linker script.
2+module main
3+
4+import os
5+
6+fn checked(argv []string) ! {
7+ result := os.exec(argv)
8+ if result.exit_code != 0 { return error('${argv}: ${result.output}') }
9+}
10+
11+fn check(arch string, sdk string, work string) ! {
12+ if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(sdk) || !os.is_abs_path(work) {
13+ return error('expected a supported architecture and absolute SDK and work paths')
14+ }
15+ os.mkdir(work)!
16+ root := os.join_path(work, 'sysroot')
17+ lib := os.join_path(root, 'usr/lib')
18+ os.mkdir_all(lib)!
19+ checked(['cp', '-a', os.join_path(sdk, 'lib'), root])!
20+ for name in ['musl-gcc-sysroot.specs', 'Scrt1.o', 'crti.o', 'crtn.o', 'libc.so', 'libssp_nonshared.a'] {
21+ checked(['cp', '-a', os.join_path(sdk, 'usr/lib', name), lib])!
22+ }
23+ cc := [os.getenv('CROSS_COMPILE') + 'gcc', '--sysroot=' + root, '-specs=' +
24+ os.join_path(lib, 'musl-gcc-sysroot.specs')]
25+ provider := os.join_path(work, 'provider.c')
26+ consumer := os.join_path(work, 'consumer.c')
27+ os.write_file(provider, 'int vinix_sdk_probe(void) { return 42; }\n')!
28+ 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')!
29+ soname := 'libvinix-sdk-probe.so.1'
30+ target_dso := os.join_path(lib, soname)
31+ checked([...cc, '-shared', '-fPIC', '-Wl,-soname,' + soname, provider, '-o', target_dso])!
32+ linker_script := os.join_path(lib, 'libvinix-sdk-probe.so')
33+ os.write_file(linker_script, 'GROUP ( /usr/lib/${soname} AS_NEEDED ( -lc ) )\n')!
34+ app := os.join_path(work, 'consumer')
35+ link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app]
36+ checked(link)!
37+ mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path',
38+ lib + ':' + os.join_path(root, 'lib'), app]
39+ if arch != os.uname().machine { run.prepend('qemu-' + arch) }
40+ checked(run)!
41+ map_text := os.read_file(os.join_path(work, 'consumer.map'))!
42+ if !map_text.contains(os.join_path(lib, 'libssp_nonshared.a(__stack_chk_fail_local.o)')) {
43+ return error('expected the packaged libssp_nonshared.a to supply __stack_chk_fail_local')
44+ }
45+ println('The linked stack-protector thunk came from the packaged development archive.')
46+ println('${arch} linked and ran the target library from GROUP(/usr/lib/${soname} AS_NEEDED(-lc)).')
47+
48+ // The same target-compatible ELF is now accessible only by a host path.
49+ host_dso := os.join_path(work, soname)
50+ os.rename(target_dso, host_dso)!
51+ os.rm(app)!
52+ os.write_file(linker_script, 'GROUP ( "${host_dso}" AS_NEEDED ( -lc ) )\n')!
53+ result := os.exec(link)
54+ if result.exit_code == 0 || !result.output.contains(host_dso) || !result.output.contains(root) {
55+ return error('expected a sysroot lookup failure for the host-only library: ${result.output}')
56+ }
57+ println(result.output.trim_space())
58+ println('${arch} rejected the same SONAME stored only outside the SDK.')
59+}
60+
61+fn main() {
62+ if os.args.len != 4 {
63+ eprintln('usage: musl-sysroot ARCH INSTALLED_ROOT NEW_WORK_DIR')
64+ exit(2)
65+ }
66+ check(os.args[1], os.args[2], os.args[3]) or {
67+ eprintln(err)
68+ exit(1)
69+ }
70+}