12 static struct disassemble_info di;
15 #define print_insn_func print_insn_little_arm
16 #define BFD_ARCH bfd_arch_arm
17 #define BFD_MACH bfd_mach_arm_4T
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
25 static asymbol **symbols;
28 /* Filter out (in place) symbols that are useless for disassembly.
29 COUNT is the number of elements in SYMBOLS.
30 Return the number of useful symbols. */
32 remove_useless_symbols (asymbol **symbols, long count)
34 asymbol **in_ptr = symbols, **out_ptr = symbols;
38 asymbol *sym = *in_ptr++;
40 if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$')
42 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
44 if (bfd_is_und_section (sym->section)
45 || bfd_is_com_section (sym->section))
47 if (sym->value + sym->section->vma == 0)
50 printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
51 (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
52 (unsigned int)sym->section->vma, sym->name);
57 return out_ptr - symbols;
60 static void slurp_symtab(const char *filename)
67 abfd = bfd_openr(filename, NULL);
69 fprintf(stderr, "failed to open: %s\n", filename);
73 if (!bfd_check_format(abfd, bfd_object))
76 if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
79 storage = bfd_get_symtab_upper_bound(abfd);
83 symbols = malloc(storage);
84 symcount = bfd_canonicalize_symtab(abfd, symbols);
88 symcount = remove_useless_symbols(symbols, symcount);
93 fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
99 /* Like target_read_memory, but slightly different parameters. */
101 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
102 struct disassemble_info *info)
104 memcpy(myaddr, (void *)(int)memaddr, len);
109 dis_asm_memory_error(int status, bfd_vma memaddr,
110 struct disassemble_info *info)
112 fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
116 dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
118 asymbol **sptr = symbols;
121 printf("%08x", (int)addr);
123 for (i = 0; i < symcount; i++) {
124 asymbol *sym = *sptr++;
126 if (addr == sym->value + sym->section->vma) {
127 printf(" <%s>", sym->name);
133 static int insn_printf(void *f, const char *format, ...)
138 va_start(args, format);
139 n = vprintf(format, args);
145 static void host_dasm_init(void)
147 slurp_symtab(g_argv[0]);
149 init_disassemble_info(&di, NULL, insn_printf);
150 di.flavour = bfd_target_unknown_flavour;
151 di.memory_error_func = dis_asm_memory_error;
152 di.print_address_func = dis_asm_print_address;
153 // di.symbol_at_address_func = dis_asm_symbol_at_address;
154 di.read_memory_func = dis_asm_read_memory;
157 di.endian = BFD_ENDIAN_LITTLE;
158 disassemble_init_for_target(&di);
161 void host_dasm(void *addr, int len)
163 bfd_vma vma_end, vma = (bfd_vma)(int)addr;
164 static int init_done = 0;
172 while (vma < vma_end) {
173 printf(" %p ", (void *)(long)vma);
174 vma += print_insn_func(vma, &di);