]>
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 FB |
7 | |
8 | /* Filled in by elfload.c. Simplistic, but will do for now. */ | |
9 | unsigned int disas_num_syms; | |
10 | void *disas_symtab; | |
11 | const char *disas_strtab; | |
12 | ||
aa0aa4fa FB |
13 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
14 | Transfer them to myaddr. */ | |
15 | int | |
16 | buffer_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. */ | |
32 | void | |
33 | perror_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 | ||
55 | void | |
56 | generic_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 | ||
65 | int | |
66 | generic_symbol_at_address (addr, info) | |
67 | bfd_vma addr; | |
68 | struct disassemble_info * info; | |
69 | { | |
70 | return 1; | |
71 | } | |
72 | ||
73 | bfd_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 | ||
84 | bfd_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 | ||
b9adb4a6 FB |
95 | /* Disassemble this for me please... (debugging). */ |
96 | void disas(FILE *out, void *code, unsigned long size, enum disas_type type) | |
97 | { | |
98 | uint8_t *pc; | |
99 | int count; | |
100 | struct disassemble_info disasm_info; | |
101 | int (*print_insn)(bfd_vma pc, disassemble_info *info); | |
102 | ||
103 | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); | |
104 | ||
105 | disasm_info.buffer = code; | |
106 | disasm_info.buffer_vma = (unsigned long)code; | |
107 | disasm_info.buffer_length = size; | |
108 | ||
109 | if (type == DISAS_TARGET) { | |
110 | #ifdef WORDS_BIGENDIAN | |
111 | disasm_info.endian = BFD_ENDIAN_BIG; | |
112 | #else | |
113 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
114 | #endif | |
115 | #ifdef __i386__ | |
116 | disasm_info.mach = bfd_mach_i386_i386; | |
117 | print_insn = print_insn_i386; | |
118 | #elif defined(__powerpc__) | |
119 | print_insn = print_insn_ppc; | |
a993ba85 FB |
120 | #elif defined(__alpha__) |
121 | print_insn = print_insn_alpha; | |
aa0aa4fa FB |
122 | #elif defined(__sparc__) |
123 | print_insn = print_insn_sparc; | |
124 | #elif defined(__arm__) | |
125 | print_insn = print_insn_arm; | |
b9adb4a6 FB |
126 | #else |
127 | fprintf(out, "Asm output not supported on this arch\n"); | |
128 | return; | |
129 | #endif | |
130 | } else { | |
131 | /* Currently only source supported in x86. */ | |
132 | disasm_info.endian = BFD_ENDIAN_LITTLE; | |
133 | if (type == DISAS_I386_I386) | |
134 | disasm_info.mach = bfd_mach_i386_i386; | |
135 | else | |
136 | disasm_info.mach = bfd_mach_i386_i8086; | |
137 | print_insn = print_insn_i386; | |
138 | } | |
139 | ||
140 | for (pc = code; pc < (uint8_t *)code + size; pc += count) { | |
141 | fprintf(out, "0x%08lx: ", (long)pc); | |
aa0aa4fa FB |
142 | #ifdef __arm__ |
143 | /* since data are included in the code, it is better to | |
144 | display code data too */ | |
145 | if (type == DISAS_TARGET) { | |
146 | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); | |
147 | } | |
148 | #endif | |
08351fb3 | 149 | count = print_insn((unsigned long)pc, &disasm_info); |
b9adb4a6 FB |
150 | fprintf(out, "\n"); |
151 | if (count < 0) | |
152 | break; | |
153 | } | |
154 | } | |
155 | ||
156 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ | |
157 | const char *lookup_symbol(void *orig_addr) | |
158 | { | |
159 | unsigned int i; | |
160 | /* Hack, because we know this is x86. */ | |
161 | Elf32_Sym *sym = disas_symtab; | |
162 | ||
163 | for (i = 0; i < disas_num_syms; i++) { | |
164 | if (sym[i].st_shndx == SHN_UNDEF | |
165 | || sym[i].st_shndx >= SHN_LORESERVE) | |
166 | continue; | |
167 | ||
168 | if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC) | |
169 | continue; | |
170 | ||
171 | if ((long)orig_addr >= sym[i].st_value | |
172 | && (long)orig_addr < sym[i].st_value + sym[i].st_size) | |
173 | return disas_strtab + sym[i].st_name; | |
174 | } | |
175 | return ""; | |
176 | } |