Skip to content

Commit 582c58c

Browse files
authored
Process addresses in the export trie as ULEB128 instead of LEB128 (SamboyCoding#427)
1 parent 4409a1b commit 582c58c

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace LibCpp2IL.MachO;
22

3-
public class MachOExportEntry(string name, long address, long flags, long other, string? importName)
3+
public class MachOExportEntry(string name, ulong address, ulong flags, ulong other, string? importName)
44
{
55
public string Name = name;
6-
public long Address = address;
7-
public long Flags = flags;
8-
public long Other = other;
6+
public ulong Address = address;
7+
public ulong Flags = flags;
8+
public ulong Other = other;
99
public string? ImportName = importName;
1010
}

LibCpp2IL/MachO/MachOExportTrie.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ private List<Node> ParseNode(string name, int offset)
3434
if (terminalSize != 0)
3535
{
3636
var flags = (ExportFlags)_reader.BaseStream.ReadLEB128Unsigned();
37-
var address = 0L;
38-
var other = 0L;
37+
var address = 0UL;
38+
var other = 0UL;
3939
string? importName = null;
4040

4141
if ((flags & ExportFlags.ReExport) != 0)
4242
{
43-
other = _reader.BaseStream.ReadLEB128Signed();
43+
other = _reader.BaseStream.ReadLEB128Unsigned();
4444
importName = _reader.ReadStringToNullAtCurrentPos();
4545
}
4646
else
4747
{
48-
address = _reader.BaseStream.ReadLEB128Signed();
48+
address = _reader.BaseStream.ReadLEB128Unsigned();
4949
if ((flags & ExportFlags.StubAndResolver) != 0)
50-
other = _reader.BaseStream.ReadLEB128Signed();
50+
other = _reader.BaseStream.ReadLEB128Unsigned();
5151
}
5252

53-
Entries.Add(new(name, address, (long)flags, other, importName));
53+
Entries.Add(new(name, address, (ulong)flags, other, importName));
5454
}
5555

5656
_reader.BaseStream.Position = childrenIndex;

LibCpp2IL/MachO/MachOFile.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.IO;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using LibCpp2IL.Logging;
@@ -16,8 +15,8 @@ public class MachOFile : Il2CppBinary
1615

1716
private readonly MachOSegmentCommand[] Segments64;
1817
private readonly MachOSection[] Sections64;
19-
private readonly Dictionary<string, long> _exportAddressesDict;
20-
private readonly Dictionary<long, string> _exportNamesDict;
18+
private readonly Dictionary<string, ulong> _exportAddressesDict;
19+
private readonly Dictionary<ulong, string> _exportNamesDict;
2120

2221
public MachOFile(MemoryStream input) : base(input)
2322
{
@@ -79,7 +78,7 @@ public MachOFile(MemoryStream input) : base(input)
7978
var exports = dyldData?.Exports ?? [];
8079

8180
_exportAddressesDict = exports.ToDictionary(e => e.Name[1..], e => e.Address); //Skip the first character, which is a leading underscore inserted by the compiler
82-
_exportNamesDict = new Dictionary<long, string>();
81+
_exportNamesDict = new Dictionary<ulong, string>();
8382
foreach (var export in exports) // there may be duplicate names
8483
{
8584
_exportNamesDict[export.Address] = export.Name[1..];
@@ -134,19 +133,19 @@ public override ulong GetVirtualAddressOfExportedFunctionByName(string toFind)
134133
if (!_exportAddressesDict.TryGetValue(toFind, out var addr))
135134
return 0;
136135

137-
return (ulong)addr;
136+
return addr;
138137
}
139138

140-
public override bool IsExportedFunction(ulong addr) => _exportNamesDict.ContainsKey((long)addr);
139+
public override bool IsExportedFunction(ulong addr) => _exportNamesDict.ContainsKey(addr);
141140

142141
public override bool TryGetExportedFunctionName(ulong addr, [NotNullWhen(true)] out string? name)
143142
{
144-
return _exportNamesDict.TryGetValue((long)addr, out name);
143+
return _exportNamesDict.TryGetValue(addr, out name);
145144
}
146145

147146
public override IEnumerable<KeyValuePair<string, ulong>> GetExportedFunctions()
148147
{
149-
return _exportAddressesDict.Select(pair => new KeyValuePair<string, ulong>(pair.Key, (ulong)pair.Value));
148+
return _exportAddressesDict.Select(pair => new KeyValuePair<string, ulong>(pair.Key, pair.Value));
150149
}
151150

152151
private MachOSection GetTextSection64()

0 commit comments

Comments
 (0)