feat(relinker): load Windows guest TLS values through register addresses #1233

Merged
hashaam1217 merged 1 commit from upstream-pr/1233 into main 2026-10-08 09:11:11 +00:00
hashaam1217 commented 2026-10-07 18:49:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-07T18:49:07Z.


What

#217: NHL 27 (PPSA34063) relinked for Windows on main (cea1ef13) now stops at Unsupported Windows guest TLS instruction (bytes: 64 48 8b 03) (offset 0x747c663), which is mov rax, fs:[rbx]. #340 and #767 handle fs:[disp32]; this adds loads through a register address.

  • mov r64 and mov r32 from fs:[mem], where mem has a base or an index register (ModRM/SIB with no displacement, disp8 or disp32), now relink. The stub saves rax and rcx, computes the offset with lea rax, [mem] from the original addressing bytes, loads the thread pointer as the existing stubs do and runs mov dst, [rax+rcx]. A 32-bit load zero-extends, as on hardware. Only mov, lea, push and pop run, so flags are kept.
  • The thread pointer is the one the existing stubs use: the end of the module's TLS block, which TPOFF64 values are written against in WindowsGuestModuleWriter. So fs:[reg] reads the same bytes as fs:[disp32] with that value.
  • These instructions can be 3 or 4 bytes, shorter than the 5-byte jump. patchAccess wrote the jump anyway, over the next instruction; every form supported so far was at least 7 bytes, so it never happened. A short site now moves the following sequential instructions into its stub, with RIP-relative displacements recomputed. A branch, call, return or another TLS access after it fails the relink with "Short guest TLS instruction is followed by an instruction that cannot move" and its offset. patchAccess throws for a site shorter than the jump.
  • Still rejected with the existing message: an rsp destination or base, RIP-relative, 16-bit, stores and ALU forms through a register address, and absolute 32-bit loads.
  • rex-x-load (64 4a 8b 04 25 28 00 00 00) leaves the rejected list: REX.X makes the SIB index r12, so it is mov rax, fs:[r12+0x28], a register-addressed load.

The register's value is not range-checked: unlike a disp32, it is only known at run time.

#1117 also edits test_tls_function_coverage.py; the two merge without conflict.

Tested

Ubuntu 24.04.5, GCC 13.3.0. Full build and ctest: 399/399 passed.

  • tls_function_coverage: 22 new execution cases over 11 addressing forms (NHL 27's 64 48 8b 03, base equal to the destination, base + index + disp8, r12/r13/rbp bases, scaled index with disp32, index only, 32-bit loads into edx and r9d), each with flags 0x202 and 0xad7, plus a RIP-relative follower and a 3-byte load that needs two followers. Each case checks the loaded value, every other register and RFLAGS, and returns 42. On Linux the test checks the stub bytes; where os.name == "nt" it runs the cases. New rejections: rsp base and destination, RIP-relative, 16-bit, a store and an ALU read through a register, and short loads followed by ret, call or another TLS access, with the follower's offset. With the relinker from main the test fails with NHL 27's message.
  • Execution on Linux: I loaded each relinked PE into memory, pointed GS at a hand-made TEB whose TLS slot holds a block copied from the template, ran the TLS callback for process attach and called the case. All 26 new cases and the 201 existing ALU and displacement cases return 42; a case that loads from the wrong offset returns 1. This is what found the short-site overwrite: the first version passed the byte checks and crashed on the 3- and 4-byte forms. The harness is not part of this PR.
  • Windows was not run locally; CI runs the cases there.
  • No title was run. NHL 27 may stop at another TLS form next; the message gives its bytes and offset.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4777012710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1233). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-07T18:49:07Z. --- ### What #217: NHL 27 (PPSA34063) relinked for Windows on `main` (`cea1ef13`) now stops at `Unsupported Windows guest TLS instruction (bytes: 64 48 8b 03) (offset 0x747c663)`, which is `mov rax, fs:[rbx]`. #340 and #767 handle `fs:[disp32]`; this adds loads through a register address. - `mov r64` and `mov r32` from `fs:[mem]`, where `mem` has a base or an index register (ModRM/SIB with no displacement, disp8 or disp32), now relink. The stub saves `rax` and `rcx`, computes the offset with `lea rax, [mem]` from the original addressing bytes, loads the thread pointer as the existing stubs do and runs `mov dst, [rax+rcx]`. A 32-bit load zero-extends, as on hardware. Only `mov`, `lea`, `push` and `pop` run, so flags are kept. - The thread pointer is the one the existing stubs use: the end of the module's TLS block, which `TPOFF64` values are written against in `WindowsGuestModuleWriter`. So `fs:[reg]` reads the same bytes as `fs:[disp32]` with that value. - These instructions can be 3 or 4 bytes, shorter than the 5-byte jump. `patchAccess` wrote the jump anyway, over the next instruction; every form supported so far was at least 7 bytes, so it never happened. A short site now moves the following sequential instructions into its stub, with RIP-relative displacements recomputed. A branch, call, return or another TLS access after it fails the relink with "Short guest TLS instruction is followed by an instruction that cannot move" and its offset. `patchAccess` throws for a site shorter than the jump. - Still rejected with the existing message: an `rsp` destination or base, RIP-relative, 16-bit, stores and ALU forms through a register address, and absolute 32-bit loads. - `rex-x-load` (`64 4a 8b 04 25 28 00 00 00`) leaves the rejected list: REX.X makes the SIB index `r12`, so it is `mov rax, fs:[r12+0x28]`, a register-addressed load. The register's value is not range-checked: unlike a `disp32`, it is only known at run time. #1117 also edits `test_tls_function_coverage.py`; the two merge without conflict. ### Tested Ubuntu 24.04.5, GCC 13.3.0. Full build and `ctest`: 399/399 passed. - `tls_function_coverage`: 22 new execution cases over 11 addressing forms (NHL 27's `64 48 8b 03`, base equal to the destination, base + index + disp8, `r12`/`r13`/`rbp` bases, scaled index with disp32, index only, 32-bit loads into `edx` and `r9d`), each with flags 0x202 and 0xad7, plus a RIP-relative follower and a 3-byte load that needs two followers. Each case checks the loaded value, every other register and RFLAGS, and returns 42. On Linux the test checks the stub bytes; where `os.name == "nt"` it runs the cases. New rejections: `rsp` base and destination, RIP-relative, 16-bit, a store and an ALU read through a register, and short loads followed by `ret`, `call` or another TLS access, with the follower's offset. With the relinker from `main` the test fails with NHL 27's message. - Execution on Linux: I loaded each relinked PE into memory, pointed `GS` at a hand-made TEB whose TLS slot holds a block copied from the template, ran the TLS callback for process attach and called the case. All 26 new cases and the 201 existing ALU and displacement cases return 42; a case that loads from the wrong offset returns 1. This is what found the short-site overwrite: the first version passed the byte checks and crashed on the 3- and 4-byte forms. The harness is not part of this PR. - Windows was not run locally; CI runs the cases there. - No title was run. NHL 27 may stop at another TLS form next; the message gives its bytes and offset. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-08 09:11:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:11:07Z.


Checked on main + this PR: build and full ctest green (430/430); tls_function_coverage fails without the WindowsTlsBuilder.cpp change. Read the stub (save/restore of rax/rcx for every destination, flags untouched, 32-bit zero-extension), the rejected forms and the short-site move with RIP-relative fix-up; all consistent with the description. Linux and Windows CI green; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056600583 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1233#issuecomment-6056600583). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:11:07Z. --- Checked on main + this PR: build and full ctest green (430/430); `tls_function_coverage` fails without the `WindowsTlsBuilder.cpp` change. Read the stub (save/restore of `rax`/`rcx` for every destination, flags untouched, 32-bit zero-extension), the rejected forms and the short-site move with RIP-relative fix-up; all consistent with the description. Linux and Windows CI green; merging.
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!1233
No description provided.