No longer crash when disassembling from the command-line

This commit is contained in:
Pierre Pronchery 2011-09-27 22:17:49 +00:00
parent b1038cab42
commit a0bdbe2ee7
4 changed files with 34 additions and 34 deletions

View File

@ -60,7 +60,6 @@ static const AsmPluginDescription _asm_plugin_description[APT_COUNT] =
/* prototypes */
static char const * _asm_guess_arch(void);
static char const * _asm_guess_format(void);
static int _asm_open(Asm * a, char const * outfile);
@ -72,6 +71,9 @@ Asm * asm_new(char const * arch, char const * format)
{
Asm * a;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, arch, format);
#endif
if((a = object_new(sizeof(*a))) == NULL)
return NULL;
a->arch = (arch != NULL) ? string_new(arch) : NULL;
@ -90,6 +92,9 @@ Asm * asm_new(char const * arch, char const * format)
/* asm_delete */
void asm_delete(Asm * a)
{
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(a->code != NULL)
code_delete(a->code);
string_delete(a->format);
@ -215,17 +220,6 @@ int asm_guess_arch(Asm * a)
}
/* asm_guess_format */
int asm_guess_format(Asm * a)
{
char const * format;
if((format = _asm_guess_format()) == NULL)
return -1;
return asm_set_format(a, format);
}
/* asm_instruction */
int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...)
{
@ -340,14 +334,6 @@ static char const * _asm_guess_arch(void)
}
/* asm_guess_format */
static char const * _asm_guess_format(void)
{
/* FIXME really guess from known/guessed architecture plug-in */
return "elf";
}
/* asm_open */
static int _asm_open(Asm * a, char const * outfile)
{
@ -356,8 +342,6 @@ static int _asm_open(Asm * a, char const * outfile)
if(arch == NULL && (arch = _asm_guess_arch()) == NULL)
return -1;
if(format == NULL)
format = _asm_guess_format();
if(a->code != NULL)
return -error_set_code(1, "%s: Operation in progress",
code_get_filename(a->code));

View File

@ -94,14 +94,16 @@ Code * code_new(char const * arch, char const * format)
{
Code * code;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, arch, format);
#endif
if((code = object_new(sizeof(*code))) == NULL)
return NULL;
memset(code, 0, sizeof(*code));
if((code->arch = arch_new(arch)) != NULL && format == NULL)
format = arch_get_format(code->arch);
code->arch = arch_new(arch);
if(format != NULL)
code->format = format_new(format);
if(code->arch == NULL || code->format == NULL)
if(code->arch == NULL)
{
code_delete(code);
return NULL;
@ -212,6 +214,9 @@ int code_delete(Code * code)
{
int ret = 0;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(code->format != NULL)
format_delete(code->format);
if(code->arch != NULL)
@ -303,8 +308,12 @@ int code_close(Code * code)
{
int ret = 0;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
ret |= arch_exit(code->arch);
ret |= format_exit(code->format);
if(code->format != NULL)
ret |= format_exit(code->format);
if(code->fp != NULL && fclose(code->fp) != 0 && ret == 0)
ret |= -error_set_code(1, "%s: %s", code->filename,
strerror(errno));
@ -402,7 +411,10 @@ int code_open(Code * code, char const * filename)
return -error_set_code(1, "%s: %s", filename, strerror(errno));
if(arch_init(code->arch, code->filename, code->fp) == 0)
{
if(format_init(code->format, arch_get_name(code->arch),
if(code->format == NULL)
code->format = format_new(arch_get_format(code->arch));
if(code->format != NULL && format_init(code->format,
arch_get_name(code->arch),
code->filename, code->fp) == 0)
return 0;
arch_exit(code->arch);

View File

@ -30,8 +30,7 @@
/* prototypes */
static int _deasm(char const * arch, char const * format, char const * filename,
int raw);
static int _deasm_buffer(char const * arch, char const * format,
char const * buffer, size_t size);
static int _deasm_buffer(char const * arch, char const * buffer, size_t size);
static int _deasm_string(char const * arch, char const * format,
char const * string);
static int _deasm_list(void);
@ -59,15 +58,14 @@ static int _deasm(char const * arch, char const * format, char const * filename,
/* deasm_buffer */
static int _deasm_buffer(char const * arch, char const * format,
char const * buffer, size_t size)
static int _deasm_buffer(char const * arch, char const * buffer, size_t size)
{
Asm * a;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if((a = asm_new(arch, format)) == NULL)
if((a = asm_new(arch, NULL)) == NULL)
return -1;
if(asm_deassemble(a, buffer, size) != 0)
error_print("deasm");
@ -111,7 +109,7 @@ static int _deasm_string(char const * arch, char const * format,
}
}
s[j] = '\0'; /* not really necessary */
ret = _deasm_buffer(arch, format, s, j);
ret = _deasm_buffer(arch, s, j);
free(s);
return ret;
}
@ -150,7 +148,7 @@ static int _deasm_list(void)
static int _usage(void)
{
fputs("Usage: deasm [-a arch][-f format] filename\n"
" deasm [-a arch][-f format] -s string\n"
" deasm [-a arch] -s string\n"
" deasm -l\n", stderr);
return 1;
}

View File

@ -105,6 +105,10 @@ Format * format_new(char const * format)
/* format_delete */
void format_delete(Format * format)
{
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
format_exit(format);
plugin_delete(format->handle);
object_delete(format);
}
@ -190,6 +194,8 @@ int format_init(Format * format, char const * arch, char const * filename,
fprintf(stderr, "DEBUG: %s(\"%s\", %p)\n", __func__, filename,
(void *)fp);
#endif
if(format->plugin->helper != NULL)
format_exit(format);
format->filename = filename;
format->fp = fp;
format->plugin->helper = &format->helper;