Add processing of v24+ modules and double-indirected method pointers

This commit is contained in:
Katy Coe
2019-10-16 16:37:12 +02:00
parent a638e86c38
commit 70def274a7
3 changed files with 52 additions and 8 deletions

View File

@@ -26,7 +26,12 @@ namespace Il2CppInspector
// type index => RVA in image where the list of field offsets for the type start (4 bytes per field) // type index => RVA in image where the list of field offsets for the type start (4 bytes per field)
public int[] FieldOffsetData { get; private set; } public int[] FieldOffsetData { get; private set; }
public List<Il2CppType> Types { get; } = new List<Il2CppType>(); // Every defined type
public List<Il2CppType> Types { get; private set; }
// From later versions of v24 onwards, this structure is stored for each module (image)
// One assembly may contain multiple modules
public Dictionary<string, Il2CppCodeGenModule> Modules { get; private set; }
protected Il2CppBinary(IFileFormatReader stream) { protected Il2CppBinary(IFileFormatReader stream) {
Image = stream; Image = stream;
@@ -62,13 +67,32 @@ namespace Il2CppInspector
} }
private void Configure(IFileFormatReader image, uint codeRegistration, uint metadataRegistration) { private void Configure(IFileFormatReader image, uint codeRegistration, uint metadataRegistration) {
// Root structures from which we find everything else
CodeRegistration = image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration); CodeRegistration = image.ReadMappedObject<Il2CppCodeRegistration>(codeRegistration);
MetadataRegistration = image.ReadMappedObject<Il2CppMetadataRegistration>(metadataRegistration); MetadataRegistration = image.ReadMappedObject<Il2CppMetadataRegistration>(metadataRegistration);
MethodPointers = image.ReadMappedArray<uint>(CodeRegistration.pmethodPointers, (int) CodeRegistration.methodPointersCount);
// The global method pointer list was deprecated in later versions of v24 in favour of Il2CppCodeGenModule
if (Image.Stream.Version <= 24.0)
MethodPointers = image.ReadMappedArray<uint>(CodeRegistration.pmethodPointers, (int) CodeRegistration.methodPointersCount);
// After v24 method pointers and RGCTX data were stored in Il2CppCodeGenModules
if (Image.Stream.Version >= 24.1) {
Modules = new Dictionary<string, Il2CppCodeGenModule>();
// Array of pointers to Il2CppCodeGenModule
var modules = image.ReadMappedObjectPointerArray<Il2CppCodeGenModule>(CodeRegistration.pcodeGenModules, (int) CodeRegistration.codeGenModulesCount);
foreach (var module in modules) {
var name = image.ReadMappedNullTerminatedString(module.moduleName);
Modules.Add(name, module);
}
}
// Field offset data. Metadata <=21.x uses a value-type array; >=21.x uses a pointer array
FieldOffsetData = image.ReadMappedArray<int>(MetadataRegistration.pfieldOffsets, MetadataRegistration.fieldOffsetsCount); FieldOffsetData = image.ReadMappedArray<int>(MetadataRegistration.pfieldOffsets, MetadataRegistration.fieldOffsetsCount);
var types = image.ReadMappedArray<uint>(MetadataRegistration.ptypes, MetadataRegistration.typesCount);
for (int i = 0; i < MetadataRegistration.typesCount; i++) // Type definitions (pointer array)
Types.Add(image.ReadMappedObject<Il2CppType>(types[i])); Types = image.ReadMappedObjectPointerArray<Il2CppType>(MetadataRegistration.ptypes, MetadataRegistration.typesCount);
} }
} }
} }

View File

@@ -14,6 +14,7 @@ namespace Il2CppInspector.Reflection {
// IL2CPP-specific data // IL2CPP-specific data
public Il2CppReflector Model { get; } public Il2CppReflector Model { get; }
public Il2CppImageDefinition Definition { get; } public Il2CppImageDefinition Definition { get; }
public Il2CppCodeGenModule Module { get; }
public int Index { get; } public int Index { get; }
// TODO: CustomAttributes // TODO: CustomAttributes
@@ -41,6 +42,10 @@ namespace Il2CppInspector.Reflection {
// TODO: Generate EntryPoint method from entryPointIndex // TODO: Generate EntryPoint method from entryPointIndex
} }
// Find corresponding module (we'll need this for method pointers)
if (Model.Package.Metadata.Version >= 24.1)
Module = Model.Package.Binary.Modules[FullName];
// Generate types in DefinedTypes from typeStart to typeStart+typeCount-1 // Generate types in DefinedTypes from typeStart to typeStart+typeCount-1
for (var t = Definition.typeStart; t < Definition.typeStart + Definition.typeCount; t++) for (var t = Definition.typeStart; t < Definition.typeStart + Definition.typeCount; t++)
DefinedTypes.Add(new TypeInfo(Model.Package, t, this)); DefinedTypes.Add(new TypeInfo(Model.Package, t, this));

View File

@@ -33,11 +33,26 @@ namespace Il2CppInspector.Reflection
base(declaringType) { base(declaringType) {
Definition = pkg.Metadata.Methods[methodIndex]; Definition = pkg.Metadata.Methods[methodIndex];
Index = methodIndex; Index = methodIndex;
Name = pkg.Strings[Definition.nameIndex];
// Find method pointer
if (Definition.methodIndex >= 0) { if (Definition.methodIndex >= 0) {
VirtualAddress = pkg.Binary.MethodPointers[Definition.methodIndex];
// Global method pointer array
if (pkg.Metadata.Version < 24.1) {
VirtualAddress = pkg.Binary.MethodPointers[Definition.methodIndex];
}
// Per-module method pointer array uses the bottom 24 bits of the method's metadata token
// Derived from il2cpp::vm::MetadataCache::GetMethodPointer
else {
var method = (Definition.token & 0xffffff) - 1;
pkg.Binary.Image.Position = pkg.Binary.Image.MapVATR(Assembly.Module.methodPointers + method * 4);
VirtualAddress = pkg.Binary.Image.ReadUInt32();
}
HasBody = true; HasBody = true;
} }
Name = pkg.Strings[Definition.nameIndex];
if ((Definition.flags & Il2CppConstants.METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == Il2CppConstants.METHOD_ATTRIBUTE_PRIVATE) if ((Definition.flags & Il2CppConstants.METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == Il2CppConstants.METHOD_ATTRIBUTE_PRIVATE)
Attributes |= MethodAttributes.Private; Attributes |= MethodAttributes.Private;