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:
authorismailchaka1 <chakankar111@gmail.com>2026-11-22 02:37:52 +0000
committerismailchaka1 <chakankar111@gmail.com>2026-11-22 02:37:52 +0000
commit2b905ef6517ba13881b33b9dcbdb37c505f37607 (patch)
treee7cfe1f22fa2b0ec7dea5c885f99ef9ea3717bed /runtime/src/pe_loader.cpp
parent67204bf8c0471335d9046890288b33e8d5e2875b (diff)
Runtime: PE loader applies HIGHLOW (32-bit) base relocations
Loader::load threw "unsupported relocation type 3" on IMAGE_REL_BASED_HIGHLOW, which the x64 Windows loader applies: 32-bit absolute addresses, as /LARGEADDRESSAWARE:NO code has. They are now moved by the delta like DIR64 ones. Where the moved address no longer fits in 32 bits, Windows would truncate it; the loader refuses the image with "a 32-bit (HIGHLOW) relocation does not fit at this base" instead. Other 32-bit forms (HIGH, LOW, HIGHADJ) stay unsupported. test_pe_loader_bounds.cpp was never built: it had no CMake target. It now has one and runs under ctest as pe_loader_bounds (its existing 37 checks pass as they are). Five new checks build an image with a HIGHLOW entry: the dword moves by exactly the delta, an address past 4 GiB is refused, and HIGH is still rejected by name. Against the old loader, three of them fail with "unsupported relocation type 3".
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index 2d691862d..ef6fab7a8 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -314,6 +314,17 @@ Module& Loader::map_image(const std::filesystem::path& path) {
314 require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint64_t), m->size, 314 require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint64_t), m->size,
315 "a relocation"); 315 "a relocation");
316 *at<uint64_t>(m->base, block->VirtualAddress + off) += delta; 316 *at<uint64_t>(m->base, block->VirtualAddress + off) += delta;
317 } else if (type == IMAGE_REL_BASED_HIGHLOW) {
318 // A 32-bit absolute address (code built /LARGEADDRESSAWARE:NO), which the x64 Windows loader
319 // also applies. Where the new address no longer fits in 32 bits, Windows would truncate it; the
320 // image would then jump or read through a wrong pointer, so the load is refused instead.
321 require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint32_t),
322 m->size, "a relocation");
323 auto* slot = at<uint32_t>(m->base, block->VirtualAddress + off);
324 const int64_t moved = static_cast<int64_t>(*slot) + delta;
325 if (moved < 0 || moved > 0xFFFFFFFFll)
326 throw std::runtime_error(m->name + ": a 32-bit (HIGHLOW) relocation does not fit at this base");
327 *slot = static_cast<uint32_t>(moved);
317 } else if (type != IMAGE_REL_BASED_ABSOLUTE) { 328 } else if (type != IMAGE_REL_BASED_ABSOLUTE) {
318 throw std::runtime_error(m->name + ": unsupported relocation type " + std::to_string(type)); 329 throw std::runtime_error(m->name + ": unsupported relocation type " + std::to_string(type));
319 } 330 }