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:
Diffstat (limited to 'runtime/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp54
1 files changed, 2 insertions, 52 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index f0a86221f..3a96092df 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -14,7 +14,6 @@
14// loader see these: they are bound through the registry, the runtime's own code calls the host directly. 14// loader see these: they are bound through the registry, the runtime's own code calls the host directly.
15#include <windows.h> 15#include <windows.h>
16 16
17#include <algorithm>
18#include <cstdio> 17#include <cstdio>
19#include <deque> 18#include <deque>
20#include <map> 19#include <map>
@@ -202,12 +201,8 @@ HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
202} 201}
203 202
204// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any 203// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any
205// bundled package module already mapped by the native loader. NULL names the title's main image once main.cpp has 204// bundled package module already mapped by the native loader.
206// set it (VIctory victory-hle src/modules/kernel.rs, GPL-3.0-or-later: "GetModuleHandleW(NULL) must return the
207// title's image, not our exe"), as the console's loader would answer: the title's process is its own.
208HMODULE WINAPI get_module_handle_a(LPCSTR name) { 205HMODULE WINAPI get_module_handle_a(LPCSTR name) {
209 if (!name)
210 if (void* main = main_image()) return static_cast<HMODULE>(main);
211 if (name) { 206 if (name) {
212 if (HMODULE h = owned_handle(name)) return h; 207 if (HMODULE h = owned_handle(name)) return h;
213 const std::string dll = module_name(name); 208 const std::string dll = module_name(name);
@@ -218,8 +213,6 @@ HMODULE WINAPI get_module_handle_a(LPCSTR name) {
218} 213}
219 214
220HMODULE WINAPI get_module_handle_w(LPCWSTR name) { 215HMODULE WINAPI get_module_handle_w(LPCWSTR name) {
221 if (!name)
222 if (void* main = main_image()) return static_cast<HMODULE>(main);
223 if (name) { 216 if (name) {
224 if (ascii_basename(name)) { 217 if (ascii_basename(name)) {
225 const std::string path = narrow(name); 218 const std::string path = narrow(name);
@@ -284,49 +277,6 @@ FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) {
284 return GetProcAddress(module, name); 277 return GetProcAddress(module, name);
285} 278}
286 279
287// GetModuleFileName of a mapped image (NULL: the main image, once set) is the file it was mapped from, the title's
288// own path, not gta6rt.exe's; Windows cannot answer for an image it did not map (ERROR_MOD_NOT_FOUND). Ported from
289// VIctory kernel.rs: the title relaunches itself and finds its folder through this path. The copy follows
290// Windows' rules: a buffer too small gets the truncated, terminated path, the return value `size` and
291// ERROR_INSUFFICIENT_BUFFER; otherwise the length without the terminator.
292bool mapped_module_path(HMODULE module, std::wstring& path) {
293 void* const base = module ? static_cast<void*>(module) : main_image();
294 return base && bundled_module_path(base, path);
295}
296
297template <class Char> DWORD copy_module_path(const std::basic_string<Char>& path, Char* out, DWORD size) {
298 if (!out || size == 0) {
299 SetLastError(ERROR_INSUFFICIENT_BUFFER);
300 return 0;
301 }
302 if (path.size() >= size) {
303 std::copy_n(path.data(), size - 1, out);
304 out[size - 1] = 0;
305 SetLastError(ERROR_INSUFFICIENT_BUFFER);
306 return size;
307 }
308 std::copy_n(path.data(), path.size(), out);
309 out[path.size()] = 0;
310 SetLastError(ERROR_SUCCESS);
311 return static_cast<DWORD>(path.size());
312}
313
314DWORD WINAPI get_module_file_name_w(HMODULE module, LPWSTR out, DWORD size) {
315 std::wstring path;
316 if (!mapped_module_path(module, path)) return GetModuleFileNameW(module, out, size);
317 return copy_module_path(path, out, size);
318}
319
320DWORD WINAPI get_module_file_name_a(HMODULE module, LPSTR out, DWORD size) {
321 std::wstring path;
322 if (!mapped_module_path(module, path)) return GetModuleFileNameA(module, out, size);
323 // In the ANSI code page, as GetModuleFileNameA gives it.
324 const int n = WideCharToMultiByte(CP_ACP, 0, path.c_str(), -1, nullptr, 0, nullptr, nullptr);
325 std::string narrow(n > 0 ? static_cast<size_t>(n - 1) : 0, ' ');
326 if (n > 1) WideCharToMultiByte(CP_ACP, 0, path.c_str(), -1, narrow.data(), n, nullptr, nullptr);
327 return copy_module_path(narrow, out, size);
328}
329
330BOOL WINAPI free_library(HMODULE module) { 280BOOL WINAPI free_library(HMODULE module) {
331 if (pseudo(module)) return TRUE; // owned modules are never unloaded 281 if (pseudo(module)) return TRUE; // owned modules are never unloaded
332 if (owns_bundled_module_handle(module)) return TRUE; // mapped modules stay live until ~Loader detaches them 282 if (owns_bundled_module_handle(module)) return TRUE; // mapped modules stay live until ~Loader detaches them
@@ -353,7 +303,7 @@ BOOL WINAPI disable_thread_calls(HMODULE module) {
353 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \ 303 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \
354 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ 304 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \
355 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \ 305 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \
356 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}, {dll, "GetModuleFileNameA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_a)}, {dll, "GetModuleFileNameW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_w)} 306 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}
357 307
358} // namespace 308} // namespace
359} // namespace rt::shims::library_loader 309} // namespace rt::shims::library_loader