Skip to content

Commit c849bf3

Browse files
ds5678SamboyCoding
authored andcommitted
Remove try catch in Il2CppBinary::TryMapRawAddressToVirtual
1 parent 1be7b0a commit c849bf3

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

Cpp2IL.Plugin.StrippedCodeRegSupport/StrippedCodeRegSupportPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Cpp2IL.Core.Api;
1+
using Cpp2IL.Core.Api;
22
using Cpp2IL.Core.Attributes;
33
using Cpp2IL.Plugin.StrippedCodeRegSupport;
44
using LibCpp2IL;
@@ -40,7 +40,7 @@ private void OnReadFail(Il2CppBinary binary, Il2CppMetadata metadata, ref Il2Cpp
4040
//We can piggyback off BinarySearcher:
4141
var searcher = new BinarySearcher(binary, metadata.MethodDefinitionCount, metadata.TypeDefinitionCount);
4242

43-
var mscorlibs = searcher.FindAllStrings("mscorlib.dll\0").Select(binary.MapRawAddressToVirtual).ToList();
43+
var mscorlibs = searcher.FindAllStrings("mscorlib.dll\0").Select(idx => binary.MapRawAddressToVirtual(idx)).ToList();
4444

4545
Logger.VerboseNewline($"Found {mscorlibs.Count} occurrences of mscorlib.dll: [{string.Join(", ", mscorlibs.Select(p => p.ToString("X")))}]");
4646

LibCpp2IL/Elf/ElfFile.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,20 @@ public override long MapVirtualAddressToRaw(ulong addr, bool throwOnError = true
709709
return (long)(addr - (section.VirtualAddress - section.RawAddress));
710710
}
711711

712-
public override ulong MapRawAddressToVirtual(uint offset)
712+
public override ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true)
713713
{
714714
if (relocationBlocks.Any(b => b.start <= offset && b.end > offset))
715-
throw new InvalidOperationException("Attempt to map a relocation block to a virtual address");
715+
if (throwOnError)
716+
throw new InvalidOperationException("Attempt to map a relocation block to a virtual address");
717+
else
718+
return 0;
716719

717-
var section = _elfProgramHeaderEntries.First(x => offset >= x.RawAddress && offset < x.RawAddress + x.RawSize);
720+
var section = _elfProgramHeaderEntries.FirstOrDefault(x => offset >= x.RawAddress && offset < x.RawAddress + x.RawSize);
721+
if (section == null)
722+
if (throwOnError)
723+
throw new InvalidOperationException($"No entry in the Elf PHT contains raw address 0x{offset:X}");
724+
else
725+
return 0;
718726

719727
return section.VirtualAddress + offset - section.RawAddress;
720728
}

LibCpp2IL/Il2CppBinary.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,13 @@ private Il2CppVariableWidthIndex<Il2CppMethodDefinition> GetGenericMethodFromInd
327327

328328
public abstract byte GetByteAtRawAddress(ulong addr);
329329
public abstract long MapVirtualAddressToRaw(ulong uiAddr, bool throwOnError = true);
330-
public abstract ulong MapRawAddressToVirtual(uint offset);
330+
public abstract ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true);
331331
public abstract ulong GetRva(ulong pointer);
332332

333333
public bool TryMapRawAddressToVirtual(in uint offset, out ulong va)
334334
{
335-
try
336-
{
337-
va = MapRawAddressToVirtual(offset);
338-
return true;
339-
}
340-
catch (Exception)
341-
{
342-
va = 0;
343-
return false;
344-
}
335+
va = MapRawAddressToVirtual(offset, false);
336+
return va != 0;
345337
}
346338

347339
public bool TryMapVirtualAddressToRaw(ulong virtAddr, out long result)

LibCpp2IL/MachO/MachOFile.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,23 @@ public override long MapVirtualAddressToRaw(ulong uiAddr, bool throwOnError = tr
111111
return (long)(sec.Offset + (uiAddr - sec.Address));
112112
}
113113

114-
public override ulong MapRawAddressToVirtual(uint offset)
114+
public override ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true)
115115
{
116116
var sec = Sections64.FirstOrDefault(s => s.Offset <= offset && offset < s.Offset + s.Size);
117117

118118
if (sec == null)
119-
throw new($"Could not find section for raw address 0x{offset:X}");
119+
if (throwOnError)
120+
throw new($"Could not find section for raw address 0x{offset:X}");
121+
else
122+
return 0;
120123

121124
return sec.Address + (offset - sec.Offset);
122125
}
123126

124127
public override ulong GetRva(ulong pointer)
125128
{
126-
return pointer; //TODO?
129+
// Mach-O doesn't have RVAs and instead uses virtual addresses, so we can just return the pointer as-is.
130+
return pointer;
127131
}
128132

129133
public override byte[] GetRawBinaryContent() => _raw;

LibCpp2IL/NintendoSwitch/NsoFile.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ public override long MapVirtualAddressToRaw(ulong addr, bool throwOnError = true
319319
return (long)(addr - (segment.MemoryOffset + NsoGlobalOffset) + segment.FileOffset);
320320
}
321321

322-
public override ulong MapRawAddressToVirtual(uint offset)
322+
public override ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true)
323323
{
324324
var segment = segments.FirstOrDefault(x => offset >= x.FileOffset && offset < x.FileOffset + x.DecompressedSize);
325325
if (segment == null)
326-
{
327-
return 0;
328-
}
326+
if (throwOnError)
327+
throw new InvalidOperationException($"NSO: Offset 0x{offset:X} is not present in any of the segments. Known segment starts are (hex) {string.Join(", ", segments.Select(s => s.FileOffset.ToString("X")))}");
328+
else
329+
return 0;
329330

330331
return offset - segment.FileOffset + (NsoGlobalOffset + segment.MemoryOffset);
331332
}

LibCpp2IL/PE/PE.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ public override long MapVirtualAddressToRaw(ulong uiAddr, bool throwOnError = tr
110110
return addr - (section.VirtualAddress - section.PointerToRawData);
111111
}
112112

113-
public override ulong MapRawAddressToVirtual(uint offset)
113+
public override ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true)
114114
{
115-
var section = peSectionHeaders.First(x => offset >= x.PointerToRawData && offset < x.PointerToRawData + x.SizeOfRawData);
115+
var section = peSectionHeaders.FirstOrDefault(x => offset >= x.PointerToRawData && offset < x.PointerToRawData + x.SizeOfRawData);
116+
if (section == null)
117+
if (throwOnError)
118+
throw new InvalidOperationException($"Provided offset, 0x{offset:X}, does not fall within any section of the PE file.");
119+
else
120+
return 0;
116121

117122
return peImageBase + section.VirtualAddress + offset - section.PointerToRawData;
118123
}

LibCpp2IL/Wasm/WasmFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public override long MapVirtualAddressToRaw(ulong uiAddr, bool throwOnError = tr
219219
return (long)uiAddr;
220220
}
221221

222-
public override ulong MapRawAddressToVirtual(uint offset)
222+
public override ulong MapRawAddressToVirtual(uint offset, bool throwOnError = true)
223223
{
224224
var data = DataSection;
225225
if (offset > data.Pointer && offset < data.Pointer + (long)data.Size)

0 commit comments

Comments
 (0)