Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
miasm2.arch.msp430.arch.mn_msp430 Class Reference
+ Inheritance diagram for miasm2.arch.msp430.arch.mn_msp430:
+ Collaboration diagram for miasm2.arch.msp430.arch.mn_msp430:

Public Member Functions

def getpc
 
def getsp
 
def check_mnemo
 
def getbits
 
def getbytes
 
def decoded2bytes
 
def gen_modes
 
def additional_info
 
def getmn
 
def reset_class
 
def getnextflow
 
def guess_mnemo
 
def init_class
 
def add_pre_dis_info
 
def pre_dis
 
def post_dis
 
def mod_fields
 
def dis
 
def fromstring
 
def dup_info
 
def get_cls_instance
 
def asm
 
def filter_asm_candidates
 
def value
 
def encodefields
 
def gen_args
 
def args2str
 
def __str__
 
def parse_prefix
 
def set_dst_symbol
 
def getdstflow
 

Public Attributes

 args_symb
 
 args
 
 fields_order
 
 to_decode
 

Static Public Attributes

string name = "msp430"
 
 regs = regs_module
 
list all_mn = []
 
dictionary bintree = {}
 
int num = 0
 
int delayslot = 0
 
dictionary pc = {None: PC}
 
dictionary sp = {None: SP}
 
tuple all_mn_mode = defaultdict(list)
 
tuple all_mn_name = defaultdict(list)
 
tuple all_mn_inst = defaultdict(list)
 
 instruction = instruction_msp430
 
int max_instruction_len = 8
 
list args_symb = []
 
int alignment = 1
 

Detailed Description

Definition at line 214 of file arch.py.

Member Function Documentation

def miasm2.core.cpu.cls_mn.__str__ (   self)
inherited

Definition at line 1430 of file cpu.py.

1431  def __str__(self):
1432  o = "%-10s " % self.name
1433  args = []
1434  for arg in self.args:
1435  # XXX todo test
1436  if not (isinstance(arg, m2_expr.Expr) or
1437  isinstance(arg.expr, m2_expr.Expr)):
1438  raise ValueError('zarb arg type')
1439  x = str(arg)
1440  args.append(x)
1441 
1442  o += self.gen_args(args)
1443  return o

+ Here is the call graph for this function:

def miasm2.core.cpu.cls_mn.add_pre_dis_info (   self,
  prefix = None 
)
inherited

Definition at line 1065 of file cpu.py.

1066  def add_pre_dis_info(self, prefix=None):
1067  return True
def miasm2.arch.msp430.arch.mn_msp430.additional_info (   self)

Definition at line 290 of file arch.py.

291  def additional_info(self):
292  info = additional_info()
293  return info
def miasm2.core.cpu.cls_mn.args2str (   self)
inherited

Definition at line 1419 of file cpu.py.

1420  def args2str(self):
1421  args = []
1422  for arg in self.args:
1423  # XXX todo test
1424  if not (isinstance(arg, m2_expr.Expr) or
1425  isinstance(arg.expr, m2_expr.Expr)):
1426  raise ValueError('zarb arg type')
1427  x = str(arg)
1428  args.append(x)
1429  return args
def miasm2.core.cpu.cls_mn.asm (   cls,
  instr,
  symbols = None 
)
inherited
Re asm instruction by searching mnemo using name and args. We then
can modify args and get the hex of a modified instruction

Definition at line 1287 of file cpu.py.

1288  def asm(cls, instr, symbols=None):
1289  """
1290  Re asm instruction by searching mnemo using name and args. We then
1291  can modify args and get the hex of a modified instruction
1292  """
1293  clist = cls.all_mn_name[instr.name]
1294  clist = [x for x in clist]
1295  vals = []
1296  candidates = []
1297  args = instr.resolve_args_with_symbols(symbols)
1298 
1299  for cc in clist:
1300 
1301  for c in cls.get_cls_instance(
1302  cc, instr.mode, instr.additional_info):
1303 
1304  cannot_parse = False
1305  if len(c.args) != len(instr.args):
1306  continue
1307 
1308  # only fix args expr
1309  for i in xrange(len(c.args)):
1310  c.args[i].expr = args[i]
1311 
1312  v = c.value(instr.mode)
1313  if not v:
1314  log.debug("cannot encode %r", c)
1315  cannot_parse = True
1316  if cannot_parse:
1317  continue
1318  vals += v
1319  candidates.append((c, v))
1320  if len(vals) == 0:
1321  raise ValueError('cannot asm %r %r' %
1322  (instr.name, [str(x) for x in instr.args]))
1323  if len(vals) != 1:
1324  log.debug('asm multiple args ret default')
1325 
1326  vals = cls.filter_asm_candidates(instr, candidates)
1327  return vals
def miasm2.arch.msp430.arch.mn_msp430.check_mnemo (   cls,
  fields 
)

Definition at line 238 of file arch.py.

239  def check_mnemo(cls, fields):
240  l = sum([x.l for x in fields])
241  assert l % 16 == 00, "len %r" % l
def miasm2.arch.msp430.arch.mn_msp430.decoded2bytes (   self,
  result 
)

Definition at line 274 of file arch.py.

275  def decoded2bytes(self, result):
276  tmp = super(mn_msp430, self).decoded2bytes(result)
277  out = []
278  for x in tmp:
279  o = ""
280  while x:
281  o += x[:2][::-1]
282  x = x[2:]
283  out.append(o)
284  return out

+ Here is the caller graph for this function:

def miasm2.core.cpu.cls_mn.dis (   cls,
  bs_o,
  mode_o = None,
  offset = 0 
)
inherited

Definition at line 1092 of file cpu.py.

1093  def dis(cls, bs_o, mode_o = None, offset=0):
1094  if not isinstance(bs_o, bin_stream):
1095  bs_o = bin_stream_str(bs_o)
1096 
1097  offset_o = offset
1098  pre_dis_info, bs, mode, offset, prefix_len = cls.pre_dis(
1099  bs_o, mode_o, offset)
1100  candidates = cls.guess_mnemo(bs, mode, pre_dis_info, offset)
1101  out = []
1102  out_c = []
1103  if hasattr(bs, 'getlen'):
1104  bs_l = bs.getlen()
1105  else:
1106  bs_l = len(bs)
1107 
1108  alias = False
1109  for c in candidates:
1110  log.debug("*" * 40, mode, c.mode)
1111  log.debug(c.fields)
1112 
1113  c = cls.all_mn_inst[c][0]
1114 
1115  c.reset_class()
1116  c.mode = mode
1117 
1118  if not c.add_pre_dis_info(pre_dis_info):
1119  continue
1120 
1121  args = []
1122  todo = {}
1123  getok = True
1124  fname_values = dict(pre_dis_info)
1125  offset_b = offset * 8
1126 
1127  total_l = 0
1128  for i, f in enumerate(c.fields_order):
1129  if f.flen is not None:
1130  l = f.flen(mode, fname_values)
1131  else:
1132  l = f.l
1133  if l is not None:
1134  total_l += l
1135  f.l = l
1136  f.is_present = True
1137  log.debug("FIELD %s %s %s %s", f.__class__, f.fname,
1138  offset_b, l)
1139  if bs_l * 8 - offset_b < l:
1140  getok = False
1141  break
1142  bv = cls.getbits(bs, mode, offset_b, l)
1143  offset_b += l
1144  if not f.fname in fname_values:
1145  fname_values[f.fname] = bv
1146  todo[i] = bv
1147  else:
1148  f.is_present = False
1149  todo[i] = None
1150 
1151  if not getok:
1152  continue
1153 
1154  c.l = prefix_len + total_l / 8
1155  for i in c.to_decode:
1156  f = c.fields_order[i]
1157  if f.is_present:
1158  ret = f.decode(todo[i])
1159  if not ret:
1160  log.debug("cannot decode %r", f)
1161  break
1162 
1163  if not ret:
1164  continue
1165  for a in c.args:
1166  a.expr = expr_simp(a.expr)
1167 
1168  c.b = cls.getbytes(bs, offset_o, c.l)
1169  c.offset = offset_o
1170  c = c.post_dis()
1171  if c is None:
1172  continue
1173  c_args = [a.expr for a in c.args]
1174  instr = cls.instruction(c.name, mode, c_args,
1175  additional_info=c.additional_info())
1176  instr.l = prefix_len + total_l / 8
1177  instr.b = cls.getbytes(bs, offset_o, instr.l)
1178  instr.offset = offset_o
1179  instr.get_info(c)
1180  if c.alias:
1181  alias = True
1182  out.append(instr)
1183  out_c.append(c)
1184  if not out:
1185  raise Disasm_Exception('cannot disasm at %X' % offset_o)
1186  if len(out) != 1:
1187  if not alias:
1188  log.warning('dis multiple args ret default')
1189 
1190  assert(len(out) == 2)
1191  for i, o in enumerate(out_c):
1192  if o.alias:
1193  return out[i]
1194  raise NotImplementedError('not fully functional')
1195  return out[0]
def miasm2.core.cpu.cls_mn.dup_info (   self,
  infos 
)
inherited

Definition at line 1272 of file cpu.py.

1273  def dup_info(self, infos):
1274  return
def miasm2.core.cpu.cls_mn.encodefields (   self,
  decoded 
)
inherited

Definition at line 1388 of file cpu.py.

1389  def encodefields(self, decoded):
1390  bits = bitobj()
1391  for p, f in decoded:
1392  setattr(self, f.fname, f)
1393 
1394  if f.value is None:
1395  continue
1396  bits.putbits(f.value, f.l)
1397 
1398  xx = bits.tostring()
1399  return bits.tostring()

+ Here is the caller graph for this function:

def miasm2.core.cpu.cls_mn.filter_asm_candidates (   cls,
  instr,
  candidates 
)
inherited

Definition at line 1329 of file cpu.py.

1330  def filter_asm_candidates(cls, instr, candidates):
1331  o = []
1332  for c, v in candidates:
1333  o += v
1334  o.sort(key=len)
1335  return o
def filter_asm_candidates
Definition: cpu.py:1329
def miasm2.core.cpu.cls_mn.fromstring (   cls,
  s,
  mode = None 
)
inherited

Definition at line 1197 of file cpu.py.

1198  def fromstring(cls, s, mode = None):
1199  global total_scans
1200  name = re.search('(\S+)', s).groups()
1201  if not name:
1202  raise ValueError('cannot find name', s)
1203  name = name[0]
1204 
1205  if not name in cls.all_mn_name:
1206  raise ValueError('unknown name', name)
1207  clist = [x for x in cls.all_mn_name[name]]
1208  out = []
1209  out_args = []
1210  parsers = defaultdict(dict)
1211 
1212  for cc in clist:
1213  for c in cls.get_cls_instance(cc, mode):
1214  args_expr = []
1215  args_str = s[len(name):].strip(' ')
1216 
1217  start = 0
1218  cannot_parse = False
1219  len_o = len(args_str)
1220 
1221  for i, f in enumerate(c.args):
1222  start_i = len_o - len(args_str)
1223  if type(f.parser) == tuple:
1224  parser = f.parser
1225  else:
1226  parser = (f.parser,)
1227  for p in parser:
1228  if p in parsers[(i, start_i)]:
1229  continue
1230  try:
1231  total_scans += 1
1232  v, start, stop = p.scanString(args_str).next()
1233  except StopIteration:
1234  v, start, stop = [None], None, None
1235  if start != 0:
1236  v, start, stop = [None], None, None
1237  parsers[(i, start_i)][p] = v[0], start, stop
1238 
1239  start, stop = f.fromstring(args_str, parsers[(i, start_i)])
1240  if start != 0:
1241  log.debug("cannot fromstring %r", args_str)
1242  cannot_parse = True
1243  break
1244  if f.expr is None:
1245  raise NotImplementedError('not fully functional')
1246  f.expr = expr_simp(f.expr)
1247  args_expr.append(f.expr)
1248  a = args_str[start:stop]
1249  args_str = args_str[stop:].strip(' ')
1250  if args_str.startswith(','):
1251  args_str = args_str[1:]
1252  args_str = args_str.strip(' ')
1253  if args_str:
1254  cannot_parse = True
1255  if cannot_parse:
1256  continue
1257 
1258  out.append(c)
1259  out_args.append(args_expr)
1260  break
1261 
1262  if len(out) == 0:
1263  raise ValueError('cannot fromstring %r' % s)
1264  if len(out) != 1:
1265  log.debug('fromstring multiple args ret default')
1266  c = out[0]
1267  c_args = out_args[0]
1268 
1269  instr = cls.instruction(c.name, mode, c_args,
1270  additional_info=c.additional_info())
1271  return instr
def miasm2.core.cpu.cls_mn.gen_args (   self,
  args 
)
inherited

Definition at line 1415 of file cpu.py.

1416  def gen_args(self, args):
1417  out = ', '.join([str(x) for x in args])
1418  return out

+ Here is the caller graph for this function:

def miasm2.arch.msp430.arch.mn_msp430.gen_modes (   cls,
  subcls,
  name,
  bases,
  dct,
  fields 
)

Definition at line 286 of file arch.py.

287  def gen_modes(cls, subcls, name, bases, dct, fields):
288  dct['mode'] = None
289  return [(subcls, name, bases, dct, fields)]
def miasm2.core.cpu.cls_mn.get_cls_instance (   cls,
  cc,
  mode,
  infos = None 
)
inherited

Definition at line 1276 of file cpu.py.

1277  def get_cls_instance(cls, cc, mode, infos=None):
1278  c = cls.all_mn_inst[cc][0]
1279 
1280  c.reset_class()
1281  c.add_pre_dis_info()
1282  c.dup_info(infos)
1283 
1284  c.mode = mode
1285  yield c
def miasm2.arch.msp430.arch.mn_msp430.getbits (   cls,
  bs,
  attrib,
  start,
  n 
)

Definition at line 243 of file arch.py.

244  def getbits(cls, bs, attrib, start, n):
245  if not n:
246  return 0
247  o = 0
248  if n > bs.getlen() * 8:
249  raise ValueError('not enought bits %r %r' % (n, len(bs.bin) * 8))
250  while n:
251  i = start / 8
252  c = cls.getbytes(bs, i)
253  if not c:
254  raise IOError
255  c = ord(c)
256  r = 8 - start % 8
257  c &= (1 << r) - 1
258  l = min(r, n)
259  c >>= (r - l)
260  o <<= l
261  o |= c
262  n -= l
263  start += l
264  return o
def miasm2.arch.msp430.arch.mn_msp430.getbytes (   cls,
  bs,
  offset,
  l = 1 
)

Definition at line 266 of file arch.py.

267  def getbytes(cls, bs, offset, l=1):
268  out = ""
269  for _ in xrange(l):
270  n_offset = (offset & ~1) + 1 - offset % 2
271  out += bs.getbytes(n_offset, 1)
272  offset += 1
273  return out

+ Here is the caller graph for this function:

def miasm2.core.cpu.cls_mn.getdstflow (   self,
  symbol_pool 
)
inherited

Definition at line 1460 of file cpu.py.

1461  def getdstflow(self, symbol_pool):
1462  return [self.args[0].expr]
1463 

+ Here is the caller graph for this function:

def miasm2.arch.msp430.arch.mn_msp430.getmn (   cls,
  name 
)

Definition at line 295 of file arch.py.

296  def getmn(cls, name):
297  return name.upper()
def miasm2.arch.msp430.arch.mn_msp430.getnextflow (   self,
  symbol_pool 
)

Definition at line 301 of file arch.py.

302  def getnextflow(self, symbol_pool):
303  raise NotImplementedError('not fully functional')
304 
def miasm2.arch.msp430.arch.mn_msp430.getpc (   cls,
  attrib 
)

Definition at line 230 of file arch.py.

231  def getpc(cls, attrib):
232  return PC
def miasm2.arch.msp430.arch.mn_msp430.getsp (   cls,
  attrib 
)

Definition at line 234 of file arch.py.

235  def getsp(cls, attrib):
236  return SP
def miasm2.core.cpu.cls_mn.guess_mnemo (   cls,
  bs,
  attrib,
  pre_dis_info,
  offset 
)
inherited

Definition at line 987 of file cpu.py.

988  def guess_mnemo(cls, bs, attrib, pre_dis_info, offset):
989  candidates = []
990 
991  candidates = set()
992 
993  fname_values = pre_dis_info
994  todo = [(dict(fname_values), branch, offset * 8)
995  for branch in cls.bintree.items()]
996  cpt = 0
997  if hasattr(bs, 'getlen'):
998  bs_l = bs.getlen()
999  else:
1000  bs_l = len(bs)
1001  for fname_values, branch, offset_b in todo:
1002  (l, fmask, fbits, fname, flen), vals = branch
1003  cpt += 1
1004 
1005  if flen is not None:
1006  l = flen(attrib, fname_values)
1007  if l is not None:
1008  try:
1009  v = cls.getbits(bs, attrib, offset_b, l)
1010  except IOError:
1011  # Raised if offset is out of bound
1012  continue
1013  offset_b += l
1014  if v & fmask != fbits:
1015  continue
1016  if fname is not None and not fname in fname_values:
1017  fname_values[fname] = v
1018  for nb, v in vals.items():
1019  if 'mn' in nb:
1020  candidates.update(v)
1021  else:
1022  todo.append((dict(fname_values), (nb, v), offset_b))
1023 
1024  candidates = [c for c in candidates]
1025 
1026  if not candidates:
1027  raise Disasm_Exception('cannot disasm (guess) at %X' % offset)
1028  return candidates
def miasm2.core.cpu.cls_mn.init_class (   self)
inherited

Definition at line 1040 of file cpu.py.

1041  def init_class(self):
1042  args = []
1043  fields_order = []
1044  to_decode = []
1045  off = 0
1046  for i, fc in enumerate(self.fields):
1047  f = fc.gen(self)
1048  f.offset = off
1049  off += f.l
1050  fields_order.append(f)
1051  to_decode.append((i, f))
1052 
1053  if isinstance(f, m_arg):
1054  args.append(f)
1055  if f.fname:
1056  setattr(self, f.fname, f)
1057  if hasattr(self, 'args_permut'):
1058  args = [args[self.args_permut[i]]
1059  for i in xrange(len(self.args_permut))]
1060  to_decode.sort(key=lambda x: (x[1].order, x[0]))
1061  to_decode = [fields_order.index(f[1]) for f in to_decode]
1062  self.args = args
1063  self.fields_order = fields_order
1064  self.to_decode = to_decode
def miasm2.core.cpu.cls_mn.mod_fields (   cls,
  fields 
)
inherited

Definition at line 1088 of file cpu.py.

1089  def mod_fields(cls, fields):
1090  return fields
def miasm2.core.cpu.cls_mn.parse_prefix (   self,
  v 
)
inherited

Definition at line 1444 of file cpu.py.

1445  def parse_prefix(self, v):
1446  return 0
def miasm2.core.cpu.cls_mn.post_dis (   self)
inherited

Definition at line 1080 of file cpu.py.

1081  def post_dis(self):
1082  return self
def miasm2.core.cpu.cls_mn.pre_dis (   cls,
  v_o,
  attrib,
  offset 
)
inherited

Definition at line 1077 of file cpu.py.

1078  def pre_dis(cls, v_o, attrib, offset):
1079  return {}, v_o, attrib, offset, 0
def miasm2.arch.msp430.arch.mn_msp430.reset_class (   self)

Definition at line 298 of file arch.py.

299  def reset_class(self):
300  super(mn_msp430, self).reset_class()
def miasm2.core.cpu.cls_mn.set_dst_symbol (   self,
  symbol_pool 
)
inherited

Definition at line 1447 of file cpu.py.

1448  def set_dst_symbol(self, symbol_pool):
1449  dst = self.getdstflow(symbol_pool)
1450  args = []
1451  for d in dst:
1452  if isinstance(d, m2_expr.ExprInt):
1453  l = symbol_pool.getby_offset_create(int(d.arg))
1454 
1455  a = m2_expr.ExprId(l.name, d.size)
1456  else:
1457  a = d
1458  args.append(a)
1459  self.args_symb = args

+ Here is the call graph for this function:

def miasm2.core.cpu.cls_mn.value (   self,
  mode 
)
inherited

Definition at line 1336 of file cpu.py.

1337  def value(self, mode):
1338  todo = [(0, 0, [(x, self.fields_order[x]) for x in self.to_decode[::-1]])]
1339 
1340  result = []
1341  done = []
1342  cpt = 0
1343 
1344  while todo:
1345  index, cur_len, to_decode = todo.pop()
1346  # TEST XXX
1347  for i, f in to_decode:
1348  setattr(self, f.fname, f)
1349  if (index, [x[1].value for x in to_decode]) in done:
1350  continue
1351  done.append((index, [x[1].value for x in to_decode]))
1352 
1353  cpt += 1
1354  can_encode = True
1355  for i, f in to_decode[index:]:
1356  f.parent.l = cur_len
1357  ret = f.encode()
1358  if not ret:
1359  log.debug('cannot encode %r', f)
1360  can_encode = False
1361  break
1362  if f.value is not None and f.l:
1363  cur_len += f.l
1364  index += 1
1365  if ret is True:
1366  continue
1367 
1368  gcpt = 0
1369  for i in ret:
1370  gcpt += 1
1371  o = []
1372  if ((index, cur_len, [xx[1].value for xx in to_decode]) in todo or
1373  (index, cur_len, [xx[1].value for xx in to_decode]) in done):
1374  raise NotImplementedError('not fully functional')
1375 
1376  for p, f in to_decode:
1377  fnew = f.clone()
1378  o.append((p, fnew))
1379  todo.append((index, cur_len, o))
1380  can_encode = False
1381 
1382  break
1383  if not can_encode:
1384  continue
1385  result.append(to_decode)
1386 
1387  return self.decoded2bytes(result)

+ Here is the call graph for this function:

Member Data Documentation

int miasm2.core.cpu.cls_mn.alignment = 1
staticinherited

Definition at line 984 of file cpu.py.

list miasm2.arch.msp430.arch.mn_msp430.all_mn = []
static

Definition at line 217 of file arch.py.

tuple miasm2.arch.msp430.arch.mn_msp430.all_mn_inst = defaultdict(list)
static

Definition at line 225 of file arch.py.

tuple miasm2.arch.msp430.arch.mn_msp430.all_mn_mode = defaultdict(list)
static

Definition at line 223 of file arch.py.

tuple miasm2.arch.msp430.arch.mn_msp430.all_mn_name = defaultdict(list)
static

Definition at line 224 of file arch.py.

miasm2.core.cpu.cls_mn.args
inherited

Definition at line 1061 of file cpu.py.

list miasm2.core.cpu.cls_mn.args_symb = []
staticinherited

Definition at line 981 of file cpu.py.

miasm2.core.cpu.cls_mn.args_symb
inherited

Definition at line 1458 of file cpu.py.

dictionary miasm2.arch.msp430.arch.mn_msp430.bintree = {}
static

Definition at line 218 of file arch.py.

int miasm2.arch.msp430.arch.mn_msp430.delayslot = 0
static

Definition at line 220 of file arch.py.

miasm2.core.cpu.cls_mn.fields_order
inherited

Definition at line 1062 of file cpu.py.

miasm2.arch.msp430.arch.mn_msp430.instruction = instruction_msp430
static

Definition at line 226 of file arch.py.

int miasm2.arch.msp430.arch.mn_msp430.max_instruction_len = 8
static

Definition at line 227 of file arch.py.

string miasm2.arch.msp430.arch.mn_msp430.name = "msp430"
static

Definition at line 215 of file arch.py.

int miasm2.arch.msp430.arch.mn_msp430.num = 0
static

Definition at line 219 of file arch.py.

dictionary miasm2.arch.msp430.arch.mn_msp430.pc = {None: PC}
static

Definition at line 221 of file arch.py.

miasm2.arch.msp430.arch.mn_msp430.regs = regs_module
static

Definition at line 216 of file arch.py.

dictionary miasm2.arch.msp430.arch.mn_msp430.sp = {None: SP}
static

Definition at line 222 of file arch.py.

miasm2.core.cpu.cls_mn.to_decode
inherited

Definition at line 1063 of file cpu.py.


The documentation for this class was generated from the following file: