Files
Luke b05c03964a Struct reading and disassembly script overhaul, various misc. loading fixes, bump to .NET 9 (#13)
* 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
2024-11-14 14:32:11 +01:00

168 lines
8.5 KiB
C#

/*
Copyright 2020-2021 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Il2CppInspector.Cpp;
using Il2CppInspector.Next;
using Il2CppInspector.Reflection;
namespace Il2CppInspector.Model
{
// A map of absolutely everything in the binary we know about
// Designed to be used by static analysis disassembly frameworks such as Capstone.NET
public class AddressMap : IDictionary<ulong, object>
{
// IL2CPP application model
public AppModel Model { get; }
// Underlying collection
// Objects this can return (subject to change):
// - AppMethod (for method function body)
// - AppMethodReference (for MethodInfo *)
// - List<CustomAttributeData> (for custom attributes generator)
// - MethodInvoker (for a Method.Invoke think)
// - string (System.String) (for a string literal)
// - Il2CppCodeRegistration
// - Il2CppMetadataRegistration
// - Dictionary<string, ulong> (for Il2CppCodeGenModules *[])
// - Il2CppCodeGenModule
// - CppFnPtrType (for il2cpp_codegen_register, Il2CPP API exports, unknown functions)
// - Export (for exports)
// - Symbol (for symbols)
public SortedDictionary<ulong, object> Items { get; } = new SortedDictionary<ulong, object>();
#region Surrogate implementation of IDictionary
// Surrogate implementation of IDictionary
public ICollection<ulong> Keys => ((IDictionary<ulong, object>) Items).Keys;
public ICollection<object> Values => ((IDictionary<ulong, object>) Items).Values;
public int Count => ((ICollection<KeyValuePair<ulong, object>>) Items).Count;
public bool IsReadOnly => ((ICollection<KeyValuePair<ulong, object>>) Items).IsReadOnly;
public object this[ulong key] { get => ((IDictionary<ulong, object>) Items)[key]; set => ((IDictionary<ulong, object>) Items)[key] = value; }
public bool TryAdd(ulong addr, object item) => Items.TryAdd(addr, item);
public void Add(ulong key, object value) => ((IDictionary<ulong, object>) Items).Add(key, value);
public bool ContainsKey(ulong key) => ((IDictionary<ulong, object>) Items).ContainsKey(key);
public bool Remove(ulong key) => ((IDictionary<ulong, object>) Items).Remove(key);
public bool TryGetValue(ulong key, out object value) => ((IDictionary<ulong, object>) Items).TryGetValue(key, out value);
public void Add(KeyValuePair<ulong, object> item) => ((ICollection<KeyValuePair<ulong, object>>) Items).Add(item);
public void Clear() => ((ICollection<KeyValuePair<ulong, object>>) Items).Clear();
public bool Contains(KeyValuePair<ulong, object> item) => ((ICollection<KeyValuePair<ulong, object>>) Items).Contains(item);
public void CopyTo(KeyValuePair<ulong, object>[] array, int arrayIndex) => ((ICollection<KeyValuePair<ulong, object>>) Items).CopyTo(array, arrayIndex);
public bool Remove(KeyValuePair<ulong, object> item) => ((ICollection<KeyValuePair<ulong, object>>) Items).Remove(item);
public IEnumerator<KeyValuePair<ulong, object>> GetEnumerator() => ((IEnumerable<KeyValuePair<ulong, object>>) Items).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable) Items).GetEnumerator();
#endregion
public AddressMap(AppModel model) {
Model = model;
build();
}
private void build() {
// Get handle to C++ types
var cppTypes = Model.CppTypeCollection;
// Start by adding all of the .NET methods from all groups
var methodInfoPtrType = cppTypes.GetType("MethodInfo *");
foreach (var method in Model.Methods.Values) {
// Method function body
// Shared generic methods may share the same address!
if (method.HasCompiledCode)
TryAdd(method.MethodCodeAddress, method);
// Method reference (MethodInfo *)
if (method.HasMethodInfo)
Add(method.MethodInfoPtrAddress,
new AppMethodReference {
Field = new CppField($"{method.CppFnPtrType.Name}__MethodInfo", methodInfoPtrType),
Method = method
});
}
// Add all custom attributes generators
// The compiler might perform ICF which will cause duplicates with the above
foreach (var cag in Model.TypeModel.CustomAttributeGeneratorsByAddress)
TryAdd(cag.Key, cag.Value);
// Add all method invokers. Multiple invoker indices may reference the same function address
foreach (var mi in Model.TypeModel.MethodInvokers.Where(m => m != null))
TryAdd(mi.VirtualAddress.Start, mi);
// String literals (metadata >= 19)
if (!Model.StringIndexesAreOrdinals)
foreach (var str in Model.Strings)
Add(str.Key, str.Value);
// Type definitions and references
var classPtrType = cppTypes.GetType("Il2CppClass *");
var classRefPtrType = cppTypes.GetType("Il2CppType *");
foreach (var type in Model.Types.Values) {
if (type.TypeClassAddress != 0xffffffff_ffffffff)
Add(type.TypeClassAddress, new AppTypeReference {
Field = new CppField($"{type.Name}__TypeInfo", classPtrType),
Type = type
});
if (type.TypeRefPtrAddress != 0xffffffff_ffffffff)
Add(type.TypeRefPtrAddress, new AppTypeReference {
Field = new CppField($"{type.Name}__TypeRef", classRefPtrType),
Type = type
});
}
// Internal metadata
var binary = Model.Package.Binary;
Add(binary.CodeRegistrationPointer, binary.CodeRegistration);
Add(binary.MetadataRegistrationPointer, binary.MetadataRegistration);
if (Model.Package.Version >= MetadataVersions.V242) {
// TODO: Add some kind of AppArray composite type for arrays as we'll be adding more later
Add(binary.CodeRegistration.CodeGenModules, binary.CodeGenModulePointers);
foreach (var ptr in binary.CodeGenModulePointers)
Add(ptr.Value, binary.Modules[ptr.Key]);
}
if (binary.RegistrationFunctionPointer != 0)
if (Model.UnityVersion.CompareTo("5.3.5") >= 0)
Add(binary.RegistrationFunctionPointer, CppFnPtrType.FromSignature(cppTypes,
"void (*il2cpp_codegen_register)(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)"));
else
Add(binary.RegistrationFunctionPointer, CppFnPtrType.FromSignature(cppTypes,
"void (*il2cpp_codegen_register)(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration)"));
// IL2CPP API exports
// Alternative names like il2cpp_class_from_type and il2cpp_class_from_il2cpp_type may point to the same address
foreach (var api in Model.AvailableAPIs) {
var address = Model.AvailableAPIs.primaryToSubkeyMapping[api.Key];
TryAdd(address, api.Value);
}
// Unknown functions
// We'll skip over all the functions already added via the method iterators above,
// leaving just the ones we haven't done any processing on
foreach (var func in Model.Package.FunctionAddresses.Keys)
TryAdd(func, CppFnPtrType.FromSignature(cppTypes, $"void (*unk_{func.ToAddressString()})()"));
// Remaining exports
var voidPtrType = cppTypes.GetType("void *");
foreach (var export in Model.Exports)
TryAdd(export.VirtualAddress, export);
// Symbols
// The symbols may also include the exports
foreach (var symbol in Model.Symbols.Values)
TryAdd(symbol.VirtualAddress, symbol);
}
}
}