feat(relinker): executable TLS relocations (TPOFF64, DTPOFF64, DTPMOD64) #5262

Merged
NaitikJoshiPro merged 1 commit from upstream-pr/5262 into main 2026-10-14 02:41:32 +00:00
NaitikJoshiPro commented 2026-10-13 15:57:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NaitikJoshiPro on 2026-10-13T15:57:10Z.


What

A PS5 executable can keep initial-exec TLS as R_X86_64_TPOFF64 GOT relocations against its own TLS block. The SuperTuxKart homebrew eboot (STKT00001) has 13, all without a symbol. The relocation allowlist rejected them (Unsupported relocation type 0x12), so the executable could not be converted for any target.

  • Relocations against the executable's own block (symbol 0, or a defined STT_TLS symbol whose value is folded into the addend) go to the Linux ELF as symbolless dynamic relocations. The dynamic loader resolves them against where it placed the executable's PT_TLS block. A relocation against an imported symbol keeps its name and is typed STT_TLS.
  • The Windows and macOS images resolve TPOFF64 and DTPOFF64 statically: the block sits AlignUp(p_memsz, p_align) below the thread pointer, the same offset that WindowsTlsBuilder and MacOsImage use. Those targets reject DTPMOD64 and imported TLS with an error.
  • A defined non-TLS symbol, a TLS relocation without PT_TLS, and DTPMOD64 with a nonzero addend fail the relink. TPOFF32 stays unsupported.

This is separate from #4739, which changes TPOFF64 in guest modules (WindowsGuestModuleWriter); this PR only changes the executable's relocations.

Tested

macOS 27.0.1 on Apple M3 (arm64), AppleClang from the Command Line Tools, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON) on main 9d0a41d5a:

  • ctest: 147/147 pass, including the new executable_tls_relocations. It covers the Linux, Windows and macOS outputs and the rejected cases, and it fails on main with Unsupported relocation type 0x12.
  • The SuperTuxKart eboot (STKT00001) converts to Linux, Windows (--windows) and macOS (--macos). The Linux ELF has 13 symbolless TPOFF64 dynamic relocations.
  • python3 tools/check_conventions.py --base upstream/main: passed.

Not run: the converted executables were not run on Linux or Windows, because I have no x86-64 host. The Linux path relies on the dynamic loader's standard handling of symbol-0 TPOFF64/DTPOFF64/DTPMOD64 in the executable.

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. It shares lines with #4952 (RelinkerPipeline.cpp, relocation symbol extraction) and #5229 (ElfConstants.hpp, RelinkerPipeline.cpp). It does not build on either and implements nothing they do (neither touches TLS relocations). I will rebase onto whichever merges first.
  • Unblocks: none (SuperTuxKart STKT00001 now converts; it has no Game bring-up discussion)
  • GPUs tested: n/a
  • AI-assisted: yes. Written with AI assistance (Claude Code), which also ran the tests listed above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4845921974 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5262). Originally posted by [NaitikJoshiPro](https://github.com/NaitikJoshiPro) on 2026-10-13T15:57:10Z. --- ### What A PS5 executable can keep initial-exec TLS as `R_X86_64_TPOFF64` GOT relocations against its own TLS block. The SuperTuxKart homebrew eboot (STKT00001) has 13, all without a symbol. The relocation allowlist rejected them (`Unsupported relocation type 0x12`), so the executable could not be converted for any target. - Relocations against the executable's own block (symbol 0, or a defined `STT_TLS` symbol whose value is folded into the addend) go to the Linux ELF as symbolless dynamic relocations. The dynamic loader resolves them against where it placed the executable's `PT_TLS` block. A relocation against an imported symbol keeps its name and is typed `STT_TLS`. - The Windows and macOS images resolve `TPOFF64` and `DTPOFF64` statically: the block sits `AlignUp(p_memsz, p_align)` below the thread pointer, the same offset that `WindowsTlsBuilder` and `MacOsImage` use. Those targets reject `DTPMOD64` and imported TLS with an error. - A defined non-TLS symbol, a TLS relocation without `PT_TLS`, and `DTPMOD64` with a nonzero addend fail the relink. `TPOFF32` stays unsupported. This is separate from #4739, which changes `TPOFF64` in guest modules (`WindowsGuestModuleWriter`); this PR only changes the executable's relocations. ### Tested macOS 27.0.1 on Apple M3 (arm64), AppleClang from the Command Line Tools, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`) on main 9d0a41d5a: - `ctest`: 147/147 pass, including the new `executable_tls_relocations`. It covers the Linux, Windows and macOS outputs and the rejected cases, and it fails on main with `Unsupported relocation type 0x12`. - The SuperTuxKart eboot (STKT00001) converts to Linux, Windows (`--windows`) and macOS (`--macos`). The Linux ELF has 13 symbolless `TPOFF64` dynamic relocations. - `python3 tools/check_conventions.py --base upstream/main`: passed. Not run: the converted executables were not run on Linux or Windows, because I have no x86-64 host. The Linux path relies on the dynamic loader's standard handling of symbol-0 `TPOFF64`/`DTPOFF64`/`DTPMOD64` in the executable. ### Checklist - [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. It shares lines with #4952 (`RelinkerPipeline.cpp`, relocation symbol extraction) and #5229 (`ElfConstants.hpp`, `RelinkerPipeline.cpp`). It does not build on either and implements nothing they do (neither touches TLS relocations). I will rebase onto whichever merges first. - [x] Unblocks: none (SuperTuxKart STKT00001 now converts; it has no Game bring-up discussion) - [x] GPUs tested: n/a - [x] AI-assisted: yes. Written with AI assistance (Claude Code), which also ran the tests listed above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-13 16:03:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T16:03:20Z.


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
#4952 core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp L263-266
#5229 core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp L146
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149753542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5262#issuecomment-6149753542). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T16:03:20Z. --- <!-- 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 | |---|---|---|---| | #4952 | `core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp` [L263-266](https://github.com/boykopovar/AnyPS5/blob/cdea950b7e8b43fee556a48356120acaefd99f96/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp#L263-L266) | | | | #5229 | `core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp` [L146](https://github.com/boykopovar/AnyPS5/blob/cdea950b7e8b43fee556a48356120acaefd99f96/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp#L146) | | |
simonbronk commented 2026-10-13 21:00:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-13T21:00:59Z.


Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, relinker-only Release, this PR (6f856a7cd) merged onto main 0aa4cb1c2 (AI-assisted: Claude Code).

  • ctest: 151/151, including executable_tls_relocations.
  • Runtime check of the Windows output, which the PR could not run: a probe built from this PR's image() fixture whose entry reads the thread pointer (mov rax, fs:[0]), loads the TPOFF64 GOT slot at 0x1000, and returns movzx eax, byte [rax + rcx] as the exit code. The TLS template holds bytes 1..8. Relinked with --windows and run:
Relocation Exit code Expected
symbol 0, addend 0 0x1 0x1
symbol 0, addend 4 0x5 0x5
symbol 0, addend 7 0x8 0x8
STT_TLS symbol (value 0x10), addend -0xd 0x4 0x4
symbol 0, addend 0x20 (past the template, zero-filled) 0x0 0x0

So the statically resolved TPOFF64 values match where the converted executable's TLS block sits below the thread pointer at run time.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6155109480 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5262#issuecomment-6155109480). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-13T21:00:59Z. --- Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, relinker-only Release, this PR (`6f856a7cd`) merged onto `main` `0aa4cb1c2` (AI-assisted: Claude Code). - `ctest`: 151/151, including `executable_tls_relocations`. - Runtime check of the Windows output, which the PR could not run: a probe built from this PR's `image()` fixture whose entry reads the thread pointer (`mov rax, fs:[0]`), loads the `TPOFF64` GOT slot at 0x1000, and returns `movzx eax, byte [rax + rcx]` as the exit code. The TLS template holds bytes 1..8. Relinked with `--windows` and run: | Relocation | Exit code | Expected | |---|---|---| | symbol 0, addend 0 | 0x1 | 0x1 | | symbol 0, addend 4 | 0x5 | 0x5 | | symbol 0, addend 7 | 0x8 | 0x8 | | `STT_TLS` symbol (value 0x10), addend -0xd | 0x4 | 0x4 | | symbol 0, addend 0x20 (past the template, zero-filled) | 0x0 | 0x0 | So the statically resolved `TPOFF64` values match where the converted executable's TLS block sits below the thread pointer at run time.
NaitikJoshiPro commented 2026-10-13 23:25:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NaitikJoshiPro on 2026-10-13T23:25:04Z.


Thanks @simonbronk, that covers the gap I couldn't test: the statically resolved TPOFF64 values for Windows output, run on real hardware, including the STT_TLS-symbol case and an offset past the template. The Linux path (symbol-0 TPOFF64/DTPOFF64/DTPMOD64 left to the dynamic loader) is still unexecuted here, so a Linux run would close the last part.

(Reply written with AI assistance, Claude Code.)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157330531 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5262#issuecomment-6157330531). Originally posted by [NaitikJoshiPro](https://github.com/NaitikJoshiPro) on 2026-10-13T23:25:04Z. --- Thanks @simonbronk, that covers the gap I couldn't test: the statically resolved `TPOFF64` values for Windows output, run on real hardware, including the `STT_TLS`-symbol case and an offset past the template. The Linux path (symbol-0 `TPOFF64`/`DTPOFF64`/`DTPMOD64` left to the dynamic loader) is still unexecuted here, so a Linux run would close the last part. (Reply written with AI assistance, Claude Code.)
simonbbby commented 2026-10-14 00:24:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-14T00:24:07Z.


Linux runtime follow-up on exact head 6f856a7cda16568b4bd7e48e0e4b9029512de4e7, no additional source changes: the executable-local TLS relocations are resolved correctly by glibc 2.39 in these synthetic probes.

Linux x86-64, GCC, relinker-only Release, BUILD_TESTING=ON: 156/156 CTests pass, including executable_tls_relocations (23.24 s total).

For runtime coverage I reused this PR's image() fixture, with TLS initializer bytes 1..8 and the same PT_TLS memory size/alignment. The entry for TPOFF64 reads fs:[0], loads the relocated GOT slot at 0x1000, reads the byte at TP + that offset, and returns it in RAX. The other entries return the resolved GOT value. Relinked to Linux and executed the unmodified output through its normal /lib64/ld-linux-x86-64.so.2 interpreter. No loader shim, preload, or post-relink byte patch was used.

The generated entry trampoline ends in UD2 after the guest entry returns, so I used GDB to inspect RAX at that expected stop and verified the stopped instruction bytes were 0f 0b.

Relocation Symbol / addend Runtime result, 3/3 runs Expected
TPOFF64 symbol 0 / 0 1 TLS byte 1
TPOFF64 symbol 0 / 4 5 TLS byte 5
TPOFF64 symbol 0 / 7 8 TLS byte 8
TPOFF64 defined STT_TLS, value 0x10 / -0xd 4 TLS byte 4
TPOFF64 symbol 0 / 0x20 0 zero-filled TLS tail
DTPOFF64 symbol 0 / 4 4 block-relative offset 4
DTPOFF64 defined STT_TLS, value 0x10 / -0xd 3 block-relative offset 3
DTPMOD64 symbol 0 / 0 1 executable's TLS module ID

8 probes × 3 = 24 successful runtime assertions. This covers local executable relocations on this Linux loader, including defined-symbol folding and TLS zero-fill. Imported TLS, a complete SuperTuxKart run, and other Linux loaders remain untested here; the DTPOFF64/DTPMOD64 rows inspect relocation values rather than calling __tls_get_addr.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158062209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5262#issuecomment-6158062209). Originally posted by [simonbbby](https://github.com/simonbbby) on 2026-10-14T00:24:07Z. --- Linux runtime follow-up on exact head `6f856a7cda16568b4bd7e48e0e4b9029512de4e7`, no additional source changes: the executable-local TLS relocations are resolved correctly by **glibc 2.39** in these synthetic probes. Linux x86-64, GCC, relinker-only Release, `BUILD_TESTING=ON`: **156/156 CTests pass**, including `executable_tls_relocations` (23.24 s total). For runtime coverage I reused this PR's `image()` fixture, with TLS initializer bytes 1..8 and the same `PT_TLS` memory size/alignment. The entry for `TPOFF64` reads `fs:[0]`, loads the relocated GOT slot at 0x1000, reads the byte at TP + that offset, and returns it in RAX. The other entries return the resolved GOT value. Relinked to Linux and executed the unmodified output through its normal `/lib64/ld-linux-x86-64.so.2` interpreter. No loader shim, preload, or post-relink byte patch was used. The generated entry trampoline ends in `UD2` after the guest entry returns, so I used GDB to inspect RAX at that expected stop and verified the stopped instruction bytes were `0f 0b`. | Relocation | Symbol / addend | Runtime result, 3/3 runs | Expected | |---|---|---|---| | TPOFF64 | symbol 0 / 0 | 1 | TLS byte 1 | | TPOFF64 | symbol 0 / 4 | 5 | TLS byte 5 | | TPOFF64 | symbol 0 / 7 | 8 | TLS byte 8 | | TPOFF64 | defined STT_TLS, value 0x10 / -0xd | 4 | TLS byte 4 | | TPOFF64 | symbol 0 / 0x20 | 0 | zero-filled TLS tail | | DTPOFF64 | symbol 0 / 4 | 4 | block-relative offset 4 | | DTPOFF64 | defined STT_TLS, value 0x10 / -0xd | 3 | block-relative offset 3 | | DTPMOD64 | symbol 0 / 0 | 1 | executable's TLS module ID | **8 probes × 3 = 24 successful runtime assertions.** This covers local executable relocations on this Linux loader, including defined-symbol folding and TLS zero-fill. Imported TLS, a complete SuperTuxKart run, and other Linux loaders remain untested here; the DTPOFF64/DTPMOD64 rows inspect relocation values rather than calling `__tls_get_addr`.
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!5262
No description provided.