Working on better support for deassembling functions
This commit is contained in:
parent
aaba31a0db
commit
7fa5dbc51f
107
src/code.c
107
src/code.c
@ -41,6 +41,14 @@ typedef struct _CodeString
|
|||||||
ssize_t length;
|
ssize_t length;
|
||||||
} CodeString;
|
} CodeString;
|
||||||
|
|
||||||
|
typedef struct _CodeFunction
|
||||||
|
{
|
||||||
|
int id;
|
||||||
|
char * name;
|
||||||
|
off_t offset;
|
||||||
|
ssize_t size;
|
||||||
|
} CodeFunction;
|
||||||
|
|
||||||
struct _Code
|
struct _Code
|
||||||
{
|
{
|
||||||
Arch * arch;
|
Arch * arch;
|
||||||
@ -49,6 +57,10 @@ struct _Code
|
|||||||
char * filename;
|
char * filename;
|
||||||
FILE * fp;
|
FILE * fp;
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
CodeFunction * functions;
|
||||||
|
size_t functions_cnt;
|
||||||
|
|
||||||
/* strings */
|
/* strings */
|
||||||
CodeString * strings;
|
CodeString * strings;
|
||||||
size_t strings_cnt;
|
size_t strings_cnt;
|
||||||
@ -56,6 +68,15 @@ struct _Code
|
|||||||
|
|
||||||
|
|
||||||
/* prototypes */
|
/* prototypes */
|
||||||
|
/* functions */
|
||||||
|
static void _code_function_delete_all(Code * code);
|
||||||
|
|
||||||
|
static CodeFunction * _code_function_get_by_id(Code * code, AsmId id);
|
||||||
|
static int _code_function_set(CodeFunction * codefunction, int id,
|
||||||
|
char const * name, off_t offset, ssize_t size);
|
||||||
|
|
||||||
|
static CodeFunction * _code_function_append(Code * code);
|
||||||
|
|
||||||
/* strings */
|
/* strings */
|
||||||
static void _code_string_delete_all(Code * code);
|
static void _code_string_delete_all(Code * code);
|
||||||
|
|
||||||
@ -191,6 +212,8 @@ int code_delete(Code * code)
|
|||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
|
_code_function_delete_all(code);
|
||||||
|
_code_string_delete_all(code);
|
||||||
if(code->format != NULL)
|
if(code->format != NULL)
|
||||||
format_delete(code->format);
|
format_delete(code->format);
|
||||||
if(code->arch != NULL)
|
if(code->arch != NULL)
|
||||||
@ -229,8 +252,8 @@ char const * code_get_format(Code * code)
|
|||||||
/* code_get_function_by_id */
|
/* code_get_function_by_id */
|
||||||
AsmFunction * code_get_function_by_id(Code * code, AsmId id)
|
AsmFunction * code_get_function_by_id(Code * code, AsmId id)
|
||||||
{
|
{
|
||||||
/* FIXME implement */
|
/* XXX CodeFunction has to be exactly like an AsmFunction */
|
||||||
return NULL;
|
return _code_function_get_by_id(code, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -246,8 +269,16 @@ AsmString * code_get_string_by_id(Code * code, AsmId id)
|
|||||||
int code_set_function(Code * code, int id, char const * name, off_t offset,
|
int code_set_function(Code * code, int id, char const * name, off_t offset,
|
||||||
ssize_t size)
|
ssize_t size)
|
||||||
{
|
{
|
||||||
/* FIXME implement */
|
CodeFunction * cf = NULL;
|
||||||
return -1;
|
|
||||||
|
if(id >= 0)
|
||||||
|
cf = _code_function_get_by_id(code, id);
|
||||||
|
if(cf == NULL)
|
||||||
|
cf = _code_function_append(code);
|
||||||
|
if(cf == NULL || _code_function_set(cf, id, name, offset, size) != 0)
|
||||||
|
return -1;
|
||||||
|
/* FIXME isn't it considered an error if no ID is known yet? */
|
||||||
|
return cf->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -263,6 +294,7 @@ int code_set_string(Code * code, int id, char const * name, off_t offset,
|
|||||||
cs = _code_string_append(code);
|
cs = _code_string_append(code);
|
||||||
if(cs == NULL || _code_string_set(cs, id, name, offset, length) != 0)
|
if(cs == NULL || _code_string_set(cs, id, name, offset, length) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
/* FIXME isn't it considered an error if no ID is known yet? */
|
||||||
return cs->id;
|
return cs->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,6 +507,73 @@ int code_section(Code * code, char const * section)
|
|||||||
|
|
||||||
/* private */
|
/* private */
|
||||||
/* functions */
|
/* functions */
|
||||||
|
/* functions */
|
||||||
|
/* code_function_delete_all */
|
||||||
|
static void _code_function_delete_all(Code * code)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for(i = 0; i < code->functions_cnt; i++)
|
||||||
|
free(code->functions[i].name);
|
||||||
|
code->functions_cnt = 0;
|
||||||
|
free(code->functions);
|
||||||
|
code->functions = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* code_function_get_by_id */
|
||||||
|
static CodeFunction * _code_function_get_by_id(Code * code, AsmId id)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for(i = 0; i < code->functions_cnt; i++)
|
||||||
|
if(code->functions[i].id >= 0
|
||||||
|
&& (AsmId)code->functions[i].id == id)
|
||||||
|
break;
|
||||||
|
if(i == code->functions_cnt)
|
||||||
|
return NULL;
|
||||||
|
return &code->functions[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* code_function_set */
|
||||||
|
static int _code_function_set(CodeFunction * codefunction, int id,
|
||||||
|
char const * name, off_t offset, ssize_t size)
|
||||||
|
{
|
||||||
|
char * p = NULL;
|
||||||
|
|
||||||
|
if(name != NULL && (p = string_new(name)) == NULL)
|
||||||
|
return -error_set_code(1, "%s", strerror(errno));
|
||||||
|
codefunction->id = id;
|
||||||
|
free(codefunction->name);
|
||||||
|
codefunction->name = p;
|
||||||
|
codefunction->offset = offset;
|
||||||
|
codefunction->size = size;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* code_function_append */
|
||||||
|
static CodeFunction * _code_function_append(Code * code)
|
||||||
|
{
|
||||||
|
CodeFunction * p;
|
||||||
|
|
||||||
|
if((p = realloc(code->functions, sizeof(*p) * (code->functions_cnt
|
||||||
|
+ 1))) == NULL)
|
||||||
|
{
|
||||||
|
error_set_code(1, "%s", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
code->functions = p;
|
||||||
|
p = &code->functions[code->functions_cnt++];
|
||||||
|
p->id = -1;
|
||||||
|
p->name = NULL;
|
||||||
|
p->offset = -1;
|
||||||
|
p->size = -1;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* strings */
|
/* strings */
|
||||||
/* code_string_delete_all */
|
/* code_string_delete_all */
|
||||||
static void _code_string_delete_all(Code * code)
|
static void _code_string_delete_all(Code * code)
|
||||||
|
Loading…
Reference in New Issue
Block a user