def EmitARM32Mul(code_buf): for ins in [ # "al" predicate is optional dis.InsFromSymbolized("mul", ["r0", "r1", "r0"]), dis.InsFromSymbolized("mov_regimm", ["pc", "lr", "lsl", "0"]) ]: code_buf.write(a32.Assemble(ins).to_bytes(4, "little"))
def disass(data): ins = a32.Disassemble(data) if ins.opcode is None: print(f"could not disassemble {data:x}") return enum_name, operands_str = symbolic.InsSymbolize(ins) print(f"{data:08x} {enum_name} {', '.join(operands_str)}") for f, o, o_str in zip(ins.opcode.fields, ins.operands, operands_str): print(f" {f.name:25} {o_str:15} ({o})") print() data2 = a32.Assemble(ins) assert data == data2 ins2 = symbolic.InsFromSymbolized(enum_name, operands_str) assert tuple(ins.operands) == tuple( ins2.operands), f"{ins.operands} vs {ins2.operands}"
def batch(): for line in sys.stdin: if not line or line.startswith("#"): continue data = int(line.split()[0], 16) ins = a32.Disassemble(data) if ins.opcode is None: print(f"could not disassemble {data:x}") continue enum_name, ops_str = symbolic.InsSymbolize(ins) print( f"{data:08x} {enum_name}{' ' if ops_str else ''}{', '.join(ops_str)}" ) data2 = a32.Assemble(ins) assert data == data2 ins2 = symbolic.InsFromSymbolized(enum_name, ops_str) assert tuple(ins.operands) == tuple( ins2.operands), f"{ins.operands} vs {ins2.operands}"
def HandleOneInstruction(count: int, line: str, data: int, actual_name: str, actual_ops: List): ins = a32.Disassemble(data) assert ins is not None, f"cannot disassemble [{count}]: {line}" assert ins.opcode is not None and ins.operands is not None, f"unknown opcode {line}" data2 = a32.Assemble(ins) assert data == data2, f"disass mismatch [{ins.opcode.NameForEnum()}] {data:x} vs {data2:x}" actual_name = FixupAliases(ins.opcode, actual_name, actual_ops) if not actual_name.startswith(ins.opcode.official_name): print("BAD NAME", ins.opcode.name, actual_name, line, end="") name, operands_str = symbolic.InsSymbolize(ins) if not OperandsMatch(ins.opcode, actual_name, actual_ops, operands_str): print(f"OPERANDS differ {operands_str} {actual_ops} in line {line}", end="") ins2 = symbolic.InsFromSymbolized(name, operands_str) assert tuple(ins.operands) == tuple(ins2.operands), f"{ins.operands} vs {ins2.operands}"
def EmitARM32Fib(code_buf): for ins in [ # e92d4030 stm sp!, {r4,r5,lr} dis.InsFromSymbolized("stmdb_update", ["al", "sp", "reglist:16432"]), dis.InsFromSymbolized("cmp_imm", ["al", "r0", "1"]), dis.InsFromSymbolized("b", ["le", "7"]), dis.InsFromSymbolized("mov_imm", ["al", "r4", "0"]), dis.InsFromSymbolized("mov_regimm", ["al", "r5", "r0", "lsl", "0"]), # dis.InsFromSymbolized("sub_imm", ["al", "r0", "r5", "1"]), dis.InsFromSymbolized("bl", ["al", "-8"]), dis.InsFromSymbolized("add_regimm", ["al", "r4", "r4", "r0", "lsl", "0"]), # # dis.InsFromSymbolized("sub_imm", ["al", "r0", "r5", "2"]), dis.InsFromSymbolized("bl", ["al", "-11"]), dis.InsFromSymbolized("add_regimm", ["al", "r0", "r4", "r0", "lsl", "0"]), # e8bd4030 ldm sp!, {r4,r5,pc} dis.InsFromSymbolized("ldmia_update", ["al", "reglist:32816", "sp"]), ]: code_buf.write(a32.Assemble(ins).to_bytes(4, "little"))
def HandleOpcode(mnemonic, token: List[str], unit: elf_unit.Unit): AddIns(unit, symbolic.InsFromSymbolized(mnemonic, token))