* Bump projects to .net 9 and update nugets * add VersionedSerialization + source generator * migrate versioning to StructVersion class, add handling/detection for 29.2/31.2 * add new struct definitions * rename serialization methods and add BinaryObjectStreamReader for interop * Rework metadata struct loading to use new struct versioning * move 29/31.1/.2 to use tags (-2022,-2023) instead of minor versions * fix metadata usage validity checks * rework code registration offsetting a bit and add second 29/31.1 condition * tweak .1 condition (again) * 29/31.2 was a psyop * also remove 29.2 from the readme * remove loading of packed dlls - this was a very unsafe feature * support auto-recovering type indices from type handles fixes loading of memory-dumped v29+ libraries since those replacee their class indices on load with a pointer to the corresponding type * support loading PEs without an export table * also read UnresolvedVirtualCallCount on regular v31 * Disable plugin loading for now * Overhaul disassembler script + add Binary Ninja target (#12) * Overhaul diassembler scripts: - No longer defines top level functions - Split into three classes: StatusHandler (like before), DisassemblerInterface (for interfacing with the used program API), ScriptContext (for definiting general functions that use the disassembler interface) - Add type annotations to all class methods and remove 2.7 compatibility stuff (Ghidra now supports Python 3 so this is unnecessary anymore) - Disassembler backends are now responsible for launching metadata/script processing, to better support disassembler differences - String handling is back in the base ScriptContext class, disassembler interfaces opt into the fake string segment creation and fall back to the old method if it isn't supported * Add Binary Ninja disassembler script backend This uses the new backend-controlled execution to launch metadata processing on a background thread to keep the ui responsive * make binary ninja script use own _BINARYNINJA_ define and add define helpers to header * Update README to account for new script and binary ninja backend * implement fake string segment functions for binary ninja but don't advertise support * also cache API function types in binary ninja backend * fix ida script and disable folders again * Fix metadata usage issues caused by it being a value type now * make TryMapVATR overrideable and implement it for ELFs * Make field offset reading use TryMapVATR to reduce exceptions * Fix NRE in Assembly ctor on < v24.2 * Update actions workflow to produce cross-platform CLI binaries, update readme to reflect .net 9 changes * workflow: only restore packages for projects that are being built * workflow: tweak caching and fix gui compilation * workflow: remove double .zip in CLI artifact name * 29/31.2 don't actually exist, this logic is not needed
308 lines
14 KiB
C#
308 lines
14 KiB
C#
// Copyright (c) 2020-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
|
|
// All rights reserved
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using Il2CppInspector.Reflection;
|
|
using Il2CppInspector.Model;
|
|
using Il2CppInspector.Next;
|
|
|
|
namespace Il2CppInspector.Outputs
|
|
{
|
|
// Output module to produce machine readable metadata in JSON format
|
|
public class JSONMetadata
|
|
{
|
|
private readonly AppModel model;
|
|
private Utf8JsonWriter writer;
|
|
|
|
// Allow non-compliant C-style comments in JSON output
|
|
public bool AllowComments { get; set; } = false;
|
|
|
|
public JSONMetadata(AppModel model) => this.model = model;
|
|
|
|
// Write JSON metadata to file
|
|
public void Write(string outputFile) {
|
|
|
|
using var fs = new FileStream(outputFile, FileMode.Create);
|
|
writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
|
|
writer.WriteStartObject();
|
|
|
|
// Output address map of everything in the binary that we recognize
|
|
writeObject(
|
|
"addressMap",
|
|
() => {
|
|
writeMethods();
|
|
writeStringLiterals();
|
|
writeUsages();
|
|
writeFunctions();
|
|
writeMetadata();
|
|
writeApis();
|
|
writeExports();
|
|
writeSymbols();
|
|
writeFields();
|
|
},
|
|
"Address map of methods, internal functions, type pointers and string literals in the binary file"
|
|
);
|
|
|
|
writer.WriteEndObject();
|
|
writer.Dispose();
|
|
}
|
|
|
|
private void writeMethods() {
|
|
writeArray("methodDefinitions", () => writeMethods(model.GetMethodGroup("types_from_methods")), "Method definitions");
|
|
writeArray("constructedGenericMethods", () => writeMethods(model.GetMethodGroup("types_from_generic_methods")), "Constructed generic methods");
|
|
|
|
// We could use ILModel.CustomAttributeGenerators here to ensure uniqueness but then we lose function name information
|
|
// TODO: Merge CAG names that deal with multiple attribute types to reflect all of the attribute type names (solving the above)
|
|
writeArray("customAttributesGenerators", () => {
|
|
foreach (var method in model.TypeModel.AttributesByIndices.Values) {
|
|
writeObject(() => writeTypedFunctionName(method.VirtualAddress.Start, method.Signature, method.Name));
|
|
}
|
|
}, "Custom attributes generators");
|
|
|
|
writeArray("methodInvokers", () => {
|
|
foreach (var method in model.TypeModel.MethodInvokers.Where(m => m != null)) {
|
|
writeObject(() => writeTypedFunctionName(method.VirtualAddress.Start, method.GetSignature(model.UnityVersion), method.Name));
|
|
}
|
|
}, "Method.Invoke thunks");
|
|
}
|
|
|
|
private void writeMethods(IEnumerable<AppMethod> methods) {
|
|
foreach (var method in methods.Where(m => m.HasCompiledCode)) {
|
|
writeObject(() => {
|
|
writeTypedFunctionName(method.MethodCodeAddress, method.CppFnPtrType.ToSignatureString(), method.ToMangledString());
|
|
writeDotNetSignature(method.Method);
|
|
|
|
var groupString = $"{method.Method.DeclaringType.Assembly.ShortName}/{method.Method.DeclaringType.FullName.Replace(".", "/")}";
|
|
writer.WriteString("group", groupString);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void writeStringLiterals() {
|
|
writeArray("stringLiterals", () => {
|
|
foreach (var str in model.Strings)
|
|
writeObject(() => {
|
|
// For version < 19
|
|
if (model.StringIndexesAreOrdinals) {
|
|
writer.WriteNumber("ordinal", str.Key);
|
|
writer.WriteString("name", $"STRINGLITERAL_{str.Key}_{stringToIdentifier(str.Value)}");
|
|
// For version >= 19
|
|
} else {
|
|
writer.WriteString("virtualAddress", str.Key.ToAddressString());
|
|
writer.WriteString("name", "StringLiteral_" + stringToIdentifier(str.Value));
|
|
}
|
|
writer.WriteString("string", str.Value);
|
|
});
|
|
}, "String literals");
|
|
}
|
|
|
|
private void writeUsages() {
|
|
// TypeInfo addresses for all types from metadata usages
|
|
writeArray("typeInfoPointers",
|
|
() => {
|
|
foreach (var type in model.Types.Values) {
|
|
// A type may have no addresses, for example an unreferenced array type
|
|
|
|
if (type.TypeClassAddress != 0xffffffff_ffffffff) {
|
|
writeObject(() => {
|
|
writeTypedName(type.TypeClassAddress, $"struct {type.Name}__Class *", type.ToMangledTypeInfoString());
|
|
writeDotNetTypeName(type.Type);
|
|
});
|
|
}
|
|
}
|
|
}, "Il2CppClass (TypeInfo) pointers");
|
|
|
|
// Reference addresses for all types from metadata usages
|
|
writeArray("typeRefPointers",
|
|
() => {
|
|
foreach (var type in model.Types.Values) {
|
|
if (type.TypeRefPtrAddress != 0xffffffff_ffffffff) {
|
|
writeObject(() => {
|
|
// A generic type definition does not have any direct C++ types, but may have a reference
|
|
writeName(type.TypeRefPtrAddress, type.ToMangledTypeRefString());
|
|
writeDotNetTypeName(type.Type);
|
|
});
|
|
}
|
|
}
|
|
}, "Il2CppType (TypeRef) pointers");
|
|
|
|
// Metedata usage methods
|
|
writeArray("methodInfoPointers",
|
|
() => {
|
|
foreach (var method in model.Methods.Values.Where(m => m.HasMethodInfo))
|
|
{
|
|
writeObject(() =>
|
|
{
|
|
writeName(method.MethodInfoPtrAddress, method.ToMangledMethodInfoString());
|
|
writeDotNetSignature(method.Method);
|
|
if (method.HasCompiledCode)
|
|
writer.WriteString("methodAddress", method.MethodCodeAddress.ToAddressString());
|
|
});
|
|
}
|
|
}, "MethodInfo pointers");
|
|
}
|
|
|
|
private void writeFunctions() {
|
|
writeArray("functionAddresses", () => {
|
|
foreach (var func in model.Package.FunctionAddresses)
|
|
writer.WriteStringValue(func.Key.ToAddressString());
|
|
}, "Function boundaries");
|
|
}
|
|
|
|
private void writeMetadata() {
|
|
var binary = model.Package.Binary;
|
|
|
|
writeArray("typeMetadata", () => {
|
|
writeObject(() => writeTypedName(binary.CodeRegistrationPointer, "struct Il2CppCodeRegistration", "g_CodeRegistration"));
|
|
writeObject(() => writeTypedName(binary.MetadataRegistrationPointer, "struct Il2CppMetadataRegistration", "g_MetadataRegistration"));
|
|
|
|
foreach (var ptr in binary.CodeGenModulePointers)
|
|
writeObject(() => writeTypedName(ptr.Value, "struct Il2CppCodeGenModule", $"g_{ptr.Key.Replace(".dll", "")}CodeGenModule"));
|
|
}, "IL2CPP Type Metadata");
|
|
|
|
// plagiarism. noun - https://www.lexico.com/en/definition/plagiarism
|
|
// the practice of taking someone else's work or ideas and passing them off as one's own.
|
|
// Synonyms: copying, piracy, theft, strealing, infringement of copyright
|
|
|
|
writeArray("functionMetadata", () => {
|
|
// This will be zero if we found the structs from the symbol table
|
|
if (binary.RegistrationFunctionPointer != 0)
|
|
if (model.UnityVersion.CompareTo("5.3.5") >= 0)
|
|
writeObject(() => writeTypedFunctionName(binary.RegistrationFunctionPointer,
|
|
"void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)",
|
|
"il2cpp_codegen_register"));
|
|
else
|
|
writeObject(() => writeTypedFunctionName(binary.RegistrationFunctionPointer,
|
|
"void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration)",
|
|
"il2cpp_codegen_register"));
|
|
}, "IL2CPP Function Metadata");
|
|
|
|
// TODO: In the future, add data ranges for the entire IL2CPP metadata tree
|
|
writeArray("arrayMetadata", () => {
|
|
if (model.Package.Version >= MetadataVersions.V242) {
|
|
writeObject(() => writeTypedArray(binary.CodeRegistration.CodeGenModules, binary.Modules.Count, "struct Il2CppCodeGenModule *", "g_CodeGenModules"));
|
|
}
|
|
}, "IL2CPP Array Metadata");
|
|
}
|
|
|
|
private void writeApis() {
|
|
var apis = model.AvailableAPIs;
|
|
writeArray("apis", () => {
|
|
foreach (var api in apis) {
|
|
var address = apis.primaryToSubkeyMapping[api.Key];
|
|
|
|
writeObject(() => writeTypedFunctionName(address, api.Value.ToSignatureString(), api.Key));
|
|
}
|
|
}, "IL2CPP API functions");
|
|
}
|
|
|
|
private void writeExports() {
|
|
var exports = model.Exports;
|
|
|
|
writeArray("exports", () => {
|
|
foreach (var export in exports) {
|
|
writeObject(() => writeName(export.VirtualAddress, export.Name));
|
|
}
|
|
}, "Exports");
|
|
}
|
|
|
|
private void writeSymbols() {
|
|
var symbols = model.Symbols.Values;
|
|
|
|
writeArray("symbols", () => {
|
|
foreach (var symbol in symbols) {
|
|
writeObject(() => {
|
|
writeName(symbol.VirtualAddress, symbol.Name);
|
|
writer.WriteString("demangledName", symbol.DemangledName);
|
|
writer.WriteString("type", symbol.Type.ToString());
|
|
});
|
|
}
|
|
}, "Symbol table");
|
|
}
|
|
|
|
private void writeFields()
|
|
{
|
|
writeArray("fields", () =>
|
|
{
|
|
foreach (var (addr, field) in model.Fields)
|
|
writeFieldObject(addr, (field.Field + "_Field").ToCIdentifier(), field.Value);
|
|
});
|
|
|
|
writeArray("fieldRvas", () =>
|
|
{
|
|
foreach (var (addr, rva) in model.FieldRvas)
|
|
writeFieldObject(addr, (rva.Field + "_FieldRva").ToCIdentifier(), rva.Value);
|
|
});
|
|
}
|
|
|
|
private void writeFieldObject(ulong addr, string name, string value)
|
|
{
|
|
writeObject(() =>
|
|
{
|
|
writer.WriteString("virtualAddress", addr.ToAddressString());
|
|
writer.WriteString("name", name);
|
|
writer.WriteString("value", value);
|
|
});
|
|
}
|
|
|
|
// JSON helpers
|
|
private void writeObject(Action objectWriter) => writeObject(null, objectWriter);
|
|
|
|
private void writeObject(string name, Action objectWriter, string description = null) {
|
|
if (AllowComments && description != null)
|
|
writer.WriteCommentValue(" " + description + " ");
|
|
if (name != null)
|
|
writer.WriteStartObject(name);
|
|
else
|
|
writer.WriteStartObject();
|
|
objectWriter();
|
|
writer.WriteEndObject();
|
|
}
|
|
|
|
private void writeArray(string name, Action arrayWriter, string description = null) {
|
|
writer.WriteStartArray(name);
|
|
if (AllowComments && description != null)
|
|
writer.WriteCommentValue(" " + description + " ");
|
|
arrayWriter();
|
|
writer.WriteEndArray();
|
|
}
|
|
|
|
private void writeName(ulong address, string name) {
|
|
writer.WriteString("virtualAddress", address.ToAddressString());
|
|
writer.WriteString("name", name.ToEscapedString());
|
|
}
|
|
|
|
private void writeTypedName(ulong address, string type, string name) {
|
|
writeName(address, name);
|
|
writer.WriteString("type", type.ToEscapedString());
|
|
}
|
|
|
|
private void writeTypedFunctionName(ulong address, string type, string name) {
|
|
writeName(address, name);
|
|
writer.WriteString("signature", type.ToEscapedString());
|
|
}
|
|
|
|
private void writeTypedArray(ulong address, int count, string type, string name) {
|
|
writeTypedName(address, type, name);
|
|
writer.WriteNumber("count", count);
|
|
}
|
|
|
|
private void writeDotNetSignature(MethodBase method) {
|
|
writer.WriteString("dotNetSignature", method.ToString().ToEscapedString());
|
|
}
|
|
|
|
private void writeDotNetTypeName(TypeInfo type) {
|
|
writer.WriteString("dotNetType", type.CSharpName);
|
|
}
|
|
|
|
private static string stringToIdentifier(string str) {
|
|
str = str.Substring(0, Math.Min(32, str.Length));
|
|
return str.ToCIdentifier();
|
|
}
|
|
}
|
|
}
|