Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

vm-xnu-rebuild.sh: keep the kernel's KC slots in step with its size (kc27-layout.py), rebuild the KC

Heyang Zhou9415dc7ee835
diff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.sh
index 677e6ac..eeba7b5 100755
--- a/scripts/vm-xnu-rebuild.sh
+++ b/scripts/vm-xnu-rebuild.sh
@@ -1,12 +1,25 @@
#!/bin/bash
-# Inside the VM: apply xnulinux's XNU patches to the k27 tree and rebuild the kernel incrementally.
+# Inside the VM: apply xnulinux's XNU patches to the k27 tree, rebuild the kernel incrementally, and (when the macOS 27.2
+# kernelcache is there) relink the kernel collection with Apple's kexts. The kernel's segment slots inside the collection
+# (k27/segaddrs.sh) follow the kernel's size: when kc27-layout.py moves them, the kernel is linked again.
set -e
cd ~/w/dvm
+export PATH=$PATH:/usr/lib/llvm-21/bin
T=out/k27/xnu
for p in ~/w/poc/xnu-patches/*.patch; do
- for f in $(grep '^+++ b/' $p | sed 's|^+++ b/||'); do git -C $T checkout -q -- $f; done
+ for f in $(grep '^+++ b/' $p | sed 's|^+++ b/||'); do git -C $T checkout -q -- $f 2>/dev/null || rm -f $T/$f; done
done
for p in ~/w/poc/xnu-patches/*.patch; do git -C $T apply $p; echo "applied $(basename $p)"; done
JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -15
+if [ -e out/kc/kc27.bin ]; then
+ cp k27/segaddrs.sh /tmp/segaddrs.before
+ python3 k27/kc27-layout.py out/kc/kc27.bin out/k27/kernel-k27.macho --write k27/segaddrs.sh | tail -2
+ if ! cmp -s /tmp/segaddrs.before k27/segaddrs.sh; then
+ echo "kernel slots moved: linking the kernel again"
+ JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -3
+ fi
+ K27_NO_BOOT=1 NATIVE= bash k27/try.sh 0 2>&1 | tail -3
+ sudo cp out/k27/kc-k27.bin /lib/firmware/xnu/kc.bin
+fi
ls -la out/k27/kernel-k27.macho
sudo cp out/k27/kernel-k27.macho /lib/firmware/xnu/kernel.macho