From 641f502ab842cf79be6ce775f2291d82d372a0be Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Sun, 16 Aug 2020 00:06:36 +0200 Subject: [PATCH] IL2CPP: Reconstruct CustomAttributesGenerators for metadata v27 --- Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs | 3 ++- .../IL2CPP/Il2CppInspector.cs | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs index d9e2298..2cf485d 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppBinary.cs @@ -48,6 +48,7 @@ namespace Il2CppInspector public long[] FieldOffsetPointers { get; private set; } // Generated functions which call constructors on custom attributes + // Only for <=24.3 public ulong[] CustomAttributeGenerators { get; private set; } // IL2CPP-generated functions which implement MethodBase.Invoke with a unique signature per invoker, defined in Il2CppInvokerTable.cpp @@ -267,7 +268,7 @@ namespace Il2CppInspector TypeReferences = Image.ReadMappedObjectPointerArray(MetadataRegistration.ptypes, (int) MetadataRegistration.typesCount); // Custom attribute constructors (function pointers) - // TODO: Custom attribute generator load is broken in metadata v27 - this was moved to Il2CppCodeGenModule + // This is managed in Il2CppInspector for metadata >= 27 if (Image.Version < 27) { CustomAttributeGenerators = Image.ReadMappedArray(CodeRegistration.customAttributeGenerators, (int) CodeRegistration.customAttributeCount); } diff --git a/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs b/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs index b1598c9..88398ee 100644 --- a/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs +++ b/Il2CppInspector.Common/IL2CPP/Il2CppInspector.cs @@ -59,7 +59,7 @@ namespace Il2CppInspector public Dictionary TypeReferenceIndicesByAddress => Binary.TypeReferenceIndicesByAddress; public List GenericInstances => Binary.GenericInstances; public Dictionary Modules => Binary.Modules; - public ulong[] CustomAttributeGenerators => Binary.CustomAttributeGenerators; + public ulong[] CustomAttributeGenerators { get; } public ulong[] MethodInvokePointers => Binary.MethodInvokePointers; public Il2CppMethodSpec[] MethodSpecs => Binary.MethodSpecs; public Dictionary GenericMethodPointers => Binary.GenericMethodPointers; @@ -193,10 +193,26 @@ namespace Il2CppInspector FieldOffsets = offsets.OrderBy(x => x.Key).Select(x => x.Value).ToList(); } + // Build list of custom attribute generators + if (Version < 27) + CustomAttributeGenerators = Binary.CustomAttributeGenerators; + + else { + var cagCount = Images.Sum(i => i.customAttributeCount); + CustomAttributeGenerators = new ulong[cagCount]; + + foreach (var image in Images) { + // Get CodeGenModule for this image + var codeGenModule = Binary.Modules[Strings[image.nameIndex]]; + var cags = BinaryImage.ReadMappedWordArray(codeGenModule.customAttributeCacheGenerator, (int) image.customAttributeCount); + cags.CopyTo(CustomAttributeGenerators, image.customAttributeStart); + } + } + // Get sorted list of function pointers from all sources var sortedFunctionPointers = (Version <= 24.1)? - Binary.GlobalMethodPointers.ToList() : - Binary.ModuleMethodPointers.SelectMany(module => module.Value).ToList(); + Binary.GlobalMethodPointers.ToList() : + Binary.ModuleMethodPointers.SelectMany(module => module.Value).ToList(); sortedFunctionPointers.AddRange(CustomAttributeGenerators); sortedFunctionPointers.AddRange(MethodInvokePointers);