diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 19:51:57 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 19:51:57 -0500 |
| commit | b88fba3956c05c5832ead10e789606e4d2287025 (patch) | |
| tree | 656a1e8a7616a6f9f538c70212be9aea8a5fb46b /runtime/src/pe_loader.cpp | |
| parent | 4b10cbe91edce03c1dd2becd57bc981e943a88ce (diff) | |
| parent | 88479ff65baa28a975c888266afad350a27aca56 (diff) | |
Merge main into fix-red-tests (#1561)
No conflicts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 3558cffb5..fc73f37f5 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1721,10 +1721,7 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string& | |||
| 1721 | if (!m->base) | 1721 | if (!m->base) |
| 1722 | m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, | 1722 | m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, |
| 1723 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 1723 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 1724 | if (!m->base) { | 1724 | if (!m->base) { // whether the image may move is decided at the relocation step below |
| 1725 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | ||
| 1726 | if (!relocs.Size) | ||
| 1727 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); | ||
| 1728 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 1725 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 1729 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); | 1726 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); |
| 1730 | } | 1727 | } |
| @@ -1749,10 +1746,19 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string& | |||
| 1749 | } | 1746 | } |
| 1750 | 1747 | ||
| 1751 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); | 1748 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 1752 | if (delta) { | 1749 | // A moved image with a relocation table is fixed up, whatever IMAGE_FILE_RELOCS_STRIPPED says. Without a table, |
| 1753 | m->relocated = true; | 1750 | // the flag refuses the move; with the flag clear the image is moved and nothing is fixed up, so it is trusted to |
| 1754 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | 1751 | // hold no absolute addresses. The Windows 11 loader does all three (runtime/README.md); that move is logged so |
| 1755 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); | 1752 | // it shows in traces. base_reservation::needed still keeps the main image's range free when it has no |
| 1753 | // relocation directory, so a main image is moved this way only where the range could not be had. | ||
| 1754 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | ||
| 1755 | if (delta) m->relocated = true; | ||
| 1756 | if (delta && !dir.Size && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) | ||
| 1757 | throw std::runtime_error(m->name + ": preferred base is taken and the image's relocations are stripped"); | ||
| 1758 | if (delta && !dir.Size) | ||
| 1759 | std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n", | ||
| 1760 | m->name.c_str(), static_cast<unsigned long long>(oh.ImageBase), static_cast<void*>(m->base)); | ||
| 1761 | if (delta && dir.Size) { | ||
| 1756 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); | 1762 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); |
| 1757 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 1763 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| 1758 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; | 1764 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; |
| @@ -2123,7 +2129,10 @@ void Loader::bind_imports(Module& m) { | |||
| 2123 | const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders; | 2129 | const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders; |
| 2124 | 2130 | ||
| 2125 | // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. | 2131 | // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. |
| 2126 | auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { | 2132 | // `delayed`: a delay import that binds to an unimplemented thunk is traced. Windows' delay-load helper would raise |
| 2133 | // 0xC06D007E (module) or 0xC06D007F (procedure) at its first call, which a title can catch for a fallback; the | ||
| 2134 | // thunk returns 0 instead, so the trace names each such import before any image code runs. | ||
| 2135 | auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva, bool delayed) { | ||
| 2127 | for (;; lookup_rva += sizeof(IMAGE_THUNK_DATA64), iat_rva += sizeof(IMAGE_THUNK_DATA64)) { | 2136 | for (;; lookup_rva += sizeof(IMAGE_THUNK_DATA64), iat_rva += sizeof(IMAGE_THUNK_DATA64)) { |
| 2128 | require_range(m.name, lookup_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import lookup table"); | 2137 | require_range(m.name, lookup_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import lookup table"); |
| 2129 | require_range(m.name, iat_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import address table"); | 2138 | require_range(m.name, iat_rva, sizeof(IMAGE_THUNK_DATA64), m.size, "an import address table"); |
| @@ -2141,6 +2150,10 @@ void Loader::bind_imports(Module& m) { | |||
| 2141 | throw std::runtime_error(m.name + ": an import address table lands in the headers"); | 2150 | throw std::runtime_error(m.name + ": an import address table lands in the headers"); |
| 2142 | at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = | 2151 | at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = |
| 2143 | reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); | 2152 | reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); |
| 2153 | if (delayed && startup_trace::enabled() && m.imports.back().binding == Binding::Unimplemented) | ||
| 2154 | startup_trace::event("delay_import_unimplemented", | ||
| 2155 | "\"module\":" + startup_trace::quote(m.name) + ",\"import\":" + | ||
| 2156 | startup_trace::quote(dll + "!" + m.imports.back().name)); | ||
| 2144 | } | 2157 | } |
| 2145 | }; | 2158 | }; |
| 2146 | 2159 | ||
| @@ -2153,7 +2166,7 @@ void Loader::bind_imports(Module& m) { | |||
| 2153 | if (!d->Name) break; | 2166 | if (!d->Name) break; |
| 2154 | std::string dll = read_string(m.name, m.base, m.size, d->Name, "an imported DLL name"); | 2167 | std::string dll = read_string(m.name, m.base, m.size, d->Name, "an imported DLL name"); |
| 2155 | uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk; | 2168 | uint32_t lookup = d->OriginalFirstThunk ? d->OriginalFirstThunk : d->FirstThunk; |
| 2156 | bind_thunks(dll, lookup, d->FirstThunk); | 2169 | bind_thunks(dll, lookup, d->FirstThunk, false); |
| 2157 | } | 2170 | } |
| 2158 | } | 2171 | } |
| 2159 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. | 2172 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. |
| @@ -2176,7 +2189,7 @@ void Loader::bind_imports(Module& m) { | |||
| 2176 | // thunk addresses as name RVAs. | 2189 | // thunk addresses as name RVAs. |
| 2177 | if (!d->ImportNameTableRVA) | 2190 | if (!d->ImportNameTableRVA) |
| 2178 | throw std::runtime_error(m.name + ": a delay-import descriptor has no name table"); | 2191 | throw std::runtime_error(m.name + ": a delay-import descriptor has no name table"); |
| 2179 | bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA); | 2192 | bind_thunks(dll, d->ImportNameTableRVA, d->ImportAddressTableRVA, true); |
| 2180 | } | 2193 | } |
| 2181 | } | 2194 | } |
| 2182 | } | 2195 | } |