about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--example/disasm/full.py10
-rw-r--r--miasm2/analysis/binary.py23
-rw-r--r--test/test_all.py4
3 files changed, 35 insertions, 2 deletions
diff --git a/example/disasm/full.py b/example/disasm/full.py
index ee0b88dd..7ff60d3b 100644
--- a/example/disasm/full.py
+++ b/example/disasm/full.py
@@ -78,7 +78,7 @@ mn, dis_engine = machine.mn, machine.dis_engine
 ira, ir = machine.ira, machine.ir
 log.info('ok')
 
-mdis = dis_engine(bs)
+mdis = dis_engine(bs, symbol_pool=cont.symbol_pool)
 # configure disasm engine
 mdis.dontdis_retcall = args.dontdis_retcall
 mdis.blocs_wd = args.blockwatchdog
@@ -86,7 +86,13 @@ mdis.dont_dis_nulstart_bloc = not args.dis_nulstart_block
 mdis.follow_call = args.followcall
 
 todo = []
-addrs = [int(a, 0) for a in args.address]
+addrs = []
+for addr in args.address:
+    try:
+        addrs.append(int(addr, 0))
+    except ValueError:
+        # Second chance, try with symbol
+        addrs.append(mdis.symbol_pool.getby_name(addr).offset)
 
 if len(addrs) == 0 and default_addr is not None:
     addrs.append(default_addr)
diff --git a/miasm2/analysis/binary.py b/miasm2/analysis/binary.py
index 900d76ab..d47ca884 100644
--- a/miasm2/analysis/binary.py
+++ b/miasm2/analysis/binary.py
@@ -2,6 +2,7 @@ import logging
 
 from miasm2.core.bin_stream import bin_stream_str, bin_stream_elf, bin_stream_pe
 from miasm2.jitter.csts import PAGE_READ
+from miasm2.core.asmbloc import asm_symbol_pool
 
 
 log = logging.getLogger("binary")
@@ -93,6 +94,7 @@ class Container(object):
         self._bin_stream = None
         self._entry_point = None
         self._arch = None
+        self._symbol_pool = asm_symbol_pool()
 
         # Launch parsing
         self.parse(*args, **kwargs)
@@ -117,6 +119,11 @@ class Container(object):
         "Return the guessed architecture"
         return self._arch
 
+    @property
+    def symbol_pool(self):
+        "asm_symbol_pool instance preloaded with container symbols (if any)"
+        return self._symbol_pool
+
 
 ## Format dependent classes
 class ContainerPE(Container):
@@ -186,6 +193,22 @@ class ContainerELF(Container):
         except Exception, error:
             raise ContainerParsingException('Cannot read ELF: %s' % error)
 
+        # Add known symbols
+        symtab = self._executable.getsectionbyname(".symtab")
+        if symtab is not None:
+            for name, symb in symtab.symbols.iteritems():
+                offset = symb.value
+                if offset != 0:
+                    try:
+                        self._symbol_pool.add_label(name, offset)
+                    except ValueError:
+                        # Two symbols points on the same offset
+                        log.warning("Same offset (%s) for %s and %s", (hex(offset),
+                                                                       name,
+                                                                       self._symbol_pool.getby_offset(offset)))
+                        continue
+
+
 
 class ContainerUnknown(Container):
     "Container abstraction for unknown format"
diff --git a/test/test_all.py b/test/test_all.py
index d633d85c..704e834f 100644
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -459,6 +459,10 @@ testset += ExampleDisasmFull(["x86_64", Example.get_sample("demo_x86_64.bin"),
                               "0x401000"], depends=[test_x86_64])
 testset += ExampleDisasmFull(["aarch64l", Example.get_sample("md5_aarch64l"),
                               "0x400A00"], depends=[test_aarch64l])
+testset += ExampleDisasmFull(["x86_32", os.path.join("..", "..", "test",
+                                                     "arch", "x86", "qemu",
+                                                     "test-i386"),
+                              "func_iret"])
 
 
 ## Expression