1 /* General "disassemble this chunk" code. Used for debugging. */
11 /* Filled in by elfload.c. Simplistic, but will do for now. */
12 unsigned int disas_num_syms;
14 const char *disas_strtab;
16 /* Get LENGTH bytes from info's buffer, at target address memaddr.
17 Transfer them to myaddr. */
19 buffer_read_memory (memaddr, myaddr, length, info)
23 struct disassemble_info *info;
25 if (memaddr < info->buffer_vma
26 || memaddr + length > info->buffer_vma + info->buffer_length)
27 /* Out of bounds. Use EIO because GDB uses it. */
29 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
33 #if !defined(CONFIG_USER_ONLY)
34 /* Get LENGTH bytes from info's buffer, at target address memaddr.
35 Transfer them to myaddr. */
37 target_read_memory (memaddr, myaddr, length, info)
41 struct disassemble_info *info;
44 for(i = 0; i < length; i++) {
45 myaddr[i] = ldub_code((void *)((long)memaddr + i));
51 /* Print an error message. We can assume that this is in response to
52 an error return from buffer_read_memory. */
54 perror_memory (status, memaddr, info)
57 struct disassemble_info *info;
61 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
63 /* Actually, address between memaddr and memaddr + len was
65 (*info->fprintf_func) (info->stream,
66 "Address 0x%llx is out of bounds.\n", memaddr);
69 /* This could be in a separate file, to save miniscule amounts of space
70 in statically linked executables. */
72 /* Just print the address is hex. This is included for completeness even
73 though both GDB and objdump provide their own (to print symbolic
77 generic_print_address (addr, info)
79 struct disassemble_info *info;
81 (*info->fprintf_func) (info->stream, "0x%llx", addr);
84 /* Just return the given address. */
87 generic_symbol_at_address (addr, info)
89 struct disassemble_info * info;
94 bfd_vma bfd_getl32 (const bfd_byte *addr)
98 v = (unsigned long) addr[0];
99 v |= (unsigned long) addr[1] << 8;
100 v |= (unsigned long) addr[2] << 16;
101 v |= (unsigned long) addr[3] << 24;
105 bfd_vma bfd_getb32 (const bfd_byte *addr)
109 v = (unsigned long) addr[0] << 24;
110 v |= (unsigned long) addr[1] << 16;
111 v |= (unsigned long) addr[2] << 8;
112 v |= (unsigned long) addr[3];
116 /* Disassemble this for me please... (debugging). 'flags' is only used
117 for i386: non zero means 16 bit code */
118 void disas(FILE *out, void *code, unsigned long size, int is_host, int flags)
122 struct disassemble_info disasm_info;
123 int (*print_insn)(bfd_vma pc, disassemble_info *info);
125 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
127 #if !defined(CONFIG_USER_ONLY)
129 disasm_info.read_memory_func = target_read_memory;
133 disasm_info.buffer = code;
134 disasm_info.buffer_vma = (unsigned long)code;
135 disasm_info.buffer_length = size;
138 #ifdef WORDS_BIGENDIAN
139 disasm_info.endian = BFD_ENDIAN_BIG;
141 disasm_info.endian = BFD_ENDIAN_LITTLE;
143 #if defined(__i386__)
144 disasm_info.mach = bfd_mach_i386_i386;
145 print_insn = print_insn_i386;
146 #elif defined(__x86_64__)
147 disasm_info.mach = bfd_mach_x86_64;
148 print_insn = print_insn_i386;
149 #elif defined(__powerpc__)
150 print_insn = print_insn_ppc;
151 #elif defined(__alpha__)
152 print_insn = print_insn_alpha;
153 #elif defined(__sparc__)
154 print_insn = print_insn_sparc;
155 #elif defined(__arm__)
156 print_insn = print_insn_arm;
158 fprintf(out, "Asm output not supported on this arch\n");
162 #ifdef TARGET_WORDS_BIGENDIAN
163 disasm_info.endian = BFD_ENDIAN_BIG;
165 disasm_info.endian = BFD_ENDIAN_LITTLE;
167 #if defined(TARGET_I386)
169 disasm_info.mach = bfd_mach_i386_i386;
171 disasm_info.mach = bfd_mach_i386_i8086;
172 print_insn = print_insn_i386;
173 #elif defined(TARGET_ARM)
174 print_insn = print_insn_arm;
175 #elif defined(TARGET_SPARC)
176 print_insn = print_insn_sparc;
177 #elif defined(TARGET_PPC)
178 print_insn = print_insn_ppc;
180 fprintf(out, "Asm output not supported on this arch\n");
185 for (pc = code; pc < (uint8_t *)code + size; pc += count) {
186 fprintf(out, "0x%08lx: ", (long)pc);
188 /* since data are included in the code, it is better to
189 display code data too */
191 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
194 count = print_insn((unsigned long)pc, &disasm_info);
201 /* Look up symbol for debugging purpose. Returns "" if unknown. */
202 const char *lookup_symbol(void *orig_addr)
205 /* Hack, because we know this is x86. */
206 Elf32_Sym *sym = disas_symtab;
208 for (i = 0; i < disas_num_syms; i++) {
209 if (sym[i].st_shndx == SHN_UNDEF
210 || sym[i].st_shndx >= SHN_LORESERVE)
213 if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
216 if ((long)orig_addr >= sym[i].st_value
217 && (long)orig_addr < sym[i].st_value + sym[i].st_size)
218 return disas_strtab + sym[i].st_name;
223 #if !defined(CONFIG_USER_ONLY)
225 static int monitor_disas_is_physical;
228 monitor_read_memory (memaddr, myaddr, length, info)
232 struct disassemble_info *info;
234 if (monitor_disas_is_physical) {
235 cpu_physical_memory_rw(memaddr, myaddr, length, 0);
237 cpu_memory_rw_debug(cpu_single_env, memaddr,myaddr, length, 0);
242 void monitor_disas(target_ulong pc, int nb_insn, int is_physical, int flags)
246 struct disassemble_info disasm_info;
247 int (*print_insn)(bfd_vma pc, disassemble_info *info);
251 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
253 monitor_disas_is_physical = is_physical;
254 disasm_info.read_memory_func = monitor_read_memory;
256 disasm_info.buffer_vma = pc;
258 #ifdef TARGET_WORDS_BIGENDIAN
259 disasm_info.endian = BFD_ENDIAN_BIG;
261 disasm_info.endian = BFD_ENDIAN_LITTLE;
263 #if defined(TARGET_I386)
265 disasm_info.mach = bfd_mach_i386_i386;
267 disasm_info.mach = bfd_mach_i386_i8086;
268 print_insn = print_insn_i386;
269 #elif defined(TARGET_ARM)
270 print_insn = print_insn_arm;
271 #elif defined(TARGET_SPARC)
272 print_insn = print_insn_sparc;
273 #elif defined(TARGET_PPC)
274 print_insn = print_insn_ppc;
276 fprintf(out, "Asm output not supported on this arch\n");
280 for(i = 0; i < nb_insn; i++) {
281 fprintf(out, "0x%08lx: ", (unsigned long)pc);
282 count = print_insn(pc, &disasm_info);