upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp41
1 files changed, 33 insertions, 8 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index d865e4179..15a62d9b4 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -47,6 +47,11 @@ std::string read_string(const std::string& name, uint8_t* base, uint32_t image_s
47 return {s, end}; 47 return {s, end};
48} 48}
49 49
50// PE/COFF: NumberOfRvaAndSizes counts the data directories; entries past it are not directories, whatever they hold.
51IMAGE_DATA_DIRECTORY directory(const IMAGE_NT_HEADERS64* nt, unsigned index) {
52 return index < nt->OptionalHeader.NumberOfRvaAndSizes ? nt->OptionalHeader.DataDirectory[index] : IMAGE_DATA_DIRECTORY{};
53}
54
50// The TLS directory holds virtual addresses, which relocations have already moved to this mapping. 55// The TLS directory holds virtual addresses, which relocations have already moved to this mapping.
51uint64_t rva_in(const Module& m, uint64_t va, const char* what) { 56uint64_t rva_in(const Module& m, uint64_t va, const char* what) {
52 const uint64_t base = reinterpret_cast<uint64_t>(m.base); 57 const uint64_t base = reinterpret_cast<uint64_t>(m.base);
@@ -161,6 +166,26 @@ Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir))
161} 166}
162 167
163Loader::~Loader() { 168Loader::~Loader() {
169 // Importers must release their resources before their dependencies. Keep every mapping, unwind table and TLS
170 // block alive throughout the notifications: a detach handler can still call an imported function or free an
171 // FLS slot whose destructor lives in another mapped image.
172 for (auto it = init_order_.rbegin(); it != init_order_.rend(); ++it) {
173 Module& m = **it;
174 if (!m.initialised) continue; // a map-only load never opts in to running image code
175 m.initialised = false;
176 try {
177 run_tls_callbacks(m, DLL_PROCESS_DETACH);
178 } catch (const std::exception& e) {
179 // A module may have changed its callback table since attach. A destructor cannot propagate the validation
180 // error; report it and still let DllMain release resources before the image is freed.
181 std::fprintf(stderr, "[rt] TLS process detach failed for %s: %s\n", m.name.c_str(), e.what());
182 }
183 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
184 if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) {
185 auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva);
186 entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored
187 }
188 }
164 shims::remove_module_lookup(this); 189 shims::remove_module_lookup(this);
165 for (auto* m : order_) { 190 for (auto* m : order_) {
166 // TLS state must go away before the image and its TLS block are unmapped. 191 // TLS state must go away before the image and its TLS block are unmapped.
@@ -201,8 +226,7 @@ Module& Loader::load(const std::filesystem::path& image) {
201// allowed. No entry in that set uses the indirect form (UnwindData bit 0). 226// allowed. No entry in that set uses the indirect form (UnwindData bit 0).
202void Loader::register_unwind(Module& m) { 227void Loader::register_unwind(Module& m) {
203 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); 228 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
204 if (nt->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXCEPTION) return; 229 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION);
205 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION];
206 // Like the system loader, a trailing partial entry is ignored. 230 // Like the system loader, a trailing partial entry is ignored.
207 const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); 231 const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION);
208 if (!count) return; 232 if (!count) return;
@@ -287,7 +311,7 @@ Module& Loader::map_image(const std::filesystem::path& path) {
287 m->base = static_cast<uint8_t*>( 311 m->base = static_cast<uint8_t*>(
288 VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 312 VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
289 if (!m->base) { 313 if (!m->base) {
290 auto& relocs = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; 314 const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC);
291 if (!relocs.Size) 315 if (!relocs.Size)
292 throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); 316 throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations");
293 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 317 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
@@ -308,7 +332,7 @@ Module& Loader::map_image(const std::filesystem::path& path) {
308 int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); 332 int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase);
309 if (delta) { 333 if (delta) {
310 m->relocated = true; 334 m->relocated = true;
311 auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; 335 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC);
312 if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); 336 if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base");
313 require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); 337 require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table");
314 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); 338 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress);
@@ -466,7 +490,7 @@ void Loader::run_tls_callbacks(Module& m, uint32_t reason) {
466 490
467void Loader::parse_exports(Module& m) { 491void Loader::parse_exports(Module& m) {
468 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); 492 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
469 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; 493 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXPORT);
470 if (!dir.Size) return; 494 if (!dir.Size) return;
471 require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); 495 require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory");
472 auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); 496 auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress);
@@ -520,8 +544,9 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
520 uint32_t ordinal = 0; 544 uint32_t ordinal = 0;
521 if (by_ordinal) { 545 if (by_ordinal) {
522 for (size_t i = 1; i < symbol.size(); ++i) { 546 for (size_t i = 1; i < symbol.size(); ++i) {
523 if (symbol[i] < '0' || symbol[i] > '9' || ordinal > 0xFFFF) return nullptr; 547 if (symbol[i] < '0' || symbol[i] > '9') return nullptr;
524 ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); 548 ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0');
549 if (ordinal > 0xFFFF) return nullptr; // checked after the digit, so "#65537" can't truncate to 1
525 } 550 }
526 } 551 }
527 552
@@ -606,7 +631,7 @@ void Loader::bind_imports(Module& m) {
606 } 631 }
607 }; 632 };
608 633
609 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; 634 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_IMPORT);
610 if (dir.Size) { 635 if (dir.Size) {
611 for (uint64_t i = 0;; ++i) { 636 for (uint64_t i = 0;; ++i) {
612 uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); 637 uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR);
@@ -619,7 +644,7 @@ void Loader::bind_imports(Module& m) {
619 } 644 }
620 } 645 }
621 // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. 646 // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run.
622 auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; 647 const auto ddir = directory(nt, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);
623 if (ddir.Size) { 648 if (ddir.Size) {
624 for (uint64_t i = 0;; ++i) { 649 for (uint64_t i = 0;; ++i) {
625 uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); 650 uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR);