From ca56a0d5eff2f6217a593babcfaff87d46daa15d Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Mon, 23 Nov 2026 15:12:44 -0500 Subject: pe_loader: a relocation table is applied whatever RELOCS_STRIPPED says (Windows 11 measurement) Zombins2 measured the Windows 11 (26200) loader with synthetic x64 DLLs in #8847. An image with IMAGE_FILE_RELOCS_STRIPPED set and a relocation table was moved and fixed up when its base was taken. The previous commit refused that case, while main relocated it. That is now back to main's behaviour: a moved image with a table is fixed up whatever the flag says, and the flag refuses a move only when there is no table. base_reservation::needed is back to main's test (the directory size) and no longer reserves for a flag-set image with a table. The core change stays: with the flag clear and no relocation directory, the image is moved with nothing fixed up and the move is logged. That matches the measurement. Windows also refused flag-set DLLs without a relocation directory at a free base (error 487), and it is not known why. The loader keeps main's behaviour there and maps them at their base. runtime/README.md records this as an open question, and loader_test's check for it no longer claims Windows parity. The flag-with-table check now expects a move with the DIR64 fix-up applied. Co-Authored-By: Claude Opus 5.5 --- runtime/src/pe_loader.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 253a773b5..1c6b91b54 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -1450,14 +1450,14 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string& } int64_t delta = reinterpret_cast(m->base) - static_cast(oh.ImageBase); - // Windows' loader (LdrRelocateImage) refuses to move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the - // flag clear and no relocation directory it moves it and fixes up nothing, so the image is trusted to hold no - // absolute addresses. That move is logged so it shows in traces. base_reservation::needed still keeps the main - // image's range free when it has no relocation directory, so a main image is moved this way only where the - // range could not be had. + // A moved image with a relocation table is fixed up, whatever IMAGE_FILE_RELOCS_STRIPPED says. Without a table, + // the flag refuses the move; with the flag clear the image is moved and nothing is fixed up, so it is trusted to + // hold no absolute addresses. The Windows 11 loader does all three (measured in #8847); that move is logged so + // it shows in traces. base_reservation::needed still keeps the main image's range free when it has no + // relocation directory, so a main image is moved this way only where the range could not be had. const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); if (delta) m->relocated = true; - if (delta && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) + if (delta && !dir.Size && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) throw std::runtime_error(m->name + ": preferred base is taken and the image's relocations are stripped"); if (delta && !dir.Size) std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n", -- cgit v1.2.3