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
file annotate diff log raw
nkeynes@362
     1
/**
nkeynes@362
     2
 * $Id: x86dasm.c,v 1.1 2007-08-28 08:46:54 nkeynes Exp $
nkeynes@362
     3
 *
nkeynes@362
     4
 * Wrapper around i386-dis to supply the same behaviour as the other
nkeynes@362
     5
 * disassembly functions.
nkeynes@362
     6
 *
nkeynes@362
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@362
     8
 *
nkeynes@362
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@362
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@362
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@362
    12
 * (at your option) any later version.
nkeynes@362
    13
 *
nkeynes@362
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@362
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@362
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@362
    17
 * GNU General Public License for more details.
nkeynes@362
    18
 */
nkeynes@362
    19
nkeynes@362
    20
#include <stdarg.h>
nkeynes@362
    21
#include "x86dasm.h"
nkeynes@362
    22
#include "bfd.h"
nkeynes@362
    23
#include "dis-asm.h"
nkeynes@362
    24
#include "sh4/sh4core.h"
nkeynes@362
    25
nkeynes@362
    26
extern const struct reg_desc_struct sh4_reg_map[];
nkeynes@362
    27
const struct cpu_desc_struct x86_cpu_desc = 
nkeynes@362
    28
    { "x86", x86_disasm_instruction, NULL, mem_has_page, 
nkeynes@362
    29
      NULL, NULL, NULL, 1, 
nkeynes@362
    30
      (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
nkeynes@362
    31
      &sh4r.pc };
nkeynes@362
    32
nkeynes@362
    33
static int x86_disasm_output( void *data, const char *format, ... );
nkeynes@362
    34
static int x86_read_memory( bfd_vma memaddr, bfd_byte *buffer, unsigned int length,
nkeynes@362
    35
			    struct disassemble_info *info );
nkeynes@362
    36
static int x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
nkeynes@362
    37
nkeynes@362
    38
static struct disassemble_info x86_disasm_info;
nkeynes@362
    39
nkeynes@362
    40
void x86_disasm_init(char *buf, uint32_t vma, int buflen)
nkeynes@362
    41
{
nkeynes@362
    42
    init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
nkeynes@362
    43
    x86_disasm_info.arch = bfd_arch_i386;
nkeynes@362
    44
    x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
nkeynes@362
    45
    x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
nkeynes@362
    46
    x86_disasm_info.buffer = buf;
nkeynes@362
    47
    x86_disasm_info.buffer_vma = vma;
nkeynes@362
    48
    x86_disasm_info.buffer_length = buflen;
nkeynes@362
    49
}
nkeynes@362
    50
nkeynes@362
    51
nkeynes@362
    52
uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
nkeynes@362
    53
{
nkeynes@362
    54
    int count, i;
nkeynes@362
    55
nkeynes@362
    56
    x86_disasm_info.stream = buf;
nkeynes@362
    57
    buf[0] = 0;
nkeynes@362
    58
    count = print_insn_i386_att( pc, &x86_disasm_info );
nkeynes@362
    59
    if( count != 0 ) {
nkeynes@362
    60
	char tmp[count];
nkeynes@362
    61
	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
nkeynes@362
    62
	for( i=0; i<count; i++ ) {
nkeynes@362
    63
	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
nkeynes@362
    64
	    opcode += 3;
nkeynes@362
    65
	}
nkeynes@362
    66
	*(opcode-1) = '\0';
nkeynes@362
    67
    }
nkeynes@362
    68
    return pc + count;
nkeynes@362
    69
}
nkeynes@362
    70
nkeynes@362
    71
int x86_disasm_output( void *data, const char *format, ... )
nkeynes@362
    72
{
nkeynes@362
    73
    char *p = (char *)data;
nkeynes@362
    74
    va_list ap;
nkeynes@362
    75
    int n;
nkeynes@362
    76
    p += strlen(p);
nkeynes@362
    77
    va_start( ap, format );
nkeynes@362
    78
    n = vsprintf( p, format, ap );
nkeynes@362
    79
    va_end( ap );
nkeynes@362
    80
    return n;
nkeynes@362
    81
}
.