type_dex.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ZUCCHINI_TYPE_DEX_H_
  5. #define COMPONENTS_ZUCCHINI_TYPE_DEX_H_
  6. #include <stdint.h>
  7. namespace zucchini {
  8. namespace dex {
  9. // Contains types that models DEX executable format data structures.
  10. // See https://source.android.com/devices/tech/dalvik/dex-format
  11. // The supported versions are 035, 037, 038, and 039.
  12. enum class FormatId : uint8_t {
  13. b, // 22b.
  14. c, // 21c, 22c, 31c, 35c, 3rc, 45cc, 4rcc.
  15. h, // 21h.
  16. i, // 31i.
  17. l, // 51l.
  18. n, // 11n.
  19. s, // 21s, 22s.
  20. t, // 10t, 20t, 21t, 22t, 30t, 31t.
  21. x, // 10x, 11x, 12x, 22x, 23x, 32x.
  22. };
  23. struct Instruction {
  24. Instruction() = default;
  25. constexpr Instruction(uint8_t opcode_in,
  26. uint8_t layout_in,
  27. FormatId format_in,
  28. uint8_t variant_in = 1)
  29. : opcode(opcode_in),
  30. layout(layout_in),
  31. format(format_in),
  32. variant(variant_in) {}
  33. // The opcode that identifies the instruction.
  34. uint8_t opcode;
  35. // Number of uint16_t units for the instruction.
  36. uint8_t layout;
  37. // Identifier that groups similar instructions, as quick filter.
  38. FormatId format;
  39. // Number of successive opcodes that have the same format.
  40. uint8_t variant = 1;
  41. };
  42. constexpr Instruction kByteCode[] = {
  43. {0x00, 1, FormatId::x},
  44. {0x01, 1, FormatId::x},
  45. {0x02, 2, FormatId::x},
  46. {0x03, 3, FormatId::x},
  47. {0x04, 1, FormatId::x},
  48. {0x05, 2, FormatId::x},
  49. {0x06, 3, FormatId::x},
  50. {0x07, 1, FormatId::x},
  51. {0x08, 2, FormatId::x},
  52. {0x09, 3, FormatId::x},
  53. {0x0A, 1, FormatId::x},
  54. {0x0B, 1, FormatId::x},
  55. {0x0C, 1, FormatId::x},
  56. {0x0D, 1, FormatId::x},
  57. {0x0E, 1, FormatId::x},
  58. {0x0F, 1, FormatId::x},
  59. {0x10, 1, FormatId::x},
  60. {0x11, 1, FormatId::x},
  61. {0x12, 1, FormatId::n},
  62. {0x13, 2, FormatId::s},
  63. {0x14, 3, FormatId::i},
  64. {0x15, 2, FormatId::h},
  65. {0x16, 2, FormatId::s},
  66. {0x17, 3, FormatId::i},
  67. {0x18, 5, FormatId::l},
  68. {0x19, 2, FormatId::h},
  69. {0x1A, 2, FormatId::c},
  70. {0x1B, 3, FormatId::c},
  71. {0x1C, 2, FormatId::c},
  72. {0x1D, 1, FormatId::x},
  73. {0x1E, 1, FormatId::x},
  74. {0x1F, 2, FormatId::c},
  75. {0x20, 2, FormatId::c},
  76. {0x21, 1, FormatId::x},
  77. {0x22, 2, FormatId::c},
  78. {0x23, 2, FormatId::c},
  79. {0x24, 3, FormatId::c},
  80. {0x25, 3, FormatId::c},
  81. {0x26, 3, FormatId::t},
  82. {0x27, 1, FormatId::x},
  83. {0x28, 1, FormatId::t},
  84. {0x29, 2, FormatId::t},
  85. {0x2A, 3, FormatId::t},
  86. {0x2B, 3, FormatId::t},
  87. {0x2C, 3, FormatId::t},
  88. {0x2D, 2, FormatId::x, 5},
  89. {0x32, 2, FormatId::t, 6},
  90. {0x38, 2, FormatId::t, 6},
  91. // {0x3E, 1, FormatId::x, 6}, unused
  92. {0x44, 2, FormatId::x, 14},
  93. {0x52, 2, FormatId::c, 14},
  94. {0x60, 2, FormatId::c, 14},
  95. {0x6E, 3, FormatId::c, 5},
  96. // {0x73, 1, FormatId::x}, unused
  97. {0x74, 3, FormatId::c, 5},
  98. // {0x79, 1, FormatId::x, 2}, unused
  99. {0x7B, 1, FormatId::x, 21},
  100. {0x90, 2, FormatId::x, 32},
  101. {0xB0, 1, FormatId::x, 32},
  102. {0xD0, 2, FormatId::s, 8},
  103. {0xD8, 2, FormatId::b, 11},
  104. // {0xE3, 1, FormatId::x, 29}, unused
  105. {0xFA, 4, FormatId::c},
  106. {0xFB, 4, FormatId::c},
  107. {0xFC, 3, FormatId::c},
  108. {0xFD, 3, FormatId::c},
  109. {0xFE, 2, FormatId::c},
  110. {0xFF, 2, FormatId::c},
  111. };
  112. // Supported by MSVC, g++, and clang++. Ensures no gaps in packing.
  113. #pragma pack(push, 1)
  114. // header_item: Appears in the header section.
  115. struct HeaderItem {
  116. uint8_t magic[8];
  117. uint32_t checksum;
  118. uint8_t signature[20];
  119. uint32_t file_size;
  120. uint32_t header_size;
  121. uint32_t endian_tag;
  122. uint32_t link_size;
  123. uint32_t link_off;
  124. uint32_t map_off;
  125. uint32_t string_ids_size;
  126. uint32_t string_ids_off;
  127. uint32_t type_ids_size;
  128. uint32_t type_ids_off;
  129. uint32_t proto_ids_size;
  130. uint32_t proto_ids_off;
  131. uint32_t field_ids_size;
  132. uint32_t field_ids_off;
  133. uint32_t method_ids_size;
  134. uint32_t method_ids_off;
  135. uint32_t class_defs_size;
  136. uint32_t class_defs_off;
  137. uint32_t data_size;
  138. uint32_t data_off;
  139. };
  140. // string_id_item: String identifiers list.
  141. struct StringIdItem {
  142. uint32_t string_data_off;
  143. };
  144. // type_id_item: Type identifiers list.
  145. struct TypeIdItem {
  146. uint32_t descriptor_idx;
  147. };
  148. // proto_id_item: Method prototype identifiers list.
  149. struct ProtoIdItem {
  150. uint32_t shorty_idx;
  151. uint32_t return_type_idx;
  152. uint32_t parameters_off;
  153. };
  154. // field_id_item: Field identifiers list.
  155. struct FieldIdItem {
  156. uint16_t class_idx;
  157. uint16_t type_idx;
  158. uint32_t name_idx;
  159. };
  160. // method_id_item: Method identifiers list.
  161. struct MethodIdItem {
  162. uint16_t class_idx;
  163. uint16_t proto_idx;
  164. uint32_t name_idx;
  165. };
  166. // class_def_item: Class definitions list.
  167. struct ClassDefItem {
  168. uint32_t class_idx;
  169. uint32_t access_flags;
  170. uint32_t superclass_idx;
  171. uint32_t interfaces_off;
  172. uint32_t source_file_idx;
  173. uint32_t annotations_off;
  174. uint32_t class_data_off;
  175. uint32_t static_values_off;
  176. };
  177. // call_site_id_item: Call site identifiers list.
  178. struct CallSiteIdItem {
  179. uint32_t call_site_off;
  180. };
  181. // method_handle_type: Determines the behavior of the MethodHandleItem.
  182. enum class MethodHandleType : uint16_t {
  183. // FieldId
  184. kStaticPut = 0x00,
  185. kStaticGet = 0x01,
  186. kInstancePut = 0x02,
  187. kInstanceGet = 0x03,
  188. // MethodId
  189. kInvokeStatic = 0x04,
  190. kInvokeInstance = 0x05,
  191. kInvokeConstructor = 0x06,
  192. kInvokeDirect = 0x07,
  193. kInvokeInterface = 0x08,
  194. // Sentinel. If new types are added put them before this and increment.
  195. kMaxMethodHandleType = 0x09
  196. };
  197. // method_handle_item: Method handles referred within the Dex file.
  198. struct MethodHandleItem {
  199. uint16_t method_handle_type;
  200. uint16_t unused_1;
  201. uint16_t field_or_method_id;
  202. uint16_t unused_2;
  203. };
  204. // code_item: Header of a code item.
  205. struct CodeItem {
  206. uint16_t registers_size;
  207. uint16_t ins_size;
  208. uint16_t outs_size;
  209. uint16_t tries_size;
  210. uint32_t debug_info_off;
  211. uint32_t insns_size;
  212. // Variable length data follow for complete code item.
  213. };
  214. // Number of valid type codes for map_item elements in map_list.
  215. // See: https://source.android.com/devices/tech/dalvik/dex-format#type-codes
  216. constexpr uint32_t kMaxItemListSize = 21;
  217. constexpr uint16_t kTypeHeaderItem = 0x0000;
  218. constexpr uint16_t kTypeStringIdItem = 0x0001;
  219. constexpr uint16_t kTypeTypeIdItem = 0x0002;
  220. constexpr uint16_t kTypeProtoIdItem = 0x0003;
  221. constexpr uint16_t kTypeFieldIdItem = 0x0004;
  222. constexpr uint16_t kTypeMethodIdItem = 0x0005;
  223. constexpr uint16_t kTypeClassDefItem = 0x0006;
  224. constexpr uint16_t kTypeCallSiteIdItem = 0x0007;
  225. constexpr uint16_t kTypeMethodHandleItem = 0x0008;
  226. constexpr uint16_t kTypeMapList = 0x1000;
  227. constexpr uint16_t kTypeTypeList = 0x1001;
  228. constexpr uint16_t kTypeAnnotationSetRefList = 0x1002;
  229. constexpr uint16_t kTypeAnnotationSetItem = 0x1003;
  230. constexpr uint16_t kTypeClassDataItem = 0x2000;
  231. constexpr uint16_t kTypeCodeItem = 0x2001;
  232. constexpr uint16_t kTypeStringDataItem = 0x2002;
  233. constexpr uint16_t kTypeDebugInfoItem = 0x2003;
  234. constexpr uint16_t kTypeAnnotationItem = 0x2004;
  235. constexpr uint16_t kTypeEncodedArrayItem = 0x2005;
  236. constexpr uint16_t kTypeAnnotationsDirectoryItem = 0x2006;
  237. constexpr uint16_t kTypeHiddenApiClassDataItem = 0xF000;
  238. // map_item
  239. struct MapItem {
  240. uint16_t type;
  241. uint16_t unused;
  242. uint32_t size;
  243. uint32_t offset;
  244. };
  245. // map_list
  246. struct MapList {
  247. uint32_t size;
  248. MapItem list[kMaxItemListSize];
  249. };
  250. // type_item
  251. struct TypeItem {
  252. uint16_t type_idx;
  253. };
  254. // annotation_set_ref_item
  255. struct AnnotationSetRefItem {
  256. uint32_t annotations_off;
  257. };
  258. // annotation_off_item
  259. struct AnnotationOffItem {
  260. uint32_t annotation_off;
  261. };
  262. // field_annotation
  263. struct FieldAnnotation {
  264. uint32_t field_idx;
  265. uint32_t annotations_off;
  266. };
  267. // method_annotation
  268. struct MethodAnnotation {
  269. uint32_t method_idx;
  270. uint32_t annotations_off;
  271. };
  272. // parameter_annotation
  273. struct ParameterAnnotation {
  274. uint32_t method_idx;
  275. uint32_t annotations_off;
  276. };
  277. // annotations_directory_item
  278. struct AnnotationsDirectoryItem {
  279. uint32_t class_annotations_off;
  280. uint32_t fields_size;
  281. uint32_t annotated_methods_size;
  282. uint32_t annotated_parameters_size;
  283. // FieldAnnotation field_annotations[fields_size];
  284. // MethodAnnotation method_annotations[annotated_methods_size];
  285. // ParameterAnnotation parameter_annotations[annotated_parameters_size];
  286. // All *Annotation are 8 bytes each.
  287. };
  288. // try_item
  289. struct TryItem {
  290. uint32_t start_addr;
  291. uint16_t insn_count;
  292. uint16_t handler_off;
  293. };
  294. #pragma pack(pop)
  295. } // namespace dex
  296. } // namespace zucchini
  297. #endif // COMPONENTS_ZUCCHINI_TYPE_DEX_H_