allow to add symbols for host_dasm

git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@848 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
notaz 2009-12-27 23:21:25 +00:00
parent b2c5ee473d
commit e8fc349e71
2 changed files with 69 additions and 21 deletions

View file

@ -23,7 +23,8 @@ static struct disassemble_info di;
/* symbols */ /* symbols */
static asymbol **symbols; static asymbol **symbols;
static long symcount; static long symcount, symstorage;
static int init_done;
/* Filter out (in place) symbols that are useless for disassembly. /* Filter out (in place) symbols that are useless for disassembly.
COUNT is the number of elements in SYMBOLS. COUNT is the number of elements in SYMBOLS.
@ -60,7 +61,6 @@ remove_useless_symbols (asymbol **symbols, long count)
static void slurp_symtab(const char *filename) static void slurp_symtab(const char *filename)
{ {
bfd *abfd; bfd *abfd;
long storage;
symcount = 0; symcount = 0;
@ -76,11 +76,14 @@ static void slurp_symtab(const char *filename)
if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
goto no_symbols; goto no_symbols;
storage = bfd_get_symtab_upper_bound(abfd); symstorage = bfd_get_symtab_upper_bound(abfd);
if (storage <= 0) if (symstorage <= 0)
goto no_symbols;
symbols = malloc(symstorage);
if (symbols == NULL)
goto no_symbols; goto no_symbols;
symbols = malloc(storage);
symcount = bfd_canonicalize_symtab(abfd, symbols); symcount = bfd_canonicalize_symtab(abfd, symbols);
if (symcount < 0) if (symcount < 0)
goto no_symbols; goto no_symbols;
@ -91,10 +94,27 @@ static void slurp_symtab(const char *filename)
no_symbols: no_symbols:
fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
if (symbols != NULL)
free(symbols);
symbols = NULL;
if (abfd != NULL) if (abfd != NULL)
bfd_close(abfd); bfd_close(abfd);
} }
static const char *lookup_name(bfd_vma addr)
{
asymbol **sptr = symbols;
int i;
for (i = 0; i < symcount; i++) {
asymbol *sym = *sptr++;
if (addr == sym->value + sym->section->vma)
return sym->name;
}
return NULL;
}
/* Like target_read_memory, but slightly different parameters. */ /* Like target_read_memory, but slightly different parameters. */
static int static int
@ -115,19 +135,13 @@ dis_asm_memory_error(int status, bfd_vma memaddr,
static void static void
dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
{ {
asymbol **sptr = symbols; const char *name;
int i;
printf("%08x", (int)addr); printf("%08x", (int)addr);
for (i = 0; i < symcount; i++) { name = lookup_name(addr);
asymbol *sym = *sptr++; if (name != NULL)
printf(" <%s>", name);
if (addr == sym->value + sym->section->vma) {
printf(" <%s>", sym->name);
break;
}
}
} }
static int insn_printf(void *f, const char *format, ...) static int insn_printf(void *f, const char *format, ...)
@ -157,23 +171,55 @@ static void host_dasm_init(void)
di.mach = BFD_MACH; di.mach = BFD_MACH;
di.endian = BFD_ENDIAN_LITTLE; di.endian = BFD_ENDIAN_LITTLE;
disassemble_init_for_target(&di); disassemble_init_for_target(&di);
init_done = 1;
} }
void host_dasm(void *addr, int len) void host_dasm(void *addr, int len)
{ {
bfd_vma vma_end, vma = (bfd_vma)(int)addr; bfd_vma vma_end, vma = (bfd_vma)(long)addr;
static int init_done = 0; const char *name;
if (!init_done) { if (!init_done)
host_dasm_init(); host_dasm_init();
init_done = 1;
}
vma_end = vma + len; vma_end = vma + len;
while (vma < vma_end) { while (vma < vma_end) {
name = lookup_name(vma);
if (name != NULL)
printf("%s:\n", name);
printf(" %p ", (void *)(long)vma); printf(" %p ", (void *)(long)vma);
vma += print_insn_func(vma, &di); vma += print_insn_func(vma, &di);
printf("\n"); printf("\n");
} }
} }
void host_dasm_new_symbol_(void *addr, const char *name)
{
bfd_vma vma = (bfd_vma)(long)addr;
asymbol *sym, **tmp;
if (!init_done)
host_dasm_init();
if (symbols == NULL)
return;
if (symstorage <= symcount * sizeof(symbols[0])) {
tmp = realloc(symbols, symstorage * 2);
if (tmp == NULL)
return;
symstorage *= 2;
symbols = tmp;
}
symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
if (symbols[symcount] == NULL)
return;
// a HACK (should use correct section), but ohwell
sym = symbols[symcount];
sym->section = symbols[0]->section;
sym->value = vma - sym->section->vma;
sym->name = name;
symcount++;
}

View file

@ -1,2 +1,4 @@
void host_dasm(void *addr, int len); void host_dasm(void *addr, int len);
void host_dasm_new_symbol_(void *addr, const char *name);
#define host_dasm_new_symbol(sym) \
host_dasm_new_symbol_(sym, #sym)