Updated some missing Java Byte code Ops and cleaned up some code parsing.

This commit is contained in:
Adam Pridgen 2013-11-21 19:19:16 -06:00 committed by pancake
parent 0c43d23a35
commit b5b6348691
9 changed files with 2206 additions and 2396 deletions

View File

@ -169,6 +169,9 @@ static int java_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len
case 0x6f: // ddiv
op->type = R_ANAL_OP_TYPE_DIV;
break;
default:
op->type = R_ANAL_OP_TYPE_UNK;
break;
}
return sz;
}

View File

@ -75,20 +75,7 @@ static int check(RBinArch *arch) {
ut16 major = (arch->buf->buf[8]<<8) | arch->buf->buf[7];
memcpy (&off, arch->buf->buf+4*sizeof(int), sizeof(int));
r_mem_copyendian ((ut8*)&off, (ut8*)&off, sizeof(int), !LIL_ENDIAN);
if (major>=45 && major<=55)
ret = R_TRUE;
// TODO: in case of failed trick attempt discard on known mach0 headers?
#if 0
/* KNOWN MACH0 HEADERS TO DISCARD */
if (off > 0 && off+5 < arch->buf->length) {
const ut8 * pbuf = arch->buf->buf+off;
if ( !memcmp (pbuf, "\xce\xfa\xed\xfe", 4) ||
!memcmp (pbuf, "\xfe\xed\xfa\xce", 4) ||
!memcmp (pbuf, "\xfe\xed\xfa\xcf", 4) ||
!memcmp (pbuf, "\xcf\xfa\xed\xfe", 4))
ret = R_FALSE;
}
#endif
ret = R_TRUE;
}
return ret;
}
@ -98,24 +85,7 @@ static int retdemangle(const char *str) {
}
static RBinAddr* binsym(RBinArch *arch, int sym) {
RBinAddr *ret = NULL;
switch (sym) {
// XXX - TODO implement the INIT FINI symbol requests
case R_BIN_SYM_INIT:
case R_BIN_SYM_FINI:
case R_BIN_SYM_ENTRY:
if (!(ret = R_NEW0 (RBinAddr)))
return NULL;
ret->offset = r_bin_java_get_entrypoint (arch->bin_obj);
break;
case R_BIN_SYM_MAIN:
if (!(ret = R_NEW0 (RBinAddr)))
return NULL;
ret->offset = ret->rva = r_bin_java_get_main (arch->bin_obj);
break;
}
return ret;
return r_bin_java_get_entrypoint(arch->bin_obj, sym);
}
static RList* lines(RBinArch *arch) {

View File

@ -787,9 +787,9 @@ static int cmd_print(void *data, const char *input) {
ut8 settings_changed = R_FALSE, bw_disassemble = R_FALSE;
char *new_arch;
ut32 pd_result = R_FALSE, processed_cmd = R_FALSE;
char *old_arch = strdup (r_config_get (core->config, "asm.arch"));
int segoff = r_config_get_i (core->config, "asm.segoff");
int old_bits = r_config_get_i (core->config, "asm.bits");
old_arch = strdup (r_config_get (core->config, "asm.arch"));
segoff = r_config_get (core->config, "asm.segoff");
old_bits = r_config_get_i (core->config, "asm.bits");
// XXX - this is necessay b/c radare will automatically
// swap flags if arch is x86 and bits == 16 see: __setsegoff in config.c

View File

@ -585,6 +585,7 @@ typedef struct r_anal_cond_t {
} RAnalCond;
typedef struct r_anal_bb_t {
char *name;
ut64 addr;
ut64 size;
ut64 jump;
@ -764,6 +765,7 @@ R_API RAnalVar *r_anal_fcn_get_var(RAnalFunction *fs, int num, int dir);
R_API char *r_anal_fcn_to_string(RAnal *a, RAnalFunction* fs);
R_API int r_anal_str_to_fcn(RAnal *a, RAnalFunction *f, const char *_str);
R_API int r_anal_fcn_count (RAnal *a, ut64 from, ut64 to);
R_API RAnalBlock *r_anal_fcn_bbget(RAnalFunction *fcn, ut64 addr); // default 20
#if 0
#define r_anal_fcn_get_refs(x) x->refs

File diff suppressed because it is too large Load Diff

View File

@ -660,9 +660,9 @@ typedef struct r_bin_java_attr_classes_t {
ut64 file_offset;
RBinJavaAttrInfo *clint_attr;
RBinJavaField *clint_field;
ut16 inner_class_info_idx;
ut16 outer_class_info_idx;
ut16 inner_name_idx;
ut16 inner_class_info_idx;
ut16 outer_class_info_idx;
ut16 inner_name_idx;
ut16 inner_class_access_flags;
ut64 size;
} RBinJavaClassesAttribute;
@ -744,7 +744,7 @@ typedef struct r_bin_java_obj_t {
R_API RList* r_bin_java_get_sections(RBinJavaObj *bin);
R_API RList* r_bin_java_get_fields(RBinJavaObj *bin);
R_API char* r_bin_java_get_version(RBinJavaObj* bin);
R_API ut64 r_bin_java_get_entrypoint(RBinJavaObj* bin);
R_API RBinAddr * r_bin_java_get_entrypoint(RBinJavaObj* bin, int sym);
R_API RList* r_bin_java_get_entrypoints(RBinJavaObj* bin);
R_API ut64 r_bin_java_get_main(RBinJavaObj* bin);
R_API RList* r_bin_java_get_symbols(RBinJavaObj* bin);
@ -1123,7 +1123,7 @@ R_API RBinClass* r_bin_java_allocate_r_bin_class();
R_API RList *r_bin_java_get_classes(RBinJavaObj *bin);
R_API RList *r_bin_java_enum_class_methods(RBinJavaObj *bin, ut16 class_idx);
R_API RList *r_bin_java_enum_class_fields(RBinJavaObj *bin, ut16 class_idx);
R_API ut64 r_bin_java_find_method_offset(RBinJavaObj *bin, char* method_name);
R_API void debug_dump_all_cp_obj();

View File

@ -10,7 +10,9 @@
#include "code.h"
#include "class.h"
#define V if(verbose)
#define V if (verbose)
static RBinJavaObj *BIN_OBJ = NULL;
@ -19,256 +21,299 @@ R_API void r_java_set_obj(RBinJavaObj *obj) {
BIN_OBJ = obj;
}
static char * java_resolve(int idx) {
static char * java_resolve_with_space(int idx) {
return java_resolve(idx, 1);
}
static char * java_resolve_without_space(int idx) {
return java_resolve(idx, 0);
}
static char * java_resolve(int idx, ut8 space_bn_name_type) {
// TODO XXX FIXME add a size parameter to the str when it is passed in
RBinJavaCPTypeObj *item = NULL, *item2 = NULL;
char *class_str = NULL,
*name_str = NULL,
*desc_str = NULL,
*string_str = NULL,
*empty = "",
*cp_name = NULL,
*cp_name2 = NULL,
*str = NULL;
*name_str = NULL,
*desc_str = NULL,
*string_str = NULL,
*empty = "",
*cp_name = NULL,
*cp_name2 = NULL,
*str = NULL;
int memory_alloc = 0;
int memory_alloc = 0;
if (BIN_OBJ && BIN_OBJ->cp_count < 1 ) {
//javasm_init(BIN_OBJ);
return NULL;
}
item = (RBinJavaCPTypeObj *) r_bin_java_get_item_from_bin_cp_list(BIN_OBJ, idx);
item = (RBinJavaCPTypeObj *) r_bin_java_get_item_from_bin_cp_list (BIN_OBJ, idx);
cp_name = ((RBinJavaCPTypeMetas *) item->metas->type_info)->name;
if (!item){
str = malloc(512);
if (!item) {
str = malloc (512);
if (str)
snprintf (str,512, "(%d) INVALID CP_OBJ", idx);
return str;
}
cp_name = ((RBinJavaCPTypeMetas *) item->metas->type_info)->name;
if ( strcmp (cp_name, "Class") == 0 ){
item2 = (RBinJavaCPTypeObj *) r_bin_java_get_item_from_bin_cp_list(BIN_OBJ, idx);
//str = r_bin_java_get_name_from_bin_cp_list(BIN_OBJ, idx-1);
if ( strcmp (cp_name, "Class") == 0 ) {
item2 = (RBinJavaCPTypeObj *) r_bin_java_get_item_from_bin_cp_list (BIN_OBJ, idx);
//str = r_bin_java_get_name_from_bin_cp_list (BIN_OBJ, idx-1);
class_str = empty;
class_str = r_bin_java_get_item_name_from_bin_cp_list(BIN_OBJ, item);
class_str = r_bin_java_get_item_name_from_bin_cp_list (BIN_OBJ, item);
if (!class_str)
class_str = empty;
name_str = r_bin_java_get_item_name_from_bin_cp_list(BIN_OBJ, item2);
name_str = r_bin_java_get_item_name_from_bin_cp_list (BIN_OBJ, item2);
if (!name_str)
name_str = empty;
desc_str = r_bin_java_get_item_desc_from_bin_cp_list(BIN_OBJ, item2);
desc_str = r_bin_java_get_item_desc_from_bin_cp_list (BIN_OBJ, item2);
if (!desc_str)
desc_str = empty;
memory_alloc = strlen(class_str) + strlen(name_str) + strlen(desc_str) + 3;
memory_alloc = strlen (class_str) + strlen (name_str) + strlen (desc_str) + 3;
if (memory_alloc)
str = malloc(memory_alloc);
if (str)
str = malloc (memory_alloc);
if (str && !space_bn_name_type)
snprintf (str, memory_alloc, "%s%s", name_str, desc_str);
else if (str && space_bn_name_type)
snprintf (str, memory_alloc, "%s %s", name_str, desc_str);
if (class_str != empty)
free (class_str);
if (name_str != empty)
free (name_str);
if (desc_str != empty)
free (desc_str);
}else if ( strcmp (cp_name, "MethodRef") == 0 ||
strcmp (cp_name, "FieldRef") == 0 ||
strcmp (cp_name, "InterfaceMethodRef") == 0) {
/*
* The MethodRef, FieldRef, and InterfaceMethodRef structures
*/
class_str = r_bin_java_get_name_from_bin_cp_list (BIN_OBJ, item->info.cp_method.class_idx);
if (!class_str)
class_str = empty;
name_str = r_bin_java_get_item_name_from_bin_cp_list (BIN_OBJ, item);
if (!name_str)
name_str = empty;
desc_str = r_bin_java_get_item_desc_from_bin_cp_list (BIN_OBJ, item);
if (!desc_str)
desc_str = empty;
memory_alloc = strlen (class_str) + strlen (name_str) + strlen (desc_str) + 3;
if (memory_alloc)
str = malloc (memory_alloc);
if (str && !space_bn_name_type)
snprintf (str, memory_alloc, "%s/%s%s", class_str, name_str, desc_str);
else if (str && space_bn_name_type)
snprintf (str, memory_alloc, "%s/%s %s", class_str, name_str, desc_str);
if (class_str != empty)
free(class_str);
free (class_str);
if (name_str != empty)
free(name_str);
free (name_str);
if (desc_str != empty)
free(desc_str);
free (desc_str);
} else if ( strcmp (cp_name, "MethodRef") == 0 ||
strcmp (cp_name, "FieldRef") == 0 ||
strcmp (cp_name, "InterfaceMethodRef") == 0) {
} else if (strcmp (cp_name, "String") == 0) {
string_str = r_bin_java_get_utf8_from_bin_cp_list (BIN_OBJ, item->info.cp_string.string_idx);
if (!string_str)
string_str = empty;
/*
* The MethodRef, FieldRef, and InterfaceMethodRef structures
*/
memory_alloc = strlen (string_str) + 4;
if (memory_alloc)
str = malloc (memory_alloc);
if (str) {
snprintf (str, "\"%s\"", string_str);
}
if (string_str != empty)
free (string_str);
class_str = r_bin_java_get_name_from_bin_cp_list(BIN_OBJ, item->info.cp_method.class_idx);
if (!class_str)
class_str = empty;
name_str = r_bin_java_get_item_name_from_bin_cp_list(BIN_OBJ, item);
} else if (strcmp (cp_name, "Utf8") == 0) {
str = malloc (item->info.cp_utf8.length+3);
if (str) {
snprintf (str, item->info.cp_utf8.length+3, "\"%s\"", item->info.cp_utf8.bytes);
}
} else if (strcmp (cp_name, "Long") == 0) {
str = malloc (34);
if (str) {
snprintf (str, 34, "0x%llx", rbin_java_raw_to_long (item->info.cp_long.bytes.raw,0));
}
} else if (strcmp (cp_name, "Double") == 0) {
str = malloc (1000);
if (str) {
snprintf (str, 1000, "%f", rbin_java_raw_to_double (item->info.cp_double.bytes.raw,0));
}
} else if (strcmp (cp_name, "Integer") == 0) {
str = malloc (34);
if (str) {
snprintf (str, 34, "0x%08x", R_BIN_JAVA_UINT (item->info.cp_integer.bytes.raw,0));
}
} else if (strcmp (cp_name, "Float") == 0) {
str = malloc (34);
if (str) {
snprintf (str, 34, "%f", R_BIN_JAVA_FLOAT (item->info.cp_float.bytes.raw,0));
}
} else if (strcmp (cp_name, "NameAndType") == 0) {
str = malloc (64);
if (str) {
name_str = r_bin_java_get_item_name_from_bin_cp_list (BIN_OBJ, item);
if (!name_str)
name_str = empty;
desc_str = r_bin_java_get_item_desc_from_bin_cp_list(BIN_OBJ, item);
desc_str = r_bin_java_get_item_desc_from_bin_cp_list (BIN_OBJ, item);
if (!desc_str)
desc_str = empty;
memory_alloc = strlen(class_str) + strlen(name_str) + strlen(desc_str) + 3;
if (memory_alloc)
str = malloc(memory_alloc);
if (str)
snprintf (str, memory_alloc, "%s/%s%s", class_str, name_str, desc_str);
if (class_str != empty)
free(class_str);
if (name_str != empty)
free(name_str);
if (desc_str != empty)
free(desc_str);
} else if (strcmp (cp_name, "String") == 0) {
string_str = r_bin_java_get_utf8_from_bin_cp_list(BIN_OBJ, item->info.cp_string.string_idx);
if(!string_str)
string_str = empty;
memory_alloc = strlen (string_str) + 4;
memory_alloc = strlen (name_str) + strlen (desc_str) + 3;
if (memory_alloc)
str = malloc (memory_alloc);
if (str)
sprintf (str, "\"%s\"", string_str);
if (string_str != empty)
free(string_str);
} else if (strcmp (cp_name, "Utf8") == 0) {
str = malloc(item->info.cp_utf8.length+3);
if (str){
snprintf (str, item->info.cp_utf8.length+3, "\"%s\"", item->info.cp_utf8.bytes);
}
} else if (strcmp (cp_name, "Long") == 0) {
str = malloc(34);
if (str){
snprintf (str, 34, "0x%llx", rbin_java_raw_to_long (item->info.cp_long.bytes.raw,0));
}
} else if (strcmp (cp_name, "Double") == 0) {
str = malloc(1000);
if (str){
snprintf (str, 1000, "%f", rbin_java_raw_to_double (item->info.cp_double.bytes.raw,0));
}
} else if (strcmp (cp_name, "Integer") == 0) {
str = malloc(34);
if (str){
snprintf (str, 34, "0x%08x", R_BIN_JAVA_UINT (item->info.cp_integer.bytes.raw,0));
}
} else if (strcmp (cp_name, "Float") == 0) {
str = malloc(34);
if (str){
snprintf (str, 34, "%f", R_BIN_JAVA_FLOAT (item->info.cp_float.bytes.raw,0));
}
} else if (strcmp (cp_name, "NameAndType") == 0) {
str = malloc(64);
if (str){
name_str = r_bin_java_get_item_name_from_bin_cp_list(BIN_OBJ, item);
if (!name_str)
name_str = empty;
desc_str = r_bin_java_get_item_desc_from_bin_cp_list(BIN_OBJ, item);
if (!desc_str)
desc_str = empty;
memory_alloc = strlen(name_str) + strlen(desc_str) + 3;
if (memory_alloc)
str = malloc(memory_alloc);
if (str)
snprintf (str, memory_alloc, "%s%s", name_str, desc_str);
if (name_str != empty)
free(name_str);
if (desc_str != empty)
free(desc_str);
}
} else{
str = malloc(16);
if (str){
snprintf (str, 16, "(null)");
}
if (str && !space_bn_name_type)
snprintf (str, memory_alloc, "%s%s", name_str, desc_str);
else if (str && space_bn_name_type)
snprintf (str, memory_alloc, "%s %s", name_str, desc_str);
if (name_str != empty)
free (name_str);
if (desc_str != empty)
free (desc_str);
}
} else {
str = malloc (16);
if (str) {
snprintf (str, 16, "(null)");
}
}
return str;
}
int java_print_opcode(ut64 addr, int idx, const ut8 *bytes, char *output, int outlen) {
char *arg = NULL; //(char *) malloc(1024);
char *arg = NULL; //(char *) malloc (1024);
switch (java_ops[idx].byte) {
case 0x12:
case 0x13:
case 0x14:
arg = java_resolve ((int)USHORT (bytes, 1));
if(arg){
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free(arg);
}else{
snprintf (output, outlen, "%s %s", java_ops[idx].name, "\0");
case 0x10: // "bipush"
snprintf (output, outlen, "%s %d", java_ops[idx].name, (char) bytes[1]);
return java_ops[idx].size;
case 0x11:
snprintf (output, outlen, "%s %d", java_ops[idx].name, (int)USHORT (bytes, 1));
return java_ops[idx].size;
case 0x15: // "iload"
case 0x16: // "lload"
case 0x17: // "fload"
case 0x18: // "dload"
case 0x19: // "aload"
case 0x37: // "lstore"
case 0x38: // "fstore"
case 0x39: // "dstore"
case 0x3a: // "astore"
case 0xbc: // "newarray"
case 0xa9: // ret <var-num>
snprintf (output, outlen, "%s %d", java_ops[idx].name, bytes[1]);
return java_ops[idx].size;
case 0x12: // ldc
arg = java_resolve_without_space ((ut16)bytes[1]);
if (arg) {
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free (arg);
}else {
snprintf (output, outlen, "%s %s", java_ops[idx].name, "\0");
}
return java_ops[idx].size;
case 0x13:
case 0x14:
arg = java_resolve_without_space ((int)USHORT (bytes, 1));
if (arg) {
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free (arg);
}else {
snprintf (output, outlen, "%s %s", java_ops[idx].name, "\0");
}
return java_ops[idx].size;
case 0x99: // ifeq
case 0x9a: // ifne
case 0x9b: // iflt
case 0x9c: // ifge
case 0x9d: // ifgt
case 0x9e: // ifle
case 0x9f: // if_icmpeq
case 0xa0: // if_icmpne
case 0xa1: // if_icmplt
case 0xa2: // if_icmpge
case 0xa3: // if_icmpgt
case 0xa4: // if_icmple
case 0xa5: // if_acmpne
case 0xa6: // if_acmpne
case 0xa7: // goto
case 0xa8: // jsr
snprintf (output, outlen, "%s 0x%08"PFMT64x, java_ops[idx].name,
addr+(int)(short)USHORT (bytes, 1));
return java_ops[idx].size;
case 0xb6: // invokevirtual
case 0xb7: // invokespecial
case 0xb8: // invokestatic
case 0xb9: // invokeinterface
case 0xba: // invokedynamic
arg = java_resolve_without_space ((int)USHORT (bytes, 1));
if (arg) {
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free (arg);
}else {
snprintf (output, outlen, "%s %s", java_ops[idx].name, "WTF?!?" );
}
return java_ops[idx].size;
arg = java_resolve_without_space ((int)USHORT (bytes, 1));
case 0xb2: // getstatic
case 0xb4: // getfield
case 0xb5: // putfield
case 0xbb: // new
case 0xbd: // anewarray
case 0xc0: // checkcast
case 0xc1: // instance of
arg = java_resolve_with_space ((int)USHORT (bytes, 1));
if (arg) {
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free (arg);
}else {
snprintf (output, outlen, "%s %s", java_ops[idx].name, "WTF?!?" );
}
return java_ops[idx].size;
}
return java_ops[idx].size;
case 0x99: // ifeq
case 0x9a: // ifne
case 0x9b: // iflt
case 0x9c: // ifge
case 0x9d: // ifgt
case 0x9e: // ifle
case 0x9f: // if_icmpeq
case 0xa0: // if_icmpne
case 0xa1: // if_icmplt
case 0xa2: // if_icmpge
case 0xa3: // if_icmpgt
case 0xa4: // if_icmple
case 0xa5: // if_acmpne
case 0xa6: // if_acmpne
case 0xa7: // goto
case 0xa8: // jsr
snprintf (output, outlen, "%s 0x%08"PFMT64x, java_ops[idx].name,
addr+(int)(short)USHORT (bytes, 1));
return java_ops[idx].size;
case 0xb2: // getstatic
case 0xb6: // invokevirtual
arg = java_resolve ((int)USHORT (bytes, 1));
if(arg){
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free(arg);
}else{
char test[2048];
//RBinJavaCPTypeObj *itm = r_bin_java_get_name_from_bin_cp_list(BIN_OBJ, ((int)USHORT (bytes, 1)));
snprintf (output, outlen, "%s %s", java_ops[idx].name, "WTF?!?" );
}
return java_ops[idx].size;
case 0xb7: // invokespecial
arg = java_resolve ((int)USHORT (bytes, 1));
if(arg){
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free(arg);
}else{
char test[2048];
//RBinJavaCPTypeObj *itm = r_bin_java_get_name_from_bin_cp_list(BIN_OBJ, ((int)USHORT (bytes, 1)));
snprintf (output, outlen, "%s %s", java_ops[idx].name, "WTF?!?" );
}
return java_ops[idx].size;
case 0xb8: // invokestatic
case 0xb9: // invokeinterface
case 0xba: // invokedynamic
arg = java_resolve ((int)USHORT (bytes, 1));
if(arg){
snprintf (output, outlen, "%s %s", java_ops[idx].name, arg);
free(arg);
}else{
char test[2048];
//RBinJavaCPTypeObj *itm = r_bin_java_get_name_from_bin_cp_list(BIN_OBJ, ((int)USHORT (bytes, 1)));
snprintf (output, outlen, "%s %s", java_ops[idx].name, "WTF?!?" );
}
return java_ops[idx].size;
}
/* process arguments */
switch (java_ops[idx].size) {
@ -286,7 +331,7 @@ int java_print_opcode(ut64 addr, int idx, const ut8 *bytes, char *output, int ou
}
R_API int r_java_disasm(ut64 addr, const ut8 *bytes, char *output, int outlen) {
//r_cons_printf("r_java_disasm: 0x%02x, 0x%0x.\n", bytes[0], addr);
//r_cons_printf ("r_java_disasm (allowed %d): 0x%02x, 0x%0x.\n", outlen, bytes[0], addr);
return java_print_opcode (addr, bytes[0], bytes, output, outlen);
}
@ -312,251 +357,3 @@ R_API int r_java_assemble(ut8 *bytes, const char *string) {
}
return 0;
}
#if 0
unsigned short read_short(FILE *fd) {
unsigned short sh = 0;
fread (&sh, 2, 1, fd);
return r_num_ntohs (sh);
}
static int attributes_walk(FILE *fd, int sz2, int fields, int verbose) {
char *name, buf[99999];
int sz, k, j=0;
for (j=0;j<sz2;j++) {
fread(buf, 6, 1, fd);
name = (get_cp (USHORT(buf,0)-1))->value;
V printf(" %2d: Name Index: %d (%s)\n", j, USHORT(buf,0), name);
// TODO add comment with constant pool index
if (fields) {
V printf("FIELD\n");
} else {
V printf (" Length: %d\n", UINT (buf, 2));
if (!name) {
printf ("**ERROR ** Cannot identify attribute name into constant pool\n");
continue;
}
if (!strcmp (name, "Code")) {
fread(buf, 8, 1, fd);
V printf(" Max Stack: %d\n", USHORT(buf, 0));
V printf(" Max Locals: %d\n", USHORT(buf, 2));
V printf(" Code Length: %d\n", UINT(buf, 4));
V printf(" Code At Offset: 0x%08"PFMT64x"\n", (ut64)ftell(fd));
fread(buf, UINT(buf, 4), 1, fd); // READ CODE
sz = read_short(fd);
V printf(" Exception table length: %d\n", sz);
for (k=0;k<sz;k++) {
fread(buf, 8, 1, fd);
V printf(" start_pc: 0x%04x\n", USHORT(buf,0));
V printf(" end_pc: 0x%04x\n", USHORT(buf,2));
V printf(" handler_pc: 0x%04x\n", USHORT(buf,4));
V printf(" catch_type: %d\n", USHORT(buf,6));
}
sz = (int)read_short(fd);
V printf(" code Attributes_count: %d\n", sz);
if (sz>0)
attributes_walk(fd, sz, fields, verbose);
} else
if (!strcmp(name, "LineNumberTable")) {
sz = (int)read_short(fd);
V printf(" Table Length: %d\n", sz);
for(k=0;k<sz;k++) {
fread(buf, 4, 1, fd);
V printf(" %2d: start_pc: 0x%04x\n", k, USHORT(buf, 0));
V printf(" line_number: %d\n", USHORT(buf, 2));
}
} else
if (!strcmp(name, "StackMapTable")) {
fread (buf, 2, 1, fd);
V printf(" StackMapTable: %d\n", USHORT(buf, 0));
} else
if (!strcmp (name, "LocalVariableTable")) {
int i;
ut32 lvtl = (ut32)read_short (fd);
for (i=0; i<lvtl; i++) {
int start_pc = start_pc = read_short (fd);
int length = length = read_short (fd);
int name_idx = name_idx = read_short (fd);
int desc_idx = desc_idx = read_short (fd);
int index = index = read_short (fd);
}
} else
if (!strcmp(name, "ConstantValue")) {
fread(buf, 2, 1, fd);
#if 0
printf(" Name Index: %d\n", USHORT(buf, 0)); // %s\n", USHORT(buf, 0), cp_items[USHORT(buf,0)-1].value);
printf(" AttributeLength: %d\n", UINT(buf, 2));
#endif
V printf(" ConstValueIndex: %d\n", USHORT(buf, 0));
} else {
fprintf (stderr, "** ERROR ** Unknown section '%s'\n", name);
return 1;
}
}
}
return 0;
}
static void check_eof(FILE *fd) {
if (feof (fd)) {
fprintf(stderr, "Unexpected eof\n");
// XXX cannot exit on a library!!
exit(0);
}
}
int java_classdump(const char *file, int verbose) {
RBinJavaClass2 cf2;
unsigned short sz, sz2;
int this_class;
char buf[0x9999];
int i,j;
FILE *fd = fopen(file, "rb");
if (fd == NULL)
return -1;
/* start parsing */
fread (&cf, 10, 1, fd); //sizeof(struct classfile), 1, fd);
if (memcmp (cf.cafebabe, "\xCA\xFE\xBA\xBE", 4)) {
eprintf ("java_classdump: Invalid header\n");
return -1;
}
javasm_init ();
/* show class version information */
V printf ("Version: 0x%02x%02x 0x%02x%02x\n",
cf.major[1],cf.major[0], cf.minor[1],cf.minor[0]);
cf.cp_count = r_num_ntohs(cf.cp_count);
if (cf.major[0]==cf.major[1] && cf.major[0]==0) {
eprintf ("Oops. this is a Mach-O\n");
return 0;
}
cf.cp_count--;
V printf ("ConstantPoolCount %d\n", cf.cp_count);
cp_items = malloc (sizeof (struct cp_item)*(cf.cp_count+1));
for (i=0;i<cf.cp_count;i++) {
struct constant_t *c;
fread (buf, 1, 1, fd);
c = NULL;
for (j=0; r_bin_java_constants[j].name; j++) {
if (r_bin_java_constants[j].tag == buf[0]) {
c = &r_bin_java_constants[j];
break;
}
}
if (c == NULL) {
eprintf ("Invalid tag '%d'\n", buf[0]);
return 0;
}
V eprintf (" %3d %s: ", i+1, c->name);
/* store constant pool item */
strcpy (cp_items[i].name, c->name);
cp_items[i].tag = c->tag;
cp_items[i].value = NULL; // no string by default
cp_items[i].off = ftell(fd)-1;
/* read bytes */
switch (c->tag) {
case 1: // utf 8 string
fread (buf, 2, 1, fd);
sz = USHORT (buf,0);
//cp_items[i].len = sz;
fread(buf, sz, 1, fd);
buf[sz] = '\0';
break;
default:
fread(buf, c->len, 1, fd);
}
memcpy (cp_items[i].bytes, buf, 5);
/* parse value */
switch(c->tag) {
case 1:
V printf ("%s\n", buf);
cp_items[i].value = strdup(buf);
break;
case 7:
V printf ("%d\n", USHORT(buf,0));
break;
case 8:
V printf ("string ptr %d\n", USHORT(buf, 0));
break;
case 9:
case 11:
case 10: // METHOD REF
V printf("class = %d, ", USHORT(buf,0));
V printf("name_type = %d\n", USHORT(buf,2));
break;
case 12:
V printf("name = %d, ", USHORT(buf,0));
V printf("descriptor = %d\n", USHORT(buf,2));
break;
default:
V printf("%d\n", UINT(buf, 40));
}
}
fread (&cf2, sizeof (RBinJavaClass2), 1, fd);
check_eof(fd);
V printf("Access flags: 0x%04x\n", cf2.access_flags);
this_class = r_num_ntohs (cf2.this_class);
V printf ("This class: %d\n", this_class);
check_eof (fd);
//printf("This class: %d (%s)\n", ntohs(cf2.this_class), cp_items[ntohs(cf2.this_class)-1].value); // XXX this is a double pointer !!1
//printf("Super class: %d (%s)\n", ntohs(cf2.super_class), cp_items[ntohs(cf2.super_class)-1].value);
sz = read_short (fd);
V printf ("Interfaces count: %d\n", sz);
if (sz>0) {
fread (buf, sz*2, 1, fd);
sz = read_short (fd);
for (i=0; i<sz; i++) {
eprintf ("interfaces: TODO\n");
}
}
sz = read_short(fd);
V printf("Fields count: %d\n", sz);
if (sz>0) {
for (i=0;i<sz;i++) {
fread(buf, 8, 1, fd);
V printf("%2d: Access Flags: %d\n", i, USHORT(buf, 0));
V printf(" Name Index: %d (%s)\n", USHORT(buf, 2), get_cp(USHORT(buf,2)-1)->value);
V printf(" Descriptor Index: %d\n", USHORT(buf, 4)); //, cp_items[USHORT(buf, 4)-1].value);
sz2 = USHORT(buf, 6);
V printf(" field Attributes Count: %d\n", sz2);
attributes_walk(fd, sz2, 1, verbose);
}
}
sz = read_short(fd);
V printf("Methods count: %d\n", sz);
if (sz>0) {
for (i=0;i<sz;i++) {
fread(buf, 8, 1, fd);
check_eof(fd);
V printf("%2d: Access Flags: %d\n", i, USHORT(buf, 0));
V printf(" Name Index: %d (%s)\n", USHORT(buf, 2), get_cp(USHORT(buf, 2)-1)->value);
V printf(" Descriptor Index: %d (%s)\n", USHORT(buf, 4), get_cp(USHORT(buf, 4)-1)->value);
sz2 = USHORT(buf, 6);
V printf(" method Attributes Count: %d\n", sz2);
attributes_walk(fd, sz2, 0, verbose);
}
}
fclose(fd);
return 0;
}
#endif

View File

@ -12,7 +12,9 @@ struct java_op {
extern struct java_op java_ops[];
static char * java_resolve(int idx);
static char * java_resolve_with_space(int idx);
static char * java_resolve_without_space(int idx);
static char * java_resolve(int idx, ut8 space_bn_name_type);
int java_print_opcode(ut64 addr, int idx, const ut8 *bytes, char *output, int outlen);
//int r_java_disasm(const ut8 *bytes, char *output, int len);
unsigned short read_short(FILE *fd);

View File

@ -54,11 +54,11 @@ struct java_op java_ops[] = {
{ "laload" , 0x2f , 1 },
{ "faload" , 0x30 , 1 },
{ "daload" , 0x31 , 1 },
{ "UNDEFINED" , 0x32 , 1 },
{ "UNDEFINED" , 0x32 , 1 },
{ "baload" , 0x33 , 1 },
{ "caload" , 0x34 , 1 },
{ "saload" , 0x35 , 1 },
{ "sastore" , 0x36 , 1 },
{ "istore" , 0x36 , 1 },
{ "lstore" , 0x37 , 2 },
{ "fstore" , 0x38 , 2 },
{ "dstore" , 0x39 , 2 },
@ -87,10 +87,10 @@ struct java_op java_ops[] = {
{ "lastore" , 0x50 , 1 },
{ "fastore" , 0x51 , 1 },
{ "dastore" , 0x52 , 1 },
{ "UNDEFINED" , 0x53 , 1 },
{ "aastore" , 0x53 , 1 },
{ "bastore" , 0x54 , 1 },
{ "castore" , 0x55 , 1 },
{ "UNDEFINED" , 0x56 , 1 },
{ "sastore" , 0x56 , 1 },
{ "pop" , 0x57 , 1 },
{ "pop2" , 0x58 , 1 },
{ "dup" , 0x59 , 1 },
@ -190,10 +190,10 @@ struct java_op java_ops[] = {
{ "invokespecial" , 0xb7 , 3 },
{ "invokestatic" , 0xb8 , 3 },
{ "invokeinterface" , 0xb9 , 5 },
{ "unused" , 0xba , 1 },
{ "invokedynamic" , 0xba , 5 },
{ "new" , 0xbb , 3 },
{ "newarray" , 0xbc , 2 },
{ "UNDEFINED" , 0xbd , 1 },
{ "anewarray" , 0xbd , 3 },
{ "arraylength" , 0xbe , 1 },
{ "athrow" , 0xbf , 1 },
{ "checkcast" , 0xc0 , 3 },
@ -201,64 +201,63 @@ struct java_op java_ops[] = {
{ "monitorenter" , 0xc2 , 1 },
{ "monitorexit" , 0xc3 , 1 },
{ "wide" , 0xc4 , 1 },
{ "multinewarray" , 0xc5 , 3 },
{ "multinewarray" , 0xc5 , 4 },
{ "ifnull" , 0xc6 , 3 },
{ "ifnonnull" , 0xc7 , 3 },
{ "goto_w" , 0xc8 , 5 },
{ "jsr_w" , 0xc9 , 5 },
{ "breakpoint" , 0xca , 1 },
{ "UNDEFINED" , 0xcb , 1 },
{ "UNDEFINED" , 0xcc , 1 },
{ "UNDEFINED" , 0xcd , 1 },
{ "UNDEFINED" , 0xce , 1 },
{ "UNDEFINED" , 0xcf , 1 },
{ "UNDEFINED" , 0xd0 , 1 },
{ "UNDEFINED" , 0xd1 , 1 },
{ "UNDEFINED" , 0xd2 , 1 },
{ "UNDEFINED" , 0xd3 , 1 },
{ "UNDEFINED" , 0xd4 , 1 },
{ "UNDEFINED" , 0xd5 , 1 },
{ "UNDEFINED" , 0xd6 , 1 },
{ "UNDEFINED" , 0xd7 , 1 },
{ "UNDEFINED" , 0xd8 , 1 },
{ "UNDEFINED" , 0xd9 , 1 },
{ "UNDEFINED" , 0xda , 1 },
{ "UNDEFINED" , 0xdb , 1 },
{ "UNDEFINED" , 0xdc , 1 },
{ "UNDEFINED" , 0xdd , 1 },
{ "UNDEFINED" , 0xde , 1 },
{ "UNDEFINED" , 0xdf , 1 },
{ "UNDEFINED" , 0xe0 , 1 },
{ "UNDEFINED" , 0xe1 , 1 },
{ "UNDEFINED" , 0xe2 , 1 },
{ "UNDEFINED" , 0xe3 , 1 },
{ "UNDEFINED" , 0xe4 , 1 },
{ "UNDEFINED" , 0xe5 , 1 },
{ "UNDEFINED" , 0xe6 , 1 },
{ "UNDEFINED" , 0xe7 , 1 },
{ "UNDEFINED" , 0xe8 , 1 },
{ "UNDEFINED" , 0xe9 , 1 },
{ "UNDEFINED" , 0xea , 1 },
{ "UNDEFINED" , 0xeb , 1 },
{ "UNDEFINED" , 0xec , 1 },
{ "UNDEFINED" , 0xed , 1 },
{ "UNDEFINED" , 0xee , 1 },
{ "UNDEFINED" , 0xef , 1 },
{ "UNDEFINED" , 0xf0 , 1 },
{ "UNDEFINED" , 0xf1 , 1 },
{ "UNDEFINED" , 0xf2 , 1 },
{ "UNDEFINED" , 0xf3 , 1 },
{ "UNDEFINED" , 0xf4 , 1 },
{ "UNDEFINED" , 0xf5 , 1 },
{ "UNDEFINED" , 0xf6 , 1 },
{ "UNDEFINED" , 0xf7 , 1 },
{ "UNDEFINED" , 0xf8 , 1 },
{ "UNDEFINED" , 0xf9 , 1 },
{ "UNDEFINED" , 0xfa , 1 },
{ "UNDEFINED" , 0xfb , 1 },
{ "UNDEFINED" , 0xfc , 1 },
{ "UNDEFINED" , 0xfd , 1 },
{ "UNDEFINED" , 0xcb , 1 },
{ "UNDEFINED" , 0xcc , 1 },
{ "UNDEFINED" , 0xcd , 1 },
{ "UNDEFINED" , 0xce , 1 },
{ "UNDEFINED" , 0xcf , 1 },
{ "UNDEFINED" , 0xd0 , 1 },
{ "UNDEFINED" , 0xd1 , 1 },
{ "UNDEFINED" , 0xd2 , 1 },
{ "UNDEFINED" , 0xd3 , 1 },
{ "UNDEFINED" , 0xd4 , 1 },
{ "UNDEFINED" , 0xd5 , 1 },
{ "UNDEFINED" , 0xd6 , 1 },
{ "UNDEFINED" , 0xd7 , 1 },
{ "UNDEFINED" , 0xd8 , 1 },
{ "UNDEFINED" , 0xd9 , 1 },
{ "UNDEFINED" , 0xda , 1 },
{ "UNDEFINED" , 0xdb , 1 },
{ "UNDEFINED" , 0xdc , 1 },
{ "UNDEFINED" , 0xdd , 1 },
{ "UNDEFINED" , 0xde , 1 },
{ "UNDEFINED" , 0xdf , 1 },
{ "UNDEFINED" , 0xe0 , 1 },
{ "UNDEFINED" , 0xe1 , 1 },
{ "UNDEFINED" , 0xe2 , 1 },
{ "UNDEFINED" , 0xe3 , 1 },
{ "UNDEFINED" , 0xe4 , 1 },
{ "UNDEFINED" , 0xe5 , 1 },
{ "UNDEFINED" , 0xe6 , 1 },
{ "UNDEFINED" , 0xe7 , 1 },
{ "UNDEFINED" , 0xe8 , 1 },
{ "UNDEFINED" , 0xe9 , 1 },
{ "UNDEFINED" , 0xea , 1 },
{ "UNDEFINED" , 0xeb , 1 },
{ "UNDEFINED" , 0xec , 1 },
{ "UNDEFINED" , 0xed , 1 },
{ "UNDEFINED" , 0xee , 1 },
{ "UNDEFINED" , 0xef , 1 },
{ "UNDEFINED" , 0xf0 , 1 },
{ "UNDEFINED" , 0xf1 , 1 },
{ "UNDEFINED" , 0xf2 , 1 },
{ "UNDEFINED" , 0xf3 , 1 },
{ "UNDEFINED" , 0xf4 , 1 },
{ "UNDEFINED" , 0xf5 , 1 },
{ "UNDEFINED" , 0xf6 , 1 },
{ "UNDEFINED" , 0xf7 , 1 },
{ "UNDEFINED" , 0xf8 , 1 },
{ "UNDEFINED" , 0xf9 , 1 },
{ "UNDEFINED" , 0xfa , 1 },
{ "UNDEFINED" , 0xfb , 1 },
{ "UNDEFINED" , 0xfc , 1 },
{ "UNDEFINED" , 0xfd , 1 },
{ "impdep1" , 0xfe , 1 },
{ "impdep2" , 0xff , 1 },
{ NULL, 0, 0 }
};
{ "impdep2" , 0xff , 1 }
};