fix(relinker): keep code analysis inside the code extent of PS4 executables #5195

Merged
SandstromPL merged 1 commit from upstream-pr/5195 into main 2026-10-14 02:49:02 +00:00
SandstromPL commented 2026-10-13 14:21:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-13T14:21:40Z.


What

This is the #3093 item "Keep code analysis inside the code extent and out of PT_INTERP. The CFG followed pointers into /libexec/ld-elf.so.1." It was found in Get-fix's experimental run of the OpenOrbis graphics sample (#3094).

The code analysis that the Windows TLS builder and the macOS image run (CodeInstructionCollector) treats every byte of an executable PT_LOAD as code. In the PS4 layout that segment also holds the PT_INTERP string before the code and read-only data after the PLT. A pointer into either becomes a code root and is decoded, for example an R_X86_64_RELATIVE addend or a REX.W rip-relative lea target:

  • /libexec/ld-elf.so.1 can decode into an fs-prefixed instruction (64 2d 65 6c 66 2e, the "d-elf."), and the conversion fails with Unsupported Windows guest TLS instruction (macOS: Unsupported macOS guest TLS instruction); the error depends on the bytes after the string;
  • data after the PLT can decode into Code analysis: overlapping instruction boundaries.

For executables and guest modules with PT_SCE_DYNLIBDATA, the code analysis now follows code only from the end of PT_INTERP to the end of the PLT after the last function of the unwind table. The end is the one #3142 gave the syscall and import call-site scans. #3142's PLT search moves unchanged into one helper (FindSceCodeEnd) that the pipeline and the code analysis share. A guest module in this layout with PLT relocations and no PLT after its last unwind function now fails the same way the executable does. The narrowed headers are used only to decide what is code; data is still read through the original headers. Executables without PT_SCE_DYNLIBDATA are not changed. A new debt entry, next to #3142's, records the code analysis extent and what it means for code after the PLT: that code is not analyzed, and a function symbol or init/fini array entry pointing into it fails. It is a separate line so that it does not touch #4717's neighbouring entry.

Linux is not affected: its patcher does not run the code analysis.

Tested

Ubuntu 24.04, GCC 13.3, relinker-only build with BUILD_TESTING=ON: 153/153 tests pass.

  • test_sce_code_extent.py gains two cases, each converted with --windows and with --macos:

    • sce-interpreter-pointer: a PT_INTERP with /libexec/ld-elf.so.1 in front of the function, and a RELATIVE relocation pointing at it.
    • sce-data-pointer-after-plt: a REX.W lea into read-only data after the PLT whose bytes decode as overlapping instructions.

    On main they fail with Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e) / Unsupported macOS guest TLS instruction (...) and with Code analysis: overlapping instruction boundaries. They pass here. The interpreter case uses gap=0x60 so that the bytes after the string decode into the fs-prefixed instruction.

  • A third case, sce-code-before-plt, places an unsupported fs-prefixed instruction at the entry point between the last unwind function and the PLT. #3093 notes that the OpenOrbis sample has 680 bytes of start-up code there. It must still fail with the TLS error, which shows that code is still analyzed. It passes on main as well. The four existing cases still pass.

  • Each part is needed:

    • Without the PT_INTERP lower bound, the interpreter cases fail.
    • Without the PLT upper bound, or with the control-flow walk kept on the whole segment, the data cases fail.
    • With the upper bound at the last unwind function instead of the end of the PLT, the code-before-PLT case fails.
    • Without the PT_SCE_DYNLIBDATA gate, tls_function_coverage_metadata and tls_function_coverage_padding fail.
  • Inputs without PT_SCE_DYNLIBDATA convert byte-identically on main and on this branch:

    • every relinker call the test suite makes (1,771 runs);
    • a matrix of 22 fixtures × Linux, Windows and macOS × with and without --to-intel × unused-filter=0 and 1 (264 conversions).
  • The OpenOrbis sample was not run here. After #4542 it stops on Windows and macOS at Code analysis: overlapping instruction boundaries (offset 0xde39f), which #4542 attributes to the inline jump tables item. Whether this PR moves that stop is not known.

  • tools/check_conventions.py from main passes with this title and body.

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
  • Unblocks: none (a #3093 item for PS4 executables; no Game bring-up discussion)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code); I reviewed and tested every line

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4844672379 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5195). Originally posted by [SandstromPL](https://github.com/SandstromPL) on 2026-10-13T14:21:40Z. --- ### What This is the #3093 item "Keep code analysis inside the code extent and out of PT_INTERP. The CFG followed pointers into `/libexec/ld-elf.so.1`." It was found in Get-fix's experimental run of the OpenOrbis `graphics` sample (#3094). The code analysis that the Windows TLS builder and the macOS image run (`CodeInstructionCollector`) treats every byte of an executable `PT_LOAD` as code. In the PS4 layout that segment also holds the `PT_INTERP` string before the code and read-only data after the PLT. A pointer into either becomes a code root and is decoded, for example an `R_X86_64_RELATIVE` addend or a REX.W rip-relative `lea` target: - `/libexec/ld-elf.so.1` can decode into an fs-prefixed instruction (`64 2d 65 6c 66 2e`, the "d-elf."), and the conversion fails with `Unsupported Windows guest TLS instruction` (macOS: `Unsupported macOS guest TLS instruction`); the error depends on the bytes after the string; - data after the PLT can decode into `Code analysis: overlapping instruction boundaries`. For executables and guest modules with `PT_SCE_DYNLIBDATA`, the code analysis now follows code only from the end of `PT_INTERP` to the end of the PLT after the last function of the unwind table. The end is the one #3142 gave the syscall and import call-site scans. #3142's PLT search moves unchanged into one helper (`FindSceCodeEnd`) that the pipeline and the code analysis share. A guest module in this layout with PLT relocations and no PLT after its last unwind function now fails the same way the executable does. The narrowed headers are used only to decide what is code; data is still read through the original headers. Executables without `PT_SCE_DYNLIBDATA` are not changed. A new debt entry, next to #3142's, records the code analysis extent and what it means for code after the PLT: that code is not analyzed, and a function symbol or init/fini array entry pointing into it fails. It is a separate line so that it does not touch #4717's neighbouring entry. Linux is not affected: its patcher does not run the code analysis. ### Tested Ubuntu 24.04, GCC 13.3, relinker-only build with `BUILD_TESTING=ON`: 153/153 tests pass. - `test_sce_code_extent.py` gains two cases, each converted with `--windows` and with `--macos`: - `sce-interpreter-pointer`: a `PT_INTERP` with `/libexec/ld-elf.so.1` in front of the function, and a RELATIVE relocation pointing at it. - `sce-data-pointer-after-plt`: a REX.W `lea` into read-only data after the PLT whose bytes decode as overlapping instructions. On main they fail with `Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e)` / `Unsupported macOS guest TLS instruction (...)` and with `Code analysis: overlapping instruction boundaries`. They pass here. The interpreter case uses `gap=0x60` so that the bytes after the string decode into the fs-prefixed instruction. - A third case, `sce-code-before-plt`, places an unsupported fs-prefixed instruction at the entry point between the last unwind function and the PLT. #3093 notes that the OpenOrbis sample has 680 bytes of start-up code there. It must still fail with the TLS error, which shows that code is still analyzed. It passes on main as well. The four existing cases still pass. - Each part is needed: - Without the `PT_INTERP` lower bound, the interpreter cases fail. - Without the PLT upper bound, or with the control-flow walk kept on the whole segment, the data cases fail. - With the upper bound at the last unwind function instead of the end of the PLT, the code-before-PLT case fails. - Without the `PT_SCE_DYNLIBDATA` gate, `tls_function_coverage_metadata` and `tls_function_coverage_padding` fail. - Inputs without `PT_SCE_DYNLIBDATA` convert byte-identically on main and on this branch: - every relinker call the test suite makes (1,771 runs); - a matrix of 22 fixtures × Linux, Windows and macOS × with and without `--to-intel` × `unused-filter=0` and `1` (264 conversions). - The OpenOrbis sample was not run here. After #4542 it stops on Windows and macOS at `Code analysis: overlapping instruction boundaries (offset 0xde39f)`, which #4542 attributes to the inline jump tables item. Whether this PR moves that stop is not known. - `tools/check_conventions.py` from main passes with this title and body. ### 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] Unblocks: none (a #3093 item for PS4 executables; no Game bring-up discussion) - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code); I reviewed and tested every line 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-13 14:56:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T14:56:51Z.


Windows test: head 1ed29156c merged onto current main ef42c5f2a, together with 9 other open pull requests in one build (the merges were conflict-free); a failure would have been bisected per PR. Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest for the tests this change adds or touches: sce_code_extent passed; python tools/check_conventions.py --base upstream/main passed for the combined tree. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148440115 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5195#issuecomment-6148440115). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T14:56:51Z. --- Windows test: head `1ed29156c` merged onto current `main` `ef42c5f2a`, together with 9 other open pull requests in one build (the merges were conflict-free); a failure would have been bisected per PR. Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest for the tests this change adds or touches: sce_code_extent passed; `python tools/check_conventions.py --base upstream/main` passed for the combined tree. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
SandstromPL commented 2026-10-13 15:42:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-13T15:42:49Z.


Thanks for testing on Windows! The head you tested (1ed29156c) was replaced by 78f620cf9, which only moves the new debt text onto its own line so it no longer conflicts with #4717; the code and tests are unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149356564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5195#issuecomment-6149356564). Originally posted by [SandstromPL](https://github.com/SandstromPL) on 2026-10-13T15:42:49Z. --- Thanks for testing on Windows! The head you tested (`1ed29156c`) was replaced by `78f620cf9`, which only moves the new debt text onto its own line so it no longer conflicts with #4717; the code and tests are unchanged.
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!5195
No description provided.