def get_microcode(func, maturity): """ Return the mba_t of the given function at the specified maturity. """ mbr = ida_hexrays.mba_ranges_t(func) hf = ida_hexrays.hexrays_failure_t() ml = ida_hexrays.mlist_t() ida_hexrays.mark_cfunc_dirty(func.start_ea) mba = ida_hexrays.gen_microcode(mbr, hf, ml, ida_hexrays.DECOMP_NO_WAIT, maturity) if not mba: print("0x%08X: %s" % (hf.errea, hf.desc())) return None return mba
def show_microcode(): """Generates and displays microcode for an address range. An address range can be a selection of code or that of the current function.""" sel, sea, eea = kw.read_range_selection(None) pfn = ida_funcs.get_func(kw.get_screen_ea()) if not sel and not pfn: return (False, "Position cursor within a function or select range") if not sel and pfn: sea = pfn.start_ea eea = pfn.end_ea addr_fmt = "%016x" if ida_ida.inf_is_64bit() else "%08x" fn_name = (ida_funcs.get_func_name(pfn.start_ea) if pfn else "0x%s-0x%s" % (addr_fmt % sea, addr_fmt % eea)) F = ida_bytes.get_flags(sea) if not ida_bytes.is_code(F): return (False, "The selected range must start with an instruction") text, mmat, mba_flags = ask_desired_maturity() if text is None and mmat is None: return (True, "Cancelled") if not sel and pfn: mbr = hr.mba_ranges_t(pfn) else: mbr = hr.mba_ranges_t() mbr.ranges.push_back(ida_range.range_t(sea, eea)) hf = hr.hexrays_failure_t() ml = hr.mlist_t() mba = hr.gen_microcode(mbr, hf, ml, hr.DECOMP_WARNINGS, mmat) if not mba: return (False, "0x%s: %s" % (addr_fmt % hf.errea, hf.desc())) vp = printer_t() mba.set_mba_flags(mba_flags) mba._print(vp) mcv = microcode_viewer_t() if not mcv.Create( mba, "0x%s-0x%s (%s)" % (addr_fmt % sea, addr_fmt % eea, text), text, fn_name, vp.get_mc()): return (False, "Error creating viewer") mcv.Show() return (True, "Successfully generated microcode for 0x%s..0x%s" % (addr_fmt % sea, addr_fmt % eea))
def scrape_unsupported_instructions(): """ Scrape all 'external' (unsupported) decompiler instructions from this IDB. Returns a tuple of two maps: ext2func = { opcode: set([func_ea, func2_ea, ...]) } func2ext = { func_ea: set([opcode1, opcode2, opcode3]) } """ miv = MinsnVisitor() ext2func = collections.defaultdict(set) func2ext = {} for address in idautils.Functions(): #address = 0x1800017E0 print("0x%08X: DECOMPILING" % address) func = ida_funcs.get_func(address) func_mbr = ida_hexrays.mba_ranges_t(func) hf = ida_hexrays.hexrays_failure_t() flags = ida_hexrays.DECOMP_NO_XREFS | ida_hexrays.DECOMP_NO_WAIT | ida_hexrays.DECOMP_WARNINGS mba = ida_hexrays.gen_microcode(func_mbr, hf, None, flags, ida_hexrays.MMAT_GENERATED) if not mba: print(" - 0x%08x: FAILED %s" % (hf.errea, hf.str)) continue miv.found = set() mba.for_all_insns(miv) # opcode --> [func_ea, func2_ea, ..] for ins_op in miv.found: ext2func[ins_op].add(address) # func_ea --> [ins_op, ins_op2, ..] func2ext[address] = miv.found print("\nDone scraping...\n") return (ext2func, func2ext)
if ida_hexrays.init_hexrays_plugin(): ea = ida_kernwin.get_screen_ea() pfn = ida_funcs.get_func(ea) w = ida_kernwin.warning if pfn: F = ida_bytes.get_flags(ea) if ida_bytes.is_code(F): gco = ida_hexrays.gco_info_t() if ida_hexrays.get_current_operand(gco): # generate microcode hf = ida_hexrays.hexrays_failure_t() mbr = ida_hexrays.mba_ranges_t(pfn) mba = ida_hexrays.gen_microcode(mbr, hf, None, ida_hexrays.DECOMP_WARNINGS, ida_hexrays.MMAT_PREOPTIMIZED) if mba: merr = mba.build_graph() if merr == ida_hexrays.MERR_OK: ncalls = mba.analyze_calls(ida_hexrays.ACFL_GUESS) if ncalls < 0: print( "%08x: failed to determine some calling conventions", pfn.start_ea) mlist = ida_hexrays.mlist_t() if gco.append_to_list(mlist, mba): ctx = ida_hexrays.op_parent_info_t() mop = mba.find_mop(ctx, ea, gco.is_def(), mlist) if mop: xrefs = ida_pro.eavec_t()
import ida_bytes import ida_range import ida_kernwin import ida_hexrays if ida_hexrays.init_hexrays_plugin(): sel, sea, eea = ida_kernwin.read_range_selection(None) w = ida_kernwin.warning if sel: F = ida_bytes.get_flags(sea) if ida_bytes.is_code(F): hf = ida_hexrays.hexrays_failure_t() mbr = ida_hexrays.mba_ranges_t() mbr.ranges.push_back(ida_range.range_t(sea, eea)) mba = ida_hexrays.gen_microcode(mbr, hf, None, ida_hexrays.DECOMP_WARNINGS) if mba: print("Successfully generated microcode for 0x%08x..0x%08x\n" % (sea, eea)) vp = ida_hexrays.vd_printer_t() mba._print(vp) else: w("0x%08x: %s" % (hf.errea, hf.str)) else: w("The selected range must start with an instruction") else: w("Please select a range of addresses to analyze") else: print('vds13: Hex-rays is not available.')