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 */ /* prototypes */
static char const * _asm_guess_arch(void); static char const * _asm_guess_arch(void);
static char const * _asm_guess_format(void);
static int _asm_open(Asm * a, char const * outfile); static int _asm_open(Asm * a, char const * outfile);
@ -72,6 +71,9 @@ Asm * asm_new(char const * arch, char const * format)
{ {
Asm * a; Asm * a;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, arch, format);
#endif
if((a = object_new(sizeof(*a))) == NULL) if((a = object_new(sizeof(*a))) == NULL)
return NULL; return NULL;
a->arch = (arch != NULL) ? string_new(arch) : NULL; a->arch = (arch != NULL) ? string_new(arch) : NULL;
@ -90,6 +92,9 @@ Asm * asm_new(char const * arch, char const * format)
/* asm_delete */ /* asm_delete */
void asm_delete(Asm * a) void asm_delete(Asm * a)
{ {
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(a->code != NULL) if(a->code != NULL)
code_delete(a->code); code_delete(a->code);
string_delete(a->format); 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 */ /* asm_instruction */
int asm_instruction(Asm * a, char const * name, unsigned int operands_cnt, ...) 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 */ /* asm_open */
static int _asm_open(Asm * a, char const * outfile) 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) if(arch == NULL && (arch = _asm_guess_arch()) == NULL)
return -1; return -1;
if(format == NULL)
format = _asm_guess_format();
if(a->code != NULL) if(a->code != NULL)
return -error_set_code(1, "%s: Operation in progress", return -error_set_code(1, "%s: Operation in progress",
code_get_filename(a->code)); code_get_filename(a->code));

View File

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

View File

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

View File

@ -105,6 +105,10 @@ Format * format_new(char const * format)
/* format_delete */ /* format_delete */
void format_delete(Format * format) void format_delete(Format * format)
{ {
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
format_exit(format);
plugin_delete(format->handle); plugin_delete(format->handle);
object_delete(format); 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, fprintf(stderr, "DEBUG: %s(\"%s\", %p)\n", __func__, filename,
(void *)fp); (void *)fp);
#endif #endif
if(format->plugin->helper != NULL)
format_exit(format);
format->filename = filename; format->filename = filename;
format->fp = fp; format->fp = fp;
format->plugin->helper = &format->helper; format->plugin->helper = &format->helper;