2 from collections
import defaultdict
4 from elfesteem
import cstruct
5 from elfesteem
import *
6 import elfesteem.elf
as elf_csts
14 log = logging.getLogger(
'loader_elf')
15 hnd = logging.StreamHandler()
16 hnd.setFormatter(logging.Formatter(
"[%(levelname)s]: %(message)s"))
18 log.setLevel(logging.CRITICAL)
21 import2addr = defaultdict(set)
23 if not hasattr(sh,
'rel'):
25 for k, v
in sh.rel.items():
26 import2addr[(
'xxx', k)].
add(v.offset)
35 for (libname, libfunc), ads
in fa.items():
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)
41 dyn_funcs[libname_s] = ad_libfunc
43 log.debug(
'patch 0x%x 0x%x %s', ad, ad_libfunc, libfunc)
45 ad, struct.pack(cstruct.size2type[e.size], ad_libfunc))
46 return runtime_lib, dyn_funcs
53 TODO XXX: implement real loader
56 e = elf_init.ELF(fdata, **kargs)
62 log.debug(
'0x%x 0x%x 0x%x 0x%x', p.ph.vaddr, p.ph.memsz, p.ph.offset,
64 data_o = e._content[p.ph.offset:p.ph.offset + p.ph.filesz]
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
71 i += [(a_addr, b_addr-2)]
72 for a, b
in i.intervals:
74 vm.add_memory_page(a, PAGE_READ | PAGE_WRITE,
"\x00"*(b+2-a))
77 for r_vaddr, data
in all_data.items():
78 vm.set_mem(r_vaddr, data)
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",
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 canon_libname_libfunc
def get_import_address_elf