diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2020-08-22 12:47:01 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2020-08-31 07:50:01 +0200 |
| commit | 80e40a3d2ca735db955807ad0605b43ca22e4e35 (patch) | |
| tree | 4d41d7b53565f833444d3520eb22eed3e8bf26f1 /miasm/core | |
| parent | 5d8beb271d9890241a6d61dd476fab26ca37ebbf (diff) | |
| download | focaccia-miasm-80e40a3d2ca735db955807ad0605b43ca22e4e35.tar.gz focaccia-miasm-80e40a3d2ca735db955807ad0605b43ca22e4e35.zip | |
Avoid generate default locationdb
Diffstat (limited to 'miasm/core')
| -rw-r--r-- | miasm/core/asmblock.py | 24 | ||||
| -rw-r--r-- | miasm/core/cpu.py | 19 | ||||
| -rw-r--r-- | miasm/core/parse_asm.py | 17 |
3 files changed, 26 insertions, 34 deletions
diff --git a/miasm/core/asmblock.py b/miasm/core/asmblock.py index 975f93c5..e293ffda 100644 --- a/miasm/core/asmblock.py +++ b/miasm/core/asmblock.py @@ -15,7 +15,6 @@ from miasm.expression.simplifications import expr_simp from miasm.core.utils import Disasm_Exception, pck from miasm.core.graph import DiGraph, DiGraphSimplifier, MatchGraphJoker from miasm.core.interval import interval -from miasm.core.locationdb import LocationDB log_asmblock = logging.getLogger("asmblock") @@ -81,11 +80,12 @@ class AsmConstraintTo(AsmConstraint): class AsmBlock(object): - def __init__(self, loc_key, alignment=1): + def __init__(self, loc_db, loc_key, alignment=1): assert isinstance(loc_key, LocKey) self.bto = set() self.lines = [] + self.loc_db = loc_db self._loc_key = loc_key self.alignment = alignment @@ -132,7 +132,7 @@ class AsmBlock(object): 'middle instruction? default middle') offsets.sort() return None - new_block = AsmBlock(loc_key) + new_block = AsmBlock(loc_db, loc_key) i = offsets.index(offset) self.lines, new_block.lines = self.lines[:i], self.lines[i:] @@ -263,12 +263,12 @@ class AsmBlockBad(AsmBlock): ERROR_IO: "IOError", } - def __init__(self, loc_key=None, alignment=1, errno=ERROR_UNKNOWN, *args, **kwargs): + def __init__(self, loc_db, loc_key=None, alignment=1, errno=ERROR_UNKNOWN, *args, **kwargs): """Instantiate an AsmBlock_bad. @loc_key, @alignment: same as AsmBlock.__init__ @errno: (optional) specify a error type associated with the block """ - super(AsmBlockBad, self).__init__(loc_key, alignment, *args, **kwargs) + super(AsmBlockBad, self).__init__(loc_db, loc_key, alignment, *args, **kwargs) self._errno = errno errno = property(lambda self: self._errno) @@ -307,7 +307,7 @@ class AsmCFG(DiGraph): AsmCFGPending = namedtuple("AsmCFGPending", ["waiter", "constraint"]) - def __init__(self, loc_db=None, *args, **kwargs): + def __init__(self, loc_db, *args, **kwargs): super(AsmCFG, self).__init__(*args, **kwargs) # Edges -> constraint self.edges2constraint = {} @@ -1196,7 +1196,7 @@ class disasmEngine(object): - dis_block_callback: callback after each new disassembled block """ - def __init__(self, arch, attrib, bin_stream, **kwargs): + def __init__(self, arch, attrib, bin_stream, loc_db, **kwargs): """Instantiate a new disassembly engine @arch: targeted architecture @attrib: architecture attribute @@ -1206,7 +1206,7 @@ class disasmEngine(object): self.arch = arch self.attrib = attrib self.bin_stream = bin_stream - self.loc_db = LocationDB() + self.loc_db = loc_db # Setup options self.dont_dis = [] @@ -1236,7 +1236,7 @@ class disasmEngine(object): offsets_to_dis = set() add_next_offset = False loc_key = self.loc_db.get_or_create_offset_location(offset) - cur_block = AsmBlock(loc_key) + cur_block = AsmBlock(self.loc_db, loc_key) log_asmblock.debug("dis at %X", int(offset)) while not in_delayslot or delayslot_count > 0: if in_delayslot: @@ -1246,7 +1246,7 @@ class disasmEngine(object): if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=AsmBlockBad.ERROR_FORBIDDEN) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=AsmBlockBad.ERROR_FORBIDDEN) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1289,7 +1289,7 @@ class disasmEngine(object): if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=error) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=error) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1303,7 +1303,7 @@ class disasmEngine(object): instr.b.count(b'\x00') == instr.l): log_asmblock.warning("reach nul instr at %X", int(off_i)) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=AsmBlockBad.ERROR_NULL_STARTING_BLOCK) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=AsmBlockBad.ERROR_NULL_STARTING_BLOCK) break # special case: flow graph modificator in delayslot diff --git a/miasm/core/cpu.py b/miasm/core/cpu.py index aee22c97..f509c9c9 100644 --- a/miasm/core/cpu.py +++ b/miasm/core/cpu.py @@ -16,7 +16,6 @@ import miasm.expression.expression as m2_expr from miasm.core.bin_stream import bin_stream, bin_stream_str from miasm.core.utils import Disasm_Exception from miasm.expression.simplifications import expr_simp -from miasm.core.locationdb import LocationDB from miasm.core.asm_ast import AstNode, AstInt, AstId, AstOp @@ -1016,17 +1015,15 @@ class instruction(object): def get_asm_next_offset(self, expr): return m2_expr.ExprInt(self.offset+self.l, expr.size) - def resolve_args_with_symbols(self, symbols=None): - if symbols is None: - symbols = LocationDB() + def resolve_args_with_symbols(self, loc_db): args_out = [] for expr in self.args: - # try to resolve symbols using symbols (0 for default value) + # try to resolve symbols using loc_db (0 for default value) loc_keys = m2_expr.get_expr_locs(expr) fixed_expr = {} for exprloc in loc_keys: loc_key = exprloc.loc_key - names = symbols.get_location_names(loc_key) + names = loc_db.get_location_names(loc_key) # special symbols if b'$' in names: fixed_expr[exprloc] = self.get_asm_offset(exprloc) @@ -1034,14 +1031,14 @@ class instruction(object): if b'_' in names: fixed_expr[exprloc] = self.get_asm_next_offset(exprloc) continue - arg_int = symbols.get_location_offset(loc_key) + arg_int = loc_db.get_location_offset(loc_key) if arg_int is not None: fixed_expr[exprloc] = m2_expr.ExprInt(arg_int, exprloc.size) continue if not names: raise ValueError('Unresolved symbol: %r' % exprloc) - offset = symbols.get_location_offset(loc_key) + offset = loc_db.get_location_offset(loc_key) if offset is None: raise ValueError( 'The offset of loc_key "%s" cannot be determined' % names @@ -1050,7 +1047,7 @@ class instruction(object): # Fix symbol with its offset size = exprloc.size if size is None: - default_size = self.get_symbol_size(exprloc, symbols) + default_size = self.get_symbol_size(exprloc, loc_db) size = default_size value = m2_expr.ExprInt(offset, size) fixed_expr[exprloc] = value @@ -1383,7 +1380,7 @@ class cls_mn(with_metaclass(metamn, object)): yield c @classmethod - def asm(cls, instr, symbols=None): + def asm(cls, instr, loc_db=None): """ Re asm instruction by searching mnemo using name and args. We then can modify args and get the hex of a modified instruction @@ -1392,7 +1389,7 @@ class cls_mn(with_metaclass(metamn, object)): clist = [x for x in clist] vals = [] candidates = [] - args = instr.resolve_args_with_symbols(symbols) + args = instr.resolve_args_with_symbols(loc_db) for cc in clist: diff --git a/miasm/core/parse_asm.py b/miasm/core/parse_asm.py index b742a2d2..509f0483 100644 --- a/miasm/core/parse_asm.py +++ b/miasm/core/parse_asm.py @@ -88,21 +88,16 @@ def asm_ast_to_expr_with_size(arg, loc_db, size): return ExprInt(arg.value, size) return None -def parse_txt(mnemo, attrib, txt, loc_db=None): - """Parse an assembly listing. Returns a couple (asmcfg, loc_db), where - asmcfg is an AsmCfg instance and loc_db the associated LocationDB +def parse_txt(mnemo, attrib, txt, loc_db): + """Parse an assembly listing. Returns an AsmCfg instance @mnemo: architecture used @attrib: architecture attribute @txt: assembly listing - @loc_db: (optional) the LocationDB instance used to handle labels - of the listing + @loc_db: the LocationDB instance used to handle labels of the listing """ - if loc_db is None: - loc_db = asmblock.LocationDB() - C_NEXT = asmblock.AsmConstraint.c_next C_TO = asmblock.AsmConstraint.c_to @@ -233,9 +228,9 @@ def parse_txt(mnemo, attrib, txt, loc_db=None): # First line must be a label. If it's not the case, generate # it. loc = guess_next_new_label(loc_db) - cur_block = asmblock.AsmBlock(loc, alignment=mnemo.alignment) + cur_block = asmblock.AsmBlock(loc_db, loc, alignment=mnemo.alignment) else: - cur_block = asmblock.AsmBlock(line, alignment=mnemo.alignment) + cur_block = asmblock.AsmBlock(loc_db, line, alignment=mnemo.alignment) i += 1 # Generate the current block asmcfg.add_block(cur_block) @@ -302,4 +297,4 @@ def parse_txt(mnemo, attrib, txt, loc_db=None): # Log block asmblock.log_asmblock.info(block) - return asmcfg, loc_db + return asmcfg |