fix(relinker): follow DT_RELR pointers as code roots in code analysis #13916
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13916"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-31T12:38:07Z.
What
#11463 added
DT_RELRand states that "the relative-relocation index also consumes RELR so reachability analysis retains its pointer targets".RelativeRelocationIndexdoes, butCodeInstructionCollector, 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 fromDT_RELAandDT_JMPREL(CodeInstructionCollector.cpp:349-350onb7d3fd3c52; line numbers below refer to it). A function reachable only through a pointer that the input stores as aDT_RELRentry 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 aDT_RELAR_X86_64_RELATIVEtriggers. With the fixture oftest_windows_tls_protected_entries.py, a pointer at 0x308 to 0x214, inside the bytes the rewrite replaces, failed--windowswith(offset 0x214)as aDT_RELAentry and converted with exit 0 as aDT_RELRentry, for the TLS rewrite and for the GS trap alike.DT_RELAandDT_JMPRELrelocations, the collector reads theDT_RELR,DT_RELRSZandDT_RELRENTtags it already parsed from the dynamic segment withReadRelrRelocations, the reader the pipeline andRelativeRelocationIndexuse, and records each slot and implicit addend like anR_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 aDT_RELArelocation on a slot thatDT_RELRalready 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. MalformedDT_RELRmetadata fails with the reader's existing messages, which the pipeline already raises when it reads the same table (RelinkerPipeline.cpp:267).DT_RELRare unchanged. WithDT_RELR, the Windows TLS builder and the macOS image builder now treat aDT_RELRpointer exactly as the same pointer inDT_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 theDT_RELAform (for exampleUnsupported Windows guest TLS instructionwhen they start with anfsprefix). The collector keeps its own tag map, as it already does forDT_RELAand the initializer arrays, instead of sharingRelativeRelocationIndex, whose input is the unused-NID filter's.DT_RELRare not known; the change makes the Windows and macOS code analysis see the same pointers as the conversion itself.pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare and cannot compare #10861, which also changesCodeInstructionCollector.cppbut does not merge withmain. Checked by hand: a trial merge of this branch with #10861 conflicts only in the files thatmainwith it already conflicts in (CodeInstructionCollector.cppanddocs/dev/debt/relinker.md), with the same single conflict block inCodeInstructionCollector.cpp; the new lines sit before the tworeadRelocationscalls because #10861 inserts its flow roots right after them. #11628, #11661, #11689 and #11698 rework the loop oftest_gs_trap_entry_roots.py, so this change adds its case only totest_windows_tls_protected_entries.py, whosemake_imagethe GS test shares; the GS trap path was checked with the sameDT_RELRinput by hand (see Tested).Tested
Windows 11, WinLibs GCC 15.2.0, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), on771dedd5e5(based onb7d3fd3c52;mainatb8f0a6b202changed none of these files and merges cleanly):make_imageintest_windows_tls_protected_entries.pygains arelrroot: the pointer at 0x308 to 0x214 comes from a one-wordDT_RELRtable at 0x780 (DT_RELRSZ8,DT_RELRENT8) instead of aDT_RELAentry, andwindows_tls_protected_entriesexpects it to fail like the other roots, withTLS rewrite would overwrite an executable entry pointat(offset 0x214)and no output. Onb7d3fd3c52the case converts with exit 0; on771dedd5e5it passes, and so do the 55 tests matchingwindows_tls_protected_entries,gs_trap_entry_roots,relr,tls_function,macos_tls,code_extent,unusedandstrict.test_gs_trap_entry_roots.pyconverted for--windowswith exit 0 onb7d3fd3c52and now fails withGS trap would overwrite an executable entry point (offset 0x214), as theDT_RELAform does.ctest --test-dir build-relinker -j 5on771dedd5e5: 543 tests, 537 passed,long_pathsskipped;annotate_module, a Python test oftools/annotate_module.pythat 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_testsand its three partsselect_tests_SelectGraphStateTests,select_tests_SelectBuildTestsandselect_tests_SelectArgumentTestsfail with the Windows symlink fixture'sFileExistsError, as onmainon this machine; this change touches nothing undertools/.python tools/check_conventions.py --base b7d3fd3c52with this title and description passed on771dedd5e5.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T12:39:09Z.
Review: APPROVED
Reviewed — looks good
The exact head
771dedd5e5a1c7b6c34c897a6b199561fd53028dadds decodedDT_RELRslot targets to the code-root and pointer maps used by relinker analysis.What I ran
Reviewed head
771dedd5e5a1c7b6c34c897a6b199561fd53028dagainst base60bdb061356db736928eee431cd8bd4c9ba68036; the merge preview was clean. Environment: Linux x86_64, Python 3.11.2.Results
mainSources
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.