Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
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
view annotate diff log raw
     1 /**
     2  * $Id: x86dasm.c,v 1.1 2007-08-28 08:46:54 nkeynes Exp $
     3  *
     4  * Wrapper around i386-dis to supply the same behaviour as the other
     5  * disassembly functions.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #include <stdarg.h>
    21 #include "x86dasm.h"
    22 #include "bfd.h"
    23 #include "dis-asm.h"
    24 #include "sh4/sh4core.h"
    26 extern const struct reg_desc_struct sh4_reg_map[];
    27 const struct cpu_desc_struct x86_cpu_desc = 
    28     { "x86", x86_disasm_instruction, NULL, mem_has_page, 
    29       NULL, NULL, NULL, 1, 
    30       (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
    31       &sh4r.pc };
    33 static int x86_disasm_output( void *data, const char *format, ... );
    34 static int x86_read_memory( bfd_vma memaddr, bfd_byte *buffer, unsigned int length,
    35 			    struct disassemble_info *info );
    36 static int x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
    38 static struct disassemble_info x86_disasm_info;
    40 void x86_disasm_init(char *buf, uint32_t vma, int buflen)
    41 {
    42     init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
    43     x86_disasm_info.arch = bfd_arch_i386;
    44     x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
    45     x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
    46     x86_disasm_info.buffer = buf;
    47     x86_disasm_info.buffer_vma = vma;
    48     x86_disasm_info.buffer_length = buflen;
    49 }
    52 uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
    53 {
    54     int count, i;
    56     x86_disasm_info.stream = buf;
    57     buf[0] = 0;
    58     count = print_insn_i386_att( pc, &x86_disasm_info );
    59     if( count != 0 ) {
    60 	char tmp[count];
    61 	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
    62 	for( i=0; i<count; i++ ) {
    63 	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
    64 	    opcode += 3;
    65 	}
    66 	*(opcode-1) = '\0';
    67     }
    68     return pc + count;
    69 }
    71 int x86_disasm_output( void *data, const char *format, ... )
    72 {
    73     char *p = (char *)data;
    74     va_list ap;
    75     int n;
    76     p += strlen(p);
    77     va_start( ap, format );
    78     n = vsprintf( p, format, ap );
    79     va_end( ap );
    80     return n;
    81 }
.