Skip to content

patch_nso.py: an NRO's header is not code - #397

Closed
v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:nro-header-skip
Closed

v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:nro-header-skip

Conversation

@v0lumehi

Copy link
Copy Markdown
Contributor

An NRO's text segment starts at offset 0, and the file's own header lies inside it: the start word, the MOD0 offset, NRO0 at +0x10, the segment table at +0x20 and the build ID at +0x40. patch_nso.py scanned the text from 0, so any header word that decodes as a rewrite site counts as one. On Final Fantasy X/X-2 HD Remaster the build ID read as ldr x18, #0xfffffffffff40370 and stopped the rewrite (LNPANDY/NX2Apple#14).

The segment table cannot give the bound. Hades' EOSSDK-Switch-Shipping.nro, for example, reads:

magic at 0x10: NRO0
text {offset, size} = (0x0, 0xa41000)
ro   {offset, size} = (0xa41000, 0x317000)
data {offset, size} = (0xd58000, 0x99000)

Change

  • code_start(d): 0x80 for an NRO, 0 for an NSO.
  • code_ranges(data_blocks, text_end, start): the gaps between the data blocks, as before, but none starting before start. The function starts below it are dropped.
  • Both scans walk these ranges: the x18 words and the rewrite sites. An NSO's ranges are unchanged.

Checked

  • New dm/tools/tests/test_nro_code_start.py takes the two functions from patch_nso.py itself, as test_jump_copies does. It checks them on a synthetic NRO laid out like Hades', with a build ID word that decodes as an x18 load: the bound is 0x80 although the segment table says 0, no code range holds that word, and NSO ranges are unchanged.
  • Hades' EOS NRO rewrites byte-identically before and after. patched.bin and every table are the same; its header has no word that reads as a site.
  • test_jump_copies, test_nso_match, test_patches, test_spin_waits, test_tail_call and test_size_prefixed_frames pass.
  • Not run: Final Fantasy X/X-2 itself, which I don't have. Final Fantasy X/X-2 HD Remaster still crashes during host_preload with MAX_MOD 128 #372 reports a later crash on that game (host_preload, MAX_MOD), which this does not touch.

Context

The NX2Apple fork had fixed this in its own copy with TEXT_LO = segs[0][1]. Because that offset is 0 for an NRO, the fix never took effect; it is discussed in LNPANDY/NX2Apple#51. Fixed here, the fork can drop its carrier for patch_nso.py and take this file with the sync.

An NRO's text segment starts at offset 0 with the file's own header in
it (the start word, the MOD0 offset, NRO0 at +0x10, the segment table at
+0x20, the build ID at +0x40): Hades' EOSSDK-Switch-Shipping.nro has text
{0x0, 0xa41000} in its segment table. The rewriter scanned the text from
0, so a header word that decodes as a site counts as one: on Final
Fantasy X/X-2 HD Remaster the build ID read as "ldr x18,
#0xfffffffffff40370" and stopped the rewrite (LNPANDY/NX2Apple#14). The
segment table cannot give the bound, since it says 0.

code_start() is 0x80 for an NRO and 0 for an NSO; code_ranges() starts
no code range before it, and the function starts below it are dropped.
Both scans (the x18 words and the rewrite sites) walk those ranges.
An NSO's ranges are as before.

test_nro_code_start.py takes the two functions from patch_nso.py, as
test_jump_copies does, and checks them on a synthetic NRO laid out as
Hades' (a build ID word that decodes as an x18 load). Hades' EOS NRO
rewrites byte-identically before and after (patched.bin and every table;
its header holds no word that reads as a site). test_jump_copies,
test_nso_match, test_patches, test_spin_waits, test_tail_call and
test_size_prefixed_frames pass.
v0lumehi added a commit to v0lumehi/NX2Apple that referenced this pull request Oct 27, 2026
…skip as seedds/nxnative#397

9f7d421 restored the NRO header skip (issue LNPANDY#14) as a carrier, and its
patcher put TEXT_LO into the site bound (starts = sorted(... TEXT_LO +
TEXT_END)) 13 lines above TEXT_LO = segs[0][1]. Every rewrite of every
game stopped there: "NameError: name 'TEXT_LO' is not defined", found
building Hades from the launcher after it pulled main.

The bound itself was also not the fix: an NRO's text segment starts at
offset 0 with the header inside it (Hades' EOSSDK-Switch-Shipping.nro:
text {0x0, 0xa41000}), so segs[0][1] is 0 and the header was still
scanned. patch_patch_nso now applies seedds/nxnative#397 instead:
code_start() is 0x80 for an NRO and 0 for an NSO, code_ranges() starts
no code range before it, and TEXT_END keeps its meaning. When LNPANDY#397 is
merged upstream, every hunk is found applied and the patcher can go.

front_mac.m, patch_nso.py and Hades II's game.tsv had patchers but were
not in PATCHED, so pr-scope refused a PR that touches them; they are in.

Checked: Hades' main and sdk rewrite (exit 0, the step that failed);
the patcher reproduces this tree from upstream's patch_nso.py and is
idempotent; a sync run with --old = --new ends SYNC OK.
test_nro_header now tests the behaviour (the header outside every code
range, an NSO unchanged, each bound assigned before the scan uses it);
test_jump_copies, test_nso_match, test_patches, test_spin_waits,
test_tail_call, test_size_prefixed_frames, test_docs and
test_env_switches pass.
LNPANDY added a commit to LNPANDY/NX2Apple that referenced this pull request Oct 27, 2026
…tests behaviour (v0lumehi)

patch_nso.py: every rewrite stops with a NameError on main (TEXT_LO); the NRO header skip as seedds/nxnative#397
seedds added a commit that referenced this pull request Oct 28, 2026
PR #397 correctly identifies instruction-like bytes in NRO metadata. Its
linear-scan cutoff still lets per-function analysis reenter the header when
code precedes the first unwind entry, failing with "jump table 44".

Keep the shared data-block exclusion already on main and extend the real
rewriter regression to cover this path. Document that both scans and function
analysis must exclude metadata. The new test fails against the original PR
and passes with the existing implementation; 108 related and documentation
tests pass. Production code is unchanged.

Refs #397

Co-authored-by: v0lumehi <[email protected]>
@seedds

seedds commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thank you for tracing the rewrite failure to NRO metadata being treated as instructions and for documenting why the segment table alone cannot exclude the header.

main already contains the NRO-header fix from 3e77816c. It excludes the metadata while preserving the entry branch and actual code. The regression coverage also exercises per-function analysis before the first unwind entry: excluding the header from the linear scans alone did not cover that path. The tests run the real rewriter and verify that the header stays byte-identical while the instruction immediately after it is rewritten.

I'm closing this PR as already addressed on main. Thank you for identifying the problem and explaining it clearly.

@seedds seedds closed this Oct 28, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
PR seedds#397 correctly identifies instruction-like bytes in NRO metadata. Its
linear-scan cutoff still lets per-function analysis reenter the header when
code precedes the first unwind entry, failing with "jump table 44".

Keep the shared data-block exclusion already on main and extend the real
rewriter regression to cover this path. Document that both scans and function
analysis must exclude metadata. The new test fails against the original PR
and passes with the existing implementation; 108 related and documentation
tests pass. Production code is unchanged.

Refs seedds#397

Co-authored-by: v0lumehi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants