コード例 #1
0
    def __init__(self, output_path, abi):
        super(MachOWriter, self).__init__(output_path)

        from peachpy.formats.macho.image import Image

        self.abi = abi
        self.image = Image(abi)
コード例 #2
0
ファイル: writer.py プロジェクト: moneytech/PeachPy
    def __init__(self, output_path, abi):
        from peachpy.formats.macho.image import Image

        self.output_path = output_path
        self.previous_writer = None
        self.abi = abi
        self.image = Image(abi)
コード例 #3
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")
コード例 #4
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)
コード例 #5
0
ファイル: writer.py プロジェクト: staugust/PeachPy
    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
コード例 #6
0
ファイル: writer.py プロジェクト: moneytech/PeachPy
    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)
コード例 #7
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)
コード例 #8
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
コード例 #9
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
    def __init__(self, output_path, abi):
        super(MachOWriter, self).__init__(output_path)

        from peachpy.formats.macho.image import Image

        self.abi = abi
        self.image = Image(abi)
コード例 #10
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
    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
コード例 #11
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
    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)
コード例 #12
0
class MSCOFFWriter(ImageWriter):
    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)

    def encode(self):
        return self.image.encode()

    def add_function(self, function):
        import peachpy.x86_64.function
        assert isinstance(function, peachpy.x86_64.function.ABIFunction), \
            "Function must be finalized with an ABI before its assembly can be used"
        from peachpy.util import roundup
        from peachpy.formats.mscoff import Symbol, SymbolType, StorageClass, Relocation, RelocationType

        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)

        rdata_offset = self.rdata_section.content_size
        rdata_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(rdata_offset, encoded_function.const_section.alignment) - rdata_offset))
        self.rdata_section.content += rdata_padding
        rdata_offset += len(rdata_padding)
        self.rdata_section.content += encoded_function.const_section.content
        self.rdata_section.alignment = \
            max(self.rdata_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            mscoff_symbol = Symbol()
            mscoff_symbol.name = symbol.name
            mscoff_symbol.value = rdata_offset + symbol.offset
            mscoff_symbol.section = self.rdata_section
            mscoff_symbol.symbol_type = SymbolType.non_function
            mscoff_symbol.storage_class = StorageClass.static
            self.image.add_symbol(mscoff_symbol)
            symbol_map[symbol] = mscoff_symbol

        for relocation in encoded_function.code_section.relocations:
            relocation_type_map = {
                4: RelocationType.x86_64_relocation_offset32,
                5: RelocationType.x86_64_relocation_plus_1_offset32,
                6: RelocationType.x86_64_relocation_plus_2_offset32,
                7: RelocationType.x86_64_relocation_plus_3_offset32,
                8: RelocationType.x86_64_relocation_plus_4_offset32,
                9: RelocationType.x86_64_relocation_plus_5_offset32
            }
            relocation_type = relocation_type_map[relocation.program_counter - relocation.offset]
            mscoff_relocation = Relocation(relocation_type,
                                           code_offset + relocation.offset,
                                           symbol_map[relocation.symbol])
            self.text_section.relocations.append(mscoff_relocation)

        function_symbol = Symbol()
        function_symbol.name = function.mangled_name
        function_symbol.value = code_offset
        function_symbol.section = self.text_section
        function_symbol.symbol_type = SymbolType.function
        function_symbol.storage_class = StorageClass.external
        self.image.add_symbol(function_symbol)
コード例 #13
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
class ELFWriter(ImageWriter):
    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

    def encode(self):
        return self.image.as_bytearray

    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)
コード例 #14
0
class MachOWriter(ImageWriter):
    def __init__(self, output_path, abi):
        super(MachOWriter, self).__init__(output_path)

        from peachpy.formats.macho.image import Image

        self.abi = abi
        self.image = Image(abi)

    def encode(self):
        return self.image.encode()

    def add_function(self, function):
        import peachpy.x86_64.function
        assert isinstance(function, peachpy.x86_64.function.ABIFunction), \
            "Function must be finalized with an ABI before its assembly can be used"

        from peachpy.formats.macho.symbol import Symbol, SymbolType, SymbolDescription, SymbolVisibility, \
            Relocation, RelocationType
        from peachpy.util import roundup

        encoded_function = function.encode()

        code_offset = len(self.image.text_section.content)
        code_padding = bytearray([encoded_function.code_section.alignment_byte] *
                                 (roundup(code_offset, encoded_function.code_section.alignment) - code_offset))
        self.image.text_section.content += code_padding
        code_offset += len(code_padding)
        self.image.text_section.content += encoded_function.code_section.content
        self.image.text_section.alignment = \
            max(self.image.text_section.alignment, encoded_function.code_section.alignment)

        const_offset = self.image.const_section.content_size
        const_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(const_offset, encoded_function.const_section.alignment) - const_offset))
        self.image.const_section.content += const_padding
        const_offset += len(const_padding)
        self.image.const_section.content += encoded_function.const_section.content
        self.image.const_section.alignment = \
            max(self.image.const_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            macho_symbol = Symbol("_" + function.mangled_name + "." + symbol.name,
                                  SymbolType.section_relative, self.image.const_section,
                                  const_offset + symbol.offset)
            macho_symbol.description = SymbolDescription.defined
            self.image.symbol_table.add_symbol(macho_symbol)
            symbol_map[symbol] = macho_symbol

        for relocation in encoded_function.code_section.relocations:
            macho_relocation = Relocation(RelocationType.x86_64_signed, code_offset + relocation.offset, 4,
                                          symbol_map[relocation.symbol], is_pc_relative=True)
            relocation_addend = relocation.offset + 4 - relocation.program_counter
            if relocation_addend != 0:
                self.image.text_section.content[code_offset + relocation.offset] = relocation_addend & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 1] = (relocation_addend >> 8) & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 2] = (relocation_addend >> 16) & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 3] = (relocation_addend >> 24) & 0xFF

            self.image.text_section.relocations.append(macho_relocation)

        function_symbol = Symbol("_" + function.mangled_name, SymbolType.section_relative, self.image.text_section,
                                 value=code_offset)
        function_symbol.description = SymbolDescription.defined
        function_symbol.visibility = SymbolVisibility.external
        self.image.symbol_table.add_symbol(function_symbol)
コード例 #15
0
class ELFWriter(ImageWriter):
    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

    def encode(self):
        return self.image.as_bytearray

    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)
コード例 #16
0
ファイル: writer.py プロジェクト: moneytech/PeachPy
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, 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.text_rela_section = None
        self.rodata_section = None

    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.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
        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)
コード例 #17
0
ファイル: writer.py プロジェクト: moneytech/PeachPy
class MSCOFFWriter:
    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)

    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.output_file.write(self.image.encode())
            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"
        from peachpy.util import roundup
        from peachpy.formats.mscoff import Symbol, SymbolType, StorageClass, Relocation, RelocationType

        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))
        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)

        rdata_offset = self.rdata_section.content_size
        rdata_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(rdata_offset, encoded_function.const_section.alignment) - rdata_offset))
        self.rdata_section.content += rdata_padding
        rdata_offset += len(rdata_padding)
        self.rdata_section.content += encoded_function.const_section.content
        self.rdata_section.alignment = \
            max(self.rdata_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            mscoff_symbol = Symbol()
            mscoff_symbol.name = symbol.name
            mscoff_symbol.value = rdata_offset + symbol.offset
            mscoff_symbol.section = self.rdata_section
            mscoff_symbol.symbol_type = SymbolType.non_function
            mscoff_symbol.storage_class = StorageClass.static
            self.image.add_symbol(mscoff_symbol)
            symbol_map[symbol] = mscoff_symbol

        for relocation in encoded_function.code_section.relocations:
            mscoff_relocation = Relocation(RelocationType.x86_64_relocation_offset32,
                                           code_offset + relocation.offset,
                                           symbol_map[relocation.symbol])
            self.text_section.relocations.append(mscoff_relocation)

        function_symbol = Symbol()
        function_symbol.name = function.name
        function_symbol.value = code_offset
        function_symbol.section = self.text_section
        function_symbol.symbol_type = SymbolType.function
        function_symbol.storage_class = StorageClass.external
        self.image.add_symbol(function_symbol)
コード例 #18
0
ファイル: writer.py プロジェクト: moneytech/PeachPy
class MachOWriter:
    def __init__(self, output_path, abi):
        from peachpy.formats.macho.image import Image

        self.output_path = output_path
        self.previous_writer = None
        self.abi = abi
        self.image = Image(abi)

    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.output_file.write(self.image.encode())
            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"

        from peachpy.formats.macho.symbol import Symbol, SymbolType, SymbolDescription, SymbolVisibility, \
            Relocation, RelocationType
        from peachpy.util import roundup

        encoded_function = function.encode()

        code_offset = len(self.image.text_section.content)
        code_padding = bytearray([encoded_function.code_section.alignment_byte] *
                                 (roundup(code_offset, encoded_function.code_section.alignment) - code_offset))
        self.image.text_section.content += code_padding
        code_offset += len(code_padding)
        self.image.text_section.content += encoded_function.code_section.content
        self.image.text_section.alignment = \
            max(self.image.text_section.alignment, encoded_function.code_section.alignment)

        const_offset = self.image.const_section.content_size
        const_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(const_offset, encoded_function.const_section.alignment) - const_offset))
        self.image.text_section.content += const_padding
        const_offset += len(const_padding)
        self.image.const_section.content += encoded_function.const_section.content
        self.image.const_section.alignment = \
            max(self.image.const_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            macho_symbol = Symbol(symbol.name,
                                  SymbolType.section_relative, self.image.const_section,
                                  const_offset + symbol.offset)
            macho_symbol.description = SymbolDescription.defined
            self.image.symbol_table.add_symbol(macho_symbol)
            symbol_map[symbol] = macho_symbol

        for relocation in encoded_function.code_section.relocations:
            macho_relocation = Relocation(RelocationType.x86_64_signed, code_offset + relocation.offset, 4,
                                          symbol_map[relocation.symbol], is_pc_relative=True)
            self.image.text_section.relocations.append(macho_relocation)

        function_symbol = Symbol("_" + function.name, SymbolType.section_relative, self.image.text_section,
                                 value=code_offset)
        function_symbol.description = SymbolDescription.defined
        function_symbol.visibility = SymbolVisibility.external
        self.image.symbol_table.add_symbol(function_symbol)
コード例 #19
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
class MSCOFFWriter(ImageWriter):
    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)

    def encode(self):
        return self.image.encode()

    def add_function(self, function):
        import peachpy.x86_64.function
        assert isinstance(function, peachpy.x86_64.function.ABIFunction), \
            "Function must be finalized with an ABI before its assembly can be used"
        from peachpy.util import roundup
        from peachpy.formats.mscoff import Symbol, SymbolType, StorageClass, Relocation, RelocationType

        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))
        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)

        rdata_offset = self.rdata_section.content_size
        rdata_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(rdata_offset, encoded_function.const_section.alignment) - rdata_offset))
        self.rdata_section.content += rdata_padding
        rdata_offset += len(rdata_padding)
        self.rdata_section.content += encoded_function.const_section.content
        self.rdata_section.alignment = \
            max(self.rdata_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            mscoff_symbol = Symbol()
            mscoff_symbol.name = symbol.name
            mscoff_symbol.value = rdata_offset + symbol.offset
            mscoff_symbol.section = self.rdata_section
            mscoff_symbol.symbol_type = SymbolType.non_function
            mscoff_symbol.storage_class = StorageClass.static
            self.image.add_symbol(mscoff_symbol)
            symbol_map[symbol] = mscoff_symbol

        for relocation in encoded_function.code_section.relocations:
            relocation_type_map = {
                4: RelocationType.x86_64_relocation_offset32,
                5: RelocationType.x86_64_relocation_plus_1_offset32,
                6: RelocationType.x86_64_relocation_plus_2_offset32,
                7: RelocationType.x86_64_relocation_plus_3_offset32,
                8: RelocationType.x86_64_relocation_plus_4_offset32,
                9: RelocationType.x86_64_relocation_plus_5_offset32
            }
            relocation_type = relocation_type_map[relocation.program_counter - relocation.offset]
            mscoff_relocation = Relocation(relocation_type,
                                           code_offset + relocation.offset,
                                           symbol_map[relocation.symbol])
            self.text_section.relocations.append(mscoff_relocation)

        function_symbol = Symbol()
        function_symbol.name = function.mangled_name
        function_symbol.value = code_offset
        function_symbol.section = self.text_section
        function_symbol.symbol_type = SymbolType.function
        function_symbol.storage_class = StorageClass.external
        self.image.add_symbol(function_symbol)
コード例 #20
0
ファイル: writer.py プロジェクト: Rinoahu/PeachPy
class MachOWriter(ImageWriter):
    def __init__(self, output_path, abi):
        super(MachOWriter, self).__init__(output_path)

        from peachpy.formats.macho.image import Image

        self.abi = abi
        self.image = Image(abi)

    def encode(self):
        return self.image.encode()

    def add_function(self, function):
        import peachpy.x86_64.function
        assert isinstance(function, peachpy.x86_64.function.ABIFunction), \
            "Function must be finalized with an ABI before its assembly can be used"

        from peachpy.formats.macho.symbol import Symbol, SymbolType, SymbolDescription, SymbolVisibility, \
            Relocation, RelocationType
        from peachpy.util import roundup

        encoded_function = function.encode()

        code_offset = len(self.image.text_section.content)
        code_padding = bytearray([encoded_function.code_section.alignment_byte] *
                                 (roundup(code_offset, encoded_function.code_section.alignment) - code_offset))
        self.image.text_section.content += code_padding
        code_offset += len(code_padding)
        self.image.text_section.content += encoded_function.code_section.content
        self.image.text_section.alignment = \
            max(self.image.text_section.alignment, encoded_function.code_section.alignment)

        const_offset = self.image.const_section.content_size
        const_padding = bytearray([encoded_function.const_section.alignment_byte] *
                                  (roundup(const_offset, encoded_function.const_section.alignment) - const_offset))
        self.image.const_section.content += const_padding
        const_offset += len(const_padding)
        self.image.const_section.content += encoded_function.const_section.content
        self.image.const_section.alignment = \
            max(self.image.const_section.alignment, encoded_function.const_section.alignment)

        # Map from PeachPy symbol to Mach-O symbol
        symbol_map = dict()
        for symbol in encoded_function.const_section.symbols:
            macho_symbol = Symbol("_" + function.mangled_name + "." + symbol.name,
                                  SymbolType.section_relative, self.image.const_section,
                                  const_offset + symbol.offset)
            macho_symbol.description = SymbolDescription.defined
            self.image.symbol_table.add_symbol(macho_symbol)
            symbol_map[symbol] = macho_symbol

        for relocation in encoded_function.code_section.relocations:
            macho_relocation = Relocation(RelocationType.x86_64_signed, code_offset + relocation.offset, 4,
                                          symbol_map[relocation.symbol], is_pc_relative=True)
            relocation_addend = relocation.offset + 4 - relocation.program_counter
            if relocation_addend != 0:
                self.image.text_section.content[code_offset + relocation.offset] = relocation_addend & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 1] = (relocation_addend >> 8) & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 2] = (relocation_addend >> 16) & 0xFF
                self.image.text_section.content[code_offset + relocation.offset + 3] = (relocation_addend >> 24) & 0xFF

            self.image.text_section.relocations.append(macho_relocation)

        function_symbol = Symbol("_" + function.mangled_name, SymbolType.section_relative, self.image.text_section,
                                 value=code_offset)
        function_symbol.description = SymbolDescription.defined
        function_symbol.visibility = SymbolVisibility.external
        self.image.symbol_table.add_symbol(function_symbol)