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:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp82
1 files changed, 18 insertions, 64 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index f48ecd940..e8c504f0f 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -870,21 +870,11 @@ bool bundled_owns_handle(void* ctx, void* module) {
870 return static_cast<Loader*>(ctx)->owns_module_handle(module); 870 return static_cast<Loader*>(ctx)->owns_module_handle(module);
871} 871}
872 872
873void* bundled_main_module(void* ctx) {
874 return static_cast<Loader*>(ctx)->main_module();
875}
876
877const std::filesystem::path* bundled_module_path(void* ctx, void* module) {
878 return static_cast<Loader*>(ctx)->module_path_for(module);
879}
880
881const shims::BundledModuleOps kBundledOps{ 873const shims::BundledModuleOps kBundledOps{
882 &bundled_find_loaded, 874 &bundled_find_loaded,
883 &bundled_load, 875 &bundled_load,
884 &bundled_get_proc, 876 &bundled_get_proc,
885 &bundled_owns_handle, 877 &bundled_owns_handle,
886 &bundled_main_module,
887 &bundled_module_path,
888}; 878};
889} // namespace 879} // namespace
890 880
@@ -1067,19 +1057,6 @@ bool Loader::owns_module_handle(void* module_handle) const {
1067 return false; 1057 return false;
1068} 1058}
1069 1059
1070void* Loader::main_module() const {
1071 std::lock_guard lock(mutex_);
1072 return order_.empty() ? nullptr : static_cast<void*>(order_.front()->base);
1073}
1074
1075const std::filesystem::path* Loader::module_path_for(void* module_handle) const {
1076 std::lock_guard lock(mutex_);
1077 if (!module_handle) return order_.empty() ? nullptr : &order_.front()->path;
1078 for (Module* m : order_)
1079 if (m->base == module_handle) return &m->path;
1080 return nullptr;
1081}
1082
1083// x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing 1060// x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing
1084// through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps 1061// through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps
1085// are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables. 1062// are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables.
@@ -1402,19 +1379,6 @@ void Loader::parse_exports(Module& m) {
1402 } 1379 }
1403} 1380}
1404 1381
1405// The file-name rule LoadLibrary applies to a bare module name, the same normalisation library_loader.cpp's
1406// module_name() uses: lower-case, a trailing '.' dropped, ".dll" added when there is no extension. Without it an
1407// import naming "GameInput" or "gameinput.dll." misses the shim and owns_dll lookups while LoadLibraryExA still
1408// binds the real System32 module.
1409std::string dll_key(std::string name) {
1410 const size_t slash = name.find_last_of("\\/");
1411 if (slash != std::string::npos) name.erase(0, slash + 1);
1412 name = lower(name);
1413 if (!name.empty() && name.back() == '.') name.pop_back();
1414 else if (name.find('.') == std::string::npos) name += ".dll";
1415 return name;
1416}
1417
1418// Follows a forwarder string to the module and export it names. Bundled modules are searched first, with or without 1382// Follows a forwarder string to the module and export it names. Bundled modules are searched first, with or without
1419// the extension, because a forwarder may point at another mapped module; system DLLs go through LoadLibraryEx, whose 1383// the extension, because a forwarder may point at another mapped module; system DLLs go through LoadLibraryEx, whose
1420// own GetProcAddress already follows its forwarders. Errors are "not found": the caller decides what to bind. 1384// own GetProcAddress already follows its forwarders. Errors are "not found": the caller decides what to bind.
@@ -1426,7 +1390,8 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
1426 1390
1427 // A forwarder can name a module the loader shims. Shims take precedence over a bundled or host copy, exactly as 1391 // A forwarder can name a module the loader shims. Shims take precedence over a bundled or host copy, exactly as
1428 // they do for a direct import in resolve(); without this, a forwarded xmem or d3d12_xs call becomes a failure thunk. 1392 // they do for a direct import in resolve(); without this, a forwarded xmem or d3d12_xs call becomes a failure thunk.
1429 std::string shim_key = dll_key(module); 1393 std::string shim_key = lower(module);
1394 if (shim_key.size() < 4 || shim_key.compare(shim_key.size() - 4, 4, ".dll") != 0) shim_key += ".dll";
1430 if (void* p = shims::find(shim_key, symbol)) { 1395 if (void* p = shims::find(shim_key, symbol)) {
1431 binding = Binding::Shim; 1396 binding = Binding::Shim;
1432 return p; 1397 return p;
@@ -1458,24 +1423,18 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
1458 return nullptr; // the bundled module exists and does not export it; don't look for a host namesake 1423 return nullptr; // the bundled module exists and does not export it; don't look for a host namesake
1459 } 1424 }
1460 1425
1461 // A module the shims own (xgameruntime.dll, gameinput.dll, d3d12_xs.dll, xmem.dll) never binds to a host 1426 HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
1462 // namesake: xgameruntime.dll and gameinput.dll exist on the PC too, and a call into the real Gaming Services 1427 if (!h) return nullptr;
1463 // runtime would bypass the offline shims the same way a direct host load would (owns_dll, registry.cpp). 1428 if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h);
1464 if (!shims::owns_dll(shim_key)) { 1429 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str());
1465 HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); 1430 if (!p) return nullptr;
1466 if (!h) return nullptr; 1431 if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
1467 if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); 1432 binding = Binding::Host;
1468 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); 1433 return reinterpret_cast<void*>(p);
1469 if (!p) return nullptr;
1470 if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
1471 binding = Binding::Host;
1472 return reinterpret_cast<void*>(p);
1473 }
1474 return nullptr;
1475} 1434}
1476 1435
1477void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { 1436void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {
1478 std::string key = dll_key(dll); 1437 std::string key = lower(dll);
1479 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; 1438 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name;
1480 1439
1481 if (void* p = shims::find(key, sym)) { 1440 if (void* p = shims::find(key, sym)) {
@@ -1499,18 +1458,13 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name
1499 } 1458 }
1500 } 1459 }
1501 // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. 1460 // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search.
1502 // A shim-owned module is skipped: xgameruntime.dll and gameinput.dll have real PC namesakes in System32, and an 1461 if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
1503 // unregistered import must end unimplemented, exactly as GetProcAddress on the pseudo-handle returns NULL 1462 if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it
1504 // (library_loader.cpp). Binding it to Gaming Services would also leave the process online-capable. 1463 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str());
1505 if (!shims::owns_dll(key)) { 1464 if (p) {
1506 if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { 1465 if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
1507 if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it 1466 m.imports.push_back({dll, sym, Binding::Host});
1508 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); 1467 return reinterpret_cast<void*>(p);
1509 if (p) {
1510 if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
1511 m.imports.push_back({dll, sym, Binding::Host});
1512 return reinterpret_cast<void*>(p);
1513 }
1514 } 1468 }
1515 } 1469 }
1516 m.imports.push_back({dll, sym, Binding::Unimplemented}); 1470 m.imports.push_back({dll, sym, Binding::Unimplemented});