From 320bb2fa714b4d2e6d02757817e25ef0586219cd Mon Sep 17 00:00:00 2001 From: Luke <17146677+LukeFZ@users.noreply.github.com> Date: Sat, 2 Dec 2023 16:27:50 +0100 Subject: [PATCH] ELF: Properly handle address outside of file-backed regions --- 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)); }