Search
lxdream.org :: lxdream/src/x86dasm/x86dasm.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/x86dasm/x86dasm.c
changeset 376:8c7587af5a5d
prev365:94cab5ad0ed8
next429:e581b90c3fb3
author nkeynes
date Thu Sep 20 08:37:19 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Move support routines to sh4.c
view annotate diff log raw
     1 /**
     2  * $Id: x86dasm.c,v 1.3 2007-09-12 09:16:47 nkeynes Exp $
     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 "x86dasm.h"
    22 #include "bfd.h"
    23 #include "dis-asm.h"
    24 #include "sh4/sh4core.h"
    26 extern const struct reg_desc_struct sh4_reg_map[];
    27 const struct cpu_desc_struct x86_cpu_desc = 
    28     { "x86", x86_disasm_instruction, NULL, mem_has_page, 
    29       NULL, NULL, NULL, 1, 
    30       (char *)&sh4r, sizeof(sh4r), sh4_reg_map,
    31       &sh4r.pc };
    33 static int x86_disasm_output( void *data, const char *format, ... );
    34 static int x86_read_memory( bfd_vma memaddr, bfd_byte *buffer, unsigned int length,
    35 			    struct disassemble_info *info );
    36 static int 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;   
    43 void x86_disasm_block(FILE *out, void *start, uint32_t len)
    44 {
    45     uint32_t start_addr = (uint32_t)start;
    46     uint32_t pc;
    47     x86_disasm_init( start, start_addr, len );
    48     for( pc = start_addr; pc < start_addr + len;  ) {
    49 	char buf[256];
    50 	char op[256];
    51 	uint32_t pc2 = x86_disasm_instruction( pc, buf, sizeof(buf), op );
    52 	fprintf( out, "%08X: %-20s %s\n", pc, op, buf );
    53 	pc = pc2;
    54     }
    55 }
    57 void x86_disasm_init(char *buf, uint32_t vma, int buflen)
    58 {
    59     init_disassemble_info( &x86_disasm_info, NULL, x86_disasm_output );
    60     x86_disasm_info.arch = bfd_arch_i386;
    61     x86_disasm_info.mach = bfd_mach_i386_i386_intel_syntax;
    62     x86_disasm_info.endian = BFD_ENDIAN_LITTLE;
    63     x86_disasm_info.buffer = buf;
    64     x86_disasm_info.buffer_vma = vma;
    65     x86_disasm_info.buffer_length = buflen;
    66     x86_disasm_info.print_address_func = x86_print_address;
    67 }
    69 void x86_set_symtab( x86_symbol *symtab, int num_symbols )
    70 {
    71     x86_symtab = symtab;
    72     x86_num_symbols = num_symbols;
    73 }
    75 static const char *x86_find_symbol( bfd_vma memaddr, struct disassemble_info *info )
    76 {
    77     int i;
    78     for( i=0; i<x86_num_symbols; i++ ) {
    79 	if( x86_symtab[i].ptr == (void *)memaddr ) {
    80 	    return x86_symtab[i].name;
    81 	}
    82     }
    83     return NULL;
    84 }
    86 static int x86_print_address( bfd_vma memaddr, struct disassemble_info *info )
    87 {
    88     const char *sym = x86_find_symbol(memaddr, info);
    89     info->fprintf_func( info->stream, "%08X", memaddr );
    90     if( sym != NULL ) {
    91 	info->fprintf_func( info->stream, " <%s>", sym );
    92     }
    93     return 0;
    94 }
    96 uint32_t x86_disasm_instruction( uint32_t pc, char *buf, int len, char *opcode )
    97 {
    98     int count, i;
   100     x86_disasm_info.stream = buf;
   101     buf[0] = 0;
   102     count = print_insn_i386_att( pc, &x86_disasm_info );
   103     if( count != 0 ) {
   104 	char tmp[count];
   105 	x86_disasm_info.read_memory_func( pc, tmp, count, &x86_disasm_info );
   106 	for( i=0; i<count; i++ ) {
   107 	    sprintf( opcode, "%02X ", ((unsigned int)tmp[i])&0xFF );
   108 	    opcode += 3;
   109 	}
   110 	*(opcode-1) = '\0';
   111     }
   112     return pc + count;
   113 }
   115 int x86_disasm_output( void *data, const char *format, ... )
   116 {
   117     char *p = (char *)data;
   118     va_list ap;
   119     int n;
   120     p += strlen(p);
   121     va_start( ap, format );
   122     n = vsprintf( p, format, ap );
   123     va_end( ap );
   124     return n;
   125 }
.