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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:44:23 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:44:23 -0500
commitb8563e949ed389fff20be91c73687a70b5213469 (patch)
treed69c6817854ce0a14a67ff1fd0f1c39dec68a91f /runtime/src/main.cpp
parent00fc23b9a9eeb1a75f1fec2b58024bea15c26663 (diff)
parent7ae1dd3de34f1542242bc8ccfc80d9b97d20cc01 (diff)
Merge main into D3D-448-GRAPHICS (#974)
main already lays graphics root arguments out in the console layout (from s8, one dword per table: #939's graphics-root-layout patch in #984's form, pack_stage(Stage::Pixel), RootArgumentLayout::graphics_console, graphics_arguments_end, LEONIDA_GRAPHICS_STAGE_LAYOUT=0 for pack()). That form stays. What this pull request adds beyond it is carried onto it: the mesh-shader root window (translator, index "root_window", mesh_ok, the two refusals) and its tests. Its own graphics spill handling (Spill::block) and LEONIDA_GRAPHICS_ROOT_LAYOUT=pc are not carried: main keeps pack()'s s0 layout for a graphics signature past s31, and its switch does what =pc did. Conflicts: - runtime/src/root_signature.h, root_signature.cpp: main's pack_stage (compute spill array, spill_dword); the Spill enum, first_spilled and spilled_dwords dropped. The header keeps the PR's evidence note, with Stage::Pixel. - runtime/shims/d3d12_com.h: main's comment and members, with the mesh window described; mesh_ok added (false by default, set only for a console-layout signature that Stage::Mesh also places); the PR's second graphics_console dropped. - runtime/shims/d3d12_vulkan_device.cpp (CreateRootSignature): main's pixel/packed form; the PR's Stage::Mesh pack and its log line added, mesh_ok = pixel.ok && mesh.ok. The mesh-shader refusal without "root_window" (no conflict) stays. - runtime/shims/d3d12_com.cpp: DispatchMesh keeps main's any_nonempty_viewport() and graphics_user_data() after the PR's mesh_ok check; address() keeps main's comment. The PR's kNoSgpr guard in constants()/address() stays, reworded: in main a spilled compute parameter has first_sgpr kNoSgpr, and kNoSgpr + offset wrapped round to s0. - runtime/src/shader_library.h: both (main's wave64_graphics, the PR's root_window). - runtime/test/d3d12_mesh_test.cpp: main's mesh shader-kind byte on both containers (also the PR's ms_old); the PR's two-parameter signature replaces main's 25-dword padding onto the s0 layout; both test blocks kept (the PR's window refusals, main's DISPATCH_MESH command signature), each in its own scope. The =pc case is dropped (main's switch is read once per process). - runtime/CMakeLists.txt (no conflict): the mesh test uses ps_draw.spv again; the ps_draw_s0.spv rule is gone. - runtime/test/glsl/ps_draw.frag, vs_depth.vert, vs_draw.vert, vs_draw_int.vert, ps_depth_write.frag and runtime/test/shaders/ps_m15, ps_m2, ps_m2_array, ps_m2_literal, ps_m2_uoff, ps_m3, ps_m3_post, ps_m3_query, vs_m15, vs_m3 (.asm): main's (the same s8 rewrite done twice); ps_draw.frag's mesh-test note updated. - runtime/test/root_signature_test.cpp (no conflict): the Spill::block cases dropped, the window cases kept. - docs/design.md: main's rows (loop guard, direct resource heap) and the PR's user-data row. - docs/d3d12_vulkan.md (no conflict): section 3.5 and the CreateRootSignature bullet rewritten for main's form. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp215
1 files changed, 212 insertions, 3 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 098a8ee40..8e3767740 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -18,6 +18,7 @@
18// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp). 18// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp).
19#include <windows.h> 19#include <windows.h>
20 20
21#include <cstdint>
21#include <cstdio> 22#include <cstdio>
22#include <cstdlib> 23#include <cstdlib>
23#include <exception> 24#include <exception>
@@ -27,15 +28,113 @@
27#include <system_error> 28#include <system_error>
28#include <vector> 29#include <vector>
29 30
31#include <cstring>
32
33namespace {
34
35// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop
36// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to
37// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than
38// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the
39// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and
40// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a
41// second so the title's watchdog never fires.
42constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35;
43constexpr std::uint32_t kWatchdogKickRva = 0x71b6331;
44constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
45
46void keep_title_watchdog_kicked(std::uint8_t* base) {
47 char value[4]{};
48 if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return;
49 MEMORY_BASIC_INFORMATION trap{}, kick{};
50 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
51 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
52 return; // not the build this was measured on: leave the title's watchdog alone
53 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
54 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
55 !(kick.Protect & writable))
56 return;
57 volatile std::uint8_t* const byte = base + kWatchdogKickRva;
58 const HANDLE thread = CreateThread(
59 nullptr, 0,
60 [](LPVOID p) -> DWORD {
61 auto* b = static_cast<volatile std::uint8_t*>(p);
62 for (;;) {
63 *b = 1;
64 Sleep(500);
65 }
66 },
67 const_cast<std::uint8_t*>(byte), 0, nullptr);
68 if (thread) {
69 CloseHandle(thread);
70 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=0; unset or 1 keeps it armed)\n");
71 }
72}
73
74// LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the
75// title's image (":n" = n consecutive values) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
76// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
77// that. Reads are checked; nothing is written.
78void start_title_peek(std::uint8_t* base) {
79 static char spec[1024];
80 const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec));
81 if (!n || n >= sizeof(spec)) return;
82 static std::uint8_t* image = base;
83 const HANDLE thread = CreateThread(
84 nullptr, 0,
85 [](LPVOID) -> DWORD {
86 char seconds[16]{};
87 DWORD period = 10;
88 if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0)
89 period = std::max<DWORD>(1, static_cast<DWORD>(std::strtoul(seconds, nullptr, 10)));
90 for (unsigned tick = 0;; ++tick) {
91 std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:";
92 for (const char* at = spec; *at;) {
93 char* end = nullptr;
94 const std::uint64_t rva = std::strtoull(at, &end, 16);
95 if (end == at) break;
96 unsigned count = 1;
97 if (*end == ':') count = static_cast<unsigned>(std::strtoul(end + 1, &end, 10));
98 char text[40];
99 std::snprintf(text, sizeof(text), " 0x%llx=", static_cast<unsigned long long>(rva));
100 line += text;
101 for (unsigned i = 0; i < count && i < 16; ++i) {
102 std::uint64_t value = 0;
103 SIZE_T got = 0;
104 const bool ok = ReadProcessMemory(GetCurrentProcess(), image + rva + 8ull * i, &value,
105 sizeof(value), &got) && got == sizeof(value);
106 std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "",
107 static_cast<unsigned long long>(value));
108 line += text;
109 }
110 at = *end == ',' ? end + 1 : end;
111 if (at == end && *end) break;
112 }
113 std::fprintf(stderr, "%s\n", line.c_str());
114 Sleep(period * 1000);
115 }
116 },
117 nullptr, 0, nullptr);
118 if (thread) CloseHandle(thread);
119}
120
121} // namespace
122
30#include "base_reservation.h" 123#include "base_reservation.h"
31#include "code_hooks.h" 124#include "code_hooks.h"
32#include "cpu_emulation.h" 125#include "cpu_emulation.h"
33#include "host_cpu.h" 126#include "host_cpu.h"
34#include "mods.h" 127#include "mods.h"
35#include "pe_loader.h" 128#include "pe_loader.h"
129#include "thread_stacks.h"
130#include "title_page_watch.h"
131#include "title_geometry_watch.h"
36#include "../shims/d3d12_trace.h" 132#include "../shims/d3d12_trace.h"
37#include "../shims/d3d12_xs_device.h" 133#include "../shims/d3d12_xs_device.h"
38#include "../shims/file_system.h" 134#include "../shims/file_system.h"
135#ifdef LEONIDA_VULKAN_DEVICE
136#include "../shims/package_splash.h"
137#endif
39#include "../shims/process_shim.h" 138#include "../shims/process_shim.h"
40#include "../shims/shims.h" 139#include "../shims/shims.h"
41#include "../shims/startup_trace.h" 140#include "../shims/startup_trace.h"
@@ -52,22 +151,76 @@ const char* binding_name(rt::Binding b) {
52 } 151 }
53} 152}
54 153
154// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
155// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
156// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
157// hold no TLS slot address. The result is read again where an image is run.
158void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
159#pragma section(".CRT$XCT", read)
160extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
161 reserve_tls_index0_at_start;
162#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
163
164// Select the settings used to display the offline first-run UI. Keep explicit caller settings.
165bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) {
166 // A variable that is set but empty is not a setting: Windows cannot hold one, but a caller on Wine can pass one
167 // (NAME= in the environment), and asking for the size alone reports it as present there.
168 const auto set_default = [](const wchar_t* name, const wchar_t* value) {
169 wchar_t first[2]{};
170 return GetEnvironmentVariableW(name, first, 2) != 0 || _wputenv_s(name, value) == 0;
171 };
172 const struct { const wchar_t* name; const wchar_t* value; } settings[] = {
173 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
174 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
175 {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"},
176 {L"LEONIDA_KEYBOARD_PAD", L"always"},
177 };
178 for (const auto& setting : settings) {
179 if (!set_default(setting.name, setting.value)) return false;
180 }
181 std::wstring executable(32768, L'\0');
182 const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size()));
183 if (!length || length >= executable.size()) return false;
184 executable.resize(length);
185 const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui";
186 if (overlay.empty()) overlay = state / L"overlay";
187 if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) {
188 std::error_code error;
189 const auto backing = state / L"xmem";
190 std::filesystem::create_directories(backing, error);
191 if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false;
192 }
193 return SetCurrentDirectoryW(package.c_str()) != FALSE;
194}
195
55int usage() { 196int usage() {
56 std::fprintf(stderr, 197 std::fprintf(stderr,
57 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 198 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
58 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" 199 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
200 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
59 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 201 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n");
60 return 2; 202 return 2;
61} 203}
62 204
63} // namespace 205} // namespace
64 206
207#ifdef LEONIDA_VULKAN_DEVICE
208static BOOL WINAPI show_package_window(HWND window, int command) {
209 const BOOL previous = ShowWindow(window, command);
210 const DWORD error = GetLastError();
211 if (command != SW_HIDE && rt::package_splash::paint_window(window))
212 rt::startup_trace::event("package_splash_window");
213 SetLastError(error);
214 return previous;
215}
216#endif
217
65int wmain(int argc, wchar_t** argv) { 218int wmain(int argc, wchar_t** argv) {
66 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 219 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
67 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 220 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
68 int archetype_lookup_arg = 0; 221 int archetype_lookup_arg = 0;
69 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context 222 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
70 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 223 bool dry_run = false, init = false, run = false, trace_d3d12 = false, first_run_ui = false;
71 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 224 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
72 std::wstring trace_file; 225 std::wstring trace_file;
73 for (int i = 1; i < argc; ++i) { 226 for (int i = 1; i < argc; ++i) {
@@ -78,6 +231,7 @@ int wmain(int argc, wchar_t** argv) {
78 } 231 }
79 if (a == L"--dry-run") dry_run = true; 232 if (a == L"--dry-run") dry_run = true;
80 else if (a == L"--init") init = true; 233 else if (a == L"--init") init = true;
234 else if (a == L"--first-run-ui") first_run_ui = true;
81 else if (a == L"--run") run = true; 235 else if (a == L"--run") run = true;
82 else if (a == L"--trace-d3d12") trace_d3d12 = true; 236 else if (a == L"--trace-d3d12") trace_d3d12 = true;
83 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 237 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
@@ -104,7 +258,8 @@ int wmain(int argc, wchar_t** argv) {
104 } 258 }
105 if (image.empty()) return usage(); 259 if (image.empty()) return usage();
106 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 260 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point.
107 if (dry_run + init + run != 1) return usage(); 261 if (dry_run + init + run + first_run_ui != 1) return usage();
262 run = run || first_run_ui;
108 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). 263 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h).
109 if ((init || run) && !rt::host_cpu::check_skipped()) 264 if ((init || run) && !rt::host_cpu::check_skipped())
110 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { 265 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
@@ -119,7 +274,21 @@ int wmain(int argc, wchar_t** argv) {
119 std::filesystem::path a = std::filesystem::absolute(p, ec); 274 std::filesystem::path a = std::filesystem::absolute(p, ec);
120 return ec ? p : a; 275 return ec ? p : a;
121 }; 276 };
277 if (first_run_ui) {
278 image = absolute_or_same(image);
279 if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir);
280 if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring();
281 }
122 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 282 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
283 // An unattended run can select a fresh write root even when its launcher supplies --overlay.
284 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) {
285 std::wstring dir(size, 0);
286 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size);
287 if (n && n < size) {
288 dir.resize(n);
289 overlay_dir = dir;
290 }
291 }
123 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); 292 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir);
124 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. 293 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved.
125 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { 294 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) {
@@ -135,8 +304,24 @@ int wmain(int argc, wchar_t** argv) {
135 if (!rt::base_reservation::inherited()) 304 if (!rt::base_reservation::inherited())
136 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); 305 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range);
137 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 306 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
307 if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) {
308 std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n");
309 return 1;
310 }
138 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 311 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
139 rt::fs::configure(game_dir, overlay_dir); 312 rt::fs::configure(game_dir, overlay_dir);
313#ifdef LEONIDA_VULKAN_DEVICE
314 if (run) {
315 rt::package_splash::configure(game_dir);
316 if (rt::package_splash::configured()) {
317 const rt::shims::Entry window_imports[] = {
318 {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
319 {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
320 };
321 rt::shims::Table{window_imports, std::size(window_imports)};
322 }
323 }
324#endif
140 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; 325 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here;
141 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. 326 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version.
142 if (mods_dir.empty()) { 327 if (mods_dir.empty()) {
@@ -168,6 +353,10 @@ int wmain(int argc, wchar_t** argv) {
168 rt::d3d12_trace::set_enabled(trace_d3d12); 353 rt::d3d12_trace::set_enabled(trace_d3d12);
169 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. 354 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first.
170 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); 355 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
356 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
357 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
358 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
359 rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
171 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 360 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
172 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 361 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
173 : "error: cannot create the trace file\n"); 362 : "error: cannot create the trace file\n");
@@ -186,6 +375,20 @@ int wmain(int argc, wchar_t** argv) {
186 try { 375 try {
187 rt::Loader loader(game_dir); 376 rt::Loader loader(game_dir);
188 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 377 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
378 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
379 // is gta6rt's own (or another executable's), so no code of it may run.
380 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
381 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
382 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
383 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
384 std::string why;
385 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
386 std::fprintf(stderr,
387 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
388 "another module's TLS through slot 0, so it is not run\n",
389 main_module.name.c_str(), why.c_str());
390 return 1;
391 }
189 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA 392 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
190 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. 393 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
191 if (view_mode_getter_rva) { 394 if (view_mode_getter_rva) {
@@ -272,7 +475,13 @@ int wmain(int argc, wchar_t** argv) {
272 } 475 }
273 std::printf("running %s\n", main_module.name.c_str()); 476 std::printf("running %s\n", main_module.name.c_str());
274 std::fflush(stdout); 477 std::fflush(stdout);
275 const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); 478 keep_title_watchdog_kicked(main_module.base);
479 start_title_peek(main_module.base);
480 int code;
481 {
482 auto stacks = rt::start_thread_stacks();
483 code = rt::process::run_entry(main_module.base + main_module.entry_rva);
484 }
276 std::fflush(stdout); 485 std::fflush(stdout);
277 std::printf("game exited with code %d\n", code); 486 std::printf("game exited with code %d\n", code);
278 rt::shims::report_called_unimplemented(); 487 rt::shims::report_called_unimplemented();