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