nios2 icon indicating copy to clipboard operation
nios2 copied to clipboard

IDA Pro 7.5 support added

Open blue-devil opened this issue 5 years ago • 0 comments

Porting for IDA 7.5

This awesome IDA Pro processor module for Altera Nios II Classic/Gen2 microprocessor architecture script doesn't work on IDA Pro 7.5. So here are my bug fixes. Please review.

Error 1

TypeError: unsupported operand type(s) for +: 'dict_values' and 'dict_values'

In Python2, dict.values() returns a list. But in Python3, it returns a special type dict_values! So we should explicitly convert them to lists.

Processor module script failed to initialize.
File: C:\Program Files\SCTTools\Disassembler\IDA 7.5\procs\nios2.py
unsupported operand type(s) for +: 'dict_values' and 'dict_values'
Traceback (most recent call last):
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idaapi.py", line 648, in IDAPython_LoadProcMod
    procobj = procmod.PROCESSOR_ENTRY()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1481, in PROCESSOR_ENTRY
    return nios2_processor_t()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1474, in __init__
    self.init_instructions()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1356, in init_instructions
    for x in self.itable_I_Type.values() + self.itable_R_Type.values() + self.itable_emulated.values() + self.itable_custom.values():
TypeError: unsupported operand type(s) for +: 'dict_values' and 'dict_values'

Solution:
Change this:

for x in self.itable_I_Type.values() + self.itable_R_Type.values() + self.itable_emulated.values() + self.itable_custom.values():

to this:

for x in list(self.itable_I_Type.values()) + list(self.itable_R_Type.values()) + list(self.itable_emulated.values()) + list(self.itable_custom.values()):

Error 2

NameError: name 'xrange' is not defined

In python2 we use xrange but in python3 it is deprecated. We should use range:

PROCESSOR_ENTRY: C:\Program Files\SCTTools\Disassembler\IDA 7.5\procs\nios2.py: name 'xrange' is not defined
Traceback (most recent call last):
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idaapi.py", line 648, in IDAPython_LoadProcMod
    procobj = procmod.PROCESSOR_ENTRY()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1481, in PROCESSOR_ENTRY
    return nios2_processor_t()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1475, in __init__
    self.init_registers()
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 1457, in init_registers
    for i in xrange(len(self.reg_names)):
NameError: name 'xrange' is not defined

Solution:
Change the two lines below:

Line 436: 		for i in xrange(1, 4):
Line 1457:		for i in xrange(len(self.reg_names)):

To these lines:

Line 436: 		for i in range(1, 4):
Line 1457:		for i in range(len(self.reg_names)):

Error 3

NameError: name 'isEnabled' is not defined

IDAPython sdk has been changed for 7.5. According to the ida support isEnabled idapython module changed to is_mapped

Exception in ida_idp.IDP_Hooks dispatcher function: SWIG director method error. Error detected when calling 'IDP_Hooks.ev_out_operand'
Traceback (most recent call last):
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4336, in ev_out_operand
    rc = self._get_notify("out_operand", mandatory_impl="ev_out_operand", imp_forced_val=1)(*args)
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4248, in f
    meth(*args)
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 389, in notify_out_operand
    if isEnabled(op.specval) == False:
NameError: name 'isEnabled' is not defined

Solution:
Change the lines below:

Line 398:				if isEnabled(op.specval) == False:
Line 407:				if isEnabled(op.specval) == False:
Line 664:				if isEnabled(op.specval):
Line 686:				if isEnabled(op.specval):

To these lines:

Line 398:				if is_mapped(op.specval) == False:
Line 407:				if is_mapped(op.specval) == False:
Line 664:				if is_mapped(op.specval):
Line 686:				if is_mapped(op.specval):

Error 4

NameError: name 'get_switch_info_ex' is not defined

IDAPython sdk has been changed for 7.5. According to the ida support get_switch_info_ex idapython module changed to get_switch_info

Exception in ida_idp.IDP_Hooks dispatcher function: SWIG director method error. Error detected when calling 'IDP_Hooks.ev_emu_insn'
Traceback (most recent call last):
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4311, in ev_emu_insn
    rc = self._get_notify("emu", mandatory_impl="ev_emu_insn")(*args)
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4254, in f
    val = meth(*args)
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 740, in notify_emu
    IsJmpReg = self.check_jmp_reg(insn)
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 495, in check_jmp_reg
    if get_switch_info_ex(insn.ea):
NameError: name 'get_switch_info_ex' is not defined

Solution:
Change these three lines below:

Line 375:			if ctx.insn.itype == self.itype_jmp and get_switch_info_ex(ctx.insn.ea) == None:
Line 495:		if get_switch_info_ex(insn.ea):
Line 556:		if get_switch_info_ex(insn.ea):

To these lines:

Line 375:			if ctx.insn.itype == self.itype_jmp and get_switch_info(ctx.insn.ea) == None:
Line 495:		if get_switch_info(insn.ea):
Line 556:		if get_switch_info(insn.ea):

Error 5

AttributeError: Property startEA has been replaced with start_ea

IDAPython sdk has been changed for 7.5. According to the ida support startEA idapython module changed to start_ea

Exception in ida_idp.IDP_Hooks dispatcher function: SWIG director method error. Error detected when calling 'IDP_Hooks.ev_emu_insn'
Traceback (most recent call last):
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4311, in ev_emu_insn
    rc = self._get_notify("emu", mandatory_impl="ev_emu_insn")(*args)
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idp.py", line 4254, in f
    val = meth(*args)
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 740, in notify_emu
    IsJmpReg = self.check_jmp_reg(insn)
  File "C:/Program Files/SCTTools/Disassembler/IDA 7.5/procs\nios2.py", line 504, in check_jmp_reg
    CurFuncStart = CurFunc.startEA
  File "C:\Program Files\SCTTools\Disassembler\IDA 7.5\python\3\ida_idaapi.py", line 938, in _raise
    raise AttributeError("Property %s has been replaced with %s" % (bad_attr, new_attr))
AttributeError: Property startEA has been replaced with start_ea

Solution:
Change the lines below:

Line 504:				CurFuncStart = CurFunc.startEA
Line 566:				CurFuncStart = CurFunc.startEA

To these lines:

Line 504:				CurFuncStart = CurFunc.start_ea
Line 566:				CurFuncStart = CurFunc.start_ea

Further work: I couldn't understand if should change the line below:

Line 638:				si.startea = prev.ea

Warning

###WARNING: the 'instruc' array size does not match 'instruc_end' value; adjusting it. I m not sure of this warning. Devs increase value of instruc_end by hand:

		# icode of the last instruction + 1
		self.instruc_end = len(Instructions) + 1

		# Array of instructions
		self.instruc = Instructions

Solution:
So IDA gives a warning about that two array sizes don't match. So i add an empty value to instruc variable:

		# icode of the last instruction + 1
		self.instruc_end = len(Instructions) + 1

		# Array of instructions
		self.instruc = Instructions
		empty = {'name': '', 'feature': 0, 'cmt': ''}
		self.instruc.append(empty)

Conclusion:

Great thanks to the developers for this nice NIOSII disassembly engine. It makes CTF solving faster 8D. I have fixed the bugs because IDA Pro has changed its Python version from 2 to 3 in IDA Pro 7.5.

blue-devil avatar Aug 12 '20 18:08 blue-devil