xref: /illumos-kvm-cmd/disas.c (revision 68396ea9)
1 /* General "disassemble this chunk" code.  Used for debugging. */
2 #include "config.h"
3 #include "dis-asm.h"
4 #include "elf.h"
5 #include <errno.h>
6 
7 #include "cpu.h"
8 #include "exec-all.h"
9 #include "disas.h"
10 
11 /* Filled in by elfload.c.  Simplistic, but will do for now. */
12 struct syminfo *syminfos = NULL;
13 
14 /* Get LENGTH bytes from info's buffer, at target address memaddr.
15    Transfer them to myaddr.  */
16 int
buffer_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)17 buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
18                    struct disassemble_info *info)
19 {
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;
26 }
27 
28 /* Get LENGTH bytes from info's buffer, at target address memaddr.
29    Transfer them to myaddr.  */
30 static int
target_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)31 target_read_memory (bfd_vma memaddr,
32                     bfd_byte *myaddr,
33                     int length,
34                     struct disassemble_info *info)
35 {
36     cpu_memory_rw_debug(cpu_single_env, memaddr, myaddr, length, 0);
37     return 0;
38 }
39 
40 /* Print an error message.  We can assume that this is in response to
41    an error return from buffer_read_memory.  */
42 void
perror_memory(int status,bfd_vma memaddr,struct disassemble_info * info)43 perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
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,
52 			   "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
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
generic_print_address(bfd_vma addr,struct disassemble_info * info)63 generic_print_address (bfd_vma addr, struct disassemble_info *info)
64 {
65     (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
66 }
67 
68 /* Just return the given address.  */
69 
70 int
generic_symbol_at_address(bfd_vma addr,struct disassemble_info * info)71 generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
72 {
73   return 1;
74 }
75 
bfd_getl64(const bfd_byte * addr)76 bfd_vma bfd_getl64 (const bfd_byte *addr)
77 {
78   unsigned long long v;
79 
80   v = (unsigned long long) addr[0];
81   v |= (unsigned long long) addr[1] << 8;
82   v |= (unsigned long long) addr[2] << 16;
83   v |= (unsigned long long) addr[3] << 24;
84   v |= (unsigned long long) addr[4] << 32;
85   v |= (unsigned long long) addr[5] << 40;
86   v |= (unsigned long long) addr[6] << 48;
87   v |= (unsigned long long) addr[7] << 56;
88   return (bfd_vma) v;
89 }
90 
bfd_getl32(const bfd_byte * addr)91 bfd_vma bfd_getl32 (const bfd_byte *addr)
92 {
93   unsigned long v;
94 
95   v = (unsigned long) addr[0];
96   v |= (unsigned long) addr[1] << 8;
97   v |= (unsigned long) addr[2] << 16;
98   v |= (unsigned long) addr[3] << 24;
99   return (bfd_vma) v;
100 }
101 
bfd_getb32(const bfd_byte * addr)102 bfd_vma bfd_getb32 (const bfd_byte *addr)
103 {
104   unsigned long v;
105 
106   v = (unsigned long) addr[0] << 24;
107   v |= (unsigned long) addr[1] << 16;
108   v |= (unsigned long) addr[2] << 8;
109   v |= (unsigned long) addr[3];
110   return (bfd_vma) v;
111 }
112 
bfd_getl16(const bfd_byte * addr)113 bfd_vma bfd_getl16 (const bfd_byte *addr)
114 {
115   unsigned long v;
116 
117   v = (unsigned long) addr[0];
118   v |= (unsigned long) addr[1] << 8;
119   return (bfd_vma) v;
120 }
121 
bfd_getb16(const bfd_byte * addr)122 bfd_vma bfd_getb16 (const bfd_byte *addr)
123 {
124   unsigned long v;
125 
126   v = (unsigned long) addr[0] << 24;
127   v |= (unsigned long) addr[1] << 16;
128   return (bfd_vma) v;
129 }
130 
131 #ifdef TARGET_ARM
132 static int
print_insn_thumb1(bfd_vma pc,disassemble_info * info)133 print_insn_thumb1(bfd_vma pc, disassemble_info *info)
134 {
135   return print_insn_arm(pc | 1, info);
136 }
137 #endif
138 
139 /* Disassemble this for me please... (debugging). 'flags' has the following
140    values:
141     i386 - nonzero means 16 bit code
142     arm  - nonzero means thumb code
143     ppc  - nonzero means little endian
144     other targets - unused
145  */
target_disas(FILE * out,target_ulong code,target_ulong size,int flags)146 void target_disas(FILE *out, target_ulong code, target_ulong size, int flags)
147 {
148     target_ulong pc;
149     int count;
150     struct disassemble_info disasm_info;
151     int (*print_insn)(bfd_vma pc, disassemble_info *info);
152 
153     INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
154 
155     disasm_info.read_memory_func = target_read_memory;
156     disasm_info.buffer_vma = code;
157     disasm_info.buffer_length = size;
158 
159 #ifdef TARGET_WORDS_BIGENDIAN
160     disasm_info.endian = BFD_ENDIAN_BIG;
161 #else
162     disasm_info.endian = BFD_ENDIAN_LITTLE;
163 #endif
164 #if defined(TARGET_I386)
165     if (flags == 2)
166         disasm_info.mach = bfd_mach_x86_64;
167     else if (flags == 1)
168         disasm_info.mach = bfd_mach_i386_i8086;
169     else
170         disasm_info.mach = bfd_mach_i386_i386;
171     print_insn = print_insn_i386;
172 #elif defined(TARGET_ARM)
173     if (flags)
174 	print_insn = print_insn_thumb1;
175     else
176 	print_insn = print_insn_arm;
177 #elif defined(TARGET_SPARC)
178     print_insn = print_insn_sparc;
179 #ifdef TARGET_SPARC64
180     disasm_info.mach = bfd_mach_sparc_v9b;
181 #endif
182 #elif defined(TARGET_PPC)
183     if (flags >> 16)
184         disasm_info.endian = BFD_ENDIAN_LITTLE;
185     if (flags & 0xFFFF) {
186         /* If we have a precise definitions of the instructions set, use it */
187         disasm_info.mach = flags & 0xFFFF;
188     } else {
189 #ifdef TARGET_PPC64
190         disasm_info.mach = bfd_mach_ppc64;
191 #else
192         disasm_info.mach = bfd_mach_ppc;
193 #endif
194     }
195     print_insn = print_insn_ppc;
196 #elif defined(TARGET_M68K)
197     print_insn = print_insn_m68k;
198 #elif defined(TARGET_MIPS)
199 #ifdef TARGET_WORDS_BIGENDIAN
200     print_insn = print_insn_big_mips;
201 #else
202     print_insn = print_insn_little_mips;
203 #endif
204 #elif defined(TARGET_SH4)
205     disasm_info.mach = bfd_mach_sh4;
206     print_insn = print_insn_sh;
207 #elif defined(TARGET_ALPHA)
208     disasm_info.mach = bfd_mach_alpha;
209     print_insn = print_insn_alpha;
210 #elif defined(TARGET_CRIS)
211     if (flags != 32) {
212         disasm_info.mach = bfd_mach_cris_v0_v10;
213         print_insn = print_insn_crisv10;
214     } else {
215         disasm_info.mach = bfd_mach_cris_v32;
216         print_insn = print_insn_crisv32;
217     }
218 #elif defined(TARGET_MICROBLAZE)
219     disasm_info.mach = bfd_arch_microblaze;
220     print_insn = print_insn_microblaze;
221 #else
222     fprintf(out, "0x" TARGET_FMT_lx
223 	    ": Asm output not supported on this arch\n", code);
224     return;
225 #endif
226 
227     for (pc = code; size > 0; pc += count, size -= count) {
228 	fprintf(out, "0x" TARGET_FMT_lx ":  ", pc);
229 	count = print_insn(pc, &disasm_info);
230 #if 0
231         {
232             int i;
233             uint8_t b;
234             fprintf(out, " {");
235             for(i = 0; i < count; i++) {
236                 target_read_memory(pc + i, &b, 1, &disasm_info);
237                 fprintf(out, " %02x", b);
238             }
239             fprintf(out, " }");
240         }
241 #endif
242 	fprintf(out, "\n");
243 	if (count < 0)
244 	    break;
245         if (size < count) {
246             fprintf(out,
247                     "Disassembler disagrees with translator over instruction "
248                     "decoding\n"
249                     "Please report this to qemu-devel@nongnu.org\n");
250             break;
251         }
252     }
253 }
254 
255 /* Disassemble this for me please... (debugging). */
disas(FILE * out,void * code,unsigned long size)256 void disas(FILE *out, void *code, unsigned long size)
257 {
258     unsigned long pc;
259     int count;
260     struct disassemble_info disasm_info;
261     int (*print_insn)(bfd_vma pc, disassemble_info *info);
262 
263     INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
264 
265     disasm_info.buffer = code;
266     disasm_info.buffer_vma = (unsigned long)code;
267     disasm_info.buffer_length = size;
268 
269 #ifdef HOST_WORDS_BIGENDIAN
270     disasm_info.endian = BFD_ENDIAN_BIG;
271 #else
272     disasm_info.endian = BFD_ENDIAN_LITTLE;
273 #endif
274 #if defined(__i386__)
275     disasm_info.mach = bfd_mach_i386_i386;
276     print_insn = print_insn_i386;
277 #elif defined(__x86_64__)
278     disasm_info.mach = bfd_mach_x86_64;
279     print_insn = print_insn_i386;
280 #elif defined(_ARCH_PPC)
281     print_insn = print_insn_ppc;
282 #elif defined(__alpha__)
283     print_insn = print_insn_alpha;
284 #elif defined(__sparc__)
285     print_insn = print_insn_sparc;
286 #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
287     disasm_info.mach = bfd_mach_sparc_v9b;
288 #endif
289 #elif defined(__arm__)
290     print_insn = print_insn_arm;
291 #elif defined(__MIPSEB__)
292     print_insn = print_insn_big_mips;
293 #elif defined(__MIPSEL__)
294     print_insn = print_insn_little_mips;
295 #elif defined(__m68k__)
296     print_insn = print_insn_m68k;
297 #elif defined(__s390__)
298     print_insn = print_insn_s390;
299 #elif defined(__hppa__)
300     print_insn = print_insn_hppa;
301 #elif defined(__ia64__)
302     print_insn = print_insn_ia64;
303 #else
304     fprintf(out, "0x%lx: Asm output not supported on this arch\n",
305 	    (long) code);
306     return;
307 #endif
308     for (pc = (unsigned long)code; size > 0; pc += count, size -= count) {
309 	fprintf(out, "0x%08lx:  ", pc);
310 	count = print_insn(pc, &disasm_info);
311 	fprintf(out, "\n");
312 	if (count < 0)
313 	    break;
314     }
315 }
316 
317 /* Look up symbol for debugging purpose.  Returns "" if unknown. */
lookup_symbol(target_ulong orig_addr)318 const char *lookup_symbol(target_ulong orig_addr)
319 {
320     const char *symbol = "";
321     struct syminfo *s;
322 
323     for (s = syminfos; s; s = s->next) {
324         symbol = s->lookup_symbol(s, orig_addr);
325         if (symbol[0] != '\0') {
326             break;
327         }
328     }
329 
330     return symbol;
331 }
332 
333 #if !defined(CONFIG_USER_ONLY)
334 
335 #include "monitor.h"
336 
337 static int monitor_disas_is_physical;
338 static CPUState *monitor_disas_env;
339 
340 static int
monitor_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)341 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
342                      struct disassemble_info *info)
343 {
344     if (monitor_disas_is_physical) {
345         cpu_physical_memory_rw(memaddr, myaddr, length, 0);
346     } else {
347         cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
348     }
349     return 0;
350 }
351 
352 static int GCC_FMT_ATTR(2, 3)
monitor_fprintf(FILE * stream,const char * fmt,...)353 monitor_fprintf(FILE *stream, const char *fmt, ...)
354 {
355     va_list ap;
356     va_start(ap, fmt);
357     monitor_vprintf((Monitor *)stream, fmt, ap);
358     va_end(ap);
359     return 0;
360 }
361 
monitor_disas(Monitor * mon,CPUState * env,target_ulong pc,int nb_insn,int is_physical,int flags)362 void monitor_disas(Monitor *mon, CPUState *env,
363                    target_ulong pc, int nb_insn, int is_physical, int flags)
364 {
365     int count, i;
366     struct disassemble_info disasm_info;
367     int (*print_insn)(bfd_vma pc, disassemble_info *info);
368 
369     INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
370 
371     monitor_disas_env = env;
372     monitor_disas_is_physical = is_physical;
373     disasm_info.read_memory_func = monitor_read_memory;
374 
375     disasm_info.buffer_vma = pc;
376 
377 #ifdef TARGET_WORDS_BIGENDIAN
378     disasm_info.endian = BFD_ENDIAN_BIG;
379 #else
380     disasm_info.endian = BFD_ENDIAN_LITTLE;
381 #endif
382 #if defined(TARGET_I386)
383     if (flags == 2)
384         disasm_info.mach = bfd_mach_x86_64;
385     else if (flags == 1)
386         disasm_info.mach = bfd_mach_i386_i8086;
387     else
388         disasm_info.mach = bfd_mach_i386_i386;
389     print_insn = print_insn_i386;
390 #elif defined(TARGET_ARM)
391     print_insn = print_insn_arm;
392 #elif defined(TARGET_ALPHA)
393     print_insn = print_insn_alpha;
394 #elif defined(TARGET_SPARC)
395     print_insn = print_insn_sparc;
396 #ifdef TARGET_SPARC64
397     disasm_info.mach = bfd_mach_sparc_v9b;
398 #endif
399 #elif defined(TARGET_PPC)
400 #ifdef TARGET_PPC64
401     disasm_info.mach = bfd_mach_ppc64;
402 #else
403     disasm_info.mach = bfd_mach_ppc;
404 #endif
405     print_insn = print_insn_ppc;
406 #elif defined(TARGET_M68K)
407     print_insn = print_insn_m68k;
408 #elif defined(TARGET_MIPS)
409 #ifdef TARGET_WORDS_BIGENDIAN
410     print_insn = print_insn_big_mips;
411 #else
412     print_insn = print_insn_little_mips;
413 #endif
414 #elif defined(TARGET_SH4)
415     disasm_info.mach = bfd_mach_sh4;
416     print_insn = print_insn_sh;
417 #else
418     monitor_printf(mon, "0x" TARGET_FMT_lx
419                    ": Asm output not supported on this arch\n", pc);
420     return;
421 #endif
422 
423     for(i = 0; i < nb_insn; i++) {
424 	monitor_printf(mon, "0x" TARGET_FMT_lx ":  ", pc);
425 	count = print_insn(pc, &disasm_info);
426 	monitor_printf(mon, "\n");
427 	if (count < 0)
428 	    break;
429         pc += count;
430     }
431 }
432 #endif
433