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