about summary refs log tree commit diff stats
path: root/miasm/core/parse_asm.py
diff options
context:
space:
mode:
authorserpilliere <devnull@localhost>2012-12-12 16:05:53 +0100
committerserpilliere <devnull@localhost>2012-12-12 16:05:53 +0100
commit10141478337fcc679c387e16d245b47cfc592e77 (patch)
treebaae9f82ba224f0509718cdf2df368b837887c52 /miasm/core/parse_asm.py
parent688ef20af335c6829a4156d0d37d3ad98b55ff21 (diff)
downloadfocaccia-miasm-10141478337fcc679c387e16d245b47cfc592e77.tar.gz
focaccia-miasm-10141478337fcc679c387e16d245b47cfc592e77.zip
enhanced parser for gcc -S masm=intel (louis granboulan)
Diffstat (limited to 'miasm/core/parse_asm.py')
-rw-r--r--miasm/core/parse_asm.py54
1 files changed, 44 insertions, 10 deletions
diff --git a/miasm/core/parse_asm.py b/miasm/core/parse_asm.py
index bec9e366..c9bdd92b 100644
--- a/miasm/core/parse_asm.py
+++ b/miasm/core/parse_asm.py
@@ -19,7 +19,7 @@ from miasm.core.asmbloc import *
 from shlex import shlex
 
 
-declarator = {'byte':'B', 'long':'I'}
+declarator = {'byte':'B', 'long':'I', 'zero':'I'}
 def guess_next_new_label(symbol_pool, gen_label_index = 0):
     i = 0
     while True:
@@ -29,6 +29,19 @@ def guess_next_new_label(symbol_pool, gen_label_index = 0):
         i+=1
 
 
+def normalize_args(a, b):
+    if len(a) != len(b):
+        log_asmbloc.debug("Incoherent number of args %d != %d"%(len(a),len(b)))
+        return
+    for i in xrange(len(a)):
+        if a[i] == b[i]:
+            continue
+        if 'imm' in b[i]:
+            continue
+        a[i] = b[i]
+        if 'ad' in b[i] and b[i]['ad']:
+            a[i]['ad'] = True
+
 def parse_txt(mnemo, txt, symbol_pool = None, gen_label_index = 0):
     if symbol_pool == None:
         symbol_pool = asm_symbol_pool()
@@ -46,6 +59,17 @@ def parse_txt(mnemo, txt, symbol_pool = None, gen_label_index = 0):
         #comment
         if re.match(r'\s*;\S*', line):
             continue
+        #labels to forget
+        r = re.match(r'\s*\.LF[BE]\d\s*:', line)
+        if r:
+            continue
+        #label beginning with .L
+        r = re.match(r'\s*(\.L\S+)\s*:', line)
+        if r:
+            l = r.groups()[0]
+            l = symbol_pool.getby_name_create(l)
+            lines.append(l)
+            continue
         #directive
         if re.match(r'\s*\.', line):
             r =  re.match(r'\s*\.(\S+)', line)
@@ -75,19 +99,29 @@ def parse_txt(mnemo, txt, symbol_pool = None, gen_label_index = 0):
                 lines.append(asm_raw(raw))
                 continue
             if directive in declarator:
-                data_raw = [x for x in shlex(line[r.end():]) if not x in ',']
-                data_int = []
-                for b in data_raw:
-                    if re.search(r'0x', b):
-                        data_int.append(int(b, 16))
-                    else:
-                        data_int.append(int(b))
-                raw = reduce(lambda x,y:x+struct.pack(declarator[directive], y), data_int, "")
+                data_raw = line[r.end():].split()
+                try:
+                    data_int = []
+                    for b in data_raw:
+                        if re.search(r'0x', b):
+                            data_int.append(int(b, 16))
+                        else:
+                            data_int.append(int(b)%(1<<32))
+                    raw = reduce(lambda x,y:x+struct.pack(declarator[directive], y), data_int, "")
+                except ValueError:
+                    raw = line
                 lines.append(asm_raw(raw))
                 continue
+            if directive == 'comm':
+                # TODO
+                continue
             if directive == 'split': #custom command
                 lines.append(asm_raw(line.strip()))
                 continue
+            if directive in [ 'file', 'intel_syntax', 'globl', 'local', 'type', 'size', 'align', 'ident', 'section' ]:
+                continue
+            if directive[0:4] == 'cfi_':
+                continue
 
             raise ValueError("unknown directive %s"%str(directive))
 
@@ -130,7 +164,7 @@ def parse_txt(mnemo, txt, symbol_pool = None, gen_label_index = 0):
             instr = mnemo.dis(c)
         else:
             instr = mnemo.asm_instr(line)
-        instr.arg = args
+        normalize_args(instr.arg, args)
         lines.append(instr)
 
     log_asmbloc.info( "___pre asm oki___")