Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 920:1e6a29649b56
prev755:ab873907b00e
next925:7cbcc51db63d
author nkeynes
date Thu Dec 04 09:06:35 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Update to print symbols for matching operands
file annotate diff log raw
nkeynes@362
     1
/**
nkeynes@586
     2
 * $Id$
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@755
    22
#include "x86dasm/x86dasm.h"
nkeynes@755
    23
#include "x86dasm/bfd.h"
nkeynes@755
    24
#include "x86dasm/dis-asm.h"
nkeynes@586
    25
#include "sh4/sh4.h"
nkeynes@527
    26
#include "sh4/sh4trans.h"
nkeynes@362
    27
nkeynes@362
    28
extern const struct reg_desc_struct sh4_reg_map[];
nkeynes@362
    29
const struct cpu_desc_struct x86_cpu_desc = 
nkeynes@755
    30
    { "x86", (disasm_func_t)x86_disasm_instruction, NULL, mem_has_page, 
nkeynes@362
    31
      NULL, NULL, NULL, 1, 
nkeynes@362
    32
      (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
nkeynes@362
    33
      &sh4r.pc };
nkeynes@362
    34
nkeynes@362
    35
static int x86_disasm_output( void *data, const char *format, ... );
nkeynes@429
    36
static void 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@365
    40
static x86_symbol *x86_symtab;
nkeynes@365
    41
static int x86_num_symbols = 0;   
nkeynes@365
    42
nkeynes@515
    43
nkeynes@515
    44
void xlat_disasm_block( FILE *out, void *block )
nkeynes@515
    45
{
nkeynes@586
    46
    uint32_t buflen = xlat_get_code_size(block);
nkeynes@515
    47
    x86_set_symtab( NULL, 0 );
nkeynes@515
    48
    x86_disasm_block( out, block, buflen );
nkeynes@515
    49
}
nkeynes@515
    50
nkeynes@376
    51
void x86_disasm_block(FILE *out, void *start, uint32_t len)
nkeynes@376
    52
{
nkeynes@527
    53
    uintptr_t start_addr = (uintptr_t)start;
nkeynes@376
    54
    uint32_t pc;
nkeynes@376
    55
    x86_disasm_init( start, start_addr, len );
nkeynes@376
    56
    for( pc = start_addr; pc < start_addr + len;  ) {
nkeynes@376
    57
	char buf[256];
nkeynes@376
    58
	char op[256];
nkeynes@376
    59
	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
nkeynes@376
    60
	fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
nkeynes@376
    61
	pc = pc2;
nkeynes@376
    62
    }
nkeynes@376
    63
}
nkeynes@376
    64
nkeynes@527
    65
void x86_disasm_init(unsigned char *buf, uintptr_t vma, int buflen)
nkeynes@362
    66
{
nkeynes@362
    67
    init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
nkeynes@362
    68
    x86_disasm_info.arch = bfd_arch_i386;
nkeynes@527
    69
#if SH4_TRANSLATOR == TARGET_X86_64
nkeynes@527
    70
    x86_disasm_info.mach =  bfd_mach_x86_64_intel_syntax;
nkeynes@527
    71
#else
nkeynes@362
    72
    x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
nkeynes@527
    73
#endif
nkeynes@362
    74
    x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
nkeynes@362
    75
    x86_disasm_info.buffer = buf;
nkeynes@362
    76
    x86_disasm_info.buffer_vma = vma;
nkeynes@362
    77
    x86_disasm_info.buffer_length = buflen;
nkeynes@365
    78
    x86_disasm_info.print_address_func = x86_print_address;
nkeynes@362
    79
}
nkeynes@362
    80
nkeynes@365
    81
void x86_set_symtab( x86_symbol *symtab, int num_symbols )
nkeynes@365
    82
{
nkeynes@365
    83
    x86_symtab = symtab;
nkeynes@365
    84
    x86_num_symbols = num_symbols;
nkeynes@365
    85
}
nkeynes@365
    86
nkeynes@365
    87
static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    88
{
nkeynes@365
    89
    int i;
nkeynes@365
    90
    for( i=0; i<x86_num_symbols; i++ ) {
nkeynes@527
    91
	if( x86_symtab[i].ptr == (void *)(uintptr_t)memaddr ) {
nkeynes@365
    92
	    return x86_symtab[i].name;
nkeynes@365
    93
	}
nkeynes@365
    94
    }
nkeynes@365
    95
    return NULL;
nkeynes@365
    96
}
nkeynes@365
    97
nkeynes@429
    98
static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
nkeynes@365
    99
{
nkeynes@365
   100
    const char *sym = x86_find_symbol(memaddr, info);
nkeynes@365
   101
    info->fprintf_func( info->stream, "%08X", memaddr );
nkeynes@365
   102
    if( sym != NULL ) {
nkeynes@365
   103
	info->fprintf_func( info->stream, " <%s>", sym );
nkeynes@365
   104
    }
nkeynes@365
   105
}
nkeynes@362
   106
nkeynes@920
   107
void x86_print_symbolic_operand( char *buf, int hex, unsigned int disp )
nkeynes@920
   108
{
nkeynes@920
   109
    const char *sym = x86_find_symbol(disp, NULL);
nkeynes@920
   110
    if( sym != NULL ) {
nkeynes@920
   111
        snprintf( buf, 50, "<%s>", sym );
nkeynes@920
   112
    } else if( hex ) {
nkeynes@920
   113
        sprintf( buf, "0x%x", disp );
nkeynes@920
   114
    } else {
nkeynes@920
   115
        sprintf( buf, "%d", (int)disp );
nkeynes@920
   116
    }
nkeynes@920
   117
}
nkeynes@920
   118
nkeynes@527
   119
uint32_t x86_disasm_instruction( uintptr_t pc, char *buf, int len, char *opcode )
nkeynes@362
   120
{
nkeynes@362
   121
    int count, i;
nkeynes@362
   122
nkeynes@362
   123
    x86_disasm_info.stream = buf;
nkeynes@362
   124
    buf[0] = 0;
nkeynes@362
   125
    count = print_insn_i386_att( pc, &x86_disasm_info );
nkeynes@362
   126
    if( count != 0 ) {
nkeynes@429
   127
	unsigned char tmp[count];
nkeynes@362
   128
	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
nkeynes@362
   129
	for( i=0; i<count; i++ ) {
nkeynes@362
   130
	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
nkeynes@362
   131
	    opcode += 3;
nkeynes@362
   132
	}
nkeynes@362
   133
	*(opcode-1) = '\0';
nkeynes@362
   134
    }
nkeynes@362
   135
    return pc + count;
nkeynes@362
   136
}
nkeynes@362
   137
nkeynes@362
   138
int x86_disasm_output( void *data, const char *format, ... )
nkeynes@362
   139
{
nkeynes@362
   140
    char *p = (char *)data;
nkeynes@362
   141
    va_list ap;
nkeynes@362
   142
    int n;
nkeynes@362
   143
    p += strlen(p);
nkeynes@362
   144
    va_start( ap, format );
nkeynes@362
   145
    n = vsprintf( p, format, ap );
nkeynes@362
   146
    va_end( ap );
nkeynes@362
   147
    return n;
nkeynes@362
   148
}
.