From e0e8d052eafe04ac27d98bd1f4e8ce7b69da2423 Mon Sep 17 00:00:00 2001 From: LukeFZ <17146677+LukeFZ@users.noreply.github.com> Date: Sat, 9 Nov 2024 15:43:22 +0100 Subject: [PATCH] make TryMapVATR overrideable and implement it for ELFs --- .../FileFormatStreams/ElfReader.cs | 21 +++++++++++++++++-- .../FileFormatStreams/FileFormatStream.cs | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Il2CppInspector.Common/FileFormatStreams/ElfReader.cs b/Il2CppInspector.Common/FileFormatStreams/ElfReader.cs index cab3c399..62d56547 100644 --- a/Il2CppInspector.Common/FileFormatStreams/ElfReader.cs +++ b/Il2CppInspector.Common/FileFormatStreams/ElfReader.cs @@ -528,11 +528,28 @@ public override IEnumerable
GetSections() { // Note if uiAddr is a valid segment but filesz < memsz and the adjusted uiAddr falls between the range of filesz and memsz, // an exception will be thrown. This area of memory is assumed to contain all zeroes. public override uint MapVATR(ulong uiAddr) { + // Additions in the argument to MapVATR may cause an overflow which should be discarded for 32-bit files + if (!TryMapVATR(uiAddr, out var offset)) + throw new InvalidOperationException("Failed to map virtual address"); + + return offset; + } + + public override bool TryMapVATR(ulong uiAddr, out uint fileOffset) + { // Additions in the argument to MapVATR may cause an overflow which should be discarded for 32-bit files if (Bits == 32) uiAddr &= 0xffff_ffff; - var program_header_table = this.PHT.First(x => uiAddr >= conv.ULong(x.p_vaddr) && uiAddr <= conv.ULong(conv.Add(x.p_vaddr, x.p_filesz))); - return (uint) (uiAddr - conv.ULong(conv.Sub(program_header_table.p_vaddr, program_header_table.p_offset))); + + var phtEntry = PHT.FirstOrDefault(x => uiAddr >= conv.ULong(x.p_vaddr) && uiAddr <= conv.ULong(conv.Add(x.p_vaddr, x.p_filesz))); + if (phtEntry == null) + { + fileOffset = 0; + return false; + } + + fileOffset = (uint)(uiAddr - conv.ULong(conv.Sub(phtEntry.p_vaddr, phtEntry.p_offset))); + return true; } public override ulong MapFileOffsetToVA(uint offset) { diff --git a/Il2CppInspector.Common/FileFormatStreams/FileFormatStream.cs b/Il2CppInspector.Common/FileFormatStreams/FileFormatStream.cs index f7db93fa..22a61f16 100644 --- a/Il2CppInspector.Common/FileFormatStreams/FileFormatStream.cs +++ b/Il2CppInspector.Common/FileFormatStreams/FileFormatStream.cs @@ -284,7 +284,7 @@ public bool TryGetSections(out IEnumerable
sections) { public virtual uint MapVATR(ulong uiAddr) => (uint) uiAddr; // Try to map an RVA to an offset in the file image - public bool TryMapVATR(ulong uiAddr, out uint fileOffset) { + public virtual bool TryMapVATR(ulong uiAddr, out uint fileOffset) { try { fileOffset = MapVATR(uiAddr); return true;