diff options
| author | Ayinaki <ayinakidev@gmail.com> | 2026-11-21 18:48:36 +0000 |
|---|---|---|
| committer | Ayinaki <ayinakidev@gmail.com> | 2026-11-21 19:00:06 +0000 |
| commit | 126062ae4a8831a564b9e3042684657c58781bef (patch) | |
| tree | 1a276cc6d4aeeccd84841d275d150d58f76a4fd4 /runtime/src/main.cpp | |
| parent | 2a732e0881cd20e7c63ba5ced7489cc5e97bc8e1 (diff) | |
Runtime: initialise a loaded module's TLS and DllMain
Mapping an image never ran anything. TLS callbacks and DllMain were documented as the caller's, and
nothing called them, so a module that needs either could not be brought up at all.
Loader::initialise() now allocates the module's TLS slot, copies the template and SizeOfZeroFill into
it, writes the slot index into the image, runs the TLS callbacks with DLL_PROCESS_ATTACH, then calls
the entry point of a DLL as DllMain. Every address it follows is bounded like the rest of the loader:
the directory, the template, the index, the callback table and each callback, with a 1 MiB ceiling on
the block. Only DLLs get DllMain; an EXE's entry point has a different signature and stays the
caller's to run, and --dry-run still runs nothing.
The tests build an image whose TLS callback and DllMain are real machine-code stubs that store a
sentinel, so they check the code actually executed rather than merely being parsed.
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index e69906c14..4477c4a69 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | // gta6rt --dry-run <image> [--game-dir DIR] | 3 | // gta6rt --dry-run <image> [--game-dir DIR] |
| 4 | // Maps the image and its bundled modules, applies relocations, binds every import and prints a report. | 4 | // Maps the image and its bundled modules, applies relocations, binds every import and prints a report. |
| 5 | // No code from the image runs. | 5 | // No code from the image runs. |
| 6 | // | ||
| 7 | // gta6rt --init <image> [--game-dir DIR] | ||
| 8 | // The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. | ||
| 6 | #include <windows.h> | 9 | #include <windows.h> |
| 7 | 10 | ||
| 8 | #include <cstdio> | 11 | #include <cstdio> |
| @@ -23,7 +26,9 @@ const char* binding_name(rt::Binding b) { | |||
| 23 | } | 26 | } |
| 24 | 27 | ||
| 25 | int usage() { | 28 | int usage() { |
| 26 | std::fprintf(stderr, "usage: gta6rt --dry-run <image> [--game-dir DIR]\n"); | 29 | std::fprintf(stderr, |
| 30 | "usage: gta6rt --dry-run <image> [--game-dir DIR]\n" | ||
| 31 | " gta6rt --init <image> [--game-dir DIR]\n"); | ||
| 27 | return 2; | 32 | return 2; |
| 28 | } | 33 | } |
| 29 | 34 | ||
| @@ -31,17 +36,20 @@ int usage() { | |||
| 31 | 36 | ||
| 32 | int wmain(int argc, wchar_t** argv) { | 37 | int wmain(int argc, wchar_t** argv) { |
| 33 | std::filesystem::path image, game_dir; | 38 | std::filesystem::path image, game_dir; |
| 34 | bool dry_run = false; | 39 | bool dry_run = false, init = false; |
| 35 | for (int i = 1; i < argc; ++i) { | 40 | for (int i = 1; i < argc; ++i) { |
| 36 | std::wstring a = argv[i]; | 41 | std::wstring a = argv[i]; |
| 37 | if (a == L"--dry-run") dry_run = true; | 42 | if (a == L"--dry-run") dry_run = true; |
| 43 | else if (a == L"--init") init = true; | ||
| 38 | else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; | 44 | else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; |
| 39 | else if (image.empty()) image = a; | 45 | else if (image.empty()) image = a; |
| 40 | else return usage(); | 46 | else return usage(); |
| 41 | } | 47 | } |
| 42 | if (image.empty()) return usage(); | 48 | if (image.empty()) return usage(); |
| 43 | if (!dry_run) { | 49 | if (!dry_run && !init) { |
| 44 | std::fprintf(stderr, "Only --dry-run exists so far: running game code needs the shims listed by it first.\n"); | 50 | std::fprintf(stderr, |
| 51 | "Only --dry-run and --init exist so far: running game code needs the shims listed by " | ||
| 52 | "--dry-run first.\n"); | ||
| 45 | return 2; | 53 | return 2; |
| 46 | } | 54 | } |
| 47 | if (game_dir.empty()) game_dir = image.parent_path(); | 55 | if (game_dir.empty()) game_dir = image.parent_path(); |
| @@ -49,6 +57,10 @@ int wmain(int argc, wchar_t** argv) { | |||
| 49 | try { | 57 | try { |
| 50 | rt::Loader loader(game_dir); | 58 | rt::Loader loader(game_dir); |
| 51 | loader.load(image); | 59 | loader.load(image); |
| 60 | if (init) { | ||
| 61 | loader.initialise(); | ||
| 62 | std::printf("%zu module(s) initialised\n", loader.modules().size()); | ||
| 63 | } | ||
| 52 | int unimplemented = 0; | 64 | int unimplemented = 0; |
| 53 | for (auto* m : loader.modules()) { | 65 | for (auto* m : loader.modules()) { |
| 54 | std::map<rt::Binding, int> counts; | 66 | std::map<rt::Binding, int> counts; |