GlobalMetadata.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include <stdint.h>
  3. #include "Assembly.h"
  4. #include "MetadataCache.h"
  5. #include "StackTrace.h"
  6. #include "il2cpp-class-internals.h"
  7. #include "il2cpp-config.h"
  8. #include "os/Mutex.h"
  9. #include "utils/dynamic_array.h"
  10. #include "vm-utils/MethodDefinitionKey.h"
  11. struct MethodInfo;
  12. struct Il2CppClass;
  13. struct Il2CppGenericContext;
  14. struct Il2CppGenericInst;
  15. struct Il2CppGenericMethod;
  16. struct Il2CppType;
  17. struct Il2CppString;
  18. struct Il2CppMethodDefinition;
  19. struct Il2CppFieldDefinition;
  20. struct Il2CppTypeDefinition;
  21. struct Il2CppParameterDefinition;
  22. typedef struct Il2CppImageGlobalMetadata
  23. {
  24. TypeDefinitionIndex typeStart;
  25. TypeDefinitionIndex exportedTypeStart;
  26. CustomAttributeIndex customAttributeStart;
  27. MethodIndex entryPointIndex;
  28. const Il2CppImage* image;
  29. } Il2CppImageGlobalMetadata;
  30. namespace il2cpp
  31. {
  32. namespace vm
  33. {
  34. enum PackingSize
  35. {
  36. Zero,
  37. One,
  38. Two,
  39. Four,
  40. Eight,
  41. Sixteen,
  42. ThirtyTwo,
  43. SixtyFour,
  44. OneHundredTwentyEight
  45. };
  46. const int kBitIsValueType = 1;
  47. const int kBitIsEnum = 2;
  48. const int kBitHasFinalizer = 3;
  49. const int kBitHasStaticConstructor = 4;
  50. const int kBitIsBlittable = 5;
  51. const int kBitIsImportOrWindowsRuntime = 6;
  52. const int kPackingSize = 7; // This uses 4 bits from bit 7 to bit 10
  53. const int kPackingSizeIsDefault = 11;
  54. const int kClassSizeIsDefault = 12; // 此参数只用于反射查询,并无实际意义
  55. const int kSpecifiedPackingSize = 13; // This uses 4 bits from bit 13 to bit 16 。此参数目前只用于反射查询,并无直接用处
  56. class GlobalMetadata
  57. {
  58. public:
  59. static void Register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions);
  60. static bool Initialize(int32_t* imagesCount, int32_t* assembliesCount);
  61. static void InitializeAllMethodMetadata();
  62. static void* InitializeRuntimeMetadata(uintptr_t* metadataPointer, bool throwOnError);
  63. static void InitializeStringLiteralTable();
  64. static void InitializeWindowsRuntimeTypeNamesTables(WindowsRuntimeTypeNameToClassMap& windowsRuntimeTypeNameToClassMap, ClassToWindowsRuntimeTypeNameMap& classToWindowsRuntimeTypeNameMap);
  65. static void InitializeUnresolvedSignatureTable(Il2CppUnresolvedSignatureMap& unresolvedSignatureMap);
  66. static void InitializeGenericMethodTable(Il2CppMethodTableMap& methodTableMap);
  67. static void BuildIl2CppImage(Il2CppImage* image, ImageIndex imageIndex, AssemblyIndex* imageAssemblyIndex);
  68. static void BuildIl2CppAssembly(Il2CppAssembly* assembly, AssemblyIndex assemblyIndex, ImageIndex* assemblyImageIndex);
  69. static void Clear();
  70. static const MethodInfo* GetAssemblyEntryPoint(const Il2CppImage* image);
  71. static Il2CppMetadataTypeHandle GetAssemblyTypeHandle(const Il2CppImage* image, AssemblyTypeIndex index);
  72. static const Il2CppAssembly* GetReferencedAssembly(const Il2CppAssembly* assembly, int32_t referencedAssemblyTableIndex, const Il2CppAssembly assembliesTable[], int assembliesCount);
  73. static Il2CppMetadataTypeHandle GetAssemblyExportedTypeHandle(const Il2CppImage* image, AssemblyExportedTypeIndex index);
  74. static Il2CppClass* GetTypeInfoFromType(const Il2CppType* type);
  75. static Il2CppClass* GetTypeInfoFromTypeDefinitionIndex(TypeDefinitionIndex index);
  76. static Il2CppClass* GetTypeInfoFromHandle(Il2CppMetadataTypeHandle handle);
  77. static const Il2CppType* GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset);
  78. static Il2CppInterfaceOffsetInfo GetInterfaceOffsetInfo(const Il2CppClass* klass, TypeInterfaceOffsetIndex index);
  79. static Il2CppMetadataTypeHandle GetTypeHandleFromIndex(TypeDefinitionIndex typeIndex);
  80. static Il2CppMetadataTypeHandle GetTypeHandleFromType(const Il2CppType* type);
  81. static bool TypeIsNested(Il2CppMetadataTypeHandle handle);
  82. static bool TypeIsValueType(Il2CppMetadataTypeHandle handle);
  83. static bool StructLayoutPackIsDefault(Il2CppMetadataTypeHandle handle);
  84. static int32_t StructLayoutPack(Il2CppMetadataTypeHandle handle);
  85. static bool StructLayoutSizeIsDefault(Il2CppMetadataTypeHandle handle);
  86. static std::pair<const char*, const char*> GetTypeNamespaceAndName(Il2CppMetadataTypeHandle handle);
  87. static Il2CppClass* GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset);
  88. static Il2CppMetadataTypeHandle GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter);
  89. static CustomAttributesCache* GenerateCustomAttributesCache(const Il2CppImage* image, uint32_t token);
  90. static CustomAttributesCache* GenerateCustomAttributesCache(Il2CppMetadataCustomAttributeHandle handle);
  91. static Il2CppMetadataCustomAttributeHandle GetCustomAttributeTypeToken(const Il2CppImage* image, uint32_t token);
  92. static std::tuple<void*, void*> GetCustomAttributeDataRange(const Il2CppImage* image, uint32_t token);
  93. static bool HasAttribute(Il2CppMetadataCustomAttributeHandle token, Il2CppClass* attribute);
  94. static bool HasAttribute(const Il2CppImage* image, uint32_t token, Il2CppClass* attribute);
  95. static const MethodInfo* GetMethodInfoFromMethodHandle(Il2CppMetadataMethodDefinitionHandle handle);
  96. static const MethodInfo* GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot);
  97. static const uint8_t* GetParameterDefaultValue(const MethodInfo* method, int32_t parameterPosition, const Il2CppType** type, bool* isExplicitySetNullDefaultValue);
  98. static Il2CppMetadataGenericContainerHandle GetGenericContainerFromIndex(GenericContainerIndex index);
  99. static const Il2CppMethodDefinition* GetMethodDefinitionFromIndex(MethodIndex index);
  100. static MethodIndex GetMethodIndexFromDefinition(const Il2CppMethodDefinition* methodDefine);
  101. static const Il2CppType* GetInterfaceFromOffset(const Il2CppTypeDefinition* def, TypeInterfaceIndex offset);
  102. static Il2CppInterfaceOffsetInfo GetInterfaceOffsetInfo(const Il2CppTypeDefinition* typeDefine, TypeInterfaceOffsetIndex index);
  103. static const Il2CppMethodDefinition* GetMethodDefinitionFromVTableSlot(const Il2CppTypeDefinition* typeDefine, int32_t vTableSlot);
  104. //static const Il2CppMethodDefinition* GetMethodDefinitionFromVTableSlot(Il2CppClass* typeDefine, int32_t vTableSlot);
  105. static void InitializeTypeHandle(Il2CppType* type);
  106. static Il2CppClass* FromTypeDefinition(TypeDefinitionIndex index);
  107. static uint8_t ConvertPackingSizeEnumToValue(PackingSize packingSize);
  108. static PackingSize ConvertPackingSizeToEnum(uint8_t packingSize);
  109. static Il2CppMetadataGenericParameterHandle GetGenericParameterFromIndexInternal(GenericParameterIndex index);
  110. static const Il2CppFieldDefinition* GetFieldDefinitionFromTypeDefAndFieldIndex(const Il2CppTypeDefinition* typeDef, FieldIndex index);
  111. static const char* GetStringFromIndex(StringIndex index);
  112. static TypeDefinitionIndex GetIndexForTypeDefinition(const Il2CppClass* klass);
  113. static TypeDefinitionIndex GetIndexForTypeDefinition(const Il2CppTypeDefinition* typeDef);
  114. static const Il2CppParameterDefinition* GetParameterDefinitionFromIndex(const Il2CppClass* klass, ParameterIndex index);
  115. static const Il2CppParameterDefinition* GetParameterDefinitionFromIndex(const Il2CppMethodDefinition* methodDef, ParameterIndex index);
  116. static const uint8_t* GetFieldDefaultValue(const FieldInfo* field, const Il2CppType** type);
  117. static uint32_t GetFieldOffset(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field);
  118. static int GetFieldMarshaledSizeForField(const FieldInfo* field);
  119. static Il2CppMetadataFieldInfo GetFieldInfo(const Il2CppClass* klass, TypeFieldIndex index);
  120. static Il2CppMetadataMethodInfo GetMethodInfo(const Il2CppClass* klass, TypeMethodIndex index);
  121. static Il2CppMetadataParameterInfo GetParameterInfo(const Il2CppClass* klass, Il2CppMetadataMethodDefinitionHandle handle, MethodParameterIndex index);
  122. static Il2CppMetadataPropertyInfo GetPropertyInfo(const Il2CppClass* klass, TypePropertyIndex index);
  123. static Il2CppMetadataEventInfo GetEventInfo(const Il2CppClass* klass, TypeEventIndex index);
  124. static Il2CppMetadataGenericContainerHandle GetGenericContainerFromGenericClass(const Il2CppGenericClass* genericClass);
  125. static Il2CppMetadataGenericContainerHandle GetGenericContainerFromMethod(Il2CppMetadataMethodDefinitionHandle handle);
  126. static Il2CppMetadataGenericParameterHandle GetGenericParameterFromType(const Il2CppType* type);
  127. static const MethodInfo* GetGenericInstanceMethod(const MethodInfo* genericMethodDefinition, const Il2CppGenericContext* context);
  128. static const Il2CppType* GetTypeFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  129. static const Il2CppGenericMethod* GetGenericMethodFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  130. static std::pair<const Il2CppType*, const MethodInfo*> GetConstrainedCallFromRgctxDefinition(const Il2CppRGCTXDefinition* rgctxDef);
  131. static Il2CppClass* GetContainerDeclaringType(Il2CppMetadataGenericContainerHandle handle);
  132. static Il2CppClass* GetParameterDeclaringType(Il2CppMetadataGenericParameterHandle handle);
  133. static Il2CppMetadataGenericParameterHandle GetGenericParameterFromIndex(Il2CppMetadataGenericContainerHandle handle, GenericContainerParameterIndex index);
  134. static const Il2CppType* GetGenericParameterConstraintFromIndex(Il2CppMetadataGenericParameterHandle handle, GenericParameterConstraintIndex index);
  135. static void MakeGenericArgType(Il2CppMetadataGenericContainerHandle containerHandle, Il2CppMetadataGenericParameterHandle paramHandle, Il2CppType* arg);
  136. static uint32_t GetGenericContainerCount(Il2CppMetadataGenericContainerHandle handle);
  137. static bool GetGenericContainerIsMethod(Il2CppMetadataGenericContainerHandle handle);
  138. static const char* GetGenericParameterName(Il2CppMetadataGenericParameterHandle handle);
  139. static Il2CppGenericParameterInfo GetGenericParameterInfo(Il2CppMetadataGenericParameterHandle handle);
  140. static uint16_t GetGenericParameterFlags(Il2CppMetadataGenericParameterHandle handle);
  141. static int16_t GetGenericConstraintCount(Il2CppMetadataGenericParameterHandle handle);
  142. static const Il2CppGenericMethod* GetGenericMethodFromTokenMethodTuple(const Il2CppTokenIndexMethodTuple* tuple);
  143. static const MethodInfo* GetMethodInfoFromCatchPoint(const Il2CppCatchPoint* cp);
  144. static const MethodInfo* GetMethodInfoFromSequencePoint(const Il2CppSequencePoint* cp);
  145. static Il2CppClass* GetTypeInfoFromTypeSourcePair(const Il2CppTypeSourceFilePair* pair);
  146. static Il2CppClass* GetTypeInfoFromTypeIndex(TypeIndex index, bool throwOnError = true);
  147. static const Il2CppType* GetIl2CppTypeFromIndex(TypeIndex index);
  148. static const MethodInfo* GetMethodInfoFromMethodDefinitionIndex(MethodIndex index);
  149. template<typename T>
  150. static inline bool IsRuntimeMetadataInitialized(T item)
  151. {
  152. // Runtime metadata items are initialized to an encoded token with the low bit set
  153. // on startup and when initialized are a pointer to an runtime metadata item
  154. // So we can rely on pointer alignment being > 1 on our supported platforms
  155. return !((uintptr_t)item & 1);
  156. }
  157. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  158. static void GetAllManagedMethods(std::vector<MethodDefinitionKey>& managedMethods);
  159. #endif
  160. };
  161. }
  162. }