2011-02-18 09:08:24 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 */
|
|
|
|
/* - nibble<.ds@gmail.com> + pancake<nopcode.org> */
|
2009-02-06 17:22:27 +00:00
|
|
|
|
2009-02-15 23:57:03 +00:00
|
|
|
#include <r_anal.h>
|
|
|
|
#include <r_util.h>
|
2010-02-26 12:08:42 +00:00
|
|
|
#include <r_list.h>
|
2010-02-28 19:07:36 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2010-05-25 23:42:22 +00:00
|
|
|
static RAnalPlugin *anal_static_plugins[] =
|
2010-02-28 19:07:36 +00:00
|
|
|
{ R_ANAL_STATIC_PLUGINS };
|
2009-02-15 23:57:03 +00:00
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
static RAnalVarType anal_default_vartypes[] =
|
2011-09-14 01:47:30 +00:00
|
|
|
{{ "char", "c", 1 },
|
2010-03-11 18:52:05 +00:00
|
|
|
{ "byte", "b", 1 },
|
2011-09-14 01:47:30 +00:00
|
|
|
{ "int", "i", 4 },
|
2010-03-11 18:52:05 +00:00
|
|
|
{ "int32", "d", 4 },
|
2011-09-14 01:47:30 +00:00
|
|
|
{ "int64", "q", 8 },
|
2010-03-11 18:52:05 +00:00
|
|
|
{ "dword", "x", 4 },
|
|
|
|
{ "float", "f", 4 },
|
|
|
|
{ NULL, NULL, 0 }};
|
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API RAnal *r_anal_new() {
|
2010-02-28 19:07:36 +00:00
|
|
|
int i;
|
2010-06-04 21:47:35 +00:00
|
|
|
RAnalPlugin *static_plugin;
|
|
|
|
RAnal *anal = R_NEW (RAnal);
|
2011-02-03 08:31:50 +00:00
|
|
|
if (!anal)
|
|
|
|
return NULL;
|
|
|
|
memset (anal, 0, sizeof (RAnal));
|
2011-10-14 08:09:53 +00:00
|
|
|
anal->diff_ops = 0;
|
|
|
|
anal->diff_thbb = R_ANAL_THRESHOLDBB;
|
|
|
|
anal->diff_thfcn = R_ANAL_THRESHOLDFCN;
|
2011-10-19 11:05:41 +00:00
|
|
|
anal->split = R_TRUE; // used from core
|
2011-03-28 08:24:01 +00:00
|
|
|
anal->queued = NULL;
|
2011-03-01 23:02:50 +00:00
|
|
|
anal->meta = r_meta_new ();
|
2011-02-03 08:31:50 +00:00
|
|
|
anal->syscall = r_syscall_new ();
|
|
|
|
r_io_bind_init (anal->iob);
|
2011-02-18 12:08:09 +00:00
|
|
|
anal->reg = r_reg_new ();
|
2011-02-03 08:31:50 +00:00
|
|
|
anal->lineswidth = 0;
|
|
|
|
anal->fcns = r_anal_fcn_list_new ();
|
2011-09-14 00:07:06 +00:00
|
|
|
anal->fcnstore = r_listrange_new ();
|
2011-02-03 08:31:50 +00:00
|
|
|
anal->refs = r_anal_ref_list_new ();
|
|
|
|
anal->vartypes = r_anal_var_type_list_new ();
|
|
|
|
r_anal_set_bits (anal, 32);
|
|
|
|
r_anal_set_big_endian (anal, R_FALSE);
|
2011-02-18 09:08:24 +00:00
|
|
|
INIT_LIST_HEAD (&anal->anals); // TODO: use RList here
|
2011-02-03 08:31:50 +00:00
|
|
|
for (i=0; anal_static_plugins[i]; i++) {
|
|
|
|
static_plugin = R_NEW (RAnalPlugin);
|
|
|
|
memcpy (static_plugin, anal_static_plugins[i], sizeof (RAnalPlugin));
|
|
|
|
r_anal_add (anal, static_plugin);
|
2010-01-31 01:30:59 +00:00
|
|
|
}
|
2011-02-03 08:31:50 +00:00
|
|
|
for (i=0; anal_default_vartypes[i].name; i++)
|
|
|
|
r_anal_var_type_add (anal, anal_default_vartypes[i].name,
|
|
|
|
anal_default_vartypes[i].size, anal_default_vartypes[i].fmt);
|
2010-01-31 01:30:59 +00:00
|
|
|
return anal;
|
2009-02-06 17:22:27 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 23:21:25 +00:00
|
|
|
R_API void r_anal_free(RAnal *anal) {
|
|
|
|
if (!anal) return;
|
|
|
|
/* TODO: Free anals here */
|
|
|
|
r_listrange_free (anal->fcnstore);
|
|
|
|
r_list_free (anal->fcns);
|
|
|
|
r_list_free (anal->vartypes);
|
2010-05-20 15:40:58 +00:00
|
|
|
free (anal);
|
|
|
|
}
|
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API void r_anal_set_user_ptr(RAnal *anal, void *user) {
|
2009-02-15 23:57:03 +00:00
|
|
|
anal->user = user;
|
2009-02-06 17:22:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 23:42:22 +00:00
|
|
|
R_API int r_anal_add(RAnal *anal, RAnalPlugin *foo) {
|
2009-02-15 23:57:03 +00:00
|
|
|
if (foo->init)
|
2010-06-16 07:42:46 +00:00
|
|
|
foo->init (anal->user);
|
|
|
|
list_add_tail (&(foo->list), &(anal->anals));
|
2009-02-06 17:22:27 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2009-02-08 23:19:06 +00:00
|
|
|
|
2009-10-12 15:41:52 +00:00
|
|
|
// TODO: Must be deprecated
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API int r_anal_list(RAnal *anal) {
|
2009-02-15 23:57:03 +00:00
|
|
|
struct list_head *pos;
|
|
|
|
list_for_each_prev(pos, &anal->anals) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RAnalPlugin *h = list_entry(pos, RAnalPlugin, list);
|
2010-08-24 20:16:03 +00:00
|
|
|
printf ("anal %-10s %s\n", h->name, h->desc);
|
2009-02-15 23:57:03 +00:00
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API int r_anal_use(RAnal *anal, const char *name) {
|
2009-02-15 23:57:03 +00:00
|
|
|
struct list_head *pos;
|
2010-10-26 16:24:14 +00:00
|
|
|
list_for_each (pos, &anal->anals) { // XXX: must be _prevmust be _prev
|
2010-05-25 23:42:22 +00:00
|
|
|
RAnalPlugin *h = list_entry(pos, RAnalPlugin, list);
|
2010-02-02 10:09:52 +00:00
|
|
|
if (!strcmp (h->name, name)) {
|
2009-02-15 23:57:03 +00:00
|
|
|
anal->cur = h;
|
2011-02-18 12:08:09 +00:00
|
|
|
r_anal_set_reg_profile (anal);
|
2009-02-15 23:57:03 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-18 12:08:09 +00:00
|
|
|
R_API int r_anal_set_reg_profile(RAnal *anal) {
|
2011-09-14 00:07:06 +00:00
|
|
|
if (anal && anal->cur && anal->cur->set_reg_profile)
|
2011-02-18 12:08:09 +00:00
|
|
|
return anal->cur->set_reg_profile (anal);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API int r_anal_set_bits(RAnal *anal, int bits) {
|
2009-02-15 23:57:03 +00:00
|
|
|
switch (bits) {
|
2009-08-14 00:37:18 +00:00
|
|
|
case 8:
|
2009-02-15 23:57:03 +00:00
|
|
|
case 16:
|
|
|
|
case 32:
|
|
|
|
case 64:
|
|
|
|
anal->bits = bits;
|
2011-02-18 12:08:09 +00:00
|
|
|
r_anal_set_reg_profile (anal);
|
2009-02-15 23:57:03 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2009-09-25 02:04:51 +00:00
|
|
|
return R_FALSE;
|
2009-02-15 23:57:03 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 11:00:04 +00:00
|
|
|
R_API int r_anal_set_big_endian(RAnal *anal, int bigend) {
|
2009-09-09 00:35:00 +00:00
|
|
|
anal->big_endian = bigend;
|
2009-02-15 23:57:03 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2010-05-16 12:04:08 +00:00
|
|
|
|
|
|
|
R_API char *r_anal_strmask (RAnal *anal, const char *data) {
|
2011-02-24 13:06:49 +00:00
|
|
|
RAnalOp *op;
|
2010-05-16 12:04:08 +00:00
|
|
|
ut8 *buf;
|
|
|
|
char *ret = NULL;
|
|
|
|
int oplen, len, idx = 0;
|
|
|
|
|
|
|
|
ret = strdup (data);
|
2011-05-11 17:19:53 +00:00
|
|
|
buf = malloc (1+strlen (data));
|
2011-02-24 13:06:49 +00:00
|
|
|
op = r_anal_op_new ();
|
|
|
|
if (op == NULL || ret == NULL || buf == NULL) {
|
|
|
|
free (op);
|
2010-05-16 12:04:08 +00:00
|
|
|
free (buf);
|
|
|
|
free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
len = r_hex_str2bin (data, buf);
|
|
|
|
while (idx < len) {
|
2011-02-24 13:06:49 +00:00
|
|
|
if ((oplen = r_anal_op (anal, op, 0, buf+idx, len-idx)) == 0)
|
2010-05-16 12:04:08 +00:00
|
|
|
break;
|
2011-02-24 13:06:49 +00:00
|
|
|
switch (op->type) {
|
2010-05-16 12:04:08 +00:00
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
2010-11-21 18:01:41 +00:00
|
|
|
case R_ANAL_OP_TYPE_UCALL:
|
2010-05-16 12:04:08 +00:00
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
2010-11-21 18:01:41 +00:00
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
2011-02-24 13:06:49 +00:00
|
|
|
if (op->nopcode != 0)
|
|
|
|
memset (ret+(idx+op->nopcode)*2, '.', (oplen-op->nopcode)*2);
|
2010-05-16 12:04:08 +00:00
|
|
|
}
|
|
|
|
idx += oplen;
|
|
|
|
}
|
2011-02-24 13:06:49 +00:00
|
|
|
free (op);
|
2010-05-16 12:04:08 +00:00
|
|
|
free (buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-06 15:26:21 +00:00
|
|
|
|
2011-02-11 10:22:43 +00:00
|
|
|
R_API void r_anal_trace_bb(RAnal *anal, ut64 addr) {
|
|
|
|
RAnalBlock *bbi;
|
|
|
|
RAnalFcn *fcni;
|
|
|
|
RListIter *iter, *iter2;
|
2011-09-03 03:09:31 +00:00
|
|
|
VERBOSE_ANAL eprintf ("bbtraced\n"); // XXX Debug msg
|
|
|
|
r_list_foreach (anal->fcns, iter, fcni) {
|
2011-02-11 10:22:43 +00:00
|
|
|
r_list_foreach (fcni->bbs, iter2, bbi) {
|
|
|
|
if (addr>=bbi->addr && addr<(bbi->addr+bbi->size)) {
|
|
|
|
bbi->traced = R_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-03 03:09:31 +00:00
|
|
|
}
|
2011-02-11 10:22:43 +00:00
|
|
|
}
|