forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalMetadata.cpp
More file actions
1857 lines (1528 loc) · 82.8 KB
/
GlobalMetadata.cpp
File metadata and controls
1857 lines (1528 loc) · 82.8 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "GlobalMetadata.h"
#include "il2cpp-config.h"
#include <map>
#include <limits>
#include <il2cpp-runtime-metadata.h>
#include "il2cpp-class-internals.h"
#include "il2cpp-tabledefs.h"
#include "il2cpp-runtime-stats.h"
#include "gc/GarbageCollector.h"
#include "metadata/ArrayMetadata.h"
#include "metadata/CustomAttributeDataReader.h"
#include "metadata/CustomAttributeCreator.h"
#include "metadata/GenericMetadata.h"
#include "metadata/GenericMethod.h"
#include "metadata/Il2CppTypeCompare.h"
#include "metadata/Il2CppTypeHash.h"
#include "metadata/Il2CppGenericContextCompare.h"
#include "metadata/Il2CppGenericContextHash.h"
#include "metadata/Il2CppGenericInstCompare.h"
#include "metadata/Il2CppGenericInstHash.h"
#include "metadata/Il2CppGenericMethodCompare.h"
#include "metadata/Il2CppGenericMethodHash.h"
#include "metadata/Il2CppSignature.h"
#include "os/Atomic.h"
#include "os/Mutex.h"
#include "utils/CallOnce.h"
#include "utils/Collections.h"
#include "utils/HashUtils.h"
#include "utils/Il2CppHashMap.h"
#include "utils/Il2CppHashSet.h"
#include "utils/InitOnce.h"
#include "utils/Memory.h"
#include "utils/StringUtils.h"
#include "utils/PathUtils.h"
#include "vm/Assembly.h"
#include "vm/Class.h"
#include "vm/ClassInlines.h"
#include "vm/GenericClass.h"
#include "vm/MetadataAlloc.h"
#include "vm/MetadataLoader.h"
#include "vm/MetadataLock.h"
#include "vm/Exception.h"
#include "vm/Method.h"
#include "vm/Object.h"
#include "vm/String.h"
#include "vm/Type.h"
#include "vm-utils/MethodDefinitionKey.h"
#include "vm-utils/NativeSymbol.h"
#include "vm-utils/VmStringUtils.h"
#include "Baselib.h"
#include "Cpp/ReentrantLock.h"
#include "GlobalMetadataFileInternals.h"
#include "hybridclr/metadata/MetadataUtil.h"
#include "hybridclr/metadata/MetadataModule.h"
static int32_t s_MetadataImagesCount = 0;
static Il2CppImageGlobalMetadata* s_MetadataImagesTable = NULL;
static TypeDefinitionIndex GetIndexForTypeDefinitionInternal(const Il2CppTypeDefinition* typeDefinition);
static GenericParameterIndex GetIndexForGenericParameter(Il2CppMetadataGenericParameterHandle handle);
static const MethodInfo* GetMethodInfoFromEncodedIndex(EncodedMethodIndex methodIndex);
static void* s_GlobalMetadata;
static const Il2CppGlobalMetadataHeader* s_GlobalMetadataHeader;
static const MethodInfo** s_MethodInfoDefinitionTable = NULL;
static Il2CppString** s_StringLiteralTable = NULL;
static il2cpp::utils::OnceFlag s_CustomAttributesOnceFlag;
static int s_CustomAttributesCount;
static const Il2CppCodeRegistration * s_GlobalMetadata_CodeRegistration;
static const Il2CppMetadataRegistration * s_Il2CppMetadataRegistration;
static Il2CppClass** s_TypeInfoTable = NULL;
static Il2CppClass** s_TypeInfoDefinitionTable = NULL;
template<typename T>
static T MetadataOffset(const void* metadata, size_t sectionOffset, size_t itemIndex)
{
return reinterpret_cast<T>(reinterpret_cast<uint8_t*>(const_cast<void*>(metadata)) + sectionOffset) + itemIndex;
}
const char* il2cpp::vm::GlobalMetadata::GetStringFromIndex(StringIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return hybridclr::metadata::MetadataModule::GetStringFromEncodeIndex(index);
}
IL2CPP_ASSERT(index <= s_GlobalMetadataHeader->stringSize);
return MetadataOffset<const char*>(s_GlobalMetadata, s_GlobalMetadataHeader->stringOffset, index);
}
static const char* GetWindowsRuntimeStringFromIndex(StringIndex index)
{
IL2CPP_ASSERT(index <= s_GlobalMetadataHeader->windowsRuntimeStringsSize);
return MetadataOffset<const char*>(s_GlobalMetadata, s_GlobalMetadataHeader->windowsRuntimeStringsOffset, index);
}
const Il2CppMethodDefinition* il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(MethodIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return hybridclr::metadata::MetadataModule::GetMethodDefinitionFromIndex(index);
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition));
return MetadataOffset<const Il2CppMethodDefinition*>(s_GlobalMetadata, s_GlobalMetadataHeader->methodsOffset, index);
}
MethodIndex il2cpp::vm::GlobalMetadata::GetMethodIndexFromDefinition(const Il2CppMethodDefinition* methodDef)
{
if (hybridclr::metadata::IsInterpreterMethod(methodDef))
{
return hybridclr::metadata::MetadataModule::GetImage(methodDef)->GetMethodIndexFromDefinition(methodDef);
}
return (MethodIndex)(methodDef - MetadataOffset<const Il2CppMethodDefinition*>(s_GlobalMetadata, s_GlobalMetadataHeader->methodsOffset, 0));
}
const MethodInfo* il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(MethodIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return hybridclr::metadata::MetadataModule::GetMethodInfoFromMethodDefinitionIndex(index);
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition));
return utils::InitOnce(&s_MethodInfoDefinitionTable[index], &g_MetadataLock, [index](il2cpp::os::FastAutoLock& _)
{
const Il2CppMethodDefinition* methodDefinition = il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(index);
Il2CppClass* typeInfo = il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeDefinitionIndex(methodDefinition->declaringType);
il2cpp::vm::Class::SetupMethods(typeInfo);
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(typeInfo->typeMetadataHandle);
return typeInfo->methods[index - typeDefinition->methodStart];
});
}
static const Il2CppEventDefinition* GetEventDefinitionFromIndex(const Il2CppImage* image, EventIndex index)
{
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->eventsSize / sizeof(Il2CppEventDefinition));
const Il2CppEventDefinition* events = (const Il2CppEventDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->eventsOffset);
return events + index;
}
static const Il2CppPropertyDefinition* GetPropertyDefinitionFromIndex(const Il2CppImage* image, PropertyIndex index)
{
if (hybridclr::metadata::IsInterpreterImage(image))
{
return hybridclr::metadata::MetadataModule::GetImage(image)->GetPropertyDefinitionFromIndex(hybridclr::metadata::DecodeMetadataIndex(index));
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->propertiesSize / sizeof(Il2CppPropertyDefinition));
const Il2CppPropertyDefinition* properties = (const Il2CppPropertyDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->propertiesOffset);
return properties + index;
}
const Il2CppParameterDefinition* il2cpp::vm::GlobalMetadata::GetParameterDefinitionFromIndex(const Il2CppClass* klass, ParameterIndex index)
{
if (hybridclr::metadata::IsInterpreterType(klass))
{
return hybridclr::metadata::MetadataModule::GetParameterDefinitionFromIndex(klass->image, index);
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->parametersSize / sizeof(Il2CppParameterDefinition)); const Il2CppParameterDefinition* parameters = (const Il2CppParameterDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->parametersOffset);
return parameters + index;
}
const Il2CppParameterDefinition* il2cpp::vm::GlobalMetadata::GetParameterDefinitionFromIndex(const Il2CppMethodDefinition* methodDef, ParameterIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(methodDef->nameIndex))
{
return hybridclr::metadata::MetadataModule::GetParameterDefinitionFromIndex(hybridclr::metadata::MetadataModule::GetImage(methodDef)->GetIl2CppImage(), index);
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->parametersSize / sizeof(Il2CppParameterDefinition));
const Il2CppParameterDefinition* parameters = (const Il2CppParameterDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->parametersOffset);
return parameters + index;
}
static const Il2CppMethodDefinition* GetMethodDefinitionFromEncodedIndex(EncodedMethodIndex methodIndex)
{
const MethodInfo* method = GetMethodInfoFromEncodedIndex(methodIndex);
if (!method)
{
return nullptr;
}
if (method->is_inflated)
{
method = method->genericMethod->methodDefinition;
}
return (const Il2CppMethodDefinition *)method->methodMetadataHandle;
}
uint8_t il2cpp::vm::GlobalMetadata::ConvertPackingSizeEnumToValue(PackingSize packingSize)
{
switch (packingSize)
{
case Zero:
return 0;
case One:
return 1;
case Two:
return 2;
case Four:
return 4;
case Eight:
return 8;
case Sixteen:
return 16;
case ThirtyTwo:
return 32;
case SixtyFour:
return 64;
case OneHundredTwentyEight:
return 128;
default:
Assert(0 && "Invalid packing size!");
return 0;
}
}
il2cpp::vm::PackingSize il2cpp::vm::GlobalMetadata::ConvertPackingSizeToEnum(uint8_t packingSize)
{
switch (packingSize)
{
case 0:
return PackingSize::Zero;
case 1:
return PackingSize::One;
case 2:
return PackingSize::Two;
case 4:
return PackingSize::Four;
case 8:
return PackingSize::Eight;
case 16:
return PackingSize::Sixteen;
case 32:
return PackingSize::ThirtyTwo;
case 64:
return PackingSize::SixtyFour;
case 128:
return OneHundredTwentyEight;
default:
Assert(0 && "Invalid packing size!");
return PackingSize::Zero;
}
}
static Il2CppGenericMethod BuildGenericMethodFromIndex(GenericMethodIndex index)
{
IL2CPP_ASSERT(index < s_Il2CppMetadataRegistration->methodSpecsCount);
const Il2CppMethodSpec* methodSpec = s_Il2CppMetadataRegistration->methodSpecs + index;
Il2CppGenericMethodKey key = il2cpp::vm::GlobalMetadata::BuildGenericMethodFromMethodSpec(methodSpec);
Il2CppGenericMethod gmethod = { 0 };
gmethod.methodDefinition = il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodHandle(key.methodDefinitionHandle);
gmethod.context = key.context;
return gmethod;
}
Il2CppGenericMethodKey il2cpp::vm::GlobalMetadata::BuildGenericMethodFromMethodSpec(const Il2CppMethodSpec* methodSpec)
{
const Il2CppMetadataMethodDefinitionHandle handle = (const Il2CppMetadataMethodDefinitionHandle)GetMethodDefinitionFromIndex(methodSpec->methodDefinitionIndex);
const Il2CppGenericInst* classInst = NULL;
const Il2CppGenericInst* methodInst = NULL;
if (methodSpec->classIndexIndex != -1)
{
IL2CPP_ASSERT(methodSpec->classIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
classInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->classIndexIndex];
}
if (methodSpec->methodIndexIndex != -1)
{
IL2CPP_ASSERT(methodSpec->methodIndexIndex < s_Il2CppMetadataRegistration->genericInstsCount);
methodInst = s_Il2CppMetadataRegistration->genericInsts[methodSpec->methodIndexIndex];
}
Il2CppGenericMethodKey key = { 0 };
key.methodDefinitionHandle = handle;
key.context.class_inst = classInst;
key.context.method_inst = methodInst;
return key;
}
static const MethodInfo* GetMethodInfoFromEncodedIndex(EncodedMethodIndex methodIndex)
{
Il2CppMetadataUsage usage = GetEncodedIndexType(methodIndex);
uint32_t index = GetDecodedMethodIndex(methodIndex);
switch (GetEncodedIndexType(methodIndex))
{
case kIl2CppMetadataUsageMethodRef:
return il2cpp::metadata::GenericMethod::GetMethod(BuildGenericMethodFromIndex(index));
case kIl2CppMetadataUsageMethodDef:
return il2cpp::vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(index);
case kIl2CppMetadataUsageInvalid:
{
switch (index)
{
case kIl2CppInvalidMetadataUsageNoData:
return NULL;
case kIl2CppInvalidMetadataUsageAmbiguousMethod:
return il2cpp::vm::Method::GetAmbiguousMethodInfo();
default:
IL2CPP_ASSERT(0);
break;
}
}
default:
IL2CPP_ASSERT(0);
break;
}
return NULL;
}
static Il2CppString* GetStringLiteralFromIndex(StringLiteralIndex index)
{
if (index == kStringLiteralIndexInvalid)
return NULL;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->stringLiteralSize / sizeof(Il2CppStringLiteral) && "Invalid string literal index ");
if (s_StringLiteralTable[index])
return s_StringLiteralTable[index];
const Il2CppStringLiteral* stringLiteral = (const Il2CppStringLiteral*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->stringLiteralOffset) + index;
Il2CppString* newString = il2cpp::vm::String::NewLen((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->stringLiteralDataOffset + stringLiteral->dataIndex, stringLiteral->length);
Il2CppString* prevString = il2cpp::os::Atomic::CompareExchangePointer<Il2CppString>(s_StringLiteralTable + index, newString, NULL);
if (prevString == NULL)
{
il2cpp::gc::GarbageCollector::SetWriteBarrier((void**)s_StringLiteralTable + index);
return newString;
}
return prevString;
}
static FieldInfo* GetFieldInfoFromIndex(EncodedMethodIndex index)
{
IL2CPP_ASSERT(s_GlobalMetadataHeader->fieldRefsSize >= 0 && index <= static_cast<uint32_t>(s_GlobalMetadataHeader->fieldRefsSize / sizeof(Il2CppFieldRef)));
const Il2CppFieldRef* fieldRef = MetadataOffset<const Il2CppFieldRef*>(s_GlobalMetadata, s_GlobalMetadataHeader->fieldRefsOffset, index);
const Il2CppClass* typeInfo = il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(fieldRef->typeIndex);
return typeInfo->fields + fieldRef->fieldIndex;
}
void il2cpp::vm::GlobalMetadata::Register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
{
s_GlobalMetadata_CodeRegistration = codeRegistration;
s_Il2CppMetadataRegistration = metadataRegistration;
}
typedef void (*Il2CppTypeUpdater)(Il2CppType*);
void il2cpp::vm::GlobalMetadata::InitializeTypeHandle(Il2CppType* type)
{
type->data.typeHandle = il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(type->data.__klassIndex);
}
static void ClearTypeHandle(Il2CppType* type)
{
type->data.__klassIndex = GetIndexForTypeDefinitionInternal(reinterpret_cast<const Il2CppTypeDefinition*>(type->data.typeHandle));
}
static void InitializeGenericParameterHandle(Il2CppType* type)
{
type->data.genericParameterHandle = il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndexInternal(type->data.__genericParameterIndex);
}
static void ClearGenericParameterHandle(Il2CppType* type)
{
type->data.__genericParameterIndex = GetIndexForGenericParameter(reinterpret_cast<Il2CppMetadataGenericParameterHandle>(type->data.genericParameterHandle));
}
static void ProcessIl2CppTypeDefinitions(Il2CppTypeUpdater updateTypeDef, Il2CppTypeUpdater updateGenericParam)
{
for (int32_t i = 0; i < s_Il2CppMetadataRegistration->typesCount; i++)
{
const Il2CppType* type = s_Il2CppMetadataRegistration->types[i];
switch (type->type)
{
case IL2CPP_TYPE_VOID:
case IL2CPP_TYPE_BOOLEAN:
case IL2CPP_TYPE_CHAR:
case IL2CPP_TYPE_I1:
case IL2CPP_TYPE_U1:
case IL2CPP_TYPE_I2:
case IL2CPP_TYPE_U2:
case IL2CPP_TYPE_I4:
case IL2CPP_TYPE_U4:
case IL2CPP_TYPE_I8:
case IL2CPP_TYPE_U8:
case IL2CPP_TYPE_R4:
case IL2CPP_TYPE_R8:
case IL2CPP_TYPE_STRING:
case IL2CPP_TYPE_VALUETYPE:
case IL2CPP_TYPE_CLASS:
case IL2CPP_TYPE_I:
case IL2CPP_TYPE_U:
case IL2CPP_TYPE_OBJECT:
case IL2CPP_TYPE_TYPEDBYREF:
// The Il2Cpp conversion process writes these types in a writeable section
// So we can const_cast them here safely
updateTypeDef(const_cast<Il2CppType*>(type));
break;
case IL2CPP_TYPE_VAR:
case IL2CPP_TYPE_MVAR:
updateGenericParam(const_cast<Il2CppType*>(type));
break;
default:
// Nothing do to
break;
}
}
}
bool il2cpp::vm::GlobalMetadata::Initialize(int32_t* imagesCount, int32_t* assembliesCount)
{
s_GlobalMetadata = vm::MetadataLoader::LoadMetadataFile("global-metadata.dat");
if (!s_GlobalMetadata)
return false;
s_GlobalMetadataHeader = (const Il2CppGlobalMetadataHeader*)s_GlobalMetadata;
IL2CPP_ASSERT(s_GlobalMetadataHeader->sanity == 0xFAB11BAF);
#if SUPPORT_METHOD_RETURN_TYPE_CUSTOM_ATTRIBUTE
IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 31);
#else
IL2CPP_ASSERT(s_GlobalMetadataHeader->version == 29);
#endif
IL2CPP_ASSERT(s_GlobalMetadataHeader->stringLiteralOffset == sizeof(Il2CppGlobalMetadataHeader));
s_MetadataImagesCount = *imagesCount = s_GlobalMetadataHeader->imagesSize / sizeof(Il2CppImageDefinition);
*assembliesCount = s_GlobalMetadataHeader->assembliesSize / sizeof(Il2CppAssemblyDefinition);
// Pre-allocate these arrays so we don't need to lock when reading later.
// These arrays hold the runtime metadata representation for metadata explicitly
// referenced during conversion. There is a corresponding table of same size
// in the converted metadata, giving a description of runtime metadata to construct.
s_MetadataImagesTable = (Il2CppImageGlobalMetadata*)IL2CPP_CALLOC(s_MetadataImagesCount, sizeof(Il2CppImageGlobalMetadata));
s_TypeInfoTable = (Il2CppClass**)IL2CPP_CALLOC(s_Il2CppMetadataRegistration->typesCount, sizeof(Il2CppClass*));
s_TypeInfoDefinitionTable = (Il2CppClass**)IL2CPP_CALLOC(s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition), sizeof(Il2CppClass*));
s_MethodInfoDefinitionTable = (const MethodInfo**)IL2CPP_CALLOC(s_GlobalMetadataHeader->methodsSize / sizeof(Il2CppMethodDefinition), sizeof(MethodInfo*));
ProcessIl2CppTypeDefinitions(InitializeTypeHandle, InitializeGenericParameterHandle);
return true;
}
void il2cpp::vm::GlobalMetadata::InitializeAllMethodMetadata()
{
for (size_t i = 0; i < s_Il2CppMetadataRegistration->metadataUsagesCount; i++)
{
uintptr_t* metadataPointer = reinterpret_cast<uintptr_t*>(s_Il2CppMetadataRegistration->metadataUsages[i]);
Il2CppMetadataUsage usage = GetEncodedIndexType(static_cast<uint32_t>(*metadataPointer));
switch (usage)
{
case kIl2CppMetadataUsageTypeInfo:
case kIl2CppMetadataUsageMethodDef:
case kIl2CppMetadataUsageMethodRef:
InitializeRuntimeMetadata(metadataPointer, false);
break;
default:
break;
}
}
}
// This method can be called from multiple threads, so it does have a data race. However, each
// thread is reading from the same read-only metadata, so each thread will set the same values.
// Therefore, we can safely ignore thread sanitizer issues in this method.
void* il2cpp::vm::GlobalMetadata::InitializeRuntimeMetadata(uintptr_t* metadataPointer, bool throwOnError) IL2CPP_DISABLE_TSAN
{
// This must be the only read of *metadataPointer
// This code has no locks and we need to ensure that we only read metadataPointer once
// so we don't read it once as an encoded token and once as an initialized pointer
uintptr_t metadataValue = (uintptr_t)os::Atomic::ReadPtrVal((intptr_t*)metadataPointer);
if (IsRuntimeMetadataInitialized(metadataValue))
return (void*)metadataValue;
uint32_t encodedToken = static_cast<uint32_t>(metadataValue);
Il2CppMetadataUsage usage = GetEncodedIndexType(encodedToken);
uint32_t decodedIndex = GetDecodedMethodIndex(encodedToken);
void* initialized = NULL;
switch (usage)
{
case kIl2CppMetadataUsageTypeInfo:
initialized = (void*)il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeIndex(decodedIndex, throwOnError);
break;
case kIl2CppMetadataUsageIl2CppType:
initialized = (void*)il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(decodedIndex);
break;
case kIl2CppMetadataUsageMethodDef:
case kIl2CppMetadataUsageMethodRef:
initialized = (void*)GetMethodInfoFromEncodedIndex(encodedToken);
break;
case kIl2CppMetadataUsageFieldInfo:
initialized = (void*)GetFieldInfoFromIndex(decodedIndex);
break;
case kIl2CppMetadataUsageStringLiteral:
initialized = (void*)GetStringLiteralFromIndex(decodedIndex);
break;
case kIl2CppMetadataUsageFieldRva:
const Il2CppType* unused;
initialized = (void*)GetFieldDefaultValue(GetFieldInfoFromIndex(decodedIndex), &unused);
{
const size_t MappedFieldDataAlignment = 8; // Should match System.Reflection.Metadata.ManagedPEBuilder.MappedFieldDataAlignment
IL2CPP_ASSERT(((uintptr_t)initialized % MappedFieldDataAlignment) == 0);
}
break;
case kIl2CppMetadataUsageInvalid:
break;
default:
IL2CPP_NOT_IMPLEMENTED(il2cpp::vm::GlobalMetadata::InitializeMethodMetadata);
break;
}
IL2CPP_ASSERT(IsRuntimeMetadataInitialized(initialized) && "ERROR: The low bit of the metadata item is still set, alignment issue");
if (initialized != NULL)
{
// Set the metadata pointer last, with a barrier, so it is the last item written
il2cpp::os::Atomic::PublishPointer((void**)metadataPointer, initialized);
}
return initialized;
}
void il2cpp::vm::GlobalMetadata::InitializeStringLiteralTable()
{
s_StringLiteralTable = (Il2CppString**)il2cpp::gc::GarbageCollector::AllocateFixed(s_GlobalMetadataHeader->stringLiteralSize / sizeof(Il2CppStringLiteral) * sizeof(Il2CppString*), NULL);
}
void il2cpp::vm::GlobalMetadata::InitializeWindowsRuntimeTypeNamesTables(WindowsRuntimeTypeNameToClassMap& windowsRuntimeTypeNameToClassMap, ClassToWindowsRuntimeTypeNameMap& classToWindowsRuntimeTypeNameMap)
{
int32_t typeCount = s_GlobalMetadataHeader->windowsRuntimeTypeNamesSize / sizeof(Il2CppWindowsRuntimeTypeNamePair);
const Il2CppWindowsRuntimeTypeNamePair* windowsRuntimeTypeNames = MetadataOffset<Il2CppWindowsRuntimeTypeNamePair*>(s_GlobalMetadata, s_GlobalMetadataHeader->windowsRuntimeTypeNamesOffset, 0);
windowsRuntimeTypeNameToClassMap.resize(typeCount / 2 + 1);
classToWindowsRuntimeTypeNameMap.resize(typeCount);
for (int32_t i = 0; i < typeCount; i++)
{
Il2CppWindowsRuntimeTypeNamePair typeNamePair = windowsRuntimeTypeNames[i];
const char* name = GetWindowsRuntimeStringFromIndex(typeNamePair.nameIndex);
const Il2CppType* type = GetIl2CppTypeFromIndex(typeNamePair.typeIndex);
Il2CppClass* klass = Class::FromIl2CppType(type);
if (!Class::IsNullable(klass))
{
// Don't add nullable types to name -> klass map because IReference`1<T> and Nullable`1<T>
// share windows runtime type names, and that would cause a collision.
windowsRuntimeTypeNameToClassMap.insert(std::make_pair(name, klass));
}
classToWindowsRuntimeTypeNameMap.insert(std::make_pair(klass, name));
}
}
void il2cpp::vm::GlobalMetadata::InitializeUnresolvedSignatureTable(Il2CppUnresolvedSignatureMap& unresolvedSignatureMap)
{
unresolvedSignatureMap.resize(s_GlobalMetadata_CodeRegistration->unresolvedIndirectCallCount);
for (uint32_t i = 0; i < s_GlobalMetadata_CodeRegistration->unresolvedIndirectCallCount; ++i)
{
const Il2CppMetadataRange* range = MetadataOffset<Il2CppMetadataRange*>(s_GlobalMetadata, s_GlobalMetadataHeader->unresolvedIndirectCallParameterRangesOffset, i);
il2cpp::metadata::Il2CppSignature signature;
signature.Count = range->length;
signature.Types = (const Il2CppType**)MetadataMalloc(range->length * sizeof(Il2CppType*));
for (int j = 0; j < range->length; ++j)
{
TypeIndex typeIndex = *MetadataOffset<TypeIndex*>(s_GlobalMetadata, s_GlobalMetadataHeader->unresolvedIndirectCallParameterTypesOffset, range->start + j);
const Il2CppType* type = GetIl2CppTypeFromIndex(typeIndex);
signature.Types[j] = type;
}
unresolvedSignatureMap.insert(std::make_pair(signature, i));
}
}
void il2cpp::vm::GlobalMetadata::InitializeGenericMethodTable(Il2CppMethodTableMap& methodTableMap)
{
methodTableMap.resize(s_Il2CppMetadataRegistration->genericMethodTableCount);
for (int32_t i = 0; i < s_Il2CppMetadataRegistration->genericMethodTableCount; i++)
{
const Il2CppGenericMethodFunctionsDefinitions* genericMethodIndices = s_Il2CppMetadataRegistration->genericMethodTable + i;
const Il2CppMethodSpec* methodSpec = s_Il2CppMetadataRegistration->methodSpecs + (genericMethodIndices->genericMethodIndex);
methodTableMap.insert(std::make_pair(metadata::Il2CppMethodSpecOrGenericMethod(methodSpec), &genericMethodIndices->indices));
}
}
static void ClearStringLiteralTable()
{
il2cpp::gc::GarbageCollector::FreeFixed(s_StringLiteralTable);
s_StringLiteralTable = NULL;
}
static void FreeAndNull(void** pointer)
{
IL2CPP_FREE(*pointer);
*pointer = NULL;
}
void il2cpp::vm::GlobalMetadata::Clear()
{
ClearStringLiteralTable();
FreeAndNull((void**)&s_MethodInfoDefinitionTable);
FreeAndNull((void**)&s_TypeInfoTable);
FreeAndNull((void**)&s_TypeInfoDefinitionTable);
ProcessIl2CppTypeDefinitions(ClearTypeHandle, ClearGenericParameterHandle);
vm::MetadataLoader::UnloadMetadataFile(s_GlobalMetadata);
s_GlobalMetadataHeader = NULL;
s_GlobalMetadata = NULL;
s_GlobalMetadata_CodeRegistration = NULL;
s_Il2CppMetadataRegistration = NULL;
}
void il2cpp::vm::GlobalMetadata::BuildIl2CppImage(Il2CppImage* image, ImageIndex imageIndex, AssemblyIndex* imageAssemblyIndex)
{
const Il2CppImageDefinition* imagesDefinitions = (const Il2CppImageDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->imagesOffset);
const Il2CppImageDefinition* imageDefinition = imagesDefinitions + imageIndex;
image->name = GetStringFromIndex(imageDefinition->nameIndex);
*imageAssemblyIndex = imageDefinition->assemblyIndex;
image->typeCount = imageDefinition->typeCount;
image->exportedTypeCount = imageDefinition->exportedTypeCount;
image->token = imageDefinition->token;
image->customAttributeCount = imageDefinition->customAttributeCount;
Il2CppImageGlobalMetadata* metadataImage = s_MetadataImagesTable + imageIndex;
metadataImage->typeStart = imageDefinition->typeStart;
metadataImage->customAttributeStart = imageDefinition->customAttributeStart;
metadataImage->entryPointIndex = imageDefinition->entryPointIndex;
metadataImage->exportedTypeStart = imageDefinition->exportedTypeStart;
metadataImage->image = image;
image->metadataHandle = reinterpret_cast<Il2CppMetadataImageHandle>(metadataImage);
}
void il2cpp::vm::GlobalMetadata::BuildIl2CppAssembly(Il2CppAssembly* assembly, AssemblyIndex assemblyIndex, ImageIndex* assemblyImageIndex)
{
const Il2CppAssemblyDefinition* assemblyDefinitions = (const Il2CppAssemblyDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->assembliesOffset);
const Il2CppAssemblyDefinition* assemblyDefinition = assemblyDefinitions + assemblyIndex;
assembly->token = assemblyDefinition->token;
assembly->referencedAssemblyStart = assemblyDefinition->referencedAssemblyStart;
assembly->referencedAssemblyCount = assemblyDefinition->referencedAssemblyCount;
Il2CppAssemblyName* assemblyName = &assembly->aname;
const Il2CppAssemblyNameDefinition* assemblyNameDefinition = &assemblyDefinition->aname;
Il2CppImage* image = il2cpp::vm::MetadataCache::GetImageFromIndex(assemblyDefinition->imageIndex);
assemblyName->name = GetStringFromIndex(assemblyNameDefinition->nameIndex);
assemblyName->culture = GetStringFromIndex(assemblyNameDefinition->cultureIndex);
assemblyName->public_key = (const uint8_t*)GetStringFromIndex(assemblyNameDefinition->publicKeyIndex);
assemblyName->hash_alg = assemblyNameDefinition->hash_alg;
assemblyName->hash_len = assemblyNameDefinition->hash_len;
assemblyName->flags = assemblyNameDefinition->flags;
assemblyName->major = assemblyNameDefinition->major;
assemblyName->minor = assemblyNameDefinition->minor;
assemblyName->build = assemblyNameDefinition->build;
assemblyName->revision = assemblyNameDefinition->revision;
memcpy(assemblyName->public_key_token, assemblyNameDefinition->public_key_token, sizeof(assemblyNameDefinition->public_key_token));
*assemblyImageIndex = assemblyDefinition->imageIndex;
}
static const Il2CppImageGlobalMetadata* GetImageMetadata(const Il2CppImage* image)
{
return reinterpret_cast<const Il2CppImageGlobalMetadata*>(image->metadataHandle);
}
const MethodInfo* il2cpp::vm::GlobalMetadata::GetAssemblyEntryPoint(const Il2CppImage* image)
{
const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
if (imageMetadata == NULL || imageMetadata->entryPointIndex == -1)
return NULL;
return GetMethodInfoFromMethodDefinitionIndex(imageMetadata->entryPointIndex);
}
Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(image->token))
{
return hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromRawIndex(image, index);
}
const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
IL2CPP_ASSERT(index >= 0 && index < static_cast<AssemblyTypeIndex>(image->typeCount));
TypeDefinitionIndex typeDefintionIndex = imageMetadata->typeStart + index;
return GetTypeHandleFromIndex(typeDefintionIndex);
}
const Il2CppAssembly* il2cpp::vm::GlobalMetadata::GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex, const Il2CppAssembly assembliesTable[], int assembliesCount)
{
IL2CPP_ASSERT(referencedAssemblyTableIndex < assembly->referencedAssemblyCount);
if (hybridclr::metadata::IsInterpreterImage(assembly->image))
{
return hybridclr::metadata::MetadataModule::GetImage(assembly->image)->GetReferencedAssembly(referencedAssemblyTableIndex, assembliesTable, assembliesCount);
}
referencedAssemblyTableIndex = assembly->referencedAssemblyStart + referencedAssemblyTableIndex;
IL2CPP_ASSERT(referencedAssemblyTableIndex >= 0 && static_cast<uint32_t>(referencedAssemblyTableIndex) <= s_GlobalMetadataHeader->referencedAssembliesSize / sizeof(int32_t));
const int32_t* referenceAssemblyIndicies = (const int32_t*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->referencedAssembliesOffset);
return assembliesTable + referenceAssemblyIndicies[referencedAssemblyTableIndex];
}
Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return hybridclr::metadata::MetadataModule::GetAssemblyExportedTypeHandleFromEncodeIndex(index);
}
if (index == kTypeDefinitionIndexInvalid)
return NULL;
IL2CPP_ASSERT(index >= 0 && index < static_cast<AssemblyExportedTypeIndex>(image->exportedTypeCount));
const Il2CppImageGlobalMetadata* imageMetadata = GetImageMetadata(image);
int32_t exportedTypeIndex = imageMetadata->exportedTypeStart + index;
IL2CPP_ASSERT(exportedTypeIndex >= 0 && static_cast<uint32_t>(exportedTypeIndex) < s_GlobalMetadataHeader->exportedTypeDefinitionsSize / sizeof(TypeDefinitionIndex));
TypeDefinitionIndex* exportedTypes = (TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->exportedTypeDefinitionsOffset);
TypeDefinitionIndex typeDefintionIndex = *(exportedTypes + exportedTypeIndex);
return GetTypeHandleFromIndex(typeDefintionIndex);
}
static const Il2CppTypeDefinition* GetTypeDefinitionForIndex(TypeDefinitionIndex index)
{
if (index == kTypeDefinitionIndexInvalid)
return NULL;
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return (const Il2CppTypeDefinition*)hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromEncodeIndex(index);
}
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
return typeDefinitions + index;
}
static TypeDefinitionIndex GetIndexForTypeDefinitionInternal(const Il2CppTypeDefinition* typeDefinition)
{
IL2CPP_ASSERT(typeDefinition);
if (hybridclr::metadata::IsInterpreterType(typeDefinition))
{
return static_cast<TypeDefinitionIndex>(hybridclr::metadata::MetadataModule::GetTypeEncodeIndex(typeDefinition));
}
const Il2CppTypeDefinition* typeDefinitions = (const Il2CppTypeDefinition*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->typeDefinitionsOffset);
IL2CPP_ASSERT(typeDefinition >= typeDefinitions && typeDefinition < typeDefinitions + s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
ptrdiff_t index = typeDefinition - typeDefinitions;
IL2CPP_ASSERT(index <= std::numeric_limits<TypeDefinitionIndex>::max());
return static_cast<TypeDefinitionIndex>(index);
}
Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromTypeDefinitionIndex(TypeDefinitionIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return hybridclr::metadata::MetadataModule::GetTypeInfoFromTypeDefinitionEncodeIndex(index);
}
if (index == kTypeIndexInvalid)
return NULL;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) < s_GlobalMetadataHeader->typeDefinitionsSize / sizeof(Il2CppTypeDefinition));
return utils::InitOnce(&s_TypeInfoDefinitionTable[index], &il2cpp::vm::g_MetadataLock, [index](il2cpp::os::FastAutoLock& _) { return FromTypeDefinition(index); });
}
Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle)
{
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
return GetTypeInfoFromTypeDefinitionIndex(GetIndexForTypeDefinitionInternal(typeDefinition));
}
Il2CppClass* il2cpp::vm::GlobalMetadata::GetTypeInfoFromType(const Il2CppType* type)
{
return GetTypeInfoFromHandle(type->data.typeHandle);
}
const Il2CppType* il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
{
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
return GetInterfaceFromOffset(typeDefinition, offset);
}
const Il2CppType* il2cpp::vm::GlobalMetadata::GetInterfaceFromOffset(const Il2CppTypeDefinition* typeDefinition, TypeInterfaceIndex offset)
{
IL2CPP_ASSERT(offset >= 0 && offset < typeDefinition->interfaces_count);
if (hybridclr::metadata::IsInterpreterType(typeDefinition))
{
return hybridclr::metadata::MetadataModule::GetInterfaceFromOffset(typeDefinition, offset);
}
InterfacesIndex index = typeDefinition->interfacesStart + offset;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->interfacesSize / sizeof(TypeIndex));
const TypeIndex* interfaceIndices = (const TypeIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->interfacesOffset);
return GetIl2CppTypeFromIndex(interfaceIndices[index]);
}
Il2CppInterfaceOffsetInfo il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index)
{
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
return GetInterfaceOffsetInfo(typeDefinition, index);
}
Il2CppInterfaceOffsetInfo il2cpp::vm::GlobalMetadata::GetInterfaceOffsetInfo(const Il2CppTypeDefinition* typeDefine, TypeInterfaceOffsetIndex index)
{
IL2CPP_ASSERT(index >= 0 && index < typeDefine->interface_offsets_count);
if (hybridclr::metadata::IsInterpreterType(typeDefine))
{
return hybridclr::metadata::MetadataModule::GetInterfaceOffsetInfo(typeDefine, index);
}
index = index + typeDefine->interfaceOffsetsStart;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->interfaceOffsetsSize / sizeof(Il2CppInterfaceOffsetPair));
const Il2CppInterfaceOffsetPair* interfaceOffsets = (const Il2CppInterfaceOffsetPair*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->interfaceOffsetsOffset);
return
{
GetIl2CppTypeFromIndex(interfaceOffsets[index].interfaceTypeIndex),
interfaceOffsets[index].offset
};
}
Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(TypeDefinitionIndex typeIndex)
{
return reinterpret_cast<Il2CppMetadataTypeHandle>(GetTypeDefinitionForIndex(typeIndex));
}
Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetTypeHandleFromType(const Il2CppType* type)
{
IL2CPP_ASSERT(type->type == IL2CPP_TYPE_CLASS || type->type == IL2CPP_TYPE_VALUETYPE);
return type->data.typeHandle;
}
bool il2cpp::vm::GlobalMetadata::TypeIsNested(Il2CppMetadataTypeHandle handle)
{
return reinterpret_cast<const Il2CppTypeDefinition*>(handle)->declaringTypeIndex != kTypeIndexInvalid;
}
bool il2cpp::vm::GlobalMetadata::TypeIsValueType(Il2CppMetadataTypeHandle handle)
{
return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kBitIsValueType - 1)) & 0x1;
}
bool il2cpp::vm::GlobalMetadata::StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle)
{
return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kPackingSizeIsDefault - 1)) & 0x1;
}
bool il2cpp::vm::GlobalMetadata::StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle)
{
return (reinterpret_cast<const Il2CppTypeDefinition*>(handle)->bitfield >> (kClassSizeIsDefault - 1)) & 0x1;
}
std::pair<const char*, const char*> il2cpp::vm::GlobalMetadata::GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle)
{
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
return std::make_pair
(
GetStringFromIndex(typeDefinition->namespaceIndex),
GetStringFromIndex(typeDefinition->nameIndex)
);
}
Il2CppClass* il2cpp::vm::GlobalMetadata::GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
{
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(klass->typeMetadataHandle);
IL2CPP_ASSERT(offset >= 0 && offset < typeDefinition->nested_type_count);
if (hybridclr::metadata::IsInterpreterType(klass))
{
return hybridclr::metadata::MetadataModule::GetNestedTypeFromOffset(klass, offset);
}
NestedTypeIndex index = typeDefinition->nestedTypesStart + offset;
IL2CPP_ASSERT(index >= 0 && static_cast<uint32_t>(index) <= s_GlobalMetadataHeader->nestedTypesSize / sizeof(TypeDefinitionIndex));
const TypeDefinitionIndex* nestedTypeIndices = (const TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->nestedTypesOffset);
return GetTypeInfoFromTypeDefinitionIndex(nestedTypeIndices[index]);
}
Il2CppMetadataTypeHandle il2cpp::vm::GlobalMetadata::GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter)
{
if (!iter)
return NULL;
const Il2CppTypeDefinition* typeDefinition = reinterpret_cast<const Il2CppTypeDefinition*>(handle);
if (hybridclr::metadata::IsInterpreterType(typeDefinition))
{
return hybridclr::metadata::MetadataModule::GetNestedTypes(handle, iter);
}
const TypeDefinitionIndex* nestedTypeIndices = (const TypeDefinitionIndex*)((const char*)s_GlobalMetadata + s_GlobalMetadataHeader->nestedTypesOffset);
if (!*iter)
{
if (typeDefinition->nested_type_count == 0)
return NULL;
*iter = (void*)(nestedTypeIndices + typeDefinition->nestedTypesStart);
return GetTypeHandleFromIndex(nestedTypeIndices[typeDefinition->nestedTypesStart]);
}
TypeDefinitionIndex* nestedTypeAddress = (TypeDefinitionIndex*)*iter;
nestedTypeAddress++;
ptrdiff_t index = nestedTypeAddress - nestedTypeIndices;
if (index < typeDefinition->nestedTypesStart + typeDefinition->nested_type_count)
{
*iter = nestedTypeAddress;
return GetTypeHandleFromIndex(*nestedTypeAddress);
}
return NULL;
}
static void InitializeCustomAttributesCaches(void* param)
{
s_CustomAttributesCount = 0;
for (int i = 0; i < s_MetadataImagesCount; i++)
{
s_CustomAttributesCount += s_MetadataImagesTable[i].image->customAttributeCount;
}
}
static int CompareTokens(const void* pkey, const void* pelem)
{
return (int)(((Il2CppCustomAttributeDataRange*)pkey)->token - ((Il2CppCustomAttributeDataRange*)pelem)->token);
}
static const Il2CppImageGlobalMetadata* GetImageForCustomAttributeIndex(CustomAttributeIndex index)
{
if (hybridclr::metadata::IsInterpreterIndex(index))
{
return reinterpret_cast<const Il2CppImageGlobalMetadata*>(hybridclr::metadata::MetadataModule::GetImageByEncodedIndex(index)->GetIl2CppImage()->metadataHandle);
}
for (int32_t imageIndex = 0; imageIndex < s_MetadataImagesCount; imageIndex++)
{
const Il2CppImageGlobalMetadata* imageMetadta = s_MetadataImagesTable + imageIndex;
IL2CPP_ASSERT(index >= 0);
if (index >= imageMetadta->customAttributeStart && static_cast<uint32_t>(index) < (imageMetadta->customAttributeStart + imageMetadta->image->customAttributeCount))
return imageMetadta;
}
IL2CPP_ASSERT(0 && "Failed to find owning image for custom attribute index");
return NULL;
}
static CustomAttributeIndex GetCustomAttributeIndex(const Il2CppCustomAttributeDataRange* attrDataRange)
{
if (attrDataRange == NULL)
return kCustomAttributeIndexInvalid;
if (hybridclr::metadata::IsInterpreterIndex(GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(*attrDataRange)))
{
return hybridclr::metadata::MetadataModule::GetImage(hybridclr::metadata::DecodeImageIndex(GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(*attrDataRange)))
->GetCustomAttributeIndex(attrDataRange->token);
}
const Il2CppCustomAttributeDataRange* attributeTypeRangeStart = MetadataOffset<const Il2CppCustomAttributeDataRange*>(s_GlobalMetadata, s_GlobalMetadataHeader->attributeDataRangeOffset, 0);
CustomAttributeIndex index = (CustomAttributeIndex)(attrDataRange - attributeTypeRangeStart);
IL2CPP_ASSERT(index >= 0 && index < (CustomAttributeIndex)(s_GlobalMetadataHeader->attributeDataRangeSize / sizeof(Il2CppCustomAttributeDataRange)));