Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals

- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-14 14:55:05 +09:00
co-authored by Claude Opus 5.5
parent 5bc5a01087
commit 4d91f80bae
106 changed files with 7009 additions and 181 deletions
+156 -19
View File
@@ -7,6 +7,7 @@
# svc #n、mrs cntpct_el0 → 宿主门(门地址取自线程槽 KGATE,进 C 后按返回点查 gates.tsv)
# 已登记剪口(CUTS,按 NSO 构建号限定):改写前逐条断言原字节,不符即中止;结果列在 <名>.cuts.tsv
# 已登记观测门(OBS,按构建号限定):入口一条指令换成「进宿主观测回调 → 执行原指令 → 跳回」,诊断零运行时改代码;列在 <名>.obs.tsv
# DM_ABI=android:槽块基址改经 Bionic 线程指针取(mrs tpidr_el0 + ldr [tp,#DM_ANDROID_TP_OFF],Linux 把用户态 tpidrro_el0 清零),fixups.tsv 表头多 ABI / TP_SLOT_OFF 两行。
# 键页只在数据段末尾保存“实际键号 × 8”,补丁代码通过键页间接取线程槽:装载器可使用任意键号。
# 输出:<名>.patched.bin(键页与补丁区接在映像后 PATCH_OFF)、<名>.fixups.tsv(布局与键页下标表头)、<名>.gates.tsv、<名>.sites.tsv、<名>.cuts.tsv、<名>.obs.tsv
# Generic NSO rewriter: Switch aarch64 module → an image that can execute natively on Apple systems.
@@ -18,6 +19,7 @@
# svc #n, mrs cntpct_el0 → host gate (gate address taken from thread slot KGATE; once in C, gates.tsv is looked up by return point)
# Registered cuts (CUTS, restricted by NSO build ID): before rewriting, the original bytes are asserted one by one and any mismatch aborts; results are listed in <name>.cuts.tsv
# Registered observation gates (OBS, restricted by build ID): one instruction at the entry is replaced with "enter the host observation callback → execute the original instruction → jump back", so diagnostics modify zero code at runtime; listed in <name>.obs.tsv
# DM_ABI=android: the slot block base is fetched via the Bionic thread pointer instead (mrs tpidr_el0 + ldr [tp,#DM_ANDROID_TP_OFF]; Linux zeroes user tpidrro_el0), and the fixups.tsv header gains two lines, ABI / TP_SLOT_OFF.
# The key page only stores "actual key number × 8" at the end of the data segment, and patch code fetches thread slots indirectly through the key page: the loader may use any key number.
# Output: <name>.patched.bin (key page and patch area appended after the image at PATCH_OFF), <name>.fixups.tsv (layout and key page index header), <name>.gates.tsv, <name>.sites.tsv, <name>.cuts.tsv, <name>.obs.tsv
import struct, re, sys, os
@@ -178,6 +180,12 @@ cuts = list(CUTS.get(BUILD_ID, []))
# 触屏版规则域默认同时带宿主私有门;规则闸门可显式关闭,保留同一出包入口和规则域。
# The touchscreen rule domain by default also carries the host-private gates; the rule gate check can turn them off explicitly, keeping the same packaging entry point and rule domain.
PRIVATE_GATES = os.environ.get("DM_PRIVATE_GATES", "1") != "0"
# 目标 ABI:apple(iOS/macOS,默认,产物逐字节不变)/ android(Linux Bionic,槽块基址经 tpidr_el0 的 TLS 槽)。
# Target ABI: apple (iOS/macOS, default, output byte-identical) / android (Linux Bionic, slot block base via a TLS slot of tpidr_el0).
ABI = os.environ.get('DM_ABI', 'apple')
assert ABI in ('apple', 'android'), f'未知 ABI {ABI}'
ANDROID_TP_OFF = int(os.environ.get('DM_ANDROID_TP_OFF', '0x10'), 0) # Bionic TLS_SLOT_APP(tp[2])
assert ANDROID_TP_OFF % 8 == 0 and 0 <= ANDROID_TP_OFF < 0x8000, f'DM_ANDROID_TP_OFF {ANDROID_TP_OFF:#x}'
PRIVATE_AIM = RULE_RUN and PRIVATE_GATES
if PRIVATE_AIM:
cuts.append((0x2faf04, 'fde7fe97', 'c11000d4',
@@ -256,6 +264,16 @@ def emit(w, kind=None):
def emit_key_addr(reg, kind):
fix.append((len(patch), kind)); emit(ADRP(reg, here(), KEY_PAGE_OFF)); emit(LDRX(reg, reg, KEY_INDEX[kind]))
def emit_slot_base(reg):
"""reg = 本线程槽块基址。apple:tpidrro_el0 即基址(宿主每线程写入);android:Linux 把用户态 tpidrro_el0 清零,
改从 Bionic 线程指针取:reg = [tpidr_el0 + ANDROID_TP_OFF](宿主在该 TLS 槽放槽块指针)。客体自己的 tpidr_el0 读写已改写成槽 KTLS,
真 tpidr_el0 始终是 Bionic 的。
reg = base of this thread's slot block. apple: tpidrro_el0 is the base (written per thread by the host); android: Linux zeroes user tpidrro_el0,
so take it from the Bionic thread pointer instead: reg = [tpidr_el0 + ANDROID_TP_OFF] (the host stores the slot block pointer in that TLS slot). The guest's own tpidr_el0
reads/writes are already rewritten to slot KTLS, so the real tpidr_el0 is always Bionic's."""
if ABI == 'apple': emit(MRS_TPIDRRO(reg))
else: emit(MRS_TPIDR(reg)); emit(LDRX(reg, reg, ANDROID_TP_OFF // 8))
def emit_slot_addr(base, key_reg, kind):
emit_key_addr(key_reg, kind); emit(ADDR(base, base, key_reg))
@@ -273,6 +291,7 @@ def LDUR(t, n, off): return 0xF8400000 | ((off & 0x1FF) << 12) | (n << 5) | t
def LDRX(t, n, imm12=0): return 0xF9400000 | (imm12 << 10) | (n << 5) | t
def STRX(t, n, imm12=0): return 0xF9000000 | (imm12 << 10) | (n << 5) | t
def MRS_TPIDRRO(t): return 0xD53BD060 | t
def MRS_TPIDR(t): return 0xD53BD040 | t
def ADRP(dd, pc, target):
imm = ((target & ~0xFFF) - (pc & ~0xFFF)) >> 12; assert -(1 << 20) <= imm < (1 << 20)
return 0x90000000 | ((imm & 3) << 29) | (((imm >> 2) & 0x7FFFF) << 5) | dd
@@ -326,13 +345,87 @@ def sp_delta(ins):
assert not re.match(r'(sp|wsp),', ins.op_str), f'writes sp: {ins.address:x} {ins.op_str}'
return 0
# ---- android:没有红区 ----
# Linux / Bionic 不保留 sp 下方:信号(含 ART / 系统发来的、不走 sigaltstack 的)随时可能落在当前栈上把 sp 下方写坏。
# 所以 android 的跳板一律先减 sp 再存借用的寄存器,用完加回;被搬进跳板、又引用 sp 的原指令改用一个存着原 sp 的寄存器 U。
# apple 走原来的红区写法,产物逐字节不变。思路来自 dodola/d2r-mobile 的 Android 移植(MIT)。
# ---- android: no red zone ----
# Linux / Bionic does not reserve the area below sp: a signal (including ones from ART / the system that do not go through sigaltstack) may land on
# the current stack at any time and overwrite what is below sp. So android trampolines always lower sp first, store the borrowed registers, and
# add it back afterwards; an original instruction moved into a trampoline that references sp uses a register U holding the original sp instead.
# apple keeps the original red-zone form, output byte-identical. The approach comes from the Android port in dodola/d2r-mobile (MIT).
def save_pair(S, T):
emit(STP_PRE(S, T, 31, -16) if ABI == 'android' else STP(S, T, 31, -16))
def restore_pair(S, T):
emit(LDP_POST(S, T, 31, 16) if ABI == 'android' else LDP(S, T, 31, -16))
def uses_sp(ins): return re.search(r'\bw?sp\b', ins.op_str) is not None
def subst_sp(ins, w, reg):
"""把原指令里的 sp / wsp 操作数换成寄存器 reg。寄存器号 31 也编码 xzr / wzr,所以逐个组合反汇编核对,只换真正是 sp 的字段。
Replace the sp / wsp operand of the original instruction with register reg. Register number 31 also encodes xzr / wzr, so disassemble every
combination and check it, replacing only the fields that really are sp."""
orig = dis(w, ins.address)
if orig.mnemonic == 'mov' and orig.op_str.endswith(', sp'): # mov xd, sp = add xd, sp, #0;换基址后 capstone 不再写成 mov,改用寄存器 mov | mov xd, sp = add xd, sp, #0; with the base replaced capstone no longer prints mov, use the register mov
assert w & 0xFFC003E0 == 0x910003E0 and (w >> 10) & 0xFFF == 0, hex(ins.address)
return 0xAA0003E0 | (reg << 16) | (w & 31)
want = re.sub(r'\b(w?sp)\b', lambda m: ('w' if m.group(1) == 'wsp' else 'x') + str(reg), orig.op_str)
pos = [p for p in (0, 5, 10, 16) if (w >> p) & 31 == 31]
for k in range(1, len(pos) + 1):
for c in combinations(pos, k):
nw = w
for p in c: nw = (nw & ~(31 << p)) | (reg << p)
try: dd = dis(nw, ins.address)
except AssertionError: continue
if dd.mnemonic == orig.mnemonic and dd.op_str == want: return nw
raise RuntimeError(f'android sp 替换失败 {ins.address:x} {orig.mnemonic} {orig.op_str}')
def android_frame(ins, scratch=32):
"""借用寄存器的暂存区放在原指令所有静态 sp 访问(含负偏移、前变址写回)之下。
Place the scratch area for borrowed registers below every static sp access of the original instruction (including negative offsets and pre-index writeback)."""
from capstone.arm64 import ARM64_OP_MEM, ARM64_REG_SP, ARM64_REG_INVALID
floor = min(0, sp_delta(ins))
for op in ins.operands:
if op.type == ARM64_OP_MEM and op.mem.base == ARM64_REG_SP:
assert op.mem.index == ARM64_REG_INVALID, f'android 不支持 sp 加寄存器下标 {ins.address:x} {ins.op_str}'
floor = min(floor, op.mem.disp)
frame = (scratch - floor + 15) & ~15
assert frame <= 0xFFF, f'android 暂存帧过大 {ins.address:x} {frame}'
return frame
def site_plain_android(ins):
S, T = pick(regs_in(ins))
dlt = sp_delta(ins)
assert dlt % 16 == 0, f'android sp 写回破坏 16 字节对齐 {ins.address:x} {dlt}'
start = here()
w = subst(ins, S)
if uses_sp(ins):
(U,) = pick(regs_in(ins) | {S, T}, 1)
frame = android_frame(ins)
emit(SUBI(31, 31, frame)); emit(STP(S, T, 31, 0)); emit(STRX(U, 31, 2)); emit(ADDI(U, 31, frame))
emit_slot_base(T); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
emit(subst_sp(ins, w, U))
emit(STRX(S, T))
emit(LDRX(U, 31, 2)); emit(LDP(S, T, 31, 0))
assert 0 <= frame + dlt <= 0xFFF, f'android sp 收尾超范围 {ins.address:x} {frame + dlt}'
if frame + dlt: emit(ADDI(31, 31, frame + dlt))
else:
assert dlt == 0
save_pair(S, T); emit_slot_base(T); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
emit(w); emit(STRX(S, T))
restore_pair(S, T)
emit(B(here(), ins.address + 4))
return start
def site_plain(ins):
if ABI == 'android': return site_plain_android(ins)
S, T = pick(regs_in(ins))
dlt = sp_delta(ins)
save = -16 if dlt == 0 else -112 # 改 sp 的指令会写到 sp 下方,暂存放到保留区深处 | Instructions that modify sp write below sp, so the scratch save is placed deep in the reserved area
assert dlt == 0 or dlt >= -96, f'sp delta {dlt} at {ins.address:x}'
start = here()
emit(STP(S, T, 31, save)); emit(MRS_TPIDRRO(T)); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
emit(STP(S, T, 31, save)); emit_slot_base(T); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
emit(subst(ins, S))
emit(STRX(S, T)) # 一律写回:capstone 会漏报写入(adds w18 实例) | Always write back: capstone can miss reporting a write (the adds w18 instance)
emit(LDP(S, T, 31, save - dlt)); emit(B(here(), ins.address + 4))
@@ -343,34 +436,35 @@ def site_cond(ins):
S, T = pick(regs_in(ins))
tgt = ins.operands[-1].imm
start = here()
emit(STP(S, T, 31, -16)); emit(MRS_TPIDRRO(T)); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
save_pair(S, T); emit_slot_base(T); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
m = ins.mnemonic
taken = here() + 4 * 3
off = (taken - here()) >> 2
nw = ((w & 0xFF000000) | ((off & 0x7FFFF) << 5) | S) if m in ('cbz', 'cbnz') else ((w & 0xFFF80000) | ((off & 0x3FFF) << 5) | S)
pc = here(); emit(nw)
dd = dis(nw, pc); assert dd.mnemonic == m and dd.operands[-1].imm == taken
emit(LDP(S, T, 31, -16)); emit(B(here(), ins.address + 4))
restore_pair(S, T); emit(B(here(), ins.address + 4))
assert here() == taken
emit(LDP(S, T, 31, -16)); emit(B(here(), tgt))
restore_pair(S, T); emit(B(here(), tgt))
return start
def site_pcrel(ins):
tgt = ins.operands[1].imm
S, T = pick(set())
start = here()
emit(STP(S, T, 31, -16)); emit(MRS_TPIDRRO(T)); emit_slot_addr(T, S, 'K18') # S 先作键页偏移暂存,下一条再装目标值 | S first serves as a temporary for the key page offset; the next instruction then loads the target value
save_pair(S, T); emit_slot_base(T); emit_slot_addr(T, S, 'K18') # S 先作键页偏移暂存,下一条再装目标值 | S first serves as a temporary for the key page offset; the next instruction then loads the target value
pc = here(); emit(ADRP(S, pc, tgt))
if ins.mnemonic == 'adr': emit(ADDI(S, S, tgt & 0xFFF))
emit(STRX(S, T))
emit(LDP(S, T, 31, -16)); emit(B(here(), ins.address + 4))
restore_pair(S, T); emit(B(here(), ins.address + 4))
return start
def site_br(ins, base, count):
"""跳转表 br x18:目标 = base + 4i。x16 暂存红区 → 等距落地垫 → 恢复 x16 → 原目标。 | Jump table br x18: target = base + 4i. Stash x16 in the red zone → equally spaced landing pads → restore x16 → original target."""
start = here()
emit(STUR(16, 31, -16)); emit(STUR(17, 31, -24)) # x16 / x17 是函数内可存活的临时寄存器,借用前存红区 | x16 / x17 are temporary registers that can stay live within a function; save them to the red zone before borrowing
emit(MRS_TPIDRRO(16)); emit_slot_addr(16, 17, 'K18'); emit(LDRX(16, 16)); emit(LDUR(17, 31, -24))
if ABI == 'android': emit(STP_PRE(16, 17, 31, -16)) # 没有红区:先减 sp 再存 | No red zone: lower sp first, then store
else: emit(STUR(16, 31, -16)); emit(STUR(17, 31, -24)) # x16 / x17 是函数内可存活的临时寄存器,借用前存红区 | x16 / x17 are temporary registers that can stay live within a function; save them to the red zone before borrowing
emit_slot_base(16); emit_slot_addr(16, 17, 'K18'); emit(LDRX(16, 16)); emit(LDRX(17, 31, 1) if ABI == 'android' else LDUR(17, 31, -24))
padbase = start + 4 * 24
delta = padbase - base
op = ADDI if delta >= 0 else SUBI; dl = abs(delta)
@@ -380,20 +474,37 @@ def site_br(ins, base, count):
assert len(ops) <= 12
for o in ops: emit(o)
emit(BR(16))
assert here() <= padbase, f'跳转表块超出落地垫基址 {base:#x}'
while here() < padbase: emit(NOP)
tails = padbase + 4 * count
for i in range(count): emit(B(here(), tails + 8 * i))
for i in range(count):
emit(0xF85F0000 | (31 << 5) | 16) # ldur x16,[sp,#-16]
emit(LDP_POST(16, 17, 31, 16) if ABI == 'android' else 0xF85F0000 | (31 << 5) | 16) # android ldp x16,x17,[sp],#16 / apple ldur x16,[sp,#-16]
emit(B(here(), base + 4 * i))
return start
def emit_slot_read(N, tmp, kind):
"""xN = 线程槽 kind 的值,借 tmp。apple:tmp 存红区;android:先减 sp 存 tmp(与 xzr 成对保持 16 字节)。
xN = value of thread slot kind, borrowing tmp. apple: tmp is saved in the red zone; android: lower sp first and store tmp (paired with xzr to keep 16 bytes)."""
if ABI == 'android':
assert N not in (18, 31) and tmp != N
emit(STP_PRE(tmp, 31, 31, -16)); emit_slot_base(N); emit_slot_addr(N, tmp, kind); emit(LDRX(N, N)); emit(LDP_POST(tmp, 31, 31, 16))
else:
emit(STUR(tmp, 31, -16)); emit_slot_base(N); emit_slot_addr(N, tmp, kind); emit(LDRX(N, N)); emit(LDUR(tmp, 31, -16)) # tmp 存红区借用 | tmp is saved by borrowing the red zone
def site_sysreg_read(ins, kind):
"""mrs xN, tpidr_el0 / tpidrro_el0 → xN = 线程槽值。 | mrs xN, tpidr_el0 / tpidrro_el0 → xN = thread slot value."""
N = ins.bytes[0] & 31
tmp = 16 if N != 16 else 17
start = here()
emit(STUR(tmp, 31, -16)); emit(MRS_TPIDRRO(N)); emit_slot_addr(N, tmp, kind); emit(LDRX(N, N)); emit(LDUR(tmp, 31, -16)) # tmp 存红区借用 | tmp is saved by borrowing the red zone
if ABI == 'android' and N == 31: pass # mrs xzr:无副作用 | mrs xzr: no effect
elif ABI == 'android' and N == 18:
# x18 是虚拟寄存器(槽 K18),不碰硬件 x18:槽 kind → 槽 K18 | x18 is virtual (slot K18), never touch hardware x18: slot kind -> slot K18
emit(SUBI(31, 31, 32)); emit(STP(16, 17, 31, 0)); emit(STRX(15, 31, 2))
emit_slot_base(17); emit_slot_addr(17, 16, kind); emit(LDRX(15, 17))
emit_slot_base(17); emit_slot_addr(17, 16, 'K18'); emit(STRX(15, 17))
emit(LDRX(15, 31, 2)); emit(LDP(16, 17, 31, 0)); emit(ADDI(31, 31, 32))
else: emit_slot_read(N, tmp, kind)
emit(B(here(), ins.address + 4))
return start
@@ -401,10 +512,19 @@ def site_tpidr_write(ins):
N = struct.unpack('<I', ins.bytes)[0] & 31
(T,) = pick({N}, 1)
start = here()
emit(0xF81F0000 | (31 << 5) | T) # stur xT,[sp,#-16]
tmp = next(x for x in (16, 17) if x not in (N, T))
emit(STUR(tmp, 31, -24)); emit(MRS_TPIDRRO(T)); emit_slot_addr(T, tmp, 'KTLS'); emit(STRX(N, T)); emit(LDUR(tmp, 31, -24))
emit(0xF85F0000 | (31 << 5) | T) # ldur xT,[sp,#-16]
if ABI == 'android' and N == 18:
# msr tpidr_el0, x18:x18 的值在槽 K18 | msr tpidr_el0, x18: the value of x18 lives in slot K18
emit(SUBI(31, 31, 32)); emit(STP(16, 17, 31, 0)); emit(STRX(15, 31, 2))
emit_slot_base(17); emit_slot_addr(17, 16, 'K18'); emit(LDRX(15, 17))
emit_slot_base(17); emit_slot_addr(17, 16, 'KTLS'); emit(STRX(15, 17))
emit(LDRX(15, 31, 2)); emit(LDP(16, 17, 31, 0)); emit(ADDI(31, 31, 32))
elif ABI == 'android':
emit(STP_PRE(T, tmp, 31, -16)); emit_slot_base(T); emit_slot_addr(T, tmp, 'KTLS'); emit(STRX(N, T)); emit(LDP_POST(T, tmp, 31, 16))
else:
emit(0xF81F0000 | (31 << 5) | T) # stur xT,[sp,#-16]
emit(STUR(tmp, 31, -24)); emit_slot_base(T); emit_slot_addr(T, tmp, 'KTLS'); emit(STRX(N, T)); emit(LDUR(tmp, 31, -24))
emit(0xF85F0000 | (31 << 5) | T) # ldur xT,[sp,#-16]
emit(B(here(), ins.address + 4))
return start
@@ -417,7 +537,7 @@ def emit_gate_call():
Fetching the slot through the key page needs a second register to be borrowed; x29 is borrowed and then immediately restored from the stack: the x29 saved by _dm_gate must be the guest's original value --
the host identifies the caller by [x29+8] (skill binding observation gate 6, private aiming gate svc86), and the crash backtrace also walks the x29 chain
(since r24 plan C it was borrowed and not returned, the x29 the host got was the slot offset from the key page, and skill icons fell back to placeholder text)."""
emit(MRS_TPIDRRO(30)); emit_slot_addr(30, 29, 'KGATE'); emit(LDRX(30, 30)); emit(LDRX(29, 31))
emit_slot_base(30); emit_slot_addr(30, 29, 'KGATE'); emit(LDRX(30, 30)); emit(LDRX(29, 31))
def site_gate(ins, kind, arg):
"""svc / mrs cntpct_el0 → 宿主门。门地址取自线程槽 KGATE;门按返回点(本块 ldp 的地址)查 gates.tsv。 | svc / mrs cntpct_el0 → host gate. The gate address is taken from thread slot KGATE; the gate looks up gates.tsv by return point (the address of this block's ldp)."""
@@ -472,18 +592,30 @@ def site_window(seg):
S, T = pick(used)
lo, hi = seg[0].address, seg[-1].address + 4
start = here()
emit(STP(S, T, 31, -16)); emit(MRS_TPIDRRO(T)); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
# android 没有红区:先减 sp 再存借用寄存器;窗口里有引用 sp 的指令时,sp 换成 U(= 原 sp),暂存帧放在它们的访问范围之下
# android has no red zone: lower sp first, then save the borrowed registers; when the window references sp, sp is replaced by U (= original sp) and the scratch frame goes below their accesses
U = frame = None
if ABI == 'android' and any(uses_sp(x) for x in seg):
assert all(sp_delta(x) == 0 for x in seg), f'android 独占窗口里改 sp {lo:#x}'
(U,) = pick(used | {S, T}, 1)
frame = max(android_frame(x) for x in seg)
emit(SUBI(31, 31, frame)); emit(STP(S, T, 31, 0)); emit(STRX(U, 31, 2)); emit(ADDI(U, 31, frame))
else:
save_pair(S, T)
emit_slot_base(T); emit_slot_addr(T, S, 'K18'); emit(LDRX(S, T))
copy0 = here(); n = len(seg)
exits = copy0 + 4 * n
stride = 32 if U is not None else 16
ex_targets = []; words = []
for k, x in enumerate(seg):
w = struct.unpack('<I', x.bytes)[0]
if has18(x): w = subst(x, S)
if U is not None and uses_sp(x): w = subst_sp(x, w, U)
m = x.mnemonic
if m in ('cbz', 'cbnz', 'tbz', 'tbnz') or m.startswith('b.'):
tgt = x.operands[-1].imm
if lo <= tgt < hi: newt = copy0 + (tgt - lo)
else: ex_targets.append(tgt); newt = exits + 16 * len(ex_targets) # 出口桩第 0 个留给顺序落出(见下),窗口外的目标从第 1 个起 | Exit stub 0 is reserved for the sequential fall-out (see below); targets outside the window start from stub 1
else: ex_targets.append(tgt); newt = exits + stride * len(ex_targets) # 出口桩第 0 个留给顺序落出(见下),窗口外的目标从第 1 个起 | Exit stub 0 is reserved for the sequential fall-out (see below); targets outside the window start from stub 1
pc = copy0 + 4 * k; off = (newt - pc) >> 2
if m in ('tbz', 'tbnz'): w = (w & 0xFFF8001F) | ((off & 0x3FFF) << 5)
else: w = (w & 0xFF00001F) | ((off & 0x7FFFF) << 5)
@@ -501,7 +633,12 @@ def site_window(seg):
# (one operation done twice / a lock-free list popped more than once). The game has 6 such windows (main 0x1848eb8 / 0x1848f28 / 0x1848f84, sdk 0x2ea0fc / 0x2ea4cc / 0x2ea644);
# windows whose only branch is a cbnz back into the window are not affected. The equivalence check used to never draw the "compare equal" direction at random, so it stayed green (equiv.py now forces both directions of window branches).
for t in [hi] + ex_targets:
emit(STRX(S, T)); emit(LDP(S, T, 31, -16)); emit(B(here(), t)); emit(NOP)
e0 = here()
emit(STRX(S, T))
if U is not None: emit(LDRX(U, 31, 2)); emit(LDP(S, T, 31, 0)); emit(ADDI(31, 31, frame))
else: restore_pair(S, T)
emit(B(here(), t))
while here() < e0 + stride: emit(NOP)
return start, lo, hi
# ---- 函数边界(eh_frame_hdr)----
@@ -788,8 +925,7 @@ for oa, wa, ob_, wb, r, key, why in params:
ins = dis(struct.unpack_from('<I', IMG, o)[0], o)
assert r not in regs_in(ins), f'参数位中间用到 x{r}:{o:#x} {ins.mnemonic} {ins.op_str}'
start = here()
tmp = 16 if r != 16 else 17
emit(STUR(tmp, 31, -16)); emit(MRS_TPIDRRO(r)); emit_slot_addr(r, tmp, key); emit(LDRX(r, r)); emit(LDUR(tmp, 31, -16)); emit(B(here(), oa + 4))
emit_slot_read(r, 16 if r != 16 else 17, key); emit(B(here(), oa + 4))
struct.pack_into('<I', IMG, oa, B(oa, start)); struct.pack_into('<I', IMG, ob_, NOP); stat('param')
# 既有三块原生规则跳板均只使用后继覆盖的死临时w8,不改w0/stat返回、sp或NZCV。
@@ -852,6 +988,7 @@ elif os.path.exists(P('rules.tsv')):
with open(P('fixups.tsv'), 'w') as f:
f.write(f'# PATCH_OFF\t{PATCH_OFF:#x}\n# KEY_PAGE_OFF\t{KEY_PAGE_OFF:#x}\n# KEY_PAGE_SIZE\t0x4000\n# TEXT_END\t{TEXT_END:#x}\n# RO_OFF\t{RO_OFF:#x}\n# DATA_OFF\t{DATA_OFF:#x}\n')
for k in KEY_INDEX: f.write(f'# KEY_{k}_IDX\t{KEY_INDEX[k]}\n')
if ABI == 'android': f.write(f'# ABI\tlinux-bionic\n# TP_SLOT_OFF\t{ANDROID_TP_OFF:#x}\n') # 苹果宿主读到非数值行即拒载 | the Apple host refuses to load on a non-numeric line
with open(P('gates.tsv'), 'w') as f:
for ret, k, arg, orig in gates: f.write(f'{ret:#x}\t{k}\t{arg}\t{orig:#x}\n')
with open(P('sites.tsv'), 'w') as f: