Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
machine.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 
4 
5 class Machine(object):
6  """Abstract machine architecture to restrict architecture dependant code"""
7 
8  __dis_engine = None # Disassembly engine
9  __mn = None # Machine instance
10  __ira = None # IR analyser
11  __jitter = None # Jit engine
12  __gdbserver = None # GdbServer handler
13 
14  __available = ["arml", "armb", "armtl", "armtb", "sh4", "x86_16", "x86_32",
15  "x86_64", "msp430", "mips32b", "mips32l",
16  "aarch64l", "aarch64b"]
17 
18 
19  def __init__(self, machine_name):
20 
21  dis_engine = None
22  mn = None
23  ira = None
24  ir = None
25  jitter = None
26  gdbserver = None
27  jit = None
28  jitter = None
29  log_jit = None
30  log_arch = None
31 
32  # Import on runtime for performance issue
33  if machine_name == "arml":
34  from miasm2.arch.arm.disasm import dis_arml as dis_engine
35  from miasm2.arch.arm import arch, jit
36  mn = arch.mn_arm
37  jitter = jit.jitter_arml
38  from miasm2.arch.arm.ira import ir_a_arml as ira
39  from miasm2.arch.arm.sem import ir_arml as ir
40  elif machine_name == "armb":
41  from miasm2.arch.arm.disasm import dis_armb as dis_engine
42  from miasm2.arch.arm import arch, jit
43  mn = arch.mn_arm
44  jitter = jit.jitter_armb
45  from miasm2.arch.arm.ira import ir_a_armb as ira
46  from miasm2.arch.arm.sem import ir_armb as ir
47  elif machine_name == "aarch64l":
48  from miasm2.arch.aarch64.disasm import dis_aarch64l as dis_engine
49  from miasm2.arch.aarch64 import arch, jit
50  mn = arch.mn_aarch64
51  jitter = jit.jitter_aarch64l
52  from miasm2.arch.aarch64.ira import ir_a_aarch64l as ira
53  from miasm2.arch.aarch64.sem import ir_aarch64l as ir
54  elif machine_name == "aarch64b":
55  from miasm2.arch.aarch64.disasm import dis_aarch64b as dis_engine
56  from miasm2.arch.aarch64 import arch, jit
57  mn = arch.mn_aarch64
58  jitter = jit.jitter_aarch64b
59  from miasm2.arch.aarch64.ira import ir_a_aarch64b as ira
60  from miasm2.arch.aarch64.sem import ir_aarch64b as ir
61  elif machine_name == "armtl":
62  from miasm2.arch.arm.disasm import dis_armtl as dis_engine
63  from miasm2.arch.arm import arch
64  mn = arch.mn_armt
65  from miasm2.arch.arm.ira import ir_a_armtl as ira
66  from miasm2.arch.arm.sem import ir_armtl as ir
67  elif machine_name == "armtb":
68  from miasm2.arch.arm.disasm import dis_armtb as dis_engine
69  from miasm2.arch.arm import arch
70  mn = arch.mn_armt
71  from miasm2.arch.arm.ira import ir_a_armtb as ira
72  from miasm2.arch.arm.sem import ir_armtb as ir
73  elif machine_name == "sh4":
74  from miasm2.arch.sh4 import arch
75  mn = arch.mn_sh4
76  elif machine_name == "x86_16":
77  from miasm2.arch.x86.disasm import dis_x86_16 as dis_engine
78  from miasm2.arch.x86 import arch, jit
79  mn = arch.mn_x86
80  jitter = jit.jitter_x86_16
81  from miasm2.arch.x86.ira import ir_a_x86_16 as ira
82  from miasm2.arch.x86.sem import ir_x86_16 as ir
83  elif machine_name == "x86_32":
84  from miasm2.arch.x86.disasm import dis_x86_32 as dis_engine
85  from miasm2.arch.x86 import arch, jit
86  mn = arch.mn_x86
87  jitter = jit.jitter_x86_32
88  from miasm2.arch.x86.ira import ir_a_x86_32 as ira
89  from miasm2.arch.x86.sem import ir_x86_32 as ir
90  from miasm2.analysis.gdbserver import GdbServer_x86_32 as gdbserver
91  elif machine_name == "x86_64":
92  from miasm2.arch.x86.disasm import dis_x86_64 as dis_engine
93  from miasm2.arch.x86 import arch, jit
94  mn = arch.mn_x86
95  jitter = jit.jitter_x86_64
96  from miasm2.arch.x86.ira import ir_a_x86_64 as ira
97  from miasm2.arch.x86.sem import ir_x86_64 as ir
98  elif machine_name == "msp430":
99  from miasm2.arch.msp430.disasm import dis_msp430 as dis_engine
100  from miasm2.arch.msp430 import arch, jit
101  mn = arch.mn_msp430
102  jitter = jit.jitter_msp430
103  from miasm2.arch.msp430.ira import ir_a_msp430 as ira
104  from miasm2.arch.msp430.sem import ir_msp430 as ir
105  from miasm2.analysis.gdbserver import GdbServer_msp430 as gdbserver
106  elif machine_name == "mips32b":
107  from miasm2.arch.mips32.disasm import dis_mips32b as dis_engine
108  from miasm2.arch.mips32 import arch, jit
109  mn = arch.mn_mips32
110  jitter = jit.jitter_mips32b
111  from miasm2.arch.mips32.ira import ir_a_mips32b as ira
112  from miasm2.arch.mips32.sem import ir_mips32b as ir
113  elif machine_name == "mips32l":
114  from miasm2.arch.mips32.disasm import dis_mips32l as dis_engine
115  from miasm2.arch.mips32 import arch, jit
116  mn = arch.mn_mips32
117  jitter = jit.jitter_mips32l
118  from miasm2.arch.mips32.ira import ir_a_mips32l as ira
119  from miasm2.arch.mips32.sem import ir_mips32l as ir
120  else:
121  raise ValueError('Unknown machine: %s' % machine_name)
122 
123  # Loggers
124  if jit is not None:
125  log_jit = jit.log
126  log_arch = arch.log
127 
128  self.__dis_engine = dis_engine
129  self.__mn = mn
130  self.__ira = ira
131  self.__jitter = jitter
132  self.__gdbserver = gdbserver
133  self.__log_jit = log_jit
134  self.__log_arch = log_arch
135  self.__base_expr = arch.base_expr
136  self.__ir = ir
137 
138  @property
139  def dis_engine(self):
140  return self.__dis_engine
141 
142  @property
143  def mn(self):
144  return self.__mn
145 
146  @property
147  def ira(self):
148  return self.__ira
149 
150  @property
151  def ir(self):
152  return self.__ir
153 
154  @property
155  def jitter(self):
156  return self.__jitter
157 
158  @property
159  def gdbserver(self):
160  return self.__gdbserver
161 
162  @property
163  def log_jit(self):
164  return self.__log_jit
165 
166  @property
167  def log_arch(self):
168  return self.__log_arch
169 
170  @property
171  def base_expr(self):
172  return self.__base_expr
173 
174  @classmethod
176  "Return a list of supported machines"
177  return cls.__available