fix(relinker): bind unresolved Windows data imports eagerly under --lazy-binding #11583

Merged
roboterspinne merged 4 commits from upstream-pr/11583 into main 2026-11-02 12:25:44 +00:00
roboterspinne commented 2026-10-26 21:22:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-26T21:22:28Z.


What

With --windows --lazy-binding, WindowsEntryStubBuilder::Build gives every unresolved non-weak executable import a lazy failure stub (line 747 on main), and WindowsPePatcher writes the stub address into the import's slot, so a data import (R_X86_64_GLOB_DAT or R_X86_64_64 against STT_OBJECT or STT_NOTYPE) reads stub code as its value. On untouched 48c160c58b a fixture whose guest code reads one byte through an unresolved STT_OBJECT GLOB_DAT slot converts with exit 0 and the program exits 0x48 with empty stderr: the slot at RVA 0x10900 holds 0x435ac4, which is .entry RVA 0x45ac4, the stub's and rsp,-16 (48 83 e4 f0). Without --lazy-binding the same input fails at startup with FAIL: unresolved ELF import NoSuchDataXyz and 0xC0000139.

  • Rule. Only JUMP_SLOT imports and imports of STT_FUNC symbols get a lazy stub. A new PeImport::Lazy flag (WindowsRelocationBuilder.cpp:82) is set for JUMP_SLOT and for any relocation whose dynsym entry has type STT_FUNC, so GLOB_DAT and R_X86_64_64 against a function (vtable and function-pointer slots) stay lazy under --lazy-binding, and if (lazyBinding) becomes if (lazyBinding && imports[index].Lazy). The lazy branch keeps its addend code: R_X86_64_64 may carry an addend, which the resolved path adds to the GetProcAddress result 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. Executable JUMP_SLOT and GLOB_DAT imports cannot carry an addend (WindowsRelocationBuilder.cpp:71-72), and guest modules reject --lazy-binding (GuestModuleBuilder.cpp:128). PeImport::Function (JUMP_SLOT, or GLOB_DAT against STT_FUNC, line 81) is unchanged, because --trace-imports uses it to choose the slots it passes through TraceImport_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 only GLOB_DAT and JUMP_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 a GLOB_DAT function slot already does. Every other unresolved non-weak import (GLOB_DAT or R_X86_64_64 against a symbol that is not STT_FUNC, such as STT_OBJECT or STT_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-binding output keeps these relocations in DT_RELA without DF_BIND_NOW; glibc binds DT_RELA at startup in either mode (elf/dynamic-link.h, _ELF_DYNAMIC_DO_RELOC: only the DT_JMPREL range gets ranges[1].lazy = (do_lazy)), so on Linux every unresolved non-weak import in DT_RELA, including GLOB_DAT and R_X86_64_64 against STT_FUNC, fails at startup, while JUMP_SLOT imports in DT_JMPREL stay lazy; the Windows output defers all STT_FUNC imports instead, as it already did for GLOB_DAT (pinned by the lazy case in test_windows_import_modules.py). This comes from the glibc source, not from a run on Linux. Weak imports and resolved imports are unchanged.
  • Tests. 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-module and guest code that calls through the slot: STT_FUNC with GLOB_DAT (unchanged bytes), STT_FUNC with R_X86_64_64, and STT_FUNC with R_X86_64_64 and addend 0x10; on Windows each must reach Transferring control to ELF entry point, then exit 0xC0000139 with FAIL: unresolved ELF import NoSuchFunctionXyz and without Searched 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_OBJECT with GLOB_DAT, STT_OBJECT with R_X86_64_64 and STT_NOTYPE with R_X86_64_64; on Windows each must exit 0xC0000139 with FAIL: unresolved ELF import NoSuchDataXyz and 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.
  • Overlap. pr_overlap.py reports 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.

  • Counter-check: windows_lazy_imports with the cases of 4671d261da on untouched 48c160c58b fails at the first data case with AssertionError: 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 with Transferring control to ELF entry point and empty stderr; reading through the slot exits 0x48 for STT_OBJECT with GLOB_DAT and with R_X86_64_64. With 4671d261da the four data cases of that version (including STT_FUNC with R_X86_64_64, which 48ec6e3572 makes lazy again) and both reading programs exit 0xC0000139 with the FAIL line and Searched libraries and without the transfer line, STT_FUNC GLOB_DAT with mov eax,0x2a; ret still exits 0x2a after the transfer line, and a weak STT_OBJECT GLOB_DAT read still exits 0xC0000005 on its zero slot, as before.
  • On 4671d261da, windows_lazy_imports passed 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 them windows_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) and copy_relocations (0.4 s).
  • Merged current main (c7b4878fa8) into the branch without conflicts (212f1cceda); main changed neither file of 4671d261da since 48c160c58b. On 212f1cceda, ctest -R 'windows_|lazy|import' -j 6: 32 tests passed, among them windows_lazy_imports (1.1 s), windows_import_modules and linux_guest_named_imports.
  • Full suite on 212f1cceda: ctest --test-dir build-relinker -j 6: 311 tests, 308 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture) and patch_advisor_unit (path with spaces, which open #10667 fixes); both failed again alone.
  • After oneandonlydean's review: merged current main (8ff4baf534) into the branch without conflicts (2a2c268110) and added 48ec6e3572 with the Lazy flag. Counter-check: the previous version of test_windows_lazy_imports.py against the new relinker fails with AssertionError: function-64: status 0x2a, stderr '' after the earlier cases passed, since function-64 now reaches the ELF entry. On 48ec6e3572, ctest -R 'windows_|lazy|import|trace' -j 6: 41 tests passed, among them windows_lazy_imports (0.9 s), windows_trace_imports (1.6 s), windows_import_modules (9.7 s), weak_imports and windows_guest_weak_imports. Full suite on 48ec6e3572: ctest --test-dir build-relinker -j 6: 369 tests, 366 passed, long_paths skipped; select_tests and patch_advisor_unit failed again, alone as well, as above.
  • python tools/check_conventions.py --base upstream/main with this title and description passed on 48ec6e3572.
  • The relinked Windows programs ran on this host. Not tested on Linux or macOS; CI runs the tests on all three hosts.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5024848372 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11583). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-26T21:22:28Z. --- ### What With `--windows --lazy-binding`, `WindowsEntryStubBuilder::Build` gives every unresolved non-weak executable import a lazy failure stub (line 747 on `main`), and `WindowsPePatcher` writes the stub address into the import's slot, so a data import (`R_X86_64_GLOB_DAT` or `R_X86_64_64` against `STT_OBJECT` or `STT_NOTYPE`) reads stub code as its value. On untouched 48c160c58b a fixture whose guest code reads one byte through an unresolved `STT_OBJECT` `GLOB_DAT` slot converts with exit 0 and the program exits 0x48 with empty stderr: the slot at RVA 0x10900 holds 0x435ac4, which is `.entry` RVA 0x45ac4, the stub's `and rsp,-16` (`48 83 e4 f0`). Without `--lazy-binding` the same input fails at startup with `FAIL: unresolved ELF import NoSuchDataXyz` and 0xC0000139. - **Rule.** Only `JUMP_SLOT` imports and imports of `STT_FUNC` symbols get a lazy stub. A new `PeImport::Lazy` flag (`WindowsRelocationBuilder.cpp:82`) is set for `JUMP_SLOT` and for any relocation whose dynsym entry has type `STT_FUNC`, so `GLOB_DAT` and `R_X86_64_64` against a function (vtable and function-pointer slots) stay lazy under `--lazy-binding`, and `if (lazyBinding)` becomes `if (lazyBinding && imports[index].Lazy)`. The lazy branch keeps its addend code: `R_X86_64_64` may carry an addend, which the resolved path adds to the `GetProcAddress` result 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. Executable `JUMP_SLOT` and `GLOB_DAT` imports cannot carry an addend (`WindowsRelocationBuilder.cpp:71-72`), and guest modules reject `--lazy-binding` (`GuestModuleBuilder.cpp:128`). `PeImport::Function` (`JUMP_SLOT`, or `GLOB_DAT` against `STT_FUNC`, line 81) is unchanged, because `--trace-imports` uses it to choose the slots it passes through `TraceImport_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 only `GLOB_DAT` and `JUMP_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 a `GLOB_DAT` function slot already does. Every other unresolved non-weak import (`GLOB_DAT` or `R_X86_64_64` against a symbol that is not `STT_FUNC`, such as `STT_OBJECT` or `STT_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-binding` output keeps these relocations in `DT_RELA` without `DF_BIND_NOW`; glibc binds `DT_RELA` at startup in either mode (`elf/dynamic-link.h`, `_ELF_DYNAMIC_DO_RELOC`: only the `DT_JMPREL` range gets `ranges[1].lazy = (do_lazy)`), so on Linux every unresolved non-weak import in `DT_RELA`, including `GLOB_DAT` and `R_X86_64_64` against `STT_FUNC`, fails at startup, while `JUMP_SLOT` imports in `DT_JMPREL` stay lazy; the Windows output defers all `STT_FUNC` imports instead, as it already did for `GLOB_DAT` (pinned by the lazy case in `test_windows_import_modules.py`). This comes from the glibc source, not from a run on Linux. Weak imports and resolved imports are unchanged. - **Tests.** `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-module` and guest code that calls through the slot: `STT_FUNC` with `GLOB_DAT` (unchanged bytes), `STT_FUNC` with `R_X86_64_64`, and `STT_FUNC` with `R_X86_64_64` and addend 0x10; on Windows each must reach `Transferring control to ELF entry point`, then exit 0xC0000139 with `FAIL: unresolved ELF import NoSuchFunctionXyz` and without `Searched 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_OBJECT` with `GLOB_DAT`, `STT_OBJECT` with `R_X86_64_64` and `STT_NOTYPE` with `R_X86_64_64`; on Windows each must exit 0xC0000139 with `FAIL: unresolved ELF import NoSuchDataXyz` and 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. - **Overlap.** `pr_overlap.py` reports no overlap with open pull requests. ### 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, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew. - Counter-check: `windows_lazy_imports` with the cases of 4671d261da on untouched 48c160c58b fails at the first data case with `AssertionError: 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 with `Transferring control to ELF entry point` and empty stderr; reading through the slot exits 0x48 for `STT_OBJECT` with `GLOB_DAT` and with `R_X86_64_64`. With 4671d261da the four data cases of that version (including `STT_FUNC` with `R_X86_64_64`, which 48ec6e3572 makes lazy again) and both reading programs exit 0xC0000139 with the `FAIL` line and `Searched libraries` and without the transfer line, `STT_FUNC` `GLOB_DAT` with `mov eax,0x2a; ret` still exits 0x2a after the transfer line, and a weak `STT_OBJECT` `GLOB_DAT` read still exits 0xC0000005 on its zero slot, as before. - On 4671d261da, `windows_lazy_imports` passed 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 them `windows_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) and `copy_relocations` (0.4 s). - Merged current `main` (c7b4878fa8) into the branch without conflicts (212f1cceda); `main` changed neither file of 4671d261da since 48c160c58b. On 212f1cceda, `ctest -R 'windows_|lazy|import' -j 6`: 32 tests passed, among them `windows_lazy_imports` (1.1 s), `windows_import_modules` and `linux_guest_named_imports`. - Full suite on 212f1cceda: `ctest --test-dir build-relinker -j 6`: 311 tests, 308 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture) and `patch_advisor_unit` (path with spaces, which open #10667 fixes); both failed again alone. - After oneandonlydean's review: merged current `main` (8ff4baf534) into the branch without conflicts (2a2c268110) and added 48ec6e3572 with the `Lazy` flag. Counter-check: the previous version of `test_windows_lazy_imports.py` against the new relinker fails with `AssertionError: function-64: status 0x2a, stderr ''` after the earlier cases passed, since `function-64` now reaches the ELF entry. On 48ec6e3572, `ctest -R 'windows_|lazy|import|trace' -j 6`: 41 tests passed, among them `windows_lazy_imports` (0.9 s), `windows_trace_imports` (1.6 s), `windows_import_modules` (9.7 s), `weak_imports` and `windows_guest_weak_imports`. Full suite on 48ec6e3572: `ctest --test-dir build-relinker -j 6`: 369 tests, 366 passed, `long_paths` skipped; `select_tests` and `patch_advisor_unit` failed again, alone as well, as above. - `python tools/check_conventions.py --base upstream/main` with this title and description passed on 48ec6e3572. - The relinked Windows programs ran on this host. Not tested on Linux or macOS; CI runs the tests on all three hosts. ### 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 (Claude Code)
MarcZX10 commented 2026-10-26 22:13:05 +00:00 (Migrated from github.com)

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_DAT and R_X86_64_64 while retaining the function GLOB_DAT call 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 on main; 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5659814765 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11583#pullrequestreview-5659814765). Originally posted by [MarcZX10](https://github.com/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_DAT` and `R_X86_64_64` while retaining the function `GLOB_DAT` call 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 on `main`; 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.
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!11583
No description provided.