2019-05-14 01:31:04 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2019 - pancake */
|
2015-08-23 01:58:49 +00:00
|
|
|
|
|
|
|
#include <r_reg.h>
|
|
|
|
#include <r_util.h>
|
2016-04-05 17:51:24 +00:00
|
|
|
#include <r_lib.h>
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
static const char *parse_alias(RReg *reg, char **tok, const int n) {
|
2016-08-05 11:25:20 +00:00
|
|
|
if (n == 2) {
|
|
|
|
int role = r_reg_get_name_idx (tok[0] + 1);
|
|
|
|
return r_reg_set_name (reg, role, tok[1])
|
2018-10-15 22:15:05 +00:00
|
|
|
? NULL
|
|
|
|
: "Invalid alias";
|
2016-08-05 11:25:20 +00:00
|
|
|
}
|
|
|
|
return "Invalid syntax";
|
2015-08-23 01:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sizes prepended with a dot are expressed in bits
|
|
|
|
// strtoul with base 0 allows the input to be in decimal/octal/hex format
|
2016-01-11 01:35:57 +00:00
|
|
|
|
|
|
|
static ut64 parse_size(char *s, char **end) {
|
|
|
|
if (*s == '.') {
|
2017-08-29 23:17:39 +00:00
|
|
|
return strtoul (s + 1, end, 10);
|
2016-01-11 01:35:57 +00:00
|
|
|
}
|
2017-08-29 23:17:39 +00:00
|
|
|
char *has_dot = strchr (s, '.');
|
|
|
|
if (has_dot) {
|
|
|
|
*has_dot++ = 0;
|
|
|
|
ut64 a = strtoul (s, end, 0) << 3;
|
|
|
|
ut64 b = strtoul (has_dot, end, 0);
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
return strtoul (s, end, 0) << 3;
|
2016-01-11 01:35:57 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2015-12-08 22:55:23 +00:00
|
|
|
static const char *parse_def(RReg *reg, char **tok, const int n) {
|
2018-09-09 00:41:47 +00:00
|
|
|
char *end;
|
2016-11-12 22:19:03 +00:00
|
|
|
int type, type2;
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2016-11-12 22:19:03 +00:00
|
|
|
if (n != 5 && n != 6) {
|
|
|
|
return "Invalid syntax: Wrong number of columns";
|
|
|
|
}
|
2018-09-09 00:41:47 +00:00
|
|
|
char *p = strchr (tok[0], '@');
|
2016-11-12 22:19:03 +00:00
|
|
|
if (p) {
|
|
|
|
char *tok0 = strdup (tok[0]);
|
|
|
|
char *at = tok0 + (p - tok[0]);
|
|
|
|
*at++ = 0;
|
|
|
|
type = r_reg_type_by_name (tok0);
|
|
|
|
type2 = r_reg_type_by_name (at);
|
2016-11-21 22:13:59 +00:00
|
|
|
free (tok0);
|
2016-11-12 22:19:03 +00:00
|
|
|
} else {
|
|
|
|
type2 = type = r_reg_type_by_name (tok[0]);
|
2017-09-15 09:32:29 +00:00
|
|
|
/* Hack to put flags in the same arena as gpr */
|
2016-11-15 15:20:10 +00:00
|
|
|
if (type == R_REG_TYPE_FLG) {
|
|
|
|
type2 = R_REG_TYPE_GPR;
|
|
|
|
}
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
|
|
|
if (type < 0 || type2 < 0) {
|
2015-08-23 01:58:49 +00:00
|
|
|
return "Invalid register type";
|
2016-08-14 17:12:52 +00:00
|
|
|
}
|
2019-05-14 01:31:04 +00:00
|
|
|
#if 1
|
2017-09-15 09:32:29 +00:00
|
|
|
if (r_reg_get (reg, tok[1], R_REG_TYPE_ALL)) {
|
2018-09-09 00:53:25 +00:00
|
|
|
eprintf ("Ignoring duplicated register definition '%s'\n", tok[1]);
|
|
|
|
return NULL;
|
|
|
|
//return "Duplicate register definition";
|
2017-09-15 09:32:29 +00:00
|
|
|
}
|
2019-05-14 01:31:04 +00:00
|
|
|
#endif
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2018-09-09 00:41:47 +00:00
|
|
|
RRegItem *item = R_NEW0 (RRegItem);
|
2016-11-12 22:19:03 +00:00
|
|
|
if (!item) {
|
|
|
|
return "Unable to allocate memory";
|
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
|
|
|
|
item->type = type;
|
|
|
|
item->name = strdup (tok[1]);
|
|
|
|
// All the numeric arguments are strictly checked
|
2016-01-11 01:35:57 +00:00
|
|
|
item->size = parse_size (tok[2], &end);
|
2015-08-23 01:58:49 +00:00
|
|
|
if (*end != '\0' || !item->size) {
|
|
|
|
r_reg_item_free (item);
|
|
|
|
return "Invalid size";
|
|
|
|
}
|
2018-08-31 01:05:41 +00:00
|
|
|
if (!strcmp (tok[3], "?")) {
|
|
|
|
item->offset = -1;
|
|
|
|
} else {
|
|
|
|
item->offset = parse_size (tok[3], &end);
|
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
if (*end != '\0') {
|
|
|
|
r_reg_item_free (item);
|
|
|
|
return "Invalid offset";
|
|
|
|
}
|
2016-01-11 01:35:57 +00:00
|
|
|
item->packed_size = parse_size (tok[4], &end);
|
2015-08-23 01:58:49 +00:00
|
|
|
if (*end != '\0') {
|
|
|
|
r_reg_item_free (item);
|
|
|
|
return "Invalid packed size";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamically update the list of supported bit sizes
|
|
|
|
reg->bits |= item->size;
|
|
|
|
|
|
|
|
// This is optional
|
2016-11-12 22:19:03 +00:00
|
|
|
if (n == 6) {
|
2015-08-23 01:58:49 +00:00
|
|
|
item->flags = strdup (tok[5]);
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2017-09-15 09:32:29 +00:00
|
|
|
item->arena = type2;
|
2018-09-09 00:41:47 +00:00
|
|
|
if (!reg->regset[type2].regs) {
|
|
|
|
reg->regset[type2].regs = r_list_newf ((RListFree)r_reg_item_free);
|
|
|
|
}
|
2016-11-14 22:58:29 +00:00
|
|
|
r_list_append (reg->regset[type2].regs, item);
|
2019-05-14 01:31:04 +00:00
|
|
|
if (!reg->regset[type2].ht_regs) {
|
|
|
|
reg->regset[type2].ht_regs = ht_pp_new0 ();
|
|
|
|
}
|
|
|
|
ht_pp_insert (reg->regset[type2].ht_regs, item->name, item);
|
2015-08-23 01:58:49 +00:00
|
|
|
|
|
|
|
// Update the overall profile size
|
2016-08-14 17:12:52 +00:00
|
|
|
if (item->offset + item->size > reg->size) {
|
2015-08-23 01:58:49 +00:00
|
|
|
reg->size = item->offset + item->size;
|
2016-08-14 17:12:52 +00:00
|
|
|
}
|
2016-11-14 22:58:29 +00:00
|
|
|
// Update the overall type of registers into a regset
|
|
|
|
reg->regset[type2].maskregstype |= ((int)1 << type);
|
2015-08-23 01:58:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PARSER_MAX_TOKENS 8
|
|
|
|
R_API int r_reg_set_profile_string(RReg *reg, const char *str) {
|
|
|
|
char *tok[PARSER_MAX_TOKENS];
|
|
|
|
char tmp[128];
|
|
|
|
int i, j, l;
|
|
|
|
const char *p = str;
|
|
|
|
|
2016-11-12 22:19:03 +00:00
|
|
|
if (!reg || !str) {
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
|
|
|
|
// Same profile, no need to change
|
2016-11-12 22:19:03 +00:00
|
|
|
if (reg->reg_profile_str && !strcmp (reg->reg_profile_str, str)) {
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
|
2015-11-25 14:20:25 +00:00
|
|
|
// we should reset all the arenas before setting the new reg profile
|
|
|
|
r_reg_arena_pop (reg);
|
2015-08-23 01:58:49 +00:00
|
|
|
// Purge the old registers
|
2016-04-03 23:59:30 +00:00
|
|
|
r_reg_free_internal (reg, true);
|
2016-11-23 23:29:34 +00:00
|
|
|
r_reg_arena_shrink (reg);
|
2015-08-23 01:58:49 +00:00
|
|
|
|
|
|
|
// Cache the profile string
|
|
|
|
reg->reg_profile_str = strdup (str);
|
|
|
|
|
|
|
|
// Line number
|
|
|
|
l = 0;
|
|
|
|
// For every line
|
|
|
|
do {
|
|
|
|
// Increment line number
|
|
|
|
l++;
|
|
|
|
// Skip comment lines
|
|
|
|
if (*p == '#') {
|
2016-01-09 02:14:18 +00:00
|
|
|
const char *q = p;
|
2016-11-12 22:19:03 +00:00
|
|
|
while (*q != '\n') {
|
2016-01-09 02:14:18 +00:00
|
|
|
q++;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2017-03-16 21:29:49 +00:00
|
|
|
reg->reg_profile_cmt = r_str_appendlen (
|
2016-01-09 02:14:18 +00:00
|
|
|
reg->reg_profile_cmt, p, (int)(q - p) + 1);
|
|
|
|
p = q;
|
2015-08-23 01:58:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
j = 0;
|
|
|
|
// For every word
|
|
|
|
while (*p) {
|
|
|
|
// Skip the whitespace
|
2016-11-12 22:19:03 +00:00
|
|
|
while (*p == ' ' || *p == '\t') {
|
2015-08-23 01:58:49 +00:00
|
|
|
p++;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
// Skip the rest of the line is a comment is encountered
|
2016-08-14 17:12:52 +00:00
|
|
|
if (*p == '#') {
|
2016-11-12 22:19:03 +00:00
|
|
|
while (*p != '\n') {
|
2015-08-23 01:58:49 +00:00
|
|
|
p++;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2016-08-14 17:12:52 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
// EOL ?
|
2016-11-12 22:19:03 +00:00
|
|
|
if (*p == '\n') {
|
2015-08-23 01:58:49 +00:00
|
|
|
break;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
// Gather a handful of chars
|
|
|
|
// Use isgraph instead of isprint because the latter considers ' ' printable
|
2016-08-14 17:12:52 +00:00
|
|
|
for (i = 0; isgraph ((const unsigned char)*p) && i < sizeof (tmp) - 1;) {
|
2015-08-23 01:58:49 +00:00
|
|
|
tmp[i++] = *p++;
|
2016-08-14 17:12:52 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
tmp[i] = '\0';
|
2015-12-08 22:55:23 +00:00
|
|
|
// Limit the number of tokens
|
2016-11-12 22:19:03 +00:00
|
|
|
if (j > PARSER_MAX_TOKENS - 1) {
|
2015-08-23 01:58:49 +00:00
|
|
|
break;
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
// Save the token
|
|
|
|
tok[j++] = strdup (tmp);
|
|
|
|
}
|
|
|
|
// Empty line, eww
|
|
|
|
if (j) {
|
2015-12-08 22:55:23 +00:00
|
|
|
// Do the actual parsing
|
2015-08-23 01:58:49 +00:00
|
|
|
char *first = tok[0];
|
|
|
|
// Check whether it's defining an alias or a register
|
2016-08-14 17:12:52 +00:00
|
|
|
const char *r = (*first == '=')
|
|
|
|
? parse_alias (reg, tok, j)
|
|
|
|
: parse_def (reg, tok, j);
|
2015-08-23 01:58:49 +00:00
|
|
|
// Clean up
|
2016-11-12 22:19:03 +00:00
|
|
|
for (i = 0; i < j; i++) {
|
2015-12-08 22:55:23 +00:00
|
|
|
free (tok[i]);
|
2016-11-12 22:19:03 +00:00
|
|
|
}
|
2015-08-23 01:58:49 +00:00
|
|
|
// Warn the user if something went wrong
|
|
|
|
if (r) {
|
2015-12-08 22:55:23 +00:00
|
|
|
eprintf ("%s: Parse error @ line %d (%s)\n",
|
2015-08-23 01:58:49 +00:00
|
|
|
__FUNCTION__, l, r);
|
2015-11-15 01:20:57 +00:00
|
|
|
//eprintf ("(%s)\n", str);
|
2015-08-23 01:58:49 +00:00
|
|
|
// Clean up
|
2016-04-03 23:59:30 +00:00
|
|
|
r_reg_free_internal (reg, false);
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2015-08-23 01:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (*p++);
|
2016-11-21 22:13:59 +00:00
|
|
|
reg->size = 0;
|
|
|
|
for (i = 0; i < R_REG_TYPE_LAST; i++) {
|
|
|
|
RRegSet *rs = ®->regset[i];
|
|
|
|
//eprintf ("* arena %s size %d\n", r_reg_get_type (i), rs->arena->size);
|
|
|
|
reg->size += rs->arena->size;
|
2016-08-14 17:12:52 +00:00
|
|
|
}
|
2016-11-21 22:13:59 +00:00
|
|
|
// Align to byte boundary if needed
|
|
|
|
//if (reg->size & 7) {
|
|
|
|
// reg->size += 8 - (reg->size & 7);
|
|
|
|
//}
|
|
|
|
//reg->size >>= 3; // bits to bytes (divide by 8)
|
2015-08-23 01:58:49 +00:00
|
|
|
r_reg_fit_arena (reg);
|
2015-11-25 14:20:25 +00:00
|
|
|
// dup the last arena to allow regdiffing
|
|
|
|
r_reg_arena_push (reg);
|
2016-05-31 12:11:09 +00:00
|
|
|
r_reg_reindex (reg);
|
2015-11-25 14:20:25 +00:00
|
|
|
// reset arenas
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2015-08-23 01:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_reg_set_profile(RReg *reg, const char *profile) {
|
|
|
|
int ret;
|
|
|
|
char *base, *file;
|
|
|
|
char *str = r_file_slurp (profile, NULL);
|
|
|
|
if (!str) {
|
2016-04-05 17:51:24 +00:00
|
|
|
base = r_sys_getenv (R_LIB_ENV);
|
2015-08-23 01:58:49 +00:00
|
|
|
if (base) {
|
2017-03-16 21:29:49 +00:00
|
|
|
file = r_str_append (base, profile);
|
2015-08-23 01:58:49 +00:00
|
|
|
str = r_file_slurp (file, NULL);
|
|
|
|
free (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!str) {
|
|
|
|
eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile);
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2015-08-23 01:58:49 +00:00
|
|
|
}
|
|
|
|
ret = r_reg_set_profile_string (reg, str);
|
|
|
|
free (str);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-09 16:18:27 +00:00
|
|
|
|
|
|
|
static int gdb_to_r2_profile(char *gdb) {
|
|
|
|
char *ptr = gdb, *ptr1, *gptr, *gptr1;
|
|
|
|
char name[16], groups[128], type[16];
|
|
|
|
const int all = 1, gpr = 2, save = 4, restore = 8, float_ = 16,
|
2018-10-15 22:15:05 +00:00
|
|
|
sse = 32, vector = 64, system = 128, mmx = 256;
|
2017-07-09 16:18:27 +00:00
|
|
|
int number, rel, offset, size, type_bits, ret;
|
|
|
|
// Every line is -
|
|
|
|
// Name Number Rel Offset Size Type Groups
|
|
|
|
|
|
|
|
// Skip whitespace at beginning of line and empty lines
|
2018-11-06 12:40:00 +00:00
|
|
|
while (isspace ((ut8)*ptr)) {
|
2017-07-09 16:18:27 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
// It's possible someone includes the heading line too. Skip it
|
|
|
|
if (r_str_startswith (ptr, "Name")) {
|
|
|
|
if (!(ptr = strchr (ptr, '\n'))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
2018-09-09 00:41:47 +00:00
|
|
|
for (;;) {
|
2017-07-09 16:18:27 +00:00
|
|
|
// Skip whitespace at beginning of line and empty lines
|
2018-11-06 12:40:00 +00:00
|
|
|
while (isspace ((ut8)*ptr)) {
|
2017-07-09 16:18:27 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
if (!*ptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((ptr1 = strchr (ptr, '\n'))) {
|
|
|
|
*ptr1 = '\0';
|
|
|
|
}
|
|
|
|
ret = sscanf (ptr, " %s %d %d %d %d %s %s", name, &number, &rel,
|
2018-10-15 22:15:05 +00:00
|
|
|
&offset, &size, type, groups);
|
2017-07-09 16:18:27 +00:00
|
|
|
// Groups is optional, others not
|
|
|
|
if (ret < 6) {
|
|
|
|
eprintf ("Could not parse line: %s\n", ptr);
|
|
|
|
if (!ptr1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ptr = ptr1 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If name is '', then skip
|
|
|
|
if (r_str_startswith (name, "''")) {
|
|
|
|
if (!ptr1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ptr = ptr1 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If size is 0, skip
|
|
|
|
if (size == 0) {
|
|
|
|
if (!ptr1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ptr = ptr1 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Parse group
|
|
|
|
gptr = groups;
|
|
|
|
type_bits = 0;
|
|
|
|
while (1) {
|
|
|
|
if ((gptr1 = strchr (gptr, ','))) {
|
|
|
|
*gptr1 = '\0';
|
|
|
|
}
|
|
|
|
if (r_str_startswith (gptr, "general")) {
|
|
|
|
type_bits |= gpr;
|
|
|
|
} else if (r_str_startswith (gptr, "all")) {
|
|
|
|
type_bits |= all;
|
|
|
|
} else if (r_str_startswith (gptr, "save")) {
|
|
|
|
type_bits |= save;
|
|
|
|
} else if (r_str_startswith (gptr, "restore")) {
|
|
|
|
type_bits |= restore;
|
|
|
|
} else if (r_str_startswith (gptr, "float")) {
|
|
|
|
type_bits |= float_;
|
|
|
|
} else if (r_str_startswith (gptr, "sse")) {
|
|
|
|
type_bits |= sse;
|
|
|
|
} else if (r_str_startswith (gptr, "mmx")) {
|
|
|
|
type_bits |= mmx;
|
|
|
|
} else if (r_str_startswith (gptr, "vector")) {
|
|
|
|
type_bits |= vector;
|
|
|
|
} else if (r_str_startswith (gptr, "system")) {
|
|
|
|
type_bits |= system;
|
|
|
|
}
|
|
|
|
if (!gptr1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gptr = gptr1 + 1;
|
|
|
|
}
|
|
|
|
// If type is not defined, skip
|
2017-07-09 23:32:50 +00:00
|
|
|
if (!*type) {
|
2017-07-09 16:18:27 +00:00
|
|
|
if (!ptr1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ptr = ptr1 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: More mappings between gdb and r2 reg groups. For now, either fpu or gpr
|
|
|
|
if (!(type_bits & sse) && !(type_bits & float_)) {
|
|
|
|
type_bits |= gpr;
|
|
|
|
}
|
|
|
|
// Print line
|
|
|
|
eprintf ("%s\t%s\t.%d\t%d\t0\n",
|
2018-10-15 22:15:05 +00:00
|
|
|
// Ref: Comment above about more register type mappings
|
|
|
|
((type_bits & mmx) || (type_bits & float_) || (type_bits & sse)) ? "fpu" : "gpr",
|
|
|
|
name, size * 8, offset);
|
2017-07-09 16:18:27 +00:00
|
|
|
// Go to next line
|
|
|
|
if (!ptr1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ptr = ptr1 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_reg_parse_gdb_profile(const char *profile_file) {
|
2018-11-20 00:13:54 +00:00
|
|
|
char *base, *str = NULL;
|
2017-07-09 16:18:27 +00:00
|
|
|
if (!(str = r_file_slurp (profile_file, NULL))) {
|
|
|
|
if ((base = r_sys_getenv (R_LIB_ENV))) {
|
2018-11-19 21:54:58 +00:00
|
|
|
char *file = r_str_append (base, profile_file);
|
|
|
|
if (file) {
|
2017-07-09 16:18:27 +00:00
|
|
|
str = r_file_slurp (file, NULL);
|
|
|
|
free (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!str) {
|
|
|
|
eprintf ("r_reg_parse_gdb_profile: Cannot find '%s'\n", profile_file);
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-09 00:41:47 +00:00
|
|
|
int ret = gdb_to_r2_profile (str);
|
2017-07-09 16:18:27 +00:00
|
|
|
free (str);
|
|
|
|
return ret;
|
|
|
|
}
|