fix(relinker): require a non-empty PT_TLS for executable TLS relocations #10730

Merged
roboterspinne merged 1 commit from upstream-pr/10730 into main 2026-10-25 15:45:23 +00:00
roboterspinne commented 2026-10-24 19:56:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T19:56:26Z.


What

extractLocal in RelinkerPipeline.cpp accepted an executable's local TPOFF64, DTPOFF64 or DTPMOD64 (symbol 0, or a defined STT_TLS symbol whose value TPOFF64 and DTPOFF64 fold into the addend) whenever a PT_TLS header existed, also one with p_memsz 0. On such an input the default and --lazy-binding Linux outputs relinked with exit 0, kept the relocation symbolless in DT_RELA (for example TPOFF64 at 0x401000 with addend 4) and kept the PT_TLS with p_filesz and p_memsz 0, while --windows and --macos exited 2 with TLS relocation without a non-empty PT_TLS segment from WindowsRelocationBuilder.cpp (Executable DTPMOD64 relocation is not supported for this target for DTPMOD64). glibc gives the main program a TLS module only if (ph->p_memsz > 0) (rtld_setup_main_map in elf/rtld.c), so l_tls_modid, l_tls_align and l_tls_offset stay 0: TPOFF64 goes through CHECK_STATIC_TLS (elf/dl-static-tls.h) to _dl_try_allocate_static_tls, which divides by l_tls_align (elf/dl-reloc.c), and DTPMOD64 stores module ID 0 (sysdeps/x86_64/dl-machine.h); this comes from reading the glibc source, not from running an output.

  • Rule. The pipeline predicate becomes ph.Type == PT_TLS && ph.MemorySize != 0 (renamed hasNonEmptyTlsSegment), the condition under which glibc creates the main program's TLS module and the one WindowsRelocationBuilder.cpp has applied to TPOFF64 and DTPOFF64 since #5262. extractLocal runs before any target code, so every target now fails with TLS relocation in an executable without a non-empty PT_TLS segment at the relocation entry's file offset. Relocations against an imported TLS symbol skip the check as before, and an empty PT_TLS without TLS relocations stays accepted (empty_tls, macos_empty_tls). The builder's own empty-PT_TLS check is no longer reached from the CLI and stays untouched; #10391 edits that file.
  • Tests. test_executable_tls_relocations.py: image() takes a tls_memsz (default TLS_MEMSZ; p_filesz is min(8, tls_memsz)), and a new loop after the failure table relinks TPOFF64 and DTPOFF64 (addend 4) and DTPMOD64 (addend 0), each against symbol 0 and the defined STT_TLS symbol, with p_memsz 0 on the default and --lazy-binding Linux, Windows and macOS outputs: 24 runs plus one Linux run with an imported TPOFF64 that still relinks; the 24 runs each assert exit 2, the identical line FAIL: TLS relocation in an executable without a non-empty PT_TLS segment (offset 0x4700) and no output file. The no-tls-segment expectation follows the new text. The existing cases with the normal PT_TLS are the positive control and still pass. On untouched code the test stops at the changed no-tls-segment expectation; with the old text there it fails at the first new case, linux-empty-tls-18-0, with exit 0.
  • Overlap. pr_overlap.py cannot compare this branch with #8076 (RelinkerPipeline.cpp), because it does not merge with main, and reports no other shared lines. Checked by hand: the hunk of #8076 covers lines 374 to 387 (unwind ranges), this PR changes lines 315 to 316 and 548 to 549.

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, relinked outputs not run.

  • Counter-check on untouched code (2fade7ef4e): the test fails at no-tls-segment with AssertionError: ('no-tls-segment', 2, 'FAIL: TLS relocation in an executable without a PT_TLS segment (offset 0x4700)\nInput: ...'); with that one expectation restored it fails with AssertionError: ('linux-empty-tls-18-0', 0, ''). A probe over the 24 new cases there: the Linux and --lazy-binding outputs exit 0 and write an output for all six relocations, --windows and --macos exit 2 with FAIL: TLS relocation without a non-empty PT_TLS segment (offset 0x1000) for TPOFF64 and DTPOFF64 and FAIL: Executable DTPMOD64 relocation is not supported for this target (offset 0x1000) for DTPMOD64.
  • With this change executable_tls_relocations passes in 3.8 s, and the 44 tests matching tls pass, among them empty_tls, macos_empty_tls, guest_tls and guest_own_tls_relocations.
  • ctest --test-dir build-relinker -j 4: 244 tests, 240 passed, long_paths skipped (LongPathsEnabled is not 1). Three known local failures: select_tests (Windows symlink fixture, FileExistsError, fails on main as well), patch_advisor_unit (build path with spaces, fix open in #10667), and shader_cache_runtime_sources, which timed out at 180 s under parallel load and passed alone in 112 s.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • 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:4998640631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10730). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T19:56:26Z. --- ### What `extractLocal` in `RelinkerPipeline.cpp` accepted an executable's local `TPOFF64`, `DTPOFF64` or `DTPMOD64` (symbol 0, or a defined `STT_TLS` symbol whose value `TPOFF64` and `DTPOFF64` fold into the addend) whenever a `PT_TLS` header existed, also one with `p_memsz` 0. On such an input the default and `--lazy-binding` Linux outputs relinked with exit 0, kept the relocation symbolless in `DT_RELA` (for example `TPOFF64` at 0x401000 with addend 4) and kept the `PT_TLS` with `p_filesz` and `p_memsz` 0, while `--windows` and `--macos` exited 2 with `TLS relocation without a non-empty PT_TLS segment` from `WindowsRelocationBuilder.cpp` (`Executable DTPMOD64 relocation is not supported for this target` for `DTPMOD64`). glibc gives the main program a TLS module only `if (ph->p_memsz > 0)` (`rtld_setup_main_map` in `elf/rtld.c`), so `l_tls_modid`, `l_tls_align` and `l_tls_offset` stay 0: `TPOFF64` goes through `CHECK_STATIC_TLS` (`elf/dl-static-tls.h`) to `_dl_try_allocate_static_tls`, which divides by `l_tls_align` (`elf/dl-reloc.c`), and `DTPMOD64` stores module ID 0 (`sysdeps/x86_64/dl-machine.h`); this comes from reading the glibc source, not from running an output. - **Rule.** The pipeline predicate becomes `ph.Type == PT_TLS && ph.MemorySize != 0` (renamed `hasNonEmptyTlsSegment`), the condition under which glibc creates the main program's TLS module and the one `WindowsRelocationBuilder.cpp` has applied to `TPOFF64` and `DTPOFF64` since #5262. `extractLocal` runs before any target code, so every target now fails with `TLS relocation in an executable without a non-empty PT_TLS segment` at the relocation entry's file offset. Relocations against an imported TLS symbol skip the check as before, and an empty `PT_TLS` without TLS relocations stays accepted (`empty_tls`, `macos_empty_tls`). The builder's own empty-`PT_TLS` check is no longer reached from the CLI and stays untouched; #10391 edits that file. - **Tests.** `test_executable_tls_relocations.py`: `image()` takes a `tls_memsz` (default `TLS_MEMSZ`; `p_filesz` is `min(8, tls_memsz)`), and a new loop after the failure table relinks `TPOFF64` and `DTPOFF64` (addend 4) and `DTPMOD64` (addend 0), each against symbol 0 and the defined `STT_TLS` symbol, with `p_memsz` 0 on the default and `--lazy-binding` Linux, Windows and macOS outputs: 24 runs plus one Linux run with an imported `TPOFF64` that still relinks; the 24 runs each assert exit 2, the identical line `FAIL: TLS relocation in an executable without a non-empty PT_TLS segment (offset 0x4700)` and no output file. The `no-tls-segment` expectation follows the new text. The existing cases with the normal `PT_TLS` are the positive control and still pass. On untouched code the test stops at the changed `no-tls-segment` expectation; with the old text there it fails at the first new case, `linux-empty-tls-18-0`, with exit 0. - **Overlap.** `pr_overlap.py` cannot compare this branch with #8076 (`RelinkerPipeline.cpp`), because it does not merge with `main`, and reports no other shared lines. Checked by hand: the hunk of #8076 covers lines 374 to 387 (unwind ranges), this PR changes lines 315 to 316 and 548 to 549. ### 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, relinked outputs not run. - Counter-check on untouched code (2fade7ef4e): the test fails at `no-tls-segment` with `AssertionError: ('no-tls-segment', 2, 'FAIL: TLS relocation in an executable without a PT_TLS segment (offset 0x4700)\nInput: ...')`; with that one expectation restored it fails with `AssertionError: ('linux-empty-tls-18-0', 0, '')`. A probe over the 24 new cases there: the Linux and `--lazy-binding` outputs exit 0 and write an output for all six relocations, `--windows` and `--macos` exit 2 with `FAIL: TLS relocation without a non-empty PT_TLS segment (offset 0x1000)` for `TPOFF64` and `DTPOFF64` and `FAIL: Executable DTPMOD64 relocation is not supported for this target (offset 0x1000)` for `DTPMOD64`. - With this change `executable_tls_relocations` passes in 3.8 s, and the 44 tests matching `tls` pass, among them `empty_tls`, `macos_empty_tls`, `guest_tls` and `guest_own_tls_relocations`. - `ctest --test-dir build-relinker -j 4`: 244 tests, 240 passed, `long_paths` skipped (`LongPathsEnabled` is not 1). Three known local failures: `select_tests` (Windows symlink fixture, `FileExistsError`, fails on `main` as well), `patch_advisor_unit` (build path with spaces, fix open in #10667), and `shader_cache_runtime_sources`, which timed out at 180 s under parallel load and passed alone in 112 s. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - 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)
MarcZX10 commented 2026-10-24 20:02:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:02:54Z.

Review: APPROVED


Reviewed the executable TLS relocation change and its regression matrix. Requiring nonzero PT_TLS p_memsz closes the Linux-only acceptance gap while leaving imported TLS relocations and empty PT_TLS without local relocations covered as controls. The tests exercise all three relocation kinds, both symbol forms and each target; the hosted Linux, Windows, macOS, x86_64 macOS, ASan, and conventions checks pass on this head. I found no blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639747925 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10730#pullrequestreview-5639747925). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:02:54Z. **Review:** APPROVED --- Reviewed the executable TLS relocation change and its regression matrix. Requiring nonzero PT_TLS p_memsz closes the Linux-only acceptance gap while leaving imported TLS relocations and empty PT_TLS without local relocations covered as controls. The tests exercise all three relocation kinds, both symbol forms and each target; the hosted Linux, Windows, macOS, x86_64 macOS, ASan, and conventions checks pass on this head. I found no blocking issue.
MarcZX10 commented 2026-10-24 20:20:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:20:02Z.

Review: APPROVED


Reviewed the complete diff at 484cb7b539. Executable-local TLS relocations now require PT_TLS with nonzero p_memsz consistently across targets; imported TLS relocations still bypass this executable-local check. The expanded fixture covers TPOFF64, DTPOFF64 and DTPMOD64 with symbol 0 and defined STT_TLS symbols across Linux, lazy-binding Linux, Windows and macOS, plus an imported-symbol control. The reported untouched-main counter-check reproduces Linux accepting empty PT_TLS; hosted Ubuntu, Windows, macOS arm64/x86_64 and ASAN relinker checks passed. Product Linux/Windows jobs were skipped by repository workflow policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639802094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10730#pullrequestreview-5639802094). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:20:02Z. **Review:** APPROVED --- Reviewed the complete diff at 484cb7b539b67248fd89880fdca3d96e8c93a141. Executable-local TLS relocations now require PT_TLS with nonzero p_memsz consistently across targets; imported TLS relocations still bypass this executable-local check. The expanded fixture covers TPOFF64, DTPOFF64 and DTPMOD64 with symbol 0 and defined STT_TLS symbols across Linux, lazy-binding Linux, Windows and macOS, plus an imported-symbol control. The reported untouched-main counter-check reproduces Linux accepting empty PT_TLS; hosted Ubuntu, Windows, macOS arm64/x86_64 and ASAN relinker checks passed. Product Linux/Windows jobs were skipped by repository workflow policy.
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!10730
No description provided.