Skip to content

Commit 23e9796

Browse files
committed
Update python bindings with changes to PPC and cs_insn structs.
1 parent 243823d commit 23e9796

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

bindings/python/capstone/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@
334334
CS_AC_INVALID = 0 # Invalid/unitialized access type.
335335
CS_AC_READ = (1 << 0) # Operand that is read from.
336336
CS_AC_WRITE = (1 << 1) # Operand that is written to.
337+
CS_AC_READ_WRITE = (2)
337338

338339
# Capstone syntax value
339340
CS_OPT_SYNTAX_DEFAULT = 1 << 1 # Default assembly syntax of all platforms (CS_OPT_SYNTAX)
@@ -479,11 +480,14 @@ class _cs_detail(ctypes.Structure):
479480
class _cs_insn(ctypes.Structure):
480481
_fields_ = (
481482
('id', ctypes.c_uint),
483+
('alias_id', ctypes.c_uint64),
482484
('address', ctypes.c_uint64),
483485
('size', ctypes.c_uint16),
484486
('bytes', ctypes.c_ubyte * 24),
485487
('mnemonic', ctypes.c_char * 32),
486488
('op_str', ctypes.c_char * 160),
489+
('is_alias', ctypes.c_bool),
490+
('usesAliasDetails', ctypes.c_bool),
487491
('detail', ctypes.POINTER(_cs_detail)),
488492
)
489493

@@ -783,7 +787,7 @@ def __gen_detail(self):
783787
elif arch == CS_ARCH_MIPS:
784788
self.operands = mips.get_arch_info(self._raw.detail.contents.arch.mips)
785789
elif arch == CS_ARCH_PPC:
786-
(self.bc, self.bh, self.update_cr0, self.operands) = \
790+
(self.bc, self.update_cr0, self.operands) = \
787791
ppc.get_arch_info(self._raw.detail.contents.arch.ppc)
788792
elif arch == CS_ARCH_SPARC:
789793
(self.cc, self.hint, self.operands) = sparc.get_arch_info(self._raw.detail.contents.arch.sparc)

bindings/python/capstone/ppc.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@ class PpcOpMem(ctypes.Structure):
99
_fields_ = (
1010
('base', ctypes.c_uint),
1111
('disp', ctypes.c_int32),
12-
)
13-
14-
class PpcOpCrx(ctypes.Structure):
15-
_fields_ = (
16-
('scale', ctypes.c_uint),
17-
('reg', ctypes.c_uint),
18-
('cond', ctypes.c_uint),
12+
('offset', ctypes.c_uint),
1913
)
2014

2115
class PpcOpValue(ctypes.Union):
2216
_fields_ = (
2317
('reg', ctypes.c_uint),
2418
('imm', ctypes.c_int64),
2519
('mem', PpcOpMem),
26-
('crx', PpcOpCrx),
2720
)
2821

2922
class PpcOp(ctypes.Structure):
3023
_fields_ = (
3124
('type', ctypes.c_uint),
3225
('value', PpcOpValue),
26+
('access', ctypes.c_uint),
27+
)
28+
29+
class PpcBC(ctypes.Structure):
30+
_fields_ = (
31+
('bo', ctypes.c_uint8),
32+
('bi', ctypes.c_uint8),
33+
('crX_bit', ctypes.c_uint),
34+
('crX', ctypes.c_uint),
35+
('hint', ctypes.c_uint),
36+
('pred_cr', ctypes.c_uint),
37+
('pred_ctr', ctypes.c_uint),
38+
('bh', ctypes.c_uint),
3339
)
3440

3541
@property
@@ -44,20 +50,14 @@ def reg(self):
4450
def mem(self):
4551
return self.value.mem
4652

47-
@property
48-
def crx(self):
49-
return self.value.crx
50-
51-
5253
class CsPpc(ctypes.Structure):
5354
_fields_ = (
54-
('bc', ctypes.c_uint),
55-
('bh', ctypes.c_uint),
55+
('bc', PpcBC),
5656
('update_cr0', ctypes.c_bool),
5757
('op_count', ctypes.c_uint8),
5858
('operands', PpcOp * 8),
5959
)
6060

6161
def get_arch_info(a):
62-
return (a.bc, a.bh, a.update_cr0, copy_ctypes_list(a.operands[:a.op_count]))
62+
return (a.bc, a.update_cr0, copy_ctypes_list(a.operands[:a.op_count]))
6363

bindings/python/pyx/ccapstone.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ cdef extern from "<capstone/capstone.h>":
1818

1919
ctypedef struct cs_insn:
2020
unsigned int id
21+
uint64_t alias_id;
2122
uint64_t address
2223
uint16_t size
2324
uint8_t bytes[24]
2425
char mnemonic[32]
2526
char op_str[160]
27+
bool is_alias;
28+
bool usesAliasDetails;
2629
cs_detail *detail
2730

2831
ctypedef enum cs_err:

bindings/python/test_ppc.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,42 @@ def print_insn_detail(insn):
3030
c = 0
3131
for i in insn.operands:
3232
if i.type == PPC_OP_REG:
33-
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
33+
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.value.reg)))
3434
if i.type == PPC_OP_IMM:
35-
print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x_32(i.imm)))
35+
print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x_32(i.value.imm)))
3636
if i.type == PPC_OP_MEM:
3737
print("\t\toperands[%u].type: MEM" % c)
38-
if i.mem.base != 0:
38+
if i.value.mem.base != 0:
3939
print("\t\t\toperands[%u].mem.base: REG = %s" \
40-
% (c, insn.reg_name(i.mem.base)))
41-
if i.mem.disp != 0:
40+
% (c, insn.reg_name(i.value.mem.base)))
41+
if i.value.mem.offset != 0:
42+
print("\t\t\toperands[%u].mem.offset: REG = %s\n" \
43+
% (c, insn.reg_name(i.value.mem.offset)))
44+
if i.value.mem.disp != 0:
4245
print("\t\t\toperands[%u].mem.disp: 0x%s" \
43-
% (c, to_x_32(i.mem.disp)))
44-
if i.type == PPC_OP_CRX:
45-
print("\t\toperands[%u].type: CRX" % c)
46-
print("\t\t\toperands[%u].crx.scale: = %u" \
47-
% (c, i.crx.scale))
48-
if i.crx.reg != 0:
49-
print("\t\t\toperands[%u].crx.reg: REG = %s" \
50-
% (c, insn.reg_name(i.crx.reg)))
51-
if i.crx.cond != 0:
52-
print("\t\t\toperands[%u].crx.cond: 0x%x" \
53-
% (c, i.crx.cond))
46+
% (c, to_x_32(i.value.mem.disp)))
47+
if i.access == CS_AC_READ:
48+
print("\t\toperands[%u].access: READ\n" % (c))
49+
elif i.access == CS_AC_WRITE:
50+
print("\t\toperands[%u].access: WRITE\n" % (c))
51+
elif i.access == CS_AC_READ | CS_AC_WRITE:
52+
print("\t\toperands[%u].access: READ | WRITE\n" % (c))
5453
c += 1
54+
if insn.bc.pred_cr != PPC_PRED_INVALID or \
55+
insn.bc.pred_ctr != PPC_PRED_INVALID:
56+
print("\tBranch:\n")
57+
print("\t\tbi: %u\n" % insn.bc.bi)
58+
print("\t\tbo: %u\n" % insn.bc.bo)
59+
if insn.bc.bh != PPC_BH_INVALID:
60+
print("\t\tbh: %u\n" %insn.bc.bh)
61+
if insn.bc.pred_cr != PPC_PRED_INVALID:
62+
print("\t\tcrX: %s\n" % insn.reg_name(insn.bc.crX))
63+
print("\t\tpred CR-bit: %u\n" % insn.bc.pred_cr)
64+
if insn.bc.pred_ctr != PPC_PRED_INVALID:
65+
print("\t\tpred CTR: %u\n" % insn.bc.pred_ctr)
66+
if insn.bc.hint != PPC_BH_INVALID:
67+
print("\t\thint: %u\n" % insn.bc.hint)
5568

56-
if insn.bc:
57-
print("\tBranch code: %u" % insn.bc)
58-
if insn.bh:
59-
print("\tBranch hint: %u" % insn.bh)
6069
if insn.update_cr0:
6170
print("\tUpdate-CR0: True")
6271

0 commit comments

Comments
 (0)