fix(relinker): follow DT_RELR pointers as code roots in code analysis #13916

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

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


What

#11463 added DT_RELR and states that "the relative-relocation index also consumes RELR so reachability analysis retains its pointer targets". RelativeRelocationIndex does, but CodeInstructionCollector, which the Windows TLS builder uses to find instructions and executable entry points (WindowsTlsBuilder.cpp:125) and the macOS image builder to find instructions (MacOsImage.cpp:294), still takes code pointers only from DT_RELA and DT_JMPREL (CodeInstructionCollector.cpp:349-350 on b7d3fd3c52; line numbers below refer to it). A function reachable only through a pointer that the input stores as a DT_RELR entry is therefore no entry root. The Windows TLS rewrite and GS trap then patch over it without the refusal (TLS rewrite would overwrite an executable entry point, WindowsTlsBuilder.cpp:149; GS trap would overwrite an executable entry point, line 239) that the same pointer as a DT_RELA R_X86_64_RELATIVE triggers. With the fixture of test_windows_tls_protected_entries.py, a pointer at 0x308 to 0x214, inside the bytes the rewrite replaces, failed --windows with (offset 0x214) as a DT_RELA entry and converted with exit 0 as a DT_RELR entry, for the TLS rewrite and for the GS trap alike.

  • Rule. Before it reads the DT_RELA and DT_JMPREL relocations, the collector reads the DT_RELR, DT_RELRSZ and DT_RELRENT tags it already parsed from the dynamic segment with ReadRelrRelocations, the reader the pipeline and RelativeRelocationIndex use, and records each slot and implicit addend like an R_X86_64_RELATIVE: as a code pointer root and in the slot map that the initializer arrays, the exception tables and the control-flow graph consult (lines 359, 366 and 514). The reader returns each slot once, and a DT_RELA relocation on a slot that DT_RELR already set is refused before code analysis, for the executable by the pipeline (RELR target overlaps RELA relocation, RelinkerPipeline.cpp:387) and for a guest module by the guest image reader (Overlapping guest relocations, GuestImageReader.cpp:278), so the collector needs no conflict check of its own. Malformed DT_RELR metadata fails with the reader's existing messages, which the pipeline already raises when it reads the same table (RelinkerPipeline.cpp:267).
  • Effect. Inputs without DT_RELR are unchanged. With DT_RELR, the Windows TLS builder and the macOS image builder now treat a DT_RELR pointer exactly as the same pointer in DT_RELA: it protects the code it points to as an entry, and a pointer to data inside an executable segment makes the collector decode those bytes as code with the same result as in the DT_RELA form (for example Unsupported Windows guest TLS instruction when they start with an fs prefix). The collector keeps its own tag map, as it already does for DT_RELA and the initializer arrays, instead of sharing RelativeRelocationIndex, whose input is the unused-NID filter's.
  • Real titles with DT_RELR are not known; the change makes the Windows and macOS code analysis see the same pointers as the conversion itself.

pr_overlap.py --branch finds no shared lines with the open pull requests it can compare and cannot compare #10861, which also changes CodeInstructionCollector.cpp but does not merge with main. Checked by hand: a trial merge of this branch with #10861 conflicts only in the files that main with it already conflicts in (CodeInstructionCollector.cpp and docs/dev/debt/relinker.md), with the same single conflict block in CodeInstructionCollector.cpp; the new lines sit before the two readRelocations calls because #10861 inserts its flow roots right after them. #11628, #11661, #11689 and #11698 rework the loop of test_gs_trap_entry_roots.py, so this change adds its case only to test_windows_tls_protected_entries.py, whose make_image the GS test shares; the GS trap path was checked with the same DT_RELR input by hand (see Tested).

Tested

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

  • make_image in test_windows_tls_protected_entries.py gains a relr root: the pointer at 0x308 to 0x214 comes from a one-word DT_RELR table at 0x780 (DT_RELRSZ 8, DT_RELRENT 8) instead of a DT_RELA entry, and windows_tls_protected_entries expects it to fail like the other roots, with TLS rewrite would overwrite an executable entry point at (offset 0x214) and no output. On b7d3fd3c52 the case converts with exit 0; on 771dedd5e5 it passes, and so do the 55 tests matching windows_tls_protected_entries, gs_trap_entry_roots, relr, tls_function, macos_tls, code_extent, unused and strict.
  • The same input with the GS load of test_gs_trap_entry_roots.py converted for --windows with exit 0 on b7d3fd3c52 and now fails with GS trap would overwrite an executable entry point (offset 0x214), as the DT_RELA form does.
  • ctest --test-dir build-relinker -j 5 on 771dedd5e5: 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 771dedd5e5.

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:5097567584 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13916). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-31T12:38:07Z. --- ### What #11463 added `DT_RELR` and states that "the relative-relocation index also consumes RELR so reachability analysis retains its pointer targets". `RelativeRelocationIndex` does, but `CodeInstructionCollector`, which the Windows TLS builder uses to find instructions and executable entry points (`WindowsTlsBuilder.cpp:125`) and the macOS image builder to find instructions (`MacOsImage.cpp:294`), still takes code pointers only from `DT_RELA` and `DT_JMPREL` (`CodeInstructionCollector.cpp:349-350` on b7d3fd3c52; line numbers below refer to it). A function reachable only through a pointer that the input stores as a `DT_RELR` entry is therefore no entry root. The Windows TLS rewrite and GS trap then patch over it without the refusal (`TLS rewrite would overwrite an executable entry point`, `WindowsTlsBuilder.cpp:149`; `GS trap would overwrite an executable entry point`, line 239) that the same pointer as a `DT_RELA` `R_X86_64_RELATIVE` triggers. With the fixture of `test_windows_tls_protected_entries.py`, a pointer at 0x308 to 0x214, inside the bytes the rewrite replaces, failed `--windows` with `(offset 0x214)` as a `DT_RELA` entry and converted with exit 0 as a `DT_RELR` entry, for the TLS rewrite and for the GS trap alike. - **Rule.** Before it reads the `DT_RELA` and `DT_JMPREL` relocations, the collector reads the `DT_RELR`, `DT_RELRSZ` and `DT_RELRENT` tags it already parsed from the dynamic segment with `ReadRelrRelocations`, the reader the pipeline and `RelativeRelocationIndex` use, and records each slot and implicit addend like an `R_X86_64_RELATIVE`: as a code pointer root and in the slot map that the initializer arrays, the exception tables and the control-flow graph consult (lines 359, 366 and 514). The reader returns each slot once, and a `DT_RELA` relocation on a slot that `DT_RELR` already set is refused before code analysis, for the executable by the pipeline (`RELR target overlaps RELA relocation`, `RelinkerPipeline.cpp:387`) and for a guest module by the guest image reader (`Overlapping guest relocations`, `GuestImageReader.cpp:278`), so the collector needs no conflict check of its own. Malformed `DT_RELR` metadata fails with the reader's existing messages, which the pipeline already raises when it reads the same table (`RelinkerPipeline.cpp:267`). - **Effect.** Inputs without `DT_RELR` are unchanged. With `DT_RELR`, the Windows TLS builder and the macOS image builder now treat a `DT_RELR` pointer exactly as the same pointer in `DT_RELA`: it protects the code it points to as an entry, and a pointer to data inside an executable segment makes the collector decode those bytes as code with the same result as in the `DT_RELA` form (for example `Unsupported Windows guest TLS instruction` when they start with an `fs` prefix). The collector keeps its own tag map, as it already does for `DT_RELA` and the initializer arrays, instead of sharing `RelativeRelocationIndex`, whose input is the unused-NID filter's. - Real titles with `DT_RELR` are not known; the change makes the Windows and macOS code analysis see the same pointers as the conversion itself. `pr_overlap.py --branch` finds no shared lines with the open pull requests it can compare and cannot compare #10861, which also changes `CodeInstructionCollector.cpp` but does not merge with `main`. Checked by hand: a trial merge of this branch with #10861 conflicts only in the files that `main` with it already conflicts in (`CodeInstructionCollector.cpp` and `docs/dev/debt/relinker.md`), with the same single conflict block in `CodeInstructionCollector.cpp`; the new lines sit before the two `readRelocations` calls because #10861 inserts its flow roots right after them. #11628, #11661, #11689 and #11698 rework the loop of `test_gs_trap_entry_roots.py`, so this change adds its case only to `test_windows_tls_protected_entries.py`, whose `make_image` the GS test shares; the GS trap path was checked with the same `DT_RELR` input by hand (see Tested). ### 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 771dedd5e5 (based on b7d3fd3c52; `main` at b8f0a6b202 changed none of these files and merges cleanly): - `make_image` in `test_windows_tls_protected_entries.py` gains a `relr` root: the pointer at 0x308 to 0x214 comes from a one-word `DT_RELR` table at 0x780 (`DT_RELRSZ` 8, `DT_RELRENT` 8) instead of a `DT_RELA` entry, and `windows_tls_protected_entries` expects it to fail like the other roots, with `TLS rewrite would overwrite an executable entry point` at `(offset 0x214)` and no output. On b7d3fd3c52 the case converts with exit 0; on 771dedd5e5 it passes, and so do the 55 tests matching `windows_tls_protected_entries`, `gs_trap_entry_roots`, `relr`, `tls_function`, `macos_tls`, `code_extent`, `unused` and `strict`. - The same input with the GS load of `test_gs_trap_entry_roots.py` converted for `--windows` with exit 0 on b7d3fd3c52 and now fails with `GS trap would overwrite an executable entry point (offset 0x214)`, as the `DT_RELA` form does. - `ctest --test-dir build-relinker -j 5` on 771dedd5e5: 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 771dedd5e5. ### 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:09 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

The exact head 771dedd5e5a1c7b6c34c897a6b199561fd53028d adds decoded DT_RELR slot targets to the code-root and pointer maps used by relinker analysis.

What I ran

Reviewed head 771dedd5e5a1c7b6c34c897a6b199561fd53028d against base 60bdb061356db736928eee431cd8bd4c9ba68036; the merge preview was clean. Environment: Linux x86_64, Python 3.11.2.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13916

Results

Check Result
Diff, conventions, test registration, mergeability Passed
Test-quality heuristic Could not run because that local-only helper is absent from the PR checkout
Exact-head build and focused tests Author reports a Windows relinker-only build and 55 focused tests passed
Full CTest suite Author reports 537/543 passed, one skip, and six unrelated timeout/symlink-fixture failures also seen on main
Local build/tests Not run

Sources

Not verified

I did not independently build or run the relinker, verify the manually checked GS-trap case, or wait for the in-progress hosted relinker matrix.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5722736825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13916#pullrequestreview-5722736825). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T12:39:09Z. **Review:** APPROVED --- # Reviewed — looks good The exact head `771dedd5e5a1c7b6c34c897a6b199561fd53028d` adds decoded `DT_RELR` slot targets to the code-root and pointer maps used by relinker analysis. ## What I ran Reviewed head `771dedd5e5a1c7b6c34c897a6b199561fd53028d` against base `60bdb061356db736928eee431cd8bd4c9ba68036`; the merge preview was clean. Environment: Linux x86_64, Python 3.11.2. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13916 ``` ## Results | Check | Result | | --- | --- | | Diff, conventions, test registration, mergeability | Passed | | Test-quality heuristic | Could not run because that local-only helper is absent from the PR checkout | | Exact-head build and focused tests | Author reports a Windows relinker-only build and 55 focused tests passed | | Full CTest suite | Author reports 537/543 passed, one skip, and six unrelated timeout/symlink-fixture failures also seen on `main` | | Local build/tests | Not run | ## Sources - [RELR slots added to the pointer map and code roots at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/771dedd5e5a1c7b6c34c897a6b199561fd53028d/core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp#L350-L355) - [Regression case for the TLS rewrite root](https://github.com/boykopovar/AnyPS5/blob/771dedd5e5a1c7b6c34c897a6b199561fd53028d/core/relinker/relinker/tests/test_windows_tls_protected_entries.py#L34-L80) ## Not verified I did not independently build or run the relinker, verify the manually checked GS-trap case, or wait for the in-progress hosted relinker matrix.
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!13916
No description provided.