Exemple #1
0
    def __init__(self, output_path, abi, input_path=None):
        from peachpy.formats.elf.image import Image
        from peachpy.formats.elf.section import TextSection

        self.output_path = output_path
        self.previous_writer = None
        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection(abi)
        self.image.bind_section(self.text_section, ".text")
Exemple #2
0
class ELFWriter:
    def __init__(self, output_path, abi, input_path=None):
        from peachpy.formats.elf.image import Image
        from peachpy.formats.elf.section import TextSection

        self.output_path = output_path
        self.previous_writer = None
        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection(abi)
        self.image.bind_section(self.text_section, ".text")

    def __enter__(self):
        global active_writer
        self.previous_writer = active_writer
        active_writer = self
        self.output_file = open(self.output_path, "w", buffering=0)
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        global active_writer
        active_writer = self.previous_writer
        self.previous_writer = None
        if exc_type is None:
            self.image.symtab.bind()
            self.output_file.write(self.image.as_bytearray)
            self.output_file.close()
            self.output_file = None
        else:
            import os
            os.unlink(self.output_file.name)
            self.output_file = None
            raise

    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)
Exemple #3
0
    def __init__(self, output_path, abi, input_path=None):
        from peachpy.formats.mscoff import Image, TextSection, ReadOnlyDataSection

        self.output_path = output_path
        self.previous_writer = None
        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection()
        self.image.add_section(self.text_section)
        self.rdata_section = ReadOnlyDataSection()
        self.image.add_section(self.rdata_section)
Exemple #4
0
    def __init__(self, output_path, abi, input_path=None):
        super(ELFWriter, self).__init__(output_path)
        from peachpy.formats.elf.image import Image
        from peachpy.formats.elf.section import TextSection

        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection()
        self.image.add_section(self.text_section)
        self.text_rela_section = None
        self.rodata_section = None
Exemple #5
0
    def __init__(self, output_path, abi, input_path=None):
        super(MSCOFFWriter, self).__init__(output_path)

        from peachpy.formats.mscoff import Image, TextSection, ReadOnlyDataSection

        self.output_path = output_path
        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection()
        self.image.add_section(self.text_section)
        self.rdata_section = ReadOnlyDataSection()
        self.image.add_section(self.rdata_section)
Exemple #6
0
    def __init__(self, output_path, abi, input_path=None):
        super(ELFWriter, self).__init__(output_path)
        from peachpy.formats.elf.image import Image
        from peachpy.formats.elf.section import TextSection, ProgramBitsSection

        self.abi = abi
        self.image = Image(abi, input_path)
        self.text_section = TextSection()
        self.image.add_section(self.text_section)
        self.gnu_stack_section = ProgramBitsSection(".note.GNU-stack", allocate=False)
        self.image.add_section(self.gnu_stack_section)
        self.text_rela_section = None
        self.rodata_section = None