root / disas.c @ d3c61721
History | View | Annotate | Download (8.4 kB)
1 | b9adb4a6 | bellard | /* General "disassemble this chunk" code. Used for debugging. */
|
---|---|---|---|
2 | 5bbe9299 | bellard | #include "config.h" |
3 | b9adb4a6 | bellard | #include "dis-asm.h" |
4 | b9adb4a6 | bellard | #include "elf.h" |
5 | aa0aa4fa | bellard | #include <errno.h> |
6 | b9adb4a6 | bellard | |
7 | c6105c0a | bellard | #include "cpu.h" |
8 | c6105c0a | bellard | #include "exec-all.h" |
9 | 9307c4c1 | bellard | #include "disas.h" |
10 | c6105c0a | bellard | |
11 | b9adb4a6 | bellard | /* Filled in by elfload.c. Simplistic, but will do for now. */
|
12 | e80cfcfc | bellard | struct syminfo *syminfos = NULL; |
13 | b9adb4a6 | bellard | |
14 | aa0aa4fa | bellard | /* Get LENGTH bytes from info's buffer, at target address memaddr.
|
15 | aa0aa4fa | bellard | Transfer them to myaddr. */
|
16 | aa0aa4fa | bellard | int
|
17 | aa0aa4fa | bellard | buffer_read_memory (memaddr, myaddr, length, info) |
18 | aa0aa4fa | bellard | bfd_vma memaddr; |
19 | aa0aa4fa | bellard | bfd_byte *myaddr; |
20 | aa0aa4fa | bellard | int length;
|
21 | aa0aa4fa | bellard | struct disassemble_info *info;
|
22 | aa0aa4fa | bellard | { |
23 | c6105c0a | bellard | if (memaddr < info->buffer_vma
|
24 | c6105c0a | bellard | || memaddr + length > info->buffer_vma + info->buffer_length) |
25 | c6105c0a | bellard | /* Out of bounds. Use EIO because GDB uses it. */
|
26 | c6105c0a | bellard | return EIO;
|
27 | c6105c0a | bellard | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); |
28 | c6105c0a | bellard | return 0; |
29 | aa0aa4fa | bellard | } |
30 | aa0aa4fa | bellard | |
31 | c6105c0a | bellard | /* Get LENGTH bytes from info's buffer, at target address memaddr.
|
32 | c6105c0a | bellard | Transfer them to myaddr. */
|
33 | c6105c0a | bellard | static int |
34 | c27004ec | bellard | target_read_memory (bfd_vma memaddr, |
35 | c27004ec | bellard | bfd_byte *myaddr, |
36 | c27004ec | bellard | int length,
|
37 | c27004ec | bellard | struct disassemble_info *info)
|
38 | c6105c0a | bellard | { |
39 | c6105c0a | bellard | int i;
|
40 | c6105c0a | bellard | for(i = 0; i < length; i++) { |
41 | c27004ec | bellard | myaddr[i] = ldub_code(memaddr + i); |
42 | c6105c0a | bellard | } |
43 | c6105c0a | bellard | return 0; |
44 | c6105c0a | bellard | } |
45 | c6105c0a | bellard | |
46 | aa0aa4fa | bellard | /* Print an error message. We can assume that this is in response to
|
47 | aa0aa4fa | bellard | an error return from buffer_read_memory. */
|
48 | aa0aa4fa | bellard | void
|
49 | aa0aa4fa | bellard | perror_memory (status, memaddr, info) |
50 | aa0aa4fa | bellard | int status;
|
51 | aa0aa4fa | bellard | bfd_vma memaddr; |
52 | aa0aa4fa | bellard | struct disassemble_info *info;
|
53 | aa0aa4fa | bellard | { |
54 | aa0aa4fa | bellard | if (status != EIO)
|
55 | aa0aa4fa | bellard | /* Can't happen. */
|
56 | aa0aa4fa | bellard | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
|
57 | aa0aa4fa | bellard | else
|
58 | aa0aa4fa | bellard | /* Actually, address between memaddr and memaddr + len was
|
59 | aa0aa4fa | bellard | out of bounds. */
|
60 | aa0aa4fa | bellard | (*info->fprintf_func) (info->stream, |
61 | d44b29c2 | bellard | "Address 0x%llx is out of bounds.\n", memaddr);
|
62 | aa0aa4fa | bellard | } |
63 | aa0aa4fa | bellard | |
64 | aa0aa4fa | bellard | /* This could be in a separate file, to save miniscule amounts of space
|
65 | aa0aa4fa | bellard | in statically linked executables. */
|
66 | aa0aa4fa | bellard | |
67 | aa0aa4fa | bellard | /* Just print the address is hex. This is included for completeness even
|
68 | aa0aa4fa | bellard | though both GDB and objdump provide their own (to print symbolic
|
69 | aa0aa4fa | bellard | addresses). */
|
70 | aa0aa4fa | bellard | |
71 | aa0aa4fa | bellard | void
|
72 | aa0aa4fa | bellard | generic_print_address (addr, info) |
73 | aa0aa4fa | bellard | bfd_vma addr; |
74 | aa0aa4fa | bellard | struct disassemble_info *info;
|
75 | aa0aa4fa | bellard | { |
76 | d44b29c2 | bellard | (*info->fprintf_func) (info->stream, "0x%llx", addr);
|
77 | aa0aa4fa | bellard | } |
78 | aa0aa4fa | bellard | |
79 | aa0aa4fa | bellard | /* Just return the given address. */
|
80 | aa0aa4fa | bellard | |
81 | aa0aa4fa | bellard | int
|
82 | aa0aa4fa | bellard | generic_symbol_at_address (addr, info) |
83 | aa0aa4fa | bellard | bfd_vma addr; |
84 | aa0aa4fa | bellard | struct disassemble_info * info;
|
85 | aa0aa4fa | bellard | { |
86 | aa0aa4fa | bellard | return 1; |
87 | aa0aa4fa | bellard | } |
88 | aa0aa4fa | bellard | |
89 | aa0aa4fa | bellard | bfd_vma bfd_getl32 (const bfd_byte *addr)
|
90 | aa0aa4fa | bellard | { |
91 | aa0aa4fa | bellard | unsigned long v; |
92 | aa0aa4fa | bellard | |
93 | aa0aa4fa | bellard | v = (unsigned long) addr[0]; |
94 | aa0aa4fa | bellard | v |= (unsigned long) addr[1] << 8; |
95 | aa0aa4fa | bellard | v |= (unsigned long) addr[2] << 16; |
96 | aa0aa4fa | bellard | v |= (unsigned long) addr[3] << 24; |
97 | aa0aa4fa | bellard | return (bfd_vma) v;
|
98 | aa0aa4fa | bellard | } |
99 | aa0aa4fa | bellard | |
100 | aa0aa4fa | bellard | bfd_vma bfd_getb32 (const bfd_byte *addr)
|
101 | aa0aa4fa | bellard | { |
102 | aa0aa4fa | bellard | unsigned long v; |
103 | aa0aa4fa | bellard | |
104 | aa0aa4fa | bellard | v = (unsigned long) addr[0] << 24; |
105 | aa0aa4fa | bellard | v |= (unsigned long) addr[1] << 16; |
106 | aa0aa4fa | bellard | v |= (unsigned long) addr[2] << 8; |
107 | aa0aa4fa | bellard | v |= (unsigned long) addr[3]; |
108 | aa0aa4fa | bellard | return (bfd_vma) v;
|
109 | aa0aa4fa | bellard | } |
110 | aa0aa4fa | bellard | |
111 | 95cbfc64 | bellard | /* Disassemble this for me please... (debugging). 'flags' is only used
|
112 | 95cbfc64 | bellard | for i386: non zero means 16 bit code */
|
113 | c27004ec | bellard | void target_disas(FILE *out, target_ulong code, unsigned long size, int flags) |
114 | b9adb4a6 | bellard | { |
115 | c27004ec | bellard | target_ulong pc; |
116 | b9adb4a6 | bellard | int count;
|
117 | b9adb4a6 | bellard | struct disassemble_info disasm_info;
|
118 | b9adb4a6 | bellard | int (*print_insn)(bfd_vma pc, disassemble_info *info);
|
119 | b9adb4a6 | bellard | |
120 | b9adb4a6 | bellard | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); |
121 | b9adb4a6 | bellard | |
122 | c27004ec | bellard | disasm_info.read_memory_func = target_read_memory; |
123 | c27004ec | bellard | disasm_info.buffer_vma = code; |
124 | c27004ec | bellard | disasm_info.buffer_length = size; |
125 | c27004ec | bellard | |
126 | c27004ec | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
127 | c27004ec | bellard | disasm_info.endian = BFD_ENDIAN_BIG; |
128 | c27004ec | bellard | #else
|
129 | c27004ec | bellard | disasm_info.endian = BFD_ENDIAN_LITTLE; |
130 | c27004ec | bellard | #endif
|
131 | c27004ec | bellard | #if defined(TARGET_I386)
|
132 | c27004ec | bellard | if (flags == 2) |
133 | c27004ec | bellard | disasm_info.mach = bfd_mach_x86_64; |
134 | c27004ec | bellard | else if (flags == 1) |
135 | c27004ec | bellard | disasm_info.mach = bfd_mach_i386_i8086; |
136 | c27004ec | bellard | else
|
137 | c27004ec | bellard | disasm_info.mach = bfd_mach_i386_i386; |
138 | c27004ec | bellard | print_insn = print_insn_i386; |
139 | c27004ec | bellard | #elif defined(TARGET_ARM)
|
140 | c27004ec | bellard | print_insn = print_insn_arm; |
141 | c27004ec | bellard | #elif defined(TARGET_SPARC)
|
142 | c27004ec | bellard | print_insn = print_insn_sparc; |
143 | c27004ec | bellard | #elif defined(TARGET_PPC)
|
144 | c27004ec | bellard | print_insn = print_insn_ppc; |
145 | c27004ec | bellard | #else
|
146 | c27004ec | bellard | fprintf(out, "Asm output not supported on this arch\n");
|
147 | c27004ec | bellard | return;
|
148 | c6105c0a | bellard | #endif
|
149 | c6105c0a | bellard | |
150 | c27004ec | bellard | for (pc = code; pc < code + size; pc += count) {
|
151 | c27004ec | bellard | #if TARGET_LONG_BITS == 64 |
152 | c27004ec | bellard | fprintf(out, "0x%016llx: ", pc);
|
153 | c27004ec | bellard | #else
|
154 | c27004ec | bellard | fprintf(out, "0x%08x: ", pc);
|
155 | c27004ec | bellard | #endif
|
156 | c27004ec | bellard | count = print_insn(pc, &disasm_info); |
157 | c27004ec | bellard | #if 0
|
158 | c27004ec | bellard | {
|
159 | c27004ec | bellard | int i;
|
160 | c27004ec | bellard | uint8_t b;
|
161 | c27004ec | bellard | fprintf(out, " {");
|
162 | c27004ec | bellard | for(i = 0; i < count; i++) {
|
163 | c27004ec | bellard | target_read_memory(pc + i, &b, 1, &disasm_info);
|
164 | c27004ec | bellard | fprintf(out, " %02x", b);
|
165 | c27004ec | bellard | }
|
166 | c27004ec | bellard | fprintf(out, " }");
|
167 | c27004ec | bellard | }
|
168 | c27004ec | bellard | #endif
|
169 | c27004ec | bellard | fprintf(out, "\n");
|
170 | c27004ec | bellard | if (count < 0) |
171 | c27004ec | bellard | break;
|
172 | c27004ec | bellard | } |
173 | c27004ec | bellard | } |
174 | c27004ec | bellard | |
175 | c27004ec | bellard | /* Disassemble this for me please... (debugging). */
|
176 | c27004ec | bellard | void disas(FILE *out, void *code, unsigned long size) |
177 | c27004ec | bellard | { |
178 | c27004ec | bellard | unsigned long pc; |
179 | c27004ec | bellard | int count;
|
180 | c27004ec | bellard | struct disassemble_info disasm_info;
|
181 | c27004ec | bellard | int (*print_insn)(bfd_vma pc, disassemble_info *info);
|
182 | c27004ec | bellard | |
183 | c27004ec | bellard | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); |
184 | c27004ec | bellard | |
185 | b9adb4a6 | bellard | disasm_info.buffer = code; |
186 | b9adb4a6 | bellard | disasm_info.buffer_vma = (unsigned long)code; |
187 | b9adb4a6 | bellard | disasm_info.buffer_length = size; |
188 | b9adb4a6 | bellard | |
189 | b9adb4a6 | bellard | #ifdef WORDS_BIGENDIAN
|
190 | c27004ec | bellard | disasm_info.endian = BFD_ENDIAN_BIG; |
191 | b9adb4a6 | bellard | #else
|
192 | c27004ec | bellard | disasm_info.endian = BFD_ENDIAN_LITTLE; |
193 | b9adb4a6 | bellard | #endif
|
194 | bc51c5c9 | bellard | #if defined(__i386__)
|
195 | c27004ec | bellard | disasm_info.mach = bfd_mach_i386_i386; |
196 | c27004ec | bellard | print_insn = print_insn_i386; |
197 | bc51c5c9 | bellard | #elif defined(__x86_64__)
|
198 | c27004ec | bellard | disasm_info.mach = bfd_mach_x86_64; |
199 | c27004ec | bellard | print_insn = print_insn_i386; |
200 | b9adb4a6 | bellard | #elif defined(__powerpc__)
|
201 | c27004ec | bellard | print_insn = print_insn_ppc; |
202 | a993ba85 | bellard | #elif defined(__alpha__)
|
203 | c27004ec | bellard | print_insn = print_insn_alpha; |
204 | aa0aa4fa | bellard | #elif defined(__sparc__)
|
205 | c27004ec | bellard | print_insn = print_insn_sparc; |
206 | aa0aa4fa | bellard | #elif defined(__arm__)
|
207 | c27004ec | bellard | print_insn = print_insn_arm; |
208 | b9adb4a6 | bellard | #else
|
209 | c27004ec | bellard | fprintf(out, "Asm output not supported on this arch\n");
|
210 | c27004ec | bellard | return;
|
211 | b9adb4a6 | bellard | #endif
|
212 | c27004ec | bellard | for (pc = (unsigned long)code; pc < (unsigned long)code + size; pc += count) { |
213 | c27004ec | bellard | fprintf(out, "0x%08lx: ", pc);
|
214 | aa0aa4fa | bellard | #ifdef __arm__
|
215 | aa0aa4fa | bellard | /* since data are included in the code, it is better to
|
216 | aa0aa4fa | bellard | display code data too */
|
217 | 95cbfc64 | bellard | if (is_host) {
|
218 | aa0aa4fa | bellard | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); |
219 | aa0aa4fa | bellard | } |
220 | aa0aa4fa | bellard | #endif
|
221 | c27004ec | bellard | count = print_insn(pc, &disasm_info); |
222 | b9adb4a6 | bellard | fprintf(out, "\n");
|
223 | b9adb4a6 | bellard | if (count < 0) |
224 | b9adb4a6 | bellard | break;
|
225 | b9adb4a6 | bellard | } |
226 | b9adb4a6 | bellard | } |
227 | b9adb4a6 | bellard | |
228 | b9adb4a6 | bellard | /* Look up symbol for debugging purpose. Returns "" if unknown. */
|
229 | c27004ec | bellard | const char *lookup_symbol(target_ulong orig_addr) |
230 | b9adb4a6 | bellard | { |
231 | b9adb4a6 | bellard | unsigned int i; |
232 | b9adb4a6 | bellard | /* Hack, because we know this is x86. */
|
233 | e80cfcfc | bellard | Elf32_Sym *sym; |
234 | e80cfcfc | bellard | struct syminfo *s;
|
235 | e80cfcfc | bellard | |
236 | e80cfcfc | bellard | for (s = syminfos; s; s = s->next) {
|
237 | e80cfcfc | bellard | sym = s->disas_symtab; |
238 | e80cfcfc | bellard | for (i = 0; i < s->disas_num_syms; i++) { |
239 | e80cfcfc | bellard | if (sym[i].st_shndx == SHN_UNDEF
|
240 | e80cfcfc | bellard | || sym[i].st_shndx >= SHN_LORESERVE) |
241 | e80cfcfc | bellard | continue;
|
242 | b9adb4a6 | bellard | |
243 | e80cfcfc | bellard | if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
|
244 | e80cfcfc | bellard | continue;
|
245 | b9adb4a6 | bellard | |
246 | c27004ec | bellard | if (orig_addr >= sym[i].st_value
|
247 | c27004ec | bellard | && orig_addr < sym[i].st_value + sym[i].st_size) |
248 | e80cfcfc | bellard | return s->disas_strtab + sym[i].st_name;
|
249 | e80cfcfc | bellard | } |
250 | b9adb4a6 | bellard | } |
251 | b9adb4a6 | bellard | return ""; |
252 | b9adb4a6 | bellard | } |
253 | 9307c4c1 | bellard | |
254 | 9307c4c1 | bellard | #if !defined(CONFIG_USER_ONLY)
|
255 | 9307c4c1 | bellard | |
256 | 3d2cfdf1 | bellard | void term_vprintf(const char *fmt, va_list ap); |
257 | 3d2cfdf1 | bellard | void term_printf(const char *fmt, ...); |
258 | 3d2cfdf1 | bellard | |
259 | 9307c4c1 | bellard | static int monitor_disas_is_physical; |
260 | 9307c4c1 | bellard | |
261 | 9307c4c1 | bellard | static int |
262 | 9307c4c1 | bellard | monitor_read_memory (memaddr, myaddr, length, info) |
263 | 9307c4c1 | bellard | bfd_vma memaddr; |
264 | 9307c4c1 | bellard | bfd_byte *myaddr; |
265 | 9307c4c1 | bellard | int length;
|
266 | 9307c4c1 | bellard | struct disassemble_info *info;
|
267 | 9307c4c1 | bellard | { |
268 | 9307c4c1 | bellard | if (monitor_disas_is_physical) {
|
269 | 9307c4c1 | bellard | cpu_physical_memory_rw(memaddr, myaddr, length, 0);
|
270 | 9307c4c1 | bellard | } else {
|
271 | 9307c4c1 | bellard | cpu_memory_rw_debug(cpu_single_env, memaddr,myaddr, length, 0);
|
272 | 9307c4c1 | bellard | } |
273 | 9307c4c1 | bellard | return 0; |
274 | 9307c4c1 | bellard | } |
275 | 9307c4c1 | bellard | |
276 | 3d2cfdf1 | bellard | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
277 | 3d2cfdf1 | bellard | { |
278 | 3d2cfdf1 | bellard | va_list ap; |
279 | 3d2cfdf1 | bellard | va_start(ap, fmt); |
280 | 3d2cfdf1 | bellard | term_vprintf(fmt, ap); |
281 | 3d2cfdf1 | bellard | va_end(ap); |
282 | 3d2cfdf1 | bellard | return 0; |
283 | 3d2cfdf1 | bellard | } |
284 | 3d2cfdf1 | bellard | |
285 | 9307c4c1 | bellard | void monitor_disas(target_ulong pc, int nb_insn, int is_physical, int flags) |
286 | 9307c4c1 | bellard | { |
287 | 9307c4c1 | bellard | int count, i;
|
288 | 9307c4c1 | bellard | struct disassemble_info disasm_info;
|
289 | 9307c4c1 | bellard | int (*print_insn)(bfd_vma pc, disassemble_info *info);
|
290 | 9307c4c1 | bellard | |
291 | 3d2cfdf1 | bellard | INIT_DISASSEMBLE_INFO(disasm_info, NULL, monitor_fprintf);
|
292 | 9307c4c1 | bellard | |
293 | 9307c4c1 | bellard | monitor_disas_is_physical = is_physical; |
294 | 9307c4c1 | bellard | disasm_info.read_memory_func = monitor_read_memory; |
295 | 9307c4c1 | bellard | |
296 | 9307c4c1 | bellard | disasm_info.buffer_vma = pc; |
297 | 9307c4c1 | bellard | |
298 | 9307c4c1 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
299 | 9307c4c1 | bellard | disasm_info.endian = BFD_ENDIAN_BIG; |
300 | 9307c4c1 | bellard | #else
|
301 | 9307c4c1 | bellard | disasm_info.endian = BFD_ENDIAN_LITTLE; |
302 | 9307c4c1 | bellard | #endif
|
303 | 9307c4c1 | bellard | #if defined(TARGET_I386)
|
304 | 9307c4c1 | bellard | if (!flags)
|
305 | 9307c4c1 | bellard | disasm_info.mach = bfd_mach_i386_i386; |
306 | 9307c4c1 | bellard | else
|
307 | 9307c4c1 | bellard | disasm_info.mach = bfd_mach_i386_i8086; |
308 | 9307c4c1 | bellard | print_insn = print_insn_i386; |
309 | 9307c4c1 | bellard | #elif defined(TARGET_ARM)
|
310 | 9307c4c1 | bellard | print_insn = print_insn_arm; |
311 | 9307c4c1 | bellard | #elif defined(TARGET_SPARC)
|
312 | 9307c4c1 | bellard | print_insn = print_insn_sparc; |
313 | 9307c4c1 | bellard | #elif defined(TARGET_PPC)
|
314 | 9307c4c1 | bellard | print_insn = print_insn_ppc; |
315 | 9307c4c1 | bellard | #else
|
316 | 7fe48483 | bellard | term_printf("Asm output not supported on this arch\n");
|
317 | 9307c4c1 | bellard | return;
|
318 | 9307c4c1 | bellard | #endif
|
319 | 9307c4c1 | bellard | |
320 | 9307c4c1 | bellard | for(i = 0; i < nb_insn; i++) { |
321 | 3d2cfdf1 | bellard | term_printf("0x%08lx: ", (unsigned long)pc); |
322 | 9307c4c1 | bellard | count = print_insn(pc, &disasm_info); |
323 | 3d2cfdf1 | bellard | term_printf("\n");
|
324 | 9307c4c1 | bellard | if (count < 0) |
325 | 9307c4c1 | bellard | break;
|
326 | 9307c4c1 | bellard | pc += count; |
327 | 9307c4c1 | bellard | } |
328 | 9307c4c1 | bellard | } |
329 | 9307c4c1 | bellard | #endif |