Skip to content

Commit 13d08bb

Browse files
committed
Add Python bindings for SH
1 parent 7572681 commit 13d08bb

File tree

8 files changed

+563
-7
lines changed

8 files changed

+563
-7
lines changed

bindings/const_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
INCL_DIR = '../include/capstone/'
77

8-
include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h', 'evm.h', 'mos65xx.h', 'wasm.h', 'bpf.h' ,'riscv.h', 'tricore.h' ]
8+
include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h', 'evm.h', 'mos65xx.h', 'wasm.h', 'bpf.h' ,'riscv.h', 'sh.h', 'tricore.h' ]
99

1010
template = {
1111
'java': {
@@ -53,6 +53,7 @@
5353
'mos65xx.h': 'mos65xx',
5454
'bpf.h': 'bpf',
5555
'riscv.h': 'riscv',
56+
'sh.h': 'sh',
5657
'tricore.h': ['TRICORE', 'TriCore'],
5758
'comment_open': '#',
5859
'comment_close': '',

bindings/python/capstone/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
'CS_ARCH_TMS320C64X',
3636
'CS_ARCH_M680X',
3737
'CS_ARCH_EVM',
38+
'CS_ARCH_MOS65XX',
3839
'CS_ARCH_BPF',
3940
'CS_ARCH_RISCV',
40-
'CS_ARCH_MOS65XX',
41+
'CS_ARCH_SH',
4142
'CS_ARCH_TRICORE',
4243
'CS_ARCH_ALL',
4344

@@ -89,6 +90,13 @@
8990
'CS_MODE_MOS65XX_65816_LONG_M',
9091
'CS_MODE_MOS65XX_65816_LONG_X',
9192
'CS_MODE_MOS65XX_65816_LONG_MX',
93+
'CS_MODE_SH2',
94+
'CS_MODE_SH2A',
95+
'CS_MODE_SH3',
96+
'CS_MODE_SH4',
97+
'CS_MODE_SH4A',
98+
'CS_MODE_SHFPU',
99+
'CS_MODE_SHDSP',
92100
'CS_MODE_TRICORE_110',
93101
'CS_MODE_TRICORE_120',
94102
'CS_MODE_TRICORE_130',
@@ -185,7 +193,7 @@
185193
CS_ARCH_WASM = 13
186194
CS_ARCH_BPF = 14
187195
CS_ARCH_RISCV = 15
188-
# CS_ARCH_SH = 16
196+
CS_ARCH_SH = 16
189197
CS_ARCH_TRICORE = 17
190198
CS_ARCH_MAX = 18
191199
CS_ARCH_ALL = 0xFFFF
@@ -239,6 +247,13 @@
239247
CS_MODE_MOS65XX_65816_LONG_M = (1 << 5) # MOS65XXX WDC 65816, 16-bit m, 8-bit x
240248
CS_MODE_MOS65XX_65816_LONG_X = (1 << 6) # MOS65XXX WDC 65816, 8-bit m, 16-bit x
241249
CS_MODE_MOS65XX_65816_LONG_MX = CS_MODE_MOS65XX_65816_LONG_M | CS_MODE_MOS65XX_65816_LONG_X
250+
CS_MODE_SH2 = 1 << 1 # SH2
251+
CS_MODE_SH2A = 1 << 2 # SH2A
252+
CS_MODE_SH3 = 1 << 3 # SH3
253+
CS_MODE_SH4 = 1 << 4 # SH4
254+
CS_MODE_SH4A = 1 << 5 # SH4A
255+
CS_MODE_SHFPU = 1 << 6 # w/ FPU
256+
CS_MODE_SHDSP = 1 << 7 # w/ DSP
242257
CS_MODE_TRICORE_110 = 1 << 1 # Tricore 1.1
243258
CS_MODE_TRICORE_120 = 1 << 2 # Tricore 1.2
244259
CS_MODE_TRICORE_130 = 1 << 3 # Tricore 1.3
@@ -387,7 +402,7 @@ def copy_ctypes_list(src):
387402
return [copy_ctypes(n) for n in src]
388403

389404
# Weird import placement because these modules are needed by the below code but need the above functions
390-
from . import arm, arm64, m68k, mips, ppc, sparc, systemz, x86, xcore, tms320c64x, m680x, evm, mos65xx, bpf, riscv, tricore
405+
from . import arm, arm64, m68k, mips, ppc, sparc, systemz, x86, xcore, tms320c64x, m680x, evm, mos65xx, bpf, riscv, sh, tricore
391406

392407
class _cs_arch(ctypes.Union):
393408
_fields_ = (
@@ -406,6 +421,7 @@ class _cs_arch(ctypes.Union):
406421
('mos65xx', mos65xx.CsMOS65xx),
407422
('bpf', bpf.CsBPF),
408423
('riscv', riscv.CsRISCV),
424+
('sh', sh.CsSH),
409425
('tricore', tricore.CsTriCore),
410426
)
411427

@@ -731,6 +747,8 @@ def __gen_detail(self):
731747
(self.operands) = bpf.get_arch_info(self._raw.detail.contents.arch.bpf)
732748
elif arch == CS_ARCH_RISCV:
733749
(self.need_effective_addr, self.operands) = riscv.get_arch_info(self._raw.detail.contents.arch.riscv)
750+
elif arch == CS_ARCH_SH:
751+
(self.sh_insn, self.sh_size, self.operands) = sh.get_arch_info(self._raw.detail.contents.arch.sh)
734752
elif arch == CS_ARCH_TRICORE:
735753
(self.update_flags, self.operands) = tricore.get_arch_info(self._raw.detail.contents.arch.tricore)
736754

@@ -1199,6 +1217,7 @@ def debug():
11991217
"sysz": CS_ARCH_SYSZ, 'xcore': CS_ARCH_XCORE, "tms320c64x": CS_ARCH_TMS320C64X,
12001218
"m680x": CS_ARCH_M680X, 'evm': CS_ARCH_EVM, 'mos65xx': CS_ARCH_MOS65XX,
12011219
'bpf': CS_ARCH_BPF, 'riscv': CS_ARCH_RISCV, 'tricore': CS_ARCH_TRICORE,
1220+
'sh': CS_ARCH_SH,
12021221
}
12031222

12041223
all_archs = ""

bindings/python/capstone/sh.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Capstone Python bindings, by Peace-Maker <peacemakerctf@gmail.com>
2+
3+
import ctypes
4+
from . import copy_ctypes_list
5+
from .sh_const import *
6+
7+
# define the API
8+
class SHOpMem(ctypes.Structure):
9+
_fields_ = (
10+
('address', ctypes.c_uint),
11+
('reg', ctypes.c_uint),
12+
('disp', ctypes.c_uint32),
13+
)
14+
15+
class SHOpDsp(ctypes.Structure):
16+
_fields_ = (
17+
('insn', ctypes.c_uint),
18+
('operand', ctypes.c_uint * 2),
19+
('r', ctypes.c_uint * 6),
20+
('cc', ctypes.c_uint),
21+
('imm', ctypes.c_uint8),
22+
('size', ctypes.c_int),
23+
)
24+
25+
class SHOpValue(ctypes.Union):
26+
_fields_ = (
27+
('imm', ctypes.c_int64),
28+
('reg', ctypes.c_uint),
29+
('mem', SHOpMem),
30+
('dsp', SHOpDsp),
31+
)
32+
33+
class SHOp(ctypes.Structure):
34+
_fields_ = (
35+
('type', ctypes.c_uint),
36+
('value', SHOpValue),
37+
)
38+
39+
@property
40+
def imm(self):
41+
return self.value.imm
42+
43+
@property
44+
def reg(self):
45+
return self.value.reg
46+
47+
@property
48+
def mem(self):
49+
return self.value.mem
50+
51+
@property
52+
def dsp(self):
53+
return self.value.dsp
54+
55+
56+
class CsSH(ctypes.Structure):
57+
_fields_ = (
58+
('insn', ctypes.c_uint),
59+
('size', ctypes.c_uint8),
60+
('op_count', ctypes.c_uint8),
61+
('operands', SHOp * 3),
62+
)
63+
64+
def get_arch_info(a):
65+
return (a.insn, a.size, copy_ctypes_list(a.operands[:a.op_count]))
66+

0 commit comments

Comments
 (0)