12 static struct disassemble_info di;
15 static asymbol **symbols;
18 /* Filter out (in place) symbols that are useless for disassembly.
19 COUNT is the number of elements in SYMBOLS.
20 Return the number of useful symbols. */
22 remove_useless_symbols (asymbol **symbols, long count)
24 asymbol **in_ptr = symbols, **out_ptr = symbols;
28 asymbol *sym = *in_ptr++;
30 if (sym->name == NULL || sym->name[0] == '\0')
32 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
34 if (bfd_is_und_section (sym->section)
35 || bfd_is_com_section (sym->section))
37 if (sym->value + sym->section->vma == 0)
40 printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
41 (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
42 (unsigned int)sym->section->vma, sym->name);
46 return out_ptr - symbols;
49 static void slurp_symtab(const char *filename)
56 abfd = bfd_openr(filename, NULL);
58 fprintf(stderr, "failed to open: %s\n", filename);
62 if (!bfd_check_format(abfd, bfd_object))
65 if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
68 storage = bfd_get_symtab_upper_bound(abfd);
72 symbols = malloc(storage);
73 symcount = bfd_canonicalize_symtab(abfd, symbols);
77 symcount = remove_useless_symbols(symbols, symcount);
82 fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
88 /* Like target_read_memory, but slightly different parameters. */
90 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
91 struct disassemble_info *info)
93 memcpy(myaddr, (void *)(int)memaddr, len);
98 dis_asm_memory_error(int status, bfd_vma memaddr,
99 struct disassemble_info *info)
101 fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
105 dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
107 asymbol **sptr = symbols;
110 printf("%08x", (int)addr);
112 for (i = 0; i < symcount; i++) {
113 asymbol *sym = *sptr++;
115 if (addr == sym->value + sym->section->vma) {
116 printf(" <%s>", sym->name);
122 static int insn_printf(void *f, const char *format, ...)
127 va_start(args, format);
128 n = vprintf(format, args);
134 static void host_dasm_init(void)
136 slurp_symtab(g_argv[0]);
138 init_disassemble_info(&di, NULL, insn_printf);
139 di.flavour = bfd_target_unknown_flavour;
140 di.memory_error_func = dis_asm_memory_error;
141 di.print_address_func = dis_asm_print_address;
142 // di.symbol_at_address_func = dis_asm_symbol_at_address;
143 di.read_memory_func = dis_asm_read_memory;
144 di.arch = bfd_arch_i386;
145 di.mach = bfd_mach_i386_i386_intel_syntax;
146 di.endian = BFD_ENDIAN_LITTLE;
147 disassemble_init_for_target(&di);
150 void host_dasm(void *addr, int len)
152 bfd_vma vma_end, vma = (bfd_vma)(int)addr;
153 static int init_done = 0;
161 while (vma < vma_end) {
162 printf(" %p ", (void *)(long)vma);
163 vma += print_insn_i386_intel(vma, &di);