fix(relinker): reject Windows executable imports bound into the TLS template #10947
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10947"
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-25T11:18:32Z.
What
An executable
R_X86_64_64orR_X86_64_GLOB_DATrelocation against a named import whose 8-byte target overlaps thePT_TLSinitialization image converted for Windows: on untouchedc7d6660ef8,relinker --skip-sce-module --windows(with or without--lazy-binding) exited 0 with an empty stderr for targets at the template start, crossing its end and crossing its start, and in the--windowsoutput for the template-start target the.gtlstemplate, which the Windows loader copies into the main thread's TLS block before the entry point and into each later thread's block, held 0 in that slot instead of the original bytes01..08.WindowsRelocationBuilder::Applywrites 0 to the slot and queues the import for the entry stub, which binds only the image copy;WindowsTlsTemplateBuilderthen copies the zeroed bytes into.gtlsand mirrors only base relocations. In ELF, ld.so relocates the image before it copies it into a thread's TLS block, so every thread reads the bound value; the input passesElfReader'sPT_TLSbounds check, the relocation type checks,Apply's import checks and the entry stub'sDT_NEEDEDcheck, and the Windows guest module writer already refuses the same case (Runtime-bound guest TLS template relocation is not supported).WindowsPePatcher::PatchthrowsRuntime-bound executable TLS template relocation is not supportedright afterWindowsRelocationBuilder::Apply, before the sections and the TLS template are built, when an executable import's[r_offset, r_offset + 8)overlaps[p_vaddr, p_vaddr + p_filesz)of aPT_TLSwithp_filesz != 0, and reports the ELF target address; the CLI exits 2 and writes no output. At that pointrelocations.Importsholds only executable imports: guest-module imports are appended later, and the guest writer already refuses those that target a guest's TLS template. Targets in the zero-filled part[p_filesz, p_memsz),RELATIVErelocations (mirrored as base relocations),TPOFF64/DTPOFF64(written statically) and empty-name weak imports (addend written statically) are not affected. The check is not inWindowsRelocationBuilder::Applybecause macOS shares it and handles the case: dyld binds the in-image slot and the TLS descriptor points at the in-image template. A title whosePT_TLSinitialization image holds a named-import relocation now stops converting for Windows instead of producing a build whose guest threads read 0 there; no such title is known: no issue or debt entry reports one, and no issue reports the guest-module refusal of the same case. The gap is recorded indocs/dev/debt/relinker.md: binding would require the entry stub to write the value into both the.gtlstemplate and the main thread's TLS block, which the loader has already allocated.test_executable_tls_relocations.pygets anSTT_FUNCimport and aneeding()helper that addsDT_NEEDED, which the Windows entry stub requires.R_X86_64_64at 0x1800 (template start) and 0x1804 (crossing the end) andGLOB_DATat 0x1800 and 0x17fc (crossing the start) must fail for--windowsand--windows --lazy-bindingwithFAIL: Runtime-bound executable TLS template relocation is not supported (offset <target>)and no output, and still convert for Linux, Linux--lazy-bindingand--macos; the import at 0x1100 (outside the TLS image), at 0x17f8 (ending at the template start) and at 0x1808 (in[p_filesz, p_memsz)) still converts for both Windows modes. On untouched code all four Windows refusal inputs exit 0 with an output file in both modes; the first failing assertion is('template-import-1800-1-1', 0, ''). The block sits before thefailurestable rather than after its loop, ahead of the block #10730 added after that loop.mainf95acfc948; #8076, #9408 and #10647, which the first check could not compare, have merged since. Open pull requests that touch the files of this PR: #6713, #10861 and #11432 (WindowsPePatcher.cpp), #10390 and #11038 (test_executable_tls_relocations.py), and 22 fordocs/dev/debt/relinker.md. Merged ontomaintogether with this change, each of them adds no conflict in these three files beyond the one it already has withmain. #5021, #6713, #10479 and #10861 already conflict withmain, so they were compared by hand. #6713 adds an empty.procparsection after the process-parameter loop, below theApplycall this check follows, and itsWindowsPePatcher.cppchange merges cleanly with this one. Its only conflict withmainis indocs/dev/debt/libc.md, which this PR does not touch, so the conflict is the same with or without this PR. #10861 adds an argument to the laterWindowsTlsBuilder::Buildcall. In the debt file it rewrites thePT_SCE_DYNLIBDATAlayout,sce_modulesyscall scan and code analysis entries and adds one entry after the code analysis entry and one at the end of the file. #10479 inserts after the read-only data entry. #5021 inserts a macOS TLS entry above the--trace-importsentry. This PR inserts between the null-symbol relocation entry and the macOS GS entry. #11432 adds an export directory, and neither it nor #10861 changesrelocations.Imports.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.executable_tls_relocationswith the new cases on untouchedc7d6660ef8fails withAssertionError: ('template-import-1800-1-1', 0, ''); all 20 Windows refusal, Linux, Linux lazy and macOS runs of the four inputs exited 0 with an output file, as did the six Windows control runs. With the change it passes in 2.4 s.ctest -R 'executable_tls_relocations|windows_tls|tls_function_coverage|macos_tls|guest_tls': 40 tests passed in 114.1 s:executable_tls_relocations,windows_tls_zero_fill,windows_tls_protected_entries,windows_tls_thread_pointer,macos_tls_alu,macos_tls_overlap,guest_tlsand the 33tls_function_coverage_*tests.16ff208dc3(#10646 merged) and rebuilt: the 39 tests matchingexecutable_tls_relocations|windows_tls|tls_function_coverage|guest_tls|executable_export_boundspassed again.main(f95acfc948) into the branch without conflicts (ca8e16751c);mainhad also changedtest_executable_tls_relocations.pyanddocs/dev/debt/relinker.md, which merged automatically. Onca8e16751c,ctest -R 'executable_tls_relocations|windows_tls|tls_function_coverage|macos_tls|guest_tls' -j 6: 42 tests passed:executable_tls_relocations(5.8 s),windows_tls_zero_fill,windows_tls_protected_entries,windows_tls_thread_pointer,macos_tls_alu,macos_tls_overlap,guest_tls,guest_tls_export_referencesand the 34tls_function_coverage_*tests.ca8e16751c:ctest --test-dir build-relinker -j 6: 310 tests, 307 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture) andpatch_advisor_unit(path with spaces, which open #10667 fixes); both failed again alone.python tools/check_conventions.py --base upstream/mainwith this title and description passed onca8e16751c.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-25T11:26:43Z.
Review: APPROVED
Reviewed exact head
a54a939. The check is placed after relocation classification and before section/TLS-template construction, where the import list contains executable named imports; it checks overlap of the full 8-byte relocation slot with the initialized PT_TLS range and rejects only Windows output. The fixtures cover both relocation types, both overlap directions, the TLS start/end boundaries, BSS-only TLS, and cross-platform/lazy-binding controls. Local diff and conventions checks passed. The author reports the focused 39-test rerun on current main and the Windows test environment; all hosted Linux, Windows, macOS, x86_64 and ASan relinker checks are green. I did not independently rerun the full relinker suite.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T11:54:15Z.
Review: APPROVED
Reviewed head
a54a939924merged onto maina5a780eaaa(clean merge). Linux x86_64, GCC 13.3, Release, relinker-only build.What I ran:
executable_tls_relocations(this PR's version) with main's relinker: fails at('template-import-1800-1-1', 0, ''), same as your Windows counter-check. With the merged relinker: pass. Main's version of the test passes on both.R_X86_64_64orGLOB_DATimport at 0x1800 converts with--windowsat exit 0, no stderr, and the first 8 bytes of the.gtlstemplate (found via the TLS directory) are00instead of01..08. A control import at 0x1100 keeps0102030405060708. Same with--windows --lazy-binding. On the merge both 0x1800 cases exit 2 withFAIL: Runtime-bound executable TLS template relocation is not supported (offset 0x1800), and the control is unchanged.ctest -R 'executable_tls_relocations|windows_tls|tls_function_coverage|macos_tls|guest_tls|executable_export_bounds'on the merge: 41/41 pass.tools/check_conventions.py --base origin/main: pass.Code read:
TargetRvaandGetRvaare bothuint32_tand get widened before the add, so no wrap. The half-open test leaves out 0x17f8 and 0x1808, and the reported offset is the ELF vaddr.Importsonly holds named executable imports of types 1/6/7 (WindowsRelocationBuilder.cpp:73). RELATIVE, TPOFF64/DTPOFF64 and empty-name weak imports are written statically, so no false positives.WindowsTlsBuilderhas already copied the template (WindowsPePatcher.cpp:109vs:78).WindowsGuestModuleWriter.cpp:38.Small non-blocking note: none of the fixtures use a
JUMP_SLOTin.rela.plt. The check doesn't look at the type, so it's covered anyway.Looks good to me.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:34:51Z.
Review: DISMISSED
Follow-up after manually merging the two PRs for the overlap check: #6713 changes the
nearbyintfdebt note from #10947’s console-observed MXCSR behavior to “uses the host rounding mode, not the console’s,” and changes_Thrd_sleepfrom retrying throughEINTRon Windows/POSIX details to a POSIX cancellation-point description. These are separate entries from this PR’s added TLS-relocation debt note, but the combined file currently has contradictorynearbyintfobservations and duplicate_Thrd_sleepentries with differing behavior. Please coordinate with #6713’s author and resolve/update the debt entries so the merge preserves both the TLS note and one evidence-consistent description for each libc behavior; update the overlap/dependency checklist as needed. I have not found a conflict in the code paths, and this request is limited to the documentation state when the branches are combined.