From 86e9499be55b85b386dab3257e2c1946e094afb6 Mon Sep 17 00:00:00 2001 From: Camille Mougey Date: Wed, 18 Feb 2015 11:04:59 +0100 Subject: Core/ParseASM: Remove wildcard import and too long lines --- miasm2/core/parse_asm.py | 61 +++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) (limited to 'miasm2/core/parse_asm.py') diff --git a/miasm2/core/parse_asm.py b/miasm2/core/parse_asm.py index dba097fa..ba2769e6 100644 --- a/miasm2/core/parse_asm.py +++ b/miasm2/core/parse_asm.py @@ -1,11 +1,12 @@ #!/usr/bin/env python #-*- coding:utf-8 -*- - import re + import miasm2.expression.expression as m2_expr -from miasm2.core.asmbloc import * +import miasm2.core.asmbloc as asmbloc from miasm2.core.utils import pck from miasm2.core.cpu import gen_base_expr, parse_ast + declarator = {'byte': 8, 'word': 16, 'dword': 32, @@ -33,12 +34,15 @@ def guess_next_new_label(symbol_pool, gen_label_index=0): def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): if symbol_pool is None: - symbol_pool = asm_symbol_pool() + symbol_pool = asmbloc.asm_symbol_pool() lines_text = [] lines_data = [] lines_bss = [] + C_NEXT = asmbloc.asm_constraint.c_next + C_TO = asmbloc.asm_constraint.c_to + lines = lines_text # parse each line for line in txt.split('\n'): @@ -79,7 +83,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): raw = raw.decode('string_escape') if directive == 'string': raw += "\x00" - lines.append(asm_raw(raw)) + lines.append(asmbloc.asm_raw(raw)) continue if directive == 'ustring': # XXX HACK @@ -87,7 +91,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): raw = line[line.find(r'"') + 1:line.rfind(r"'")] + "\x00" raw = raw.decode('string_escape') raw = "".join(map(lambda x: x + '\x00', raw)) - lines.append(asm_raw(raw)) + lines.append(asmbloc.asm_raw(raw)) continue if directive in declarator: data_raw = line[r.end():].split(' ', 1)[1] @@ -99,7 +103,8 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): # parser variable, operand, base_expr = gen_base_expr() my_var_parser = parse_ast(lambda x:m2_expr.ExprId(x, size), - lambda x:m2_expr.ExprInt_fromsize(size, x)) + lambda x: + m2_expr.ExprInt_fromsize(size, x)) base_expr.setParseAction(my_var_parser) for b in data_raw: @@ -108,7 +113,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): data_int.append(x.canonize()) p = size2pck[size] raw = data_int - x = asm_raw(raw) + x = asmbloc.asm_raw(raw) x.element_size = size lines.append(x) continue @@ -116,12 +121,12 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): # TODO continue if directive == 'split': # custom command - x = asm_raw() + x = asmbloc.asm_raw() x.split = True lines.append(x) continue if directive == 'dontsplit': # custom command - lines.append(asm_raw(line.strip())) + lines.append(asmbloc.asm_raw(line.strip())) continue if directive in ['file', 'intel_syntax', 'globl', 'local', 'type', 'size', 'align', 'ident', 'section']: @@ -148,7 +153,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): instr.dstflow2label(symbol_pool) lines.append(instr) - log_asmbloc.info("___pre asm oki___") + asmbloc.log_asmbloc.info("___pre asm oki___") # make blocs # gen_label_index = 0 @@ -165,12 +170,12 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): # print 'DEAL', lines[i], state # no current bloc if state == 0: - if not isinstance(lines[i], asm_label): + if not isinstance(lines[i], asmbloc.asm_label): l = guess_next_new_label(symbol_pool) lines[i:i] = [l] else: l = lines[i] - b = asm_bloc(l) + b = asmbloc.asm_bloc(l) b.bloc_num = bloc_num bloc_num += 1 blocs.append(b) @@ -178,14 +183,14 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): i += 1 if bloc_to_nlink: # print 'nlink!' - bloc_to_nlink.addto( - asm_constraint(b.label, asm_constraint.c_next)) + bloc_to_nlink.addto(asmbloc.asm_constraint(b.label, + C_NEXT)) bloc_to_nlink = None # in bloc elif state == 1: - # asm_raw - if isinstance(lines[i], asm_raw): + # asmbloc.asm_raw + if isinstance(lines[i], asmbloc.asm_raw): if hasattr(lines[i], 'split'): state = 0 block_may_link = False @@ -202,41 +207,29 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): b.addline(lines[i]) i += 1 """ - # asm_label - elif isinstance(lines[i], asm_label): + # asmbloc.asm_label + elif isinstance(lines[i], asmbloc.asm_label): if block_may_link: # print 'nlink!' b.addto( - asm_constraint(lines[i], asm_constraint.c_next)) + asmbloc.asm_constraint(lines[i], C_NEXT)) block_may_link = False state = 0 # instruction else: b.addline(lines[i]) if lines[i].dstflow(): - ''' - mydst = lines[i].args - if len(mydst)==1 and mnemo.get_symbols(mydst[0]): - arg = dict(mydst[0]) - symbs = mnemo.get_symbols(arg) - """ - TODO XXX redo this (as many miasm parts) - """ - l = symbs[0][0] - lines[i].setdstflow([l]) - b.addto(asm_constraint(l, asm_constraint.c_to)) - ''' for x in lines[i].getdstflow(symbol_pool): if not isinstance(x, m2_expr.ExprId): continue if x in mnemo.regs.all_regs_ids: continue - b.addto(asm_constraint(x, asm_constraint.c_to)) + b.addto(asmbloc.asm_constraint(x, C_TO)) # TODO XXX redo this really if not lines[i].breakflow() and i + 1 < len(lines): - if isinstance(lines[i + 1], asm_label): + if isinstance(lines[i + 1], asmbloc.asm_label): l = lines[i + 1] else: l = guess_next_new_label(symbol_pool) @@ -254,6 +247,6 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): i += 1 for b in blocs_sections[0]: - log_asmbloc.info(b) + asmbloc.log_asmbloc.info(b) return blocs_sections, symbol_pool -- cgit 1.4.1 From 1a4d3a0c58da4291fc640aaf9e636a6e02c16145 Mon Sep 17 00:00:00 2001 From: Camille Mougey Date: Wed, 18 Feb 2015 12:31:41 +0100 Subject: Core/ParseASM: Remove some warning |invalid-name |1 | +---------------------------+------------+ |undefined-loop-variable |5 | +---------------------------+------------+ |unused-variable |4 | +---------------------------+------------+ |unused-import |1 | +---------------------------+------------+ |deprecated-lambda |1 | +---------------------------+------------+ |bad-builtin |1 | --- miasm2/core/parse_asm.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'miasm2/core/parse_asm.py') diff --git a/miasm2/core/parse_asm.py b/miasm2/core/parse_asm.py index ba2769e6..414bfedf 100644 --- a/miasm2/core/parse_asm.py +++ b/miasm2/core/parse_asm.py @@ -4,7 +4,6 @@ import re import miasm2.expression.expression as m2_expr import miasm2.core.asmbloc as asmbloc -from miasm2.core.utils import pck from miasm2.core.cpu import gen_base_expr, parse_ast declarator = {'byte': 8, @@ -90,7 +89,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): line = line.replace(r'\n', '\n').replace(r'\r', '\r') raw = line[line.find(r'"') + 1:line.rfind(r"'")] + "\x00" raw = raw.decode('string_escape') - raw = "".join(map(lambda x: x + '\x00', raw)) + raw = "".join([string + '\x00' for string in raw]) lines.append(asmbloc.asm_raw(raw)) continue if directive in declarator: @@ -98,10 +97,9 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): data_raw = data_raw.split(',') size = declarator[directive] data_int = [] - has_symb = False # parser - variable, operand, base_expr = gen_base_expr() + base_expr = gen_base_expr()[2] my_var_parser = parse_ast(lambda x:m2_expr.ExprId(x, size), lambda x: m2_expr.ExprInt_fromsize(size, x)) @@ -111,7 +109,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): b = b.strip() x = base_expr.parseString(b)[0] data_int.append(x.canonize()) - p = size2pck[size] + raw = data_int x = asmbloc.asm_raw(raw) x.element_size = size @@ -159,6 +157,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): blocs_sections = [] bloc_num = 0 + b = None for lines in [lines_text, lines_data, lines_bss]: state = 0 i = 0 @@ -246,7 +245,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): i += 1 - for b in blocs_sections[0]: - asmbloc.log_asmbloc.info(b) + for block in blocs_sections[0]: + asmbloc.log_asmbloc.info(block) return blocs_sections, symbol_pool -- cgit 1.4.1 From d7b6cd2f8a992b56bec4a8aa60f1cdf99a560583 Mon Sep 17 00:00:00 2001 From: Camille Mougey Date: Wed, 18 Feb 2015 13:55:41 +0100 Subject: Core: General PyLinting --- miasm2/core/asmbloc.py | 52 ++++++++++++++++++++++-------------------------- miasm2/core/cpu.py | 4 +++- miasm2/core/parse_asm.py | 16 ++------------- 3 files changed, 29 insertions(+), 43 deletions(-) (limited to 'miasm2/core/parse_asm.py') diff --git a/miasm2/core/asmbloc.py b/miasm2/core/asmbloc.py index cb8423f6..4770b597 100644 --- a/miasm2/core/asmbloc.py +++ b/miasm2/core/asmbloc.py @@ -70,7 +70,7 @@ class asm_label: def __eq__(self, a): if isinstance(a, asm_label): - return self._hash == a._hash + return self._hash == hash(a) else: return False @@ -154,7 +154,7 @@ class asm_bloc: self.lines.append(l) def addto(self, c): - assert(type(self.bto) is set) + assert type(self.bto) is set self.bto.add(c) def split(self, offset, l): @@ -274,21 +274,21 @@ class asm_symbol_pool: """ if isinstance(obj, asm_label): if obj.name in self.s: - del(self.s[obj.name]) + del self.s[obj.name] if obj.offset is not None and obj.offset in self.s_offset: - del(self.s_offset[obj.offset]) + del self.s_offset[obj.offset] else: offset = int(obj) if offset in self.s_offset: obj = self.s_offset[offset] - del(self.s_offset[offset]) + del self.s_offset[offset] if obj.name in self.s: - del(self.s[obj.name]) + del self.s[obj.name] def del_offset(self, l=None): if l is not None: if l.offset in self.s_offset: - del(self.s_offset[l.offset]) + del self.s_offset[l.offset] l.offset = None else: self.s_offset = {} @@ -317,7 +317,7 @@ class asm_symbol_pool: if not s.name in self.s: log_asmbloc.warn('unk symb') return - del(self.s[s.name]) + del self.s[s.name] s.name = newname self.s[s.name] = s @@ -327,7 +327,7 @@ class asm_symbol_pool: if not label.name in self.s: raise ValueError('label %s not in symbol pool' % label) if not isinstance(label.offset, list) and label.offset in self.s_offset: - del(self.s_offset[label.offset]) + del self.s_offset[label.offset] label.offset = offset if not isinstance(label.offset, list): self.s_offset[label.offset] = label @@ -440,7 +440,8 @@ def dis_bloc(mnemo, pool_bin, cur_bloc, offset, job_done, symbol_pool, dst = instr.getdstflow(symbol_pool) dstn = [] for d in dst: - if isinstance(d, m2_expr.ExprId) and isinstance(d.name, asm_label): + if isinstance(d, m2_expr.ExprId) and \ + isinstance(d.name, asm_label): dstn.append(d.name) dst = dstn if (not instr.is_subcall()) or follow_call: @@ -699,7 +700,7 @@ def group_blocs(blocs): continue if c.label in groups_bloc: b += groups_bloc[c.label] - del(groups_bloc[c.label]) + del groups_bloc[c.label] groups_bloc[b[0].label] = b found_in_group = True break @@ -784,7 +785,8 @@ def gen_non_free_mapping(group_bloc, dont_erase=[]): return non_free_mapping -def resolve_symbol(group_bloc, symbol_pool, dont_erase=[], max_offset=0xFFFFFFFF): +def resolve_symbol(group_bloc, symbol_pool, dont_erase=[], + max_offset=0xFFFFFFFF): """ place all asmblocs """ @@ -824,7 +826,7 @@ def resolve_symbol(group_bloc, symbol_pool, dont_erase=[], max_offset=0xFFFFFFFF log_asmbloc.debug( "consumed %d rest: %d" % (g.total_max_l, int(tmp))) free_interval[g] = tmp - del(free_interval[x]) + del free_interval[x] symbol_pool.set_offset( g, [group_bloc[x][-1].label, group_bloc[x][-1], 1]) g.fixedblocs = True @@ -853,7 +855,7 @@ def resolve_symbol(group_bloc, symbol_pool, dont_erase=[], max_offset=0xFFFFFFFF log_asmbloc.debug( "consumed %d rest: %d" % (g.total_max_l, int(tmp))) free_interval[g] = tmp - del(free_interval[k]) + del free_interval[k] g.fixedblocs = True break @@ -910,8 +912,6 @@ def calc_symbol_offset(symbol_pool): for label in symbol_pool.items(): if label.offset is None: - # raise ValueError("symbol missing?", label) - #print "symbol missing?? %s" % label label.offset_g = None continue if not is_int(label.offset): @@ -935,7 +935,8 @@ def calc_symbol_offset(symbol_pool): s_to_use.add(l) -def asmbloc_final(mnemo, blocs, symbol_pool, symb_reloc_off=None, conservative = False): +def asmbloc_final(mnemo, blocs, symbol_pool, symb_reloc_off=None, + conservative=False): log_asmbloc.info("asmbloc_final") if symb_reloc_off is None: symb_reloc_off = {} @@ -954,7 +955,6 @@ def asmbloc_final(mnemo, blocs, symbol_pool, symb_reloc_off=None, conservative = symbols = asm_symbol_pool() for s, v in symbol_pool.s.items(): symbols.add_label(s, v.offset_g) - # print symbols # test if bad encoded relative for bloc in blocs: @@ -986,7 +986,6 @@ def asmbloc_final(mnemo, blocs, symbol_pool, symb_reloc_off=None, conservative = c, candidates = conservative_asm( mnemo, instr, symbol_reloc_off, conservative) - # print candidates for i, e in enumerate(sav_a): instr.args[i] = e @@ -1016,12 +1015,10 @@ def asmbloc_final(mnemo, blocs, symbol_pool, symb_reloc_off=None, conservative = if my_s is not None: my_symb_reloc_off[bloc.label].append(offset_i + my_s) offset_i += instr.l - assert(len(instr.data) == instr.l) + assert len(instr.data) == instr.l # we have fixed all relative values # recompute good offsets for label in symbol_pool.items(): - # if label.offset_g is None: - # fdfd symbol_pool.set_offset(label, label.offset_g) for a, b in my_symb_reloc_off.items(): @@ -1044,7 +1041,7 @@ def asm_resolve_final(mnemo, blocs, symbol_pool, dont_erase=[], for bloc in resolved_b: offset = bloc.label.offset for line in bloc.lines: - assert(line.data is not None) + assert line.data is not None patches[offset] = line.data for cur_pos in xrange(line.l): if offset + cur_pos in written_bytes: @@ -1232,10 +1229,9 @@ def bloc_merge(blocs, dont_merge=[]): # and parent has only one son if len(bp.bto) != 1: continue - """ - and will not create next loop composed of constraint_next from son to - parent - """ + # and will not create next loop composed of constraint_next from son to + # parent + path = bloc_find_path_next(blocs, blocby_label, b, bp) if path: continue @@ -1266,7 +1262,7 @@ def bloc_merge(blocs, dont_merge=[]): bp.lines += b.lines bp.bto = b.bto - del(blocs[i]) + del blocs[i] i = -1 diff --git a/miasm2/core/cpu.py b/miasm2/core/cpu.py index cb1b56cc..f8fdb4ff 100644 --- a/miasm2/core/cpu.py +++ b/miasm2/core/cpu.py @@ -1132,7 +1132,9 @@ class cls_mn(object): total_l += l f.l = l f.is_present = True - log.debug("FIELD", f.__class__, f.fname, offset_b, l) + log.debug("FIELD %s %s %s %s" % (f.__class__, + f.fname, + offset_b, l)) if bs_l * 8 - offset_b < l: getok = False break diff --git a/miasm2/core/parse_asm.py b/miasm2/core/parse_asm.py index 414bfedf..09003ed9 100644 --- a/miasm2/core/parse_asm.py +++ b/miasm2/core/parse_asm.py @@ -100,7 +100,7 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): # parser base_expr = gen_base_expr()[2] - my_var_parser = parse_ast(lambda x:m2_expr.ExprId(x, size), + my_var_parser = parse_ast(lambda x: m2_expr.ExprId(x, size), lambda x: m2_expr.ExprInt_fromsize(size, x)) base_expr.setParseAction(my_var_parser) @@ -153,7 +153,6 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): asmbloc.log_asmbloc.info("___pre asm oki___") # make blocs - # gen_label_index = 0 blocs_sections = [] bloc_num = 0 @@ -166,7 +165,6 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): bloc_to_nlink = None block_may_link = False while i < len(lines): - # print 'DEAL', lines[i], state # no current bloc if state == 0: if not isinstance(lines[i], asmbloc.asm_label): @@ -181,35 +179,25 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0): state = 1 i += 1 if bloc_to_nlink: - # print 'nlink!' bloc_to_nlink.addto(asmbloc.asm_constraint(b.label, C_NEXT)) bloc_to_nlink = None # in bloc elif state == 1: - # asmbloc.asm_raw if isinstance(lines[i], asmbloc.asm_raw): if hasattr(lines[i], 'split'): state = 0 block_may_link = False i += 1 - else: #if lines[i].raw.startswith('.dontsplit'): - # raw asm are link by default - # print 'dontsplit' + else: state = 1 block_may_link = True b.addline(lines[i]) i += 1 - """ - else: - b.addline(lines[i]) - i += 1 - """ # asmbloc.asm_label elif isinstance(lines[i], asmbloc.asm_label): if block_may_link: - # print 'nlink!' b.addto( asmbloc.asm_constraint(lines[i], C_NEXT)) block_may_link = False -- cgit 1.4.1