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

Manually merged
github merged 3 commits from gh-pr/643/head into gh-pr/643/base 2026-11-26 09:13:33 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-24 10:48 UTC · original
aslan2142:delay-import-thread-order-notes → main · +133 −9 · 5 files · 3 commits · merged 2026-11-25 22:55 UTC

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

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 21625bca8c; 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 21625bca8: 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).

**@⁠aslan2142** on GitHub · 2026-11-24 10:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/643) `aslan2142:delay-import-thread-order-notes` → `main` · +133 −9 · 5 files · 3 commits · merged 2026-11-25 22:55 UTC Claimed in the coordination issue as: OLD-7423 https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 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 21625bca8c675c62f9dbffc318ce6a59d57faf31; 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 21625bca8: 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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 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 <[email protected]>
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 <[email protected]>
#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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:13:32 +00:00
github manually merged commit 722fee9af6 into gh-pr/643/base 2026-11-26 09:13:33 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 01:48 UTC · original

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 (273b3bdf) touched the same block. It is a comment-only rebase. No code findings. AI-assisted review.

Recommendation: Fix first

**@⁠PierreEbele** on GitHub · 2026-11-25 01:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/643#issuecomment-7149983700) 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 (273b3bdf) touched the same block. It is a comment-only rebase. No code findings. AI-assisted review. **Recommendation:** Fix first
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 22:55 UTC · original

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

  • Review point (conflict): main merged in. The runtime/shims/startup_trace.h example-event comment keeps main's tls_index0 event and the exception's region/regs fields, adds this PR's delay_import_unimplemented and renumbers seq (afea66650). It is a comment-only change.
  • Merge with #634: both PRs reword the same runtime/README.md paragraph on the loader. After #634, main was merged in again: #634's paragraph stays, and its relocation sentence takes this PR's wording (IMAGE_REL_BASED_DIR64 and HIGHLOW, rejecting the other types).
  • Tested: the full Windows suite under Wine on main 5fab93967 with all nine of these runtime PRs merged together (#634, #643, #684, #657, #677, #638, #710, #688, #671), on llvmpipe and the gfx1036 iGPU (RADV): no test went from pass to fail. startup_trace and loader pass on both.
  • Checked on main 21d3662ad + 722fee9af: it merges cleanly; the semantic suite passes (493 files, 6,318 tests, 0 files failed); the 15 docs and writer tests pass; check_no_game_data.py is clean on the 5 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 22:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/643#issuecomment-7176951397) Merged: `pe_loader` traces delay imports bound to an unimplemented thunk; documents delay-load and thread order. - **Review point (conflict):** `main` merged in. The `runtime/shims/startup_trace.h` example-event comment keeps `main`'s `tls_index0` event and the exception's `region`/`regs` fields, adds this PR's `delay_import_unimplemented` and renumbers `seq` (`afea66650`). It is a comment-only change. - **Merge with #634:** both PRs reword the same `runtime/README.md` paragraph on the loader. After #634, `main` was merged in again: #634's paragraph stays, and its relocation sentence takes this PR's wording (`IMAGE_REL_BASED_DIR64` and `HIGHLOW`, rejecting the other types). - **Tested:** the full Windows suite under Wine on `main` `5fab93967` with all nine of these runtime PRs merged together (#634, #643, #684, #657, #677, #638, #710, #688, #671), on llvmpipe and the gfx1036 iGPU (RADV): no test went from pass to fail. `startup_trace` and `loader` pass on both. - **Checked on `main` `21d3662ad` + `722fee9af`:** it merges cleanly; the semantic suite passes (493 files, 6,318 tests, 0 files failed); the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 5 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.