upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 49917bf18..6c4d0e641 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -44,6 +44,16 @@ const char* binding_name(rt::Binding b) {
44 } 44 }
45} 45}
46 46
47// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
48// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
49// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
50// hold no TLS slot address. The result is read again where an image is run.
51void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
52#pragma section(".CRT$XCT", read)
53extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
54 reserve_tls_index0_at_start;
55#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
56
47int usage() { 57int usage() {
48 std::fprintf(stderr, 58 std::fprintf(stderr,
49 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" 59 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
@@ -105,6 +115,20 @@ int wmain(int argc, wchar_t** argv) {
105 try { 115 try {
106 rt::Loader loader(game_dir); 116 rt::Loader loader(game_dir);
107 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 117 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
118 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
119 // is gta6rt's own (or another executable's), so no code of it may run.
120 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
121 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
122 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
123 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
124 std::string why;
125 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
126 std::fprintf(stderr,
127 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
128 "another module's TLS through slot 0, so it is not run\n",
129 main_module.name.c_str(), why.c_str());
130 return 1;
131 }
108 if (run) rt::process::set_command_line(image.filename().wstring(), game_args); 132 if (run) rt::process::set_command_line(image.filename().wstring(), game_args);
109 if (init || run) { 133 if (init || run) {
110 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs 134 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs