| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
from capstone import * |
| 6 |
from capstone.ppc import * |
| 7 |
|
| 8 |
PPC_CODE = b"\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14" |
| 9 |
PPC_CODE2 = b"\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f" |
| 10 |
PPC_CODE3 = b"\x10\x00\x1f\xec\xe0\x6d\x80\x04\xe4\x6d\x80\x04\x10\x60\x1c\x4c\x10\x60\x1c\x0c\xf0\x6d\x80\x04\xf4\x6d\x80\x04\x10\x60\x1c\x4e\x10\x60\x1c\x0e\x10\x60\x1a\x10\x10\x60\x1a\x11\x10\x63\x20\x2a\x10\x63\x20\x2b\x10\x83\x20\x40\x10\x83\x20\xC0\x10\x83\x20\x00\x10\x83\x20\x80\x10\x63\x20\x24\x10\x63\x20\x25\x10\x63\x29\x3a\x10\x63\x29\x3b\x10\x63\x29\x1c\x10\x63\x29\x1d\x10\x63\x29\x1e\x10\x63\x29\x1f\x10\x63\x24\x20\x10\x63\x24\x21\x10\x63\x24\x60\x10\x63\x24\x61\x10\x63\x24\xA0\x10\x63\x24\xA1\x10\x63\x24\xE0\x10\x63\x24\xE1\x10\x60\x20\x90\x10\x60\x20\x91\x10\x63\x29\x38\x10\x63\x29\x39\x10\x63\x01\x32\x10\x63\x01\x33\x10\x63\x01\x18\x10\x63\x01\x19\x10\x63\x01\x1A\x10\x63\x01\x1B\x10\x60\x19\x10\x10\x60\x19\x11\x10\x60\x18\x50\x10\x60\x18\x51\x10\x63\x29\x3e\x10\x63\x29\x3f\x10\x63\x29\x3c\x10\x63\x29\x3d\x10\x60\x18\x30\x10\x60\x18\x31\x10\x60\x18\x34\x10\x60\x18\x35\x10\x63\x29\x2e\x10\x63\x29\x2f\x10\x63\x20\x28\x10\x63\x20\x29\x10\x63\x29\x14\x10\x63\x29\x15\x10\x63\x29\x16\x10\x63\x29\x17" |
| 11 |
|
| 12 |
all_tests = ( |
| 13 |
(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, PPC_CODE, "PPC-64"), |
| 14 |
(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN + CS_MODE_QPX, PPC_CODE2, "PPC-64 + QPX"), |
| 15 |
(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN + CS_MODE_32 + CS_MODE_PS, PPC_CODE3, "PPC + PS"), |
| 16 |
) |
| 17 |
mask32 = lambda v: v & 0xFFFFFFFF |
| 18 |
|
| 19 |
def print_insn_detail(insn): |
| 20 |
|
| 21 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 22 |
|
| 23 |
|
| 24 |
if insn.id == 0: |
| 25 |
return |
| 26 |
|
| 27 |
if len(insn.operands) > 0: |
| 28 |
print("\top_count: %u" % len(insn.operands)) |
| 29 |
c = 0 |
| 30 |
for i in insn.operands: |
| 31 |
if i.type == PPC_OP_REG: |
| 32 |
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg))) |
| 33 |
if i.type == PPC_OP_IMM: |
| 34 |
print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask32(i.imm))) |
| 35 |
if i.type == PPC_OP_MEM: |
| 36 |
print("\t\toperands[%u].type: MEM" % c) |
| 37 |
if i.mem.base != 0: |
| 38 |
print("\t\t\toperands[%u].mem.base: REG = %s" \ |
| 39 |
% (c, insn.reg_name(i.mem.base))) |
| 40 |
if i.mem.disp != 0: |
| 41 |
print("\t\t\toperands[%u].mem.disp: 0x%x" \ |
| 42 |
% (c, mask32(i.mem.disp))) |
| 43 |
if i.type == PPC_OP_CRX: |
| 44 |
print("\t\toperands[%u].type: CRX" % c) |
| 45 |
print("\t\t\toperands[%u].crx.scale: = %u" \ |
| 46 |
% (c, i.crx.scale)) |
| 47 |
if i.crx.reg != 0: |
| 48 |
print("\t\t\toperands[%u].crx.reg: REG = %s" \ |
| 49 |
% (c, insn.reg_name(i.crx.reg))) |
| 50 |
if i.crx.cond != 0: |
| 51 |
print("\t\t\toperands[%u].crx.cond: 0x%x" \ |
| 52 |
% (c, i.crx.cond)) |
| 53 |
c += 1 |
| 54 |
|
| 55 |
if insn.bc: |
| 56 |
print("\tBranch code: %u" % insn.bc) |
| 57 |
if insn.bh: |
| 58 |
print("\tBranch hint: %u" % insn.bh) |
| 59 |
if insn.update_cr0: |
| 60 |
print("\tUpdate-CR0: True") |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def test_class(): |
| 65 |
|
| 66 |
for (arch, mode, code, comment) in all_tests: |
| 67 |
print("*" * 16) |
| 68 |
print("Platform: %s" % comment) |
| 69 |
print("Code: %s" % code.hex(' ')) |
| 70 |
print("Disasm:") |
| 71 |
|
| 72 |
try: |
| 73 |
md = Cs(arch, mode) |
| 74 |
md.detail = True |
| 75 |
for insn in md.disasm(code, 0x1000): |
| 76 |
print_insn_detail(insn) |
| 77 |
print () |
| 78 |
print("0x%x:\n" % (insn.address + insn.size)) |
| 79 |
except CsError as e: |
| 80 |
print("ERROR: %s" % e) |
| 81 |
|
| 82 |
|
| 83 |
if __name__ == '__main__': |
| 84 |
test_class() |