def __bytes__(self): headers = {} headers[0] = bytes(self.Ehdr) # Phdrs cursor = self.Ehdr.e_phoff for Phdr in self.Phdr_table: headers[cursor] = Phdr cursor += self.Ehdr.e_phentsize # Shdrs cursor = self.Ehdr.e_shoff for Shdr in self.Shdr_table: headers[cursor] = Shdr cursor += self.Ehdr.e_shentsize # programs for i, Phdr in enumerate(self.Phdr_table): if len(self.programs[i]) != 0 and not isinstance(self.programs[i], memoryview): headers[Phdr.p_offset] = self.programs[i] # sections for i, Shdr in enumerate(self.Shdr_table): if Shdr.sh_offset < 0: pass elif len(self.sections[i]) != 0 and not isinstance(self.sections[i], memoryview): headers[Shdr.sh_offset] = self.sections[i] # find file size end_of_file = sorted(headers.keys())[-1] end_of_file += len(headers[end_of_file]) # Set all invalid offsets to end of file for i, Shdr in enumerate(self.Shdr_table): if Shdr.sh_offset < 0: Shdr.sh_offset = end_of_file # create and populate buffer b = bytes(end_of_file) for off in headers: # TODO: there's something wrong, when hdr is not bytes, but only # simulates it hdr = headers[off] if isinstance(hdr, list): hdr_as_bytes = b'' for e in hdr: hdr_as_bytes += bytes(e) hdr = hdr_as_bytes else: hdr = bytes(hdr) size = len(hdr) # expand to file size aligned = align(bytes(off) + hdr, end_of_file) # xor into b b = makeelf.utils.bytes_xor(b, aligned) return b
def __bytes__(self): max_val = type(self)._max_value() field_width = Enum._field_width(0, max_val) b = Enum._value_as_bytes(int(self)) # if last set byte is less than width of the field, align b = align(b, field_width) if sys.byteorder == 'little': b = bytes(reversed(b)) return b
def __bytes__(self): packet = self.EI_MAG + bytes(self.EI_CLASS) + bytes(self.EI_DATA) + \ bytes(self.EI_VERSION) + bytes(self.EI_OSABI) return align(packet, 16)