| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * - MAME license. |
| 9 | * See the COPYING file in the top-level directory. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <stdarg.h> |
| 16 | #include <bfd.h> |
| 17 | #include <dis-asm.h> |
| 18 | |
| 19 | #include "host_dasm.h" |
| 20 | |
| 21 | extern char **g_argv; |
| 22 | |
| 23 | static struct disassemble_info di; |
| 24 | |
| 25 | #if defined __arm__ |
| 26 | #define print_insn_func print_insn_little_arm |
| 27 | #define BFD_ARCH bfd_arch_arm |
| 28 | #define BFD_MACH bfd_mach_arm_unknown |
| 29 | #define DASM_OPTS "reg-names-std" |
| 30 | #elif defined __aarch64__ |
| 31 | #define print_insn_func print_insn_aarch64 |
| 32 | #define BFD_ARCH bfd_arch_aarch64 |
| 33 | #define BFD_MACH bfd_mach_aarch64 |
| 34 | #define DASM_OPTS NULL |
| 35 | #elif defined __mips__ |
| 36 | #define print_insn_func print_insn_little_mips |
| 37 | #define BFD_ARCH bfd_arch_mips |
| 38 | #define BFD_MACH bfd_mach_mipsisa32 |
| 39 | #define DASM_OPTS NULL |
| 40 | #elif defined __riscv |
| 41 | #define print_insn_func print_insn_riscv |
| 42 | #define BFD_ARCH bfd_arch_riscv |
| 43 | #define BFD_MACH bfd_mach_riscv64 |
| 44 | #define DASM_OPTS NULL |
| 45 | #elif defined __powerpc__ |
| 46 | #define print_insn_func print_insn_little_powerpc |
| 47 | #define BFD_ARCH bfd_arch_powerpc |
| 48 | #define BFD_MACH bfd_mach_ppc64 |
| 49 | #define DASM_OPTS NULL |
| 50 | #elif defined(__x86_64__) || defined(__i386__) |
| 51 | #define print_insn_func print_insn_i386_intel |
| 52 | #define BFD_ARCH bfd_arch_i386 |
| 53 | #ifdef __x86_64__ |
| 54 | #define BFD_MACH bfd_mach_x86_64_intel_syntax |
| 55 | #else |
| 56 | #define BFD_MACH bfd_mach_i386_i386_intel_syntax |
| 57 | #endif |
| 58 | #define DASM_OPTS NULL |
| 59 | #else |
| 60 | #error "missing arch support" |
| 61 | #endif |
| 62 | |
| 63 | /* symbols */ |
| 64 | static asymbol **symbols; |
| 65 | static long symcount, symstorage; |
| 66 | static int init_done; |
| 67 | |
| 68 | /* Filter out (in place) symbols that are useless for disassembly. |
| 69 | COUNT is the number of elements in SYMBOLS. |
| 70 | Return the number of useful symbols. */ |
| 71 | static long |
| 72 | remove_useless_symbols (asymbol **symbols, long count) |
| 73 | { |
| 74 | asymbol **in_ptr = symbols, **out_ptr = symbols; |
| 75 | |
| 76 | while (--count >= 0) |
| 77 | { |
| 78 | asymbol *sym = *in_ptr++; |
| 79 | |
| 80 | if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$') |
| 81 | continue; |
| 82 | if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) |
| 83 | continue; |
| 84 | if (bfd_is_und_section (sym->section) |
| 85 | || bfd_is_com_section (sym->section)) |
| 86 | continue; |
| 87 | if (sym->value + sym->section->vma == 0) |
| 88 | continue; |
| 89 | /* |
| 90 | printf("sym: %08lx %04x %08x v %08x \"%s\"\n", |
| 91 | (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i, |
| 92 | (unsigned int)sym->section->vma, sym->name); |
| 93 | */ |
| 94 | *out_ptr++ = sym; |
| 95 | } |
| 96 | |
| 97 | return out_ptr - symbols; |
| 98 | } |
| 99 | |
| 100 | static void slurp_symtab(const char *filename) |
| 101 | { |
| 102 | bfd *abfd; |
| 103 | |
| 104 | symcount = 0; |
| 105 | |
| 106 | abfd = bfd_openr(filename, NULL); |
| 107 | if (abfd == NULL) { |
| 108 | fprintf(stderr, "failed to open: %s\n", filename); |
| 109 | goto no_symbols; |
| 110 | } |
| 111 | |
| 112 | if (!bfd_check_format(abfd, bfd_object)) |
| 113 | goto no_symbols; |
| 114 | |
| 115 | if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) |
| 116 | goto no_symbols; |
| 117 | |
| 118 | symstorage = bfd_get_symtab_upper_bound(abfd); |
| 119 | if (symstorage <= 0) |
| 120 | goto no_symbols; |
| 121 | |
| 122 | symbols = malloc(symstorage); |
| 123 | if (symbols == NULL) |
| 124 | goto no_symbols; |
| 125 | |
| 126 | symcount = bfd_canonicalize_symtab(abfd, symbols); |
| 127 | if (symcount < 0) |
| 128 | goto no_symbols; |
| 129 | |
| 130 | symcount = remove_useless_symbols(symbols, symcount); |
| 131 | // bfd_close(abfd); |
| 132 | return; |
| 133 | |
| 134 | no_symbols: |
| 135 | fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); |
| 136 | if (symbols != NULL) |
| 137 | free(symbols); |
| 138 | symbols = NULL; |
| 139 | if (abfd != NULL) |
| 140 | bfd_close(abfd); |
| 141 | } |
| 142 | |
| 143 | static const char *lookup_name(bfd_vma addr) |
| 144 | { |
| 145 | asymbol **sptr = symbols; |
| 146 | int i; |
| 147 | |
| 148 | for (i = 0; i < symcount; i++) { |
| 149 | asymbol *sym = *sptr++; |
| 150 | |
| 151 | if (addr == sym->value + sym->section->vma) |
| 152 | return sym->name; |
| 153 | } |
| 154 | |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /* Like target_read_memory, but slightly different parameters. */ |
| 159 | static int |
| 160 | dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len, |
| 161 | struct disassemble_info *info) |
| 162 | { |
| 163 | memcpy(myaddr, (void *)(long)memaddr, len); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void |
| 168 | dis_asm_memory_error(int status, bfd_vma memaddr, |
| 169 | struct disassemble_info *info) |
| 170 | { |
| 171 | fprintf(stderr, "memory_error %p\n", (void *)(long)memaddr); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) |
| 176 | { |
| 177 | const char *name; |
| 178 | |
| 179 | printf("%08x", (int)addr); |
| 180 | |
| 181 | name = lookup_name(addr); |
| 182 | if (name != NULL) |
| 183 | printf(" <%s>", name); |
| 184 | } |
| 185 | |
| 186 | static int insn_printf(void *f, const char *format, ...) |
| 187 | { |
| 188 | va_list args; |
| 189 | size_t n; |
| 190 | |
| 191 | va_start(args, format); |
| 192 | n = vprintf(format, args); |
| 193 | va_end(args); |
| 194 | |
| 195 | return n; |
| 196 | } |
| 197 | |
| 198 | static void host_dasm_init(void) |
| 199 | { |
| 200 | bfd_init(); |
| 201 | slurp_symtab(g_argv[0]); |
| 202 | |
| 203 | init_disassemble_info(&di, NULL, insn_printf); |
| 204 | di.flavour = bfd_target_unknown_flavour; |
| 205 | di.memory_error_func = dis_asm_memory_error; |
| 206 | di.print_address_func = dis_asm_print_address; |
| 207 | // di.symbol_at_address_func = dis_asm_symbol_at_address; |
| 208 | di.read_memory_func = dis_asm_read_memory; |
| 209 | di.arch = BFD_ARCH; |
| 210 | di.mach = BFD_MACH; |
| 211 | di.endian = BFD_ENDIAN_LITTLE; |
| 212 | di.disassembler_options = DASM_OPTS; |
| 213 | disassemble_init_for_target(&di); |
| 214 | init_done = 1; |
| 215 | } |
| 216 | |
| 217 | void host_dasm(void *addr, int len) |
| 218 | { |
| 219 | bfd_vma vma_end, vma = (bfd_vma)(long)addr; |
| 220 | const char *name; |
| 221 | |
| 222 | if (!init_done) |
| 223 | host_dasm_init(); |
| 224 | |
| 225 | vma_end = vma + len; |
| 226 | while (vma < vma_end) { |
| 227 | name = lookup_name(vma); |
| 228 | if (name != NULL) |
| 229 | printf("%s:\n", name); |
| 230 | |
| 231 | printf(" %08lx ", (long)vma); |
| 232 | vma += print_insn_func(vma, &di); |
| 233 | printf("\n"); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void host_dasm_new_symbol_(void *addr, const char *name) |
| 238 | { |
| 239 | bfd_vma vma = (bfd_vma)(long)addr; |
| 240 | asymbol *sym, **tmp; |
| 241 | |
| 242 | if (!init_done) |
| 243 | host_dasm_init(); |
| 244 | if (symbols == NULL) |
| 245 | return; |
| 246 | if (symstorage <= symcount * sizeof(symbols[0])) { |
| 247 | tmp = realloc(symbols, symstorage * 2); |
| 248 | if (tmp == NULL) |
| 249 | return; |
| 250 | symstorage *= 2; |
| 251 | symbols = tmp; |
| 252 | } |
| 253 | |
| 254 | symbols[symcount] = calloc(sizeof(*symbols[0]), 1); |
| 255 | if (symbols[symcount] == NULL) |
| 256 | return; |
| 257 | |
| 258 | // a HACK (should use correct section), but ohwell |
| 259 | sym = symbols[symcount]; |
| 260 | sym->section = symbols[0]->section; |
| 261 | sym->value = vma - sym->section->vma; |
| 262 | sym->name = name; |
| 263 | symcount++; |
| 264 | } |
| 265 | |