upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.h
diff options
context:
space:
mode:
authorKam471 <kam471@users.noreply.github.com>2026-11-21 22:19:03 +0000
committerKam471 <kam471@users.noreply.github.com>2026-11-21 23:29:24 +0000
commit993a2cbf72a9509ca730e509ea38e455d35f5a74 (patch)
treea3d6dc8481bfeabb7e5a94840ef4474504add23d /runtime/src/pe_loader.h
parentd7cbc3e7cb4d031c790dddd265a83fb6a1a1a7a5 (diff)
Runtime: install static TLS for mapped images
Diffstat (limited to 'runtime/src/pe_loader.h')
-rw-r--r--runtime/src/pe_loader.h49
1 files changed, 40 insertions, 9 deletions
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h
index 5f6d1b7a5..6946f1cc0 100644
--- a/runtime/src/pe_loader.h
+++ b/runtime/src/pe_loader.h
@@ -1,7 +1,9 @@
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 and registers its unwind table. 2// Maps an image, applies relocations, binds every import through the shim registry, registers its unwind table and
3// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the 3// installs static TLS for the calling thread.
4// module's TLS (slot, template and callbacks) and calls a DLL's entry point as DllMain. 4// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the module's TLS
5// (slot, template and callbacks) and calls a DLL's entry point as DllMain.
6// gta6rt_tls_host.dll must sit next to the executable; the loader reuses the module TLS index the OS assigns it.
5#pragma once 7#pragma once
6#include <cstdint> 8#include <cstdint>
7#include <filesystem> 9#include <filesystem>
@@ -13,6 +15,9 @@ namespace rt {
13 15
14enum class Binding { Shim, Host, Module, Unimplemented }; 16enum class Binding { Shim, Host, Module, Unimplemented };
15 17
18// No module TLS index has been assigned (matches TLS_OUT_OF_INDEXES without pulling <windows.h> into this header).
19inline constexpr uint32_t kNoTlsIndex = 0xFFFFFFFFu;
20
16struct ImportRecord { 21struct ImportRecord {
17 std::string dll; 22 std::string dll;
18 std::string name; // "#123" for ordinals 23 std::string name; // "#123" for ordinals
@@ -27,12 +32,6 @@ struct Module {
27 uint32_t size = 0; 32 uint32_t size = 0;
28 uint32_t entry_rva = 0; 33 uint32_t entry_rva = 0;
29 bool relocated = false; 34 bool relocated = false;
30
31 // Filled in by initialise(). The TLS slot and block are owned by the loader and released with it.
32 void* tls_block = nullptr;
33 uint64_t tls_block_size = 0;
34 unsigned tls_slot = 0;
35 bool has_tls_slot = false;
36 bool initialised = false; 35 bool initialised = false;
37 36
38 // The image's exception directory (.pdata), registered with RtlAddFunctionTable by load() so that exception 37 // The image's exception directory (.pdata), registered with RtlAddFunctionTable by load() so that exception
@@ -43,14 +42,31 @@ struct Module {
43 std::vector<ImportRecord> imports; 42 std::vector<ImportRecord> imports;
44 std::map<std::string, void*> exports; // by name, plus "#ordinal" 43 std::map<std::string, void*> exports; // by name, plus "#ordinal"
45 std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports 44 std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports
45
46 // Static TLS (IMAGE_DIRECTORY_ENTRY_TLS). load() installs tls_block in the calling thread and writes tls_index to
47 // the image's _tls_index. tls_index names TEB->ThreadLocalStoragePointer[tls_index], the array
48 // __declspec(thread) reads; it is not a TlsAlloc slot. The loader reserves the index by loading
49 // gta6rt_tls_host.dll, so only one manually mapped TLS module can be live at a time; a second one is rejected
50 // rather than sharing (and corrupting) the slot. install_tls_for_thread() must be called for every other thread
51 // the runtime creates.
52 uint32_t tls_index = kNoTlsIndex;
53 void* tls_block = nullptr; // calling thread's copy of the image's template
54 void* tls_saved_block = nullptr; // helper DLL block replaced in that thread, restored by ~Loader
55 uint32_t tls_block_size = 0; // raw template + zero fill
56 const uint8_t* tls_template = nullptr; // raw template in the mapped image
57 uint32_t tls_raw_size = 0;
58 uint32_t tls_zero_fill = 0;
46}; 59};
47 60
48class Loader { 61class Loader {
49public: 62public:
50 explicit Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) {} 63 explicit Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) {}
64 // Restores the TLS slot for the calling thread. Destroy the Loader on the thread
65 // that called load() while any mapped TLS module is still live.
51 ~Loader(); 66 ~Loader();
52 67
53 // Loads an image and (recursively) the bundled modules it imports. Throws std::runtime_error on failure. 68 // Loads an image and (recursively) the bundled modules it imports. Throws std::runtime_error on failure.
69 // Static TLS is installed here, before any image code can run; initialise() does not install it again.
54 Module& load(const std::filesystem::path& image); 70 Module& load(const std::filesystem::path& image);
55 71
56 const std::vector<Module*>& modules() const { return order_; } 72 const std::vector<Module*>& modules() const { return order_; }
@@ -62,11 +78,26 @@ public:
62 // Runs each loaded module's TLS callbacks and then its DllMain(DLL_PROCESS_ATTACH), dependencies first. 78 // Runs each loaded module's TLS callbacks and then its DllMain(DLL_PROCESS_ATTACH), dependencies first.
63 // Only DLLs get DllMain called: an EXE's entry point is the caller's to run. Throws on a corrupt TLS 79 // Only DLLs get DllMain called: an EXE's entry point is the caller's to run. Throws on a corrupt TLS
64 // directory, a TLS callback outside its image, or a DllMain that returns FALSE. 80 // directory, a TLS callback outside its image, or a DllMain that returns FALSE.
81 // TLS was already installed by load(), so this is safe to call on the loading thread without a second install.
65 void initialise(); 82 void initialise();
66 83
84 // Calls the image's IMAGE_TLS_DIRECTORY64 callbacks in order with (base, reason, nullptr). This is opt-in:
85 // load() never runs image code, and initialise() is the only call inside the Loader that uses it. No-op for
86 // images without TLS callbacks. The loader does not deliver thread attach/detach events: install the thread's
87 // TLS first, then call this with DLL_THREAD_ATTACH/DETACH, and uninstall after the detach callbacks.
88 void run_tls_callbacks(Module& m, uint32_t reason);
89
90 // Installs/removes this module's template copy in the calling thread's TEB slot. load() does this for its caller;
91 // the runtime must call install before mapped code runs on any other thread and uninstall before that thread
92 // exits. Only one TLS module can be installed in a thread. These do not run TLS callbacks (use run_tls_callbacks
93 // with DLL_THREAD_ATTACH/DETACH for that).
94 void install_tls_for_thread(Module& m);
95 void uninstall_tls_for_thread(Module& m);
96
67private: 97private:
68 void initialise(Module& m); 98 void initialise(Module& m);
69 Module& map_image(const std::filesystem::path& path); 99 Module& map_image(const std::filesystem::path& path);
100 void initialize_tls(Module& m);
70 void bind_imports(Module& m); 101 void bind_imports(Module& m);
71 void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal); 102 void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal);
72 void* resolve_forwarder(const std::string& spec, Binding& binding, int depth); 103 void* resolve_forwarder(const std::string& spec, Binding& binding, int depth);