feat(relinker): load the fs:[0] thread pointer into any register in Windows guest code #222

Merged
YossiYad merged 1 commit from upstream-pr/222 into main 2026-10-02 07:06:12 +00:00
YossiYad commented 2026-10-01 16:29:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-01T16:29:25Z.


What

The Windows TLS builder accepted the thread pointer load only into RAX (64 48 8b 04 25 00 00 00 00). The same instruction with another destination (mov rcx, fs:[0], mov r9, fs:[0], ...) failed the relink with "Unsupported Windows guest TLS instruction".

  • WindowsTlsBuilder now takes mov r64, fs:[0] for every destination except RSP (REX 48 or 4c, any ModRM reg field). The stub computes the pointer in RAX as before, moves it into the destination and restores RAX and RCX; the destination is the only register that changes. The bytes emitted for the RAX load and for the fs:[0x28] store are unchanged.
  • test_tls_function_coverage.py relinks one image per destination register. Each body sets EAX and ECX, loads the thread pointer, returns 0 if either register changed and otherwise the TLS value 42. The RSP form is checked to still fail.

Possibly the cause of #217, not confirmed: the report has the offset but not the instruction bytes.

Tested

  • Linux (Ubuntu 24.04 container, GCC 13): full ctest passes, 89 tests. Disassembled the generated .gtcode stubs for RAX, RCX, RDX, R9 and R12.
  • Windows: the Build workflow on the fork branch, where tls_function_coverage executes the 15 relinked executables: https://github.com/YossiYad/AnyPS5/actions/runs/36886340037
  • No game title was relinked with this change.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4706557298 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/222). Originally posted by [YossiYad](https://github.com/YossiYad) on 2026-10-01T16:29:25Z. --- ### What The Windows TLS builder accepted the thread pointer load only into RAX (`64 48 8b 04 25 00 00 00 00`). The same instruction with another destination (`mov rcx, fs:[0]`, `mov r9, fs:[0]`, ...) failed the relink with "Unsupported Windows guest TLS instruction". - `WindowsTlsBuilder` now takes `mov r64, fs:[0]` for every destination except RSP (REX `48` or `4c`, any ModRM reg field). The stub computes the pointer in RAX as before, moves it into the destination and restores RAX and RCX; the destination is the only register that changes. The bytes emitted for the RAX load and for the `fs:[0x28]` store are unchanged. - `test_tls_function_coverage.py` relinks one image per destination register. Each body sets EAX and ECX, loads the thread pointer, returns 0 if either register changed and otherwise the TLS value 42. The RSP form is checked to still fail. Possibly the cause of #217, not confirmed: the report has the offset but not the instruction bytes. ### Tested - Linux (Ubuntu 24.04 container, GCC 13): full `ctest` passes, 89 tests. Disassembled the generated `.gtcode` stubs for RAX, RCX, RDX, R9 and R12. - Windows: the `Build` workflow on the fork branch, where `tls_function_coverage` executes the 15 relinked executables: https://github.com/YossiYad/AnyPS5/actions/runs/36886340037 - No game title was relinked with this change. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
QDarkRex commented 2026-10-02 04:14:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-02T04:14:42Z.


Tested this independently on Linux: same 28 synthetic fs:/gs: instruction forms through the relinker built from main (709d7fe) and from this branch (53d4ef4), --windows --skip-sce-module.

Newly accepted with this PR, as described: mov rcx/rdx/r8/r14, fs:0 and rax with REX.R. mov rsp, fs:0 still fails, as intended, and the previously supported forms still pass.

Still rejected, in case one of these turns out to be what #217 hits:

  • reads at a non-zero offset: mov rax, fs:0x28, mov rax, fs:0x10, mov eax, fs:-4, mov rax, fs:-8, cmp rax, fs:0x28
  • 32-bit load: mov eax, fs:0
  • register-based: fs:[rax], fs:[rax+rcx], fs:[rax+0x10], fs:[rax+disp32], fs:[rip+disp]
  • stores other than the dword one to fs:0x28: mov fs:0, rax, REX.W mov fs:0x28, imm32, mov fs:0x30, imm32
  • add rax, fs:0, xchg rax, fs:0, mov rax, gs:0x58

I've asked the #217 reporter for the bytes at the failing offset and whether --skip-sce-module changes the result (the same check also runs on sce_module files, so the failure may not be in eboot.bin).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5945486733 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/222#issuecomment-5945486733). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-10-02T04:14:42Z. --- Tested this independently on Linux: same 28 synthetic `fs:`/`gs:` instruction forms through the relinker built from `main` (709d7fe) and from this branch (53d4ef4), `--windows --skip-sce-module`. Newly accepted with this PR, as described: `mov rcx/rdx/r8/r14, fs:0` and `rax` with REX.R. `mov rsp, fs:0` still fails, as intended, and the previously supported forms still pass. Still rejected, in case one of these turns out to be what #217 hits: - reads at a non-zero offset: `mov rax, fs:0x28`, `mov rax, fs:0x10`, `mov eax, fs:-4`, `mov rax, fs:-8`, `cmp rax, fs:0x28` - 32-bit load: `mov eax, fs:0` - register-based: `fs:[rax]`, `fs:[rax+rcx]`, `fs:[rax+0x10]`, `fs:[rax+disp32]`, `fs:[rip+disp]` - stores other than the dword one to `fs:0x28`: `mov fs:0, rax`, REX.W `mov fs:0x28, imm32`, `mov fs:0x30, imm32` - `add rax, fs:0`, `xchg rax, fs:0`, `mov rax, gs:0x58` I've asked the #217 reporter for the bytes at the failing offset and whether `--skip-sce-module` changes the result (the same check also runs on `sce_module` files, so the failure may not be in `eboot.bin`).
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!222
No description provided.