aad81ad9 |
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 | |
6ad8f1d6 |
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 | |
aad81ad9 |
24 | /* symbols */ |
25 | static asymbol **symbols; |
26 | static long symcount; |
27 | |
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. */ |
31 | static long |
32 | remove_useless_symbols (asymbol **symbols, long count) |
33 | { |
34 | asymbol **in_ptr = symbols, **out_ptr = symbols; |
35 | |
36 | while (--count >= 0) |
37 | { |
38 | asymbol *sym = *in_ptr++; |
39 | |
40 | if (sym->name == NULL || sym->name[0] == '\0') |
41 | continue; |
42 | if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) |
43 | continue; |
44 | if (bfd_is_und_section (sym->section) |
45 | || bfd_is_com_section (sym->section)) |
46 | continue; |
47 | if (sym->value + sym->section->vma == 0) |
48 | continue; |
49 | /* |
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); |
53 | */ |
54 | *out_ptr++ = sym; |
55 | } |
56 | return out_ptr - symbols; |
57 | } |
58 | |
59 | static void slurp_symtab(const char *filename) |
60 | { |
61 | bfd *abfd; |
62 | long storage; |
63 | |
64 | symcount = 0; |
65 | |
66 | abfd = bfd_openr(filename, NULL); |
67 | if (abfd == NULL) { |
68 | fprintf(stderr, "failed to open: %s\n", filename); |
69 | goto no_symbols; |
70 | } |
71 | |
72 | if (!bfd_check_format(abfd, bfd_object)) |
73 | goto no_symbols; |
74 | |
75 | if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) |
76 | goto no_symbols; |
77 | |
78 | storage = bfd_get_symtab_upper_bound(abfd); |
79 | if (storage <= 0) |
80 | goto no_symbols; |
81 | |
82 | symbols = malloc(storage); |
83 | symcount = bfd_canonicalize_symtab(abfd, symbols); |
84 | if (symcount < 0) |
85 | goto no_symbols; |
86 | |
87 | symcount = remove_useless_symbols(symbols, symcount); |
88 | // bfd_close(abfd); |
89 | return; |
90 | |
91 | no_symbols: |
92 | fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd)); |
93 | if (abfd != NULL) |
94 | bfd_close(abfd); |
95 | } |
96 | |
97 | |
98 | /* Like target_read_memory, but slightly different parameters. */ |
99 | static int |
100 | dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len, |
101 | struct disassemble_info *info) |
102 | { |
103 | memcpy(myaddr, (void *)(int)memaddr, len); |
104 | return 0; |
105 | } |
106 | |
107 | static void |
108 | dis_asm_memory_error(int status, bfd_vma memaddr, |
109 | struct disassemble_info *info) |
110 | { |
111 | fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr); |
112 | } |
113 | |
114 | static void |
115 | dis_asm_print_address(bfd_vma addr, struct disassemble_info *info) |
116 | { |
117 | asymbol **sptr = symbols; |
118 | int i; |
119 | |
120 | printf("%08x", (int)addr); |
121 | |
122 | for (i = 0; i < symcount; i++) { |
123 | asymbol *sym = *sptr++; |
124 | |
125 | if (addr == sym->value + sym->section->vma) { |
126 | printf(" <%s>", sym->name); |
127 | break; |
128 | } |
129 | } |
130 | } |
131 | |
132 | static int insn_printf(void *f, const char *format, ...) |
133 | { |
134 | va_list args; |
135 | size_t n; |
136 | |
137 | va_start(args, format); |
138 | n = vprintf(format, args); |
139 | va_end(args); |
140 | |
141 | return n; |
142 | } |
143 | |
144 | static void host_dasm_init(void) |
145 | { |
146 | slurp_symtab(g_argv[0]); |
147 | |
148 | init_disassemble_info(&di, NULL, insn_printf); |
149 | di.flavour = bfd_target_unknown_flavour; |
150 | di.memory_error_func = dis_asm_memory_error; |
151 | di.print_address_func = dis_asm_print_address; |
152 | // di.symbol_at_address_func = dis_asm_symbol_at_address; |
153 | di.read_memory_func = dis_asm_read_memory; |
6ad8f1d6 |
154 | di.arch = BFD_ARCH; |
155 | di.mach = BFD_MACH; |
aad81ad9 |
156 | di.endian = BFD_ENDIAN_LITTLE; |
157 | disassemble_init_for_target(&di); |
158 | } |
159 | |
160 | void host_dasm(void *addr, int len) |
161 | { |
162 | bfd_vma vma_end, vma = (bfd_vma)(int)addr; |
163 | static int init_done = 0; |
164 | |
165 | if (!init_done) { |
166 | host_dasm_init(); |
167 | init_done = 1; |
168 | } |
169 | |
170 | vma_end = vma + len; |
171 | while (vma < vma_end) { |
172 | printf(" %p ", (void *)(long)vma); |
6ad8f1d6 |
173 | vma += print_insn_func(vma, &di); |
aad81ad9 |
174 | printf("\n"); |
175 | } |
176 | } |
177 | |