2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
19 #include "host_dasm.h"
23 static struct disassemble_info di;
26 #define print_insn_func print_insn_little_arm
27 #define BFD_ARCH bfd_arch_arm
28 #define BFD_MACH bfd_mach_arm_4T
30 #define print_insn_func print_insn_i386_intel
31 #define BFD_ARCH bfd_arch_i386
32 #define BFD_MACH bfd_mach_i386_i386_intel_syntax
36 static asymbol **symbols;
37 static long symcount, symstorage;
40 /* Filter out (in place) symbols that are useless for disassembly.
41 COUNT is the number of elements in SYMBOLS.
42 Return the number of useful symbols. */
44 remove_useless_symbols (asymbol **symbols, long count)
46 asymbol **in_ptr = symbols, **out_ptr = symbols;
50 asymbol *sym = *in_ptr++;
52 if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$')
54 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
56 if (bfd_is_und_section (sym->section)
57 || bfd_is_com_section (sym->section))
59 if (sym->value + sym->section->vma == 0)
62 printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
63 (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
64 (unsigned int)sym->section->vma, sym->name);
69 return out_ptr - symbols;
72 static void slurp_symtab(const char *filename)
78 abfd = bfd_openr(filename, NULL);
80 fprintf(stderr, "failed to open: %s\n", filename);
84 if (!bfd_check_format(abfd, bfd_object))
87 if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
90 symstorage = bfd_get_symtab_upper_bound(abfd);
94 symbols = malloc(symstorage);
98 symcount = bfd_canonicalize_symtab(abfd, symbols);
102 symcount = remove_useless_symbols(symbols, symcount);
107 fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
115 static const char *lookup_name(bfd_vma addr)
117 asymbol **sptr = symbols;
120 for (i = 0; i < symcount; i++) {
121 asymbol *sym = *sptr++;
123 if (addr == sym->value + sym->section->vma)
130 /* Like target_read_memory, but slightly different parameters. */
132 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
133 struct disassemble_info *info)
135 memcpy(myaddr, (void *)(int)memaddr, len);
140 dis_asm_memory_error(int status, bfd_vma memaddr,
141 struct disassemble_info *info)
143 fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
147 dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
151 printf("%08x", (int)addr);
153 name = lookup_name(addr);
155 printf(" <%s>", name);
158 static int insn_printf(void *f, const char *format, ...)
163 va_start(args, format);
164 n = vprintf(format, args);
170 static void host_dasm_init(void)
173 slurp_symtab(g_argv[0]);
175 init_disassemble_info(&di, NULL, insn_printf);
176 di.flavour = bfd_target_unknown_flavour;
177 di.memory_error_func = dis_asm_memory_error;
178 di.print_address_func = dis_asm_print_address;
179 // di.symbol_at_address_func = dis_asm_symbol_at_address;
180 di.read_memory_func = dis_asm_read_memory;
183 di.endian = BFD_ENDIAN_LITTLE;
184 disassemble_init_for_target(&di);
188 void host_dasm(void *addr, int len)
190 bfd_vma vma_end, vma = (bfd_vma)(long)addr;
197 while (vma < vma_end) {
198 name = lookup_name(vma);
200 printf("%s:\n", name);
202 printf(" %08lx ", (long)vma);
203 vma += print_insn_func(vma, &di);
208 void host_dasm_new_symbol_(void *addr, const char *name)
210 bfd_vma vma = (bfd_vma)(long)addr;
217 if (symstorage <= symcount * sizeof(symbols[0])) {
218 tmp = realloc(symbols, symstorage * 2);
225 symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
226 if (symbols[symcount] == NULL)
229 // a HACK (should use correct section), but ohwell
230 sym = symbols[symcount];
231 sym->section = symbols[0]->section;
232 sym->value = vma - sym->section->vma;