Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second step of cparse integration #4

Merged
merged 3 commits into from
Jul 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libr/anal/anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <r_io.h>
#include "../config.h"

static RAnalPlugin *anal_static_plugins[] =
static RAnalPlugin *anal_static_plugins[] =
{ R_ANAL_STATIC_PLUGINS };

/*
static RAnalVarType anal_default_vartypes[] =
{{ "char", "c", 1 },
{ "byte", "b", 1 },
Expand All @@ -19,6 +20,7 @@ static RAnalVarType anal_default_vartypes[] =
{ "dword", "x", 4 },
{ "float", "f", 4 },
{ NULL, NULL, 0 }};
*/

R_API RAnal *r_anal_new() {
int i;
Expand All @@ -40,7 +42,7 @@ R_API RAnal *r_anal_new() {
anal->fcns = r_anal_fcn_list_new ();
anal->fcnstore = r_listrange_new ();
anal->refs = r_anal_ref_list_new ();
anal->vartypes = r_anal_var_type_list_new ();
anal->types = r_anal_type_list_new ();
r_anal_set_bits (anal, 32);
r_anal_set_big_endian (anal, R_FALSE);
INIT_LIST_HEAD (&anal->anals); // TODO: use RList here
Expand All @@ -49,9 +51,11 @@ R_API RAnal *r_anal_new() {
memcpy (static_plugin, anal_static_plugins[i], sizeof (RAnalPlugin));
r_anal_add (anal, static_plugin);
}
/*
for (i=0; anal_default_vartypes[i].name; i++)
r_anal_var_type_add (anal, anal_default_vartypes[i].name,
anal_default_vartypes[i].size, anal_default_vartypes[i].fmt);
*/
return anal;
}

Expand All @@ -62,7 +66,7 @@ R_API void r_anal_free(RAnal *anal) {
r_list_free (anal->fcns);
// r_listrange_free (anal->fcnstore); // might provoke double frees since this is used in r_anal_fcn_insert()
r_list_free (anal->refs);
r_list_free (anal->vartypes);
r_list_free (anal->types);
r_list_free (anal->meta->data);
r_reg_free(anal->reg);
r_syscall_free(anal->syscall);
Expand Down Expand Up @@ -166,7 +170,7 @@ R_API char *r_anal_strmask (RAnal *anal, const char *data) {

R_API void r_anal_trace_bb(RAnal *anal, ut64 addr) {
RAnalBlock *bbi;
RAnalFcn *fcni;
RAnalFunction *fcni;
RListIter *iter, *iter2;
VERBOSE_ANAL eprintf ("bbtraced\n"); // XXX Debug msg
r_list_foreach (anal->fcns, iter, fcni) {
Expand Down
2 changes: 1 addition & 1 deletion libr/anal/bb.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ R_API inline int r_anal_bb_is_in_offset (RAnalBlock *bb, ut64 off) {

R_API RAnalBlock *r_anal_bb_from_offset(RAnal *anal, ut64 off) {
RListIter *iter, *iter2;
RAnalFcn *fcn;
RAnalFunction *fcn;
RAnalBlock *bb;
r_list_foreach (anal->fcns, iter, fcn)
r_list_foreach (fcn->bbs, iter2, bb)
Expand Down
2 changes: 1 addition & 1 deletion libr/anal/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ R_API void r_anal_cc_reset (RAnalCC *cc) {
//XXX: may overflow. this is vulnerable. needs fix
R_API char *r_anal_cc_to_string (RAnal *anal, RAnalCC* cc) {
RSyscallItem *si;
RAnalFcn *fcn;
RAnalFunction *fcn;
char str[1024], buf[64];
int i, eax = 0; // eax = arg0

Expand Down
11 changes: 1 addition & 10 deletions libr/anal/cparse/cdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@ typedef struct Token Token;
#define R_ANAL_UINT64_T 4

#define NONE_SIGN 11
#define NONE_MODIFIER 12
#define NONE_QUALIFIER 12

#define R_ANAL_VAR_STATIC 0
#define R_ANAL_VAR_CONST 1
#define R_ANAL_VAR_REGISTER 2
#define R_ANAL_VAR_VOLATILE 3

#define R_ANAL_FMODIFIER_NONE 0
#define R_ANAL_FMODIFIER_STATIC 1
#define R_ANAL_FMODIFIER_VOLATILE 2
#define R_ANAL_FMODIFIER_INLINE 3

#define R_ANAL_CALLCONV_NONE 0
#define R_ANAL_CALLCONV_STDCALL 1
#define R_ANAL_CALLCONV_CCALL 2

RAnalType* new_variable_node(char* name, short type, short sign, short modifier);
RAnalType* new_pointer_node(char* name, short type, short sign, short modifier);
RAnalType* new_array_node(char* name, short type, short sign, short modifier, long size);
Expand Down
62 changes: 34 additions & 28 deletions libr/anal/cparse/cparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,37 @@
#define INLINE 5
#define VOLATILE 6
#define STATIC 7
#define STDCALL 8
#define CCALL 9
#define ATTRIBUTE 10
#define COMMA 11
#define STRUCT 12
#define OBRACE 13
#define EBRACE 14
#define UNION 15
#define ASTERISK 16
#define LBRACKET 17
#define RBRACKET 18
#define NUMBER 19
#define CHAR 20
#define SHORT 21
#define INTEGER 22
#define LONG 23
#define FLOAT 24
#define DOUBLE 25
#define VOID 26
#define UINT8 27
#define UINT16 28
#define UINT32 29
#define UINT64 30
#define SIGNED 31
#define UNSIGNED 32
#define CONST 33
#define REGISTER 34
#define IDENTIFIER 35
#define NAKED 8
#define VIRTUAL 9
#define STDCALL 10
#define CDECL 11
#define FASTCALL 12
#define PASCALCALL 13
#define WINAPI 14
#define THISCALL 15
#define ATTRIBUTE 16
#define COMMA 17
#define STRUCT 18
#define OBRACE 19
#define EBRACE 20
#define UNION 21
#define ASTERISK 22
#define LBRACKET 23
#define RBRACKET 24
#define NUMBER 25
#define CHAR 26
#define SHORT 27
#define INTEGER 28
#define LONG 29
#define FLOAT 30
#define DOUBLE 31
#define VOID 32
#define UINT8 33
#define UINT16 34
#define UINT32 35
#define UINT64 36
#define SIGNED 37
#define UNSIGNED 38
#define CONST 39
#define REGISTER 40
#define IDENTIFIER 41
9 changes: 8 additions & 1 deletion libr/anal/cparse/cparse.l
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"static" { return(STATIC); }
"volatile" { return(VOLATILE); }
"inline" { return(INLINE); }
"naked" { return(NAKED); }
"virtual" { return(VIRTUAL); }
"struct" { return(STRUCT); }
"union" { return(UNION); }
"function" { return(FUNCTION); }
Expand All @@ -41,7 +43,12 @@

"__attribute__" { return(ATTRIBUTE); }
"__stdcall" { return(STDCALL); }
"__ccall" { return(CCALL); }
"__cdecl" { return(CDECL); }
"__fastcall" { return(FASTCALL); }
"__pascal" { return(PASCALCALL); }
"WINAPI" { return(WINAPI); }
"thiscall" { return(THISCALL); }


"{" { return(OBRACE); }
"}" { return(EBRACE); }
Expand Down
54 changes: 30 additions & 24 deletions libr/anal/cparse/cparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,36 @@ def(A) ::= pointer(B). { A = B; }
def(A) ::= array(B). { A = B; }

function(A) ::= FUNCTION type(B) name(C) LPARENT arglist(D) RPARENT. {
A = new_function_node(C.sval, B.dval, D, R_ANAL_FMODIFIER_NONE, R_ANAL_CALLCONV_NONE, NULL);
A = new_function_node(C.sval, B.dval, D, R_ANAL_FQUALIFIER_NONE, R_ANAL_CC_TYPE_NONE, NULL);
}
function(A) ::= FUNCTION fmodifier(B) type(C) name(D) LPARENT arglist(E) RPARENT. {
A = new_function_node(D.sval, C.dval, E, B.dval, R_ANAL_CALLCONV_NONE, NULL);
function(A) ::= FUNCTION fqualifier(B) type(C) name(D) LPARENT arglist(E) RPARENT. {
A = new_function_node(D.sval, C.dval, E, B.dval, R_ANAL_CC_TYPE_NONE, NULL);
}
function(A) ::= FUNCTION callconvention(B) type(C) name(D) LPARENT arglist(E) RPARENT. {
A = new_function_node(D.sval, C.dval, E, R_ANAL_FMODIFIER_NONE, B.dval, NULL);
A = new_function_node(D.sval, C.dval, E, R_ANAL_FQUALIFIER_NONE, B.dval, NULL);
}
function(A) ::= FUNCTION callconvention(B) fmodifier(C) type(D) name(E) LPARENT arglist(F) RPARENT. {
function(A) ::= FUNCTION callconvention(B) fqualifier(C) type(D) name(E) LPARENT arglist(F) RPARENT. {
A = new_function_node(E.sval, D.dval, F, C.dval, B.dval, NULL);
}
function(A) ::= FUNCTION attribute(B) fmodifier(C) type(D) name(E) LPARENT arglist(F) RPARENT. {
A = new_function_node(E.sval, D.dval, F, C.dval, R_ANAL_CALLCONV_NONE, B.sval);
function(A) ::= FUNCTION attribute(B) fqualifier(C) type(D) name(E) LPARENT arglist(F) RPARENT. {
A = new_function_node(E.sval, D.dval, F, C.dval, R_ANAL_CC_TYPE_NONE, B.sval);
}
function(A) ::= FUNCTION attribute(B) callconvention(C) fmodifier(D) type(E) name(F) LPARENT arglist(G) RPARENT. {
function(A) ::= FUNCTION attribute(B) callconvention(C) fqualifier(D) type(E) name(F) LPARENT arglist(G) RPARENT. {
A = new_function_node(F.sval, E.dval, G, D.dval, C.dval, B.sval);
}

fmodifier(A) ::= INLINE. { A.sval = "inline"; A.dval = R_ANAL_FMODIFIER_INLINE; }
fmodifier(A) ::= VOLATILE. { A.sval = "volatile"; A.dval = R_ANAL_FMODIFIER_VOLATILE; }
fmodifier(A) ::= STATIC. { A.sval = "static"; A.dval = R_ANAL_FMODIFIER_STATIC; }
fqualifier(A) ::= INLINE. { A.sval = "inline"; A.dval = R_ANAL_FQUALIFIER_INLINE; }
fqualifier(A) ::= VOLATILE. { A.sval = "volatile"; A.dval = R_ANAL_FQUALIFIER_VOLATILE; }
fqualifier(A) ::= STATIC. { A.sval = "static"; A.dval = R_ANAL_FQUALIFIER_STATIC; }
fqualifier(A) ::= NAKED. { A.sval = "naked"; A.dval = R_ANAL_FQUALIFIER_NAKED; }
fqualifier(A) ::= VIRTUAL. { A.sval = "virtual"; A.dval = R_ANAL_FQUALIFIER_VIRTUAL; }

callconvention(A) ::= STDCALL. { A.sval = "__stdcall"; A.dval = R_ANAL_CALLCONV_STDCALL; }
callconvention(A) ::= CCALL. { A.sval = "__ccall"; A.dval = R_ANAL_CALLCONV_CCALL; }
callconvention(A) ::= STDCALL. { A.sval = "__stdcall"; A.dval = R_ANAL_CC_TYPE_STDCALL; }
callconvention(A) ::= CDECL. { A.sval = "__cdecl"; A.dval = R_ANAL_CC_TYPE_CDECL; }
callconvention(A) ::= FASTCALL. { A.sval = "__fastcall"; A.dval = R_ANAL_CC_TYPE_FASTCALL; }
callconvention(A) ::= PASCALCALL. { A.sval = "__pascal"; A.dval = R_ANAL_CC_TYPE_PASCAL; }
callconvention(A) ::= WINAPI. { A.sval = "WINAPI"; A.dval = R_ANAL_CC_TYPE_WINAPI; }
callconvention(A) ::= THISCALL. { A.sval = "__thiscall"; A.dval = R_ANAL_CC_TYPE_THISCALL; }

attribute(A) ::= ATTRIBUTE LPARENT LPARENT name(B) RPARENT RPARENT. {
A.sval = B.sval; A.dval = 0;
Expand All @@ -79,10 +85,10 @@ struct(A) ::= STRUCT name(B) OBRACE deflist(C) EBRACE. {
union(A) ::= UNION name(B) OBRACE deflist(C) EBRACE. {
A = new_union_node(B.sval, C);
}
variable(A) ::= modifier(E) signedness(D) type(C) name(B). {
variable(A) ::= qualifier(E) signedness(D) type(C) name(B). {
A = new_variable_node(B.sval, C.dval, D.dval, E.dval);
}
variable(A) ::= modifier(E) shorttype(C) name(B). {
variable(A) ::= qualifier(E) shorttype(C) name(B). {
switch (C.dval) {
case R_ANAL_UINT8_T:
A = new_variable_node(B.sval, R_ANAL_TYPE_SHORT, R_ANAL_TYPE_UNSIGNED, E.dval);
Expand All @@ -100,10 +106,10 @@ variable(A) ::= modifier(E) shorttype(C) name(B). {
break;
}
}
pointer(A) ::= modifier(E) signedness(D) type(C) ASTERISK name(B). {
pointer(A) ::= qualifier(E) signedness(D) type(C) ASTERISK name(B). {
A = new_pointer_node(B.sval, C.dval, D.dval, E.dval);
}
pointer(A) ::= modifier(E) shorttype(C) ASTERISK name(B). {
pointer(A) ::= qualifier(E) shorttype(C) ASTERISK name(B). {
switch (C.dval) {
case R_ANAL_UINT8_T:
A = new_pointer_node(B.sval, R_ANAL_TYPE_SHORT, R_ANAL_TYPE_UNSIGNED, E.dval);
Expand All @@ -121,10 +127,10 @@ pointer(A) ::= modifier(E) shorttype(C) ASTERISK name(B). {
break;
}
}
array(A) ::= modifier(F) signedness(E) type(D) name(B) LBRACKET size(C) RBRACKET. {
array(A) ::= qualifier(F) signedness(E) type(D) name(B) LBRACKET size(C) RBRACKET. {
A = new_array_node(B.sval, D.dval, E.dval, F.dval, C.dval);
}
array(A) ::= modifier(F) shorttype(D) name(B) LBRACKET size(C) RBRACKET. {
array(A) ::= qualifier(F) shorttype(D) name(B) LBRACKET size(C) RBRACKET. {
switch (D.dval) {
case R_ANAL_UINT8_T:
A = new_array_node(B.sval, R_ANAL_TYPE_SHORT, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval);
Expand Down Expand Up @@ -159,10 +165,10 @@ shorttype(A) ::= UINT64. { A.dval = R_ANAL_UINT64_T; }
signedness(A) ::= . { A.sval = ""; A.dval = NONE_SIGN; }
signedness(A) ::= SIGNED. { A.sval = "signed"; A.dval = R_ANAL_TYPE_SIGNED; }
signedness(A) ::= UNSIGNED. { A.sval = "unsigned"; A.dval = R_ANAL_TYPE_UNSIGNED; }
modifier(A) ::= . { A.sval = ""; A.dval = NONE_MODIFIER; }
modifier(A) ::= STATIC. { A.sval = "static"; A.dval = R_ANAL_VAR_STATIC; }
modifier(A) ::= CONST. {A.sval = "const"; A.dval = R_ANAL_VAR_CONST; }
modifier(A) ::= REGISTER. { A.sval = "register"; A.dval = R_ANAL_VAR_REGISTER; }
modifier(A) ::= VOLATILE. { A.sval = "volatile"; A.dval = R_ANAL_VAR_VOLATILE; }
qualifier(A) ::= . { A.sval = ""; A.dval = NONE_QUALIFIER; }
qualifier(A) ::= STATIC. { A.sval = "static"; A.dval = R_ANAL_VAR_STATIC; }
qualifier(A) ::= CONST. {A.sval = "const"; A.dval = R_ANAL_VAR_CONST; }
qualifier(A) ::= REGISTER. { A.sval = "register"; A.dval = R_ANAL_VAR_REGISTER; }
qualifier(A) ::= VOLATILE. { A.sval = "volatile"; A.dval = R_ANAL_VAR_VOLATILE; }
name(A) ::= IDENTIFIER(B). { A.sval = B.sval; }

6 changes: 4 additions & 2 deletions libr/anal/cparse/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static int new_tree() {
return 0;
}

#if 0
static int print_tree(RAnalType *t) {
RAnalType *p = t;
if (!p) {
Expand All @@ -22,7 +23,7 @@ static int print_tree(RAnalType *t) {
eprintf("ptr %s\n", p->custom.p->name);
break;
case R_ANAL_TYPE_ARRAY:
eprintf("arr %s[%ld]\n", p->custom.a->name, p->custom.a->count);
eprintf("arr %s[%lld]\n", p->custom.a->name, p->custom.a->count);
break;
case R_ANAL_TYPE_STRUCT:
eprintf("Entering struct %s...\n", p->custom.s->name);
Expand All @@ -44,6 +45,7 @@ static int print_tree(RAnalType *t) {
}
return R_TRUE;
}
#endif

RAnalType* new_variable_node(char* name, short type, short sign, short modifier) {
RAnalTypeVar *ivar = R_NEW (RAnalTypeVar);
Expand Down Expand Up @@ -113,7 +115,7 @@ RAnalType* new_union_node(char* name, RAnalType *defs) {
/* Function can return another function or have multiple returns */
//item_list* new_function_node(char* name, item_list *rets, item_list *args)
RAnalType* new_function_node(char* name, short ret_type, RAnalType *args, short fmodifier, short callconvention, char* attributes) {
RAnalTypeFunction *ifnc = R_NEW (RAnalTypeFunction);
RAnalFunction *ifnc = R_NEW (RAnalFunction);
RAnalType *tmp = R_NEW (RAnalType);
ifnc->name = name;
ifnc->rets = ret_type;
Expand Down
6 changes: 3 additions & 3 deletions libr/anal/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ R_API int r_anal_diff_fingerprint_bb(RAnal *anal, RAnalBlock *bb) {
return bb->size;
}

R_API int r_anal_diff_fingerprint_fcn(RAnal *anal, RAnalFcn *fcn) {
R_API int r_anal_diff_fingerprint_fcn(RAnal *anal, RAnalFunction *fcn) {
RAnalBlock *bb;
RListIter *iter;
int len = 0;
Expand All @@ -94,7 +94,7 @@ R_API int r_anal_diff_fingerprint_fcn(RAnal *anal, RAnalFcn *fcn) {
return len;
}

R_API int r_anal_diff_bb(RAnal *anal, RAnalFcn *fcn, RAnalFcn *fcn2) {
R_API int r_anal_diff_bb(RAnal *anal, RAnalFunction *fcn, RAnalFunction *fcn2) {
RAnalBlock *bb, *bb2, *mbb, *mbb2;
RListIter *iter, *iter2;
double t, ot;
Expand Down Expand Up @@ -141,7 +141,7 @@ R_API int r_anal_diff_bb(RAnal *anal, RAnalFcn *fcn, RAnalFcn *fcn2) {
}

R_API int r_anal_diff_fcn(RAnal *anal, RList *fcns, RList *fcns2) {
RAnalFcn *fcn, *fcn2, *mfcn, *mfcn2;
RAnalFunction *fcn, *fcn2, *mfcn, *mfcn2;
RListIter *iter, *iter2;
ut64 maxsize, minsize;
double t, ot;
Expand Down
Loading