Ejemplo n.º 1
0
    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.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.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.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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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.image.text_section.content)

        self.image.text_section.append(function_code)

        from peachpy.formats.macho.symbol import Symbol, SymbolDescription, SymbolType, SymbolVisibility

        function_symbol = Symbol(self.abi)
        function_symbol.description = SymbolDescription.Defined
        function_symbol.type = SymbolType.SectionRelative
        function_symbol.visibility = SymbolVisibility.External
        function_symbol.string_index = self.image.string_table.add("_" + function.name)
        function_symbol.section_index = self.image.text_section.index
        function_symbol.value = function_offset
        self.image.symbols.append(function_symbol)