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

Closed
opened 2026-11-26 20:18:42 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/643
Author: @aslan2142
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: delay-import-thread-order-notes


📝 Commits (3)

  • 82d6883 pe_loader: trace delay imports bound to an unimplemented thunk; note delay-load and thread-attach order
  • afea666 Merge main into delay-import-thread-order-notes
  • 722fee9 Merge main into delay-import-thread-order-notes (#643)

📊 Changes

5 files changed (+133 additions, -9 deletions)

View changed files

📝 docs/runtime_imports.md (+16 -0)
📝 runtime/README.md (+3 -1)
📝 runtime/shims/startup_trace.h (+5 -4)
📝 runtime/src/pe_loader.cpp (+10 -3)
📝 runtime/test/startup_trace_test.cpp (+99 -1)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/643 **Author:** [@aslan2142](https://github.com/aslan2142) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `delay-import-thread-order-notes` --- ### 📝 Commits (3) - [`82d6883`](https://github.com/ubiquitarianism/leonida/commit/82d6883bed1c18dfb4d409314e7805262aded79e) pe_loader: trace delay imports bound to an unimplemented thunk; note delay-load and thread-attach order - [`afea666`](https://github.com/ubiquitarianism/leonida/commit/afea66650c7eb88823ce9fa9090ae0250bd72530) Merge main into delay-import-thread-order-notes - [`722fee9`](https://github.com/ubiquitarianism/leonida/commit/722fee9af66d61f0f9420f02f580e4b529ab0cbe) Merge main into delay-import-thread-order-notes (#643) ### 📊 Changes **5 files changed** (+133 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `docs/runtime_imports.md` (+16 -0) 📝 `runtime/README.md` (+3 -1) 📝 `runtime/shims/startup_trace.h` (+5 -4) 📝 `runtime/src/pe_loader.cpp` (+10 -3) 📝 `runtime/test/startup_trace_test.cpp` (+99 -1) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:42 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#683
No description provided.