forked from SamboyCoding/Cpp2IL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Il2CppBinary.cs
442 lines (377 loc) · 21.5 KB
/
Il2CppBinary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Logging;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibCpp2IL
{
public abstract class Il2CppBinary : ClassReadingBinaryReader
{
public InstructionSet InstructionSet;
protected readonly long maxMetadataUsages;
private Il2CppMetadataRegistration metadataRegistration;
private Il2CppCodeRegistration codeRegistration;
protected ulong[] methodPointers;
private ulong[] genericMethodPointers;
private ulong[] invokerPointers;
private ulong[]? customAttributeGenerators; //Pre-27 only
protected long[] fieldOffsets;
protected ulong[] metadataUsages; //Pre-27 only
protected ulong[][] codeGenModuleMethodPointers; //24.2+
protected Il2CppType[] types;
private Il2CppGenericMethodFunctionsDefinitions[] genericMethodTables;
protected Il2CppGenericInst[] genericInsts;
protected Il2CppMethodSpec[] methodSpecs;
protected Il2CppCodeGenModule[] codeGenModules; //24.2+
protected Il2CppTokenRangePair[][] codegenModuleRgctxRanges;
protected Il2CppRGCTXDefinition[][] codegenModuleRgctxs;
protected Dictionary<int, ulong> genericMethodDictionary;
protected readonly Dictionary<ulong, Il2CppType> typesDict = new();
public readonly Dictionary<Il2CppMethodDefinition, List<Il2CppGenericMethodRef>> ConcreteGenericMethods = new();
public readonly Dictionary<ulong, List<Il2CppGenericMethodRef>> ConcreteGenericImplementationsByAddress = new();
public ulong[] TypeDefinitionSizePointers;
protected Il2CppBinary(MemoryStream input, long maxMetadataUsages) : base(input)
{
this.maxMetadataUsages = maxMetadataUsages;
}
public abstract long RawLength { get; }
public int NumTypes => types.Length;
public void Init(ulong pCodeRegistration, ulong pMetadataRegistration)
{
codeRegistration = ReadClassAtVirtualAddress<Il2CppCodeRegistration>(pCodeRegistration);
metadataRegistration = ReadClassAtVirtualAddress<Il2CppMetadataRegistration>(pMetadataRegistration);
LibLogger.Verbose("\tReading generic instances...");
var start = DateTime.Now;
genericInsts = Array.ConvertAll(ReadClassArrayAtVirtualAddress<ulong>(metadataRegistration.genericInsts, metadataRegistration.genericInstsCount), ReadClassAtVirtualAddress<Il2CppGenericInst>);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading generic method pointers...");
start = DateTime.Now;
genericMethodPointers = ReadClassArrayAtVirtualAddress<ulong>(codeRegistration.genericMethodPointers, (long) codeRegistration.genericMethodPointersCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading invoker pointers...");
start = DateTime.Now;
invokerPointers = ReadClassArrayAtVirtualAddress<ulong>(codeRegistration.invokerPointers, (long) codeRegistration.invokerPointersCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
if (LibCpp2IlMain.MetadataVersion < 27)
{
LibLogger.Verbose("\tReading custom attribute generators...");
start = DateTime.Now;
customAttributeGenerators = ReadClassArrayAtVirtualAddress<ulong>(codeRegistration.customAttributeGeneratorListAddress, (long) codeRegistration.customAttributeCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
LibLogger.Verbose("\tReading field offsets...");
start = DateTime.Now;
fieldOffsets = ReadClassArrayAtVirtualAddress<long>(metadataRegistration.fieldOffsetListAddress, metadataRegistration.fieldOffsetsCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading types...");
start = DateTime.Now;
var typesAddress = ReadClassArrayAtVirtualAddress<ulong>(metadataRegistration.typeAddressListAddress, metadataRegistration.numTypes);
types = new Il2CppType[metadataRegistration.numTypes];
for (var i = 0; i < metadataRegistration.numTypes; ++i)
{
types[i] = ReadClassAtVirtualAddress<Il2CppType>(typesAddress[i]);
types[i].Init();
typesDict[typesAddress[i]] = types[i];
}
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading type definition sizes...");
start = DateTime.Now;
TypeDefinitionSizePointers = ReadClassArrayAtVirtualAddress<ulong>(metadataRegistration.typeDefinitionsSizes, metadataRegistration.typeDefinitionsSizesCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
if (metadataRegistration.metadataUsages != 0)
{
//Empty in v27
LibLogger.Verbose("\tReading metadata usages...");
start = DateTime.Now;
metadataUsages = ReadClassArrayAtVirtualAddress<ulong>(metadataRegistration.metadataUsages, (long)Math.Max((decimal) metadataRegistration.metadataUsagesCount, maxMetadataUsages));
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
if (LibCpp2IlMain.MetadataVersion >= 24.2f)
{
LibLogger.VerboseNewline("\tReading code gen modules...");
start = DateTime.Now;
var codeGenModulePtrs = ReadClassArrayAtVirtualAddress<ulong>(codeRegistration.addrCodeGenModulePtrs, (long) codeRegistration.codeGenModulesCount);
codeGenModules = new Il2CppCodeGenModule[codeGenModulePtrs.Length];
codeGenModuleMethodPointers = new ulong[codeGenModulePtrs.Length][];
codegenModuleRgctxRanges = new Il2CppTokenRangePair[codeGenModulePtrs.Length][];
codegenModuleRgctxs = new Il2CppRGCTXDefinition[codeGenModulePtrs.Length][];
for (var i = 0; i < codeGenModulePtrs.Length; i++)
{
var codeGenModule = ReadClassAtVirtualAddress<Il2CppCodeGenModule>(codeGenModulePtrs[i]);
codeGenModules[i] = codeGenModule;
string name = ReadStringToNull(MapVirtualAddressToRaw(codeGenModule.moduleName));
LibLogger.VerboseNewline($"\t\t-Read module data for {name}, contains {codeGenModule.methodPointerCount} method pointers starting at 0x{codeGenModule.methodPointers:X}");
if (codeGenModule.methodPointerCount > 0)
{
try
{
var ptrs = ReadClassArrayAtVirtualAddress<ulong>(codeGenModule.methodPointers, codeGenModule.methodPointerCount);
codeGenModuleMethodPointers[i] = ptrs;
LibLogger.VerboseNewline($"\t\t\t-Read {codeGenModule.methodPointerCount} method pointers.");
}
catch (Exception e)
{
LibLogger.VerboseNewline($"\t\t\tWARNING: Unable to get function pointers for {name}: {e.Message}");
codeGenModuleMethodPointers[i] = new ulong[codeGenModule.methodPointerCount];
}
}
if (codeGenModule.rgctxRangesCount > 0)
{
try
{
var ranges = ReadClassArrayAtVirtualAddress<Il2CppTokenRangePair>(codeGenModule.pRgctxRanges, codeGenModule.rgctxRangesCount);
codegenModuleRgctxRanges[i] = ranges;
LibLogger.VerboseNewline($"\t\t\t-Read {codeGenModule.rgctxRangesCount} RGCTX ranges.");
}
catch (Exception e)
{
LibLogger.VerboseNewline($"\t\t\tWARNING: Unable to get RGCTX ranges for {name}: {e.Message}");
codegenModuleRgctxRanges[i] = new Il2CppTokenRangePair[codeGenModule.rgctxRangesCount];
}
}
if (codeGenModule.rgctxsCount > 0)
{
try
{
var rgctxs = ReadClassArrayAtVirtualAddress<Il2CppRGCTXDefinition>(codeGenModule.rgctxs, codeGenModule.rgctxsCount);
codegenModuleRgctxs[i] = rgctxs;
LibLogger.VerboseNewline($"\t\t\t-Read {codeGenModule.rgctxsCount} RGCTXs.");
}
catch (Exception e)
{
LibLogger.VerboseNewline($"\t\t\tWARNING: Unable to get RGCTXs for {name}: {e.Message}");
codegenModuleRgctxs[i] = new Il2CppRGCTXDefinition[codeGenModule.rgctxsCount];
}
}
}
LibLogger.VerboseNewline($"\tOK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
else
{
LibLogger.Verbose("\tReading method pointers...");
start = DateTime.Now;
methodPointers = ReadClassArrayAtVirtualAddress<ulong>(codeRegistration.methodPointers, (long) codeRegistration.methodPointersCount);
LibLogger.VerboseNewline($"Read {methodPointers.Length} OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
LibLogger.Verbose("\tReading generic method tables...");
start = DateTime.Now;
genericMethodTables = ReadClassArrayAtVirtualAddress<Il2CppGenericMethodFunctionsDefinitions>(metadataRegistration.genericMethodTable, metadataRegistration.genericMethodTableCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading method specifications...");
start = DateTime.Now;
methodSpecs = ReadClassArrayAtVirtualAddress<Il2CppMethodSpec>(metadataRegistration.methodSpecs, metadataRegistration.methodSpecsCount);
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
LibLogger.Verbose("\tReading generic methods...");
start = DateTime.Now;
genericMethodDictionary = new Dictionary<int, ulong>();
foreach (var table in genericMethodTables)
{
var genericMethodIndex = table.genericMethodIndex;
var genericMethodPointerIndex = table.indices.methodIndex;
var methodDefIndex = GetGenericMethodFromIndex(genericMethodIndex, genericMethodPointerIndex, out _);
if (!genericMethodDictionary.ContainsKey(methodDefIndex) && genericMethodPointerIndex < genericMethodPointers.Length)
{
genericMethodDictionary.TryAdd(methodDefIndex, genericMethodPointers[genericMethodPointerIndex]);
}
}
LibLogger.VerboseNewline($"OK ({(DateTime.Now - start).TotalMilliseconds} ms)");
}
private int GetGenericMethodFromIndex(int genericMethodIndex, int genericMethodPointerIndex, out Il2CppGenericMethodRef? genericMethodRef)
{
var methodSpec = GetMethodSpec(genericMethodIndex);
var methodDefIndex = methodSpec.methodDefinitionIndex;
genericMethodRef = new Il2CppGenericMethodRef(methodSpec);
if (genericMethodPointerIndex >= 0)
{
if(genericMethodPointerIndex < genericMethodPointers.Length)
genericMethodRef.GenericVariantPtr = genericMethodPointers[genericMethodPointerIndex];
}
if (!ConcreteGenericMethods.ContainsKey(genericMethodRef.BaseMethod))
ConcreteGenericMethods[genericMethodRef.BaseMethod] = new List<Il2CppGenericMethodRef>();
ConcreteGenericMethods[genericMethodRef.BaseMethod].Add(genericMethodRef);
if (genericMethodRef.GenericVariantPtr > 0)
{
if (!ConcreteGenericImplementationsByAddress.ContainsKey(genericMethodRef.GenericVariantPtr))
ConcreteGenericImplementationsByAddress[genericMethodRef.GenericVariantPtr] = new List<Il2CppGenericMethodRef>();
ConcreteGenericImplementationsByAddress[genericMethodRef.GenericVariantPtr].Add(genericMethodRef);
}
return methodDefIndex;
}
public abstract byte GetByteAtRawAddress(ulong addr);
public abstract long MapVirtualAddressToRaw(ulong uiAddr);
public abstract ulong MapRawAddressToVirtual(uint offset);
public abstract ulong GetRVA(ulong pointer);
public bool TryMapRawAddressToVirtual(in uint offset, out ulong va)
{
try
{
va = MapRawAddressToVirtual(offset);
return true;
}
catch (Exception)
{
va = 0;
return false;
}
}
public bool TryMapVirtualAddressToRaw(ulong virtAddr, out long result)
{
try
{
result = MapVirtualAddressToRaw(virtAddr);
return true;
}
catch (Exception)
{
result = 0;
return false;
}
}
public T[] ReadClassArrayAtVirtualAddress<T>(ulong addr, long count) where T : new()
{
return ReadClassArrayAtRawAddr<T>(MapVirtualAddressToRaw(addr), count);
}
public T ReadClassAtVirtualAddress<T>(ulong addr) where T: new()
{
return ReadClassAtRawAddr<T>(MapVirtualAddressToRaw(addr));
}
public Il2CppGenericInst GetGenericInst(int index) => genericInsts[index];
public Il2CppMethodSpec GetMethodSpec(int index) => index >= methodSpecs.Length
? throw new ArgumentException($"GetMethodSpec: index {index} >= length {methodSpecs.Length}")
: index < 0
? throw new ArgumentException($"GetMethodSpec: index {index} < 0")
: methodSpecs[index];
public Il2CppType GetType(int index) => types[index];
public ulong GetRawMetadataUsage(uint index) => metadataUsages[index];
public ulong[] GetCodegenModuleMethodPointers(int codegenModuleIndex) => codeGenModuleMethodPointers[codegenModuleIndex];
public Il2CppCodeGenModule? GetCodegenModuleByName(string name) => codeGenModules.FirstOrDefault(m => m.Name == name);
public int GetCodegenModuleIndex(Il2CppCodeGenModule module) => Array.IndexOf(codeGenModules, module);
public int GetCodegenModuleIndexByName(string name) => GetCodegenModuleByName(name) is { } module ? GetCodegenModuleIndex(module) : -1;
public Il2CppTokenRangePair[] GetRGCTXRangePairsForModule(Il2CppCodeGenModule module) => codegenModuleRgctxRanges[GetCodegenModuleIndex(module)];
public Il2CppRGCTXDefinition[] GetRGCTXDataForPair(Il2CppCodeGenModule module, Il2CppTokenRangePair rangePair) => codegenModuleRgctxs[GetCodegenModuleIndex(module)].Skip(rangePair.start).Take(rangePair.length).ToArray();
public Il2CppType GetIl2CppTypeFromPointer(ulong pointer)
{
return typesDict[pointer];
}
public ulong[] GetPointers(ulong pointer, long count)
{
if (is32Bit)
return Array.ConvertAll(ReadClassArrayAtVirtualAddress<uint>(pointer, count), x => (ulong) x);
return ReadClassArrayAtVirtualAddress<ulong>(pointer, count);
}
public int GetFieldOffsetFromIndex(int typeIndex, int fieldIndexInType, int fieldIndex, bool isValueType, bool isStatic)
{
try
{
var offset = -1;
if (LibCpp2IlMain.MetadataVersion > 21)
{
var ptr = (ulong) fieldOffsets[typeIndex];
if (ptr > 0)
{
offset = ReadClassAtRawAddr<int>((long) ((ulong) MapVirtualAddressToRaw(ptr) + 4ul * (ulong) fieldIndexInType));
}
}
else
{
offset = (int) fieldOffsets[fieldIndex];
}
if (offset > 0)
{
if (isValueType && !isStatic)
{
if (is32Bit)
{
offset -= 8;
}
else
{
offset -= 16;
}
}
}
return offset;
}
catch
{
return -1;
}
}
public ulong GetMethodPointer(int methodIndex, int methodDefinitionIndex, int imageIndex, uint methodToken)
{
if (LibCpp2IlMain.MetadataVersion >= 24.2f)
{
if (genericMethodDictionary.TryGetValue(methodDefinitionIndex, out var methodPointer))
{
return methodPointer;
}
var ptrs = codeGenModuleMethodPointers[imageIndex];
var methodPointerIndex = methodToken & 0x00FFFFFFu;
return ptrs[methodPointerIndex - 1];
}
else
{
if (methodIndex >= 0)
{
return methodPointers[methodIndex];
}
genericMethodDictionary.TryGetValue(methodDefinitionIndex, out var methodPointer);
return methodPointer;
}
}
public ulong GetCustomAttributeGenerator(int index) => customAttributeGenerators![index];
public ulong[] AllCustomAttributeGenerators => LibCpp2IlMain.MetadataVersion >= 29 ? Array.Empty<ulong>() : LibCpp2IlMain.MetadataVersion >= 27 ? AllCustomAttributeGeneratorsV27 : customAttributeGenerators!;
private ulong[] AllCustomAttributeGeneratorsV27 =>
LibCpp2IlMain.TheMetadata!.imageDefinitions
.Select(i => (image: i, cgm: GetCodegenModuleByName(i.Name!)!, ptrSize: is32Bit ? 4UL : 8UL))
.SelectMany(tuple => LibCpp2ILUtils.Range(0, (int) tuple.image.customAttributeCount).Select(o => tuple.cgm.customAttributeCacheGenerator + (ulong) o * tuple.ptrSize))
.Select(p => ReadClassAtVirtualAddress<ulong>(p))
.ToArray();
public abstract byte[] GetRawBinaryContent();
public abstract ulong GetVirtualAddressOfExportedFunctionByName(string toFind);
public abstract ulong[] GetAllExportedIl2CppFunctionPointers();
public abstract byte[] GetEntirePrimaryExecutableSection();
public abstract ulong GetVirtualAddressOfPrimaryExecutableSection();
public (ulong pCodeRegistration, ulong pMetadataRegistration) PlusSearch(int methodCount, int typeDefinitionsCount)
{
ulong pCodeRegistration = 0;
ulong pMetadataRegistration;
LibLogger.VerboseNewline("\tAttempting to locate code and metadata registration functions...");
var plusSearch = new BinarySearcher(this, methodCount, typeDefinitionsCount);
LibLogger.VerboseNewline("\t\t-Searching for MetadataReg...");
pMetadataRegistration = LibCpp2IlMain.MetadataVersion < 24.5f
? plusSearch.FindMetadataRegistrationPre24_5()
: plusSearch.FindMetadataRegistrationPost24_5();
LibLogger.VerboseNewline("\t\t-Searching for CodeReg...");
if (pCodeRegistration == 0)
{
if (LibCpp2IlMain.MetadataVersion >= 24.2f)
{
LibLogger.VerboseNewline("\t\t\tUsing mscorlib full-disassembly approach to get codereg, this may take a while...");
pCodeRegistration = plusSearch.FindCodeRegistrationPost2019();
}
else
pCodeRegistration = plusSearch.FindCodeRegistrationPre2019();
}
if (pCodeRegistration == 0 && LibCpp2IlMain.Settings.AllowManualMetadataAndCodeRegInput)
{
LibLogger.Info("Couldn't identify a CodeRegistration address. If you know it, enter it now, otherwise enter nothing or zero to fail: ");
var crInput = Console.ReadLine();
ulong.TryParse(crInput, NumberStyles.HexNumber, null, out pCodeRegistration);
}
if (pMetadataRegistration == 0 && LibCpp2IlMain.Settings.AllowManualMetadataAndCodeRegInput)
{
LibLogger.Info("Couldn't identify a MetadataRegistration address. If you know it, enter it now, otherwise enter nothing or zero to fail: ");
var mrInput = Console.ReadLine();
ulong.TryParse(mrInput, NumberStyles.HexNumber, null, out pMetadataRegistration);
}
return (pCodeRegistration, pMetadataRegistration);
}
}
}