Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 429:e581b90c3fb3
prev376:8c7587af5a5d
next480:d28c2992f5ee
author nkeynes
date Mon Oct 08 11:52:13 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
file annotate diff log raw
nkeynes@362
     1
/**
nkeynes@429
     2
 * $Id: x86dasm.c,v 1.4 2007-10-08 11:48:10 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@429
    34
static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
nkeynes@362
    35
nkeynes@362
    36
static struct disassemble_info x86_disasm_info;
nkeynes@362
    37
nkeynes@365
    38
static x86_symbol *x86_symtab;
nkeynes@365
    39
static int x86_num_symbols = 0;   
nkeynes@365
    40
nkeynes@376
    41
void x86_disasm_block(FILE *out, void *start, uint32_t len)
nkeynes@376
    42
{
nkeynes@376
    43
    uint32_t start_addr = (uint32_t)start;
nkeynes@376
    44
    uint32_t pc;
nkeynes@376
    45
    x86_disasm_init( start, start_addr, len );
nkeynes@376
    46
    for( pc = start_addr; pc < start_addr + len;  ) {
nkeynes@376
    47
	char buf[256];
nkeynes@376
    48
	char op[256];
nkeynes@376
    49
	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
nkeynes@376
    50
	fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
nkeynes@376
    51
	pc = pc2;
nkeynes@376
    52
    }
nkeynes@376
    53
}
nkeynes@376
    54
nkeynes@429
    55
void x86_disasm_init(unsigned char *buf, uint32_t vma, int buflen)
nkeynes@362
    56
{
nkeynes@362
    57
    init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
nkeynes@362
    58
    x86_disasm_info.arch = bfd_arch_i386;
nkeynes@362
    59
    x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
nkeynes@362
    60
    x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
nkeynes@362
    61
    x86_disasm_info.buffer = buf;
nkeynes@362
    62
    x86_disasm_info.buffer_vma = vma;
nkeynes@362
    63
    x86_disasm_info.buffer_length = buflen;
nkeynes@365
    64
    x86_disasm_info.print_address_func = x86_print_address;
nkeynes@362
    65
}
nkeynes@362
    66
nkeynes@365
    67
void x86_set_symtab( x86_symbol *symtab, int num_symbols )
nkeynes@365
    68
{
nkeynes@365
    69
    x86_symtab = symtab;
nkeynes@365
    70
    x86_num_symbols = num_symbols;
nkeynes@365
    71
}
nkeynes@365
    72
nkeynes@365
    73
static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    74
{
nkeynes@365
    75
    int i;
nkeynes@365
    76
    for( i=0; i<x86_num_symbols; i++ ) {
nkeynes@429
    77
	if( x86_symtab[i].ptr == (void *)(uint32_t)memaddr ) {
nkeynes@365
    78
	    return x86_symtab[i].name;
nkeynes@365
    79
	}
nkeynes@365
    80
    }
nkeynes@365
    81
    return NULL;
nkeynes@365
    82
}
nkeynes@365
    83
nkeynes@429
    84
static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    85
{
nkeynes@365
    86
    const char *sym = x86_find_symbol(memaddr, info);
nkeynes@365
    87
    info->fprintf_func( info->stream, "%08X", memaddr );
nkeynes@365
    88
    if( sym != NULL ) {
nkeynes@365
    89
	info->fprintf_func( info->stream, " <%s>", sym );
nkeynes@365
    90
    }
nkeynes@365
    91
}
nkeynes@362
    92
nkeynes@362
    93
uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
nkeynes@362
    94
{
nkeynes@362
    95
    int count, i;
nkeynes@362
    96
nkeynes@362
    97
    x86_disasm_info.stream = buf;
nkeynes@362
    98
    buf[0] = 0;
nkeynes@362
    99
    count = print_insn_i386_att( pc, &x86_disasm_info );
nkeynes@362
   100
    if( count != 0 ) {
nkeynes@429
   101
	unsigned char tmp[count];
nkeynes@362
   102
	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
nkeynes@362
   103
	for( i=0; i<count; i++ ) {
nkeynes@362
   104
	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
nkeynes@362
   105
	    opcode += 3;
nkeynes@362
   106
	}
nkeynes@362
   107
	*(opcode-1) = '\0';
nkeynes@362
   108
    }
nkeynes@362
   109
    return pc + count;
nkeynes@362
   110
}
nkeynes@362
   111
nkeynes@362
   112
int x86_disasm_output( void *data, const char *format, ... )
nkeynes@362
   113
{
nkeynes@362
   114
    char *p = (char *)data;
nkeynes@362
   115
    va_list ap;
nkeynes@362
   116
    int n;
nkeynes@362
   117
    p += strlen(p);
nkeynes@362
   118
    va_start( ap, format );
nkeynes@362
   119
    n = vsprintf( p, format, ap );
nkeynes@362
   120
    va_end( ap );
nkeynes@362
   121
    return n;
nkeynes@362
   122
}
.