-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24c9c0f
commit 9e22967
Showing
8 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
|
||
namespace LibCpp2IL.MachO | ||
{ | ||
public class MachODynamicLinkerCommand : ReadableClass | ||
{ | ||
public int RebaseOffset; | ||
public int RebaseSize; | ||
public int BindOffset; | ||
public int BindSize; | ||
public int WeakBindOffset; | ||
public int WeakBindSize; | ||
public int LazyBindOffset; | ||
public int LazyBindSize; | ||
public int ExportOffset; | ||
public int ExportSize; | ||
|
||
public MachOExportEntry[] Exports = Array.Empty<MachOExportEntry>(); | ||
|
||
public override void Read(ClassReadingBinaryReader reader) | ||
{ | ||
RebaseOffset = reader.ReadInt32(); | ||
RebaseSize = reader.ReadInt32(); | ||
BindOffset = reader.ReadInt32(); | ||
BindSize = reader.ReadInt32(); | ||
WeakBindOffset = reader.ReadInt32(); | ||
WeakBindSize = reader.ReadInt32(); | ||
LazyBindOffset = reader.ReadInt32(); | ||
LazyBindSize = reader.ReadInt32(); | ||
ExportOffset = reader.ReadInt32(); | ||
ExportSize = reader.ReadInt32(); | ||
|
||
var returnTo = reader.BaseStream.Position; | ||
|
||
reader.BaseStream.Position = ExportOffset; | ||
|
||
var exports = new MachOExportTrie(reader); | ||
Exports = exports.Entries.ToArray(); | ||
|
||
reader.BaseStream.Position = returnTo; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace LibCpp2IL.MachO | ||
{ | ||
public class MachOExportEntry | ||
{ | ||
public string Name; | ||
public long Address; | ||
public long Flags; | ||
public long Other; | ||
public string? ImportName; | ||
|
||
public MachOExportEntry(string name, long address, long flags, long other, string? importName) | ||
{ | ||
Name = name; | ||
Address = address; | ||
Flags = flags; | ||
Other = other; | ||
ImportName = importName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace LibCpp2IL.MachO | ||
{ | ||
public class MachOExportTrie | ||
{ | ||
public List<MachOExportEntry> Entries = new(); | ||
|
||
private ClassReadingBinaryReader _reader; | ||
private long _basePtr; | ||
|
||
public MachOExportTrie(ClassReadingBinaryReader reader) | ||
{ | ||
_reader = reader; | ||
_basePtr = reader.BaseStream.Position; | ||
|
||
var children = ParseNode("", 0); | ||
while (children.Count > 0) | ||
{ | ||
var current = children[0]; | ||
children.RemoveAt(0); | ||
children.AddRange(ParseNode(current.Name, current.Offset)); | ||
} | ||
} | ||
|
||
private List<Node> ParseNode(string name, int offset) | ||
{ | ||
var children = new List<Node>(); | ||
_reader.BaseStream.Position = _basePtr + offset; | ||
|
||
var terminalSize = _reader.BaseStream.ReadLEB128Unsigned(); | ||
var childrenIndex = _reader.BaseStream.Position + (long)terminalSize; | ||
if (terminalSize != 0) | ||
{ | ||
var flags = (ExportFlags) _reader.BaseStream.ReadLEB128Unsigned(); | ||
var address = 0L; | ||
var other = 0L; | ||
string? importName = null; | ||
|
||
if ((flags & ExportFlags.ReExport) != 0) | ||
{ | ||
other = _reader.BaseStream.ReadLEB128Signed(); | ||
importName = _reader.ReadStringToNullAtCurrentPos(); | ||
} | ||
else | ||
{ | ||
address = _reader.BaseStream.ReadLEB128Signed(); | ||
if ((flags & ExportFlags.StubAndResolver) != 0) | ||
other = _reader.BaseStream.ReadLEB128Signed(); | ||
} | ||
|
||
Entries.Add(new(name, address, (long) flags, other, importName)); | ||
} | ||
|
||
_reader.BaseStream.Position = childrenIndex; | ||
var numChildren = _reader.BaseStream.ReadLEB128Unsigned(); | ||
for (var i = 0ul; i < numChildren; i++) | ||
{ | ||
var childName = _reader.ReadStringToNullAtCurrentPos(); | ||
var childOffset = _reader.BaseStream.ReadLEB128Unsigned(); | ||
children.Add(new Node {Name = name + childName, Offset = (int) childOffset}); | ||
} | ||
|
||
return children; | ||
} | ||
|
||
[Flags] | ||
private enum ExportFlags | ||
{ | ||
KindRegular = 0, | ||
KindThreadLocal = 1, | ||
KindAbsolute = 2, | ||
WeakDefinition = 4, | ||
ReExport = 8, | ||
StubAndResolver = 0x10 | ||
} | ||
|
||
private struct Node | ||
{ | ||
public string Name; | ||
public int Offset; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace LibCpp2IL.MachO | ||
{ | ||
public class MachOSymtabCommand : ReadableClass | ||
{ | ||
public uint SymbolTableOffset; | ||
public uint NumSymbols; | ||
public uint StringTableOffset; | ||
public uint StringTableSize; | ||
|
||
public MachOSymtabEntry[] Symbols = Array.Empty<MachOSymtabEntry>(); | ||
|
||
public override void Read(ClassReadingBinaryReader reader) | ||
{ | ||
SymbolTableOffset = reader.ReadUInt32(); | ||
NumSymbols = reader.ReadUInt32(); | ||
StringTableOffset = reader.ReadUInt32(); | ||
StringTableSize = reader.ReadUInt32(); | ||
|
||
var returnTo = reader.BaseStream.Position; | ||
|
||
reader.BaseStream.Position = SymbolTableOffset; | ||
|
||
Symbols = new MachOSymtabEntry[NumSymbols]; | ||
for (var i = 0; i < NumSymbols; i++) | ||
{ | ||
Symbols[i] = new(); | ||
Symbols[i].Read(reader, this); | ||
} | ||
|
||
reader.BaseStream.Position = returnTo; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace LibCpp2IL.MachO | ||
{ | ||
public class MachOSymtabEntry | ||
{ | ||
public uint NameOffset; | ||
public byte Type; | ||
public byte Section; | ||
public ushort Description; | ||
public ulong Value; // Architecture sized | ||
|
||
public string Name; | ||
|
||
public bool IsExternal => (Type & 0b1) == 0b1; | ||
public bool IsSymbolicDebugging => (Type & 0b1110_0000) != 0; | ||
public bool IsPrivateExternal => (Type & 0b0001_0000) == 0b0001_0000; | ||
|
||
private byte TypeBits => (byte) (Type & 0b1110); | ||
|
||
public bool IsTypeUndefined => Section == 0 && TypeBits == 0b0000; | ||
public bool IsTypeAbsolute => Section == 0 && TypeBits == 0b0010; | ||
public bool IsTypePreboundUndefined => Section == 0 && TypeBits == 0b1100; | ||
public bool IsTypeIndirect => Section == 0 && TypeBits == 0b1010; | ||
public bool IsTypeSection => TypeBits == 0b1110; | ||
|
||
public void Read(ClassReadingBinaryReader reader, MachOSymtabCommand machOSymtabCommand) | ||
{ | ||
NameOffset = reader.ReadUInt32(); | ||
Type = reader.ReadByte(); | ||
Section = reader.ReadByte(); | ||
Description = reader.ReadUInt16(); | ||
Value = reader.ReadNUint(); | ||
|
||
var returnTo = reader.BaseStream.Position; | ||
Name = reader.ReadStringToNullNoLock(machOSymtabCommand.StringTableOffset + NameOffset); | ||
reader.BaseStream.Position = returnTo; | ||
} | ||
} | ||
} |