X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fhost_dasm.c;h=66a83ea9ff95548c5f173b1593a977d9ffbae1e3;hb=HEAD;hp=d2d6d0a9bf4b6252fdc085ea59b5686d9b041d59;hpb=61d87c7eddb013b1ca1275ffc2c2e3a51d5136a7;p=libpicofe.git diff --git a/linux/host_dasm.c b/linux/host_dasm.c index d2d6d0a..a5e66c9 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 @@ -10,20 +21,50 @@ extern char **g_argv; static struct disassemble_info di; +static disassembler_ftype print_insn_func; -#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_mipsisa64r2 +#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 +#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 +101,6 @@ remove_useless_symbols (asymbol **symbols, long count) static void slurp_symtab(const char *filename) { bfd *abfd; - long storage; symcount = 0; @@ -76,11 +116,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 +134,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 *)memaddr, len); return 0; } @@ -109,25 +169,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, ...) @@ -142,10 +196,20 @@ static int insn_printf(void *f, const char *format, ...) return n; } +static int print_insn_hex(bfd_vma addr, struct disassemble_info *info) +{ + unsigned op; + + dis_asm_read_memory(addr, (bfd_byte *)&op, 4, info); + printf("%p %08lx",(void *)addr, (long)op); + return 4; +} + static void host_dasm_init(void) { bfd_init(); - slurp_symtab(g_argv[0]); + if (g_argv && g_argv[0]) + slurp_symtab(g_argv[0]); init_disassemble_info(&di, NULL, insn_printf); di.flavour = bfd_target_unknown_flavour; @@ -156,24 +220,61 @@ 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); +#ifndef print_insn_func + print_insn_func = disassembler(BFD_ARCH, 0, BFD_MACH, NULL); + if (!print_insn_func) print_insn_func = print_insn_hex; +#endif + 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)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++; +} +