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-21 14:40:15 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-21 14:40:15 -0500
commit8bf99edc7a437f13e01fa4bc8b22b49b473e569c (patch)
tree4da9c23f1b292b04922fd7a28023720a40fd7074 /runtime/src
parentd279b288684060cdfd486a841fbd185ac37d865b (diff)
parent126062ae4a8831a564b9e3042684657c58781bef (diff)
Merge pull request #95 from Ayinaki
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/main.cpp20
-rw-r--r--runtime/src/pe_loader.cpp84
-rw-r--r--runtime/src/pe_loader.h16
3 files changed, 115 insertions, 5 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index e69906c14..4477c4a69 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -3,6 +3,9 @@
3// gta6rt --dry-run <image> [--game-dir DIR] 3// gta6rt --dry-run <image> [--game-dir DIR]
4// Maps the image and its bundled modules, applies relocations, binds every import and prints a report. 4// Maps the image and its bundled modules, applies relocations, binds every import and prints a report.
5// No code from the image runs. 5// No code from the image runs.
6//
7// gta6rt --init <image> [--game-dir DIR]
8// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
6#include <windows.h> 9#include <windows.h>
7 10
8#include <cstdio> 11#include <cstdio>
@@ -23,7 +26,9 @@ const char* binding_name(rt::Binding b) {
23} 26}
24 27
25int usage() { 28int usage() {
26 std::fprintf(stderr, "usage: gta6rt --dry-run <image> [--game-dir DIR]\n"); 29 std::fprintf(stderr,
30 "usage: gta6rt --dry-run <image> [--game-dir DIR]\n"
31 " gta6rt --init <image> [--game-dir DIR]\n");
27 return 2; 32 return 2;
28} 33}
29 34
@@ -31,17 +36,20 @@ int usage() {
31 36
32int wmain(int argc, wchar_t** argv) { 37int wmain(int argc, wchar_t** argv) {
33 std::filesystem::path image, game_dir; 38 std::filesystem::path image, game_dir;
34 bool dry_run = false; 39 bool dry_run = false, init = false;
35 for (int i = 1; i < argc; ++i) { 40 for (int i = 1; i < argc; ++i) {
36 std::wstring a = argv[i]; 41 std::wstring a = argv[i];
37 if (a == L"--dry-run") dry_run = true; 42 if (a == L"--dry-run") dry_run = true;
43 else if (a == L"--init") init = true;
38 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 44 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
39 else if (image.empty()) image = a; 45 else if (image.empty()) image = a;
40 else return usage(); 46 else return usage();
41 } 47 }
42 if (image.empty()) return usage(); 48 if (image.empty()) return usage();
43 if (!dry_run) { 49 if (!dry_run && !init) {
44 std::fprintf(stderr, "Only --dry-run exists so far: running game code needs the shims listed by it first.\n"); 50 std::fprintf(stderr,
51 "Only --dry-run and --init exist so far: running game code needs the shims listed by "
52 "--dry-run first.\n");
45 return 2; 53 return 2;
46 } 54 }
47 if (game_dir.empty()) game_dir = image.parent_path(); 55 if (game_dir.empty()) game_dir = image.parent_path();
@@ -49,6 +57,10 @@ int wmain(int argc, wchar_t** argv) {
49 try { 57 try {
50 rt::Loader loader(game_dir); 58 rt::Loader loader(game_dir);
51 loader.load(image); 59 loader.load(image);
60 if (init) {
61 loader.initialise();
62 std::printf("%zu module(s) initialised\n", loader.modules().size());
63 }
52 int unimplemented = 0; 64 int unimplemented = 0;
53 for (auto* m : loader.modules()) { 65 for (auto* m : loader.modules()) {
54 std::map<rt::Binding, int> counts; 66 std::map<rt::Binding, int> counts;
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index e6a9501ab..f26527989 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -56,6 +56,8 @@ bool prefer_host_copy(const std::string& dll) {
56 56
57Loader::~Loader() { 57Loader::~Loader() {
58 for (auto* m : order_) { 58 for (auto* m : order_) {
59 if (m->tls_block) VirtualFree(m->tls_block, 0, MEM_RELEASE);
60 if (m->has_tls_slot) TlsFree(m->tls_slot);
59 if (m->base) VirtualFree(m->base, 0, MEM_RELEASE); 61 if (m->base) VirtualFree(m->base, 0, MEM_RELEASE);
60 delete m; 62 delete m;
61 } 63 }
@@ -70,6 +72,88 @@ Module& Loader::load(const std::filesystem::path& image) {
70 return m; 72 return m;
71} 73}
72 74
75void Loader::initialise() {
76 // order_ is dependency order: a module is pushed after the modules it imports.
77 for (auto* m : order_) initialise(*m);
78}
79
80void Loader::initialise(Module& m) {
81 if (m.initialised) return;
82 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
83 const uint64_t base = reinterpret_cast<uint64_t>(m.base);
84
85 // The TLS directory holds virtual addresses, which relocations have already moved to this mapping.
86 auto rva_of = [&](uint64_t va, const char* what) {
87 if (va < base || va - base >= m.size)
88 throw std::runtime_error(m.name + ": " + what + " is outside the image");
89 return va - base;
90 };
91
92 auto& tls_dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
93 if (tls_dir.Size) {
94 require_range(m.name, tls_dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory");
95 auto* tls = at<IMAGE_TLS_DIRECTORY64>(m.base, tls_dir.VirtualAddress);
96
97 uint64_t start = 0, template_size = 0;
98 if (tls->StartAddressOfRawData || tls->EndAddressOfRawData) {
99 start = rva_of(tls->StartAddressOfRawData, "the TLS template");
100 uint64_t end = rva_of(tls->EndAddressOfRawData, "the end of the TLS template");
101 if (end < start) throw std::runtime_error(m.name + ": the TLS template ends before it starts");
102 template_size = end - start;
103 require_range(m.name, start, template_size, m.size, "the TLS template");
104 }
105 m.tls_block_size = template_size + tls->SizeOfZeroFill;
106 if (m.tls_block_size > (1u << 20)) throw std::runtime_error(m.name + ": the TLS block is larger than 1 MiB");
107 if (m.tls_block_size) {
108 m.tls_block = VirtualAlloc(nullptr, static_cast<SIZE_T>(m.tls_block_size), MEM_RESERVE | MEM_COMMIT,
109 PAGE_READWRITE);
110 if (!m.tls_block) throw std::runtime_error(m.name + ": cannot allocate the TLS block");
111 if (template_size) std::memcpy(m.tls_block, m.base + start, static_cast<size_t>(template_size));
112 }
113
114 if (tls->AddressOfIndex) {
115 uint64_t index_rva = rva_of(tls->AddressOfIndex, "the TLS index");
116 require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index");
117 DWORD slot = TlsAlloc();
118 if (slot == TLS_OUT_OF_INDEXES) throw std::runtime_error(m.name + ": no TLS slot is free");
119 m.tls_slot = slot;
120 m.has_tls_slot = true;
121 if (m.tls_block) TlsSetValue(slot, m.tls_block);
122 // The index lives in a writable section, but protect_sections already ran, so lift it for the write.
123 auto* index = m.base + index_rva;
124 auto* page = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(index) & ~uintptr_t(0xFFF));
125 DWORD old = 0, ignored = 0;
126 if (!VirtualProtect(page, 0x1000, PAGE_READWRITE, &old))
127 throw std::runtime_error(m.name + ": the TLS index can't be written");
128 *reinterpret_cast<uint32_t*>(index) = slot;
129 VirtualProtect(page, 0x1000, old, &ignored);
130 }
131
132 if (tls->AddressOfCallBacks) {
133 uint64_t table_rva = rva_of(tls->AddressOfCallBacks, "the TLS callback table");
134 for (uint64_t i = 0;; ++i) {
135 uint64_t entry_rva = table_rva + i * sizeof(uint64_t);
136 require_range(m.name, entry_rva, sizeof(uint64_t), m.size, "a TLS callback table entry");
137 uint64_t fn = *at<uint64_t>(m.base, static_cast<uint32_t>(entry_rva));
138 if (!fn) break;
139 auto callback = reinterpret_cast<void(NTAPI*)(void*, DWORD, void*)>(m.base + rva_of(fn, "a TLS callback"));
140 callback(m.base, DLL_PROCESS_ATTACH, nullptr);
141 }
142 }
143 }
144
145 // Only a DLL gets DllMain. An EXE's entry point has a different signature and is the caller's to run.
146 if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) {
147 if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image");
148 auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva);
149 if (!entry(m.base, DLL_PROCESS_ATTACH, nullptr)) {
150 m.initialised = true; // it ran: a second DLL_PROCESS_ATTACH would be wrong
151 throw std::runtime_error(m.name + ": DllMain returned FALSE");
152 }
153 }
154 m.initialised = true;
155}
156
73Module& Loader::map_image(const std::filesystem::path& path) { 157Module& Loader::map_image(const std::filesystem::path& path) {
74 auto file = read_file(path); 158 auto file = read_file(path);
75 const std::string name = path.filename().string(); 159 const std::string name = path.filename().string();
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h
index 5d3218f81..e6e7c07c7 100644
--- a/runtime/src/pe_loader.h
+++ b/runtime/src/pe_loader.h
@@ -1,6 +1,7 @@
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 and binds every import through the shim registry. 2// Maps an image, applies relocations and binds every import through the shim registry.
3// It never runs code from the image: DllMain, TLS callbacks and the entry point are left to the caller. 3// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the
4// module's TLS (slot, template and callbacks) and calls a DLL's entry point as DllMain.
4#pragma once 5#pragma once
5#include <cstdint> 6#include <cstdint>
6#include <filesystem> 7#include <filesystem>
@@ -26,6 +27,13 @@ struct Module {
26 uint32_t size = 0; 27 uint32_t size = 0;
27 uint32_t entry_rva = 0; 28 uint32_t entry_rva = 0;
28 bool relocated = false; 29 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;
29 std::vector<ImportRecord> imports; 37 std::vector<ImportRecord> imports;
30 std::map<std::string, void*> exports; // by name, plus "#ordinal" 38 std::map<std::string, void*> exports; // by name, plus "#ordinal"
31}; 39};
@@ -40,7 +48,13 @@ public:
40 48
41 const std::vector<Module*>& modules() const { return order_; } 49 const std::vector<Module*>& modules() const { return order_; }
42 50
51 // Runs each loaded module's TLS callbacks and then its DllMain(DLL_PROCESS_ATTACH), dependencies first.
52 // Only DLLs get DllMain called: an EXE's entry point is the caller's to run. Throws on a corrupt TLS
53 // directory, a TLS callback outside its image, or a DllMain that returns FALSE.
54 void initialise();
55
43private: 56private:
57 void initialise(Module& m);
44 Module& map_image(const std::filesystem::path& path); 58 Module& map_image(const std::filesystem::path& path);
45 void bind_imports(Module& m); 59 void bind_imports(Module& m);
46 void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal); 60 void* resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal);