diff options
Diffstat (limited to 'runtime/src/pe_loader.h')
| -rw-r--r-- | runtime/src/pe_loader.h | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index c87f157a4..307837351 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h | |||
| @@ -27,7 +27,8 @@ struct ImportRecord { | |||
| 27 | }; | 27 | }; |
| 28 | 28 | ||
| 29 | struct Module { | 29 | struct Module { |
| 30 | std::string name; | 30 | std::string name; // UTF-8 basename for diagnostics and traces, not a lookup key |
| 31 | std::string narrow_key; // legacy lower-case narrow basename; empty when it cannot round-trip | ||
| 31 | std::filesystem::path path; | 32 | std::filesystem::path path; |
| 32 | uint8_t* base = nullptr; | 33 | uint8_t* base = nullptr; |
| 33 | uint64_t preferred_base = 0; | 34 | uint64_t preferred_base = 0; |
| @@ -41,6 +42,10 @@ struct Module { | |||
| 41 | void* function_table = nullptr; | 42 | void* function_table = nullptr; |
| 42 | uint32_t function_count = 0; | 43 | uint32_t function_count = 0; |
| 43 | 44 | ||
| 45 | // Mapped for a run-time LoadLibrary or a GetProcAddress forwarder (a dynamic load), not by a host load() (the | ||
| 46 | // start-up graph, a static load). DllMain's DLL_PROCESS_ATTACH gets lpvReserved NULL only for a dynamic load. | ||
| 47 | bool dynamic_load = false; | ||
| 48 | |||
| 44 | std::vector<ImportRecord> imports; | 49 | std::vector<ImportRecord> imports; |
| 45 | std::map<std::string, void*> exports; // by name, plus "#ordinal" | 50 | std::map<std::string, void*> exports; // by name, plus "#ordinal" |
| 46 | std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports | 51 | std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports |
| @@ -50,7 +55,8 @@ struct Module { | |||
| 50 | // __declspec(thread) reads; it is not a TlsAlloc slot. The loader reserves distinct indices by loading | 55 | // __declspec(thread) reads; it is not a TlsAlloc slot. The loader reserves distinct indices by loading |
| 51 | // gta6rt_tls_host.dll (and cloning it on demand for additional simultaneous TLS modules, up to 16 at a time). | 56 | // gta6rt_tls_host.dll (and cloning it on demand for additional simultaneous TLS modules, up to 16 at a time). |
| 52 | // Every thread started after load() gets its own copy through the helper's DLL_THREAD_ATTACH, and gives it back | 57 | // Every thread started after load() gets its own copy through the helper's DLL_THREAD_ATTACH, and gives it back |
| 53 | // at DLL_THREAD_DETACH; install_tls_for_thread() is for threads that already existed. | 58 | // at DLL_THREAD_DETACH. A thread that was already running gets its copy from load() itself, as it would from |
| 59 | // Windows for a DLL Windows loads: a block, no DLL_THREAD_ATTACH, and DLL_THREAD_DETACH when it ends. | ||
| 54 | uint32_t tls_index = kNoTlsIndex; | 60 | uint32_t tls_index = kNoTlsIndex; |
| 55 | uint32_t tls_slot = kNoTlsIndex; // index into the loader's reserved TLS-host slot pool | 61 | uint32_t tls_slot = kNoTlsIndex; // index into the loader's reserved TLS-host slot pool |
| 56 | uint64_t tls_generation = 0; // unique token per TLS initialisation, distinguishing reused slots/addresses | 62 | uint64_t tls_generation = 0; // unique token per TLS initialisation, distinguishing reused slots/addresses |
| @@ -104,24 +110,32 @@ public: | |||
| 104 | void run_tls_callbacks(Module& m, uint32_t reason); | 110 | void run_tls_callbacks(Module& m, uint32_t reason); |
| 105 | 111 | ||
| 106 | // Installs/removes this module's template copy in the calling thread's TEB slot. load() does this for its caller | 112 | // Installs/removes this module's template copy in the calling thread's TEB slot. load() does this for its caller |
| 107 | // and the thread events for threads started after it; both are no-ops on a thread that already has this module's | 113 | // and for every other thread running at the time, and the thread events for threads started after it, so on any |
| 108 | // copy. Call install on a thread that existed before load(), before mapped code runs there, and uninstall before | 114 | // of those both are no-ops. They remain for a thread load() could not reach (pe_loader.cpp, "Threads that |
| 109 | // that thread exits. Each module's copy sits in its own TEB slot, so one thread can hold copies of several | 115 | // existed before load()", lists the cases): install before mapped code runs there, uninstall before the thread |
| 110 | // modules. These do not run TLS callbacks (use | 116 | // exits. Each module's copy sits in its own TEB slot, so one thread can hold copies of several modules. These do |
| 111 | // run_tls_callbacks with DLL_THREAD_ATTACH/DETACH for that). | 117 | // not run TLS callbacks (use run_tls_callbacks with DLL_THREAD_ATTACH/DETACH for that). |
| 112 | void install_tls_for_thread(Module& m); | 118 | void install_tls_for_thread(Module& m); |
| 113 | void uninstall_tls_for_thread(Module& m); | 119 | void uninstall_tls_for_thread(Module& m); |
| 114 | 120 | ||
| 115 | // Run-time module lookup/loading used by the library_loader shim for bundled package DLLs. | 121 | // Run-time module lookup/loading used by the library_loader shim for bundled package DLLs. |
| 116 | void* find_loaded_module(const std::string& dll_lower) const; | 122 | void* find_loaded_module(const std::string& dll_lower) const; |
| 117 | void* load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled); | 123 | void* load_runtime_module(const std::string& dll_lower, uint32_t flags, bool& handled); |
| 124 | // The same by the module's native basename (any Unicode name, matched case-insensitively as Windows matches | ||
| 125 | // module names), for the W entries of LoadLibrary / GetModuleHandle. | ||
| 126 | void* find_loaded_module_w(const std::wstring& basename) const; | ||
| 127 | void* load_runtime_module_w(const std::wstring& basename, uint32_t flags, bool& handled); | ||
| 118 | void* resolve_module_export(void* module_handle, const std::string& sym, bool& owns_handle); | 128 | void* resolve_module_export(void* module_handle, const std::string& sym, bool& owns_handle); |
| 119 | bool owns_module_handle(void* module_handle) const; | 129 | bool owns_module_handle(void* module_handle) const; |
| 120 | 130 | ||
| 121 | private: | 131 | private: |
| 122 | void initialise(Module& m); | 132 | void initialise(Module& m); |
| 123 | static void set_initialised(Module& m, bool value); | 133 | static void set_initialised(Module& m, bool value); |
| 124 | Module& map_image(const std::filesystem::path& path); | 134 | Module* find_narrow_module(const std::string& dll_lower) const; // caller holds mutex_ |
| 135 | Module* find_wide_module(const std::wstring& basename) const; // caller holds mutex_ | ||
| 136 | void* load_runtime_candidate(Module* existing, const std::filesystem::path& candidate, uint32_t flags, | ||
| 137 | const std::string& display); // caller holds mutex_ | ||
| 138 | Module& map_image(const std::filesystem::path& path, const std::string& narrow_key); | ||
| 125 | void initialize_tls(Module& m); | 139 | void initialize_tls(Module& m); |
| 126 | void bind_imports(Module& m); | 140 | void bind_imports(Module& m); |
| 127 | void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal); | 141 | void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal); |
| @@ -132,9 +146,10 @@ private: | |||
| 132 | 146 | ||
| 133 | mutable std::recursive_mutex mutex_; | 147 | mutable std::recursive_mutex mutex_; |
| 134 | std::filesystem::path game_dir_; | 148 | std::filesystem::path game_dir_; |
| 135 | std::map<std::string, Module*> by_name_; // lower-case file name | 149 | std::map<std::filesystem::path::string_type, Module*> by_name_; // native basename, ASCII case folded |
| 136 | std::vector<Module*> order_; // mapping order; owns the modules | 150 | std::vector<Module*> order_; // mapping order; owns the modules |
| 137 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() | 151 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() |
| 152 | int runtime_loads_ = 0; // > 0 while a run-time load or forwarder maps modules (under mutex_): they are dynamic | ||
| 138 | 153 | ||
| 139 | // What find_address reads: one entry per mapped image, added once its memory exists. Under ranges_mutex_, a leaf | 154 | // What find_address reads: one entry per mapped image, added once its memory exists. Under ranges_mutex_, a leaf |
| 140 | // lock: mutex_ is held while image code runs and the registry holds its own lock around the lookup, so the | 155 | // lock: mutex_ is held while image code runs and the registry holds its own lock around the lookup, so the |