]>
Commit | Line | Data |
---|---|---|
b9adb4a6 | 1 | /* General "disassemble this chunk" code. Used for debugging. */ |
5bbe9299 | 2 | #include "config.h" |
b9adb4a6 | 3 | #include "dis-asm.h" |
b9adb4a6 | 4 | #include "elf.h" |
aa0aa4fa | 5 | #include <errno.h> |
b9adb4a6 | 6 | |
c6105c0a FB |
7 | #include "cpu.h" |
8 | #include "exec-all.h" | |
9307c4c1 | 9 | #include "disas.h" |
c6105c0a | 10 | |
b9adb4a6 | 11 | /* Filled in by elfload.c. Simplistic, but will do for now. */ |
e80cfcfc | 12 | struct syminfo *syminfos = NULL; |
b9adb4a6 | 13 | |
aa0aa4fa FB |
14 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
15 | Transfer them to myaddr. */ | |
16 | int | |
17 | buffer_read_memory (memaddr, myaddr, length, info) | |
18 | bfd_vma memaddr; | |
19 | bfd_byte *myaddr; | |
20 | int length; | |
21 | struct disassemble_info *info; | |
22 | { | |
c6105c0a FB |
23 | if (memaddr < info->buffer_vma |
24 | || memaddr + length > info->buffer_vma + info->buffer_length) | |
25 | /* Out of bounds. Use EIO because GDB uses it. */ | |
26 | return EIO; | |
27 | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); | |
28 | return 0; | |
aa0aa4fa FB |
29 | } |
30 | ||
c6105c0a FB |
31 | #if !defined(CONFIG_USER_ONLY) |
32 | /* Get LENGTH bytes from info's buffer, at target address memaddr. | |
33 | Transfer them to myaddr. */ | |
34 | static int | |
35 | target_read_memory (memaddr, myaddr, length, info) | |
36 | bfd_vma memaddr; | |
37 | bfd_byte *myaddr; | |
38 | int length; | |
39 | struct disassemble_info *info; | |
40 | { | |
41 | int i; | |
42 | for(i = 0; i < length; i++) { | |
baf8ebf0 | 43 | myaddr[i] = ldub_code((void *)((long)memaddr + i)); |
c6105c0a FB |
44 | } |
45 | return 0; | |
46 | } | |
47 | #endif | |
48 | ||
aa0aa4fa FB |
49 | /* Print an error message. We can assume that this is in response to |
50 | an error return from buffer_read_memory. */ | |
51 | void | |
52 | perror_memory (status, memaddr, info) | |
53 | int status; | |
54 | bfd_vma memaddr; | |
55 | struct disassemble_info *info; | |
56 | { | |
57 | if (status != EIO) | |
58 | /* Can't happen. */ | |
59 | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status); | |
60 | else | |
61 | /* Actually, address between memaddr and memaddr + len was | |
62 | out of bounds. */ | |
63 | (*info->fprintf_func) (info->stream, | |
d44b29c2 | 64 | "Address 0x%llx is out of bounds.\n", memaddr); |
aa0aa4fa FB |
65 | } |
66 | ||
67 | /* This could be in a separate file, to save miniscule amounts of space | |
68 | in statically linked executables. */ | |
69 | ||
70 | /* Just print the address is hex. This is included for completeness even | |
71 | though both GDB and objdump provide their own (to print symbolic | |
72 | addresses). */ | |
73 | ||
74 | void | |
75 | generic_print_address (addr, info) | |
76 | bfd_vma addr; | |
77 | struct disassemble_info *info; | |
78 | { | |
d44b29c2 | 79 | (*info->fprintf_func) (info->stream, "0x%llx", addr); |
aa0aa4fa FB |
80 | } |
81 | ||
82 | /* Just return the given address. */ | |
83 | ||
84 | int | |
85 | generic_symbol_at_address (addr, info) | |
86 | bfd_vma addr; | |
87 | struct disassemble_info * info; | |
88 | { | |
89 | return 1; | |
90 | } | |
91 | ||
92 | bfd_vma bfd_getl32 (const bfd_byte *addr) | |
93 | { | |
94 | unsigned long v; | |
95 | ||
96 | v = (unsigned long) addr[0]; | |
97 | v |= (unsigned long) addr[1] << 8; | |
98 | v |= (unsigned long) addr[2] << 16; | |
99 | v |= (unsigned long) addr[3] << 24; | |
100 | return (bfd_vma) v; | |
101 | } | |
102 | ||
103 | bfd_vma bfd_getb32 (const bfd_byte *addr) | |
104 | { | |
105 | unsigned long v; | |
106 | ||
107 | v = (unsigned long) addr[0] << 24; | |
108 | v |= (unsigned long) addr[1] << 16; | |
109 | v |= (unsigned long) addr[2] << 8; | |
110 | v |= (unsigned long) addr[3]; | |
111 | return (bfd_vma) v; | |
112 | } | |
113 | ||
95cbfc64 FB |
114 | /* Disassemble this for me please... (debugging). 'flags' is only used |
115 | for i386: non zero means 16 bit code */ | |
116 | void disas(FILE *out, void *code, unsigned long size, int is_host, int flags) | |
b9adb4a6 FB |
117 | { |
118 | uint8_t *pc; | |
119 | int count; | |
120 | struct disassemble_info disasm_info; | |
121 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
122 | ||
123 | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); | |
124 | ||
c6105c0a FB |
125 | #if !defined(CONFIG_USER_ONLY) |
126 | if (!is_host) { | |
127 | disasm_info.read_memory_func = target_read_memory; | |
128 | } | |
129 | #endif | |
130 | ||
b9adb4a6 FB |
131 | disasm_info.buffer = code; |
132 | disasm_info.buffer_vma = (unsigned long)code; | |
133 | disasm_info.buffer_length = size; | |
134 | ||
95cbfc64 | 135 | if (is_host) { |
b9adb4a6 FB |
136 | #ifdef WORDS_BIGENDIAN |
137 | disasm_info.endian = BFD_ENDIAN_BIG; | |
138 | #else | |
139 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
140 | #endif | |
bc51c5c9 | 141 | #if defined(__i386__) |
b9adb4a6 FB |
142 | disasm_info.mach = bfd_mach_i386_i386; |
143 | print_insn = print_insn_i386; | |
bc51c5c9 FB |
144 | #elif defined(__x86_64__) |
145 | disasm_info.mach = bfd_mach_x86_64; | |
146 | print_insn = print_insn_i386; | |
b9adb4a6 FB |
147 | #elif defined(__powerpc__) |
148 | print_insn = print_insn_ppc; | |
a993ba85 FB |
149 | #elif defined(__alpha__) |
150 | print_insn = print_insn_alpha; | |
aa0aa4fa FB |
151 | #elif defined(__sparc__) |
152 | print_insn = print_insn_sparc; | |
153 | #elif defined(__arm__) | |
154 | print_insn = print_insn_arm; | |
b9adb4a6 FB |
155 | #else |
156 | fprintf(out, "Asm output not supported on this arch\n"); | |
157 | return; | |
158 | #endif | |
159 | } else { | |
95cbfc64 FB |
160 | #ifdef TARGET_WORDS_BIGENDIAN |
161 | disasm_info.endian = BFD_ENDIAN_BIG; | |
162 | #else | |
b9adb4a6 | 163 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
95cbfc64 FB |
164 | #endif |
165 | #if defined(TARGET_I386) | |
166 | if (!flags) | |
b9adb4a6 FB |
167 | disasm_info.mach = bfd_mach_i386_i386; |
168 | else | |
169 | disasm_info.mach = bfd_mach_i386_i8086; | |
170 | print_insn = print_insn_i386; | |
95cbfc64 FB |
171 | #elif defined(TARGET_ARM) |
172 | print_insn = print_insn_arm; | |
93ac68bc FB |
173 | #elif defined(TARGET_SPARC) |
174 | print_insn = print_insn_sparc; | |
67867308 FB |
175 | #elif defined(TARGET_PPC) |
176 | print_insn = print_insn_ppc; | |
95cbfc64 FB |
177 | #else |
178 | fprintf(out, "Asm output not supported on this arch\n"); | |
179 | return; | |
180 | #endif | |
b9adb4a6 FB |
181 | } |
182 | ||
183 | for (pc = code; pc < (uint8_t *)code + size; pc += count) { | |
184 | fprintf(out, "0x%08lx: ", (long)pc); | |
aa0aa4fa FB |
185 | #ifdef __arm__ |
186 | /* since data are included in the code, it is better to | |
187 | display code data too */ | |
95cbfc64 | 188 | if (is_host) { |
aa0aa4fa FB |
189 | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); |
190 | } | |
191 | #endif | |
08351fb3 | 192 | count = print_insn((unsigned long)pc, &disasm_info); |
b9adb4a6 FB |
193 | fprintf(out, "\n"); |
194 | if (count < 0) | |
195 | break; | |
196 | } | |
197 | } | |
198 | ||
199 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ | |
200 | const char *lookup_symbol(void *orig_addr) | |
201 | { | |
202 | unsigned int i; | |
203 | /* Hack, because we know this is x86. */ | |
e80cfcfc FB |
204 | Elf32_Sym *sym; |
205 | struct syminfo *s; | |
206 | ||
207 | for (s = syminfos; s; s = s->next) { | |
208 | sym = s->disas_symtab; | |
209 | for (i = 0; i < s->disas_num_syms; i++) { | |
210 | if (sym[i].st_shndx == SHN_UNDEF | |
211 | || sym[i].st_shndx >= SHN_LORESERVE) | |
212 | continue; | |
b9adb4a6 | 213 | |
e80cfcfc FB |
214 | if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC) |
215 | continue; | |
b9adb4a6 | 216 | |
e80cfcfc FB |
217 | if ((long)orig_addr >= sym[i].st_value |
218 | && (long)orig_addr < sym[i].st_value + sym[i].st_size) | |
219 | return s->disas_strtab + sym[i].st_name; | |
220 | } | |
b9adb4a6 FB |
221 | } |
222 | return ""; | |
223 | } | |
9307c4c1 FB |
224 | |
225 | #if !defined(CONFIG_USER_ONLY) | |
226 | ||
3d2cfdf1 FB |
227 | void term_vprintf(const char *fmt, va_list ap); |
228 | void term_printf(const char *fmt, ...); | |
229 | ||
9307c4c1 FB |
230 | static int monitor_disas_is_physical; |
231 | ||
232 | static int | |
233 | monitor_read_memory (memaddr, myaddr, length, info) | |
234 | bfd_vma memaddr; | |
235 | bfd_byte *myaddr; | |
236 | int length; | |
237 | struct disassemble_info *info; | |
238 | { | |
239 | if (monitor_disas_is_physical) { | |
240 | cpu_physical_memory_rw(memaddr, myaddr, length, 0); | |
241 | } else { | |
242 | cpu_memory_rw_debug(cpu_single_env, memaddr,myaddr, length, 0); | |
243 | } | |
244 | return 0; | |
245 | } | |
246 | ||
3d2cfdf1 FB |
247 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
248 | { | |
249 | va_list ap; | |
250 | va_start(ap, fmt); | |
251 | term_vprintf(fmt, ap); | |
252 | va_end(ap); | |
253 | return 0; | |
254 | } | |
255 | ||
9307c4c1 FB |
256 | void monitor_disas(target_ulong pc, int nb_insn, int is_physical, int flags) |
257 | { | |
9307c4c1 FB |
258 | int count, i; |
259 | struct disassemble_info disasm_info; | |
260 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
261 | ||
3d2cfdf1 | 262 | INIT_DISASSEMBLE_INFO(disasm_info, NULL, monitor_fprintf); |
9307c4c1 FB |
263 | |
264 | monitor_disas_is_physical = is_physical; | |
265 | disasm_info.read_memory_func = monitor_read_memory; | |
266 | ||
267 | disasm_info.buffer_vma = pc; | |
268 | ||
269 | #ifdef TARGET_WORDS_BIGENDIAN | |
270 | disasm_info.endian = BFD_ENDIAN_BIG; | |
271 | #else | |
272 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
273 | #endif | |
274 | #if defined(TARGET_I386) | |
275 | if (!flags) | |
276 | disasm_info.mach = bfd_mach_i386_i386; | |
277 | else | |
278 | disasm_info.mach = bfd_mach_i386_i8086; | |
279 | print_insn = print_insn_i386; | |
280 | #elif defined(TARGET_ARM) | |
281 | print_insn = print_insn_arm; | |
282 | #elif defined(TARGET_SPARC) | |
283 | print_insn = print_insn_sparc; | |
284 | #elif defined(TARGET_PPC) | |
285 | print_insn = print_insn_ppc; | |
286 | #else | |
7fe48483 | 287 | term_printf("Asm output not supported on this arch\n"); |
9307c4c1 FB |
288 | return; |
289 | #endif | |
290 | ||
291 | for(i = 0; i < nb_insn; i++) { | |
3d2cfdf1 | 292 | term_printf("0x%08lx: ", (unsigned long)pc); |
9307c4c1 | 293 | count = print_insn(pc, &disasm_info); |
3d2cfdf1 | 294 | term_printf("\n"); |
9307c4c1 FB |
295 | if (count < 0) |
296 | break; | |
297 | pc += count; | |
298 | } | |
299 | } | |
300 | #endif |