Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

fix: recognize ADR references in the seputil gigalocker patch

黑曜猫bc960bc606cc
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/Sep/CustomFirmwareSeputil.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/Sep/CustomFirmwareSeputil.swift
index 4556bc93..9d0a8daf 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/Sep/CustomFirmwareSeputil.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CustomFirmware/ExecutablePatches/Sep/CustomFirmwareSeputil.swift
@@ -17,7 +17,7 @@
// inside `__TEXT,__cstring`, so no instruction is assembled here and the ARM64
// encoder has nothing to contribute. Capstone is used for the *anchor*: the
// literal has to be the one seputil actually formats with, which is proven by
-// finding the `adrp`/`add` pair in `__TEXT,__text` that materialises its
+// finding the `adr` or `adrp`/`add` reference in `__TEXT,__text` that materialises its
// address.
//
// Anchoring, in order, none of it positional:
@@ -28,7 +28,7 @@
// `<dir>/<2-byte field>.gl`, where the field reads `%s` (pristine) or `AA`
// (already patched). Whole-literal equality, so the tail of a longer
// string cannot match.
-// 3. An `adrp`+`add` in `__TEXT,__text` computing that literal's VA, matched
+// 3. An `adr` or `adrp`+`add` in `__TEXT,__text` computing that literal's VA, matched
// on decoded operands.
// 4. The field to rewrite is the one after the literal's own last `/`.
//
@@ -217,7 +217,7 @@ public enum CustomFirmwareSeputil {
let references = references(to: site.literalVMA, in: data, text: text)
guard !references.isEmpty else {
throw PatcherError.patchSiteNotFound(
- "seputil: \"\(site.literal)\" @ 0x\(hex(site.literalVMA)) has no adrp+add "
+ "seputil: \"\(site.literal)\" @ 0x\(hex(site.literalVMA)) has no adr or adrp+add "
+ "reference in \(textSection.segment),\(textSection.section); "
+ "it is not the literal the gigalocker path is built from",
)
@@ -411,6 +411,13 @@ public enum CustomFirmwareSeputil {
var sites: [UInt64] = []
for (index, insn) in instructions.enumerated() {
+ // iOS 17 uses ADR for the same whole format literal.
+ if insn.mnemonic == "adr", let operands = insn.detail?.operands,
+ operands.count == 2, operands[0].type == .register,
+ operands[1].type == .immediate, UInt64(bitPattern: operands[1].imm) == vma {
+ sites.append(insn.address)
+ continue
+ }
guard insn.mnemonic == "adrp",
let operands = insn.detail?.operands,
operands.count == 2,
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareSeputilTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareSeputilTests.swift
index 75e23262..75f62b98 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareSeputilTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmware/CustomFirmwareSeputilTests.swift
@@ -558,6 +558,16 @@ struct CustomFirmwareSeputilShapeTests {
)
}
+ @Test func `accepts the iOS 17 ADR format literal reference`() {
+ // ADR x2 in the pristine iPhone15,4 21H16 seputil. This is an input
+ // specimen, not an instruction emitted by the patcher.
+ let pc: UInt64 = 0x1_0000_B85C
+ let target: UInt64 = 0x1_0001_D3B3
+ let code = Data([0xA2, 0xDA, 0x08, 0x70])
+ #expect(CustomFirmwareSeputil.references(to: target, in: code, text: text(size: 4, address: pc)) == [pc])
+ #expect(CustomFirmwareSeputil.references(to: target + 1, in: code, text: text(size: 4, address: pc)).isEmpty)
+ }
+
@Test func `pairs an adrp with its add`() throws {
let pc: UInt64 = 0x1_0000_0000
let target: UInt64 = 0x1_0001_BDCF