Skip to content

Commit

Permalink
Fix iOS file loading and account for if the next function is not mapp…
Browse files Browse the repository at this point in the history
…able to a raw address in the binary
  • Loading branch information
gabriel-nsiqueira authored and SamboyCoding committed Aug 9, 2024
1 parent c568b9e commit e26ecc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cpp2IL.Core/Utils/MiscUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public static ulong GetAddressOfNextFunctionStart(ulong current)
if (ret <= current && upper == _allKnownFunctionStarts.Count - 1)
return 0;

if (!LibCpp2IlMain.Binary!.TryMapVirtualAddressToRaw(ret, out _))
return 0;

return ret;
}

Expand Down
17 changes: 16 additions & 1 deletion LibCpp2IL/MachO/MachOFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,23 @@ public MachOFile(MemoryStream input) : base(input)

var dyldData = _loadCommands.FirstOrDefault(c => c.Command is LoadCommandId.LC_DYLD_INFO or LoadCommandId.LC_DYLD_INFO_ONLY)?.CommandData as MachODynamicLinkerCommand;
var exports = dyldData?.Exports ?? Array.Empty<MachOExportEntry>();
// DEBUG
// var debugDict = new Dictionary<long, string>();
// for (int index = 0; index < exports.Length; ++index) {
// var export = exports[index];
// Console.WriteLine($"Export: {export.Name[1..]} -> 0x{export.Address:X}");
// // detect duplicate
// if (debugDict.ContainsKey(export.Address) && debugDict[export.Address] != export.Name[1..]) {
// Console.WriteLine($"Duplicate: {export.Name[1..]} -> 0x{export.Address:X}");
// }
// debugDict[export.Address] = export.Name[1..];
// }
_exportAddressesDict = exports.ToDictionary(e => e.Name[1..], e => e.Address); //Skip the first character, which is a leading underscore inserted by the compiler
_exportNamesDict = _exportAddressesDict.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
_exportNamesDict = new Dictionary<long, string>();
foreach (var export in exports) // there may be duplicate names
{
_exportNamesDict[export.Address] = export.Name[1..];
}

LibLogger.VerboseNewline($"Found {_exportAddressesDict.Count} exports in the DYLD info load command.");

Expand Down

0 comments on commit e26ecc9

Please sign in to comment.