fix(relinker): relocate Windows guest TLS from its thread pointer #4739

Merged
Ondra97 merged 1 commit from upstream-pr/4739 into main 2026-10-14 06:33:32 +00:00
Ondra97 commented 2026-10-12 20:17:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T20:17:33Z.


What

A Windows guest module with an 8-byte TLS image aligned to 0, 1 or 8 bytes receives a TPOFF64 displacement of -16 even though its actual thread pointer is 8 bytes after the template. Its TLS getter reads another location instead of the initialized value. Compute TPOFF64 relative to the same thread-pointer offset used by WindowsTlsBuilder; keep the physical block allocation and minimum 16-byte alignment unchanged.

The native TLS regression uses the existing dynamic/executable fixtures. Move those two functions unchanged into guest_import_fixtures.py, imported by both guest_tls and windows_import_modules; retain their old Windows-module re-exports for existing callers. This removes the circular import reported when #3579 imports guest_with_tls and data_directory in the opposite direction. No second fixture implementation or #3579 production change is added here.

Tested

Windows, MinGW-w64 GCC 15.2.0, Python 3.14, Release, relinker-only, main cac4e9688.

  • Own branch: full relinker build, 140/140 CTests pass in 24.92 s. All six TLS alignment/size profiles execute and return 42. The original regression failed on baseline (-16 instead of -8, incorrect native TLS value).
  • Original #4739 a135491d9 plus #3579 67ddb3624: reproduced circular ImportError in 14 of 65 Python modules; each PR alone imports successfully. Fixed combination: all 66 modules import in fresh subprocesses, both entrypoint orders pass, and 17 fixture profiles are byte-identical, including all six TLS getters.
  • Exact combined source on the same main, including #3579's real C++ loader and Python changes: full relinker build passes, 14/14 affected runtime CTests pass in 3.83 s, including guest_tls, windows_import_modules, guest_symbol_names, weak_imports and the ten transitive callers. The five changed/new source files match the actual clean Git merge tree, so this is a combined runtime check rather than an import-only check.
  • The prior generated-PE control comparison found every main executable and the three unaffected guest modules byte-identical to baseline; affected modules change only the TPOFF64 slot. Moving the fixture helpers does not change their bodies or generated input bytes.

The committed conventions check and git diff --check pass. No game or PS5 console was run; Linux/macOS runtime validation was not performed locally. No performance improvement is claimed.

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
  • Unblocks: none
  • AI-assisted: yes. — Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834532217 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4739). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T20:17:33Z. --- ### What A Windows guest module with an 8-byte TLS image aligned to 0, 1 or 8 bytes receives a TPOFF64 displacement of -16 even though its actual thread pointer is 8 bytes after the template. Its TLS getter reads another location instead of the initialized value. Compute TPOFF64 relative to the same thread-pointer offset used by WindowsTlsBuilder; keep the physical block allocation and minimum 16-byte alignment unchanged. The native TLS regression uses the existing dynamic/executable fixtures. Move those two functions unchanged into guest_import_fixtures.py, imported by both guest_tls and windows_import_modules; retain their old Windows-module re-exports for existing callers. This removes the circular import reported when #3579 imports guest_with_tls and data_directory in the opposite direction. No second fixture implementation or #3579 production change is added here. ### Tested Windows, MinGW-w64 GCC 15.2.0, Python 3.14, Release, relinker-only, main cac4e9688. - Own branch: full relinker build, 140/140 CTests pass in 24.92 s. All six TLS alignment/size profiles execute and return 42. The original regression failed on baseline (-16 instead of -8, incorrect native TLS value). - Original #4739 a135491d9 plus #3579 67ddb3624: reproduced circular ImportError in 14 of 65 Python modules; each PR alone imports successfully. Fixed combination: all 66 modules import in fresh subprocesses, both entrypoint orders pass, and 17 fixture profiles are byte-identical, including all six TLS getters. - Exact combined source on the same main, including #3579's real C++ loader and Python changes: full relinker build passes, 14/14 affected runtime CTests pass in 3.83 s, including guest_tls, windows_import_modules, guest_symbol_names, weak_imports and the ten transitive callers. The five changed/new source files match the actual clean Git merge tree, so this is a combined runtime check rather than an import-only check. - The prior generated-PE control comparison found every main executable and the three unaffected guest modules byte-identical to baseline; affected modules change only the TPOFF64 slot. Moving the fixture helpers does not change their bodies or generated input bytes. The committed conventions check and git diff --check pass. No game or PS5 console was run; Linux/macOS runtime validation was not performed locally. No performance improvement is claimed. ### 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 - [x] Unblocks: none - [x] AI-assisted: yes. — Codex
xXPBSXx commented 2026-10-13 04:52:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T04:52:44Z.


Windows test: head a135491d9 merged onto current main e51428bf2 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter ^guest_tls$: guest_tls passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6138508344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4739#issuecomment-6138508344). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T04:52:44Z. --- Windows test: head `a135491d9` merged onto current `main` `e51428bf2` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `^guest_tls$`: guest_tls passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
nicolasfalesy commented 2026-10-13 08:00:59 +00:00 (Migrated from github.com)

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


Heads-up for #3579 and #4739 (posting the same note on both): they merge without a textual conflict, but together they create a circular import between two relinker test modules.

  • #3579 adds from test_guest_tls import data_directory, guest_with_tls to test_windows_import_modules.py.
  • #4739 adds from test_windows_import_modules import dynamic, executable to test_guest_tls.py.

Linux (Arch, Python 3.14, Release), main 7e181fcc3 with both merged: guest_tls, guest_symbol_names, windows_import_modules and weak_imports fail with

ImportError: cannot import name 'dynamic' from 'test_windows_import_modules'

and the tests that import those modules fail the same way (guest_none_relocations, guest_gnu_hash, guest_module_identity, tls_diagnostics, ...). With either PR alone, guest_tls and windows_import_modules pass. Whichever lands second will need to move the shared helpers (for example into a small common test module) instead of importing across.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6141073053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4739#issuecomment-6141073053). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T08:00:59Z. --- Heads-up for #3579 and #4739 (posting the same note on both): they merge without a textual conflict, but together they create a circular import between two relinker test modules. - #3579 adds `from test_guest_tls import data_directory, guest_with_tls` to `test_windows_import_modules.py`. - #4739 adds `from test_windows_import_modules import dynamic, executable` to `test_guest_tls.py`. Linux (Arch, Python 3.14, Release), `main` 7e181fcc3 with both merged: `guest_tls`, `guest_symbol_names`, `windows_import_modules` and `weak_imports` fail with ``` ImportError: cannot import name 'dynamic' from 'test_windows_import_modules' ``` and the tests that import those modules fail the same way (`guest_none_relocations`, `guest_gnu_hash`, `guest_module_identity`, `tls_diagnostics`, ...). With either PR alone, `guest_tls` and `windows_import_modules` pass. Whichever lands second will need to move the shared helpers (for example into a small common test module) instead of importing across.
Ondra97 commented 2026-10-13 09:09:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-13T09:09:22Z.


Thanks for catching this. I reproduced the circular import with #3579 and moved dynamic/executable unchanged into guest_import_fixtures.py, retaining their Windows-module re-exports for existing callers.

#4739 is updated at 723d02c6b. Its full relinker suite passes 140/140 tests. I also built the exact combination with #3579 at 67ddb3624 and ran all 14 affected runtime tests successfully, including guest_tls and windows_import_modules. Both import orders and all 66 Python modules now load successfully.

AI-assisted: yes. — Codex

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142203356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4739#issuecomment-6142203356). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-13T09:09:22Z. --- Thanks for catching this. I reproduced the circular import with #3579 and moved dynamic/executable unchanged into guest_import_fixtures.py, retaining their Windows-module re-exports for existing callers. #4739 is updated at 723d02c6b. Its full relinker suite passes 140/140 tests. I also built the exact combination with #3579 at 67ddb3624 and ran all 14 affected runtime tests successfully, including guest_tls and windows_import_modules. Both import orders and all 66 Python modules now load successfully. AI-assisted: yes. — Codex
github-actions[bot] commented 2026-10-13 16:34:18 +00:00 (Migrated from github.com)

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


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
#3965 core/relinker/relinker/tests/test_windows_import_modules.py L115-121
#4774 core/relinker/relinker/tests/test_windows_import_modules.py L115-121, L190-215
#5406 core/relinker/relinker/tests/test_windows_import_modules.py L190-215
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150335340 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4739#issuecomment-6150335340). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T16:34:18Z. --- <!-- 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 | |---|---|---|---| | #3965 | `core/relinker/relinker/tests/test_windows_import_modules.py` [L115-121](https://github.com/boykopovar/AnyPS5/blob/1e7272196389c30eff9af9e91a51c72d781f46c6/core/relinker/relinker/tests/test_windows_import_modules.py#L115-L121) | | | | #4774 | `core/relinker/relinker/tests/test_windows_import_modules.py` [L115-121](https://github.com/boykopovar/AnyPS5/blob/1e7272196389c30eff9af9e91a51c72d781f46c6/core/relinker/relinker/tests/test_windows_import_modules.py#L115-L121), [L190-215](https://github.com/boykopovar/AnyPS5/blob/1e7272196389c30eff9af9e91a51c72d781f46c6/core/relinker/relinker/tests/test_windows_import_modules.py#L190-L215) | | | | #5406 | `core/relinker/relinker/tests/test_windows_import_modules.py` [L190-215](https://github.com/boykopovar/AnyPS5/blob/1e7272196389c30eff9af9e91a51c72d781f46c6/core/relinker/relinker/tests/test_windows_import_modules.py#L190-L215) | | |
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!4739
No description provided.