2015-05-21 00:26:54 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2015 - pancake */
|
2009-09-18 18:11:42 +00:00
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <r_reg.h>
|
2009-09-18 18:11:42 +00:00
|
|
|
#include <r_util.h>
|
2009-09-19 19:54:22 +00:00
|
|
|
#include <list.h>
|
2009-09-18 18:11:42 +00:00
|
|
|
|
2014-01-08 22:23:06 +00:00
|
|
|
R_LIB_VERSION (r_reg);
|
2013-06-14 00:51:33 +00:00
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
static const char *types[R_REG_TYPE_LAST + 1] = {
|
|
|
|
"gpr", "drx", "fpu", "mmx", "xmm", "flg", "seg", NULL};
|
2010-01-19 10:25:17 +00:00
|
|
|
|
2010-02-18 15:36:55 +00:00
|
|
|
R_API const char *r_reg_get_type(int idx) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (idx >= 0 && idx < R_REG_TYPE_LAST)
|
2010-02-18 15:36:55 +00:00
|
|
|
return types[idx];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-23 01:58:49 +00:00
|
|
|
R_API int r_reg_type_by_name(const char *str) {
|
|
|
|
int i;
|
2015-12-08 22:55:23 +00:00
|
|
|
for (i = 0; i < R_REG_TYPE_LAST && types[i]; i++)
|
2015-08-23 01:58:49 +00:00
|
|
|
if (!strcmp (types[i], str))
|
|
|
|
return i;
|
|
|
|
if (!strcmp (str, "all"))
|
|
|
|
return R_REG_TYPE_ALL;
|
|
|
|
eprintf ("Unknown register type: '%s'\n", str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_reg_item_free(RRegItem *item) {
|
2010-09-18 00:51:17 +00:00
|
|
|
free (item->name);
|
2010-09-24 02:09:39 +00:00
|
|
|
free (item->flags);
|
2010-09-18 00:51:17 +00:00
|
|
|
free (item);
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:31 +00:00
|
|
|
R_API int r_reg_get_name_idx(const char *type) {
|
2015-10-31 00:57:52 +00:00
|
|
|
if (!type || !*type) return -1;
|
2015-12-08 22:55:23 +00:00
|
|
|
switch (*type | (type[1] << 8)) {
|
2015-10-31 00:57:52 +00:00
|
|
|
/* flags */
|
2015-12-08 22:55:23 +00:00
|
|
|
case 'Z' + ('F' << 8): return R_REG_NAME_ZF;
|
|
|
|
case 'S' + ('F' << 8): return R_REG_NAME_SF;
|
|
|
|
case 'C' + ('F' << 8): return R_REG_NAME_CF;
|
|
|
|
case 'O' + ('F' << 8):
|
|
|
|
return R_REG_NAME_OF;
|
2015-10-31 00:57:52 +00:00
|
|
|
/* gpr */
|
2015-12-08 22:55:23 +00:00
|
|
|
case 'P' + ('C' << 8): return R_REG_NAME_PC;
|
|
|
|
case 'S' + ('R' << 8): return R_REG_NAME_SR;
|
2015-12-31 13:10:11 +00:00
|
|
|
case 'L' + ('R' << 8): return R_REG_NAME_LR;
|
2015-12-08 22:55:23 +00:00
|
|
|
case 'S' + ('P' << 8): return R_REG_NAME_SP;
|
|
|
|
case 'B' + ('P' << 8): return R_REG_NAME_BP;
|
|
|
|
case 'S' + ('N' << 8):
|
|
|
|
return R_REG_NAME_SN;
|
2015-10-31 00:57:52 +00:00
|
|
|
/* args */
|
2015-12-08 22:55:23 +00:00
|
|
|
case 'A' + ('0' << 8): return R_REG_NAME_A0;
|
|
|
|
case 'A' + ('1' << 8): return R_REG_NAME_A1;
|
|
|
|
case 'A' + ('2' << 8): return R_REG_NAME_A2;
|
|
|
|
case 'A' + ('3' << 8): return R_REG_NAME_A3;
|
|
|
|
case 'A' + ('4' << 8): return R_REG_NAME_A4;
|
|
|
|
case 'A' + ('5' << 8): return R_REG_NAME_A5;
|
|
|
|
case 'A' + ('6' << 8):
|
|
|
|
return R_REG_NAME_A6;
|
2015-10-31 00:57:52 +00:00
|
|
|
/* return values */
|
2015-12-08 22:55:23 +00:00
|
|
|
case 'R' + ('0' << 8): return R_REG_NAME_R0;
|
|
|
|
case 'R' + ('1' << 8): return R_REG_NAME_R1;
|
|
|
|
case 'R' + ('2' << 8): return R_REG_NAME_R2;
|
|
|
|
case 'R' + ('3' << 8): return R_REG_NAME_R3;
|
2010-02-03 13:34:00 +00:00
|
|
|
}
|
2010-06-22 18:27:14 +00:00
|
|
|
return -1;
|
2010-02-03 17:15:31 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API int r_reg_set_name(RReg *reg, int role, const char *name) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (role >= 0 && role < R_REG_NAME_LAST) {
|
2010-02-03 13:34:00 +00:00
|
|
|
reg->name[role] = r_str_dup (reg->name[role], name);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2010-09-18 00:51:17 +00:00
|
|
|
}
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2010-02-03 13:34:00 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API const char *r_reg_get_name(RReg *reg, int role) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (reg && role >= 0 && role < R_REG_NAME_LAST)
|
2010-02-03 17:15:31 +00:00
|
|
|
return reg->name[role];
|
2010-02-28 21:58:21 +00:00
|
|
|
return NULL;
|
2010-02-03 17:15:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
static const char *roles[R_REG_NAME_LAST + 1] = {
|
2015-10-31 00:57:52 +00:00
|
|
|
"PC", "SP", "SR", "BP", "Ao", "A1",
|
|
|
|
"A2", "A3", "A4", "A5", "A6", "ZF",
|
2015-12-08 22:55:23 +00:00
|
|
|
"SF", "CF", "OF", "SB", NULL};
|
2015-10-31 00:57:52 +00:00
|
|
|
|
|
|
|
R_API const char *r_reg_get_role(int role) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (role >= 0 && role < R_REG_NAME_LAST)
|
2015-10-31 00:57:52 +00:00
|
|
|
return roles[role];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API void r_reg_free_internal(RReg *reg) {
|
|
|
|
int i;
|
2014-09-06 01:59:28 +00:00
|
|
|
|
2016-01-09 02:14:18 +00:00
|
|
|
R_FREE (reg->reg_profile_str);
|
|
|
|
R_FREE (reg->reg_profile_cmt);
|
2014-09-06 01:59:28 +00:00
|
|
|
|
|
|
|
for (i = 0; i < R_REG_NAME_LAST; i++) {
|
2014-08-24 23:18:35 +00:00
|
|
|
if (reg->name[i]) {
|
|
|
|
free (reg->name[i]);
|
|
|
|
reg->name[i] = NULL;
|
|
|
|
}
|
2014-08-25 02:27:25 +00:00
|
|
|
}
|
2015-12-08 22:55:23 +00:00
|
|
|
for (i = 0; i < R_REG_TYPE_LAST; i++) {
|
2014-08-25 02:27:25 +00:00
|
|
|
r_list_purge (reg->regset[i].regs);
|
2012-12-23 12:52:57 +00:00
|
|
|
reg->regset[i].regs = r_list_newf ((RListFree)r_reg_item_free);
|
2011-07-02 21:15:40 +00:00
|
|
|
}
|
2014-09-06 01:59:28 +00:00
|
|
|
reg->size = 0;
|
2010-09-18 00:51:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 09:43:02 +00:00
|
|
|
R_API void r_reg_free(RReg *reg) {
|
|
|
|
int i;
|
2014-09-06 01:59:28 +00:00
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
if (!reg)
|
2014-09-06 01:59:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < R_REG_TYPE_LAST; i++) {
|
2014-05-02 14:54:27 +00:00
|
|
|
r_list_purge (reg->regset[i].pool);
|
2012-03-07 09:43:02 +00:00
|
|
|
reg->regset[i].pool = NULL;
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
2012-03-07 09:43:02 +00:00
|
|
|
r_reg_free_internal (reg);
|
|
|
|
free (reg);
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API RReg *r_reg_new() {
|
2011-06-04 01:14:04 +00:00
|
|
|
RRegArena *arena;
|
2014-09-06 01:59:28 +00:00
|
|
|
RReg *reg = R_NEW0 (RReg);
|
|
|
|
int i;
|
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
for (i = 0; i < R_REG_TYPE_LAST; i++) {
|
2011-06-04 01:14:04 +00:00
|
|
|
arena = r_reg_arena_new (0);
|
2014-05-03 07:09:10 +00:00
|
|
|
if (!arena) {
|
|
|
|
free (reg);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-12-23 12:52:57 +00:00
|
|
|
reg->regset[i].pool = r_list_newf ((RListFree)r_reg_arena_free);
|
|
|
|
reg->regset[i].regs = r_list_newf ((RListFree)r_reg_item_free);
|
2013-08-29 14:59:24 +00:00
|
|
|
reg->regset[i].arena = arena;
|
2009-09-13 22:37:28 +00:00
|
|
|
}
|
2015-11-23 23:58:03 +00:00
|
|
|
r_reg_arena_push (reg);
|
|
|
|
#if 0
|
2015-01-27 11:46:15 +00:00
|
|
|
/* swap arena back and forth to avoid lost reg sets */
|
2015-09-14 00:08:31 +00:00
|
|
|
r_reg_arena_swap (reg, false);
|
|
|
|
r_reg_arena_swap (reg, false);
|
2015-11-23 23:58:03 +00:00
|
|
|
#endif
|
2009-09-13 22:37:28 +00:00
|
|
|
return reg;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2014-01-28 03:03:35 +00:00
|
|
|
R_API ut64 r_reg_setv(RReg *reg, const char *name, ut64 val) {
|
|
|
|
return r_reg_set_value (reg, r_reg_get (reg, name, -1), val);
|
|
|
|
}
|
|
|
|
|
2013-06-20 00:49:39 +00:00
|
|
|
R_API ut64 r_reg_getv(RReg *reg, const char *name) {
|
|
|
|
return r_reg_get_value (reg, r_reg_get (reg, name, -1));
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API RRegItem *r_reg_get(RReg *reg, const char *name, int type) {
|
|
|
|
RListIter *iter;
|
|
|
|
RRegItem *r;
|
2009-10-12 15:41:52 +00:00
|
|
|
int i, e;
|
2010-10-14 17:01:14 +00:00
|
|
|
if (!reg || !name)
|
2010-06-16 07:42:46 +00:00
|
|
|
return NULL;
|
2009-10-12 15:41:52 +00:00
|
|
|
if (type == -1) {
|
|
|
|
i = 0;
|
|
|
|
e = R_REG_TYPE_LAST;
|
|
|
|
} else {
|
|
|
|
i = type;
|
2015-12-08 22:55:23 +00:00
|
|
|
e = type + 1;
|
2009-10-12 15:41:52 +00:00
|
|
|
}
|
2009-09-18 18:11:42 +00:00
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
for (; i < e; i++) {
|
2010-09-18 00:51:17 +00:00
|
|
|
r_list_foreach (reg->regset[i].regs, iter, r) {
|
2014-08-08 00:11:23 +00:00
|
|
|
if (r->name && !strcmp (r->name, name)) {
|
2010-09-18 00:51:17 +00:00
|
|
|
return r;
|
2015-01-27 11:46:15 +00:00
|
|
|
}
|
2010-09-18 00:51:17 +00:00
|
|
|
}
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API RList *r_reg_get_list(RReg *reg, int type) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (type < 0 || type > (R_REG_TYPE_LAST - 1))
|
2009-09-18 18:11:42 +00:00
|
|
|
return NULL;
|
2010-09-18 00:51:17 +00:00
|
|
|
return reg->regset[type].regs;
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
2010-09-20 12:02:45 +00:00
|
|
|
|
2010-09-24 14:45:56 +00:00
|
|
|
R_API ut64 r_reg_cmp(RReg *reg, RRegItem *item) {
|
2015-10-22 11:38:30 +00:00
|
|
|
RRegArena *dst, *src;
|
2012-06-14 00:18:15 +00:00
|
|
|
ut64 ret, ret2;
|
2013-08-29 14:59:24 +00:00
|
|
|
RListIter *it;
|
2015-10-22 11:38:30 +00:00
|
|
|
int ptr = !(reg->iters % 2);
|
|
|
|
int len = (item->size / 8); // TODO: must use r_mem_bitcmp or so.. flags not correctly checked
|
2010-09-24 14:45:56 +00:00
|
|
|
int off = BITS2BYTES (item->offset);
|
2013-08-29 14:59:24 +00:00
|
|
|
it = r_list_head (reg->regset[item->type].pool);
|
|
|
|
if (!it || !it->n)
|
|
|
|
return UT64_MAX;
|
|
|
|
src = r_list_head (reg->regset[item->type].pool)->data;
|
|
|
|
dst = it->n->data;
|
2015-12-08 22:55:23 +00:00
|
|
|
if (off + len > src->size) len = src->size - off;
|
|
|
|
if (off + len > dst->size) len = dst->size - off;
|
|
|
|
if (len > 1 && memcmp (dst->bytes + off, src->bytes + off, len)) {
|
2010-09-24 14:45:56 +00:00
|
|
|
r_reg_arena_set (reg, ptr, 0);
|
|
|
|
ret = r_reg_get_value (reg, item);
|
|
|
|
r_reg_arena_set (reg, !ptr, 0);
|
2012-06-14 00:18:15 +00:00
|
|
|
ret2 = r_reg_get_value (reg, item);
|
2015-10-22 11:38:30 +00:00
|
|
|
return ret - ret2;
|
2010-09-24 14:45:56 +00:00
|
|
|
}
|
|
|
|
return 0LL;
|
2010-09-20 12:02:45 +00:00
|
|
|
}
|
2014-06-26 10:18:45 +00:00
|
|
|
|
2014-07-03 11:48:35 +00:00
|
|
|
// TODO regsize is in bits, delta in bytes, maybe we should standarize this..
|
2015-12-08 22:55:23 +00:00
|
|
|
R_API RRegItem *r_reg_get_at(RReg *reg, int type, int regsize, int delta) {
|
2014-06-26 10:18:45 +00:00
|
|
|
RList *list = r_reg_get_list (reg, type);
|
|
|
|
RRegItem *ri;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (list, iter, ri) {
|
|
|
|
if (ri->size == regsize) {
|
2015-10-22 11:38:30 +00:00
|
|
|
if (BITS2BYTES (ri->offset) == delta)
|
2014-06-26 10:18:45 +00:00
|
|
|
return ri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the next register in the current regset that differs from */
|
2015-12-08 22:55:23 +00:00
|
|
|
R_API RRegItem *r_reg_next_diff(RReg *reg, int type, const ut8 *buf, int buflen, RRegItem *prev_ri, int regsize) {
|
2014-07-03 11:48:35 +00:00
|
|
|
int bregsize = BITS2BYTES (regsize);
|
2014-06-26 10:18:45 +00:00
|
|
|
RRegArena *arena;
|
|
|
|
int delta;
|
2015-12-08 22:55:23 +00:00
|
|
|
if (type < 0 || type > (R_REG_TYPE_LAST - 1))
|
2014-06-26 10:18:45 +00:00
|
|
|
return NULL;
|
|
|
|
arena = reg->regset[type].arena;
|
2015-12-08 22:55:23 +00:00
|
|
|
delta = prev_ri? prev_ri->offset + prev_ri->size: 0;
|
2014-06-27 13:33:25 +00:00
|
|
|
for (;;) {
|
2015-12-08 22:55:23 +00:00
|
|
|
if (delta + bregsize >= arena->size || delta + bregsize >= buflen)
|
2014-06-27 13:33:25 +00:00
|
|
|
break;
|
2015-12-08 22:55:23 +00:00
|
|
|
if (memcmp (arena->bytes + delta, buf + delta, bregsize)) {
|
2014-07-03 11:48:35 +00:00
|
|
|
RRegItem *ri = r_reg_get_at (reg, type, regsize, delta);
|
2014-06-27 13:33:25 +00:00
|
|
|
if (ri) return ri;
|
|
|
|
}
|
2014-07-03 11:48:35 +00:00
|
|
|
delta += bregsize;
|
|
|
|
}
|
2014-06-26 10:18:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-15 00:24:22 +00:00
|
|
|
|
|
|
|
R_API RRegSet *r_reg_regset_get(RReg *r, int type) {
|
|
|
|
RRegSet *rs;
|
2015-12-08 22:55:23 +00:00
|
|
|
if (type < 0 || type >= R_REG_TYPE_LAST)
|
2014-10-15 00:24:22 +00:00
|
|
|
return NULL;
|
|
|
|
rs = &r->regset[type];
|
|
|
|
if (rs->arena)
|
|
|
|
return rs;
|
|
|
|
return NULL;
|
|
|
|
}
|