2017-02-02 12:08:08 +00:00
|
|
|
|
/* radare - LGPL - Copyright 2009-2017 - pancake, nibble */
|
2010-02-26 20:00:03 +00:00
|
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
|
#include <r_list.h>
|
2016-10-26 23:02:26 +00:00
|
|
|
|
#include <r_flag.h>
|
2010-02-26 20:00:03 +00:00
|
|
|
|
#include <r_core.h>
|
2015-11-25 11:14:40 +00:00
|
|
|
|
#include <r_bin.h>
|
2010-02-26 20:00:03 +00:00
|
|
|
|
|
2015-10-14 21:50:16 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2016-08-21 09:39:37 +00:00
|
|
|
|
#define SLOW_IO 0
|
2015-07-27 21:43:38 +00:00
|
|
|
|
#define HASNEXT_FOREVER 1
|
2013-10-08 02:58:51 +00:00
|
|
|
|
|
2015-07-31 10:40:04 +00:00
|
|
|
|
#define HINTCMD_ADDR(hint,x,y) if(hint->x) \
|
2015-12-10 22:42:15 +00:00
|
|
|
|
r_cons_printf (y" @ 0x%"PFMT64x"\n", hint->x, hint->addr)
|
|
|
|
|
#define HINTCMD(hint,x,y,json) if(hint->x) \
|
2016-11-01 18:47:34 +00:00
|
|
|
|
r_cons_printf (y"", hint->x)
|
2015-07-31 10:40:04 +00:00
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
RAnal *a;
|
|
|
|
|
int mode;
|
|
|
|
|
int count;
|
|
|
|
|
} HintListState;
|
|
|
|
|
|
2016-11-01 18:47:34 +00:00
|
|
|
|
static void add_string_ref(RCore *core, ut64 xref_to);
|
|
|
|
|
static int cmpfcn(const void *_a, const void *_b);
|
2016-03-16 15:24:23 +00:00
|
|
|
|
|
2016-04-07 09:10:43 +00:00
|
|
|
|
static void loganal(ut64 from, ut64 to, int depth) {
|
2014-08-10 13:39:00 +00:00
|
|
|
|
r_cons_clear_line (1);
|
2016-04-07 09:10:43 +00:00
|
|
|
|
eprintf ("0x%08"PFMT64x" > 0x%08"PFMT64x" %d\r", from, to, depth);
|
2014-08-10 13:39:00 +00:00
|
|
|
|
}
|
2013-11-03 12:47:34 +00:00
|
|
|
|
|
2016-03-14 00:45:25 +00:00
|
|
|
|
static RCore *mycore = NULL;
|
|
|
|
|
|
2016-11-21 01:37:48 +00:00
|
|
|
|
|
2016-03-14 00:45:25 +00:00
|
|
|
|
// XXX: copypaste from anal/data.c
|
|
|
|
|
#define MINLEN 1
|
|
|
|
|
static int is_string (const ut8 *buf, int size, int *len) {
|
|
|
|
|
int i;
|
2016-08-22 16:32:18 +00:00
|
|
|
|
if (size < 1) {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
return 0;
|
2016-08-22 16:32:18 +00:00
|
|
|
|
}
|
|
|
|
|
if (size > 3 && buf[0] && !buf[1] && buf[2] && !buf[3]) {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
*len = 1; // XXX: TODO: Measure wide string length
|
|
|
|
|
return 2; // is wide
|
|
|
|
|
}
|
2016-08-22 16:32:18 +00:00
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
|
if (!buf[i] && i > MINLEN) {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
*len = i;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (buf[i] == 10 || buf[i] == 13 || buf[i] == 9) {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-03-14 08:42:54 +00:00
|
|
|
|
if (buf[i] < 32 || buf[i] > 127) {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
// not ascii text
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!IS_PRINTABLE (buf[i])) {
|
|
|
|
|
*len = i;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*len = i;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 17:46:45 +00:00
|
|
|
|
#if 0
|
2016-03-14 08:42:54 +00:00
|
|
|
|
// Detect if there's code in the given address
|
|
|
|
|
// - falls in section named 'text'
|
|
|
|
|
// - section has exec bit, some const strings are in there
|
|
|
|
|
// - addr is in different section than core->offset
|
|
|
|
|
static bool iscodesection(RCore *core, ut64 addr) {
|
|
|
|
|
RIOSection *s = r_io_section_vget (core->io, addr);
|
2016-08-22 16:32:18 +00:00
|
|
|
|
if (s && s->name && strstr (s->name, "text")) {
|
2016-03-14 08:42:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
// BSS return (s && s->rwx & R_IO_WRITE)? 0: 1;
|
|
|
|
|
// Cstring return (s && s->rwx & R_IO_EXEC)? 1: 0;
|
|
|
|
|
}
|
2016-12-05 17:46:45 +00:00
|
|
|
|
#endif
|
2016-03-14 08:42:54 +00:00
|
|
|
|
|
2016-11-01 18:42:43 +00:00
|
|
|
|
static char *is_string_at(RCore *core, ut64 addr, int *olen) {
|
2016-03-14 08:42:54 +00:00
|
|
|
|
ut8 *str;
|
2016-03-14 00:45:25 +00:00
|
|
|
|
int ret, len = 0;
|
2016-12-05 17:46:45 +00:00
|
|
|
|
//there can be strings in code section
|
|
|
|
|
#if 0
|
2016-03-14 08:42:54 +00:00
|
|
|
|
if (iscodesection (core, addr)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-12-05 17:46:45 +00:00
|
|
|
|
#endif
|
2016-03-14 08:42:54 +00:00
|
|
|
|
str = calloc (1024, 1);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!str) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-14 00:45:25 +00:00
|
|
|
|
r_io_read_at (core->io, addr, str, 1024);
|
|
|
|
|
str[1023] = 0;
|
2016-03-14 08:42:54 +00:00
|
|
|
|
// check if current section have no exec bit
|
2016-03-14 00:45:25 +00:00
|
|
|
|
ret = is_string (str, 1024, &len);
|
|
|
|
|
if (!ret || len < 1) {
|
|
|
|
|
ret = 0;
|
|
|
|
|
free (str);
|
|
|
|
|
len = -1;
|
|
|
|
|
} else if (olen) {
|
|
|
|
|
*olen = len;
|
|
|
|
|
}
|
|
|
|
|
return ret? (char *)str: NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* returns the R_ANAL_ADDR_TYPE_* of the address 'addr' */
|
2016-11-01 18:42:43 +00:00
|
|
|
|
R_API ut64 r_core_anal_address(RCore *core, ut64 addr) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
ut64 types = 0;
|
2014-10-15 00:42:08 +00:00
|
|
|
|
RRegSet *rs = NULL;
|
2016-10-20 13:02:25 +00:00
|
|
|
|
if (!core) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-10-15 00:42:08 +00:00
|
|
|
|
if (core->dbg && core->dbg->reg) {
|
|
|
|
|
rs = r_reg_regset_get (core->dbg->reg, R_REG_TYPE_GPR);
|
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
if (rs) {
|
|
|
|
|
RRegItem *r;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
r_list_foreach (rs->regs, iter, r) {
|
2016-11-17 11:04:00 +00:00
|
|
|
|
if (r->type == R_REG_TYPE_GPR) {
|
|
|
|
|
ut64 val = r_reg_getv(core->dbg->reg, r->name);
|
|
|
|
|
if (addr == val) {
|
|
|
|
|
types |= R_ANAL_ADDR_TYPE_REG;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (r_flag_get_i (core->flags, addr)) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_FLAG;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (r_anal_get_fcn_in (core->anal, addr, 0)) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_FUNC;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
// check registers
|
2016-10-20 13:02:25 +00:00
|
|
|
|
if (core->io && core->io->debug && core->dbg) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
RDebugMap *map;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
// use 'dm'
|
2015-10-01 17:23:29 +00:00
|
|
|
|
// XXX: this line makes r2 debugging MUCH slower
|
|
|
|
|
// r_debug_map_sync (core->dbg);
|
2014-10-15 00:24:22 +00:00
|
|
|
|
r_list_foreach (core->dbg->maps, iter, map) {
|
|
|
|
|
if (addr >= map->addr && addr < map->addr_end) {
|
2014-12-04 23:18:51 +00:00
|
|
|
|
if (map->name && map->name[0] == '/') {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (core->io && core->io->desc &&
|
|
|
|
|
core->io->desc->name &&
|
|
|
|
|
!strcmp (map->name,
|
|
|
|
|
core->io->desc->name)) {
|
2014-10-15 14:05:47 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_PROGRAM;
|
|
|
|
|
} else {
|
|
|
|
|
types |= R_ANAL_ADDR_TYPE_LIBRARY;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (map->perm & R_IO_EXEC) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_EXEC;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (map->perm & R_IO_READ) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_READ;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (map->perm & R_IO_WRITE) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_WRITE;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
// find function
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (map->name && strstr (map->name, "heap")) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_HEAP;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (map->name && strstr (map->name, "stack")) {
|
2014-10-15 00:24:22 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_STACK;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-11-06 22:23:43 +00:00
|
|
|
|
int _rwx = -1;
|
2014-10-15 00:24:22 +00:00
|
|
|
|
RIOSection *ios;
|
|
|
|
|
RListIter *iter;
|
2015-04-10 09:22:45 +00:00
|
|
|
|
if (core->io) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
// sections
|
|
|
|
|
r_list_foreach (core->io->sections, iter, ios) {
|
|
|
|
|
if (addr >= ios->vaddr && addr < (ios->vaddr + ios->vsize)) {
|
|
|
|
|
// sections overlap, so we want to get the one with lower perms
|
|
|
|
|
_rwx = (_rwx != -1) ? R_MIN (_rwx, ios->rwx) : ios->rwx;
|
|
|
|
|
// TODO: we should identify which maps come from the program or other
|
|
|
|
|
//types |= R_ANAL_ADDR_TYPE_PROGRAM;
|
|
|
|
|
// find function those sections should be created by hand or esil init
|
|
|
|
|
if (strstr (ios->name, "heap")) {
|
|
|
|
|
types |= R_ANAL_ADDR_TYPE_HEAP;
|
|
|
|
|
}
|
|
|
|
|
if (strstr (ios->name, "stack")) {
|
|
|
|
|
types |= R_ANAL_ADDR_TYPE_STACK;
|
|
|
|
|
}
|
2014-11-06 22:23:43 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-06 22:23:43 +00:00
|
|
|
|
if (_rwx != -1) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (_rwx & R_IO_EXEC) {
|
2014-11-06 22:23:43 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_EXEC;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (_rwx & R_IO_READ) {
|
2014-11-06 22:23:43 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_READ;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (_rwx & R_IO_WRITE) {
|
2014-11-06 22:23:43 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_WRITE;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-11-06 22:23:43 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
}
|
2014-10-24 19:31:46 +00:00
|
|
|
|
|
|
|
|
|
// check if it's ascii
|
|
|
|
|
if (addr != 0) {
|
2015-01-14 01:00:55 +00:00
|
|
|
|
int not_ascii = 0;
|
2014-10-24 19:31:46 +00:00
|
|
|
|
int i, failed_sequence, dir, on;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
|
ut8 n = (addr >> (i * 8)) & 0xff;
|
|
|
|
|
if (n && !IS_PRINTABLE (n)) {
|
|
|
|
|
not_ascii = 1;
|
|
|
|
|
}
|
2014-10-24 19:31:46 +00:00
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!not_ascii) {
|
2014-10-24 19:31:46 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_ASCII;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-10-24 19:31:46 +00:00
|
|
|
|
failed_sequence = 0;
|
|
|
|
|
dir = on = -1;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
|
ut8 n = (addr >> (i * 8)) & 0xff;
|
2014-10-24 19:31:46 +00:00
|
|
|
|
if (on != -1) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (dir == -1) {
|
|
|
|
|
dir = (n > on)? 1: -1;
|
|
|
|
|
}
|
|
|
|
|
if (n == on + dir) {
|
2014-10-24 19:31:46 +00:00
|
|
|
|
// ok
|
|
|
|
|
} else {
|
|
|
|
|
failed_sequence = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
on = n;
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!failed_sequence) {
|
2014-10-24 19:31:46 +00:00
|
|
|
|
types |= R_ANAL_ADDR_TYPE_SEQUENCE;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-10-24 19:31:46 +00:00
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 19:39:19 +00:00
|
|
|
|
/*this only autoname those function that start with fcn.* or sym.func.* */
|
|
|
|
|
R_API void r_core_anal_autoname_all_fcns(RCore *core) {
|
|
|
|
|
RListIter *it;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (core->anal->fcns, it, fcn) {
|
|
|
|
|
char *name = r_core_anal_fcn_autoname (core, fcn->addr, 0);
|
|
|
|
|
if (name && (!strncmp (fcn->name, "fcn.", 4) || !strncmp (fcn->name, "sym.func.", 9))) {
|
|
|
|
|
r_flag_rename (core->flags, r_flag_get (core->flags, fcn->name), name);
|
|
|
|
|
free (fcn->name);
|
|
|
|
|
fcn->name = name;
|
2016-03-24 23:25:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
free (name);
|
2016-03-24 19:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-02 14:48:25 +00:00
|
|
|
|
|
|
|
|
|
static bool blacklisted_word(char* name) {
|
|
|
|
|
const char * list[] = {
|
|
|
|
|
"__stack_chk_guard", "__stderrp", "__stdinp", "__stdoutp", "_DefaultRuneLocale"
|
|
|
|
|
};
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < sizeof (list) / sizeof (list[0]); i++) {
|
|
|
|
|
if (strstr (name, list[i])) { return true; }
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* suggest a name for the function at the address 'addr'.
|
2015-09-14 10:35:38 +00:00
|
|
|
|
* If dump is true, every strings associated with the function is printed */
|
2015-07-06 09:30:35 +00:00
|
|
|
|
R_API char *r_core_anal_fcn_autoname(RCore *core, ut64 addr, int dump) {
|
2014-09-12 01:17:02 +00:00
|
|
|
|
int use_getopt = 0;
|
|
|
|
|
int use_isatty = 0;
|
|
|
|
|
char *do_call = NULL;
|
2014-09-26 16:10:33 +00:00
|
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, addr, 0);
|
2014-09-12 01:17:02 +00:00
|
|
|
|
if (fcn) {
|
|
|
|
|
RAnalRef *ref;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
r_list_foreach (fcn->refs, iter, ref) {
|
|
|
|
|
RFlagItem *f = r_flag_get_i (core->flags, ref->addr);
|
|
|
|
|
if (f) {
|
2015-07-06 09:30:35 +00:00
|
|
|
|
if (dump) {
|
2016-03-24 19:39:19 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" 0x%08"PFMT64x" %s\n", ref->at, ref->addr, f->name);
|
2015-07-06 09:30:35 +00:00
|
|
|
|
}
|
2016-09-02 14:48:25 +00:00
|
|
|
|
if (blacklisted_word (f->name)) {
|
2016-12-02 19:44:32 +00:00
|
|
|
|
break;
|
2016-09-02 14:48:25 +00:00
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (strstr (f->name, ".isatty")) {
|
2014-09-12 01:17:02 +00:00
|
|
|
|
use_isatty = 1;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (strstr (f->name, ".getopt")) {
|
2014-09-12 01:17:02 +00:00
|
|
|
|
use_getopt = 1;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-09-12 01:17:02 +00:00
|
|
|
|
if (!strncmp (f->name, "sym.imp.", 8)) {
|
|
|
|
|
free (do_call);
|
|
|
|
|
do_call = strdup (f->name+8);
|
2015-12-29 11:07:33 +00:00
|
|
|
|
break;
|
2016-12-02 19:44:32 +00:00
|
|
|
|
}
|
|
|
|
|
if (!strncmp (f->name, "reloc.", 6)) {
|
2014-09-15 17:35:49 +00:00
|
|
|
|
free (do_call);
|
|
|
|
|
do_call = strdup (f->name+6);
|
2015-12-29 11:07:33 +00:00
|
|
|
|
break;
|
2014-09-12 01:17:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: append counter if name already exists
|
|
|
|
|
if (use_getopt) {
|
2015-12-21 14:53:53 +00:00
|
|
|
|
RFlagItem *item = r_flag_get (core->flags, "main");
|
2014-09-12 01:17:02 +00:00
|
|
|
|
free (do_call);
|
|
|
|
|
// if referenced from entrypoint. this should be main
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (item && item->offset == addr) {
|
2015-12-21 14:53:53 +00:00
|
|
|
|
return strdup ("main"); // main?
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-09-12 01:17:02 +00:00
|
|
|
|
return strdup ("parse_args"); // main?
|
|
|
|
|
}
|
|
|
|
|
if (use_isatty) {
|
2016-03-24 19:39:19 +00:00
|
|
|
|
char *ret = r_str_newf ("sub.setup_tty_%s_%x", do_call, addr & 0xfff);
|
2014-09-12 01:17:02 +00:00
|
|
|
|
free (do_call);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
if (do_call) {
|
2015-12-21 14:53:53 +00:00
|
|
|
|
char *ret = r_str_newf ("sub.%s_%x", do_call, addr & 0xfff);
|
2014-09-12 01:17:02 +00:00
|
|
|
|
free (do_call);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 18:42:43 +00:00
|
|
|
|
static ut64 *next_append(ut64 *next, int *nexti, ut64 v) {
|
2016-05-24 20:22:15 +00:00
|
|
|
|
ut64 *tmp_next = realloc (next, sizeof (ut64) * (1 + *nexti));
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!tmp_next) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-05-24 20:22:15 +00:00
|
|
|
|
next = tmp_next;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
next[*nexti] = v;
|
|
|
|
|
(*nexti)++;
|
|
|
|
|
return next;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-25 11:14:40 +00:00
|
|
|
|
static void r_anal_set_stringrefs(RCore *core, RAnalFunction *fcn) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalRef *ref;
|
|
|
|
|
r_list_foreach (fcn->refs, iter, ref) {
|
|
|
|
|
if (ref->type == R_ANAL_REF_TYPE_DATA &&
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_bin_is_string (core->bin, ref->addr)) {
|
2015-11-25 11:14:40 +00:00
|
|
|
|
ref->type = R_ANAL_REF_TYPE_STRING;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-11-25 11:14:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 11:49:30 +00:00
|
|
|
|
static int r_anal_try_get_fcn(RCore *core, RAnalRef *ref, int fcndepth, int refdepth) {
|
|
|
|
|
ut8 *buf;
|
2015-12-08 09:54:57 +00:00
|
|
|
|
ut16 bufsz = 1000;
|
2015-12-04 11:49:30 +00:00
|
|
|
|
RIOSection *sec;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!refdepth) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-12-04 11:49:30 +00:00
|
|
|
|
sec = r_io_section_vget (core->io, ref->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!sec) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-12-04 11:49:30 +00:00
|
|
|
|
buf = calloc (bufsz, 1);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
eprintf ("Error: malloc (buf)\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
r_io_read_at (core->io, ref->addr, buf, bufsz);
|
|
|
|
|
|
|
|
|
|
if (sec->rwx & R_IO_EXEC &&
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_anal_check_fcn (core->anal, buf, bufsz, ref->addr, sec->vaddr,
|
|
|
|
|
sec->vaddr + sec->vsize)) {
|
2015-12-04 11:49:30 +00:00
|
|
|
|
if (core->anal->limit) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (ref->addr < core->anal->limit->from ||
|
|
|
|
|
ref->addr > core->anal->limit->to) {
|
|
|
|
|
free (buf);
|
2015-12-04 11:49:30 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_core_anal_fcn (core, ref->addr, ref->at, ref->type, fcndepth - 1);
|
2015-12-04 11:49:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
ut64 offs, sz = core->anal->bits >> 3;
|
|
|
|
|
RAnalRef ref1;
|
|
|
|
|
ref1.type = R_ANAL_REF_TYPE_DATA;
|
|
|
|
|
ref1.at = ref->addr;
|
|
|
|
|
ref1.addr = 0;
|
2016-08-16 12:22:26 +00:00
|
|
|
|
ut32 i32;
|
|
|
|
|
ut16 i16;
|
|
|
|
|
ut8 i8;
|
2015-12-04 11:49:30 +00:00
|
|
|
|
for (offs = 0; offs < bufsz; offs += sz, ref1.at += sz) {
|
2016-08-16 12:22:26 +00:00
|
|
|
|
ut8* bo = buf + offs;
|
|
|
|
|
bool be = core->anal->big_endian;
|
|
|
|
|
switch (sz) {
|
|
|
|
|
case 1:
|
|
|
|
|
i8 = r_read_ble8 (bo);
|
|
|
|
|
ref1.addr = (ut64)i8;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
i16 = r_read_ble16 (bo, be);
|
|
|
|
|
ref1.addr = (ut64)i16;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
i32 = r_read_ble32 (bo, be);
|
|
|
|
|
ref1.addr = (ut64)i32;
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
ref1.addr = r_read_ble64 (bo, be);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_anal_try_get_fcn (core, &ref1, fcndepth, refdepth - 1);
|
2015-12-04 11:49:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-03 10:41:26 +00:00
|
|
|
|
static int r_anal_analyze_fcn_refs(RCore *core, RAnalFunction *fcn, int depth) {
|
2015-12-14 15:09:18 +00:00
|
|
|
|
RListIter *iter, *tmp;
|
2015-12-03 10:41:26 +00:00
|
|
|
|
RAnalRef *ref;
|
2015-12-04 11:49:30 +00:00
|
|
|
|
|
2015-12-14 15:09:18 +00:00
|
|
|
|
r_list_foreach_safe (fcn->refs, iter, tmp, ref) {
|
2015-12-03 10:41:26 +00:00
|
|
|
|
if (ref->addr != UT64_MAX) {
|
|
|
|
|
switch (ref->type) {
|
|
|
|
|
case 'd':
|
2015-12-04 11:49:30 +00:00
|
|
|
|
if (core->anal->opt.followdatarefs) {
|
|
|
|
|
r_anal_try_get_fcn (core, ref, depth, 2);
|
2015-12-03 10:41:26 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_REF_TYPE_CODE:
|
|
|
|
|
case R_ANAL_REF_TYPE_CALL:
|
|
|
|
|
r_core_anal_fcn (core, ref->addr, ref->at, ref->type, depth-1);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// TODO: fix memleak here, fcn not freed even though it is
|
|
|
|
|
// added in core->anal->fcns which is freed in r_anal_free()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-04 11:49:30 +00:00
|
|
|
|
|
2015-12-03 10:41:26 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
static int core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth) {
|
|
|
|
|
int has_next = r_config_get_i (core->config, "anal.hasnext");
|
|
|
|
|
RAnalHint *hint;
|
2015-12-13 23:43:16 +00:00
|
|
|
|
ut8 *buf = NULL;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
int i, nexti = 0;
|
|
|
|
|
ut64 *next = NULL;
|
2015-12-13 23:43:16 +00:00
|
|
|
|
int buflen, fcnlen;
|
|
|
|
|
RAnalFunction *fcn = r_anal_fcn_new ();
|
2015-07-31 17:12:16 +00:00
|
|
|
|
if (!fcn) {
|
|
|
|
|
eprintf ("Error: new (fcn)\n");
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
2016-08-01 12:46:17 +00:00
|
|
|
|
fcn->cc = r_anal_cc_default (core->anal);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
hint = r_anal_hint_get (core->anal, at);
|
|
|
|
|
if (hint && hint->bits == 16) {
|
|
|
|
|
// expand 16bit for function
|
|
|
|
|
fcn->bits = 16;
|
2016-06-09 21:34:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
fcn->bits = core->anal->bits;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
|
|
|
|
fcn->addr = at;
|
2016-05-15 12:37:22 +00:00
|
|
|
|
r_anal_fcn_set_size (fcn, 0);
|
2016-12-01 09:48:00 +00:00
|
|
|
|
RFlagItem *fi = r_flag_get_at (core->flags, at, false);
|
2016-09-18 23:18:39 +00:00
|
|
|
|
if (fi && fi->name && strncmp (fi->name, "sect", 4)) {
|
2016-09-18 22:23:06 +00:00
|
|
|
|
fcn->name = strdup (fi->name);
|
|
|
|
|
} else {
|
|
|
|
|
fcn->name = r_str_newf ("fcn.%08"PFMT64x, at);
|
|
|
|
|
}
|
2015-12-10 15:22:24 +00:00
|
|
|
|
buf = malloc (core->anal->opt.bb_max_size);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
if (!buf) {
|
|
|
|
|
eprintf ("Error: malloc (buf)\n");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
RFlagItem *f;
|
|
|
|
|
RAnalRef *ref;
|
2016-05-15 12:37:22 +00:00
|
|
|
|
int delta = r_anal_fcn_size (fcn);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
// XXX hack slow check io error
|
2016-08-01 16:41:07 +00:00
|
|
|
|
if ((buflen = r_io_read_at (core->io, at + delta, buf, 4) != 4)) {
|
2016-02-03 11:53:35 +00:00
|
|
|
|
eprintf ("read errro\n");
|
2015-07-31 17:12:16 +00:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
// real read.
|
|
|
|
|
// this is unnecessary if its contiguous
|
2015-12-10 15:22:24 +00:00
|
|
|
|
buflen = r_io_read_at (core->io, at+delta, buf, core->anal->opt.bb_max_size);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
if (core->io->va && !core->io->raw) {
|
2015-09-06 22:30:48 +00:00
|
|
|
|
if (!r_io_is_valid_offset (core->io, at+delta, !core->anal->opt.noncode)) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
break;
|
2016-08-01 16:41:07 +00:00
|
|
|
|
}
|
|
|
|
|
fcnlen = r_anal_fcn (core->anal, fcn, at + delta, buf, buflen, reftype);
|
|
|
|
|
if (core->anal->opt.searchstringrefs) {
|
|
|
|
|
r_anal_set_stringrefs (core, fcn);
|
|
|
|
|
}
|
|
|
|
|
if (fcnlen < 0) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
switch (fcnlen) {
|
|
|
|
|
case R_ANAL_RET_ERROR:
|
|
|
|
|
case R_ANAL_RET_NEW:
|
|
|
|
|
case R_ANAL_RET_DUP:
|
|
|
|
|
case R_ANAL_RET_END:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-08-01 16:41:07 +00:00
|
|
|
|
eprintf ("Oops. Negative fcnsize at 0x%08"PFMT64x" (%d)\n", at, fcnlen);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-15 20:17:59 +00:00
|
|
|
|
f = r_flag_get_i2 (core->flags, fcn->addr);
|
2017-01-23 02:09:33 +00:00
|
|
|
|
|
|
|
|
|
// XXX sometimes renaming a function here is done wrong.
|
2017-01-15 20:17:59 +00:00
|
|
|
|
#if 0
|
2017-01-11 23:14:45 +00:00
|
|
|
|
core->flags->space_strict = true;
|
2017-01-14 16:03:35 +00:00
|
|
|
|
//XXX fcn's API should handle this for us
|
2017-01-11 23:14:45 +00:00
|
|
|
|
f = r_flag_get_at (core->flags, fcn->addr, true);
|
2017-01-14 15:56:05 +00:00
|
|
|
|
if (f && f->name && strncmp (f->name, "sect", 4) &&
|
2017-01-23 02:09:33 +00:00
|
|
|
|
R_FREE (fcn->name);
|
2017-01-14 16:03:35 +00:00
|
|
|
|
strncmp (f->name, "sym.func.", 9) &&
|
|
|
|
|
strncmp (f->name, "loc", 3)) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
fcn->name = strdup (f->name);
|
2016-02-23 12:39:24 +00:00
|
|
|
|
} else {
|
2017-01-11 23:14:45 +00:00
|
|
|
|
f = r_flag_get_i2 (core->flags, fcn->addr);
|
2017-01-14 15:56:05 +00:00
|
|
|
|
if (f && f->name && strncmp (f->name, "sect", 4) &&
|
|
|
|
|
strncmp (f->name, "sym.func.", 9)) {
|
2017-01-15 20:17:59 +00:00
|
|
|
|
#else
|
|
|
|
|
if (f && f->name && strncmp (f->name, "sect", 4)) {
|
2017-01-24 13:41:39 +00:00
|
|
|
|
if (!strncmp (fcn->name, "loc.", 4)) {
|
|
|
|
|
R_FREE (fcn->name);
|
|
|
|
|
fcn->name = strdup (f->name);
|
|
|
|
|
}
|
2017-01-23 02:09:33 +00:00
|
|
|
|
if (!strncmp (fcn->name, "fcn.", 4)) {
|
|
|
|
|
R_FREE (fcn->name);
|
|
|
|
|
fcn->name = strdup (f->name);
|
|
|
|
|
}
|
2017-01-15 20:17:59 +00:00
|
|
|
|
} else {
|
2017-01-23 02:09:33 +00:00
|
|
|
|
R_FREE (fcn->name);
|
2017-01-15 20:17:59 +00:00
|
|
|
|
f = r_flag_get_i (core->flags, fcn->addr);
|
|
|
|
|
if (f && *f->name && strncmp (f->name, "sect", 4)) {
|
|
|
|
|
#endif
|
2016-02-23 12:39:24 +00:00
|
|
|
|
fcn->name = strdup (f->name);
|
|
|
|
|
} else {
|
|
|
|
|
fcn->name = r_str_newf ("fcn.%08"PFMT64x, fcn->addr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
if (fcnlen == R_ANAL_RET_ERROR ||
|
2016-05-15 12:37:22 +00:00
|
|
|
|
(fcnlen == R_ANAL_RET_END && r_anal_fcn_size (fcn) < 1)) { /* Error analyzing function */
|
2016-08-01 16:41:07 +00:00
|
|
|
|
if (core->anal->opt.followbrokenfcnsrefs) {
|
2016-12-05 17:46:45 +00:00
|
|
|
|
r_anal_analyze_fcn_refs (core, fcn, depth);
|
2016-08-01 16:41:07 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
goto error;
|
|
|
|
|
} else if (fcnlen == R_ANAL_RET_END) { /* Function analysis complete */
|
|
|
|
|
f = r_flag_get_i2 (core->flags, fcn->addr);
|
2016-10-20 11:05:58 +00:00
|
|
|
|
R_FREE (fcn->name);
|
2016-02-07 20:44:35 +00:00
|
|
|
|
if (f && f->name) { /* Check if it's already flagged */
|
2015-07-31 17:12:16 +00:00
|
|
|
|
fcn->name = strdup (f->name);
|
|
|
|
|
} else {
|
2016-09-18 22:23:06 +00:00
|
|
|
|
f = r_flag_get_i (core->flags, fcn->addr);
|
2016-09-18 23:18:39 +00:00
|
|
|
|
if (f && *f->name && strncmp (f->name, "sect", 4)) {
|
2016-09-18 22:23:06 +00:00
|
|
|
|
fcn->name = strdup (f->name);
|
|
|
|
|
} else {
|
|
|
|
|
fcn->name = r_str_newf ("%s.%08"PFMT64x,
|
2016-07-25 18:15:50 +00:00
|
|
|
|
r_anal_fcn_type_tostring (fcn->type), fcn->addr);
|
2016-09-18 22:23:06 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* Add flag */
|
|
|
|
|
r_flag_space_push (core->flags, "functions");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_flag_set (core->flags, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
|
2015-07-31 17:12:16 +00:00
|
|
|
|
r_flag_space_pop (core->flags);
|
|
|
|
|
}
|
|
|
|
|
// XXX fixes overlined function ranges wtf // fcn->addr = at;
|
|
|
|
|
/* TODO: Dupped analysis, needs more optimization */
|
|
|
|
|
fcn->depth = 256;
|
2015-09-14 10:35:38 +00:00
|
|
|
|
r_core_anal_bb (core, fcn, fcn->addr, true);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
// hack
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!fcn->depth) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
eprintf ("Analysis depth reached at 0x%08"PFMT64x"\n", fcn->addr);
|
|
|
|
|
} else {
|
|
|
|
|
fcn->depth = 256 - fcn->depth;
|
|
|
|
|
}
|
2017-01-15 20:17:59 +00:00
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* New function: Add initial xref */
|
|
|
|
|
if (from != UT64_MAX) {
|
|
|
|
|
// We shuold not use fcn->xrefs .. because that should be only via api (on top of sdb)
|
|
|
|
|
// the concepts of refs and xrefs are a bit twisted in the old implementation
|
|
|
|
|
ref = r_anal_ref_new ();
|
|
|
|
|
if (!ref) {
|
|
|
|
|
eprintf ("Error: new (xref)\n");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2016-09-09 17:28:47 +00:00
|
|
|
|
if (fcn->type == R_ANAL_FCN_TYPE_LOC) {
|
|
|
|
|
RAnalFunction *f = r_anal_get_fcn_in (core->anal, from, -1);
|
|
|
|
|
if (f) {
|
|
|
|
|
if (!f->fcn_locs) {
|
|
|
|
|
f->fcn_locs = r_anal_fcn_list_new ();
|
|
|
|
|
}
|
|
|
|
|
r_list_append (f->fcn_locs, fcn);
|
|
|
|
|
r_list_sort (f->fcn_locs, &cmpfcn);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
ref->addr = from;
|
|
|
|
|
ref->at = fcn->addr;
|
|
|
|
|
ref->type = reftype;
|
|
|
|
|
r_list_append (fcn->xrefs, ref);
|
|
|
|
|
// XXX this is creating dupped entries in the refs list with invalid reftypes, wtf?
|
|
|
|
|
r_anal_xrefs_set (core->anal, reftype, from, fcn->addr);
|
|
|
|
|
}
|
|
|
|
|
// XXX: this is wrong. See CID 1134565
|
|
|
|
|
r_anal_fcn_insert (core->anal, fcn);
|
|
|
|
|
if (has_next) {
|
2016-05-15 12:37:22 +00:00
|
|
|
|
ut64 addr = fcn->addr + r_anal_fcn_size (fcn);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
RIOSection *sect = r_io_section_vget (core->io, addr);
|
|
|
|
|
// only get next if found on an executable section
|
|
|
|
|
if (!sect || (sect && sect->rwx & 1)) {
|
|
|
|
|
for (i = 0; i < nexti; i++) {
|
2016-08-01 16:41:07 +00:00
|
|
|
|
if (next[i] == addr) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
break;
|
2016-08-01 16:41:07 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
|
|
|
|
if (i == nexti) {
|
2016-05-15 12:37:22 +00:00
|
|
|
|
ut64 at = fcn->addr + r_anal_fcn_size (fcn);
|
2016-04-21 22:03:09 +00:00
|
|
|
|
while (true) {
|
|
|
|
|
const RAnalMetaItem *mi = r_meta_find (core->anal, at, R_META_TYPE_ANY, 0);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!mi) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-04-21 22:03:09 +00:00
|
|
|
|
at += mi->size;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
// TODO: ensure next address is function after padding (nop or trap or wat)
|
|
|
|
|
// XXX noisy for test cases because we want to clear the stderr
|
|
|
|
|
r_cons_clear_line (1);
|
2016-04-21 22:03:09 +00:00
|
|
|
|
loganal (fcn->addr, at, 10000 - depth);
|
|
|
|
|
next = next_append (next, &nexti, at);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-01 16:41:07 +00:00
|
|
|
|
if (!r_anal_analyze_fcn_refs (core, fcn, depth)) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
|
|
|
|
} while (fcnlen != R_ANAL_RET_END);
|
|
|
|
|
R_FREE (buf);
|
|
|
|
|
|
|
|
|
|
if (has_next) {
|
|
|
|
|
for (i = 0; i < nexti; i++) {
|
2016-12-20 15:11:11 +00:00
|
|
|
|
if (!next[i] || r_anal_get_fcn_in (core->anal, next[i], 0)) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
r_core_anal_fcn (core, next[i], from, 0, depth - 1);
|
|
|
|
|
}
|
|
|
|
|
free (next);
|
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
free (buf);
|
|
|
|
|
// ugly hack to free fcn
|
|
|
|
|
if (fcn) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!r_anal_fcn_size (fcn) || fcn->addr == UT64_MAX) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
r_anal_fcn_free (fcn);
|
|
|
|
|
fcn = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: mark this function as not properly analyzed
|
|
|
|
|
if (!fcn->name) {
|
|
|
|
|
// XXX dupped code.
|
2016-11-01 18:42:43 +00:00
|
|
|
|
fcn->name = r_str_newf (
|
|
|
|
|
"%s.%08" PFMT64x,
|
|
|
|
|
r_anal_fcn_type_tostring (fcn->type),
|
|
|
|
|
at);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* Add flag */
|
|
|
|
|
r_flag_space_push (core->flags, "functions");
|
2016-05-15 12:37:22 +00:00
|
|
|
|
r_flag_set (core->flags, fcn->name, at, r_anal_fcn_size (fcn));
|
2015-07-31 17:12:16 +00:00
|
|
|
|
r_flag_space_pop (core->flags);
|
|
|
|
|
}
|
|
|
|
|
r_anal_fcn_insert (core->anal, fcn);
|
|
|
|
|
}
|
|
|
|
|
if (fcn && has_next) {
|
2016-05-15 12:37:22 +00:00
|
|
|
|
ut64 newaddr = fcn->addr + r_anal_fcn_size (fcn);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
RIOSection *sect = r_io_section_vget (core->io, newaddr);
|
|
|
|
|
if (!sect || (sect && (sect->rwx & 1))) {
|
|
|
|
|
next = next_append (next, &nexti, newaddr);
|
|
|
|
|
for (i = 0; i < nexti; i++) {
|
|
|
|
|
if (!next[i]) continue;
|
|
|
|
|
#if HASNEXT_FOREVER
|
|
|
|
|
r_core_anal_fcn (core, next[i], next[i], 0, 9999);
|
|
|
|
|
#else
|
|
|
|
|
r_core_anal_fcn (core, next[i], next[i], 0, depth - 1);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
free (next);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* decode and return the RANalOp at the address addr */
|
2016-11-01 18:42:43 +00:00
|
|
|
|
R_API RAnalOp* r_core_anal_op(RCore *core, ut64 addr) {
|
2014-09-12 00:37:49 +00:00
|
|
|
|
int len;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
RAnalOp *op;
|
2015-08-13 20:14:43 +00:00
|
|
|
|
ut8 buf[128];
|
2015-07-31 17:12:16 +00:00
|
|
|
|
ut8 *ptr;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
RAsmOp asmop;
|
|
|
|
|
|
2015-08-13 15:24:45 +00:00
|
|
|
|
op = R_NEW0 (RAnalOp);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!op) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
if (addr >= core->offset && addr + 16 < core->offset + core->blocksize) {
|
2014-09-12 00:37:49 +00:00
|
|
|
|
int delta = (addr - core->offset);
|
|
|
|
|
ptr = core->block + delta;
|
|
|
|
|
len = core->blocksize - delta;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (len < 1) {
|
|
|
|
|
goto err_op;
|
|
|
|
|
}
|
2014-09-12 00:37:49 +00:00
|
|
|
|
} else {
|
2015-08-13 15:24:45 +00:00
|
|
|
|
if (r_io_read_at (core->io, addr, buf, sizeof (buf)) < 1) {
|
2015-08-13 20:14:43 +00:00
|
|
|
|
goto err_op;
|
2015-08-13 15:24:45 +00:00
|
|
|
|
}
|
2014-09-12 00:37:49 +00:00
|
|
|
|
ptr = buf;
|
|
|
|
|
len = sizeof (buf);
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (r_anal_op (core->anal, op, addr, ptr, len) < 1) {
|
|
|
|
|
goto err_op;
|
|
|
|
|
}
|
2015-08-13 20:14:43 +00:00
|
|
|
|
|
2014-10-14 10:29:34 +00:00
|
|
|
|
// decode instruction here
|
2015-07-31 10:40:04 +00:00
|
|
|
|
r_asm_set_pc (core->assembler, addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (r_asm_disassemble (core->assembler, &asmop, ptr, len) > 0) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
op->mnemonic = strdup (asmop.buf_asm);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
return op;
|
2015-08-13 20:14:43 +00:00
|
|
|
|
|
|
|
|
|
err_op:
|
|
|
|
|
free (op);
|
|
|
|
|
return NULL;
|
2013-11-03 12:47:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 09:50:35 +00:00
|
|
|
|
static void print_hint_h_format(RAnalHint* hint) {
|
2016-11-01 18:47:34 +00:00
|
|
|
|
r_cons_printf (" 0x%08"PFMT64x" - 0x%08"PFMT64x" =>", hint->addr, hint->addr + hint->size);
|
2016-05-31 09:50:35 +00:00
|
|
|
|
HINTCMD (hint, arch, " arch='%s'", false);
|
|
|
|
|
HINTCMD (hint, bits, " bits=%d", false);
|
|
|
|
|
HINTCMD (hint, size, " size=%d", false);
|
|
|
|
|
HINTCMD (hint, opcode, " opcode='%s'", false);
|
|
|
|
|
HINTCMD (hint, syntax, " syntax='%s'", false);
|
|
|
|
|
HINTCMD (hint, immbase, " immbase=%d", false);
|
|
|
|
|
HINTCMD (hint, esil, " esil='%s'", false);
|
|
|
|
|
r_cons_newline ();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-11 09:53:44 +00:00
|
|
|
|
static int cb(void *p, const char *k, const char *v) {
|
2014-03-11 01:47:10 +00:00
|
|
|
|
HintListState *hls = p;
|
2017-01-11 11:23:48 +00:00
|
|
|
|
RAnalHint *hint = r_anal_hint_from_string (hls->a, sdb_atoi (k + 5), v);
|
2014-03-11 01:47:10 +00:00
|
|
|
|
switch (hls->mode) {
|
2014-03-11 09:53:44 +00:00
|
|
|
|
case 's':
|
|
|
|
|
r_cons_printf ("%s=%s\n", k, v);
|
2017-01-11 11:23:48 +00:00
|
|
|
|
break;
|
2014-03-11 01:47:10 +00:00
|
|
|
|
case '*':
|
2015-07-31 10:40:04 +00:00
|
|
|
|
HINTCMD_ADDR (hint, arch, "aha %s");
|
|
|
|
|
HINTCMD_ADDR (hint, bits, "ahb %d");
|
2015-12-10 22:42:15 +00:00
|
|
|
|
HINTCMD_ADDR (hint, size, "ahs %d");
|
2015-07-31 10:40:04 +00:00
|
|
|
|
HINTCMD_ADDR (hint, opcode, "aho %s");
|
2015-12-10 22:42:15 +00:00
|
|
|
|
HINTCMD_ADDR (hint, syntax, "ahS %s");
|
|
|
|
|
HINTCMD_ADDR (hint, immbase, "ahi %d");
|
|
|
|
|
HINTCMD_ADDR (hint, esil, "ahe %s");
|
2014-03-11 01:47:10 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
2014-04-29 01:36:04 +00:00
|
|
|
|
r_cons_printf ("%s{\"from\":%"PFMT64d",\"to\":%"PFMT64d,
|
2014-03-11 01:47:10 +00:00
|
|
|
|
hls->count>0?",":"", hint->addr, hint->addr+hint->size);
|
2015-12-10 22:42:15 +00:00
|
|
|
|
HINTCMD (hint, arch, ",\"arch\":\"%s\"", true); // XXX: arch must not contain strange chars
|
|
|
|
|
HINTCMD (hint, bits, ",\"bits\":%d", true);
|
|
|
|
|
HINTCMD (hint, size, ",\"size\":%d", true);
|
|
|
|
|
HINTCMD (hint, opcode, ",\"opcode\":\"%s\"", true);
|
|
|
|
|
HINTCMD (hint, syntax, ",\"syntax\":\"%s\"", true);
|
|
|
|
|
HINTCMD (hint, immbase, ",\"immbase\":%d", true);
|
|
|
|
|
HINTCMD (hint, esil, ",\"esil\":\"%s\"", true);
|
|
|
|
|
HINTCMD (hint, ptr, ",\"ptr\":\"0x%"PFMT64x"x\"", true);
|
2016-09-22 21:57:16 +00:00
|
|
|
|
r_cons_print ("}");
|
2014-03-11 01:47:10 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-09-22 21:57:16 +00:00
|
|
|
|
print_hint_h_format (hint);
|
2016-05-31 09:50:35 +00:00
|
|
|
|
break;
|
2014-03-11 01:47:10 +00:00
|
|
|
|
}
|
2014-04-29 22:06:40 +00:00
|
|
|
|
free (hint);
|
2014-03-11 01:47:10 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 18:47:34 +00:00
|
|
|
|
R_API void r_core_anal_hint_print(RAnal* a, ut64 addr, int mode) {
|
2016-11-01 17:53:09 +00:00
|
|
|
|
RAnalHint *hint = r_anal_hint_get (a, addr);
|
2016-10-05 13:59:41 +00:00
|
|
|
|
if (!hint) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-01 18:47:34 +00:00
|
|
|
|
if (mode == '*') {
|
|
|
|
|
HINTCMD_ADDR (hint, arch, "aha %s");
|
|
|
|
|
HINTCMD_ADDR (hint, bits, "ahb %d");
|
|
|
|
|
HINTCMD_ADDR (hint, size, "ahs %d");
|
|
|
|
|
HINTCMD_ADDR (hint, opcode, "aho %s");
|
|
|
|
|
HINTCMD_ADDR (hint, syntax, "ahS %s");
|
|
|
|
|
HINTCMD_ADDR (hint, immbase, "ahi %d");
|
|
|
|
|
HINTCMD_ADDR (hint, esil, "ahe %s");
|
|
|
|
|
} else {
|
|
|
|
|
print_hint_h_format (hint);
|
|
|
|
|
}
|
2016-11-01 17:53:09 +00:00
|
|
|
|
free (hint);
|
2016-05-31 09:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 18:47:34 +00:00
|
|
|
|
R_API void r_core_anal_hint_list(RAnal *a, int mode) {
|
2014-03-11 01:47:10 +00:00
|
|
|
|
HintListState hls = {};
|
|
|
|
|
hls.mode = mode;
|
|
|
|
|
hls.count = 0;
|
2014-03-11 09:53:44 +00:00
|
|
|
|
hls.a = a;
|
2016-10-05 13:59:41 +00:00
|
|
|
|
if (mode == 'j') {
|
|
|
|
|
r_cons_strcat ("[");
|
|
|
|
|
}
|
2017-01-11 11:23:48 +00:00
|
|
|
|
#if 0
|
2014-03-11 01:47:10 +00:00
|
|
|
|
sdb_foreach (a->sdb_hints, cb, &hls);
|
2017-01-11 11:23:48 +00:00
|
|
|
|
#else
|
|
|
|
|
SdbList *ls = sdb_foreach_list (a->sdb_hints, true);
|
|
|
|
|
SdbListIter *lsi;
|
|
|
|
|
SdbKv *kv;
|
|
|
|
|
ls_foreach (ls, lsi, kv) {
|
|
|
|
|
cb (&hls, kv->key, kv->value);
|
|
|
|
|
}
|
|
|
|
|
ls_free (ls);
|
|
|
|
|
#endif
|
2016-10-05 13:59:41 +00:00
|
|
|
|
if (mode == 'j') {
|
|
|
|
|
r_cons_strcat ("]\n");
|
|
|
|
|
}
|
2013-01-22 17:08:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 10:40:04 +00:00
|
|
|
|
static char *core_anal_graph_label(RCore *core, RAnalBlock *bb, int opts) {
|
|
|
|
|
int is_html = r_cons_singleton ()->is_html;
|
|
|
|
|
int is_json = opts & R_CORE_ANAL_JSON;
|
2010-03-15 18:47:26 +00:00
|
|
|
|
char cmd[1024], file[1024], *cmdstr = NULL, *filestr = NULL, *str = NULL;
|
2014-07-18 13:18:09 +00:00
|
|
|
|
int line = 0, oline = 0, idx = 0;
|
2011-11-15 08:56:22 +00:00
|
|
|
|
ut64 at;
|
2010-02-27 10:56:41 +00:00
|
|
|
|
|
2010-05-24 16:35:08 +00:00
|
|
|
|
if (opts & R_CORE_ANAL_GRAPHLINES) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
#if R_ANAL_BB_HAS_OPS
|
2011-11-15 08:56:22 +00:00
|
|
|
|
RAnalOp *opi;
|
|
|
|
|
RListIter *iter;
|
2011-02-24 13:06:49 +00:00
|
|
|
|
r_list_foreach (bb->ops, iter, opi) {
|
2016-09-22 21:57:16 +00:00
|
|
|
|
r_bin_addr2line (core->bin, opi->addr, file, sizeof (file) - 1, &line);
|
2011-11-15 08:56:22 +00:00
|
|
|
|
#else
|
2016-03-30 23:16:19 +00:00
|
|
|
|
for (at = bb->addr; at < bb->addr + bb->size; at += 2) {
|
2016-09-22 21:57:16 +00:00
|
|
|
|
r_bin_addr2line (core->bin, at, file, sizeof (file) - 1, &line);
|
2011-11-15 08:56:22 +00:00
|
|
|
|
#endif
|
2010-03-15 18:47:26 +00:00
|
|
|
|
if (line != 0 && line != oline && strcmp (file, "??")) {
|
|
|
|
|
filestr = r_file_slurp_line (file, line, 0);
|
|
|
|
|
if (filestr) {
|
2014-07-20 23:16:54 +00:00
|
|
|
|
int flen = strlen (filestr);
|
|
|
|
|
cmdstr = realloc (cmdstr, idx + flen + 8);
|
|
|
|
|
memcpy (cmdstr + idx, filestr, flen);
|
|
|
|
|
idx += flen;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
if (is_json) {
|
2016-03-30 23:16:19 +00:00
|
|
|
|
strcpy (cmdstr + idx, "\\n");
|
2016-09-22 21:57:16 +00:00
|
|
|
|
idx += 2;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else if (is_html) {
|
2016-03-30 23:16:19 +00:00
|
|
|
|
strcpy (cmdstr + idx, "<br />");
|
2016-09-22 21:57:16 +00:00
|
|
|
|
idx += 6;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else {
|
2016-03-30 23:16:19 +00:00
|
|
|
|
strcpy (cmdstr + idx, "\\l");
|
2016-09-22 21:57:16 +00:00
|
|
|
|
idx += 2;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
2010-03-15 18:47:26 +00:00
|
|
|
|
free (filestr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
oline = line;
|
|
|
|
|
}
|
2010-05-24 16:35:08 +00:00
|
|
|
|
} else if (opts & R_CORE_ANAL_GRAPHBODY) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
snprintf (cmd, sizeof (cmd),
|
|
|
|
|
"pD %d @e:asm.comments=0 @ 0x%08" PFMT64x, bb->size,
|
|
|
|
|
bb->addr);
|
2010-03-24 00:24:27 +00:00
|
|
|
|
cmdstr = r_core_cmd_str (core, cmd);
|
2010-03-15 18:47:26 +00:00
|
|
|
|
}
|
|
|
|
|
if (cmdstr) {
|
2014-07-02 00:15:30 +00:00
|
|
|
|
str = r_str_escape_dot (cmdstr);
|
2010-02-27 10:56:41 +00:00
|
|
|
|
free (cmdstr);
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 00:03:44 +00:00
|
|
|
|
static char *palColorFor(const char *k) {
|
|
|
|
|
RCons *cons = r_cons_singleton ();
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!cons) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-31 00:03:44 +00:00
|
|
|
|
const char *c = r_cons_pal_get (k);
|
|
|
|
|
if (c) {
|
|
|
|
|
ut8 r = 0, g = 0, b = 0;
|
|
|
|
|
r_cons_rgb_parse (c, &r, &g, &b, NULL);
|
|
|
|
|
return r_cons_rgb_tostring (r, g, b);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 17:23:24 +00:00
|
|
|
|
static void core_anal_color_curr_node(RCore *core, struct r_anal_bb_t *bbi) {
|
|
|
|
|
bool color_current = r_config_get_i (core->config, "graph.gv.current");
|
|
|
|
|
char *pal_curr = palColorFor ("graph.current");
|
|
|
|
|
bool current = r_anal_bb_is_in_offset (bbi, core->offset);
|
|
|
|
|
|
|
|
|
|
if (current && color_current) {
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" ", bbi->addr);
|
|
|
|
|
r_cons_printf ("\t[fillcolor=%s style=filled shape=box];\n", pal_curr);
|
|
|
|
|
}
|
2016-11-29 10:52:24 +00:00
|
|
|
|
free (pal_curr);
|
2016-11-25 17:23:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 10:28:44 +00:00
|
|
|
|
static int core_anal_graph_nodes(RCore *core, RAnalFunction *fcn, int opts) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
int is_html = r_cons_singleton ()->is_html;
|
|
|
|
|
int is_json = opts & R_CORE_ANAL_JSON;
|
|
|
|
|
int is_keva = opts & R_CORE_ANAL_KEYVALUE;
|
2011-02-11 15:56:38 +00:00
|
|
|
|
struct r_anal_bb_t *bbi;
|
|
|
|
|
RListIter *iter;
|
2012-09-05 01:25:03 +00:00
|
|
|
|
int left = 300;
|
2013-01-24 02:48:24 +00:00
|
|
|
|
int count = 0;
|
2016-03-30 10:28:44 +00:00
|
|
|
|
int nodes = 0;
|
2013-02-07 08:41:05 +00:00
|
|
|
|
int top = 0;
|
|
|
|
|
char *str;
|
2014-04-27 00:48:42 +00:00
|
|
|
|
Sdb *DB = NULL;
|
2016-03-31 00:03:44 +00:00
|
|
|
|
char *pal_jump = palColorFor ("graph.true");
|
|
|
|
|
char *pal_fail = palColorFor ("graph.false");
|
|
|
|
|
char *pal_trfa = palColorFor ("graph.trufae");
|
2016-05-03 22:50:33 +00:00
|
|
|
|
char *pal_curr = palColorFor ("graph.current");
|
|
|
|
|
char *pal_traced = palColorFor ("graph.traced");
|
|
|
|
|
char *pal_box4 = palColorFor ("graph.box4");
|
|
|
|
|
bool color_current = r_config_get_i (core->config, "graph.gv.current");
|
2010-03-01 19:13:36 +00:00
|
|
|
|
|
2014-04-27 00:48:42 +00:00
|
|
|
|
if (is_keva) {
|
|
|
|
|
char ns[64];
|
2014-06-05 22:06:30 +00:00
|
|
|
|
DB = sdb_ns (core->anal->sdb, "graph", 1);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
snprintf (ns, sizeof (ns), "fcn.0x%08"PFMT64x, fcn->addr);
|
2014-06-05 22:06:30 +00:00
|
|
|
|
DB = sdb_ns (DB, ns, 1);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_keva) {
|
|
|
|
|
char *ename = sdb_encode ((const ut8*)fcn->name, -1);
|
|
|
|
|
sdb_set (DB, "name", fcn->name, 0);
|
|
|
|
|
sdb_set (DB, "ename", ename, 0);
|
|
|
|
|
free (ename);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->nargs > 0) {
|
2014-04-27 00:48:42 +00:00
|
|
|
|
sdb_num_set (DB, "nargs", fcn->nargs, 0);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-05-15 12:37:22 +00:00
|
|
|
|
sdb_num_set (DB, "size", r_anal_fcn_size (fcn), 0);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->stack > 0) {
|
2014-04-27 00:48:42 +00:00
|
|
|
|
sdb_num_set (DB, "stack", fcn->stack, 0);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-04-27 00:48:42 +00:00
|
|
|
|
sdb_set (DB, "pos", "0,0", 0); // needs to run layout
|
|
|
|
|
sdb_set (DB, "type", r_anal_fcn_type_tostring (fcn->type), 0);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else if (is_json) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
// TODO: show vars, refs and xrefs
|
|
|
|
|
r_cons_printf ("{\"name\":\"%s\"", fcn->name);
|
|
|
|
|
r_cons_printf (",\"offset\":%"PFMT64d, fcn->addr);
|
|
|
|
|
r_cons_printf (",\"ninstr\":%"PFMT64d, fcn->ninstr);
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_cons_printf (",\"nargs\":%d",
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 1) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 1) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 1));
|
|
|
|
|
r_cons_printf (",\"nlocals\":%d",
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 0) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 0) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 0));
|
2016-05-15 12:37:22 +00:00
|
|
|
|
r_cons_printf (",\"size\":%d", r_anal_fcn_size (fcn));
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf (",\"stack\":%d", fcn->stack);
|
|
|
|
|
r_cons_printf (",\"type\":%d", fcn->type); // TODO: output string
|
|
|
|
|
//r_cons_printf (",\"cc\":%d", fcn->call); // TODO: calling convention
|
|
|
|
|
if (fcn->dsc) r_cons_printf (",\"signature\":\"%s\"", fcn->dsc);
|
|
|
|
|
r_cons_printf (",\"blocks\":[");
|
|
|
|
|
}
|
2011-02-04 13:03:59 +00:00
|
|
|
|
r_list_foreach (fcn->bbs, iter, bbi) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
count ++;
|
2014-04-27 00:48:42 +00:00
|
|
|
|
if (is_keva) {
|
|
|
|
|
char key[128];
|
2014-04-27 00:55:18 +00:00
|
|
|
|
sdb_array_push_num (DB, "bbs", bbi->addr, 0);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
snprintf (key, sizeof (key), "bb.0x%08"PFMT64x".size", bbi->addr);
|
|
|
|
|
sdb_num_set (DB, key, bbi->size, 0); // bb.<addr>.size=<num>
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else if (is_json) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
RDebugTracepoint *t = r_debug_trace_get (core->dbg, bbi->addr);
|
|
|
|
|
ut8 *buf = malloc (bbi->size);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (count > 1) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf (",");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf ("{\"offset\":%"PFMT64d",\"size\":%"PFMT64d, bbi->addr, bbi->size);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (bbi->jump != UT64_MAX) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf (",\"jump\":%"PFMT64d, bbi->jump);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (bbi->fail != -1) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf (",\"fail\":%"PFMT64d, bbi->fail);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
if (t) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_cons_printf (
|
|
|
|
|
",\"trace\":{\"count\":%d,\"times\":%"
|
|
|
|
|
"d}",
|
|
|
|
|
t->count, t->times);
|
2015-01-31 18:24:18 +00:00
|
|
|
|
}
|
2017-02-05 00:24:50 +00:00
|
|
|
|
r_cons_printf (",\"ops\":[");
|
2015-07-31 10:40:04 +00:00
|
|
|
|
if (buf) {
|
|
|
|
|
r_io_read_at (core->io, bbi->addr, buf, bbi->size);
|
|
|
|
|
r_core_print_disasm_json (core, bbi->addr, buf, bbi->size, 0);
|
|
|
|
|
free (buf);
|
2016-09-22 21:57:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
eprintf ("cannot allocate %d bytes\n", bbi->size);
|
|
|
|
|
}
|
2017-02-05 00:24:50 +00:00
|
|
|
|
r_cons_printf ("]}");
|
2013-01-24 02:48:24 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-04-27 00:48:42 +00:00
|
|
|
|
if (bbi->jump != UT64_MAX) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
2014-04-27 00:48:42 +00:00
|
|
|
|
if (is_keva) {
|
|
|
|
|
char key[128];
|
|
|
|
|
char val[128];
|
|
|
|
|
snprintf (key, sizeof (key), "bb.0x%08"PFMT64x".to", bbi->addr);
|
|
|
|
|
if (bbi->fail != UT64_MAX) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
snprintf (val, sizeof (val), "0x%08"PFMT64x, bbi->jump);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
} else {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
snprintf (val, sizeof (val), "0x%08"PFMT64x ",0x%08"PFMT64x,
|
2015-07-31 10:40:04 +00:00
|
|
|
|
bbi->jump, bbi->fail);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
}
|
|
|
|
|
// bb.<addr>.to=<jump>,<fail>
|
|
|
|
|
sdb_set (DB, key, val, 0);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else if (is_html) {
|
2012-09-05 01:25:03 +00:00
|
|
|
|
r_cons_printf ("<div class=\"connector _0x%08"PFMT64x" _0x%08"PFMT64x"\">\n"
|
2012-09-21 01:05:00 +00:00
|
|
|
|
" <img class=\"connector-end\" src=\"img/arrow.gif\" /></div>\n",
|
2015-07-31 10:40:04 +00:00
|
|
|
|
bbi->addr, bbi->jump);
|
|
|
|
|
} else if (!is_json) {
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf ("\t\"0x%08"PFMT64x"_0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"_0x%08"PFMT64x"\" "
|
|
|
|
|
// "[color=\"%s\"];\n", fcn->addr, bbi->addr, fcn->addr, bbi->jump,
|
|
|
|
|
// bbi->fail != -1 ? "green" : "blue");
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
|
|
|
|
|
"[color=\"%s\"];\n", bbi->addr, bbi->jump,
|
2016-03-31 00:03:44 +00:00
|
|
|
|
bbi->fail != -1 ? pal_jump : pal_trfa);
|
2016-11-25 17:23:24 +00:00
|
|
|
|
core_anal_color_curr_node (core, bbi);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
2011-02-04 13:03:59 +00:00
|
|
|
|
}
|
2014-04-27 00:48:42 +00:00
|
|
|
|
if (bbi->fail != -1) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
2012-09-05 01:25:03 +00:00
|
|
|
|
if (is_html) {
|
|
|
|
|
r_cons_printf ("<div class=\"connector _0x%08"PFMT64x" _0x%08"PFMT64x"\">\n"
|
2013-01-24 02:48:24 +00:00
|
|
|
|
" <img class=\"connector-end\" src=\"img/arrow.gif\"/></div>\n",
|
2015-07-31 10:40:04 +00:00
|
|
|
|
bbi->addr, bbi->fail);
|
|
|
|
|
} else if (!is_keva) {
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf ("\t\"0x%08"PFMT64x"_0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"_0x%08"PFMT64x"\" "
|
|
|
|
|
// "[color=\"red\"];\n", fcn->addr, bbi->addr, fcn->addr, bbi->fail);
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
|
2016-03-31 00:03:44 +00:00
|
|
|
|
"[color=\"%s\"];\n", bbi->addr, bbi->fail, pal_fail);
|
2016-11-25 17:23:24 +00:00
|
|
|
|
core_anal_color_curr_node (core, bbi);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
2011-02-04 13:03:59 +00:00
|
|
|
|
}
|
2014-04-03 19:02:52 +00:00
|
|
|
|
if (bbi->switch_op) {
|
|
|
|
|
RAnalCaseOp *caseop;
|
|
|
|
|
RListIter *iter;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
|
2014-04-03 19:02:52 +00:00
|
|
|
|
if (is_html) {
|
|
|
|
|
r_cons_printf ("<div class=\"connector _0x%08"PFMT64x" _0x%08"PFMT64x"\">\n"
|
|
|
|
|
" <img class=\"connector-end\" src=\"img/arrow.gif\"/></div>\n",
|
2015-07-31 10:40:04 +00:00
|
|
|
|
bbi->addr, bbi->fail);
|
|
|
|
|
} else if (!is_keva) {
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf ("\t\"0x%08"PFMT64x"_0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"_0x%08"PFMT64x"\" "
|
|
|
|
|
// "[color=\"red\"];\n", fcn->addr, bbi->addr, fcn->addr, bbi->fail);
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
|
2016-03-31 00:03:44 +00:00
|
|
|
|
"[color=\"%s\"];\n", pal_fail, bbi->addr, bbi->fail);
|
2016-11-25 17:23:24 +00:00
|
|
|
|
core_anal_color_curr_node (core, bbi);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
2014-04-03 19:02:52 +00:00
|
|
|
|
|
|
|
|
|
r_list_foreach (bbi->switch_op->cases, iter, caseop) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
|
|
|
|
if (is_keva) {
|
|
|
|
|
char key[128];
|
|
|
|
|
snprintf (key, sizeof (key),
|
|
|
|
|
"bb.0x%08"PFMT64x".switch.%"PFMT64d,
|
|
|
|
|
bbi->addr, caseop->value);
|
|
|
|
|
sdb_num_set (DB, key, caseop->jump, 0);
|
|
|
|
|
snprintf (key, sizeof (key),
|
|
|
|
|
"bb.0x%08"PFMT64x".switch", bbi->addr);
|
|
|
|
|
sdb_array_add_num (DB, key, caseop->value, 0);
|
|
|
|
|
} else if (is_html) {
|
|
|
|
|
r_cons_printf ("<div class=\"connector _0x%08"PFMT64x" _0x%08"PFMT64x"\">\n"
|
|
|
|
|
" <img class=\"connector-end\" src=\"img/arrow.gif\"/></div>\n",
|
|
|
|
|
caseop->addr, caseop->jump);
|
|
|
|
|
} else {
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf ("\t\"0x%08"PFMT64x"_0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"_0x%08"PFMT64x"\" "
|
|
|
|
|
// "[color=\"red\"];\n", fcn->addr, caseop->addr, fcn->addr, caseop->jump);
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
|
2016-11-25 17:23:24 +00:00
|
|
|
|
"[color2=\"%s\"];\n", pal_fail, caseop->addr, caseop->jump);
|
|
|
|
|
core_anal_color_curr_node (core, bbi);
|
2014-04-03 19:02:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
if ((str = core_anal_graph_label (core, bbi, opts))) {
|
2011-02-04 13:03:59 +00:00
|
|
|
|
if (opts & R_CORE_ANAL_GRAPHDIFF) {
|
2013-10-23 22:36:07 +00:00
|
|
|
|
const char *difftype = bbi->diff? (\
|
|
|
|
|
bbi->diff->type==R_ANAL_DIFF_TYPE_MATCH? "lightgray":
|
2016-09-22 21:57:16 +00:00
|
|
|
|
bbi->diff->type==R_ANAL_DIFF_TYPE_UNMATCH? "yellow": "red"): "gray";
|
2014-04-27 00:48:42 +00:00
|
|
|
|
const char *diffname = bbi->diff? (\
|
|
|
|
|
bbi->diff->type==R_ANAL_DIFF_TYPE_MATCH? "match":
|
|
|
|
|
bbi->diff->type==R_ANAL_DIFF_TYPE_UNMATCH? "unmatch": "new"): "unk";
|
|
|
|
|
if (is_keva) {
|
|
|
|
|
sdb_set (DB, "diff", diffname, 0);
|
|
|
|
|
sdb_set (DB, "label", str, 0);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else if (!is_json) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf (" \"0x%08"PFMT64x"_0x%08"PFMT64x"\" [color=\"%s\","
|
|
|
|
|
// " label=\"%s\", URL=\"%s/0x%08"PFMT64x"\"]\n",
|
|
|
|
|
// fcn->addr, bbi->addr, difftype, str, fcn->name, bbi->addr);
|
2016-09-22 21:57:16 +00:00
|
|
|
|
r_cons_printf (" \"0x%08"PFMT64x"\" [fillcolor=\"%s\","
|
2014-04-27 00:48:42 +00:00
|
|
|
|
" label=\"%s\", URL=\"%s/0x%08"PFMT64x"\"]\n",
|
2016-03-30 22:16:33 +00:00
|
|
|
|
bbi->addr, difftype, str, fcn->name, bbi->addr);
|
2014-04-27 00:48:42 +00:00
|
|
|
|
}
|
2011-02-04 13:03:59 +00:00
|
|
|
|
} else {
|
2012-09-05 01:25:03 +00:00
|
|
|
|
if (is_html) {
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
2014-04-27 00:48:42 +00:00
|
|
|
|
r_cons_printf ("<p class=\"block draggable\" style=\""
|
|
|
|
|
"top: %dpx; left: %dpx; width: 400px;\" id=\""
|
|
|
|
|
"_0x%08"PFMT64x"\">\n%s</p>\n",
|
|
|
|
|
top, left, bbi->addr, str);
|
2012-09-05 01:25:03 +00:00
|
|
|
|
left = left? 0: 600;
|
|
|
|
|
if (!left) top += 250;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else if (!is_json && !is_keva) {
|
2016-05-17 15:35:23 +00:00
|
|
|
|
bool current = r_anal_bb_is_in_offset (bbi, core->offset);
|
|
|
|
|
const char *label_color = bbi->traced
|
|
|
|
|
? pal_traced
|
|
|
|
|
: (current && color_current)
|
|
|
|
|
? pal_curr
|
|
|
|
|
: pal_box4;
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes++;
|
2016-03-30 22:16:33 +00:00
|
|
|
|
//r_cons_printf (" \"0x%08"PFMT64x"_0x%08"PFMT64x"\" ["
|
|
|
|
|
// "URL=\"%s/0x%08"PFMT64x"\", color=\"%s\", label=\"%s\"]\n",
|
|
|
|
|
// fcn->addr, bbi->addr,
|
|
|
|
|
// fcn->name, bbi->addr,
|
|
|
|
|
// bbi->traced?"yellow":"lightgray", str);
|
2016-05-03 22:50:33 +00:00
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" ["
|
2016-05-17 15:35:23 +00:00
|
|
|
|
"URL=\"%s/0x%08"PFMT64x"\", fillcolor=\"%s\", color=\"%s\", label=\"%s\"]\n",
|
2016-03-30 22:16:33 +00:00
|
|
|
|
bbi->addr, fcn->name, bbi->addr,
|
2016-05-17 15:35:23 +00:00
|
|
|
|
current? "yellow": "lightgray", label_color, str);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
2010-03-01 19:13:36 +00:00
|
|
|
|
}
|
2011-02-04 13:03:59 +00:00
|
|
|
|
free (str);
|
2010-03-01 19:13:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-22 16:32:18 +00:00
|
|
|
|
if (is_json) {
|
2016-09-22 21:57:16 +00:00
|
|
|
|
r_cons_print ("]}");
|
2016-08-22 16:32:18 +00:00
|
|
|
|
}
|
2016-03-31 00:25:54 +00:00
|
|
|
|
free (pal_jump);
|
|
|
|
|
free (pal_fail);
|
|
|
|
|
free (pal_trfa);
|
2016-06-25 17:43:05 +00:00
|
|
|
|
free (pal_curr);
|
|
|
|
|
free (pal_traced);
|
|
|
|
|
free (pal_box4);
|
2016-03-30 10:28:44 +00:00
|
|
|
|
return nodes;
|
2010-03-01 19:13:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* analyze a RAnalBlock at the address at and add that to the fcn function. */
|
2012-07-22 08:00:35 +00:00
|
|
|
|
R_API int r_core_anal_bb(RCore *core, RAnalFunction *fcn, ut64 at, int head) {
|
2016-08-21 09:39:37 +00:00
|
|
|
|
RAnalBlock *bb, *bbi;
|
2010-06-02 17:17:47 +00:00
|
|
|
|
RListIter *iter;
|
2010-02-27 18:12:06 +00:00
|
|
|
|
ut64 jump, fail;
|
2011-12-16 15:33:06 +00:00
|
|
|
|
ut8 *buf = NULL;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
int buflen, bblen = 0, rc = true;
|
|
|
|
|
int ret = R_ANAL_RET_NEW;
|
2010-02-26 20:00:03 +00:00
|
|
|
|
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (--fcn->depth <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
|
|
|
|
|
bb = r_anal_bb_new ();
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!bb) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
|
|
|
|
|
if (core->anal->split) {
|
2015-03-16 01:52:26 +00:00
|
|
|
|
ret = r_anal_fcn_split_bb (core->anal, fcn, bb, at);
|
|
|
|
|
} else {
|
|
|
|
|
r_list_foreach (fcn->bbs, iter, bbi) {
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (at == bbi->addr) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
ret = R_ANAL_RET_DUP;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2015-03-16 01:52:26 +00:00
|
|
|
|
}
|
2011-11-13 23:21:25 +00:00
|
|
|
|
}
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (ret == R_ANAL_RET_DUP) {
|
|
|
|
|
/* Dupped basic block */
|
2011-12-16 15:33:06 +00:00
|
|
|
|
goto error;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
|
2015-01-31 11:48:15 +00:00
|
|
|
|
if (ret == R_ANAL_RET_NEW) { /* New bb */
|
2012-08-31 09:45:06 +00:00
|
|
|
|
// XXX: use static buffer size of 512 or so
|
2015-12-10 15:22:24 +00:00
|
|
|
|
buf = malloc (core->anal->opt.bb_max_size);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (!buf) {
|
2011-12-16 15:33:06 +00:00
|
|
|
|
goto error;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2010-02-28 19:07:36 +00:00
|
|
|
|
do {
|
2016-08-21 09:39:37 +00:00
|
|
|
|
#if SLOW_IO
|
|
|
|
|
if (r_io_read_at (core->io, at + bblen, buf, 4) != 4) { // ETOOSLOW
|
2011-12-16 15:33:06 +00:00
|
|
|
|
goto error;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2016-08-22 16:32:18 +00:00
|
|
|
|
r_core_read_at (core, at + bblen, buf, core->anal->opt.bb_max_size);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
#else
|
2016-08-22 16:32:18 +00:00
|
|
|
|
if (r_io_read_at (core->io, at + bblen, buf, core->anal->opt.bb_max_size) != core->anal->opt.bb_max_size) { // ETOOSLOW
|
2016-08-21 09:39:37 +00:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (!r_io_is_valid_offset (core->io, at + bblen, !core->anal->opt.noncode)) {
|
2012-08-31 14:30:41 +00:00
|
|
|
|
goto error;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2015-12-10 15:22:24 +00:00
|
|
|
|
buflen = core->anal->opt.bb_max_size;
|
2014-04-29 01:36:04 +00:00
|
|
|
|
bblen = r_anal_bb (core->anal, bb, at+bblen, buf, buflen, head);
|
2016-08-22 16:32:18 +00:00
|
|
|
|
if (bblen == R_ANAL_RET_ERROR || (bblen == R_ANAL_RET_END && bb->size < 1)) { /* Error analyzing bb */
|
2011-12-16 15:33:06 +00:00
|
|
|
|
goto error;
|
2016-08-22 16:32:18 +00:00
|
|
|
|
}
|
|
|
|
|
if (bblen == R_ANAL_RET_END) { /* bb analysis complete */
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (core->anal->split) {
|
2014-09-21 23:39:24 +00:00
|
|
|
|
ret = r_anal_fcn_bb_overlaps (fcn, bb);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2010-06-02 17:17:47 +00:00
|
|
|
|
if (ret == R_ANAL_RET_NEW) {
|
2016-08-21 01:07:19 +00:00
|
|
|
|
r_anal_fcn_bbadd (fcn, bb);
|
2010-03-01 15:50:37 +00:00
|
|
|
|
fail = bb->fail;
|
|
|
|
|
jump = bb->jump;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (fail != -1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
r_core_anal_bb (core, fcn, fail, false);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
if (jump != -1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
r_core_anal_bb (core, fcn, jump, false);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2010-03-01 15:50:37 +00:00
|
|
|
|
}
|
2010-02-28 19:07:36 +00:00
|
|
|
|
}
|
2010-03-01 15:50:37 +00:00
|
|
|
|
} while (bblen != R_ANAL_RET_END);
|
2015-01-31 11:48:15 +00:00
|
|
|
|
free (buf);
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2010-02-26 20:00:03 +00:00
|
|
|
|
}
|
2015-01-31 11:48:15 +00:00
|
|
|
|
goto fin;
|
2011-12-16 15:33:06 +00:00
|
|
|
|
error:
|
2015-09-14 10:35:38 +00:00
|
|
|
|
rc = false;
|
2015-01-31 11:48:15 +00:00
|
|
|
|
fin:
|
2014-05-02 16:29:00 +00:00
|
|
|
|
r_list_delete_data (fcn->bbs, bb);
|
2012-08-31 09:45:06 +00:00
|
|
|
|
r_anal_bb_free (bb);
|
|
|
|
|
free (buf);
|
2015-01-31 11:48:15 +00:00
|
|
|
|
return rc;
|
2010-02-26 20:00:03 +00:00
|
|
|
|
}
|
2010-02-27 10:56:41 +00:00
|
|
|
|
|
2015-09-12 15:53:16 +00:00
|
|
|
|
/* returns the address of the basic block that contains addr or UT64_MAX if
|
|
|
|
|
* there is no such basic block */
|
|
|
|
|
R_API ut64 r_core_anal_get_bbaddr(RCore *core, ut64 addr) {
|
2016-11-02 12:21:21 +00:00
|
|
|
|
RAnalBlock *bbi;
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcni;
|
2016-11-02 12:21:21 +00:00
|
|
|
|
RListIter *iter, *iter2;
|
2015-09-12 15:53:16 +00:00
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcni) {
|
2016-11-02 12:21:21 +00:00
|
|
|
|
r_list_foreach (fcni->bbs, iter2, bbi) {
|
|
|
|
|
if (addr >= bbi->addr && addr < bbi->addr + bbi->size) {
|
|
|
|
|
return bbi->addr;
|
|
|
|
|
}
|
2015-09-12 15:53:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return UT64_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* seek basic block that contains address addr or just addr if there's no such
|
|
|
|
|
* basic block */
|
|
|
|
|
R_API int r_core_anal_bb_seek(RCore *core, ut64 addr) {
|
|
|
|
|
ut64 bbaddr = r_core_anal_get_bbaddr (core, addr);
|
|
|
|
|
if (bbaddr != UT64_MAX) {
|
|
|
|
|
addr = bbaddr;
|
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return r_core_seek (core, addr, false);
|
2010-03-03 17:05:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 00:37:49 +00:00
|
|
|
|
R_API int r_core_anal_esil_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth) {
|
|
|
|
|
const char *esil;
|
|
|
|
|
RAnalOp *op;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
eprintf ("TODO\n");
|
2014-09-12 00:37:49 +00:00
|
|
|
|
while (1) {
|
|
|
|
|
// TODO: Implement the proper logic for doing esil analysis
|
|
|
|
|
op = r_core_anal_op (core, at);
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (!op) {
|
2014-09-12 00:37:49 +00:00
|
|
|
|
break;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2014-09-12 00:37:49 +00:00
|
|
|
|
esil = R_STRBUF_SAFEGET (&op->esil);
|
|
|
|
|
eprintf ("0x%08"PFMT64x" %d %s\n", at, op->size, esil);
|
|
|
|
|
at += op->size;
|
|
|
|
|
// esilIsRet()
|
|
|
|
|
// esilIsCall()
|
|
|
|
|
// esilIsJmp()
|
|
|
|
|
r_anal_op_free (op);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-15 10:11:25 +00:00
|
|
|
|
// XXX: This function takes sometimes forever
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* analyze a RAnalFunction at the address 'at'.
|
|
|
|
|
* If the function has been already analyzed, it adds a
|
|
|
|
|
* reference to that fcn */
|
2010-11-23 13:05:23 +00:00
|
|
|
|
R_API int r_core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth) {
|
2016-09-05 18:42:04 +00:00
|
|
|
|
if (from == UT64_MAX && r_anal_get_fcn_in (core->anal, at, 0)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 23:30:40 +00:00
|
|
|
|
bool use_esil = r_config_get_i (core->config, "anal.esil");
|
2015-07-31 17:12:16 +00:00
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
RListIter *iter;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
|
2015-07-04 23:44:45 +00:00
|
|
|
|
if (core->io->va && !core->io->raw) {
|
2016-03-15 10:28:56 +00:00
|
|
|
|
if (!r_io_is_valid_offset (core->io, at, !core->anal->opt.noncode)) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2016-03-15 10:28:56 +00:00
|
|
|
|
}
|
2015-07-04 23:44:45 +00:00
|
|
|
|
}
|
2015-01-12 01:33:57 +00:00
|
|
|
|
if (r_config_get_i (core->config, "anal.a2f")) {
|
|
|
|
|
r_core_cmd0 (core, ".a2f");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-09-12 00:37:49 +00:00
|
|
|
|
if (use_esil) {
|
|
|
|
|
return r_core_anal_esil_fcn (core, at, from, reftype, depth);
|
|
|
|
|
}
|
2010-03-03 11:08:27 +00:00
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* if there is an anal plugin and it wants to analyze the function itself,
|
|
|
|
|
* run it instead of the normal analysis */
|
2014-01-02 05:09:46 +00:00
|
|
|
|
if (core->anal->cur && core->anal->cur->analyze_fns) {
|
2014-01-10 16:20:23 +00:00
|
|
|
|
int result = R_ANAL_RET_ERROR;
|
2016-10-24 23:12:06 +00:00
|
|
|
|
result = core->anal->cur->analyze_fns (core->anal, at, from, reftype, depth);
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* update the flags after running the analysis function of the plugin */
|
2015-04-22 00:19:21 +00:00
|
|
|
|
r_flag_space_push (core->flags, "functions");
|
2015-07-31 17:12:16 +00:00
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcn) {
|
2016-10-24 23:12:06 +00:00
|
|
|
|
r_flag_set (core->flags, fcn->name, fcn->addr, r_anal_fcn_size (fcn));
|
2014-01-13 21:49:20 +00:00
|
|
|
|
}
|
2015-04-22 00:19:21 +00:00
|
|
|
|
r_flag_space_pop (core->flags);
|
2014-01-10 16:20:23 +00:00
|
|
|
|
return result;
|
2014-01-02 05:09:46 +00:00
|
|
|
|
}
|
2016-10-24 23:12:06 +00:00
|
|
|
|
if (from != UT64_MAX && !at) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (at == UT64_MAX || depth < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2016-10-24 23:12:06 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-09-12 00:37:49 +00:00
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
fcn = r_anal_get_fcn_in (core->anal, at, 0);
|
2015-07-27 21:43:38 +00:00
|
|
|
|
if (fcn) {
|
2016-10-24 23:12:06 +00:00
|
|
|
|
if (fcn->addr == at) {
|
|
|
|
|
return 0; // already analyzed function
|
|
|
|
|
}
|
2016-05-14 14:37:24 +00:00
|
|
|
|
if (r_anal_fcn_is_in_offset (fcn, from)) { // inner function
|
2015-11-19 11:57:30 +00:00
|
|
|
|
RAnalRef *ref;
|
2015-07-31 17:12:16 +00:00
|
|
|
|
|
2015-11-19 11:57:30 +00:00
|
|
|
|
// XXX: use r_anal-xrefs api and sdb
|
|
|
|
|
// If the xref is new, add it
|
|
|
|
|
// avoid dupes
|
2016-10-24 23:12:06 +00:00
|
|
|
|
r_list_foreach (fcn->xrefs, iter, ref) {
|
|
|
|
|
if (from == ref->addr) {
|
2015-11-19 11:57:30 +00:00
|
|
|
|
return true;
|
2016-10-24 23:12:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-17 01:24:15 +00:00
|
|
|
|
ref = r_anal_ref_new ();
|
2015-11-19 11:57:30 +00:00
|
|
|
|
if (!ref) {
|
|
|
|
|
eprintf ("Error: new (xref)\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
ref->addr = from;
|
|
|
|
|
ref->at = at;
|
|
|
|
|
ref->type = reftype;
|
|
|
|
|
if (reftype == R_ANAL_REF_TYPE_DATA) {
|
|
|
|
|
// XXX HACK TO AVOID INVALID REFS
|
2015-11-17 01:24:15 +00:00
|
|
|
|
r_list_append (fcn->xrefs, ref);
|
2015-11-19 11:57:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
free (ref);
|
|
|
|
|
}
|
|
|
|
|
// we should analyze and add code ref otherwise aaa != aac
|
|
|
|
|
if (from != UT64_MAX) {
|
|
|
|
|
// We shuold not use fcn->xrefs .. because that should be only via api (on top of sdb)
|
|
|
|
|
// the concepts of refs and xrefs are a bit twisted in the old implementation
|
|
|
|
|
ref = r_anal_ref_new ();
|
|
|
|
|
if (ref) {
|
|
|
|
|
ref->addr = from;
|
|
|
|
|
ref->at = fcn->addr;
|
|
|
|
|
ref->type = reftype;
|
|
|
|
|
r_list_append (fcn->xrefs, ref);
|
|
|
|
|
// XXX this is creating dupped entries in the refs list with invalid reftypes, wtf?
|
|
|
|
|
r_anal_xrefs_set (core->anal, reftype, from, fcn->addr);
|
2016-10-24 23:12:06 +00:00
|
|
|
|
} else {
|
|
|
|
|
eprintf ("Error: new (xref)\n");
|
|
|
|
|
}
|
2015-11-19 11:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// split function if overlaps
|
|
|
|
|
r_anal_fcn_resize (fcn, at - fcn->addr);
|
2015-11-17 01:24:15 +00:00
|
|
|
|
}
|
2012-08-31 09:45:06 +00:00
|
|
|
|
}
|
2015-07-31 17:12:16 +00:00
|
|
|
|
return core_anal_fcn (core, at, from, reftype, depth);
|
2010-03-03 11:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 17:12:16 +00:00
|
|
|
|
/* if addr is 0, remove all functions
|
|
|
|
|
* otherwise remove the function addr falls into */
|
2010-03-24 00:24:27 +00:00
|
|
|
|
R_API int r_core_anal_fcn_clean(RCore *core, ut64 addr) {
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcni;
|
2012-02-14 17:10:52 +00:00
|
|
|
|
RListIter *iter, *iter_tmp;
|
2010-03-03 11:08:27 +00:00
|
|
|
|
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (!addr) {
|
2014-05-02 14:54:27 +00:00
|
|
|
|
r_list_purge (core->anal->fcns);
|
2010-05-19 22:59:42 +00:00
|
|
|
|
if (!(core->anal->fcns = r_anal_fcn_list_new ()))
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2011-04-29 11:55:27 +00:00
|
|
|
|
} else {
|
2012-02-14 17:10:52 +00:00
|
|
|
|
r_list_foreach_safe (core->anal->fcns, iter, iter_tmp, fcni) {
|
2016-11-01 17:53:09 +00:00
|
|
|
|
if (r_anal_fcn_in (fcni, addr)) {
|
2011-04-29 11:55:27 +00:00
|
|
|
|
r_list_delete (core->anal->fcns, iter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2010-03-03 11:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-10 00:52:11 +00:00
|
|
|
|
#define FMT_NO 0
|
|
|
|
|
#define FMT_GV 1
|
|
|
|
|
#define FMT_JS 2
|
2015-09-07 16:20:29 +00:00
|
|
|
|
R_API void r_core_anal_coderefs(RCore *core, ut64 addr, int fmt) {
|
2014-06-14 01:22:16 +00:00
|
|
|
|
RAnalFunction fakefr = {0};
|
2011-11-23 01:29:09 +00:00
|
|
|
|
const char *font = r_config_get (core->config, "graph.font");
|
2016-11-21 01:37:48 +00:00
|
|
|
|
const char *format = r_config_get (core->config, "graph.format");
|
2015-07-31 10:40:04 +00:00
|
|
|
|
int is_html = r_cons_singleton ()->is_html;
|
2016-05-03 22:11:28 +00:00
|
|
|
|
bool refgraph = r_config_get_i (core->config, "graph.refs");
|
2013-02-07 08:41:05 +00:00
|
|
|
|
int first, first2, showhdr = 0;
|
2010-06-14 14:20:54 +00:00
|
|
|
|
RListIter *iter, *iter2;
|
2013-02-07 08:41:05 +00:00
|
|
|
|
const int hideempty = 1;
|
|
|
|
|
const int usenames = 1;
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcni;
|
2013-02-07 08:41:05 +00:00
|
|
|
|
RAnalRef *fcnr;
|
2016-11-21 01:37:48 +00:00
|
|
|
|
bool isGML = !strcmp (format, "gml");
|
2010-03-24 00:24:27 +00:00
|
|
|
|
|
2016-11-21 01:37:48 +00:00
|
|
|
|
bool gmlFcnGraph = false;
|
|
|
|
|
if (!strcmp (format, "gmlfcn")) {
|
|
|
|
|
isGML = true;
|
|
|
|
|
gmlFcnGraph = true;
|
|
|
|
|
}
|
2016-05-03 23:59:11 +00:00
|
|
|
|
ut64 from = r_config_get_i (core->config, "graph.from");
|
|
|
|
|
ut64 to = r_config_get_i (core->config, "graph.to");
|
|
|
|
|
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fmt == 2) {
|
2016-03-24 16:28:41 +00:00
|
|
|
|
r_cons_printf ("[");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-11-21 01:37:48 +00:00
|
|
|
|
if (fmt == 1 && isGML) {
|
|
|
|
|
r_cons_printf ("graph\n[\n"
|
|
|
|
|
"hierarchic\t1\n"
|
|
|
|
|
"label\t\"\"\n"
|
|
|
|
|
"directed\t1\n");
|
|
|
|
|
}
|
2013-02-07 08:41:05 +00:00
|
|
|
|
first = 0;
|
2016-11-21 01:37:48 +00:00
|
|
|
|
ut64 base = UT64_MAX;
|
|
|
|
|
int iteration = 0;
|
|
|
|
|
repeat:
|
2010-05-19 22:59:42 +00:00
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcni) {
|
2016-11-21 01:37:48 +00:00
|
|
|
|
if (base == UT64_MAX) {
|
|
|
|
|
base = fcni->addr;
|
|
|
|
|
}
|
2016-05-03 23:59:11 +00:00
|
|
|
|
if (from != UT64_MAX && addr < from) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (to != UT64_MAX && addr > to) {
|
2010-03-24 00:24:27 +00:00
|
|
|
|
continue;
|
2016-05-03 23:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
if (addr != UT64_MAX && addr != fcni->addr) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!fmt) {
|
2012-12-10 00:52:11 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x"\n", fcni->addr);
|
2016-11-21 01:37:48 +00:00
|
|
|
|
} else if (fmt == 1 && isGML) {
|
|
|
|
|
RFlagItem *flag = r_flag_get_i (core->flags, fcni->addr);
|
|
|
|
|
if (iteration == 0) {
|
2016-11-21 10:02:23 +00:00
|
|
|
|
char *msg = flag? strdup (flag->name): r_str_newf ("0x%08"PFMT64x, fcni->addr);
|
2016-11-21 01:37:48 +00:00
|
|
|
|
r_cons_printf ("\tnode [\n"
|
|
|
|
|
"\t\tid\t%"PFMT64d"\n"
|
|
|
|
|
"\t\tlabel\t\"%s\"\n"
|
|
|
|
|
"\t]\n", fcni->addr - base, msg);
|
|
|
|
|
free (msg);
|
|
|
|
|
}
|
2016-03-24 16:28:41 +00:00
|
|
|
|
} else if (fmt == 2) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (hideempty && !r_list_length (fcni->refs)) {
|
2012-12-10 00:52:11 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (usenames) {
|
|
|
|
|
r_cons_printf (
|
|
|
|
|
"%s{\"name\":\"%s\", "
|
|
|
|
|
"\"size\":%d,\"imports\":[",
|
|
|
|
|
first ? "," : "", fcni->name,
|
|
|
|
|
r_anal_fcn_size (fcni));
|
|
|
|
|
} else {
|
|
|
|
|
r_cons_printf ("%s{\"name\":\"0x%08" PFMT64x
|
|
|
|
|
"\", \"size\":%d,\"imports\":[",
|
|
|
|
|
first ? "," : "", fcni->addr,
|
|
|
|
|
r_anal_fcn_size (fcni));
|
|
|
|
|
}
|
2012-12-10 00:52:11 +00:00
|
|
|
|
first = 1;
|
|
|
|
|
}
|
|
|
|
|
first2 = 0;
|
2016-05-03 22:11:28 +00:00
|
|
|
|
// TODO: maybe fcni->calls instead ?
|
2010-03-24 00:24:27 +00:00
|
|
|
|
r_list_foreach (fcni->refs, iter2, fcnr) {
|
2014-09-26 16:10:33 +00:00
|
|
|
|
RAnalFunction *fr = r_anal_get_fcn_in (core->anal, fcnr->addr, 0);
|
2014-06-14 01:22:16 +00:00
|
|
|
|
if (!fr) {
|
|
|
|
|
fr = &fakefr;
|
2015-09-09 09:16:49 +00:00
|
|
|
|
if (fr) {
|
|
|
|
|
free (fr->name);
|
|
|
|
|
fr->name = r_str_newf ("unk.0x%"PFMT64x, fcnr->addr);
|
|
|
|
|
}
|
2014-06-14 01:22:16 +00:00
|
|
|
|
}
|
2012-09-05 01:25:03 +00:00
|
|
|
|
if (!is_html && !showhdr) {
|
2016-03-24 16:28:41 +00:00
|
|
|
|
if (fmt == 1) {
|
2016-11-21 01:37:48 +00:00
|
|
|
|
if (isGML) {
|
|
|
|
|
/*
|
|
|
|
|
r_cons_printf ("Creator \"radare2\"\n"
|
|
|
|
|
"Version \"2.14\"\n"
|
|
|
|
|
"graph\n[\n"
|
|
|
|
|
"hierarchic\t1\n"
|
|
|
|
|
"label\t\"\"\n"
|
|
|
|
|
"directed\t1\n");
|
|
|
|
|
*/
|
|
|
|
|
} else {
|
|
|
|
|
const char * gv_edge = r_config_get (core->config, "graph.gv.edge");
|
|
|
|
|
const char * gv_node = r_config_get (core->config, "graph.gv.node");
|
|
|
|
|
const char * gv_grph = r_config_get (core->config, "graph.gv.graph");
|
|
|
|
|
if (!gv_edge || !*gv_edge) {
|
|
|
|
|
gv_edge = "arrowhead=\"vee\"";
|
|
|
|
|
}
|
|
|
|
|
if (!gv_node || !*gv_node) {
|
|
|
|
|
gv_node = "fillcolor=gray style=filled shape=box";
|
|
|
|
|
}
|
|
|
|
|
if (!gv_grph || !*gv_grph) {
|
|
|
|
|
gv_grph = "bgcolor=white";
|
|
|
|
|
}
|
|
|
|
|
r_cons_printf ("digraph code {\n"
|
|
|
|
|
"\tgraph [%s fontname=\"%s\"];\n"
|
|
|
|
|
"\tnode [%s];\n"
|
|
|
|
|
"\tedge [%s];\n", gv_grph, font, gv_node, gv_edge);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-09-09 09:16:49 +00:00
|
|
|
|
}
|
2011-09-22 22:16:29 +00:00
|
|
|
|
showhdr = 1;
|
|
|
|
|
}
|
2010-06-14 14:20:54 +00:00
|
|
|
|
// TODO: display only code or data refs?
|
2010-06-13 22:57:40 +00:00
|
|
|
|
RFlagItem *flag = r_flag_get_i (core->flags, fcnr->addr);
|
2015-09-09 09:16:49 +00:00
|
|
|
|
if (fmt == 1) {
|
2016-11-21 01:37:48 +00:00
|
|
|
|
if (isGML) {
|
|
|
|
|
if (iteration == 0) {
|
|
|
|
|
if (gmlFcnGraph) {
|
|
|
|
|
char *msg = flag? strdup(flag->name): r_str_newf ("0x%08"PFMT64x, fcnr->addr);
|
|
|
|
|
r_cons_printf ("\tnode [\n"
|
|
|
|
|
"\t\tid\t%"PFMT64d"\n"
|
|
|
|
|
"\t\tlabel\t\"%s\"\n"
|
|
|
|
|
"\t]\n", fcnr->addr - base, msg
|
|
|
|
|
);
|
|
|
|
|
r_cons_printf ("\tedge [\n"
|
|
|
|
|
"\t\tsource %"PFMT64d"\n"
|
|
|
|
|
"\t\ttarget %"PFMT64d"\n"
|
|
|
|
|
"\t]\n", fcni->addr-base, fcnr->addr-base
|
|
|
|
|
);
|
|
|
|
|
free (msg);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
r_cons_printf ("\tedge [\n"
|
|
|
|
|
"\t\tsource %"PFMT64d"\n"
|
|
|
|
|
"\t\ttarget %"PFMT64d"\n"
|
|
|
|
|
/*
|
|
|
|
|
"graphics\n"
|
|
|
|
|
"[\n"
|
|
|
|
|
" fill \"%s\"\n"
|
|
|
|
|
" targetArrow \"standard\"\n"
|
|
|
|
|
"]\n"
|
|
|
|
|
*/
|
|
|
|
|
"\t]\n", fcni->addr-base, fcnr->addr-base //, "#000000"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (flag && flag->name) {
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
|
|
|
|
|
"[label=\"%s\" color=\"%s\" URL=\"%s/0x%08"PFMT64x"\"];\n",
|
|
|
|
|
fcni->addr, fcnr->addr, flag->name,
|
|
|
|
|
(fcnr->type==R_ANAL_REF_TYPE_CODE ||
|
|
|
|
|
fcnr->type==R_ANAL_REF_TYPE_CALL)?"green":"red",
|
|
|
|
|
flag->name, fcnr->addr);
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\" "
|
|
|
|
|
"[label=\"%s\""
|
|
|
|
|
" URL=\"%s/0x%08"PFMT64x"\"];\n",
|
|
|
|
|
fcnr->addr, flag->name,
|
|
|
|
|
flag->name, fcnr->addr);
|
|
|
|
|
}
|
2016-03-24 16:28:41 +00:00
|
|
|
|
}
|
|
|
|
|
} else if (fmt == 2) {
|
2012-12-10 00:52:11 +00:00
|
|
|
|
if (fr) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!hideempty || (hideempty && r_list_length (fr->refs) > 0)) {
|
|
|
|
|
if (usenames) {
|
2013-02-13 16:54:20 +00:00
|
|
|
|
r_cons_printf ("%s\"%s\"", first2?",":"", fr->name);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else {
|
2016-03-24 16:28:41 +00:00
|
|
|
|
r_cons_printf ("%s\"0x%08"PFMT64x"\"", first2?",":"", fr->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2012-12-10 00:52:11 +00:00
|
|
|
|
first2 = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-24 16:28:41 +00:00
|
|
|
|
} else {
|
2016-05-03 22:11:28 +00:00
|
|
|
|
if (refgraph || fcnr->type == 'C') {
|
|
|
|
|
// TODO: avoid recreating nodes unnecessarily
|
|
|
|
|
r_cons_printf ("agn %s\n", fcni->name);
|
|
|
|
|
r_cons_printf ("agn %s\n", fr->name);
|
|
|
|
|
r_cons_printf ("age %s %s\n", fcni->name, fr->name);
|
|
|
|
|
} else {
|
|
|
|
|
r_cons_printf ("# - 0x%08"PFMT64x" (%c)\n", fcnr->addr, fcnr->type);
|
|
|
|
|
}
|
2016-03-24 16:28:41 +00:00
|
|
|
|
}
|
2010-03-24 00:24:27 +00:00
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fmt == 2) {
|
|
|
|
|
r_cons_printf ("]}");
|
|
|
|
|
}
|
2010-03-24 00:24:27 +00:00
|
|
|
|
}
|
2016-11-21 01:37:48 +00:00
|
|
|
|
if (iteration == 0 && fmt == 1 && isGML) {
|
|
|
|
|
iteration++;
|
|
|
|
|
if (!gmlFcnGraph) {
|
|
|
|
|
goto repeat;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (showhdr && fmt == 1) {
|
2016-11-21 01:37:48 +00:00
|
|
|
|
r_cons_printf ("%s\n", isGML? "]": "}");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (fmt == 2) {
|
2016-03-24 16:28:41 +00:00
|
|
|
|
r_cons_printf ("]\n");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2010-03-24 00:24:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-22 08:00:35 +00:00
|
|
|
|
static void fcn_list_bbs(RAnalFunction *fcn) {
|
2011-02-11 10:22:43 +00:00
|
|
|
|
RAnalBlock *bbi;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
|
|
|
|
|
r_list_foreach (fcn->bbs, iter, bbi) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_cons_printf ("afb+ 0x%08" PFMT64x " 0x%08" PFMT64x " %d ",
|
|
|
|
|
fcn->addr, bbi->addr, bbi->size);
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" ", bbi->jump);
|
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" ", bbi->fail);
|
|
|
|
|
if (bbi->type != R_ANAL_BB_TYPE_NULL) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if ((bbi->type & R_ANAL_BB_TYPE_BODY)) {
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("b");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if ((bbi->type & R_ANAL_BB_TYPE_FOOT)) {
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("f");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if ((bbi->type & R_ANAL_BB_TYPE_HEAD)) {
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("h");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if ((bbi->type & R_ANAL_BB_TYPE_LAST)) {
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("l");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else {
|
|
|
|
|
r_cons_printf ("n");
|
|
|
|
|
}
|
2013-10-23 22:36:07 +00:00
|
|
|
|
if (bbi->diff) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (bbi->diff->type == R_ANAL_DIFF_TYPE_MATCH) {
|
2013-10-23 22:36:07 +00:00
|
|
|
|
r_cons_printf (" m");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else if (bbi->diff->type == R_ANAL_DIFF_TYPE_UNMATCH) {
|
2013-10-23 22:36:07 +00:00
|
|
|
|
r_cons_printf (" u");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
r_cons_printf (" n");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2013-10-23 22:36:07 +00:00
|
|
|
|
}
|
2011-02-11 10:22:43 +00:00
|
|
|
|
r_cons_printf ("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 11:54:40 +00:00
|
|
|
|
R_API int r_core_anal_fcn_list_size(RCore *core) {
|
2016-03-07 03:12:11 +00:00
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
ut32 total = 0;
|
|
|
|
|
|
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcn) {
|
2016-05-15 12:37:22 +00:00
|
|
|
|
total += r_anal_fcn_size (fcn);
|
2016-03-07 03:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
r_cons_printf ("%d\n", total);
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 13:40:11 +00:00
|
|
|
|
static int cmpfcn(const void *_a, const void *_b) {
|
|
|
|
|
const RAnalFunction *_fcn1 = _a, *_fcn2 = _b;
|
|
|
|
|
return (_fcn1->addr > _fcn2->addr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 15:54:17 +00:00
|
|
|
|
/* Fill out metadata struct of functions */
|
2016-12-19 11:30:17 +00:00
|
|
|
|
static int fcnlist_gather_metadata(RAnal *anal, RList *fcns) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
RListIter *iter;
|
2013-02-07 08:41:05 +00:00
|
|
|
|
RAnalFunction *fcn;
|
2016-12-19 11:30:17 +00:00
|
|
|
|
RList *refs = NULL;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
|
|
|
|
// Count the number of references and number of calls
|
|
|
|
|
RListIter *callrefiter;
|
|
|
|
|
RAnalRef *ref;
|
|
|
|
|
int numcallrefs = 0;
|
|
|
|
|
r_list_foreach (fcn->refs, callrefiter, ref) {
|
|
|
|
|
if (ref->type == R_ANAL_REF_TYPE_CALL) {
|
|
|
|
|
numcallrefs++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-21 09:39:37 +00:00
|
|
|
|
fcn->meta.numcallrefs = numcallrefs;
|
2016-12-19 11:30:17 +00:00
|
|
|
|
refs = r_anal_xrefs_get (anal, fcn->addr);
|
|
|
|
|
fcn->meta.numrefs = refs? refs->length: 0;
|
|
|
|
|
r_list_free (refs);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
// Determine the bounds of the functions address space
|
|
|
|
|
ut64 min = UT64_MAX;
|
|
|
|
|
ut64 max = UT64_MIN;
|
|
|
|
|
|
|
|
|
|
RListIter *bbsiter;
|
|
|
|
|
RAnalBlock *bbi;
|
|
|
|
|
r_list_foreach (fcn->bbs, bbsiter, bbi) {
|
|
|
|
|
if (max < bbi->addr + bbi->size) {
|
|
|
|
|
max = bbi->addr + bbi->size;
|
|
|
|
|
}
|
|
|
|
|
if (min > bbi->addr) {
|
|
|
|
|
min = bbi->addr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fcn->meta.min = min;
|
|
|
|
|
fcn->meta.max = max;
|
|
|
|
|
}
|
|
|
|
|
// TODO: Determine sgnc, sgec
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *get_fcn_name(RCore *core, RAnalFunction *fcn) {
|
|
|
|
|
bool demangle;
|
2016-02-07 20:44:35 +00:00
|
|
|
|
const char *lang;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
demangle = r_config_get_i (core->config, "bin.demangle");
|
2016-02-07 20:44:35 +00:00
|
|
|
|
lang = demangle ? r_config_get (core->config, "bin.lang") : NULL;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
char *name = strdup (fcn->name ? fcn->name : "");
|
|
|
|
|
if (demangle) {
|
2016-11-22 13:58:42 +00:00
|
|
|
|
char *tmp = r_bin_demangle (core->bin->cur, lang, name, fcn->addr);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
if (tmp) {
|
|
|
|
|
free (name);
|
|
|
|
|
name = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 16:50:52 +00:00
|
|
|
|
static int count_edges(RAnalFunction *fcn, int *ebbs) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
int edges = 0;
|
|
|
|
|
if (ebbs) {
|
|
|
|
|
*ebbs = 0;
|
|
|
|
|
}
|
|
|
|
|
r_list_foreach (fcn->bbs, iter, bb) {
|
|
|
|
|
if (ebbs && bb->jump == UT64_MAX && bb->fail == UT64_MAX) {
|
|
|
|
|
*ebbs = *ebbs + 1;
|
|
|
|
|
} else {
|
|
|
|
|
if (bb->jump != UT64_MAX) {
|
|
|
|
|
edges ++;
|
|
|
|
|
}
|
|
|
|
|
if (bb->fail != UT64_MAX) {
|
|
|
|
|
edges ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return edges;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 22:08:37 +00:00
|
|
|
|
#define FCN_LIST_VERBOSE_ENTRY "%s0x%08"PFMT64x" %4d %5d %5d %5d %4d 0x%08"PFMT64x" %5d 0x%08"PFMT64x" %5d %4d %6d %4d %5d %s%s\n"
|
2016-06-14 15:54:17 +00:00
|
|
|
|
static int fcn_print_verbose(RCore *core, RAnalFunction *fcn, bool use_color) {
|
|
|
|
|
char *name = get_fcn_name(core, fcn);
|
2016-11-02 16:50:52 +00:00
|
|
|
|
int ebbs = 0;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
const char *color = "";
|
|
|
|
|
const char *color_end = "";
|
|
|
|
|
if (use_color) {
|
|
|
|
|
color_end = Color_RESET;
|
|
|
|
|
if (strstr (name, "sym.imp.")) {
|
|
|
|
|
color = Color_YELLOW;
|
|
|
|
|
} else if (strstr (name, "sym.")) {
|
|
|
|
|
color = Color_GREEN;
|
|
|
|
|
} else if (strstr (name, "sub.")) {
|
|
|
|
|
color = Color_MAGENTA;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r_cons_printf (FCN_LIST_VERBOSE_ENTRY, color,
|
|
|
|
|
fcn->addr,
|
|
|
|
|
r_anal_fcn_realsize (fcn),
|
|
|
|
|
r_list_length (fcn->bbs),
|
2016-11-02 16:50:52 +00:00
|
|
|
|
count_edges (fcn, &ebbs),
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_anal_fcn_cc (fcn),
|
2017-01-13 22:08:37 +00:00
|
|
|
|
r_anal_fcn_cost (core->anal, fcn),
|
2016-06-14 15:54:17 +00:00
|
|
|
|
fcn->meta.min,
|
|
|
|
|
r_anal_fcn_size (fcn),
|
|
|
|
|
fcn->meta.max,
|
|
|
|
|
fcn->meta.numcallrefs,
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 0) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 0) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 0),
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 1) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 1) +
|
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 1),
|
2016-06-14 15:54:17 +00:00
|
|
|
|
fcn->meta.numrefs,
|
|
|
|
|
fcn->maxstack,
|
|
|
|
|
name,
|
|
|
|
|
color_end);
|
|
|
|
|
free (name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_list_verbose(RCore *core, RList *fcns) {
|
2016-05-16 01:46:23 +00:00
|
|
|
|
bool use_color = r_config_get_i (core->config, "scr.color");
|
2010-03-03 11:08:27 +00:00
|
|
|
|
|
2017-01-13 22:08:37 +00:00
|
|
|
|
r_cons_printf ("%-11s %4s %5s %5s %5s %4s %11s range %-11s %s %s %s %s %s %s\n",
|
|
|
|
|
"address", "size", "nbbs", "edges", "cc", "cost", "min bound", "max bound",
|
2016-07-19 19:50:55 +00:00
|
|
|
|
"calls", "locals", "args", "xref", "frame", "name");
|
2017-01-13 22:08:37 +00:00
|
|
|
|
r_cons_printf ("%-11s %-4s %-5s %-5s %-5s %-4s %-11s ===== %-11s %s %s %s %s %s %s\n",
|
|
|
|
|
"===========", "====", "=====", "=====", "=====", "====", "===========", "===========",
|
2016-07-19 19:50:55 +00:00
|
|
|
|
"=====", "======", "====", "====", "=====", "====");
|
2016-02-07 20:44:35 +00:00
|
|
|
|
|
2016-06-14 15:54:17 +00:00
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
|
|
|
|
fcn_print_verbose (core, fcn, use_color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_print_default(RCore *core, RAnalFunction *fcn, bool quiet) {
|
|
|
|
|
if (quiet) {
|
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" ", fcn->addr);
|
|
|
|
|
} else {
|
|
|
|
|
char *msg, *name = get_fcn_name (core, fcn);
|
|
|
|
|
int realsize = r_anal_fcn_realsize (fcn);
|
|
|
|
|
int size = r_anal_fcn_size (fcn);
|
|
|
|
|
if (realsize == size) {
|
|
|
|
|
msg = r_str_newf ("%-12d", size);
|
|
|
|
|
} else {
|
|
|
|
|
msg = r_str_newf ("%-4d -> %-4d", size, realsize);
|
2012-10-30 09:08:06 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" %4d %4s %s\n",
|
|
|
|
|
fcn->addr, r_list_length (fcn->bbs), msg, name);
|
|
|
|
|
free (name);
|
|
|
|
|
free (msg);
|
2012-10-30 09:08:06 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_list_default(RCore *core, RList *fcns, bool quiet) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
|
|
|
|
fcn_print_default (core, fcn, quiet);
|
|
|
|
|
}
|
|
|
|
|
if (quiet) {
|
|
|
|
|
r_cons_newline ();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_print_json(RCore *core, RAnalFunction *fcn) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalRef *refi;
|
|
|
|
|
int first = 1;
|
2016-08-16 17:35:25 +00:00
|
|
|
|
int ebbs = 0;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
char *name = get_fcn_name (core, fcn);
|
|
|
|
|
r_cons_printf ("{\"offset\":%"PFMT64d",\"name\":\"%s\",\"size\":%d",
|
|
|
|
|
fcn->addr, name, r_anal_fcn_size (fcn));
|
|
|
|
|
r_cons_printf (",\"realsz\":%d", r_anal_fcn_realsize (fcn));
|
|
|
|
|
r_cons_printf (",\"cc\":%d", r_anal_fcn_cc (fcn));
|
2017-01-13 22:08:37 +00:00
|
|
|
|
r_cons_printf (",\"cost\":%d", r_anal_fcn_cost (core->anal, fcn));
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (",\"nbbs\":%d", r_list_length (fcn->bbs));
|
2016-08-16 17:35:25 +00:00
|
|
|
|
r_cons_printf (",\"edges\":%d", count_edges (fcn, &ebbs));
|
|
|
|
|
r_cons_printf (",\"ebbs\":%d", ebbs);
|
2016-07-27 11:50:14 +00:00
|
|
|
|
r_cons_printf (",\"calltype\":\"%s\"", fcn->cc);
|
2016-07-25 18:15:50 +00:00
|
|
|
|
r_cons_printf (",\"type\":\"%s\"", r_anal_fcn_type_tostring (fcn->type));
|
|
|
|
|
if (fcn->type == R_ANAL_FCN_TYPE_FCN || fcn->type == R_ANAL_FCN_TYPE_SYM) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (",\"diff\":\"%s\"",
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_MATCH?"MATCH":
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_UNMATCH?"UNMATCH":"NEW");
|
2016-07-25 18:15:50 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (",\"callrefs\":[");
|
2016-10-30 11:16:46 +00:00
|
|
|
|
int outdegree = 0;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_list_foreach (fcn->refs, iter, refi) {
|
2016-10-30 11:16:46 +00:00
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CALL) {
|
|
|
|
|
outdegree++;
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CODE ||
|
2016-11-01 18:42:43 +00:00
|
|
|
|
refi->type == R_ANAL_REF_TYPE_CALL) {
|
2016-07-14 21:51:09 +00:00
|
|
|
|
r_cons_printf ("%s{\"addr\":%"PFMT64d",\"type\":\"%c\",\"at\":%"PFMT64d"}",
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first?"":",",
|
|
|
|
|
refi->addr,
|
2016-07-14 21:51:09 +00:00
|
|
|
|
refi->type == R_ANAL_REF_TYPE_CALL?'C':'J',
|
|
|
|
|
refi->at);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first = 0;
|
2016-05-16 01:46:23 +00:00
|
|
|
|
}
|
2016-05-06 09:47:21 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
first = 1;
|
|
|
|
|
r_cons_printf ("],\"datarefs\":[");
|
|
|
|
|
r_list_foreach (fcn->refs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_DATA) {
|
|
|
|
|
r_cons_printf ("%s%"PFMT64d, first?"":",", refi->addr);
|
|
|
|
|
first = 0;
|
2015-04-02 15:11:59 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
}
|
2014-08-08 12:40:50 +00:00
|
|
|
|
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first = 1;
|
2016-10-30 11:16:46 +00:00
|
|
|
|
int indegree = 0;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("],\"codexrefs\":[");
|
|
|
|
|
r_list_foreach (fcn->xrefs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CODE ||
|
2016-11-01 18:42:43 +00:00
|
|
|
|
refi->type == R_ANAL_REF_TYPE_CALL) {
|
2016-10-30 11:16:46 +00:00
|
|
|
|
indegree++;
|
2016-07-14 21:51:09 +00:00
|
|
|
|
r_cons_printf ("%s{\"addr\":%"PFMT64d",\"type\":\"%c\",\"at\":%"PFMT64d"}",
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first?"":",",
|
|
|
|
|
refi->addr,
|
2016-07-14 21:51:09 +00:00
|
|
|
|
refi->type==R_ANAL_REF_TYPE_CALL?'C':'J',
|
|
|
|
|
refi->at);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-08 12:40:50 +00:00
|
|
|
|
|
2016-06-14 15:54:17 +00:00
|
|
|
|
first = 1;
|
|
|
|
|
r_cons_printf ("],\"dataxrefs\":[");
|
|
|
|
|
r_list_foreach (fcn->xrefs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_DATA) {
|
|
|
|
|
r_cons_printf ("%s%"PFMT64d, first?"":",", refi->addr);
|
|
|
|
|
first = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
r_cons_printf ("]");
|
|
|
|
|
|
|
|
|
|
if (fcn->type == R_ANAL_FCN_TYPE_FCN || fcn->type == R_ANAL_FCN_TYPE_SYM) {
|
|
|
|
|
r_cons_printf (",\"difftype\":\"%s\"",
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_MATCH?"match":
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_UNMATCH?"unmatch":"new");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->diff->addr != -1) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (",\"diffaddr\":%"PFMT64d, fcn->diff->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (fcn->diff->name != NULL) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (",\"diffname\":\"%s\"", fcn->diff->name);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
}
|
2016-10-30 11:16:46 +00:00
|
|
|
|
r_cons_printf (",\"indegree\":%d", indegree);
|
|
|
|
|
r_cons_printf (",\"outdegree\":%d", outdegree);
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_cons_printf (",\"nargs\":%d",
|
2016-12-19 13:18:49 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 1) +
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 1) +
|
2016-12-19 13:18:49 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 1));
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_cons_printf (",\"nlocals\":%d",
|
2016-12-19 13:18:49 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 'b', 0) +
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 'r', 0) +
|
2016-12-19 13:18:49 +00:00
|
|
|
|
r_anal_var_count (core->anal, fcn, 's', 0));
|
2016-07-19 19:50:55 +00:00
|
|
|
|
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("}");
|
|
|
|
|
free (name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-08-08 12:40:50 +00:00
|
|
|
|
|
2016-07-29 17:34:43 +00:00
|
|
|
|
static int fcn_list_json(RCore *core, RList *fcns, bool quiet) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
2017-02-04 16:35:09 +00:00
|
|
|
|
bool first = true;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("[");
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
2017-02-04 16:35:09 +00:00
|
|
|
|
if (first) {
|
|
|
|
|
first = false;
|
|
|
|
|
} else {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_cons_printf (",");
|
|
|
|
|
}
|
2016-07-29 17:34:43 +00:00
|
|
|
|
if (quiet) {
|
|
|
|
|
r_cons_printf ("\"0x%08"PFMT64x"\"", fcn->addr);
|
|
|
|
|
} else {
|
|
|
|
|
fcn_print_json (core, fcn);
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
}
|
|
|
|
|
r_cons_printf ("]\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_print_detail(RCore *core, RAnalFunction *fcn) {
|
|
|
|
|
char *name = get_fcn_name (core, fcn);
|
|
|
|
|
r_cons_printf ("f %s %d 0x%08"PFMT64x"\n", name, r_anal_fcn_size (fcn), fcn->addr);
|
|
|
|
|
r_cons_printf ("af+ 0x%08"PFMT64x" %d %s %c %c\n",
|
|
|
|
|
fcn->addr, r_anal_fcn_size (fcn), name,
|
|
|
|
|
fcn->type == R_ANAL_FCN_TYPE_LOC?'l':
|
|
|
|
|
fcn->type == R_ANAL_FCN_TYPE_SYM?'s':
|
|
|
|
|
fcn->type == R_ANAL_FCN_TYPE_IMP?'i':'f',
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_MATCH?'m':
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_UNMATCH?'u':'n');
|
2016-07-27 11:50:14 +00:00
|
|
|
|
r_cons_printf ("afC %s @ 0x%08"PFMT64x"\n", fcn->cc, fcn->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->folded) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("afF @ 0x%08"PFMT64x"\n", fcn->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
fcn_list_bbs (fcn);
|
|
|
|
|
/* show variables and arguments */
|
2016-07-01 14:15:29 +00:00
|
|
|
|
r_core_cmdf (core, "afvb* @ 0x%"PFMT64x"\n", fcn->addr);
|
|
|
|
|
r_core_cmdf (core, "afvr* @ 0x%"PFMT64x"\n", fcn->addr);
|
|
|
|
|
r_core_cmdf (core, "afvs* @ 0x%"PFMT64x"\n", fcn->addr);
|
2016-10-14 19:54:00 +00:00
|
|
|
|
/*Saving Function stack frame*/
|
|
|
|
|
r_cons_printf ("afS %"PFMT64d" @ 0x%"PFMT64x"\n", fcn->maxstack, fcn->addr);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
free (name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_print_legacy(RCore *core, RAnalFunction *fcn) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalRef *refi;
|
2016-08-16 17:35:25 +00:00
|
|
|
|
int ebbs = 0;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
char *name = get_fcn_name (core, fcn);
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("#\noffset: 0x%08"PFMT64x"\nname: %s\nsize: %"PFMT64d,
|
2016-06-14 15:54:17 +00:00
|
|
|
|
fcn->addr, name, (ut64)r_anal_fcn_size (fcn));
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\nrealsz: %d", r_anal_fcn_realsize (fcn));
|
|
|
|
|
r_cons_printf ("\nstackframe: %d", fcn->maxstack);
|
|
|
|
|
r_cons_printf ("\ncall-convention: %s", fcn->cc);
|
|
|
|
|
r_cons_printf ("\ncyclomatic-cost : %d", r_anal_fcn_cost (core->anal, fcn));
|
|
|
|
|
r_cons_printf ("\ncyclomatic-complexity: %d", r_anal_fcn_cc (fcn));
|
|
|
|
|
r_cons_printf ("\nbits: %d", fcn->bits);
|
|
|
|
|
r_cons_printf ("\ntype: %s", r_anal_fcn_type_tostring (fcn->type));
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->type == R_ANAL_FCN_TYPE_FCN || fcn->type == R_ANAL_FCN_TYPE_SYM) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf (" [%s]",
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_MATCH?"MATCH":
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_UNMATCH?"UNMATCH":"NEW");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\nnum-bbs: %d", r_list_length (fcn->bbs));
|
|
|
|
|
r_cons_printf ("\nedges: %d", count_edges (fcn, &ebbs));
|
|
|
|
|
r_cons_printf ("\nend-bbs: %d", ebbs);
|
|
|
|
|
r_cons_printf ("\ncall-refs: ");
|
2016-10-30 11:16:46 +00:00
|
|
|
|
int outdegree = 0;
|
|
|
|
|
r_list_foreach (fcn->refs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CALL) {
|
|
|
|
|
outdegree++;
|
|
|
|
|
}
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CODE || refi->type == R_ANAL_REF_TYPE_CALL) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" %c ", refi->addr,
|
|
|
|
|
refi->type == R_ANAL_REF_TYPE_CALL?'C':'J');
|
2016-10-30 11:16:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\ndata-refs: ");
|
2016-10-30 11:16:46 +00:00
|
|
|
|
r_list_foreach (fcn->refs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_DATA) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" ", refi->addr);
|
2016-10-30 11:16:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
2016-10-30 11:16:46 +00:00
|
|
|
|
int indegree = 0;
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\ncode-xrefs: ");
|
2016-10-30 11:16:46 +00:00
|
|
|
|
r_list_foreach (fcn->xrefs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_CODE || refi->type == R_ANAL_REF_TYPE_CALL) {
|
|
|
|
|
indegree++;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" %c ", refi->addr,
|
|
|
|
|
refi->type == R_ANAL_REF_TYPE_CALL?'C':'J');
|
2016-10-30 11:16:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\nin-degree: %d", indegree);
|
|
|
|
|
r_cons_printf ("\nout-degree: %d", outdegree);
|
|
|
|
|
r_cons_printf ("\ndata-xrefs: ");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
r_list_foreach (fcn->xrefs, iter, refi) {
|
|
|
|
|
if (refi->type == R_ANAL_REF_TYPE_DATA) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_cons_printf ("0x%08"PFMT64x" ", refi->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
if (fcn->type == R_ANAL_FCN_TYPE_FCN || fcn->type == R_ANAL_FCN_TYPE_SYM) {
|
2016-07-19 19:50:55 +00:00
|
|
|
|
int args_count = r_anal_var_count (core->anal, fcn, 'b', 1);
|
|
|
|
|
args_count += r_anal_var_count (core->anal, fcn, 's', 1);
|
|
|
|
|
args_count += r_anal_var_count (core->anal, fcn, 'r', 1);
|
|
|
|
|
int var_count = r_anal_var_count (core->anal, fcn, 'b', 0);
|
|
|
|
|
var_count += r_anal_var_count (core->anal, fcn, 's', 0);
|
|
|
|
|
var_count += r_anal_var_count (core->anal, fcn, 'r', 0);
|
|
|
|
|
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("\nlocals:%d\nargs: %d\n", var_count, args_count);
|
2016-07-19 19:50:55 +00:00
|
|
|
|
r_anal_var_list_show (core->anal, fcn, 'b', 0);
|
|
|
|
|
r_anal_var_list_show (core->anal, fcn, 's', 0);
|
|
|
|
|
r_anal_var_list_show (core->anal, fcn, 'r', 0);
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("diff: type: %s",
|
2016-06-14 15:54:17 +00:00
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_MATCH?"match":
|
|
|
|
|
fcn->diff->type == R_ANAL_DIFF_TYPE_UNMATCH?"unmatch":"new");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcn->diff->addr != -1) {
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("addr: 0x%"PFMT64x, fcn->diff->addr);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (fcn->diff->name != NULL) {
|
2017-01-25 16:52:55 +00:00
|
|
|
|
r_cons_printf ("function: %s", fcn->diff->name);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
}
|
|
|
|
|
free (name);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_list_detail(RCore *core, RList *fcns) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
|
|
|
|
fcn_print_detail (core, fcn);
|
|
|
|
|
}
|
|
|
|
|
r_cons_newline ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fcn_list_legacy(RCore *core, RList *fcns)
|
|
|
|
|
{
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (fcns, iter, fcn) {
|
|
|
|
|
fcn_print_legacy (core, fcn);
|
|
|
|
|
}
|
|
|
|
|
r_cons_newline ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-29 17:34:43 +00:00
|
|
|
|
R_API int r_core_anal_fcn_list(RCore *core, const char *input, const char *rad) {
|
2016-12-19 11:30:17 +00:00
|
|
|
|
RList *fcns = NULL;
|
|
|
|
|
if (!core || !core->anal) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fcns = core->anal->fcns;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (r_list_empty (fcns)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
r_list_sort (fcns, &cmpfcn);
|
2016-12-19 11:30:17 +00:00
|
|
|
|
fcnlist_gather_metadata (core->anal, fcns);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
2016-06-22 08:29:16 +00:00
|
|
|
|
if (input) {// input points to a filter argument
|
|
|
|
|
const char *name = input;
|
2016-06-14 15:54:17 +00:00
|
|
|
|
ut64 addr;
|
|
|
|
|
addr = core->offset;
|
|
|
|
|
if (*input) {
|
2016-06-22 08:29:16 +00:00
|
|
|
|
name = input + 1;
|
|
|
|
|
addr = r_num_math (core->num, name);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fcns = r_list_new ();
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!fcns) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcn) {
|
2016-11-01 17:53:09 +00:00
|
|
|
|
if (r_anal_fcn_in (fcn, addr) || (!strcmp (name, fcn->name))) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
r_list_append (fcns, fcn);
|
2011-02-11 10:22:43 +00:00
|
|
|
|
}
|
2010-08-02 10:42:59 +00:00
|
|
|
|
}
|
2015-01-31 18:24:18 +00:00
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
|
|
|
|
|
r_list_sort (core->anal->fcns, &cmpfcn);
|
2016-07-29 17:34:43 +00:00
|
|
|
|
switch (*rad) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
case 's':
|
|
|
|
|
r_core_anal_fcn_list_size (core);
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
fcn_list_verbose (core, fcns);
|
|
|
|
|
break;
|
|
|
|
|
case 'q':
|
2016-07-29 17:34:43 +00:00
|
|
|
|
if (rad[1] == 'j') {
|
|
|
|
|
fcn_list_json (core, fcns, true);
|
|
|
|
|
} else {
|
|
|
|
|
fcn_list_default (core, fcns, true);
|
|
|
|
|
}
|
2016-06-14 15:54:17 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
2016-07-29 17:34:43 +00:00
|
|
|
|
fcn_list_json (core, fcns, false);
|
2016-06-14 15:54:17 +00:00
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
fcn_list_detail (core, fcns);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
fcn_list_legacy (core, fcns);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fcn_list_default (core, fcns, false);
|
|
|
|
|
break;
|
2014-08-08 12:40:50 +00:00
|
|
|
|
}
|
2016-06-21 21:01:09 +00:00
|
|
|
|
//make sure you don't free core->anal->fcns
|
|
|
|
|
if (input && core->anal->fcns != fcns) {
|
2016-06-14 15:54:17 +00:00
|
|
|
|
// The list does not own the its members, so don't purge.
|
|
|
|
|
free (fcns);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2010-03-03 11:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-14 00:18:15 +00:00
|
|
|
|
static RList *recurse(RCore *core, RAnalBlock *from, RAnalBlock *dest);
|
|
|
|
|
|
|
|
|
|
static RList *recurse_bb(RCore *core, ut64 addr, RAnalBlock *dest) {
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
RList *ret;
|
|
|
|
|
bb = r_anal_bb_from_offset (core->anal, addr);
|
|
|
|
|
if (bb == dest) {
|
|
|
|
|
eprintf ("path found!");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
ret = recurse (core, bb, dest);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
return ret? ret : NULL;
|
2012-06-14 00:18:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static RList *recurse(RCore *core, RAnalBlock *from, RAnalBlock *dest) {
|
2015-07-31 17:12:16 +00:00
|
|
|
|
recurse_bb (core, from->jump, dest);
|
|
|
|
|
recurse_bb (core, from->fail, dest);
|
2012-06-14 00:18:15 +00:00
|
|
|
|
|
|
|
|
|
/* same for all calls */
|
|
|
|
|
// TODO: RAnalBlock must contain a linked list of calls
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 16:49:25 +00:00
|
|
|
|
R_API void fcn_callconv(RCore *core, RAnalFunction *fcn) {
|
2016-06-17 12:26:24 +00:00
|
|
|
|
ut8 *tbuf, *buf;
|
2016-04-21 19:19:01 +00:00
|
|
|
|
RListIter *tmp = NULL;
|
|
|
|
|
RAnalBlock *bb = NULL;
|
2016-12-09 14:52:53 +00:00
|
|
|
|
RAnalOp *op = NULL;
|
|
|
|
|
ut64 pos;
|
2016-06-17 12:26:24 +00:00
|
|
|
|
|
|
|
|
|
if (!core || !core->anal || !fcn || core->anal->opt.bb_max_size < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-17 16:38:25 +00:00
|
|
|
|
int bb_size = core->anal->opt.bb_max_size;
|
|
|
|
|
buf = calloc (1, bb_size);
|
|
|
|
|
if (!buf) {
|
2016-04-21 19:19:01 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
r_list_foreach (fcn->bbs, tmp, bb) {
|
2016-06-17 16:38:25 +00:00
|
|
|
|
if (bb->size < 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (bb->size > bb_size) {
|
|
|
|
|
tbuf = realloc (buf, bb->size);
|
|
|
|
|
if (!tbuf) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
buf = tbuf;
|
|
|
|
|
bb_size = bb->size;
|
2016-06-17 12:26:24 +00:00
|
|
|
|
}
|
2016-04-21 19:19:01 +00:00
|
|
|
|
if (r_io_read_at (core->io, bb->addr, buf, bb->size) != bb->size) {
|
2016-12-26 04:06:43 +00:00
|
|
|
|
// eprintf ("read error\n");
|
2016-06-17 12:26:24 +00:00
|
|
|
|
break;
|
2016-04-21 19:19:01 +00:00
|
|
|
|
}
|
2016-12-09 14:52:53 +00:00
|
|
|
|
pos = bb->addr;
|
|
|
|
|
while (pos < bb->addr + bb->size) {
|
|
|
|
|
op = r_core_anal_op (core, pos);
|
|
|
|
|
if (!op) {
|
2016-12-26 04:06:43 +00:00
|
|
|
|
// eprintf ("Cannot get op\n");
|
2016-06-17 16:38:25 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2016-12-09 14:52:53 +00:00
|
|
|
|
fill_args (core->anal, fcn, op);
|
2017-02-02 12:08:08 +00:00
|
|
|
|
int opsize = op->size;
|
2016-12-09 14:52:53 +00:00
|
|
|
|
r_anal_op_free (op);
|
2017-02-02 12:08:08 +00:00
|
|
|
|
if (opsize < 1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pos += opsize;
|
2016-04-21 19:19:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 14:04:54 +00:00
|
|
|
|
|
2016-04-21 19:19:01 +00:00
|
|
|
|
free (buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-14 00:18:15 +00:00
|
|
|
|
R_API RList* r_core_anal_graph_to(RCore *core, ut64 addr, int n) {
|
2012-08-08 15:19:48 +00:00
|
|
|
|
RAnalBlock *bb, *root = NULL, *dest = NULL;
|
2012-06-14 00:18:15 +00:00
|
|
|
|
RListIter *iter, *iter2;
|
2012-08-08 15:19:48 +00:00
|
|
|
|
RList *list2 = NULL, *list = NULL;
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcn;
|
2012-06-14 00:18:15 +00:00
|
|
|
|
|
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcn) {
|
2016-08-21 09:39:37 +00:00
|
|
|
|
if (!r_anal_fcn_is_in_offset (fcn, core->offset)) {
|
2012-06-14 00:18:15 +00:00
|
|
|
|
continue;
|
2016-08-21 09:39:37 +00:00
|
|
|
|
}
|
2012-06-14 00:18:15 +00:00
|
|
|
|
r_list_foreach (fcn->bbs, iter2, bb) {
|
|
|
|
|
if (r_anal_bb_is_in_offset (bb, addr)) {
|
|
|
|
|
dest = bb;
|
|
|
|
|
}
|
|
|
|
|
if (r_anal_bb_is_in_offset (bb, core->offset)) {
|
|
|
|
|
root = bb;
|
|
|
|
|
r_list_append (list, list2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (root && dest) {
|
|
|
|
|
if (dest == root) {
|
|
|
|
|
eprintf ("Source and destination are the same\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
eprintf ("ROOT BB 0x%08"PFMT64x"\n", root->addr);
|
|
|
|
|
eprintf ("DEST BB 0x%08"PFMT64x"\n", dest->addr);
|
|
|
|
|
list = r_list_new ();
|
|
|
|
|
printf ("=> 0x%08"PFMT64x"\n", root->jump);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
eprintf ("Unable to find source or destination basic block\n");
|
|
|
|
|
}
|
2012-06-14 00:18:15 +00:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-24 00:24:27 +00:00
|
|
|
|
R_API int r_core_anal_graph(RCore *core, ut64 addr, int opts) {
|
2014-12-22 03:16:50 +00:00
|
|
|
|
ut64 from = r_config_get_i (core->config, "graph.from");
|
|
|
|
|
ut64 to = r_config_get_i (core->config, "graph.to");
|
2012-09-05 01:25:03 +00:00
|
|
|
|
const char *font = r_config_get (core->config, "graph.font");
|
|
|
|
|
int is_html = r_cons_singleton ()->is_html;
|
2013-01-24 02:48:24 +00:00
|
|
|
|
int is_json = opts & R_CORE_ANAL_JSON;
|
2014-04-27 00:48:42 +00:00
|
|
|
|
int is_keva = opts & R_CORE_ANAL_KEYVALUE;
|
2016-11-23 17:36:11 +00:00
|
|
|
|
RConfigHold *hc;
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcni;
|
2011-02-04 13:03:59 +00:00
|
|
|
|
RListIter *iter;
|
2016-03-30 10:28:44 +00:00
|
|
|
|
int nodes = 0;
|
2013-02-07 08:41:05 +00:00
|
|
|
|
int count = 0;
|
2010-02-27 10:56:41 +00:00
|
|
|
|
|
2016-03-22 00:55:37 +00:00
|
|
|
|
if (!addr) {
|
|
|
|
|
addr = core->offset;
|
|
|
|
|
}
|
2015-05-19 15:41:09 +00:00
|
|
|
|
if (r_list_empty (core->anal->fcns)) {
|
|
|
|
|
eprintf ("No functions to diff\n");
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2015-05-19 15:41:09 +00:00
|
|
|
|
}
|
2016-11-23 17:36:11 +00:00
|
|
|
|
hc = r_config_hold_new (core->config);
|
|
|
|
|
if (!hc) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
r_config_save_num (hc, "asm.lines", "asm.bytes", "asm.dwarf", NULL);
|
2016-03-22 00:55:37 +00:00
|
|
|
|
//opts |= R_CORE_ANAL_GRAPHBODY;
|
2010-05-19 22:59:42 +00:00
|
|
|
|
r_config_set_i (core->config, "asm.lines", 0);
|
|
|
|
|
r_config_set_i (core->config, "asm.bytes", 0);
|
|
|
|
|
r_config_set_i (core->config, "asm.dwarf", 0);
|
2016-03-31 00:12:53 +00:00
|
|
|
|
if (!is_html && !is_json && !is_keva) {
|
2016-03-31 00:25:54 +00:00
|
|
|
|
const char * gv_edge = r_config_get (core->config, "graph.gv.edge");
|
|
|
|
|
const char * gv_node = r_config_get (core->config, "graph.gv.node");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!gv_edge || !*gv_edge) {
|
2016-03-31 00:25:54 +00:00
|
|
|
|
gv_edge = "arrowhead=\"vee\"";
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-03-31 00:25:54 +00:00
|
|
|
|
if (!gv_node || !*gv_node) {
|
2016-09-22 21:57:16 +00:00
|
|
|
|
gv_node = "fillcolor=gray style=filled shape=box";
|
2016-03-31 00:25:54 +00:00
|
|
|
|
}
|
2014-04-27 00:48:42 +00:00
|
|
|
|
r_cons_printf ("digraph code {\n"
|
2016-03-31 00:25:54 +00:00
|
|
|
|
"\tgraph [bgcolor=white fontsize=8 fontname=\"%s\"];\n"
|
|
|
|
|
"\tnode [%s];\n"
|
|
|
|
|
"\tedge [%s];\n", font, gv_node, gv_edge);
|
2016-03-31 00:12:53 +00:00
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (is_json) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf ("[");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcni) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (fcni->type & (R_ANAL_FCN_TYPE_SYM | R_ANAL_FCN_TYPE_FCN) &&
|
|
|
|
|
(!addr || r_anal_fcn_in (fcni, addr))) {
|
2014-12-22 03:16:50 +00:00
|
|
|
|
if (!addr && (from != UT64_MAX && to != UT64_MAX)) {
|
|
|
|
|
if (fcni->addr < from || fcni->addr > to) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (is_json && count++ > 0) {
|
|
|
|
|
r_cons_printf (",");
|
|
|
|
|
}
|
2016-03-30 10:28:44 +00:00
|
|
|
|
nodes += core_anal_graph_nodes (core, fcni, opts);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (addr != 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-01-24 02:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-30 10:28:44 +00:00
|
|
|
|
if (!nodes) {
|
|
|
|
|
if (!is_html && !is_json && !is_keva) {
|
|
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, addr, 0);
|
|
|
|
|
r_cons_printf ("\t\"0x%08"PFMT64x"\";\n", fcn? fcn->addr: addr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!is_keva && !is_html && !is_json) {
|
|
|
|
|
r_cons_printf ("}\n");
|
|
|
|
|
}
|
|
|
|
|
if (is_json) {
|
2013-01-24 02:48:24 +00:00
|
|
|
|
r_cons_printf ("]\n");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-11-23 17:36:11 +00:00
|
|
|
|
r_config_restore (hc);
|
|
|
|
|
r_config_hold_free (hc);
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2010-02-27 10:56:41 +00:00
|
|
|
|
}
|
2010-03-03 13:35:18 +00:00
|
|
|
|
|
2016-02-09 17:52:26 +00:00
|
|
|
|
static int core_anal_followptr(RCore *core, int type, ut64 at, ut64 ptr, ut64 ref, int code, int depth) {
|
2010-08-19 00:30:12 +00:00
|
|
|
|
ut64 dataptr;
|
2016-04-26 09:09:15 +00:00
|
|
|
|
int wordsize;
|
2016-12-25 22:37:21 +00:00
|
|
|
|
// SLOW Operation try to reduce as much as possible -- eprintf ("READ %d %llx\n", wordsize, ptr);
|
|
|
|
|
if (!ptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-08 09:52:10 +00:00
|
|
|
|
if (ptr == ref) {
|
2016-02-21 23:22:52 +00:00
|
|
|
|
if (code) {
|
|
|
|
|
r_anal_ref_add (core->anal, ref, at, type? type: 'c');
|
|
|
|
|
} else {
|
|
|
|
|
r_anal_ref_add (core->anal, ref, at, 'd');
|
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2010-09-08 09:52:10 +00:00
|
|
|
|
}
|
2016-05-14 08:39:39 +00:00
|
|
|
|
if (depth < 1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2016-05-14 08:39:39 +00:00
|
|
|
|
}
|
|
|
|
|
wordsize = (int)(core->anal->bits / 8);
|
|
|
|
|
if ((dataptr = r_io_read_i (core->io, ptr, wordsize)) == -1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2016-05-14 08:39:39 +00:00
|
|
|
|
}
|
2016-02-09 17:52:26 +00:00
|
|
|
|
return core_anal_followptr (core, type, at, dataptr, ref, code, depth - 1);
|
2010-08-19 00:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define OPSZ 8
|
2010-06-24 22:21:22 +00:00
|
|
|
|
R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) {
|
|
|
|
|
ut8 *buf = (ut8 *)malloc (core->blocksize);
|
2010-08-19 00:30:12 +00:00
|
|
|
|
int ptrdepth = r_config_get_i (core->config, "anal.ptrdepth");
|
2010-06-24 22:21:22 +00:00
|
|
|
|
int ret, i, count = 0;
|
2011-11-13 23:21:25 +00:00
|
|
|
|
RAnalOp op = {0};
|
2010-08-19 00:30:12 +00:00
|
|
|
|
ut64 at;
|
2014-02-27 09:38:24 +00:00
|
|
|
|
char bckwrds, do_bckwrd_srch;
|
2010-06-24 22:21:22 +00:00
|
|
|
|
// TODO: get current section range here or gtfo
|
|
|
|
|
// ???
|
|
|
|
|
// XXX must read bytes correctly
|
2014-02-27 09:38:24 +00:00
|
|
|
|
do_bckwrd_srch = bckwrds = core->search->bckwrds;
|
2016-08-28 23:09:10 +00:00
|
|
|
|
if (!buf) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-05-28 02:34:12 +00:00
|
|
|
|
r_io_use_desc (core->io, core->file->desc);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!ref) {
|
2010-06-24 22:21:22 +00:00
|
|
|
|
eprintf ("Null reference search is not supported\n");
|
2014-12-23 09:52:01 +00:00
|
|
|
|
free (buf);
|
2014-12-22 00:55:17 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_push (NULL, NULL);
|
2016-04-07 09:10:43 +00:00
|
|
|
|
if (core->blocksize > OPSZ) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
if (bckwrds) {
|
|
|
|
|
if (from + core->blocksize > to) {
|
2014-02-27 09:38:24 +00:00
|
|
|
|
at = from;
|
2015-09-14 10:35:38 +00:00
|
|
|
|
do_bckwrd_srch = false;
|
2015-10-14 12:06:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
at = to - core->blocksize;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
at = from;
|
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
while ((!bckwrds && at < to) || bckwrds) {
|
2017-01-16 15:57:42 +00:00
|
|
|
|
eprintf ("\r[0x%08"PFMT64x"-0x%08"PFMT64x"] ", at, to);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2010-08-19 00:30:12 +00:00
|
|
|
|
break;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2013-02-13 16:54:20 +00:00
|
|
|
|
// TODO: this can be probably enhaced
|
2010-08-19 00:30:12 +00:00
|
|
|
|
ret = r_io_read_at (core->io, at, buf, core->blocksize);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (ret != core->blocksize) {
|
2010-08-19 00:30:12 +00:00
|
|
|
|
break;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-02-09 17:52:26 +00:00
|
|
|
|
for (i = bckwrds ? (core->blocksize - OPSZ - 1) : 0;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
(!bckwrds && i < core->blocksize - OPSZ) ||
|
|
|
|
|
(bckwrds && i > 0);
|
|
|
|
|
bckwrds ? i-- : i++) {
|
|
|
|
|
if (r_cons_is_breaked ()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-11-13 23:21:25 +00:00
|
|
|
|
r_anal_op_fini (&op);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!r_anal_op (core->anal, &op, at + i,
|
|
|
|
|
buf + i, core->blocksize - i)) {
|
2010-08-19 00:30:12 +00:00
|
|
|
|
continue;
|
2016-05-14 08:39:39 +00:00
|
|
|
|
}
|
2015-10-14 12:06:46 +00:00
|
|
|
|
switch (op.type) {
|
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_CCALL:
|
2010-09-08 09:52:10 +00:00
|
|
|
|
if (op.jump != -1 &&
|
2016-02-09 17:52:26 +00:00
|
|
|
|
core_anal_followptr (core, 'C',
|
2015-10-14 12:06:46 +00:00
|
|
|
|
at + i, op.jump, ref,
|
|
|
|
|
true, 0)) {
|
2010-09-08 09:52:10 +00:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
2015-10-14 12:06:46 +00:00
|
|
|
|
break;
|
2016-02-09 17:52:26 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCJMP:
|
2015-10-14 12:06:46 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_IJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_RJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRJMP:
|
2016-05-29 22:38:35 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_MJMP:
|
2016-02-09 17:52:26 +00:00
|
|
|
|
if (op.ptr != -1 &&
|
|
|
|
|
core_anal_followptr (core, 'c',
|
|
|
|
|
at + i, op.ptr, ref,
|
|
|
|
|
true ,1)) {
|
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2015-10-14 12:06:46 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCALL:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_ICALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_RCALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRCALL:
|
2015-10-14 12:06:46 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCCALL:
|
2013-06-09 01:25:32 +00:00
|
|
|
|
if (op.ptr != -1 &&
|
2016-02-09 17:52:26 +00:00
|
|
|
|
core_anal_followptr (core, 'C',
|
2015-10-14 12:06:46 +00:00
|
|
|
|
at + i, op.ptr, ref,
|
|
|
|
|
true ,1)) {
|
2010-09-08 09:52:10 +00:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
2015-10-14 12:06:46 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2013-06-09 01:25:32 +00:00
|
|
|
|
if (op.ptr != -1 &&
|
2016-02-09 17:52:26 +00:00
|
|
|
|
core_anal_followptr (core, 'd',
|
2016-05-14 08:39:39 +00:00
|
|
|
|
at + i, op.ptr, ref,
|
2015-09-14 10:35:38 +00:00
|
|
|
|
false, ptrdepth)) {
|
2010-09-08 09:52:10 +00:00
|
|
|
|
count ++;
|
|
|
|
|
}
|
2015-10-14 12:06:46 +00:00
|
|
|
|
break;
|
2010-08-19 00:30:12 +00:00
|
|
|
|
}
|
2016-12-26 04:02:06 +00:00
|
|
|
|
i += op.size - 1;
|
2010-06-24 22:21:22 +00:00
|
|
|
|
}
|
2014-12-22 00:55:17 +00:00
|
|
|
|
if (bckwrds) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!do_bckwrd_srch) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-12-22 00:55:17 +00:00
|
|
|
|
if (at > from + core->blocksize - OPSZ) {
|
|
|
|
|
at -= core->blocksize;
|
|
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
do_bckwrd_srch = false;
|
2014-02-27 09:38:24 +00:00
|
|
|
|
at = from;
|
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
} else {
|
|
|
|
|
at += core->blocksize - OPSZ;
|
|
|
|
|
}
|
2010-06-24 22:21:22 +00:00
|
|
|
|
}
|
2015-10-14 12:06:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
eprintf ("error: block size too small\n");
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_pop ();
|
2010-06-24 22:21:22 +00:00
|
|
|
|
free (buf);
|
2011-11-13 23:21:25 +00:00
|
|
|
|
r_anal_op_fini (&op);
|
2010-06-24 22:21:22 +00:00
|
|
|
|
return count;
|
|
|
|
|
}
|
2010-09-28 16:05:31 +00:00
|
|
|
|
|
2015-07-11 04:53:58 +00:00
|
|
|
|
R_API int r_core_anal_search_xrefs(RCore *core, ut64 from, ut64 to, int rad) {
|
2015-09-07 16:20:29 +00:00
|
|
|
|
int cfg_debug = r_config_get_i (core->config, "cfg.debug");
|
2016-03-14 08:42:54 +00:00
|
|
|
|
bool cfg_anal_strings = r_config_get_i (core->config, "anal.strings");
|
2015-07-11 04:53:58 +00:00
|
|
|
|
ut8 *buf;
|
|
|
|
|
ut64 at;
|
|
|
|
|
int count = 0;
|
2016-04-07 09:10:43 +00:00
|
|
|
|
RAnalOp op = { 0 };
|
2015-09-23 15:57:40 +00:00
|
|
|
|
if (from == to) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (from > to) {
|
|
|
|
|
eprintf ("Invalid range (0x%"PFMT64x
|
|
|
|
|
" >= 0x%"PFMT64x")\n", from, to);
|
2015-07-11 04:53:58 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (core->blocksize <= OPSZ) {
|
|
|
|
|
eprintf ("Error: block size too small\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
buf = (ut8 *)malloc (core->blocksize);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
eprintf ("Error: cannot allocate a block\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (rad == 'j') {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
r_cons_printf ("{");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
r_io_use_desc (core->io, core->file->desc);
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_push (NULL, NULL);
|
2015-07-11 04:53:58 +00:00
|
|
|
|
at = from;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
while (at < to && !r_cons_is_breaked ()) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
int i, ret;
|
|
|
|
|
ret = r_io_read_at (core->io, at, buf, core->blocksize);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (ret != core->blocksize && at + ret-OPSZ < to) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
break;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
i = 0;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
while (at + i < to && i < ret-OPSZ && !r_cons_is_breaked ()) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
RAnalRefType type;
|
|
|
|
|
ut64 xref_from, xref_to;
|
2016-12-05 17:46:45 +00:00
|
|
|
|
xref_from = at + i;
|
2015-07-11 04:53:58 +00:00
|
|
|
|
r_anal_op_fini (&op);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
ret = r_anal_op (core->anal, &op, at + i, buf + i, core->blocksize - i);
|
2015-07-11 04:53:58 +00:00
|
|
|
|
i += (ret > 0) ? ret : 1;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (ret <= 0 || at + i > to) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
// Get reference type and target address
|
|
|
|
|
type = R_ANAL_REF_TYPE_NULL;
|
|
|
|
|
switch (op.type) {
|
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
|
type = R_ANAL_REF_TYPE_CODE;
|
|
|
|
|
xref_to = op.jump;
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_CCALL:
|
|
|
|
|
type = R_ANAL_REF_TYPE_CALL;
|
|
|
|
|
xref_to = op.jump;
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_IJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_RJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRJMP:
|
2016-05-29 22:38:35 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_MJMP:
|
2015-07-11 04:53:58 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCJMP:
|
|
|
|
|
type = R_ANAL_REF_TYPE_CODE;
|
|
|
|
|
xref_to = op.ptr;
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_UCALL:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_ICALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_RCALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRCALL:
|
2015-07-11 04:53:58 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCCALL:
|
|
|
|
|
type = R_ANAL_REF_TYPE_CALL;
|
|
|
|
|
xref_to = op.ptr;
|
|
|
|
|
break;
|
2015-08-09 01:23:40 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_LOAD:
|
|
|
|
|
type = R_ANAL_REF_TYPE_DATA;
|
|
|
|
|
xref_to = op.ptr;
|
|
|
|
|
break;
|
2015-07-11 04:53:58 +00:00
|
|
|
|
default:
|
|
|
|
|
if (op.ptr != -1) {
|
|
|
|
|
type = R_ANAL_REF_TYPE_DATA;
|
|
|
|
|
xref_to = op.ptr;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate the reference. If virtual addressing is enabled, we
|
|
|
|
|
// allow only references to virtual addresses in order to reduce
|
|
|
|
|
// the number of false positives. In debugger mode, the reference
|
|
|
|
|
// must point to a mapped memory region.
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (type == R_ANAL_REF_TYPE_NULL) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (!r_core_is_valid_offset (core, xref_to)) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-09-07 16:20:29 +00:00
|
|
|
|
if (cfg_debug) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!r_debug_map_get (core->dbg, xref_to)) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
} else if (core->io->va) {
|
2015-07-17 09:10:28 +00:00
|
|
|
|
RListIter *iter = NULL;
|
2015-07-11 04:53:58 +00:00
|
|
|
|
RIOSection *s;
|
|
|
|
|
r_list_foreach (core->io->sections, iter, s) {
|
|
|
|
|
if (xref_to >= s->vaddr && xref_to < s->vaddr + s->vsize) {
|
2016-11-20 18:20:14 +00:00
|
|
|
|
if (s->vaddr) break;
|
2015-07-11 04:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!iter) {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
continue;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
if (!rad) {
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (cfg_anal_strings && type == R_ANAL_REF_TYPE_DATA) {
|
2016-03-14 08:42:54 +00:00
|
|
|
|
int len = 0;
|
|
|
|
|
char *str_string = is_string_at (core, xref_to, &len);
|
|
|
|
|
if (str_string) {
|
|
|
|
|
r_name_filter (str_string, -1);
|
|
|
|
|
char *str_flagname = r_str_newf ("str.%s", str_string);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
r_flag_space_push (core->flags, "strings");
|
2016-03-14 08:42:54 +00:00
|
|
|
|
(void)r_flag_set (core->flags, str_flagname, xref_to, 1);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
r_flag_space_pop (core->flags);
|
2016-03-14 08:42:54 +00:00
|
|
|
|
}
|
|
|
|
|
if (len > 0) {
|
2016-12-05 17:46:45 +00:00
|
|
|
|
r_meta_add (core->anal, R_META_TYPE_STRING, xref_to,
|
|
|
|
|
xref_to + len, (const char *)str_string);
|
2016-03-14 08:42:54 +00:00
|
|
|
|
}
|
2016-12-05 17:46:45 +00:00
|
|
|
|
free (str_string);
|
2016-03-14 00:45:25 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
// Add to SDB
|
|
|
|
|
r_anal_xrefs_set (core->anal, type, xref_from, xref_to);
|
|
|
|
|
} else if (rad == 'j') {
|
|
|
|
|
// Output JSON
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (count > 0) {
|
|
|
|
|
r_cons_printf (",");
|
|
|
|
|
}
|
2015-10-27 11:10:32 +00:00
|
|
|
|
r_cons_printf ("\"0x%"PFMT64x"\":\"0x%"PFMT64x"\"", xref_to, xref_from);
|
2015-07-11 04:53:58 +00:00
|
|
|
|
} else {
|
2016-03-14 00:45:25 +00:00
|
|
|
|
int len = 0;
|
2015-07-11 04:53:58 +00:00
|
|
|
|
// Display in radare commands format
|
|
|
|
|
char *cmd;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case R_ANAL_REF_TYPE_CODE: cmd = "axc"; break;
|
|
|
|
|
case R_ANAL_REF_TYPE_CALL: cmd = "axC"; break;
|
|
|
|
|
case R_ANAL_REF_TYPE_DATA: cmd = "axd"; break;
|
|
|
|
|
default: cmd = "ax"; break;
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_printf ("%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n", cmd, xref_to, xref_from);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
if (cfg_anal_strings && type == R_ANAL_REF_TYPE_DATA) {
|
2016-03-14 08:42:54 +00:00
|
|
|
|
char *str_flagname = is_string_at (core, xref_to, &len);
|
|
|
|
|
if (str_flagname) {
|
|
|
|
|
ut64 str_addr = xref_to;
|
|
|
|
|
r_name_filter (str_flagname, -1);
|
2016-12-05 17:46:45 +00:00
|
|
|
|
r_cons_printf ("f str.%s=0x%"PFMT64x"\n", str_flagname, str_addr);
|
|
|
|
|
r_cons_printf ("Cs %d @ 0x%"PFMT64x"\n", len, str_addr);
|
2016-03-14 08:42:54 +00:00
|
|
|
|
free (str_flagname);
|
|
|
|
|
}
|
2016-03-14 00:45:25 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
at += i;
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_pop ();
|
2015-07-11 04:53:58 +00:00
|
|
|
|
free (buf);
|
|
|
|
|
r_anal_op_fini (&op);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (rad == 'j') {
|
2015-07-11 04:53:58 +00:00
|
|
|
|
r_cons_printf ("}\n");
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2015-07-11 04:53:58 +00:00
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-28 16:05:31 +00:00
|
|
|
|
R_API int r_core_anal_ref_list(RCore *core, int rad) {
|
2013-10-24 11:59:19 +00:00
|
|
|
|
r_anal_xrefs_list (core->anal, rad);
|
2013-07-19 01:35:45 +00:00
|
|
|
|
return 0;
|
2010-09-28 16:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 10:05:46 +00:00
|
|
|
|
static bool isValidSymbol(RBinSymbol *symbol) {
|
|
|
|
|
if (symbol && symbol->type) {
|
|
|
|
|
const char *type = symbol->type;
|
|
|
|
|
return (!strcmp (type, "FUNC") || !strcmp (type, "METH"));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-27 03:20:19 +00:00
|
|
|
|
R_API int r_core_anal_all(RCore *core) {
|
|
|
|
|
RList *list;
|
|
|
|
|
RListIter *iter;
|
2014-12-17 11:09:33 +00:00
|
|
|
|
RFlagItem *item;
|
2012-07-22 08:00:35 +00:00
|
|
|
|
RAnalFunction *fcni;
|
2010-11-27 03:20:19 +00:00
|
|
|
|
RBinAddr *binmain;
|
|
|
|
|
RBinAddr *entry;
|
|
|
|
|
RBinSymbol *symbol;
|
2014-12-17 11:09:33 +00:00
|
|
|
|
int depth = r_config_get_i (core->config, "anal.depth");
|
2016-06-25 23:52:53 +00:00
|
|
|
|
bool anal_vars = r_config_get_i (core->config, "anal.vars");
|
2010-11-27 03:20:19 +00:00
|
|
|
|
|
|
|
|
|
/* Analyze Functions */
|
|
|
|
|
/* Entries */
|
2014-12-17 11:09:33 +00:00
|
|
|
|
item = r_flag_get (core->flags, "entry0");
|
2013-08-21 22:11:43 +00:00
|
|
|
|
if (item) {
|
2011-11-21 23:59:20 +00:00
|
|
|
|
r_core_anal_fcn (core, item->offset, -1, R_ANAL_REF_TYPE_NULL, depth);
|
2014-11-13 17:19:42 +00:00
|
|
|
|
r_core_cmdf (core, "afn entry0 0x%08"PFMT64x, item->offset);
|
2013-08-21 22:11:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
r_core_cmd0 (core, "af");
|
|
|
|
|
}
|
2015-10-13 20:58:42 +00:00
|
|
|
|
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_push (NULL, NULL);
|
2013-10-19 22:25:37 +00:00
|
|
|
|
/* Main */
|
2014-11-13 15:31:03 +00:00
|
|
|
|
if ((binmain = r_bin_get_sym (core->bin, R_BIN_SYM_MAIN)) != NULL) {
|
2015-10-19 21:10:49 +00:00
|
|
|
|
ut64 addr = r_bin_get_vaddr (core->bin, binmain->paddr, binmain->vaddr);
|
2014-11-13 15:31:03 +00:00
|
|
|
|
r_core_anal_fcn (core, addr, -1, R_ANAL_REF_TYPE_NULL, depth);
|
|
|
|
|
}
|
2015-10-19 21:10:49 +00:00
|
|
|
|
if ((list = r_bin_get_entries (core->bin)) != NULL) {
|
|
|
|
|
r_list_foreach (list, iter, entry) {
|
|
|
|
|
ut64 addr = r_bin_get_vaddr (core->bin, entry->paddr, entry->vaddr);
|
|
|
|
|
r_core_anal_fcn (core, addr, -1, R_ANAL_REF_TYPE_NULL, depth);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-29 03:28:37 +00:00
|
|
|
|
/* Symbols (Imports are already analyzed by rabin2 on init) */
|
2015-10-19 21:10:49 +00:00
|
|
|
|
if ((list = r_bin_get_symbols (core->bin)) != NULL) {
|
2011-12-05 08:55:44 +00:00
|
|
|
|
r_list_foreach (list, iter, symbol) {
|
2016-11-20 18:20:14 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2011-12-05 08:55:44 +00:00
|
|
|
|
break;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-09-28 10:05:46 +00:00
|
|
|
|
if (isValidSymbol (symbol)) {
|
2015-10-19 21:10:49 +00:00
|
|
|
|
ut64 addr = r_bin_get_vaddr (core->bin, symbol->paddr,
|
|
|
|
|
symbol->vaddr);
|
|
|
|
|
r_core_anal_fcn (core, addr, -1,
|
|
|
|
|
R_ANAL_REF_TYPE_NULL, depth);
|
2015-08-25 22:30:46 +00:00
|
|
|
|
}
|
2011-12-05 08:55:44 +00:00
|
|
|
|
}
|
2015-10-19 21:10:49 +00:00
|
|
|
|
}
|
2016-06-25 23:52:53 +00:00
|
|
|
|
if (anal_vars) {
|
|
|
|
|
/* Set fcn type to R_ANAL_FCN_TYPE_SYM for symbols */
|
|
|
|
|
r_list_foreach (core->anal->fcns, iter, fcni) {
|
2016-11-20 18:20:14 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2016-06-25 23:52:53 +00:00
|
|
|
|
break;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
}
|
2016-07-01 14:15:29 +00:00
|
|
|
|
if (r_config_get_i (core->config, "anal.vars")) {
|
|
|
|
|
r_anal_var_delete_all (core->anal, fcni->addr, 'r');
|
|
|
|
|
r_anal_var_delete_all (core->anal, fcni->addr, 'b');
|
|
|
|
|
r_anal_var_delete_all (core->anal, fcni->addr, 's');
|
|
|
|
|
fcn_callconv (core, fcni);
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
if (!strncmp (fcni->name, "sym.", 4) || !strncmp (fcni->name, "main", 4)) {
|
2016-06-25 23:52:53 +00:00
|
|
|
|
fcni->type = R_ANAL_FCN_TYPE_SYM;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
}
|
2016-06-25 23:52:53 +00:00
|
|
|
|
}
|
2011-12-05 08:55:44 +00:00
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_pop ();
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2010-11-27 03:20:19 +00:00
|
|
|
|
}
|
2012-09-02 23:27:52 +00:00
|
|
|
|
|
|
|
|
|
R_API void r_core_anal_setup_enviroment (RCore *core) {
|
|
|
|
|
char key[128], *str = NULL;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RConfigNode *kv;
|
|
|
|
|
r_list_foreach (core->config->nodes, iter, kv) {
|
2014-01-18 22:02:53 +00:00
|
|
|
|
int kvlen = strlen (kv->name);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (kvlen >= sizeof (key)) {
|
2014-01-18 22:02:53 +00:00
|
|
|
|
return;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-01-18 22:02:53 +00:00
|
|
|
|
strcpy (key, kv->name);
|
2012-09-02 23:27:52 +00:00
|
|
|
|
r_str_case (key, 1);
|
|
|
|
|
r_str_replace_char (key, '.', '_');
|
|
|
|
|
#define RANAL_PARSE_STRING_ONLY 1
|
|
|
|
|
#if RANAL_PARSE_STRING_ONLY
|
|
|
|
|
r_anal_type_define (core->anal, key, kv->value);
|
|
|
|
|
#else
|
|
|
|
|
if (kv->flags & CN_INT) {
|
|
|
|
|
r_anal_type_define_i (core->anal, key, kv->i_value);
|
|
|
|
|
} else if (kv->flags & CN_BOOL) {
|
|
|
|
|
r_anal_type_define (core->anal, key, kv->i_value? "": NULL);
|
2014-01-18 22:02:53 +00:00
|
|
|
|
} else r_anal_type_define (core->anal, key, kv->value);
|
2012-09-02 23:27:52 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
r_anal_type_header (core->anal, str);
|
|
|
|
|
free (str);
|
|
|
|
|
}
|
2012-11-20 02:59:00 +00:00
|
|
|
|
|
|
|
|
|
R_API int r_core_anal_data (RCore *core, ut64 addr, int count, int depth) {
|
2014-01-19 23:14:00 +00:00
|
|
|
|
RAnalData *d;
|
2012-11-20 02:59:00 +00:00
|
|
|
|
ut64 dstaddr = 0LL;
|
|
|
|
|
ut8 *buf = core->block;
|
|
|
|
|
int len = core->blocksize;
|
|
|
|
|
int word = core->assembler->bits /8;
|
2013-03-03 04:03:48 +00:00
|
|
|
|
char *str;
|
2015-07-31 10:40:04 +00:00
|
|
|
|
int i, j;
|
2012-11-20 02:59:00 +00:00
|
|
|
|
|
2015-01-14 01:00:55 +00:00
|
|
|
|
count = R_MIN (count, len);
|
2016-10-20 11:47:56 +00:00
|
|
|
|
buf = malloc (len + 1);
|
|
|
|
|
if (!buf) {
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return false;
|
2016-10-20 11:47:56 +00:00
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
memset (buf, 0xff, len);
|
|
|
|
|
r_io_read_at (core->io, addr, buf, len);
|
|
|
|
|
buf[len-1] = 0;
|
2012-11-20 02:59:00 +00:00
|
|
|
|
|
2016-10-20 11:47:56 +00:00
|
|
|
|
for (i = j = 0; j < count; j++ ) {
|
|
|
|
|
if (i >= len) {
|
2016-04-26 09:09:15 +00:00
|
|
|
|
r_io_read_at (core->io, addr + i, buf, len);
|
2016-10-20 11:47:56 +00:00
|
|
|
|
buf[len] = 0;
|
2013-02-26 21:03:02 +00:00
|
|
|
|
addr += i;
|
2013-02-21 10:31:04 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-10-20 11:47:56 +00:00
|
|
|
|
/* r_anal_data requires null-terminated buffer according to coverity */
|
|
|
|
|
/* but it should not.. so this must be fixed in anal/data.c instead of */
|
|
|
|
|
/* null terminating here */
|
2016-04-26 09:09:15 +00:00
|
|
|
|
d = r_anal_data (core->anal, addr + i, buf + i, len - i);
|
2012-11-20 11:17:46 +00:00
|
|
|
|
str = r_anal_data_to_string (d);
|
2016-06-26 04:51:17 +00:00
|
|
|
|
r_cons_println (str);
|
2014-04-29 01:36:04 +00:00
|
|
|
|
|
2014-10-15 00:24:22 +00:00
|
|
|
|
if (d) {
|
|
|
|
|
switch (d->type) {
|
2016-04-07 09:10:43 +00:00
|
|
|
|
case R_ANAL_DATA_TYPE_POINTER:
|
|
|
|
|
r_cons_printf ("`- ");
|
2016-04-26 09:09:15 +00:00
|
|
|
|
dstaddr = r_mem_get_num (buf + i, word);
|
2016-10-20 11:47:56 +00:00
|
|
|
|
if (depth > 0) {
|
2016-04-26 09:09:15 +00:00
|
|
|
|
r_core_anal_data (core, dstaddr, 1, depth - 1);
|
2016-10-20 11:47:56 +00:00
|
|
|
|
}
|
2016-04-07 09:10:43 +00:00
|
|
|
|
i += word;
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_DATA_TYPE_STRING:
|
|
|
|
|
buf[len-1] = 0;
|
2016-04-26 09:09:15 +00:00
|
|
|
|
i += strlen ((const char*)buf + i) + 1;
|
2016-04-07 09:10:43 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
i += (d->len > 3)? d->len: word;
|
|
|
|
|
break;
|
2014-10-15 00:24:22 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2012-11-20 02:59:00 +00:00
|
|
|
|
i += word;
|
|
|
|
|
}
|
2012-11-20 11:17:46 +00:00
|
|
|
|
free (str);
|
|
|
|
|
r_anal_data_free (d);
|
2015-07-31 10:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
free (buf);
|
2015-09-14 10:35:38 +00:00
|
|
|
|
return true;
|
2012-11-20 02:59:00 +00:00
|
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
|
|
|
|
|
|
/* core analysis stats */
|
|
|
|
|
/* stats --- colorful bar */
|
2016-11-01 18:42:43 +00:00
|
|
|
|
R_API RCoreAnalStats* r_core_anal_get_stats(RCore *core, ut64 from, ut64 to, ut64 step) {
|
2013-02-06 09:35:23 +00:00
|
|
|
|
RFlagItem *f;
|
2013-02-07 02:09:53 +00:00
|
|
|
|
RAnalFunction *F;
|
2017-01-29 17:21:29 +00:00
|
|
|
|
RBinSymbol *S;
|
2013-02-06 09:35:23 +00:00
|
|
|
|
RListIter *iter;
|
2015-07-19 19:12:35 +00:00
|
|
|
|
RCoreAnalStats *as = NULL;
|
2013-02-10 02:18:20 +00:00
|
|
|
|
int piece, as_size, blocks;
|
2016-12-02 19:44:32 +00:00
|
|
|
|
ut64 at;
|
2015-07-19 19:12:35 +00:00
|
|
|
|
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (from == to) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-07-19 19:12:35 +00:00
|
|
|
|
as = R_NEW0 (RCoreAnalStats);
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (!as) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (step < 1) {
|
|
|
|
|
step = 1;
|
|
|
|
|
}
|
2016-12-02 20:08:09 +00:00
|
|
|
|
blocks = (to - from) / step;
|
2016-12-02 19:44:32 +00:00
|
|
|
|
as_size = (1 + blocks) * sizeof (RCoreAnalStatsItem);
|
2013-02-06 09:35:23 +00:00
|
|
|
|
as->block = malloc (as_size);
|
2015-07-19 19:12:35 +00:00
|
|
|
|
if (!as->block) {
|
|
|
|
|
free (as);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
|
memset (as->block, 0, as_size);
|
2016-12-02 19:44:32 +00:00
|
|
|
|
for (at = from; at < to; at += step) {
|
|
|
|
|
piece = (at - from) / step;
|
|
|
|
|
as->block[piece].rwx = r_io_section_get_rwx (core->io, at);
|
|
|
|
|
}
|
2013-02-07 02:09:53 +00:00
|
|
|
|
// iter all flags
|
|
|
|
|
r_list_foreach (core->flags->flags, iter, f) {
|
|
|
|
|
//if (f->offset+f->size < from) continue;
|
2016-12-02 19:44:32 +00:00
|
|
|
|
if (f->offset < from || f->offset > to) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-12-02 19:44:32 +00:00
|
|
|
|
piece = (f->offset - from) / step;
|
2013-02-06 09:35:23 +00:00
|
|
|
|
as->block[piece].flags++;
|
|
|
|
|
}
|
2017-01-29 17:21:29 +00:00
|
|
|
|
// iter all functions
|
2013-02-07 02:09:53 +00:00
|
|
|
|
r_list_foreach (core->anal->fcns, iter, F) {
|
2016-12-02 19:44:32 +00:00
|
|
|
|
if (F->addr < from || F->addr > to) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-12-02 19:44:32 +00:00
|
|
|
|
piece = (F->addr - from) / step;
|
2013-02-07 02:09:53 +00:00
|
|
|
|
as->block[piece].functions++;
|
|
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
|
// iter all symbols
|
2017-01-29 17:21:29 +00:00
|
|
|
|
r_list_foreach (r_bin_get_symbols (core->bin), iter, S) {
|
|
|
|
|
if (S->vaddr < from || S->vaddr > to) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
piece = (S->vaddr - from) / step;
|
|
|
|
|
as->block[piece].symbols++;
|
|
|
|
|
}
|
|
|
|
|
RList *metas = r_meta_enumerate (core->anal, -1);
|
|
|
|
|
RAnalMetaItem *M;
|
|
|
|
|
r_list_foreach (metas, iter, M) {
|
|
|
|
|
if (M->from < from || M->to > to) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
piece = (M->from - from) / step;
|
|
|
|
|
switch (M->type) {
|
|
|
|
|
case R_META_TYPE_STRING:
|
|
|
|
|
as->block[piece].strings++;
|
|
|
|
|
break;
|
|
|
|
|
case R_META_TYPE_COMMENT:
|
|
|
|
|
as->block[piece].comments++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// iter all comments
|
2013-02-06 09:35:23 +00:00
|
|
|
|
// iter all strings
|
|
|
|
|
return as;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
R_API void r_core_anal_stats_free (RCoreAnalStats *s) {
|
|
|
|
|
free (s);
|
|
|
|
|
}
|
2014-03-24 23:48:42 +00:00
|
|
|
|
|
2016-11-20 18:20:14 +00:00
|
|
|
|
R_API RList* r_core_anal_cycles(RCore *core, int ccl) {
|
2014-03-24 23:48:42 +00:00
|
|
|
|
ut64 addr = core->offset;
|
2016-04-07 09:10:43 +00:00
|
|
|
|
int depth = 0;
|
2014-03-24 23:48:42 +00:00
|
|
|
|
RAnalOp *op = NULL;
|
2016-06-02 01:19:31 +00:00
|
|
|
|
RAnalCycleFrame *prev = NULL, *cf = NULL;
|
2014-03-24 23:48:42 +00:00
|
|
|
|
RAnalCycleHook *ch;
|
|
|
|
|
RList *hooks = r_list_new ();
|
2016-06-02 01:19:31 +00:00
|
|
|
|
if (!hooks) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
cf = r_anal_cycle_frame_new ();
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_push (NULL, NULL);
|
|
|
|
|
while (cf && !r_cons_is_breaked ()) {
|
2014-05-06 08:52:31 +00:00
|
|
|
|
if ((op = r_core_anal_op (core, addr)) && (op->cycles) && (ccl > 0)) {
|
2014-03-24 23:48:42 +00:00
|
|
|
|
r_cons_clear_line (1);
|
|
|
|
|
eprintf ("%i -- ", ccl);
|
|
|
|
|
addr += op->size;
|
|
|
|
|
switch (op->type) {
|
2016-04-07 09:10:43 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
|
addr = op->jump;
|
|
|
|
|
ccl -= op->cycles;
|
|
|
|
|
loganal (op->addr, addr, depth);
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
2016-05-29 22:38:35 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_MJMP:
|
2016-04-07 09:10:43 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_UCALL:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_ICALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_RCALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRCALL:
|
2016-04-07 09:10:43 +00:00
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
ch->addr = op->addr;
|
|
|
|
|
eprintf ("0x%08"PFMT64x" > ?\r", op->addr);
|
|
|
|
|
ch->cycles = ccl;
|
|
|
|
|
r_list_append (hooks, ch);
|
|
|
|
|
ch = NULL;
|
|
|
|
|
while (!ch && cf) {
|
|
|
|
|
ch = r_list_pop (cf->hooks);
|
|
|
|
|
if (ch) {
|
|
|
|
|
addr = ch->addr;
|
|
|
|
|
ccl = ch->cycles;
|
|
|
|
|
free (ch);
|
|
|
|
|
} else {
|
|
|
|
|
r_anal_cycle_frame_free (cf);
|
|
|
|
|
cf = prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (cf) {
|
2016-04-07 09:10:43 +00:00
|
|
|
|
prev = cf->prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2016-04-07 09:10:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
ch->addr = addr;
|
|
|
|
|
ch->cycles = ccl - op->failcycles;
|
|
|
|
|
r_list_push (cf->hooks, ch);
|
|
|
|
|
ch = NULL;
|
|
|
|
|
addr = op->jump;
|
|
|
|
|
loganal (op->addr, addr, depth);
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_UCJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_UCCALL:
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
ch->addr = op->addr;
|
|
|
|
|
ch->cycles = ccl;
|
|
|
|
|
r_list_append (hooks, ch);
|
|
|
|
|
ch = NULL;
|
|
|
|
|
ccl -= op->failcycles;
|
|
|
|
|
eprintf ("0x%08"PFMT64x" > ?\r", op->addr);
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_CCALL:
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
ch->addr = addr;
|
|
|
|
|
ch->cycles = ccl - op->failcycles;
|
|
|
|
|
r_list_push (cf->hooks, ch);
|
|
|
|
|
ch = NULL;
|
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
2016-08-20 22:53:39 +00:00
|
|
|
|
if (op->addr != op->jump) { //no selfies
|
2016-04-07 09:10:43 +00:00
|
|
|
|
cf->naddr = addr;
|
|
|
|
|
prev = cf;
|
|
|
|
|
cf = r_anal_cycle_frame_new ();
|
|
|
|
|
cf->prev = prev;
|
|
|
|
|
}
|
|
|
|
|
ccl -= op->cycles;
|
|
|
|
|
addr = op->jump;
|
|
|
|
|
loganal (op->addr, addr, depth);
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_RET:
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
if (prev) {
|
|
|
|
|
ch->addr = prev->naddr;
|
2014-03-24 23:48:42 +00:00
|
|
|
|
ccl -= op->cycles;
|
|
|
|
|
ch->cycles = ccl;
|
2016-04-07 09:10:43 +00:00
|
|
|
|
r_list_push (prev->hooks, ch);
|
|
|
|
|
eprintf ("0x%08"PFMT64x" < 0x%08"PFMT64x"\r", prev->naddr, op->addr);
|
|
|
|
|
} else {
|
2014-03-24 23:48:42 +00:00
|
|
|
|
ch->addr = op->addr;
|
|
|
|
|
ch->cycles = ccl;
|
|
|
|
|
r_list_append (hooks, ch);
|
2016-04-07 09:10:43 +00:00
|
|
|
|
eprintf ("? < 0x%08"PFMT64x"\r", op->addr);
|
|
|
|
|
}
|
|
|
|
|
ch = NULL;
|
|
|
|
|
while (!ch && cf) {
|
|
|
|
|
ch = r_list_pop (cf->hooks);
|
|
|
|
|
if (ch) {
|
|
|
|
|
addr = ch->addr;
|
|
|
|
|
ccl = ch->cycles;
|
|
|
|
|
free (ch);
|
2014-03-24 23:48:42 +00:00
|
|
|
|
} else {
|
2016-04-07 09:10:43 +00:00
|
|
|
|
r_anal_cycle_frame_free (cf);
|
|
|
|
|
cf = prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (cf) {
|
2016-04-07 09:10:43 +00:00
|
|
|
|
prev = cf->prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-03-24 23:48:42 +00:00
|
|
|
|
}
|
2016-04-07 09:10:43 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_CRET:
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
|
|
|
|
if (prev) {
|
|
|
|
|
ch->addr = prev->naddr;
|
|
|
|
|
ch->cycles = ccl - op->cycles;
|
|
|
|
|
r_list_push (prev->hooks, ch);
|
|
|
|
|
eprintf ("0x%08"PFMT64x" < 0x%08"PFMT64x"\r", prev->naddr, op->addr);
|
|
|
|
|
} else {
|
|
|
|
|
ch->addr = op->addr;
|
|
|
|
|
ch->cycles = ccl - op->cycles;
|
|
|
|
|
r_list_append (hooks, ch);
|
|
|
|
|
eprintf ("? < 0x%08"PFMT64x"\r", op->addr);
|
|
|
|
|
}
|
|
|
|
|
ccl -= op->failcycles;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ccl -= op->cycles;
|
|
|
|
|
eprintf ("0x%08"PFMT64x"\r", op->addr);
|
|
|
|
|
break;
|
2014-03-24 23:48:42 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ch = R_NEW0 (RAnalCycleHook);
|
2016-05-24 20:22:15 +00:00
|
|
|
|
if (!ch) {
|
2016-06-02 01:19:31 +00:00
|
|
|
|
r_anal_cycle_frame_free (cf);
|
2016-05-24 20:22:15 +00:00
|
|
|
|
r_list_free (hooks);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-03-24 23:48:42 +00:00
|
|
|
|
ch->addr = addr;
|
|
|
|
|
ch->cycles = ccl;
|
|
|
|
|
r_list_append (hooks, ch);
|
|
|
|
|
ch = NULL;
|
|
|
|
|
while (!ch && cf) {
|
|
|
|
|
ch = r_list_pop (cf->hooks);
|
|
|
|
|
if (ch) {
|
|
|
|
|
addr = ch->addr;
|
|
|
|
|
ccl = ch->cycles;
|
|
|
|
|
free (ch);
|
|
|
|
|
} else {
|
|
|
|
|
r_anal_cycle_frame_free (cf);
|
|
|
|
|
cf = prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (cf) {
|
2014-03-24 23:48:42 +00:00
|
|
|
|
prev = cf->prev;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-03-24 23:48:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-06 08:52:31 +00:00
|
|
|
|
r_anal_op_free (op);
|
2014-03-24 23:48:42 +00:00
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
if (r_cons_is_breaked ()) {
|
2014-03-24 23:48:42 +00:00
|
|
|
|
while (cf) {
|
|
|
|
|
ch = r_list_pop (cf->hooks);
|
|
|
|
|
while (ch) {
|
|
|
|
|
free (ch);
|
|
|
|
|
ch = r_list_pop (cf->hooks);
|
|
|
|
|
}
|
|
|
|
|
prev = cf->prev;
|
|
|
|
|
r_anal_cycle_frame_free (cf);
|
|
|
|
|
cf = prev;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_pop ();
|
2014-03-24 23:48:42 +00:00
|
|
|
|
return hooks;
|
2014-09-24 20:45:38 +00:00
|
|
|
|
}
|
2014-09-22 13:00:41 +00:00
|
|
|
|
|
|
|
|
|
R_API void r_core_anal_undefine (RCore *core, ut64 off) {
|
|
|
|
|
RAnalFunction *f;
|
|
|
|
|
r_anal_fcn_del_locs (core->anal, off);
|
2014-09-26 13:40:17 +00:00
|
|
|
|
f = r_anal_get_fcn_in (core->anal, off, 0);
|
2015-05-13 22:31:42 +00:00
|
|
|
|
if (f) {
|
|
|
|
|
if (!strncmp (f->name, "fcn.", 4)) {
|
2016-02-20 14:25:27 +00:00
|
|
|
|
r_flag_unset_name (core->flags, f->name);
|
2015-05-13 22:31:42 +00:00
|
|
|
|
}
|
2016-05-15 12:37:22 +00:00
|
|
|
|
r_meta_del (core->anal, R_META_TYPE_ANY, off, r_anal_fcn_size (f), "");
|
2015-05-13 22:31:42 +00:00
|
|
|
|
}
|
2014-09-22 13:00:41 +00:00
|
|
|
|
r_anal_fcn_del (core->anal, off);
|
|
|
|
|
}
|
2014-11-15 01:07:08 +00:00
|
|
|
|
|
|
|
|
|
/* Join function at addr2 into function at addr */
|
|
|
|
|
// addr use to be core->offset
|
|
|
|
|
R_API void r_core_anal_fcn_merge (RCore *core, ut64 addr, ut64 addr2) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
ut64 min = 0;
|
|
|
|
|
ut64 max = 0;
|
|
|
|
|
int first = 1;
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
RAnalFunction *f1 = r_anal_get_fcn_at (core->anal, addr, 0);
|
|
|
|
|
RAnalFunction *f2 = r_anal_get_fcn_at (core->anal, addr2, 0);
|
|
|
|
|
if (!f1 || !f2) {
|
2016-01-04 01:19:02 +00:00
|
|
|
|
eprintf ("Cannot find function\n");
|
2014-11-15 01:07:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-24 14:47:41 +00:00
|
|
|
|
if (f1 == f2) {
|
|
|
|
|
eprintf ("Cannot merge the same function\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-11-15 01:07:08 +00:00
|
|
|
|
// join all basic blocks from f1 into f2 if they are not
|
|
|
|
|
// delete f2
|
2015-01-22 01:22:29 +00:00
|
|
|
|
eprintf ("Merge 0x%08"PFMT64x" into 0x%08"PFMT64x"\n", addr, addr2);
|
2014-11-15 01:07:08 +00:00
|
|
|
|
r_list_foreach (f1->bbs, iter, bb) {
|
|
|
|
|
if (first) {
|
|
|
|
|
min = bb->addr;
|
|
|
|
|
max = bb->addr + bb->size;
|
|
|
|
|
first = 0;
|
|
|
|
|
} else {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (bb->addr < min) {
|
2014-11-15 01:07:08 +00:00
|
|
|
|
min = bb->addr;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (bb->addr + bb->size > max) {
|
2014-11-15 01:07:08 +00:00
|
|
|
|
max = bb->addr + bb->size;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-11-15 01:07:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
r_list_foreach (f2->bbs, iter, bb) {
|
|
|
|
|
if (first) {
|
|
|
|
|
min = bb->addr;
|
|
|
|
|
max = bb->addr + bb->size;
|
|
|
|
|
first = 0;
|
|
|
|
|
} else {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (bb->addr < min) {
|
2014-11-15 01:07:08 +00:00
|
|
|
|
min = bb->addr;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (bb->addr + bb->size > max) {
|
2014-11-15 01:07:08 +00:00
|
|
|
|
max = bb->addr + bb->size;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2014-11-15 01:07:08 +00:00
|
|
|
|
}
|
2016-08-21 01:07:19 +00:00
|
|
|
|
r_anal_fcn_bbadd (f1, bb);
|
2014-11-15 01:07:08 +00:00
|
|
|
|
}
|
2015-07-31 10:40:04 +00:00
|
|
|
|
// TODO: import data/code/refs
|
2014-11-15 01:07:08 +00:00
|
|
|
|
// update size
|
|
|
|
|
f1->addr = R_MIN (addr, addr2);
|
2016-05-15 12:37:22 +00:00
|
|
|
|
r_anal_fcn_set_size (f1, max - min);
|
2014-11-15 01:07:08 +00:00
|
|
|
|
// resize
|
|
|
|
|
f2->bbs = NULL;
|
|
|
|
|
r_list_delete_data (core->anal->fcns, f2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
R_API void r_core_anal_auto_merge (RCore *core, ut64 addr) {
|
2015-07-31 10:40:04 +00:00
|
|
|
|
/* TODO: implement me */
|
2014-11-15 01:07:08 +00:00
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
|
2015-07-24 16:18:17 +00:00
|
|
|
|
|
2017-01-25 10:11:14 +00:00
|
|
|
|
static bool myvalid(RIO *io, ut64 addr) {
|
2016-11-01 18:42:43 +00:00
|
|
|
|
if (addr < 0x100) {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
return false;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (addr == UT32_MAX || addr == UT64_MAX) {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
return false;
|
2016-11-01 18:42:43 +00:00
|
|
|
|
}
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (!r_io_is_valid_offset (io, addr, 0)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int esilbreak_mem_write(RAnalEsil *esil, ut64 addr, const ut8 *buf, int len) {
|
|
|
|
|
/* do nothing */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 23:30:40 +00:00
|
|
|
|
/* TODO: move into RCore? */
|
2015-08-09 01:23:40 +00:00
|
|
|
|
static ut64 esilbreak_last_read = UT64_MAX;
|
2017-01-25 10:11:14 +00:00
|
|
|
|
static ut64 esilbreak_last_data = UT64_MAX;
|
2015-08-09 01:23:40 +00:00
|
|
|
|
|
2016-06-13 23:30:40 +00:00
|
|
|
|
static ut64 ntarget = UT64_MAX;
|
|
|
|
|
|
2015-07-24 13:20:32 +00:00
|
|
|
|
static int esilbreak_mem_read(RAnalEsil *esil, ut64 addr, ut8 *buf, int len) {
|
|
|
|
|
ut8 str[128];
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (addr != UT64_MAX) {
|
2016-09-15 13:03:54 +00:00
|
|
|
|
esilbreak_last_read = addr;
|
2017-01-25 10:11:14 +00:00
|
|
|
|
}
|
|
|
|
|
if (myvalid (mycore->io, addr)) {
|
|
|
|
|
ut8 buf[8];
|
|
|
|
|
ut64 refptr;
|
|
|
|
|
if (len == 8) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if (r_io_read_at (mycore->io, addr, (ut8*)buf, sizeof (buf)) != sizeof (buf)) {
|
|
|
|
|
/* invalid read */
|
2017-01-25 10:11:14 +00:00
|
|
|
|
refptr = UT64_MAX;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
} else {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
refptr = r_read_ble64 (buf, esil->anal->big_endian);
|
|
|
|
|
esilbreak_last_data = refptr;
|
2016-03-06 23:18:06 +00:00
|
|
|
|
}
|
2017-01-25 10:11:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (r_io_read_at (mycore->io, addr, (ut8*)buf, sizeof (buf)) != sizeof (buf)) {
|
|
|
|
|
/* invalid read */
|
|
|
|
|
refptr = UT64_MAX;
|
|
|
|
|
} else {
|
|
|
|
|
refptr = (ut64)r_read_ble32 (buf, esil->anal->big_endian);
|
|
|
|
|
esilbreak_last_data = refptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bool validRef = false;
|
|
|
|
|
if (myvalid (mycore->io, refptr)) {
|
|
|
|
|
if (ntarget == UT64_MAX || ntarget == refptr) {
|
|
|
|
|
r_core_cmdf (mycore, "axd 0x%"PFMT64x" 0x%"PFMT64x,
|
2016-06-13 23:30:40 +00:00
|
|
|
|
(ut64)refptr, esil->address);
|
|
|
|
|
str[0] = 0;
|
|
|
|
|
if (r_io_read_at (mycore->io, refptr, str, sizeof (str)) < 1) {
|
|
|
|
|
eprintf ("Invalid read\n");
|
|
|
|
|
str[0] = 0;
|
|
|
|
|
}
|
2017-01-25 10:11:14 +00:00
|
|
|
|
str[sizeof (str) - 1] = 0;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
add_string_ref (mycore, refptr);
|
2017-01-25 10:11:14 +00:00
|
|
|
|
esilbreak_last_data = UT64_MAX;
|
|
|
|
|
validRef = true;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
}
|
2017-01-25 10:11:14 +00:00
|
|
|
|
|
|
|
|
|
/** resolve ptr */
|
|
|
|
|
if (ntarget == UT64_MAX || ntarget == addr || (ntarget == UT64_MAX && !validRef)) {
|
|
|
|
|
r_core_cmdf (mycore, "axd 0x%"PFMT64x" 0x%"PFMT64x,
|
|
|
|
|
addr, esil->address);
|
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
}
|
|
|
|
|
return 0; // fallback
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 16:38:07 +00:00
|
|
|
|
static bool esil_anal_stop = false;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
static void cccb(void *u) {
|
2015-09-25 16:38:07 +00:00
|
|
|
|
esil_anal_stop = true;
|
|
|
|
|
eprintf ("^C\n");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 20:44:48 +00:00
|
|
|
|
static void add_string_ref(RCore *core, ut64 xref_to) {
|
2016-03-16 12:08:27 +00:00
|
|
|
|
int len = 0;
|
|
|
|
|
char *str_flagname;
|
2017-01-19 20:44:48 +00:00
|
|
|
|
if (xref_to == UT64_MAX || !xref_to) {
|
2016-03-16 12:08:27 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
str_flagname = is_string_at (core, xref_to, &len);
|
|
|
|
|
if (str_flagname) {
|
|
|
|
|
r_name_filter (str_flagname, -1);
|
|
|
|
|
char *flagname = sdb_fmt (0, "str.%s", str_flagname);
|
2016-03-24 11:40:09 +00:00
|
|
|
|
r_flag_space_push (core->flags, "strings");
|
2016-03-16 12:08:27 +00:00
|
|
|
|
r_flag_set (core->flags, flagname, xref_to, len);
|
2016-03-24 11:40:09 +00:00
|
|
|
|
r_flag_space_pop (core->flags);
|
2016-03-16 12:08:27 +00:00
|
|
|
|
r_meta_add (core->anal, 's', xref_to, xref_to + len, str_flagname);
|
|
|
|
|
//r_cons_printf ("Cs %d @ 0x%"PFMT64x"\n", len, xref_to);
|
|
|
|
|
free (str_flagname);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 20:44:48 +00:00
|
|
|
|
static int esilbreak_reg_write(RAnalEsil *esil, const char *name, ut64 *val) {
|
|
|
|
|
RAnal *anal = NULL;
|
|
|
|
|
RAnalOp *op = NULL;
|
|
|
|
|
if (!esil) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
anal = esil->anal;
|
|
|
|
|
op = esil->user;
|
|
|
|
|
//specific case to handle blx/bx cases in arm through emulation
|
|
|
|
|
if (anal && anal->cur && anal->cur->arch && anal->bits < 33 &&
|
|
|
|
|
strstr (anal->cur->arch, "arm") && !strcmp (name, "pc") && op) {
|
|
|
|
|
switch (op->id) {
|
|
|
|
|
//Thoses values comes from capstone so basically for others plugin
|
|
|
|
|
//will not work since they not fill analop.id
|
|
|
|
|
//do not include here capstone's headers
|
|
|
|
|
case 14: //ARM_INS_BLX
|
|
|
|
|
case 15: //ARM_INS_BX
|
|
|
|
|
{
|
|
|
|
|
if (!(*val & 1)) {
|
|
|
|
|
r_anal_hint_set_bits (anal, *val, 32);
|
|
|
|
|
} else {
|
|
|
|
|
r_anal_hint_set_bits (anal, *val - 1, 16);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 23:30:40 +00:00
|
|
|
|
R_API void r_core_anal_esil(RCore *core, const char *str, const char *target) {
|
2016-03-16 12:08:27 +00:00
|
|
|
|
bool cfg_anal_strings = r_config_get_i (core->config, "anal.strings");
|
2015-07-24 13:20:32 +00:00
|
|
|
|
RAnalEsil *ESIL = core->anal->esil;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
ut64 refptr = 0LL;
|
2015-07-24 17:33:22 +00:00
|
|
|
|
const char *pcname;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
#if 0
|
2015-09-25 16:38:07 +00:00
|
|
|
|
RAsmOp asmop;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
#endif
|
2016-06-27 21:26:13 +00:00
|
|
|
|
RAnalOp op = {0};
|
2015-07-24 13:20:32 +00:00
|
|
|
|
ut8 *buf = NULL;
|
2016-09-07 18:33:41 +00:00
|
|
|
|
bool end_address_set = false;
|
2015-07-24 13:20:32 +00:00
|
|
|
|
int i, iend;
|
|
|
|
|
int minopsize = 4; // XXX this depends on asm->mininstrsize
|
|
|
|
|
ut64 addr = core->offset;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
ut64 end = 0LL;
|
2015-07-24 17:33:22 +00:00
|
|
|
|
ut64 cur;
|
|
|
|
|
|
|
|
|
|
mycore = core;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
if (!strcmp (str, "?")) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
eprintf ("Usage: aae[f] [len] [addr] - analyze refs in function, section or len bytes with esil\n");
|
2017-01-25 10:11:14 +00:00
|
|
|
|
eprintf (" aae $SS @ $S - analyze the whole section\n");
|
|
|
|
|
eprintf (" aae $SS str.Hello @ $S - find references for str.Hellow\n");
|
2015-07-24 16:18:17 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-01-04 01:16:22 +00:00
|
|
|
|
#define CHECKREF(x) ((refptr && x == refptr) || !refptr)
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if (target) {
|
2017-01-04 01:16:22 +00:00
|
|
|
|
const char *expr = r_str_chop_ro (target);
|
|
|
|
|
if (*expr) {
|
|
|
|
|
refptr = ntarget = r_num_math (core->num, expr);
|
|
|
|
|
if (!refptr) {
|
|
|
|
|
ntarget = refptr = addr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-13 23:30:40 +00:00
|
|
|
|
} else {
|
|
|
|
|
ntarget = UT64_MAX;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
refptr = 0LL;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
2015-07-24 16:18:17 +00:00
|
|
|
|
if (!strcmp (str, "f")) {
|
|
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
|
|
|
|
if (fcn) {
|
|
|
|
|
addr = fcn->addr;
|
2016-05-15 12:37:22 +00:00
|
|
|
|
end = fcn->addr + r_anal_fcn_size (fcn);
|
2016-09-07 18:33:41 +00:00
|
|
|
|
end_address_set = true;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-07 18:33:41 +00:00
|
|
|
|
|
|
|
|
|
if (!end_address_set) {
|
|
|
|
|
if (str[0] == ' ') {
|
|
|
|
|
end = addr + r_num_math (core->num, str + 1);
|
2015-07-24 16:18:17 +00:00
|
|
|
|
} else {
|
2016-09-07 18:33:41 +00:00
|
|
|
|
RIOSection *sect = r_io_section_vget (core->io, addr);
|
|
|
|
|
if (sect) {
|
|
|
|
|
end = sect->vaddr + sect->size;
|
|
|
|
|
} else {
|
2015-07-25 07:58:11 +00:00
|
|
|
|
end = addr + core->blocksize;
|
2016-09-07 18:33:41 +00:00
|
|
|
|
}
|
2015-07-24 16:18:17 +00:00
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
}
|
2016-09-07 18:33:41 +00:00
|
|
|
|
|
2015-07-24 13:20:32 +00:00
|
|
|
|
iend = end - addr;
|
2016-03-16 11:51:26 +00:00
|
|
|
|
if (iend < 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-03-16 12:08:27 +00:00
|
|
|
|
buf = malloc (iend + 2);
|
2016-09-19 12:44:47 +00:00
|
|
|
|
if (!buf) {
|
2016-03-16 11:51:26 +00:00
|
|
|
|
perror ("malloc");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-15 13:03:54 +00:00
|
|
|
|
esilbreak_last_read = UT64_MAX;
|
2016-03-16 12:08:27 +00:00
|
|
|
|
r_io_read_at (core->io, addr, buf, iend + 1);
|
2015-07-24 13:20:32 +00:00
|
|
|
|
if (!ESIL) {
|
|
|
|
|
r_core_cmd0 (core, "aei");
|
|
|
|
|
ESIL = core->anal->esil;
|
|
|
|
|
if (!ESIL) {
|
|
|
|
|
eprintf ("ESIL not initialized\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 20:44:48 +00:00
|
|
|
|
ESIL->cb.hook_reg_write = &esilbreak_reg_write;
|
|
|
|
|
//this is necessary for the hook to read the id of analop
|
|
|
|
|
ESIL->user = &op;
|
2015-07-24 13:20:32 +00:00
|
|
|
|
ESIL->cb.hook_mem_read = &esilbreak_mem_read;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
if (!core->io->cached) {
|
|
|
|
|
ESIL->cb.hook_mem_write = &esilbreak_mem_write;
|
|
|
|
|
}
|
2015-08-11 07:46:28 +00:00
|
|
|
|
//eprintf ("Analyzing ESIL refs from 0x%"PFMT64x" - 0x%"PFMT64x"\n", addr, end);
|
2015-07-24 17:33:22 +00:00
|
|
|
|
// TODO: backup/restore register state before/after analysis
|
|
|
|
|
pcname = r_reg_get_name (core->anal->reg, R_REG_NAME_PC);
|
2016-03-16 15:24:23 +00:00
|
|
|
|
if (!pcname || !*pcname) {
|
|
|
|
|
eprintf ("Cannot find program counter register in the current profile.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-09-25 16:38:07 +00:00
|
|
|
|
esil_anal_stop = false;
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_push (cccb, core);
|
2016-11-15 11:55:09 +00:00
|
|
|
|
|
|
|
|
|
int opalign = r_anal_archinfo (core->anal, R_ANAL_ARCHINFO_ALIGN);
|
2016-11-15 18:26:55 +00:00
|
|
|
|
int in = r_syscall_get_swi (core->anal->syscall);
|
2016-11-15 11:55:09 +00:00
|
|
|
|
const char *sn = r_reg_get_name (core->anal->reg, R_REG_NAME_SN);
|
2017-01-24 01:46:49 +00:00
|
|
|
|
r_reg_arena_push (core->anal->reg);
|
2016-03-06 18:34:36 +00:00
|
|
|
|
for (i = 0; i < iend; i++) {
|
2015-09-25 16:38:07 +00:00
|
|
|
|
if (esil_anal_stop || r_cons_is_breaked ()) {
|
2015-07-24 16:18:17 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
cur = addr + i;
|
2016-11-15 11:55:09 +00:00
|
|
|
|
/* realign address if needed */
|
|
|
|
|
if (opalign > 0) {
|
|
|
|
|
cur -= (cur % opalign);
|
|
|
|
|
}
|
2017-01-04 01:16:22 +00:00
|
|
|
|
R_FREE (op.mnemonic);
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if (!r_anal_op (core->anal, &op, cur, buf + i, iend - i)) {
|
2015-09-25 16:38:07 +00:00
|
|
|
|
i += minopsize - 1;
|
|
|
|
|
}
|
|
|
|
|
r_asm_set_pc (core->assembler, cur);
|
2016-03-06 18:34:36 +00:00
|
|
|
|
//we need to check again i because buf+i may goes beyond its boundaries
|
|
|
|
|
//because of i+= minopsize - 1
|
2016-03-16 15:24:23 +00:00
|
|
|
|
if (i > iend) {
|
2016-03-06 18:34:36 +00:00
|
|
|
|
break;
|
2016-03-16 15:24:23 +00:00
|
|
|
|
}
|
2016-03-06 18:34:36 +00:00
|
|
|
|
if (op.size < 1) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
i += minopsize - 1;
|
2015-09-25 16:38:07 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-11-15 11:55:09 +00:00
|
|
|
|
switch (op.type) {
|
|
|
|
|
case R_ANAL_OP_TYPE_SWI:
|
2017-01-04 01:16:22 +00:00
|
|
|
|
if (!refptr && (in == -1 || op.val == in)) {
|
2016-11-15 15:35:40 +00:00
|
|
|
|
r_flag_space_set (core->flags, "syscalls");
|
2016-11-15 11:55:09 +00:00
|
|
|
|
int snv = (int)r_reg_getv (core->anal->reg, sn);
|
2016-11-15 18:26:55 +00:00
|
|
|
|
if (snv > 0) {
|
|
|
|
|
RSyscallItem *si = r_syscall_get (core->anal->syscall, snv, in);
|
|
|
|
|
if (si) {
|
|
|
|
|
// eprintf ("0x%08"PFMT64x" SYSCALL %-4d %s\n", cur, snv, si->name);
|
|
|
|
|
r_flag_set_next (core->flags, sdb_fmt (0, "syscall.%s", si->name), cur, 1);
|
|
|
|
|
} else {
|
|
|
|
|
// eprintf ("0x%08"PFMT64x" SYSCALL %d\n", cur, snv);
|
|
|
|
|
r_flag_set_next (core->flags, sdb_fmt (0, "syscall.%d", snv), cur, 1);
|
|
|
|
|
}
|
|
|
|
|
r_flag_space_set (core->flags, NULL);
|
2016-11-15 11:55:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-09-25 16:38:07 +00:00
|
|
|
|
if (1) {
|
|
|
|
|
const char *esilstr = R_STRBUF_SAFEGET (&op.esil);
|
|
|
|
|
r_anal_esil_set_pc (ESIL, cur);
|
2016-09-12 16:41:11 +00:00
|
|
|
|
i += op.size - 1;
|
|
|
|
|
if (!esilstr || !*esilstr) {
|
2015-09-25 16:38:07 +00:00
|
|
|
|
continue;
|
2016-09-12 16:41:11 +00:00
|
|
|
|
}
|
2015-10-05 12:43:17 +00:00
|
|
|
|
(void)r_anal_esil_parse (ESIL, esilstr);
|
2015-09-25 16:38:07 +00:00
|
|
|
|
// looks like ^C is handled by esil_parse !!!!
|
|
|
|
|
//r_anal_esil_dumpstack (ESIL);
|
|
|
|
|
r_anal_esil_stack_free (ESIL);
|
|
|
|
|
switch (op.type) {
|
2016-03-16 12:08:27 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_LEA:
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if ((target && op.ptr == ntarget) || !target) {
|
2016-11-16 19:37:40 +00:00
|
|
|
|
if (core->anal->cur && strcmp (core->anal->cur->arch, "arm")) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if (cfg_anal_strings) {
|
2017-01-04 01:16:22 +00:00
|
|
|
|
if (CHECKREF(op.ptr)) {
|
|
|
|
|
r_anal_ref_add (core->anal, op.ptr, cur, 'd');
|
|
|
|
|
if ((target && op.ptr == ntarget) || !target) {
|
|
|
|
|
add_string_ref (core, op.ptr);
|
|
|
|
|
}
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-11 02:07:51 +00:00
|
|
|
|
}
|
2016-03-16 12:08:27 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2016-03-16 14:14:05 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_ADD:
|
|
|
|
|
/* TODO: test if this is valid for other archs too */
|
2016-11-16 19:37:40 +00:00
|
|
|
|
if (core->anal->bits == 64 && core->anal->cur && !strcmp (core->anal->cur->arch, "arm")) {
|
2016-05-11 02:07:51 +00:00
|
|
|
|
ut64 dst = ESIL->cur;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if ((target && dst == ntarget) || !target) {
|
2017-01-04 01:16:22 +00:00
|
|
|
|
if (CHECKREF(dst)) {
|
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'd');
|
|
|
|
|
}
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
2016-11-16 19:37:40 +00:00
|
|
|
|
} else if ((core->anal->bits == 32 && core->anal->cur && !strcmp (core->anal->cur->arch, "mips"))) {
|
2016-06-27 21:26:13 +00:00
|
|
|
|
ut64 dst = ESIL->cur;
|
2016-11-16 19:37:40 +00:00
|
|
|
|
if (!op.src[0] || !op.src[0]->reg || !op.src[0]->reg->name) {
|
2016-03-23 16:04:16 +00:00
|
|
|
|
break;
|
2016-11-16 19:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (!strcmp (op.src[0]->reg->name, "sp")) {
|
2016-03-23 16:04:16 +00:00
|
|
|
|
break;
|
2016-11-16 19:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (!strcmp (op.src[0]->reg->name, "zero")) {
|
2016-03-23 16:04:16 +00:00
|
|
|
|
break;
|
2016-11-16 19:37:40 +00:00
|
|
|
|
}
|
2016-06-13 23:30:40 +00:00
|
|
|
|
if ((target && dst == ntarget) || !target) {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (dst > 0xffff && op.src[1] && (dst & 0xffff) == (op.src[1]->imm & 0xffff) && myvalid (mycore->io, dst)) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
RFlagItem *f;
|
|
|
|
|
char *str;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
if (CHECKREF(dst) || CHECKREF(cur)) {
|
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'd');
|
|
|
|
|
if (cfg_anal_strings) {
|
|
|
|
|
add_string_ref (core, dst);
|
|
|
|
|
}
|
|
|
|
|
if ((f = r_flag_get_i2 (core->flags, dst))) {
|
|
|
|
|
r_meta_set_string (core->anal, R_META_TYPE_COMMENT, cur, f->name);
|
|
|
|
|
} else if ((str = is_string_at (mycore, dst, NULL))) {
|
|
|
|
|
char *str2 = sdb_fmt (0, "esilref: '%s'", str);
|
|
|
|
|
r_meta_set_string (core->anal, R_META_TYPE_COMMENT, cur, str2);
|
|
|
|
|
free (str);
|
|
|
|
|
}
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
2016-03-16 14:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2015-09-25 16:38:07 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_LOAD:
|
|
|
|
|
{
|
2016-03-16 12:08:27 +00:00
|
|
|
|
ut64 dst = esilbreak_last_read;
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (dst != UT64_MAX && CHECKREF(dst)) {
|
|
|
|
|
if (myvalid (mycore->io, dst)) {
|
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'd');
|
|
|
|
|
if (cfg_anal_strings) {
|
|
|
|
|
add_string_ref (core, dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dst = esilbreak_last_data;
|
|
|
|
|
if (dst != UT64_MAX && CHECKREF(dst)) {
|
|
|
|
|
if (myvalid (mycore->io, dst)) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'd');
|
|
|
|
|
if (cfg_anal_strings) {
|
|
|
|
|
add_string_ref (core, dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-01-04 01:16:22 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
|
{
|
|
|
|
|
ut64 dst = op.jump;
|
|
|
|
|
if (CHECKREF(dst)) {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (myvalid (core->io, dst)) {
|
2017-01-04 01:16:22 +00:00
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'c');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-06-13 23:30:40 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
|
|
|
|
{
|
|
|
|
|
ut64 dst = op.jump;
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (CHECKREF (dst)) {
|
|
|
|
|
if (myvalid (core->io, dst)) {
|
2016-06-13 23:30:40 +00:00
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, 'C');
|
2016-03-16 12:08:27 +00:00
|
|
|
|
}
|
2015-09-25 16:38:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_UCALL:
|
2016-09-22 11:42:06 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_ICALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_RCALL:
|
|
|
|
|
case R_ANAL_OP_TYPE_IRCALL:
|
2016-05-29 22:38:35 +00:00
|
|
|
|
case R_ANAL_OP_TYPE_MJMP:
|
2016-03-16 15:24:23 +00:00
|
|
|
|
{
|
|
|
|
|
ut64 dst = core->anal->esil->jump_target;
|
|
|
|
|
if (dst == UT64_MAX) {
|
|
|
|
|
dst = r_reg_getv (core->anal->reg, pcname);
|
|
|
|
|
}
|
2017-01-04 01:16:22 +00:00
|
|
|
|
if (CHECKREF(dst)) {
|
2017-01-25 10:11:14 +00:00
|
|
|
|
if (myvalid (core->io, dst)) {
|
2016-09-22 11:42:06 +00:00
|
|
|
|
RAnalRefType ref =
|
2016-09-22 17:04:02 +00:00
|
|
|
|
(op.type & R_ANAL_OP_TYPE_MASK) == R_ANAL_OP_TYPE_UCALL
|
2016-09-06 21:00:59 +00:00
|
|
|
|
? R_ANAL_REF_TYPE_CALL
|
|
|
|
|
: R_ANAL_REF_TYPE_CODE;
|
|
|
|
|
r_anal_ref_add (core->anal, dst, cur, ref);
|
2016-06-13 23:30:40 +00:00
|
|
|
|
}
|
2015-08-09 01:23:40 +00:00
|
|
|
|
}
|
2015-07-24 17:33:22 +00:00
|
|
|
|
}
|
2015-09-25 16:38:07 +00:00
|
|
|
|
break;
|
2015-07-24 16:18:17 +00:00
|
|
|
|
}
|
2015-07-24 13:20:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-27 21:26:13 +00:00
|
|
|
|
free (buf);
|
|
|
|
|
free (op.mnemonic);
|
2016-11-20 18:20:14 +00:00
|
|
|
|
r_cons_break_pop ();
|
2017-01-24 01:46:49 +00:00
|
|
|
|
// restore register
|
|
|
|
|
r_reg_arena_pop (core->anal->reg);
|
2015-07-24 13:20:32 +00:00
|
|
|
|
}
|
2017-02-06 11:04:26 +00:00
|
|
|
|
|
|
|
|
|
// -------------------- TODO: move to anal/abb.c
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
ut64 addr;
|
|
|
|
|
ut64 len;
|
|
|
|
|
ut8 *buf;
|
|
|
|
|
ut64 bb_addr;
|
|
|
|
|
RList *bbs;
|
|
|
|
|
RList *nextbbs;
|
|
|
|
|
} AbbState;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
ut64 addr;
|
|
|
|
|
int bits;
|
|
|
|
|
int type;
|
|
|
|
|
} AbbAddr;
|
|
|
|
|
|
|
|
|
|
static AbbState *abbstate_new (ut64 len) {
|
|
|
|
|
ut8 *buf = malloc (len);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
AbbState *abb = R_NEW0 (AbbState);
|
|
|
|
|
if (!abb) {
|
|
|
|
|
free (buf);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
abb->buf = buf;
|
|
|
|
|
abb->len = len;
|
|
|
|
|
abb->bbs = r_list_new ();
|
|
|
|
|
if (!abb->bbs) {
|
|
|
|
|
return NULL;
|
|
|
|
|
free (buf);
|
|
|
|
|
}
|
|
|
|
|
abb->nextbbs = r_list_newf (free);
|
|
|
|
|
// TODO: add more boring nullchks
|
|
|
|
|
return abb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool appendNextBB(AbbState *abb, ut64 addr, int bits) {
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
r_list_foreach (abb->bbs, iter, bb) {
|
|
|
|
|
if (addr == bb->addr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AbbAddr *n;
|
|
|
|
|
r_list_foreach (abb->nextbbs, iter, n) {
|
|
|
|
|
if (addr == n->addr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
n = R_NEW0 (AbbAddr);
|
|
|
|
|
n->addr = addr;
|
|
|
|
|
n->bits = bits;
|
|
|
|
|
n->type = 0;
|
|
|
|
|
r_list_append (abb->nextbbs, n);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static RAnalBlock *parseOpcode(AbbState *abb, RAnalOp *aop) {//, ut64 *bb_addr, RList *bbs, RList *nextbbs) {
|
|
|
|
|
bool eob = false;
|
|
|
|
|
// eprintf ("%d\n", aop->size);
|
|
|
|
|
switch (aop->type) {
|
|
|
|
|
case R_ANAL_OP_TYPE_RET:
|
|
|
|
|
case R_ANAL_OP_TYPE_TRAP:
|
|
|
|
|
case R_ANAL_OP_TYPE_UJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_RJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_MJMP:
|
|
|
|
|
eob = true;
|
|
|
|
|
break;
|
|
|
|
|
#if 1
|
|
|
|
|
case R_ANAL_OP_TYPE_CALL:
|
|
|
|
|
if (aop->jump != UT64_MAX) {
|
|
|
|
|
appendNextBB (abb, aop->jump, 0);
|
|
|
|
|
}
|
|
|
|
|
if (aop->fail != UT64_MAX) {
|
|
|
|
|
appendNextBB (abb, aop->fail, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
case R_ANAL_OP_TYPE_CJMP:
|
|
|
|
|
case R_ANAL_OP_TYPE_JMP:
|
|
|
|
|
if (aop->jump != UT64_MAX) {
|
|
|
|
|
appendNextBB (abb, aop->jump, 0);
|
|
|
|
|
}
|
|
|
|
|
if (aop->fail != UT64_MAX) {
|
|
|
|
|
appendNextBB (abb, aop->fail, 0);
|
|
|
|
|
}
|
|
|
|
|
eob = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (eob) {
|
|
|
|
|
RAnalBlock *bb = R_NEW0 (RAnalBlock);
|
|
|
|
|
bb->jump = aop->jump;
|
|
|
|
|
bb->fail = aop->fail;
|
|
|
|
|
bb->addr = abb->bb_addr;
|
|
|
|
|
bb->size = aop->addr - abb->bb_addr + aop->size;
|
|
|
|
|
abb->bb_addr = bb->addr + bb->size;
|
|
|
|
|
return bb;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int bbExist(AbbState *abb, ut64 addr) {
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
r_list_foreach (abb->bbs, iter, bb) {
|
|
|
|
|
if (bb->addr == addr) {
|
|
|
|
|
return bb->size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
R_API bool core_anal_bbs(RCore *core, ut64 len) {
|
|
|
|
|
AbbState *abb = abbstate_new (len);
|
|
|
|
|
if (!abb) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
int i;
|
|
|
|
|
RAnalBlock *bb;
|
|
|
|
|
RAnalOp aop;
|
|
|
|
|
RListIter *iter;
|
|
|
|
|
ut64 at = core->offset;
|
|
|
|
|
abb->addr = at;
|
|
|
|
|
(void)r_io_read_at (core->io, abb->addr, abb->buf, len);
|
|
|
|
|
int ti = -1;
|
|
|
|
|
for (i = 0; i < len ; i++) {
|
|
|
|
|
mountain:
|
|
|
|
|
if (!r_anal_op (core->anal, &aop, abb->addr + i, abb->buf + i, R_MIN (len -i, 16))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int next = bbExist (abb, at + i);
|
|
|
|
|
if (next > 0) {
|
|
|
|
|
i += next - 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
bb = parseOpcode (abb, &aop);
|
|
|
|
|
if (bb) {
|
|
|
|
|
r_list_append (abb->bbs, bb);
|
|
|
|
|
if (!r_list_empty (abb->nextbbs)) {
|
|
|
|
|
do {
|
|
|
|
|
AbbAddr *nat = r_list_pop (abb->nextbbs);
|
|
|
|
|
if (!bbExist (abb, nat->addr)) {
|
|
|
|
|
if (nat->addr > at && nat->addr< at + len) {
|
|
|
|
|
if (ti == -1) {
|
|
|
|
|
ti = i;
|
|
|
|
|
}
|
|
|
|
|
i = nat->addr - at;
|
|
|
|
|
abb->bb_addr = nat->addr;
|
|
|
|
|
free (nat);
|
|
|
|
|
goto mountain;
|
|
|
|
|
} else {
|
|
|
|
|
eprintf ("Out of bounds basic block for 0x%08"PFMT64x"\n", nat->addr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free (nat);
|
|
|
|
|
} while (!r_list_empty (abb->nextbbs));
|
|
|
|
|
if (ti != -1) {
|
|
|
|
|
i = ti;
|
|
|
|
|
ti = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i += aop.size - 1;
|
|
|
|
|
r_anal_op_fini (&aop);
|
|
|
|
|
}
|
|
|
|
|
// show results
|
|
|
|
|
r_list_foreach (abb->bbs, iter, bb) {
|
|
|
|
|
RFlagItem *f = r_flag_get_at (core->flags, bb->addr, true);
|
|
|
|
|
char *name;
|
|
|
|
|
if (f) {
|
|
|
|
|
if (f->offset != bb->addr) {
|
|
|
|
|
name = r_str_newf ("%s+0x%x", f->name, bb->addr - f->offset);
|
|
|
|
|
} else {
|
|
|
|
|
name = strdup (f->name);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
name = r_str_newf ("bb.%"PFMT64x, bb->addr);
|
|
|
|
|
}
|
|
|
|
|
r_cons_printf ("agn 0x%08"PFMT64x" \"%s\"\n", bb->addr, name);
|
|
|
|
|
free (name);
|
|
|
|
|
}
|
|
|
|
|
r_list_foreach (abb->bbs, iter, bb) {
|
|
|
|
|
if (bb->jump != UT64_MAX) {
|
|
|
|
|
r_cons_printf ("age 0x%08"PFMT64x" 0x%08"PFMT64x"\n", bb->addr, bb->jump);
|
|
|
|
|
}
|
|
|
|
|
if (bb->fail != UT64_MAX) {
|
|
|
|
|
r_cons_printf ("age 0x%08"PFMT64x" 0x%08"PFMT64x"\n", bb->addr, bb->fail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|