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 from upstream-pr/643 into main 2026-11-25 22:55:51 +00:00
aslan2142 commented 2026-11-24 10:48:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-24T10:48:35Z.


Claimed in the coordination issue as: OLD-7423 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7129211250

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).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5470697958 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/643). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-24T10:48:35Z. --- Claimed in the coordination issue as: OLD-7423 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7129211250 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).
PierreEbele commented 2026-11-25 01:48:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by PierreEbele on 2026-11-25T01:48:42Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7149983700 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/643#issuecomment-7149983700). Originally posted by [PierreEbele](https://github.com/PierreEbele) on 2026-11-25T01:48:42Z. --- 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
aslan2142 commented 2026-11-25 22:55:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T22:55:54Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7176951397 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/643#issuecomment-7176951397). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T22:55:54Z. --- 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).
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
ProjectLeonida/mandy!643
No description provided.