Skip to content

Commit d715b3e

Browse files
author
Anciety
authored
Merge pull request #24 from ioo0s/mode_set
Add disasm mode setting support. This should close #23
2 parents ad80245 + 9a6dbae commit d715b3e

File tree

8 files changed

+335
-20
lines changed

8 files changed

+335
-20
lines changed

bindings/python/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ classifier = [
1313

1414
[dependencies]
1515
sleighcraft = { path = "../../sleighcraft" }
16+
num_enum = "0.5.1"
1617

1718
[dependencies.pyo3]
1819
version = "0.13.2"

bindings/python/bincraft/__init__.py

Lines changed: 234 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,237 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from .bincraft import *
16+
from .bincraft import *
17+
18+
__all__ = [
19+
'Sleigh',
20+
'ARCH_6502',
21+
'ARCH_6805',
22+
'ARCH_6809',
23+
'ARCH_8048',
24+
'ARCH_8051',
25+
'ARCH_8085',
26+
'ARCH_68020',
27+
'ARCH_68030',
28+
'ARCH_68040',
29+
'ARCH_80251',
30+
'ARCH_80390',
31+
'ARCH_AARCH64',
32+
'ARCH_AARCH64BE',
33+
'ARCH_ARM4_BE',
34+
'ARCH_ARM4_LE',
35+
'ARCH_ARM4T_BE',
36+
'ARCH_ARM4T_LE',
37+
'ARCH_ARM5_BE',
38+
'ARCH_ARM5_LE',
39+
'ARCH_ARM5T_BE',
40+
'ARCH_ARM5T_LE',
41+
'ARCH_ARM6_BE',
42+
'ARCH_ARM6_LE',
43+
'ARCH_ARM7_BE',
44+
'ARCH_ARM7_LE',
45+
'ARCH_ARM8_BE',
46+
'ARCH_ARM8_LE',
47+
'ARCH_AVR8',
48+
'ARCH_AVR8E',
49+
'ARCH_AVR8EIND',
50+
'ARCH_AVR8XMEGA',
51+
'ARCH_AVR32A',
52+
'ARCH_COLDFIRE',
53+
'ARCH_CP1600',
54+
'ARCH_CR16B',
55+
'ARCH_CR16C',
56+
'ARCH_DALVIK',
57+
'ARCH_DATA_BE_64',
58+
'ARCH_DATA_LE_64',
59+
'ARCH_DSPIC30F',
60+
'ARCH_DSPIC33C',
61+
'ARCH_DSPIC33E',
62+
'ARCH_DSPIC33F',
63+
'ARCH_HC05',
64+
'ARCH_HC08',
65+
'ARCH_HCS08',
66+
'ARCH_HCS12',
67+
'ARCH_JVM',
68+
'ARCH_M8C',
69+
'ARCH_MCS96',
70+
'ARCH_MIPS32BE',
71+
'ARCH_MIPS32LE',
72+
'ARCH_MIPS32R6BE',
73+
'ARCH_MIPS32R6LE',
74+
'ARCH_MIPS64BE',
75+
'ARCH_MIPS64LE',
76+
'ARCH_MX51',
77+
'ARCH_PA_RISC32BE',
78+
'ARCH_PIC12C5XX',
79+
'ARCH_PIC16',
80+
'ARCH_PIC16C5X',
81+
'ARCH_PIC16F',
82+
'ARCH_PIC17C7XX',
83+
'ARCH_PIC18',
84+
'ARCH_PIC24E',
85+
'ARCH_PIC24F',
86+
'ARCH_PIC24H',
87+
'ARCH_PPC_32_4XX_BE',
88+
'ARCH_PPC_32_4XX_LE',
89+
'ARCH_PPC_32_BE',
90+
'ARCH_PPC_32_LE',
91+
'ARCH_PPC_32_QUICCIII_BE',
92+
'ARCH_PPC_32_QUICCIII_LE',
93+
'ARCH_PPC_64_BE',
94+
'ARCH_PPC_64_ISA_ALTIVEC_BE',
95+
'ARCH_PPC_64_ISA_ALTIVEC_LE',
96+
'ARCH_PPC_64_ISA_ALTIVEC_VLE_BE',
97+
'ARCH_PPC_64_ISA_BE',
98+
'ARCH_PPC_64_ISA_LE',
99+
'ARCH_PPC_64_ISA_VLE_BE',
100+
'ARCH_PPC_64_LE',
101+
'ARCH_RISCV',
102+
'ARCH_SH_1',
103+
'ARCH_SH_2',
104+
'ARCH_SH_2A',
105+
'ARCH_SPARCV9_32',
106+
'ARCH_SPARCV9_64',
107+
'ARCH_SUPERH4_BE',
108+
'ARCH_SUPERH4_LE',
109+
'ARCH_TI_MSP430',
110+
'ARCH_TI_MSP430X',
111+
'ARCH_TOY_BE_POSSTACK',
112+
'ARCH_TOY_BE',
113+
'ARCH_TOY_BUILDER_BE_ALIGN2',
114+
'ARCH_TOY_BUILDER_BE',
115+
'ARCH_TOY_BUILDER_LE_ALIGN2',
116+
'ARCH_TOY_BUILDER_LE',
117+
'ARCH_TOY_LE',
118+
'ARCH_TOY_WSZ_BE',
119+
'ARCH_TOY_WSZ_LE',
120+
'ARCH_TOY64_BE_HARVARD',
121+
'ARCH_TOY64_BE',
122+
'ARCH_TOY64_LE',
123+
'ARCH_TRICORE',
124+
'ARCH_V850',
125+
'ARCH_X86_64',
126+
'ARCH_X86',
127+
'ARCH_Z80',
128+
'ARCH_Z180',
129+
'MODE_16',
130+
'MODE_32',
131+
'MODE_64',
132+
]
133+
134+
# arch
135+
ARCH_6502 = "6502"
136+
ARCH_6805 = "6805"
137+
ARCH_6809 = "6809"
138+
ARCH_8048 = "8048"
139+
ARCH_8051 = "8051"
140+
ARCH_8085 = "8085"
141+
ARCH_68020 = "68020"
142+
ARCH_68030 = "68030"
143+
ARCH_68040 = "68040"
144+
ARCH_80251 = "80251"
145+
ARCH_80390 = "80390"
146+
ARCH_AARCH64 = "AARCH64"
147+
ARCH_AARCH64BE = "AARCH64BE"
148+
ARCH_ARM4_BE = "ARM4_be"
149+
ARCH_ARM4_LE = "ARM4_le"
150+
ARCH_ARM4T_BE = "ARM4t_be"
151+
ARCH_ARM4T_LE = "ARM4t_le"
152+
ARCH_ARM5_BE = "ARM5_be"
153+
ARCH_ARM5_LE = "ARM5_le"
154+
ARCH_ARM5T_BE = "ARM5t_be"
155+
ARCH_ARM5T_LE = "ARM5t_le"
156+
ARCH_ARM6_BE = "ARM6_be"
157+
ARCH_ARM6_LE = "ARM6_le"
158+
ARCH_ARM7_BE = "ARM7_be"
159+
ARCH_ARM7_LE = "ARM7_le"
160+
ARCH_ARM8_BE = "ARM8_be"
161+
ARCH_ARM8_LE = "ARM8_le"
162+
ARCH_AVR8 = "avr8"
163+
ARCH_AVR8E = "avr8e"
164+
ARCH_AVR8EIND = "avr8eind"
165+
ARCH_AVR8XMEGA = "avr8xmega"
166+
ARCH_AVR32A = "avr32a"
167+
ARCH_COLDFIRE = "coldfire"
168+
ARCH_CP1600 = "CP1600"
169+
ARCH_CR16B = "CR16B"
170+
ARCH_CR16C = "CR16C"
171+
ARCH_DALVIK = "Dalvik"
172+
ARCH_DATA_BE_64 = "data-be-64"
173+
ARCH_DATA_LE_64 = "data-le-64"
174+
ARCH_DSPIC30F = "dsPIC30F"
175+
ARCH_DSPIC33C = "dsPIC33C"
176+
ARCH_DSPIC33E = "dsPIC33E"
177+
ARCH_DSPIC33F = "dsPIC33F"
178+
ARCH_HC05 = "HC05"
179+
ARCH_HC08 = "HC08"
180+
ARCH_HCS08 = "HCS08"
181+
ARCH_HCS12 = "HCS12"
182+
ARCH_JVM = "JVM"
183+
ARCH_M8C = "m8c"
184+
ARCH_MCS96 = "MCS96"
185+
ARCH_MIPS32BE = "mips32be"
186+
ARCH_MIPS32LE = "mips32le"
187+
ARCH_MIPS32R6BE = "mips32R6be"
188+
ARCH_MIPS32R6LE = "mips32R6le"
189+
ARCH_MIPS64BE = "mips64be"
190+
ARCH_MIPS64LE = "mips64le"
191+
ARCH_MX51 = "mx51"
192+
ARCH_PA_RISC32BE = "pa-risc32be"
193+
ARCH_PIC12C5XX = "pic12c5xx"
194+
ARCH_PIC16 = "pic16"
195+
ARCH_PIC16C5X = "pic16c5x"
196+
ARCH_PIC16F = "pic16f"
197+
ARCH_PIC17C7XX = "pic17c7xx"
198+
ARCH_PIC18 = "pic18"
199+
ARCH_PIC24E = "PIC24E"
200+
ARCH_PIC24F = "PIC24F"
201+
ARCH_PIC24H = "PIC24H"
202+
ARCH_PPC_32_4XX_BE = "ppc_32_4xx_be"
203+
ARCH_PPC_32_4XX_LE = "ppc_32_4xx_le"
204+
ARCH_PPC_32_BE = "ppc_32_be"
205+
ARCH_PPC_32_LE = "ppc_32_le"
206+
ARCH_PPC_32_QUICCIII_BE = "ppc_32_quicciii_be"
207+
ARCH_PPC_32_QUICCIII_LE = "ppc_32_quicciii_le"
208+
ARCH_PPC_64_BE = "ppc_64_be"
209+
ARCH_PPC_64_ISA_ALTIVEC_BE = "ppc_64_isa_altivec_be"
210+
ARCH_PPC_64_ISA_ALTIVEC_LE = "ppc_64_isa_altivec_le"
211+
ARCH_PPC_64_ISA_ALTIVEC_VLE_BE = "ppc_64_isa_altivec_vle_be"
212+
ARCH_PPC_64_ISA_BE = "ppc_64_isa_be"
213+
ARCH_PPC_64_ISA_LE = "ppc_64_isa_le"
214+
ARCH_PPC_64_ISA_VLE_BE = "ppc_64_isa_vle_be"
215+
ARCH_PPC_64_LE = "ppc_64_le"
216+
ARCH_RISCV = "riscv"
217+
ARCH_SH_1 = "sh-1"
218+
ARCH_SH_2 = "sh-2"
219+
ARCH_SH_2A = "sh-2a"
220+
ARCH_SPARCV9_32 = "SparcV9_32"
221+
ARCH_SPARCV9_64 = "SparcV9_64"
222+
ARCH_SUPERH4_BE = "SuperH4_be"
223+
ARCH_SUPERH4_LE = "SuperH4_le"
224+
ARCH_TI_MSP430 = "TI_MSP430"
225+
ARCH_TI_MSP430X = "TI_MSP430X"
226+
ARCH_TOY_BE_POSSTACK = "toy_be_posStack"
227+
ARCH_TOY_BE = "toy_be"
228+
ARCH_TOY_BUILDER_BE_ALIGN2 = "toy_builder_be_align2"
229+
ARCH_TOY_BUILDER_BE = "toy_builder_be"
230+
ARCH_TOY_BUILDER_LE_ALIGN2 = "toy_builder_le_align2"
231+
ARCH_TOY_BUILDER_LE = "toy_builder_le"
232+
ARCH_TOY_LE = "toy_le"
233+
ARCH_TOY_WSZ_BE = "toy_wsz_be"
234+
ARCH_TOY_WSZ_LE = "toy_wsz_le"
235+
ARCH_TOY64_BE_HARVARD = "toy64_be_harvard"
236+
ARCH_TOY64_BE = "toy64_be"
237+
ARCH_TOY64_LE = "toy64_le"
238+
ARCH_TRICORE = "tricore"
239+
ARCH_V850 = "V850"
240+
ARCH_X86_64 = "x86-64"
241+
ARCH_X86 = "x86"
242+
ARCH_Z80 = "z80"
243+
ARCH_Z180 = "z180"
244+
245+
# disasm mode
246+
MODE_16 = 0
247+
MODE_32 = 1
248+
MODE_64 = 2
249+

bindings/python/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
22
// Copyright 2021 StarCrossTech
3-
//
3+
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
66
// You may obtain a copy of the License at
7-
//
7+
//
88
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
9+
//
1010
// Unless required by applicable law or agreed to in writing, software
1111
// distributed under the License is distributed on an "AS IS" BASIS,
1212
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,10 +17,9 @@ use pyo3::class::basic::PyObjectProtocol;
1717
use pyo3::prelude::*;
1818
use pyo3::types::{PyList, PyLong, PyUnicode};
1919
use sleighcraft::error::Error;
20-
use sleighcraft::{
21-
arch, CollectingAssemblyEmit, CollectingPcodeEmit, PlainLoadImage, SleighBuilder,
22-
};
20+
use sleighcraft::{arch, CollectingAssemblyEmit, CollectingPcodeEmit, PlainLoadImage, SleighBuilder, Mode};
2321
use sleighcraft::{Address, Instruction, PcodeInstruction, PcodeVarnodeData};
22+
use std::convert::TryFrom;
2423

2524
#[pyclass]
2625
#[derive(Clone, PartialEq, Eq)]
@@ -263,18 +262,24 @@ impl PyInstruction {
263262
pub struct Sleigh {
264263
spec: Option<String>,
265264
code: Option<Vec<u8>>,
265+
mode: Option<Mode>
266266
}
267267

268268
#[pymethods]
269269
impl Sleigh {
270270
#[new]
271-
pub fn new(spec: &PyUnicode, code: &PyList) -> Self {
271+
#[args(mode = "0")]
272+
pub fn new(spec: &PyUnicode, code: &PyList, mode: i32) -> Self {
272273
let sp: &str = spec.extract().unwrap();
273274
let spec = arch(sp);
274275
let codes: Vec<u8> = code.extract().unwrap();
275276
let spec = Option::from(spec.unwrap().to_string());
277+
let mode = {
278+
let mode =Mode::try_from(mode);
279+
mode.ok()
280+
};
276281
let code = Option::from(codes);
277-
Sleigh { spec, code }
282+
Sleigh { spec, code, mode}
278283
}
279284

280285
#[text_signature = "($self, start, cnt)"]
@@ -285,6 +290,7 @@ impl Sleigh {
285290
let mut loader = PlainLoadImage::from_buf(self.code.as_ref().unwrap().as_ref(), start);
286291
sleigh_builder.loader(&mut loader);
287292
sleigh_builder.spec(self.spec.as_ref().unwrap().as_str());
293+
sleigh_builder.mode(self.mode.unwrap());
288294

289295
let mut asm_emit = CollectingAssemblyEmit::default();
290296
let mut pcode_emit = CollectingPcodeEmit::default();

sleighcraft/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ repository = "https://github.com/ret2lab/bincraft/"
1313
[dependencies]
1414
cxx = "1.0"
1515
once_cell = "1.6.0"
16+
num_enum = "0.5.1"
1617

1718
[dependencies.pyo3]
1819
version = "0.13.2"

sleighcraft/src/cpp/bridge/disasm.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@
1818
#include <iostream>
1919
#include "proxies/address_proxy.hh"
2020

21-
void SleighProxy::set_spec(const rust::Str spec_content) {
21+
void SleighProxy::set_spec(const rust::Str spec_content, int mode) {
2222
stringstream ss;
2323
ss << spec_content;
2424

2525
Element *root = storage.parseDocument(ss)->getRoot();
2626
storage.registerTag(root);
2727

2828
translator.initialize(storage);
29+
30+
this->ctx.setVariableDefault("addrsize",mode);
31+
this->ctx.setVariableDefault("opsize",mode);
2932
}
3033

31-
void SleighProxy::setSpecFromPath(const rust::Str path) {
34+
void SleighProxy::setSpecFromPath(const rust::Str path,int mode) {
3235
string cxxpath = string(path);
3336
Element *root = storage.openDocument(cxxpath)->getRoot();
3437
storage.registerTag(root);
3538

3639
translator.initialize(storage);
40+
41+
this->ctx.setVariableDefault("addrsize",mode); // Address size is 32-bit
42+
this->ctx.setVariableDefault("opsize",mode); // Operand size is 32-bit
3743
}
3844

3945
unique_ptr<SleighProxy> new_sleigh_proxy(RustLoadImage &ld) {

sleighcraft/src/cpp/bridge/disasm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class SleighProxy {
9696
public:
9797
SleighProxy(RustLoadImage &ld): loader(ld), translator(&loader, &this->ctx) {}
9898

99-
void setSpecFromPath(const rust::Str path);
100-
void set_spec(const rust::Str spec_content);
99+
void setSpecFromPath(const rust::Str path, int mode);
100+
void set_spec(const rust::Str spec_content, int mode);
101101
void decode_with(RustAssemblyEmit& asm_emit, RustPcodeEmit& pcode_emit, uint64_t start);
102102

103103
private:

0 commit comments

Comments
 (0)