Rizin
unix-like reverse engineering framework and cli tools
dex.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #ifndef RZ_DEX_H
6 #define RZ_DEX_H
7 
8 #include <rz_types.h>
9 #include <rz_util.h>
10 #include <rz_bin.h>
11 
12 #define RZ_DEX_RELOC_TARGETS "reloc-targets"
13 #define RZ_DEX_VIRT_ADDRESS 0x0100000000
14 #define RZ_DEX_RELOC_ADDRESS 0x8000000000
15 
16 typedef enum {
39 
40 typedef enum {
41  ACCESS_FLAG_PUBLIC /* */ = 0x00001,
42  ACCESS_FLAG_PRIVATE /* */ = 0x00002,
43  ACCESS_FLAG_PROTECTED /* */ = 0x00004,
44  ACCESS_FLAG_STATIC /* */ = 0x00008,
45  ACCESS_FLAG_FINAL /* */ = 0x00010,
46  ACCESS_FLAG_SYNCHRONIZED /* */ = 0x00020,
47  ACCESS_FLAG_BRIDGE /* */ = 0x00040,
48  ACCESS_FLAG_VARARGS /* */ = 0x00080,
49  ACCESS_FLAG_NATIVE /* */ = 0x00100,
50  ACCESS_FLAG_INTERFACE /* */ = 0x00200,
51  ACCESS_FLAG_ABSTRACT /* */ = 0x00400,
52  ACCESS_FLAG_STRICT /* */ = 0x00800,
53  ACCESS_FLAG_SYNTHETIC /* */ = 0x01000,
54  ACCESS_FLAG_ANNOTATION /* */ = 0x02000,
55  ACCESS_FLAG_ENUM /* */ = 0x04000,
56  ACCESS_FLAG_MODULE /* */ = 0x08000,
57  ACCESS_FLAG_CONSTRUCTOR /* */ = 0x10000,
60 
61 typedef struct dex_map_item_t {
62  ut16 map_type; /* DexMapItemType */
68 #define DEX_MAP_ITEM_SIZE (12)
69 
70 typedef struct dex_string_t {
73  char *data;
75 // DexString structure size is variable.
76 
77 typedef ut32 DexTypeId;
78 #define DEX_TYPE_ID_SIZE (sizeof(DexTypeId))
79 
80 typedef struct dex_proto_id_t {
87 #define DEX_PROTO_ID_SIZE (0xC)
88 
89 typedef struct dex_field_id_t {
95 #define DEX_FIELD_ID_SIZE (8)
96 
97 typedef struct dex_method_id_t {
102  /* code_* values are filled when parsing EncodedMethod */
106 #define DEX_METHOD_ID_SIZE (8)
107 
108 typedef struct dex_encoded_field_t {
113 
114 typedef struct dex_encoded_method_t {
118 
119  /* core related data */
127  /*ut16 padding*/
128  /*try_item[tries_size]*/
129  /*encoded_catch_handler_list handlers */
131 
132 // small note: on the official documentation all the
133 // variables are set as uint (aka ut32) but on their
134 // libdexfile defines some fields within class_def
135 // as ut16 + padding; to uniform with the real used
136 // code you will find some useless _padding variables
137 typedef struct dex_class_def_t {
149 
152 
153  RzList /*<DexEncodedField>*/ *static_fields;
154  RzList /*<DexEncodedField>*/ *instance_fields;
155  RzList /*<DexEncodedMethod>*/ *direct_methods;
156  RzList /*<DexEncodedMethod>*/ *virtual_methods;
158 #define DEX_CLASS_DEF_SIZE (0x20)
159 
160 typedef struct dex_t {
162  ut8 magic[4];
188 
189  /* lists */
190  RzPVector /*<DexString>*/ *strings;
191  RzPVector /*<DexProtoId>*/ *proto_ids;
192  RzPVector /*<DexFieldId>*/ *field_ids;
193  RzPVector /*<DexMethodId>*/ *method_ids;
194  RzPVector /*<DexClassDef>*/ *class_defs;
195 
197 
203 
206 
209 
210 RZ_API RZ_OWN RzList /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex);
211 RZ_API RZ_OWN RzList /*<RzBinClass *>*/ *rz_bin_dex_classes(RZ_NONNULL RzBinDex *dex);
212 RZ_API RZ_OWN RzList /*<RzBinField *>*/ *rz_bin_dex_fields(RZ_NONNULL RzBinDex *dex);
213 RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex);
214 RZ_API RZ_OWN RzList /*<RzBinSymbol *>*/ *rz_bin_dex_symbols(RZ_NONNULL RzBinDex *dex);
215 RZ_API RZ_OWN RzList /*<RzBinImport *>*/ *rz_bin_dex_imports(RZ_NONNULL RzBinDex *dex);
220 
228 
232 
235 
236 #endif /* RZ_DEX_H */
#define RZ_API
uint16_t ut16
uint32_t ut32
RZ_API RZ_OWN RzList * rz_bin_dex_symbols(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinSymbol*> containing the dex symbols.
Definition: dex.c:1267
RZ_API RZ_OWN RzList * rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex)
Returns a RzList<char*> containing the dex libraries.
Definition: dex.c:1520
DexMapItemType
Definition: dex.h:16
@ DEX_MAP_ITEM_TYPE_STRING_ID_ITEM
Definition: dex.h:18
@ DEX_MAP_ITEM_TYPE_CLASS_DEF_ITEM
Definition: dex.h:23
@ DEX_MAP_ITEM_TYPE_ANNOTATION_ITEM
Definition: dex.h:34
@ DEX_MAP_ITEM_TYPE_METHOD_ID_ITEM
Definition: dex.h:22
@ DEX_MAP_ITEM_TYPE_ANNOTATION_SET_REF_LIST
Definition: dex.h:28
@ DEX_MAP_ITEM_TYPE_HIDDENAPI_CLASS_DATA_ITEM
Definition: dex.h:37
@ DEX_MAP_ITEM_TYPE_TYPE_LIST
Definition: dex.h:27
@ DEX_MAP_ITEM_TYPE_STRING_DATA_ITEM
Definition: dex.h:32
@ DEX_MAP_ITEM_TYPE_MAP_LIST
Definition: dex.h:26
@ DEX_MAP_ITEM_TYPE_DEBUG_INFO_ITEM
Definition: dex.h:33
@ DEX_MAP_ITEM_TYPE_HEADER_ITEM
Definition: dex.h:17
@ DEX_MAP_ITEM_TYPE_METHOD_HANDLE_ITEM
Definition: dex.h:25
@ DEX_MAP_ITEM_TYPE_PROTO_ID_ITEM
Definition: dex.h:20
@ DEX_MAP_ITEM_TYPE_ANNOTATIONS_DIRECTORY_ITEM
Definition: dex.h:36
@ DEX_MAP_ITEM_TYPE_ENCODED_ARRAY_ITEM
Definition: dex.h:35
@ DEX_MAP_ITEM_TYPE_FIELD_ID_ITEM
Definition: dex.h:21
@ DEX_MAP_ITEM_TYPE_ANNOTATION_SET_ITEM
Definition: dex.h:29
@ DEX_MAP_ITEM_TYPE_CLASS_DATA_ITEM
Definition: dex.h:30
@ DEX_MAP_ITEM_TYPE_CALL_SITE_ID_ITEM
Definition: dex.h:24
@ DEX_MAP_ITEM_TYPE_TYPE_ID_ITEM
Definition: dex.h:19
@ DEX_MAP_ITEM_TYPE_CODE_ITEM
Definition: dex.h:31
struct dex_field_id_t DexFieldId
struct dex_string_t DexString
RZ_API void rz_bin_dex_checksum(RZ_NONNULL RzBinDex *dex, RZ_NONNULL RzBinHash *hash)
Sets the RzBinHash dex checksum (adler32)
Definition: dex.c:1990
RZ_API void rz_bin_dex_free(RZ_NULLABLE RzBinDex *dex)
Frees a RzBinDex struct.
Definition: dex.c:649
RZ_API RZ_OWN char * rz_bin_dex_resolve_class_by_idx(RZ_NONNULL RzBinDex *dex, ut32 class_idx)
Returns the resolved string linked to the given class id.
Definition: dex.c:1972
RZ_API RZ_OWN char * rz_bin_dex_resolve_string_by_idx(RZ_NONNULL RzBinDex *dex, ut32 string_idx)
Returns the resolved string linked to the given string id.
Definition: dex.c:1963
ut32 DexTypeId
Definition: dex.h:77
struct dex_encoded_method_t DexEncodedMethod
RZ_API ut64 rz_bin_dex_resolve_method_offset_by_idx(RZ_NONNULL RzBinDex *dex, ut32 method_idx)
Returns the resolved offset linked to the given method id.
Definition: dex.c:1928
struct dex_encoded_field_t DexEncodedField
struct dex_method_id_t DexMethodId
RZ_API RZ_OWN char * rz_bin_dex_resolve_method_by_idx(RZ_NONNULL RzBinDex *dex, ut32 method_idx)
Returns the resolved string linked to the given method id.
Definition: dex.c:1829
RZ_API RZ_OWN RzBinAddr * rz_bin_dex_resolve_symbol(RZ_NONNULL RzBinDex *dex, RzBinSpecialSymbol resolve)
Returns a RzBinAddr pointer containing the resolved RzBinSpecialSymbol.
Definition: dex.c:1670
struct dex_map_item_t DexMapItem
DexAccessFlag
Definition: dex.h:40
@ ACCESS_FLAG_MODULE
Definition: dex.h:56
@ ACCESS_FLAG_SYNTHETIC
Definition: dex.h:53
@ ACCESS_FLAG_CONSTRUCTOR
Definition: dex.h:57
@ ACCESS_FLAG_SYNCHRONIZED
Definition: dex.h:46
@ ACCESS_FLAG_VARARGS
Definition: dex.h:48
@ ACCESS_FLAG_DECLARED_SYNCHRONIZED
Definition: dex.h:58
@ ACCESS_FLAG_NATIVE
Definition: dex.h:49
@ ACCESS_FLAG_PRIVATE
Definition: dex.h:42
@ ACCESS_FLAG_ENUM
Definition: dex.h:55
@ ACCESS_FLAG_PROTECTED
Definition: dex.h:43
@ ACCESS_FLAG_STATIC
Definition: dex.h:44
@ ACCESS_FLAG_ANNOTATION
Definition: dex.h:54
@ ACCESS_FLAG_BRIDGE
Definition: dex.h:47
@ ACCESS_FLAG_STRICT
Definition: dex.h:52
@ ACCESS_FLAG_ABSTRACT
Definition: dex.h:51
@ ACCESS_FLAG_FINAL
Definition: dex.h:45
@ ACCESS_FLAG_INTERFACE
Definition: dex.h:50
@ ACCESS_FLAG_PUBLIC
Definition: dex.h:41
RZ_API RZ_BORROW RzBuffer * rz_bin_dex_relocations(RZ_NONNULL RzBinDex *dex)
Definition: dex.c:1821
RZ_API RZ_OWN RzList * rz_bin_dex_fields(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinField*> containing the dex fields.
Definition: dex.c:1232
RZ_API RZ_OWN RzList * rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinSection*> containing the dex sections.
Definition: dex.c:1200
RZ_API ut64 rz_bin_dex_debug_info(RZ_NONNULL RzBinDex *dex)
Returns the dex debug info RzBin values.
Definition: dex.c:2041
RZ_API RZ_OWN RzBinDex * rz_bin_dex_new(RZ_NONNULL RzBuffer *buf, ut64 base, RZ_NONNULL Sdb *kv)
Parses the dex file and returns a RzBinDex struct.
Definition: dex.c:669
RZ_API ut64 rz_bin_dex_resolve_string_offset_by_idx(RZ_NONNULL RzBinDex *dex, ut32 string_idx)
Returns the resolved offset linked to the given string id.
Definition: dex.c:1900
RZ_API RZ_OWN RzList * rz_bin_dex_classes(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinClass*> containing the dex classes.
Definition: dex.c:1131
RZ_API RZ_OWN RzList * rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinAddr*> containing the dex entripoints.
Definition: dex.c:1792
RZ_API RZ_OWN char * rz_bin_dex_resolve_type_id_by_idx(RZ_NONNULL RzBinDex *dex, ut32 type_idx)
Returns the resolved string linked to the given type id.
Definition: dex.c:1949
RZ_API RZ_OWN char * rz_bin_dex_resolve_field_by_idx(RZ_NONNULL RzBinDex *dex, ut32 field_idx)
Returns the resolved string linked to the given field id.
Definition: dex.c:1863
RZ_API RZ_OWN RzList * rz_bin_dex_imports(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinImport*> containing the dex imports.
Definition: dex.c:1379
RZ_API RZ_OWN char * rz_bin_dex_version(RZ_NONNULL RzBinDex *dex)
Returns the dex version (string format)
Definition: dex.c:2016
RZ_API RZ_OWN char * rz_bin_dex_access_flags_readable(ut32 access_flags)
Definition: dex.c:710
struct dex_class_def_t DexClassDef
struct dex_proto_id_t DexProtoId
RZ_API RZ_OWN RzList * rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex)
Returns a RzList<RzBinString*> containing the dex strings.
Definition: dex.c:735
RZ_API ut64 rz_bin_dex_resolve_type_id_offset_by_idx(RZ_NONNULL RzBinDex *dex, ut32 type_idx)
Returns the resolved offset linked to the given type id.
Definition: dex.c:1914
RZ_API void rz_bin_dex_sha1(RZ_NONNULL RzBinDex *dex, RZ_NONNULL RzBinHash *hash)
Sets the RzBinHash dex digest (sha1)
Definition: dex.c:2003
struct dex_t RzBinDex
RZ_API RZ_OWN char * rz_bin_dex_resolve_proto_by_idx(RZ_NONNULL RzBinDex *dex, ut32 proto_idx)
Returns the resolved string linked to the given prototype id.
Definition: dex.c:1981
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RzBinSpecialSymbol
Definition: rz_bin.h:136
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_BORROW
Definition: rz_types.h:63
RzList * instance_fields
Definition: dex.h:154
RzList * direct_methods
Definition: dex.h:155
ut32 class_data_offset
Definition: dex.h:146
ut32 n_interfaces
Definition: dex.h:150
ut32 access_flags
Definition: dex.h:140
ut32 source_file_idx
Definition: dex.h:144
ut16 * interfaces
Definition: dex.h:151
ut32 annotations_offset
Definition: dex.h:145
ut32 interfaces_offset
Definition: dex.h:143
ut32 static_values_offset
Definition: dex.h:147
ut16 _padding2
Definition: dex.h:142
ut16 _padding1
Definition: dex.h:139
ut64 offset
Definition: dex.h:148
ut16 superclass_idx
Definition: dex.h:141
ut16 class_idx
Definition: dex.h:138
RzList * virtual_methods
Definition: dex.h:156
RzList * static_fields
Definition: dex.h:153
ut64 field_idx
Definition: dex.h:110
ut64 access_flags
Definition: dex.h:111
ut16 registers_size
Definition: dex.h:120
ut64 code_offset
Definition: dex.h:126
ut32 debug_info_offset
Definition: dex.h:124
ut64 access_flags
Definition: dex.h:117
ut32 name_idx
Definition: dex.h:92
ut64 offset
Definition: dex.h:93
ut16 type_idx
Definition: dex.h:91
ut16 class_idx
Definition: dex.h:90
ut32 map_size
Definition: dex.h:64
ut64 offset
Definition: dex.h:66
ut16 unused
Definition: dex.h:63
ut16 map_type
Definition: dex.h:62
ut32 map_offset
Definition: dex.h:65
ut64 code_size
Definition: dex.h:104
ut32 name_idx
Definition: dex.h:100
ut16 class_idx
Definition: dex.h:98
ut64 code_offset
Definition: dex.h:103
ut16 proto_idx
Definition: dex.h:99
ut64 offset
Definition: dex.h:101
ut32 shorty_idx
Definition: dex.h:81
ut64 offset
Definition: dex.h:85
ut32 return_type_idx
Definition: dex.h:82
ut16 * type_list
Definition: dex.h:84
ut32 type_list_size
Definition: dex.h:83
ut64 offset
Definition: dex.h:72
char * data
Definition: dex.h:73
ut64 size
Definition: dex.h:71
Definition: dex.h:160
ut64 signature_offset
Definition: dex.h:167
ut32 data_size
Definition: dex.h:186
ut32 type_ids_offset
Definition: dex.h:177
ut32 string_ids_size
Definition: dex.h:174
ut32 proto_ids_size
Definition: dex.h:178
ut8 signature[20]
Definition: dex.h:166
ut32 endian_tag
Definition: dex.h:170
ut32 class_defs_size
Definition: dex.h:184
ut64 relocs_offset
Definition: dex.h:198
ut32 type_ids_size
Definition: dex.h:176
ut32 method_ids_size
Definition: dex.h:182
ut32 checksum
Definition: dex.h:164
ut32 link_offset
Definition: dex.h:172
RzPVector * strings
Definition: dex.h:190
ut32 relocs_size
Definition: dex.h:199
ut8 * relocs_code
Definition: dex.h:200
RzPVector * method_ids
Definition: dex.h:193
ut32 header_size
Definition: dex.h:169
RzPVector * field_ids
Definition: dex.h:192
ut32 data_offset
Definition: dex.h:187
RzPVector * class_defs
Definition: dex.h:194
DexTypeId * types
Definition: dex.h:196
ut64 header_offset
Definition: dex.h:161
ut32 link_size
Definition: dex.h:171
ut32 string_ids_offset
Definition: dex.h:175
RzBuffer * relocs_buffer
Definition: dex.h:201
ut32 method_ids_offset
Definition: dex.h:183
ut32 class_defs_offset
Definition: dex.h:185
ut32 field_ids_offset
Definition: dex.h:181
ut64 checksum_offset
Definition: dex.h:165
ut8 magic[4]
Definition: dex.h:162
ut32 map_offset
Definition: dex.h:173
RzPVector * proto_ids
Definition: dex.h:191
ut32 field_ids_size
Definition: dex.h:180
ut32 proto_ids_offset
Definition: dex.h:179
ut32 file_size
Definition: dex.h:168
Definition: sdb.h:63
static const char * resolve(struct Type *t, const char *foo, const char **bar)
Definition: swift.c:91
ut64(WINAPI *w32_GetEnabledXStateFeatures)()