4 * Wrapper around i386-dis to supply the same behaviour as the other
5 * disassembly functions.
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 #include "x86dasm/x86dasm.h"
23 #include "x86dasm/bfd.h"
24 #include "x86dasm/dis-asm.h"
26 #include "sh4/sh4trans.h"
28 extern const struct reg_desc_struct sh4_reg_map[];
29 const struct cpu_desc_struct x86_cpu_desc =
30 { "x86", (disasm_func_t)x86_disasm_instruction, NULL, mem_has_page,
32 (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
35 static int x86_disasm_output( void *data, const char *format, ... );
36 static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
38 static struct disassemble_info x86_disasm_info;
40 static x86_symbol *x86_symtab;
41 static int x86_num_symbols = 0;
44 void xlat_disasm_block( FILE *out, void *block )
46 uint32_t buflen = xlat_get_code_size(block);
47 x86_set_symtab( NULL, 0 );
48 x86_disasm_block( out, block, buflen );
51 void x86_disasm_block(FILE *out, void *start, uint32_t len)
53 uintptr_t start_addr = (uintptr_t)start;
55 x86_disasm_init( start, start_addr, len );
56 for( pc = start_addr; pc < start_addr + len; ) {
59 uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
60 fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
65 void x86_disasm_init(unsigned char *buf, uintptr_t vma, int buflen)
67 init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
68 x86_disasm_info.arch = bfd_arch_i386;
69 #if SH4_TRANSLATOR == TARGET_X86_64
70 x86_disasm_info.mach = bfd_mach_x86_64_intel_syntax;
72 x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
74 x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
75 x86_disasm_info.buffer = buf;
76 x86_disasm_info.buffer_vma = vma;
77 x86_disasm_info.buffer_length = buflen;
78 x86_disasm_info.print_address_func = x86_print_address;
81 void x86_set_symtab( x86_symbol *symtab, int num_symbols )
84 x86_num_symbols = num_symbols;
87 static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
90 for( i=0; i<x86_num_symbols; i++ ) {
91 if( x86_symtab[i].ptr == (void *)(uintptr_t)memaddr ) {
92 return x86_symtab[i].name;
98 static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
100 const char *sym = x86_find_symbol(memaddr, info);
101 info->fprintf_func( info->stream, "%08X", memaddr );
103 info->fprintf_func( info->stream, " <%s>", sym );
107 void x86_print_symbolic_operand( char *buf, int hex, unsigned int disp )
109 const char *sym = x86_find_symbol(disp, NULL);
111 snprintf( buf, 50, "<%s>", sym );
113 sprintf( buf, "0x%x", disp );
115 sprintf( buf, "%d", (int)disp );
119 uint32_t x86_disasm_instruction( uintptr_t pc, char *buf, int len, char *opcode )
123 x86_disasm_info.stream = buf;
125 count = print_insn_i386_att( pc, &x86_disasm_info );
127 unsigned char tmp[count];
128 x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
129 for( i=0; i<count; i++ ) {
130 sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
138 int x86_disasm_output( void *data, const char *format, ... )
140 char *p = (char *)data;
144 va_start( ap, format );
145 n = vsprintf( p, format, ap );
.