root / disas.c @ c321f673
History | View | Annotate | Download (5.5 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 "disas.h" |
5 | b9adb4a6 | bellard | #include "elf.h" |
6 | aa0aa4fa | bellard | #include <errno.h> |
7 | b9adb4a6 | bellard | |
8 | c6105c0a | bellard | #include "cpu.h" |
9 | c6105c0a | bellard | #include "exec-all.h" |
10 | c6105c0a | bellard | |
11 | b9adb4a6 | bellard | /* Filled in by elfload.c. Simplistic, but will do for now. */
|
12 | b9adb4a6 | bellard | unsigned int disas_num_syms; |
13 | b9adb4a6 | bellard | void *disas_symtab;
|
14 | b9adb4a6 | bellard | const char *disas_strtab; |
15 | b9adb4a6 | bellard | |
16 | aa0aa4fa | bellard | /* Get LENGTH bytes from info's buffer, at target address memaddr.
|
17 | aa0aa4fa | bellard | Transfer them to myaddr. */
|
18 | aa0aa4fa | bellard | int
|
19 | aa0aa4fa | bellard | buffer_read_memory (memaddr, myaddr, length, info) |
20 | aa0aa4fa | bellard | bfd_vma memaddr; |
21 | aa0aa4fa | bellard | bfd_byte *myaddr; |
22 | aa0aa4fa | bellard | int length;
|
23 | aa0aa4fa | bellard | struct disassemble_info *info;
|
24 | aa0aa4fa | bellard | { |
25 | c6105c0a | bellard | if (memaddr < info->buffer_vma
|
26 | c6105c0a | bellard | || memaddr + length > info->buffer_vma + info->buffer_length) |
27 | c6105c0a | bellard | /* Out of bounds. Use EIO because GDB uses it. */
|
28 | c6105c0a | bellard | return EIO;
|
29 | c6105c0a | bellard | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); |
30 | c6105c0a | bellard | return 0; |
31 | aa0aa4fa | bellard | } |
32 | aa0aa4fa | bellard | |
33 | c6105c0a | bellard | #if !defined(CONFIG_USER_ONLY)
|
34 | c6105c0a | bellard | /* Get LENGTH bytes from info's buffer, at target address memaddr.
|
35 | c6105c0a | bellard | Transfer them to myaddr. */
|
36 | c6105c0a | bellard | static int |
37 | c6105c0a | bellard | target_read_memory (memaddr, myaddr, length, info) |
38 | c6105c0a | bellard | bfd_vma memaddr; |
39 | c6105c0a | bellard | bfd_byte *myaddr; |
40 | c6105c0a | bellard | int length;
|
41 | c6105c0a | bellard | struct disassemble_info *info;
|
42 | c6105c0a | bellard | { |
43 | c6105c0a | bellard | int i;
|
44 | c6105c0a | bellard | for(i = 0; i < length; i++) { |
45 | baf8ebf0 | bellard | myaddr[i] = ldub_code((void *)((long)memaddr + i)); |
46 | c6105c0a | bellard | } |
47 | c6105c0a | bellard | return 0; |
48 | c6105c0a | bellard | } |
49 | c6105c0a | bellard | #endif
|
50 | c6105c0a | bellard | |
51 | aa0aa4fa | bellard | /* Print an error message. We can assume that this is in response to
|
52 | aa0aa4fa | bellard | an error return from buffer_read_memory. */
|
53 | aa0aa4fa | bellard | void
|
54 | aa0aa4fa | bellard | perror_memory (status, memaddr, info) |
55 | aa0aa4fa | bellard | int status;
|
56 | aa0aa4fa | bellard | bfd_vma memaddr; |
57 | aa0aa4fa | bellard | struct disassemble_info *info;
|
58 | aa0aa4fa | bellard | { |
59 | aa0aa4fa | bellard | if (status != EIO)
|
60 | aa0aa4fa | bellard | /* Can't happen. */
|
61 | aa0aa4fa | bellard | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
|
62 | aa0aa4fa | bellard | else
|
63 | aa0aa4fa | bellard | /* Actually, address between memaddr and memaddr + len was
|
64 | aa0aa4fa | bellard | out of bounds. */
|
65 | aa0aa4fa | bellard | (*info->fprintf_func) (info->stream, |
66 | d44b29c2 | bellard | "Address 0x%llx is out of bounds.\n", memaddr);
|
67 | aa0aa4fa | bellard | } |
68 | aa0aa4fa | bellard | |
69 | aa0aa4fa | bellard | /* This could be in a separate file, to save miniscule amounts of space
|
70 | aa0aa4fa | bellard | in statically linked executables. */
|
71 | aa0aa4fa | bellard | |
72 | aa0aa4fa | bellard | /* Just print the address is hex. This is included for completeness even
|
73 | aa0aa4fa | bellard | though both GDB and objdump provide their own (to print symbolic
|
74 | aa0aa4fa | bellard | addresses). */
|
75 | aa0aa4fa | bellard | |
76 | aa0aa4fa | bellard | void
|
77 | aa0aa4fa | bellard | generic_print_address (addr, info) |
78 | aa0aa4fa | bellard | bfd_vma addr; |
79 | aa0aa4fa | bellard | struct disassemble_info *info;
|
80 | aa0aa4fa | bellard | { |
81 | d44b29c2 | bellard | (*info->fprintf_func) (info->stream, "0x%llx", addr);
|
82 | aa0aa4fa | bellard | } |
83 | aa0aa4fa | bellard | |
84 | aa0aa4fa | bellard | /* Just return the given address. */
|
85 | aa0aa4fa | bellard | |
86 | aa0aa4fa | bellard | int
|
87 | aa0aa4fa | bellard | generic_symbol_at_address (addr, info) |
88 | aa0aa4fa | bellard | bfd_vma addr; |
89 | aa0aa4fa | bellard | struct disassemble_info * info;
|
90 | aa0aa4fa | bellard | { |
91 | aa0aa4fa | bellard | return 1; |
92 | aa0aa4fa | bellard | } |
93 | aa0aa4fa | bellard | |
94 | aa0aa4fa | bellard | bfd_vma bfd_getl32 (const bfd_byte *addr)
|
95 | aa0aa4fa | bellard | { |
96 | aa0aa4fa | bellard | unsigned long v; |
97 | aa0aa4fa | bellard | |
98 | aa0aa4fa | bellard | v = (unsigned long) addr[0]; |
99 | aa0aa4fa | bellard | v |= (unsigned long) addr[1] << 8; |
100 | aa0aa4fa | bellard | v |= (unsigned long) addr[2] << 16; |
101 | aa0aa4fa | bellard | v |= (unsigned long) addr[3] << 24; |
102 | aa0aa4fa | bellard | return (bfd_vma) v;
|
103 | aa0aa4fa | bellard | } |
104 | aa0aa4fa | bellard | |
105 | aa0aa4fa | bellard | bfd_vma bfd_getb32 (const bfd_byte *addr)
|
106 | aa0aa4fa | bellard | { |
107 | aa0aa4fa | bellard | unsigned long v; |
108 | aa0aa4fa | bellard | |
109 | aa0aa4fa | bellard | v = (unsigned long) addr[0] << 24; |
110 | aa0aa4fa | bellard | v |= (unsigned long) addr[1] << 16; |
111 | aa0aa4fa | bellard | v |= (unsigned long) addr[2] << 8; |
112 | aa0aa4fa | bellard | v |= (unsigned long) addr[3]; |
113 | aa0aa4fa | bellard | return (bfd_vma) v;
|
114 | aa0aa4fa | bellard | } |
115 | aa0aa4fa | bellard | |
116 | 95cbfc64 | bellard | /* Disassemble this for me please... (debugging). 'flags' is only used
|
117 | 95cbfc64 | bellard | for i386: non zero means 16 bit code */
|
118 | 95cbfc64 | bellard | void disas(FILE *out, void *code, unsigned long size, int is_host, int flags) |
119 | b9adb4a6 | bellard | { |
120 | b9adb4a6 | bellard | uint8_t *pc; |
121 | b9adb4a6 | bellard | int count;
|
122 | b9adb4a6 | bellard | struct disassemble_info disasm_info;
|
123 | b9adb4a6 | bellard | int (*print_insn)(bfd_vma pc, disassemble_info *info);
|
124 | b9adb4a6 | bellard | |
125 | b9adb4a6 | bellard | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); |
126 | b9adb4a6 | bellard | |
127 | c6105c0a | bellard | #if !defined(CONFIG_USER_ONLY)
|
128 | c6105c0a | bellard | if (!is_host) {
|
129 | c6105c0a | bellard | disasm_info.read_memory_func = target_read_memory; |
130 | c6105c0a | bellard | } |
131 | c6105c0a | bellard | #endif
|
132 | c6105c0a | bellard | |
133 | b9adb4a6 | bellard | disasm_info.buffer = code; |
134 | b9adb4a6 | bellard | disasm_info.buffer_vma = (unsigned long)code; |
135 | b9adb4a6 | bellard | disasm_info.buffer_length = size; |
136 | b9adb4a6 | bellard | |
137 | 95cbfc64 | bellard | if (is_host) {
|
138 | b9adb4a6 | bellard | #ifdef WORDS_BIGENDIAN
|
139 | b9adb4a6 | bellard | disasm_info.endian = BFD_ENDIAN_BIG; |
140 | b9adb4a6 | bellard | #else
|
141 | b9adb4a6 | bellard | disasm_info.endian = BFD_ENDIAN_LITTLE; |
142 | b9adb4a6 | bellard | #endif
|
143 | b9adb4a6 | bellard | #ifdef __i386__
|
144 | b9adb4a6 | bellard | disasm_info.mach = bfd_mach_i386_i386; |
145 | b9adb4a6 | bellard | print_insn = print_insn_i386; |
146 | b9adb4a6 | bellard | #elif defined(__powerpc__)
|
147 | b9adb4a6 | bellard | print_insn = print_insn_ppc; |
148 | a993ba85 | bellard | #elif defined(__alpha__)
|
149 | a993ba85 | bellard | print_insn = print_insn_alpha; |
150 | aa0aa4fa | bellard | #elif defined(__sparc__)
|
151 | aa0aa4fa | bellard | print_insn = print_insn_sparc; |
152 | aa0aa4fa | bellard | #elif defined(__arm__)
|
153 | aa0aa4fa | bellard | print_insn = print_insn_arm; |
154 | b9adb4a6 | bellard | #else
|
155 | b9adb4a6 | bellard | fprintf(out, "Asm output not supported on this arch\n");
|
156 | b9adb4a6 | bellard | return;
|
157 | b9adb4a6 | bellard | #endif
|
158 | b9adb4a6 | bellard | } else {
|
159 | 95cbfc64 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
160 | 95cbfc64 | bellard | disasm_info.endian = BFD_ENDIAN_BIG; |
161 | 95cbfc64 | bellard | #else
|
162 | b9adb4a6 | bellard | disasm_info.endian = BFD_ENDIAN_LITTLE; |
163 | 95cbfc64 | bellard | #endif
|
164 | 95cbfc64 | bellard | #if defined(TARGET_I386)
|
165 | 95cbfc64 | bellard | if (!flags)
|
166 | b9adb4a6 | bellard | disasm_info.mach = bfd_mach_i386_i386; |
167 | b9adb4a6 | bellard | else
|
168 | b9adb4a6 | bellard | disasm_info.mach = bfd_mach_i386_i8086; |
169 | b9adb4a6 | bellard | print_insn = print_insn_i386; |
170 | 95cbfc64 | bellard | #elif defined(TARGET_ARM)
|
171 | 95cbfc64 | bellard | print_insn = print_insn_arm; |
172 | 93ac68bc | bellard | #elif defined(TARGET_SPARC)
|
173 | 93ac68bc | bellard | print_insn = print_insn_sparc; |
174 | 95cbfc64 | bellard | #else
|
175 | 95cbfc64 | bellard | fprintf(out, "Asm output not supported on this arch\n");
|
176 | 95cbfc64 | bellard | return;
|
177 | 95cbfc64 | bellard | #endif
|
178 | b9adb4a6 | bellard | } |
179 | b9adb4a6 | bellard | |
180 | b9adb4a6 | bellard | for (pc = code; pc < (uint8_t *)code + size; pc += count) {
|
181 | b9adb4a6 | bellard | fprintf(out, "0x%08lx: ", (long)pc); |
182 | aa0aa4fa | bellard | #ifdef __arm__
|
183 | aa0aa4fa | bellard | /* since data are included in the code, it is better to
|
184 | aa0aa4fa | bellard | display code data too */
|
185 | 95cbfc64 | bellard | if (is_host) {
|
186 | aa0aa4fa | bellard | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); |
187 | aa0aa4fa | bellard | } |
188 | aa0aa4fa | bellard | #endif
|
189 | 08351fb3 | bellard | count = print_insn((unsigned long)pc, &disasm_info); |
190 | b9adb4a6 | bellard | fprintf(out, "\n");
|
191 | b9adb4a6 | bellard | if (count < 0) |
192 | b9adb4a6 | bellard | break;
|
193 | b9adb4a6 | bellard | } |
194 | b9adb4a6 | bellard | } |
195 | b9adb4a6 | bellard | |
196 | b9adb4a6 | bellard | /* Look up symbol for debugging purpose. Returns "" if unknown. */
|
197 | b9adb4a6 | bellard | const char *lookup_symbol(void *orig_addr) |
198 | b9adb4a6 | bellard | { |
199 | b9adb4a6 | bellard | unsigned int i; |
200 | b9adb4a6 | bellard | /* Hack, because we know this is x86. */
|
201 | b9adb4a6 | bellard | Elf32_Sym *sym = disas_symtab; |
202 | b9adb4a6 | bellard | |
203 | b9adb4a6 | bellard | for (i = 0; i < disas_num_syms; i++) { |
204 | b9adb4a6 | bellard | if (sym[i].st_shndx == SHN_UNDEF
|
205 | b9adb4a6 | bellard | || sym[i].st_shndx >= SHN_LORESERVE) |
206 | b9adb4a6 | bellard | continue;
|
207 | b9adb4a6 | bellard | |
208 | b9adb4a6 | bellard | if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
|
209 | b9adb4a6 | bellard | continue;
|
210 | b9adb4a6 | bellard | |
211 | b9adb4a6 | bellard | if ((long)orig_addr >= sym[i].st_value |
212 | b9adb4a6 | bellard | && (long)orig_addr < sym[i].st_value + sym[i].st_size)
|
213 | b9adb4a6 | bellard | return disas_strtab + sym[i].st_name;
|
214 | b9adb4a6 | bellard | } |
215 | b9adb4a6 | bellard | return ""; |
216 | b9adb4a6 | bellard | } |