Rizin
unix-like reverse engineering framework and cli tools
bin_java.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 
9 #include "../format/java/class_bin.h"
10 #include "../../asm/arch/java/const.h"
11 
12 #define rz_bin_file_get_java_class(bf) ((RzBinJavaClass *)bf->o->bin_obj)
13 
14 static RzBinInfo *info(RzBinFile *bf) {
16  if (!jclass) {
17  return NULL;
18  }
19  RzBinInfo *binfo = RZ_NEW0(RzBinInfo);
20  if (!binfo) {
21  return NULL;
22  }
24  binfo->file = strdup(bf->file);
25  binfo->type = strdup("JAVA CLASS");
27  binfo->has_va = false;
28  binfo->rclass = strdup("class");
29  binfo->os = strdup("any");
30  binfo->subsystem = strdup("any");
31  binfo->machine = strdup("jvm");
32  binfo->arch = strdup("java");
33  binfo->bits = 32;
34  binfo->big_endian = true;
36  return binfo;
37 }
38 
39 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
41  if (!jclass) {
42  return false;
43  }
44  obj->bin_obj = jclass;
45  return true;
46 }
47 
48 static void destroy(RzBinFile *bf) {
50 }
51 
52 static bool check_buffer(RzBuffer *b) {
53  if (rz_buf_size(b) > 32) {
54  ut8 buf[4];
55  rz_buf_read_at(b, 0, buf, sizeof(buf));
56  return !memcmp(buf, "\xca\xfe\xba\xbe", 4);
57  }
58  return false;
59 }
60 
61 static ut64 baddr(RzBinFile *bf) {
62  return 0;
63 }
64 
65 static Sdb *get_sdb(RzBinFile *bf) {
66  return bf->sdb;
67 }
68 
69 static void free_rz_bin_class(void /*RzBinClass*/ *k) {
70  RzBinClass *bclass = (RzBinClass *)k;
71  if (bclass) {
72  rz_list_free(bclass->methods);
73  rz_list_free(bclass->fields);
74  free(bclass->name);
75  free(bclass->super);
76  free(bclass->visibility_str);
77  free(bclass);
78  }
79 }
80 
81 static RzList *classes(RzBinFile *bf) {
82  RzBinClass *bclass = NULL;
83  RzList *classes = NULL;
85  if (!jclass) {
86  return NULL;
87  }
88 
90  if (!classes) {
91  return NULL;
92  }
93 
94  bclass = RZ_NEW0(RzBinClass);
95  if (!bclass) {
97  return NULL;
98  }
99  rz_list_append(classes, bclass);
100 
105 
108  if (!bclass->methods || !bclass->fields) {
110  return NULL;
111  }
112 
113  return classes;
114 }
115 
116 static RzList *imports(RzBinFile *bf) {
118  if (!jclass) {
119  return NULL;
120  }
121 
123 }
124 
125 static RzList *sections(RzBinFile *bf) {
127  if (!jclass) {
128  return NULL;
129  }
130 
132 }
133 
134 static RzList *symbols(RzBinFile *bf) {
135  RzList *tmp;
137  if (!jclass) {
138  return NULL;
139  }
140 
142  if (!list) {
143  return NULL;
144  }
145 
148  rz_list_free(tmp);
149 
152  rz_list_free(tmp);
153  return list;
154 }
155 
156 static RzList *fields(RzBinFile *bf) {
158  if (!jclass) {
159  return NULL;
160  }
161 
163 }
164 
165 static RzList *libs(RzBinFile *bf) {
167  if (!jclass) {
168  return NULL;
169  }
170 
172 }
173 
176  if (!jclass) {
177  return NULL;
178  }
179 
181 }
182 
185  if (!jclass) {
186  return NULL;
187  }
188 
190 }
191 
192 static RzList *strings(RzBinFile *bf) {
194  if (!jclass) {
195  return NULL;
196  }
197 
199 }
200 
201 static int demangle_type(const char *str) {
202  return RZ_BIN_LANGUAGE_JAVA;
203 }
204 
205 static char *enrich_asm(RzBinFile *bf, const char *asm_str, int asm_len) {
207  if (!jclass) {
208  return NULL;
209  }
210  for (int i = 0; i < asm_len; ++i) {
211  if (!strncmp(asm_str + i, JAVA_ASM_CONSTANT_POOL_STR, strlen(JAVA_ASM_CONSTANT_POOL_STR))) {
212  const char *snum = asm_str + i + strlen(JAVA_ASM_CONSTANT_POOL_STR);
213  if (!IS_DIGIT(*snum)) {
215  continue;
216  }
217  int index = atoi(snum);
219  if (!tmp) {
221  return NULL;
222  }
223  char *result = rz_str_newf("%.*s%s", i, asm_str, tmp);
224  free(tmp);
225  return result;
226  }
227  }
228  return NULL;
229 }
230 
232  .name = "java",
233  .desc = "java bin plugin",
234  .license = "LGPL3",
235  .get_sdb = &get_sdb,
236  .load_buffer = &load_buffer,
237  .destroy = &destroy,
238  .check_buffer = &check_buffer,
239  .baddr = &baddr,
240  .binsym = &binsym,
241  .entries = &entrypoints,
243  .sections = sections,
244  .symbols = symbols,
245  .imports = &imports,
246  .strings = &strings,
247  .enrich_asm = &enrich_asm,
248  .info = &info,
249  .fields = fields,
250  .libs = libs,
251  .classes = classes,
252  .demangle_type = demangle_type,
253  .minstrlen = 3,
254 };
255 
256 #ifndef RZ_PLUGIN_INCORE
259  .data = &rz_bin_plugin_java,
261 };
262 #endif
lzma_index ** i
Definition: index.h:629
RZ_API RZ_OWN RzList * rz_bin_maps_of_file_sections(RZ_NONNULL RzBinFile *binfile)
Create a list of RzBinMap from RzBinSections queried from the given file.
Definition: bin.c:1040
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol sym)
Definition: bin_java.c:174
static Sdb * get_sdb(RzBinFile *bf)
Definition: bin_java.c:65
RzBinPlugin rz_bin_plugin_java
Definition: bin_java.c:231
static RzList * classes(RzBinFile *bf)
Definition: bin_java.c:81
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_java.c:39
static RzList * symbols(RzBinFile *bf)
Definition: bin_java.c:134
static char * enrich_asm(RzBinFile *bf, const char *asm_str, int asm_len)
Definition: bin_java.c:205
static RzList * libs(RzBinFile *bf)
Definition: bin_java.c:165
#define rz_bin_file_get_java_class(bf)
Definition: bin_java.c:12
static RzList * strings(RzBinFile *bf)
Definition: bin_java.c:192
static RzList * fields(RzBinFile *bf)
Definition: bin_java.c:156
static void destroy(RzBinFile *bf)
Definition: bin_java.c:48
RZ_API RzLibStruct rizin_plugin
Definition: bin_java.c:257
static bool check_buffer(RzBuffer *b)
Definition: bin_java.c:52
static void free_rz_bin_class(void *k)
Definition: bin_java.c:69
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_java.c:14
static ut64 baddr(RzBinFile *bf)
Definition: bin_java.c:61
static RzList * sections(RzBinFile *bf)
Definition: bin_java.c:125
static RzList * imports(RzBinFile *bf)
Definition: bin_java.c:116
static int demangle_type(const char *str)
Definition: bin_java.c:201
static RzList * entrypoints(RzBinFile *bf)
Definition: bin_java.c:183
RZ_API RZ_BORROW const char * rz_bin_java_class_language(RZ_NONNULL RzBinJavaClass *bin)
Definition: class_bin.c:378
RZ_API RZ_OWN RzList * rz_bin_java_class_fields_as_symbols(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinSymbol*> containing the class fields.
Definition: class_bin.c:1227
RZ_API RZ_OWN RzList * rz_bin_java_class_const_pool_as_symbols(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinSymbol*> containing the class const pool.
Definition: class_bin.c:1435
RZ_API RZ_OWN RzList * rz_bin_java_class_methods_as_symbols(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinSymbol*> containing the class methods.
Definition: class_bin.c:1052
RZ_API ut64 rz_bin_java_class_debug_info(RZ_NONNULL RzBinJavaClass *bin)
Definition: class_bin.c:351
RZ_API RZ_OWN RzList * rz_bin_java_class_as_libraries(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<char*> containing the class libraries.
Definition: class_bin.c:1846
RZ_API RZ_OWN RzBinJavaClass * rz_bin_java_class_new(RZ_NONNULL RzBuffer *buf, ut64 offset, RZ_NONNULL Sdb *kv)
Parses the java class file and returns a RzBinJavaClass struct.
Definition: class_bin.c:289
RZ_API RZ_OWN RzList * rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinString*> containing the strings.
Definition: class_bin.c:986
RZ_API RZ_OWN RzList * rz_bin_java_class_const_pool_as_imports(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinImport*> containing the class const pool.
Definition: class_bin.c:1509
RZ_API RZ_OWN RzList * rz_bin_java_class_as_sections(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinSection*> containing the class sections.
Definition: class_bin.c:1718
RZ_API RZ_OWN char * rz_bin_java_class_version(RZ_NONNULL RzBinJavaClass *bin)
Parses the java class file and returns a RzBinJavaClass struct.
Definition: class_bin.c:306
RZ_API RZ_OWN char * rz_bin_java_class_const_pool_resolve_index(RZ_NONNULL RzBinJavaClass *bin, st32 index)
Returns the string linked to the class const pool index.
Definition: class_bin.c:644
RZ_API RZ_OWN char * rz_bin_java_class_access_flags_readable(RZ_NONNULL RzBinJavaClass *bin, ut16 mask)
Returns the readable class access flags.
Definition: class_bin.c:485
RZ_API RZ_OWN RzList * rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinAddr*> containing the entrypoints.
Definition: class_bin.c:927
RZ_API RZ_OWN char * rz_bin_java_class_name(RZ_NONNULL RzBinJavaClass *bin)
Returns the class name.
Definition: class_bin.c:447
RZ_API RZ_OWN RzList * rz_bin_java_class_fields_as_binfields(RZ_NONNULL RzBinJavaClass *bin)
Returns a RzList<RzBinField*> containing the class fields.
Definition: class_bin.c:1277
RZ_API RZ_OWN char * rz_bin_java_class_super(RZ_NONNULL RzBinJavaClass *bin)
Returns the class super name.
Definition: class_bin.c:466
RZ_API RZ_OWN RzBinAddr * rz_bin_java_class_resolve_symbol(RZ_NONNULL RzBinJavaClass *bin, RzBinSpecialSymbol resolve)
Resolves and returns the RzBinAddr struct linked to the input RzBinSpecialSymbol.
Definition: class_bin.c:870
RZ_API void rz_bin_java_class_free(RZ_NULLABLE RzBinJavaClass *bin)
Frees a RzBinJavaClass pointer.
Definition: class_bin.c:407
RZ_API ut32 rz_bin_java_class_access_flags(RZ_NONNULL RzBinJavaClass *bin)
Definition: class_bin.c:477
#define ACCESS_FLAG_MASK_ALL_NO_SUPER
Definition: class_bin.h:37
#define JAVA_ASM_CONSTANT_POOL_STR
Definition: const.h:7
#define RZ_API
#define NULL
Definition: cris-opc.c:27
const char * k
Definition: dsignal.c:11
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
jobject jclass
Definition: jni.h:104
uint8_t ut8
Definition: lh5801.h:11
static void list(RzEgg *egg)
Definition: rz-gg.c:52
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 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 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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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")
#define rz_warn_if_reached()
Definition: rz_assert.h:29
@ RZ_BIN_LANGUAGE_JAVA
Definition: rz_bin.h:147
RzBinSpecialSymbol
Definition: rz_bin.h:136
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define IS_DIGIT(x)
Definition: rz_str_util.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
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
ut64 loadaddr
starting physical address to read from the target file
Definition: rz_bin.h:249
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RZ_DEPRECATE Sdb * sdb
deprecated, put info in C structures instead of this
Definition: rz_bin.h:315
char * file
Definition: rz_bin.h:299
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * subsystem
Definition: rz_bin.h:220
char * machine
Definition: rz_bin.h:216
const char * lang
Definition: rz_bin.h:224
char * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
ut64 dbg_info
Definition: rz_bin.h:240
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
RzBinObjectLoadOptions opts
Definition: rz_bin.h:260
void * bin_obj
Definition: rz_bin.h:293
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()