| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <bfd.h> |
| 6 | #include <dis-asm.h> |
| 7 | |
| 8 | #include "host_dasm.h" |
| 9 | |
| 10 | extern char **g_argv; |
| 11 | |
| 12 | static struct disassemble_info di; |
| 13 | |
| 14 | #ifdef ARM |
| 15 | #define print_insn_func print_insn_little_arm |
| 16 | #define BFD_ARCH bfd_arch_arm |
| 17 | #define BFD_MACH bfd_mach_arm_4T |
| 18 | #else |
| 19 | #define print_insn_func print_insn_i386_intel |
| 20 | #define BFD_ARCH bfd_arch_i386 |
| 21 | #define BFD_MACH bfd_mach_i386_i386_intel_syntax |
| 22 | #endif |
| 23 | |
| 24 | /* symbols */ |
| 25 | static asymbol **symbols; |
| 26 | static long symcount, symstorage; |
| 27 | static int init_done; |
| 28 | |
| 29 | /* Filter out (in place) symbols that are useless for disassembly. |
| 30 | COUNT is the number of elements in SYMBOLS. |
| 31 | Return the number of useful symbols. */ |
| 32 | static long |
| 33 | remove_useless_symbols (asymbol **symbols, long count) |
| 34 | { |
| 35 | asymbol **in_ptr = symbols, **out_ptr = symbols; |
| 36 | |
| 37 | while (--count >= 0) |
| 38 | { |
| 39 | asymbol *sym = *in_ptr++; |
| 40 | |
| 41 | if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$') |
| 42 | continue; |
| 43 | if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) |
| 44 | continue; |
| 45 | if (bfd_is_und_section (sym->section) |
| 46 | || bfd_is_com_section (sym->section)) |
| 47 | continue; |
| 48 | if (sym->value + sym->section->vma == 0) |
| 49 | continue; |
| 50 | /* |
| 51 | printf("sym: %08lx %04x %08x v %08x \"%s\"\n", |
| 52 | (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i, |
| 53 | (unsigned int)sym->section->vma, sym->name); |
| 54 | */ |
| 55 | *out_ptr++ = sym; |
| 56 | } |
| 57 | |
| 58 | return out_ptr - symbols; |
| 59 | } |
| 60 | |
| 61 | static void slurp_symtab(const char *filename) |
| 62 | { |
| 63 | bfd *abfd; |
| 64 | |
| 65 | symcount = 0; |
| 66 | |
| 67 | abfd = bfd_openr(filename, NULL); |
| 68 | if (abfd == NULL) { |
| 69 | fprintf(stderr, "failed to open: %s\n", filename); |
| 70 | goto no_symbols; |
| 71 | } |
| 72 | |
| 73 | if (!bfd_check_format(abfd, bfd_object)) |
| 74 | goto no_symbols; |
| 75 | |
| 76 | if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) |
| 77 | goto no_symbols; |
| 78 | |
| 79 | symstorage = bfd_get_symtab_upper_bound(abfd); |
| 80 | if (symstorage <= 0) |
| 81 | goto no_symbols; |
| 82 | |
| 83 | symbols = malloc(symstorage); |
| 84 | if (symbols == NULL) |
| 85 | goto no_symbols; |
| 86 | |
| 87 | symcount = bfd_canonicalize_symtab(abfd, symbols); |
| 88 | if (symcount < 0) |
| 89 | goto no_symbols; |
| 90 | |
| 91 | symcount = remove_useless_symbols(symbols, symcount); |
| 92 | // bfd_close(abfd); |
| 93 | return; |
| 94 | |
| 95 | no_symbols: |
| 96 | fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); |
| 97 | if (symbols != NULL) |
| 98 | free(symbols); |
| 99 | symbols = NULL; |
| 100 | if (abfd != NULL) |
| 101 | bfd_close(abfd); |
| 102 | } |
| 103 | |
| 104 | static const char *lookup_name(bfd_vma addr) |
| 105 | { |
| 106 | asymbol **sptr = symbols; |
| 107 | int i; |
| 108 | |
| 109 | for (i = 0; i < symcount; i++) { |
| 110 | asymbol *sym = *sptr++; |
| 111 | |
| 112 | if (addr == sym->value + sym->section->vma) |
| 113 | return sym->name; |
| 114 | } |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /* Like target_read_memory, but slightly different parameters. */ |
| 120 | static int |
| 121 | dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len, |
| 122 | struct disassemble_info *info) |
| 123 | { |
| 124 | memcpy(myaddr, (void *)(int)memaddr, len); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | dis_asm_memory_error(int status, bfd_vma memaddr, |
| 130 | struct disassemble_info *info) |
| 131 | { |
| 132 | fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) |
| 137 | { |
| 138 | const char *name; |
| 139 | |
| 140 | printf("%08x", (int)addr); |
| 141 | |
| 142 | name = lookup_name(addr); |
| 143 | if (name != NULL) |
| 144 | printf(" <%s>", name); |
| 145 | } |
| 146 | |
| 147 | static int insn_printf(void *f, const char *format, ...) |
| 148 | { |
| 149 | va_list args; |
| 150 | size_t n; |
| 151 | |
| 152 | va_start(args, format); |
| 153 | n = vprintf(format, args); |
| 154 | va_end(args); |
| 155 | |
| 156 | return n; |
| 157 | } |
| 158 | |
| 159 | static void host_dasm_init(void) |
| 160 | { |
| 161 | bfd_init(); |
| 162 | slurp_symtab(g_argv[0]); |
| 163 | |
| 164 | init_disassemble_info(&di, NULL, insn_printf); |
| 165 | di.flavour = bfd_target_unknown_flavour; |
| 166 | di.memory_error_func = dis_asm_memory_error; |
| 167 | di.print_address_func = dis_asm_print_address; |
| 168 | // di.symbol_at_address_func = dis_asm_symbol_at_address; |
| 169 | di.read_memory_func = dis_asm_read_memory; |
| 170 | di.arch = BFD_ARCH; |
| 171 | di.mach = BFD_MACH; |
| 172 | di.endian = BFD_ENDIAN_LITTLE; |
| 173 | disassemble_init_for_target(&di); |
| 174 | init_done = 1; |
| 175 | } |
| 176 | |
| 177 | void host_dasm(void *addr, int len) |
| 178 | { |
| 179 | bfd_vma vma_end, vma = (bfd_vma)(long)addr; |
| 180 | const char *name; |
| 181 | |
| 182 | if (!init_done) |
| 183 | host_dasm_init(); |
| 184 | |
| 185 | vma_end = vma + len; |
| 186 | while (vma < vma_end) { |
| 187 | name = lookup_name(vma); |
| 188 | if (name != NULL) |
| 189 | printf("%s:\n", name); |
| 190 | |
| 191 | printf(" %p ", (void *)(long)vma); |
| 192 | vma += print_insn_func(vma, &di); |
| 193 | printf("\n"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void host_dasm_new_symbol_(void *addr, const char *name) |
| 198 | { |
| 199 | bfd_vma vma = (bfd_vma)(long)addr; |
| 200 | asymbol *sym, **tmp; |
| 201 | |
| 202 | if (!init_done) |
| 203 | host_dasm_init(); |
| 204 | if (symbols == NULL) |
| 205 | return; |
| 206 | if (symstorage <= symcount * sizeof(symbols[0])) { |
| 207 | tmp = realloc(symbols, symstorage * 2); |
| 208 | if (tmp == NULL) |
| 209 | return; |
| 210 | symstorage *= 2; |
| 211 | symbols = tmp; |
| 212 | } |
| 213 | |
| 214 | symbols[symcount] = calloc(sizeof(*symbols[0]), 1); |
| 215 | if (symbols[symcount] == NULL) |
| 216 | return; |
| 217 | |
| 218 | // a HACK (should use correct section), but ohwell |
| 219 | sym = symbols[symcount]; |
| 220 | sym->section = symbols[0]->section; |
| 221 | sym->value = vma - sym->section->vma; |
| 222 | sym->name = name; |
| 223 | symcount++; |
| 224 | } |
| 225 | |