From 921a46c38fb4258f090929e8654d98e6c818f49e Mon Sep 17 00:00:00 2001 From: Marcin Bukat Date: Wed, 12 Nov 2014 11:06:34 +0100 Subject: [PATCH] mips: Fix j/jal target address calculation --- arch/Mips/MipsDisassembler.c | 8 ++++---- arch/Mips/MipsInstPrinter.c | 25 ------------------------- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/arch/Mips/MipsDisassembler.c b/arch/Mips/MipsDisassembler.c index 6ce6d5c7e..73017218e 100644 --- a/arch/Mips/MipsDisassembler.c +++ b/arch/Mips/MipsDisassembler.c @@ -1215,8 +1215,8 @@ static DecodeStatus DecodeCOP2RegisterClass(MCInst *Inst, static DecodeStatus DecodeBranchTarget(MCInst *Inst, unsigned Offset, uint64_t Address, MCRegisterInfo *Decoder) { - int32_t BranchOffset = (SignExtend32(Offset, 16) * 4) + 4; - MCOperand_CreateImm0(Inst, BranchOffset); + uint64_t TargetAddress = (SignExtend32(Offset, 16) * 4) + Address + 4; + MCOperand_CreateImm0(Inst, TargetAddress); return MCDisassembler_Success; } @@ -1224,8 +1224,8 @@ static DecodeStatus DecodeBranchTarget(MCInst *Inst, static DecodeStatus DecodeJumpTarget(MCInst *Inst, unsigned Insn, uint64_t Address, MCRegisterInfo *Decoder) { - unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2; - MCOperand_CreateImm0(Inst, JumpOffset); + uint64_t TargetAddress = (fieldFromInstruction(Insn, 0, 26) << 2) | ((Address + 4) & ~0x0FFFFFFF); + MCOperand_CreateImm0(Inst, TargetAddress); return MCDisassembler_Success; } diff --git a/arch/Mips/MipsInstPrinter.c b/arch/Mips/MipsInstPrinter.c index a9acc3761..7bd69910a 100644 --- a/arch/Mips/MipsInstPrinter.c +++ b/arch/Mips/MipsInstPrinter.c @@ -180,27 +180,6 @@ void Mips_printInst(MCInst *MI, SStream *O, void *info) } } -// check to see if @id is opcode of a relative branch instruction -static bool relativeBranch(unsigned int id) -{ - static unsigned int branchIns[] = { - Mips_BEQ, Mips_BC1F, Mips_BGEZ, Mips_BGEZAL, Mips_BGTZ, - Mips_BLEZ, Mips_BLTZ, Mips_BLTZAL, Mips_BNE, Mips_BC1T, - Mips_BEQL, Mips_BGEZALL, Mips_BGEZL, Mips_BGTZL, Mips_BLEZL, - Mips_BLTZALL, Mips_BLTZL, Mips_BNEL, Mips_BC1F, Mips_BC1FL, - Mips_BC1TL, Mips_BC0F, - }; - int i; - - for(i = 0; i < ARR_SIZE(branchIns); i++) { - if (id == branchIns[i]) - return true; - } - - // not found - return false; -} - static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) { MCOperand *Op = MCInst_getOperand(MI, OpNo); @@ -236,10 +215,6 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) if (MI->csh->detail) MI->flat_insn->detail->mips.operands[MI->flat_insn->detail->mips.op_count].mem.disp = imm; } else { - if (relativeBranch(MI->Opcode)) { - imm += MI->address; - } - if (imm >= 0) { if (imm > HEX_THRESHOLD) SStream_concat(O, "0x%"PRIx64, imm);