fix(relinker): take LEA addresses in PS5 images as code only when code uses them as code pointers #14107

Merged
Khlopyev merged 6 commits from upstream-pr/14107 into main 2026-11-12 19:22:56 +00:00
Khlopyev commented 2026-10-31 19:12:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Khlopyev on 2026-10-31T19:12:51Z.


What

In images without PT_SCE_DYNLIBDATA, the Windows and macOS code analysis follows every address a REX.W LEA computes as code. Tables that assembly keeps in .text are referenced exactly that way, so they were decoded as instructions:

  • a GS access look-alike inside such a table is rewritten to UD2 (silent data corruption; the conversion succeeds);
  • undecodable or FS look-alike bytes fail the conversion (Unsupported Windows guest TLS instruction (bytes: 64 6f), Instruction truncated ...).

OpenSSL's x86-64 assembly keeps its constants in .text up to 3.3, which titles and middleware bundle (#7547 is ubiservices.prx). With current main, 7 of 12 OpenSSL x86-64 modules built that way fail the Windows and macOS code analysis.

#8166 added a rule for PS4 images. This PR covers the other images that have unwind or symbol functions (under --function-symbol-roots, which ignores symbol sizes, only unwind functions count): a LEA target outside them is followed as code only when the computed register is used as a code pointer on some path of at most 64 instructions through direct jumps, both edges of conditional branches and the return of calls: the target of an indirect call or jump, stored or pushed, kept in an argument register at a call, or returned in rax. Register moves, test and cmp keep the register; after a call only rbx, rbp and r12 to r15 keep it. Every other target joins the existing pointer targets taken as data, so it is reported in the same message and still protects its entry from TLS patches. Targets inside functions, relocation targets, PS4 images and images without unwind or symbol functions keep their current behaviour. The gap left by the rule is recorded in docs/dev/debt/relinker.md.

tools/pr_overlap.py reports overlapping lines with #12154, #12282 and #13825 (the BuildControlFlowGraph call in CodeInstructionCollector.cpp) and with #11525 (docs/dev/debt/relinker.md). It could not compare #5021, #7531, #9793, #10792 and #10861 because they conflict with main. Of all of these, only #10861 changes addAddressTakenRoot: it takes the targets of LEAs without REX.W or with a 32-bit address size as data and keeps the REX.W targets that this PR narrows as pointer roots, so the two PRs change the same lines.

Tested

  • Ubuntu 24.04, GCC 13.3, relinker-only Release: ctest 315/315.
  • Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 (UCRT, r7, as in CI), relinker-only Release: ctest 813/815 at the current head, merged with main 2f4d22d9e5: select_tests_MissingCommandTests and vulkan_validation_gate_commands fail because this Windows account may not create symbolic links (WinError 1314). On the first commit annotate_module exceeded its 20 s timeout in two runs on this machine with GNU objdump on PATH and passed without the timeout (53 tests, 1 skipped, 98 s); in the last run it passed in 11 s.
  • New code_analysis_address_data, for --windows and --macos: a table read through the LEA register keeps its GS look-alike bytes, a callback passed in rdi to a call is still analysed (its GS access becomes UD2), and a table that starts with 0F 78 converts. On main the first case rewrites the table to 0f 0b 90 90. The test also covers a callback in rbx that is checked with test rbx, rbx before call rbx, one called only on the taken edge of a conditional branch, and one kept in rbx across another call: the GS access in each becomes UD2, while the first commit left all three unchanged. A table checked with test before it is read keeps its bytes.
  • OpenSSL x86-64 perlasm (sha256, sha512, sha1, aes, vpaes, aesni, bsaes, chacha, poly1305, ghash, cmll, ecp_nistz256) generated from OpenSSL master with the .section .rodata directives removed (the 3.3-and-older layout), each linked into a guest module with .eh_frame, converted with --windows and --macos under --skip-syscall-check: main fails 14 of 24 conversions; with this PR all 24 convert and the code segment of every output is byte-identical to the input. Measured on the first commit and not repeated after the second.
  • Existing windows_tls_protected_entries and tls_function_coverage (address-taken TLS callback) pass unchanged.
  • Not tested: a title, or running a converted module on a Windows or macOS host.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5100692017 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14107). Originally posted by [Khlopyev](https://github.com/Khlopyev) on 2026-10-31T19:12:51Z. --- ### What In images without `PT_SCE_DYNLIBDATA`, the Windows and macOS code analysis follows every address a REX.W `LEA` computes as code. Tables that assembly keeps in `.text` are referenced exactly that way, so they were decoded as instructions: - a GS access look-alike inside such a table is rewritten to `UD2` (silent data corruption; the conversion succeeds); - undecodable or FS look-alike bytes fail the conversion (`Unsupported Windows guest TLS instruction (bytes: 64 6f)`, `Instruction truncated ...`). OpenSSL's x86-64 assembly keeps its constants in `.text` up to 3.3, which titles and middleware bundle (#7547 is `ubiservices.prx`). With current main, 7 of 12 OpenSSL x86-64 modules built that way fail the Windows and macOS code analysis. #8166 added a rule for PS4 images. This PR covers the other images that have unwind or symbol functions (under `--function-symbol-roots`, which ignores symbol sizes, only unwind functions count): a `LEA` target outside them is followed as code only when the computed register is used as a code pointer on some path of at most 64 instructions through direct jumps, both edges of conditional branches and the return of calls: the target of an indirect call or jump, stored or pushed, kept in an argument register at a call, or returned in `rax`. Register moves, `test` and `cmp` keep the register; after a call only `rbx`, `rbp` and `r12` to `r15` keep it. Every other target joins the existing pointer targets taken as data, so it is reported in the same message and still protects its entry from TLS patches. Targets inside functions, relocation targets, PS4 images and images without unwind or symbol functions keep their current behaviour. The gap left by the rule is recorded in `docs/dev/debt/relinker.md`. `tools/pr_overlap.py` reports overlapping lines with #12154, #12282 and #13825 (the `BuildControlFlowGraph` call in `CodeInstructionCollector.cpp`) and with #11525 (`docs/dev/debt/relinker.md`). It could not compare #5021, #7531, #9793, #10792 and #10861 because they conflict with `main`. Of all of these, only #10861 changes `addAddressTakenRoot`: it takes the targets of `LEA`s without REX.W or with a 32-bit address size as data and keeps the REX.W targets that this PR narrows as pointer roots, so the two PRs change the same lines. ### Tested - Ubuntu 24.04, GCC 13.3, relinker-only Release: `ctest` 315/315. - Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 (UCRT, r7, as in CI), relinker-only Release: `ctest` 813/815 at the current head, merged with `main` `2f4d22d9e5`: `select_tests_MissingCommandTests` and `vulkan_validation_gate_commands` fail because this Windows account may not create symbolic links (WinError 1314). On the first commit `annotate_module` exceeded its 20 s timeout in two runs on this machine with GNU objdump on `PATH` and passed without the timeout (53 tests, 1 skipped, 98 s); in the last run it passed in 11 s. - New `code_analysis_address_data`, for `--windows` and `--macos`: a table read through the `LEA` register keeps its GS look-alike bytes, a callback passed in `rdi` to a call is still analysed (its GS access becomes `UD2`), and a table that starts with `0F 78` converts. On main the first case rewrites the table to `0f 0b 90 90`. The test also covers a callback in `rbx` that is checked with `test rbx, rbx` before `call rbx`, one called only on the taken edge of a conditional branch, and one kept in `rbx` across another call: the GS access in each becomes `UD2`, while the first commit left all three unchanged. A table checked with `test` before it is read keeps its bytes. - OpenSSL x86-64 perlasm (sha256, sha512, sha1, aes, vpaes, aesni, bsaes, chacha, poly1305, ghash, cmll, ecp_nistz256) generated from OpenSSL master with the `.section .rodata` directives removed (the 3.3-and-older layout), each linked into a guest module with `.eh_frame`, converted with `--windows` and `--macos` under `--skip-syscall-check`: main fails 14 of 24 conversions; with this PR all 24 convert and the code segment of every output is byte-identical to the input. Measured on the first commit and not repeated after the second. - Existing `windows_tls_protected_entries` and `tls_function_coverage` (address-taken TLS callback) pass unchanged. - Not tested: a title, or running a converted module on a Windows or macOS host. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
slopgobblercobbler commented 2026-10-31 19:38:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-31T19:38:45Z.

Review: COMMENTED


Audited the relinker code analysis refactor for PS5 images with unwind/symbol tables:

  • Code pointer classification heuristic: UsedAsCodeAddress cleanly differentiates data table pointers from function/callback pointers computed via REX.W LEA. Inspecting up to 64 instructions along the fall-through and direct-jump path strikes an effective balance between analysis depth and bounded execution time.
  • System V AMD64 ABI and control-flow tracking:
    • Argument registers (rdi, rsi, rdx, rcx, r8, r9) are correctly masked via RegisterBit on direct calls and tail jumps (Call and UnconditionalJump).
    • Indirect transfers (0xff /2 and 0xff /4) on tracked registers and returns in rax (RegisterBit(0)) are properly handled.
    • SIB base/index decoding and memory dereference checks (memory & tracked) accurately reject pointers used as memory operands (table loads/stores), classifying them as data roots.
    • Vector prefixes (VEX 0xc4/0xc5, EVEX 0x62, and XOP 0x8f) terminate tracking immediately, preventing misclassification from SIMD data computation.
  • Scope boundary preservation:
    • PS4 binaries with PT_SCE_DYNLIBDATA (!sceFunctions.empty()), images lacking unwind/symbol functions (functions.empty()), and targets already within known function boundaries continue to follow established pointer-root paths.
    • In BuildControlFlowGraph, updating the entries check to sceFunctions.empty() && functions.empty() ensures PS5 binaries with unwind records do not treat every raw pointer as an unconditional CFG root.
  • Technical debt and conventions:
    • Technical debt in docs/dev/debt/relinker.md is well under the 1,000 character limit and clearly describes the 64-instruction search horizon and VEX/EVEX/XOP boundaries.
    • Zero inline comments rule is strictly observed in CodeInstructionCollector.cpp.
    • Verified with python tools/check_conventions.py (0 findings/errors on the changed lines).
  • Test coverage:
    • The new test_code_analysis_address_data.py test suite tests both --windows and --macos targets, verifying that GS look-alike data tables are preserved rather than rewritten to 0f 0b 90 90 (UD2), while callbacks passed in rdi remain reachable for analysis.

Excellent, thorough fix for .text constant tables in OpenSSL 3.3 and related middleware.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724601384 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14107#pullrequestreview-5724601384). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-31T19:38:45Z. **Review:** COMMENTED --- Audited the relinker code analysis refactor for PS5 images with unwind/symbol tables: - Code pointer classification heuristic: `UsedAsCodeAddress` cleanly differentiates data table pointers from function/callback pointers computed via REX.W `LEA`. Inspecting up to 64 instructions along the fall-through and direct-jump path strikes an effective balance between analysis depth and bounded execution time. - System V AMD64 ABI and control-flow tracking: - Argument registers (`rdi`, `rsi`, `rdx`, `rcx`, `r8`, `r9`) are correctly masked via `RegisterBit` on direct calls and tail jumps (`Call` and `UnconditionalJump`). - Indirect transfers (`0xff /2` and `0xff /4`) on tracked registers and returns in `rax` (`RegisterBit(0)`) are properly handled. - SIB base/index decoding and memory dereference checks (`memory & tracked`) accurately reject pointers used as memory operands (table loads/stores), classifying them as data roots. - Vector prefixes (VEX `0xc4`/`0xc5`, EVEX `0x62`, and XOP `0x8f`) terminate tracking immediately, preventing misclassification from SIMD data computation. - Scope boundary preservation: - PS4 binaries with `PT_SCE_DYNLIBDATA` (`!sceFunctions.empty()`), images lacking unwind/symbol functions (`functions.empty()`), and targets already within known function boundaries continue to follow established pointer-root paths. - In `BuildControlFlowGraph`, updating the entries check to `sceFunctions.empty() && functions.empty()` ensures PS5 binaries with unwind records do not treat every raw pointer as an unconditional CFG root. - Technical debt and conventions: - Technical debt in `docs/dev/debt/relinker.md` is well under the 1,000 character limit and clearly describes the 64-instruction search horizon and VEX/EVEX/XOP boundaries. - Zero inline comments rule is strictly observed in `CodeInstructionCollector.cpp`. - Verified with `python tools/check_conventions.py` (0 findings/errors on the changed lines). - Test coverage: - The new `test_code_analysis_address_data.py` test suite tests both `--windows` and `--macos` targets, verifying that GS look-alike data tables are preserved rather than rewritten to `0f 0b 90 90` (`UD2`), while callbacks passed in `rdi` remain reachable for analysis. Excellent, thorough fix for `.text` constant tables in OpenSSL 3.3 and related middleware.
gabichuuuuu commented 2026-11-06 23:31:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-06T23:31:57Z.

Review: COMMENTED


Tested on Windows 11 x64 with WinLibs GCC 15.2.0 15.2.0posix-14.0.0-ucrt-r7 (as in CI), its CMake 4.3.1 and Ninja 1.13.2, and Python 3.15.0. Relinker-only Release build.

  • Head 4fd7f9e3 merged into main d7c58042: ctest --output-on-failure --timeout 120 --parallel 4 passed 793/793, including code_analysis_address_data.
  • test_code_analysis_address_data.py run against the main relinker fails, because the table is rewritten to 0f 0b 90 90. The new test therefore covers the change.

Four more cases, built with the test's own module and convert helpers on --windows, each with a control. On main, every callback case gets its GS access rewritten and both table cases get the table rewritten. With this PR:

Case Code after the LEA This PR Control Control
cmov lea rax, cb; test rbx, rbx; cmove rbx, rax; call rbx GS kept (data) mov rbx, rax GS rewritten
path order lea rbx, cb; test edi, edi; jne use; 60 × nop; jmp use; use: mov esi, 1; call rbx GS kept (data) 50 × nop GS rewritten
nop lea rax, cb; 66 90; mov [rdi], rax GS kept (data) without 66 90 GS rewritten
movzx lea rdx, table; movzx edx, byte [rdi]; call f table rewritten mov edx, [rdi] table intact

In CodeInstructionCollector.cpp at the head:

  • cmov and movzx: L132 skips every two-byte opcode. cmovcc does not copy the tracked register, and movzx, movsx, setcc and imul r, rm do not end it.
  • Path order: visited (L55, L58) keys on the position and the tracked set, not on the path length. The fall-through is followed before the pending taken edge (L159–162). Here the fall-through reaches use at instruction 63 and runs out, and the taken edge, which reaches use at instruction 3, is then refused as visited. The same order decides which states the 256 cap keeps.
  • nop: L85–86 also applies RegisterBit(0) to 0x90 (nop, 66 90, pause), so the path ends whenever rax is tracked. Without REX, 0xb4–0xb7 map to rsp–rdi instead of ah–bh.

The first three cases move callbacks that main treats as code to the data side, so their GS access is no longer rewritten. The new test does not cover a store or push, a return in rax, an indirect jmp, or an argument at an indirect call.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5808443190 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14107#pullrequestreview-5808443190). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-06T23:31:57Z. **Review:** COMMENTED --- Tested on Windows 11 x64 with WinLibs GCC 15.2.0 `15.2.0posix-14.0.0-ucrt-r7` (as in CI), its CMake 4.3.1 and Ninja 1.13.2, and Python 3.15.0. Relinker-only Release build. - Head `4fd7f9e3` merged into `main` `d7c58042`: `ctest --output-on-failure --timeout 120 --parallel 4` passed 793/793, including `code_analysis_address_data`. - `test_code_analysis_address_data.py` run against the `main` relinker fails, because the table is rewritten to `0f 0b 90 90`. The new test therefore covers the change. Four more cases, built with the test's own `module` and `convert` helpers on `--windows`, each with a control. On `main`, every callback case gets its GS access rewritten and both table cases get the table rewritten. With this PR: | Case | Code after the `LEA` | This PR | Control | Control | |---|---|---|---|---| | cmov | `lea rax, cb; test rbx, rbx; cmove rbx, rax; call rbx` | GS kept (data) | `mov rbx, rax` | GS rewritten | | path order | `lea rbx, cb; test edi, edi; jne use; 60 × nop; jmp use; use: mov esi, 1; call rbx` | GS kept (data) | 50 × `nop` | GS rewritten | | nop | `lea rax, cb; 66 90; mov [rdi], rax` | GS kept (data) | without `66 90` | GS rewritten | | movzx | `lea rdx, table; movzx edx, byte [rdi]; call f` | table rewritten | `mov edx, [rdi]` | table intact | In `CodeInstructionCollector.cpp` at the head: - cmov and movzx: L132 skips every two-byte opcode. `cmovcc` does not copy the tracked register, and `movzx`, `movsx`, `setcc` and `imul r, rm` do not end it. - Path order: `visited` (L55, L58) keys on the position and the tracked set, not on the path length. The fall-through is followed before the pending taken edge (L159–162). Here the fall-through reaches `use` at instruction 63 and runs out, and the taken edge, which reaches `use` at instruction 3, is then refused as visited. The same order decides which states the 256 cap keeps. - nop: L85–86 also applies `RegisterBit(0)` to `0x90` (`nop`, `66 90`, `pause`), so the path ends whenever `rax` is tracked. Without REX, `0xb4`–`0xb7` map to `rsp`–`rdi` instead of `ah`–`bh`. The first three cases move callbacks that `main` treats as code to the data side, so their GS access is no longer rewritten. The new test does not cover a store or push, a return in `rax`, an indirect `jmp`, or an argument at an indirect call. AI-assisted: yes.
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!14107
No description provided.