X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fhost_dasm.c;h=66a83ea9ff95548c5f173b1593a977d9ffbae1e3;hb=21082d0b2b9910727770674cef9b68b9e97a3155;hp=3cc9c7b7dca37de4033c5c697ddfe6290c9577e0;hpb=c908877fcdddab2db3e70e674fd54f80d19a9dac;p=libpicofe.git diff --git a/linux/host_dasm.c b/linux/host_dasm.c index 3cc9c7b..66a83ea 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,19 +22,28 @@ 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 -#else +#define BFD_MACH bfd_mach_arm_unknown +#define DASM_OPTS "reg-names-std" +#elif defined(__x86_64__) || defined(__i386__) #define print_insn_func print_insn_i386_intel #define BFD_ARCH bfd_arch_i386 +#ifdef __x86_64__ +#define BFD_MACH bfd_mach_x86_64_intel_syntax +#else #define BFD_MACH bfd_mach_i386_i386_intel_syntax #endif +#define DASM_OPTS NULL +#else +#error "missing arch support" +#endif /* 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 +80,6 @@ remove_useless_symbols (asymbol **symbols, long count) static void slurp_symtab(const char *filename) { bfd *abfd; - long storage; symcount = 0; @@ -76,11 +95,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,17 +113,34 @@ 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 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len, struct disassemble_info *info) { - memcpy(myaddr, (void *)(int)memaddr, len); + memcpy(myaddr, (void *)(long)memaddr, len); return 0; } @@ -109,25 +148,19 @@ static void dis_asm_memory_error(int status, bfd_vma memaddr, struct disassemble_info *info) { - fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr); + fprintf(stderr, "memory_error %p\n", (void *)(long)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, ...) @@ -144,6 +177,7 @@ static int insn_printf(void *f, const char *format, ...) static void host_dasm_init(void) { + bfd_init(); slurp_symtab(g_argv[0]); init_disassemble_info(&di, NULL, insn_printf); @@ -155,24 +189,57 @@ static void host_dasm_init(void) di.arch = BFD_ARCH; di.mach = BFD_MACH; di.endian = BFD_ENDIAN_LITTLE; + di.disassembler_options = DASM_OPTS; 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++; +} +