From eae22cd05948833d622791ca0764960b797f6539 Mon Sep 17 00:00:00 2001 From: astunm Date: Wed, 11 Nov 2026 08:04:43 -0500 Subject: [PATCH] Parse relocation directories starting inside a section --- pe-parser-library/src/parse.cpp | 3 ++- tests/directory_bounds_test.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pe-parser-library/src/parse.cpp b/pe-parser-library/src/parse.cpp index 194cbf2..b03e268 100644 --- a/pe-parser-library/src/parse.cpp +++ b/pe-parser-library/src/parse.cpp @@ -1816,7 +1816,8 @@ bool getRelocations(parsed_pe *p) { auto rvaofft = static_cast(vaAddr - d.sectionBase); - while (rvaofft < relocDir.Size) { + const auto endOffset = static_cast(rvaofft) + relocDir.Size; + while (rvaofft < endOffset) { std::uint32_t pageRva; std::uint32_t blockSize; diff --git a/tests/directory_bounds_test.cpp b/tests/directory_bounds_test.cpp index 2d5e3b7..99cda3a 100644 --- a/tests/directory_bounds_test.cpp +++ b/tests/directory_bounds_test.cpp @@ -271,6 +271,35 @@ TEST_CASE("oversized section raw data range is rejected", "[sections]") { REQUIRE(GetPEErr() == PEERR_SECT); } +TEST_CASE("relocation directories can start inside a section", + "[relocations]") { + const auto directoryOffset = GENERATE(0U, 0x100U); + auto pe = makeMinimalPe(); + putDataDirectory(pe, DIR_BASERELOC, kSectionRva + directoryOffset, 24); + const auto rawOffset = sectionOffset(kSectionRva + directoryOffset); + put32(pe, rawOffset, 0x2000); + put32(pe, rawOffset + 4, 12); + put16(pe, rawOffset + 8, 0x3020); + put32(pe, rawOffset + 12, 0x3000); + put32(pe, rawOffset + 16, 12); + put16(pe, rawOffset + 20, 0x3030); + + auto p = parse(pe); + REQUIRE(p); + + std::vector addresses; + IterRelocs( + p.get(), + [](void *ctx, const VA &address, const reloc_type &type) { + if (type == RELOC_HIGHLOW) { + static_cast *>(ctx)->push_back(address); + } + return 0; + }, + &addresses); + REQUIRE(addresses == std::vector{0x2020, 0x3030}); +} + TEST_CASE("debug directory data can end at section boundary", "[debug]") { auto pe = makeMinimalPe(); putDataDirectory(pe, DIR_DEBUG, kDebugDirectoryRva, sizeof(debug_dir_entry));