make MapFileOffsetToVA return ulong.MaxValue instead of throwing to improve perf and debugging experience, other elf improvements

This commit is contained in:
LukeFZ
2023-11-29 19:50:56 +01:00
parent 7c8e0eee3e
commit e6355bb1b4
2 changed files with 865 additions and 850 deletions

View File

@@ -400,6 +400,8 @@ namespace Il2CppInspector
pTables.Add((SHT_DYNSYM.sh_offset, conv.Div(SHT_DYNSYM.sh_size, SHT_DYNSYM.sh_entsize), SHT_STRTAB.sh_offset)); pTables.Add((SHT_DYNSYM.sh_offset, conv.Div(SHT_DYNSYM.sh_size, SHT_DYNSYM.sh_entsize), SHT_STRTAB.sh_offset));
} }
var maxStrtabAddr = conv.Add(SHT_STRTAB.sh_offset, SHT_STRTAB.sh_size);
// Symbol table in dynamic section (DT_SYMTAB) // Symbol table in dynamic section (DT_SYMTAB)
// Normally the same as .dynsym except that .dynsym may be removed in stripped binaries // Normally the same as .dynsym except that .dynsym may be removed in stripped binaries
@@ -407,8 +409,10 @@ namespace Il2CppInspector
if (GetDynamicEntry(Elf.DT_STRTAB) is elf_dynamic<TWord> DT_STRTAB) { if (GetDynamicEntry(Elf.DT_STRTAB) is elf_dynamic<TWord> DT_STRTAB) {
if (GetDynamicEntry(Elf.DT_SYMTAB) is elf_dynamic<TWord> DT_SYMTAB) { if (GetDynamicEntry(Elf.DT_SYMTAB) is elf_dynamic<TWord> DT_SYMTAB) {
// Find the next pointer in the dynamic table to calculate the length of the symbol table // Find the next pointer in the dynamic table to calculate the length of the symbol table
var end = (from x in DynamicTable where conv.Gt(x.d_un, DT_SYMTAB.d_un) orderby x.d_un select x).First().d_un; var orderedTable = (from x in DynamicTable where conv.Gt(x.d_un, DT_SYMTAB.d_un) orderby x.d_un select x).ToList();
if (orderedTable.Count != 0)
{
var end = orderedTable.First().d_un;
// Dynamic symbol table // Dynamic symbol table
pTables.Add(( pTables.Add((
conv.FromUInt(MapVATR(conv.ULong(DT_SYMTAB.d_un))), conv.FromUInt(MapVATR(conv.ULong(DT_SYMTAB.d_un))),
@@ -417,6 +421,7 @@ namespace Il2CppInspector
)); ));
} }
} }
}
// Now iterate through all of the symbol and string tables we found to build a full list // Now iterate through all of the symbol and string tables we found to build a full list
symbolTable.Clear(); symbolTable.Clear();
@@ -425,7 +430,11 @@ namespace Il2CppInspector
foreach (var pTab in pTables) { foreach (var pTab in pTables) {
var symbol_table = ReadArray<TSym>(conv.Long(pTab.offset), conv.Int(pTab.count)); var symbol_table = ReadArray<TSym>(conv.Long(pTab.offset), conv.Int(pTab.count));
foreach (var symbol in symbol_table) { foreach (var symbol in symbol_table)
{
if (conv.Gt(conv.FromUInt(symbol.st_name), maxStrtabAddr))
break; // Skip invalid symbol table indices, since we might read past it on obfuscated binaries
string name = string.Empty; string name = string.Empty;
try { try {
name = ReadNullTerminatedString(conv.Long(pTab.strings) + symbol.st_name); name = ReadNullTerminatedString(conv.Long(pTab.strings) + symbol.st_name);
@@ -520,7 +529,7 @@ namespace Il2CppInspector
public override ulong MapFileOffsetToVA(uint offset) { public override ulong MapFileOffsetToVA(uint offset) {
// Exclude relocation areas // Exclude relocation areas
if (reverseMapExclusions.Any(r => offset >= r.Start && offset <= r.End)) if (reverseMapExclusions.Any(r => offset >= r.Start && offset <= r.End))
throw new InvalidOperationException("Attempt to map to a relocation address"); return ulong.MaxValue;
var section = PHT.First(x => offset >= conv.Int(x.p_offset) && offset < conv.Int(x.p_offset) + conv.Int(x.p_filesz)); var section = PHT.First(x => offset >= conv.Int(x.p_offset) && offset < conv.Int(x.p_offset) + conv.Int(x.p_filesz));
return conv.ULong(section.p_vaddr) + offset - conv.ULong(section.p_offset); return conv.ULong(section.p_vaddr) + offset - conv.ULong(section.p_offset);

View File

@@ -272,6 +272,12 @@ namespace Il2CppInspector
public bool TryMapFileOffsetToVA(uint offset, out ulong va) { public bool TryMapFileOffsetToVA(uint offset, out ulong va) {
try { try {
va = MapFileOffsetToVA(offset); va = MapFileOffsetToVA(offset);
if (va == ulong.MaxValue)
{
va = 0;
return false;
}
return true; return true;
} }
catch (InvalidOperationException) { catch (InvalidOperationException) {