Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 515:5e5bb1dd369e
prev480:d28c2992f5ee
next527:14c9489f647e
author nkeynes
date Thu Nov 15 08:17:17 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix compilation of the unit tests
file annotate diff log raw
nkeynes@362
     1
/**
nkeynes@480
     2
 * $Id: x86dasm.c,v 1.5 2007-10-31 11:53:35 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@480
    21
#include <string.h>
nkeynes@362
    22
#include "x86dasm.h"
nkeynes@362
    23
#include "bfd.h"
nkeynes@362
    24
#include "dis-asm.h"
nkeynes@362
    25
#include "sh4/sh4core.h"
nkeynes@362
    26
nkeynes@362
    27
extern const struct reg_desc_struct sh4_reg_map[];
nkeynes@362
    28
const struct cpu_desc_struct x86_cpu_desc = 
nkeynes@362
    29
    { "x86", x86_disasm_instruction, NULL, mem_has_page, 
nkeynes@362
    30
      NULL, NULL, NULL, 1, 
nkeynes@362
    31
      (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
nkeynes@362
    32
      &sh4r.pc };
nkeynes@362
    33
nkeynes@362
    34
static int x86_disasm_output( void *data, const char *format, ... );
nkeynes@429
    35
static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
nkeynes@362
    36
nkeynes@362
    37
static struct disassemble_info x86_disasm_info;
nkeynes@362
    38
nkeynes@365
    39
static x86_symbol *x86_symtab;
nkeynes@365
    40
static int x86_num_symbols = 0;   
nkeynes@365
    41
nkeynes@515
    42
nkeynes@515
    43
void xlat_disasm_block( FILE *out, void *block )
nkeynes@515
    44
{
nkeynes@515
    45
    uint32_t buflen = xlat_get_block_size(block);
nkeynes@515
    46
    x86_set_symtab( NULL, 0 );
nkeynes@515
    47
    x86_disasm_block( out, block, buflen );
nkeynes@515
    48
}
nkeynes@515
    49
nkeynes@376
    50
void x86_disasm_block(FILE *out, void *start, uint32_t len)
nkeynes@376
    51
{
nkeynes@376
    52
    uint32_t start_addr = (uint32_t)start;
nkeynes@376
    53
    uint32_t pc;
nkeynes@376
    54
    x86_disasm_init( start, start_addr, len );
nkeynes@376
    55
    for( pc = start_addr; pc < start_addr + len;  ) {
nkeynes@376
    56
	char buf[256];
nkeynes@376
    57
	char op[256];
nkeynes@376
    58
	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
nkeynes@376
    59
	fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
nkeynes@376
    60
	pc = pc2;
nkeynes@376
    61
    }
nkeynes@376
    62
}
nkeynes@376
    63
nkeynes@429
    64
void x86_disasm_init(unsigned char *buf, uint32_t vma, int buflen)
nkeynes@362
    65
{
nkeynes@362
    66
    init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
nkeynes@362
    67
    x86_disasm_info.arch = bfd_arch_i386;
nkeynes@362
    68
    x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
nkeynes@362
    69
    x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
nkeynes@362
    70
    x86_disasm_info.buffer = buf;
nkeynes@362
    71
    x86_disasm_info.buffer_vma = vma;
nkeynes@362
    72
    x86_disasm_info.buffer_length = buflen;
nkeynes@365
    73
    x86_disasm_info.print_address_func = x86_print_address;
nkeynes@362
    74
}
nkeynes@362
    75
nkeynes@365
    76
void x86_set_symtab( x86_symbol *symtab, int num_symbols )
nkeynes@365
    77
{
nkeynes@365
    78
    x86_symtab = symtab;
nkeynes@365
    79
    x86_num_symbols = num_symbols;
nkeynes@365
    80
}
nkeynes@365
    81
nkeynes@365
    82
static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    83
{
nkeynes@365
    84
    int i;
nkeynes@365
    85
    for( i=0; i<x86_num_symbols; i++ ) {
nkeynes@429
    86
	if( x86_symtab[i].ptr == (void *)(uint32_t)memaddr ) {
nkeynes@365
    87
	    return x86_symtab[i].name;
nkeynes@365
    88
	}
nkeynes@365
    89
    }
nkeynes@365
    90
    return NULL;
nkeynes@365
    91
}
nkeynes@365
    92
nkeynes@429
    93
static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    94
{
nkeynes@365
    95
    const char *sym = x86_find_symbol(memaddr, info);
nkeynes@365
    96
    info->fprintf_func( info->stream, "%08X", memaddr );
nkeynes@365
    97
    if( sym != NULL ) {
nkeynes@365
    98
	info->fprintf_func( info->stream, " <%s>", sym );
nkeynes@365
    99
    }
nkeynes@365
   100
}
nkeynes@362
   101
nkeynes@362
   102
uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
nkeynes@362
   103
{
nkeynes@362
   104
    int count, i;
nkeynes@362
   105
nkeynes@362
   106
    x86_disasm_info.stream = buf;
nkeynes@362
   107
    buf[0] = 0;
nkeynes@362
   108
    count = print_insn_i386_att( pc, &x86_disasm_info );
nkeynes@362
   109
    if( count != 0 ) {
nkeynes@429
   110
	unsigned char tmp[count];
nkeynes@362
   111
	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
nkeynes@362
   112
	for( i=0; i<count; i++ ) {
nkeynes@362
   113
	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
nkeynes@362
   114
	    opcode += 3;
nkeynes@362
   115
	}
nkeynes@362
   116
	*(opcode-1) = '\0';
nkeynes@362
   117
    }
nkeynes@362
   118
    return pc + count;
nkeynes@362
   119
}
nkeynes@362
   120
nkeynes@362
   121
int x86_disasm_output( void *data, const char *format, ... )
nkeynes@362
   122
{
nkeynes@362
   123
    char *p = (char *)data;
nkeynes@362
   124
    va_list ap;
nkeynes@362
   125
    int n;
nkeynes@362
   126
    p += strlen(p);
nkeynes@362
   127
    va_start( ap, format );
nkeynes@362
   128
    n = vsprintf( p, format, ap );
nkeynes@362
   129
    va_end( ap );
nkeynes@362
   130
    return n;
nkeynes@362
   131
}
.