fix(relinker): resume scans after undecodable bytes between PS4 unwind functions #7660

Merged
savvaniss merged 4 commits from upstream-pr/7660 into main 2026-10-20 11:17:15 +00:00
savvaniss commented 2026-10-18 20:46:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-18T20:46:10Z.


What

In PS4 (PT_SCE_DYNLIBDATA) executables and modules, the bytes between unwind functions are 16-byte slots of function tails and NOP padding, and a tail can begin inside an instruction. Hasbro Family Fun Pack (CUSA03312) has a slot that is a lone 48 followed by a 15-byte NOP, which decodes as one 16-byte instruction, so the syscall scan failed the relink with Cannot decode instruction: Instruction exceeds 15 bytes. This is stop 2 in my #3093 comment; @MarcZX10 suggested a focused PR with synthetic fixtures that stays strict inside FDE ranges.

  • For SCE images, the syscall scan (executable and sce_module paths) and the call-site scan now get the segment's unwind function ranges (SceUnwindRanges, from the same ReadExceptionFunctions list that FindSceCodeEnd uses).
  • Outside every range, an undecodable byte is skipped and the scan resumes at the next byte; a decoding outside the ranges that would cross into one ends at the range start, so gap bytes cannot change how a function is decoded.
  • Inside a range a decode failure still fails the relink, and images without ranges (SysV, or no FDE in the segment) are scanned exactly as before.
  • The CFG analysis is unchanged and stays strict for every reached instruction.
  • Stop 9 from the same comment (Unsupported Windows guest TLS instruction (bytes: 65 f0 5b)) needs nothing here: since #6102 the Windows TLS builder traps GS-prefixed instructions.
  • The rule is recorded in docs/dev/debt/relinker.md.

Tested

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON) on main f3059421f. The branch is rebased onto 6e27fc8f0; nothing under core/relinker changed in between.

  • Relinker CTest: 197 of 197 pass, including the new sce_code_gaps and amd64_only_converter with its new scanner case.
  • test_sce_code_gaps.py against a relinker built from unchanged main f3059421f, case by case:
Case main This PR
SCE executable, slot in a gap (Linux, Windows, macOS) Cannot decode instruction ... (offset 0x210) converts, PLT call site resolved
SCE module, slot before its function (Linux, Windows, macOS) Cannot decode instruction ... (offset 0x1000) converts
Slot followed by syscall in the gap Cannot decode instruction Forbidden syscall instruction at code offset 0x220
mov eax, imm32 opcode in the gap byte before a function that starts with syscall converts: the mov swallows the syscall Forbidden syscall instruction at code offset 0x200
Slot inside the unwind function Cannot decode instruction same
Slot in a gap of a SysV executable Cannot decode instruction same
SCE module, slot inside its function Cannot decode instruction same
  • CUSA03312, base 01.00 and patch 01.02 from my own dump: this branch merged locally with #6619, #5138, #6357, #6684, #6381, #6354, #6618 and 3b158c1e4 from #6977, without the earlier local experiment. The Linux and --windows conversions of the eboot and its 9 bundled modules complete (External prx references: 1345). Without this change the eboot stops at stop 2 as described in #3093.
  • After merging main with #6354 (c3d1363df): relinker CTest 199 of 199.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • Risk and Monopoly Plus (CUSA03312), 20 Oct: risk.self and monopoly.self convert with this change. In the stack of #7198 (on an RX 9070 XT), Risk then plays a local match against the AI and Monopoly Plus reaches its loading screen. boggle.self and tp.self still stop at a syscall pattern inside a 15 MB data gap.

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 under Silent stubs in their library's technical debt file
  • 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. Overlaps textually with #6595 and #6618, which change the same block of RelinkerPipeline.cpp; whichever lands second needs a short rebase. #6354 has merged and is included since c3d1363df.
  • Fixes: none
  • Unblocks: CUSA03312 #7198
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4912667569 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7660). Originally posted by [savvaniss](https://github.com/savvaniss) on 2026-10-18T20:46:10Z. --- ### What In PS4 (`PT_SCE_DYNLIBDATA`) executables and modules, the bytes between unwind functions are 16-byte slots of function tails and NOP padding, and a tail can begin inside an instruction. Hasbro Family Fun Pack (CUSA03312) has a slot that is a lone `48` followed by a 15-byte NOP, which decodes as one 16-byte instruction, so the syscall scan failed the relink with `Cannot decode instruction: Instruction exceeds 15 bytes`. This is stop 2 in [my #3093 comment](https://github.com/boykopovar/AnyPS5/issues/3093#issuecomment-6235820957); @MarcZX10 suggested a focused PR with synthetic fixtures that stays strict inside FDE ranges. - For SCE images, the syscall scan (executable and `sce_module` paths) and the call-site scan now get the segment's unwind function ranges (`SceUnwindRanges`, from the same `ReadExceptionFunctions` list that `FindSceCodeEnd` uses). - Outside every range, an undecodable byte is skipped and the scan resumes at the next byte; a decoding outside the ranges that would cross into one ends at the range start, so gap bytes cannot change how a function is decoded. - Inside a range a decode failure still fails the relink, and images without ranges (SysV, or no FDE in the segment) are scanned exactly as before. - The CFG analysis is unchanged and stays strict for every reached instruction. - Stop 9 from the same comment (`Unsupported Windows guest TLS instruction (bytes: 65 f0 5b)`) needs nothing here: since #6102 the Windows TLS builder traps GS-prefixed instructions. - The rule is recorded in `docs/dev/debt/relinker.md`. ### Tested Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`) on `main` `f3059421f`. The branch is rebased onto `6e27fc8f0`; nothing under `core/relinker` changed in between. - Relinker CTest: 197 of 197 pass, including the new `sce_code_gaps` and `amd64_only_converter` with its new scanner case. - `test_sce_code_gaps.py` against a relinker built from unchanged `main` `f3059421f`, case by case: | Case | `main` | This PR | |---|---|---| | SCE executable, slot in a gap (Linux, Windows, macOS) | `Cannot decode instruction ... (offset 0x210)` | converts, PLT call site resolved | | SCE module, slot before its function (Linux, Windows, macOS) | `Cannot decode instruction ... (offset 0x1000)` | converts | | Slot followed by `syscall` in the gap | `Cannot decode instruction` | `Forbidden syscall instruction at code offset 0x220` | | `mov eax, imm32` opcode in the gap byte before a function that starts with `syscall` | converts: the `mov` swallows the syscall | `Forbidden syscall instruction at code offset 0x200` | | Slot inside the unwind function | `Cannot decode instruction` | same | | Slot in a gap of a SysV executable | `Cannot decode instruction` | same | | SCE module, slot inside its function | `Cannot decode instruction` | same | - CUSA03312, base 01.00 and patch 01.02 from my own dump: this branch merged locally with #6619, #5138, #6357, #6684, #6381, #6354, #6618 and 3b158c1e4 from #6977, without the earlier local experiment. The Linux and `--windows` conversions of the eboot and its 9 bundled modules complete (`External prx references: 1345`). Without this change the eboot stops at stop 2 as described in #3093. - After merging `main` with #6354 (`c3d1363df`): relinker CTest 199 of 199. - `python3 tools/check_conventions.py --base origin/main`: passed. - **Risk and Monopoly Plus (CUSA03312), 20 Oct:** `risk.self` and `monopoly.self` convert with this change. In the stack of #7198 (on an RX 9070 XT), Risk then plays a local match against the AI and Monopoly Plus reaches its loading screen. `boggle.self` and `tp.self` still stop at a syscall pattern inside a 15 MB data gap. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none. Overlaps textually with #6595 and #6618, which change the same block of `RelinkerPipeline.cpp`; whichever lands second needs a short rebase. #6354 has merged and is included since `c3d1363df`. - [x] Fixes: none - [x] Unblocks: CUSA03312 #7198 - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-18 20:57:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T20:57:46Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8042 core/relinker/codegen/include/codegen/IInstructionScanner.hpp L20
core/relinker/codegen/src/InstructionScanner.cpp L13, L18, L22, L28-29, L41, L59, L64, L67-68
core/relinker/relinker/src/analysis/SyscallScanner.cpp L190
PR 8076 core/relinker/relinker/src/guest/GuestModuleBuilder.cpp L359
core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp L383-384, L388-391
docs/dev/debt/relinker.md L21

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 6044 core/relinker/CMakeLists.txt
PR 6618 core/relinker/CMakeLists.txt
core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp
PR 6879 core/relinker/CMakeLists.txt
PR 6977 core/relinker/CMakeLists.txt
core/relinker/relinker/src/analysis/SceCodeExtent.cpp
core/relinker/relinker/src/guest/GuestModuleBuilder.cpp
core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp
docs/dev/debt/relinker.md
PR 7332 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255060453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7660#issuecomment-6255060453). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T20:57:46Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 8042](https://www.github.com/boykopovar/AnyPS5/pull/8042) | `core/relinker/codegen/include/codegen/IInstructionScanner.hpp` [L20](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/include/codegen/IInstructionScanner.hpp#L20)<br>`core/relinker/codegen/src/InstructionScanner.cpp` [L13](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L13), [L18](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L18), [L22](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L22), [L28-29](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L28-L29), [L41](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L41), [L59](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L59), [L64](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L64), [L67-68](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/codegen/src/InstructionScanner.cpp#L67-L68)<br>`core/relinker/relinker/src/analysis/SyscallScanner.cpp` [L190](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/relinker/src/analysis/SyscallScanner.cpp#L190) | | | | [PR 8076](https://www.github.com/boykopovar/AnyPS5/pull/8076) | `core/relinker/relinker/src/guest/GuestModuleBuilder.cpp` [L359](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp#L359)<br>`core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp` [L383-384](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp#L383-L384), [L388-391](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp#L388-L391)<br>`docs/dev/debt/relinker.md` [L21](https://github.com/boykopovar/AnyPS5/blob/77c18cefaedd47d497e3e6b1c3bcbbc25c828855/docs/dev/debt/relinker.md#L21) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/relinker/CMakeLists.txt` | | [PR 6618](https://www.github.com/boykopovar/AnyPS5/pull/6618) | `core/relinker/CMakeLists.txt`<br>`core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt`<br>`core/relinker/relinker/src/analysis/SceCodeExtent.cpp`<br>`core/relinker/relinker/src/guest/GuestModuleBuilder.cpp`<br>`core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp`<br>`docs/dev/debt/relinker.md` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` |
wachi35676 commented 2026-10-19 00:13:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:13:05Z.


Tested at 45a2e3dd4 on Windows 11 / GCC 15.2 and WSL Ubuntu 24.04 / gcc 13.3, main 8a456334f. sce_code_gaps and amd64_only_converter pass on both, and the other relinker tests are unchanged. On Windows that is 136/137: the one failure, windows_import_modules, fails the same way on main here.

Running each case of test_sce_code_gaps.py against a relinker built from main reproduces your table exactly:

  • the six gap and module cases plus syscall-after-slot fail with Instruction exceeds 15 bytes (offset 0x210/0x1000);
  • instruction-into-function converts;
  • the three strict cases fail on both.

Overlap: this conflicts textually in RelinkerPipeline.cpp with #6595 and #6618. Both are open, and both rewrite the same syscall-scan loop from the same ReadExceptionFunctions list, so whichever lands second needs a rebase. Since you tested with #6618 merged, maybe list it under Depends on or Overlaps.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256917730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7660#issuecomment-6256917730). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T00:13:05Z. --- Tested at `45a2e3dd4` on Windows 11 / GCC 15.2 and WSL Ubuntu 24.04 / gcc 13.3, `main` `8a456334f`. `sce_code_gaps` and `amd64_only_converter` pass on both, and the other relinker tests are unchanged. On Windows that is 136/137: the one failure, `windows_import_modules`, fails the same way on `main` here. Running each case of `test_sce_code_gaps.py` against a relinker built from `main` reproduces your table exactly: - the six gap and module cases plus `syscall-after-slot` fail with `Instruction exceeds 15 bytes (offset 0x210/0x1000)`; - `instruction-into-function` converts; - the three strict cases fail on both. Overlap: this conflicts textually in `RelinkerPipeline.cpp` with #6595 and #6618. Both are open, and both rewrite the same syscall-scan loop from the same `ReadExceptionFunctions` list, so whichever lands second needs a rebase. Since you tested with #6618 merged, maybe list it under Depends on or Overlaps. AI-assisted: yes (Claude Code); results above were run on this machine.
savvaniss commented 2026-10-19 06:42:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-19T06:42:28Z.


@wachi35676 thanks for reproducing the table on both toolchains.

On #6618: this change doesn't need it. The 11 fixture cases and the relinker suite pass on main without it, as your run shows. Hasbro's Windows conversion needed #6618 for a separate reason: a jump table at the end of an unwind function in libSceFios2.prx, which lies inside an FDE range and so isn't a gap (details on #6618). I've kept #6595 and #6618 as textual overlaps in the description and updated it now that #6354 has merged.

Since then I merged main (with #6354) into the branch as c3d1363df. Only docs/dev/debt/relinker.md conflicted, and all 199 relinker tests pass at that head on Windows 11 / WinLibs GCC 15.2.0 r7.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6261340398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7660#issuecomment-6261340398). Originally posted by [savvaniss](https://github.com/savvaniss) on 2026-10-19T06:42:28Z. --- @wachi35676 thanks for reproducing the table on both toolchains. On #6618: this change doesn't need it. The 11 fixture cases and the relinker suite pass on `main` without it, as your run shows. Hasbro's Windows conversion needed #6618 for a separate reason: a jump table at the end of an unwind function in `libSceFios2.prx`, which lies inside an FDE range and so isn't a gap (details on #6618). I've kept #6595 and #6618 as textual overlaps in the description and updated it now that #6354 has merged. Since then I merged `main` (with #6354) into the branch as `c3d1363df`. Only `docs/dev/debt/relinker.md` conflicted, and all 199 relinker tests pass at that head on Windows 11 / WinLibs GCC 15.2.0 r7. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-20 11:18:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T11:18:04Z.


Re-checked against current main: the merge is clean and the scanner change is unchanged. Undecodable bytes are skipped only outside every unwind range, a decoding that crosses into a range ends at its start, a syscall in the gap is still rejected, and inside a range (or without ranges, SysV) a decode failure still fails the relink. scannerUnwindRanges and test_sce_code_gaps.py cover the positive and negative cases and need the new behaviour; relinker CI is green on Linux, Windows, macOS and ASan; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6289655331 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7660#issuecomment-6289655331). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T11:18:04Z. --- Re-checked against current main: the merge is clean and the scanner change is unchanged. Undecodable bytes are skipped only outside every unwind range, a decoding that crosses into a range ends at its start, a `syscall` in the gap is still rejected, and inside a range (or without ranges, SysV) a decode failure still fails the relink. `scannerUnwindRanges` and `test_sce_code_gaps.py` cover the positive and negative cases and need the new behaviour; relinker CI is green on Linux, Windows, macOS and ASan; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7660
No description provided.