X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fhost_dasm.c;h=d35bc9f56497aeaad6284f2d4b0bdcfdab5f4ba6;hb=f89d84717ae3536779f04cdfb57cf940d2bd8ade;hp=d2d6d0a9bf4b6252fdc085ea59b5686d9b041d59;hpb=61d87c7eddb013b1ca1275ffc2c2e3a51d5136a7;p=libpicofe.git diff --git a/linux/host_dasm.c b/linux/host_dasm.c index d2d6d0a..d35bc9f 100644 --- a/linux/host_dasm.c +++ b/linux/host_dasm.c @@ -1,3 +1,14 @@ +/* + * (C) Gražvydas "notaz" Ignotas, 2009-2010 + * + * This work is licensed under the terms of any of these licenses + * (at your option): + * - GNU GPL, version 2 or later. + * - GNU LGPL, version 2.1 or later. + * - MAME license. + * See the COPYING file in the top-level directory. + */ + #include #include #include @@ -11,7 +22,7 @@ extern char **g_argv; static struct disassemble_info di; -#ifdef ARM +#ifdef __arm__ #define print_insn_func print_insn_little_arm #define BFD_ARCH bfd_arch_arm #define BFD_MACH bfd_mach_arm_4T @@ -23,7 +34,8 @@ static struct disassemble_info di; /* 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. COUNT is the number of elements in SYMBOLS. @@ -60,7 +72,6 @@ remove_useless_symbols (asymbol **symbols, long count) static void slurp_symtab(const char *filename) { bfd *abfd; - long storage; symcount = 0; @@ -76,11 +87,14 @@ static void slurp_symtab(const char *filename) if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) goto no_symbols; - storage = bfd_get_symtab_upper_bound(abfd); - if (storage <= 0) + symstorage = bfd_get_symtab_upper_bound(abfd); + if (symstorage <= 0) + goto no_symbols; + + symbols = malloc(symstorage); + if (symbols == NULL) goto no_symbols; - symbols = malloc(storage); symcount = bfd_canonicalize_symtab(abfd, symbols); if (symcount < 0) goto no_symbols; @@ -91,10 +105,27 @@ static void slurp_symtab(const char *filename) no_symbols: fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); + if (symbols != NULL) + free(symbols); + symbols = NULL; if (abfd != NULL) 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. */ static int @@ -115,19 +146,13 @@ dis_asm_memory_error(int status, bfd_vma memaddr, static void dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) { - asymbol **sptr = symbols; - int i; + const char *name; printf("%08x", (int)addr); - for (i = 0; i < symcount; i++) { - asymbol *sym = *sptr++; - - if (addr == sym->value + sym->section->vma) { - printf(" <%s>", sym->name); - break; - } - } + name = lookup_name(addr); + if (name != NULL) + printf(" <%s>", name); } static int insn_printf(void *f, const char *format, ...) @@ -157,23 +182,55 @@ static void host_dasm_init(void) di.mach = BFD_MACH; di.endian = BFD_ENDIAN_LITTLE; disassemble_init_for_target(&di); + init_done = 1; } void host_dasm(void *addr, int len) { - bfd_vma vma_end, vma = (bfd_vma)(int)addr; - static int init_done = 0; + bfd_vma vma_end, vma = (bfd_vma)(long)addr; + const char *name; - if (!init_done) { + if (!init_done) host_dasm_init(); - init_done = 1; - } vma_end = vma + len; while (vma < vma_end) { - printf(" %p ", (void *)(long)vma); + name = lookup_name(vma); + if (name != NULL) + printf("%s:\n", name); + + printf(" %08lx ", (long)vma); vma += print_insn_func(vma, &di); 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++; +} +