diff options
| -rw-r--r-- | miasm2/analysis/binary.py | 6 | ||||
| -rw-r--r-- | miasm2/jitter/loader/elf.py | 18 |
2 files changed, 23 insertions, 1 deletions
diff --git a/miasm2/analysis/binary.py b/miasm2/analysis/binary.py index 27ae9ce7..98dd7b6a 100644 --- a/miasm2/analysis/binary.py +++ b/miasm2/analysis/binary.py @@ -158,7 +158,8 @@ class ContainerELF(Container): "Container abstraction for ELF" def parse(self, data, vm=None): - from miasm2.jitter.loader.elf import vm_load_elf, preload_elf + from miasm2.jitter.loader.elf import \ + vm_load_elf, preload_elf, guess_arch from elfesteem import elf_init # Parse signature @@ -174,6 +175,9 @@ class ContainerELF(Container): except Exception, error: raise ContainerParsingException('Cannot read ELF: %s' % error) + # Guess the architecture + self._arch = guess_arch(self._executable) + # Build the bin_stream instance and set the entry point try: self._bin_stream = bin_stream_elf(self._executable.virt) diff --git a/miasm2/jitter/loader/elf.py b/miasm2/jitter/loader/elf.py index 916b37c4..c0427e79 100644 --- a/miasm2/jitter/loader/elf.py +++ b/miasm2/jitter/loader/elf.py @@ -3,6 +3,8 @@ from collections import defaultdict from elfesteem import cstruct from elfesteem import * +import elfesteem.elf as elf_csts + from miasm2.jitter.csts import * from miasm2.jitter.loader.utils import canon_libname_libfunc, libimp from miasm2.core.interval import interval @@ -80,3 +82,19 @@ def vm_load_elf(vm, fdata, **kargs): class libimp_elf(libimp): pass + + +# machine, size, sex -> arch_name +ELF_machine = {(elf_csts.EM_ARM, 32, elf_csts.ELFDATA2LSB): "arml", + (elf_csts.EM_ARM, 32, elf_csts.ELFDATA2MSB): "armb", + (elf_csts.EM_MIPS, 32, elf_csts.ELFDATA2MSB): "mips32b", + (elf_csts.EM_MIPS, 32, elf_csts.ELFDATA2LSB): "mips32l", + (elf_csts.EM_386, 32, elf_csts.ELFDATA2LSB): "x86_32", + (elf_csts.EM_X86_64, 64, elf_csts.ELFDATA2LSB): "x86_64", + (elf_csts.EM_SH, 32, elf_csts.ELFDATA2LSB): "sh4", + } + +def guess_arch(elf): + """Return the architecture specified by the ELF container @elf. + If unknown, return None""" + return ELF_machine.get((elf.Ehdr.machine, elf.size, elf.sex), None) |