Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 362:dc40e2064dc4
next365:94cab5ad0ed8
author nkeynes
date Tue Aug 28 08:46:54 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add the i386 disassembler from binutils (why write your own if you don't
have to) to use for translator validation
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/x86dasm/x86dasm.c Tue Aug 28 08:46:54 2007 +0000
1.3 @@ -0,0 +1,81 @@
1.4 +/**
1.5 + * $Id: x86dasm.c,v 1.1 2007-08-28 08:46:54 nkeynes Exp $
1.6 + *
1.7 + * Wrapper around i386-dis to supply the same behaviour as the other
1.8 + * disassembly functions.
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +
1.23 +#include <stdarg.h>
1.24 +#include "x86dasm.h"
1.25 +#include "bfd.h"
1.26 +#include "dis-asm.h"
1.27 +#include "sh4/sh4core.h"
1.28 +
1.29 +extern const struct reg_desc_struct sh4_reg_map[];
1.30 +const struct cpu_desc_struct x86_cpu_desc =
1.31 + { "x86", x86_disasm_instruction, NULL, mem_has_page,
1.32 + NULL, NULL, NULL, 1,
1.33 + (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
1.34 + &sh4r.pc };
1.35 +
1.36 +static int x86_disasm_output( void *data, const char *format, ... );
1.37 +static int x86_read_memory( bfd_vma memaddr, bfd_byte *buffer, unsigned int length,
1.38 + struct disassemble_info *info );
1.39 +static int x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
1.40 +
1.41 +static struct disassemble_info x86_disasm_info;
1.42 +
1.43 +void x86_disasm_init(char *buf, uint32_t vma, int buflen)
1.44 +{
1.45 + init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
1.46 + x86_disasm_info.arch = bfd_arch_i386;
1.47 + x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
1.48 + x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
1.49 + x86_disasm_info.buffer = buf;
1.50 + x86_disasm_info.buffer_vma = vma;
1.51 + x86_disasm_info.buffer_length = buflen;
1.52 +}
1.53 +
1.54 +
1.55 +uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
1.56 +{
1.57 + int count, i;
1.58 +
1.59 + x86_disasm_info.stream = buf;
1.60 + buf[0] = 0;
1.61 + count = print_insn_i386_att( pc, &x86_disasm_info );
1.62 + if( count != 0 ) {
1.63 + char tmp[count];
1.64 + x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
1.65 + for( i=0; i<count; i++ ) {
1.66 + sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
1.67 + opcode += 3;
1.68 + }
1.69 + *(opcode-1) = '\0';
1.70 + }
1.71 + return pc + count;
1.72 +}
1.73 +
1.74 +int x86_disasm_output( void *data, const char *format, ... )
1.75 +{
1.76 + char *p = (char *)data;
1.77 + va_list ap;
1.78 + int n;
1.79 + p += strlen(p);
1.80 + va_start( ap, format );
1.81 + n = vsprintf( p, format, ap );
1.82 + va_end( ap );
1.83 + return n;
1.84 +}
.