1 /* General "disassemble this chunk" code. Used for debugging. */
7 /* Filled in by elfload.c. Simplistic, but will do for now. */
8 unsigned int disas_num_syms;
10 const char *disas_strtab;
12 /* Get LENGTH bytes from info's buffer, at target address memaddr.
13 Transfer them to myaddr. */
15 buffer_read_memory (memaddr, myaddr, length, info)
19 struct disassemble_info *info;
21 if (memaddr < info->buffer_vma
22 || memaddr + length > info->buffer_vma + info->buffer_length)
23 /* Out of bounds. Use EIO because GDB uses it. */
25 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
29 /* Print an error message. We can assume that this is in response to
30 an error return from buffer_read_memory. */
32 perror_memory (status, memaddr, info)
35 struct disassemble_info *info;
39 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
41 /* Actually, address between memaddr and memaddr + len was
43 (*info->fprintf_func) (info->stream,
44 "Address 0x%x is out of bounds.\n", memaddr);
47 /* This could be in a separate file, to save miniscule amounts of space
48 in statically linked executables. */
50 /* Just print the address is hex. This is included for completeness even
51 though both GDB and objdump provide their own (to print symbolic
55 generic_print_address (addr, info)
57 struct disassemble_info *info;
59 (*info->fprintf_func) (info->stream, "0x%x", addr);
62 /* Just return the given address. */
65 generic_symbol_at_address (addr, info)
67 struct disassemble_info * info;
72 bfd_vma bfd_getl32 (const bfd_byte *addr)
76 v = (unsigned long) addr[0];
77 v |= (unsigned long) addr[1] << 8;
78 v |= (unsigned long) addr[2] << 16;
79 v |= (unsigned long) addr[3] << 24;
83 bfd_vma bfd_getb32 (const bfd_byte *addr)
87 v = (unsigned long) addr[0] << 24;
88 v |= (unsigned long) addr[1] << 16;
89 v |= (unsigned long) addr[2] << 8;
90 v |= (unsigned long) addr[3];
94 /* Disassemble this for me please... (debugging). */
95 void disas(FILE *out, void *code, unsigned long size, enum disas_type type)
99 struct disassemble_info disasm_info;
100 int (*print_insn)(bfd_vma pc, disassemble_info *info);
102 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
104 disasm_info.buffer = code;
105 disasm_info.buffer_vma = (unsigned long)code;
106 disasm_info.buffer_length = size;
108 if (type == DISAS_TARGET) {
109 #ifdef WORDS_BIGENDIAN
110 disasm_info.endian = BFD_ENDIAN_BIG;
112 disasm_info.endian = BFD_ENDIAN_LITTLE;
115 disasm_info.mach = bfd_mach_i386_i386;
116 print_insn = print_insn_i386;
117 #elif defined(__powerpc__)
118 print_insn = print_insn_ppc;
119 #elif defined(__alpha__)
120 print_insn = print_insn_alpha;
121 #elif defined(__sparc__)
122 print_insn = print_insn_sparc;
123 #elif defined(__arm__)
124 print_insn = print_insn_arm;
126 fprintf(out, "Asm output not supported on this arch\n");
130 /* Currently only source supported in x86. */
131 disasm_info.endian = BFD_ENDIAN_LITTLE;
132 if (type == DISAS_I386_I386)
133 disasm_info.mach = bfd_mach_i386_i386;
135 disasm_info.mach = bfd_mach_i386_i8086;
136 print_insn = print_insn_i386;
139 for (pc = code; pc < (uint8_t *)code + size; pc += count) {
140 fprintf(out, "0x%08lx: ", (long)pc);
142 /* since data are included in the code, it is better to
143 display code data too */
144 if (type == DISAS_TARGET) {
145 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
148 count = print_insn((unsigned long)pc, &disasm_info);
155 /* Look up symbol for debugging purpose. Returns "" if unknown. */
156 const char *lookup_symbol(void *orig_addr)
159 /* Hack, because we know this is x86. */
160 Elf32_Sym *sym = disas_symtab;
162 for (i = 0; i < disas_num_syms; i++) {
163 if (sym[i].st_shndx == SHN_UNDEF
164 || sym[i].st_shndx >= SHN_LORESERVE)
167 if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC)
170 if ((long)orig_addr >= sym[i].st_value
171 && (long)orig_addr < sym[i].st_value + sym[i].st_size)
172 return disas_strtab + sym[i].st_name;