diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 14:40:15 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 14:40:15 -0500 |
| commit | 8bf99edc7a437f13e01fa4bc8b22b49b473e569c (patch) | |
| tree | 4da9c23f1b292b04922fd7a28023720a40fd7074 /runtime/src/pe_loader.cpp | |
| parent | d279b288684060cdfd486a841fbd185ac37d865b (diff) | |
| parent | 126062ae4a8831a564b9e3042684657c58781bef (diff) | |
Merge pull request #95 from Ayinaki
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index e6a9501ab..f26527989 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -56,6 +56,8 @@ bool prefer_host_copy(const std::string& dll) { | |||
| 56 | 56 | ||
| 57 | Loader::~Loader() { | 57 | Loader::~Loader() { |
| 58 | for (auto* m : order_) { | 58 | for (auto* m : order_) { |
| 59 | if (m->tls_block) VirtualFree(m->tls_block, 0, MEM_RELEASE); | ||
| 60 | if (m->has_tls_slot) TlsFree(m->tls_slot); | ||
| 59 | if (m->base) VirtualFree(m->base, 0, MEM_RELEASE); | 61 | if (m->base) VirtualFree(m->base, 0, MEM_RELEASE); |
| 60 | delete m; | 62 | delete m; |
| 61 | } | 63 | } |
| @@ -70,6 +72,88 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 70 | return m; | 72 | return m; |
| 71 | } | 73 | } |
| 72 | 74 | ||
| 75 | void Loader::initialise() { | ||
| 76 | // order_ is dependency order: a module is pushed after the modules it imports. | ||
| 77 | for (auto* m : order_) initialise(*m); | ||
| 78 | } | ||
| 79 | |||
| 80 | void Loader::initialise(Module& m) { | ||
| 81 | if (m.initialised) return; | ||
| 82 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | ||
| 83 | const uint64_t base = reinterpret_cast<uint64_t>(m.base); | ||
| 84 | |||
| 85 | // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. | ||
| 86 | auto rva_of = [&](uint64_t va, const char* what) { | ||
| 87 | if (va < base || va - base >= m.size) | ||
| 88 | throw std::runtime_error(m.name + ": " + what + " is outside the image"); | ||
| 89 | return va - base; | ||
| 90 | }; | ||
| 91 | |||
| 92 | auto& tls_dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; | ||
| 93 | if (tls_dir.Size) { | ||
| 94 | require_range(m.name, tls_dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory"); | ||
| 95 | auto* tls = at<IMAGE_TLS_DIRECTORY64>(m.base, tls_dir.VirtualAddress); | ||
| 96 | |||
| 97 | uint64_t start = 0, template_size = 0; | ||
| 98 | if (tls->StartAddressOfRawData || tls->EndAddressOfRawData) { | ||
| 99 | start = rva_of(tls->StartAddressOfRawData, "the TLS template"); | ||
| 100 | uint64_t end = rva_of(tls->EndAddressOfRawData, "the end of the TLS template"); | ||
| 101 | if (end < start) throw std::runtime_error(m.name + ": the TLS template ends before it starts"); | ||
| 102 | template_size = end - start; | ||
| 103 | require_range(m.name, start, template_size, m.size, "the TLS template"); | ||
| 104 | } | ||
| 105 | m.tls_block_size = template_size + tls->SizeOfZeroFill; | ||
| 106 | if (m.tls_block_size > (1u << 20)) throw std::runtime_error(m.name + ": the TLS block is larger than 1 MiB"); | ||
| 107 | if (m.tls_block_size) { | ||
| 108 | m.tls_block = VirtualAlloc(nullptr, static_cast<SIZE_T>(m.tls_block_size), MEM_RESERVE | MEM_COMMIT, | ||
| 109 | PAGE_READWRITE); | ||
| 110 | if (!m.tls_block) throw std::runtime_error(m.name + ": cannot allocate the TLS block"); | ||
| 111 | if (template_size) std::memcpy(m.tls_block, m.base + start, static_cast<size_t>(template_size)); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (tls->AddressOfIndex) { | ||
| 115 | uint64_t index_rva = rva_of(tls->AddressOfIndex, "the TLS index"); | ||
| 116 | require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); | ||
| 117 | DWORD slot = TlsAlloc(); | ||
| 118 | if (slot == TLS_OUT_OF_INDEXES) throw std::runtime_error(m.name + ": no TLS slot is free"); | ||
| 119 | m.tls_slot = slot; | ||
| 120 | m.has_tls_slot = true; | ||
| 121 | if (m.tls_block) TlsSetValue(slot, m.tls_block); | ||
| 122 | // The index lives in a writable section, but protect_sections already ran, so lift it for the write. | ||
| 123 | auto* index = m.base + index_rva; | ||
| 124 | auto* page = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(index) & ~uintptr_t(0xFFF)); | ||
| 125 | DWORD old = 0, ignored = 0; | ||
| 126 | if (!VirtualProtect(page, 0x1000, PAGE_READWRITE, &old)) | ||
| 127 | throw std::runtime_error(m.name + ": the TLS index can't be written"); | ||
| 128 | *reinterpret_cast<uint32_t*>(index) = slot; | ||
| 129 | VirtualProtect(page, 0x1000, old, &ignored); | ||
| 130 | } | ||
| 131 | |||
| 132 | if (tls->AddressOfCallBacks) { | ||
| 133 | uint64_t table_rva = rva_of(tls->AddressOfCallBacks, "the TLS callback table"); | ||
| 134 | for (uint64_t i = 0;; ++i) { | ||
| 135 | uint64_t entry_rva = table_rva + i * sizeof(uint64_t); | ||
| 136 | require_range(m.name, entry_rva, sizeof(uint64_t), m.size, "a TLS callback table entry"); | ||
| 137 | uint64_t fn = *at<uint64_t>(m.base, static_cast<uint32_t>(entry_rva)); | ||
| 138 | if (!fn) break; | ||
| 139 | auto callback = reinterpret_cast<void(NTAPI*)(void*, DWORD, void*)>(m.base + rva_of(fn, "a TLS callback")); | ||
| 140 | callback(m.base, DLL_PROCESS_ATTACH, nullptr); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | // Only a DLL gets DllMain. An EXE's entry point has a different signature and is the caller's to run. | ||
| 146 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { | ||
| 147 | if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image"); | ||
| 148 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); | ||
| 149 | if (!entry(m.base, DLL_PROCESS_ATTACH, nullptr)) { | ||
| 150 | m.initialised = true; // it ran: a second DLL_PROCESS_ATTACH would be wrong | ||
| 151 | throw std::runtime_error(m.name + ": DllMain returned FALSE"); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | m.initialised = true; | ||
| 155 | } | ||
| 156 | |||
| 73 | Module& Loader::map_image(const std::filesystem::path& path) { | 157 | Module& Loader::map_image(const std::filesystem::path& path) { |
| 74 | auto file = read_file(path); | 158 | auto file = read_file(path); |
| 75 | const std::string name = path.filename().string(); | 159 | const std::string name = path.filename().string(); |