fix(relinker): bind unresolved Windows data imports eagerly under --lazy-binding #11583
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11583"
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-26T21:22:28Z.
What
With
--windows --lazy-binding,WindowsEntryStubBuilder::Buildgives every unresolved non-weak executable import a lazy failure stub (line 747 onmain), andWindowsPePatcherwrites the stub address into the import's slot, so a data import (R_X86_64_GLOB_DATorR_X86_64_64againstSTT_OBJECTorSTT_NOTYPE) reads stub code as its value. On untouched48c160c58ba fixture whose guest code reads one byte through an unresolvedSTT_OBJECTGLOB_DATslot converts with exit 0 and the program exits 0x48 with empty stderr: the slot at RVA 0x10900 holds 0x435ac4, which is.entryRVA 0x45ac4, the stub'sand rsp,-16(48 83 e4 f0). Without--lazy-bindingthe same input fails at startup withFAIL: unresolved ELF import NoSuchDataXyzand 0xC0000139.JUMP_SLOTimports and imports ofSTT_FUNCsymbols get a lazy stub. A newPeImport::Lazyflag (WindowsRelocationBuilder.cpp:82) is set forJUMP_SLOTand for any relocation whose dynsym entry has typeSTT_FUNC, soGLOB_DATandR_X86_64_64against a function (vtable and function-pointer slots) stay lazy under--lazy-binding, andif (lazyBinding)becomesif (lazyBinding && imports[index].Lazy). The lazy branch keeps its addend code:R_X86_64_64may carry an addend, which the resolved path adds to theGetProcAddressresult before the slot is written (S + A); the unresolved path leaves the stub address in the slot, so such a slot reports the missing import when called. ExecutableJUMP_SLOTandGLOB_DATimports cannot carry an addend (WindowsRelocationBuilder.cpp:71-72), and guest modules reject--lazy-binding(GuestModuleBuilder.cpp:128).PeImport::Function(JUMP_SLOT, orGLOB_DATagainstSTT_FUNC, line 81) is unchanged, because--trace-importsuses it to choose the slots it passes throughTraceImport_nid_no_patch(WindowsEntryStubBuilder.cpp:688,MacOsMachOPatcher.cpp:106); widening it would put trace thunks into vtable slots on Windows and macOS, while the Linux tracer (LinuxElfPatcher.cpp:201-204) wraps onlyGLOB_DATandJUMP_SLOT. A lazy stub reports the missing import only when it is called; a function-pointer slot that is read or compared instead sees the stub address, as aGLOB_DATfunction slot already does. Every other unresolved non-weak import (GLOB_DATorR_X86_64_64against a symbol that is notSTT_FUNC, such asSTT_OBJECTorSTT_NOTYPE) takes the existing eager path of the same loop:FAIL: unresolved ELF import <name>,Searched libraries, then 0xC0000139 before the ELF entry; no new message. The Linux--lazy-bindingoutput keeps these relocations inDT_RELAwithoutDF_BIND_NOW; glibc bindsDT_RELAat startup in either mode (elf/dynamic-link.h,_ELF_DYNAMIC_DO_RELOC: only theDT_JMPRELrange getsranges[1].lazy = (do_lazy)), so on Linux every unresolved non-weak import inDT_RELA, includingGLOB_DATandR_X86_64_64againstSTT_FUNC, fails at startup, whileJUMP_SLOTimports inDT_JMPRELstay lazy; the Windows output defers allSTT_FUNCimports instead, as it already did forGLOB_DAT(pinned by the lazy case intest_windows_import_modules.py). This comes from the glibc source, not from a run on Linux. Weak imports and resolved imports are unchanged.test_windows_lazy_imports.py(from #10945) takes the symbol info, relocation type, code and addend as parameters. Three function cases relink with--windows --lazy-binding --skip-sce-moduleand guest code that calls through the slot:STT_FUNCwithGLOB_DAT(unchanged bytes),STT_FUNCwithR_X86_64_64, andSTT_FUNCwithR_X86_64_64and addend 0x10; on Windows each must reachTransferring control to ELF entry point, then exit 0xC0000139 withFAIL: unresolved ELF import NoSuchFunctionXyzand withoutSearched libraries, and the addend case must contain the addend code (48 ba <addend> 48 01 d0) in the output on every host. Three data cases with guest code that never touches the slot (mov eax,0x2a; ret):STT_OBJECTwithGLOB_DAT,STT_OBJECTwithR_X86_64_64andSTT_NOTYPEwithR_X86_64_64; on Windows each must exit 0xC0000139 withFAIL: unresolved ELF import NoSuchDataXyzand without the transfer line. The resolved path with a nonzero addend is not run, because it needs a provider that exports the symbol; its bytes are the same as those of the eager branch.pr_overlap.pyreports no overlap with open pull requests.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.windows_lazy_importswith the cases of4671d261daon untouched48c160c58bfails at the first data case withAssertionError: object-glob-dat: status 0x2a, stderr '', after the function case passed. Run one by one, all four data cases convert with exit 0 and their programs exit 0x2a withTransferring control to ELF entry pointand empty stderr; reading through the slot exits 0x48 forSTT_OBJECTwithGLOB_DATand withR_X86_64_64. With4671d261dathe four data cases of that version (includingSTT_FUNCwithR_X86_64_64, which48ec6e3572makes lazy again) and both reading programs exit 0xC0000139 with theFAILline andSearched librariesand without the transfer line,STT_FUNCGLOB_DATwithmov eax,0x2a; retstill exits 0x2a after the transfer line, and a weakSTT_OBJECTGLOB_DATread still exits 0xC0000005 on its zero slot, as before.4671d261da,windows_lazy_importspassed in 1.0 s.ctest -R 'windows_|lazy|import|copy_relocations|executable_|needed_libraries|rela_size|entry_stub|cli_args' -j 5: 42 tests passed in 16.2 s, among themwindows_import_modules(12.1 s),windows_fixed_base(4.3 s),executable_export_bounds(4.1 s),executable_tls_relocations(4.1 s),rela_size(2.8 s),windows_trace_imports(2.7 s),needed_libraries(1.7 s),weak_imports(1.0 s) andcopy_relocations(0.4 s).main(c7b4878fa8) into the branch without conflicts (212f1cceda);mainchanged neither file of4671d261dasince48c160c58b. On212f1cceda,ctest -R 'windows_|lazy|import' -j 6: 32 tests passed, among themwindows_lazy_imports(1.1 s),windows_import_modulesandlinux_guest_named_imports.212f1cceda:ctest --test-dir build-relinker -j 6: 311 tests, 308 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.main(8ff4baf534) into the branch without conflicts (2a2c268110) and added48ec6e3572with theLazyflag. Counter-check: the previous version oftest_windows_lazy_imports.pyagainst the new relinker fails withAssertionError: function-64: status 0x2a, stderr ''after the earlier cases passed, sincefunction-64now reaches the ELF entry. On48ec6e3572,ctest -R 'windows_|lazy|import|trace' -j 6: 41 tests passed, among themwindows_lazy_imports(0.9 s),windows_trace_imports(1.6 s),windows_import_modules(9.7 s),weak_importsandwindows_guest_weak_imports. Full suite on48ec6e3572:ctest --test-dir build-relinker -j 6: 369 tests, 366 passed,long_pathsskipped;select_testsandpatch_advisor_unitfailed again, alone as well, as above.python tools/check_conventions.py --base upstream/mainwith this title and description passed on48ec6e3572.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-26T22:13:05Z.
Review: APPROVED
The change limits lazy failure stubs to imports classified as functions by the existing relocation metadata. Removing the addend handling from that branch is consistent with the documented relocation rules, and the new fixture matrix covers object/notype
GLOB_DATandR_X86_64_64while retaining the functionGLOB_DATcall case. The author reports a Windows counter-check on the old and changed revisions, 42 focused tests and the full 311-test suite with two failures also present onmain; the hosted Linux, Windows and macOS relinker matrix and ASan all pass. The hosted full product jobs are skipped by path policy. I did not rerun the suite locally.