upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
authorRafa23xz <174075006+Rafa23xz@users.noreply.github.com>2026-11-23 20:51:59 +0100
committerRafa23xz <174075006+Rafa23xz@users.noreply.github.com>2026-11-23 20:51:59 +0100
commit7fc6fa72851362875b7105087030ac651db92b35 (patch)
treec0099a9e15f5006df89c55db5982baf628f47654 /runtime/src/pe_loader.cpp
parent867d922f09b86583859796a174df89555fb32da8 (diff)
runtime: opt-in compat-module directory (LEONIDA_COMPAT_DIR)
When LEONIDA_COMPAT_DIR names a folder, an import whose DLL is a file there binds to that DLL's export. Windows loads it from the full path (LoadLibraryExW, DLL-load-dir + System32 search, never the game folder), so it takes no manual static-TLS slot. The step comes before shims, bundled modules and the host, and the DLL owns all imports of its module: a missing symbol binds to the unimplemented thunk, not a shim. Unset or empty: no change. Measured on the 1.0.2.1 executable: --dry-run goes from 20 unimplemented imports (AcpHal 9, chad 5, xapu 5, XCRDAPI 1) to 0 with the four matching omonkeyz/xbox-emulator crates in the folder (and their TOMBOX_* static initializers turned off; see runtime/README.md). loader_test: five cases with a synthetic OS-loadable DLL (no game data). AI-assisted (Claude Code).
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index e5becb472..318362746 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -944,6 +944,9 @@ struct RuntimeLoadScope {
944} // namespace 944} // namespace
945 945
946Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { 946Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) {
947 std::vector<wchar_t> compat(32768);
948 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.data(), static_cast<DWORD>(compat.size()));
949 if (n > 0 && n < compat.size()) compat_dir_ = std::filesystem::path(compat.data());
947 shims::add_module_lookup(this, &lookup_in_loader); 950 shims::add_module_lookup(this, &lookup_in_loader);
948 shims::add_bundled_loader(this, &kBundledOps); 951 shims::add_bundled_loader(this, &kBundledOps);
949} 952}
@@ -1563,10 +1566,41 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
1563 return reinterpret_cast<void*>(p); 1566 return reinterpret_cast<void*>(p);
1564} 1567}
1565 1568
1569bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal,
1570 bool by_ordinal, void*& out) {
1571 out = nullptr;
1572 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false;
1573 HMODULE h = nullptr;
1574 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) {
1575 h = static_cast<HMODULE>(it->second);
1576 } else {
1577 const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll);
1578 std::error_code ec;
1579 if (!std::filesystem::is_regular_file(candidate, ec)) return false;
1580 // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game
1581 // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL.
1582 h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
1583 if (!h)
1584 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " +
1585 "load it (error " + std::to_string(GetLastError()) + ")");
1586 compat_modules_.emplace(key, h);
1587 std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str());
1588 }
1589 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()));
1590 return true;
1591}
1592
1566void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { 1593void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {
1567 std::string key = lower(dll); 1594 std::string key = lower(dll);
1568 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; 1595 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name;
1569 1596
1597 // Opt-in compat modules come first: a DLL the player put in LEONIDA_COMPAT_DIR owns all of its imports, so one
1598 // console module is never served half by a shim and half by the drop-in (they would not share state).
1599 if (void* p = nullptr; resolve_compat(key, dll, name, ordinal, by_ordinal, p)) {
1600 m.imports.push_back({dll, sym, p ? Binding::Host : Binding::Unimplemented});
1601 return p ? p : shims::make_unimplemented_thunk(dll, sym);
1602 }
1603
1570 if (void* p = shims::find(key, sym)) { 1604 if (void* p = shims::find(key, sym)) {
1571 m.imports.push_back({dll, sym, Binding::Shim}); 1605 m.imports.push_back({dll, sym, Binding::Shim});
1572 return p; 1606 return p;