From 514dd145dcf9a26cf8210304644572f18874643e Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 22 Nov 2012 18:06:02 +0100 Subject: [PATCH] Use anal.x86.udis86 when asm.bits=16 from anal.x86 Fixes in the anal.x86.udis86 analysis Add pcj command to show bytes of current block in json --- libr/anal/p/anal_x86.c | 5 +++++ libr/anal/p/anal_x86_udis86.c | 36 ++++++++++++++++++++++++++++------- libr/print/print.c | 11 ++++++++++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/libr/anal/p/anal_x86.c b/libr/anal/p/anal_x86.c index 551ea92589b8f..255edcdcf9a71 100644 --- a/libr/anal/p/anal_x86.c +++ b/libr/anal/p/anal_x86.c @@ -798,6 +798,7 @@ static void anal_int(RAnal *anal, RAnalOp *op, x86im_instr_object io) { } } +extern int x86_udis86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len); static int x86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) { x86im_instr_object io; st64 imm; @@ -807,6 +808,10 @@ static int x86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) if (data == NULL) return 0; + if (anal->bits == 16) { + return x86_udis86_op (anal, op, addr, data, len); + } + memset (op, '\0', sizeof (RAnalOp)); op->type = R_ANAL_OP_TYPE_UNK; op->addr = addr; diff --git a/libr/anal/p/anal_x86_udis86.c b/libr/anal/p/anal_x86_udis86.c index e9ed626cdf866..2ca771db55ea6 100644 --- a/libr/anal/p/anal_x86_udis86.c +++ b/libr/anal/p/anal_x86_udis86.c @@ -8,16 +8,18 @@ #include "udis86/types.h" #include "udis86/extern.h" -static ut64 getval(int bits, ud_operand_t *op) { +static st64 getval(ud_operand_t *op) { + int bits = op->size; switch (bits) { - case 8: return op->lval.sbyte; - case 16: return op->lval.uword; + case 8: return (char)op->lval.sbyte; + case 16: return (short) op->lval.uword; case 32: return op->lval.udword; case 64: return op->lval.uqword; } return 0LL; } -static int x86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) { + +int x86_udis86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) { int oplen; struct ud u; ud_init (&u); @@ -34,11 +36,31 @@ static int x86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) switch (u.mnemonic) { case UD_Ijmp: op->type = R_ANAL_OP_TYPE_JMP; - op->jump = oplen + getval (anal->bits, &u.operand[0]); + op->jump = addr + oplen + getval (&u.operand[0]); + break; + case UD_Ijz: + case UD_Ijnz: + case UD_Ijb: + case UD_Ijbe: + case UD_Ija: + case UD_Ijs: + case UD_Ijns: + case UD_Ijo: + case UD_Ijno: + case UD_Ijp: + case UD_Ijnp: + case UD_Ijl: + case UD_Ijge: + case UD_Ijle: + case UD_Ijg: + case UD_Ijcxz: + op->type = R_ANAL_OP_TYPE_CJMP; + op->jump = addr + oplen + getval (&u.operand[0]); + op->fail = addr+oplen; break; case UD_Icall: op->type = R_ANAL_OP_TYPE_CALL; - op->jump = oplen + getval (anal->bits, &u.operand[0]); + op->jump = oplen + getval (&u.operand[0]); op->fail = addr+oplen; break; case UD_Iret: @@ -216,7 +238,7 @@ struct r_anal_plugin_t r_anal_plugin_x86_udis86 = { .bits = 16|32|64, .init = NULL, .fini = NULL, - .op = &x86_op, + .op = &x86_udis86_op, .set_reg_profile = &set_reg_profile, .fingerprint_bb = NULL, .fingerprint_fcn = NULL, diff --git a/libr/print/print.c b/libr/print/print.c index f85519807d2ba..1b047c95adeca 100644 --- a/libr/print/print.c +++ b/libr/print/print.c @@ -162,7 +162,16 @@ R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang) { int i, w = p->cols*0.7; switch (lang) { case '?': - eprintf ("Valid print code formats are: C and Python\n"); + eprintf ("Valid print code formats are: JSON, C and Python (pcj, pc, pcp) \n"); + break; + case 'j': + p->printf ("["); + for (i=0; !p->interrupt && iprintf ("0x%02x%s", buf[i], (i+1printf ("]\n"); break; case 'P': case 'p':