diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index bfeab4c7f..5514edba6 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -904,6 +904,15 @@ const shims::BundledModuleOps kBundledOps{ | |||
| 904 | &bundled_get_proc, | 904 | &bundled_get_proc, |
| 905 | &bundled_owns_handle, | 905 | &bundled_owns_handle, |
| 906 | }; | 906 | }; |
| 907 | |||
| 908 | // Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). | ||
| 909 | struct RuntimeLoadScope { | ||
| 910 | explicit RuntimeLoadScope(int& depth) : depth_(depth) { ++depth_; } | ||
| 911 | ~RuntimeLoadScope() { --depth_; } | ||
| 912 | RuntimeLoadScope(const RuntimeLoadScope&) = delete; | ||
| 913 | RuntimeLoadScope& operator=(const RuntimeLoadScope&) = delete; | ||
| 914 | int& depth_; | ||
| 915 | }; | ||
| 907 | } // namespace | 916 | } // namespace |
| 908 | 917 | ||
| 909 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { | 918 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { |
| @@ -965,6 +974,7 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 965 | Module* mapped = nullptr; | 974 | Module* mapped = nullptr; |
| 966 | try { | 975 | try { |
| 967 | mapped = &map_image(image, narrow_key); | 976 | mapped = &map_image(image, narrow_key); |
| 977 | mapped->dynamic_load = runtime_loads_ > 0; // before bind_imports: its dependencies are marked the same way | ||
| 968 | bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m | 978 | bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m |
| 969 | protect_sections(*mapped); | 979 | protect_sections(*mapped); |
| 970 | register_unwind(*mapped); | 980 | register_unwind(*mapped); |
| @@ -1004,6 +1014,7 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 1004 | if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr; | 1014 | if (dll_lower.empty() || prefer_host_copy(dll_lower)) return nullptr; |
| 1005 | 1015 | ||
| 1006 | std::lock_guard lock(mutex_); | 1016 | std::lock_guard lock(mutex_); |
| 1017 | const RuntimeLoadScope dynamic(runtime_loads_); | ||
| 1007 | Module* existing = find_narrow_module(dll_lower); | 1018 | Module* existing = find_narrow_module(dll_lower); |
| 1008 | const std::filesystem::path candidate = game_dir_ / dll_lower; | 1019 | const std::filesystem::path candidate = game_dir_ / dll_lower; |
| 1009 | std::error_code ec; | 1020 | std::error_code ec; |
| @@ -1065,6 +1076,7 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym, | |||
| 1065 | if (auto it = target->exports.find(sym); it != target->exports.end()) return it->second; | 1076 | if (auto it = target->exports.find(sym); it != target->exports.end()) return it->second; |
| 1066 | if (auto it = target->forwarders.find(sym); it != target->forwarders.end()) { | 1077 | if (auto it = target->forwarders.find(sym); it != target->forwarders.end()) { |
| 1067 | try { | 1078 | try { |
| 1079 | const RuntimeLoadScope dynamic(runtime_loads_); | ||
| 1068 | Binding binding = Binding::Unimplemented; | 1080 | Binding binding = Binding::Unimplemented; |
| 1069 | void* fn = resolve_forwarder(it->second, binding, 0); | 1081 | void* fn = resolve_forwarder(it->second, binding, 0); |
| 1070 | if (binding == Binding::Module) initialise(); | 1082 | if (binding == Binding::Module) initialise(); |
| @@ -1151,7 +1163,11 @@ void Loader::initialise(Module& m) { | |||
| 1151 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 1163 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 1152 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { | 1164 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { |
| 1153 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); | 1165 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); |
| 1154 | const BOOL ok = entry(m.base, DLL_PROCESS_ATTACH, nullptr); | 1166 | // lpvReserved "is NULL for dynamic loads and non-NULL for static loads" (DllMain docs). What Windows points |
| 1167 | // it at is its own; a static load gets a CONTEXT captured here, so a DLL that reads through it reads memory. | ||
| 1168 | CONTEXT context{}; | ||
| 1169 | if (!m.dynamic_load) RtlCaptureContext(&context); | ||
| 1170 | const BOOL ok = entry(m.base, DLL_PROCESS_ATTACH, m.dynamic_load ? nullptr : &context); | ||
| 1155 | if (startup_trace::enabled()) | 1171 | if (startup_trace::enabled()) |
| 1156 | startup_trace::event("dllmain", "\"module\":" + startup_trace::quote(m.name) + | 1172 | startup_trace::event("dllmain", "\"module\":" + startup_trace::quote(m.name) + |
| 1157 | ",\"result\":" + std::to_string(ok)); | 1173 | ",\"result\":" + std::to_string(ok)); |