Skip to content

Commit

Permalink
Add profile to specify format specifications for different programmin…
Browse files Browse the repository at this point in the history
…g languages/compiler
  • Loading branch information
sivaramaaa authored and Anton Kochkov committed Jul 23, 2018
1 parent 1efd337 commit c5928fa
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 45 deletions.
1 change: 1 addition & 0 deletions libr/anal/anal.c
Expand Up @@ -79,6 +79,7 @@ R_API RAnal *r_anal_new() {
anal->sdb_meta = sdb_ns (anal->sdb, "meta", 1);
anal->sdb_hints = sdb_ns (anal->sdb, "hints", 1);
anal->sdb_types = sdb_ns (anal->sdb, "types", 1);
anal->sdb_fmts = sdb_ns (anal->sdb, "spec", 1);
anal->sdb_cc = sdb_ns (anal->sdb, "cc", 1);
anal->sdb_zigns = sdb_ns (anal->sdb, "zigns", 1);
anal->zign_path = strdup ("");
Expand Down
1 change: 1 addition & 0 deletions libr/anal/d/Makefile
Expand Up @@ -12,6 +12,7 @@ F+= types-x86-macos-64
F+= types-arm-ios-16
F+= types-arm-ios-32
F+= types-arm-ios-64
F+= spec
F+= cc-x86-64
F+= cc-x86-32
F+= cc-mips-32
Expand Down
1 change: 1 addition & 0 deletions libr/anal/d/meson.build
Expand Up @@ -11,6 +11,7 @@ sdb_files = [
'types-arm-ios-16',
'types-arm-ios-32',
'types-arm-ios-64',
'spec',
'cc-x86-64',
'cc-x86-32',
'cc-mips-32',
Expand Down
15 changes: 15 additions & 0 deletions libr/anal/d/spec.sdb.txt
@@ -0,0 +1,15 @@
gcc=spec
spec.gcc.c=char
spec.gcc.d=int
spec.gcc.f=float
spec.gcc.g=double
spec.gcc.ld=long int
spec.gcc.li=long int
spec.gcc.lf=double
spec.gcc.lu=unsigned long int
spec.gcc.llu=unsigned long long int
spec.gcc.lli=long long int
spec.gcc.lld=long long int
spec.gcc.p=void *
spec.gcc.s=const char *
spec.gcc.u=unsigned int
61 changes: 16 additions & 45 deletions libr/core/anal_tp.c
Expand Up @@ -150,55 +150,26 @@ static ut64 get_addr(Sdb *trace, const char *regname, int idx) {
return r_num_math (NULL, sdb_const_get (trace, query, 0));
}

static RList *parse_format(char *fmt) {
static RList *parse_format(RCore *core, char *fmt) {
RList *ret = r_list_new();
Sdb *s = core->anal->sdb_fmts;
const char *spec = r_config_get (core->config, "anal.spec");
char arr[10] = {0};
char *ptr = strchr (fmt, '%');
char *type = NULL;
fmt[strlen(fmt) - 1] = '\0';
while (ptr) {
ptr += 1;
switch(ptr[0]) {
case 'c':
type = "char";
break;
case 'f':
type = "float";
break;
case 'g':
type = "double";
break;
case 'l':
switch (ptr [1]) {
case 'f': // "%lf"
type = "double";
break;
case 'u': // "%lu"
type = "unsigned long int";
break;
case 'l':
if (ptr [2] == 'u') { // "%llu"
type = "unsigned long long int";
} else {
type = "long long int";
}
break;
default:
type = "long int";
}
break;
case 'p':
type = "void *";
break;
case 's':
type = "const char *";
break;
case 'u':
type = "unsigned int";
break;
case 'd':
default:
type = "int";
// strip [width] specifier
while (IS_DIGIT (*ptr)) { ptr++; }
strncpy (arr, ptr, sizeof(arr));
char *tmp = arr;
while (tmp && (IS_LOWER (*tmp) || IS_UPPER (*tmp))) { tmp++; }
*tmp = '\0';
const char *query = sdb_fmt ("spec.%s.%s", spec, arr);
char *type = (char *) sdb_const_get (s, query, 0);
if (type) {
r_list_append (ret, type);
}
r_list_append (ret, type);
ptr = strchr (ptr, '%');
}
return ret;
Expand Down Expand Up @@ -299,7 +270,7 @@ static void type_match(RCore *core, ut64 addr, char *fcn_name, ut64 baddr, const
if ((op->ptr && op->ptr != UT64_MAX) && !strcmp (name, "format")) {
RFlagItem *f = r_flag_get_i (core->flags, op->ptr);
if (f && !strncmp (f->name, "str", 3)) {
types = parse_format (f->realname);
types = parse_format (core, f->realname);
max += r_list_length (types);
format = true;
}
Expand Down
5 changes: 5 additions & 0 deletions libr/core/cbin.c
Expand Up @@ -823,6 +823,11 @@ static int bin_info(RCore *r, int mode) {
}
r_core_anal_type_init (r);
r_core_anal_cc_init (r);
const char *dir_prefix = r_config_get (r->config, "dir.prefix");
char *spath = sdb_fmt ("%s/"R2_SDB_FCNSIGN"/spec.sdb", dir_prefix);
if (r_file_exists (spath)) {
sdb_concat_by_path (r->anal->sdb_fmts, spath);
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions libr/core/cconfig.c
Expand Up @@ -2378,6 +2378,7 @@ R_API int r_core_config_init(RCore *core) {
SETPREF ("anal.hasnext", "false", "Continue analysis after each function");
SETPREF ("anal.esil", "false", "Use the new ESIL code analysis");
SETCB ("anal.strings", "false", &cb_analstrings, "Identify and register strings during analysis (aar only)");
SETPREF ("anal.spec", "gcc", "Set profile for specifying format chars used in type analysis");
SETCB ("anal.vars", "true", &cb_analvars, "Analyze local variables and arguments");
SETPREF ("anal.vinfun", "true", "Search values in functions (aav) (false by default to only find on non-code)");
SETPREF ("anal.vinfunrange", "false", "Search values outside function ranges (requires anal.vinfun=false)\n");
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_anal.h
Expand Up @@ -653,6 +653,7 @@ typedef struct r_anal_t {
RAnalRange *limit;
RList *plugins;
Sdb *sdb_types;
Sdb *sdb_fmts;
Sdb *sdb_meta; // TODO: Future r_meta api
Sdb *sdb_zigns;

Expand Down

0 comments on commit c5928fa

Please sign in to comment.