Пример #1
0
static int disassemble(RAsm *a, RAsmOp *aop, const ut8 *buf, int len) {
	m68k_word bof[8] = {0};
	int iaddr = (int)a->pc;
	char opcode[256], operands[256];
	const unsigned char *buf2;
	int ilen ;
	static struct DisasmPara_68k dp;
	char *buf_asm;
	/* initialize DisasmPara */
	*operands = *opcode = 0;
	memcpy (bof, buf, R_MIN(len, sizeof(bof)));
	dp.opcode = opcode;
	dp.operands = operands;
	dp.iaddr = (m68k_word *) (size_t)iaddr;
	dp.instr = bof;
	buf2 = (const ut8*)M68k_Disassemble (&dp);
	if (!buf2) {
		// invalid instruction
		return aop->size = 2;
	}
	ilen = (buf2-(const ut8*)bof);
	if (*operands)
		buf_asm = sdb_fmt ("%s %s", opcode, operands);
	else
		buf_asm = sdb_fmt ("%s", opcode);
	r_str_rmch (buf_asm, '#');
	r_asm_op_set_asm (aop, buf_asm);
	aop->size = ilen;
	return aop->size;
}
Пример #2
0
// - name should be allocated on the heap
R_API char *r_bin_filter_name(RBinFile *bf, Sdb *db, ut64 vaddr, char *name) {
	r_return_val_if_fail (db && name, NULL);

	char *resname = name;
	const char *uname = sdb_fmt ("%" PFMT64x ".%s", vaddr, resname);
	ut32 vhash = sdb_hash (uname); // vaddr hash - unique
	ut32 hash = sdb_hash (resname); // name hash - if dupped and not in unique hash must insert
	int count = sdb_num_inc (db, sdb_fmt ("%x", hash), 1, 0);

	if (sdb_exists (db, sdb_fmt ("%x", vhash))) {
		// TODO: symbol is dupped, so symbol can be removed!
		return resname;
	}
	sdb_num_set (db, sdb_fmt ("%x", vhash), 1, 0);
	if (vaddr) {
		char *p = hashify (resname, vaddr);
		if (p) {
			resname = p;
		}
	}
	if (count > 1) {
		char *p = r_str_appendf (resname, "_%d", count - 1);
		if (p) {
			resname = p;
		}

		// two symbols at different addresses and same name wtf
		//	eprintf ("Symbol '%s' dupped!\n", sym->name);
	}
	return resname;
}
Пример #3
0
static int stack_clean (RCore *core, ut64 addr, RAnalFunction *fcn) {
	int offset, ret;
	char *tmp, *str, *sig;
	RAnalOp *op = r_core_anal_op (core, addr);
	if (!op) {
		return 0;
	}
	str = strdup (r_strbuf_get (&op->esil));
	if (!str) {
		return 0;
	}
	tmp = strchr (str, ',');
	if (!tmp) {
		free (str);
		return 0;
	}
	*tmp++ = 0;

	offset = r_num_math (core->num, str);
	const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
	sig = sdb_fmt (-1, "%s,+=", sp);
	ret = 0;
	if (!strncmp (tmp, sig, strlen (sig))) {
		const char *esil = sdb_fmt (-1, "%d,%s,-=", offset, sp);
		r_anal_esil_parse (core->anal->esil, esil);
		r_anal_esil_dumpstack (core->anal->esil);
		r_anal_esil_stack_free (core->anal->esil);
		r_core_esil_step (core, UT64_MAX, NULL);
		ret = op->size;
	}
	r_anal_op_free (op);
	free (str);
	return ret;
}
Пример #4
0
R_API char *r_type_link_at (Sdb *TDB, ut64 addr) {
	char* res = NULL;

	if (addr == UT64_MAX) {
		return NULL;
	}
	char* query = sdb_fmt ("link.%08"PFMT64x, addr);
	res = sdb_get (TDB, query, 0);
	if (!res) { // resolve struct memb if possible for given addr
		SdbKv *kv;
		SdbListIter *sdb_iter;
		SdbList *sdb_list = sdb_foreach_list (TDB, true);
		ls_foreach (sdb_list, sdb_iter, kv) {
			if (strncmp (kv->key, "link.", strlen ("link."))) {
				continue;
			}
			const char *linkptr = sdb_fmt ("0x%s", kv->key + strlen ("link."));
			ut64 baseaddr = r_num_math (NULL, linkptr);
			int delta = (addr > baseaddr)? addr - baseaddr: -1;
			res = r_type_get_struct_memb (TDB, kv->value, delta);
			if (res) {
				break;
			}
		}
		ls_free (sdb_list);
	}
Пример #5
0
R_API RSyscallItem *r_syscall_get(RSyscall *s, int num, int swi) {
	const char *ret, *ret2, *key;
	if (!s || !s->db) {
		eprintf ("Syscall database not loaded\n");
		return NULL;
	}
	swi = getswi (s, swi);
	if (swi < 16) {
		key = sdb_fmt (0, "%d.%d", swi, num);
	} else {
		key = sdb_fmt (0, "0x%02x.%d", swi, num);
	}
	ret = sdb_const_get (s->db, key, 0);
	if (!ret) {
		key = sdb_fmt (0, "0x%02x.0x%02x", swi, num); // Workaround until Syscall SDB is fixed 
		ret = sdb_const_get (s->db, key, 0);
		if (!ret) {
			return NULL;
		}	
	}
	ret2 = sdb_const_get (s->db, ret, 0);
	if (!ret2) {
		return NULL;
	}
	return r_syscall_item_new_from_string (ret, ret2);
}
Пример #6
0
static void addptr(RList *ret, const char *name, ut64 addr, const ut8 *b, int len) {
	if (b && rjmp (b)) {
		addsym (ret, sdb_fmt ("vector.%s", name), addr);
		ut64 ptr_addr = rjmp_dest (addr, b + addr);
		addsym (ret, sdb_fmt ("syscall.%s", name), ptr_addr);
	}
}
Пример #7
0
R_API void r_anal_type_match(RCore *core, RAnalFunction *fcn) {
	bool esil_var[STATES_SIZE] = {false};
	if (!core ) {
		return;
	}
	if (!r_anal_emul_init (core, esil_var) || !fcn ) {
		r_anal_emul_restore (core, esil_var);
		return;
	}
	const char *pc = r_reg_get_name (core->anal->reg, R_REG_NAME_PC);
	ut64 addr = fcn->addr;
	r_reg_setv (core->dbg->reg, pc, fcn->addr);
	r_debug_reg_sync (core->dbg, -1, true);
	r_cons_break (NULL, NULL);
	while (!r_cons_is_breaked ()) {
		RAnalOp *op = r_core_anal_op (core, addr);
		int loop_count = sdb_num_get ( core->anal->esil->db_trace, sdb_fmt (-1, "0x%"PFMT64x".count", addr), 0);
		if (loop_count > LOOP_MAX) {
			eprintf ("Unfortunately your evilly engineered %s function trapped my most innocent `aftm` in an infinite loop.\n", fcn->name);
			eprintf ("I kept trace log for you to review and find out how bad things were going to happen by yourself.\n");
			eprintf ("You can view this log by `ate`. Meanwhile, I will train on how to behave with such behaviour without bothering you.\n");
			return;
		}
		sdb_num_set (core->anal->esil->db_trace, sdb_fmt (-1, "0x%"PFMT64x".count", addr), loop_count + 1, 0);
		if (!op || op->type == R_ANAL_OP_TYPE_RET) {
			r_anal_emul_restore (core, esil_var);
			return;
		}
		if (op->type == R_ANAL_OP_TYPE_CALL) {
			RAnalFunction *fcn_call = r_anal_get_fcn_in (core->anal, op->jump, -1);
			//eprintf ("in the middle of %s\n", fcn_call->name);
			if (fcn_call) {
				type_match (core, addr, fcn_call->name);
			} else {
				eprintf ("Cannot find function at 0x%08"PFMT64x"\n", op->jump);
			}
			addr += op->size;
			r_anal_op_free (op);
			r_reg_setv (core->dbg->reg, pc, addr);
			r_debug_reg_sync (core->dbg, -1, true);
			r_anal_esil_set_pc (core->anal->esil, addr);
			addr += stack_clean (core, addr, fcn);
			r_reg_setv (core->dbg->reg, pc, addr);
			r_debug_reg_sync (core->dbg, -1, true);
			r_anal_esil_set_pc (core->anal->esil, addr);
			continue;
		} else {
			r_core_esil_step (core, UT64_MAX, NULL);
			r_anal_op_free (op);
		}
		r_core_cmd0 (core, ".ar*");
		addr = r_reg_getv (core->anal->reg, pc);
	}
	r_cons_break_end ();
	r_anal_emul_restore (core, esil_var);

}
Пример #8
0
R_API char *r_type_enum_member(Sdb *TDB, const char *name, const char *member, ut64 val) {
	const char *q;
	if (r_type_kind (TDB, name) != R_TYPE_ENUM) {
		return NULL;
	}
	if (member) {
		q = sdb_fmt ("enum.%s.%s", name, member);
	} else {
		q = sdb_fmt ("enum.%s.0x%x", name, val);
	}
	return sdb_get (TDB, q, 0);
}
Пример #9
0
// avr
R_API int r_anal_var_access (RAnal *a, ut64 var_addr, char kind, int scope, int delta, int xs_type, ut64 xs_addr) {
	const char *var_global;
	const char *xs_type_str = xs_type? "writes": "reads";
// TODO: kind is not used
	if (scope>0) { // local
		char *var_local = sdb_fmt (0, "var.0x%"PFMT64x".%d.%d.%s",
			var_addr, scope, delta, xs_type_str);
		return sdb_array_add_num (DB, var_local, xs_addr, 0);
	}
	// global
	sdb_add (DB, sdb_fmt (0,"var.0x%"PFMT64x, var_addr), "a,", 0);
	var_global = sdb_fmt (0, "var.0x%"PFMT64x".%s", var_addr, xs_type_str);
	return sdb_array_add_num (DB, var_global, xs_addr, 0);
}
Пример #10
0
R_API const char* r_syscall_sysreg(RSyscall *s, const char *type, ut64 num) {
	if (!s || !s->db) {
		return NULL;
	}
	const char *key = sdb_fmt (0, "%s,%"PFMT64d, type, num);
	return sdb_const_get (s->db, key, 0);
}
Пример #11
0
R_API char *r_type_enum_getbitfield(Sdb *TDB, const char *name, ut64 val) {
	char *q, *ret = NULL;
	const char *res;
	int i;

	if (r_type_kind (TDB, name) != R_TYPE_ENUM) {
		return NULL;
	}
	bool isFirst = true;
	ret = r_str_appendf (ret, "0x%08"PFMT64x" : ", val);
	for (i = 0; i < 32; i++) {
		if (!(val & (1 << i))) {
			continue;
		}
		q = sdb_fmt ("enum.%s.0x%x", name, (1<<i));
                res = sdb_const_get (TDB, q, 0);
                if (isFirst) {
			isFirst = false;
                } else {
			ret = r_str_append (ret, " | ");
                }
                if (res) {
			ret = r_str_append (ret, res);
                } else {
			ret = r_str_appendf (ret, "0x%x", (1<<i));
                }
	}
	return ret;
}
Пример #12
0
static int art_header_load(ARTHeader *art, RBuffer *buf, Sdb *db) {
	/* TODO: handle read errors here */
	(void)r_buf_fread_at (buf, 0, (ut8*)art, "IIiiiiiiiiiii", 1);
	sdb_set (db, "img.base", sdb_fmt (0, "0x%x", art->image_base), 0);
	sdb_set (db, "img.size", sdb_fmt (0, "0x%x", art->image_size), 0);
	sdb_set (db, "art.checksum", sdb_fmt (0, "0x%x", art->checksum), 0);
	sdb_set (db, "art.version", sdb_fmt (0, "%c%c%c",
		art->version[0], art->version[1], art->version[2]), 0);
	sdb_set (db, "oat.begin", sdb_fmt (0, "0x%x", art->oat_begin), 0);
	sdb_set (db, "oat.end", sdb_fmt (0, "0x%x", art->oat_end), 0);
	sdb_set (db, "oat_data.begin", sdb_fmt (0, "0x%x", art->oat_data_begin), 0);
	sdb_set (db, "oat_data.end", sdb_fmt (0, "0x%x", art->oat_data_end), 0);
	sdb_set (db, "patch_delta", sdb_fmt (0, "0x%x", art->patch_delta), 0);
	sdb_set (db, "image_roots", sdb_fmt (0, "0x%x", art->image_roots), 0);
	return R_TRUE;
}
Пример #13
0
R_API RAnalVar *r_anal_var_get (RAnal *a, ut64 addr, char kind, int scope, int delta) {
	RAnalVar *av;
	struct VarType vt;
	RAnalFunction *fcn = r_anal_get_fcn_in (a, addr, 0);
	if (!fcn)
		return NULL;
	if (delta<0) {
		kind = 'v';
		delta = -delta;
	}
	char *vardef = sdb_get (DB,
		sdb_fmt (0, "var.0x%"PFMT64x".%c.%d.%d",
			fcn->addr, kind, scope, delta), 0);
	if (!vardef)
		return NULL;
	sdb_fmt_tobin (vardef, SDB_VARTYPE_FMT, &vt);

	av = R_NEW0 (RAnalVar);
	av->addr = addr;
	av->scope = scope;
	av->delta = delta;
	av->name = strdup (vt.name);
	av->size = vt.size;
	av->type = strdup (vt.type);

	sdb_fmt_free (&vt, SDB_VARTYPE_FMT);
	// TODO:
	// get name from sdb
	// get size from sdb
	// get type from sdb
	return av;
}
Пример #14
0
R_API RAnalBlock * r_anal_state_search_bb(RAnalState* state, ut64 addr) {
	/*
	 *   Return 0 if no rehash is needed, otherwise return 1
	 */
	const char *key = sdb_fmt (0, "0x%08"PFMT64x, addr);
	RAnalBlock *tmp_bb = ht_find (state->ht, key, NULL);
	return tmp_bb;
}
Пример #15
0
static int typelist (void *p, const char *k, const char *v) {
	r_cons_printf ("tk %s = %s\n", k, v);
#if 0
	if (!strcmp (v, "func")) {
		const char *rv = sdb_const_get (DB,
						sdb_fmt (0, "func.%s.ret", k), 0);
		r_cons_printf ("# %s %s(", rv, k);
		for (i = 0; i < 16; i++) {
			char *av = sdb_get (DB,
					sdb_fmt (0, "func.%s.arg.%d", k, i), 0);
			if (!av) break;
			r_str_replace_char (av, ',', ' ');
			r_cons_printf ("%s%s", i? ", ": "", av);
			free (av);
		}
		r_cons_printf (");\n");
		// signature in pf for asf
		r_cons_printf ("asf %s=", k);
		// formats
		for (i = 0; i < 16; i++) {
			const char *fmt;
			char *comma, *av = sdb_get (DB,
						sdb_fmt (0, "func.%s.arg.%d", k, i), 0);
			if (!av) break;
			comma = strchr (av, ',');
			if (comma) *comma = 0;
			fmt = sdb_const_get (DB, sdb_fmt (0, "type.%s", av), 0);
			r_cons_printf ("%s", fmt);
			if (comma) *comma = ',';
			free (av);
		}
		// names
		for (i = 0; i < 16; i++) {
			char *comma, *av = sdb_get (DB,
						sdb_fmt (0, "func.%s.arg.%d", k, i), 0);
			if (!av) break;
			comma = strchr (av, ',');
			if (comma) *comma++ = 0;
			r_cons_printf (" %s", comma);
			free (av);
		}
		r_cons_newline ();
	}
#endif
	return 1;
}
Пример #16
0
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
	ebc_command_t cmd = { {0}, {0} };
	int ret = ebc_decode_command (buf, &cmd);
	const char *buf_asm = (cmd.operands[0])
		? sdb_fmt ("%s %s", cmd.instr, cmd.operands): cmd.instr;
	r_asm_op_set_asm (op, buf_asm);
	return op->size = ret;
}
Пример #17
0
static void pair_str (const char *a, const char *b, int mode, int last) {
	if (IS_MODE_JSON (mode)) {
		if (!b) b = "";
		pair (a, sdb_fmt (0, "\"%s\"", b), mode, last);
	} else {
		pair (a, b, mode, last);
	}
}
Пример #18
0
R_API int r_anal_var_add (RAnal *a, ut64 addr, int scope, int delta, char kind, const char *type, int size, const char *name) {
	char *var_def;
	if (!kind) kind ='v';
	if (!type) type = "int";
//eprintf ("VAR ADD 0x%llx  - %d\n", addr, delta);
	switch (kind) {
	case 'a':
	case 'r':
	case 'v':
		break;
	default:
		eprintf ("Invalid var kind '%c'\n", kind);
		return R_FALSE;
	}
	var_def = sdb_fmt (0,"%c,%s,%d,%s", kind, type, size, name);
	if (scope>0) {
		/* local variable */
		char *fcn_key = sdb_fmt (1, "fcn.0x%"PFMT64x".%c", addr, kind);
		char *var_key = sdb_fmt (2, "var.0x%"PFMT64x".%c.%d.%d", addr, kind, scope, delta);
		char *name_key = sdb_fmt (3, "var.0x%"PFMT64x".%c.%d.%s", addr, kind, scope, name);
		char *shortvar = sdb_fmt (4, "%d.%d", scope, delta);
		sdb_array_add (DB, fcn_key, shortvar, 0);
		sdb_set (DB, var_key, var_def, 0);
		sdb_num_set (DB, name_key, delta, 0);
	} else {
		/* global variable */
		char *var_global = sdb_fmt (1, "var.0x%"PFMT64x, addr);
		char *var_def = sdb_fmt (2,"%c.%s,%d,%s", kind, type, size, name);
		sdb_array_add (DB, var_global, var_def, 0);
	}
//	ls_sort (DB->ht->list, mystrcmp);
	return R_TRUE;
}
Пример #19
0
static void set_fcn_args_info(RAnalFuncArg *arg, RAnal *anal, const char *fcn_name, const char *cc, int arg_num) {
	if (!fcn_name || !arg || !anal) {
		return;
	}
	Sdb *TDB = anal->sdb_types;
	arg->name = r_type_func_args_name (TDB, fcn_name, arg_num);
	arg->orig_c_type = r_type_func_args_type (TDB, fcn_name, arg_num);
	if (!strncmp ("const ", arg->orig_c_type, 6)) {
		arg->c_type = arg->orig_c_type + 6;
	} else {
		arg->c_type = arg->orig_c_type;
	}
	const char *query = sdb_fmt ("type.%s", arg->c_type);
	arg->fmt = sdb_const_get (TDB, query, 0);
	const char *t_query = sdb_fmt ("type.%s.size", arg->c_type);
	arg->size = sdb_num_get (TDB, t_query, 0) / 8;
	arg->cc_source = r_anal_cc_arg (anal, cc, arg_num + 1);
}
Пример #20
0
R_API void r_bin_filter_sym(RBinFile *bf, Sdb *db, ut64 vaddr, RBinSymbol *sym) {
	if (!db || !sym || !sym->name) {
		return;
	}
	char *name = sym->name;
	// if (!strncmp (sym->name, "imp.", 4)) {
	// demangle symbol name depending on the language specs if any
	if (bf && bf->o && bf->o->lang) {
		const char *lang = r_bin_lang_tostring (bf->o->lang);
		char *dn = r_bin_demangle (bf, lang, sym->name, sym->vaddr);
		if (dn && *dn) {
			sym->dname = dn;
			// XXX this is wrong but is required for this test to pass
			// pmb:new pancake$ bin/r2r.js db/formats/mangling/swift
			sym->name = dn;
			// extract class information from demangled symbol name
			char *p = strchr (dn, '.');
			if (p) {
				if (IS_UPPER (*dn)) {
					sym->classname = strdup (dn);
					sym->classname[p - dn] = 0;
				} else if (IS_UPPER (p[1])) {
					sym->classname = strdup (p + 1);
					p = strchr (sym->classname, '.');
					if (p) {
						*p = 0;
					}
				}
			}
		}
	}

	// XXX this is very slow, must be optimized
	const char *uname = sdb_fmt ("%" PFMT64x ".%s", vaddr, name);
	ut32 vhash = sdb_hash (uname); // vaddr hash - unique
	ut32 hash = sdb_hash (name); // name hash - if dupped and not in unique hash must insert
	int count = sdb_num_inc (db, sdb_fmt ("%x", hash), 1, 0);
	if (sdb_exists (db, sdb_fmt ("%x", vhash))) {
		// TODO: symbol is dupped, so symbol can be removed!
		return;
	}
	sdb_num_set (db, sdb_fmt ("%x", vhash), 1, 0);
	sym->dup_count = count - 1;
}
Пример #21
0
R_API char *r_type_get_struct_memb(Sdb *TDB, const char *type, int offset) {
	int i, typesize = 0;
	char *res = NULL;

	if (offset < 0) {
		return NULL;
	}
	char* query = sdb_fmt ("struct.%s", type);
	char *members = sdb_get (TDB, query, 0);
	if (!members) {
		//eprintf ("%s is not a struct\n", type);
		return NULL;
	}
	int nargs = r_str_split (members, ',');
	for (i = 0; i < nargs ; i++) {
		const char *name = r_str_word_get0 (members, i);
		if (!name) {
			break;
		}
		query = sdb_fmt ("struct.%s.%s", type, name);
		char *subtype = sdb_get (TDB, query, 0);
		if (!subtype) {
			break;
		}
		int len = r_str_split (subtype, ',');
		if (len < 3) {
			free (subtype);
			break;
		}
		int val = r_num_math (NULL, r_str_word_get0 (subtype, len - 1));
		int arrsz = val ? val : 1;
		if ((typesize / 8) == offset) {
			res = r_str_newf ("%s.%s", type, name);
			free (subtype);
			break;
		}
		typesize += r_type_get_bitsize (TDB, subtype) * arrsz;
		free (subtype);
	}
	free (members);
	return res;
}
Пример #22
0
static RAnalVar *get_used_var(RAnal *anal, RAnalOp *op) {
	char *inst_key = sdb_fmt (0, "inst.0x%"PFMT64x".vars", op->addr);
	const char *var_def = sdb_const_get (anal->sdb_fcns, inst_key, 0);
	struct VarUsedType vut;
	RAnalVar *res;

	if (sdb_fmt_tobin (var_def, SDB_VARUSED_FMT, &vut) != 4) {
		return NULL;
	}
	res = r_anal_var_get (anal, vut.fcn_addr, vut.type[0], vut.scope, vut.delta);
	sdb_fmt_free (&vut, SDB_VARUSED_FMT);
	return res;
}
Пример #23
0
R_API void r_anal_state_insert_bb(RAnalState* state, RAnalBlock *bb) {
	if (!state || !bb) {
		return;
	}
	if (!r_anal_state_search_bb (state, bb->addr) && state->current_fcn) {
		r_list_append (state->current_fcn->bbs, bb);
        state->bytes_consumed += state->current_bb->op_sz;
		const char *key = sdb_fmt (0, "0x%08"PFMT64x, bb->addr);
		if (!ht_insert (state->ht, key, bb)) {
			eprintf ("Inserted bb 0x%04"PFMT64x" failure\n", bb->addr);
		}
	}
}
Пример #24
0
static Sdb *openDatabase(Sdb *db, const char *name) {
#define SYSCALLPATH "/share/radare2/" R2_VERSION
	const char *file = sdb_fmt (0, "%s/%s/%s.sdb",
		r_sys_prefix (NULL), SYSCALLPATH, name);
	if (!r_file_exists (file)) {
	//	eprintf ("r_syscall_setup: Cannot find '%s'\n", file);
		return false;
	}
	if (!db) {
		return sdb_new (0, file, 0);
	}
	sdb_reset (db);
	sdb_open (db, file);
	return db;
}
Пример #25
0
static bool _fill_bin_symbol(struct r_bin_coff_obj *bin, int idx, RBinSymbol **sym) {
	RBinSymbol *ptr = *sym;
	char * coffname = NULL;
	struct coff_symbol *s = NULL;
	if (idx < 0 || idx > bin->hdr.f_nsyms) {
		return false;
	}
	if (!bin->symbols) {
		return false;
	}
	s = &bin->symbols[idx];
	coffname = r_coff_symbol_name (bin, s);
	if (!coffname) {
		return false;
	}
	ptr->name = strdup (coffname);
	free (coffname);
	ptr->forwarder = r_str_const ("NONE");

	switch (s->n_sclass) {
	case COFF_SYM_CLASS_FUNCTION:
		ptr->type = r_str_const ("FUNC");
		break;
	case COFF_SYM_CLASS_FILE:
		ptr->type = r_str_const ("FILE");
		break;
	case COFF_SYM_CLASS_SECTION:
		ptr->type = r_str_const ("SECTION");
		break;
	case COFF_SYM_CLASS_EXTERNAL:
		ptr->type = r_str_const ("EXTERNAL");
		break;
	case COFF_SYM_CLASS_STATIC:
		ptr->type = r_str_const ("STATIC");
		break;
	default:
		ptr->type = r_str_const (sdb_fmt (0, "%i", s->n_sclass));
		break;
	}
	if (bin->symbols[idx].n_scnum < bin->hdr.f_nscns &&
	    bin->symbols[idx].n_scnum > 0) {
		//first index is 0 that is why -1
		ptr->paddr = bin->scn_hdrs[s->n_scnum - 1].s_scnptr + s->n_value;
	}
	ptr->size = 4;
	ptr->ordinal = 0;
	return true;
}
Пример #26
0
R_API int r_anal_var_add(RAnal *a, ut64 addr, int scope, int delta, char kind, const char *type, int size, const char *name) {
	char *var_def;
	if (!kind) {
		kind = R_ANAL_VAR_KIND_BPV;
	}
	if (!type) {
		type = "int";
	}
	switch (kind) {
	case R_ANAL_VAR_KIND_BPV: // base pointer var/args
	case R_ANAL_VAR_KIND_SPV: // stack pointer var/args
	case R_ANAL_VAR_KIND_REG: // registers args
		break;
	default:
		eprintf ("Invalid var kind '%c'\n", kind);
		return false;
	}
	var_def = sdb_fmt (0,"%c,%s,%d,%s", kind, type, size, name);
	if (scope > 0) {
		char *sign = "";
		if (delta < 0) {
			delta = -delta;
			sign = "_";
		}
		/* local variable */
		char *fcn_key = sdb_fmt (1, "fcn.0x%"PFMT64x".%c", addr, kind);
		char *var_key = sdb_fmt (2, "var.0x%"PFMT64x".%c.%d.%s%d", addr, kind, scope, sign, delta);
		char *name_key = sdb_fmt (3, "var.0x%"PFMT64x".%d.%s", addr, scope, name);
		char *shortvar = sdb_fmt (4, "%d.%s%d", scope, sign, delta);
		sdb_array_add (DB, fcn_key, shortvar, 0);
		sdb_set (DB, var_key, var_def, 0);
		if (*sign) {
			delta = -delta;
		}
		char *name_val = sdb_fmt (5, "%c,%d", kind, delta);
		sdb_set (DB, name_key, name_val, 0);
	} else {
		/* global variable */
		char *var_global = sdb_fmt (1, "var.0x%"PFMT64x, addr);
		char *var_def = sdb_fmt (2,"%c.%s,%d,%s", kind, type, size, name);
		sdb_array_add (DB, var_global, var_def, 0);
	}
//	ls_sort (DB->ht->list, mystrcmp);
	return true;
}
Пример #27
0
static int tms320c64x_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
	cs_insn* insn;
	int n = -1, ret = -1;
	int mode = 0;
	if (op) {
		memset (op, 0, sizeof (RAsmOp));
		op->size = 4;
	}
	if (cd != 0) {
		cs_close (&cd);
	}
	ret = cs_open (CS_ARCH_TMS320C64X, mode, &cd);
	if (ret) {
		goto fin;
	}
	cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
	if (!op) {
		return 0;
	}
	n = cs_disasm (cd, buf, len, a->pc, 1, &insn);
	if (n < 1) {
		r_asm_op_set_asm (op, "invalid");
		op->size = 4;
		ret = -1;
		goto beach;
	} else {
		ret = 4;
	}
	if (insn->size < 1) {
		goto beach;
	}
	op->size = insn->size;
	char *buf_asm = sdb_fmt ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str);
	r_str_replace_char (buf_asm, '%', 0);
	r_str_case (buf_asm, false);
	r_asm_op_set_asm (op, buf_asm);
	cs_free (insn, n);
	beach:
	// cs_close (&cd);
	fin:
	return ret;
}
Пример #28
0
R_API RAnalVar *get_link_var(RAnal *anal, ut64 faddr, RAnalVar *var) {
	const char *var_local = sdb_fmt ("var.0x%"PFMT64x".%d.%d.%s",
			faddr, 1, var->delta, "reads");
	const char *xss = sdb_const_get (anal->sdb_fcns, var_local, 0);
	ut64 addr = r_num_math (NULL, xss);
	char *inst_key = r_str_newf ("inst.0x%"PFMT64x".lvar", addr);
	char *var_def = sdb_get (anal->sdb_fcns, inst_key, 0);

	if (!var_def) {
		free (inst_key);
		return NULL;
	}
	struct VarUsedType vut;
	RAnalVar *res = NULL;
	if (sdb_fmt_tobin (var_def, SDB_VARUSED_FMT, &vut) == 4) {
		res = r_anal_var_get (anal, vut.fcn_addr, vut.type[0], vut.scope, vut.delta);
		sdb_fmt_free (&vut, SDB_VARUSED_FMT);
	}
	free (inst_key);
	free (var_def);
	return res;
}
Пример #29
0
static void cons_pal_update_event(RConsContext *ctx) {
	Sdb *db = sdb_new0 ();
	int i, n = 0;
	char **color;
	/* Compute cons->pal values */
	for (i = 0; keys[i].name; i++) {
		RColor *rcolor = (RColor *) (((ut8 *) &(ctx->cpal)) + keys[i].coff);
		color = (char **) (((ut8 *) &(ctx->pal)) + keys[i].off);
		// Color is dynamically allocated, needs to be freed
		if (*color) {
			R_FREE (*color);
		}
		*color = r_cons_rgb_str_mode (ctx->color, NULL, 0, rcolor);
		const char *rgb = sdb_fmt ("rgb:%02x%02x%02x", rcolor->r, rcolor->g, rcolor->b);
		sdb_set (db, rgb, "1", 0);
	}
	SdbList *list = sdb_foreach_list (db, true);
	SdbListIter *iter;
	SdbKv *kv;
	r_cons_rainbow_free (ctx);
	r_cons_rainbow_new (ctx, list->length);
	ls_foreach (list, iter, kv) {
		ctx->pal.rainbow[n++] = strdup (sdbkv_key (kv));
	}
Пример #30
0
R_API void r_cons_pal_update_event() {
	Sdb *db = sdb_new0 ();
	int i, n = 0;
	char **color;
	/* Compute cons->pal values */
	for (i = 0; keys[i].name; i++) {
		RColor *rcolor = RCOLOR_AT (i);
		color = COLOR_AT (i);
		// Color is dynamically allocated, needs to be freed
		if (*color) {
			R_FREE (*color);
		}
		*color = r_cons_rgb_str (NULL, 0, rcolor);
		const char *rgb = sdb_fmt ("rgb:%02x%02x%02x", rcolor->r, rcolor->g, rcolor->b);
		sdb_set (db, rgb, "1", 0);
	}
	SdbList *list = sdb_foreach_list (db, true);
	SdbListIter *iter;
	SdbKv *kv;
	r_cons_rainbow_free ();
	r_cons_rainbow_new (list->length);
	ls_foreach (list, iter, kv) {
		r_cons_singleton ()->pal.rainbow[n++] = strdup (kv->key);
	}