def as_bytearray(self): from peachpy.formats.elf.file import FileHeader file_header = FileHeader(self.abi) file_header.section_header_table_offset = file_header.size file_header.section_header_entries_count = len(self.sections) file_header.section_name_string_table_index = 1 data = file_header.as_bytearray # Update section offsets data_offset = file_header.size + self.sections[0].header.size * len(self.sections) for section in self.sections: if section.header.address_alignment != 0: if data_offset % section.header.address_alignment != 0: padding_length = section.header.address_alignment - data_offset % section.header.address_alignment data_offset += padding_length section.header.offset = data_offset data_offset += section.header.content_size # Write section headers for section in self.sections: data += section.header.as_bytearray # Write section content for section in self.sections: if section.header.address_alignment != 0: if len(data) % section.header.address_alignment != 0: padding_length = section.header.address_alignment - len(data) % section.header.address_alignment padding_data = bytearray([0] * padding_length) data += padding_data data += section.content return data
def as_bytearray(self): import six from peachpy.formats.elf.file import FileHeader from peachpy.formats.elf.section import Section, StringSection, SymbolSection from peachpy.util import roundup file_header = FileHeader(self.abi) file_header.section_header_table_offset = file_header.size file_header.section_header_entries_count = len(self.sections) file_header.section_name_string_table_index = 1 data = file_header.as_bytearray # Collect strings from sections for section in self.sections: self.shstrtab.add(section.name) if isinstance(section, StringSection): pass elif isinstance(section, SymbolSection): for symbol in six.iterkeys(section.symbol_index_map): self.strtab.add(symbol.name) # Layout sections data_offset = file_header.size + Section.get_header_size( self.abi) * len(self.sections) section_offsets = [] for section in self.sections: if section.alignment != 0: data_offset = roundup(data_offset, section.alignment) section_offsets.append(data_offset) data_offset += section.get_content_size(self.abi) from peachpy.encoder import Encoder encoder = Encoder(self.abi.endianness, self.abi.elf_bitness) section_index_map = { section: index for index, section in enumerate(self.sections) } # Write section headers for section, offset in zip(self.sections, section_offsets): data += section.encode_header(encoder, self.shstrtab._string_index_map, section_index_map, offset) # Write section content for section in self.sections: padding = bytearray( roundup(len(data), section.alignment) - len(data)) data += padding data += section.encode_content(encoder, self.strtab._string_index_map, section_index_map, self.symtab.symbol_index_map) return data
def as_bytearray(self): import six from peachpy.formats.elf.file import FileHeader from peachpy.formats.elf.section import Section, StringSection, SymbolSection from peachpy.util import roundup file_header = FileHeader(self.abi) file_header.section_header_table_offset = file_header.size file_header.section_header_entries_count = len(self.sections) file_header.section_name_string_table_index = 1 data = file_header.as_bytearray # Collect strings from sections for section in self.sections: self.shstrtab.add(section.name) if isinstance(section, StringSection): pass elif isinstance(section, SymbolSection): for symbol in six.iterkeys(section.symbol_index_map): self.strtab.add(symbol.name) # Layout sections data_offset = file_header.size + Section.get_header_size(self.abi) * len(self.sections) section_offsets = [] for section in self.sections: if section.alignment != 0: data_offset = roundup(data_offset, section.alignment) section_offsets.append(data_offset) data_offset += section.get_content_size(self.abi) from peachpy.encoder import Encoder encoder = Encoder(self.abi.endianness, self.abi.elf_bitness) section_index_map = {section: index for index, section in enumerate(self.sections)} # Write section headers for section, offset in zip(self.sections, section_offsets): data += section.encode_header(encoder, self.shstrtab._string_index_map, section_index_map, offset) # Write section content for section in self.sections: padding = bytearray(roundup(len(data), section.alignment) - len(data)) data += padding data += section.encode_content( encoder, self.strtab._string_index_map, section_index_map, self.symtab.symbol_index_map ) return data
def runTest(self): from peachpy.formats.elf.file import FileHeader import peachpy.arm.abi file_header = FileHeader(peachpy.arm.abi.arm_gnueabi) file_header_bytes = file_header.as_bytearray self.assertEqual(len(file_header_bytes), file_header.file_header_size, "ELF header size must match the value specified in the ELF header")