def pil_split(src, prefix): from sectools.common.parsegen.elf.format import ParseGenElf, pack_phdrs, PF_OS_SEGMENT_HASH # Load the data p_obj = ParseGenElf(load_data_from_file(src)) # File names elfhdr = prefix + '.mdt' # Cleanup for eachpattern in [elfhdr, (prefix + '.b*')]: import glob for eachfile in glob.glob(eachpattern): try: os.remove(eachfile) except Exception: pass # Create the mdt data elfdata = p_obj.ehdr.pack() + pack_phdrs(p_obj.phdrs) # Dump the segments for idx, phdr in enumerate(p_obj.phdrs): path = prefix + ('.b%02d' % idx) store_data_to_file(path, p_obj.segments[phdr]) if phdr.f_os_segment_type == PF_OS_SEGMENT_HASH: elfdata += p_obj.segments[phdr] # Dump the final elfhdr mdt store_data_to_file(elfhdr, elfdata)
def is_elf(self, data): retval = False try: ParseGenElf(data) retval = True except Exception: pass return retval
def __init__(self, data=None, version=None, debug_dir=None, debug_prefix=None, debug_suffix=None, elf_class=None): self.elfparser = ParseGenElf(data, _class = elf_class) if data is None: phdr = self.elfparser.get_new_phdr_entry()() self.dbgpparser = ParseGenDbgp(data, version, debug_dir, debug_prefix, debug_suffix) self.elfparser.add_segment(phdr, self.dbgpparser.get_data()) else: phdr = self.elfparser.phdrs[-1] data = self.elfparser.get_segment_data(phdr) self.dbgpparser = ParseGenDbgp(data, version, debug_dir, debug_prefix, debug_suffix)
class ParseGenDbgpElf(ParseGenDbgp): def __init__(self, data=None, version=None, debug_dir=None, debug_prefix=None, debug_suffix=None, elf_class=None): self.elfparser = ParseGenElf(data, _class=elf_class) if data is None: phdr = self.elfparser.get_new_phdr_entry()() self.dbgpparser = ParseGenDbgp(data, version, debug_dir, debug_prefix, debug_suffix) self.elfparser.add_segment(phdr, self.dbgpparser.get_data()) else: phdr = self.elfparser.phdrs[-1] data = self.elfparser.get_segment_data(phdr) self.dbgpparser = ParseGenDbgp(data, version, debug_dir, debug_prefix, debug_suffix) def get_data(self): data = self.dbgpparser.get_data() self.elfparser.phdrs[-1].p_offset = df.OFFSET self.elfparser.phdrs[-1].p_type = PT_LOAD self.elfparser.update_segment(self.elfparser.phdrs[-1], data) data = self.elfparser.get_data() return data @property def phys_addr(self): return self.elfparser.phdrs[-1].p_paddr @phys_addr.setter def phys_addr(self, address): self.elfparser.phdrs[-1].p_paddr = address self.elfparser.phdrs[-1].p_vaddr = address self.elfparser.ehdr.e_entry = address def __repr__(self): return (repr(self.elfparser) + '\n' + repr(self.dbgpparser))