def get_swval(start_ea): """ea_t -> (str, op_ty, int)""" # XXX imperative ida.get_switch_info(start_ea) # confirm this insn has switch info distance = 0 ea = ida.prev_head(start_ea) while True: if distance > 10: raise SwitchTroubleError( "gave up looking for switch value between %s..%s" % (ida.atoa(ea), ida.atoa(start_ea)) ) if ida.is_switch_insn(ea): mnem = ida.get_mnem(ea) # dunno if there are other common switch idioms if mnem == "sltiu": return (mnem, ida.get_op(ea, 1), 1) elif list(ida.code_refs_from(ea, 0)) != []: raise SwitchTroubleError( "encountered branch/jump while looking for switch value " + "between %s..%s" % (ida.atoa(ea), ida.atoa(start_ea)) ) ea = ida.prev_head(ea) distance += 1
def get_swval(start_ea): '''ea_t -> (str, op_ty, int)''' # XXX imperative ida.get_switch_info(start_ea) # confirm this insn has switch info distance = 0 ea = ida.prev_head(start_ea) while True: if distance > 10: raise SwitchTroubleError( 'gave up looking for switch value between %s..%s' % (ida.atoa(ea), ida.atoa(start_ea))) if ida.is_switch_insn(ea): mnem = ida.get_mnem(ea) # dunno if there are other common switch idioms if mnem == 'sltiu': return (mnem, ida.get_op(ea, 1), 1) elif list(ida.code_refs_from(ea, 0)) != []: raise SwitchTroubleError( 'encountered branch/jump while looking for switch value ' + 'between %s..%s' % (ida.atoa(ea), ida.atoa(start_ea))) ea = ida.prev_head(ea) distance += 1
def get_arg_for_va_function(callee, start_ea): '''str -> ea_t -> str''' # XXX hacky; not a very general function # XXX imperative # get a relevant item needed for processing a variadic function sw = { 'printf' : regs.gpr(abi.arg_regs[0]), 'scanf' : regs.gpr(abi.arg_regs[0]), 'sscanf' : regs.gpr(abi.arg_regs[1]) } try: wanted_reg = sw[callee] except KeyError: raise utils.BugError('unrecognized callee %s' % callee) distance = 0 fn = ida.get_func(start_ea) # first, look at the delay slot ea = ida.next_head(start_ea, fn.endEA) while True: if distance > 10: raise VarargsError( 'gave up looking for needed varargs argument for %s between ' + '%s..%s' % (ida.atoa(ea), ida.atoa(start_ea))) if ea == start_ea: ea = ida.prev_head(ea) continue # skip the call insn elif list(ida.code_refs_from(ea, 0)) != []: raise VarargsError( 'encountered branch/jump while looking for varargs argument ' + 'between %s..%s' % (ida.atoa(ea), ida.atoa(start_ea))) rd = ida.get_op(ea, 0) if rd.val == wanted_reg: opvals = ida.get_opvals(ea) # XXX should try to track stkvar values s = ida.get_string(opvals[-1].target) if s is not None: return s ea = ida.prev_head(ea) distance += 1
def get_arg_for_va_function(callee, start_ea): """str -> ea_t -> str""" # XXX hacky; not a very general function # XXX imperative # get a relevant item needed for processing a variadic function sw = {"printf": regs.gpr(abi.arg_regs[0]), "scanf": regs.gpr(abi.arg_regs[0]), "sscanf": regs.gpr(abi.arg_regs[1])} try: wanted_reg = sw[callee] except KeyError: raise utils.BugError("unrecognized callee %s" % callee) distance = 0 fn = ida.get_func(start_ea) # first, look at the delay slot ea = ida.next_head(start_ea, fn.endEA) while True: if distance > 10: raise VarargsError( "gave up looking for needed varargs argument for %s between " + "%s..%s" % (ida.atoa(ea), ida.atoa(start_ea)) ) if ea == start_ea: ea = ida.prev_head(ea) continue # skip the call insn elif list(ida.code_refs_from(ea, 0)) != []: raise VarargsError( "encountered branch/jump while looking for varargs argument " + "between %s..%s" % (ida.atoa(ea), ida.atoa(start_ea)) ) rd = ida.get_op(ea, 0) if rd.val == wanted_reg: opvals = ida.get_opvals(ea) # XXX should try to track stkvar values s = ida.get_string(opvals[-1].target) if s is not None: return s ea = ida.prev_head(ea) distance += 1