Skip to content

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

C

tools/dev/kcxref.py: BL callers of an address in a KC

Heyang Zhoue6d75354ba75
diff --git a/tools/dev/kcxref.py b/tools/dev/kcxref.py
new file mode 100644
index 0000000..43bc9df
--- /dev/null
+++ b/tools/dev/kcxref.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+# Find BL callers of a target in [lo, hi) of a fileset KC and print the instruction words after each (debugging aid).
+import struct, sys
+path, lo, hi, target = sys.argv[1], int(sys.argv[2], 16), int(sys.argv[3], 16), int(sys.argv[4], 16)
+d = open(path, 'rb').read()
+ncmds = struct.unpack_from('<I', d, 16)[0]; off = 32
+for _ in range(ncmds):
+ cmd, sz = struct.unpack_from('<II', d, off)
+ if cmd == 0x19:
+ vmaddr, vmsize, fileoff, filesize = struct.unpack_from('<QQQQ', d, off + 24)
+ a = max(lo, vmaddr); b = min(hi, vmaddr + filesize)
+ for va in range(a & ~3, b, 4):
+ w = struct.unpack_from('<I', d, fileoff + va - vmaddr)[0]
+ if (w & 0xfc000000) == 0x94000000:
+ imm = w & 0x3ffffff
+ if imm & 0x2000000: imm -= 1 << 26
+ if va + imm * 4 == target:
+ print('%x' % va)
+ off += sz