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:
authoraslan2142 <jan.trencansky@protonmail.com>2026-11-25 23:53:26 +0100
committerGitHub <noreply@github.com>2026-11-25 23:53:26 +0100
commit21d3662ad8497154a07a6ad7c2bf72e60fdbb9fa (patch)
tree74ac86ef40be0edb83d1ef9821358d2bba455652 /runtime/src/pe_loader.cpp
parent5fab9396763ca4d052af8abc71f29ad200e7b5ef (diff)
parentbb256ebbf32d7493e3d867923ec737e60d3f9f9d (diff)
Merge pull request #634 from aslan2142/loader-rebase-without-reloc-dir
OLD-8847: restore old upstream PR 8847 - pe_loader: move an image with no relocation directory; refuse only on IMAGE_FILE_RELOCS_STRIPPED
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 3558cffb5..840d02541 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;