upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/library_loader.cpp
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:11:36 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 15:11:36 -0500
commit5a7507d508bdf03dd8cefec95809927919e23340 (patch)
treef0827312f9d37b92a15c546249fe1341c6588b1a /runtime/shims/library_loader.cpp
parent825fc61a07931c56d8057f152c65aae75ddfae8e (diff)
parent663c4d95060dfe63bb77461976ed8c4734fef345 (diff)
Merge pull request #2750 from admin
Diffstat (limited to 'runtime/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp32
1 files changed, 30 insertions, 2 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index 66feefb9b..2fa04c75f 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -9,15 +9,17 @@
9// So for a module the shim registry owns (rt::shims::owns_dll: xgameruntime.dll, d3d12_xs.dll, xmem.dll, ...), these 9// So for a module the shim registry owns (rt::shims::owns_dll: xgameruntime.dll, d3d12_xs.dll, xmem.dll, ...), these
10// return a pseudo-handle, and GetProcAddress on it resolves names (and "#ordinal") through rt::shims::find. A name the 10// return a pseudo-handle, and GetProcAddress on it resolves names (and "#ordinal") through rt::shims::find. A name the
11// registry doesn't serve returns NULL with ERROR_PROC_NOT_FOUND, as a real module would, and is logged once. Every 11// registry doesn't serve returns NULL with ERROR_PROC_NOT_FOUND, as a real module would, and is logged once. Every
12// other module goes to the host unchanged. Only images mapped by the loader see these: they are bound through the 12// other module uses the same named shims as static imports, falling back to the host. Only images mapped by the
13// registry, the runtime's own code calls the host directly. 13// loader see these: they are bound through the registry, the runtime's own code calls the host directly.
14#include <windows.h> 14#include <windows.h>
15 15
16#include <cstdio> 16#include <cstdio>
17#include <deque> 17#include <deque>
18#include <map>
18#include <mutex> 19#include <mutex>
19#include <set> 20#include <set>
20#include <string> 21#include <string>
22#include <vector>
21 23
22#include "shims.h" 24#include "shims.h"
23 25
@@ -67,6 +69,25 @@ std::string module_name(std::string path) {
67 return path; 69 return path;
68} 70}
69 71
72std::string host_module_name(HMODULE module) {
73 std::lock_guard lock(loader_mutex());
74 static auto* names = new std::map<HMODULE, std::string>;
75 if (auto it = names->find(module); it != names->end()) return it->second;
76
77 std::vector<wchar_t> path(MAX_PATH);
78 for (;;) {
79 const DWORD length = GetModuleFileNameW(module, path.data(), static_cast<DWORD>(path.size()));
80 if (!length) return {};
81 if (length < path.size()) {
82 const std::string name = module_name(narrow(path.data()));
83 names->emplace(module, name);
84 return name;
85 }
86 if (path.size() == 32768) return {};
87 path.resize(path.size() > 16384 ? 32768 : path.size() * 2);
88 }
89}
90
70} // namespace 91} // namespace
71 92
72// The pseudo-handle for an owned module, or nullptr when the shims don't own `path`'s module. 93// The pseudo-handle for an owned module, or nullptr when the shims don't own `path`'s module.
@@ -205,6 +226,13 @@ FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) {
205 SetLastError(ERROR_PROC_NOT_FOUND); 226 SetLastError(ERROR_PROC_NOT_FOUND);
206 return nullptr; 227 return nullptr;
207 } 228 }
229 if (module && !IS_INTRESOURCE(name)) {
230 const DWORD error = GetLastError();
231 const std::string dll = host_module_name(module);
232 void* shim = dll.empty() ? nullptr : find(dll, what);
233 SetLastError(error);
234 if (shim) return reinterpret_cast<FARPROC>(shim);
235 }
208 return GetProcAddress(module, name); 236 return GetProcAddress(module, name);
209} 237}
210 238