fix(relinker): tolerate unwind ranges that split an instruction #1326

Merged
gjonn merged 1 commit from upstream-pr/1326 into main 2026-10-08 11:40:05 +00:00
gjonn commented 2026-10-07 23:37:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-07T23:37:52Z.


What

Some unwind ranges in a title (version 01.710) start or end inside an instruction: of 156,935 FDEs, 3 begin and 10 end mid-instruction relative to a linear decode. Relinking with --windows --to-intel failed twice because of this.

  1. Code analysis: branch into skipped range tail (offset 0xba1812). The range [0xba1790, 0xba1813) ends one byte into 4c 0f a3 eb (bt rbx, r13). The per-range sweep cannot decode it and records the byte as a skipped tail, then jb 0xba1812 from the next range is rejected. A static branch target inside a skipped tail is now accepted when an instruction decodes there within its executable segment; the CFG pass then collects it with the segment as its bound. Targets that do not decode are still rejected.
  2. Unsupported Windows guest TLS instruction (bytes: 65 2e 62 69 6e 00 00 48 89) (offset 0xb10000). An FDE begins at 0xb0c000, two bytes into movabs rax, "ve.bin\0\0" at 0xb0bffd. Decoding from the FDE start gives a GS-prefixed instruction that overlaps the real one. The collector already tolerates overlaps that involve no FS prefix; the TLS builder now skips a non-FS segment-prefixed instruction that starts inside an earlier collected instruction instead of treating it as a TLS access. FS accesses and GS instructions that do not overlap anything behave as before.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0: all 30 relinker tests registered on Windows pass.
  • New tls_function_coverage cases: branch-into-cut-tail-unwind, branch-into-cut-tail-symbol and function-begins-inside-instruction fail on main with the two errors above and pass with this change. branch-into-undecodable-tail (a branch one byte into an extrq that the range end cuts, where 0f 78 without its prefix does not decode) is still rejected.
  • A title with such ranges relinks with --windows --to-intel, starts, and reaches its first file reads.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4779784775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1326). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-07T23:37:52Z. --- ### What Some unwind ranges in a title (version 01.710) start or end inside an instruction: of 156,935 FDEs, 3 begin and 10 end mid-instruction relative to a linear decode. Relinking with `--windows --to-intel` failed twice because of this. 1. `Code analysis: branch into skipped range tail (offset 0xba1812)`. The range `[0xba1790, 0xba1813)` ends one byte into `4c 0f a3 eb` (`bt rbx, r13`). The per-range sweep cannot decode it and records the byte as a skipped tail, then `jb 0xba1812` from the next range is rejected. A static branch target inside a skipped tail is now accepted when an instruction decodes there within its executable segment; the CFG pass then collects it with the segment as its bound. Targets that do not decode are still rejected. 2. `Unsupported Windows guest TLS instruction (bytes: 65 2e 62 69 6e 00 00 48 89) (offset 0xb10000)`. An FDE begins at `0xb0c000`, two bytes into `movabs rax, "ve.bin\0\0"` at `0xb0bffd`. Decoding from the FDE start gives a GS-prefixed instruction that overlaps the real one. The collector already tolerates overlaps that involve no FS prefix; the TLS builder now skips a non-FS segment-prefixed instruction that starts inside an earlier collected instruction instead of treating it as a TLS access. FS accesses and GS instructions that do not overlap anything behave as before. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0: all 30 relinker tests registered on Windows pass. - New `tls_function_coverage` cases: `branch-into-cut-tail-unwind`, `branch-into-cut-tail-symbol` and `function-begins-inside-instruction` fail on `main` with the two errors above and pass with this change. `branch-into-undecodable-tail` (a branch one byte into an `extrq` that the range end cuts, where `0f 78` without its prefix does not decode) is still rejected. - A title with such ranges relinks with `--windows --to-intel`, starts, and reaches its first file reads. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] AI-assisted: yes
hashaam1217 commented 2026-10-08 02:12:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-08T02:12:45Z.


Checked on Linux: Ubuntu 24.04.5, GCC 13.3.0, Release build, Python 3.12.3.

  • At 471afdf0, tls_function_coverage, tls_diagnostics, empty_tls, guest_tls, elf_offsets and elf_header pass.
  • With WindowsTlsBuilder.cpp and CodeInstructionCollector.cpp reverted to main but the new tests kept, tls_function_coverage fails at branch-into-cut-tail-unwind, as described.
  • This PR also touches the same WindowsTlsBuilder.cpp loop and test file as my #1233 and #1234. All three merge into main without conflicts in any order. With all three stacked, the six tests above pass.

#1234 runs every converted image on x86-64 Linux through windows_image_runner.py, and each must exit with 42. So in the stacked run, the three new cases that convert successfully (branch-into-cut-tail-unwind, branch-into-cut-tail-symbol, function-begins-inside-instruction) were also executed on the host, and they return 42.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6050764833 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1326#issuecomment-6050764833). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-08T02:12:45Z. --- Checked on Linux: Ubuntu 24.04.5, GCC 13.3.0, Release build, Python 3.12.3. - At 471afdf0, `tls_function_coverage`, `tls_diagnostics`, `empty_tls`, `guest_tls`, `elf_offsets` and `elf_header` pass. - With `WindowsTlsBuilder.cpp` and `CodeInstructionCollector.cpp` reverted to main but the new tests kept, `tls_function_coverage` fails at `branch-into-cut-tail-unwind`, as described. - This PR also touches the same `WindowsTlsBuilder.cpp` loop and test file as my #1233 and #1234. All three merge into main without conflicts in any order. With all three stacked, the six tests above pass. #1234 runs every converted image on x86-64 Linux through `windows_image_runner.py`, and each must exit with 42. So in the stacked run, the three new cases that convert successfully (`branch-into-cut-tail-unwind`, `branch-into-cut-tail-symbol`, `function-begins-inside-instruction`) were also executed on the host, and they return 42.
adriadam10 commented 2026-10-08 11:26:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T11:26:32Z.


Checked: build + ctest green on the PR head; the new tls_function_coverage cases fail with the two source changes reverted and pass with them; branch-into-undecodable-tail is still rejected. Conflicts with main were adjacency only in CodeInstructionCollector.cpp (both blocks kept), rebased onto main with build + ctest green (442/442); merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058826861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1326#issuecomment-6058826861). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T11:26:32Z. --- Checked: build + ctest green on the PR head; the new `tls_function_coverage` cases fail with the two source changes reverted and pass with them; `branch-into-undecodable-tail` is still rejected. Conflicts with main were adjacency only in `CodeInstructionCollector.cpp` (both blocks kept), rebased onto main with build + ctest green (442/442); merging.
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!1326
No description provided.