1 /* radare - LGPL - Copyright 2009-2019 - pancake, nibble, Adam Pridgen <dso@rice.edu || adam.pridgen@thecoverofnight.com> */
2 
3 #include <r_types.h>
4 #include <r_util.h>
5 #include <r_lib.h>
6 #include <r_bin.h>
7 
8 #include "../../shlr/java/class.h"
9 #include "../../shlr/java/code.h"
10 
11 #define IFDBG_BIN_JAVA if (0)
12 
13 static Sdb *DB = NULL;
14 static void add_bin_obj_to_sdb(RBinJavaObj *bin);
15 static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj);
16 
init(void * user)17 static int init(void *user) {
18 	IFDBG_BIN_JAVA eprintf("Calling plugin init = %d.\n", DB? 1: 0);
19 	if (!DB) {
20 		IFDBG_BIN_JAVA eprintf("plugin DB beeing initted.\n");
21 		DB = sdb_new ("bin.java", NULL, 0);
22 	} else {
23 		IFDBG_BIN_JAVA eprintf("plugin DB already initted.\n");
24 	}
25 	return 0;
26 }
27 
fini(void * user)28 static int fini(void *user) {
29 	IFDBG_BIN_JAVA eprintf("Calling plugin fini = %d.\n", DB? 1: 0);
30 	if (!DB) {
31 		IFDBG_BIN_JAVA eprintf("plugin DB already uninited.\n");
32 	} else {
33 		IFDBG_BIN_JAVA eprintf("plugin DB beeing uninited.\n");
34 		sdb_free (DB);
35 		DB = NULL;
36 	}
37 	return 0;
38 }
39 
add_sdb_bin_obj(const char * key,RBinJavaObj * bin_obj)40 static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj) {
41 	int result = false;
42 	char *addr, value[1024] = {
43 		0
44 	};
45 	addr = sdb_itoa ((ut64) (size_t) bin_obj, value, 16);
46 	if (key && bin_obj && DB) {
47 		IFDBG_BIN_JAVA eprintf("Adding %s:%s to the bin_objs db\n", key, addr);
48 		sdb_set (DB, key, addr, 0);
49 		result = true;
50 	}
51 	return result;
52 }
53 
add_bin_obj_to_sdb(RBinJavaObj * bin)54 static void add_bin_obj_to_sdb(RBinJavaObj *bin) {
55 	if (!bin) {
56 		return;
57 	}
58 	char *jvcname = r_bin_java_build_obj_key (bin);
59 	add_sdb_bin_obj (jvcname, bin);
60 	bin->AllJavaBinObjs = DB;
61 	free (jvcname);
62 }
63 
get_sdb(RBinFile * bf)64 static Sdb *get_sdb(RBinFile *bf) {
65 	RBinObject *o = bf->o;
66 	struct r_bin_java_obj_t *bin;
67 	if (!o) {
68 		return NULL;
69 	}
70 	bin = (struct r_bin_java_obj_t *) o->bin_obj;
71 	if (bin->kv) {
72 		return bin->kv;
73 	}
74 	return NULL;
75 }
76 
load_buffer(RBinFile * bf,void ** bin_obj,RBuffer * buf,ut64 loadaddr,Sdb * sdb)77 static bool load_buffer(RBinFile * bf, void **bin_obj, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
78 	struct r_bin_java_obj_t *tmp_bin_obj = NULL;
79 	RBuffer *tbuf = r_buf_ref (buf);
80 	tmp_bin_obj = r_bin_java_new_buf (tbuf, loadaddr, sdb);
81 	if (!tmp_bin_obj) {
82 		return false;
83 	}
84 	*bin_obj = tmp_bin_obj;
85 	add_bin_obj_to_sdb (tmp_bin_obj);
86 	if (bf && bf->file) {
87 		tmp_bin_obj->file = strdup (bf->file);
88 	}
89 	r_buf_free (tbuf);
90 	return true;
91 }
92 
destroy(RBinFile * bf)93 static void destroy(RBinFile *bf) {
94 	r_bin_java_free ((struct r_bin_java_obj_t *) bf->o->bin_obj);
95 	sdb_free (DB);
96 	DB = NULL;
97 }
98 
entries(RBinFile * bf)99 static RList *entries(RBinFile *bf) {
100 	return r_bin_java_get_entrypoints (bf->o->bin_obj);
101 }
102 
baddr(RBinFile * bf)103 static ut64 baddr(RBinFile *bf) {
104 	return 0;
105 }
106 
classes(RBinFile * bf)107 static RList *classes(RBinFile *bf) {
108 	return r_bin_java_get_classes ((struct r_bin_java_obj_t *) bf->o->bin_obj);
109 }
110 
symbols(RBinFile * bf)111 static RList *symbols(RBinFile *bf) {
112 	return r_bin_java_get_symbols ((struct r_bin_java_obj_t *) bf->o->bin_obj);
113 }
114 
strings(RBinFile * bf)115 static RList *strings(RBinFile *bf) {
116 	return r_bin_java_get_strings ((struct r_bin_java_obj_t *) bf->o->bin_obj);
117 }
118 
info(RBinFile * bf)119 static RBinInfo *info(RBinFile *bf) {
120 	RBinJavaObj *jo = bf->o->bin_obj;
121 	RBinInfo *ret = R_NEW0 (RBinInfo);
122 	if (!ret) {
123 		return NULL;
124 	}
125 	ret->lang = (jo && jo->lang) ? jo->lang : "java";
126 	ret->file = strdup (bf->file);
127 	ret->type = strdup ("JAVA CLASS");
128 	ret->bclass = r_bin_java_get_version (bf->o->bin_obj);
129 	ret->has_va = 0;
130 	// ret->has_lit = true;
131 	ret->rclass = strdup ("class");
132 	ret->os = strdup ("any");
133 	ret->subsystem = strdup ("any");
134 	ret->machine = strdup ("jvm");
135 	ret->arch = strdup ("java");
136 	ret->bits = 32;
137 	ret->big_endian = 0;
138 	ret->dbg_info = 4 | 8; /* LineNums | Syms */
139 	return ret;
140 }
141 
check_buffer(RBuffer * b)142 static bool check_buffer(RBuffer *b) {
143 	if (r_buf_size (b) > 32) {
144 		ut8 buf[4];
145 		r_buf_read_at (b, 0, buf, sizeof (buf));
146 		if (!memcmp (buf, "\xca\xfe\xba\xbe", 4)) {
147 			int off = r_buf_read_be32_at (b, 4 * sizeof (int));
148 			int version = r_buf_read_be16_at (b, 6);
149 			if (off > 0 && version < 1024) {
150 				return true;
151 			}
152 		}
153 	}
154 	return false;
155 }
156 
retdemangle(const char * str)157 static int retdemangle(const char *str) {
158 	return R_BIN_NM_JAVA;
159 }
160 
binsym(RBinFile * bf,int sym)161 static RBinAddr *binsym(RBinFile *bf, int sym) {
162 	return r_bin_java_get_entrypoint (bf->o->bin_obj, sym);
163 }
164 
lines(RBinFile * bf)165 static R_BORROW RList *lines(RBinFile *bf) {
166 	return NULL;
167 #if 0
168 	char *file = bf->file? strdup (bf->file): strdup ("");
169 	RList *list = r_list_newf (free);
170 	// XXX the owner of this list should be the plugin, so we are leaking here
171 	file = r_str_replace (file, ".class", ".java", 0);
172 	/*
173 	   int i;
174 	   RBinJavaObj *b = bf->o->bin_obj;
175 	   for (i=0; i<b->lines.count; i++) {
176 	        RBinDwarfRow *row = R_NEW0(RBinDwarfRow);
177 	        r_bin_dwarf_line_new (row, b->lines.addr[i], file, b->lines.line[i]);
178 	        r_list_append (list, row);
179 	   }*/
180 	free (file);
181 	return list;
182 #endif
183 }
184 
sections(RBinFile * bf)185 static RList *sections(RBinFile *bf) {
186 	return r_bin_java_get_sections (bf->o->bin_obj);
187 }
188 
imports(RBinFile * bf)189 static RList *imports(RBinFile *bf) {
190 	return r_bin_java_get_imports (bf->o->bin_obj);
191 }
192 
fields(RBinFile * bf)193 static RList *fields(RBinFile *bf) {
194 	return NULL;// r_bin_java_get_fields (bf->o->bin_obj);
195 }
196 
libs(RBinFile * bf)197 static RList *libs(RBinFile *bf) {
198 	return r_bin_java_get_lib_names (bf->o->bin_obj);
199 }
200 
201 RBinPlugin r_bin_plugin_java = {
202 	.name = "java",
203 	.desc = "java bin plugin",
204 	.license = "LGPL3",
205 	.init = init,
206 	.fini = fini,
207 	.get_sdb = &get_sdb,
208 	.load_buffer = &load_buffer,
209 	.destroy = &destroy,
210 	.check_buffer = &check_buffer,
211 	.baddr = &baddr,
212 	.binsym = binsym,
213 	.entries = &entries,
214 	.sections = sections,
215 	.symbols = symbols,
216 	.imports = &imports,
217 	.strings = &strings,
218 	.info = &info,
219 	.fields = fields,
220 	.libs = libs,
221 	.lines = &lines,
222 	.classes = classes,
223 	.demangle_type = retdemangle,
224 	.minstrlen = 3,
225 };
226 
227 #ifndef R2_PLUGIN_INCORE
228 R_API RLibStruct radare_plugin = {
229 	.type = R_LIB_TYPE_BIN,
230 	.data = &r_bin_plugin_java,
231 	.version = R2_VERSION
232 };
233 #endif
234