From fd42ad0cfa936e96be0a8eb0b5146a0ac9f3a8b1 Mon Sep 17 00:00:00 2001 From: Luke <17146677+LukeFZ@users.noreply.github.com> Date: Mon, 1 Jan 2024 04:08:12 +0100 Subject: [PATCH] ELF: Properly handle address outside of file-backed regions (#240) --- LibCpp2IL/Elf/ElfFile.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/LibCpp2IL/Elf/ElfFile.cs b/LibCpp2IL/Elf/ElfFile.cs index 65c5c997..7b5e4c11 100644 --- a/LibCpp2IL/Elf/ElfFile.cs +++ b/LibCpp2IL/Elf/ElfFile.cs @@ -691,6 +691,13 @@ public override long MapVirtualAddressToRaw(ulong addr, bool throwOnError = true throw new InvalidOperationException($"No entry in the Elf PHT contains virtual address 0x{addr:X}"); else return VirtToRawInvalidNoMatch; + + if (addr >= section.VirtualAddress + section.RawSize) + if (throwOnError) + throw new InvalidOperationException( + $"Virtual address {section.VirtualAddress:X} is located outside of the file-backed portion of Elf PHT section at 0x{section.VirtualAddress:X}"); + else + return VirtToRawInvalidOutOfBounds; return (long) (addr - (section.VirtualAddress - section.RawAddress)); }