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.cpp87
1 files changed, 76 insertions, 11 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp
index 2fa04c75f..9d3810705 100644
--- a/runtime/shims/library_loader.cpp
+++ b/runtime/shims/library_loader.cpp
@@ -14,7 +14,9 @@
14#include <windows.h> 14#include <windows.h>
15 15
16#include <cstdio> 16#include <cstdio>
17#include <cstring>
17#include <deque> 18#include <deque>
19#include <filesystem>
18#include <map> 20#include <map>
19#include <mutex> 21#include <mutex>
20#include <set> 22#include <set>
@@ -169,26 +171,87 @@ HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) {
169// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any 171// GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any
170// bundled package module already mapped by the native loader. 172// bundled package module already mapped by the native loader.
171HMODULE WINAPI get_module_handle_a(LPCSTR name) { 173HMODULE WINAPI get_module_handle_a(LPCSTR name) {
172 if (name) { 174 if (!name) {
173 if (HMODULE h = owned_handle(name)) return h; 175 if (void* h = bundled_main_module()) return reinterpret_cast<HMODULE>(h);
174 const std::string dll = module_name(name); 176 return GetModuleHandleA(name);
175 if (!dll.empty())
176 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
177 } 177 }
178 if (HMODULE h = owned_handle(name)) return h;
179 const std::string dll = module_name(name);
180 if (!dll.empty())
181 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
178 return GetModuleHandleA(name); 182 return GetModuleHandleA(name);
179} 183}
180 184
181HMODULE WINAPI get_module_handle_w(LPCWSTR name) { 185HMODULE WINAPI get_module_handle_w(LPCWSTR name) {
182 if (name) { 186 if (!name) {
183 const std::string path = narrow(name); 187 // The process's own image: the mapped exe, not gta6rt.
184 if (HMODULE h = owned_handle(path)) return h; 188 if (void* h = bundled_main_module()) return reinterpret_cast<HMODULE>(h);
185 const std::string dll = module_name(path); 189 return GetModuleHandleW(name);
186 if (!dll.empty())
187 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
188 } 190 }
191 const std::string path = narrow(name);
192 if (HMODULE h = owned_handle(path)) return h;
193 const std::string dll = module_name(path);
194 if (!dll.empty())
195 if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h);
189 return GetModuleHandleW(name); 196 return GetModuleHandleW(name);
190} 197}
191 198
199// The Win32 spelling of a stored module path: backslashes throughout, and a unix-absolute path (a leading "/", only
200// possible under Wine) becomes Z:\..., the drive Wine maps at the filesystem root. A relative stored path is made
201// absolute through the host first, so the result is always drive-qualified.
202std::wstring windows_form(const std::filesystem::path& p) {
203 std::wstring s = p.native();
204 for (auto& c : s)
205 if (c == L'/') c = L'\\';
206 if (s.size() >= 2 && s[1] != L':' && s[0] == L'\\' && s[1] != L'\\') s.insert(0, L"Z:");
207 if (s.size() < 2 || s[1] != L':') {
208 std::error_code ec;
209 s = std::filesystem::absolute(s, ec).native();
210 for (auto& c : s)
211 if (c == L'/') c = L'\\';
212 if (s.size() >= 2 && s[1] != L':' && s[0] == L'\\' && s[1] != L'\\') s.insert(0, L"Z:");
213 }
214 return s;
215}
216
217// GetModuleFileName answers for the mapped images: nullptr is the process's own image (the exe the loader mapped,
218// not gta6rt.exe) and a bundled module's handle names that module. Host modules fall through.
219DWORD WINAPI get_module_file_name_w(HMODULE module, LPWSTR buffer, DWORD size) {
220 const std::filesystem::path* p = bundled_module_path(module);
221 if (!p) return GetModuleFileNameW(module, buffer, size);
222 if (!buffer || !size) {
223 SetLastError(ERROR_INVALID_PARAMETER);
224 return 0;
225 }
226 const std::wstring s = windows_form(*p);
227 if (s.size() >= size) {
228 std::wmemcpy(buffer, s.c_str(), size - 1);
229 buffer[size - 1] = 0;
230 SetLastError(ERROR_INSUFFICIENT_BUFFER);
231 return size;
232 }
233 std::wmemcpy(buffer, s.c_str(), s.size() + 1);
234 return static_cast<DWORD>(s.size());
235}
236
237DWORD WINAPI get_module_file_name_a(HMODULE module, LPSTR buffer, DWORD size) {
238 const std::filesystem::path* p = bundled_module_path(module);
239 if (!p) return GetModuleFileNameA(module, buffer, size);
240 if (!buffer || !size) {
241 SetLastError(ERROR_INVALID_PARAMETER);
242 return 0;
243 }
244 const std::string s = narrow(windows_form(*p).c_str());
245 if (s.size() >= size) {
246 std::memcpy(buffer, s.c_str(), size - 1);
247 buffer[size - 1] = 0;
248 SetLastError(ERROR_INSUFFICIENT_BUFFER);
249 return size;
250 }
251 std::memcpy(buffer, s.c_str(), s.size() + 1);
252 return static_cast<DWORD>(s.size());
253}
254
192FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { 255FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) {
193 if (const Pseudo* p = pseudo(module)) { 256 if (const Pseudo* p = pseudo(module)) {
194 if (!name) { 257 if (!name) {
@@ -260,6 +323,8 @@ BOOL WINAPI disable_thread_calls(HMODULE module) {
260 {dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \ 323 {dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \
261 {dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \ 324 {dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \
262 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \ 325 {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \
326 {dll, "GetModuleFileNameA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_a)}, \
327 {dll, "GetModuleFileNameW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_file_name_w)}, \
263 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ 328 {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \
264 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \ 329 {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \
265 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)} 330 {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)}