ida 手动补充不识别的指令

from ida_lines import COLOR_INSN, COLOR_MACRO 
from ida_idp import CUSTOM_INSN_ITYPE, IDP_Hooks, ph_get_regnames, ph_get_id, PLFM_XTENSA
from ida_bytes import get_bytes
from ida_idaapi import plugin_t, PLUGIN_PROC, PLUGIN_HIDE, PLUGIN_SKIP, PLUGIN_KEEP
from ida_ua import o_displ, o_reg, o_imm, dt_dword, OOF_ADDR
from struct import unpack

DEBUG_PLUGIN = True

NEWINSN_COLOR = COLOR_MACRO if DEBUG_PLUGIN else COLOR_INSN

class NewInstructionsx:
    (NN_quou,
    NN_muluh) = range(CUSTOM_INSN_ITYPE, CUSTOM_INSN_ITYPE+2)
    
    lst = {NN_quou:"quou",
           NN_muluh:"muluh"}
#--------------------------------------------------------------------------
class xtensa_idp_hook_t(IDP_Hooks):
    def __init__(self):
        IDP_Hooks.__init__(self)

    def decode_instruction(self, insn):
        buf = get_bytes(insn.ea, 4)
        
        if buf[2] == 0x5f and (buf[0] & 0xc0) == 0xc0:
            print("0x%08X bytes %02X %02X %02X" % (insn.ea , buf[2] , buf[1] , buf[0]))
            insn.itype = NewInstructionsx.NN_quou
            insn.size = 4
            insn.Op1.type = o_reg
            insn.Op1.reg = 1
            insn.Op2.type = o_reg
            insn.Op2.reg = 2 
            #insn.Op3.type = o_imm
            #insn.Op3.reg = 3
            return True
            
        if buf[2] == 0xA2 and (buf[0] & 0xF) == 0:
            insn.itype = NewInstructionsx.NN_muluh
            insn.size = 3
            insn.Op1.type = o_reg
            insn.Op1.reg = buf[1] >> 4
            insn.Op2.type = o_reg
            insn.Op2.reg = buf[1] & 0xF 
            insn.Op3.type = o_reg
            insn.Op3.reg = buf[0] >> 4
            return True
             
        return False

    def ev_ana_insn(self, insn):
        return self.decode_instruction(insn)

    def ev_out_mnem(self, outctx):
        insntype = outctx.insn.itype
        global NEWINSN_COLOR

        if (insntype >= CUSTOM_INSN_ITYPE) and (insntype in NewInstructionsx.lst):
            mnem = NewInstructionsx.lst[insntype]
            outctx.out_tagon(NEWINSN_COLOR)
            outctx.out_line(mnem)
            outctx.out_tagoff(NEWINSN_COLOR)
            print("mnem = %s" % mnem)

            # TODO: how can MNEM_width be determined programmatically?
            MNEM_WIDTH = 12
            width = max(1, MNEM_WIDTH - len(mnem))
            outctx.out_line(' ' * width)

            return True
        return False


    def ev_out_operand(self, outctx, op):
        insn = outctx.insn
        
        
        #print("CUSTOM_INSN_ITYPE = %x" % CUSTOM_INSN_ITYPE) 
        #print("NewInstructionsx.NN_quou = %x" % NewInstructionsx.NN_quou) 
        #print("NewInstructionsx.NN_muluh = %x" % NewInstructionsx.NN_muluh) 
        #print("NewInstructionsx.NN_ld_hu = %x" % NewInstructionsx.NN_ld_hu) 
        
        #print("NewInstructionsx.NN_ld_hu = ", NewInstructionsx.NN_ld_hu) 
        #print("NewInstructions.NN_st_h = %X" % NewInstructionsx.NN_st_h) 
        if insn.itype in [NewInstructionsx.NN_quou, NewInstructionsx.NN_muluh]:
            #print("insn.itype = %X" % insn.itype)  
            #print("op.type = %X" % op.type)  
            
            if op.type == o_displ:
                print("o_displ = %X" % o_displ)  
                outctx.out_value(op, OOF_ADDR)
                outctx.out_register(ph_get_regnames()[op.reg])
                return True
        return False
#--------------------------------------------------------------------------
class XtensaESP(plugin_t):
    flags = PLUGIN_PROC | PLUGIN_HIDE
    comment = ""
    wanted_hotkey = ""
    help = "Adds support for additional Xtensa instructions"
    wanted_name = "XtensaESP"

    def __init__(self):
        self.prochook = None
        print ("%s initialized3." % XtensaESP.wanted_name)

    def init(self):
        #if ph_get_id() != PLFM_XTENSA:
        if ph_get_id() != 13:
            print ("%s initialized1." % XtensaESP.wanted_name)
            print ("PLFM_XTENSA = %d." % PLFM_XTENSA)
            print ("ph_get_id() = %d." % ph_get_id())
            return PLUGIN_SKIP

        self.prochook = xtensa_idp_hook_t()
        self.prochook.hook()
        print ("%s initialized2." % XtensaESP.wanted_name)
        return PLUGIN_KEEP

    def run(self, arg):
        pass

    def term(self):
        if self.prochook:
            self.prochook.unhook()
#--------------------------------------------------------------------------
def PLUGIN_ENTRY():
    return XtensaESP()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值