loader: don't bind shim-owned imports to host namesakes; document import routing

This commit is contained in:
Nolan Cotter
2026-11-22 09:17:06 -07:00
parent 75b2ea486c
commit bb052d8575
3 changed files with 104 additions and 15 deletions

View File

@ -1082,14 +1082,20 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
return nullptr; // the bundled module exists and does not export it; don't look for a host namesake
}
HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!h) return nullptr;
if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h);
FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str());
if (!p) return nullptr;
if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
binding = Binding::Host;
return reinterpret_cast<void*>(p);
// A module the shims own (xgameruntime.dll, gameinput.dll, d3d12_xs.dll, xmem.dll) never binds to a host
// namesake: xgameruntime.dll and gameinput.dll exist on the PC too, and a call into the real Gaming Services
// runtime would bypass the offline shims the same way a direct host load would (owns_dll, registry.cpp).
if (!shims::owns_dll(shim_key)) {
HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!h) return nullptr;
if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h);
FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str());
if (!p) return nullptr;
if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
binding = Binding::Host;
return reinterpret_cast<void*>(p);
}
return nullptr;
}
void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {
@ -1117,13 +1123,18 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name
}
}
// Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search.
if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it
FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str());
if (p) {
if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
m.imports.push_back({dll, sym, Binding::Host});
return reinterpret_cast<void*>(p);
// A shim-owned module is skipped: xgameruntime.dll and gameinput.dll have real PC namesakes in System32, and an
// unregistered import must end unimplemented, exactly as GetProcAddress on the pseudo-handle returns NULL
// (library_loader.cpp). Binding it to Gaming Services would also leave the process online-capable.
if (!shims::owns_dll(key)) {
if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it
FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str());
if (p) {
if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p));
m.imports.push_back({dll, sym, Binding::Host});
return reinterpret_cast<void*>(p);
}
}
}
m.imports.push_back({dll, sym, Binding::Unimplemented});