- Update udis86 to git head
* r_anal
  - Print more fields in test_anal
* r_socket
  - Rename socket_udp_connect to r_socket_udp_connect
* Fix build
This commit is contained in:
Nibble 2010-08-24 19:59:19 +02:00
parent c6a115c602
commit c83d11ffd4
18 changed files with 4744 additions and 4872 deletions

View File

@ -14,16 +14,38 @@ static int usage() {
}
static int analyze(RAnal *anal, RAnalOp *aop, ut64 offset, ut8* buf, int len) {
char *stackop = NULL;
int ret;
ret = r_anal_aop (anal, aop, offset, buf, len);
if (ret) {
switch (aop->stackop) {
case R_ANAL_STACK_NULL:
stackop = strdup ("null");
break;
case R_ANAL_STACK_NOP:
stackop = strdup ("nop");
break;
case R_ANAL_STACK_INCSTACK:
stackop = strdup ("incstack");
break;
case R_ANAL_STACK_GET:
stackop = strdup ("get");
break;
case R_ANAL_STACK_SET:
stackop = strdup ("set");
break;
}
eprintf ("jump: 0x%08"PFMT64x"\n"
"fail: 0x%08"PFMT64x"\n"
"ref: 0x%08"PFMT64x"\n"
"value: 0x%08"PFMT64x"\n"
"stackop: %s\n"
"stackptr: %"PFMT64d"\n",
aop->jump, aop->fail, aop->ref, aop->value, aop->stackptr);
aop->jump, aop->fail, aop->ref, aop->value,
stackop?stackop:"unk", aop->stackptr);
free (stackop);
}
return ret;
}

View File

@ -1,22 +0,0 @@
Copyright (c) 2002, 2003, 2004, 2005, 2006 <vivek@sig9.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,40 +0,0 @@
Udis86 Disassembler for x86 and x86-64 (AMD64)
==============================================
(http://udis86.sourceforge.net/)
Udis86 is an easy-to-use minimalistic disassembler library (libudis86) for the
x86 and x86-64 (AMD64) range of instruction set architectures. The primary
intent of the design and development of udis86 is to aid software development
projects that entail binary code analysis.
Salient features of libudis86:
* Full support for the x86 and x86-64 (AMD64) ISAs.
* Includes support for all MMX, SSE, FPU(x87), and AMD 3Dnow! Instructions
* Supports 16/32/64 Bits disassembly modes.
* Capable of generating output in AT&T or INTEL assembly language syntax.
* Supports different types of inputs - File, Buffered, and Hooks.
* Thread-safe and Re-entrant.
You can reach the author at vivek[at]sig9[dot]com.
Building Udis86
===============
On Unix(-like) Systems:
$ ./configure
$ make
$ sudo make install
This will build the library as well as a simple command line front end (udcli).
Using libudis86
===============
See docs/doc.html
Using udcli
===========
See docs/doc.html

View File

@ -1,25 +1,62 @@
/* -----------------------------------------------------------------------------
* decode.c
*
* Copyright (c) 2005, 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
/* udis86 - libudis86/decode.c
*
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#ifndef HAVE_ASSERT_H
# define assert(x)
#else /* !HAVE_ASSERT_H */
# include <assert.h>
#endif /* HAVE_ASSERT_H */
#include "types.h"
#include "itab.h"
#include "input.h"
#include "decode.h"
#define dbg(x, n...)
/* #define dbg printf */
#ifndef __UD_STANDALONE__
# include <string.h>
#endif /* __UD_STANDALONE__ */
/* The max number of prefixes to an instruction */
#define MAX_PREFIXES 15
static struct ud_itab_entry ie_invalid = { UD_Iinvalid, O_NONE, O_NONE, O_NONE, P_none };
static struct ud_itab_entry ie_pause = { UD_Ipause, O_NONE, O_NONE, O_NONE, P_none };
static struct ud_itab_entry ie_nop = { UD_Inop, O_NONE, O_NONE, O_NONE, P_none };
/* instruction aliases and special cases */
static struct ud_itab_entry s_ie__invalid =
{ UD_Iinvalid, O_NONE, O_NONE, O_NONE, P_none };
static struct ud_itab_entry s_ie__pause =
{ UD_Ipause, O_NONE, O_NONE, O_NONE, P_none };
static struct ud_itab_entry s_ie__nop =
{ UD_Inop, O_NONE, O_NONE, O_NONE, P_none };
/* Looks up mnemonic code in the mnemonic string table
@ -27,9 +64,7 @@ static struct ud_itab_entry ie_nop = { UD_Inop, O_NONE, O_NONE, O_NONE,
*/
const char * ud_lookup_mnemonic( enum ud_mnemonic_code c )
{
if ( c < UD_Id3vil )
return ud_mnemonics_str[ c ];
return NULL;
return ud_mnemonics_str[ c ];
}
@ -154,176 +189,149 @@ static int get_prefixes( struct ud* u )
}
static inline unsigned int modrm( struct ud * u )
{
if ( !u->have_modrm ) {
u->modrm = inp_next( u );
u->have_modrm = 1;
}
return u->modrm;
}
/* Searches the instruction tables for the right entry.
*/
static int search_itab( struct ud * u )
{
struct ud_itab_entry * e = NULL;
enum ud_itab_index table;
uint8_t peek;
uint8_t did_peek = 0;
uint8_t curr;
uint8_t index;
extern uint16_t ud_itab__0[];
uint16_t ptr;
/* if in state of error, return */
inp_next( u );
if ( u->error )
return -1;
ptr = ud_itab__0[ inp_curr( u ) ];
/* get first byte of opcode. */
inp_next(u);
if ( u->error )
return -1;
curr = inp_curr(u);
/* resolve xchg, nop, pause crazyness */
if ( 0x90 == curr ) {
/* special case: nop */
if ( inp_curr( u ) == 0x90 ) {
if ( !( u->dis_mode == 64 && REX_B( u->pfx_rex ) ) ) {
if ( u->pfx_rep ) {
u->pfx_rep = 0;
e = & ie_pause;
u->itab_entry = & s_ie__pause;
} else {
e = & ie_nop;
u->itab_entry = & s_ie__nop;
}
goto found_entry;
u->mnemonic = u->itab_entry->mnemonic;
return 0;
}
}
/* get top-level table */
if ( 0x0F == curr ) {
table = ITAB__0F;
curr = inp_next(u);
if ( u->error )
return -1;
while ( 0x8000 & ptr ) {
uint8_t idx = 0;
/* 2byte opcodes can be modified by 0x66, F3, and F2 prefixes */
if ( 0x66 == u->pfx_insn ) {
if ( ud_itab_list[ ITAB__PFX_SSE66__0F ][ curr ].mnemonic != UD_Iinvalid ) {
table = ITAB__PFX_SSE66__0F;
u->pfx_opr = 0;
}
} else if ( 0xF2 == u->pfx_insn ) {
if ( ud_itab_list[ ITAB__PFX_SSEF2__0F ][ curr ].mnemonic != UD_Iinvalid ) {
table = ITAB__PFX_SSEF2__0F;
u->pfx_repne = 0;
}
} else if ( 0xF3 == u->pfx_insn ) {
if ( ud_itab_list[ ITAB__PFX_SSEF3__0F ][ curr ].mnemonic != UD_Iinvalid ) {
table = ITAB__PFX_SSEF3__0F;
u->pfx_repe = 0;
u->pfx_rep = 0;
}
u->le = &ud_lookup_table_list[ ( ~0x8000 & ptr ) ];
switch ( u->le->type ) {
case UD_TAB__OPC_TABLE:
idx = inp_curr( u );
break;
case UD_TAB__OPC_3BYTE:
inp_next( u );
if ( u->error )
return -1;
idx = inp_curr( u );
break;
case UD_TAB__OPC_2BYTE:
/* 00 = 0
* f2 = 1
* f3 = 2
* 66 = 3
*/
idx = ( ( u->pfx_insn & 0xf ) + 1 ) / 2;
inp_next( u );
if ( ud_lookup_table_list[ u->le->table[ idx ] & ~0x8000 ].table[ inp_curr( u ) ] == 0 ) {
idx = 0;
} else {
switch ( u->pfx_insn ) {
case 0xf2:
u->pfx_repne = 0;
break;
case 0xf3:
u->pfx_rep = 0;
u->pfx_repe = 0;
break;
case 0x66:
u->pfx_opr = 0;
/* recalculate operand mode */
if ( u->dis_mode == 64 ) {
u->opr_mode = REX_W( u->pfx_rex ) ? 64 : 32;
} else {
u->opr_mode = u->dis_mode;
}
break;
}
}
break;
case UD_TAB__OPC_MOD:
/* !11 = 0
* 11 = 1
*/
idx = ( MODRM_MOD( modrm( u ) ) + 1 ) / 4;
break;
case UD_TAB__OPC_MODE:
/* 16 = 0
* 32 = 1
* 64 = 2
*/
idx = ( u->dis_mode / 32 );
break;
case UD_TAB__OPC_OSIZE:
/* 16 = 0
* 32 = 1
* 64 = 2
*/
idx = ( u->opr_mode / 32 );
break;
case UD_TAB__OPC_ASIZE:
/* 16 = 0
* 32 = 1
* 64 = 2
*/
idx = ( u->adr_mode / 32 );
break;
case UD_TAB__OPC_X87:
idx = modrm( u ) - 0xC0;
break;
case UD_TAB__OPC_VENDOR:
if ( u->vendor == UD_VENDOR_ANY ) {
/* choose a valid entry */
if ( u->le->table[ idx ] != 0 )
idx = 0;
else
idx = 1;
} else if ( u->vendor == UD_VENDOR_AMD ) {
idx = 0;
} else {
idx = 1;
}
break;
case UD_TAB__OPC_RM:
idx = MODRM_RM( modrm( u ) );
break;
case UD_TAB__OPC_REG:
idx = MODRM_REG( modrm( u ) );
break;
default:
idx = 0;
assert( !"Invalid table type" );
}
/* pick an instruction from the 1byte table */
} else {
table = ITAB__1BYTE;
}
index = curr;
search:
e = & ud_itab_list[ table ][ index ];
/* if mnemonic constant is a standard instruction constant
* our search is over.
*/
if ( e->mnemonic < UD_Id3vil ) {
if ( e->mnemonic == UD_Iinvalid ) {
if ( did_peek ) {
inp_next( u ); if ( u->error ) return -1;
}
goto found_entry;
}
goto found_entry;
}
table = e->prefix;
switch ( e->mnemonic )
{
case UD_Igrp_reg:
peek = inp_peek( u );
did_peek = 1;
index = MODRM_REG( peek );
break;
case UD_Igrp_mod:
peek = inp_peek( u );
did_peek = 1;
index = MODRM_MOD( peek );
if ( index == 3 )
index = ITAB__MOD_INDX__11;
else
index = ITAB__MOD_INDX__NOT_11;
break;
case UD_Igrp_rm:
curr = inp_next( u );
did_peek = 0;
if ( u->error )
if ( u->error )
return -1;
index = MODRM_RM( curr );
break;
case UD_Igrp_x87:
curr = inp_next( u );
did_peek = 0;
if ( u->error )
return -1;
index = curr - 0xC0;
break;
case UD_Igrp_osize:
if ( u->opr_mode == 64 )
index = ITAB__MODE_INDX__64;
else if ( u->opr_mode == 32 )
index = ITAB__MODE_INDX__32;
else
index = ITAB__MODE_INDX__16;
break;
case UD_Igrp_asize:
if ( u->adr_mode == 64 )
index = ITAB__MODE_INDX__64;
else if ( u->adr_mode == 32 )
index = ITAB__MODE_INDX__32;
else
index = ITAB__MODE_INDX__16;
break;
case UD_Igrp_mode:
if ( u->dis_mode == 64 )
index = ITAB__MODE_INDX__64;
else if ( u->dis_mode == 32 )
index = ITAB__MODE_INDX__32;
else
index = ITAB__MODE_INDX__16;
break;
case UD_Igrp_vendor:
if ( u->vendor == UD_VENDOR_INTEL )
index = ITAB__VENDOR_INDX__INTEL;
else if ( u->vendor == UD_VENDOR_AMD )
index = ITAB__VENDOR_INDX__AMD;
else
assert( !"unrecognized vendor id" );
break;
case UD_Id3vil:
assert( !"invalid instruction mnemonic constant Id3vil" );
break;
default:
assert( !"invalid instruction mnemonic constant" );
break;
ptr = u->le->table[ idx ];
}
goto search;
found_entry:
u->itab_entry = e;
u->itab_entry = &ud_itab[ ptr ];
u->mnemonic = u->itab_entry->mnemonic;
return 0;
@ -357,12 +365,12 @@ static int resolve_mnemonic( struct ud* u )
u->br_near = 0;
/* readjust operand sizes for call/jmp instrcutions */
if ( u->mnemonic == UD_Icall || u->mnemonic == UD_Ijmp ) {
/* WP: 16bit pointer */
/* WP: 16:16 pointer */
if ( u->operand[ 0 ].size == SZ_WP ) {
u->operand[ 0 ].size = 16;
u->br_far = 1;
u->br_near= 0;
/* DP: 32bit pointer */
/* DP: 32:32 pointer */
} else if ( u->operand[ 0 ].size == SZ_DP ) {
u->operand[ 0 ].size = 32;
u->br_far = 1;
@ -373,7 +381,7 @@ static int resolve_mnemonic( struct ud* u )
}
/* resolve 3dnow weirdness. */
} else if ( u->mnemonic == UD_I3dnow ) {
u->mnemonic = ud_itab_list[ ITAB__3DNOW ][ inp_curr( u ) ].mnemonic;
u->mnemonic = ud_itab[ u->le->table[ inp_curr( u ) ] ].mnemonic;
}
/* SWAPGS is only valid in 64bits mode */
if ( u->mnemonic == UD_Iswapgs && u->dis_mode != 64 ) {
@ -441,19 +449,23 @@ decode_gpr(register struct ud* u, unsigned int s, unsigned char rm)
* -----------------------------------------------------------------------------
*/
static enum ud_type
resolve_gpr64(struct ud* u, enum ud_operand_code gpr_op)
resolve_gpr64(struct ud* u, enum ud_operand_code gpr_op, enum ud_operand_size * size)
{
if (gpr_op >= OP_rAXr8 && gpr_op <= OP_rDIr15)
gpr_op = (gpr_op - OP_rAXr8) | (REX_B(u->pfx_rex) << 3);
else gpr_op = (gpr_op - OP_rAX);
if (u->opr_mode == 16)
if (u->opr_mode == 16) {
*size = 16;
return gpr_op + UD_R_AX;
}
if (u->dis_mode == 32 ||
(u->opr_mode == 32 && ! (REX_W(u->pfx_rex) || u->default64))) {
*size = 32;
return gpr_op + UD_R_EAX;
}
*size = 64;
return gpr_op + UD_R_RAX;
}
@ -520,12 +532,10 @@ decode_modrm(struct ud* u, struct ud_operand *op, unsigned int s,
{
unsigned char mod, rm, reg;
inp_next(u);
/* get mod, r/m and reg fields */
mod = MODRM_MOD(inp_curr(u));
rm = (REX_B(u->pfx_rex) << 3) | MODRM_RM(inp_curr(u));
reg = (REX_R(u->pfx_rex) << 3) | MODRM_REG(inp_curr(u));
mod = MODRM_MOD(modrm(u));
rm = (REX_B(u->pfx_rex) << 3) | MODRM_RM(modrm(u));
reg = (REX_R(u->pfx_rex) << 3) | MODRM_REG(modrm(u));
op->size = resolve_operand_size(u, s);
@ -708,17 +718,28 @@ static int disasm_operands(register struct ud* u)
unsigned int mop3s = u->itab_entry->operand3.size;
/* iop = instruction operand */
register struct ud_operand* iop = u->operand;
struct ud_operand* iop = u->operand;
switch(mop1t) {
case OP_A :
decode_a(u, &(iop[0]));
break;
case OP_MR:
if ( MODRM_MOD( modrm( u ) ) == 3 ) {
decode_modrm( u, &(iop[0]), SZ_V, T_GPR, NULL, 0, T_NONE );
} else {
if ( mop1s == SZ_WV )
decode_modrm( u, &(iop[0]), SZ_W, T_GPR, NULL, 0, T_NONE );
else if ( mop1s == SZ_BV )
decode_modrm( u, &(iop[0]), SZ_B, T_GPR, NULL, 0, T_NONE );
}
break;
/* M[b] ... */
case OP_M :
if (MODRM_MOD(inp_peek(u)) == 3)
if (MODRM_MOD(modrm(u)) == 3)
u->error= 1;
/* E, G/P/V/I/CL/1/S */
case OP_E :
@ -734,9 +755,12 @@ static int disasm_operands(register struct ud* u)
}
else if (mop2t == OP_P)
decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_MMX);
else if (mop2t == OP_V)
else if (mop2t == OP_V) {
decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_XMM);
else if (mop2t == OP_S)
if (mop3t == OP_I) {
decode_imm(u, mop3s, &(iop[2]));
}
} else if (mop2t == OP_S)
decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_SEG);
else {
decode_modrm(u, &(iop[0]), mop1s, T_GPR, NULL, 0, T_NONE);
@ -756,7 +780,7 @@ static int disasm_operands(register struct ud* u)
/* G, E/PR[,I]/VR */
case OP_G :
if (mop2t == OP_M) {
if (MODRM_MOD(inp_peek(u)) == 3)
if (MODRM_MOD(modrm( u )) == 3)
u->error= 1;
decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_GPR);
} else if (mop2t == OP_E) {
@ -768,9 +792,11 @@ static int disasm_operands(register struct ud* u)
if (mop3t == OP_I)
decode_imm(u, mop3s, &(iop[2]));
} else if (mop2t == OP_VR) {
if (MODRM_MOD(inp_peek(u)) != 3)
if (MODRM_MOD(modrm(u)) != 3)
u->error = 1;
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_GPR);
if (mop3t == OP_I)
decode_imm(u, mop3s, &(iop[2]));
} else if (mop2t == OP_W)
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_GPR);
break;
@ -801,13 +827,13 @@ static int disasm_operands(register struct ud* u)
case OP_rSP : case OP_rBP : case OP_rSI : case OP_rDI :
iop[0].type = UD_OP_REG;
iop[0].base = resolve_gpr64(u, mop1t);
iop[0].base = resolve_gpr64(u, mop1t, &(iop[0].size));
if (mop2t == OP_I)
decode_imm(u, mop2s, &(iop[1]));
else if (mop2t >= OP_rAX && mop2t <= OP_rDI) {
iop[1].type = UD_OP_REG;
iop[1].base = resolve_gpr64(u, mop2t);
iop[1].base = resolve_gpr64(u, mop2t, &(iop[1].size));
}
else if (mop2t == OP_O) {
decode_o(u, mop2s, &(iop[1]));
@ -835,6 +861,7 @@ static int disasm_operands(register struct ud* u)
case OP_eSP : case OP_eBP : case OP_eSI : case OP_eDI :
iop[0].type = UD_OP_REG;
iop[0].base = resolve_gpr32(u, mop1t);
iop[0].size = u->opr_mode == 16 ? 16 : 32;
if (mop2t == OP_DX) {
iop[1].type = UD_OP_REG;
iop[1].base = UD_R_DX;
@ -865,7 +892,7 @@ static int disasm_operands(register struct ud* u)
/* PR, I */
case OP_PR:
if (MODRM_MOD(inp_peek(u)) != 3)
if (MODRM_MOD(modrm(u)) != 3)
u->error = 1;
decode_modrm(u, &(iop[0]), mop1s, T_MMX, NULL, 0, T_NONE);
if (mop2t == OP_I)
@ -874,7 +901,7 @@ static int disasm_operands(register struct ud* u)
/* VR, I */
case OP_VR:
if (MODRM_MOD(inp_peek(u)) != 3)
if (MODRM_MOD(modrm(u)) != 3)
u->error = 1;
decode_modrm(u, &(iop[0]), mop1s, T_XMM, NULL, 0, T_NONE);
if (mop2t == OP_I)
@ -890,7 +917,7 @@ static int disasm_operands(register struct ud* u)
} else if (mop2t == OP_W) {
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_MMX);
} else if (mop2t == OP_VR) {
if (MODRM_MOD(inp_peek(u)) != 3)
if (MODRM_MOD(modrm(u)) != 3)
u->error = 1;
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_MMX);
} else if (mop2t == OP_E) {
@ -936,27 +963,23 @@ static int disasm_operands(register struct ud* u)
/* V, W[,I]/Q/M/E */
case OP_V :
if (mop2t == OP_W) {
/* special cases for movlps and movhps */
if (MODRM_MOD(inp_peek(u)) == 3) {
if (u->mnemonic == UD_Imovlps)
u->mnemonic = UD_Imovhlps;
else
if (u->mnemonic == UD_Imovhps)
u->mnemonic = UD_Imovlhps;
}
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_XMM);
if (mop3t == OP_I)
decode_imm(u, mop3s, &(iop[2]));
} else if (mop2t == OP_Q)
decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_XMM);
else if (mop2t == OP_M) {
if (MODRM_MOD(inp_peek(u)) == 3)
if (MODRM_MOD(modrm(u)) == 3)
u->error= 1;
decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_XMM);
} else if (mop2t == OP_E) {
decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_XMM);
} else if (mop2t == OP_PR) {
if (MODRM_MOD(modrm(u)) != 3) u->error = 1;
decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_XMM);
} else if (mop2t == OP_VR) {
if (MODRM_MOD(modrm(u)) != 3) u->error = 1;
decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_XMM);
}
break;
@ -1002,7 +1025,7 @@ static int disasm_operands(register struct ud* u)
else if (mop2t == OP_eAX)
iop[1].base = resolve_gpr32(u, mop2t);
else if (mop2t == OP_rAX)
iop[1].base = resolve_gpr64(u, mop2t);
iop[1].base = resolve_gpr64(u, mop2t, &(iop[1].size));
break;
/* 3 */
@ -1055,11 +1078,11 @@ static int clear_insn(register struct ud* u)
u->pfx_repne = 0;
u->pfx_rep = 0;
u->pfx_repe = 0;
u->pfx_seg = 0;
u->pfx_rex = 0;
u->pfx_insn = 0;
u->mnemonic = UD_Inone;
u->itab_entry = NULL;
u->have_modrm = 0;
memset( &u->operand[ 0 ], 0, sizeof( struct ud_operand ) );
memset( &u->operand[ 1 ], 0, sizeof( struct ud_operand ) );
@ -1168,15 +1191,23 @@ unsigned int ud_decode( struct ud* u )
; /* error */
}
/* Plugin decode error. */
/* Handle decode error. */
if ( u->error ) {
/* clear out the decode data. */
clear_insn( u );
/* mark the sequence of bytes as invalid. */
u->itab_entry = & ie_invalid;
u->itab_entry = & s_ie__invalid;
u->mnemonic = u->itab_entry->mnemonic;
}
/* maybe this stray segment override byte
* should be spewed out?
*/
if ( !P_SEG( u->itab_entry->prefix ) &&
u->operand[0].type != UD_OP_MEM &&
u->operand[1].type != UD_OP_MEM )
u->pfx_seg = 0;
u->insn_offset = u->pc; /* set offset of instruction */
u->insn_fill = 0; /* set translation buffer index to 0 */
u->pc += u->inp_ctr; /* move program counter by bytes decoded */

View File

@ -1,6 +1,34 @@
/* udis86 - libudis86/decode.h
*
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UD_DECODE_H
#define UD_DECODE_H
#include "types.h"
#include "itab.h"
#define MAX_INSN_LENGTH 15
/* register classes */
@ -14,6 +42,8 @@
/* itab prefix bits */
#define P_none ( 0 )
#define P_cast ( 1 << 0 )
#define P_CAST(n) ( ( n >> 0 ) & 1 )
#define P_c1 ( 1 << 0 )
#define P_C1(n) ( ( n >> 0 ) & 1 )
#define P_rexb ( 1 << 1 )
@ -40,6 +70,10 @@
#define P_REXX(n) ( ( n >> 11 ) & 1 )
#define P_ImpAddr ( 1 << 12 )
#define P_IMPADDR(n) ( ( n >> 12 ) & 1 )
#define P_seg ( 1 << 13 )
#define P_SEG(n) ( ( n >> 13 ) & 1 )
#define P_sext ( 1 << 14 )
#define P_SEXT(n) ( ( n >> 14 ) & 1 )
/* rex prefix bits */
#define REX_W(r) ( ( 0xF & ( r ) ) >> 3 )
@ -99,8 +133,10 @@ enum ud_operand_code {
OP_V, OP_W, OP_Q, OP_P,
OP_R, OP_C, OP_D, OP_VR, OP_PR
};
OP_R, OP_C, OP_D, OP_VR, OP_PR,
OP_MR
} UD_ATTR_PACKED;
/* operand size constants */
@ -124,122 +160,12 @@ enum ud_operand_size {
SZ_D = 32,
SZ_Q = 64,
SZ_T = 80,
};
SZ_O = 128,
/* itab entry operand definitions */
SZ_WV = 17,
SZ_BV = 18,
#define O_rSPr12 { OP_rSPr12, SZ_NA }
#define O_BL { OP_BL, SZ_NA }
#define O_BH { OP_BH, SZ_NA }
#define O_BP { OP_BP, SZ_NA }
#define O_AHr12b { OP_AHr12b, SZ_NA }
#define O_BX { OP_BX, SZ_NA }
#define O_Jz { OP_J, SZ_Z }
#define O_Jv { OP_J, SZ_V }
#define O_Jb { OP_J, SZ_B }
#define O_rSIr14 { OP_rSIr14, SZ_NA }
#define O_GS { OP_GS, SZ_NA }
#define O_D { OP_D, SZ_NA }
#define O_rBPr13 { OP_rBPr13, SZ_NA }
#define O_Ob { OP_O, SZ_B }
#define O_P { OP_P, SZ_NA }
#define O_Ow { OP_O, SZ_W }
#define O_Ov { OP_O, SZ_V }
#define O_Gw { OP_G, SZ_W }
#define O_Gv { OP_G, SZ_V }
#define O_rDX { OP_rDX, SZ_NA }
#define O_Gx { OP_G, SZ_MDQ }
#define O_Gd { OP_G, SZ_D }
#define O_Gb { OP_G, SZ_B }
#define O_rBXr11 { OP_rBXr11, SZ_NA }
#define O_rDI { OP_rDI, SZ_NA }
#define O_rSI { OP_rSI, SZ_NA }
#define O_ALr8b { OP_ALr8b, SZ_NA }
#define O_eDI { OP_eDI, SZ_NA }
#define O_Gz { OP_G, SZ_Z }
#define O_eDX { OP_eDX, SZ_NA }
#define O_DHr14b { OP_DHr14b, SZ_NA }
#define O_rSP { OP_rSP, SZ_NA }
#define O_PR { OP_PR, SZ_NA }
#define O_NONE { OP_NONE, SZ_NA }
#define O_rCX { OP_rCX, SZ_NA }
#define O_jWP { OP_J, SZ_WP }
#define O_rDXr10 { OP_rDXr10, SZ_NA }
#define O_Md { OP_M, SZ_D }
#define O_C { OP_C, SZ_NA }
#define O_G { OP_G, SZ_NA }
#define O_Mb { OP_M, SZ_B }
#define O_Mt { OP_M, SZ_T }
#define O_S { OP_S, SZ_NA }
#define O_Mq { OP_M, SZ_Q }
#define O_W { OP_W, SZ_NA }
#define O_ES { OP_ES, SZ_NA }
#define O_rBX { OP_rBX, SZ_NA }
#define O_Ed { OP_E, SZ_D }
#define O_DLr10b { OP_DLr10b, SZ_NA }
#define O_Mw { OP_M, SZ_W }
#define O_Eb { OP_E, SZ_B }
#define O_Ex { OP_E, SZ_MDQ }
#define O_Ez { OP_E, SZ_Z }
#define O_Ew { OP_E, SZ_W }
#define O_Ev { OP_E, SZ_V }
#define O_Ep { OP_E, SZ_P }
#define O_FS { OP_FS, SZ_NA }
#define O_Ms { OP_M, SZ_W }
#define O_rAXr8 { OP_rAXr8, SZ_NA }
#define O_eBP { OP_eBP, SZ_NA }
#define O_Isb { OP_I, SZ_SB }
#define O_eBX { OP_eBX, SZ_NA }
#define O_rCXr9 { OP_rCXr9, SZ_NA }
#define O_jDP { OP_J, SZ_DP }
#define O_CH { OP_CH, SZ_NA }
#define O_CL { OP_CL, SZ_NA }
#define O_R { OP_R, SZ_RDQ }
#define O_V { OP_V, SZ_NA }
#define O_CS { OP_CS, SZ_NA }
#define O_CHr13b { OP_CHr13b, SZ_NA }
#define O_eCX { OP_eCX, SZ_NA }
#define O_eSP { OP_eSP, SZ_NA }
#define O_SS { OP_SS, SZ_NA }
#define O_SP { OP_SP, SZ_NA }
#define O_BLr11b { OP_BLr11b, SZ_NA }
#define O_SI { OP_SI, SZ_NA }
#define O_eSI { OP_eSI, SZ_NA }
#define O_DL { OP_DL, SZ_NA }
#define O_DH { OP_DH, SZ_NA }
#define O_DI { OP_DI, SZ_NA }
#define O_DX { OP_DX, SZ_NA }
#define O_rBP { OP_rBP, SZ_NA }
#define O_Gvw { OP_G, SZ_MDQ }
#define O_I1 { OP_I1, SZ_NA }
#define O_I3 { OP_I3, SZ_NA }
#define O_DS { OP_DS, SZ_NA }
#define O_ST4 { OP_ST4, SZ_NA }
#define O_ST5 { OP_ST5, SZ_NA }
#define O_ST6 { OP_ST6, SZ_NA }
#define O_ST7 { OP_ST7, SZ_NA }
#define O_ST0 { OP_ST0, SZ_NA }
#define O_ST1 { OP_ST1, SZ_NA }
#define O_ST2 { OP_ST2, SZ_NA }
#define O_ST3 { OP_ST3, SZ_NA }
#define O_E { OP_E, SZ_NA }
#define O_AH { OP_AH, SZ_NA }
#define O_M { OP_M, SZ_NA }
#define O_AL { OP_AL, SZ_NA }
#define O_CLr9b { OP_CLr9b, SZ_NA }
#define O_Q { OP_Q, SZ_NA }
#define O_eAX { OP_eAX, SZ_NA }
#define O_VR { OP_VR, SZ_NA }
#define O_AX { OP_AX, SZ_NA }
#define O_rAX { OP_rAX, SZ_NA }
#define O_Iz { OP_I, SZ_Z }
#define O_rDIr15 { OP_rDIr15, SZ_NA }
#define O_Iw { OP_I, SZ_W }
#define O_Iv { OP_I, SZ_V }
#define O_Ap { OP_A, SZ_P }
#define O_CX { OP_CX, SZ_NA }
#define O_Ib { OP_I, SZ_B }
#define O_BHr15b { OP_BHr15b, SZ_NA }
} UD_ATTR_PACKED;
/* A single operand of an entry in the instruction table.
@ -264,8 +190,66 @@ struct ud_itab_entry
uint32_t prefix;
};
struct ud_lookup_table_list_entry {
const uint16_t *table;
enum ud_table_type type;
const char *meta;
};
extern const char * ud_lookup_mnemonic( enum ud_mnemonic_code c );
static inline unsigned int sse_pfx_idx( const unsigned int pfx )
{
/* 00 = 0
* f2 = 1
* f3 = 2
* 66 = 3
*/
return ( ( pfx & 0xf ) + 1 ) / 2;
}
static inline unsigned int mode_idx( const unsigned int mode )
{
/* 16 = 0
* 32 = 1
* 64 = 2
*/
return ( mode / 32 );
}
static inline unsigned int modrm_mod_idx( const unsigned int mod )
{
/* !11 = 0
* 11 = 1
*/
return ( mod + 1 ) / 4;
}
static inline unsigned int vendor_idx( const unsigned int vendor )
{
switch ( vendor ) {
case UD_VENDOR_AMD: return 0;
case UD_VENDOR_INTEL: return 1;
case UD_VENDOR_ANY: return 2;
default: return 2;
}
}
static inline unsigned int is_group_ptr( uint16_t ptr )
{
return ( 0x8000 & ptr );
}
static inline unsigned int group_idx( uint16_t ptr )
{
return ( ~0x8000 & ptr );
}
extern struct ud_itab_entry ud_itab[];
extern struct ud_lookup_table_list_entry ud_lookup_table_list[];
#endif /* UD_DECODE_H */
/* vim:cindent

View File

@ -1,9 +1,27 @@
/* -----------------------------------------------------------------------------
* extern.h
/* udis86 - libudis86/extern.h
*
* Copyright (c) 2004, 2005, 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UD_EXTERN_H
#define UD_EXTERN_H
@ -12,7 +30,6 @@
extern "C" {
#endif
#include <stdio.h>
#include "types.h"
/* ============================= PUBLIC API ================================= */
@ -59,6 +76,10 @@ extern unsigned int ud_insn_len(struct ud* u);
extern const char* ud_lookup_mnemonic(enum ud_mnemonic_code c);
extern void ud_set_user_opaque_data(struct ud*, void*);
extern void *ud_get_user_opaque_data(struct ud*);
/* ========================================================================== */
#ifdef __cplusplus

View File

@ -1,9 +1,27 @@
/* -----------------------------------------------------------------------------
* input.c
/* udis86 - libudis86/input.c
*
* Copyright (c) 2004, 2005, 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "extern.h"
#include "types.h"
@ -44,6 +62,18 @@ ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*))
inp_init(u);
}
extern void
ud_set_user_opaque_data( struct ud * u, void * opaque )
{
u->user_opaque_data = opaque;
}
extern void *
ud_get_user_opaque_data( struct ud * u )
{
return u->user_opaque_data;
}
/* =============================================================================
* ud_inp_set_buffer() - Set buffer as input.
* =============================================================================

View File

@ -1,9 +1,27 @@
/* -----------------------------------------------------------------------------
* input.h
/* udis86 - libudis86/input.h
*
* Copyright (c) 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UD_INPUT_H
#define UD_INPUT_H

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,28 @@
/* -----------------------------------------------------------------------------
* syn-att.c
/* udis86 - libudis86/syn-att.c
*
* Copyright (c) 2004, 2005, 2006 Vivek Mohan <vivek@sig9.com>
* All rights reserved. See (LICENSE)
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "types.h"
#include "extern.h"
#include "decode.h"
@ -33,7 +50,6 @@ opr_cast(struct ud* u, struct ud_operand* op)
static void
gen_operand(struct ud* u, struct ud_operand* op)
{
int op_f = op->base;
switch(op->type) {
case UD_OP_REG:
mkasm(u, "%%%s", ud_reg_tab[op->base - UD_R_AL]);
@ -48,27 +64,12 @@ gen_operand(struct ud* u, struct ud_operand* op)
mkasm(u, "-0x%x", (-op->lval.sbyte) & 0xff);
else mkasm(u, "0x%x", op->lval.sbyte);
}
else if (op->offset == 16) {
if (op->lval.sbyte < 0)
mkasm(u, "-0x%x", -op->lval.uword);
else if (op->lval.sbyte > 0)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.uword);
} else if (op->offset == 32) {
if (u->adr_mode == 64) {
if (op->lval.sdword < 0)
mkasm(u, "-0x%x", -op->lval.sdword);
else if (op->lval.sdword > 0)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sdword);
} else {
if (op->lval.sdword < 0)
mkasm(u, "-0x%lx", -op->lval.udword);
else if (op->lval.sdword > 0)
mkasm(u, "%s0x%lx", (op_f)?"+":"",op->lval.udword);
}
} else if (op->offset == 64) {
// TODO: Implement 64 bit negative offsets
else if (op->offset == 16)
mkasm(u, "0x%x", op->lval.uword);
else if (op->offset == 32)
mkasm(u, "0x%lx", op->lval.udword);
else if (op->offset == 64)
mkasm(u, "0x" FMT64 "x", op->lval.uqword);
}
if (op->base)
mkasm(u, "(%%%s", ud_reg_tab[op->base - UD_R_AL]);
@ -84,15 +85,29 @@ gen_operand(struct ud* u, struct ud_operand* op)
mkasm(u, ")");
break;
case UD_OP_IMM:
switch (op->size) {
case 8: mkasm(u, "$0x%x", op->lval.ubyte); break;
case 16: mkasm(u, "$0x%x", op->lval.uword); break;
case 32: mkasm(u, "$0x%lx", op->lval.udword); break;
case 64: mkasm(u, "$0x" FMT64 "x", op->lval.uqword); break;
default: break;
}
case UD_OP_IMM: {
int64_t imm = 0;
uint64_t sext_mask = 0xffffffffffffffffull;
unsigned sext_size = op->size;
switch (op->size) {
case 8: imm = op->lval.sbyte; break;
case 16: imm = op->lval.sword; break;
case 32: imm = op->lval.sdword; break;
case 64: imm = op->lval.sqword; break;
}
if ( P_SEXT( u->itab_entry->prefix ) ) {
sext_size = u->operand[ 0 ].size;
if ( u->mnemonic == UD_Ipush )
/* push sign-extends to operand size */
sext_size = u->opr_mode;
}
if ( sext_size < 64 )
sext_mask = ( 1ull << sext_size ) - 1;
mkasm( u, "0x" FMT64 "x", imm & sext_mask );
break;
}
case UD_OP_JIMM:
switch (op->size) {
@ -100,10 +115,10 @@ gen_operand(struct ud* u, struct ud_operand* op)
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sbyte);
break;
case 16:
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sword);
mkasm(u, "0x" FMT64 "x", ( u->pc + op->lval.sword ) & 0xffff );
break;
case 32:
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sdword);
mkasm(u, "0x" FMT64 "x", ( u->pc + op->lval.sdword ) & 0xfffffffful );
break;
default:break;
}

View File

@ -1,11 +1,28 @@
/* -----------------------------------------------------------------------------
* syn-intel.c
/* udis86 - libudis86/syn-intel.c
*
* Copyright (c) 2002, 2003, 2004 Vivek Mohan <vivek@sig9.com>
* All rights reserved. See (LICENSE)
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "types.h"
#include "extern.h"
#include "decode.h"
@ -29,8 +46,6 @@ opr_cast(struct ud* u, struct ud_operand* op)
}
if (u->br_far)
mkasm(u, "far ");
else if (u->br_near)
mkasm(u, "near ");
}
/* -----------------------------------------------------------------------------
@ -74,44 +89,51 @@ static void gen_operand(struct ud* u, struct ud_operand* op, int syn_cast)
if (op->offset == 8) {
if (op->lval.sbyte < 0)
mkasm(u, "-0x%x", -op->lval.sbyte);
else if (op->lval.sbyte > 0)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sbyte);
} else if (op->offset == 16) {
if (op->lval.sbyte < 0)
mkasm(u, "-0x%x", -op->lval.uword);
else if (op->lval.sbyte > 0)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.uword);
} else if (op->offset == 32) {
else mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sbyte);
}
else if (op->offset == 16)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.uword);
else if (op->offset == 32) {
if (u->adr_mode == 64) {
if (op->lval.sdword < 0)
mkasm(u, "-0x%x", -op->lval.sdword);
else if (op->lval.sdword > 0)
mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sdword);
} else {
if (op->lval.sdword < 0)
mkasm(u, "-0x%lx", -op->lval.udword);
else if (op->lval.sdword > 0)
mkasm(u, "%s0x%lx", (op_f)?"+":"",op->lval.udword);
}
} else if (op->offset == 64) {
// TODO: Implement 64 bit negative offsets
mkasm(u, "%s0x" FMT64 "x", (op_f) ? "+" : "", op->lval.uqword);
else mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sdword);
}
else mkasm(u, "%s0x%lx", (op_f) ? "+" : "", op->lval.udword);
}
else if (op->offset == 64)
mkasm(u, "%s0x" FMT64 "x", (op_f) ? "+" : "", op->lval.uqword);
mkasm(u, "]");
break;
}
case UD_OP_IMM:
if (syn_cast) opr_cast(u, op);
switch (op->size) {
case 8: mkasm(u, "0x%x", op->lval.ubyte); break;
case 16: mkasm(u, "0x%x", op->lval.uword); break;
case 32: mkasm(u, "0x%lx", op->lval.udword); break;
case 64: mkasm(u, "0x" FMT64 "x", op->lval.uqword); break;
default: break;
}
case UD_OP_IMM: {
int64_t imm = 0;
uint64_t sext_mask = 0xffffffffffffffffull;
unsigned sext_size = op->size;
if (syn_cast)
opr_cast(u, op);
switch (op->size) {
case 8: imm = op->lval.sbyte; break;
case 16: imm = op->lval.sword; break;
case 32: imm = op->lval.sdword; break;
case 64: imm = op->lval.sqword; break;
}
if ( P_SEXT( u->itab_entry->prefix ) ) {
sext_size = u->operand[ 0 ].size;
if ( u->mnemonic == UD_Ipush )
/* push sign-extends to operand size */
sext_size = u->opr_mode;
}
if ( sext_size < 64 )
sext_mask = ( 1ull << sext_size ) - 1;
mkasm( u, "0x" FMT64 "x", imm & sext_mask );
break;
}
case UD_OP_JIMM:
if (syn_cast) opr_cast(u, op);
@ -120,10 +142,10 @@ static void gen_operand(struct ud* u, struct ud_operand* op, int syn_cast)
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sbyte);
break;
case 16:
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sword);
mkasm(u, "0x" FMT64 "x", ( u->pc + op->lval.sword ) & 0xffff );
break;
case 32:
mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sdword);
mkasm(u, "0x" FMT64 "x", ( u->pc + op->lval.sdword ) & 0xfffffffful );
break;
default:break;
}
@ -187,26 +209,58 @@ extern void ud_translate_intel(struct ud* u)
}
}
if ( u->pfx_seg &&
u->operand[0].type != UD_OP_MEM &&
u->operand[1].type != UD_OP_MEM ) {
mkasm(u, "%s ", ud_reg_tab[u->pfx_seg - UD_R_AL]);
}
if (u->pfx_lock)
mkasm(u, "lock ");
if (u->pfx_rep)
mkasm(u, "rep ");
if (u->pfx_repne)
mkasm(u, "repne ");
if (u->implicit_addr && u->pfx_seg)
mkasm(u, "%s ", ud_reg_tab[u->pfx_seg - UD_R_AL]);
/* print the instruction mnemonic */
mkasm(u, "%s ", ud_lookup_mnemonic(u->mnemonic));
/* operand 1 */
if (u->operand[0].type != UD_NONE) {
gen_operand(u, &u->operand[0], u->c1);
int cast = 0;
if ( u->operand[0].type == UD_OP_IMM &&
u->operand[1].type == UD_NONE )
cast = u->c1;
if ( u->operand[0].type == UD_OP_MEM ) {
cast = u->c1;
if ( u->operand[1].type == UD_OP_IMM ||
u->operand[1].type == UD_OP_CONST )
cast = 1;
if ( u->operand[1].type == UD_NONE )
cast = 1;
if ( ( u->operand[0].size != u->operand[1].size ) && u->operand[1].size )
cast = 1;
} else if ( u->operand[ 0 ].type == UD_OP_JIMM ) {
if ( u->operand[ 0 ].size > 8 ) cast = 1;
}
gen_operand(u, &u->operand[0], cast);
}
/* operand 2 */
if (u->operand[1].type != UD_NONE) {
int cast = 0;
mkasm(u, ", ");
gen_operand(u, &u->operand[1], u->c2);
if ( u->operand[1].type == UD_OP_MEM ) {
cast = u->c1;
if ( u->operand[0].type != UD_OP_REG )
cast = 1;
if ( u->operand[0].size != u->operand[1].size && u->operand[1].size )
cast = 1;
if ( u->operand[0].type == UD_OP_REG &&
u->operand[0].base >= UD_R_ES &&
u->operand[0].base <= UD_R_GS )
cast = 0;
}
gen_operand(u, &u->operand[1], cast );
}
/* operand 3 */

View File

@ -1,9 +1,27 @@
/* -----------------------------------------------------------------------------
* syn.c
/* udis86 - libudis86/syn.c
*
* Copyright (c) 2002, 2003, 2004 Vivek Mohan <vivek@sig9.com>
* All rights reserved. See (LICENSE)
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* -----------------------------------------------------------------------------

View File

@ -1,3 +1,28 @@
/* udis86 - libudis86/syn.h
*
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* -----------------------------------------------------------------------------
* syn.h
*
@ -8,9 +33,10 @@
#ifndef UD_SYN_H
#define UD_SYN_H
#include <stdio.h>
#include <stdarg.h>
#include "types.h"
#ifndef __UD_STANDALONE__
# include <stdarg.h>
#endif /* __UD_STANDALONE__ */
extern const char* ud_reg_tab[];

View File

@ -1,14 +1,49 @@
/* -----------------------------------------------------------------------------
* types.h
/* udis86 - libudis86/types.h
*
* Copyright (c) 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UD_TYPES_H
#define UD_TYPES_H
#include <stdio.h>
#ifdef __KERNEL__
/* -D__KERNEL__ is automatically passed on the command line when
building something as part of the Linux kernel */
# include <linux/kernel.h>
# include <linux/string.h>
# define __UD_STANDALONE__ 1
#endif /* __KERNEL__ */
#ifndef __UD_STANDALONE__
# include <stdio.h>
#endif /* __UD_STANDALONE__ */
/* gcc specific extensions */
#ifdef __GNUC__
# define UD_ATTR_PACKED __attribute__((packed))
#else
# define UD_ATTR_PACKED
#endif /* UD_ATTR_PACKED */
#ifdef _MSC_VER
# define FMT64 "%I64"
@ -22,11 +57,11 @@
typedef __int64 int64_t;
#else
# define FMT64 "%ll"
# include <inttypes.h>
# ifndef __UD_STANDALONE__
# include <inttypes.h>
# endif /* __UD_STANDALONE__ */
#endif
#include "itab.h"
/* -----------------------------------------------------------------------------
* All possible "types" of objects in udis86. Order is Important!
* -----------------------------------------------------------------------------
@ -97,6 +132,8 @@ enum ud_type
UD_OP_JIMM, UD_OP_CONST
};
#include "itab.h"
/* -----------------------------------------------------------------------------
* struct ud_operand - Disassembled instruction Operand.
* -----------------------------------------------------------------------------
@ -136,7 +173,9 @@ struct ud
int (*inp_hook) (struct ud*);
uint8_t inp_curr;
uint8_t inp_fill;
#ifndef __UD_STANDALONE__
FILE* inp_file;
#endif
uint8_t inp_ctr;
uint8_t* inp_buff;
uint8_t* inp_buff_end;
@ -173,7 +212,11 @@ struct ud
uint8_t c3;
uint8_t inp_cache[256];
uint8_t inp_sess[64];
uint8_t have_modrm;
uint8_t modrm;
void * user_opaque_data;
struct ud_itab_entry * itab_entry;
struct ud_lookup_table_list_entry *le;
};
/* -----------------------------------------------------------------------------
@ -192,6 +235,7 @@ typedef struct ud_operand ud_operand_t;
#define UD_INP_CACHE_SZ 32
#define UD_VENDOR_AMD 0
#define UD_VENDOR_INTEL 1
#define UD_VENDOR_ANY 2
#define bail_out(ud,error_code) longjmp( (ud)->bailout, error_code )
#define try_decode(ud) if ( setjmp( (ud)->bailout ) == 0 )

View File

@ -1,18 +1,37 @@
/* -----------------------------------------------------------------------------
* udis86.c
/* udis86 - libudis86/udis86.c
*
* Copyright (c) 2004, 2005, 2006, Vivek Mohan <vivek@sig9.com>
* All rights reserved. See LICENSE
* -----------------------------------------------------------------------------
* Copyright (c) 2002-2009 Vivek Thampi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "input.h"
#include "extern.h"
#ifndef __UD_STANDALONE__
# include <stdlib.h>
# include <string.h>
#endif /* __UD_STANDALONE__ */
/* =============================================================================
* ud_init() - Initializes ud_t object.
* =============================================================================
@ -77,6 +96,9 @@ ud_set_vendor(struct ud* u, unsigned v)
case UD_VENDOR_INTEL:
u->vendor = v;
break;
case UD_VENDOR_ANY:
u->vendor = v;
break;
default:
u->vendor = UD_VENDOR_AMD;
}

View File

@ -2,7 +2,7 @@ NAME=r_core
DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib
DEPS+=r_debug r_hash r_bin r_lang r_io r_anal r_parse r_print r_bp
DEPS+=r_reg r_search r_syscall r_sign r_diff r_vm
DEPS+=r_reg r_search r_syscall r_sign r_diff r_vm r_socket
OBJ=core.o cmd.o file.o config.o visual.o io.o yank.o libs.o anal.o project.o gdiff.o asm.o rtr.o

View File

@ -303,7 +303,7 @@ R_API char *r_socket_to_string(int fd) {
#endif
}
R_API int socket_udp_connect(const char *host, int port) {
R_API int r_socket_udp_connect(const char *host, int port) {
struct sockaddr_in sa;
struct hostent *he;
int s;