Rizin
unix-like reverse engineering framework and cli tools
dex.h File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_bin.h>

Go to the source code of this file.

Classes

struct  dex_map_item_t
 
struct  dex_string_t
 
struct  dex_proto_id_t
 
struct  dex_field_id_t
 
struct  dex_method_id_t
 
struct  dex_encoded_field_t
 
struct  dex_encoded_method_t
 
struct  dex_class_def_t
 
struct  dex_t
 

Macros

#define RZ_DEX_RELOC_TARGETS   "reloc-targets"
 
#define RZ_DEX_VIRT_ADDRESS   0x0100000000
 
#define RZ_DEX_RELOC_ADDRESS   0x8000000000
 
#define DEX_MAP_ITEM_SIZE   (12)
 
#define DEX_TYPE_ID_SIZE   (sizeof(DexTypeId))
 
#define DEX_PROTO_ID_SIZE   (0xC)
 
#define DEX_FIELD_ID_SIZE   (8)
 
#define DEX_METHOD_ID_SIZE   (8)
 
#define DEX_CLASS_DEF_SIZE   (0x20)
 

Typedefs

typedef struct dex_map_item_t DexMapItem
 
typedef struct dex_string_t DexString
 
typedef ut32 DexTypeId
 
typedef struct dex_proto_id_t DexProtoId
 
typedef struct dex_field_id_t DexFieldId
 
typedef struct dex_method_id_t DexMethodId
 
typedef struct dex_encoded_field_t DexEncodedField
 
typedef struct dex_encoded_method_t DexEncodedMethod
 
typedef struct dex_class_def_t DexClassDef
 
typedef struct dex_t RzBinDex
 

Enumerations

enum  DexMapItemType {
  DEX_MAP_ITEM_TYPE_HEADER_ITEM = 0x0000 , DEX_MAP_ITEM_TYPE_STRING_ID_ITEM = 0x0001 , DEX_MAP_ITEM_TYPE_TYPE_ID_ITEM = 0x0002 , DEX_MAP_ITEM_TYPE_PROTO_ID_ITEM = 0x0003 ,
  DEX_MAP_ITEM_TYPE_FIELD_ID_ITEM = 0x0004 , DEX_MAP_ITEM_TYPE_METHOD_ID_ITEM = 0x0005 , DEX_MAP_ITEM_TYPE_CLASS_DEF_ITEM = 0x0006 , DEX_MAP_ITEM_TYPE_CALL_SITE_ID_ITEM = 0x0007 ,
  DEX_MAP_ITEM_TYPE_METHOD_HANDLE_ITEM = 0x0008 , DEX_MAP_ITEM_TYPE_MAP_LIST = 0x1000 , DEX_MAP_ITEM_TYPE_TYPE_LIST = 0x1001 , DEX_MAP_ITEM_TYPE_ANNOTATION_SET_REF_LIST = 0x1002 ,
  DEX_MAP_ITEM_TYPE_ANNOTATION_SET_ITEM = 0x1003 , DEX_MAP_ITEM_TYPE_CLASS_DATA_ITEM = 0x2000 , DEX_MAP_ITEM_TYPE_CODE_ITEM = 0x2001 , DEX_MAP_ITEM_TYPE_STRING_DATA_ITEM = 0x2002 ,
  DEX_MAP_ITEM_TYPE_DEBUG_INFO_ITEM = 0x2003 , DEX_MAP_ITEM_TYPE_ANNOTATION_ITEM = 0x2004 , DEX_MAP_ITEM_TYPE_ENCODED_ARRAY_ITEM = 0x2005 , DEX_MAP_ITEM_TYPE_ANNOTATIONS_DIRECTORY_ITEM = 0x2006 ,
  DEX_MAP_ITEM_TYPE_HIDDENAPI_CLASS_DATA_ITEM = 0xF000
}
 
enum  DexAccessFlag {
  ACCESS_FLAG_PUBLIC = 0x00001 , ACCESS_FLAG_PRIVATE = 0x00002 , ACCESS_FLAG_PROTECTED = 0x00004 , ACCESS_FLAG_STATIC = 0x00008 ,
  ACCESS_FLAG_FINAL = 0x00010 , ACCESS_FLAG_SYNCHRONIZED = 0x00020 , ACCESS_FLAG_BRIDGE = 0x00040 , ACCESS_FLAG_VARARGS = 0x00080 ,
  ACCESS_FLAG_NATIVE = 0x00100 , ACCESS_FLAG_INTERFACE = 0x00200 , ACCESS_FLAG_ABSTRACT = 0x00400 , ACCESS_FLAG_STRICT = 0x00800 ,
  ACCESS_FLAG_SYNTHETIC = 0x01000 , ACCESS_FLAG_ANNOTATION = 0x02000 , ACCESS_FLAG_ENUM = 0x04000 , ACCESS_FLAG_MODULE = 0x08000 ,
  ACCESS_FLAG_CONSTRUCTOR = 0x10000 , ACCESS_FLAG_DECLARED_SYNCHRONIZED = 0x20000
}
 

Functions

RZ_API RZ_OWN RzBinDexrz_bin_dex_new (RZ_NONNULL RzBuffer *buf, ut64 base, RZ_NONNULL Sdb *kv)
 Parses the dex file and returns a RzBinDex struct. More...
 
RZ_API void rz_bin_dex_free (RZ_NULLABLE RzBinDex *dex)
 Frees a RzBinDex struct. More...
 
RZ_API RZ_OWN char * rz_bin_dex_version (RZ_NONNULL RzBinDex *dex)
 Returns the dex version (string format) More...
 
RZ_API ut64 rz_bin_dex_debug_info (RZ_NONNULL RzBinDex *dex)
 Returns the dex debug info RzBin values. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_strings (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinString*> containing the dex strings. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_classes (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinClass*> containing the dex classes. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_fields (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinField*> containing the dex fields. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_sections (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinSection*> containing the dex sections. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_symbols (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinSymbol*> containing the dex symbols. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_imports (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinImport*> containing the dex imports. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_libraries (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<char*> containing the dex libraries. More...
 
RZ_API RZ_OWN RzBinAddrrz_bin_dex_resolve_symbol (RZ_NONNULL RzBinDex *dex, RzBinSpecialSymbol resolve)
 Returns a RzBinAddr pointer containing the resolved RzBinSpecialSymbol. More...
 
RZ_API RZ_OWN RzListrz_bin_dex_entrypoints (RZ_NONNULL RzBinDex *dex)
 Returns a RzList<RzBinAddr*> containing the dex entripoints. More...
 
RZ_API RZ_BORROW RzBufferrz_bin_dex_relocations (RZ_NONNULL RzBinDex *dex)
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
RZ_API RZ_OWN char * rz_bin_dex_access_flags_readable (ut32 access_flags)
 
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. More...
 
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. More...
 
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. More...
 
RZ_API void rz_bin_dex_checksum (RZ_NONNULL RzBinDex *dex, RZ_NONNULL RzBinHash *hash)
 Sets the RzBinHash dex checksum (adler32) More...
 
RZ_API void rz_bin_dex_sha1 (RZ_NONNULL RzBinDex *dex, RZ_NONNULL RzBinHash *hash)
 Sets the RzBinHash dex digest (sha1) More...
 

Macro Definition Documentation

◆ DEX_CLASS_DEF_SIZE

#define DEX_CLASS_DEF_SIZE   (0x20)

Definition at line 158 of file dex.h.

◆ DEX_FIELD_ID_SIZE

#define DEX_FIELD_ID_SIZE   (8)

Definition at line 95 of file dex.h.

◆ DEX_MAP_ITEM_SIZE

#define DEX_MAP_ITEM_SIZE   (12)

Definition at line 68 of file dex.h.

◆ DEX_METHOD_ID_SIZE

#define DEX_METHOD_ID_SIZE   (8)

Definition at line 106 of file dex.h.

◆ DEX_PROTO_ID_SIZE

#define DEX_PROTO_ID_SIZE   (0xC)

Definition at line 87 of file dex.h.

◆ DEX_TYPE_ID_SIZE

#define DEX_TYPE_ID_SIZE   (sizeof(DexTypeId))

Definition at line 78 of file dex.h.

◆ RZ_DEX_RELOC_ADDRESS

#define RZ_DEX_RELOC_ADDRESS   0x8000000000

Definition at line 14 of file dex.h.

◆ RZ_DEX_RELOC_TARGETS

#define RZ_DEX_RELOC_TARGETS   "reloc-targets"

Definition at line 12 of file dex.h.

◆ RZ_DEX_VIRT_ADDRESS

#define RZ_DEX_VIRT_ADDRESS   0x0100000000

Definition at line 13 of file dex.h.

Typedef Documentation

◆ DexClassDef

typedef struct dex_class_def_t DexClassDef

◆ DexEncodedField

◆ DexEncodedMethod

◆ DexFieldId

typedef struct dex_field_id_t DexFieldId

◆ DexMapItem

typedef struct dex_map_item_t DexMapItem

◆ DexMethodId

typedef struct dex_method_id_t DexMethodId

◆ DexProtoId

typedef struct dex_proto_id_t DexProtoId

◆ DexString

typedef struct dex_string_t DexString

◆ DexTypeId

typedef ut32 DexTypeId

Definition at line 77 of file dex.h.

◆ RzBinDex

typedef struct dex_t RzBinDex

Enumeration Type Documentation

◆ DexAccessFlag

Enumerator
ACCESS_FLAG_PUBLIC 
ACCESS_FLAG_PRIVATE 
ACCESS_FLAG_PROTECTED 
ACCESS_FLAG_STATIC 
ACCESS_FLAG_FINAL 
ACCESS_FLAG_SYNCHRONIZED 
ACCESS_FLAG_BRIDGE 
ACCESS_FLAG_VARARGS 
ACCESS_FLAG_NATIVE 
ACCESS_FLAG_INTERFACE 
ACCESS_FLAG_ABSTRACT 
ACCESS_FLAG_STRICT 
ACCESS_FLAG_SYNTHETIC 
ACCESS_FLAG_ANNOTATION 
ACCESS_FLAG_ENUM 
ACCESS_FLAG_MODULE 
ACCESS_FLAG_CONSTRUCTOR 
ACCESS_FLAG_DECLARED_SYNCHRONIZED 

Definition at line 40 of file dex.h.

40  {
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,
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

◆ DexMapItemType

Enumerator
DEX_MAP_ITEM_TYPE_HEADER_ITEM 
DEX_MAP_ITEM_TYPE_STRING_ID_ITEM 
DEX_MAP_ITEM_TYPE_TYPE_ID_ITEM 
DEX_MAP_ITEM_TYPE_PROTO_ID_ITEM 
DEX_MAP_ITEM_TYPE_FIELD_ID_ITEM 
DEX_MAP_ITEM_TYPE_METHOD_ID_ITEM 
DEX_MAP_ITEM_TYPE_CLASS_DEF_ITEM 
DEX_MAP_ITEM_TYPE_CALL_SITE_ID_ITEM 
DEX_MAP_ITEM_TYPE_METHOD_HANDLE_ITEM 
DEX_MAP_ITEM_TYPE_MAP_LIST 
DEX_MAP_ITEM_TYPE_TYPE_LIST 
DEX_MAP_ITEM_TYPE_ANNOTATION_SET_REF_LIST 
DEX_MAP_ITEM_TYPE_ANNOTATION_SET_ITEM 
DEX_MAP_ITEM_TYPE_CLASS_DATA_ITEM 
DEX_MAP_ITEM_TYPE_CODE_ITEM 
DEX_MAP_ITEM_TYPE_STRING_DATA_ITEM 
DEX_MAP_ITEM_TYPE_DEBUG_INFO_ITEM 
DEX_MAP_ITEM_TYPE_ANNOTATION_ITEM 
DEX_MAP_ITEM_TYPE_ENCODED_ARRAY_ITEM 
DEX_MAP_ITEM_TYPE_ANNOTATIONS_DIRECTORY_ITEM 
DEX_MAP_ITEM_TYPE_HIDDENAPI_CLASS_DATA_ITEM 

Definition at line 16 of file dex.h.

16  {
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

Function Documentation

◆ rz_bin_dex_access_flags_readable()

RZ_API RZ_OWN char* rz_bin_dex_access_flags_readable ( ut32  access_flags)

Definition at line 710 of file dex.c.

710  {
711  RzStrBuf *sb = NULL;
712  for (ut32 i = 0; i < CLASS_ACCESS_FLAGS_SIZE; ++i) {
714  if (afr->flag == ACCESS_FLAG_VARARGS) {
715  continue;
716  }
717  if (access_flags & afr->flag) {
718  if (!sb) {
719  sb = rz_strbuf_new(afr->readable);
720  if (!sb) {
721  return NULL;
722  }
723  } else {
724  rz_strbuf_appendf(sb, " %s", afr->readable);
725  }
726  }
727  }
728 
729  return sb ? rz_strbuf_drain(sb) : NULL;
730 }
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
static const DexAccessFlagsReadable access_flags_list[CLASS_ACCESS_FLAGS_SIZE]
Definition: dex.c:48
#define CLASS_ACCESS_FLAGS_SIZE
Definition: dex.c:47
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
const char * readable
Definition: dex.c:13

References ACCESS_FLAG_VARARGS, access_flags_list, CLASS_ACCESS_FLAGS_SIZE, dex_access_flags_readable_t::flag, i, NULL, dex_access_flags_readable_t::readable, rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), and sb.

Referenced by decode_access_flags(), dex_field_to_bin_field(), dex_field_to_symbol(), dex_method_to_symbol(), and rz_bin_dex_classes().

◆ rz_bin_dex_checksum()

RZ_API void rz_bin_dex_checksum ( RZ_NONNULL RzBinDex dex,
RZ_NONNULL RzBinHash hash 
)

Sets the RzBinHash dex checksum (adler32)

Definition at line 1990 of file dex.c.

1990  {
1991  rz_return_if_fail(dex && hash);
1992  hash->type = "adler32";
1993  hash->len = sizeof(dex->checksum);
1994  hash->addr = dex->checksum_offset;
1995  hash->from = dex->checksum_offset + sizeof(dex->checksum);
1996  hash->to = dex->file_size - hash->from;
1997  rz_write_le32(hash->buf, dex->checksum);
1998 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
static void rz_write_le32(void *dest, ut32 val)
Definition: rz_endian.h:256

References rz_return_if_fail, and rz_write_le32().

Referenced by info().

◆ rz_bin_dex_classes()

RZ_API RZ_OWN RzList* rz_bin_dex_classes ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinClass*> containing the dex classes.

Definition at line 1131 of file dex.c.

1131  {
1133 
1134  DexClassDef *class_def;
1135  RzBinClass *bclass = NULL;
1136  RzList *classes = NULL;
1137  void **it;
1138 
1139  ut32 n_methods = rz_pvector_len(dex->method_ids);
1140  ut32 n_fields = rz_pvector_len(dex->field_ids);
1141 
1142  ut8 *inserted_methods = RZ_NEWS0(ut8, n_methods);
1143  ut8 *inserted_fields = RZ_NEWS0(ut8, n_fields);
1144  if ((n_methods > 0 && !inserted_methods) || (n_fields > 0 && !inserted_fields)) {
1145  free(inserted_fields);
1146  free(inserted_methods);
1147  return NULL;
1148  }
1149 
1151  if (!classes) {
1152  free(inserted_fields);
1153  free(inserted_methods);
1154  return NULL;
1155  }
1156 
1157  rz_pvector_foreach (dex->class_defs, it) {
1158  class_def = (DexClassDef *)*it;
1159  bclass = RZ_NEW0(RzBinClass);
1160  if (!bclass) {
1161  break;
1162  }
1163 
1164  bclass->name = dex_resolve_type_id(dex, class_def->class_idx);
1165  bclass->super = dex_resolve_type_id(dex, class_def->superclass_idx);
1166  bclass->visibility = class_def->access_flags;
1168  bclass->index = class_def->class_idx;
1169  bclass->addr = class_def->offset;
1170  bclass->methods = dex_resolve_methods_in_class(dex, class_def, inserted_methods);
1171  bclass->fields = dex_resolve_fields_in_class(dex, class_def, inserted_fields);
1172 
1173  if (!rz_list_append(classes, bclass)) {
1174  free_rz_bin_class(bclass);
1175  break;
1176  }
1177  }
1178  free(inserted_fields);
1179  free(inserted_methods);
1180 
1181  return classes;
1182 }
static RzList * classes(RzBinFile *bf)
Definition: bin_dex.c:71
static RzList * dex_resolve_fields_in_class(RzBinDex *dex, DexClassDef *class_def, ut8 *inserted)
Definition: dex.c:1008
static RzList * dex_resolve_methods_in_class(RzBinDex *dex, DexClassDef *class_def, ut8 *inserted)
Definition: dex.c:939
static void free_rz_bin_class(RzBinClass *bclass)
Definition: dex.c:1116
static char * dex_resolve_type_id(RzBinDex *dex, ut32 type_idx)
Definition: dex.c:783
RZ_API RZ_OWN char * rz_bin_dex_access_flags_readable(ut32 access_flags)
Definition: dex.c:710
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
static size_t rz_pvector_len(const RzPVector *vec)
Definition: rz_vector.h:231
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
ut32 access_flags
Definition: dex.h:140
ut64 offset
Definition: dex.h:148
ut16 superclass_idx
Definition: dex.h:141
ut16 class_idx
Definition: dex.h:138
char * super
Definition: rz_bin.h:649
RzList * fields
Definition: rz_bin.h:654
char * name
Definition: rz_bin.h:647
int visibility
Definition: rz_bin.h:656
RzList * methods
Definition: rz_bin.h:653
char * visibility_str
Definition: rz_bin.h:650

References dex_class_def_t::access_flags, rz_bin_class_t::addr, dex_class_def_t::class_idx, classes(), dex_resolve_fields_in_class(), dex_resolve_methods_in_class(), dex_resolve_type_id(), rz_bin_class_t::fields, free(), free_rz_bin_class(), rz_bin_class_t::index, rz_bin_class_t::methods, rz_bin_class_t::name, NULL, dex_class_def_t::offset, rz_bin_dex_access_flags_readable(), rz_list_append(), rz_list_newf(), RZ_NEW0, RZ_NEWS0, rz_pvector_foreach, rz_pvector_len(), rz_return_val_if_fail, rz_bin_class_t::super, dex_class_def_t::superclass_idx, rz_bin_class_t::visibility, and rz_bin_class_t::visibility_str.

Referenced by classes().

◆ rz_bin_dex_debug_info()

RZ_API ut64 rz_bin_dex_debug_info ( RZ_NONNULL RzBinDex dex)

Returns the dex debug info RzBin values.

Definition at line 2041 of file dex.c.

2041  {
2042  rz_return_val_if_fail(dex, 0);
2043  // TODO
2044  return 0;
2045 }

References rz_return_val_if_fail.

Referenced by info().

◆ rz_bin_dex_entrypoints()

RZ_API RZ_OWN RzList* rz_bin_dex_entrypoints ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinAddr*> containing the dex entripoints.

Definition at line 1792 of file dex.c.

1792  {
1794 
1795  DexClassDef *class_def;
1796  RzList *list = NULL;
1797  RzList *entrypoints = NULL;
1798  void **it;
1799 
1801  if (!entrypoints) {
1802  return NULL;
1803  }
1804 
1805  rz_pvector_foreach (dex->class_defs, it) {
1806  class_def = (DexClassDef *)*it;
1807  list = dex_resolve_entrypoints_in_class(dex, class_def);
1808  if (list) {
1810  rz_list_free(list);
1811  }
1812  }
1813 
1814  return entrypoints;
1815 }
static RzList * entrypoints(RzBinFile *bf)
Definition: bin_dex.c:134
static RzList * dex_resolve_entrypoints_in_class(RzBinDex *dex, DexClassDef *class_def)
Definition: dex.c:1693
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API bool rz_list_join(RZ_NONNULL RzList *list1, RZ_NONNULL RzList *list2)
Joins 2 list into one (list2 pointer needs to be freed by the user)
Definition: list.c:209
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137

References dex_resolve_entrypoints_in_class(), entrypoints(), free(), list(), NULL, rz_list_free(), rz_list_join(), rz_list_newf(), rz_pvector_foreach, and rz_return_val_if_fail.

Referenced by entrypoints().

◆ rz_bin_dex_fields()

RZ_API RZ_OWN RzList* rz_bin_dex_fields ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinField*> containing the dex fields.

Definition at line 1232 of file dex.c.

1232  {
1234 
1235  DexClassDef *class_def;
1236  RzList *fields = NULL;
1237  void **it;
1238  ut32 n_fields = rz_pvector_len(dex->field_ids);
1239  ut8 *inserted = RZ_NEWS0(ut8, n_fields);
1240  if (n_fields > 0 && !inserted) {
1241  free(inserted);
1242  return NULL;
1243  }
1244 
1246  if (!fields) {
1247  free(inserted);
1248  return NULL;
1249  }
1250 
1251  rz_pvector_foreach (dex->class_defs, it) {
1252  class_def = (DexClassDef *)*it;
1253  RzList *class_fields = dex_resolve_fields_in_class(dex, class_def, inserted);
1254  if (class_fields) {
1255  rz_list_join(fields, class_fields);
1256  rz_list_free(class_fields);
1257  }
1258  }
1259  free(inserted);
1260 
1261  return fields;
1262 }
const aarch64_field fields[]
Definition: aarch64-opc.c:205
RZ_API void rz_bin_field_free(RzBinField *field)
Definition: bin.c:950

References dex_resolve_fields_in_class(), fields, free(), NULL, rz_bin_field_free(), rz_list_free(), rz_list_join(), rz_list_newf(), RZ_NEWS0, rz_pvector_foreach, rz_pvector_len(), and rz_return_val_if_fail.

Referenced by fields().

◆ rz_bin_dex_free()

RZ_API void rz_bin_dex_free ( RZ_NULLABLE RzBinDex dex)

Frees a RzBinDex struct.

Definition at line 649 of file dex.c.

649  {
650  if (!dex) {
651  return;
652  }
653 
654  rz_pvector_free(dex->strings);
655  rz_pvector_free(dex->proto_ids);
656  rz_pvector_free(dex->field_ids);
657  rz_pvector_free(dex->method_ids);
658  rz_pvector_free(dex->class_defs);
659  rz_buf_free(dex->relocs_buffer);
660 
661  free(dex->types);
662  free(dex->relocs_code);
663  free(dex);
664 }
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API void rz_pvector_free(RzPVector *vec)
Definition: vector.c:336

References free(), rz_buf_free(), and rz_pvector_free().

Referenced by destroy(), dex_parse(), and rz_bin_dex_new().

◆ rz_bin_dex_imports()

RZ_API RZ_OWN RzList* rz_bin_dex_imports ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinImport*> containing the dex imports.

Definition at line 1379 of file dex.c.

1379  {
1381 
1383  DexMethodId *method_id;
1384  DexClassDef *class_def;
1385  RzList *imports = NULL;
1386  ut32 *class_ids = NULL;
1387  void **vit;
1388 
1389  ut32 n_classes = rz_pvector_len(dex->class_defs);
1390  if (n_classes < 1) {
1392  }
1393 
1394  class_ids = RZ_NEWS0(ut32, n_classes);
1395  if (!class_ids) {
1396  return NULL;
1397  }
1398 
1399  ut32 j = 0;
1400  rz_pvector_foreach (dex->class_defs, vit) {
1401  class_def = (DexClassDef *)*vit;
1402  class_ids[j] = class_def->class_idx;
1403  j++;
1404  }
1405 
1407  if (!imports) {
1408  free(class_ids);
1409  return NULL;
1410  }
1411 
1412  ut32 ordinal = 0;
1413  rz_pvector_foreach (dex->field_ids, vit) {
1414  field_id = (DexFieldId *)*vit;
1415  bool class_found = false;
1416  for (ut32 i = 0; i < n_classes; ++i) {
1417  if (field_id->class_idx == class_ids[i]) {
1418  class_found = true;
1419  break;
1420  }
1421  }
1422  if (class_found) {
1423  continue;
1424  }
1425 
1426  RzBinImport *import = RZ_NEW0(RzBinImport);
1427  if (!import) {
1428  break;
1429  }
1430 
1431  char *object = dex_resolve_type_id(dex, field_id->class_idx);
1432  if (!object) {
1433  free(import);
1434  break;
1435  }
1436  rz_str_replace_char(object, ';', 0);
1437 
1438  char *class_name = (char *)rz_str_rchr(object, NULL, '/');
1439  if (class_name) {
1440  class_name[0] = 0;
1441  class_name++;
1442  }
1443  rz_str_replace_ch(object, '/', '.', 1);
1444 
1445  import->name = dex_resolve_string_id(dex, field_id->name_idx);
1446  import->libname = class_name ? strdup(object + 1) : NULL;
1447  import->classname = strdup(class_name ? class_name : object + 1);
1448  import->bind = RZ_BIN_BIND_WEAK_STR;
1449  import->type = RZ_BIN_TYPE_FIELD_STR;
1450  import->ordinal = ordinal;
1451  free(object);
1452 
1453  if (!rz_list_append(imports, import)) {
1454  rz_bin_import_free(import);
1455  break;
1456  }
1457  ordinal++;
1458  }
1459 
1460  rz_pvector_foreach (dex->method_ids, vit) {
1461  method_id = (DexMethodId *)*vit;
1462  bool class_found = false;
1463  for (ut32 i = 0; i < n_classes; ++i) {
1464  if (method_id->class_idx == class_ids[i]) {
1465  class_found = true;
1466  break;
1467  }
1468  }
1469  if (class_found) {
1470  continue;
1471  }
1472 
1473  RzBinImport *import = RZ_NEW0(RzBinImport);
1474  if (!import) {
1475  break;
1476  }
1477 
1478  char *object = dex_resolve_type_id(dex, method_id->class_idx);
1479  if (!object) {
1480  rz_bin_import_free(import);
1481  break;
1482  }
1483  rz_str_replace_char(object, ';', 0);
1484 
1485  char *class_name = (char *)rz_str_rchr(object, NULL, '/');
1486  if (class_name) {
1487  class_name[0] = 0;
1488  class_name++;
1489  }
1490  rz_str_replace_ch(object, '/', '.', 1);
1491 
1492  char *name = dex_resolve_string_id(dex, method_id->name_idx);
1493  import->name = dex_resolve_proto_id(dex, name, method_id->proto_idx, false);
1494  import->libname = class_name ? strdup(object + 1) : NULL;
1495  import->classname = strdup(class_name ? class_name : object + 1);
1496  import->bind = RZ_BIN_BIND_WEAK_STR;
1497  import->type = RZ_BIN_TYPE_FUNC_STR;
1498  import->ordinal = ordinal;
1499  free(name);
1500  free(object);
1501 
1502  if (!rz_list_append(imports, import)) {
1503  rz_bin_import_free(import);
1504  break;
1505  }
1506  ordinal++;
1507  }
1508 
1509  free(class_ids);
1510  return imports;
1511 }
RZ_API void rz_bin_import_free(RzBinImport *imp)
Definition: bin.c:137
RzList * imports(RzBinFile *bf)
Definition: bin_ne.c:106
static char * dex_resolve_string_id(RzBinDex *dex, ut32 string_idx)
Definition: dex.c:774
static char * dex_resolve_proto_id(RzBinDex *dex, const char *name, ut32 proto_idx, bool varargs)
Definition: dex.c:792
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
@ field_id
Definition: parser.c:1736
#define RZ_BIN_BIND_WEAK_STR
Definition: rz_bin.h:108
#define RZ_BIN_TYPE_FIELD_STR
Definition: rz_bin.h:120
#define RZ_BIN_TYPE_FUNC_STR
Definition: rz_bin.h:119
RZ_API const char * rz_str_rchr(const char *base, const char *p, int ch)
Definition: str.c:829
RZ_API int rz_str_replace_ch(char *s, char a, char b, bool g)
Definition: str.c:139
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
ut32 name_idx
Definition: dex.h:100
ut16 class_idx
Definition: dex.h:98
ut16 proto_idx
Definition: dex.h:99
Definition: z80asm.h:102

References dex_method_id_t::class_idx, dex_class_def_t::class_idx, dex_resolve_proto_id(), dex_resolve_string_id(), dex_resolve_type_id(), field_id, free(), i, imports(), dex_method_id_t::name_idx, NULL, dex_method_id_t::proto_idx, RZ_BIN_BIND_WEAK_STR, rz_bin_import_free(), RZ_BIN_TYPE_FIELD_STR, RZ_BIN_TYPE_FUNC_STR, rz_list_append(), rz_list_newf(), RZ_NEW0, RZ_NEWS0, rz_pvector_foreach, rz_pvector_len(), rz_return_val_if_fail, rz_str_rchr(), rz_str_replace_ch(), rz_str_replace_char(), and strdup().

Referenced by imports().

◆ rz_bin_dex_libraries()

RZ_API RZ_OWN RzList* rz_bin_dex_libraries ( RZ_NONNULL RzBinDex dex)

Returns a RzList<char*> containing the dex libraries.

Definition at line 1520 of file dex.c.

1520  {
1522 
1523  DexMethodId *method_id;
1524  DexClassDef *class_def;
1525  RzList *libraries = NULL;
1526  ut32 *class_ids = NULL;
1527  void **vit;
1528 
1529  ut32 n_classes = rz_pvector_len(dex->class_defs);
1530  if (n_classes < 1) {
1531  return rz_list_newf((RzListFree)free);
1532  }
1533 
1534  class_ids = RZ_NEWS0(ut32, n_classes);
1535  if (!class_ids) {
1536  return NULL;
1537  }
1538 
1539  ut32 j = 0;
1540  rz_pvector_foreach (dex->class_defs, vit) {
1541  class_def = (DexClassDef *)*vit;
1542  class_ids[j] = class_def->class_idx;
1543  j++;
1544  }
1545 
1547  if (!libraries) {
1548  free(class_ids);
1549  return NULL;
1550  }
1551 
1552  rz_pvector_foreach (dex->method_ids, vit) {
1553  method_id = (DexMethodId *)*vit;
1554  bool class_found = false;
1555  for (ut32 i = 0; i < n_classes; ++i) {
1556  if (method_id->class_idx == class_ids[i]) {
1557  class_found = true;
1558  break;
1559  }
1560  }
1561  if (class_found) {
1562  continue;
1563  }
1564 
1565  char *object = dex_resolve_type_id(dex, method_id->class_idx);
1566  if (RZ_STR_ISEMPTY(object) || *object != 'L' || !strncmp(object, "Ljava/", strlen("Ljava/"))) {
1567  free(object);
1568  continue;
1569  }
1570 
1571  char *p = object;
1572  if ((p = strchr(p, '/')) && (p = strchr(p + 1, '/'))) {
1573  *p = 0;
1574  p = rz_str_newf("%s/*;", object);
1575  free(object);
1576  object = p;
1577  }
1578 
1579  if (rz_list_find(libraries, object, compare_strings)) {
1580  free(object);
1581  continue;
1582  }
1583 
1584  if (!rz_list_append(libraries, object)) {
1585  free(object);
1586  break;
1587  }
1588  }
1589 
1590  free(class_ids);
1591  return libraries;
1592 }
static RzList * libraries(RzBinFile *bf)
Definition: bin_dex.c:116
static int compare_strings(const void *a, const void *b)
Definition: dex.c:1513
void * p
Definition: libc.cpp:67
RZ_API RZ_BORROW RzListIter * rz_list_find(RZ_NONNULL const RzList *list, const void *p, RZ_NONNULL RzListComparator cmp)
Returns RzListIter element which matches via the RzListComparator.
Definition: list.c:620
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67

References dex_method_id_t::class_idx, dex_class_def_t::class_idx, compare_strings(), dex_resolve_type_id(), free(), i, libraries(), NULL, p, rz_list_append(), rz_list_find(), rz_list_newf(), RZ_NEWS0, rz_pvector_foreach, rz_pvector_len(), rz_return_val_if_fail, RZ_STR_ISEMPTY, and rz_str_newf().

Referenced by libraries().

◆ rz_bin_dex_new()

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 at line 669 of file dex.c.

669  {
671 
672  RzBinDex *dex = (RzBinDex *)RZ_NEW0(RzBinDex);
673  if (!dex) {
674  return NULL;
675  }
676 
678  if (!dex->strings) {
679  rz_bin_dex_free(dex);
680  return NULL;
681  }
683  if (!dex->proto_ids) {
684  rz_bin_dex_free(dex);
685  return NULL;
686  }
688  if (!dex->field_ids) {
689  rz_bin_dex_free(dex);
690  return NULL;
691  }
693  if (!dex->method_ids) {
694  rz_bin_dex_free(dex);
695  return NULL;
696  }
698  if (!dex->class_defs) {
699  rz_bin_dex_free(dex);
700  return NULL;
701  }
702 
703  if (!dex_parse(dex, base, buf)) {
704  return NULL;
705  }
706 
707  return dex;
708 }
#define dex_method_id_free
Definition: dex.c:165
static void dex_proto_id_free(DexProtoId *string)
Definition: dex.c:104
RZ_API void rz_bin_dex_free(RZ_NULLABLE RzBinDex *dex)
Frees a RzBinDex struct.
Definition: dex.c:649
static bool dex_parse(RzBinDex *dex, ut64 base, RzBuffer *buf)
Definition: dex.c:479
static void dex_string_free(DexString *string)
Definition: dex.c:69
static void dex_class_def_free(DexClassDef *class_def)
Definition: dex.c:183
#define dex_field_id_free
Definition: dex.c:147
voidpf void * buf
Definition: ioapi.h:138
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43
Definition: dex.h:160
RzPVector * strings
Definition: dex.h:190
RzPVector * method_ids
Definition: dex.h:193
RzPVector * field_ids
Definition: dex.h:192
RzPVector * class_defs
Definition: dex.h:194
RzPVector * proto_ids
Definition: dex.h:191

References dex_t::class_defs, dex_class_def_free(), dex_field_id_free, dex_method_id_free, dex_parse(), dex_proto_id_free(), dex_string_free(), dex_t::field_ids, dex_t::method_ids, NULL, dex_t::proto_ids, rz_bin_dex_free(), RZ_NEW0, rz_pvector_new(), rz_return_val_if_fail, and dex_t::strings.

Referenced by load_buffer().

◆ rz_bin_dex_relocations()

RZ_API RZ_BORROW RzBuffer* rz_bin_dex_relocations ( RZ_NONNULL RzBinDex dex)

This method will create a buffer filled with return-void values to mapped to the imports and allow the code to generate xrefs pointing to the imports

Definition at line 1821 of file dex.c.

1821  {
1823  return dex->relocs_buffer;
1824 }

References NULL, and rz_return_val_if_fail.

Referenced by virtual_files().

◆ rz_bin_dex_resolve_class_by_idx()

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 at line 1972 of file dex.c.

1972  {
1974 
1975  return dex_resolve_type_id(dex, class_idx);
1976 }

References dex_resolve_type_id(), NULL, and rz_return_val_if_fail.

Referenced by get_name().

◆ rz_bin_dex_resolve_field_by_idx()

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 at line 1863 of file dex.c.

1863  {
1865 
1866  if (field_idx >= rz_pvector_len(dex->field_ids)) {
1867  return NULL;
1868  }
1869 
1870  DexFieldId *field_id = (DexFieldId *)rz_pvector_at(dex->field_ids, field_idx);
1871  char *class_name = dex_resolve_type_id(dex, field_id->class_idx);
1872  if (!class_name) {
1873  return NULL;
1874  }
1875 
1876  char *name = dex_resolve_string_id(dex, field_id->name_idx);
1877  if (!name) {
1878  free(class_name);
1879  return NULL;
1880  }
1881 
1882  char *type = dex_resolve_type_id(dex, field_id->type_idx);
1883  if (!type) {
1884  free(class_name);
1885  free(name);
1886  return NULL;
1887  }
1888 
1889  char *method = rz_str_newf("%s->%s %s", class_name, name, type);
1890 
1891  free(type);
1892  free(class_name);
1893  free(name);
1894  return method;
1895 }
int type
Definition: mipsasm.c:17
static void * rz_pvector_at(const RzPVector *vec, size_t index)
Definition: rz_vector.h:236

References dex_resolve_string_id(), dex_resolve_type_id(), field_id, free(), NULL, rz_pvector_at(), rz_pvector_len(), rz_return_val_if_fail, rz_str_newf(), and type.

Referenced by get_name().

◆ rz_bin_dex_resolve_method_by_idx()

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 at line 1829 of file dex.c.

1829  {
1831 
1832  if (method_idx >= rz_pvector_len(dex->method_ids)) {
1833  return NULL;
1834  }
1835 
1836  DexMethodId *method_id = (DexMethodId *)rz_pvector_at(dex->method_ids, method_idx);
1837  char *name = dex_resolve_string_id(dex, method_id->name_idx);
1838  if (!name) {
1839  return NULL;
1840  }
1841 
1842  char *proto = dex_resolve_proto_id(dex, name, method_id->proto_idx, false);
1843  RZ_FREE(name);
1844  if (!proto) {
1845  return NULL;
1846  }
1847 
1848  name = dex_resolve_type_id(dex, method_id->class_idx);
1849  if (!name) {
1850  free(proto);
1851  return NULL;
1852  }
1853 
1854  char *method = rz_str_newf("%s->%s", name, proto);
1855  free(name);
1856  free(proto);
1857  return method;
1858 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References dex_method_id_t::class_idx, dex_resolve_proto_id(), dex_resolve_string_id(), dex_resolve_type_id(), free(), dex_method_id_t::name_idx, NULL, dex_method_id_t::proto_idx, RZ_FREE, rz_pvector_at(), rz_pvector_len(), rz_return_val_if_fail, and rz_str_newf().

Referenced by get_name().

◆ rz_bin_dex_resolve_method_offset_by_idx()

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 at line 1928 of file dex.c.

1928  {
1930 
1931  if (method_idx >= rz_pvector_len(dex->method_ids)) {
1932  RZ_LOG_INFO("cannot find method with index %u\n", method_idx);
1933  return UT64_MAX;
1934  }
1935 
1936  DexMethodId *method = (DexMethodId *)rz_pvector_at(dex->method_ids, method_idx);
1937  if (method->code_offset) {
1938  if (method->code_offset < RZ_DEX_RELOC_ADDRESS) {
1939  return RZ_DEX_VIRT_ADDRESS + method->code_offset;
1940  }
1941  return method->code_offset;
1942  }
1943  return UT64_MAX;
1944 }
#define RZ_DEX_RELOC_ADDRESS
Definition: dex.h:14
#define RZ_DEX_VIRT_ADDRESS
Definition: dex.h:13
#define RZ_LOG_INFO(fmtstr,...)
Definition: rz_log.h:54
#define UT64_MAX
Definition: rz_types_base.h:86
ut64 code_offset
Definition: dex.h:103

References dex_method_id_t::code_offset, RZ_DEX_RELOC_ADDRESS, RZ_DEX_VIRT_ADDRESS, RZ_LOG_INFO, rz_pvector_at(), rz_pvector_len(), rz_return_val_if_fail, and UT64_MAX.

Referenced by get_offset().

◆ rz_bin_dex_resolve_proto_by_idx()

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 at line 1981 of file dex.c.

1981  {
1983 
1984  return dex_resolve_proto_id(dex, "", proto_idx, false);
1985 }

References dex_resolve_proto_id(), NULL, and rz_return_val_if_fail.

Referenced by dex_print_encoded_method(), and get_name().

◆ rz_bin_dex_resolve_string_by_idx()

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 at line 1963 of file dex.c.

1963  {
1965 
1966  return dex_resolve_string_id(dex, string_idx);
1967 }

References dex_resolve_string_id(), NULL, and rz_return_val_if_fail.

Referenced by dex_print_encoded_field(), dex_print_encoded_method(), get_name(), and rz_bin_dex_resolve_type_id_by_idx().

◆ rz_bin_dex_resolve_string_offset_by_idx()

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 at line 1900 of file dex.c.

1900  {
1902 
1903  DexString *string = dex_resolve_string_id_native(dex, string_idx);
1904  if (!string) {
1905  RZ_LOG_INFO("cannot find string with index %u\n", string_idx);
1906  return UT64_MAX;
1907  }
1908  return RZ_DEX_VIRT_ADDRESS + string->offset;
1909 }
static DexString * dex_resolve_string_id_native(RzBinDex *dex, ut32 string_idx)
Definition: dex.c:767

References dex_resolve_string_id_native(), RZ_DEX_VIRT_ADDRESS, RZ_LOG_INFO, rz_return_val_if_fail, and UT64_MAX.

Referenced by get_offset(), and rz_bin_dex_resolve_type_id_offset_by_idx().

◆ rz_bin_dex_resolve_symbol()

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 at line 1670 of file dex.c.

1670  {
1672 
1673  DexClassDef *class_def;
1674  void **it;
1675 
1676  RzBinAddr *ret = RZ_NEW0(RzBinAddr);
1677  if (!ret) {
1678  return NULL;
1679  }
1680  ret->paddr = UT64_MAX;
1681  ret->vaddr = UT64_MAX;
1682 
1683  rz_pvector_foreach (dex->class_defs, it) {
1684  class_def = (DexClassDef *)*it;
1685  if (dex_resolve_symbol_in_class_methods(dex, class_def, resolve, &ret->paddr, &ret->vaddr)) {
1686  break;
1687  }
1688  }
1689 
1690  return ret;
1691 }
static bool dex_resolve_symbol_in_class_methods(RzBinDex *dex, DexClassDef *class_def, RzBinSpecialSymbol resolve, ut64 *paddr, ut64 *vaddr)
Definition: dex.c:1594
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
static const char * resolve(struct Type *t, const char *foo, const char **bar)
Definition: swift.c:91

References dex_resolve_symbol_in_class_methods(), NULL, rz_bin_addr_t::paddr, resolve(), RZ_NEW0, rz_pvector_foreach, rz_return_val_if_fail, UT64_MAX, and rz_bin_addr_t::vaddr.

Referenced by binsym().

◆ rz_bin_dex_resolve_type_id_by_idx()

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 at line 1949 of file dex.c.

1949  {
1951 
1952  if (type_idx >= dex->type_ids_size) {
1953  RZ_LOG_INFO("cannot find type_id with index %u\n", type_idx);
1954  return NULL;
1955  }
1956  DexTypeId type_id = dex->types[type_idx];
1957  return rz_bin_dex_resolve_string_by_idx(dex, type_id);
1958 }
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

References NULL, rz_bin_dex_resolve_string_by_idx(), RZ_LOG_INFO, and rz_return_val_if_fail.

Referenced by dex_print_class_def(), dex_print_class_def_exports(), dex_print_encoded_field(), and dex_print_encoded_method().

◆ rz_bin_dex_resolve_type_id_offset_by_idx()

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 at line 1914 of file dex.c.

1914  {
1916 
1917  if (type_idx >= dex->type_ids_size) {
1918  RZ_LOG_INFO("cannot find type_id with index %u\n", type_idx);
1919  return UT64_MAX;
1920  }
1921  DexTypeId type_id = dex->types[type_idx];
1922  return rz_bin_dex_resolve_string_offset_by_idx(dex, type_id);
1923 }
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

References rz_bin_dex_resolve_string_offset_by_idx(), RZ_LOG_INFO, rz_return_val_if_fail, and UT64_MAX.

Referenced by get_offset().

◆ rz_bin_dex_sections()

RZ_API RZ_OWN RzList* rz_bin_dex_sections ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinSection*> containing the dex sections.

Definition at line 1200 of file dex.c.

1200  {
1202 
1204  RzList *sections = NULL;
1205 
1207  if (!sections) {
1208  return NULL;
1209  }
1210  section = section_new("data", RZ_PERM_RWX, dex->data_size, dex->data_offset, RZ_DEX_VIRT_ADDRESS + dex->data_offset);
1211  if (section && !rz_list_append(sections, section)) {
1213  }
1214  section = section_new("file", RZ_PERM_R, dex->file_size, dex->header_offset, 0);
1215  if (section && !rz_list_append(sections, section)) {
1217  }
1218 
1219  if (dex->relocs_code) {
1220  section = section_new(RZ_DEX_RELOC_TARGETS, RZ_PERM_RWX, dex->relocs_size, 0, dex->relocs_offset);
1221  if (section && !rz_list_append(sections, section)) {
1223  }
1224  }
1225 
1226  return sections;
1227 }
RZ_API void rz_bin_section_free(RzBinSection *bs)
Definition: bin.c:1116
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
static RzBinSection * section_new(const char *name, ut32 perm, ut32 size, ut64 paddr, ut64 vaddr)
Definition: dex.c:1184
#define RZ_DEX_RELOC_TARGETS
Definition: dex.h:12
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_RWX
Definition: rz_types.h:98

References NULL, rz_bin_section_free(), RZ_DEX_RELOC_TARGETS, RZ_DEX_VIRT_ADDRESS, rz_list_append(), rz_list_newf(), RZ_PERM_R, RZ_PERM_RWX, rz_return_val_if_fail, section_new(), and sections().

Referenced by sections().

◆ rz_bin_dex_sha1()

RZ_API void rz_bin_dex_sha1 ( RZ_NONNULL RzBinDex dex,
RZ_NONNULL RzBinHash hash 
)

Sets the RzBinHash dex digest (sha1)

Definition at line 2003 of file dex.c.

2003  {
2004  rz_return_if_fail(dex && hash);
2005  hash->type = "sha1";
2006  hash->len = 20;
2007  hash->addr = dex->signature_offset;
2008  hash->from = dex->signature_offset + sizeof(dex->signature);
2009  hash->to = dex->file_size - hash->from;
2010  memcpy(hash->buf, dex->signature, sizeof(dex->signature));
2011 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References memcpy(), and rz_return_if_fail.

Referenced by info().

◆ rz_bin_dex_strings()

RZ_API RZ_OWN RzList* rz_bin_dex_strings ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinString*> containing the dex strings.

Definition at line 735 of file dex.c.

735  {
737 
738  DexString *string;
739  void **it;
741  if (!strings) {
742  return NULL;
743  }
744 
745  ut32 ordinal = 0;
746  rz_pvector_foreach (dex->strings, it) {
747  string = (DexString *)*it;
749  if (!bstr) {
750  continue;
751  }
752  bstr->paddr = string->offset;
753  bstr->vaddr = RZ_DEX_VIRT_ADDRESS + string->offset;
754  bstr->ordinal = ordinal;
755  bstr->length = string->size;
756  bstr->size = string->size;
757  bstr->string = rz_str_ndup(string->data, string->size);
758  bstr->type = RZ_STRING_ENC_UTF8;
759  if (!rz_list_append(strings, bstr)) {
760  free(bstr);
761  }
762  ordinal++;
763  }
764  return strings;
765 }
RZ_API void rz_bin_string_free(void *_str)
Definition: bin.c:192
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type strings(--mime-type and\n" " --mime-encoding)\n") OPT('s'
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
@ RZ_STRING_ENC_UTF8
Definition: rz_str.h:21
char * data
Definition: dex.h:73
ut64 size
Definition: dex.h:71
ut32 ordinal
Definition: rz_bin.h:755
char * string
Definition: rz_bin.h:752

References dex_string_t::data, free(), rz_bin_string_t::length, NULL, rz_bin_string_t::ordinal, rz_bin_string_t::paddr, rz_bin_string_free(), RZ_DEX_VIRT_ADDRESS, rz_list_append(), rz_list_newf(), RZ_NEW0, rz_pvector_foreach, rz_return_val_if_fail, rz_str_ndup(), RZ_STRING_ENC_UTF8, dex_string_t::size, rz_bin_string_t::size, rz_bin_string_t::string, strings(), rz_bin_string_t::type, and rz_bin_string_t::vaddr.

Referenced by strings().

◆ rz_bin_dex_symbols()

RZ_API RZ_OWN RzList* rz_bin_dex_symbols ( RZ_NONNULL RzBinDex dex)

Returns a RzList<RzBinSymbol*> containing the dex symbols.

Definition at line 1267 of file dex.c.

1267  {
1269 
1270  DexClassDef *class_def;
1272  DexMethodId *method_id;
1273  RzList *class_symbols = NULL;
1274  RzList *symbols = NULL;
1275  void **vit;
1276  ut8 *inserted_methods = NULL;
1277  ut8 *inserted_fields = NULL;
1278  ut32 n_methods = rz_pvector_len(dex->method_ids);
1279  ut32 n_fields = rz_pvector_len(dex->field_ids);
1280 
1281  inserted_methods = RZ_NEWS0(ut8, n_methods);
1282  inserted_fields = RZ_NEWS0(ut8, n_fields);
1283  if ((n_methods > 0 && !inserted_methods) || (n_fields > 0 && !inserted_fields)) {
1284  free(inserted_fields);
1285  free(inserted_methods);
1286  return NULL;
1287  }
1288 
1290  if (!symbols) {
1291  free(inserted_fields);
1292  free(inserted_methods);
1293  return NULL;
1294  }
1295 
1296  rz_pvector_foreach (dex->class_defs, vit) {
1297  class_def = (DexClassDef *)*vit;
1298 
1299  class_symbols = dex_resolve_fields_in_class_as_symbols(dex, class_def, inserted_fields);
1300  if (class_symbols) {
1301  rz_list_join(symbols, class_symbols);
1302  rz_list_free(class_symbols);
1303  }
1304 
1305  class_symbols = dex_resolve_methods_in_class(dex, class_def, inserted_methods);
1306  if (class_symbols) {
1307  rz_list_join(symbols, class_symbols);
1308  rz_list_free(class_symbols);
1309  }
1310  }
1311 
1312  ut32 j = 0;
1313  rz_pvector_foreach (dex->field_ids, vit) {
1314  field_id = (DexFieldId *)*vit;
1315  if (inserted_fields[j++]) {
1316  continue;
1317  }
1318 
1319  RzBinSymbol *field = RZ_NEW0(RzBinSymbol);
1320  if (!field) {
1321  break;
1322  }
1323 
1324  field->name = dex_resolve_string_id(dex, field_id->name_idx);
1325  field->classname = dex_resolve_type_id(dex, field_id->class_idx);
1326  field->libname = dex_resolve_library(field->classname);
1327  field->bind = RZ_BIN_BIND_WEAK_STR;
1328  field->type = RZ_BIN_TYPE_FIELD_STR;
1329  field->is_imported = true;
1330 
1331  if (!rz_list_append(symbols, field)) {
1332  rz_bin_symbol_free(field);
1333  break;
1334  }
1335  }
1336 
1337  j = 0;
1338  rz_pvector_foreach (dex->method_ids, vit) {
1339  method_id = (DexMethodId *)*vit;
1340  if (inserted_methods[j++]) {
1341  continue;
1342  }
1343 
1344  RzBinSymbol *method = RZ_NEW0(RzBinSymbol);
1345  if (!method) {
1346  break;
1347  }
1348 
1349  method->name = dex_resolve_string_id(dex, method_id->name_idx);
1350  method->classname = dex_resolve_type_id(dex, method_id->class_idx);
1351  method->libname = dex_resolve_library(method->classname);
1352  method->dname = dex_resolve_proto_id(dex, method->name, method_id->proto_idx, false);
1353  method->bind = RZ_BIN_BIND_WEAK_STR;
1354  method->is_imported = true;
1355  method->type = RZ_BIN_TYPE_METH_STR;
1356  if (method_id->code_offset < RZ_DEX_RELOC_ADDRESS) {
1357  method->vaddr = RZ_DEX_VIRT_ADDRESS + method_id->code_offset;
1358  method->paddr = method_id->code_offset;
1359  } else {
1360  method->vaddr = method_id->code_offset;
1361  method->paddr = 0;
1362  }
1363  method->size = method_id->code_size;
1364 
1365  if (!rz_list_append(symbols, method)) {
1366  rz_bin_symbol_free(method);
1367  break;
1368  }
1369  }
1370 
1371  free(inserted_fields);
1372  free(inserted_methods);
1373  return symbols;
1374 }
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
RzList * symbols(RzBinFile *bf)
Definition: bin_ne.c:102
static RzList * dex_resolve_fields_in_class_as_symbols(RzBinDex *dex, DexClassDef *class_def, ut8 *inserted)
Definition: dex.c:1075
static char * dex_resolve_library(const char *library)
Definition: dex.c:899
#define RZ_BIN_TYPE_METH_STR
Definition: rz_bin.h:122
ut64 code_size
Definition: dex.h:104
const char * bind
Definition: rz_bin.h:681
bool is_imported
Definition: rz_bin.h:684
const char * type
Definition: rz_bin.h:682
char * name
Definition: rz_bin.h:675
char * classname
Definition: rz_bin.h:678
char * dname
Definition: rz_bin.h:676
char * libname
Definition: rz_bin.h:677

References rz_bin_symbol_t::bind, dex_method_id_t::class_idx, rz_bin_symbol_t::classname, dex_method_id_t::code_offset, dex_method_id_t::code_size, dex_resolve_fields_in_class_as_symbols(), dex_resolve_library(), dex_resolve_methods_in_class(), dex_resolve_proto_id(), dex_resolve_string_id(), dex_resolve_type_id(), rz_bin_symbol_t::dname, field_id, free(), rz_bin_symbol_t::is_imported, rz_bin_symbol_t::libname, rz_bin_symbol_t::name, dex_method_id_t::name_idx, NULL, rz_bin_symbol_t::paddr, dex_method_id_t::proto_idx, RZ_BIN_BIND_WEAK_STR, rz_bin_symbol_free(), RZ_BIN_TYPE_FIELD_STR, RZ_BIN_TYPE_METH_STR, RZ_DEX_RELOC_ADDRESS, RZ_DEX_VIRT_ADDRESS, rz_list_append(), rz_list_free(), rz_list_join(), rz_list_newf(), RZ_NEW0, RZ_NEWS0, rz_pvector_foreach, rz_pvector_len(), rz_return_val_if_fail, rz_bin_symbol_t::size, symbols(), rz_bin_symbol_t::type, and rz_bin_symbol_t::vaddr.

Referenced by symbols().

◆ rz_bin_dex_version()

RZ_API RZ_OWN char* rz_bin_dex_version ( RZ_NONNULL RzBinDex dex)

Returns the dex version (string format)

Definition at line 2016 of file dex.c.

2016  {
2018  // https://cs.android.com/android/platform/superproject/+/master:dalvik/dx/src/com/android/dex/DexFormat.java;l=55;bpv=1;bpt=0
2019  // https://developer.android.com/studio/releases/platforms
2020  if (!strncmp((char *)dex->version, "009", 3)) {
2021  return strdup("Android M3 release (Nov-Dec 2007)");
2022  } else if (!strncmp((char *)dex->version, "013", 3)) {
2023  return strdup("Android M5 release (Feb-Mar 2008)");
2024  } else if (!strncmp((char *)dex->version, "035", 3)) {
2025  return strdup("Android 3.2 (API level 13 and earlier)");
2026  } else if (!strncmp((char *)dex->version, "037", 3)) {
2027  return strdup("Android 7 (API level 24 and earlier)");
2028  } else if (!strncmp((char *)dex->version, "038", 3)) {
2029  return strdup("Android 8 (API level 26 and earlier)");
2030  } else if (!strncmp((char *)dex->version, "039", 3)) {
2031  return strdup("Android 9 (API level 28 and earlier)");
2032  } else if (!strncmp((char *)dex->version, "040", 3)) {
2033  return strdup("Android 10+ (Aug 2019)");
2034  }
2035  return NULL;
2036 }

References NULL, rz_return_val_if_fail, and strdup().

Referenced by info(), and rz_cmd_dexs_handler().