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-01-19 10:25:17 +00:00
|
|
|
R_API const char *r_reg_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
|
|
|
};
|
|
|
|
|
2009-09-25 02:04:51 +00:00
|
|
|
static void r_reg_free_internal(struct r_reg_t *reg) {
|
2009-09-19 22:28:06 +00:00
|
|
|
struct list_head *pos, *n;
|
|
|
|
struct r_reg_item_t *r;
|
|
|
|
int i;
|
|
|
|
|
2010-02-03 17:15:31 +00:00
|
|
|
for (i=0; i<R_REG_TYPE_LAST; i++) {
|
2010-02-03 13:34:00 +00:00
|
|
|
list_for_each_safe (pos, n, ®->regset[i].regs) {
|
|
|
|
r = list_entry (pos, struct r_reg_item_t, list);
|
|
|
|
list_del (&r->list);
|
|
|
|
free (r);
|
2009-09-19 22:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
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-02-03 13:34:00 +00:00
|
|
|
int role = type[0] + (type[1]<<8);
|
|
|
|
switch (role) {
|
|
|
|
case 'p'+('c'<<8):
|
|
|
|
role = R_REG_NAME_PC;
|
|
|
|
break;
|
|
|
|
case 's'+('p'<<8):
|
|
|
|
role = R_REG_NAME_SP;
|
|
|
|
break;
|
|
|
|
case 'b'+('p'<<8):
|
|
|
|
role = R_REG_NAME_BP;
|
|
|
|
break;
|
|
|
|
case 'a'+('0'<<8):
|
|
|
|
role = R_REG_NAME_A0;
|
|
|
|
break;
|
|
|
|
case 'a'+('1'<<8):
|
|
|
|
role = R_REG_NAME_A1;
|
|
|
|
break;
|
|
|
|
case 'a'+('2'<<8):
|
|
|
|
role = R_REG_NAME_A2;
|
|
|
|
break;
|
|
|
|
case 'a'+('3'<<8):
|
|
|
|
role = R_REG_NAME_A3;
|
|
|
|
break;
|
|
|
|
default:
|
2010-02-03 17:15:31 +00:00
|
|
|
role = -1;
|
2010-02-03 13:34:00 +00:00
|
|
|
}
|
2010-02-03 17:15:31 +00:00
|
|
|
return role;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_reg_set_name(struct r_reg_t *reg, int role, const char *name) {
|
|
|
|
int ret = R_TRUE;
|
|
|
|
// TODO: ensure this range check in a define.. somewhere
|
|
|
|
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-02-03 17:15:31 +00:00
|
|
|
} else ret = R_FALSE;
|
2010-02-03 13:34:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:31 +00:00
|
|
|
R_API const char *r_reg_get_name(struct r_reg_t *reg, int role) {
|
|
|
|
if (role>=0 && role<R_REG_NAME_LAST)
|
|
|
|
return reg->name[role];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-09-18 18:11:42 +00:00
|
|
|
R_API struct r_reg_t *r_reg_free(struct r_reg_t *reg)
|
|
|
|
{
|
|
|
|
if (reg) {
|
|
|
|
// TODO: free more things here
|
2010-01-21 01:38:52 +00:00
|
|
|
free (reg);
|
2009-09-18 18:11:42 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-13 22:37:28 +00:00
|
|
|
R_API struct r_reg_t *r_reg_init(struct r_reg_t *reg)
|
|
|
|
{
|
2009-09-19 19:54:22 +00:00
|
|
|
int i;
|
2010-02-05 11:21:37 +00:00
|
|
|
if (!reg)
|
|
|
|
return NULL;
|
|
|
|
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++) {
|
|
|
|
INIT_LIST_HEAD (®->regset[i].arenas);
|
|
|
|
INIT_LIST_HEAD (®->regset[i].regs);
|
|
|
|
if ((reg->regset[i].arena = r_reg_arena_new (0)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
list_add_tail (®->regset[i].arena->list,
|
|
|
|
®->regset[i].arenas);
|
2009-09-13 22:37:28 +00:00
|
|
|
}
|
|
|
|
return reg;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-09-25 02:04:51 +00:00
|
|
|
static struct r_reg_item_t *r_reg_item_new() {
|
2010-02-05 11:21:37 +00:00
|
|
|
struct r_reg_item_t *item = R_NEW (struct r_reg_item_t);
|
|
|
|
memset (item, 0, sizeof(RRegisterItem));
|
2009-09-19 19:54:22 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2009-09-25 02:04:51 +00:00
|
|
|
R_API int r_reg_type_by_name(const char *str)
|
2009-09-19 19:54:22 +00:00
|
|
|
{
|
2010-01-19 10:25:17 +00:00
|
|
|
int i;
|
2010-01-21 01:38:52 +00:00
|
|
|
for (i=0; r_reg_types[i] && i<R_REG_TYPE_LAST; i++) {
|
2010-01-19 10:25:17 +00:00
|
|
|
if (!strcmp (r_reg_types[i], str))
|
|
|
|
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 */
|
|
|
|
static int r_reg_set_word(struct r_reg_item_t *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;
|
|
|
|
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 */
|
|
|
|
R_API int r_reg_set_profile_string(struct r_reg_t *reg, const char *str)
|
2009-09-13 22:37:28 +00:00
|
|
|
{
|
2010-01-26 00:28:33 +00:00
|
|
|
RRegisterItem *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];
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
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-02-03 13:34:00 +00:00
|
|
|
// UGLY PASTAFARIAN TO PARSE
|
|
|
|
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
|
2009-09-19 19:54:22 +00:00
|
|
|
if (lastchar != ' ' && lastchar != '\t') {
|
2010-02-03 13:34:00 +00:00
|
|
|
r_reg_set_word (item, word, buf);
|
2010-02-03 17:15:31 +00:00
|
|
|
}
|
2010-02-03 13:34:00 +00:00
|
|
|
chidx = 0;
|
|
|
|
word++;
|
2009-09-19 19:54:22 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2010-02-03 17:15:31 +00:00
|
|
|
if (setname != -1) {
|
|
|
|
r_reg_set_name (reg, setname, buf);
|
|
|
|
} else
|
2009-09-25 02:04:51 +00:00
|
|
|
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) {
|
|
|
|
list_add_tail(&item->list, ®->regset[item->type].regs);
|
|
|
|
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:
|
|
|
|
if (chidx > 128) // WTF!!
|
|
|
|
return R_FALSE;
|
|
|
|
buf[chidx++] = *str;
|
|
|
|
buf[chidx] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastchar = *str;
|
|
|
|
str++;
|
|
|
|
}
|
2010-01-21 01:38:52 +00:00
|
|
|
free (item->name);
|
|
|
|
free (item);
|
|
|
|
r_reg_fit_arena (reg);
|
2009-09-19 19:54:22 +00:00
|
|
|
|
|
|
|
/* do we reach the end ? */
|
|
|
|
if (!*str) ret = R_TRUE;
|
2009-09-18 18:11:42 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 18:11:42 +00:00
|
|
|
R_API int r_reg_set_profile(struct r_reg_t *reg, const char *profile)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
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-02-03 13:34:00 +00:00
|
|
|
str = r_file_slurp (profile, NULL);
|
2009-09-18 18:11:42 +00:00
|
|
|
if (str == NULL) {
|
|
|
|
// 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-01-19 10:25:17 +00:00
|
|
|
file = r_str_concat (strdup(base), profile);
|
|
|
|
str = r_file_slurp (file, NULL);
|
2009-09-18 18:11:42 +00:00
|
|
|
free(file);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-18 18:11:42 +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
|
|
|
|
2009-10-12 15:41:52 +00:00
|
|
|
R_API struct r_reg_item_t *r_reg_get(struct r_reg_t *reg, const char *name, int type)
|
2009-09-18 18:11:42 +00:00
|
|
|
{
|
|
|
|
struct list_head *pos;
|
|
|
|
struct r_reg_item_t *r;
|
2009-10-12 15:41:52 +00:00
|
|
|
int i, e;
|
|
|
|
|
|
|
|
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-02-03 13:34:00 +00:00
|
|
|
for (; i<e; i++) {
|
2009-09-18 18:11:42 +00:00
|
|
|
list_for_each(pos, ®->regset[i].regs) {
|
|
|
|
r = list_entry(pos, struct r_reg_item_t, list);
|
|
|
|
if (!strcmp(r->name, name))
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API struct list_head *r_reg_get_list(struct r_reg_t *reg, int type)
|
|
|
|
{
|
|
|
|
if (type < 0 || type > R_REG_TYPE_LAST)
|
|
|
|
return NULL;
|
|
|
|
return ®->regset[type].regs;
|
|
|
|
}
|