Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Skip the NRO header when scanning text for rewrite sites (issue #14)
LNPANDY4b863234f758
diff --git a/dm/tools/nso_code.py b/dm/tools/nso_code.pyindex b40bdacd..0413251f 100644--- a/dm/tools/nso_code.py+++ b/dm/tools/nso_code.py@@ -85,9 +85,12 @@ def fde_ranges(img):return sorted(out)-def data_blocks(img, text_end, md):- """Sorted [(start, end)] of the data blocks in text [0, text_end) (see the module docstring). md: a capstone AArch64 disassembler"""- gaps, last = [], 0+def data_blocks(img, text_end, md, lo=0):+ """Sorted [(start, end)] of the data blocks in text [lo, text_end) (see the module docstring).+ md: a capstone AArch64 disassembler. lo is the text segment's offset inside img: 0 for an NSO,+ 0x80 for an NRO, whose file starts with a header (GitHub issue #14 — without it the header's+ build ID was read as instructions)."""+ gaps, last = [], lofor s, e in fde_ranges(img):if s >= text_end: breakif s > last: gaps.append((last, s))diff --git a/dm/tools/patch_nso.py b/dm/tools/patch_nso.pyindex 7b2f2eba..5235e05a 100644--- a/dm/tools/patch_nso.py+++ b/dm/tools/patch_nso.py@@ -469,8 +469,15 @@ starts = sorted(x for x in starts if x < TEXT_END)# Data blocks in the text (nso_code.py: Absolum's main starts with 1.7 MB of them) are not code: words there that read as rewrite sites are left alonesys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))import nso_code-DATA_BLOCKS = nso_code.data_blocks(IMG, TEXT_END, md)-CODE_RANGES = list(zip([0] + [e for _, e in DATA_BLOCKS], [s for s, _ in DATA_BLOCKS] + [TEXT_END]))+DATA_BLOCKS = nso_code.data_blocks(IMG, TEXT_LO + TEXT_END, md, lo=TEXT_LO)+# The text segment starts at its memory offset: 0 for an NSO (the image is rebuilt from 0) and+# 0x80 for an NRO (the file is the image preceded by its header, so text sits at 0x80). Scanning+# an NRO from 0 read the header's build ID as instructions and reported+# "44 ldr x18, #0xfffffffffff40370" on Final Fantasy X/X-2 HD Remaster (GitHub issue #14).+# TEXT_LO is where the code really begins in IMG; data_blocks() gets the same lower bound so its+# returned offsets are already IMG-absolute and CODE_RANGES needs no further shift.+TEXT_LO = segs[0][1]+CODE_RANGES = list(zip([TEXT_LO] + [e for _, e in DATA_BLOCKS], [s for s, _ in DATA_BLOCKS] + [TEXT_LO + TEXT_END]))md_lite = Cs(CS_ARCH_ARM64, CS_MODE_ARM) # no details: the x18 scan reads only the operand textR18 = re.compile(r'\b[xw]18\b')def x18_words(lo, hi):diff --git a/dm/tools/tests/test_nro_header.py b/dm/tools/tests/test_nro_header.pynew file mode 100644index 00000000..c5f07b46--- /dev/null+++ b/dm/tools/tests/test_nro_header.py@@ -0,0 +1,67 @@+"""The text scan must start at the text segment's offset, not at 0 (GitHub issue #14).++An NRO file is the image itself preceded by a header: its text segment starts at 0x80, while an+NSO's text starts at 0 after decompression. Scanning an NRO from 0 read the header's build ID as+instructions and reported `44 ldr x18, #0xfffffffffff40370` on Final Fantasy X/X-2 HD Remaster.++Testing the real code path end to end needs a structurally complete NRO (MOD0 block, FDE tables),+which is a lot of fixture for the question "does the lower bound hold". These tests check the two+things that were wrong instead:++1. `nso_code.data_blocks(..., lo=N)` never returns a block that starts below N.+2. `patch_nso.py` passes the text segment's own offset as that bound, which is 0x80 for an NRO and+ 0 for an NSO — asserted on the source, since the value comes from the file being rewritten.+"""+import sys+import unittest+from pathlib import Path++HERE = Path(__file__).resolve()+sys.path.insert(0, str(HERE.parents[1]))++import nso_code # noqa: E402++TOOLS = HERE.parents[1]+++class DataBlockLowerBound(unittest.TestCase):+ def test_gaps_start_at_lo(self):+ """The first gap must begin at `lo`, not at 0.++ data_blocks() derives its gaps from the FDE table, which needs a structurally complete+ NRO to exercise. The bound itself is one line — `gaps, last = [], lo` — so it is checked+ by patching fde_ranges to report one covered range and reading the gaps back.+ """+ real = nso_code.fde_ranges+ try:+ nso_code.fde_ranges = lambda img: [(0x100, 0x200)]+ from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM+ md = Cs(CS_ARCH_ARM64, CS_MODE_ARM)+ img = bytes(0x400)+ # text_end 0x300, lo 0x80: the gaps are [0x80,0x100) and [0x200,0x300), never below 0x80+ blocks = nso_code.data_blocks(img, 0x300, md, lo=0x80)+ self.assertTrue(all(s >= 0x80 for s, _ in blocks),+ f'a block starts below the text segment: {blocks}')+ blocks0 = nso_code.data_blocks(img, 0x300, md)+ self.assertTrue(all(s >= 0 for s, _ in blocks0), 'default lo must not break NSOs')+ finally:+ nso_code.fde_ranges = real++ def test_lo_defaults_to_zero(self):+ """An NSO keeps the old behaviour: no header, so the scan starts at 0."""+ self.assertEqual(nso_code.data_blocks.__defaults__, (0,),+ 'data_blocks must default lo to 0 so the NSO path is unchanged')+++class PatchNsoPassesTheOffset(unittest.TestCase):+ def test_text_lo_comes_from_the_segment(self):+ src = (TOOLS / 'patch_nso.py').read_text(encoding='utf-8')+ # TEXT_LO must be the text segment's memory offset, and both the scan and CODE_RANGES must+ # use it. Hardcoding 0x80 would break NSOs; dropping it would bring the NRO bug back.+ self.assertIn('TEXT_LO = segs[0][1]', src)+ self.assertIn('data_blocks(IMG, TEXT_LO + TEXT_END, md, lo=TEXT_LO)', src)+ self.assertRegex(src, r'CODE_RANGES = list\(zip\(\[TEXT_LO\]')+++if __name__ == '__main__':+ unittest.main(verbosity=2)