blob: dc1957b9621d47b1742d6e544a96276ae110a798 [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"
4#include "disas.h"
5#include "elf.h"
bellardaa0aa4f2003-06-09 15:23:31 +00006#include <errno.h>
bellardb9adb4a2003-04-29 20:41:16 +00007
8/* Filled in by elfload.c. Simplistic, but will do for now. */
9unsigned int disas_num_syms;
10void *disas_symtab;
11const char *disas_strtab;
12
bellardaa0aa4f2003-06-09 15:23:31 +000013/* Get LENGTH bytes from info's buffer, at target address memaddr.
14 Transfer them to myaddr. */
15int
16buffer_read_memory (memaddr, myaddr, length, info)
17 bfd_vma memaddr;
18 bfd_byte *myaddr;
19 int length;
20 struct disassemble_info *info;
21{
22 if (memaddr < info->buffer_vma
23 || memaddr + length > info->buffer_vma + info->buffer_length)
24 /* Out of bounds. Use EIO because GDB uses it. */
25 return EIO;
26 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
27 return 0;
28}
29
30/* Print an error message. We can assume that this is in response to
31 an error return from buffer_read_memory. */
32void
33perror_memory (status, memaddr, info)
34 int status;
35 bfd_vma memaddr;
36 struct disassemble_info *info;
37{
38 if (status != EIO)
39 /* Can't happen. */
40 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
41 else
42 /* Actually, address between memaddr and memaddr + len was
43 out of bounds. */
44 (*info->fprintf_func) (info->stream,
45 "Address 0x%x is out of bounds.\n", memaddr);
46}
47
48/* This could be in a separate file, to save miniscule amounts of space
49 in statically linked executables. */
50
51/* Just print the address is hex. This is included for completeness even
52 though both GDB and objdump provide their own (to print symbolic
53 addresses). */
54
55void
56generic_print_address (addr, info)
57 bfd_vma addr;
58 struct disassemble_info *info;
59{
60 (*info->fprintf_func) (info->stream, "0x%x", addr);
61}
62
63/* Just return the given address. */
64
65int
66generic_symbol_at_address (addr, info)
67 bfd_vma addr;
68 struct disassemble_info * info;
69{
70 return 1;
71}
72
73bfd_vma bfd_getl32 (const bfd_byte *addr)
74{
75 unsigned long v;
76
77 v = (unsigned long) addr[0];
78 v |= (unsigned long) addr[1] << 8;
79 v |= (unsigned long) addr[2] << 16;
80 v |= (unsigned long) addr[3] << 24;
81 return (bfd_vma) v;
82}
83
84bfd_vma bfd_getb32 (const bfd_byte *addr)
85{
86 unsigned long v;
87
88 v = (unsigned long) addr[0] << 24;
89 v |= (unsigned long) addr[1] << 16;
90 v |= (unsigned long) addr[2] << 8;
91 v |= (unsigned long) addr[3];
92 return (bfd_vma) v;
93}
94
bellard95cbfc62003-06-15 19:44:10 +000095/* Disassemble this for me please... (debugging). 'flags' is only used
96 for i386: non zero means 16 bit code */
97void disas(FILE *out, void *code, unsigned long size, int is_host, int flags)
bellardb9adb4a2003-04-29 20:41:16 +000098{
99 uint8_t *pc;
100 int count;
101 struct disassemble_info disasm_info;
102 int (*print_insn)(bfd_vma pc, disassemble_info *info);
103
104 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
105
106 disasm_info.buffer = code;
107 disasm_info.buffer_vma = (unsigned long)code;
108 disasm_info.buffer_length = size;
109
bellard95cbfc62003-06-15 19:44:10 +0000110 if (is_host) {
bellardb9adb4a2003-04-29 20:41:16 +0000111#ifdef WORDS_BIGENDIAN
112 disasm_info.endian = BFD_ENDIAN_BIG;
113#else
114 disasm_info.endian = BFD_ENDIAN_LITTLE;
115#endif
116#ifdef __i386__
117 disasm_info.mach = bfd_mach_i386_i386;
118 print_insn = print_insn_i386;
119#elif defined(__powerpc__)
120 print_insn = print_insn_ppc;
bellarda993ba82003-05-11 12:25:45 +0000121#elif defined(__alpha__)
122 print_insn = print_insn_alpha;
bellardaa0aa4f2003-06-09 15:23:31 +0000123#elif defined(__sparc__)
124 print_insn = print_insn_sparc;
125#elif defined(__arm__)
126 print_insn = print_insn_arm;
bellardb9adb4a2003-04-29 20:41:16 +0000127#else
128 fprintf(out, "Asm output not supported on this arch\n");
129 return;
130#endif
131 } else {
bellard95cbfc62003-06-15 19:44:10 +0000132#ifdef TARGET_WORDS_BIGENDIAN
133 disasm_info.endian = BFD_ENDIAN_BIG;
134#else
bellardb9adb4a2003-04-29 20:41:16 +0000135 disasm_info.endian = BFD_ENDIAN_LITTLE;
bellard95cbfc62003-06-15 19:44:10 +0000136#endif
137#if defined(TARGET_I386)
138 if (!flags)
bellardb9adb4a2003-04-29 20:41:16 +0000139 disasm_info.mach = bfd_mach_i386_i386;
140 else
141 disasm_info.mach = bfd_mach_i386_i8086;
142 print_insn = print_insn_i386;
bellard95cbfc62003-06-15 19:44:10 +0000143#elif defined(TARGET_ARM)
144 print_insn = print_insn_arm;
145#else
146 fprintf(out, "Asm output not supported on this arch\n");
147 return;
148#endif
bellardb9adb4a2003-04-29 20:41:16 +0000149 }
150
151 for (pc = code; pc < (uint8_t *)code + size; pc += count) {
152 fprintf(out, "0x%08lx: ", (long)pc);
bellardaa0aa4f2003-06-09 15:23:31 +0000153#ifdef __arm__
154 /* since data are included in the code, it is better to
155 display code data too */
bellard95cbfc62003-06-15 19:44:10 +0000156 if (is_host) {
bellardaa0aa4f2003-06-09 15:23:31 +0000157 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
158 }
159#endif
bellard08351fb2003-05-25 16:42:20 +0000160 count = print_insn((unsigned long)pc, &disasm_info);
bellardb9adb4a2003-04-29 20:41:16 +0000161 fprintf(out, "\n");
162 if (count < 0)
163 break;
164 }
165}
166
167/* Look up symbol for debugging purpose. Returns "" if unknown. */
168const char *lookup_symbol(void *orig_addr)
169{
170 unsigned int i;
171 /* Hack, because we know this is x86. */
172 Elf32_Sym *sym = disas_symtab;
173
174 for (i = 0; i < disas_num_syms; i++) {
175 if (sym[i].st_shndx == SHN_UNDEF
176 || sym[i].st_shndx >= SHN_LORESERVE)
177 continue;
178
179 if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
180 continue;
181
182 if ((long)orig_addr >= sym[i].st_value
183 && (long)orig_addr < sym[i].st_value + sym[i].st_size)
184 return disas_strtab + sym[i].st_name;
185 }
186 return "";
187}