fix(relinker): require a non-empty PT_TLS for executable TLS relocations #10730
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10730"
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-24T19:56:26Z.
What
extractLocalinRelinkerPipeline.cppaccepted an executable's localTPOFF64,DTPOFF64orDTPMOD64(symbol 0, or a definedSTT_TLSsymbol whose valueTPOFF64andDTPOFF64fold into the addend) whenever aPT_TLSheader existed, also one withp_memsz0. On such an input the default and--lazy-bindingLinux outputs relinked with exit 0, kept the relocation symbolless inDT_RELA(for exampleTPOFF64at 0x401000 with addend 4) and kept thePT_TLSwithp_fileszandp_memsz0, while--windowsand--macosexited 2 withTLS relocation without a non-empty PT_TLS segmentfromWindowsRelocationBuilder.cpp(Executable DTPMOD64 relocation is not supported for this targetforDTPMOD64). glibc gives the main program a TLS module onlyif (ph->p_memsz > 0)(rtld_setup_main_mapinelf/rtld.c), sol_tls_modid,l_tls_alignandl_tls_offsetstay 0:TPOFF64goes throughCHECK_STATIC_TLS(elf/dl-static-tls.h) to_dl_try_allocate_static_tls, which divides byl_tls_align(elf/dl-reloc.c), andDTPMOD64stores module ID 0 (sysdeps/x86_64/dl-machine.h); this comes from reading the glibc source, not from running an output.ph.Type == PT_TLS && ph.MemorySize != 0(renamedhasNonEmptyTlsSegment), the condition under which glibc creates the main program's TLS module and the oneWindowsRelocationBuilder.cpphas applied toTPOFF64andDTPOFF64since #5262.extractLocalruns before any target code, so every target now fails withTLS relocation in an executable without a non-empty PT_TLS segmentat the relocation entry's file offset. Relocations against an imported TLS symbol skip the check as before, and an emptyPT_TLSwithout TLS relocations stays accepted (empty_tls,macos_empty_tls). The builder's own empty-PT_TLScheck is no longer reached from the CLI and stays untouched; #10391 edits that file.test_executable_tls_relocations.py:image()takes atls_memsz(defaultTLS_MEMSZ;p_fileszismin(8, tls_memsz)), and a new loop after the failure table relinksTPOFF64andDTPOFF64(addend 4) andDTPMOD64(addend 0), each against symbol 0 and the definedSTT_TLSsymbol, withp_memsz0 on the default and--lazy-bindingLinux, Windows and macOS outputs: 24 runs plus one Linux run with an importedTPOFF64that still relinks; the 24 runs each assert exit 2, the identical lineFAIL: TLS relocation in an executable without a non-empty PT_TLS segment (offset 0x4700)and no output file. Theno-tls-segmentexpectation follows the new text. The existing cases with the normalPT_TLSare the positive control and still pass. On untouched code the test stops at the changedno-tls-segmentexpectation; with the old text there it fails at the first new case,linux-empty-tls-18-0, with exit 0.pr_overlap.pycannot compare this branch with #8076 (RelinkerPipeline.cpp), because it does not merge withmain, 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.2fade7ef4e): the test fails atno-tls-segmentwithAssertionError: ('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 withAssertionError: ('linux-empty-tls-18-0', 0, ''). A probe over the 24 new cases there: the Linux and--lazy-bindingoutputs exit 0 and write an output for all six relocations,--windowsand--macosexit 2 withFAIL: TLS relocation without a non-empty PT_TLS segment (offset 0x1000)forTPOFF64andDTPOFF64andFAIL: Executable DTPMOD64 relocation is not supported for this target (offset 0x1000)forDTPMOD64.executable_tls_relocationspasses in 3.8 s, and the 44 tests matchingtlspass, among themempty_tls,macos_empty_tls,guest_tlsandguest_own_tls_relocations.ctest --test-dir build-relinker -j 4: 244 tests, 240 passed,long_pathsskipped (LongPathsEnabledis not 1). Three known local failures:select_tests(Windows symlink fixture,FileExistsError, fails onmainas well),patch_advisor_unit(build path with spaces, fix open in #10667), andshader_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/mainwith this title and description passed.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-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.
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.