diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 15:34:37 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-23 15:34:37 -0500 |
| commit | b7a3fed28d53bb8c53b74eeff2ab65165a0d1c92 (patch) | |
| tree | 88b5c7b70af7a793feb37091873c9a26e1ac8a33 /runtime/src/pe_loader.h | |
| parent | 867d922f09b86583859796a174df89555fb32da8 (diff) | |
pe_loader: give a /GA executable its own TLS at index 0 (retail boot AV at exe+0x104075)
The retail game_scarlett_master.exe is built with /GA: it reads its TLS as
ThreadLocalStoragePointer[0] directly, without _tls_index, because the process
EXE always owns index 0. Mapped into gta6rt, index 0 belongs to gta6rt.exe, so
the game's allocator read gta6rt's TLS block and faulted at exe+0x104075.
load() now rewrites each `mov r64, gs:[58h]` in the EXE's executable sections
(53,246 sites in the retail exe) to `mov r64, gs:[1480h + 8 * slot]`, reading a
TlsAlloc slot (TEB->TlsSlots). Each thread's value there is a small view with
the image's TLS block at [0] and at [tls_index]. The rewrite is the same length
and runs before any of the image's code, TLS callbacks included. DLLs and
images whose index is already 0 are left alone; LEONIDA_TLS_GA_PATCH=0 turns it
off.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.h')
| -rw-r--r-- | runtime/src/pe_loader.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index 307837351..3f71a38a7 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h | |||
| @@ -66,6 +66,12 @@ struct Module { | |||
| 66 | const uint8_t* tls_template = nullptr; // raw template in the mapped image | 66 | const uint8_t* tls_template = nullptr; // raw template in the mapped image |
| 67 | uint32_t tls_raw_size = 0; | 67 | uint32_t tls_raw_size = 0; |
| 68 | uint32_t tls_zero_fill = 0; | 68 | uint32_t tls_zero_fill = 0; |
| 69 | // An EXE built with /GA reads its own TLS as ThreadLocalStoragePointer[0] without _tls_index: the process EXE | ||
| 70 | // always has index 0. Mapped into gta6rt, index 0 belongs to gta6rt.exe, so load() rewrites each such read | ||
| 71 | // (`mov r64, gs:[58h]`) to `mov r64, gs:[1480h + 8 * tls_ga_slot]`, a TlsAlloc slot (TEB->TlsSlots) whose value in | ||
| 72 | // each thread is a small array with this image's block at [0] and at [tls_index]. kNoTlsIndex when not patched. | ||
| 73 | uint32_t tls_ga_slot = kNoTlsIndex; | ||
| 74 | size_t tls_ga_sites = 0; | ||
| 69 | }; | 75 | }; |
| 70 | 76 | ||
| 71 | class Loader { | 77 | class Loader { |