upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 03:14:21 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 03:14:21 -0500
commiteda43133f8deac7d4a8b422a6bd74e923dd4ebc5 (patch)
treebc3aad56e2b601a8ce9f1b73a89340f35918ec7a /runtime/src
parent2437ae42a4bfaba74b91eb3a9520965367531020 (diff)
parentcc7651422956d1f362bdf32f900e543b357056eb (diff)
Merge pull request #1172 from farhat
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/pe_loader.cpp20
-rw-r--r--runtime/src/pe_loader.h11
2 files changed, 26 insertions, 5 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index cd1a38bcc..15a62d9b4 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -166,6 +166,26 @@ Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir))
166} 166}
167 167
168Loader::~Loader() { 168Loader::~Loader() {
169 // Importers must release their resources before their dependencies. Keep every mapping, unwind table and TLS
170 // block alive throughout the notifications: a detach handler can still call an imported function or free an
171 // FLS slot whose destructor lives in another mapped image.
172 for (auto it = init_order_.rbegin(); it != init_order_.rend(); ++it) {
173 Module& m = **it;
174 if (!m.initialised) continue; // a map-only load never opts in to running image code
175 m.initialised = false;
176 try {
177 run_tls_callbacks(m, DLL_PROCESS_DETACH);
178 } catch (const std::exception& e) {
179 // A module may have changed its callback table since attach. A destructor cannot propagate the validation
180 // error; report it and still let DllMain release resources before the image is freed.
181 std::fprintf(stderr, "[rt] TLS process detach failed for %s: %s\n", m.name.c_str(), e.what());
182 }
183 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
184 if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) {
185 auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva);
186 entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored
187 }
188 }
169 shims::remove_module_lookup(this); 189 shims::remove_module_lookup(this);
170 for (auto* m : order_) { 190 for (auto* m : order_) {
171 // TLS state must go away before the image and its TLS block are unmapped. 191 // TLS state must go away before the image and its TLS block are unmapped.
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h
index ebcf411ec..6d6c94751 100644
--- a/runtime/src/pe_loader.h
+++ b/runtime/src/pe_loader.h
@@ -1,8 +1,8 @@
1// Native PE mapper for Xbox Series (x86-64) images. 1// Native PE mapper for Xbox Series (x86-64) images.
2// Maps an image, applies relocations, binds every import through the shim registry, registers its unwind table and 2// Maps an image, applies relocations, binds every import through the shim registry, registers its unwind table and
3// installs static TLS for the calling thread. 3// installs static TLS for the calling thread.
4// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the module's TLS 4// Mapping never runs code from the image. Loader::initialise() delivers process attach to TLS callbacks and DllMain;
5// (slot, template and callbacks) and calls a DLL's entry point as DllMain. 5// ~Loader() delivers process detach before unmapping initialised modules. The TLS slot and template are set up by load().
6// gta6rt_tls_host.dll must sit next to the executable; the loader reuses the module TLS index the OS assigns it. 6// gta6rt_tls_host.dll must sit next to the executable; the loader reuses the module TLS index the OS assigns it.
7#pragma once 7#pragma once
8#include <cstdint> 8#include <cstdint>
@@ -65,8 +65,9 @@ public:
65 explicit Loader(std::filesystem::path game_dir); 65 explicit Loader(std::filesystem::path game_dir);
66 Loader(const Loader&) = delete; 66 Loader(const Loader&) = delete;
67 Loader& operator=(const Loader&) = delete; 67 Loader& operator=(const Loader&) = delete;
68 // Restores the TLS slot for the calling thread. Destroy the Loader on the thread 68 // Delivers process detach to initialised modules in reverse dependency order before freeing any mappings or TLS.
69 // that called load() while any mapped TLS module is still live. 69 // Restores the TLS slot for the calling thread. Destroy the Loader on the thread that called load() while any
70 // mapped TLS module is still live; all other threads executing mapped code must have finished first.
70 ~Loader(); 71 ~Loader();
71 72
72 // Loads an image and (recursively) the bundled modules it imports. Throws std::runtime_error on failure. 73 // Loads an image and (recursively) the bundled modules it imports. Throws std::runtime_error on failure.
@@ -86,7 +87,7 @@ public:
86 void initialise(); 87 void initialise();
87 88
88 // Calls the image's IMAGE_TLS_DIRECTORY64 callbacks in order with (base, reason, nullptr). This is opt-in: 89 // Calls the image's IMAGE_TLS_DIRECTORY64 callbacks in order with (base, reason, nullptr). This is opt-in:
89 // load() never runs image code, and initialise() is the only call inside the Loader that uses it. No-op for 90 // load() never runs image code; initialise() and ~Loader() use it for process attach/detach. No-op for
90 // images without TLS callbacks. The loader does not deliver thread attach/detach events: install the thread's 91 // images without TLS callbacks. The loader does not deliver thread attach/detach events: install the thread's
91 // TLS first, then call this with DLL_THREAD_ATTACH/DETACH, and uninstall after the detach callbacks. 92 // TLS first, then call this with DLL_THREAD_ATTACH/DETACH, and uninstall after the detach callbacks.
92 void run_tls_callbacks(Module& m, uint32_t reason); 93 void run_tls_callbacks(Module& m, uint32_t reason);