about summary refs log tree commit diff stats
path: root/miasm2/core/parse_asm.py
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2015-03-27 14:50:11 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2015-04-01 23:47:37 +0200
commitb148652ebd8e8b02aa9514cc3d21098c7002d694 (patch)
tree7dfa95414e79b5d84c2c89d25d96ea6dfdca1182 /miasm2/core/parse_asm.py
parentf9c49e92dada2aa51ca594f435f962617796c116 (diff)
downloadmiasm-b148652ebd8e8b02aa9514cc3d21098c7002d694.tar.gz
miasm-b148652ebd8e8b02aa9514cc3d21098c7002d694.zip
Parse_asm: generate asm_label on symbol parsing
For an unknown symbol, instead of generating ExprId('toto'), it will generate
ExprId(asm_label('toto')). As label is generated in the architecture, this label
must be catched in the parse_asm module to be inserted in the current
symbol_pool.
Diffstat (limited to 'miasm2/core/parse_asm.py')
-rw-r--r--miasm2/core/parse_asm.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/miasm2/core/parse_asm.py b/miasm2/core/parse_asm.py
index b42bdbcc..6bec9651 100644
--- a/miasm2/core/parse_asm.py
+++ b/miasm2/core/parse_asm.py
@@ -30,6 +30,25 @@ def guess_next_new_label(symbol_pool, gen_label_index=0):
             return symbol_pool.add_label(name)
         i += 1
 
+def replace_expr_labels(e, symbol_pool, replace_id):
+    if not isinstance(e, m2_expr.ExprId):
+        return e
+    if not isinstance(e.name, asmbloc.asm_label):
+        return e
+    old_lbl = e.name
+    new_lbl = symbol_pool.getby_name_create(old_lbl.name)
+    replace_id[e] = m2_expr.ExprId(new_lbl, e.size)
+    return m2_expr.ExprId(new_lbl, e.size)
+
+def replace_orphan_labels(instr, symbol_pool):
+    for i, arg in enumerate(instr.args):
+        replace_id = {}
+        arg.visit(lambda e:replace_expr_labels(e,
+                                               symbol_pool,
+                                               replace_id))
+        instr.args[i] = instr.args[i].replace_expr(replace_id)
+
+
 
 def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0):
     if symbol_pool is None:
@@ -147,6 +166,10 @@ def parse_txt(mnemo, attrib, txt, symbol_pool=None, gen_label_index=0):
             line = line[:line.find(';')]
         line = line.strip(' ').strip('\t')
         instr = mnemo.fromstring(line, attrib)
+
+        # replace orphan asm_label with labels from symbol_pool
+        replace_orphan_labels(instr, symbol_pool)
+
         if instr.dstflow():
             instr.dstflow2label(symbol_pool)
         lines.append(instr)