musl: align AArch64 ELF segments for the Vinix kernel
@@ -4,11 +4,13 @@ native-arm := memq(string->symbol("aarch64"), features())
44target-arch := if string?(selected-arch) { selected-arch } else if native-arm { "aarch64" } else { "x86_64" }
55cross-prefix := get-environment-variable("CROSS_COMPILE")
66prefix := if string?(cross-prefix) { cross-prefix } else { "" }
7+page-flags := if string=?(target-arch, "aarch64") { " -Wl,-z,max-page-size=16384" } else { "" }
8+release := if string=?(target-arch, "aarch64") { 4 } else { 3 }
79
810defpkgbuild(musl,
911 #:pkgname, "musl",
1012 #:pkgver, "1.2.6",
11- #:pkgrel, 2,
13+ #:pkgrel, release,
1214 #:pkgdesc, "Vinix C runtime, development headers and GCC specs",
1315 #:arch, [#:x86-64, #:aarch64],
1416 #:url, "https://musl.libc.org/",
@@ -42,7 +44,7 @@ defpkgbuild(musl,
4244 #:build, fn() {
4345 run("env", string-append("CC=", prefix, "gcc"), string-append("AR=", prefix, "ar"),
4446 string-append("RANLIB=", prefix, "ranlib"), "CFLAGS=-fstack-protector-strong -DVINIX_MALLOC_RETAIN=1",
45- string-append("LDFLAGS=-Wl,-soname,libc.musl-", target-arch, ".so.1"), "musl-1.2.6/configure",
47+ string-append("LDFLAGS=-Wl,-soname,libc.musl-", target-arch, ".so.1", page-flags), "musl-1.2.6/configure",
4648 string-append("--target=", target-arch, "-linux-musl"), "--prefix=/usr", "--syslibdir=/lib", "--enable-wrapper=gcc",
4749 "--enable-optimize=internal,malloc,malloc/mallocng/*.c,string")
4850 run("make", "WRAPCC_GCC=gcc")
@@ -63,9 +65,16 @@ defpkgbuild(musl,
6365 ; Pass the sysroot to GNU ld as well, so absolute paths in target linker
6466 ; scripts resolve inside the SDK. Keep the interpreter path guest-absolute.
6567 specs := string-append(pkgdir(), "/usr/lib/musl-gcc-sysroot.specs")
68+ native-specs := string-append(pkgdir(), "/usr/lib/musl-gcc.specs")
6669 run("cp", string-append(pkgdir(), "/usr/lib/musl-gcc.specs"), specs)
6770 run("sed", "-i", "-e", "s|/usr/include|%R/usr/include|g", "-e", "s|/usr/lib|%R/usr/lib|g",
6871 "-e", "s|^-dynamic-linker |--sysroot=%R -dynamic-linker |", specs)
6972 run("grep", "-F", "--", string-append("--sysroot=%R -dynamic-linker /lib/", loader-name), specs)
73+ ; The arm64 kernel maps executable segments on 16 KiB pages.
74+ if string=?(target-arch, "aarch64") {
75+ ; Bootlin places a 4 KiB -Wl option after *link. A self spec appends the
76+ ; target link option after those driver arguments, including -nostdlib.
77+ 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)
78+ }
7079 run("install", "-Dm644", "musl-1.2.6/COPYRIGHT", string-append(pkgdir(), "/usr/share/licenses/musl/COPYRIGHT"))
7180 })
@@ -2,6 +2,27 @@
22module main
33
44import os
5+import strconv
6+
7+fn check_load_segments(path string, page u64) ! {
8+ result := os.exec(['readelf', '-lW', path])
9+ if result.exit_code != 0 { return error(result.output) }
10+ mut count := 0
11+ for line in result.output.split_into_lines() {
12+ fields := line.fields()
13+ if fields.len == 0 || fields[0] != 'LOAD' { continue }
14+ offset := strconv.parse_uint(fields[1], 0, 64)!
15+ address := strconv.parse_uint(fields[2], 0, 64)!
16+ alignment := strconv.parse_uint(fields.last(), 0, 64)!
17+ if alignment < page || address % page != offset % page {
18+ return error('LOAD segment cannot use ${page}-byte Vinix pages in ${path}: ${line}')
19+ }
20+ count++
21+ }
22+ if count == 0 { return error('no loadable ELF segments in ${path}') }
23+ println('${os.base(path)} has ${count} LOAD segments compatible with ${page}-byte Vinix pages.')
24+}
25+
526
627fn checked(argv []string) ! {
728 result := os.exec(argv)
@@ -34,6 +55,10 @@ fn check(arch string, sdk string, work string) ! {
3455 app := os.join_path(work, 'consumer')
3556 link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app]
3657 checked(link)!
58+ page := if arch == 'aarch64' { u64(16384) } else { u64(4096) }
59+ for file in [target_dso, app, os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')] {
60+ check_load_segments(file, page)!
61+ }
3762 mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path',
3863 lib + ':' + os.join_path(root, 'lib'), app]
3964 if arch != os.uname().machine { run.prepend('qemu-' + arch) }