upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index d865e4179..e1ebfe01f 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -161,6 +161,26 @@ Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir))
161} 161}
162 162
163Loader::~Loader() { 163Loader::~Loader() {
164 // Importers must release their resources before their dependencies. Keep every mapping, unwind table and TLS
165 // block alive throughout the notifications: a detach handler can still call an imported function or free an
166 // FLS slot whose destructor lives in another mapped image.
167 for (auto it = init_order_.rbegin(); it != init_order_.rend(); ++it) {
168 Module& m = **it;
169 if (!m.initialised) continue; // a map-only load never opts in to running image code
170 m.initialised = false;
171 try {
172 run_tls_callbacks(m, DLL_PROCESS_DETACH);
173 } catch (const std::exception& e) {
174 // A module may have changed its callback table since attach. A destructor cannot propagate the validation
175 // error; report it and still let DllMain release resources before the image is freed.
176 std::fprintf(stderr, "[rt] TLS process detach failed for %s: %s\n", m.name.c_str(), e.what());
177 }
178 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
179 if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) {
180 auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva);
181 entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored
182 }
183 }
164 shims::remove_module_lookup(this); 184 shims::remove_module_lookup(this);
165 for (auto* m : order_) { 185 for (auto* m : order_) {
166 // TLS state must go away before the image and its TLS block are unmapped. 186 // TLS state must go away before the image and its TLS block are unmapped.