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

Public Member Functions

def __init__
 
def mod_pc
 
def get_ir
 
def expr_fix_regs_for_mode
 
def expraff_fix_regs_for_mode
 
def irbloc_fix_regs_for_mode
 
def instr2ir
 
def get_label
 
def get_bloc
 
def add_instr
 
def merge_multi_affect
 
def getby_offset
 
def gen_pc_update
 
def add_bloc
 
def expr_fix_regs_for_mode
 
def expraff_fix_regs_for_mode
 
def irbloc_fix_regs_for_mode
 
def is_pc_written
 
def set_empty_dst_to_next
 
def gen_edges
 
def post_add_bloc
 
def get_instr_label
 
def gen_label
 
def get_next_label
 
def simplify_blocs
 
def replace_expr_in_ir
 
def get_rw
 
def ExprIsLabel
 

Public Attributes

 do_stk_segm
 
 do_ds_segm
 
 do_str_segm
 
 do_all_segm
 
 pc
 
 sp
 
 IRDst
 
 symbol_pool
 
 blocs
 
 arch
 
 attrib
 

Detailed Description

Definition at line 3804 of file sem.py.

Constructor & Destructor Documentation

def miasm2.arch.x86.sem.ir_x86_16.__init__ (   self,
  symbol_pool = None 
)

Definition at line 3806 of file sem.py.

3807  def __init__(self, symbol_pool=None):
3808  ir.__init__(self, mn_x86, 16, symbol_pool)
3809  self.do_stk_segm = False
3810  self.do_ds_segm = False
3811  self.do_str_segm = False
3812  self.do_all_segm = False
3813  self.pc = IP
3814  self.sp = SP
3815  self.IRDst = m2_expr.ExprId('IRDst', 16)

Member Function Documentation

def miasm2.ir.ir.ir.add_bloc (   self,
  bloc,
  gen_pc_updt = False 
)
inherited

Definition at line 235 of file ir.py.

236  def add_bloc(self, bloc, gen_pc_updt = False):
237  c = None
238  ir_blocs_all = []
239  for l in bloc.lines:
240  if c is None:
241  label = self.get_instr_label(l)
242  c = irbloc(label, [], [])
243  ir_blocs_all.append(c)
244  ir_bloc_cur, ir_blocs_extra = self.instr2ir(l)
245 
246  if gen_pc_updt is not False:
247  self.gen_pc_update(c, l)
248 
249  c.irs.append(ir_bloc_cur)
250  c.lines.append(l)
251 
252 
253  if ir_blocs_extra:
254  for b in ir_blocs_extra:
255  b.lines = [l] * len(b.irs)
256  ir_blocs_all += ir_blocs_extra
257  c = None
258  self.post_add_bloc(bloc, ir_blocs_all)
259  return ir_blocs_all
def get_instr_label
Definition: ir.py:303
def instr2ir
Definition: ir.py:134
def gen_pc_update
Definition: ir.py:230
def add_bloc
Definition: ir.py:235
def post_add_bloc
Definition: ir.py:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.add_instr (   self,
  l,
  ad = 0,
  gen_pc_updt = False 
)
inherited

Definition at line 160 of file ir.py.

161  def add_instr(self, l, ad=0, gen_pc_updt = False):
162  b = asmbloc.asm_bloc(l)
163  b.lines = [l]
164  self.add_bloc(b, gen_pc_updt)
def add_instr
Definition: ir.py:160
def add_bloc
Definition: ir.py:235

+ Here is the call graph for this function:

def miasm2.ir.ir.ir.expr_fix_regs_for_mode (   self,
  e,
  args,
  kwargs 
)
inherited

Definition at line 260 of file ir.py.

261  def expr_fix_regs_for_mode(self, e, *args, **kwargs):
262  return e
def expr_fix_regs_for_mode
Definition: ir.py:260
def miasm2.arch.x86.sem.ir_x86_16.expr_fix_regs_for_mode (   self,
  e,
  mode = 64 
)

Definition at line 3898 of file sem.py.

3899  def expr_fix_regs_for_mode(self, e, mode=64):
3900  return e.replace_expr(replace_regs[mode])

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.expraff_fix_regs_for_mode (   self,
  e,
  args,
  kwargs 
)
inherited

Definition at line 263 of file ir.py.

264  def expraff_fix_regs_for_mode(self, e, *args, **kwargs):
265  return e
def expraff_fix_regs_for_mode
Definition: ir.py:263
def miasm2.arch.x86.sem.ir_x86_16.expraff_fix_regs_for_mode (   self,
  e,
  mode = 64 
)

Definition at line 3901 of file sem.py.

3902  def expraff_fix_regs_for_mode(self, e, mode=64):
3903  dst = self.expr_fix_regs_for_mode(e.dst, mode)
3904  src = self.expr_fix_regs_for_mode(e.src, mode)
3905  return m2_expr.ExprAff(dst, src)

+ Here is the call graph for this function:

def miasm2.ir.ir.ir.ExprIsLabel (   self,
  l 
)
inherited

Definition at line 337 of file ir.py.

338  def ExprIsLabel(self, l):
339  return isinstance(l, m2_expr.ExprId) and isinstance(l.name,
340  asmbloc.asm_label)
def ExprIsLabel
Definition: ir.py:337

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.gen_edges (   self,
  bloc,
  ir_blocs 
)
inherited

Definition at line 286 of file ir.py.

287  def gen_edges(self, bloc, ir_blocs):
288  pass
def gen_edges
Definition: ir.py:286

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.gen_label (   self)
inherited

Definition at line 309 of file ir.py.

310  def gen_label(self):
311  # TODO: fix hardcoded offset
312  l = self.symbol_pool.gen_label()
313  return l
def gen_label
Definition: ir.py:309

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.gen_pc_update (   self,
  c,
  l 
)
inherited

Definition at line 230 of file ir.py.

231  def gen_pc_update(self, c, l):
232  c.irs.append([m2_expr.ExprAff(self.pc, m2_expr.ExprInt_from(self.pc,
233  l.offset))])
234  c.lines.append(l)
def gen_pc_update
Definition: ir.py:230

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.get_bloc (   self,
  ad 
)
inherited
Returns the irbloc associated to an ExprId/ExprInt/label/int
@ad: an ExprId/ExprInt/label/int

Definition at line 153 of file ir.py.

154  def get_bloc(self, ad):
155  """Returns the irbloc associated to an ExprId/ExprInt/label/int
156  @ad: an ExprId/ExprInt/label/int"""
157 
158  label = self.get_label(ad)
159  return self.blocs.get(label, None)
def get_bloc
Definition: ir.py:153
def get_label
Definition: ir.py:138

+ Here is the call graph for this function:

def miasm2.ir.ir.ir.get_instr_label (   self,
  instr 
)
inherited
Returns the label associated to an instruction
@instr: current instruction

Definition at line 303 of file ir.py.

304  def get_instr_label(self, instr):
305  """Returns the label associated to an instruction
306  @instr: current instruction"""
307 
308  return self.symbol_pool.getby_offset_create(instr.offset)
def get_instr_label
Definition: ir.py:303

+ Here is the caller graph for this function:

def miasm2.arch.x86.sem.ir_x86_16.get_ir (   self,
  instr 
)

Definition at line 3819 of file sem.py.

3820  def get_ir(self, instr):
3821  args = instr.args[:]
3822  args = [arg.replace_expr(float_replace) for arg in args]
3823  my_ss = None
3824  if self.do_ds_segm:
3825  my_ss = DS
3826  if self.do_all_segm and instr.additional_info.g2.value:
3827  my_ss = {1: CS, 2: SS, 3: DS, 4: ES, 5: FS, 6: GS}[
3828  instr.additional_info.g2.value]
3829  if my_ss is not None:
3830  for i, a in enumerate(args):
3831  if isinstance(a, m2_expr.ExprMem) and not a.is_op_segm():
3832  args[i] = m2_expr.ExprMem(m2_expr.ExprOp('segm', my_ss,
3833  a.arg), a.size)
3834 
3835  if not instr.name.lower() in mnemo_func:
3836  raise NotImplementedError("Mnemonic %s not implemented" % instr.name)
3837 
3838  instr_ir, extra_ir = mnemo_func[
3839  instr.name.lower()](self, instr, *args)
3840  self.mod_pc(instr, instr_ir, extra_ir)
3841 
3842  self.mod_pc(instr, instr_ir, extra_ir)
3843  instr.additional_info.except_on_instr = False
3844  if instr.additional_info.g1.value & 6 == 0 or \
3845  not instr.name in repeat_mn:
3846  return instr_ir, extra_ir
3847  if instr.name == "MOVSD" and len(instr.args) == 2:
3848  return instr_ir, extra_ir
3849 
3850  instr.additional_info.except_on_instr = True
3851  # get instruction size
3852  s = {"B": 8, "W": 16, "D": 32, 'Q': 64}[instr.name[-1]]
3853  size = instr.v_opmode()
3854  c_reg = mRCX[instr.mode][:size]
3855  out_ir = []
3856  zf_val = None
3857  # set if zf is tested (cmps, scas)
3858  for e in instr_ir: # +[updt_c]:
3859  if e.dst == zf:
3860  zf_val = e.src
3861 
3862  cond_dec = m2_expr.ExprCond(c_reg - m2_expr.ExprInt_from(c_reg, 1),
3863  m2_expr.ExprInt1(0), m2_expr.ExprInt1(1))
3864  # end condition
3865  if zf_val is None:
3866  c_cond = cond_dec
3867  elif instr.additional_info.g1.value & 2: # REPNE
3868  c_cond = cond_dec | zf
3869  elif instr.additional_info.g1.value & 4: # REP
3870  c_cond = cond_dec | (zf ^ m2_expr.ExprInt1(1))
3871 
3872  # gen while
3873  lbl_do = m2_expr.ExprId(self.gen_label(), instr.mode)
3874  lbl_end = m2_expr.ExprId(self.gen_label(), instr.mode)
3875  lbl_skip = m2_expr.ExprId(self.get_next_label(instr), instr.mode)
3876  lbl_next = m2_expr.ExprId(self.get_next_label(instr), instr.mode)
3877 
3878  for b in extra_ir:
3879  for ir in b.irs:
3880  for i, e in enumerate(ir):
3881  src = e.src.replace_expr({lbl_next: lbl_end})
3882  ir[i] = m2_expr.ExprAff(e.dst, src)
3883  cond_bloc = []
3884  cond_bloc.append(m2_expr.ExprAff(c_reg,
3885  c_reg - m2_expr.ExprInt_from(c_reg,
3886  1)))
3887  cond_bloc.append(m2_expr.ExprAff(self.IRDst, m2_expr.ExprCond(c_cond,
3888  lbl_skip,
3889  lbl_do)))
3890  cond_bloc = irbloc(lbl_end.name, [cond_bloc])
3891  e_do = instr_ir
3892 
3893  c = irbloc(lbl_do.name, [e_do])
3894  c.except_automod = False
3895  e_n = [m2_expr.ExprAff(self.IRDst, m2_expr.ExprCond(c_reg, lbl_do,
3896  lbl_skip))]
3897  return e_n, [cond_bloc, c] + extra_ir
def gen_label
Definition: ir.py:309
def get_next_label
Definition: ir.py:314

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.get_label (   self,
  ad 
)
inherited
Transforms an ExprId/ExprInt/label/int into a label
@ad: an ExprId/ExprInt/label/int

Definition at line 138 of file ir.py.

139  def get_label(self, ad):
140  """Transforms an ExprId/ExprInt/label/int into a label
141  @ad: an ExprId/ExprInt/label/int"""
142 
143  if (isinstance(ad, m2_expr.ExprId) and
144  isinstance(ad.name, asmbloc.asm_label)):
145  ad = ad.name
146  if isinstance(ad, m2_expr.ExprInt):
147  ad = int(ad.arg)
148  if type(ad) in [int, long]:
149  ad = self.symbol_pool.getby_offset_create(ad)
150  elif isinstance(ad, asmbloc.asm_label):
151  ad = self.symbol_pool.getby_name_create(ad.name)
152  return ad
def get_label
Definition: ir.py:138

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.get_next_label (   self,
  instr 
)
inherited

Definition at line 314 of file ir.py.

315  def get_next_label(self, instr):
316  l = self.symbol_pool.getby_offset_create(instr.offset + instr.l)
317  return l
def get_next_label
Definition: ir.py:314

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.get_rw (   self,
  regs_ids = [] 
)
inherited
Calls get_rw(irb) for each bloc
@regs_ids : ids of registers used in IR

Definition at line 329 of file ir.py.

330  def get_rw(self, regs_ids = []):
331  """
332  Calls get_rw(irb) for each bloc
333  @regs_ids : ids of registers used in IR
334  """
335  for b in self.blocs.values():
336  b.get_rw(regs_ids)
def get_rw
Definition: ir.py:329

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.getby_offset (   self,
  offset 
)
inherited

Definition at line 222 of file ir.py.

223  def getby_offset(self, offset):
224  out = set()
225  for irb in self.blocs.values():
226  for l in irb.lines:
227  if l.offset <= offset < l.offset + l.l:
228  out.add(irb)
229  return out
def getby_offset
Definition: ir.py:222
def miasm2.ir.ir.ir.instr2ir (   self,
  l 
)
inherited

Definition at line 134 of file ir.py.

135  def instr2ir(self, l):
136  ir_bloc_cur, ir_blocs_extra = self.get_ir(l)
137  return ir_bloc_cur, ir_blocs_extra
def instr2ir
Definition: ir.py:134

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.irbloc_fix_regs_for_mode (   self,
  irbloc,
  args,
  kwargs 
)
inherited

Definition at line 266 of file ir.py.

267  def irbloc_fix_regs_for_mode(self, irbloc, *args, **kwargs):
268  return
def irbloc_fix_regs_for_mode
Definition: ir.py:266

+ Here is the caller graph for this function:

def miasm2.arch.x86.sem.ir_x86_16.irbloc_fix_regs_for_mode (   self,
  irbloc,
  mode = 64 
)

Definition at line 3906 of file sem.py.

3907  def irbloc_fix_regs_for_mode(self, irbloc, mode=64):
3908  for irs in irbloc.irs:
3909  for i, e in enumerate(irs):
3910  """
3911  special case for 64 bits:
3912  if destination is a 32 bit reg, zero extend the 64 bit reg
3913  """
3914  if mode == 64:
3915  if (isinstance(e.dst, m2_expr.ExprId) and \
3916  e.dst.size == 32 and \
3917  e.dst in replace_regs[64]):
3918  src = self.expr_fix_regs_for_mode(e.src, mode)
3919  dst = replace_regs[64][e.dst].arg
3920  e = m2_expr.ExprAff(dst, src.zeroExtend(64))
3921  irs[i] = self.expr_fix_regs_for_mode(e, mode)
3922  irbloc.dst = self.expr_fix_regs_for_mode(irbloc.dst, mode)
3923 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.is_pc_written (   self,
  b 
)
inherited

Definition at line 269 of file ir.py.

270  def is_pc_written(self, b):
271  all_pc = self.arch.pc.values()
272  for irs in b.irs:
273  for ir in irs:
274  if ir.dst in all_pc:
275  return ir
276  return None
def is_pc_written
Definition: ir.py:269
def miasm2.ir.ir.ir.merge_multi_affect (   self,
  affect_list 
)
inherited
If multiple affection to a same ExprId are present in @affect_list,
merge them (in place).
For instance, XCGH AH, AL semantic is
[
    RAX = {RAX[0:8],0,8, RAX[0:8],8,16, RAX[16:64],16,64}
    RAX = {RAX[8:16],0,8, RAX[8:64],8,64}
]
This function will update @affect_list to replace previous ExprAff by
[
    RAX = {RAX[8:16],0,8, RAX[0:8],8,16, RAX[16:64],16,64}
]

Definition at line 165 of file ir.py.

166  def merge_multi_affect(self, affect_list):
167  """
168  If multiple affection to a same ExprId are present in @affect_list,
169  merge them (in place).
170  For instance, XCGH AH, AL semantic is
171  [
172  RAX = {RAX[0:8],0,8, RAX[0:8],8,16, RAX[16:64],16,64}
173  RAX = {RAX[8:16],0,8, RAX[8:64],8,64}
174  ]
175  This function will update @affect_list to replace previous ExprAff by
176  [
177  RAX = {RAX[8:16],0,8, RAX[0:8],8,16, RAX[16:64],16,64}
178  ]
179  """
180 
181  # Extract side effect
182  effect = {}
183  for expr in affect_list:
184  effect[expr.dst] = effect.get(expr.dst, []) + [expr]
185 
186  # Find candidates
187  for dst, expr_list in effect.items():
188  if len(expr_list) <= 1:
189  continue
190 
191  # Only treat ExprCompose list
192  if any(map(lambda e: not(isinstance(e.src, m2_expr.ExprCompose)),
193  expr_list)):
194  continue
195 
196  # Find collision
197  e_colision = reduce(lambda x, y: x.union(y),
198  (e.get_modified_slice() for e in expr_list),
199  set())
200  # Sort interval collision
201  known_intervals = sorted([(x[1], x[2]) for x in e_colision])
202 
203  # Fill with missing data
204  missing_i = get_missing_interval(known_intervals, 0, dst.size)
205 
206  remaining = ((m2_expr.ExprSlice(dst, *interval),
207  interval[0],
208  interval[1])
209  for interval in missing_i)
210 
211  # Build the merging expression
212  slices = sorted(e_colision.union(remaining), key=lambda x: x[1])
213  final_dst = m2_expr.ExprCompose(slices)
214 
215  # Remove unused expression
216  for expr in expr_list:
217  affect_list.remove(expr)
218 
219  # Add the merged one
220  affect_list.append(m2_expr.ExprAff(dst, final_dst))
221 
def merge_multi_affect
Definition: ir.py:165

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.arch.x86.sem.ir_x86_16.mod_pc (   self,
  instr,
  instr_ir,
  extra_ir 
)

Definition at line 3816 of file sem.py.

3817  def mod_pc(self, instr, instr_ir, extra_ir):
3818  pass

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.post_add_bloc (   self,
  bloc,
  ir_blocs 
)
inherited

Definition at line 289 of file ir.py.

290  def post_add_bloc(self, bloc, ir_blocs):
291  self.set_empty_dst_to_next(bloc, ir_blocs)
292  self.gen_edges(bloc, ir_blocs)
293 
294  for irb in ir_blocs:
295  self.irbloc_fix_regs_for_mode(irb, self.attrib)
296 
297  # Detect multi-affectation
298  for affect_list in irb.irs:
299  self.merge_multi_affect(affect_list)
300 
301  self.blocs[irb.label] = irb
302 
def set_empty_dst_to_next
Definition: ir.py:277
def irbloc_fix_regs_for_mode
Definition: ir.py:266
def gen_edges
Definition: ir.py:286
def merge_multi_affect
Definition: ir.py:165
def post_add_bloc
Definition: ir.py:289

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.replace_expr_in_ir (   self,
  bloc,
  rep 
)
inherited

Definition at line 324 of file ir.py.

325  def replace_expr_in_ir(self, bloc, rep):
326  for irs in bloc.irs:
327  for i, l in enumerate(irs):
328  irs[i] = l.replace_expr(rep)
def replace_expr_in_ir
Definition: ir.py:324
def miasm2.ir.ir.ir.set_empty_dst_to_next (   self,
  bloc,
  ir_blocs 
)
inherited

Definition at line 277 of file ir.py.

278  def set_empty_dst_to_next(self, bloc, ir_blocs):
279  for b in ir_blocs:
280  if b.dst is not None:
281  continue
282  dst = m2_expr.ExprId(self.get_next_label(bloc.lines[-1]),
283  self.pc.size)
284  b.irs.append([m2_expr.ExprAff(self.IRDst, dst)])
285  b.lines.append(b.lines[-1])
def set_empty_dst_to_next
Definition: ir.py:277
def get_next_label
Definition: ir.py:314

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.ir.ir.simplify_blocs (   self)
inherited

Definition at line 318 of file ir.py.

319  def simplify_blocs(self):
320  for b in self.blocs.values():
321  for ir in b.irs:
322  for i, r in enumerate(ir):
323  ir[i] = m2_expr.ExprAff(expr_simp(r.dst), expr_simp(r.src))
def simplify_blocs
Definition: ir.py:318

+ Here is the caller graph for this function:

Member Data Documentation

miasm2.ir.ir.ir.arch
inherited

Definition at line 131 of file ir.py.

miasm2.ir.ir.ir.attrib
inherited

Definition at line 132 of file ir.py.

miasm2.ir.ir.ir.blocs
inherited

Definition at line 128 of file ir.py.

miasm2.arch.x86.sem.ir_x86_16.do_all_segm

Definition at line 3811 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.do_ds_segm

Definition at line 3809 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.do_stk_segm

Definition at line 3808 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.do_str_segm

Definition at line 3810 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.IRDst

Definition at line 3814 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.pc

Definition at line 3812 of file sem.py.

miasm2.arch.x86.sem.ir_x86_16.sp

Definition at line 3813 of file sem.py.

miasm2.ir.ir.ir.symbol_pool
inherited

Definition at line 127 of file ir.py.


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