def __init__(self, elf, data, strs): self.elf = elf if elf.bits == 32: fmt = 'IIIBBH' else: fmt = 'IBBHQQ' if elf.bits == 32: self.name_index, self.value, self.size, self.info, self.other, self.shndx = \ unpack(fmt, data, target=elf) else: self.name_index, self.info, self.other, self.shndx, self.value, self.size = \ unpack(fmt, data, target=elf) self.type_id = self.info & 15 self.binding = self.Binding(self.info >> 4) self.visibility = self.Visibility(self.other & 3) self.name = strs[self.name_index:].split('\0', 1)[0] try: self.type = self.Type(self.type_id) except ValueError: self.type = self.Type.unknown
def _parse_header(self, data): """ Parse the ELF header in ``data`` and populate the properties. Args: data(bytes): The ELF header. """ (magic, word_size, byte_order, version, osabi, abi_version, _), data = \ unpack('4sBBBBB7s', data[:16]), data[16:] assert magic == self._ELF_MAGIC, 'Missing ELF magic' assert word_size in (1, 2), 'Invalid word size' assert byte_order in (1, 2), 'Invalid byte order' assert version == 1, 'Invalid version' self.osabi = self.OSABI(osabi) self.abi_version = abi_version (type_, machine, version), data = unpack('HHI', data[:8], endian=self.endian), data[8:] self.type = self.Type(type_) self.machine = ELF.Machine(machine) assert version == 1, 'Invalid version' if self.machine is ELF.Machine.i386: arch = Target.Arch.x86 assert word_size == 1, 'Unexpected ELF64 for machine type x86' assert byte_order == 1, 'Unexpected big-endian for machine type x86' elif self.machine is ELF.Machine.x86_64: arch = Target.Arch.x86 assert word_size == 2, 'Unexpected ELF32 for machine type x64_64' assert byte_order == 1, 'Unexpected big-endian for machine type x86' elif self.machine is ELF.Machine.arm: arch = Target.Arch.arm assert word_size == 1, 'Unexpected ELF64 for machine type arm' elif self.machine is ELF.Machine.aarch64: arch = Target.Arch.arm assert word_size == 2, 'Unexpected ELF32 for machine type aarch64' else: arch = Target.Arch.unknown self.arch = arch self.bits = 32 * word_size self.endian = byte_order - 1 if self.bits == 32: fmt = 'IIIIHHHHHH' else: fmt = 'QQQIHHHHHH' fmt_size = pack_size(fmt) (self.entry, self.phoff, self.shoff, self.flags, self.hsize, self.phentsize, self.phnum, self.shentsize, self.shnum, self.shstrndx) = \ unpack(fmt, data[:fmt_size], target=self)
def __init__(self, elf, data): if elf.bits == 32: fmt = 'IIIIIIII' fmt_fields = [ 'type_id', 'offset', 'vaddr', 'paddr', 'filesz', 'memsz', 'flags', 'align', ] else: fmt = 'IIQQQQQQ' fmt_fields = [ 'type_id', 'flags', 'offset', 'vaddr', 'paddr', 'filesz', 'memsz', 'align', ] fmt_size = pack_size(fmt) for key, value in zip(fmt_fields, unpack(fmt, data[:fmt_size], target=elf)): setattr(self, key, value) try: self.type = self.Type(self.type_id) except ValueError: self.type = self.Type.unknown
def __init__(self, elf, data): self.elf = elf if elf.bits == 32: fmt = 'IIIIIIIIII' else: fmt = 'IIQQQQIIQQ' fmt_size = pack_size(fmt) for key, value in zip([ 'name_index', 'type_id', 'flags', 'addr', 'offset', 'size', 'link', 'info', 'addralign', 'entsize', ], unpack(fmt, data[:fmt_size], target=elf)): setattr(self, key, value) try: self.type = self.Type(self.type_id) except ValueError: self.type = self.Type.unknown
def __init__(self, elf, data): self.elf = elf if elf.bits == 32: fmt = 'IIIIIIIIII' else: fmt = 'IIQQQQIIQQ' fmt_size = pack_size(fmt) for key, value in zip( [ 'name_index', 'type_id', 'flags', 'addr', 'offset', 'size', 'link', 'info', 'addralign', 'entsize', ], unpack(fmt, data[:fmt_size], target=elf) ): setattr(self, key, value) try: self.type = self.Type(self.type_id) except ValueError: self.type = self.Type.unknown
def _ensure_dynamic_section_loaded(self): if self._dynamic_section_entries is None: try: section = self.get_section_header('.dynamic') data = section.content except KeyError: data = [] if self.bits == 32: fmt = 'iI' else: fmt = 'QQ' fmt_size = pack_size(fmt) self._dynamic_section_entries = [ self.DynamicSectionEntry(*unpack(fmt, data[i:i + fmt_size], target=self)) for i in range(0, len(data), fmt_size) ]
def _ensure_dynamic_section_loaded(self): if self._dynamic_section_entries is None: try: section = self.get_section_header('.dynamic') data = section.content except KeyError: data = [] if self.bits == 32: fmt = 'iI' else: fmt = 'QQ' fmt_size = pack_size(fmt) self._dynamic_section_entries = [ self.DynamicSectionEntry( *unpack(fmt, data[i:i + fmt_size], target=self)) for i in range(0, len(data), fmt_size) ]
def __init__(self, elf, data): if elf.bits == 32: fmt = 'IIIIIIII' fmt_fields = [ 'type_id', 'offset', 'vaddr', 'paddr', 'filesz', 'memsz', 'flags', 'align', ] else: fmt = 'IIQQQQQQ' fmt_fields = [ 'type_id', 'flags', 'offset', 'vaddr', 'paddr', 'filesz', 'memsz', 'align', ] fmt_size = pack_size(fmt) for key, value in zip( fmt_fields, unpack(fmt, data[:fmt_size], target=elf) ): setattr(self, key, value) try: self.type = self.Type(self.type_id) except ValueError: self.type = self.Type.unknown
def read_float_binary(): return unpack('d', fp.read(8), target=MARSHAL_TARGET)[0]