il2cpp-metadata.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include <stdint.h>
  4. #include "il2cpp-tokentype.h"
  5. // This file contains the structures specifying how we store converted metadata.
  6. // These structures have 3 constraints:
  7. // 1. These structures will be stored in an external file, and as such must not contain any pointers.
  8. // All references to other metadata should occur via an index into a corresponding table.
  9. // 2. These structures are assumed to be const. Either const structures in the binary or mapped as
  10. // readonly memory from an external file. Do not add any 'calculated' fields which will be written to at runtime.
  11. // 3. These structures should be optimized for size. Other structures are used at runtime which can
  12. // be larger to store cached information
  13. typedef int32_t TypeIndex;
  14. typedef int32_t TypeDefinitionIndex;
  15. typedef int32_t FieldIndex;
  16. typedef int32_t DefaultValueIndex;
  17. typedef int32_t DefaultValueDataIndex;
  18. typedef int32_t CustomAttributeIndex;
  19. typedef int32_t ParameterIndex;
  20. typedef int32_t MethodIndex;
  21. typedef int32_t GenericMethodIndex;
  22. typedef int32_t PropertyIndex;
  23. typedef int32_t EventIndex;
  24. typedef int32_t GenericContainerIndex;
  25. typedef int32_t GenericParameterIndex;
  26. typedef int16_t GenericParameterConstraintIndex;
  27. typedef int32_t NestedTypeIndex;
  28. typedef int32_t InterfacesIndex;
  29. typedef int32_t VTableIndex;
  30. typedef int32_t InterfaceOffsetIndex;
  31. typedef int32_t RGCTXIndex;
  32. typedef int32_t StringIndex;
  33. typedef int32_t StringLiteralIndex;
  34. typedef int32_t GenericInstIndex;
  35. typedef int32_t ImageIndex;
  36. typedef int32_t AssemblyIndex;
  37. typedef int32_t InteropDataIndex;
  38. static const TypeIndex kTypeIndexInvalid = -1;
  39. static const TypeDefinitionIndex kTypeDefinitionIndexInvalid = -1;
  40. static const DefaultValueDataIndex kDefaultValueIndexNull = -1;
  41. static const CustomAttributeIndex kCustomAttributeIndexInvalid = -1;
  42. static const EventIndex kEventIndexInvalid = -1;
  43. static const FieldIndex kFieldIndexInvalid = -1;
  44. static const MethodIndex kMethodIndexInvalid = -1;
  45. static const PropertyIndex kPropertyIndexInvalid = -1;
  46. static const GenericContainerIndex kGenericContainerIndexInvalid = -1;
  47. static const GenericParameterIndex kGenericParameterIndexInvalid = -1;
  48. static const RGCTXIndex kRGCTXIndexInvalid = -1;
  49. static const StringLiteralIndex kStringLiteralIndexInvalid = -1;
  50. static const InteropDataIndex kInteropDataIndexInvalid = -1;
  51. // Encoded index (1 bit)
  52. // MethodDef - 0
  53. // MethodSpec - 1
  54. // We use the top 3 bits to indicate what table to index into
  55. // Type Binary Hex
  56. // Il2CppClass 001 0x20000000
  57. // Il2CppType 010 0x40000000
  58. // MethodInfo 011 0x60000000
  59. // FieldInfo 100 0x80000000
  60. // StringLiteral 101 0xA0000000
  61. // MethodRef 110 0xC0000000
  62. typedef uint32_t EncodedMethodIndex;
  63. enum Il2CppMetadataUsage
  64. {
  65. kIl2CppMetadataUsageInvalid,
  66. kIl2CppMetadataUsageTypeInfo,
  67. kIl2CppMetadataUsageIl2CppType,
  68. kIl2CppMetadataUsageMethodDef,
  69. kIl2CppMetadataUsageFieldInfo,
  70. kIl2CppMetadataUsageStringLiteral,
  71. kIl2CppMetadataUsageMethodRef,
  72. };
  73. #ifdef __cplusplus
  74. static inline Il2CppMetadataUsage GetEncodedIndexType(EncodedMethodIndex index)
  75. {
  76. return (Il2CppMetadataUsage)((index & 0xE0000000) >> 29);
  77. }
  78. static inline uint32_t GetDecodedMethodIndex(EncodedMethodIndex index)
  79. {
  80. return index & 0x1FFFFFFFU;
  81. }
  82. static inline uint32_t GetTokenType(uint32_t token)
  83. {
  84. return token & 0xFF000000;
  85. }
  86. static inline uint32_t GetTokenRowId(uint32_t token)
  87. {
  88. return token & 0x00FFFFFF;
  89. }
  90. #endif
  91. struct Il2CppImage;
  92. struct Il2CppType;
  93. struct Il2CppTypeDefinitionMetadata;
  94. typedef union Il2CppRGCTXDefinitionData
  95. {
  96. int32_t rgctxDataDummy;
  97. MethodIndex methodIndex;
  98. TypeIndex typeIndex;
  99. } Il2CppRGCTXDefinitionData;
  100. typedef enum Il2CppRGCTXDataType
  101. {
  102. IL2CPP_RGCTX_DATA_INVALID,
  103. IL2CPP_RGCTX_DATA_TYPE,
  104. IL2CPP_RGCTX_DATA_CLASS,
  105. IL2CPP_RGCTX_DATA_METHOD,
  106. IL2CPP_RGCTX_DATA_ARRAY,
  107. } Il2CppRGCTXDataType;
  108. #if RUNTIME_MONO
  109. typedef struct MonoRGCTXDefinition
  110. {
  111. Il2CppRGCTXDataType type;
  112. AssemblyIndex assemblyIndex;
  113. int32_t token;
  114. int32_t generic_parameter_index;
  115. } MonoRGCTXDefinition;
  116. #else
  117. typedef struct Il2CppRGCTXDefinition
  118. {
  119. Il2CppRGCTXDataType type;
  120. Il2CppRGCTXDefinitionData data;
  121. } Il2CppRGCTXDefinition;
  122. #endif
  123. typedef struct Il2CppInterfaceOffsetPair
  124. {
  125. TypeIndex interfaceTypeIndex;
  126. int32_t offset;
  127. } Il2CppInterfaceOffsetPair;
  128. typedef struct Il2CppTypeDefinition
  129. {
  130. StringIndex nameIndex;
  131. StringIndex namespaceIndex;
  132. TypeIndex byvalTypeIndex;
  133. TypeIndex byrefTypeIndex;
  134. TypeIndex declaringTypeIndex;
  135. TypeIndex parentIndex;
  136. TypeIndex elementTypeIndex; // we can probably remove this one. Only used for enums
  137. GenericContainerIndex genericContainerIndex;
  138. uint32_t flags;
  139. FieldIndex fieldStart;
  140. MethodIndex methodStart;
  141. EventIndex eventStart;
  142. PropertyIndex propertyStart;
  143. NestedTypeIndex nestedTypesStart;
  144. InterfacesIndex interfacesStart;
  145. VTableIndex vtableStart;
  146. InterfacesIndex interfaceOffsetsStart;
  147. uint16_t method_count;
  148. uint16_t property_count;
  149. uint16_t field_count;
  150. uint16_t event_count;
  151. uint16_t nested_type_count;
  152. uint16_t vtable_count;
  153. uint16_t interfaces_count;
  154. uint16_t interface_offsets_count;
  155. // bitfield to portably encode boolean values as single bits
  156. // 01 - valuetype;
  157. // 02 - enumtype;
  158. // 03 - has_finalize;
  159. // 04 - has_cctor;
  160. // 05 - is_blittable;
  161. // 06 - is_import_or_windows_runtime;
  162. // 07-10 - One of nine possible PackingSize values (0, 1, 2, 4, 8, 16, 32, 64, or 128)
  163. uint32_t bitfield;
  164. uint32_t token;
  165. } Il2CppTypeDefinition;
  166. typedef struct Il2CppFieldDefinition
  167. {
  168. StringIndex nameIndex;
  169. TypeIndex typeIndex;
  170. uint32_t token;
  171. } Il2CppFieldDefinition;
  172. typedef struct Il2CppFieldDefaultValue
  173. {
  174. FieldIndex fieldIndex;
  175. TypeIndex typeIndex;
  176. DefaultValueDataIndex dataIndex;
  177. } Il2CppFieldDefaultValue;
  178. typedef struct Il2CppFieldMarshaledSize
  179. {
  180. FieldIndex fieldIndex;
  181. TypeIndex typeIndex;
  182. int32_t size;
  183. } Il2CppFieldMarshaledSize;
  184. typedef struct Il2CppFieldRef
  185. {
  186. TypeIndex typeIndex;
  187. FieldIndex fieldIndex; // local offset into type fields
  188. } Il2CppFieldRef;
  189. typedef struct Il2CppParameterDefinition
  190. {
  191. StringIndex nameIndex;
  192. uint32_t token;
  193. TypeIndex typeIndex;
  194. } Il2CppParameterDefinition;
  195. typedef struct Il2CppParameterDefaultValue
  196. {
  197. ParameterIndex parameterIndex;
  198. TypeIndex typeIndex;
  199. DefaultValueDataIndex dataIndex;
  200. } Il2CppParameterDefaultValue;
  201. typedef struct Il2CppMethodDefinition
  202. {
  203. StringIndex nameIndex;
  204. TypeDefinitionIndex declaringType;
  205. TypeIndex returnType;
  206. ParameterIndex parameterStart;
  207. GenericContainerIndex genericContainerIndex;
  208. uint32_t token;
  209. uint16_t flags;
  210. uint16_t iflags;
  211. uint16_t slot;
  212. uint16_t parameterCount;
  213. } Il2CppMethodDefinition;
  214. typedef struct Il2CppEventDefinition
  215. {
  216. StringIndex nameIndex;
  217. TypeIndex typeIndex;
  218. MethodIndex add;
  219. MethodIndex remove;
  220. MethodIndex raise;
  221. uint32_t token;
  222. } Il2CppEventDefinition;
  223. typedef struct Il2CppPropertyDefinition
  224. {
  225. StringIndex nameIndex;
  226. MethodIndex get;
  227. MethodIndex set;
  228. uint32_t attrs;
  229. uint32_t token;
  230. } Il2CppPropertyDefinition;
  231. typedef struct Il2CppMethodSpec
  232. {
  233. MethodIndex methodDefinitionIndex;
  234. GenericInstIndex classIndexIndex;
  235. GenericInstIndex methodIndexIndex;
  236. } Il2CppMethodSpec;
  237. typedef struct Il2CppStringLiteral
  238. {
  239. uint32_t length;
  240. StringLiteralIndex dataIndex;
  241. } Il2CppStringLiteral;
  242. typedef struct
  243. {
  244. MethodIndex methodIndex;
  245. MethodIndex invokerIndex;
  246. } Il2CppGenericMethodIndices;
  247. typedef struct Il2CppGenericMethodFunctionsDefinitions
  248. {
  249. GenericMethodIndex genericMethodIndex;
  250. Il2CppGenericMethodIndices indices;
  251. } Il2CppGenericMethodFunctionsDefinitions;
  252. #define PUBLIC_KEY_BYTE_LENGTH 8
  253. static const int kPublicKeyByteLength = PUBLIC_KEY_BYTE_LENGTH;
  254. typedef struct Il2CppAssemblyNameDefinition
  255. {
  256. StringIndex nameIndex;
  257. StringIndex cultureIndex;
  258. StringIndex hashValueIndex;
  259. StringIndex publicKeyIndex;
  260. uint32_t hash_alg;
  261. int32_t hash_len;
  262. uint32_t flags;
  263. int32_t major;
  264. int32_t minor;
  265. int32_t build;
  266. int32_t revision;
  267. uint8_t public_key_token[PUBLIC_KEY_BYTE_LENGTH];
  268. } Il2CppAssemblyNameDefinition;
  269. typedef struct Il2CppImageDefinition
  270. {
  271. StringIndex nameIndex;
  272. AssemblyIndex assemblyIndex;
  273. TypeDefinitionIndex typeStart;
  274. uint32_t typeCount;
  275. TypeDefinitionIndex exportedTypeStart;
  276. uint32_t exportedTypeCount;
  277. MethodIndex entryPointIndex;
  278. uint32_t token;
  279. CustomAttributeIndex customAttributeStart;
  280. uint32_t customAttributeCount;
  281. } Il2CppImageDefinition;
  282. typedef struct Il2CppAssemblyDefinition
  283. {
  284. ImageIndex imageIndex;
  285. uint32_t token;
  286. int32_t referencedAssemblyStart;
  287. int32_t referencedAssemblyCount;
  288. Il2CppAssemblyNameDefinition aname;
  289. } Il2CppAssemblyDefinition;
  290. typedef struct Il2CppMetadataUsageList
  291. {
  292. uint32_t start;
  293. uint32_t count;
  294. } Il2CppMetadataUsageList;
  295. typedef struct Il2CppMetadataUsagePair
  296. {
  297. uint32_t destinationIndex;
  298. uint32_t encodedSourceIndex;
  299. } Il2CppMetadataUsagePair;
  300. typedef struct Il2CppCustomAttributeTypeRange
  301. {
  302. uint32_t token;
  303. int32_t start;
  304. int32_t count;
  305. } Il2CppCustomAttributeTypeRange;
  306. typedef struct Il2CppRange
  307. {
  308. int32_t start;
  309. int32_t length;
  310. } Il2CppRange;
  311. typedef struct Il2CppWindowsRuntimeTypeNamePair
  312. {
  313. StringIndex nameIndex;
  314. TypeIndex typeIndex;
  315. } Il2CppWindowsRuntimeTypeNamePair;
  316. #pragma pack(push, p1,4)
  317. typedef struct Il2CppGlobalMetadataHeader
  318. {
  319. int32_t sanity;
  320. int32_t version;
  321. int32_t stringLiteralOffset; // string data for managed code
  322. int32_t stringLiteralCount;
  323. int32_t stringLiteralDataOffset;
  324. int32_t stringLiteralDataCount;
  325. int32_t stringOffset; // string data for metadata
  326. int32_t stringCount;
  327. int32_t eventsOffset; // Il2CppEventDefinition
  328. int32_t eventsCount;
  329. int32_t propertiesOffset; // Il2CppPropertyDefinition
  330. int32_t propertiesCount;
  331. int32_t methodsOffset; // Il2CppMethodDefinition
  332. int32_t methodsCount;
  333. int32_t parameterDefaultValuesOffset; // Il2CppParameterDefaultValue
  334. int32_t parameterDefaultValuesCount;
  335. int32_t fieldDefaultValuesOffset; // Il2CppFieldDefaultValue
  336. int32_t fieldDefaultValuesCount;
  337. int32_t fieldAndParameterDefaultValueDataOffset; // uint8_t
  338. int32_t fieldAndParameterDefaultValueDataCount;
  339. int32_t fieldMarshaledSizesOffset; // Il2CppFieldMarshaledSize
  340. int32_t fieldMarshaledSizesCount;
  341. int32_t parametersOffset; // Il2CppParameterDefinition
  342. int32_t parametersCount;
  343. int32_t fieldsOffset; // Il2CppFieldDefinition
  344. int32_t fieldsCount;
  345. int32_t genericParametersOffset; // Il2CppGenericParameter
  346. int32_t genericParametersCount;
  347. int32_t genericParameterConstraintsOffset; // TypeIndex
  348. int32_t genericParameterConstraintsCount;
  349. int32_t genericContainersOffset; // Il2CppGenericContainer
  350. int32_t genericContainersCount;
  351. int32_t nestedTypesOffset; // TypeDefinitionIndex
  352. int32_t nestedTypesCount;
  353. int32_t interfacesOffset; // TypeIndex
  354. int32_t interfacesCount;
  355. int32_t vtableMethodsOffset; // EncodedMethodIndex
  356. int32_t vtableMethodsCount;
  357. int32_t interfaceOffsetsOffset; // Il2CppInterfaceOffsetPair
  358. int32_t interfaceOffsetsCount;
  359. int32_t typeDefinitionsOffset; // Il2CppTypeDefinition
  360. int32_t typeDefinitionsCount;
  361. int32_t imagesOffset; // Il2CppImageDefinition
  362. int32_t imagesCount;
  363. int32_t assembliesOffset; // Il2CppAssemblyDefinition
  364. int32_t assembliesCount;
  365. int32_t metadataUsageListsOffset; // Il2CppMetadataUsageList
  366. int32_t metadataUsageListsCount;
  367. int32_t metadataUsagePairsOffset; // Il2CppMetadataUsagePair
  368. int32_t metadataUsagePairsCount;
  369. int32_t fieldRefsOffset; // Il2CppFieldRef
  370. int32_t fieldRefsCount;
  371. int32_t referencedAssembliesOffset; // int32_t
  372. int32_t referencedAssembliesCount;
  373. int32_t attributesInfoOffset; // Il2CppCustomAttributeTypeRange
  374. int32_t attributesInfoCount;
  375. int32_t attributeTypesOffset; // TypeIndex
  376. int32_t attributeTypesCount;
  377. int32_t unresolvedVirtualCallParameterTypesOffset; // TypeIndex
  378. int32_t unresolvedVirtualCallParameterTypesCount;
  379. int32_t unresolvedVirtualCallParameterRangesOffset; // Il2CppRange
  380. int32_t unresolvedVirtualCallParameterRangesCount;
  381. int32_t windowsRuntimeTypeNamesOffset; // Il2CppWindowsRuntimeTypeNamePair
  382. int32_t windowsRuntimeTypeNamesSize;
  383. int32_t exportedTypeDefinitionsOffset; // TypeDefinitionIndex
  384. int32_t exportedTypeDefinitionsCount;
  385. } Il2CppGlobalMetadataHeader;
  386. #pragma pack(pop, p1)
  387. #if RUNTIME_MONO
  388. #pragma pack(push, p1,4)
  389. typedef struct Il2CppGlobalMonoMetadataHeader
  390. {
  391. int32_t sanity;
  392. int32_t version;
  393. int32_t stringOffset; // string data for metadata
  394. int32_t stringCount;
  395. int32_t genericMethodInfoMappingOffset; // hash -> generic MonoMethodInfo mapping
  396. int32_t genericMethodInfoMappingCount;
  397. int32_t monoStringOffset; // mono strings
  398. int32_t monoStringCount;
  399. int32_t methodMetadataOffset; // method metadata
  400. int32_t methodMetadataCount;
  401. int32_t genericArgumentIndicesOffset; // generic argument indices
  402. int32_t genericArgumentIndicesCount;
  403. int32_t typeTableOffset; // type table
  404. int32_t typeTableCount;
  405. int32_t fieldTableOffset; // field table
  406. int32_t fieldTableCount;
  407. int32_t genericMethodIndexTableOffset; // generic method index table
  408. int32_t genericMethodIndexTableCount;
  409. int32_t metaDataUsageListsTableOffset; // meta data usage lists table
  410. int32_t metaDataUsageListsTableCount;
  411. int32_t metaDataUsagePairsTableOffset; // meta data usage pairs table
  412. int32_t metaDataUsagePairsTableCount;
  413. int32_t assemblyNameTableOffset; // assembly names
  414. int32_t assemblyNameTableCount;
  415. } Il2CppGlobalMonoMetadataHeader;
  416. #pragma pack(pop, p1)
  417. #endif