]>
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 | |
3a742b76 PB |
17 | buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
18 | struct disassemble_info *info) | |
aa0aa4fa | 19 | { |
c6105c0a FB |
20 | if (memaddr < info->buffer_vma |
21 | || memaddr + length > info->buffer_vma + info->buffer_length) | |
22 | /* Out of bounds. Use EIO because GDB uses it. */ | |
23 | return EIO; | |
24 | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); | |
25 | return 0; | |
aa0aa4fa FB |
26 | } |
27 | ||
c6105c0a FB |
28 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
29 | Transfer them to myaddr. */ | |
30 | static int | |
c27004ec FB |
31 | target_read_memory (bfd_vma memaddr, |
32 | bfd_byte *myaddr, | |
33 | int length, | |
34 | struct disassemble_info *info) | |
c6105c0a | 35 | { |
e612a1f7 | 36 | cpu_memory_rw_debug(cpu_single_env, memaddr, myaddr, length, 0); |
c6105c0a FB |
37 | return 0; |
38 | } | |
c6105c0a | 39 | |
aa0aa4fa FB |
40 | /* Print an error message. We can assume that this is in response to |
41 | an error return from buffer_read_memory. */ | |
42 | void | |
3a742b76 | 43 | perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info) |
aa0aa4fa FB |
44 | { |
45 | if (status != EIO) | |
46 | /* Can't happen. */ | |
47 | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status); | |
48 | else | |
49 | /* Actually, address between memaddr and memaddr + len was | |
50 | out of bounds. */ | |
51 | (*info->fprintf_func) (info->stream, | |
26a76461 | 52 | "Address 0x%" PRIx64 " is out of bounds.\n", memaddr); |
aa0aa4fa FB |
53 | } |
54 | ||
55 | /* This could be in a separate file, to save miniscule amounts of space | |
56 | in statically linked executables. */ | |
57 | ||
58 | /* Just print the address is hex. This is included for completeness even | |
59 | though both GDB and objdump provide their own (to print symbolic | |
60 | addresses). */ | |
61 | ||
62 | void | |
3a742b76 | 63 | generic_print_address (bfd_vma addr, struct disassemble_info *info) |
aa0aa4fa | 64 | { |
26a76461 | 65 | (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr); |
aa0aa4fa FB |
66 | } |
67 | ||
68 | /* Just return the given address. */ | |
69 | ||
70 | int | |
3a742b76 | 71 | generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info) |
aa0aa4fa FB |
72 | { |
73 | return 1; | |
74 | } | |
75 | ||
76 | bfd_vma bfd_getl32 (const bfd_byte *addr) | |
77 | { | |
78 | unsigned long v; | |
79 | ||
80 | v = (unsigned long) addr[0]; | |
81 | v |= (unsigned long) addr[1] << 8; | |
82 | v |= (unsigned long) addr[2] << 16; | |
83 | v |= (unsigned long) addr[3] << 24; | |
84 | return (bfd_vma) v; | |
85 | } | |
86 | ||
87 | bfd_vma bfd_getb32 (const bfd_byte *addr) | |
88 | { | |
89 | unsigned long v; | |
90 | ||
91 | v = (unsigned long) addr[0] << 24; | |
92 | v |= (unsigned long) addr[1] << 16; | |
93 | v |= (unsigned long) addr[2] << 8; | |
94 | v |= (unsigned long) addr[3]; | |
95 | return (bfd_vma) v; | |
96 | } | |
97 | ||
6af0bf9c FB |
98 | bfd_vma bfd_getl16 (const bfd_byte *addr) |
99 | { | |
100 | unsigned long v; | |
101 | ||
102 | v = (unsigned long) addr[0]; | |
103 | v |= (unsigned long) addr[1] << 8; | |
104 | return (bfd_vma) v; | |
105 | } | |
106 | ||
107 | bfd_vma bfd_getb16 (const bfd_byte *addr) | |
108 | { | |
109 | unsigned long v; | |
110 | ||
111 | v = (unsigned long) addr[0] << 24; | |
112 | v |= (unsigned long) addr[1] << 16; | |
113 | return (bfd_vma) v; | |
114 | } | |
115 | ||
c2d551ff FB |
116 | #ifdef TARGET_ARM |
117 | static int | |
118 | print_insn_thumb1(bfd_vma pc, disassemble_info *info) | |
119 | { | |
120 | return print_insn_arm(pc | 1, info); | |
121 | } | |
122 | #endif | |
123 | ||
e91c8a77 | 124 | /* Disassemble this for me please... (debugging). 'flags' has the following |
c2d551ff FB |
125 | values: |
126 | i386 - nonzero means 16 bit code | |
5fafdf24 | 127 | arm - nonzero means thumb code |
6a00d601 | 128 | ppc - nonzero means little endian |
c2d551ff FB |
129 | other targets - unused |
130 | */ | |
83b34f8b | 131 | void target_disas(FILE *out, target_ulong code, target_ulong size, int flags) |
b9adb4a6 | 132 | { |
c27004ec | 133 | target_ulong pc; |
b9adb4a6 FB |
134 | int count; |
135 | struct disassemble_info disasm_info; | |
136 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
137 | ||
138 | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); | |
139 | ||
c27004ec FB |
140 | disasm_info.read_memory_func = target_read_memory; |
141 | disasm_info.buffer_vma = code; | |
142 | disasm_info.buffer_length = size; | |
143 | ||
144 | #ifdef TARGET_WORDS_BIGENDIAN | |
145 | disasm_info.endian = BFD_ENDIAN_BIG; | |
146 | #else | |
147 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
148 | #endif | |
149 | #if defined(TARGET_I386) | |
150 | if (flags == 2) | |
151 | disasm_info.mach = bfd_mach_x86_64; | |
5fafdf24 | 152 | else if (flags == 1) |
c27004ec FB |
153 | disasm_info.mach = bfd_mach_i386_i8086; |
154 | else | |
155 | disasm_info.mach = bfd_mach_i386_i386; | |
156 | print_insn = print_insn_i386; | |
157 | #elif defined(TARGET_ARM) | |
c2d551ff FB |
158 | if (flags) |
159 | print_insn = print_insn_thumb1; | |
160 | else | |
161 | print_insn = print_insn_arm; | |
c27004ec FB |
162 | #elif defined(TARGET_SPARC) |
163 | print_insn = print_insn_sparc; | |
3475187d FB |
164 | #ifdef TARGET_SPARC64 |
165 | disasm_info.mach = bfd_mach_sparc_v9b; | |
3b46e624 | 166 | #endif |
c27004ec | 167 | #elif defined(TARGET_PPC) |
237c0af0 | 168 | if (flags >> 16) |
111bfab3 | 169 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
237c0af0 JM |
170 | if (flags & 0xFFFF) { |
171 | /* If we have a precise definitions of the instructions set, use it */ | |
172 | disasm_info.mach = flags & 0xFFFF; | |
173 | } else { | |
a2458627 | 174 | #ifdef TARGET_PPC64 |
237c0af0 | 175 | disasm_info.mach = bfd_mach_ppc64; |
a2458627 | 176 | #else |
237c0af0 | 177 | disasm_info.mach = bfd_mach_ppc; |
a2458627 | 178 | #endif |
237c0af0 | 179 | } |
c27004ec | 180 | print_insn = print_insn_ppc; |
e6e5906b PB |
181 | #elif defined(TARGET_M68K) |
182 | print_insn = print_insn_m68k; | |
6af0bf9c | 183 | #elif defined(TARGET_MIPS) |
76b3030c | 184 | #ifdef TARGET_WORDS_BIGENDIAN |
6af0bf9c | 185 | print_insn = print_insn_big_mips; |
76b3030c FB |
186 | #else |
187 | print_insn = print_insn_little_mips; | |
188 | #endif | |
fdf9b3e8 FB |
189 | #elif defined(TARGET_SH4) |
190 | disasm_info.mach = bfd_mach_sh4; | |
191 | print_insn = print_insn_sh; | |
eddf68a6 JM |
192 | #elif defined(TARGET_ALPHA) |
193 | disasm_info.mach = bfd_mach_alpha; | |
194 | print_insn = print_insn_alpha; | |
a25fd137 TS |
195 | #elif defined(TARGET_CRIS) |
196 | disasm_info.mach = bfd_mach_cris_v32; | |
197 | print_insn = print_insn_crisv32; | |
c27004ec | 198 | #else |
b8076a74 FB |
199 | fprintf(out, "0x" TARGET_FMT_lx |
200 | ": Asm output not supported on this arch\n", code); | |
c27004ec | 201 | return; |
c6105c0a FB |
202 | #endif |
203 | ||
7e000c2e | 204 | for (pc = code; size > 0; pc += count, size -= count) { |
fa15e030 | 205 | fprintf(out, "0x" TARGET_FMT_lx ": ", pc); |
c27004ec FB |
206 | count = print_insn(pc, &disasm_info); |
207 | #if 0 | |
208 | { | |
209 | int i; | |
210 | uint8_t b; | |
211 | fprintf(out, " {"); | |
212 | for(i = 0; i < count; i++) { | |
213 | target_read_memory(pc + i, &b, 1, &disasm_info); | |
214 | fprintf(out, " %02x", b); | |
215 | } | |
216 | fprintf(out, " }"); | |
217 | } | |
218 | #endif | |
219 | fprintf(out, "\n"); | |
220 | if (count < 0) | |
221 | break; | |
754d00ae | 222 | if (size < count) { |
223 | fprintf(out, | |
224 | "Disassembler disagrees with translator over instruction " | |
225 | "decoding\n" | |
226 | "Please report this to qemu-devel@nongnu.org\n"); | |
227 | break; | |
228 | } | |
c27004ec FB |
229 | } |
230 | } | |
231 | ||
232 | /* Disassemble this for me please... (debugging). */ | |
233 | void disas(FILE *out, void *code, unsigned long size) | |
234 | { | |
235 | unsigned long pc; | |
236 | int count; | |
237 | struct disassemble_info disasm_info; | |
238 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
239 | ||
240 | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); | |
241 | ||
b9adb4a6 FB |
242 | disasm_info.buffer = code; |
243 | disasm_info.buffer_vma = (unsigned long)code; | |
244 | disasm_info.buffer_length = size; | |
245 | ||
b9adb4a6 | 246 | #ifdef WORDS_BIGENDIAN |
c27004ec | 247 | disasm_info.endian = BFD_ENDIAN_BIG; |
b9adb4a6 | 248 | #else |
c27004ec | 249 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
b9adb4a6 | 250 | #endif |
bc51c5c9 | 251 | #if defined(__i386__) |
c27004ec FB |
252 | disasm_info.mach = bfd_mach_i386_i386; |
253 | print_insn = print_insn_i386; | |
bc51c5c9 | 254 | #elif defined(__x86_64__) |
c27004ec FB |
255 | disasm_info.mach = bfd_mach_x86_64; |
256 | print_insn = print_insn_i386; | |
e58ffeb3 | 257 | #elif defined(_ARCH_PPC) |
c27004ec | 258 | print_insn = print_insn_ppc; |
a993ba85 | 259 | #elif defined(__alpha__) |
c27004ec | 260 | print_insn = print_insn_alpha; |
aa0aa4fa | 261 | #elif defined(__sparc__) |
c27004ec | 262 | print_insn = print_insn_sparc; |
6ecd4534 BS |
263 | #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__) |
264 | disasm_info.mach = bfd_mach_sparc_v9b; | |
265 | #endif | |
5fafdf24 | 266 | #elif defined(__arm__) |
c27004ec | 267 | print_insn = print_insn_arm; |
6af0bf9c FB |
268 | #elif defined(__MIPSEB__) |
269 | print_insn = print_insn_big_mips; | |
270 | #elif defined(__MIPSEL__) | |
271 | print_insn = print_insn_little_mips; | |
48024e4a FB |
272 | #elif defined(__m68k__) |
273 | print_insn = print_insn_m68k; | |
8f860bb8 TS |
274 | #elif defined(__s390__) |
275 | print_insn = print_insn_s390; | |
f54b3f92 AJ |
276 | #elif defined(__hppa__) |
277 | print_insn = print_insn_hppa; | |
b9adb4a6 | 278 | #else |
b8076a74 FB |
279 | fprintf(out, "0x%lx: Asm output not supported on this arch\n", |
280 | (long) code); | |
c27004ec | 281 | return; |
b9adb4a6 | 282 | #endif |
7e000c2e | 283 | for (pc = (unsigned long)code; size > 0; pc += count, size -= count) { |
c27004ec | 284 | fprintf(out, "0x%08lx: ", pc); |
aa0aa4fa | 285 | #ifdef __arm__ |
46152182 | 286 | /* since data is included in the code, it is better to |
aa0aa4fa | 287 | display code data too */ |
46152182 | 288 | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); |
aa0aa4fa | 289 | #endif |
c27004ec | 290 | count = print_insn(pc, &disasm_info); |
b9adb4a6 FB |
291 | fprintf(out, "\n"); |
292 | if (count < 0) | |
293 | break; | |
294 | } | |
295 | } | |
296 | ||
297 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ | |
c27004ec | 298 | const char *lookup_symbol(target_ulong orig_addr) |
b9adb4a6 | 299 | { |
49918a75 | 300 | const char *symbol = ""; |
e80cfcfc | 301 | struct syminfo *s; |
3b46e624 | 302 | |
e80cfcfc | 303 | for (s = syminfos; s; s = s->next) { |
49918a75 PB |
304 | symbol = s->lookup_symbol(s, orig_addr); |
305 | if (symbol[0] != '\0') { | |
306 | break; | |
307 | } | |
b9adb4a6 | 308 | } |
49918a75 PB |
309 | |
310 | return symbol; | |
b9adb4a6 | 311 | } |
9307c4c1 FB |
312 | |
313 | #if !defined(CONFIG_USER_ONLY) | |
314 | ||
376253ec | 315 | #include "monitor.h" |
3d2cfdf1 | 316 | |
9307c4c1 | 317 | static int monitor_disas_is_physical; |
6a00d601 | 318 | static CPUState *monitor_disas_env; |
9307c4c1 FB |
319 | |
320 | static int | |
a5f1b965 BS |
321 | monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length, |
322 | struct disassemble_info *info) | |
9307c4c1 FB |
323 | { |
324 | if (monitor_disas_is_physical) { | |
325 | cpu_physical_memory_rw(memaddr, myaddr, length, 0); | |
326 | } else { | |
6a00d601 | 327 | cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0); |
9307c4c1 FB |
328 | } |
329 | return 0; | |
330 | } | |
331 | ||
3d2cfdf1 FB |
332 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
333 | { | |
334 | va_list ap; | |
335 | va_start(ap, fmt); | |
376253ec | 336 | monitor_vprintf((Monitor *)stream, fmt, ap); |
3d2cfdf1 FB |
337 | va_end(ap); |
338 | return 0; | |
339 | } | |
340 | ||
376253ec | 341 | void monitor_disas(Monitor *mon, CPUState *env, |
6a00d601 | 342 | target_ulong pc, int nb_insn, int is_physical, int flags) |
9307c4c1 | 343 | { |
9307c4c1 FB |
344 | int count, i; |
345 | struct disassemble_info disasm_info; | |
346 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
347 | ||
376253ec | 348 | INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf); |
9307c4c1 | 349 | |
6a00d601 | 350 | monitor_disas_env = env; |
9307c4c1 FB |
351 | monitor_disas_is_physical = is_physical; |
352 | disasm_info.read_memory_func = monitor_read_memory; | |
353 | ||
354 | disasm_info.buffer_vma = pc; | |
355 | ||
356 | #ifdef TARGET_WORDS_BIGENDIAN | |
357 | disasm_info.endian = BFD_ENDIAN_BIG; | |
358 | #else | |
359 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
360 | #endif | |
361 | #if defined(TARGET_I386) | |
fa15e030 FB |
362 | if (flags == 2) |
363 | disasm_info.mach = bfd_mach_x86_64; | |
5fafdf24 | 364 | else if (flags == 1) |
9307c4c1 | 365 | disasm_info.mach = bfd_mach_i386_i8086; |
fa15e030 FB |
366 | else |
367 | disasm_info.mach = bfd_mach_i386_i386; | |
9307c4c1 FB |
368 | print_insn = print_insn_i386; |
369 | #elif defined(TARGET_ARM) | |
370 | print_insn = print_insn_arm; | |
cbd669da TS |
371 | #elif defined(TARGET_ALPHA) |
372 | print_insn = print_insn_alpha; | |
9307c4c1 FB |
373 | #elif defined(TARGET_SPARC) |
374 | print_insn = print_insn_sparc; | |
682c4f15 BS |
375 | #ifdef TARGET_SPARC64 |
376 | disasm_info.mach = bfd_mach_sparc_v9b; | |
377 | #endif | |
9307c4c1 | 378 | #elif defined(TARGET_PPC) |
a2458627 FB |
379 | #ifdef TARGET_PPC64 |
380 | disasm_info.mach = bfd_mach_ppc64; | |
381 | #else | |
382 | disasm_info.mach = bfd_mach_ppc; | |
383 | #endif | |
9307c4c1 | 384 | print_insn = print_insn_ppc; |
e6e5906b PB |
385 | #elif defined(TARGET_M68K) |
386 | print_insn = print_insn_m68k; | |
6af0bf9c | 387 | #elif defined(TARGET_MIPS) |
76b3030c | 388 | #ifdef TARGET_WORDS_BIGENDIAN |
6af0bf9c | 389 | print_insn = print_insn_big_mips; |
76b3030c FB |
390 | #else |
391 | print_insn = print_insn_little_mips; | |
392 | #endif | |
9307c4c1 | 393 | #else |
376253ec AL |
394 | monitor_printf(mon, "0x" TARGET_FMT_lx |
395 | ": Asm output not supported on this arch\n", pc); | |
9307c4c1 FB |
396 | return; |
397 | #endif | |
398 | ||
399 | for(i = 0; i < nb_insn; i++) { | |
376253ec | 400 | monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc); |
9307c4c1 | 401 | count = print_insn(pc, &disasm_info); |
376253ec | 402 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
403 | if (count < 0) |
404 | break; | |
405 | pc += count; | |
406 | } | |
407 | } | |
408 | #endif |