fix(relinker): place the Windows thread pointer after the TLS size rounded to its own alignment #3290

Merged
reyani-dev merged 2 commits from upstream-pr/3290 into main 2026-10-11 07:26:25 +00:00
reyani-dev commented 2026-10-11 05:35:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T05:35:18Z.


What

Carries @xXPBSXx's fix from #3136, which was shared as a branch while intake was paused. Both commits are theirs, unchanged (8e03bb0a fix, 272b9d13 test), and merge cleanly onto current main.

On Windows, the relinker's .gtls stub set the guest thread pointer blockSize bytes past the start of the per-thread block, where blockSize = align_up(p_memsz, max(p_align, 16)). PS5 code (FreeBSD amd64 variant II) reads TLS variables at tp - align_up(p_memsz, p_align). When the two roundings differ, every TLS read and write lands that many bytes before the variable. The stub now uses align_up(p_memsz, p_align) for the thread pointer and for the displacement bounds check. The block allocation keeps blockSize, so the self-pointer and the stack guard at tp + 0x28 still fit inside it, and both are written through the same loadPointer.

The new windows_tls_thread_pointer test builds an ELF with PT_TLS memsz = 0x21, align = 8 and reads the first variable at tp - 0x28.

Closes #3136.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, relinker-only Release build (ANYPS5_RELINKER_ONLY=ON), the two commits merged onto main 961b25fe:

  • Relinker ctest: 101/101, including windows_tls_thread_pointer. It builds without warnings.
  • The new test run against a relinker built from main fails as described: read 0x63 instead of the first variable 0x2a.

Homebrew titles affected. I checked PT_TLS in the 48 unwrapped catalog executables. Three have p_memsz = 0x258, p_align = 8: Prospero Explorer (PPSA99109), Prospero Multi Tools (PPSA99110) and Prospero Vibrate (PPSA99920). The other 45 have no TLS. For those three:

  • main and this PR both relink them (--windows, rc 0).
  • In the generated stubs, the thread-pointer lea rax, [rax + imm32] that follows the TLS-slot load is 0x260 on main and 0x258 with this PR. That holds in every stub: 686, 719 and 671 converted TLS accesses respectively.
  • So on Windows, main reads every thread variable of these titles 8 bytes before its address.

I did not run the titles (static conversion only). The author found the bug at runtime in Resident Evil 4 (PPSA07411), where a thread read another thread variable.

check_conventions.py --base upstream/main passes, and pr_overlap.py reports no overlap.

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:4816851758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3290). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T05:35:18Z. --- ### What Carries @xXPBSXx's fix from #3136, which was shared as a branch while intake was paused. Both commits are theirs, unchanged (`8e03bb0a` fix, `272b9d13` test), and merge cleanly onto current `main`. On Windows, the relinker's `.gtls` stub set the guest thread pointer `blockSize` bytes past the start of the per-thread block, where `blockSize = align_up(p_memsz, max(p_align, 16))`. PS5 code (FreeBSD amd64 variant II) reads TLS variables at `tp - align_up(p_memsz, p_align)`. When the two roundings differ, every TLS read and write lands that many bytes before the variable. The stub now uses `align_up(p_memsz, p_align)` for the thread pointer and for the displacement bounds check. The block allocation keeps `blockSize`, so the self-pointer and the stack guard at `tp + 0x28` still fit inside it, and both are written through the same `loadPointer`. The new `windows_tls_thread_pointer` test builds an ELF with PT_TLS `memsz = 0x21`, `align = 8` and reads the first variable at `tp - 0x28`. Closes #3136. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, relinker-only Release build (`ANYPS5_RELINKER_ONLY=ON`), the two commits merged onto `main` `961b25fe`: - Relinker `ctest`: 101/101, including `windows_tls_thread_pointer`. It builds without warnings. - The new test run against a relinker built from `main` fails as described: `read 0x63 instead of the first variable 0x2a`. **Homebrew titles affected.** I checked PT_TLS in the 48 unwrapped catalog executables. Three have `p_memsz = 0x258`, `p_align = 8`: Prospero Explorer (PPSA99109), Prospero Multi Tools (PPSA99110) and Prospero Vibrate (PPSA99920). The other 45 have no TLS. For those three: - `main` and this PR both relink them (`--windows`, rc 0). - In the generated stubs, the thread-pointer `lea rax, [rax + imm32]` that follows the TLS-slot load is `0x260` on `main` and `0x258` with this PR. That holds in every stub: 686, 719 and 671 converted TLS accesses respectively. - So on Windows, `main` reads every thread variable of these titles 8 bytes before its address. I did not run the titles (static conversion only). The author found the bug at runtime in Resident Evil 4 (PPSA07411), where a thread read another thread variable. `check_conventions.py --base upstream/main` passes, and `pr_overlap.py` reports no overlap. ### 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-11 07:26:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:26:21Z.


Checked the diff: the thread pointer and the displacement bound now use align_up(p_memsz, p_align), while the block allocation keeps blockSize, so the self-pointer and the stack guard at tp + 0x28 stay inside .gtls. The relinker suite passes locally (95/95, Linux) and the relinker (windows-latest) job, which runs windows_tls_thread_pointer on Windows, is green on this head; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106663378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3290#issuecomment-6106663378). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:26:21Z. --- Checked the diff: the thread pointer and the displacement bound now use `align_up(p_memsz, p_align)`, while the block allocation keeps `blockSize`, so the self-pointer and the stack guard at `tp + 0x28` stay inside `.gtls`. The relinker suite passes locally (95/95, Linux) and the `relinker (windows-latest)` job, which runs `windows_tls_thread_pointer` on Windows, is green on this head; 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!3290
No description provided.