Skip to content

fix(relinker): tolerate trailing data in TLS scan - #299

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
Pouare514:fix/relinker-tls-tail
Oct 4, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
Pouare514:fix/relinker-tls-tail

Conversation

@Pouare514

@Pouare514 Pouare514 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What

The TLS code scan over unwind function ranges failed the whole relink when a range ended with undecodable bytes. TLOU Part I has an FDE range ending with trailing jump-table data, so the scan now tolerates a short tail (at most 15 bytes, the longest x64 instruction) without an FS prefix instead of aborting. A branch target that lands inside a skipped tail is still rejected, and an overlapping instruction that starts with, or lands inside, an FS-prefixed instruction is rejected as overlapping boundaries (the 0x64 scan now starts at the overlapping address instead of after the previous instruction).

Tested

  • Windows, test_tls_function_coverage.py against the rebuilt relinker: all pass, including the restored overlapping-entry expectation (Code analysis: overlapping instruction boundaries) and the new overlapping-fs-prefix case (FS-prefixed instruction starting inside the previous one is rejected).
  • ctest empty_tls, tls_function_coverage, guest_tls: 3/3 pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, the tail rule reads as sound to me: an FS instruction starting in the skipped bytes would have its 0x64 byte there, so the no-0x64 check plus the branch check covers it. The overlap part has a hole though: the 0x64 search starts at previousEnd, so an FS-prefixed instruction that starts inside the previous one (0x64 at address, before previousEnd) is accepted, stays in the instruction set, and its TLS patch would overwrite bytes of the earlier instruction. Searching from address to spanEnd would close it. Could you also restore the PR description (it stops after the first sentence) with the Tested section and the checklist, and add a new test case rather than changing what overlapping-entry expects?

@Pouare514
Pouare514 force-pushed the fix/relinker-tls-tail branch from 66fd86c to 49feb79 Compare October 4, 2026 13:42
@Pouare514

Copy link
Copy Markdown
Contributor Author

Done: the 0x64 scan now starts at the overlapping address, plus a prefix scan of the overlapped previous instruction so a branch landing inside an FS-prefixed instruction (like overlapping-entry) is still rejected as overlapping boundaries. overlapping-entry expectation restored, new overlapping-fs-prefix case added. ctest empty_tls/tls_function_coverage/guest_tls 3/3 green.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, starting the scan at address closes the hole, and the new case covers it. One more thing about the check on the previous instruction: it looks for 0x64 only in its first 4 bytes, but WindowsTlsBuilder patches any instruction whose decoded SegmentPrefix is 0x64 and accepts any number of 0x66 prefixes before it. So a previous instruction like 66 66 66 66 64 48 8b 04 25 ... passes this check and still gets patched. Decoding the previous instruction and checking its SegmentPrefix matches exactly what gets patched. With that it looks ready to merge from my side. The red Windows job is the guest_audio_in_no_device flake that #532 fixed.

@Pouare514

Copy link
Copy Markdown
Contributor Author

Done: the previous-instruction check now decodes it and compares SegmentPrefix to 0x64, matching exactly what WindowsTlsBuilder patches (any number of 0x66 prefixes included). Added an overlapping-fs-prefix-66 case with four 0x66 prefixes; verified it fails without the fix and passes with it. Also dropped the stray libSceAudioIn hunk from this branch.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, decoding the previous instruction and comparing its SegmentPrefix with 0x64 is exactly the set WindowsTlsBuilder patches, and overlapping-fs-prefix-66 covers the four 0x66 case. tls_function_coverage and empty_tls pass here on this head merged with current main, and CI is green. Ready to merge from my side.

A small note, no action needed: the libSceAudioIn line in this branch is now the same as main's #532, so it merges to nothing. It only shows up in the diff because the branch is based before #532.

@oneandonlydean
oneandonlydean merged commit d220149 into boykopovar:main Oct 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants