Skip to content

OLD-7423: restore old upstream PR 7423 - pe_loader: trace delay imports bound to an unimplemented thunk; document delay-load and thread order - #643

Merged
aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
aslan2142:delay-import-thread-order-notes
Nov 25, 2026
Merged

aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
aslan2142:delay-import-thread-order-notes

Conversation

@aslan2142

@aslan2142 aslan2142 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: OLD-7423 #14 (comment)

Restoration of my original pull request, old upstream PR 7423 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 21625bc; one conflict in the runtime/README.md trace-event table, resolved by keeping main's newer xmem_attributes and xmem_physical_pages rows and adding the new row above them, and the old #N references in the commit message are now plain text ("old upstream 761", "old upstream 7231"). No code changes beyond the rebase.

What and why

  • The gap: delay imports are bound eagerly in Loader::bind_imports (runtime/src/pe_loader.cpp), so --dry-run shows the whole import surface. A delay-loaded DLL or function that nothing serves becomes an unimplemented thunk, which returns 0 at its first call. On Windows, the delay-load helper would raise 0xC06D007E or 0xC06D007F there instead, and a title can catch that and take a fallback. That fallback never runs here, and nothing reported it.
  • The trace event: --trace now writes a delay_import_unimplemented event (module, import) for each such import when the image is loaded, before any image code runs. The binding itself is unchanged. Ordinary imports give no such event.
  • docs/runtime_imports.md: a new section on where the loader differs from Windows:
    • eager delay binding, as above;
    • DLL_THREAD_ATTACH reaches TLS callbacks in TLS slot order (DLL_THREAD_DETACH in reverse). A module takes its slot in map_image, before bind_imports loads its dependencies, so the executable's callbacks run before those of the DLLs it imports. Windows does not document its own order.
  • runtime/README.md:
    • the trace-event table lists the new event;
    • the PE loader paragraph now says it applies DIR64 and HIGHLOW relocations and rejects the other types, which matches the relocation code on main.
  • runtime/shims/startup_trace.h: the example trace in the header comment shows the new event.
  • startup_trace_test: a synthetic DLL image with one ordinary import that nothing serves and two delay imports: one missing, one bound to kernel32!GetTickCount. The test expects exactly one delay_import_unimplemented event, naming the missing delay import.
  • Credit: the findings came from cynbun in the old upstream coordination issue.

Tested

Linux, Wine 11.22. startup_trace_test and gta6rt_tls_host.dll were built with clang for the mingw target (x86_64-w64-mingw32). The MSVC /W4 /permissive- build is left to CI.

  • main at 21625bc: 26 checks, 7 failed.
  • main with only this PR's test copied in: 30 checks, 9 failed. The 2 new delay-import checks fail.
  • This branch: 30 checks, 7 failed. All 4 delay-image checks pass, including the 2 that failed on main.
  • The same 7 checks fail on unmodified main too. They come from Wine and mingw limits (an SEH fault inside the vectored handler, OutputDebugStringW, and the access-violation event), not from this change.
  • git diff --check is clean, and python -I scripts/check_no_game_data.py passes on the changed files.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

AI-assisted (Claude Code, Claude Opus 5.5).

…delay-load and thread-attach order

Delay imports are bound eagerly (deliberate: --dry-run shows the whole import surface). A delay-loaded DLL or
function that nothing serves becomes an unimplemented thunk returning 0 at its first call, where Windows'
delay-load helper raises 0xC06D007E / 0xC06D007F, which a title can catch for a fallback. The binding is
unchanged; --trace now writes a delay_import_unimplemented event (module, import) for each such import at load,
so the first boot names them before any image code runs.

docs/runtime_imports.md gets two notes on where the loader differs from Windows: the eager delay binding above,
and that DLL_THREAD_ATTACH reaches TLS callbacks in TLS slot order, which puts the executable before the DLLs it
imports (Windows' order is undocumented). runtime/README.md lists the new trace event and now says the loader
applies DIR64 and HIGHLOW relocations and rejects HIGH, LOW and HIGHADJ (old upstream 761).

startup_trace_test gets a synthetic DLL with one ordinary and two delay imports (one missing, one bound to
kernel32): exactly one delay_import_unimplemented event, naming the missing delay import.

Findings by cynbun in the old upstream coordination issue (old upstream 7231).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142 aslan2142 changed the title OLD-7423: restore ItsNotRexed/Leonida#7423 - pe_loader: trace delay imports bound to an unimplemented thunk; document delay-load and thread order OLD-7423: restore old upstream PR 7423 - pe_loader: trace delay imports bound to an unimplemented thunk; document delay-load and thread order Nov 24, 2026
@PierreEbele

Copy link
Copy Markdown
Contributor

Reviewed 82d6883: read the full diff. check_no_game_data on the touched files is clean. A sandboxed probe rebuilds the new test's synthetic delay image and parses it with pe_inspect.py. On both main and this branch it finds the 3 imports the test asserts. resolve() pushes one ImportRecord per import, so m.imports.back() is the import just bound. The docs match the code: relocation types, TLS-slot thread-event order and eager delay binding. On this head's windows CI job, startup_trace passes. The job's only failure is crt_timed_wait, which #831 has since fixed on main.

One thing to fix: the branch no longer merges into current main. There is a conflict in runtime/shims/startup_trace.h, in the header's example-event comment (the seq renumbering), because #984 (273b3bd) touched the same block. It is a comment-only rebase. No code findings. AI-assisted review.

Recommendation: Fix first

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
runtime/shims/startup_trace.h: main added the tls_index0 example event
and the exception's region/regs fields to the header comment; keep them,
add this branch's delay_import_unimplemented example and renumber seq.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ProjectLeonida#634 rewrote the same runtime/README.md paragraph on the loader. Its text
stays, and its relocation sentence takes this PR's wording: DIR64 and
HIGHLOW relocations, the other types rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 6ef26a6 into ProjectLeonida:main Nov 25, 2026
2 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator Author

Merged: pe_loader traces delay imports bound to an unimplemented thunk; documents delay-load and thread order.

AI-assisted (Claude Code, Claude Opus 5.5).

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants