diff options
| author | MoonEyes <14891700+MoonEyes@users.noreply.github.com> | 2026-11-28 20:42:44 +0100 |
|---|---|---|
| committer | MoonEyes <14891700+MoonEyes@users.noreply.github.com> | 2026-11-28 20:42:44 +0100 |
| commit | 3c108a33959e82a143f7a6c055f0c20f876c9842 (patch) | |
| tree | 063194b3f8b02994d47a5c411b513db4c21ae51f /runtime/src/pe_loader.cpp | |
| parent | b5447692f28361a4a0a0ad6c774dc656d9232418 (diff) | |
| parent | 32deb48c1a69f11dd07275fc9c3348193a780200 (diff) | |
Merge remote-tracking branch 'origin/main' into claude/d3d-bundle-execute-indirect-37f83c
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 5e89ac2ab..d44160505 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #include <windows.h> | 3 | #include <windows.h> |
| 4 | #include <intrin.h> | 4 | #include <intrin.h> |
| 5 | #include <share.h> | ||
| 5 | #include <tlhelp32.h> | 6 | #include <tlhelp32.h> |
| 6 | 7 | ||
| 7 | #include <algorithm> | 8 | #include <algorithm> |
| @@ -10,7 +11,7 @@ | |||
| 10 | #include <cstdio> | 11 | #include <cstdio> |
| 11 | #include <cstdlib> | 12 | #include <cstdlib> |
| 12 | #include <cstring> | 13 | #include <cstring> |
| 13 | #include <fstream> | 14 | #include <memory> |
| 14 | #include <mutex> | 15 | #include <mutex> |
| 15 | #include <stdexcept> | 16 | #include <stdexcept> |
| 16 | #include <system_error> | 17 | #include <system_error> |
| @@ -77,9 +78,16 @@ std::string path_utf8(const std::filesystem::path& path) { | |||
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | std::vector<uint8_t> read_file(const std::filesystem::path& p) { | 80 | std::vector<uint8_t> read_file(const std::filesystem::path& p) { |
| 80 | std::ifstream f(p, std::ios::binary); | 81 | std::unique_ptr<std::FILE, decltype(&std::fclose)> file( |
| 81 | if (!f) throw std::runtime_error("cannot open " + path_utf8(p)); | 82 | _wfsopen(p.c_str(), L"rb", _SH_DENYNO), std::fclose); |
| 82 | return {std::istreambuf_iterator<char>(f), {}}; | 83 | if (!file) throw std::runtime_error("cannot open " + path_utf8(p)); |
| 84 | |||
| 85 | std::vector<uint8_t> bytes; | ||
| 86 | std::array<uint8_t, 64 * 1024> block; | ||
| 87 | while (const auto count = std::fread(block.data(), 1, block.size(), file.get())) | ||
| 88 | bytes.insert(bytes.end(), block.data(), block.data() + count); | ||
| 89 | if (std::ferror(file.get())) throw std::runtime_error("cannot read " + path_utf8(p)); | ||
| 90 | return bytes; | ||
| 83 | } | 91 | } |
| 84 | 92 | ||
| 85 | template <class T> | 93 | template <class T> |
| @@ -1526,6 +1534,8 @@ void* Loader::load_runtime_module_w(const std::wstring& basename, uint32_t flags | |||
| 1526 | if (prefer_host_copy(narrow)) return nullptr; | 1534 | if (prefer_host_copy(narrow)) return nullptr; |
| 1527 | 1535 | ||
| 1528 | std::lock_guard lock(mutex_); | 1536 | std::lock_guard lock(mutex_); |
| 1537 | // As load_runtime_module: a run-time load, so DllMain gets a null lpvReserved. | ||
| 1538 | const RuntimeLoadScope dynamic(runtime_loads_); | ||
| 1529 | Module* existing = find_wide_module(basename); | 1539 | Module* existing = find_wide_module(basename); |
| 1530 | const std::filesystem::path candidate = game_dir_ / basename; | 1540 | const std::filesystem::path candidate = game_dir_ / basename; |
| 1531 | std::error_code ec; | 1541 | std::error_code ec; |
| @@ -1912,6 +1922,25 @@ void Loader::initialize_tls(Module& m) { | |||
| 1912 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); | 1922 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); |
| 1913 | } | 1923 | } |
| 1914 | 1924 | ||
| 1925 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1926 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included, and before the owner is | ||
| 1927 | // published below: from then on a thread event (install_thread_block) reads m.tls_ga_slot | ||
| 1928 | // (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT). | ||
| 1929 | if (executable && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1930 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1931 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1932 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1933 | m.name.c_str()); | ||
| 1934 | } else { | ||
| 1935 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1936 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1937 | std::fprintf(stderr, | ||
| 1938 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1939 | "its module TLS index is %u\n", | ||
| 1940 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1941 | } | ||
| 1942 | } | ||
| 1943 | |||
| 1915 | // The fields are complete before a thread event can see the owner. | 1944 | // The fields are complete before a thread event can see the owner. |
| 1916 | ExclusiveTlsLock lock; | 1945 | ExclusiveTlsLock lock; |
| 1917 | Module* expected = nullptr; | 1946 | Module* expected = nullptr; |
| @@ -1930,26 +1959,6 @@ void Loader::initialize_tls(Module& m) { | |||
| 1930 | // VirtualProtect dance. | 1959 | // VirtualProtect dance. |
| 1931 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; | 1960 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; |
| 1932 | } | 1961 | } |
| 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 { | 1962 | try { |
| 1954 | install_thread_block(m); | 1963 | install_thread_block(m); |
| 1955 | } catch (...) { | 1964 | } catch (...) { |