2010-01-19 10:25:17 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
|
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
|
|
|
|
2010-02-18 15:36:55 +00:00
|
|
|
static const char *types[R_REG_TYPE_LAST+1] = {
|
2010-01-21 01:38:52 +00:00
|
|
|
"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) {
|
|
|
|
if (idx>=0 && idx<R_REG_TYPE_LAST)
|
|
|
|
return types[idx];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
static void r_reg_item_free(RRegItem *item) {
|
|
|
|
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) {
|
2010-06-22 18:27:14 +00:00
|
|
|
if (type)
|
|
|
|
switch (*type | (type[1]<<8)) {
|
|
|
|
case 'p'+('c'<<8): return R_REG_NAME_PC;
|
|
|
|
case 's'+('r'<<8): return R_REG_NAME_SR;
|
|
|
|
case 's'+('p'<<8): return R_REG_NAME_SP;
|
|
|
|
case 'b'+('p'<<8): return R_REG_NAME_BP;
|
|
|
|
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;
|
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) {
|
|
|
|
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);
|
2010-09-18 00:51:17 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
return R_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) {
|
2010-06-16 07:42:46 +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
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API void r_reg_free_internal(RReg *reg) {
|
|
|
|
int i;
|
2010-09-23 18:42:35 +00:00
|
|
|
for (i=0; i<R_REG_TYPE_LAST; i++)
|
2010-09-18 00:51:17 +00:00
|
|
|
r_list_destroy (reg->regset[i].regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RReg *r_reg_free(RReg *reg) {
|
2009-09-18 18:11:42 +00:00
|
|
|
if (reg) {
|
2010-09-23 18:42:35 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<R_REG_TYPE_LAST; i++)
|
|
|
|
r_list_destroy (reg->regset[i].pool);
|
2010-09-18 00:51:17 +00:00
|
|
|
r_reg_free_internal (reg);
|
2010-01-21 01:38:52 +00:00
|
|
|
free (reg);
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API RReg *r_reg_new() {
|
|
|
|
RReg *reg = R_NEW (RReg);
|
2009-09-19 19:54:22 +00:00
|
|
|
int i;
|
2010-09-23 18:42:35 +00:00
|
|
|
reg->iters = 0;
|
2010-02-05 11:21:37 +00:00
|
|
|
reg->profile = NULL;
|
|
|
|
for (i=0; i<R_REG_NAME_LAST; i++)
|
|
|
|
reg->name[i] = NULL;
|
|
|
|
for (i=0; i<R_REG_TYPE_LAST; i++) {
|
2010-09-18 00:51:17 +00:00
|
|
|
reg->regset[i].pool = r_list_new ();
|
|
|
|
reg->regset[i].pool->free = (RListFree)r_reg_arena_free;
|
|
|
|
reg->regset[i].regs = r_list_new ();
|
|
|
|
reg->regset[i].regs->free = (RListFree)r_reg_item_free;
|
|
|
|
if (!(reg->regset[i].arena = r_reg_arena_new (0)))
|
2010-02-05 11:21:37 +00:00
|
|
|
return NULL;
|
2010-09-18 00:51:17 +00:00
|
|
|
r_list_append (reg->regset[i].pool, reg->regset[i].arena);
|
2009-09-13 22:37:28 +00:00
|
|
|
}
|
|
|
|
return reg;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
static RRegItem *r_reg_item_new() {
|
|
|
|
RRegItem *item = R_NEW (RRegItem);
|
|
|
|
memset (item, 0, sizeof (RRegItem));
|
2009-09-19 19:54:22 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2010-02-28 21:58:21 +00:00
|
|
|
R_API int r_reg_type_by_name(const char *str) {
|
2010-01-19 10:25:17 +00:00
|
|
|
int i;
|
2010-06-16 07:42:46 +00:00
|
|
|
for (i=0; types[i] && i<R_REG_TYPE_LAST; i++)
|
2010-02-18 15:36:55 +00:00
|
|
|
if (!strcmp (types[i], str))
|
2010-01-19 10:25:17 +00:00
|
|
|
return i;
|
|
|
|
if (!strcmp (str, "all"))
|
|
|
|
return R_REG_TYPE_ALL;
|
|
|
|
eprintf ("Unknown register type: '%s'\n", str);
|
|
|
|
return R_REG_TYPE_LAST;
|
2009-09-25 02:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: make this parser better and cleaner */
|
2010-09-18 00:51:17 +00:00
|
|
|
static int r_reg_set_word(RRegItem *item, int idx, char *word) {
|
2009-09-19 19:54:22 +00:00
|
|
|
int ret = R_TRUE;
|
|
|
|
switch(idx) {
|
|
|
|
case 0:
|
2010-02-03 13:34:00 +00:00
|
|
|
item->type = r_reg_type_by_name (word);
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2010-02-03 13:34:00 +00:00
|
|
|
item->name = strdup (word);
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
/* spaguetti ftw!!1 */
|
|
|
|
case 2:
|
|
|
|
if (*word=='.') // XXX; this is kinda ugly
|
2010-02-03 13:34:00 +00:00
|
|
|
item->size = atoi (word+1);
|
|
|
|
else item->size = atoi (word)*8;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (*word=='.') // XXX; this is kinda ugly
|
2010-02-03 13:34:00 +00:00
|
|
|
item->offset = atoi (word+1);
|
|
|
|
else item->offset = atoi (word)*8;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (*word=='.') // XXX; this is kinda ugly
|
2010-02-03 13:34:00 +00:00
|
|
|
item->packed_size = atoi (word+1);
|
|
|
|
else item->packed_size = atoi (word)*8;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
2010-09-24 02:09:39 +00:00
|
|
|
case 5:
|
|
|
|
item->flags = strdup (word);
|
|
|
|
break;
|
2009-09-19 19:54:22 +00:00
|
|
|
default:
|
2010-02-03 17:15:31 +00:00
|
|
|
eprintf ("register set fail (%s)\n", word);
|
2009-09-19 19:54:22 +00:00
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: make this parser better and cleaner */
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API int r_reg_set_profile_string(RReg *reg, const char *str) {
|
|
|
|
RRegItem *item;
|
2010-02-03 17:15:31 +00:00
|
|
|
int setname = -1;
|
2009-09-18 18:11:42 +00:00
|
|
|
int ret = R_FALSE;
|
2009-09-19 19:54:22 +00:00
|
|
|
int lastchar = 0;
|
|
|
|
int chidx = 0;
|
2010-01-21 01:38:52 +00:00
|
|
|
int word = 0;
|
2009-09-19 19:54:22 +00:00
|
|
|
char buf[256];
|
|
|
|
|
2010-06-16 07:42:46 +00:00
|
|
|
if (!str||!reg)
|
2009-09-19 19:54:22 +00:00
|
|
|
return R_FALSE;
|
2010-02-03 13:34:00 +00:00
|
|
|
buf[0] = '\0';
|
2009-09-19 19:54:22 +00:00
|
|
|
/* format file is: 'type name size offset packedsize' */
|
2010-02-03 13:34:00 +00:00
|
|
|
r_reg_free_internal (reg);
|
|
|
|
item = r_reg_item_new ();
|
2009-09-19 19:54:22 +00:00
|
|
|
|
2010-02-03 13:34:00 +00:00
|
|
|
while (*str) {
|
2009-09-19 19:54:22 +00:00
|
|
|
if (*str == '#') {
|
|
|
|
/* skip until newline */
|
2010-02-03 13:34:00 +00:00
|
|
|
while (*str && *str != '\n') str++;
|
2009-09-19 19:54:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-03 13:34:00 +00:00
|
|
|
switch (*str) {
|
2009-09-19 19:54:22 +00:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2010-09-18 00:51:17 +00:00
|
|
|
/* UGLY PASTAFARIAN PARSING */
|
2010-02-03 13:34:00 +00:00
|
|
|
if (word==0 && *buf=='=') {
|
2010-02-03 17:15:31 +00:00
|
|
|
setname = r_reg_get_name_idx (buf+1);
|
|
|
|
if (setname == -1)
|
|
|
|
eprintf ("Invalid register type: '%s'\n", buf+1);
|
2010-02-03 13:34:00 +00:00
|
|
|
} else
|
2010-06-16 07:42:46 +00:00
|
|
|
if (lastchar != ' ' && lastchar != '\t')
|
2010-02-03 13:34:00 +00:00
|
|
|
r_reg_set_word (item, word, buf);
|
|
|
|
chidx = 0;
|
|
|
|
word++;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2010-06-16 07:42:46 +00:00
|
|
|
if (setname != -1)
|
2010-02-03 17:15:31 +00:00
|
|
|
r_reg_set_name (reg, setname, buf);
|
2010-06-16 07:42:46 +00:00
|
|
|
else if (word>3) {
|
2010-02-03 13:34:00 +00:00
|
|
|
r_reg_set_word (item, word, buf);
|
2009-09-25 02:04:51 +00:00
|
|
|
if (item->name != NULL) {
|
2010-09-18 00:51:17 +00:00
|
|
|
r_list_append (reg->regset[item->type].regs, item);
|
2009-09-25 02:04:51 +00:00
|
|
|
item = r_reg_item_new();
|
|
|
|
}
|
2009-09-19 19:54:22 +00:00
|
|
|
}
|
|
|
|
chidx = word = 0;
|
2010-02-03 17:15:31 +00:00
|
|
|
setname = -1;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-06-16 07:42:46 +00:00
|
|
|
if (chidx>128) // WTF!!
|
2009-09-19 19:54:22 +00:00
|
|
|
return R_FALSE;
|
|
|
|
buf[chidx++] = *str;
|
|
|
|
buf[chidx] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastchar = *str;
|
|
|
|
str++;
|
|
|
|
}
|
2010-09-18 00:51:17 +00:00
|
|
|
r_reg_item_free (item);
|
2010-01-21 01:38:52 +00:00
|
|
|
r_reg_fit_arena (reg);
|
2010-09-18 00:51:17 +00:00
|
|
|
|
|
|
|
return *str?ret:R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
R_API int r_reg_set_profile(RReg *reg, const char *profile) {
|
2009-09-13 22:37:28 +00:00
|
|
|
int ret = R_FALSE;
|
2009-09-18 18:11:42 +00:00
|
|
|
const char *base;
|
2010-01-19 10:25:17 +00:00
|
|
|
char *str, *file;
|
2009-09-18 18:11:42 +00:00
|
|
|
/* TODO: append .regs extension to filename */
|
2010-06-16 07:42:46 +00:00
|
|
|
if ((str = r_file_slurp (profile, NULL))==NULL) {
|
2009-09-18 18:11:42 +00:00
|
|
|
// XXX we must define this varname in r_lib.h /compiletime/
|
2010-01-19 10:25:17 +00:00
|
|
|
base = r_sys_getenv ("LIBR_PLUGINS");
|
2009-09-18 18:11:42 +00:00
|
|
|
if (base) {
|
2010-06-16 07:42:46 +00:00
|
|
|
file = r_str_concat (strdup (base), profile);
|
2010-01-19 10:25:17 +00:00
|
|
|
str = r_file_slurp (file, NULL);
|
2010-06-16 07:42:46 +00:00
|
|
|
free (file);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-16 07:42:46 +00:00
|
|
|
if (str) ret = r_reg_set_profile_string (reg, str);
|
2010-01-19 10:25:17 +00:00
|
|
|
else eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile);
|
2009-02-16 23:09:40 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-09-18 18:11:42 +00:00
|
|
|
|
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;
|
|
|
|
e = type+1;
|
|
|
|
}
|
2009-09-18 18:11:42 +00:00
|
|
|
|
2010-09-18 00:51:17 +00:00
|
|
|
for (; i<e; i++) {
|
|
|
|
r_list_foreach (reg->regset[i].regs, iter, r) {
|
|
|
|
if (!strcmp (r->name, name))
|
|
|
|
return r;
|
|
|
|
}
|
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) {
|
2010-06-16 07:42:46 +00:00
|
|
|
if (type<0 || type>R_REG_TYPE_LAST)
|
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) {
|
2010-09-20 12:02:45 +00:00
|
|
|
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);
|
2010-09-23 18:42:35 +00:00
|
|
|
RRegArena *src = r_list_head (reg->regset[item->type].pool)->data;
|
|
|
|
RRegArena *dst = r_list_head (reg->regset[item->type].pool)->n->data;
|
2010-09-24 14:45:56 +00:00
|
|
|
if (memcmp (dst->bytes+off, src->bytes+off, len)) {
|
|
|
|
ut64 ret;
|
|
|
|
int ptr = !(reg->iters%2);
|
|
|
|
r_reg_arena_set (reg, ptr, 0);
|
|
|
|
ret = r_reg_get_value (reg, item);
|
|
|
|
r_reg_arena_set (reg, !ptr, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0LL;
|
2010-09-20 12:02:45 +00:00
|
|
|
}
|
|
|
|
|