5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
15 #include "host_dasm.h"
19 static struct disassemble_info di;
22 #define print_insn_func print_insn_little_arm
23 #define BFD_ARCH bfd_arch_arm
24 #define BFD_MACH bfd_mach_arm_4T
26 #define print_insn_func print_insn_i386_intel
27 #define BFD_ARCH bfd_arch_i386
28 #define BFD_MACH bfd_mach_i386_i386_intel_syntax
32 static asymbol **symbols;
33 static long symcount, symstorage;
36 /* Filter out (in place) symbols that are useless for disassembly.
37 COUNT is the number of elements in SYMBOLS.
38 Return the number of useful symbols. */
40 remove_useless_symbols (asymbol **symbols, long count)
42 asymbol **in_ptr = symbols, **out_ptr = symbols;
46 asymbol *sym = *in_ptr++;
48 if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$')
50 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
52 if (bfd_is_und_section (sym->section)
53 || bfd_is_com_section (sym->section))
55 if (sym->value + sym->section->vma == 0)
58 printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
59 (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
60 (unsigned int)sym->section->vma, sym->name);
65 return out_ptr - symbols;
68 static void slurp_symtab(const char *filename)
74 abfd = bfd_openr(filename, NULL);
76 fprintf(stderr, "failed to open: %s\n", filename);
80 if (!bfd_check_format(abfd, bfd_object))
83 if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
86 symstorage = bfd_get_symtab_upper_bound(abfd);
90 symbols = malloc(symstorage);
94 symcount = bfd_canonicalize_symtab(abfd, symbols);
98 symcount = remove_useless_symbols(symbols, symcount);
103 fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
111 static const char *lookup_name(bfd_vma addr)
113 asymbol **sptr = symbols;
116 for (i = 0; i < symcount; i++) {
117 asymbol *sym = *sptr++;
119 if (addr == sym->value + sym->section->vma)
126 /* Like target_read_memory, but slightly different parameters. */
128 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
129 struct disassemble_info *info)
131 memcpy(myaddr, (void *)(int)memaddr, len);
136 dis_asm_memory_error(int status, bfd_vma memaddr,
137 struct disassemble_info *info)
139 fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
143 dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
147 printf("%08x", (int)addr);
149 name = lookup_name(addr);
151 printf(" <%s>", name);
154 static int insn_printf(void *f, const char *format, ...)
159 va_start(args, format);
160 n = vprintf(format, args);
166 static void host_dasm_init(void)
169 slurp_symtab(g_argv[0]);
171 init_disassemble_info(&di, NULL, insn_printf);
172 di.flavour = bfd_target_unknown_flavour;
173 di.memory_error_func = dis_asm_memory_error;
174 di.print_address_func = dis_asm_print_address;
175 // di.symbol_at_address_func = dis_asm_symbol_at_address;
176 di.read_memory_func = dis_asm_read_memory;
179 di.endian = BFD_ENDIAN_LITTLE;
180 disassemble_init_for_target(&di);
184 void host_dasm(void *addr, int len)
186 bfd_vma vma_end, vma = (bfd_vma)(long)addr;
193 while (vma < vma_end) {
194 name = lookup_name(vma);
196 printf("%s:\n", name);
198 printf(" %08lx ", (long)vma);
199 vma += print_insn_func(vma, &di);
204 void host_dasm_new_symbol_(void *addr, const char *name)
206 bfd_vma vma = (bfd_vma)(long)addr;
213 if (symstorage <= symcount * sizeof(symbols[0])) {
214 tmp = realloc(symbols, symstorage * 2);
221 symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
222 if (symbols[symcount] == NULL)
225 // a HACK (should use correct section), but ohwell
226 sym = symbols[symcount];
227 sym->section = symbols[0]->section;
228 sym->value = vma - sym->section->vma;