Skip to content

fix(relinker): keep the interpreter path out of the scanned code - #7996

Closed
javierUDP wants to merge 2 commits into
boykopovar:mainfrom
javierUDP:fix/interp-outside-code-scan
Closed

javierUDP wants to merge 2 commits into
boykopovar:mainfrom
javierUDP:fix/interp-outside-code-scan

Conversation

@javierUDP

Copy link
Copy Markdown
Contributor

What

The code analysis trims an interpreter path from the start of an executable segment only for images that have a PT_SCE_DYNLIBDATA block. An executable without one keeps that path in the scanned range.

/libexec/ld-elf.so.1 then gets walked as code: a R_X86_64_RELATIVE relocation with addend 0 roots the control flow graph at the first byte of the segment, and the 0x64 of ld-elf decodes as an FS-prefixed instruction, which the Windows and macOS TLS rewriters refuse:

FAIL: Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e) (offset 0x400a)

Linux output is unaffected because it keeps the guest %fs accesses. The trim now runs for both layouts, ending at the segment for an image without the block. This is the PT_INTERP item of the remaining PS4 work in #3093.

Tested

Apple M5, macOS 26.4, AppleClang 21.0.0, relinker-only build of current main.

The public OpenOrbis homebrew samples reproduce it, so no title dump is needed: samples-elf-llvm-18.zip from OpenOrbis-PS4-Toolchain v0.5.3 (GPL-3.0). Their executable segment starts with the interpreter path at vaddr 0, and they carry the addend-0 relocation.

--windows, 21 sample images main this branch
17 executables (graphics, SDL2, hello_world, piglet, font, input, networking, …) all refuse all convert
3 modules (libc, libSceFios2, right) convert convert

--macos refuses and converts the same way; graphics converts on all three targets with the change.

  • New interpreter_in_code test: an image without an SCE block whose executable segment begins with the interpreter path, reached through an addend-0 RELATIVE relocation. It fails on main with the message above and passes here, for --windows and --macos, with a second image that has no interpreter path as the control.
  • Full suite: 207/207 (206 before this test).
  • python3 tools/check_conventions.py --base origin/main passes.

No Linux or Windows host was available, so the converted images were not executed; the change only narrows what the analysis scans.

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: n/a
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: n/a
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

The code analysis trims an interpreter path from the start of an
executable segment only for images that have a PT_SCE_DYNLIBDATA block.
An executable without one keeps that path in the scanned range, so
/libexec/ld-elf.so.1 is walked as code: a RELATIVE relocation with
addend 0 roots the control flow graph at the first byte of the segment,
and the 0x64 of "ld-elf" decodes as an FS-prefixed instruction that the
Windows and macOS TLS rewriters refuse. The trim now runs for both
layouts, ending at the segment for an image without the block.
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

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
PR 6044 core/relinker/CMakeLists.txt
PR 6204 core/relinker/CMakeLists.txt
PR 6618 core/relinker/CMakeLists.txt
core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
PR 6713 core/relinker/CMakeLists.txt
PR 6879 core/relinker/CMakeLists.txt
PR 6977 core/relinker/CMakeLists.txt
core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
PR 7037 core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
PR 7173 core/relinker/CMakeLists.txt
PR 7417 core/relinker/CMakeLists.txt
PR 7531 core/relinker/CMakeLists.txt
PR 7532 core/relinker/CMakeLists.txt
PR 7704 core/relinker/CMakeLists.txt
PR 7837 core/relinker/CMakeLists.txt
PR 8063 core/relinker/CMakeLists.txt
PR 8076 core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
PR 8118 core/relinker/CMakeLists.txt
PR 8142 core/relinker/CMakeLists.txt
PR 8251 core/relinker/CMakeLists.txt

@wachi35676

Copy link
Copy Markdown
Contributor

Verdict: confirmed on Windows and Linux, with one edge case below.

Environment: Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release build. This PR was merged onto main 9127ea3. Linux: WSL Ubuntu 24.04, GCC 13.3.

  • OpenOrbis samples-elf-llvm-18.zip from the official v0.5.3 release, all 21 ELFs, converted with relinker --skip-sce-module and --windows or --macos:
    • The 17 executables are refused on main with Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e) (offset 0x400a), or the macOS equivalent. All of them convert with this PR, on both targets.
    • The 3 modules (libc, libSceFios2, right) convert on both, and their outputs are byte-identical to main.
  • PS5 homebrew without PT_INTERP: PPSA99005, PPSA47402, PPSA99581, and PPSA99995 with its 16 Wine modules. Relinked with --windows --to-intel, --macos --to-rosetta and Linux --to-intel, all 12 output sets are byte-identical to main.
  • Negative control: with only CodeInstructionCollector.cpp reverted to main, interpreter_in_code fails with Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e) (offset 0x100a).
  • ctest: Windows 213/213, after a serial rerun of 10 tests that timed out under -j4 because of host load. WSL 218/218.

Edge case: trimToCode sets the start of the analyzed code to the end of any PT_INTERP that lies inside the executable segment, not only one at its start. Before this PR, only SCE images got that trim. Now an image without an SCE block whose PT_INTERP follows the code in the same executable segment loses the code in front of it. Probe: the make_image("register", "unwind") image from test_tls_function_coverage.py, plus a sixth program header PT_INTERP at 0x1f00 (size 21, /libexec/ld-elf.so.1) inside the executable segment 0x1000-0x2000, with the code at 0x1200.

--windows main this PR
no PT_INTERP rc 0 rc 0
PT_INTERP at segment start 0x1000 rc 0 rc 0
PT_INTERP at 0x1f00, after the code rc 0 rc 2 Code analysis: invalid function range (offset 0x1200)

It fails loudly, and linkers normally put .interp before .text. Trimming only when the interpreter range starts at the segment start would avoid it.

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

Trimming the end of any PT_INTERP inside an executable segment dropped
the code in front of one that follows it, so an image without an SCE
block whose interpreter sits after its code lost its entry points. The
trim now applies to an interpreter that starts the segment, and the SCE
path keeps taking any interpreter inside the scanned range, as before.
@javierUDP

Copy link
Copy Markdown
Contributor Author

Confirmed and fixed in 32b2a9196. You are right, and the probe reproduces here: Apple M5, macOS 26.4, AppleClang 21.0.0, relinker-only build.

--windows, make_image("register", "unwind") main 7db5ba1d7 32b2a9196
no PT_INTERP rc 0 rc 0 rc 0
PT_INTERP at the segment start rc 0 rc 0 rc 0
PT_INTERP at 0x1f00, after the code rc 0 rc 2 rc 0

trimToCode now takes an interpreter only when it starts the segment, and the SCE path keeps taking any interpreter inside the scanned range, so nothing changes for images with an SCE block. I kept it to that rather than reordering the scan, since the trim is only meant to skip a path that precedes the code.

interpreter_in_code gained a third image whose PT_INTERP follows the code. It fails on 7db5ba1d7 with Code analysis: no code entry points and passes on 32b2a9196, so the case is covered from here on. The suite is 207/207 and the 17 OpenOrbis executables still convert on --windows and --macos.

Thanks for the Windows and Linux runs, and for the byte-identical check on the PS5 homebrew: that covers the ground I could not, since I have neither host.

@wachi35676

Copy link
Copy Markdown
Contributor

Re-tested 32b2a9196. The edge case is fixed, and nothing else moved.

Environment: Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release build. The PR is merged onto main 3aa35dc, and that main is the baseline. Linux: WSL Ubuntu 24.04, GCC 13.3, -DBUILD_TESTING=ON.

Same probe as before: make_image("register", "unwind") plus a PT_INTERP (size 21, /libexec/ld-elf.so.1), converted with --skip-sce-module --windows.

--windows main 3aa35dc 7db5ba1d7 32b2a9196
no PT_INTERP rc 0 rc 0 rc 0
PT_INTERP at the segment start 0x1000 rc 0 rc 0 rc 0
PT_INTERP at 0x1f00, after the code rc 0 rc 2 invalid function range (offset 0x1200) rc 0

In every rc 0 case, the original TLS bytes at 0x1240 appear once in the output, as they do on main.

  • OpenOrbis v0.5.3 samples-elf-llvm-18.zip, --skip-sce-module: all 17 executables convert with --windows and with --macos. On main they still fail with Unsupported Windows guest TLS instruction (bytes: 64 2d 65 6c 66 2e) (offset 0x400a). The 3 modules give the same bytes as main. All 40 outputs are also byte-identical to the ones from 7db5ba1d7.
  • PS5 homebrew without PT_INTERP (PPSA99005, PPSA47402, PPSA99581, and PPSA99995 with its Wine modules): relinked with --windows --to-intel, --macos --to-rosetta and Linux --to-intel. All 12 output sets are byte-identical to main.
  • Negative control: with only CodeInstructionCollector.cpp set back to 7db5ba1d7, interpreter_in_code fails on the new trailing image with Code analysis: no code entry points. It passes on 32b2a9196.
  • ctest, Windows: 213/214 under -j4. guest_none_relocations timed out under host load and passed on a serial rerun, so 214/214.
  • ctest, WSL: 218/219. shader_cache_runtime_sources failed because its nested cmake configure timed out after 15 s while other work was loading the host. This PR does not touch that test. It passed twice on a rerun, so 219/219.
  • tools/check_conventions.py: passes.

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

@javierUDP

Copy link
Copy Markdown
Contributor Author

@wachi35676 thanks for the second round, and for diffing all 40 outputs against 7db5ba1d7 byte for byte: that is what shows the edge-case fix changed nothing else. The Windows and Linux runs and the PS5 homebrew checks cover the ground I cannot, since I have neither host.

On the overlap notice: I checked the one pull request in it that shares CodeInstructionCollector.cpp, #6618. It does not merge into main on its own. Merged into main 49fd13a74 by itself it conflicts in CodeInstructionCollector.cpp and RelinkerPipeline.cpp, the same two files it conflicts in when merged on top of this branch, and this branch does not touch RelinkerPipeline.cpp at all. So the conflict is between #6618 and main rather than with this pull request, and it needs a rebase either way.

The other pull requests in the notice share only core/relinker/CMakeLists.txt, where this branch adds one add_test block next to the other relinker Python tests.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. This now conflicts with main in core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp, after #8166 (which changes how the code roots and the syscall scan treat data between PS4 unwind functions) merged today. Could you rebase onto current main, keeping both rules (the new SCE gap handling and your interpreter trim for images without PT_SCE_DYNLIBDATA)? Once it merges cleanly we'll run the relinker CI and a relink-identity check on our test titles, since this path runs for PS5 executables too.

@javierUDP javierUDP closed this Oct 20, 2026
@javierUDP
javierUDP deleted the fix/interp-outside-code-scan branch October 20, 2026 17:58
@javierUDP

Copy link
Copy Markdown
Contributor Author

@oneandonlydean rebased onto current main as you asked, keeping both rules: the sceFunctions and merged unwind ranges from #8166, and the interpreter trim for images without PT_SCE_DYNLIBDATA.

This pull request closed when I republished its branch, and GitHub will not reopen it, so the rebased work is in #8599. Apologies for the churn — the two commits and the discussion here are unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants