diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-23 15:12:44 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 04:58:41 -0500 |
| commit | ca56a0d5eff2f6217a593babcfaff87d46daa15d (patch) | |
| tree | 6807765965a62150a371a00fb0d9ad33be4684da /runtime | |
| parent | 0ddf806359d30022a3152e61c93ed077212d25aa (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/README.md | 9 | ||||
| -rw-r--r-- | runtime/src/base_reservation.h | 11 | ||||
| -rw-r--r-- | runtime/src/pe_loader.cpp | 12 | ||||
| -rw-r--r-- | runtime/test/loader_test.cpp | 35 |
4 files changed, 44 insertions, 23 deletions
diff --git a/runtime/README.md b/runtime/README.md index b91689582..abe3e841b 100644 --- a/runtime/README.md +++ b/runtime/README.md | |||
| @@ -133,8 +133,13 @@ with the seed on stderr; a seed of 0 uses a fixed non-zero state. Unset, the hos | |||
| 133 | ## PE loader | 133 | ## PE loader |
| 134 | 134 | ||
| 135 | `src/pe_loader.cpp` maps x86-64 images at their preferred base, or elsewhere (with relocations) when | 135 | `src/pe_loader.cpp` maps x86-64 images at their preferred base, or elsewhere (with relocations) when |
| 136 | that base is taken. As on Windows, only `IMAGE_FILE_RELOCS_STRIPPED` refuses the move; an image with the flag | 136 | that base is taken. An image with a relocation table is moved and fixed up whatever `IMAGE_FILE_RELOCS_STRIPPED` |
| 137 | clear and no relocation directory is moved with nothing fixed up, and the loader logs that. It copies headers and | 137 | says; without one, the flag refuses the move, and an image with the flag clear is moved with nothing fixed up (the |
| 138 | loader logs that). The Windows 11 loader does the same in these three cases, as measured by Zombins2 with | ||
| 139 | synthetic DLLs ([#8847](https://github.com/ItsNotRexed/Leonida/pull/8847#issuecomment-7112587898)). Open | ||
| 140 | question from the same measurement: Windows also refused flag-set DLLs without a relocation directory at a free | ||
| 141 | preferred base (error 487, linker-built `/FIXED` DLLs included), and why is not known; the loader still maps those | ||
| 142 | at their base, as before. It copies headers and | ||
| 138 | sections, applies `IMAGE_REL_BASED_DIR64` relocations, binds | 143 | sections, applies `IMAGE_REL_BASED_DIR64` relocations, binds |
| 139 | imports by name or ordinal, parses the export table, sets section protections, and registers the | 144 | imports by name or ordinal, parses the export table, sets section protections, and registers the |
| 140 | image's x64 unwind table (`.pdata`) with `RtlAddFunctionTable`, so exceptions raised in or passing | 145 | image's x64 unwind table (`.pdata`) with `RtlAddFunctionTable`, so exceptions raised in or passing |
diff --git a/runtime/src/base_reservation.h b/runtime/src/base_reservation.h index 485cf5917..5ccfe1aaa 100644 --- a/runtime/src/base_reservation.h +++ b/runtime/src/base_reservation.h | |||
| @@ -63,10 +63,10 @@ inline void* commit_reserved(std::uint64_t base, std::uint64_t size) { | |||
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | // The preferred range of `image` when it has no relocations and that range is not free in this process; nullopt | 65 | // The preferred range of `image` when it has no relocations and that range is not free in this process; nullopt |
| 66 | // when the image can be mapped as it is (or is not a readable PE32+, which the loader reports itself). "No | 66 | // when the image can be mapped as it is (or is not a readable PE32+, which the loader reports itself). Without a |
| 67 | // relocations" is either IMAGE_FILE_RELOCS_STRIPPED, which the loader refuses to move, or an empty relocation | 67 | // relocation directory the loader refuses to move an image that has IMAGE_FILE_RELOCS_STRIPPED, and moves one with |
| 68 | // directory with the flag clear, which it moves with nothing fixed up (as Windows does): that is right only for an | 68 | // the flag clear with nothing fixed up: that is right only for an image with no absolute addresses in it, so its |
| 69 | // image with no absolute addresses in it, so its range is kept free all the same. | 69 | // range is kept free either way. An image with a relocation table is moved and fixed up whatever the flag says. |
| 70 | inline std::optional<Range> needed(const std::filesystem::path& image) { | 70 | inline std::optional<Range> needed(const std::filesystem::path& image) { |
| 71 | std::ifstream file(image, std::ios::binary); | 71 | std::ifstream file(image, std::ios::binary); |
| 72 | IMAGE_DOS_HEADER dos{}; | 72 | IMAGE_DOS_HEADER dos{}; |
| @@ -78,8 +78,7 @@ inline std::optional<Range> needed(const std::filesystem::path& image) { | |||
| 78 | !nt.OptionalHeader.SizeOfImage) | 78 | !nt.OptionalHeader.SizeOfImage) |
| 79 | return std::nullopt; | 79 | return std::nullopt; |
| 80 | const auto& oh = nt.OptionalHeader; | 80 | const auto& oh = nt.OptionalHeader; |
| 81 | if (!(nt.FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) && | 81 | if (oh.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC && |
| 82 | oh.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC && | ||
| 83 | oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size) | 82 | oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size) |
| 84 | return std::nullopt; // relocatable: the loader moves it if the base is taken | 83 | return std::nullopt; // relocatable: the loader moves it if the base is taken |
| 85 | const Range r{oh.ImageBase, (static_cast<std::uint64_t>(oh.SizeOfImage) + 0xffff) & ~std::uint64_t{0xffff}}; | 84 | const Range r{oh.ImageBase, (static_cast<std::uint64_t>(oh.SizeOfImage) + 0xffff) & ~std::uint64_t{0xffff}}; |
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& | |||
| 1450 | } | 1450 | } |
| 1451 | 1451 | ||
| 1452 | 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); |
| 1453 | // Windows' loader (LdrRelocateImage) refuses to move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the | 1453 | // A moved image with a relocation table is fixed up, whatever IMAGE_FILE_RELOCS_STRIPPED says. Without a table, |
| 1454 | // flag clear and no relocation directory it moves it and fixes up nothing, so the image is trusted to hold no | 1454 | // the flag refuses the move; with the flag clear the image is moved and nothing is fixed up, so it is trusted to |
| 1455 | // absolute addresses. That move is logged so it shows in traces. base_reservation::needed still keeps the main | 1455 | // hold no absolute addresses. The Windows 11 loader does all three (measured in #8847); that move is logged so |
| 1456 | // image's range free when it has no relocation directory, so a main image is moved this way only where the | 1456 | // it shows in traces. base_reservation::needed still keeps the main image's range free when it has no |
| 1457 | // range could not be had. | 1457 | // relocation directory, so a main image is moved this way only where the range could not be had. |
| 1458 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); | 1458 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 1459 | if (delta) m->relocated = true; | 1459 | if (delta) m->relocated = true; |
| 1460 | if (delta && (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) | 1460 | if (delta && !dir.Size && (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"); | 1461 | throw std::runtime_error(m->name + ": preferred base is taken and the image's relocations are stripped"); |
| 1462 | if (delta && !dir.Size) | 1462 | if (delta && !dir.Size) |
| 1463 | std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n", | 1463 | std::fprintf(stderr, "[rt] loader: %s: preferred base %#llx is taken, moved to %p with no relocations\n", |
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index a49a75385..926becd12 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -654,10 +654,11 @@ void test_initialise_rejects() { | |||
| 654 | } | 654 | } |
| 655 | } | 655 | } |
| 656 | 656 | ||
| 657 | // An image with no relocation directory whose preferred base is taken. Windows' loader (LdrRelocateImage) refuses to | 657 | // IMAGE_FILE_RELOCS_STRIPPED against a relocation directory, when the preferred base is taken. Measured on the |
| 658 | // move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the flag clear it maps it elsewhere and fixes up | 658 | // Windows 11 (26200) loader with synthetic DLLs (#8847): with no relocation directory the flag refuses the move and |
| 659 | // nothing, as it does for a second copy of such a DLL. The DllMain stub stores through an absolute address in this | 659 | // an image with the flag clear is moved with nothing fixed up; with a relocation table the image is moved and fixed |
| 660 | // process, not in the image, so it runs correctly wherever the image lands. | 660 | // up whatever the flag says. The DllMain stub stores through an absolute address in this process, not in the image, |
| 661 | // so it runs correctly wherever the image lands. | ||
| 661 | void test_moved_without_relocations() { | 662 | void test_moved_without_relocations() { |
| 662 | auto img = make_image(); | 663 | auto img = make_image(); |
| 663 | auto* nt = nt_of(img); | 664 | auto* nt = nt_of(img); |
| @@ -704,13 +705,16 @@ void test_moved_without_relocations() { | |||
| 704 | } | 705 | } |
| 705 | if (held) VirtualFree(held, 0, MEM_RELEASE); | 706 | if (held) VirtualFree(held, 0, MEM_RELEASE); |
| 706 | { | 707 | { |
| 707 | // The flag only forbids moving: at a free base such an image maps as usual. | 708 | // At a free base such an image maps, as it did before. Not Windows parity: the Windows 11 loader refused these |
| 709 | // DLLs (error 487) at a free base too, for a reason not yet known (#8847); runtime/README.md keeps the question. | ||
| 708 | auto stripped = img; | 710 | auto stripped = img; |
| 709 | nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; | 711 | nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; |
| 710 | expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED maps at its free base", stripped); | 712 | expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED and no relocations maps at its free base", stripped); |
| 711 | } | 713 | } |
| 712 | { | 714 | { |
| 713 | // Windows refuses on the flag, whatever the relocation directory holds. ImageBase 0 makes the loader move it. | 715 | // With a relocation table the flag does not stop the move: the table is applied, as Windows does it. |
| 716 | // ImageBase 0 makes the loader move it; the one DIR64 entry points at the qword at RVA 0x1100. | ||
| 717 | const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is moved and fixed up"; | ||
| 714 | auto stripped = make_image(); | 718 | auto stripped = make_image(); |
| 715 | auto* snt = nt_of(stripped); | 719 | auto* snt = nt_of(stripped); |
| 716 | snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; | 720 | snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; |
| @@ -719,8 +723,21 @@ void test_moved_without_relocations() { | |||
| 719 | auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200); | 723 | auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200); |
| 720 | block->VirtualAddress = 0x1000; | 724 | block->VirtualAddress = 0x1000; |
| 721 | block->SizeOfBlock = 0x0A; | 725 | block->SizeOfBlock = 0x0A; |
| 722 | reinterpret_cast<uint16_t*>(block + 1)[0] = IMAGE_REL_BASED_DIR64 << 12; | 726 | reinterpret_cast<uint16_t*>(block + 1)[0] = (IMAGE_REL_BASED_DIR64 << 12) | 0x100; |
| 723 | expect_rejects("an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is not moved", stripped); | 727 | uint64_t original; |
| 728 | std::memcpy(&original, stripped.data() + 0x300, sizeof original); | ||
| 729 | auto p = write_image(stripped); | ||
| 730 | try { | ||
| 731 | rt::Loader loader(g_dir); | ||
| 732 | rt::Module& m = loader.load(p); | ||
| 733 | uint64_t moved; | ||
| 734 | std::memcpy(&moved, m.base + 0x1100, sizeof moved); | ||
| 735 | if (!m.relocated) failed(what, "it was not moved"); | ||
| 736 | else if (moved != original + reinterpret_cast<uint64_t>(m.base)) failed(what, "the pointer wasn't fixed up"); | ||
| 737 | else pass(what); | ||
| 738 | } catch (const std::exception& e) { | ||
| 739 | failed(what, e.what()); | ||
| 740 | } | ||
| 724 | } | 741 | } |
| 725 | } | 742 | } |
| 726 | 743 | ||