fix(relinker): reject non-TLS executable relocations against STT_TLS symbols #11881

Merged
roboterspinne merged 1 commit from upstream-pr/11881 into main 2026-10-28 18:23:46 +00:00
roboterspinne commented 2026-10-27 11:13:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-27T11:13:25Z.


What

extractRela in RelinkerPipeline.cpp checks the symbol type only for R_X86_64_DTPMOD64, DTPOFF64 and TPOFF64 (line 345); a relocation of any other type against an STT_TLS symbol, defined or undefined, is accepted without a symbol-type check (line 406). On untouched 801d5b9bc1 the test_executable_tls_relocations.py fixture with one R_X86_64_64, PC32, GLOB_DAT or 32 relocation against the defined STT_TLS symbol Tls or the undefined STT_TLS symbol ImportedTls converts with exit 0 for Linux and --lazy-binding, and for macOS with R_X86_64_64 and GLOB_DAT; the Linux output carries, for example, GLOB_DAT at 0x401000 against Tls typed STT_TLS. Windows fails on the same inputs only later and for unrelated reasons (ELF imports have no DT_NEEDED libraries, Relocation type 2 is not supported for this target).

  • Rule. The gABI says that symbols of type STT_TLS can be referenced only by special thread-local storage relocations, and that a defined one gives the assigned offset for the symbol, not the actual address, while the x86-64 psABI computes R_X86_64_64, PC32, 32, GLOB_DAT and JUMP_SLOT from S as an address. GuestImageReader.cpp:267 already rejects R_X86_64_64, GLOB_DAT and JUMP_SLOT against STT_TLS in guest modules (Invalid guest symbol relocation). extractRela now throws Non-TLS relocation names an STT_TLS symbol with the relocation entry's offset right after symbolEntryPos (two lines), mirroring the converse check TLS relocation names a defined symbol that is not STT_TLS (line 345); it runs for DT_RELA and DT_JMPREL before every target's writer, so all four targets fail with the same message and write no output. TLS relocations against STT_TLS symbols, imported or defined, are unchanged. This changes one expectation of #10017: test_import_symbol_types.py relinks GLOB_DAT and JUMP_SLOT imports for every type value in range(16) and expected type 6 to relink typed STT_TLS; nothing in #10017 or its review names a title or loader that uses such an import, so type 6 is covered only by the loop, and it now expects exit 2 with the new message and no output while the other 15 types keep their assertion.
  • Tests. test_executable_tls_relocations.py relinks R_X86_64_64, PC32, GLOB_DAT and 32 against Tls and ImportedTls on Linux, --lazy-binding, --windows and --macos (32 cases, symbol_bytes=96 so that macOS also reaches exit 0 on main) and asserts exit 2, FAIL: Non-TLS relocation names an STT_TLS symbol (offset 0x4700) and no output; the same four relocations against the STT_OBJECT symbol Data still convert on Linux. JUMP_SLOT in DT_RELA is not used there because SysVDynamicSectionBuilder.cpp:122 already rejects it on main (JUMP_SLOT relocation is not aligned to the original .rela.plt entry size); the changed type-6 case of test_import_symbol_types.py covers JUMP_SLOT in DT_JMPREL and GLOB_DAT in DT_RELA with global and weak binding.
  • Overlap. pr_overlap.py reports no shared lines with open pull requests that merge with main. It cannot compare this branch with #10861 (RelinkerPipeline.cpp), because that pull request does not merge with main. Checked by hand at #10861's head 8a4fa9adf7: its RelinkerPipeline.cpp hunks are at the includes, after kEtCore and from line 409 of its base on, not at the symbolEntryPos line in extractRela (line 393 on main) where this PR adds the check; a trial merge of this branch with #10861 conflicts only in CodeInstructionCollector.cpp, as main with #10861 does.

Tested

Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.

  • Counter-check: on untouched 801d5b9bc1 executable_tls_relocations fails at the first new case with AssertionError: ('linux-non-tls-1-1', 0, '') and import_symbol_types with AssertionError: (6, 1, 0, ''). Run one by one there, the 32 new cases give exit 0 with an output on Linux and --lazy-binding for all four types and on macOS for R_X86_64_64 and GLOB_DAT, and exit 2 on Windows with ELF imports have no DT_NEEDED libraries or Relocation type 2/10 is not supported for this target and on macOS with the latter; the four type-6 cases of import_symbol_types give exit 0. With the change every one of these cases exits 2 with the new message and no output, and the four Data controls still exit 0.
  • With the change, executable_tls_relocations passed in 14.1 s alone (22.5 s in the focused run under load; 8.7 s for the file of main) and import_symbol_types in 4.1 s alone. ctest -R 'tls|import_symbol|import|executable_' on b6c9e39abb: 66 tests passed, among them guest_tls_export_references, guest_own_tls_relocations, linux_named_imports, windows_named_imports, weak_imports and executable_export_bounds.
  • Full suite: ctest --test-dir build-relinker -j 5 on b6c9e39abb: 369 tests, 366 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture) and patch_advisor_unit (path with spaces, which open #10667 fixes); both failed again alone.
  • python tools/check_conventions.py --base upstream/main with this title and description passed on b6c9e39abb.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5032945633 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11881). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-27T11:13:25Z. --- ### What `extractRela` in `RelinkerPipeline.cpp` checks the symbol type only for `R_X86_64_DTPMOD64`, `DTPOFF64` and `TPOFF64` (line 345); a relocation of any other type against an `STT_TLS` symbol, defined or undefined, is accepted without a symbol-type check (line 406). On untouched 801d5b9bc1 the `test_executable_tls_relocations.py` fixture with one `R_X86_64_64`, `PC32`, `GLOB_DAT` or `32` relocation against the defined `STT_TLS` symbol `Tls` or the undefined `STT_TLS` symbol `ImportedTls` converts with exit 0 for Linux and `--lazy-binding`, and for macOS with `R_X86_64_64` and `GLOB_DAT`; the Linux output carries, for example, `GLOB_DAT` at 0x401000 against `Tls` typed `STT_TLS`. Windows fails on the same inputs only later and for unrelated reasons (`ELF imports have no DT_NEEDED libraries`, `Relocation type 2 is not supported for this target`). - **Rule.** The gABI says that symbols of type `STT_TLS` can be referenced only by special thread-local storage relocations, and that a defined one gives the assigned offset for the symbol, not the actual address, while the x86-64 psABI computes `R_X86_64_64`, `PC32`, `32`, `GLOB_DAT` and `JUMP_SLOT` from `S` as an address. `GuestImageReader.cpp:267` already rejects `R_X86_64_64`, `GLOB_DAT` and `JUMP_SLOT` against `STT_TLS` in guest modules (`Invalid guest symbol relocation`). `extractRela` now throws `Non-TLS relocation names an STT_TLS symbol` with the relocation entry's offset right after `symbolEntryPos` (two lines), mirroring the converse check `TLS relocation names a defined symbol that is not STT_TLS` (line 345); it runs for `DT_RELA` and `DT_JMPREL` before every target's writer, so all four targets fail with the same message and write no output. TLS relocations against `STT_TLS` symbols, imported or defined, are unchanged. This changes one expectation of #10017: `test_import_symbol_types.py` relinks `GLOB_DAT` and `JUMP_SLOT` imports for every type value in `range(16)` and expected type 6 to relink typed `STT_TLS`; nothing in #10017 or its review names a title or loader that uses such an import, so type 6 is covered only by the loop, and it now expects exit 2 with the new message and no output while the other 15 types keep their assertion. - **Tests.** `test_executable_tls_relocations.py` relinks `R_X86_64_64`, `PC32`, `GLOB_DAT` and `32` against `Tls` and `ImportedTls` on Linux, `--lazy-binding`, `--windows` and `--macos` (32 cases, `symbol_bytes=96` so that macOS also reaches exit 0 on `main`) and asserts exit 2, `FAIL: Non-TLS relocation names an STT_TLS symbol (offset 0x4700)` and no output; the same four relocations against the `STT_OBJECT` symbol `Data` still convert on Linux. `JUMP_SLOT` in `DT_RELA` is not used there because `SysVDynamicSectionBuilder.cpp:122` already rejects it on `main` (`JUMP_SLOT relocation is not aligned to the original .rela.plt entry size`); the changed type-6 case of `test_import_symbol_types.py` covers `JUMP_SLOT` in `DT_JMPREL` and `GLOB_DAT` in `DT_RELA` with global and weak binding. - **Overlap.** `pr_overlap.py` reports no shared lines with open pull requests that merge with `main`. It cannot compare this branch with #10861 (`RelinkerPipeline.cpp`), because that pull request does not merge with `main`. Checked by hand at #10861's head 8a4fa9adf7: its `RelinkerPipeline.cpp` hunks are at the includes, after `kEtCore` and from line 409 of its base on, not at the `symbolEntryPos` line in `extractRela` (line 393 on `main`) where this PR adds the check; a trial merge of this branch with #10861 conflicts only in `CodeInstructionCollector.cpp`, as `main` with #10861 does. ### 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, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew. - Counter-check: on untouched 801d5b9bc1 `executable_tls_relocations` fails at the first new case with `AssertionError: ('linux-non-tls-1-1', 0, '')` and `import_symbol_types` with `AssertionError: (6, 1, 0, '')`. Run one by one there, the 32 new cases give exit 0 with an output on Linux and `--lazy-binding` for all four types and on macOS for `R_X86_64_64` and `GLOB_DAT`, and exit 2 on Windows with `ELF imports have no DT_NEEDED libraries` or `Relocation type 2/10 is not supported for this target` and on macOS with the latter; the four type-6 cases of `import_symbol_types` give exit 0. With the change every one of these cases exits 2 with the new message and no output, and the four `Data` controls still exit 0. - With the change, `executable_tls_relocations` passed in 14.1 s alone (22.5 s in the focused run under load; 8.7 s for the file of `main`) and `import_symbol_types` in 4.1 s alone. `ctest -R 'tls|import_symbol|import|executable_'` on b6c9e39abb: 66 tests passed, among them `guest_tls_export_references`, `guest_own_tls_relocations`, `linux_named_imports`, `windows_named_imports`, `weak_imports` and `executable_export_bounds`. - Full suite: `ctest --test-dir build-relinker -j 5` on b6c9e39abb: 369 tests, 366 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture) and `patch_advisor_unit` (path with spaces, which open #10667 fixes); both failed again alone. - `python tools/check_conventions.py --base upstream/main` with this title and description passed on b6c9e39abb. - Not tested on Linux or macOS; CI runs the tests on all three hosts. ### 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 (Claude Code)
rooklz commented 2026-10-27 11:58:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-27T11:58:00Z.

Review: APPROVED


Build-verified on macOS at head b6c9e39abb (ANYPS5_RELINKER_ONLY build, touched suites, full cest 361/361 — also covers the pending macOS CI lanes).

  • Red/green reproduced with the PR's own cases: with only src/ reverted to base 801d5b9bc1, linux-non-tls-1-1 (R_X86_64_64 against the defined STT_TLS symbol) and the STT_TLS import case in test_import_symbol_types.py both convert with exit 0, exactly as the body describes; at head both fail with the new message and no output file.
  • Scope: RelinkerPipeline.cpp holds the only relocation import extraction for both lanes; the check sits after the R_X86_64_NONE/RELATIVE/PLT/TLS continues, so those orderings are untouched, and the read is bounds-safe through symbolEntryPos. Verified isTlsRelocation guards DTPMOD64/DTPOFF64/TPOFF64 so legitimate TLS relocs against STT_TLS symbols still pass (the suite's original cases) and the new non-tls-*-data positive controls confirm ordinary symbols keep converting on every target.
  • Freshness: none of the three touched files changed between 801d5b9bc1 and current main, so the merge is trivial.

Nothing blocking.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5666665512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11881#pullrequestreview-5666665512). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-27T11:58:00Z. **Review:** APPROVED --- Build-verified on macOS at head b6c9e39abb (ANYPS5_RELINKER_ONLY build, touched suites, full cest 361/361 — also covers the pending macOS CI lanes). - Red/green reproduced with the PR's own cases: with only `src/` reverted to base 801d5b9bc1, `linux-non-tls-1-1` (R_X86_64_64 against the defined STT_TLS symbol) and the STT_TLS import case in test_import_symbol_types.py both convert with exit 0, exactly as the body describes; at head both fail with the new message and no output file. - Scope: RelinkerPipeline.cpp holds the only relocation import extraction for both lanes; the check sits after the R_X86_64_NONE/RELATIVE/PLT/TLS continues, so those orderings are untouched, and the read is bounds-safe through symbolEntryPos. Verified `isTlsRelocation` guards DTPMOD64/DTPOFF64/TPOFF64 so legitimate TLS relocs against STT_TLS symbols still pass (the suite's original cases) and the new `non-tls-*-data` positive controls confirm ordinary symbols keep converting on every target. - Freshness: none of the three touched files changed between 801d5b9bc1 and current main, so the merge is trivial. Nothing blocking.
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!11881
No description provided.