Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
elf.py
Go to the documentation of this file.
1 import struct
2 from collections import defaultdict
3 
4 from elfesteem import cstruct
5 from elfesteem import *
6 import elfesteem.elf as elf_csts
7 
8 from miasm2.jitter.csts import *
9 from miasm2.jitter.loader.utils import canon_libname_libfunc, libimp
10 from miasm2.core.interval import interval
11 
12 import logging
13 
14 log = logging.getLogger('loader_elf')
15 hnd = logging.StreamHandler()
16 hnd.setFormatter(logging.Formatter("[%(levelname)s]: %(message)s"))
17 log.addHandler(hnd)
18 log.setLevel(logging.CRITICAL)
19 
21  import2addr = defaultdict(set)
22  for sh in e.sh:
23  if not hasattr(sh, 'rel'):
24  continue
25  for k, v in sh.rel.items():
26  import2addr[('xxx', k)].add(v.offset)
27  return import2addr
28 
29 
30 def preload_elf(vm, e, runtime_lib, patch_vm_imp=True):
31  # XXX quick hack
33  dyn_funcs = {}
34  # log.debug('imported funcs: %s' % fa)
35  for (libname, libfunc), ads in fa.items():
36  for ad in ads:
37  ad_base_lib = runtime_lib.lib_get_add_base(libname)
38  ad_libfunc = runtime_lib.lib_get_add_func(ad_base_lib, libfunc, ad)
39 
40  libname_s = canon_libname_libfunc(libname, libfunc)
41  dyn_funcs[libname_s] = ad_libfunc
42  if patch_vm_imp:
43  log.debug('patch 0x%x 0x%x %s', ad, ad_libfunc, libfunc)
44  vm.set_mem(
45  ad, struct.pack(cstruct.size2type[e.size], ad_libfunc))
46  return runtime_lib, dyn_funcs
47 
48 
49 
50 def vm_load_elf(vm, fdata, **kargs):
51  """
52  Very dirty elf loader
53  TODO XXX: implement real loader
54  """
55  #log.setLevel(logging.DEBUG)
56  e = elf_init.ELF(fdata, **kargs)
57  i = interval()
58  all_data = {}
59  for p in e.ph.phlist:
60  if p.ph.type != 1:
61  continue
62  log.debug('0x%x 0x%x 0x%x 0x%x', p.ph.vaddr, p.ph.memsz, p.ph.offset,
63  p.ph.filesz)
64  data_o = e._content[p.ph.offset:p.ph.offset + p.ph.filesz]
65  addr_o = p.ph.vaddr
66  a_addr = addr_o & ~0xFFF
67  b_addr = addr_o + max(p.ph.memsz, p.ph.filesz)
68  b_addr = (b_addr + 0xFFF) & ~0xFFF
69  all_data[addr_o] = data_o
70  # -2: Trick to avoid merging 2 consecutive pages
71  i += [(a_addr, b_addr-2)]
72  for a, b in i.intervals:
73  #print hex(a), hex(b)
74  vm.add_memory_page(a, PAGE_READ | PAGE_WRITE, "\x00"*(b+2-a))
75 
76 
77  for r_vaddr, data in all_data.items():
78  vm.set_mem(r_vaddr, data)
79  return e
80 
82  pass
83 
84 
85 # machine, size, sex -> arch_name
86 ELF_machine = {(elf_csts.EM_ARM, 32, elf_csts.ELFDATA2LSB): "arml",
87  (elf_csts.EM_ARM, 32, elf_csts.ELFDATA2MSB): "armb",
88  (elf_csts.EM_AARCH64, 64, elf_csts.ELFDATA2LSB): "aarch64l",
89  (elf_csts.EM_AARCH64, 64, elf_csts.ELFDATA2MSB): "aarch64b",
90  (elf_csts.EM_MIPS, 32, elf_csts.ELFDATA2MSB): "mips32b",
91  (elf_csts.EM_MIPS, 32, elf_csts.ELFDATA2LSB): "mips32l",
92  (elf_csts.EM_386, 32, elf_csts.ELFDATA2LSB): "x86_32",
93  (elf_csts.EM_X86_64, 64, elf_csts.ELFDATA2LSB): "x86_64",
94  (elf_csts.EM_SH, 32, elf_csts.ELFDATA2LSB): "sh4",
95  }
96 
97 def guess_arch(elf):
98  """Return the architecture specified by the ELF container @elf.
99  If unknown, return None"""
100  return ELF_machine.get((elf.Ehdr.machine, elf.size, elf.sex), None)
def get_import_address_elf
Definition: elf.py:20