GlobalMetadata.h 12 KB

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