diff options
| author | MoonEyes <ducoinbenjamin@yahoo.fr> | 2026-11-28 18:51:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 18:51:52 +0100 |
| commit | cd124a402ecff17e4da6f7e35942d261d55401b0 (patch) | |
| tree | e47278c6f13ea86d3692db38686a0706daa1ab86 /runtime/src/pe_loader.cpp | |
| parent | 4fd360e1ca51cfda629a6f0fa0fc597e463afef3 (diff) | |
| parent | 1a49f137576cc2051a0ee49f3f3731506054e1ce (diff) | |
Merge pull request #2499 from Tyrannys/rt-loader-runtime-scope-and-tls-slot
loader: wide run-time loads are dynamic; set tls_ga_slot before publishing (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT)
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 5e89ac2ab..d6ac0e1a4 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1526,6 +1526,8 @@ void* Loader::load_runtime_module_w(const std::wstring& basename, uint32_t flags | |||
| 1526 | if (prefer_host_copy(narrow)) return nullptr; | 1526 | if (prefer_host_copy(narrow)) return nullptr; |
| 1527 | 1527 | ||
| 1528 | std::lock_guard lock(mutex_); | 1528 | std::lock_guard lock(mutex_); |
| 1529 | // As load_runtime_module: a run-time load, so DllMain gets a null lpvReserved. | ||
| 1530 | const RuntimeLoadScope dynamic(runtime_loads_); | ||
| 1529 | Module* existing = find_wide_module(basename); | 1531 | Module* existing = find_wide_module(basename); |
| 1530 | const std::filesystem::path candidate = game_dir_ / basename; | 1532 | const std::filesystem::path candidate = game_dir_ / basename; |
| 1531 | std::error_code ec; | 1533 | std::error_code ec; |
| @@ -1912,6 +1914,25 @@ void Loader::initialize_tls(Module& m) { | |||
| 1912 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); | 1914 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); |
| 1913 | } | 1915 | } |
| 1914 | 1916 | ||
| 1917 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1918 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included, and before the owner is | ||
| 1919 | // published below: from then on a thread event (install_thread_block) reads m.tls_ga_slot | ||
| 1920 | // (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT). | ||
| 1921 | if (executable && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1922 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1923 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1924 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1925 | m.name.c_str()); | ||
| 1926 | } else { | ||
| 1927 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1928 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1929 | std::fprintf(stderr, | ||
| 1930 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1931 | "its module TLS index is %u\n", | ||
| 1932 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1933 | } | ||
| 1934 | } | ||
| 1935 | |||
| 1915 | // The fields are complete before a thread event can see the owner. | 1936 | // The fields are complete before a thread event can see the owner. |
| 1916 | ExclusiveTlsLock lock; | 1937 | ExclusiveTlsLock lock; |
| 1917 | Module* expected = nullptr; | 1938 | Module* expected = nullptr; |
| @@ -1930,26 +1951,6 @@ void Loader::initialize_tls(Module& m) { | |||
| 1930 | // VirtualProtect dance. | 1951 | // VirtualProtect dance. |
| 1931 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; | 1952 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; |
| 1932 | } | 1953 | } |
| 1933 | { | ||
| 1934 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1935 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included. | ||
| 1936 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 1937 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 1938 | if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1939 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1940 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1941 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1942 | m.name.c_str()); | ||
| 1943 | } else { | ||
| 1944 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1945 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1946 | std::fprintf(stderr, | ||
| 1947 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1948 | "its module TLS index is %u\n", | ||
| 1949 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1950 | } | ||
| 1951 | } | ||
| 1952 | } | ||
| 1953 | try { | 1954 | try { |
| 1954 | install_thread_block(m); | 1955 | install_thread_block(m); |
| 1955 | } catch (...) { | 1956 | } catch (...) { |