AppModel: Add initial handling of methods (AppMethod)

This commit is contained in:
Katy Coe
2020-07-09 21:25:19 +02:00
parent a9606f5c9e
commit 6015d9e0ab
4 changed files with 378 additions and 16 deletions

View File

@@ -0,0 +1,35 @@
/*
Copyright 2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved.
*/
using Il2CppInspector.Cpp;
using Il2CppInspector.Reflection;
namespace Il2CppInspector.Model
{
// Class that represents a composite IL/C++ method
public class AppMethod
{
// The corresponding C++ function pointer type
public CppFnPtrType CppFnPtrType { get; internal set; }
// The corresponding .NET method
public MethodBase Method { get; internal set; }
// The VA of the MethodInfo* (VA of the pointer to the MethodInfo) object which defines this method
public ulong MethodInfoPtrAddress { get; internal set; }
// The VA of the method code itself, or 0 if unknown/not compiled
public ulong MethodCodeAddress => Method.VirtualAddress?.Start ?? 0;
public AppMethod(MethodBase method, CppFnPtrType cppMethod, ulong methodInfoPtr = 0xffffffff_ffffffff) {
Method = method;
CppFnPtrType = cppMethod;
MethodInfoPtrAddress = methodInfoPtr;
}
public override string ToString() => CppFnPtrType.ToSignatureString();
}
}

View File

@@ -8,6 +8,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Aron.Weiler;
using Il2CppInspector.Cpp;
using Il2CppInspector.Cpp.UnityHeaders;
using Il2CppInspector.Reflection;
@@ -32,22 +33,6 @@ namespace Il2CppInspector.Model
public ulong VirtualAddress { get; internal set; }
}
// Class that represents a composite IL/C++ method
public class AppMethod
{
// The corresponding C++ function pointer type
public CppFnPtrType CppFnPtrType { get; internal set; }
// The corresponding .NET method
public MethodBase ILMethod { get; internal set; }
// The VA of the MethodInfo* (VA of the pointer to the MethodInfo) object which defines this method
public ulong MethodInfoPtrAddress { get; internal set; }
// The VA of the method code itself, or 0 if unknown/not compiled
public ulong MethodCodeAddress => ILMethod.VirtualAddress?.Start ?? 0;
}
// Class that represents the entire structure of the IL2CPP binary realized as C++ types and code,
// correlated with .NET types where applicable. Primarily designed to enable automated static analysis of disassembly code.
public class AppModel : IEnumerable<CppType>
@@ -71,6 +56,9 @@ namespace Il2CppInspector.Model
// The types are ordered to enable the production of code output without forward dependencies
public List<CppType> DependencyOrderedTypes { get; private set; }
// Composite mapping of all the .NET methods in the IL2CPP binary
public MultiKeyDictionary<MethodBase, CppFnPtrType, AppMethod> Methods = new MultiKeyDictionary<MethodBase, CppFnPtrType, AppMethod>();
// The .NET type model for the application
public TypeModel ILModel { get; }
@@ -143,6 +131,9 @@ namespace Il2CppInspector.Model
foreach (var method in ILModel.MethodsByDefinitionIndex.Where(m => m.VirtualAddress.HasValue)) {
declarationGenerator.IncludeMethod(method);
DependencyOrderedTypes.AddRange(declarationGenerator.GenerateRemainingTypeDeclarations());
var fnPtr = declarationGenerator.GenerateMethodDeclaration(method);
Methods.Add(method, fnPtr, new AppMethod(method, fnPtr));
}
// Add generic methods to C++ type model
@@ -151,6 +142,9 @@ namespace Il2CppInspector.Model
foreach (var method in ILModel.GenericMethods.Values.Where(m => m.VirtualAddress.HasValue)) {
declarationGenerator.IncludeMethod(method);
DependencyOrderedTypes.AddRange(declarationGenerator.GenerateRemainingTypeDeclarations());
var fnPtr = declarationGenerator.GenerateMethodDeclaration(method);
Methods.Add(method, fnPtr, new AppMethod(method, fnPtr));
}
// Add metadata usage types to C++ type model
@@ -159,6 +153,8 @@ namespace Il2CppInspector.Model
if (Package.MetadataUsages != null)
foreach (var usage in Package.MetadataUsages) {
var address = usage.VirtualAddress;
switch (usage.Type) {
case MetadataUsageType.Type:
case MetadataUsageType.TypeInfo:
@@ -171,6 +167,8 @@ namespace Il2CppInspector.Model
var method = ILModel.GetMetadataUsageMethod(usage);
declarationGenerator.IncludeMethod(method);
DependencyOrderedTypes.AddRange(declarationGenerator.GenerateRemainingTypeDeclarations());
Methods[method].MethodInfoPtrAddress = address;
break;
}
}