diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 02:47:49 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 02:47:49 -0500 |
| commit | 943182b7763b52013c1617906655a29d61334188 (patch) | |
| tree | fdbc063eb37d97d28c6d4074e304a4c7851016f0 /runtime/src/pe_loader.cpp | |
| parent | 5b972b0b7c9e186daba44f03f335db05add0c7fb (diff) | |
| parent | 236b0acec3891b773ead57d3113b2f469d0e4a9f (diff) | |
Merge pull request #1126 from quackottu
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index d865e4179..cd1a38bcc 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. | ||
| 51 | IMAGE_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. |
| 51 | uint64_t rva_in(const Module& m, uint64_t va, const char* what) { | 56 | uint64_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); |
| @@ -201,8 +206,7 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 201 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). | 206 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). |
| 202 | void Loader::register_unwind(Module& m) { | 207 | void Loader::register_unwind(Module& m) { |
| 203 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 208 | 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; | 209 | 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. | 210 | // Like the system loader, a trailing partial entry is ignored. |
| 207 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); | 211 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); |
| 208 | if (!count) return; | 212 | if (!count) return; |
| @@ -287,7 +291,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 287 | m->base = static_cast<uint8_t*>( | 291 | m->base = static_cast<uint8_t*>( |
| 288 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 292 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 289 | if (!m->base) { | 293 | if (!m->base) { |
| 290 | auto& relocs = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 294 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 291 | if (!relocs.Size) | 295 | if (!relocs.Size) |
| 292 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); | 296 | 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)); | 297 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| @@ -308,7 +312,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); | 312 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 309 | if (delta) { | 313 | if (delta) { |
| 310 | m->relocated = true; | 314 | m->relocated = true; |
| 311 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 315 | 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"); | 316 | 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"); | 317 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); |
| 314 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 318 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| @@ -466,7 +470,7 @@ void Loader::run_tls_callbacks(Module& m, uint32_t reason) { | |||
| 466 | 470 | ||
| 467 | void Loader::parse_exports(Module& m) { | 471 | void Loader::parse_exports(Module& m) { |
| 468 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 472 | 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]; | 473 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 470 | if (!dir.Size) return; | 474 | if (!dir.Size) return; |
| 471 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); | 475 | 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); | 476 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); |
| @@ -520,8 +524,9 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 520 | uint32_t ordinal = 0; | 524 | uint32_t ordinal = 0; |
| 521 | if (by_ordinal) { | 525 | if (by_ordinal) { |
| 522 | for (size_t i = 1; i < symbol.size(); ++i) { | 526 | for (size_t i = 1; i < symbol.size(); ++i) { |
| 523 | if (symbol[i] < '0' || symbol[i] > '9' || ordinal > 0xFFFF) return nullptr; | 527 | if (symbol[i] < '0' || symbol[i] > '9') return nullptr; |
| 524 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); | 528 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); |
| 529 | if (ordinal > 0xFFFF) return nullptr; // checked after the digit, so "#65537" can't truncate to 1 | ||
| 525 | } | 530 | } |
| 526 | } | 531 | } |
| 527 | 532 | ||
| @@ -606,7 +611,7 @@ void Loader::bind_imports(Module& m) { | |||
| 606 | } | 611 | } |
| 607 | }; | 612 | }; |
| 608 | 613 | ||
| 609 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | 614 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_IMPORT); |
| 610 | if (dir.Size) { | 615 | if (dir.Size) { |
| 611 | for (uint64_t i = 0;; ++i) { | 616 | for (uint64_t i = 0;; ++i) { |
| 612 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); | 617 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); |
| @@ -619,7 +624,7 @@ void Loader::bind_imports(Module& m) { | |||
| 619 | } | 624 | } |
| 620 | } | 625 | } |
| 621 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. | 626 | // 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]; | 627 | const auto ddir = directory(nt, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT); |
| 623 | if (ddir.Size) { | 628 | if (ddir.Size) { |
| 624 | for (uint64_t i = 0;; ++i) { | 629 | for (uint64_t i = 0;; ++i) { |
| 625 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); | 630 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); |