From b28bb93ccb406d640e69f2603b8296c1f4cecb68 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 11 Sep 2025 10:11:05 -0400 Subject: [PATCH] Handle non-section data with valid RVA --- .../Extensions.PortableExecutable.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/SabreTools.Serialization/Extensions.PortableExecutable.cs b/SabreTools.Serialization/Extensions.PortableExecutable.cs index e32464fc..0962a503 100644 --- a/SabreTools.Serialization/Extensions.PortableExecutable.cs +++ b/SabreTools.Serialization/Extensions.PortableExecutable.cs @@ -33,6 +33,7 @@ namespace SabreTools.Serialization return rva - matchingSection.VirtualAddress + matchingSection.PointerToRawData; // Loop through all of the sections + uint maxVirtualAddress = 0, maxRawPointer = 0; for (int i = 0; i < sections.Length; i++) { // If the section "starts" at 0, just skip it @@ -44,6 +45,13 @@ namespace SabreTools.Serialization if (rva < section.VirtualAddress) continue; + // Cache the maximum matching section data, in case of a miss + if (rva >= section.VirtualAddress) + { + maxVirtualAddress = section.VirtualAddress; + maxRawPointer = section.PointerToRawData; + } + // Attempt to derive the physical address from the current section if (section.VirtualSize != 0 && rva <= section.VirtualAddress + section.VirtualSize) return rva - section.VirtualAddress + section.PointerToRawData; @@ -51,7 +59,7 @@ namespace SabreTools.Serialization return rva - section.VirtualAddress + section.PointerToRawData; } - return 0; + return maxRawPointer != 0 ? rva - maxVirtualAddress + maxRawPointer : 0; } ///