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