From 1e73617660c9d8df08d6c7df2ba5a9a5774c0213 Mon Sep 17 00:00:00 2001 From: omardev29 <154835571+omardev29@users.noreply.github.com> Date: Sun, 22 Nov 2026 05:27:51 +0100 Subject: Loader: redirect RtlPcToFileHeader in the host MSVC runtime so C++ exceptions work _CxxThrowException (vcruntime140.dll) stores the throwing image's base in the exception record, from RtlPcToFileHeader(pThrowInfo), and the frame handler adds the throw's RVAs to it. Windows only knows images its own loader mapped, so for a mapped image the base is 0, no catch matches, and the process ends with 0xE06D7363: tiny_game's LEONIDA_RUN_EH=1 check on main. When the loader binds a host copy of the MSVC runtime (prefer_host_copy, direct or through a forwarder), it points that DLL's imports of RtlPcToFileHeader by name at a version that asks Windows first and then the live Loaders' mapped modules. vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll in System32 import it (llvm-readobj --coff-imports); the package's FidelityFX upscaler and XboxBvhBuilder.dll import _CxxThrowException from vcruntime140.dll. The redirect is done once per DLL, before any mapped code runs, and stays for the process lifetime; with no live Loader it answers as Windows does. --- runtime/src/pe_loader.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f4579ec27..8865e6ddc 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -266,6 +267,59 @@ bool prefer_host_copy(const std::string& dll) { return false; } +// MSVC C++ exceptions in a mapped image. _CxxThrowException (vcruntime140.dll) puts the throwing image's base in the +// exception record, from RtlPcToFileHeader(pThrowInfo); the frame handler adds the ThrowInfo's RVAs (catchable types, +// copy constructor, destructor) to it. RtlPcToFileHeader only knows images the Windows loader mapped, so for ours the +// base is 0 and the exception can't be matched to a catch. The host copies the loader binds get their import of it +// redirected: the Windows answer first, then the modules a live Loader has mapped (shims::mapped_image_base). +using PcToFileHeaderFn = PVOID(NTAPI*)(PVOID, PVOID*); +std::atomic g_host_pc_to_file_header{nullptr}; + +PVOID NTAPI pc_to_file_header(PVOID pc, PVOID* base) { + if (PVOID found = g_host_pc_to_file_header.load()(pc, base)) return found; + uint64_t mapped = 0; + if (!shims::mapped_image_base(reinterpret_cast(pc), mapped)) { + *base = nullptr; + return nullptr; + } + *base = reinterpret_cast(mapped); + return *base; +} + +// Points every import of RtlPcToFileHeader by name in a host module at pc_to_file_header, once per module. The +// module is a system DLL the Windows loader has already validated and bound, so its tables are read unchecked. +// The redirect stays for the process lifetime; with no live Loader it answers exactly as Windows does. +void redirect_pc_to_file_header(HMODULE host) { + static std::mutex mutex; + static std::vector done; + std::lock_guard lock(mutex); + if (std::find(done.begin(), done.end(), host) != done.end()) return; + done.push_back(host); + + auto* base = reinterpret_cast(host); + auto* nt = at(base, reinterpret_cast(base)->e_lfanew); + const auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; + if (!dir.Size || !dir.VirtualAddress) return; + for (auto* d = at(base, dir.VirtualAddress); d->Name; ++d) { + if (!d->OriginalFirstThunk) continue; // no name table to match against + auto* names = at(base, d->OriginalFirstThunk); + auto* iat = at(base, d->FirstThunk); + for (; names->u1.AddressOfData; ++names, ++iat) { + if (IMAGE_SNAP_BY_ORDINAL64(names->u1.Ordinal)) continue; + auto* by_name = at(base, static_cast(names->u1.AddressOfData)); + if (std::strcmp(reinterpret_cast(by_name->Name), "RtlPcToFileHeader") != 0) continue; + auto* slot = reinterpret_cast(&iat->u1.Function); + if (*slot == reinterpret_cast(&pc_to_file_header)) continue; + PcToFileHeaderFn expected = nullptr; // the first bound address is the Windows function + g_host_pc_to_file_header.compare_exchange_strong(expected, reinterpret_cast(*slot)); + DWORD old = 0; + if (!VirtualProtect(slot, sizeof(void*), PAGE_READWRITE, &old)) continue; + *slot = reinterpret_cast(&pc_to_file_header); + VirtualProtect(slot, sizeof(void*), old, &old); + } + } +} + } // namespace namespace { @@ -653,6 +707,7 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); if (!h) return nullptr; + if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast(ordinal)) : symbol.c_str()); if (!p) return nullptr; binding = Binding::Host; @@ -685,6 +740,7 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name } // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { + if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); if (p) { m.imports.push_back({dll, sym, Binding::Host}); -- cgit v1.2.3