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

this #14

Merged
merged 3 commits into from
Sep 7, 2012
Merged

this #14

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ config.mk
plugins.cfg
langs.cfg
.*.swp
*.un~

15 changes: 8 additions & 7 deletions libr/anal/cparse/cdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ typedef struct Token Token;
#define R_ANAL_VAR_REGISTER 2
#define R_ANAL_VAR_VOLATILE 3

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);
RAnalType* new_struct_node(char* name, RAnalType *defs);
RAnalType* new_union_node(char* name, RAnalType *defs);
RAnalType* new_variable_node(char* name, short type, short sign, short modifier, RAnalAttr *valattr);
RAnalType* new_pointer_node(char* name, short type, short sign, short modifier, RAnalAttr *valattr);
RAnalType* new_array_node(char* name, short type, short sign, short modifier, long size, RAnalAttr *valattr);
RAnalType* new_struct_node(char* name, RAnalType *defs, RAnalAttr *valattr);
RAnalType* new_union_node(char* name, RAnalType *defs, RAnalAttr *valattr);
RAnalType* new_alloca_node(long address, long size, RAnalType *defs);
RAnalLocals* new_locals_node(RAnalType *defs);
RAnalType* new_function_node(char* name, short ret_type, RAnalType *args, short fmodifier, short callconvention, char* attributes, RAnalLocals *locals);
RAnalLocals* new_locals_node(RAnalType *defs, RAnalAttr *valattr);
RAnalAttr* new_attribute(char* name, char* value);
RAnalType* new_function_node(char* name, short ret_type, RAnalType *args, short fmodifier, short callconvention, char* attributes, RAnalLocals *locals, RAnalAttr *valattr);

1 change: 1 addition & 0 deletions libr/anal/cparse/cparse.l
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
")" { return(RPARENT); }
";" { return(SEMICOLON); }
"," { return(COMMA); }
"=" { return(EQUATION); }

[ \t\v\n\f] { }
[a-zA-Z_][a-zA-Z0-9_]* {
Expand Down
94 changes: 55 additions & 39 deletions libr/anal/cparse/cparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
%type deflist {RAnalType *}
%type def {RAnalType *}
%type function {RAnalType *}
%type attriblist {RAnalAttr *}
%type attrib {RAnalAttr *}
%type arglist {RAnalType *}
%type argdef {RAnalType *}
%type struct {RAnalType *}
Expand Down Expand Up @@ -53,23 +55,37 @@ def(A) ::= variable(B). { A = B; }
def(A) ::= pointer(B). { A = B; }
def(A) ::= array(B). { A = B; }

function(A) ::= FUNCTION type(B) name(C) LPARENT arglist(D) RPARENT locals(E). {
A = new_function_node(C.sval, B.dval, D, R_ANAL_FQUALIFIER_NONE, R_ANAL_CC_TYPE_NONE, NULL, E);
attriblist ::=.
attriblist(A) ::= attrib(B) attriblist(C). {
B->next = C;
A = B;
}
attrib(A) ::= LBRACKET name(B) RBRACKET. {
A = new_attribute(B.sval, NULL);
}
attrib(A) ::= LBRACKET name(B) EQUATION attrval(C) RBRACKET. {
A = new_attribute(B.sval, C.sval);
}
attrval(A) ::= IDENTIFIER(B). { A.sval = B.sval; }


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

fqualifier(A) ::= INLINE. { A.sval = "inline"; A.dval = R_ANAL_FQUALIFIER_INLINE; }
Expand All @@ -96,76 +112,76 @@ argdef(A) ::= pointer(B). { A = B; }
argdef(A) ::= array(B). { A = B; }

locals ::= .
locals(A) ::= OBRACE deflist (B) EBRACE. {
A = new_locals_node(B);
locals(A) ::= attriblist(T) OBRACE deflist (B) EBRACE. {
A = new_locals_node(B, T);
}
struct(A) ::= STRUCT name(B) OBRACE deflist(C) EBRACE. {
A = new_struct_node(B.sval, C);
struct(A) ::= attriblist(T) STRUCT name(B) OBRACE deflist(C) EBRACE. {
A = new_struct_node(B.sval, C, T);
}
union(A) ::= UNION name(B) OBRACE deflist(C) EBRACE. {
A = new_union_node(B.sval, C);
union(A) ::= attriblist(T) UNION name(B) OBRACE deflist(C) EBRACE. {
A = new_union_node(B.sval, C, T);
}
alloca(A) ::= ALLOCA AT address(B) LPARENT size(C) RPARENT OBRACE deflist(D) EBRACE. {
A = new_alloca_node(B.dval, C.dval, D);
}
variable(A) ::= qualifier(E) signedness(D) type(C) name(B). {
A = new_variable_node(B.sval, C.dval, D.dval, E.dval);
variable(A) ::= attriblist(T) qualifier(E) signedness(D) type(C) name(B). {
A = new_variable_node(B.sval, C.dval, D.dval, E.dval, T);
}
variable(A) ::= qualifier(E) shorttype(C) name(B). {
variable(A) ::= attriblist(T) qualifier(E) shorttype(C) name(B). {
switch (C.dval) {
case R_ANAL_UINT8_T:
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT16_T:
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT32_T:
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT64_T:
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_variable_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
default:
break;
}
}
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) ::= attriblist(T) qualifier(E) signedness(D) type(C) ASTERISK name(B). {
A = new_pointer_node(B.sval, C.dval, D.dval, E.dval, T);
}
pointer(A) ::= qualifier(E) shorttype(C) ASTERISK name(B). {
pointer(A) ::= attriblist(T) qualifier(E) shorttype(C) ASTERISK name(B). {
switch (C.dval) {
case R_ANAL_UINT8_T:
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT16_T:
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT32_T:
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
case R_ANAL_UINT64_T:
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, E.dval);
A = new_pointer_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, E.dval, T);
break;
default:
break;
}
}
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) ::= attriblist(T) 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, T);
}
array(A) ::= qualifier(F) shorttype(D) name(B) LBRACKET size(C) RBRACKET. {
array(A) ::= attriblist(T) 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_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval);
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_BYTE, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval, T);
break;
case R_ANAL_UINT16_T:
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval);
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_WORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval, T);
break;
case R_ANAL_UINT32_T:
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval);
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_DWORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval, T);
break;
case R_ANAL_UINT64_T:
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval);
A = new_array_node(B.sval, R_ANAL_VAR_TYPE_QWORD, R_ANAL_TYPE_UNSIGNED, F.dval, C.dval, T);
break;
default:
break;
Expand Down
45 changes: 38 additions & 7 deletions libr/anal/cparse/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static int new_tree() {
return 0;
}

RAnalType* new_variable_node(char* name, short type, short sign, short modifier) {
RAnalType* new_variable_node(char* name, short type, short sign, short modifier, RAnalAttr* valattr) {
RAnalTypeVar *ivar = R_NEW (RAnalTypeVar);
RAnalType *tmp;
ivar->name = name;
Expand All @@ -29,7 +29,7 @@ RAnalType* new_variable_node(char* name, short type, short sign, short modifier)
return tmp;
}

RAnalType* new_pointer_node(char* name, short type, short sign, short modifier) {
RAnalType* new_pointer_node(char* name, short type, short sign, short modifier, RAnalAttr* valattr) {
RAnalTypePtr *iptr = R_NEW (RAnalTypePtr);
RAnalType *tmp;
iptr->name = name;
Expand All @@ -47,7 +47,7 @@ RAnalType* new_pointer_node(char* name, short type, short sign, short modifier)
return tmp;
}

RAnalType* new_array_node(char* name, short type, short sign, short modifier, long size) {
RAnalType* new_array_node(char* name, short type, short sign, short modifier, long size, RAnalAttr *valattr) {
RAnalTypeArray *iarr = R_NEW (RAnalTypeArray);
RAnalType *tmp;
iarr->name = name;
Expand All @@ -67,7 +67,7 @@ RAnalType* new_array_node(char* name, short type, short sign, short modifier, lo
return tmp;
}

RAnalType* new_struct_node(char* name, RAnalType *defs) {
RAnalType* new_struct_node(char* name, RAnalType *defs, RAnalAttr *valattr) {
RAnalTypeStruct *istr = R_NEW (RAnalTypeStruct);
RAnalType *tmp = R_NEW (RAnalType);
istr->name = name;
Expand All @@ -83,7 +83,7 @@ RAnalType* new_struct_node(char* name, RAnalType *defs) {
return tmp;
}

RAnalType* new_union_node(char* name, RAnalType *defs) {
RAnalType* new_union_node(char* name, RAnalType *defs, RAnalAttr *valattr) {
RAnalTypeUnion *iun = R_NEW (RAnalTypeUnion);
RAnalType *tmp = R_NEW (RAnalType);
iun->name = name;
Expand All @@ -110,15 +110,46 @@ RAnalType* new_alloca_node(long address, long size, RAnalType *defs) {
return tmp;
}

RAnalLocals* new_locals_node(RAnalType *defs) {
RAnalLocals* new_locals_node(RAnalType *defs, RAnalAttr *valattr) {
RAnalLocals *il = R_NEW (RAnalLocals);
il->items = defs;
return il;
}

#define ENDIANESS_BIG 1234;
#define ENDIANESS_SMALL 3412;

RAnalAttr* new_attribute(char* name, char* value) {
RAnalAttr *tmp = R_NEW0 (RAnalAttr);
tmp->key = name;
/* TODO: add parsing of various attributes */
if ((!strncmp(name, "pack", 4)) |
(!strncmp(name, "align", 5))) {
tmp->value = atol(value);
} else if ((!strncmp(name, "noreturn", 8)) | (!strncmp(name, "null", 4))) {
tmp->value = 0;
} else if (!strncmp(name, "color", 5)) {
/* TODO: Implement colorizing attributes */
} else if (!strncmp(name, "format", 6)) {
/* TODO: Implement format attributes */
} else if (!strncmp(name, "cconv", 5)) {
/* TODO: Implement calling convention stuff */
} else if (!strncmp(name, "endian", 6)) {
if (!strncmp(value, "big", 3)) {
tmp->value = ENDIANESS_BIG;
} else {
tmp->value = ENDIANESS_SMALL;
}
}
tmp->next = NULL;
return tmp;
}

/* 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, RAnalLocals *locals) {
RAnalType* new_function_node(char* name, short ret_type, RAnalType *args,
short fmodifier, short callconvention, char* attributes,
RAnalLocals *locals, RAnalAttr* valattr) {
RAnalFunction *ifnc = R_NEW (RAnalFunction);
RAnalType *tmp = R_NEW (RAnalType);
ifnc->name = name;
Expand Down
7 changes: 7 additions & 0 deletions libr/include/r_anal.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ typedef struct r_anal_locals_t {
RAnalType *items;
} RAnalLocals;

typedef struct r_anal_attr_t RAnalAttr;
struct r_anal_attr_t {
char *key;
long value;
RAnalAttr *next;
};

typedef struct r_anal_fcn_store_t {
RHashTable64 *h;
RList *l;
Expand Down