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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-23 14:36:22 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 04:58:41 -0500
commit0ddf806359d30022a3152e61c93ed077212d25aa (patch)
treecfad9153849f453afbde3123b8e61afd8e436c5a /runtime/src/pe_loader.cpp
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
pe_loader: move an image with no relocation directory, refuse only on IMAGE_FILE_RELOCS_STRIPPED
The loader refused any image whose preferred base was taken when it had no relocation directory. Windows' loader decides on the file header flag instead (LdrRelocateImage, the same in Wine's perform_relocations and ReactOS's LdrRelocateImageWithBias): IMAGE_FILE_RELOCS_STRIPPED set means the image cannot move and is refused (STATUS_CONFLICTING_ADDRESSES); with the flag clear and an empty directory the image is moved with nothing to fix up, as a second copy of such a DLL is. map_image now does the same, for an EXE and a DLL alike: it refuses to move a flag-set image (also one that has a relocation table, as Windows does), moves a flag-clear image without a directory and logs one "[rt] loader: ... moved ... with no relocations" line, so a move without fix-ups shows in traces. base_reservation::needed keeps reserving the main image's range for both kinds of image without relocations, and now also for a flag-set image with a table, so the main image keeps its base wherever it did before. The retail executable and every package DLL have a relocation table and the flag clear, so neither path applies to them. loader_test covers a flag-clear image moved when its base is held (it loads elsewhere, nothing is fixed up, its DllMain runs), the same image with the flag set (refused), the flag at a free base (maps), and the flag with a relocation table (refused). test_failed_dependency_not_registered's dependency shared its parent's base and was refused for that; it now fails on its corrupt import table, which is what the test describes. 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.cpp22
1 files changed, 14 insertions, 8 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index c29a288a9..253a773b5 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -1425,10 +1425,7 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string&
1425 if (!m->base) 1425 if (!m->base)
1426 m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, 1426 m->base = static_cast<uint8_t*>(VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage,
1427 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 1427 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
1428 if (!m->base) { 1428 if (!m->base) { // whether the image may move is decided at the relocation step below
1429 const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC);
1430 if (!relocs.Size)
1431 throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations");
1432 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 1429 m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
1433 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); 1430 if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed");
1434 } 1431 }
@@ -1453,10 +1450,19 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string&
1453 } 1450 }
1454 1451
1455 int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); 1452 int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase);
1456 if (delta) { 1453 // Windows' loader (LdrRelocateImage) refuses to move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the
1457 m->relocated = true; 1454 // flag clear and no relocation directory it moves it and fixes up nothing, so the image is trusted to hold no
1458 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); 1455 // absolute addresses. That move is logged so it shows in traces. base_reservation::needed still keeps the main
1459 if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); 1456 // image's range free when it has no relocation directory, so a main image is moved this way only where the
1457 // range could not be had.
1458 const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC);
1459 if (delta) m->relocated = true;
1460 if (delta && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED))
1461 throw std::runtime_error(m->name + ": preferred base is taken and the image's relocations are stripped");
1462 if (delta && !dir.Size)
1463 std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n",
1464 m->name.c_str(), static_cast<unsigned long long>(oh.ImageBase), static_cast<void*>(m->base));
1465 if (delta && dir.Size) {
1460 require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); 1466 require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table");
1461 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); 1467 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress);
1462 auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; 1468 auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size;