From 784ae321dc040b6bcd9fe6196f263e0d73e53fcf Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 03:32:42 +0100 Subject: [PATCH] musl: align AArch64 ELF segments for the Vinix kernel --- musl/PKGBUILD.vile | 13 +++++++++++-- tests/toolchain/musl-sysroot.v | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/musl/PKGBUILD.vile b/musl/PKGBUILD.vile index 522d673..7d5fca6 100644 --- a/musl/PKGBUILD.vile +++ b/musl/PKGBUILD.vile @@ -4,11 +4,13 @@ native-arm := memq(string->symbol("aarch64"), features()) target-arch := if string?(selected-arch) { selected-arch } else if native-arm { "aarch64" } else { "x86_64" } cross-prefix := get-environment-variable("CROSS_COMPILE") prefix := if string?(cross-prefix) { cross-prefix } else { "" } +page-flags := if string=?(target-arch, "aarch64") { " -Wl,-z,max-page-size=16384" } else { "" } +release := if string=?(target-arch, "aarch64") { 4 } else { 3 } defpkgbuild(musl, #:pkgname, "musl", #:pkgver, "1.2.6", - #:pkgrel, 2, + #:pkgrel, release, #:pkgdesc, "Vinix C runtime, development headers and GCC specs", #:arch, [#:x86-64, #:aarch64], #:url, "https://musl.libc.org/", @@ -42,7 +44,7 @@ defpkgbuild(musl, #:build, fn() { run("env", string-append("CC=", prefix, "gcc"), string-append("AR=", prefix, "ar"), string-append("RANLIB=", prefix, "ranlib"), "CFLAGS=-fstack-protector-strong -DVINIX_MALLOC_RETAIN=1", - string-append("LDFLAGS=-Wl,-soname,libc.musl-", target-arch, ".so.1"), "musl-1.2.6/configure", + string-append("LDFLAGS=-Wl,-soname,libc.musl-", target-arch, ".so.1", page-flags), "musl-1.2.6/configure", 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") @@ -63,9 +65,16 @@ defpkgbuild(musl, ; 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") + native-specs := string-append(pkgdir(), "/usr/lib/musl-gcc.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", "-e", "s|^-dynamic-linker |--sysroot=%R -dynamic-linker |", specs) run("grep", "-F", "--", string-append("--sysroot=%R -dynamic-linker /lib/", loader-name), specs) + ; The arm64 kernel maps executable segments on 16 KiB pages. + if string=?(target-arch, "aarch64") { + ; Bootlin places a 4 KiB -Wl option after *link. A self spec appends the + ; target link option after those driver arguments, including -nostdlib. + run("sed", "-i", "1i%rename self_spec vinix_original_self_spec\\n\\n*self_spec:\\n%(vinix_original_self_spec) %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:-Wl,-z,max-page-size=16384}}}}}}\\n", native-specs, 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 index 337b191..ff62cd7 100644 --- a/tests/toolchain/musl-sysroot.v +++ b/tests/toolchain/musl-sysroot.v @@ -2,6 +2,27 @@ module main import os +import strconv + +fn check_load_segments(path string, page u64) ! { + result := os.exec(['readelf', '-lW', path]) + if result.exit_code != 0 { return error(result.output) } + mut count := 0 + for line in result.output.split_into_lines() { + fields := line.fields() + if fields.len == 0 || fields[0] != 'LOAD' { continue } + offset := strconv.parse_uint(fields[1], 0, 64)! + address := strconv.parse_uint(fields[2], 0, 64)! + alignment := strconv.parse_uint(fields.last(), 0, 64)! + if alignment < page || address % page != offset % page { + return error('LOAD segment cannot use ${page}-byte Vinix pages in ${path}: ${line}') + } + count++ + } + if count == 0 { return error('no loadable ELF segments in ${path}') } + println('${os.base(path)} has ${count} LOAD segments compatible with ${page}-byte Vinix pages.') +} + fn checked(argv []string) ! { result := os.exec(argv) @@ -34,6 +55,10 @@ fn check(arch string, sdk string, work string) ! { 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)! + page := if arch == 'aarch64' { u64(16384) } else { u64(4096) } + for file in [target_dso, app, os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')] { + check_load_segments(file, page)! + } 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) } -- 2.56.1