diff options
Diffstat (limited to 'runtime/shims/library_loader.cpp')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 87 |
1 files changed, 11 insertions, 76 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp index 9d3810705..2fa04c75f 100644 --- a/runtime/shims/library_loader.cpp +++ b/runtime/shims/library_loader.cpp | |||
| @@ -14,9 +14,7 @@ | |||
| 14 | #include <windows.h> | 14 | #include <windows.h> |
| 15 | 15 | ||
| 16 | #include <cstdio> | 16 | #include <cstdio> |
| 17 | #include <cstring> | ||
| 18 | #include <deque> | 17 | #include <deque> |
| 19 | #include <filesystem> | ||
| 20 | #include <map> | 18 | #include <map> |
| 21 | #include <mutex> | 19 | #include <mutex> |
| 22 | #include <set> | 20 | #include <set> |
| @@ -171,87 +169,26 @@ HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { | |||
| 171 | // GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any | 169 | // GetModuleHandle answers for an owned module as if it were loaded (the GDK may probe before it loads), and for any |
| 172 | // bundled package module already mapped by the native loader. | 170 | // bundled package module already mapped by the native loader. |
| 173 | HMODULE WINAPI get_module_handle_a(LPCSTR name) { | 171 | HMODULE WINAPI get_module_handle_a(LPCSTR name) { |
| 174 | if (!name) { | 172 | if (name) { |
| 175 | if (void* h = bundled_main_module()) return reinterpret_cast<HMODULE>(h); | 173 | if (HMODULE h = owned_handle(name)) return h; |
| 176 | return GetModuleHandleA(name); | 174 | const std::string dll = module_name(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); | ||
| 182 | return GetModuleHandleA(name); | 178 | return GetModuleHandleA(name); |
| 183 | } | 179 | } |
| 184 | 180 | ||
| 185 | HMODULE WINAPI get_module_handle_w(LPCWSTR name) { | 181 | HMODULE WINAPI get_module_handle_w(LPCWSTR name) { |
| 186 | if (!name) { | 182 | if (name) { |
| 187 | // The process's own image: the mapped exe, not gta6rt. | 183 | const std::string path = narrow(name); |
| 188 | if (void* h = bundled_main_module()) return reinterpret_cast<HMODULE>(h); | 184 | if (HMODULE h = owned_handle(path)) return h; |
| 189 | return GetModuleHandleW(name); | 185 | const std::string dll = module_name(path); |
| 186 | if (!dll.empty()) | ||
| 187 | if (void* h = find_loaded_bundled_module(dll)) return reinterpret_cast<HMODULE>(h); | ||
| 190 | } | 188 | } |
| 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); | ||
| 196 | return GetModuleHandleW(name); | 189 | return GetModuleHandleW(name); |
| 197 | } | 190 | } |
| 198 | 191 | ||
| 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. | ||
| 202 | std::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. | ||
| 219 | DWORD 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 | |||
| 237 | DWORD 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 | |||
| 255 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { | 192 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { |
| 256 | if (const Pseudo* p = pseudo(module)) { | 193 | if (const Pseudo* p = pseudo(module)) { |
| 257 | if (!name) { | 194 | if (!name) { |
| @@ -323,8 +260,6 @@ BOOL WINAPI disable_thread_calls(HMODULE module) { | |||
| 323 | {dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \ | 260 | {dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \ |
| 324 | {dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \ | 261 | {dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \ |
| 325 | {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \ | 262 | {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)}, \ | ||
| 328 | {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ | 263 | {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ |
| 329 | {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \ | 264 | {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)}, \ |
| 330 | {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)} | 265 | {dll, "DisableThreadLibraryCalls", reinterpret_cast<void*>(&rt::shims::library_loader::disable_thread_calls)} |