Consider the architecture's description when printing addresses

This commit is contained in:
Pierre Pronchery 2011-09-28 00:30:40 +00:00
parent e411b708b0
commit a890a5699d

View File

@ -334,9 +334,13 @@ int code_decode(Code * code, int raw)
/* code_decode_at */ /* code_decode_at */
static void _decode_at_print_address(ArchDescription * description,
unsigned long address);
int code_decode_at(Code * code, char const * section, off_t offset, int code_decode_at(Code * code, char const * section, off_t offset,
size_t size, off_t base) size_t size, off_t base)
{ {
ArchDescription * description;
ArchInstructionCall * calls = NULL; ArchInstructionCall * calls = NULL;
size_t calls_cnt = 0; size_t calls_cnt = 0;
size_t i; size_t i;
@ -346,30 +350,53 @@ int code_decode_at(Code * code, char const * section, off_t offset,
if(arch_decode_at(code->arch, code, &calls, &calls_cnt, offset, size, if(arch_decode_at(code->arch, code, &calls, &calls_cnt, offset, size,
base) != 0) base) != 0)
return -1; return -1;
description = arch_get_description(code->arch);
if(size != 0) if(size != 0)
printf("\n%08lx:\n", (long)base); _decode_at_print_address(description, base);
for(i = 0; i < calls_cnt; i++) for(i = 0; i < calls_cnt; i++)
code_print(code, &calls[i]); code_print(code, description, &calls[i]);
free(calls); free(calls);
if(arch_seek(code->arch, offset + size, SEEK_SET) < 0) if(arch_seek(code->arch, offset + size, SEEK_SET) < 0)
return -1; return -1;
return 0; return 0;
} }
static void _decode_at_print_address(ArchDescription * description,
unsigned long address)
{
uint32_t size = (description != NULL) ? description->address_size : 32;
char const * format = "\n%08lx:\n";
switch(size)
{
case 64:
format = "\n%016lx:\n";
break;
case 20:
format = "\n%05lx:\n";
break;
default:
break;
}
printf(format, address);
}
/* code_decode_buffer */ /* code_decode_buffer */
int code_decode_buffer(Code * code, char const * buffer, size_t size) int code_decode_buffer(Code * code, char const * buffer, size_t size)
{ {
int ret; int ret;
ArchDescription * description;
ArchInstructionCall * calls = NULL; ArchInstructionCall * calls = NULL;
size_t calls_cnt = 0; size_t calls_cnt = 0;
size_t i; size_t i;
arch_init_buffer(code->arch, buffer, size); arch_init_buffer(code->arch, buffer, size);
description = arch_get_description(code->arch);
if((ret = arch_decode(code->arch, code, &calls, &calls_cnt, 0)) == 0) if((ret = arch_decode(code->arch, code, &calls, &calls_cnt, 0)) == 0)
{ {
for(i = 0; i < calls_cnt; i++) for(i = 0; i < calls_cnt; i++)
code_print(code, &calls[i]); code_print(code, description, &calls[i]);
free(calls); free(calls);
} }
arch_exit(code->arch); arch_exit(code->arch);
@ -429,9 +456,12 @@ int code_open(Code * code, char const * filename)
/* code_print */ /* code_print */
static void _print_address(ArchDescription * description,
unsigned long address);
static void _print_immediate(ArchOperand * ao); static void _print_immediate(ArchOperand * ao);
int code_print(Code * code, ArchInstructionCall * call) int code_print(Code * code, ArchDescription * description,
ArchInstructionCall * call)
{ {
char const * sep = " "; char const * sep = " ";
size_t i; size_t i;
@ -439,9 +469,11 @@ int code_print(Code * code, ArchInstructionCall * call)
ArchOperand * ao; ArchOperand * ao;
char const * name; char const * name;
if(description == NULL)
description = arch_get_description(code->arch);
if(arch_seek(code->arch, call->offset, SEEK_SET) < 0) if(arch_seek(code->arch, call->offset, SEEK_SET) < 0)
return -1; return -1;
printf("%8lx:", (long)call->base); _print_address(description, call->base);
for(i = 0; i < call->size; i++) for(i = 0; i < call->size; i++)
{ {
if(arch_read(code->arch, &u8, sizeof(u8)) != sizeof(u8)) if(arch_read(code->arch, &u8, sizeof(u8)) != sizeof(u8))
@ -486,6 +518,25 @@ int code_print(Code * code, ArchInstructionCall * call)
return 0; return 0;
} }
static void _print_address(ArchDescription * description, unsigned long address)
{
uint32_t size = (description != NULL) ? description->address_size : 32;
char const * format = "%8lx:";
switch(size)
{
case 64:
format = "%16lx:";
break;
case 20:
format = "%5lx:";
break;
default:
break;
}
printf(format, address);
}
static void _print_immediate(ArchOperand * ao) static void _print_immediate(ArchOperand * ao)
{ {
printf("%s$0x%lx", ao->value.immediate.negative ? "-" : "", printf("%s$0x%lx", ao->value.immediate.negative ? "-" : "",