fix(relinker): require a non-empty PT_TLS for guest module TLS relocations #13915

Merged
roboterspinne merged 2 commits from upstream-pr/13915 into main 2026-11-01 09:40:41 +00:00
roboterspinne commented 2026-10-31 12:37:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-31T12:37:49Z.


What

#10730 made executable TLS relocations require a PT_TLS with a nonzero p_memsz; glibc gives an object a TLS module only for such a PT_TLS (rtld_setup_main_map in elf/rtld.c for the main program, _dl_map_object_from_fd in elf/dl-load.c, which skips an empty PT_TLS, for loaded objects). Guest modules still use the old condition. On b7d3fd3c52 (line numbers below refer to it) the guest image reader accepts relocations that name the module's own TLS block whenever a PT_TLS header exists, also one with p_memsz 0: a DTPMOD64, DTPOFF64 or TPOFF64 against symbol 0 and a DTPMOD64 or DTPOFF64 against the zero-value section symbol of ownTlsBlock need only tls != nullptr (GuestImageReader.cpp:300-301), and one against an STT_TLS symbol the module defines needs only the symbol-table bound of line 211 (TLS symbol exceeds TLS segment), which a symbol with value 0 and size 0 passes when p_memsz is 0. What happens next depends on the target and the type: for a DTPMOD64 or DTPOFF64 against the section symbol of a module, plain or SCE-tagged, whose PT_TLS has p_filesz and p_memsz 0, Linux output kept both relocations with exit 0; --windows refused the DTPMOD64 with Guest TLS relocation has no TLS block (WindowsGuestModuleWriter.cpp:94) and --macos with Guest TLS module relocation without a non-empty PT_TLS (MacOsImage.cpp:456), while both converted the DTPOFF64 with exit 0.

  • Rule. The reader computes tls != nullptr && tls->MemorySize != 0 where it checks a TLS relocation, uses it in place of tls != nullptr for symbol 0 and the section symbol, and also requires it for a relocation against an STT_TLS symbol with a nonzero section index, as #10730 does for executables with symbol 0 and defined STT_TLS symbols. These relocations then fail as Invalid guest TLS relocation, the message the reader already gives a symbol-0 TLS relocation in a module without PT_TLS, on every target and before any output. Relocations against an undefined STT_TLS symbol (an import from another module), modules with a nonempty PT_TLS and modules without TLS relocations are unchanged. The predicate is computed at the point of use, not with the program header loop, so the header loop and its PT_TLS checks stay as they are.

pr_overlap.py --branch reports no overlap with open pull requests.

Tested

Windows 11, WinLibs GCC 15.2.0, relinker-only build (-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), on 015778c83e (based on b7d3fd3c52; main at b8f0a6b202 changed none of these files and merges cleanly):

  • guest_own_tls_relocations gains cases with a PT_TLS of p_filesz and p_memsz 0 for a plain and an SCE-tagged module: DTPMOD64 and DTPOFF64 against the section symbol, and DTPMOD64, DTPOFF64 and TPOFF64 against symbol 0 and against a local STT_TLS symbol defined in section 1, must fail with Invalid guest TLS relocation and write neither the module nor the output, for Linux, --windows and --macos; the same three relocations against the defined symbol with the fixture's nonempty PT_TLS must still convert on all three targets. On b7d3fd3c52 the test fails at the first case (exit 0 on Linux), and with only the first commit of this branch (94c6552030) at the first defined-symbol case (exit 0 on Linux); on 015778c83e it passes, and so do the 48 tests matching tls.
  • ctest --test-dir build-relinker -j 5 on 015778c83e: 543 tests, 537 passed, long_paths skipped; annotate_module, a Python test of tools/annotate_module.py that does not run the relinker, timed out under the parallel load and passes alone in about 15.5 s (it sits close to its 20 s limit on this machine); select_tests and its three parts select_tests_SelectGraphStateTests, select_tests_SelectBuildTests and select_tests_SelectArgumentTests fail with the Windows symlink fixture's FileExistsError, as on main on this machine; this change touches nothing under tools/.
  • python tools/check_conventions.py --base b7d3fd3c52 with this title and description passed on 015778c83e.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5097565702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13915). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-31T12:37:49Z. --- ### What #10730 made executable TLS relocations require a `PT_TLS` with a nonzero `p_memsz`; glibc gives an object a TLS module only for such a `PT_TLS` (`rtld_setup_main_map` in `elf/rtld.c` for the main program, `_dl_map_object_from_fd` in `elf/dl-load.c`, which skips an empty `PT_TLS`, for loaded objects). Guest modules still use the old condition. On b7d3fd3c52 (line numbers below refer to it) the guest image reader accepts relocations that name the module's own TLS block whenever a `PT_TLS` header exists, also one with `p_memsz` 0: a `DTPMOD64`, `DTPOFF64` or `TPOFF64` against symbol 0 and a `DTPMOD64` or `DTPOFF64` against the zero-value section symbol of `ownTlsBlock` need only `tls != nullptr` (`GuestImageReader.cpp:300-301`), and one against an `STT_TLS` symbol the module defines needs only the symbol-table bound of line 211 (`TLS symbol exceeds TLS segment`), which a symbol with value 0 and size 0 passes when `p_memsz` is 0. What happens next depends on the target and the type: for a `DTPMOD64` or `DTPOFF64` against the section symbol of a module, plain or SCE-tagged, whose `PT_TLS` has `p_filesz` and `p_memsz` 0, Linux output kept both relocations with exit 0; `--windows` refused the `DTPMOD64` with `Guest TLS relocation has no TLS block` (`WindowsGuestModuleWriter.cpp:94`) and `--macos` with `Guest TLS module relocation without a non-empty PT_TLS` (`MacOsImage.cpp:456`), while both converted the `DTPOFF64` with exit 0. - **Rule.** The reader computes `tls != nullptr && tls->MemorySize != 0` where it checks a TLS relocation, uses it in place of `tls != nullptr` for symbol 0 and the section symbol, and also requires it for a relocation against an `STT_TLS` symbol with a nonzero section index, as #10730 does for executables with symbol 0 and defined `STT_TLS` symbols. These relocations then fail as `Invalid guest TLS relocation`, the message the reader already gives a symbol-0 TLS relocation in a module without `PT_TLS`, on every target and before any output. Relocations against an undefined `STT_TLS` symbol (an import from another module), modules with a nonempty `PT_TLS` and modules without TLS relocations are unchanged. The predicate is computed at the point of use, not with the program header loop, so the header loop and its `PT_TLS` checks stay as they are. `pr_overlap.py --branch` reports no overlap with open pull requests. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2.0, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON`, Ninja, Release), on 015778c83e (based on b7d3fd3c52; `main` at b8f0a6b202 changed none of these files and merges cleanly): - `guest_own_tls_relocations` gains cases with a `PT_TLS` of `p_filesz` and `p_memsz` 0 for a plain and an SCE-tagged module: `DTPMOD64` and `DTPOFF64` against the section symbol, and `DTPMOD64`, `DTPOFF64` and `TPOFF64` against symbol 0 and against a local `STT_TLS` symbol defined in section 1, must fail with `Invalid guest TLS relocation` and write neither the module nor the output, for Linux, `--windows` and `--macos`; the same three relocations against the defined symbol with the fixture's nonempty `PT_TLS` must still convert on all three targets. On b7d3fd3c52 the test fails at the first case (exit 0 on Linux), and with only the first commit of this branch (94c6552030) at the first defined-symbol case (exit 0 on Linux); on 015778c83e it passes, and so do the 48 tests matching `tls`. - `ctest --test-dir build-relinker -j 5` on 015778c83e: 543 tests, 537 passed, `long_paths` skipped; `annotate_module`, a Python test of `tools/annotate_module.py` that does not run the relinker, timed out under the parallel load and passes alone in about 15.5 s (it sits close to its 20 s limit on this machine); `select_tests` and its three parts `select_tests_SelectGraphStateTests`, `select_tests_SelectBuildTests` and `select_tests_SelectArgumentTests` fail with the Windows symlink fixture's `FileExistsError`, as on `main` on this machine; this change touches nothing under `tools/`. - `python tools/check_conventions.py --base b7d3fd3c52` with this title and description passed on 015778c83e. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
MarcZX10 commented 2026-10-31 12:39:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T12:39:40Z.

Review: APPROVED


Reviewed — looks good

The exact head 015778c83ef889b64980ddc96f621a768dc20350 now rejects guest-module TLS relocations when the module has no non-empty PT_TLS, while retaining the imported-symbol path.

I ran python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13915 against base 60bdb061356db736928eee431cd8bd4c9ba68036: diff, conventions, Python syntax, test registration, and mergeability passed. The author reports an exact-head Windows relinker-only build, 48 TLS-focused tests, and a 543-test CTest run with 537 passing, one skipped, and six unrelated failures also present on main. I did not build or run tests locally; hosted relinker jobs were still in progress at the latest refresh.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5722739188 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13915#pullrequestreview-5722739188). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T12:39:40Z. **Review:** APPROVED --- # Reviewed — looks good The exact head `015778c83ef889b64980ddc96f621a768dc20350` now rejects guest-module TLS relocations when the module has no non-empty `PT_TLS`, while retaining the imported-symbol path. I ran `python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13915` against base `60bdb061356db736928eee431cd8bd4c9ba68036`: diff, conventions, Python syntax, test registration, and mergeability passed. The author reports an exact-head Windows relinker-only build, 48 TLS-focused tests, and a 543-test CTest run with 537 passing, one skipped, and six unrelated failures also present on `main`. I did not build or run tests locally; hosted relinker jobs were still in progress at the latest refresh. - [TLS block validation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/015778c83ef889b64980ddc96f621a768dc20350/core/relinker/relinker/src/guest/GuestImageReader.cpp#L295-L303) - [Empty TLS cases and non-empty control cases](https://github.com/boykopovar/AnyPS5/blob/015778c83ef889b64980ddc96f621a768dc20350/core/relinker/relinker/tests/test_guest_own_tls_relocations.py#L74-L88)
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!13915
No description provided.