Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 561:533f6b478071
prev527:14c9489f647e
next564:dc7b5ffb0535
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <string.h>
    22 #include "x86dasm.h"
    23 #include "bfd.h"
    24 #include "dis-asm.h"
    25 #include "sh4/sh4core.h"
    26 #include "sh4/sh4trans.h"
    28 extern const struct reg_desc_struct sh4_reg_map[];
    29 const struct cpu_desc_struct x86_cpu_desc = 
    30     { "x86", x86_disasm_instruction, NULL, mem_has_page, 
    31       NULL, NULL, NULL, 1, 
    32       (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
    33       &sh4r.pc };
    35 static int x86_disasm_output( void *data, const char *format, ... );
    36 static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info );
    38 static struct disassemble_info x86_disasm_info;
    40 static x86_symbol *x86_symtab;
    41 static int x86_num_symbols = 0;   
    44 void xlat_disasm_block( FILE *out, void *block )
    45 {
    46     uint32_t buflen = xlat_get_block_size(block);
    47     x86_set_symtab( NULL, 0 );
    48     x86_disasm_block( out, block, buflen );
    49 }
    51 void x86_disasm_block(FILE *out, void *start, uint32_t len)
    52 {
    53     uintptr_t start_addr = (uintptr_t)start;
    54     uint32_t pc;
    55     x86_disasm_init( start, start_addr, len );
    56     for( pc = start_addr; pc < start_addr + len;  ) {
    57 	char buf[256];
    58 	char op[256];
    59 	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
    60 	fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
    61 	pc = pc2;
    62     }
    63 }
    65 void x86_disasm_init(unsigned char *buf, uintptr_t vma, int buflen)
    66 {
    67     init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
    68     x86_disasm_info.arch = bfd_arch_i386;
    69 #if SH4_TRANSLATOR == TARGET_X86_64
    70     x86_disasm_info.mach =  bfd_mach_x86_64_intel_syntax;
    71 #else
    72     x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
    73 #endif
    74     x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
    75     x86_disasm_info.buffer = buf;
    76     x86_disasm_info.buffer_vma = vma;
    77     x86_disasm_info.buffer_length = buflen;
    78     x86_disasm_info.print_address_func = x86_print_address;
    79 }
    81 void x86_set_symtab( x86_symbol *symtab, int num_symbols )
    82 {
    83     x86_symtab = symtab;
    84     x86_num_symbols = num_symbols;
    85 }
    87 static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
    88 {
    89     int i;
    90     for( i=0; i<x86_num_symbols; i++ ) {
    91 	if( x86_symtab[i].ptr == (void *)(uintptr_t)memaddr ) {
    92 	    return x86_symtab[i].name;
    93 	}
    94     }
    95     return NULL;
    96 }
    98 static void x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
    99 {
   100     const char *sym = x86_find_symbol(memaddr, info);
   101     info->fprintf_func( info->stream, "%08X", memaddr );
   102     if( sym != NULL ) {
   103 	info->fprintf_func( info->stream, " <%s>", sym );
   104     }
   105 }
   107 uint32_t x86_disasm_instruction( uintptr_t pc, char *buf, int len, char *opcode )
   108 {
   109     int count, i;
   111     x86_disasm_info.stream = buf;
   112     buf[0] = 0;
   113     count = print_insn_i386_att( pc, &x86_disasm_info );
   114     if( count != 0 ) {
   115 	unsigned char tmp[count];
   116 	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
   117 	for( i=0; i<count; i++ ) {
   118 	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
   119 	    opcode += 3;
   120 	}
   121 	*(opcode-1) = '\0';
   122     }
   123     return pc + count;
   124 }
   126 int x86_disasm_output( void *data, const char *format, ... )
   127 {
   128     char *p = (char *)data;
   129     va_list ap;
   130     int n;
   131     p += strlen(p);
   132     va_start( ap, format );
   133     n = vsprintf( p, format, ap );
   134     va_end( ap );
   135     return n;
   136 }
.