def __init__(self, abi, source=None): from peachpy.formats.elf.section import null_section, StringSection, SymbolSection, SectionIndex from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType self.abi = abi self.shstrtab = StringSection(".shstrtab") self.strtab = StringSection(".strtab") self.symtab = SymbolSection(string_table=self.strtab) self.sections = [null_section, self.shstrtab, self.strtab, self.symtab] self._section_names = set( [self.shstrtab.name, self.strtab.name, self.symtab.name]) if source: source_symbol = Symbol() source_symbol.value = 0 source_symbol.binding = SymbolBinding.local source_symbol.type = SymbolType.file source_symbol.name = source source_symbol.size = 0 source_symbol.section = SectionIndex.absolute self.symtab.add(source_symbol)
def __init__(self, abi, source=None): from peachpy.formats.elf.section import null_section, StringSection, SymbolSection, SectionIndex from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType self.abi = abi self.shstrtab = StringSection(".shstrtab") self.strtab = StringSection(".strtab") self.symtab = SymbolSection(string_table=self.strtab) self.sections = [null_section, self.shstrtab, self.strtab, self.symtab] self._section_names = set([self.shstrtab.name, self.strtab.name, self.symtab.name]) if source: source_symbol = Symbol() source_symbol.value = 0 source_symbol.binding = SymbolBinding.local source_symbol.type = SymbolType.file source_symbol.name = source source_symbol.size = 0 source_symbol.section = SectionIndex.absolute self.symtab.add(source_symbol)
def add_function(self, function): import peachpy.x86_64.function assert isinstance(function, peachpy.x86_64.function.ABIFunction), \ "Function must be bindinded to an ABI before its assembly can be used" encoded_function = function.encode() function_code = encoded_function.as_bytearray function_offset = len(self.text_section.content) self.text_section.append(function_code) from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType function_symbol = Symbol(self.abi) function_symbol.name_index = self.image.strtab.add(function.name) function_symbol.value = function_offset function_symbol.content_size = len(function_code) function_symbol.section_index = self.text_section.index function_symbol.binding = SymbolBinding.Global function_symbol.type = SymbolType.Function self.image.symtab.add(function_symbol)
def __init__(self, abi, source=None): from peachpy.formats.elf.section import NullSection, StringSection, SymbolSection, SectionIndex from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType self.abi = abi self.sections = [] null = NullSection(abi) self.bind_section(null) shstrtab = StringSection(abi) self.bind_section(shstrtab, ".shstrtab") strtab = StringSection(abi) self.bind_section(strtab, ".strtab") symtab = SymbolSection(abi) symtab.header.link_index = strtab.index symtab.header.info = 0 self.bind_section(symtab, ".symtab") if source: source_symbol = Symbol(abi) source_symbol.value = 0 source_symbol.binding = SymbolBinding.Local source_symbol.type = SymbolType.File source_symbol.name_index = strtab.add(source) source_symbol.content_size = 0 source_symbol.section_index = SectionIndex.Absolute symtab.add(source_symbol)
def add_function(self, function): import peachpy.x86_64.function from peachpy.util import roundup assert isinstance(function, peachpy.x86_64.function.ABIFunction), \ "Function must be finalized with an ABI before its assembly can be used" encoded_function = function.encode() code_offset = len(self.text_section.content) code_padding = bytearray([encoded_function.code_section.alignment_byte] * (roundup(code_offset, encoded_function.code_section.alignment) - code_offset)) self.text_section.content += code_padding code_offset += len(code_padding) self.text_section.content += encoded_function.code_section.content self.text_section.alignment = max(self.text_section.alignment, encoded_function.code_section.alignment) const_offset = 0 if encoded_function.const_section.content: if self.rodata_section is None: from peachpy.formats.elf.section import ReadOnlyDataSection self.rodata_section = ReadOnlyDataSection() self.image.add_section(self.rodata_section) const_offset = self.rodata_section.get_content_size(self.abi) const_padding = bytearray([encoded_function.const_section.alignment_byte] * (roundup(const_offset, encoded_function.const_section.alignment) - const_offset)) self.rodata_section.content += const_padding const_offset += len(const_padding) self.rodata_section.content += encoded_function.const_section.content self.rodata_section.alignment = max(self.rodata_section.alignment, encoded_function.const_section.alignment) # Map from symbol name to symbol index from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType symbol_map = dict() for symbol in encoded_function.const_section.symbols: const_symbol = Symbol() const_symbol.name = function.mangled_name + "." + symbol.name const_symbol.value = const_offset + symbol.offset const_symbol.size = symbol.size const_symbol.section = self.rodata_section const_symbol.binding = SymbolBinding.local const_symbol.type = SymbolType.data_object self.image.symtab.add(const_symbol) symbol_map[symbol] = const_symbol if encoded_function.code_section.relocations: if self.text_rela_section is None: from peachpy.formats.elf.section import RelocationsWithAddendSection self.text_rela_section = RelocationsWithAddendSection(self.text_section, self.image.symtab) self.image.add_section(self.text_rela_section) from peachpy.formats.elf.symbol import RelocationWithAddend, RelocationType for relocation in encoded_function.code_section.relocations: elf_relocation = RelocationWithAddend(RelocationType.x86_64_pc32, code_offset + relocation.offset, symbol_map[relocation.symbol], relocation.offset - relocation.program_counter) self.text_rela_section.add(elf_relocation) function_symbol = Symbol() function_symbol.name = function.mangled_name function_symbol.value = code_offset function_symbol.content_size = len(encoded_function.code_section) function_symbol.section = self.text_section function_symbol.binding = SymbolBinding.global_ function_symbol.type = SymbolType.function self.image.symtab.add(function_symbol)
def add_function(self, function): import peachpy.x86_64.function from peachpy.util import roundup assert isinstance(function, peachpy.x86_64.function.ABIFunction), \ "Function must be bindinded to an ABI before its assembly can be used" encoded_function = function.encode() code_offset = len(self.text_section.content) code_padding = bytearray([encoded_function.code_section.alignment_byte] * (roundup(code_offset, encoded_function.code_section.alignment) - code_offset)) self.text_section.content += code_padding code_offset += len(code_padding) self.text_section.content += encoded_function.code_section.content self.text_section.alignment = max(self.text_section.alignment, encoded_function.code_section.alignment) const_offset = 0 if encoded_function.const_section.content: if self.rodata_section is None: from peachpy.formats.elf.section import ReadOnlyDataSection self.rodata_section = ReadOnlyDataSection() self.image.add_section(self.rodata_section) const_offset = self.rodata_section.get_content_size(self.abi) const_padding = bytearray([encoded_function.const_section.alignment_byte] * (roundup(const_offset, encoded_function.const_section.alignment) - const_offset)) self.rodata_section.content += const_padding const_offset += len(const_padding) self.rodata_section.content += encoded_function.const_section.content self.rodata_section.alignment = max(self.rodata_section.alignment, encoded_function.const_section.alignment) # Map from symbol name to symbol index from peachpy.formats.elf.symbol import Symbol, SymbolBinding, SymbolType symbol_map = dict() for symbol in encoded_function.const_section.symbols: const_symbol = Symbol() const_symbol.name = symbol.name const_symbol.value = const_offset + symbol.offset const_symbol.size = symbol.size const_symbol.section = self.rodata_section const_symbol.binding = SymbolBinding.local const_symbol.type = SymbolType.data_object self.image.symtab.add(const_symbol) symbol_map[symbol] = const_symbol if encoded_function.code_section.relocations: if self.text_rela_section is None: from peachpy.formats.elf.section import RelocationsWithAddendSection self.text_rela_section = RelocationsWithAddendSection(self.text_section, self.image.symtab) self.image.add_section(self.text_rela_section) from peachpy.formats.elf.symbol import RelocationWithAddend, RelocationType for relocation in encoded_function.code_section.relocations: elf_relocation = RelocationWithAddend(RelocationType.x86_64_pc32, relocation.offset, symbol_map[relocation.symbol], -4) self.text_rela_section.add(elf_relocation) function_symbol = Symbol() function_symbol.name = function.name function_symbol.value = code_offset function_symbol.content_size = len(encoded_function.code_section) function_symbol.section = self.text_section function_symbol.binding = SymbolBinding.global_ function_symbol.type = SymbolType.function self.image.symtab.add(function_symbol)