diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f26527989..caf5f0ad5 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -252,14 +252,62 @@ void Loader::parse_exports(Module& m) { | |||
| 252 | auto* funcs = at<uint32_t>(m.base, ed->AddressOfFunctions); | 252 | auto* funcs = at<uint32_t>(m.base, ed->AddressOfFunctions); |
| 253 | auto* names = at<uint32_t>(m.base, ed->AddressOfNames); | 253 | auto* names = at<uint32_t>(m.base, ed->AddressOfNames); |
| 254 | auto* ords = at<uint16_t>(m.base, ed->AddressOfNameOrdinals); | 254 | auto* ords = at<uint16_t>(m.base, ed->AddressOfNameOrdinals); |
| 255 | // An export whose RVA lands inside the export directory is a forwarder string ("KERNEL32.Sleep", | ||
| 256 | // "api-ms-win-core-file-l1-1-0.CreateFileW"), not code. Storing base + that RVA would hand callers a pointer | ||
| 257 | // into the string, so forwarders are kept apart and followed when an import asks for one. | ||
| 258 | const uint64_t dir_begin = dir.VirtualAddress, dir_end = dir_begin + dir.Size; | ||
| 259 | auto add_export = [&](const std::string& key, uint32_t rva) { | ||
| 260 | if (rva >= dir_begin && rva < dir_end) | ||
| 261 | m.forwarders[key] = read_string(m.name, m.base, m.size, rva, "a forwarded export"); | ||
| 262 | else | ||
| 263 | m.exports[key] = m.base + rva; | ||
| 264 | }; | ||
| 255 | for (uint32_t i = 0; i < ed->NumberOfFunctions; ++i) | 265 | for (uint32_t i = 0; i < ed->NumberOfFunctions; ++i) |
| 256 | if (funcs[i]) m.exports["#" + std::to_string(ed->Base + i)] = m.base + funcs[i]; | 266 | if (funcs[i]) add_export("#" + std::to_string(ed->Base + i), funcs[i]); |
| 257 | for (uint32_t i = 0; i < ed->NumberOfNames; ++i) { | 267 | for (uint32_t i = 0; i < ed->NumberOfNames; ++i) { |
| 258 | if (ords[i] >= ed->NumberOfFunctions) | 268 | if (ords[i] >= ed->NumberOfFunctions) |
| 259 | throw std::runtime_error(m.name + ": an export name points past the address table"); | 269 | throw std::runtime_error(m.name + ": an export name points past the address table"); |
| 260 | m.exports[read_string(m.name, m.base, m.size, names[i], "an export name")] = m.base + funcs[ords[i]]; | 270 | add_export(read_string(m.name, m.base, m.size, names[i], "an export name"), funcs[ords[i]]); |
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | // Follows a forwarder string to the module and export it names. Bundled modules are searched first, with or without | ||
| 275 | // the extension, because a forwarder may point at another mapped module; system DLLs go through LoadLibraryEx, whose | ||
| 276 | // own GetProcAddress already follows its forwarders. Errors are "not found": the caller decides what to bind. | ||
| 277 | void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int depth) { | ||
| 278 | if (depth >= 8) return nullptr; // a longer chain is a loop between modules, not a re-export | ||
| 279 | auto dot = spec.rfind('.'); | ||
| 280 | if (dot == std::string::npos || dot == 0 || dot + 1 >= spec.size()) return nullptr; | ||
| 281 | const std::string module = spec.substr(0, dot), symbol = spec.substr(dot + 1); | ||
| 282 | const bool by_ordinal = symbol.size() > 1 && symbol[0] == '#'; | ||
| 283 | uint32_t ordinal = 0; | ||
| 284 | if (by_ordinal) { | ||
| 285 | for (size_t i = 1; i < symbol.size(); ++i) { | ||
| 286 | if (symbol[i] < '0' || symbol[i] > '9' || ordinal > 0xFFFF) return nullptr; | ||
| 287 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | const std::filesystem::path candidates[] = {game_dir_ / module, game_dir_ / (module + ".dll")}; | ||
| 292 | for (const auto& candidate : candidates) { | ||
| 293 | std::error_code ec; | ||
| 294 | if (prefer_host_copy(lower(module)) || !std::filesystem::exists(candidate, ec)) continue; | ||
| 295 | Module& dep = load(candidate); | ||
| 296 | if (auto it = dep.exports.find(symbol); it != dep.exports.end()) { | ||
| 297 | binding = Binding::Module; | ||
| 298 | return it->second; | ||
| 299 | } | ||
| 300 | if (auto it = dep.forwarders.find(symbol); it != dep.forwarders.end()) | ||
| 301 | return resolve_forwarder(it->second, binding, depth + 1); | ||
| 302 | return nullptr; // the bundled module exists and does not export it; don't look for a host namesake | ||
| 261 | } | 303 | } |
| 262 | // Forwarded exports (RVA inside the export directory) aren't followed yet; none of the bundled DLLs use them. | 304 | |
| 305 | HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 306 | if (!h) return nullptr; | ||
| 307 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); | ||
| 308 | if (!p) return nullptr; | ||
| 309 | binding = Binding::Host; | ||
| 310 | return reinterpret_cast<void*>(p); | ||
| 263 | } | 311 | } |
| 264 | 312 | ||
| 265 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { | 313 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { |
| @@ -278,6 +326,13 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name | |||
| 278 | m.imports.push_back({dll, sym, Binding::Module}); | 326 | m.imports.push_back({dll, sym, Binding::Module}); |
| 279 | return it->second; | 327 | return it->second; |
| 280 | } | 328 | } |
| 329 | if (auto it = dep.forwarders.find(sym); it != dep.forwarders.end()) { | ||
| 330 | Binding binding = Binding::Unimplemented; | ||
| 331 | if (void* p = resolve_forwarder(it->second, binding, 0)) { | ||
| 332 | m.imports.push_back({dll, sym, binding}); | ||
| 333 | return p; | ||
| 334 | } | ||
| 335 | } | ||
| 281 | } | 336 | } |
| 282 | // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. | 337 | // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. |
| 283 | if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { | 338 | if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { |
| @@ -334,8 +389,16 @@ void Loader::bind_imports(Module& m) { | |||
| 334 | require_range(m.name, rva, sizeof(IMAGE_DELAYLOAD_DESCRIPTOR), m.size, "the delay-import table"); | 389 | require_range(m.name, rva, sizeof(IMAGE_DELAYLOAD_DESCRIPTOR), m.size, "the delay-import table"); |
| 335 | auto* d = at<IMAGE_DELAYLOAD_DESCRIPTOR>(m.base, static_cast<uint32_t>(rva)); | 390 | auto* d = at<IMAGE_DELAYLOAD_DESCRIPTOR>(m.base, static_cast<uint32_t>(rva)); |
| 336 | if (!d->DllNameRVA) break; | 391 | if (!d->DllNameRVA) break; |
| 392 | // x64 descriptors always carry dlattrRva (0x1). Without it the fields would be 32-bit virtual | ||
| 393 | // addresses, which cannot express a 64-bit image base, so the descriptor is not usable as written. | ||
| 394 | if (!d->Attributes.RvaBased) | ||
| 395 | throw std::runtime_error(m.name + ": a delay-import descriptor without the RVA attribute"); | ||
| 396 | if (!d->ImportAddressTableRVA) | ||
| 397 | throw std::runtime_error(m.name + ": a delay-import descriptor has no address table"); | ||
| 337 | std::string dll = read_string(m.name, m.base, m.size, d->DllNameRVA, "a delay-imported DLL name"); | 398 | std::string dll = read_string(m.name, m.base, m.size, d->DllNameRVA, "a delay-imported DLL name"); |
| 338 | bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA); | 399 | // The name table is optional: without it, entries are read back through the address table itself. |
| 400 | bind_thunks(dll, d->ImportNameTableRVA ? d->ImportNameTableRVA : d->ImportAddressTableRVA, | ||
| 401 | d->ImportAddressTableRVA); | ||
| 339 | } | 402 | } |
| 340 | } | 403 | } |
| 341 | } | 404 | } |