blob: bfab8c3bb76e6c23006232d0f86aa96739b343c6 [file] [log] [blame]
bellardb9adb4a2003-04-29 20:41:16 +00001/* General "disassemble this chunk" code. Used for debugging. */
bellard5bbe9292003-06-09 19:38:38 +00002#include "config.h"
bellardb9adb4a2003-04-29 20:41:16 +00003#include "dis-asm.h"
bellardb9adb4a2003-04-29 20:41:16 +00004#include "elf.h"
bellardaa0aa4f2003-06-09 15:23:31 +00005#include <errno.h>
bellardb9adb4a2003-04-29 20:41:16 +00006
bellardc6105c02003-10-27 21:13:58 +00007#include "cpu.h"
8#include "exec-all.h"
bellard9307c4c2004-04-04 12:57:25 +00009#include "disas.h"
bellardc6105c02003-10-27 21:13:58 +000010
bellardb9adb4a2003-04-29 20:41:16 +000011/* Filled in by elfload.c. Simplistic, but will do for now. */
bellarde80cfcf2004-12-19 23:18:01 +000012struct syminfo *syminfos = NULL;
bellardb9adb4a2003-04-29 20:41:16 +000013
bellardaa0aa4f2003-06-09 15:23:31 +000014/* Get LENGTH bytes from info's buffer, at target address memaddr.
15 Transfer them to myaddr. */
16int
17buffer_read_memory (memaddr, myaddr, length, info)
18 bfd_vma memaddr;
19 bfd_byte *myaddr;
20 int length;
21 struct disassemble_info *info;
22{
bellardc6105c02003-10-27 21:13:58 +000023 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;
bellardaa0aa4f2003-06-09 15:23:31 +000029}
30
bellardc6105c02003-10-27 21:13:58 +000031#if !defined(CONFIG_USER_ONLY)
32/* Get LENGTH bytes from info's buffer, at target address memaddr.
33 Transfer them to myaddr. */
34static int
35target_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++) {
bellardbaf8ebf2003-10-27 23:57:40 +000043 myaddr[i] = ldub_code((void *)((long)memaddr + i));
bellardc6105c02003-10-27 21:13:58 +000044 }
45 return 0;
46}
47#endif
48
bellardaa0aa4f2003-06-09 15:23:31 +000049/* Print an error message. We can assume that this is in response to
50 an error return from buffer_read_memory. */
51void
52perror_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,
bellardd44b29c2003-07-13 17:29:55 +000064 "Address 0x%llx is out of bounds.\n", memaddr);
bellardaa0aa4f2003-06-09 15:23:31 +000065}
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
74void
75generic_print_address (addr, info)
76 bfd_vma addr;
77 struct disassemble_info *info;
78{
bellardd44b29c2003-07-13 17:29:55 +000079 (*info->fprintf_func) (info->stream, "0x%llx", addr);
bellardaa0aa4f2003-06-09 15:23:31 +000080}
81
82/* Just return the given address. */
83
84int
85generic_symbol_at_address (addr, info)
86 bfd_vma addr;
87 struct disassemble_info * info;
88{
89 return 1;
90}
91
92bfd_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
103bfd_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
bellard95cbfc62003-06-15 19:44:10 +0000114/* Disassemble this for me please... (debugging). 'flags' is only used
115 for i386: non zero means 16 bit code */
116void disas(FILE *out, void *code, unsigned long size, int is_host, int flags)
bellardb9adb4a2003-04-29 20:41:16 +0000117{
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
bellardc6105c02003-10-27 21:13:58 +0000125#if !defined(CONFIG_USER_ONLY)
126 if (!is_host) {
127 disasm_info.read_memory_func = target_read_memory;
128 }
129#endif
130
bellardb9adb4a2003-04-29 20:41:16 +0000131 disasm_info.buffer = code;
132 disasm_info.buffer_vma = (unsigned long)code;
133 disasm_info.buffer_length = size;
134
bellard95cbfc62003-06-15 19:44:10 +0000135 if (is_host) {
bellardb9adb4a2003-04-29 20:41:16 +0000136#ifdef WORDS_BIGENDIAN
137 disasm_info.endian = BFD_ENDIAN_BIG;
138#else
139 disasm_info.endian = BFD_ENDIAN_LITTLE;
140#endif
bellardbc51c5c2004-03-17 23:46:04 +0000141#if defined(__i386__)
bellardb9adb4a2003-04-29 20:41:16 +0000142 disasm_info.mach = bfd_mach_i386_i386;
143 print_insn = print_insn_i386;
bellardbc51c5c2004-03-17 23:46:04 +0000144#elif defined(__x86_64__)
145 disasm_info.mach = bfd_mach_x86_64;
146 print_insn = print_insn_i386;
bellardb9adb4a2003-04-29 20:41:16 +0000147#elif defined(__powerpc__)
148 print_insn = print_insn_ppc;
bellarda993ba82003-05-11 12:25:45 +0000149#elif defined(__alpha__)
150 print_insn = print_insn_alpha;
bellardaa0aa4f2003-06-09 15:23:31 +0000151#elif defined(__sparc__)
152 print_insn = print_insn_sparc;
153#elif defined(__arm__)
154 print_insn = print_insn_arm;
bellardb9adb4a2003-04-29 20:41:16 +0000155#else
156 fprintf(out, "Asm output not supported on this arch\n");
157 return;
158#endif
159 } else {
bellard95cbfc62003-06-15 19:44:10 +0000160#ifdef TARGET_WORDS_BIGENDIAN
161 disasm_info.endian = BFD_ENDIAN_BIG;
162#else
bellardb9adb4a2003-04-29 20:41:16 +0000163 disasm_info.endian = BFD_ENDIAN_LITTLE;
bellard95cbfc62003-06-15 19:44:10 +0000164#endif
165#if defined(TARGET_I386)
166 if (!flags)
bellardb9adb4a2003-04-29 20:41:16 +0000167 disasm_info.mach = bfd_mach_i386_i386;
168 else
169 disasm_info.mach = bfd_mach_i386_i8086;
170 print_insn = print_insn_i386;
bellard95cbfc62003-06-15 19:44:10 +0000171#elif defined(TARGET_ARM)
172 print_insn = print_insn_arm;
bellard93ac68b2003-09-30 20:57:29 +0000173#elif defined(TARGET_SPARC)
174 print_insn = print_insn_sparc;
bellard67867302003-11-23 17:05:30 +0000175#elif defined(TARGET_PPC)
176 print_insn = print_insn_ppc;
bellard95cbfc62003-06-15 19:44:10 +0000177#else
178 fprintf(out, "Asm output not supported on this arch\n");
179 return;
180#endif
bellardb9adb4a2003-04-29 20:41:16 +0000181 }
182
183 for (pc = code; pc < (uint8_t *)code + size; pc += count) {
184 fprintf(out, "0x%08lx: ", (long)pc);
bellardaa0aa4f2003-06-09 15:23:31 +0000185#ifdef __arm__
186 /* since data are included in the code, it is better to
187 display code data too */
bellard95cbfc62003-06-15 19:44:10 +0000188 if (is_host) {
bellardaa0aa4f2003-06-09 15:23:31 +0000189 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
190 }
191#endif
bellard08351fb2003-05-25 16:42:20 +0000192 count = print_insn((unsigned long)pc, &disasm_info);
bellardb9adb4a2003-04-29 20:41:16 +0000193 fprintf(out, "\n");
194 if (count < 0)
195 break;
196 }
197}
198
199/* Look up symbol for debugging purpose. Returns "" if unknown. */
200const char *lookup_symbol(void *orig_addr)
201{
202 unsigned int i;
203 /* Hack, because we know this is x86. */
bellarde80cfcf2004-12-19 23:18:01 +0000204 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;
bellardb9adb4a2003-04-29 20:41:16 +0000213
bellarde80cfcf2004-12-19 23:18:01 +0000214 if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
215 continue;
bellardb9adb4a2003-04-29 20:41:16 +0000216
bellarde80cfcf2004-12-19 23:18:01 +0000217 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 }
bellardb9adb4a2003-04-29 20:41:16 +0000221 }
222 return "";
223}
bellard9307c4c2004-04-04 12:57:25 +0000224
225#if !defined(CONFIG_USER_ONLY)
226
bellard3d2cfdf2004-08-01 21:49:07 +0000227void term_vprintf(const char *fmt, va_list ap);
228void term_printf(const char *fmt, ...);
229
bellard9307c4c2004-04-04 12:57:25 +0000230static int monitor_disas_is_physical;
231
232static int
233monitor_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
bellard3d2cfdf2004-08-01 21:49:07 +0000247static 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
bellard9307c4c2004-04-04 12:57:25 +0000256void monitor_disas(target_ulong pc, int nb_insn, int is_physical, int flags)
257{
bellard9307c4c2004-04-04 12:57:25 +0000258 int count, i;
259 struct disassemble_info disasm_info;
260 int (*print_insn)(bfd_vma pc, disassemble_info *info);
261
bellard3d2cfdf2004-08-01 21:49:07 +0000262 INIT_DISASSEMBLE_INFO(disasm_info, NULL, monitor_fprintf);
bellard9307c4c2004-04-04 12:57:25 +0000263
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
bellard7fe48482004-10-09 18:08:01 +0000287 term_printf("Asm output not supported on this arch\n");
bellard9307c4c2004-04-04 12:57:25 +0000288 return;
289#endif
290
291 for(i = 0; i < nb_insn; i++) {
bellard3d2cfdf2004-08-01 21:49:07 +0000292 term_printf("0x%08lx: ", (unsigned long)pc);
bellard9307c4c2004-04-04 12:57:25 +0000293 count = print_insn(pc, &disasm_info);
bellard3d2cfdf2004-08-01 21:49:07 +0000294 term_printf("\n");
bellard9307c4c2004-04-04 12:57:25 +0000295 if (count < 0)
296 break;
297 pc += count;
298 }
299}
300#endif