Skip to content

fix(relinker): load libraries searched for unmatched Windows imports - #3970

Closed
MarcZX10 wants to merge 6 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-relinker-windows-load-searched
Closed

MarcZX10 wants to merge 6 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-relinker-windows-load-searched

Conversation

@MarcZX10

Copy link
Copy Markdown
Contributor

What

Carries David's fix for #3369, preserving his original commit author. Windows relinking now retains host libraries in the load list when an unmatched import can be searched in any host library and its module name does not identify a needed library. This fixes the case where a provider such as libSceAppContent.prx was omitted because the import module had a different name.

The change leaves the existing optional diagnostic for a missing, unused dependency available. A follow-up commit restores its string because the skip path still references it.

Tested

  • Linux x86-64, GCC 12.2, Release, relinker-only build: the relinker built and CTest passed 114/114. The Windows-only import-loading scenarios are not exercised on Linux.
  • Locally: tools/check_conventions.py --base upstream/main and git diff --check pass.
  • The issue author reports Windows 11 windows_import_modules and guest_module_identity passed five repetitions, and the relinker-only suite passed 105/105 on Windows. Their title check reports Middle Ages Ping Pong progressed beyond its prior unresolved import and stopped later on a shared-memory limitation; it was not a complete title run.

Checklist

  • Based on current main
  • One topic per PR
  • No new third-party code
  • AI-assisted: yes; original contributor's commit and attribution preserved

Depends on: none

@github-actions github-actions Bot added relinker needs rebase Conflicts with main, rebase needed labels Oct 11, 2026
@TideOPL

TideOPL commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Windows relinker CI fails: relinker (windows-latest, g++) fails in windows_import_modules on this PR:

assert run.returncode == 22 and 'unused.prx' not in run.stderr, (run.returncode, run.stdout, run.stderr)
AssertionError: (3221225781, '', 'Failed to load module: ...\unscoped-native-import\libs\unused.prx
GetLastError: 126 - The specified module could not be found.\n')

The new std::fill(imported.begin() + guestModules.size(), imported.end(), true) marks every host library for loading, including the missing unused.prx that main correctly skips. The PR's own run was Linux-only, so this Windows-only failure didn't show up there.

The condition is also very broad: one unmatched module with no placed library, plus one host import, loads every DT_NEEDED library. Could it be narrowed to the libraries that actually export one of the unmatched NIDs?

AI-assisted: yes. Written with AI assistance (Claude Code); I reviewed it.

@MarcZX10

MarcZX10 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

The previous Windows run exposed that an unscoped import with no declared module could leave an empty entry in UnmatchedImportModules, causing the startup stub to load unrelated native dependencies. I changed the reader to ignore empty declared-module names. Locally, the relinker builds with GCC 12.2 and the windows_import_modules test passes under Linux CTest (1/1). That Linux run does not execute the test Windows-only generated executable case; the new Windows CI run will validate that path.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 11, 2026
@MarcZX10

Copy link
Copy Markdown
Contributor Author

I traced the Windows CI failure to unscoped imports whose guest export names include symbol suffixes (for example, shared#A#B) while the executable import uses the base name (shared). The startup stub now matches guest exports by that base name, so unrelated missing dependencies are not loaded for a guest-resolved import. Linux Release relinker build succeeded, and windows_import_modules passed locally (1/1). The updated Windows CI run is queued and will verify the platform-specific execution.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Follow-up pushed as 2cade45. The Windows failure was caused by marking every native dependency as required for unscoped imports, which made a missing optional unused.prx fatal. Those libraries remain in the symbol search order, but an absent optional dependency can now be skipped. Local Linux release build and windows_import_modules pass; awaiting the Windows CI rerun.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The Windows CI failure was from the skip-SCE-module host import case: an absent native dependency must stay fatal when no guest module provider is known. This follow-up passes the skip-SCE-module mode into the Windows entry stub and keeps that strict check scoped to unplaced unscoped host imports.\n\nValidation: the Linux relinker CTest suite passed 114/114, including windows_import_modules; current Ubuntu and Windows relinker CI jobs and conventions pass. The macOS relinker and Intel context jobs are still queued.

@Dextroid17 Dextroid17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux — the effect is visible in the produced artifact (6 of the 9 new cases emit a different PE), and the Windows job in this PR's own CI is what runs the assertions that catch it.

Head 036aa2dd, merged onto my checkout's upstream/main at 752db7da (145 commits ahead of this PR's own GitHub base 0a8b1d32). The API reports MERGEABLE.

What the change does, measured rather than read

I built relinker twice — once at the head, once with all nine implementation files at upstream/main (everything except the test) — and ran the test's own fixture machinery with a persistent work directory in place of its TemporaryDirectory, then hashed every produced file:

files produced: PR=294   control=294
identical: 288     differing: 6

host-provider-1/output.exe   PR=63ecc442b5c6ca3f   ctl=85629dc2624839c5
host-provider-2/output.exe   PR=08f504f104145b39   ctl=340ca644a785daed
host-provider-3/output.exe   PR=ec1123ceb2396f5c   ctl=bd37f946745a9c31
host-provider-4/output.exe   PR=ed41b85d282806b2   ctl=5b8b77f2c03c0a91
host-provider-7/output.exe   PR=1add0c5088cd8634   ctl=d5570bb4c0452696
host-provider-8/output.exe   PR=75828bb46b6c3fe3   ctl=5fa500c35aba9caa

All six are cases this PR adds (the whole host-provider-0..8 loop is new); the other 288 of the 294 files are byte-identical, which is the regression statement for the surrounding behaviour. Sizes match for every one of the six (275,968 and 310,784 bytes). Besides the stub body, exactly two derived fields move — both by the same 20 bytes:

.entry VirtualSize in the section table   @0x208    control 0x22dd  ->  head 0x22c9
.entry end-address in .startup            @0x30e4c  control 0x452aa ->  head 0x45296

and in the stub body itself, at file offset 269074:

head    : 48 8d 0d 14 69 ff ff | ff 15 a9 d8 fc ff | 48 85 c0   lea rcx,[rip+…]; call [rip+…]; test rax,rax
control : 8b 05 e4 e8 fe ff    | 83 f8 7e          | 0f 84 …    mov eax,[rip+…]; cmp eax,0x7e; je …

So the control's stub guards the load behind cmp eax,0x7e; je … where the head's reaches the call directly — the retained entry changes which path the stub takes. That is a static reading; the runtime difference is exactly what the assertions below test. Note that no library-name string is added to the PE (I checked unused.prx in both ASCII and UTF-16 — absent in both), so an assertion on the name would find nothing here.

What the Linux arms do and do not prove

  • ctest --test-dir build -R '^windows_import_modules$' → head 1/1 Passed (~0.10 s) and control 1/1 Passed — identical, because the new outcome assertions sit inside if os.name == 'nt'. The block's assert result.returncode == 0 calls do run on Linux (the relink still has to succeed), but nothing on Linux asserts the retention itself, so the Linux suite is not coverage for this change.
  • Neighbouring module-resolution tests on the head: guest_module_identity, guest_needed_modules, guest_symbol_names, windows_working_directory → 4/4 Passed.
  • --skip-sce-module is not new: it is already parsed on main (core/relinker/main.cpp), so the test does not depend on the flag arriving with this change.

The coverage is better than the description says — and this PR's own job has already proved it bites

"The Windows-only import-loading scenarios are not exercised on Linux" is true, but it reads as though nothing runs them. The relinker job is not gated the way linux/windows are (those report skipping on a PR); it runs on windows-latest here, with -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, and the file registers windows_import_modules with no OS guard. From this PR's head run (38184891028, job 114605106512):

Start 100: windows_import_modules
100/119 Test #100: windows_import_modules .........................   Passed    1.41 sec

1.41 s against ~0.10 s locally is the produced .exes being executed — the part only Windows can do. And the job has already caught a regression in this very PR: on commit 2cade451 the same test failed on the same runner,

100/119 Test #100: windows_import_modules .........................***Failed    1.30 sec
  File ".../test_windows_import_modules.py", line 455, in main
    assert run.returncode != 0 and 'Failed to load module:' in run.stderr and 'unused.prx' in run.stderr, (index, ...)

which is the new assertion, failing on a real defect rather than by plumbing. That pair of lines is worth more in the description than the current caveat.

One question about intent — bae3a2f8 says "ignore empty unmatched import modules", but only the reader does. That commit touches GuestImageReader.cpp alone (it is the hunk adding && !name.empty()), while RelinkerPipeline.cpp:353 still inserts the declared module unconditionally, so an empty string reaches UnmatchedImportModules whenever a ref has no library and no declared module — which DeclaredImportModule returns by design in both cases (domain/ImportModule.hpp:21-23). On the stub side the set's emptiness is itself a condition (WindowsEntryStubBuilder.cpp:184, unmatched.empty() || …): an empty member cannot flip the already-empty case (that disjunct was already true), but where every real unmatched module is among the searched libraries, the disjunct is false and the any_of at :185 finds no library whose stem is "", so unplacedModule becomes true. I could not reach that state from the suite, and the Windows job is green, so this is a question about intent rather than a defect I can demonstrate: was the pipeline's unconditional insert meant to keep the empty value, or to drop it the way the reader now does?

Not verified, so it is not claimed: the title run (the issue reports a partial one that stopped later on a shared-memory limitation), and Windows execution by me — the Windows evidence here is the CI log above.

Hermes Agent 🤖 at your service

@Dextroid17

Copy link
Copy Markdown
Contributor

One clarification to my review above, having now read the thread. @MarcZX100's account of the earlier Windows failure already names this area — "an unscoped import with no declared module could leave an empty entry in UnmatchedImportModules, causing the startup stub to load unrelated native dependencies". My question is narrower than that and is not answered by it, so here is the delta precisely:

  • bae3a2f8 guards the consumer: GuestImageReader.cpp now computes the declared module only for a non-empty name.
  • The producer is RelinkerPipeline.cpp:353, which still inserts whatever DeclaredImportModule returns — and that returns {} by design when the modules map is empty or the symbol has no # (domain/ImportModule.hpp:21-23).

So an empty member can still reach the set the stub reads at WindowsEntryStubBuilder.cpp:184. If the intent is that no empty member ever arrives there, the pipeline's insert is where I would drop it; if the pipeline is meant to keep it and the stub meant to ignore it, that answers the question and I will leave it.

Hermes Agent 🤖 at your service

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4140 core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp L11, L17
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp L44
core/relinker/main.cpp L151

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing: #3369 is fixed on main by #3365 (the run on main 8b679d8 in the issue loads libSceAppContent.prx), and this adds a second load rule on top of it for --skip-sce-module only. Reopen with a title that still stops on an unmatched import on current main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants