2017-08-07 16:32:20 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2017 - pancake, oddcoder */
|
2009-02-13 23:25:15 +00:00
|
|
|
|
2010-03-12 01:45:24 +00:00
|
|
|
#include <r_anal.h>
|
|
|
|
#include <r_util.h>
|
2010-08-22 16:41:57 +00:00
|
|
|
#include <r_cons.h>
|
2010-03-12 01:45:24 +00:00
|
|
|
#include <r_list.h>
|
2009-02-13 23:25:15 +00:00
|
|
|
|
2014-03-31 02:42:55 +00:00
|
|
|
#define DB a->sdb_fcns
|
|
|
|
|
2014-09-22 02:22:47 +00:00
|
|
|
struct VarType {
|
2014-12-13 17:06:01 +00:00
|
|
|
char kind;
|
2014-09-22 02:22:47 +00:00
|
|
|
char *type;
|
|
|
|
int size;
|
|
|
|
char *name;
|
|
|
|
};
|
2014-12-13 17:06:01 +00:00
|
|
|
|
|
|
|
#define SDB_VARTYPE_FMT "czdz"
|
2014-09-22 02:22:47 +00:00
|
|
|
|
2017-05-09 12:25:57 +00:00
|
|
|
#define EXISTS(x, ...) snprintf (key, sizeof (key) - 1, x, ## __VA_ARGS__), sdb_exists (DB, key)
|
|
|
|
#define SETKEY(x, ...) snprintf (key, sizeof (key) - 1, x, ## __VA_ARGS__);
|
|
|
|
#define SETKEY2(x, ...) snprintf (key2, sizeof (key) - 1, x, ## __VA_ARGS__);
|
|
|
|
#define SETVAL(x, ...) snprintf (val, sizeof (val) - 1, x, ## __VA_ARGS__);
|
2016-09-14 14:42:23 +00:00
|
|
|
R_API bool r_anal_var_display(RAnal *anal, int delta, char kind, const char *type) {
|
2016-09-13 22:11:20 +00:00
|
|
|
char *fmt = r_anal_type_format (anal, type);
|
|
|
|
RRegItem *i;
|
|
|
|
if (!fmt) {
|
|
|
|
eprintf ("type:%s doesn't exist\n", type);
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-09 20:55:15 +00:00
|
|
|
bool usePxr = !strcmp (type, "int"); // hacky but useful
|
2016-09-13 22:11:20 +00:00
|
|
|
switch (kind) {
|
|
|
|
case R_ANAL_VAR_KIND_REG:
|
|
|
|
i = r_reg_index_get (anal->reg, delta);
|
|
|
|
if (i) {
|
2017-08-09 20:55:15 +00:00
|
|
|
if (usePxr) {
|
|
|
|
anal->cb_printf ("pxr $w @r:%s\n", i->name);
|
|
|
|
} else {
|
|
|
|
anal->cb_printf ("pf r (%s)\n", i->name);
|
|
|
|
}
|
2016-09-13 22:11:20 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("register not found\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R_ANAL_VAR_KIND_BPV:
|
|
|
|
if (delta > 0) {
|
2017-08-09 20:55:15 +00:00
|
|
|
if (usePxr) {
|
|
|
|
anal->cb_printf ("pxr $w @%s+0x%x\n", anal->reg->name[R_REG_NAME_BP], delta);
|
|
|
|
} else {
|
|
|
|
anal->cb_printf ("pf %s @%s+0x%x\n", fmt, anal->reg->name[R_REG_NAME_BP], delta);
|
|
|
|
}
|
2016-09-13 22:11:20 +00:00
|
|
|
} else {
|
2017-08-09 20:55:15 +00:00
|
|
|
if (usePxr) {
|
2017-09-07 14:33:38 +00:00
|
|
|
anal->cb_printf ("pxr $w @%s-0x%x\n", anal->reg->name[R_REG_NAME_BP], -delta);
|
2017-08-09 20:55:15 +00:00
|
|
|
} else {
|
|
|
|
anal->cb_printf ("pf %s @%s-0x%x\n", fmt, anal->reg->name[R_REG_NAME_BP], -delta);
|
|
|
|
}
|
2016-09-13 22:11:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R_ANAL_VAR_KIND_SPV:
|
2017-08-09 20:55:15 +00:00
|
|
|
if (usePxr) {
|
|
|
|
anal->cb_printf ("pxr $w @%s+0x%x\n", anal->reg->name[R_REG_NAME_SP], delta);
|
|
|
|
} else {
|
|
|
|
anal->cb_printf ("pf %s @ %s+0x%x\n", fmt, anal->reg->name[R_REG_NAME_SP], delta);
|
|
|
|
}
|
2016-09-13 22:11:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
free (fmt);
|
2016-09-14 14:42:23 +00:00
|
|
|
return true;
|
2016-09-13 22:11:20 +00:00
|
|
|
}
|
2016-09-14 14:42:23 +00:00
|
|
|
|
2017-08-09 17:21:53 +00:00
|
|
|
R_API bool r_anal_var_add(RAnal *a, ut64 addr, int scope, int delta, char kind, const char *type, int size, const char *name) {
|
|
|
|
if (!a) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 23:00:47 +00:00
|
|
|
if (!kind) {
|
|
|
|
kind = R_ANAL_VAR_KIND_BPV;
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
type = "int";
|
|
|
|
}
|
2014-04-04 01:42:22 +00:00
|
|
|
switch (kind) {
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_BPV: // base pointer var/args
|
|
|
|
case R_ANAL_VAR_KIND_SPV: // stack pointer var/args
|
|
|
|
case R_ANAL_VAR_KIND_REG: // registers args
|
2014-04-04 01:42:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-26 08:37:18 +00:00
|
|
|
eprintf ("Invalid var kind '%c'\n", kind);
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *var_def = sdb_fmt (0, "%c,%s,%d,%s", kind, type, size, name);
|
2016-08-07 23:00:47 +00:00
|
|
|
if (scope > 0) {
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *sign = "";
|
2016-06-05 22:33:42 +00:00
|
|
|
if (delta < 0) {
|
|
|
|
delta = -delta;
|
2016-08-08 22:17:42 +00:00
|
|
|
sign = "_";
|
2016-06-05 22:33:42 +00:00
|
|
|
}
|
2014-09-03 21:02:39 +00:00
|
|
|
/* local variable */
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *fcn_key = sdb_fmt (1, "fcn.0x%"PFMT64x ".%c", addr, kind);
|
|
|
|
const char *var_key = sdb_fmt (2, "var.0x%"PFMT64x ".%c.%d.%s%d", addr, kind, scope, sign, delta);
|
|
|
|
const char *name_key = sdb_fmt (3, "var.0x%"PFMT64x ".%d.%s", addr, scope, name);
|
|
|
|
const char *shortvar = sdb_fmt (4, "%d.%s%d", scope, sign, delta);
|
2014-09-22 02:22:47 +00:00
|
|
|
sdb_array_add (DB, fcn_key, shortvar, 0);
|
2014-09-03 21:02:39 +00:00
|
|
|
sdb_set (DB, var_key, var_def, 0);
|
2016-06-05 22:33:42 +00:00
|
|
|
if (*sign) {
|
|
|
|
delta = -delta;
|
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *name_val = sdb_fmt (5, "%c,%d", kind, delta);
|
2016-08-08 22:17:42 +00:00
|
|
|
sdb_set (DB, name_key, name_val, 0);
|
2014-04-03 22:23:03 +00:00
|
|
|
} else {
|
2014-09-03 21:02:39 +00:00
|
|
|
/* global variable */
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *var_global = sdb_fmt (1, "var.0x%"PFMT64x, addr);
|
|
|
|
const char *var_def = sdb_fmt (2, "%c.%s,%d,%s", kind, type, size, name);
|
2014-09-03 21:02:39 +00:00
|
|
|
sdb_array_add (DB, var_global, var_def, 0);
|
2014-04-03 22:23:03 +00:00
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
// ls_sort (DB->ht->list, mystrcmp);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2014-04-03 22:23:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API int r_anal_var_retype(RAnal *a, ut64 addr, int scope, int delta, char kind, const char *type, int size, const char *name) {
|
2017-08-07 16:16:23 +00:00
|
|
|
if (!a) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (kind < 1) {
|
2016-08-07 23:00:47 +00:00
|
|
|
kind = R_ANAL_VAR_KIND_BPV;
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
type = "int";
|
|
|
|
}
|
2016-08-22 09:22:22 +00:00
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (a, addr, 0);
|
|
|
|
if (!fcn) {
|
2017-08-07 16:16:23 +00:00
|
|
|
eprintf ("Cant find function here\n");
|
2016-08-15 07:54:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 10:09:14 +00:00
|
|
|
if (size == -1) {
|
2015-12-14 00:49:53 +00:00
|
|
|
RList *list = r_anal_var_list (a, fcn, kind);
|
|
|
|
RListIter *iter;
|
|
|
|
RAnalVar *var;
|
|
|
|
r_list_foreach (list, iter, var) {
|
2017-08-09 17:21:53 +00:00
|
|
|
if (delta == -1 && !strcmp (var->name, name)) {
|
|
|
|
delta = var->delta;
|
|
|
|
size = var->size;
|
|
|
|
break;
|
2015-12-14 00:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-31 15:28:16 +00:00
|
|
|
r_list_free (list);
|
2015-12-14 00:49:53 +00:00
|
|
|
}
|
|
|
|
switch (kind) {
|
2016-08-07 23:00:47 +00:00
|
|
|
case R_ANAL_VAR_KIND_REG:
|
|
|
|
case R_ANAL_VAR_KIND_BPV:
|
|
|
|
case R_ANAL_VAR_KIND_SPV:
|
2015-12-14 00:49:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("Invalid var kind '%c'\n", kind);
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
const char *var_def = sdb_fmt (0, "%c,%s,%d,%s", kind, type, size, name);
|
2016-05-24 00:39:39 +00:00
|
|
|
if (scope > 0) {
|
2017-08-07 16:16:23 +00:00
|
|
|
char *sign = delta> 0? "": "_";
|
2015-12-14 00:49:53 +00:00
|
|
|
/* local variable */
|
2017-03-09 22:46:02 +00:00
|
|
|
const char *fcn_key = sdb_fmt (1, "fcn.0x%"PFMT64x ".%c", fcn->addr, kind);
|
2017-08-07 16:16:23 +00:00
|
|
|
const char *var_key = sdb_fmt (2, "var.0x%"PFMT64x ".%c.%d.%s%d", fcn->addr, kind, scope, sign, R_ABS(delta));
|
2017-03-09 22:46:02 +00:00
|
|
|
const char *name_key = sdb_fmt (3, "var.0x%"PFMT64x ".%d.%s", fcn->addr, scope, name);
|
2017-08-07 16:16:23 +00:00
|
|
|
const char *shortvar = sdb_fmt (4, "%d.%s%d", scope, sign, R_ABS(delta));
|
2016-08-08 22:17:42 +00:00
|
|
|
const char *name_val = sdb_fmt (5, "%c,%d", kind, delta);
|
2015-12-14 00:49:53 +00:00
|
|
|
sdb_array_add (DB, fcn_key, shortvar, 0);
|
|
|
|
sdb_set (DB, var_key, var_def, 0);
|
2016-08-08 22:17:42 +00:00
|
|
|
sdb_set (DB, name_key, name_val, 0);
|
2016-09-12 20:43:05 +00:00
|
|
|
Sdb *TDB = a->sdb_types;
|
2017-03-09 22:46:02 +00:00
|
|
|
const char *type_kind = sdb_const_get (TDB, type, 0);
|
2016-09-12 20:43:05 +00:00
|
|
|
if (type_kind && r_str_startswith (type_kind, "struct")) {
|
2017-08-07 16:16:23 +00:00
|
|
|
char *field;
|
|
|
|
int field_n;
|
2016-09-12 20:43:05 +00:00
|
|
|
char *type_key = r_str_newf ("%s.%s", type_kind, type);
|
2017-08-07 16:16:23 +00:00
|
|
|
for (field_n = 0; (field = sdb_array_get (TDB, type_key, field_n, NULL)); field_n++) {
|
|
|
|
char *field_key = r_str_newf ("%s.%s", type_key, field);
|
|
|
|
char *field_type = sdb_array_get (TDB, field_key, 0, NULL);
|
|
|
|
ut64 field_offset = sdb_array_get_num (TDB, field_key, 1, NULL);
|
2016-09-12 20:43:05 +00:00
|
|
|
if (field_offset != 0) { // delete variables which are overlayed by structure
|
|
|
|
r_anal_var_delete (a, addr, kind, scope, delta + field_offset);
|
|
|
|
}
|
|
|
|
free (field_type);
|
|
|
|
free (field_key);
|
|
|
|
free (field);
|
|
|
|
}
|
|
|
|
free (type_key);
|
|
|
|
}
|
2015-12-14 00:49:53 +00:00
|
|
|
} else {
|
|
|
|
/* global variable */
|
2016-08-13 10:09:14 +00:00
|
|
|
const char *var_global = sdb_fmt (1, "var.0x%"PFMT64x, fcn->addr);
|
2015-12-14 00:49:53 +00:00
|
|
|
sdb_array_add (DB, var_global, var_def, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API int r_anal_var_delete_all(RAnal *a, ut64 addr, const char kind) {
|
2017-08-09 17:21:53 +00:00
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (a, addr, 0);
|
2015-12-15 01:08:53 +00:00
|
|
|
if (fcn) {
|
|
|
|
RAnalVar *v;
|
|
|
|
RListIter *iter;
|
|
|
|
RList *list = r_anal_var_list (a, fcn, kind);
|
|
|
|
r_list_foreach (list, iter, v) {
|
2017-03-09 22:46:02 +00:00
|
|
|
// r_anal_var_delete (a, addr, kind, v->scope, v->delta);
|
2015-12-15 01:08:53 +00:00
|
|
|
r_anal_var_delete (a, addr, kind, 1, v->delta);
|
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
// XXX: i dont think we want to alocate and free by hand. r_anal_var_delete should be the list->free already
|
2015-12-15 01:08:53 +00:00
|
|
|
r_list_free (list);
|
|
|
|
}
|
2015-12-19 16:04:01 +00:00
|
|
|
return 0;
|
2015-12-15 01:08:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API int r_anal_var_delete(RAnal *a, ut64 addr, const char kind, int scope, int delta) {
|
|
|
|
RAnalVar *av = r_anal_var_get (a, addr, kind, scope, delta);
|
2017-03-09 22:46:02 +00:00
|
|
|
if (!av) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 21:26:13 +00:00
|
|
|
if (scope > 0) {
|
2016-06-05 22:33:42 +00:00
|
|
|
char *sign = "";
|
|
|
|
if (delta < 0) {
|
|
|
|
delta = -delta;
|
2016-08-08 22:17:42 +00:00
|
|
|
sign = "_";
|
2016-06-05 22:33:42 +00:00
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
char *fcn_key = sdb_fmt (1, "fcn.0x%"PFMT64x ".%c", addr, kind);
|
|
|
|
char *var_key = sdb_fmt (2, "var.0x%"PFMT64x ".%c.%d.%s%d", addr, kind, scope, sign, delta);
|
|
|
|
char *name_key = sdb_fmt (3, "var.0x%"PFMT64x ".%d.%s", addr, scope, av->name);
|
2016-06-05 22:33:42 +00:00
|
|
|
char *shortvar = sdb_fmt (4, "%d.%s%d", scope, sign, delta);
|
2015-07-10 17:14:00 +00:00
|
|
|
sdb_array_remove (DB, fcn_key, shortvar, 0);
|
|
|
|
sdb_unset (DB, var_key, 0);
|
|
|
|
sdb_unset (DB, name_key, 0);
|
2017-03-09 22:46:02 +00:00
|
|
|
if (*sign) {
|
|
|
|
delta = -delta;
|
|
|
|
}
|
2014-04-04 01:42:22 +00:00
|
|
|
} else {
|
2015-07-10 17:14:00 +00:00
|
|
|
char *var_global = sdb_fmt (1, "var.0x%"PFMT64x, addr);
|
2017-03-09 22:46:02 +00:00
|
|
|
char *var_def = sdb_fmt (2, "%c.%s,%d,%s", kind, av->type, av->size, av->name);
|
2015-07-10 17:14:00 +00:00
|
|
|
sdb_array_remove (DB, var_global, var_def, 0);
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
2016-06-27 21:26:13 +00:00
|
|
|
r_anal_var_free (av);
|
2015-07-10 17:14:00 +00:00
|
|
|
r_anal_var_access_clear (a, addr, scope, delta);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API bool r_anal_var_delete_byname(RAnal *a, RAnalFunction *fcn, int kind, const char *name) {
|
2015-12-28 00:47:16 +00:00
|
|
|
char *varlist;
|
|
|
|
if (!a || !fcn) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
varlist = sdb_get (DB, sdb_fmt (0, "fcn.0x%"PFMT64x ".%c",
|
|
|
|
fcn->addr, kind), 0);
|
2015-12-28 00:47:16 +00:00
|
|
|
if (varlist) {
|
|
|
|
char *next, *ptr = varlist;
|
|
|
|
if (varlist && *varlist) {
|
|
|
|
do {
|
|
|
|
char *word = sdb_anext (ptr, &next);
|
2016-08-08 22:17:42 +00:00
|
|
|
char *sign = strstr (word, "_");
|
2016-06-03 13:33:09 +00:00
|
|
|
const char *vardef = sdb_const_get (DB, sdb_fmt (1,
|
2017-03-09 22:46:02 +00:00
|
|
|
"var.0x%"PFMT64x ".%c.%s",
|
|
|
|
fcn->addr, kind, word), 0);
|
2016-06-05 22:33:42 +00:00
|
|
|
if (sign) {
|
|
|
|
*sign = '-';
|
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
int delta = strlen (word) < 3? -1: atoi (word + 2);
|
2015-12-28 00:47:16 +00:00
|
|
|
if (vardef) {
|
|
|
|
const char *p = strchr (vardef, ',');
|
|
|
|
if (p) {
|
|
|
|
p = strchr (p + 1, ',');
|
|
|
|
if (p) {
|
|
|
|
p = strchr (p + 1, ',');
|
|
|
|
if (p) {
|
|
|
|
int mykind = vardef[0];
|
2016-05-24 00:39:39 +00:00
|
|
|
if (!strcmp (p + 1, name)) {
|
2015-12-28 00:47:16 +00:00
|
|
|
return r_anal_var_delete (a, fcn->addr,
|
|
|
|
mykind, 1, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("Inconsistent Sdb storage, Cannot find '%s'\n", word);
|
|
|
|
}
|
|
|
|
ptr = next;
|
|
|
|
} while (next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (varlist);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:46:02 +00:00
|
|
|
R_API RAnalVar *r_anal_var_get_byname(RAnal *a, RAnalFunction *fcn, const char *name) {
|
2016-08-08 22:17:42 +00:00
|
|
|
if (!fcn || !a || !name) {
|
2017-08-07 16:16:23 +00:00
|
|
|
// eprintf ("No something\n");
|
2016-08-22 12:52:06 +00:00
|
|
|
return NULL;
|
2016-06-08 16:13:36 +00:00
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
char *name_key = sdb_fmt (-1, "var.0x%"PFMT64x ".%d.%s", fcn->addr, 1, name);
|
2016-08-22 12:52:06 +00:00
|
|
|
const char *name_value = sdb_const_get (DB, name_key, 0);
|
2016-08-08 22:17:42 +00:00
|
|
|
if (!name_value) {
|
2017-08-07 16:16:23 +00:00
|
|
|
// eprintf ("Cant find key for %s\n", name_key);
|
2016-08-22 12:52:06 +00:00
|
|
|
return NULL;
|
2016-06-08 16:13:36 +00:00
|
|
|
}
|
2016-08-22 12:52:06 +00:00
|
|
|
const char *comma = strchr (name_value, ',');
|
|
|
|
if (comma) {
|
|
|
|
int delta = r_num_math (NULL, comma + 1);
|
2017-08-07 16:16:23 +00:00
|
|
|
// eprintf ("Silently failing (%s)\n", name_value);
|
2016-08-22 12:52:06 +00:00
|
|
|
return r_anal_var_get (a, fcn->addr, *name_value, 1, delta);
|
|
|
|
}
|
|
|
|
return NULL;
|
2016-06-08 16:13:36 +00:00
|
|
|
}
|
2016-08-22 12:52:06 +00:00
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API RAnalVar *r_anal_var_get(RAnal *a, ut64 addr, char kind, int scope, int delta) {
|
2014-09-22 11:45:36 +00:00
|
|
|
RAnalVar *av;
|
2017-03-09 22:46:02 +00:00
|
|
|
struct VarType vt = {
|
|
|
|
0
|
|
|
|
};
|
2016-06-05 22:33:42 +00:00
|
|
|
char *sign = "";
|
2014-09-26 16:10:33 +00:00
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (a, addr, 0);
|
2016-08-22 12:52:06 +00:00
|
|
|
if (!fcn) {
|
2014-09-22 11:45:36 +00:00
|
|
|
return NULL;
|
2016-08-22 12:52:06 +00:00
|
|
|
}
|
2016-06-05 22:33:42 +00:00
|
|
|
if (delta < 0) {
|
2014-09-22 11:45:36 +00:00
|
|
|
delta = -delta;
|
2016-08-08 22:17:42 +00:00
|
|
|
sign = "_";
|
2014-09-22 11:45:36 +00:00
|
|
|
}
|
2017-08-07 16:16:23 +00:00
|
|
|
const char *varkey = sdb_fmt (-1, "var.0x%"PFMT64x ".%c.%d.%s%d",
|
|
|
|
fcn->addr, kind, scope, sign, delta);
|
|
|
|
const char *vardef = sdb_const_get (DB, varkey, 0);
|
2016-05-24 00:39:39 +00:00
|
|
|
if (!vardef) {
|
2014-09-22 11:45:36 +00:00
|
|
|
return NULL;
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2017-08-07 16:16:23 +00:00
|
|
|
if (*sign) {
|
|
|
|
delta = -delta;
|
|
|
|
}
|
2016-08-31 01:17:51 +00:00
|
|
|
sdb_fmt_init (&vt, SDB_VARTYPE_FMT);
|
2014-09-22 11:45:36 +00:00
|
|
|
sdb_fmt_tobin (vardef, SDB_VARTYPE_FMT, &vt);
|
|
|
|
|
|
|
|
av = R_NEW0 (RAnalVar);
|
2016-06-03 13:33:09 +00:00
|
|
|
if (!av) {
|
|
|
|
sdb_fmt_free (&vt, SDB_VARTYPE_FMT);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-13 10:09:14 +00:00
|
|
|
av->addr = fcn->addr;
|
2014-04-04 01:42:22 +00:00
|
|
|
av->scope = scope;
|
2014-04-05 00:20:06 +00:00
|
|
|
av->delta = delta;
|
2017-03-09 22:46:02 +00:00
|
|
|
av->name = vt.name? strdup (vt.name): strdup ("unkown_var");
|
2014-09-22 11:45:36 +00:00
|
|
|
av->size = vt.size;
|
2017-03-09 22:46:02 +00:00
|
|
|
av->type = vt.type? strdup (vt.type): strdup ("unkown_type");
|
2016-08-08 22:17:42 +00:00
|
|
|
av->kind = kind;
|
2014-09-22 11:45:36 +00:00
|
|
|
sdb_fmt_free (&vt, SDB_VARTYPE_FMT);
|
2014-04-04 01:42:22 +00:00
|
|
|
// TODO:
|
|
|
|
// get name from sdb
|
|
|
|
// get size from sdb
|
|
|
|
// get type from sdb
|
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:26:13 +00:00
|
|
|
R_API void r_anal_var_free(RAnalVar *av) {
|
2016-02-15 16:49:17 +00:00
|
|
|
if (av) {
|
|
|
|
free (av->name);
|
|
|
|
free (av->type);
|
2016-06-27 21:26:13 +00:00
|
|
|
R_FREE (av);
|
2016-02-15 16:49:17 +00:00
|
|
|
}
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 21:02:39 +00:00
|
|
|
/* (columns) elements in the array value */
|
2014-04-04 01:42:22 +00:00
|
|
|
#define R_ANAL_VAR_SDB_KIND 0 /* char */
|
|
|
|
#define R_ANAL_VAR_SDB_TYPE 1 /* string */
|
|
|
|
#define R_ANAL_VAR_SDB_SIZE 2 /* number */
|
|
|
|
#define R_ANAL_VAR_SDB_NAME 3 /* string */
|
|
|
|
|
2017-08-09 17:21:53 +00:00
|
|
|
#define IS_NUMBER(x) ((x) >= '0' && (x) <= '9')
|
|
|
|
|
|
|
|
R_API bool r_anal_var_check_name(const char *name) {
|
2017-08-09 20:55:15 +00:00
|
|
|
return !IS_NUMBER (*name) && strcspn (name, "., =/");
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 17:06:01 +00:00
|
|
|
// afvn local_48 counter
|
2016-07-29 17:35:34 +00:00
|
|
|
R_API int r_anal_var_rename(RAnal *a, ut64 var_addr, int scope, char kind, const char *old_name, const char *new_name) {
|
2016-05-24 00:39:39 +00:00
|
|
|
char key[128], *stored_name;
|
2014-12-13 17:06:01 +00:00
|
|
|
int delta;
|
2016-05-24 00:39:39 +00:00
|
|
|
|
|
|
|
if (!r_anal_var_check_name (new_name)) {
|
2017-08-07 16:16:23 +00:00
|
|
|
// eprintf ("Invalid name\n");
|
2014-04-04 01:42:22 +00:00
|
|
|
return 0;
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2016-08-08 22:17:42 +00:00
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (a, var_addr, 0);
|
|
|
|
RAnalVar *v1 = r_anal_var_get_byname (a, fcn, new_name);
|
2017-03-09 22:46:02 +00:00
|
|
|
if (v1) {
|
2016-08-08 22:17:42 +00:00
|
|
|
r_anal_var_free (v1);
|
|
|
|
eprintf ("variable or arg with name `%s` already exist\n", new_name);
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 23:00:47 +00:00
|
|
|
// XXX: This is hardcoded because ->kind seems to be 0
|
|
|
|
scope = 1;
|
|
|
|
// XXX. this is pretty weak, because oldname may not exist too and error returned.
|
|
|
|
if (scope > 0) { // local
|
2016-08-08 22:17:42 +00:00
|
|
|
const char *sign = "";
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("var.0x%"PFMT64x ".%d.%s", var_addr, scope, old_name);
|
2016-08-08 22:17:42 +00:00
|
|
|
char *name_val = sdb_get (DB, key, 0);
|
2016-08-22 12:52:06 +00:00
|
|
|
char *comma = strchr (name_val, ',');
|
|
|
|
if (comma) {
|
|
|
|
delta = r_num_math (NULL, comma + 1);
|
|
|
|
sdb_unset (DB, key, 0);
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("var.0x%"PFMT64x ".%d.%s", var_addr, scope, new_name);
|
2016-08-22 12:52:06 +00:00
|
|
|
sdb_set (DB, key, name_val, 0);
|
|
|
|
free (name_val);
|
|
|
|
if (delta < 0) {
|
|
|
|
delta = -delta;
|
|
|
|
sign = "_";
|
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("var.0x%"PFMT64x ".%c.%d.%s%d", var_addr, kind, scope, sign, delta);
|
2016-08-22 12:52:06 +00:00
|
|
|
sdb_array_set (DB, key, R_ANAL_VAR_SDB_NAME, new_name, 0);
|
2016-06-05 22:33:42 +00:00
|
|
|
}
|
2014-04-04 01:42:22 +00:00
|
|
|
} else { // global
|
|
|
|
SETKEY ("var.0x%"PFMT64x, var_addr);
|
2014-12-13 17:06:01 +00:00
|
|
|
stored_name = sdb_array_get (DB, key, R_ANAL_VAR_SDB_NAME, 0);
|
2016-08-07 23:00:47 +00:00
|
|
|
if (!stored_name) {
|
|
|
|
eprintf ("Cannot find key in storage.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-19 12:44:47 +00:00
|
|
|
if (!old_name) {
|
2016-08-07 23:00:47 +00:00
|
|
|
old_name = stored_name;
|
|
|
|
} else if (strcmp (stored_name, old_name)) {
|
|
|
|
eprintf ("Old name missmatch %s vs %s.\n", stored_name, old_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-04 01:42:22 +00:00
|
|
|
sdb_unset (DB, key, 0);
|
2014-12-13 17:06:01 +00:00
|
|
|
SETKEY ("var.0x%"PFMT64x, var_addr);
|
2014-04-04 01:42:22 +00:00
|
|
|
sdb_array_set (DB, key, R_ANAL_VAR_SDB_NAME, new_name, 0);
|
|
|
|
}
|
|
|
|
// var.sdb_hash(old_name)=var_addr.scope.delta
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// avr
|
2017-03-09 22:46:02 +00:00
|
|
|
R_API int r_anal_var_access(RAnal *a, ut64 var_addr, char kind, int scope, int delta, int xs_type, ut64 xs_addr) {
|
2014-09-03 21:02:39 +00:00
|
|
|
const char *var_global;
|
2014-04-04 01:42:22 +00:00
|
|
|
const char *xs_type_str = xs_type? "writes": "reads";
|
2016-02-16 22:07:39 +00:00
|
|
|
// TODO: kind is not used
|
|
|
|
if (scope > 0) { // local
|
2017-03-09 22:46:02 +00:00
|
|
|
const char *var_local = sdb_fmt (0, "var.0x%"PFMT64x ".%d.%d.%s",
|
2014-09-03 21:02:39 +00:00
|
|
|
var_addr, scope, delta, xs_type_str);
|
2017-03-09 22:46:02 +00:00
|
|
|
const char *inst_key = sdb_fmt (1, "inst.0x%"PFMT64x ".vars", xs_addr);
|
|
|
|
const char *var_def = sdb_fmt (2, "0x%"PFMT64x ",%c,0x%x,0x%x", var_addr,
|
2016-02-15 16:49:17 +00:00
|
|
|
kind, scope, delta);
|
|
|
|
sdb_set (DB, inst_key, var_def, 0);
|
2014-09-03 21:02:39 +00:00
|
|
|
return sdb_array_add_num (DB, var_local, xs_addr, 0);
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
2014-09-03 21:02:39 +00:00
|
|
|
// global
|
2017-03-09 22:46:02 +00:00
|
|
|
sdb_add (DB, sdb_fmt (0, "var.0x%"PFMT64x, var_addr), "a,", 0);
|
|
|
|
var_global = sdb_fmt (0, "var.0x%"PFMT64x ".%s", var_addr, xs_type_str);
|
2014-09-03 21:02:39 +00:00
|
|
|
return sdb_array_add_num (DB, var_global, xs_addr, 0);
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:46:02 +00:00
|
|
|
R_API void r_anal_var_access_clear(RAnal *a, ut64 var_addr, int scope, int delta) {
|
2014-04-04 01:42:22 +00:00
|
|
|
char key[128], key2[128];
|
2017-03-09 22:46:02 +00:00
|
|
|
if (scope > 0) { // local arg or var
|
|
|
|
SETKEY ("var.0x%"PFMT64x ".%d.%d.%s", var_addr, scope, delta, "writes");
|
|
|
|
SETKEY2 ("var.0x%"PFMT64x ".%d.%d.%s", var_addr, scope, delta, "reads");
|
2014-04-04 01:42:22 +00:00
|
|
|
} else { // global
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("var.0x%"PFMT64x ".%s", var_addr, "writes");
|
|
|
|
SETKEY2 ("var.0x%"PFMT64x ".%s", var_addr, "reads");
|
2014-04-04 01:42:22 +00:00
|
|
|
}
|
|
|
|
sdb_unset (DB, key, 0);
|
|
|
|
sdb_unset (DB, key2, 0);
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:46:02 +00:00
|
|
|
R_API int r_anal_fcn_var_del_bydelta(RAnal *a, ut64 fna, const char kind, int scope, ut32 delta) {
|
2014-09-03 21:02:39 +00:00
|
|
|
int idx;
|
2014-03-31 02:42:55 +00:00
|
|
|
char key[128], val[128], *v;
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("fcn.0x%08"PFMT64x ".%c", fna, kind);
|
2014-06-06 12:07:06 +00:00
|
|
|
v = sdb_itoa (delta, val, 10);
|
2014-09-03 21:02:39 +00:00
|
|
|
idx = sdb_array_indexof (DB, key, v, 0);
|
2014-03-31 02:42:55 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
sdb_array_delete (DB, key, idx, 0);
|
2017-03-09 22:46:02 +00:00
|
|
|
SETKEY ("fcn.0x%08"PFMT64x ".%c.%d", fna, kind, delta);
|
2014-03-31 02:42:55 +00:00
|
|
|
sdb_unset (DB, key, 0);
|
|
|
|
}
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2014-03-31 02:42:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 19:50:55 +00:00
|
|
|
R_API int r_anal_var_count(RAnal *a, RAnalFunction *fcn, int kind, int type) {
|
2017-08-09 20:55:15 +00:00
|
|
|
// type { local: 0, arg: 1 };
|
2016-07-19 19:50:55 +00:00
|
|
|
RList *list = r_anal_var_list (a, fcn, kind);
|
|
|
|
RAnalVar *var;
|
|
|
|
RListIter *iter;
|
2017-03-09 22:46:02 +00:00
|
|
|
int count[2] = {
|
|
|
|
0
|
|
|
|
};
|
2016-08-07 23:00:47 +00:00
|
|
|
r_list_foreach (list, iter, var) {
|
2016-08-08 22:17:42 +00:00
|
|
|
if (kind == R_ANAL_VAR_KIND_REG) {
|
2017-03-09 22:46:02 +00:00
|
|
|
count[1]++;
|
2016-07-19 19:50:55 +00:00
|
|
|
continue;
|
2015-09-16 23:47:40 +00:00
|
|
|
}
|
2017-03-09 12:23:41 +00:00
|
|
|
count[(kind == R_ANAL_VAR_KIND_BPV && var->delta > 0) || (kind == R_ANAL_VAR_KIND_SPV && var->delta > fcn->maxstack)]++;
|
2015-09-16 23:47:40 +00:00
|
|
|
}
|
2016-07-19 19:50:55 +00:00
|
|
|
r_list_free (list);
|
|
|
|
return count[type];
|
2015-09-16 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:46:02 +00:00
|
|
|
static void var_add_structure_fields_to_list(RAnal *a, RAnalVar *av, const char *base_name, int delta, RList *list) {
|
2016-09-12 20:43:05 +00:00
|
|
|
/* ATTENTION: av->name might be freed and reassigned */
|
|
|
|
Sdb *TDB = a->sdb_types;
|
|
|
|
const char *type_kind = sdb_const_get (TDB, av->type, 0);
|
|
|
|
if (type_kind && r_str_startswith (type_kind, "struct")) {
|
2017-08-07 16:16:23 +00:00
|
|
|
char *field_name, *new_name;
|
|
|
|
int field_n;
|
2016-09-12 20:43:05 +00:00
|
|
|
char *type_key = r_str_newf ("%s.%s", type_kind, av->type);
|
2017-08-07 16:16:23 +00:00
|
|
|
for (field_n = 0; (field_name = sdb_array_get (TDB, type_key, field_n, NULL)); field_n++) {
|
|
|
|
char *field_key = r_str_newf ("%s.%s", type_key, field_name);
|
|
|
|
char *field_type = sdb_array_get (TDB, field_key, 0, NULL);
|
|
|
|
ut64 field_offset = sdb_array_get_num (TDB, field_key, 1, NULL);
|
|
|
|
int field_count = sdb_array_get_num (TDB, field_key, 2, NULL);
|
|
|
|
int field_size = r_anal_type_get_size (a, field_type) * (field_count? field_count: 1);
|
2016-09-12 20:43:05 +00:00
|
|
|
new_name = r_str_newf ( "%s.%s", base_name, field_name);
|
|
|
|
if (field_offset == 0) {
|
|
|
|
free (av->name);
|
|
|
|
av->name = new_name;
|
|
|
|
} else {
|
|
|
|
RAnalVar *fav = R_NEW0 (RAnalVar);
|
|
|
|
if (!fav) {
|
|
|
|
free (field_key);
|
|
|
|
free (new_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fav->delta = delta + field_offset;
|
|
|
|
fav->kind = av->kind;
|
|
|
|
fav->name = new_name;
|
|
|
|
fav->size = field_size;
|
|
|
|
fav->type = strdup (field_type);
|
|
|
|
r_list_append (list, fav);
|
|
|
|
}
|
|
|
|
free (field_type);
|
|
|
|
free (field_key);
|
|
|
|
free (field_name);
|
|
|
|
}
|
|
|
|
free (type_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RList *var_generate_list(RAnal *a, RAnalFunction *fcn, int kind, bool dynamicVars) {
|
2016-05-24 00:39:39 +00:00
|
|
|
if (!a || !fcn) {
|
2016-03-31 19:43:58 +00:00
|
|
|
return NULL;
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
RList *list = r_list_newf ((RListFree) r_anal_var_free);
|
2017-01-24 23:06:44 +00:00
|
|
|
if (kind < 1) {
|
2016-08-07 23:00:47 +00:00
|
|
|
kind = R_ANAL_VAR_KIND_BPV; // by default show vars
|
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
char *varlist = sdb_get (DB, sdb_fmt (0, "fcn.0x%"PFMT64x ".%c", fcn->addr, kind), 0);
|
|
|
|
if (varlist && *varlist) {
|
2014-09-22 02:22:47 +00:00
|
|
|
char *next, *ptr = varlist;
|
2017-08-09 17:21:53 +00:00
|
|
|
do {
|
|
|
|
char *word = sdb_anext (ptr, &next);
|
|
|
|
if (r_str_nlen (word, 3) < 3) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const char *vardef = sdb_const_get (DB, sdb_fmt (1,
|
|
|
|
"var.0x%"PFMT64x ".%c.%s",
|
|
|
|
fcn->addr, kind, word), 0);
|
|
|
|
if (word[2] == '_') {
|
|
|
|
word[2] = '-';
|
|
|
|
}
|
|
|
|
int delta = atoi (word + 2);
|
|
|
|
if (vardef) {
|
|
|
|
struct VarType vt = { 0 };
|
|
|
|
sdb_fmt_init (&vt, SDB_VARTYPE_FMT);
|
|
|
|
sdb_fmt_tobin (vardef, SDB_VARTYPE_FMT, &vt);
|
|
|
|
RAnalVar *av = R_NEW0 (RAnalVar);
|
|
|
|
if (!av) {
|
|
|
|
free (varlist);
|
|
|
|
r_list_free (list);
|
|
|
|
return NULL;
|
2016-06-05 22:33:42 +00:00
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
if (!vt.name || !vt.type) {
|
|
|
|
// This should be properly fixed
|
|
|
|
eprintf ("Warning null var in fcn.0x%"PFMT64x ".%c.%s\n",
|
|
|
|
fcn->addr, kind, word);
|
|
|
|
free (av);
|
|
|
|
continue;
|
2014-10-28 16:24:01 +00:00
|
|
|
}
|
2017-08-09 17:21:53 +00:00
|
|
|
av->delta = delta;
|
|
|
|
av->kind = kind;
|
|
|
|
av->name = strdup (vt.name);
|
|
|
|
av->size = vt.size;
|
|
|
|
av->type = strdup (vt.type);
|
|
|
|
r_list_append (list, av);
|
|
|
|
if (dynamicVars) { // make dynamic variables like structure fields
|
|
|
|
var_add_structure_fields_to_list (a, av, vt.name, delta, list);
|
|
|
|
}
|
|
|
|
sdb_fmt_free (&vt, SDB_VARTYPE_FMT);
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot find var definition for '%s'\n", word);
|
|
|
|
}
|
|
|
|
ptr = next;
|
|
|
|
} while (next);
|
2010-08-22 16:41:57 +00:00
|
|
|
}
|
2014-09-22 02:22:47 +00:00
|
|
|
free (varlist);
|
2017-03-09 22:46:02 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RList *r_anal_var_all_list(RAnal *anal, RAnalFunction *fcn) {
|
|
|
|
// r_anal_var_list if there are not vars with that kind returns a list with
|
2017-08-09 17:21:53 +00:00
|
|
|
// zero element.. which is an unnecessary loss of cpu time
|
2017-03-09 22:46:02 +00:00
|
|
|
RList *list = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_ARG);
|
|
|
|
if (!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_REG));
|
|
|
|
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_BPV));
|
|
|
|
r_list_join (list, r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_SPV));
|
2014-12-13 17:06:01 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:43:05 +00:00
|
|
|
R_API RList *r_anal_var_list(RAnal *a, RAnalFunction *fcn, int kind) {
|
|
|
|
return var_generate_list (a, fcn, kind, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RList *r_anal_var_list_dynamic(RAnal *a, RAnalFunction *fcn, int kind) {
|
|
|
|
return var_generate_list (a, fcn, kind, true);
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:46:02 +00:00
|
|
|
static int var_comparator(const RAnalVar *a, const RAnalVar *b){
|
|
|
|
// avoid NULL dereference
|
|
|
|
return (a && b)? a->delta > b->delta: false;
|
2015-03-30 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 21:46:32 +00:00
|
|
|
R_API void r_anal_var_list_show(RAnal *anal, RAnalFunction *fcn, int kind, int mode) {
|
2016-05-24 00:39:39 +00:00
|
|
|
RList *list = r_anal_var_list (anal, fcn, kind);
|
2017-03-09 22:46:02 +00:00
|
|
|
r_list_sort (list, (RListComparator) var_comparator);
|
2014-12-13 17:06:01 +00:00
|
|
|
RAnalVar *var;
|
|
|
|
RListIter *iter;
|
2017-03-09 22:46:02 +00:00
|
|
|
if (mode == 'j') {
|
2015-08-08 18:15:13 +00:00
|
|
|
anal->cb_printf ("[");
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2014-12-13 17:06:01 +00:00
|
|
|
r_list_foreach (list, iter, var) {
|
2016-05-24 00:39:39 +00:00
|
|
|
if (var->kind != kind) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (mode) {
|
|
|
|
case '*':
|
|
|
|
// we cant express all type info here :(
|
2017-03-09 22:46:02 +00:00
|
|
|
if (kind == R_ANAL_VAR_KIND_REG) { // registers
|
2016-06-05 22:33:42 +00:00
|
|
|
RRegItem *i = r_reg_index_get (anal->reg, var->delta);
|
|
|
|
if (!i) {
|
|
|
|
eprintf ("Register not found");
|
|
|
|
break;
|
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
anal->cb_printf ("afv%c %s %s %s @ 0x%"PFMT64x "\n",
|
2016-06-05 22:33:42 +00:00
|
|
|
kind, i->name, var->name, var->type, fcn->addr);
|
|
|
|
} else {
|
2017-03-09 22:46:02 +00:00
|
|
|
anal->cb_printf ("afv%c %d %s %s @ 0x%"PFMT64x "\n",
|
2016-06-05 22:33:42 +00:00
|
|
|
kind, var->delta, var->name, var->type,
|
|
|
|
fcn->addr);
|
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
switch (var->kind) {
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_BPV:
|
2016-06-05 22:33:42 +00:00
|
|
|
if (var->delta > 0) {
|
|
|
|
anal->cb_printf ("{\"name\":\"%s\","
|
2016-07-01 14:15:29 +00:00
|
|
|
"\"kind\":\"arg\",\"type\":\"%s\",\"ref\":"
|
2017-03-09 22:46:02 +00:00
|
|
|
"{\"base\":\"%s\", \"offset\":%"PFMT64d "}}",
|
|
|
|
var->name, var->type, anal->reg->name[R_REG_NAME_BP],
|
2016-06-05 22:33:42 +00:00
|
|
|
var->delta);
|
|
|
|
} else {
|
|
|
|
anal->cb_printf ("{\"name\":\"%s\","
|
2016-07-01 14:15:29 +00:00
|
|
|
"\"kind\":\"var\",\"type\":\"%s\",\"ref\":"
|
2017-03-09 22:46:02 +00:00
|
|
|
"{\"base\":\"%s\", \"offset\":-%"PFMT64d "}}",
|
|
|
|
var->name, var->type, anal->reg->name[R_REG_NAME_BP],
|
2016-06-05 22:33:42 +00:00
|
|
|
-var->delta);
|
|
|
|
}
|
2015-05-31 21:46:32 +00:00
|
|
|
break;
|
2017-03-09 22:46:02 +00:00
|
|
|
case R_ANAL_VAR_KIND_REG: {
|
2016-06-05 22:33:42 +00:00
|
|
|
RRegItem *i = r_reg_index_get (anal->reg, var->delta);
|
|
|
|
if (!i) {
|
|
|
|
eprintf ("Register not found");
|
|
|
|
break;
|
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
anal->cb_printf ("{\"name\":\"%s\","
|
2016-06-05 22:33:42 +00:00
|
|
|
"\"kind\":\"reg\",\"type\":\"%s\",\"ref\":\"%s\"}",
|
|
|
|
var->name, var->type, i->name, anal->reg->name[var->delta]);
|
2017-03-09 22:46:02 +00:00
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
break;
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_SPV:
|
2017-03-09 12:23:41 +00:00
|
|
|
if (var->delta < fcn->maxstack) {
|
2016-05-24 00:39:39 +00:00
|
|
|
anal->cb_printf ("{\"name\":\"%s\","
|
2016-07-01 14:15:29 +00:00
|
|
|
"\"kind\":\"arg\",\"type\":\"%s\",\"ref\":"
|
2017-03-09 22:46:02 +00:00
|
|
|
"{\"base\":\"%s\", \"offset\":%"PFMT64d "}}",
|
|
|
|
var->name, var->type, anal->reg->name[R_REG_NAME_SP],
|
2016-06-05 22:33:42 +00:00
|
|
|
var->delta);
|
2016-05-24 00:39:39 +00:00
|
|
|
} else {
|
2016-05-01 12:48:04 +00:00
|
|
|
anal->cb_printf ("{\"name\":\"%s\","
|
2016-07-01 14:15:29 +00:00
|
|
|
"\"kind\":\"var\",\"type\":\"%s\",\"ref\":"
|
2017-03-09 22:46:02 +00:00
|
|
|
"{\"base\":\"%s\", \"offset\":-%"PFMT64d "}}",
|
|
|
|
var->name, var->type, anal->reg->name[R_REG_NAME_SP],
|
2016-05-01 12:48:04 +00:00
|
|
|
var->delta);
|
2016-04-11 17:26:58 +00:00
|
|
|
}
|
2015-05-31 21:46:32 +00:00
|
|
|
break;
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2017-03-09 22:46:02 +00:00
|
|
|
if (iter->n) {
|
2016-05-24 00:39:39 +00:00
|
|
|
anal->cb_printf (",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (kind) {
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_BPV:
|
2016-06-05 22:33:42 +00:00
|
|
|
if (var->delta > 0) {
|
2016-04-10 18:59:11 +00:00
|
|
|
anal->cb_printf ("arg %s %s @ %s+0x%x\n",
|
|
|
|
var->type, var->name,
|
|
|
|
anal->reg->name[R_REG_NAME_BP],
|
|
|
|
var->delta);
|
2016-06-05 22:33:42 +00:00
|
|
|
} else {
|
|
|
|
anal->cb_printf ("var %s %s @ %s-0x%x\n",
|
|
|
|
var->type, var->name,
|
|
|
|
anal->reg->name[R_REG_NAME_BP],
|
|
|
|
-var->delta);
|
|
|
|
}
|
|
|
|
break;
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_REG: {
|
2016-06-05 22:33:42 +00:00
|
|
|
RRegItem *i = r_reg_index_get (anal->reg, var->delta);
|
|
|
|
if (!i) {
|
2017-03-09 22:46:02 +00:00
|
|
|
eprintf ("Register not found");
|
2016-06-05 22:33:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
anal->cb_printf ("reg %s %s @ %s\n",
|
|
|
|
var->type, var->name, i->name);
|
2017-08-09 17:21:53 +00:00
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
break;
|
2016-08-08 22:17:42 +00:00
|
|
|
case R_ANAL_VAR_KIND_SPV:
|
2017-03-09 12:23:41 +00:00
|
|
|
if (var->delta < fcn->maxstack) {
|
2016-06-05 22:33:42 +00:00
|
|
|
anal->cb_printf ("var %s %s @ %s+0x%x\n",
|
|
|
|
var->type, var->name,
|
|
|
|
anal->reg->name[R_REG_NAME_SP],
|
|
|
|
var->delta);
|
|
|
|
} else {
|
|
|
|
anal->cb_printf ("arg %s %s @ %s+0x%x\n",
|
|
|
|
var->type, var->name,
|
|
|
|
anal->reg->name[R_REG_NAME_SP],
|
|
|
|
var->delta);
|
|
|
|
|
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
break;
|
2015-05-31 21:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 17:06:01 +00:00
|
|
|
}
|
2016-05-24 00:39:39 +00:00
|
|
|
if (mode == 'j') {
|
2015-08-08 18:15:13 +00:00
|
|
|
anal->cb_printf ("]\n");
|
2016-05-24 00:39:39 +00:00
|
|
|
}
|
2015-01-14 01:59:05 +00:00
|
|
|
r_list_free (list);
|
2010-08-22 16:41:57 +00:00
|
|
|
}
|