X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fhost_dasm.c;h=24af1b8f59480371dcd08082800df3926f150a2c;hb=a7a40556a936f03daa01b9723c096dad76ca79cd;hp=99476891eb877fbbf51de1d216b19639de96ec16;hpb=6ad8f1d68665a1d6becba9c8fdfda7dbac38db54;p=libpicofe.git diff --git a/linux/host_dasm.c b/linux/host_dasm.c index 9947689..24af1b8 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,23 +22,48 @@ extern char **g_argv; static struct disassemble_info di; -#ifdef ARM +#if defined __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 __aarch64__ +#define print_insn_func print_insn_aarch64 +#define BFD_ARCH bfd_arch_aarch64 +#define BFD_MACH bfd_mach_aarch64 +#define DASM_OPTS NULL +#elif defined __mips__ +#define print_insn_func print_insn_little_mips +#define BFD_ARCH bfd_arch_mips +#define BFD_MACH bfd_mach_mipsisa32 +#define DASM_OPTS NULL +#elif defined __riscv +#define print_insn_func print_insn_riscv +#define BFD_ARCH bfd_arch_riscv +#define BFD_MACH bfd_mach_riscv64 +#define DASM_OPTS NULL +#elif defined __powerpc__ +#define print_insn_func print_insn_little_powerpc +#define BFD_ARCH bfd_arch_powerpc +#define BFD_MACH bfd_mach_ppc64 +#define DASM_OPTS NULL +#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 - -/* hacks for ARM */ -int floatformat_to_double; -int floatformat_ieee_single_little; +#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. @@ -41,7 +77,7 @@ remove_useless_symbols (asymbol **symbols, long count) { asymbol *sym = *in_ptr++; - if (sym->name == NULL || sym->name[0] == '\0') + if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$') continue; if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) continue; @@ -57,13 +93,13 @@ remove_useless_symbols (asymbol **symbols, long count) */ *out_ptr++ = sym; } + return out_ptr - symbols; } static void slurp_symtab(const char *filename) { bfd *abfd; - long storage; symcount = 0; @@ -79,11 +115,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; @@ -94,17 +133,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; } @@ -112,25 +168,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, ...) @@ -147,6 +197,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); @@ -158,24 +209,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++; +} +