diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index edbc3cbec..bdb3df6f5 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -352,10 +352,34 @@ bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t | |||
| 352 | } | 352 | } |
| 353 | return false; | 353 | return false; |
| 354 | } | 354 | } |
| 355 | |||
| 356 | void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { | ||
| 357 | return static_cast<Loader*>(ctx)->find_loaded_module(dll_lower); | ||
| 358 | } | ||
| 359 | |||
| 360 | void* bundled_load(void* ctx, const std::string& dll_lower, bool& handled) { | ||
| 361 | return static_cast<Loader*>(ctx)->load_runtime_module(dll_lower, handled); | ||
| 362 | } | ||
| 363 | |||
| 364 | void* bundled_get_proc(void* ctx, void* module, const std::string& sym, bool& owns_handle) { | ||
| 365 | return static_cast<Loader*>(ctx)->resolve_module_export(module, sym, owns_handle); | ||
| 366 | } | ||
| 367 | |||
| 368 | bool bundled_owns_handle(void* ctx, void* module) { | ||
| 369 | return static_cast<Loader*>(ctx)->owns_module_handle(module); | ||
| 370 | } | ||
| 371 | |||
| 372 | const shims::BundledModuleOps kBundledOps{ | ||
| 373 | &bundled_find_loaded, | ||
| 374 | &bundled_load, | ||
| 375 | &bundled_get_proc, | ||
| 376 | &bundled_owns_handle, | ||
| 377 | }; | ||
| 355 | } // namespace | 378 | } // namespace |
| 356 | 379 | ||
| 357 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { | 380 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { |
| 358 | shims::add_module_lookup(this, &lookup_in_loader); | 381 | shims::add_module_lookup(this, &lookup_in_loader); |
| 382 | shims::add_bundled_loader(this, &kBundledOps); | ||
| 359 | } | 383 | } |
| 360 | 384 | ||
| 361 | Loader::~Loader() { | 385 | Loader::~Loader() { |
| @@ -379,6 +403,7 @@ Loader::~Loader() { | |||
| 379 | entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored | 403 | entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored |
| 380 | } | 404 | } |
| 381 | } | 405 | } |
| 406 | shims::remove_bundled_loader(this); | ||
| 382 | shims::remove_module_lookup(this); | 407 | shims::remove_module_lookup(this); |
| 383 | for (auto* m : order_) { | 408 | for (auto* m : order_) { |
| 384 | // TLS state must go away before the image and its TLS block are unmapped. | 409 | // TLS state must go away before the image and its TLS block are unmapped. |
| @@ -401,6 +426,7 @@ Loader::~Loader() { | |||
| 401 | } | 426 | } |
| 402 | 427 | ||
| 403 | Module& Loader::load(const std::filesystem::path& image) { | 428 | Module& Loader::load(const std::filesystem::path& image) { |
| 429 | std::lock_guard lock(mutex_); | ||
| 404 | std::string key = lower(image.filename().string()); | 430 | std::string key = lower(image.filename().string()); |
| 405 | if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; | 431 | if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; |
| 406 | Module& m = map_image(image); | 432 | Module& m = map_image(image); |
| @@ -416,6 +442,86 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 416 | return m; | 442 | return m; |
| 417 | } | 443 | } |
| 418 | 444 | ||
| 445 | void* Loader::find_loaded_module(const std::string& dll_lower) const { | ||
| 446 | std::lock_guard lock(mutex_); | ||
| 447 | auto it = by_name_.find(dll_lower); | ||
| 448 | return it == by_name_.end() ? nullptr : static_cast<void*>(it->second->base); | ||
| 449 | } | ||
| 450 | |||
| 451 | void* Loader::load_runtime_module(const std::string& dll_lower, bool& handled) { | ||
| 452 | handled = false; | ||
| 453 | if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr; | ||
| 454 | |||
| 455 | std::lock_guard lock(mutex_); | ||
| 456 | if (auto it = by_name_.find(dll_lower); it != by_name_.end()) { | ||
| 457 | handled = true; | ||
| 458 | try { | ||
| 459 | initialise(); | ||
| 460 | return static_cast<void*>(it->second->base); | ||
| 461 | } catch (const std::exception& e) { | ||
| 462 | std::fprintf(stderr, "[rt] loader: run-time initialise of %s failed: %s\n", dll_lower.c_str(), e.what()); | ||
| 463 | SetLastError(ERROR_DLL_INIT_FAILED); | ||
| 464 | return nullptr; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | const std::filesystem::path candidate = game_dir_ / dll_lower; | ||
| 469 | std::error_code ec; | ||
| 470 | if (!std::filesystem::is_regular_file(candidate, ec)) return nullptr; | ||
| 471 | |||
| 472 | handled = true; | ||
| 473 | try { | ||
| 474 | Module& m = load(candidate); | ||
| 475 | initialise(); | ||
| 476 | std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str()); | ||
| 477 | return static_cast<void*>(m.base); | ||
| 478 | } catch (const std::exception& e) { | ||
| 479 | // Do not leave a failed run-time load indexed by name so later GetModuleHandle/LoadLibrary calls do not | ||
| 480 | // observe a half-initialised module. ~Loader still frees any VirtualAlloc'd mapping in order_. | ||
| 481 | by_name_.erase(dll_lower); | ||
| 482 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); | ||
| 483 | SetLastError(ERROR_DLL_INIT_FAILED); | ||
| 484 | return nullptr; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | void* Loader::resolve_module_export(void* module_handle, const std::string& sym, bool& owns_handle) { | ||
| 489 | owns_handle = false; | ||
| 490 | if (!module_handle) return nullptr; | ||
| 491 | std::lock_guard lock(mutex_); | ||
| 492 | Module* target = nullptr; | ||
| 493 | for (Module* m : order_) { | ||
| 494 | if (m->base == module_handle) { | ||
| 495 | target = m; | ||
| 496 | break; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | if (!target) return nullptr; | ||
| 500 | owns_handle = true; | ||
| 501 | if (auto it = target->exports.find(sym); it != target->exports.end()) return it->second; | ||
| 502 | if (auto it = target->forwarders.find(sym); it != target->forwarders.end()) { | ||
| 503 | try { | ||
| 504 | Binding binding = Binding::Unimplemented; | ||
| 505 | void* fn = resolve_forwarder(it->second, binding, 0); | ||
| 506 | if (binding == Binding::Module) initialise(); | ||
| 507 | return fn; | ||
| 508 | } catch (const std::exception& e) { | ||
| 509 | std::fprintf(stderr, "[rt] loader: forwarder %s from %s failed: %s\n", it->second.c_str(), | ||
| 510 | target->name.c_str(), e.what()); | ||
| 511 | return nullptr; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | return nullptr; | ||
| 515 | } | ||
| 516 | |||
| 517 | bool Loader::owns_module_handle(void* module_handle) const { | ||
| 518 | if (!module_handle) return false; | ||
| 519 | std::lock_guard lock(mutex_); | ||
| 520 | for (const Module* m : order_) | ||
| 521 | if (m->base == module_handle) return true; | ||
| 522 | return false; | ||
| 523 | } | ||
| 524 | |||
| 419 | // x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing | 525 | // x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing |
| 420 | // through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps | 526 | // through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps |
| 421 | // are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables. | 527 | // are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables. |