Skip to content

Commit d219946

Browse files
committed
Update Disarm
1 parent 9306f21 commit d219946

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

Cpp2IL.InstructionSets.ArmV8/ArmV8InstructionSet.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ public override Memory<byte> GetRawBytesForMethod(MethodAnalysisContext context,
2828
return LibCpp2IlMain.Binary!.GetRawBinaryContent().AsMemory(ptrAsInt, count);
2929
}
3030

31-
var result = ArmV8Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer);
32-
var endVa = result.EndVirtualAddress;
31+
ArmV8Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer, out var endVirtualAddress);
3332

3433
var start = (int) context.AppContext.Binary.MapVirtualAddressToRaw(context.UnderlyingPointer);
35-
var end = (int) context.AppContext.Binary.MapVirtualAddressToRaw(endVa);
36-
34+
var end = (int) context.AppContext.Binary.MapVirtualAddressToRaw(endVirtualAddress);
35+
3736
return context.AppContext.Binary.GetRawBinaryContent().AsMemory(start, end - start);
3837
}
3938

4039
public override List<InstructionSetIndependentInstruction> GetIsilFromMethod(MethodAnalysisContext context)
4140
{
42-
var result = ArmV8Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer);
41+
var instructions = ArmV8Utils.GetArm64MethodBodyAtVirtualAddress(context.UnderlyingPointer, out var endVirtualAddress);
4342

4443
throw new NotImplementedException();
4544
}
@@ -49,5 +48,5 @@ public override BaseKeyFunctionAddresses CreateKeyFunctionAddressesInstance()
4948
return new ArmV8KeyFunctionAddresses();
5049
}
5150

52-
public override string PrintAssembly(MethodAnalysisContext context) => string.Join("\n", Disassembler.Disassemble(context.RawBytes.Span, context.UnderlyingPointer).Instructions);
51+
public override string PrintAssembly(MethodAnalysisContext context) => string.Join("\n", Disassembler.Disassemble(context.RawBytes, context.UnderlyingPointer));
5352
}

Cpp2IL.InstructionSets.ArmV8/ArmV8KeyFunctionAddresses.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ namespace Cpp2IL.InstructionSets.ArmV8;
66

77
public class ArmV8KeyFunctionAddresses : BaseKeyFunctionAddresses
88
{
9-
private Arm64DisassemblyResult? _cachedDisassembledBytes;
9+
private List<Arm64Instruction>? _cachedDisassembledBytes;
1010

11-
private Arm64DisassemblyResult DisassembleTextSection()
11+
private List<Arm64Instruction> DisassembleTextSection()
1212
{
1313
if (_cachedDisassembledBytes == null)
1414
{
1515
var toDisasm = LibCpp2IlMain.Binary!.GetEntirePrimaryExecutableSection();
16-
_cachedDisassembledBytes = Disassembler.Disassemble(toDisasm, LibCpp2IlMain.Binary.GetVirtualAddressOfPrimaryExecutableSection());
16+
_cachedDisassembledBytes = Disassembler.Disassemble(toDisasm, LibCpp2IlMain.Binary.GetVirtualAddressOfPrimaryExecutableSection()).ToList();
1717
}
1818

19-
return _cachedDisassembledBytes.Value;
19+
return _cachedDisassembledBytes;
2020
}
2121

2222
protected override IEnumerable<ulong> FindAllThunkFunctions(ulong addr, uint maxBytesBack = 0, params ulong[] addressesToIgnore)
@@ -25,7 +25,7 @@ protected override IEnumerable<ulong> FindAllThunkFunctions(ulong addr, uint max
2525
var disassembly = DisassembleTextSection();
2626

2727
//Find all jumps to the target address
28-
var matchingJmps = disassembly.Instructions.Where(i => i.Mnemonic is Arm64Mnemonic.B or Arm64Mnemonic.BL && i.BranchTarget == addr).ToList();
28+
var matchingJmps = disassembly.Where(i => i.Mnemonic is Arm64Mnemonic.B or Arm64Mnemonic.BL && i.BranchTarget == addr).ToList();
2929

3030
foreach (var matchingJmp in matchingJmps)
3131
{
@@ -81,6 +81,6 @@ protected override int GetCallerCount(ulong toWhere)
8181
var disassembly = DisassembleTextSection();
8282

8383
//Find all jumps to the target address
84-
return disassembly.Instructions.Count(i => i.Mnemonic is Arm64Mnemonic.B or Arm64Mnemonic.BL && i.BranchTarget == toWhere);
84+
return disassembly.Count(i => i.Mnemonic is Arm64Mnemonic.B or Arm64Mnemonic.BL && i.BranchTarget == toWhere);
8585
}
8686
}

Cpp2IL.InstructionSets.ArmV8/ArmV8Utils.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,51 @@ namespace Cpp2IL.InstructionSets.ArmV8;
66

77
internal static class ArmV8Utils
88
{
9-
public static Arm64DisassemblyResult GetArm64MethodBodyAtVirtualAddress(ulong virtAddress, bool managed = true, int count = -1)
9+
public static IEnumerable<Arm64Instruction> GetArm64MethodBodyAtVirtualAddress(ulong virtualAddress, out ulong endVirtualAddress, bool managed = true, int count = -1)
1010
{
1111
if (managed)
1212
{
13-
var startOfNext = MiscUtils.GetAddressOfNextFunctionStart(virtAddress);
13+
var startOfNext = MiscUtils.GetAddressOfNextFunctionStart(virtualAddress);
1414

1515
//We have to fall through to default behavior for the last method because we cannot accurately pinpoint its end
1616
if (startOfNext > 0)
1717
{
1818
var rawStartOfNextMethod = LibCpp2IlMain.Binary!.MapVirtualAddressToRaw(startOfNext);
1919

20-
var rawStart = LibCpp2IlMain.Binary.MapVirtualAddressToRaw(virtAddress);
20+
var rawStart = LibCpp2IlMain.Binary.MapVirtualAddressToRaw(virtualAddress);
2121
if (rawStartOfNextMethod < rawStart)
2222
rawStartOfNextMethod = LibCpp2IlMain.Binary.RawLength;
2323

24-
var bytes = LibCpp2IlMain.Binary.GetRawBinaryContent().AsSpan((int)rawStart, (int)(rawStartOfNextMethod - rawStart));
24+
var bytes = LibCpp2IlMain.Binary.GetRawBinaryContent().AsMemory((int)rawStart, (int)(rawStartOfNextMethod - rawStart));
2525

26-
return Disassemble(bytes, virtAddress);
26+
return Disassemble(bytes, virtualAddress, out endVirtualAddress);
2727
}
2828
}
2929

3030
//Unmanaged function, look for first b
31-
var pos = (int)LibCpp2IlMain.Binary!.MapVirtualAddressToRaw(virtAddress);
31+
var pos = (int)LibCpp2IlMain.Binary!.MapVirtualAddressToRaw(virtualAddress);
3232
var allBytes = LibCpp2IlMain.Binary.GetRawBinaryContent();
33-
var span = allBytes.AsSpan(pos, 4);
34-
Arm64DisassemblyResult ret = new();
33+
34+
var instructions = new List<Arm64Instruction>();
3535

36-
while ((count == -1 || ret.Instructions.Count < count) && !ret.Instructions.Any(i => i.Mnemonic is Arm64Mnemonic.B))
36+
endVirtualAddress = virtualAddress;
37+
foreach (var instruction in Disassembler.Disassemble(allBytes.AsSpan(pos), virtualAddress, Disassembler.Options.IgnoreErrors))
3738
{
38-
ret = Disassemble(span, virtAddress);
39-
40-
//All arm64 instructions are 4 bytes
41-
span = allBytes.AsSpan(pos, span.Length + 4);
39+
instructions.Add(instruction);
40+
endVirtualAddress = instruction.Address;
41+
if (instruction.Mnemonic == Arm64Mnemonic.B) break;
42+
if (count != -1 && instructions.Count >= count) break;
4243
}
4344

44-
return ret;
45+
return instructions;
4546
}
4647

47-
private static Arm64DisassemblyResult Disassemble(Span<byte> bytes, ulong virtAddress)
48+
private static IEnumerable<Arm64Instruction> Disassemble(ReadOnlyMemory<byte> bytes, ulong virtualAddress, out ulong endVirtualAddress)
4849
{
4950
try
5051
{
51-
return Disassembler.Disassemble(bytes, virtAddress);
52+
endVirtualAddress = virtualAddress + (ulong)bytes.Length;
53+
return Disassembler.Disassemble(bytes, virtualAddress, Disassembler.Options.IgnoreErrors);
5254
}
5355
catch (Exception e)
5456
{

Cpp2IL.InstructionSets.ArmV8/Cpp2IL.InstructionSets.ArmV8.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
<ItemGroup>
88
<ProjectReference Include="..\Cpp2IL.Core\Cpp2IL.Core.csproj" />
9-
<PackageReference Include="Disarm" Version="2022.1.0-master.26" />
9+
<PackageReference Include="Disarm" Version="2022.1.0-master.34" />
1010
</ItemGroup>
1111
</Project>

0 commit comments

Comments
 (0)