Improve MetadataUsages and add types for globals

- Add string contents to names of StringLiteral objects to improve
decompilation output
- Add string constants for metadata version < 19 via an enum (if you can
identify and type il2cpp_codegen_string_literal_from_index correctly,
all enums resolve nicely to their string contents)
- Add types to metadata usages and globals
- Send method definitions & method references through writeMethods
for consistency
This commit is contained in:
Robert Xiao
2020-04-20 00:42:14 -07:00
committed by Katy
parent 4ba48b9c75
commit caa7eaf870

View File

@@ -9,6 +9,7 @@ using System.IO;
using System.Text; using System.Text;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
using Il2CppInspector.Outputs.UnityHeaders; using Il2CppInspector.Outputs.UnityHeaders;
using System.Text.RegularExpressions;
namespace Il2CppInspector.Outputs namespace Il2CppInspector.Outputs
{ {
@@ -61,11 +62,6 @@ namespace Il2CppInspector.Outputs
writeLine( writeLine(
@"import idaapi @"import idaapi
def SetString(addr, comm):
name = 'StringLiteral_' + str(addr)
ret = idc.set_name(addr, name, SN_NOWARN)
idc.set_cmt(addr, comm, 1)
def SetName(addr, name): def SetName(addr, name):
ret = idc.set_name(addr, name, SN_NOWARN | SN_NOCHECK) ret = idc.set_name(addr, name, SN_NOWARN | SN_NOCHECK)
if ret == 0: if ret == 0:
@@ -74,6 +70,11 @@ def SetName(addr, name):
def MakeFunction(start): def MakeFunction(start):
ida_funcs.add_func(start) ida_funcs.add_func(start)
def SetType(addr, type):
ret = idc.SetType(addr, type)
if ret == 0:
print('SetType(0x%x, %r) failed!' % (addr, type))
"); ");
// Compatibility (in a separate decl block in case these are already defined) // Compatibility (in a separate decl block in case these are already defined)
@@ -94,17 +95,10 @@ typedef __int64 int64_t;
private void writeMethods() { private void writeMethods() {
writeSectionHeader("Method definitions"); writeSectionHeader("Method definitions");
foreach (var type in model.Types) { writeMethods(model.MethodsByDefinitionIndex);
writeMethods(type.Name, type.DeclaredConstructors);
writeMethods(type.Name, type.DeclaredMethods);
}
writeSectionHeader("Constructed generic methods"); writeSectionHeader("Constructed generic methods");
foreach (var method in model.GenericMethods.Values.Where(m => m.VirtualAddress.HasValue)) { writeMethods(model.GenericMethods.Values);
var address = method.VirtualAddress.Value.Start;
writeName(address, $"{method.DeclaringType.Name}_{method.Name}{method.GetFullTypeParametersString()}");
writeComment(address, method);
}
writeSectionHeader("Custom attributes generators"); writeSectionHeader("Custom attributes generators");
foreach (var method in model.AttributesByIndices.Values.Where(m => m.VirtualAddress.HasValue)) { foreach (var method in model.AttributesByIndices.Values.Where(m => m.VirtualAddress.HasValue)) {
@@ -121,35 +115,68 @@ typedef __int64 int64_t;
} }
} }
private void writeMethods(string typeName, IEnumerable<MethodBase> methods) { private void writeMethods(IEnumerable<MethodBase> methods) {
foreach (var method in methods.Where(m => m.VirtualAddress.HasValue)) { foreach (var method in methods.Where(m => m.VirtualAddress.HasValue)) {
var address = method.VirtualAddress.Value.Start; var address = method.VirtualAddress.Value.Start;
writeName(address, $"{typeName}_{method.Name}"); writeName(address, $"{method.DeclaringType.Name}_{method.Name}{method.GetFullTypeParametersString()}");
writeComment(address, method); writeComment(address, method);
} }
} }
private static string sanitizeIdentifier(string str) {
return Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
}
private static string stringToIdentifier(string str) {
str = str.Substring(0, Math.Min(32, str.Length));
return sanitizeIdentifier(str);
}
private void writeUsages() { private void writeUsages() {
if (model.Package.MetadataUsages == null) { if (model.Package.MetadataUsages == null) {
/* Version < 19 - no MetadataUsages table */ /* Version < 19 calls `il2cpp_codegen_string_literal_from_index` to get string literals.
* Unfortunately, metadata references are just loose globals in Il2CppMetadataUsage.cpp
* so we can't automatically name those. Next best thing is to define an enum for the strings. */
var enumSrc = new StringBuilder();
enumSrc.Append("enum StringLiteralIndex {\n");
for (int i = 0; i < model.Package.StringLiterals.Length; i++) {
var str = model.Package.StringLiterals[i];
enumSrc.Append($" STRINGLITERAL_{i}_{stringToIdentifier(str)},\n");
}
enumSrc.Append("};\n");
writeDecls(enumSrc.ToString());
return; return;
} }
foreach (var usage in model.Package.MetadataUsages) { foreach (var usage in model.Package.MetadataUsages) {
var address = usage.VirtualAddress; var address = usage.VirtualAddress;
var name = model.GetMetadataUsageName(usage); string name;
if (usage.Type != MetadataUsageType.StringLiteral) switch (usage.Type) {
writeName(address, $"{name}_{usage.Type}"); case MetadataUsageType.StringLiteral:
else var str = model.GetMetadataUsageName(usage);
writeString(address, name); writeName(address, $"StringLiteral_{stringToIdentifier(str)}");
writeComment(address, str);
if (usage.Type == MetadataUsageType.MethodDef || usage.Type == MetadataUsageType.MethodRef) { break;
var method = model.GetMetadataUsageMethod(usage); case MetadataUsageType.Type:
writeComment(address, method); case MetadataUsageType.TypeInfo:
} else if (usage.Type != MetadataUsageType.StringLiteral) { var type = model.GetMetadataUsageType(usage);
var type = model.GetMetadataUsageType(usage); name = sanitizeIdentifier(type.Name);
writeComment(address, type); if (usage.Type == MetadataUsageType.TypeInfo)
writeTypedName(address, $"struct Il2CppClass *", $"{name}__TypeInfo");
else
writeTypedName(address, $"struct Il2CppType *", $"{name}__TypeRef");
writeComment(address, type.CSharpName);
break;
case MetadataUsageType.MethodDef:
case MetadataUsageType.MethodRef:
var method = model.GetMetadataUsageMethod(usage);
name = sanitizeIdentifier(method.Name);
writeTypedName(address, "struct MethodInfo *", $"{name}__MethodInfo");
writeComment(address, method);
break;
} }
} }
} }
@@ -164,14 +191,15 @@ typedef __int64 int64_t;
// TODO: In the future, add struct definitions/fields, data ranges and the entire IL2CPP metadata tree // TODO: In the future, add struct definitions/fields, data ranges and the entire IL2CPP metadata tree
writeName(binary.CodeRegistrationPointer, "g_CodeRegistration"); writeTypedName(binary.CodeRegistrationPointer, "struct Il2CppCodeRegistration", "g_CodeRegistration");
writeName(binary.MetadataRegistrationPointer, "g_MetadataRegistration"); writeTypedName(binary.MetadataRegistrationPointer, "struct Il2CppMetadataRegistration", "g_MetadataRegistration");
if (model.Package.Version >= 24.2) if (model.Package.Version >= 24.2)
writeName(binary.CodeRegistration.pcodeGenModules, "g_CodeGenModules"); writeTypedName(binary.CodeRegistration.pcodeGenModules,
$"struct Il2CppCodeGenModule *[{binary.CodeRegistration.codeGenModulesCount}]", "g_CodeGenModules");
foreach (var ptr in binary.CodeGenModulePointers) foreach (var ptr in binary.CodeGenModulePointers)
writeName(ptr.Value, $"g_{ptr.Key.Replace(".dll", "")}CodeGenModule"); writeTypedName(ptr.Value, "struct Il2CppCodeGenModule", $"g_{ptr.Key.Replace(".dll", "")}CodeGenModule");
// This will be zero if we found the structs from the symbol table // This will be zero if we found the structs from the symbol table
if (binary.RegistrationFunctionPointer != 0) if (binary.RegistrationFunctionPointer != 0)
@@ -198,8 +226,9 @@ typedef __int64 int64_t;
writeLine($"SetName({address.ToAddressString()}, r'{name.ToEscapedString()}')"); writeLine($"SetName({address.ToAddressString()}, r'{name.ToEscapedString()}')");
} }
private void writeString(ulong address, string str) { private void writeTypedName(ulong address, string type, string name) {
writeLine($"SetString({address.ToAddressString()}, r'{str.ToEscapedString()}')"); writeName(address, name);
writeLine($"SetType({address.ToAddressString()}, r'{type.ToEscapedString()}')");
} }
private void writeComment(ulong address, object comment) { private void writeComment(ulong address, object comment) {