Skip to content

loader: follow forwarded exports and validate delay-import descriptors - #122

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Ayinaki:runtime/loader-forwarders
Nov 21, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Ayinaki:runtime/loader-forwarders

Conversation

@Ayinaki

@Ayinaki Ayinaki commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What and why

parse_exports stored base + RVA for every export. An export whose RVA lands inside the export directory is not code: it is a forwarder string such as KERNEL32.Sleep, and an import of one was given a pointer into that string. The function ended with a comment saying forwarders were not followed yet. This follows them.

Resolution order per forwarder: a bundled module in the game folder, named with or without .dll in the string, then LoadLibraryEx with LOAD_LIBRARY_SEARCH_SYSTEM32 and GetProcAddress, which follows host forwarders itself. A chain deeper than eight hops is treated as a loop and stops. A forwarder that resolves to nothing records the import as unimplemented and binds the usual failure thunk, so --dry-run still reports it.

The delay-import walk assumed two things no image guarantees. A descriptor without dlattrRva stores 32-bit virtual addresses, which cannot address an x64 image base, so it is now rejected with a message instead of being read as RVAs. A descriptor with a zero ImportAddressTableRVA is rejected instead of writing through the image base. A zero ImportNameTableRVA is legal and now reads its entries back through the address table.

This touches pe_loader.cpp, pe_loader.h and loader_test.cpp, which open PR #93 also edits for static TLS and unwind. Expect a conflict there if both land, in regions away from this change. The branch is rebased onto main at 4c75ce5, so the merged #95 TLS and DllMain tests are present and still pass.

Tested

  • cmake --build D:\Leonida-work\cbuild2 --config Release --clean-first, MSVC: builds clean.
  • loader_test: 37 checks, 0 failed. Six of them are from the merged TLS/DllMain work; five are new here: a forwarder that binds through to kernel32!GetCurrentProcessId, a forwarder to a module that isn't there binding as unimplemented, two modules forwarding to each other stopping, a descriptor without the RVA attribute being rejected, and a delay import with no name table binding through its address table.
  • ctest -C Release: 7 of 7 pass.
  • Every fixture is built byte by byte inside the test. No game files are read.
  • Not run: an image that really forwards exports. The design notes record that none of the 16 bundled DLLs do, so this path is covered by synthetic fixtures only.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • N/A: no translator rule changed, so nothing to add to the contract table in docs/design.md.

@Ayinaki

Ayinaki commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

The two red jobs are the repo-wide numpy failure, not this change. On main at d51bddc they fail the same way: translator-tests dies collecting tests/test_amd_swizzle.py (which imports amd_swizzle -> numpy at module scope), and check ends with ModuleNotFoundError: No module named 'numpy' from the same import chain. Both semantic-tests and build pass here. #118 fixes both halves; #100 covers only the tex_headers half. This PR touches neither file.

parse_exports stored base + RVA for every export, but an export whose RVA lands inside the export
directory is a forwarder string ("KERNEL32.Sleep"), not code, so an import of one received a pointer
into the string. Forwarders are now recorded separately and followed when an import asks for one:
bundled modules first, then LoadLibraryEx/GetProcAddress for system DLLs, with a depth cap so two
modules re-exporting each other stop instead of recursing. An unresolvable forwarder records the
import as unimplemented.

The delay-import walk also assumed every descriptor is RVA-based and has a name table. A descriptor
without dlattrRva stores 32-bit virtual addresses, which cannot address an x64 image base, so it is
rejected with a message instead of being misread. A missing address table is rejected, and a missing
name table now reads its entries back through the address table.
@Ayinaki
Ayinaki force-pushed the runtime/loader-forwarders branch from d6f6e69 to ff573ee Compare November 21, 2026 19:57
@Ayinaki

Ayinaki commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 4c75ce5, which carries the merged #95 and #118. The numpy failure is gone: all five checks pass now (build, check, translator-tests, both semantic-tests), and loader_test is 37 checks / 0 failed with the TLS and DllMain tests alongside the new ones.

@dea7hsmiless

Copy link
Copy Markdown
Contributor

Delay-import follow-up from the review, for the record.

I scanned the installed x64 C:\Windows\System32 tree with a stdlib PE32+ parser: 4,620 DLLs, 17,558 delay-import descriptors.

  • descriptors with ImportNameTableRVA == 0: 0
  • descriptors without dlattrRva: 0

So rejecting a descriptor without dlattrRva matches every real Windows binary I can see. The zero-INT fallback added here is also not exercised by any of them. If it was added for a specific image, it would help to record which one and what its IAT contains; otherwise a clear error would avoid reading an IAT that in MSVC output holds thunk VAs, not name RVAs. No action needed for this PR; just recording the measurement.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants