diff options
Diffstat (limited to 'miasm2/arch/arm')
| -rw-r--r-- | miasm2/arch/arm/arch.py | 106 | ||||
| -rw-r--r-- | miasm2/arch/arm/disasm.py | 3 | ||||
| -rw-r--r-- | miasm2/arch/arm/jit.py | 6 | ||||
| -rw-r--r-- | miasm2/arch/arm/sem.py | 90 |
4 files changed, 107 insertions, 98 deletions
diff --git a/miasm2/arch/arm/arch.py b/miasm2/arch/arm/arch.py index 35574a84..17b57ba4 100644 --- a/miasm2/arch/arm/arch.py +++ b/miasm2/arch/arm/arch.py @@ -343,62 +343,67 @@ class instruction_arm(instruction): super(instruction_arm, self).__init__(*args, **kargs) @staticmethod - def arg2str(e, pos = None): + def arg2str(expr, index=None, symbol_pool=None): wb = False - if isinstance(e, ExprId) or isinstance(e, ExprInt): - return str(e) - if isinstance(e, ExprOp) and e.op in expr2shift_dct: - if len(e.args) == 1: - return '%s %s' % (e.args[0], expr2shift_dct[e.op]) - elif len(e.args) == 2: - return '%s %s %s' % (e.args[0], expr2shift_dct[e.op], e.args[1]) + if expr.is_id() or expr.is_int(): + return str(expr) + elif expr.is_loc(): + if symbol_pool is not None: + return symbol_pool.str_loc_key(expr.loc_key) + else: + return str(expr) + if isinstance(expr, ExprOp) and expr.op in expr2shift_dct: + if len(expr.args) == 1: + return '%s %s' % (expr.args[0], expr2shift_dct[expr.op]) + elif len(expr.args) == 2: + return '%s %s %s' % (expr.args[0], expr2shift_dct[expr.op], expr.args[1]) else: raise NotImplementedError('zarb arg2str') sb = False - if isinstance(e, ExprOp) and e.op == "sbit": + if isinstance(expr, ExprOp) and expr.op == "sbit": sb = True - e = e.args[0] - if isinstance(e, ExprOp) and e.op == "reglist": - o = [gpregs.expr.index(x) for x in e.args] + expr = expr.args[0] + if isinstance(expr, ExprOp) and expr.op == "reglist": + o = [gpregs.expr.index(x) for x in expr.args] out = reglist2str(o) if sb: out += "^" return out - if isinstance(e, ExprOp) and e.op == 'wback': + if isinstance(expr, ExprOp) and expr.op == 'wback': wb = True - e = e.args[0] - if isinstance(e, ExprId): - out = str(e) + expr = expr.args[0] + if isinstance(expr, ExprId): + out = str(expr) if wb: out += "!" return out - if not isinstance(e, ExprMem): - return str(e) + if not isinstance(expr, ExprMem): + return str(expr) - e = e.arg - if isinstance(e, ExprOp) and e.op == 'wback': + expr = expr.arg + if isinstance(expr, ExprOp) and expr.op == 'wback': wb = True - e = e.args[0] + expr = expr.args[0] - if isinstance(e, ExprId): - r, s = e, None - elif len(e.args) == 1 and isinstance(e.args[0], ExprId): - r, s = e.args[0], None - elif isinstance(e.args[0], ExprId): - r, s = e.args[0], e.args[1] + if isinstance(expr, ExprId): + r, s = expr, None + elif len(expr.args) == 1 and isinstance(expr.args[0], ExprId): + r, s = expr.args[0], None + elif isinstance(expr.args[0], ExprId): + r, s = expr.args[0], expr.args[1] else: - r, s = e.args[0].args + r, s = expr.args[0].args if isinstance(s, ExprOp) and s.op in expr2shift_dct: s = ' '.join([str(x) for x in s.args[0], expr2shift_dct[s.op], s.args[1]]) - if isinstance(e, ExprOp) and e.op == 'postinc': + if isinstance(expr, ExprOp) and expr.op == 'postinc': o = '[%s]' % r if s and not (isinstance(s, ExprInt) and s.arg == 0): o += ', %s' % s @@ -418,16 +423,15 @@ class instruction_arm(instruction): return self.name in conditional_branch + unconditional_branch def dstflow2label(self, symbol_pool): - e = self.args[0] - if not isinstance(e, ExprInt): + expr = self.args[0] + if not isinstance(expr, ExprInt): return if self.name == 'BLX': - ad = e.arg + self.offset + addr = expr.arg + self.offset else: - ad = e.arg + self.offset - l = symbol_pool.getby_offset_create(ad) - s = ExprId(l, e.size) - self.args[0] = s + addr = expr.arg + self.offset + loc_key = symbol_pool.getby_offset_create(addr) + self.args[0] = ExprLoc(loc_key, expr.size) def breakflow(self): if self.name in conditional_branch + unconditional_branch: @@ -492,27 +496,29 @@ class instruction_armt(instruction_arm): def dstflow2label(self, symbol_pool): if self.name in ["CBZ", "CBNZ"]: - e = self.args[1] + expr = self.args[1] else: - e = self.args[0] - if not isinstance(e, ExprInt): + expr = self.args[0] + if not isinstance(expr, ExprInt): return if self.name == 'BLX': - ad = e.arg + (self.offset & 0xfffffffc) + addr = expr.arg + (self.offset & 0xfffffffc) elif self.name == 'BL': - ad = e.arg + self.offset + addr = expr.arg + self.offset elif self.name.startswith('BP'): - ad = e.arg + self.offset + addr = expr.arg + self.offset elif self.name.startswith('CB'): - ad = e.arg + self.offset + self.l + 2 + addr = expr.arg + self.offset + self.l + 2 else: - ad = e.arg + self.offset - l = symbol_pool.getby_offset_create(ad) - s = ExprId(l, e.size) + addr = expr.arg + self.offset + + loc_key = symbol_pool.getby_offset_create(addr) + dst = ExprLoc(loc_key, expr.size) + if self.name in ["CBZ", "CBNZ"]: - self.args[1] = s + self.args[1] = dst else: - self.args[0] = s + self.args[0] = dst def breakflow(self): if self.name in conditional_branch + unconditional_branch +["CBZ", "CBNZ", 'TBB', 'TBH']: @@ -774,8 +780,8 @@ class arm_arg(m_arg): return arg.name if arg.name in gpregs.str: return None - label = symbol_pool.getby_name_create(arg.name) - return ExprId(label, 32) + loc_key = symbol_pool.getby_name_create(arg.name) + return ExprLoc(loc_key, 32) if isinstance(arg, AstOp): args = [self.asm_ast_to_expr(tmp, symbol_pool) for tmp in arg.args] if None in args: diff --git a/miasm2/arch/arm/disasm.py b/miasm2/arch/arm/disasm.py index 586fa903..956a894b 100644 --- a/miasm2/arch/arm/disasm.py +++ b/miasm2/arch/arm/disasm.py @@ -24,7 +24,8 @@ def cb_arm_fix_call(mn, cur_bloc, symbol_pool, offsets_to_dis, *args, **kwargs): return if not l2.args[1] in values: return - cur_bloc.add_cst(l1.offset + 4, AsmConstraint.c_next, symbol_pool) + loc_key_cst = self.symbol_pool.getby_offset_create(l1.offset + 4) + cur_bloc.add_cst(loc_key_cst, AsmConstraint.c_next, symbol_pool) offsets_to_dis.add(l1.offset + 4) cb_arm_funcs = [cb_arm_fix_call] diff --git a/miasm2/arch/arm/jit.py b/miasm2/arch/arm/jit.py index 1a37b7f1..b92e2c32 100644 --- a/miasm2/arch/arm/jit.py +++ b/miasm2/arch/arm/jit.py @@ -7,6 +7,7 @@ from miasm2.arch.arm.sem import ir_armb, ir_arml, ir_armtl, ir_armtb, cond_dct_i from miasm2.jitter.codegen import CGen from miasm2.expression.expression import ExprId, ExprAff, ExprCond from miasm2.ir.ir import IRBlock, AssignBlock +from miasm2.ir.translators.C import TranslatorC log = logging.getLogger('jit_arm') hnd = logging.StreamHandler() @@ -17,11 +18,6 @@ log.setLevel(logging.CRITICAL) class arm_CGen(CGen): - def __init__(self, ir_arch): - self.ir_arch = ir_arch - self.PC = self.ir_arch.arch.regs.PC - self.init_arch_C() - def block2assignblks(self, block): """ diff --git a/miasm2/arch/arm/sem.py b/miasm2/arch/arm/sem.py index 2cf2e5a4..c80e9826 100644 --- a/miasm2/arch/arm/sem.py +++ b/miasm2/arch/arm/sem.py @@ -441,16 +441,16 @@ def sdiv(ir, instr, a, b, c=None): if c is None: b, c = a, b - lbl_div = ExprId(ir.gen_label(), ir.IRDst.size) - lbl_except = ExprId(ir.gen_label(), ir.IRDst.size) - lbl_next = ExprId(ir.get_next_label(instr), ir.IRDst.size) + loc_div = ExprLoc(ir.symbol_pool.gen_loc_key(), ir.IRDst.size) + loc_except = ExprId(ir.symbol_pool.gen_loc_key(), ir.IRDst.size) + loc_next = ExprLoc(ir.get_next_loc_key(instr), ir.IRDst.size) - e.append(ExprAff(ir.IRDst, ExprCond(c, lbl_div, lbl_except))) + e.append(ExprAff(ir.IRDst, ExprCond(c, loc_div, loc_except))) do_except = [] do_except.append(ExprAff(exception_flags, ExprInt(EXCEPT_DIV_BY_ZERO, exception_flags.size))) - do_except.append(ExprAff(ir.IRDst, lbl_next)) - blk_except = IRBlock(lbl_except.name, [AssignBlock(do_except, instr)]) + do_except.append(ExprAff(ir.IRDst, loc_next)) + blk_except = IRBlock(loc_except.loc_key, [AssignBlock(do_except, instr)]) @@ -461,8 +461,8 @@ def sdiv(ir, instr, a, b, c=None): if dst is not None: do_div.append(ExprAff(ir.IRDst, r)) - do_div.append(ExprAff(ir.IRDst, lbl_next)) - blk_div = IRBlock(lbl_div.name, [AssignBlock(do_div, instr)]) + do_div.append(ExprAff(ir.IRDst, loc_next)) + blk_div = IRBlock(loc_div.loc_key, [AssignBlock(do_div, instr)]) return e, [blk_div, blk_except] @@ -474,16 +474,16 @@ def udiv(ir, instr, a, b, c=None): - lbl_div = ExprId(ir.gen_label(), ir.IRDst.size) - lbl_except = ExprId(ir.gen_label(), ir.IRDst.size) - lbl_next = ExprId(ir.get_next_label(instr), ir.IRDst.size) + loc_div = ExprLoc(ir.symbol_pool.gen_loc_key(), ir.IRDst.size) + loc_except = ExprLoc(ir.symbol_pool.gen_loc_key(), ir.IRDst.size) + loc_next = ExprLoc(ir.get_next_loc_key(instr), ir.IRDst.size) - e.append(ExprAff(ir.IRDst, ExprCond(c, lbl_div, lbl_except))) + e.append(ExprAff(ir.IRDst, ExprCond(c, loc_div, loc_except))) do_except = [] do_except.append(ExprAff(exception_flags, ExprInt(EXCEPT_DIV_BY_ZERO, exception_flags.size))) - do_except.append(ExprAff(ir.IRDst, lbl_next)) - blk_except = IRBlock(lbl_except.name, [AssignBlock(do_except, instr)]) + do_except.append(ExprAff(ir.IRDst, loc_next)) + blk_except = IRBlock(loc_except.loc_key, [AssignBlock(do_except, instr)]) r = ExprOp("udiv", b, c) @@ -493,8 +493,8 @@ def udiv(ir, instr, a, b, c=None): if dst is not None: do_div.append(ExprAff(ir.IRDst, r)) - do_div.append(ExprAff(ir.IRDst, lbl_next)) - blk_div = IRBlock(lbl_div.name, [AssignBlock(do_div, instr)]) + do_div.append(ExprAff(ir.IRDst, loc_next)) + blk_div = IRBlock(loc_div.loc_key, [AssignBlock(do_div, instr)]) return e, [blk_div, blk_except] @@ -932,19 +932,20 @@ def pop(ir, instr, a): def cbz(ir, instr, a, b): e = [] - lbl_next = ExprId(ir.get_next_label(instr), 32) - e.append(ExprAff(ir.IRDst, ExprCond(a, lbl_next, b))) + loc_next = ir.get_next_loc_key(instr) + loc_next_expr = ExprLoc(loc_next, 32) + e.append(ExprAff(ir.IRDst, ExprCond(a, loc_next_expr, b))) return e, [] def cbnz(ir, instr, a, b): e = [] - lbl_next = ExprId(ir.get_next_label(instr), 32) - e.append(ExprAff(ir.IRDst, ExprCond(a, b, lbl_next))) + loc_next = ir.get_next_loc_key(instr) + loc_next_expr = ExprLoc(loc_next, 32) + e.append(ir.IRDst, ExprCond(a, b, loc_next_expr)) return e, [] - def uxtb(ir, instr, a, b): e = [] r = b[:8].zeroExtend(32) @@ -1264,10 +1265,14 @@ def add_condition_expr(ir, instr, cond, instr_ir, extra_ir): raise ValueError('unknown condition %r' % cond) cond = tab_cond[cond] - lbl_next = ExprId(ir.get_next_label(instr), 32) - lbl_do = ExprId(ir.gen_label(), 32) - dst_cond = ExprCond(cond, lbl_do, lbl_next) + + loc_next = ir.get_next_loc_key(instr) + loc_next_expr = ExprLoc(loc_next, 32) + loc_do = ir.symbol_pool.gen_loc_key() + loc_do_expr = ExprLoc(loc_do, 32) + + dst_cond = ExprCond(cond, loc_do_expr, loc_next_expr) assert(isinstance(instr_ir, list)) has_irdst = False @@ -1276,8 +1281,8 @@ def add_condition_expr(ir, instr, cond, instr_ir, extra_ir): has_irdst = True break if not has_irdst: - instr_ir.append(ExprAff(ir.IRDst, lbl_next)) - e_do = IRBlock(lbl_do.name, [AssignBlock(instr_ir, instr)]) + instr_ir.append(ExprAff(ir.IRDst, loc_next_expr)) + e_do = IRBlock(loc_do, [AssignBlock(instr_ir, instr)]) e = [ExprAff(ir.IRDst, dst_cond)] return e, [e_do] + extra_ir @@ -1527,7 +1532,7 @@ class ir_arml(IntermediateRepresentation): raise ValueError("IT name invalid %s" % instr) return out, instr.args[0] - def do_it_block(self, label, index, block, assignments, gen_pc_updt): + def do_it_block(self, loc, index, block, assignments, gen_pc_updt): instr = block.lines[index] it_hints, it_cond = self.parse_itt(instr) cond_num = cond_dct_inv[it_cond.name] @@ -1539,14 +1544,14 @@ class ir_arml(IntermediateRepresentation): ir_blocks_all = [] # Gen dummy irblock for IT instr - label_next = self.get_next_label(instr) - dst = ExprAff(self.IRDst, ExprId(label_next, 32)) + loc_next = self.get_next_loc_key(instr) + dst = ExprAff(self.IRDst, ExprId(loc_next, 32)) dst_blk = AssignBlock([dst], instr) assignments.append(dst_blk) - irblock = IRBlock(label, assignments) + irblock = IRBlock(loc, assignments) ir_blocks_all.append([irblock]) - label = label_next + loc = loc_next assignments = [] for hint in it_hints: irblocks = [] @@ -1554,33 +1559,33 @@ class ir_arml(IntermediateRepresentation): instr = block.lines[index] # Add conditionnal jump to current irblock - label_do = self.symbol_pool.gen_label() - label_next = self.get_next_label(instr) + loc_do = self.symbol_pool.gen_loc_key() + loc_next = self.get_next_loc_key(instr) if hint: local_cond = ~cond_eq else: local_cond = cond_eq - dst = ExprAff(self.IRDst, ExprCond(local_cond, ExprId(label_do, 32), ExprId(label_next, 32))) + dst = ExprAff(self.IRDst, ExprCond(local_cond, ExprLoc(loc_do, 32), ExprLoc(loc_next, 32))) dst_blk = AssignBlock([dst], instr) assignments.append(dst_blk) - irblock = IRBlock(label, assignments) + irblock = IRBlock(loc, assignments) irblocks.append(irblock) assignments = [] - label = label_do + loc = loc_do split = self.add_instr_to_irblock(block, instr, assignments, irblocks, gen_pc_updt) if split: raise NotImplementedError("Unsupported instr in IT block (%s)" % instr) - dst = ExprAff(self.IRDst, ExprId(label_next, 32)) + dst = ExprAff(self.IRDst, ExprId(loc_next, 32)) dst_blk = AssignBlock([dst], instr) assignments.append(dst_blk) - irblock = IRBlock(label, assignments) + irblock = IRBlock(loc, assignments) irblocks.append(irblock) - label = label_next + loc = loc_next assignments = [] ir_blocks_all.append(irblocks) return index, ir_blocks_all @@ -1594,7 +1599,8 @@ class ir_arml(IntermediateRepresentation): it_hints = None it_cond = None - label = None + label = block.loc_key + assignments = [] ir_blocks_all = [] index = -1 while index + 1 < len(block.lines): @@ -1602,7 +1608,7 @@ class ir_arml(IntermediateRepresentation): instr = block.lines[index] if label is None: assignments = [] - label = self.get_instr_label(instr) + label = self.get_loc_key_for_instr(instr) if instr.name.startswith("IT"): index, irblocks_it = self.do_it_block(label, index, block, assignments, gen_pc_updt) for irblocks in irblocks_it: @@ -1621,7 +1627,7 @@ class ir_arml(IntermediateRepresentation): new_ir_blocks_all = self.post_add_block(block, ir_blocks_all) for irblock in new_ir_blocks_all: - self.blocks[irblock.label] = irblock + self.blocks[irblock.loc_key] = irblock return new_ir_blocks_all |