Exemplo n.º 1
0
    def as_bytearray(self):
        import peachpy.encoder

        encoder = peachpy.encoder.Encoder(self.abi.endianness)

        if self.abi.pointer_size == 4:
            return encoder.fixed_string(self.name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint32(self.address or 0) + \
                encoder.uint32(self.content_size) + \
                encoder.uint32(self.offset) + \
                encoder.uint32(self.alignment) + \
                encoder.uint32(self.relocation_offset or 0) + \
                encoder.uint32(self.relocation_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(8)
        else:
            return encoder.fixed_string(self.name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint64(self.address or 0) + \
                encoder.uint64(self.content_size) + \
                encoder.uint32(self.offset) + \
                encoder.uint32(self.alignment) + \
                encoder.uint32(self.relocation_offset or 0) + \
                encoder.uint32(self.relocation_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(12)
Exemplo n.º 2
0
    def as_bytearray(self):
        import peachpy.encoder

        encoder = peachpy.encoder.Encoder(self.abi.endianness)

        if self.abi.pointer_size == 4:
            return (
                encoder.fixed_string(self.name, 16)
                + encoder.fixed_string(self.segment_name, 16)
                + encoder.uint32(self.address or 0)
                + encoder.uint32(self.content_size)
                + encoder.uint32(self.offset)
                + encoder.uint32(self.alignment)
                + encoder.uint32(self.relocation_offset or 0)
                + encoder.uint32(self.relocation_count)
                + encoder.uint32(self.type | self.attributes)
                + bytearray(8)
            )
        else:
            return (
                encoder.fixed_string(self.name, 16)
                + encoder.fixed_string(self.segment_name, 16)
                + encoder.uint64(self.address or 0)
                + encoder.uint64(self.content_size)
                + encoder.uint32(self.offset)
                + encoder.uint32(self.alignment)
                + encoder.uint32(self.relocation_offset or 0)
                + encoder.uint32(self.relocation_count)
                + encoder.uint32(self.type | self.attributes)
                + bytearray(12)
            )
Exemplo n.º 3
0
    def encode_command(self, encoder, offset, address, relocations_offset):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)

        if len(self.relocations) == 0:
            relocations_offset = 0
        if encoder.bitness == 32:
            return encoder.fixed_string(self.section_name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint32(address) + \
                encoder.uint32(self.content_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(self.log2_alignment) + \
                encoder.uint32(relocations_offset) + \
                encoder.uint32(self.relocations_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(8)
        else:
            return encoder.fixed_string(self.section_name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint64(address) + \
                encoder.uint64(self.content_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(self.log2_alignment) + \
                encoder.uint32(relocations_offset) + \
                encoder.uint32(self.relocations_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(12)
Exemplo n.º 4
0
    def encode(self, encoder, name_index_map, section_index_map):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)
        assert encoder.bitness in [32, 64]
        assert self.name in name_index_map
        from peachpy.formats.elf.section import SectionIndex
        assert self.section is None or isinstance(self.section, SectionIndex) or self.section in section_index_map

        name_index = name_index_map[self.name]
        section_index = SectionIndex.absolute
        if self.section is not None:
            if isinstance(self.section, SectionIndex):
                section_index = self.section
            else:
                section_index = section_index_map[self.section]
        if encoder.bitness == 32:
            return encoder.uint32(name_index) + \
                encoder.uint32(self.value) + \
                encoder.uint32(self.size) + \
                encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
                encoder.uint8(0) + \
                encoder.uint16(section_index)
        else:
            return encoder.uint32(name_index) + \
                encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
                encoder.uint8(0) + \
                encoder.uint16(section_index) + \
                encoder.uint64(self.value) + \
                encoder.uint64(self.size)
Exemplo n.º 5
0
    def encode(self, encoder, name_index_map, section_index_map):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)
        assert encoder.bitness in [32, 64]
        assert self.name in name_index_map
        from peachpy.formats.elf.section import SectionIndex
        assert self.section is None or isinstance(
            self.section, SectionIndex) or self.section in section_index_map

        name_index = name_index_map[self.name]
        section_index = SectionIndex.absolute
        if self.section is not None:
            if isinstance(self.section, SectionIndex):
                section_index = self.section
            else:
                section_index = section_index_map[self.section]
        if encoder.bitness == 32:
            return encoder.uint32(name_index) + \
                encoder.uint32(self.value) + \
                encoder.uint32(self.size) + \
                encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
                encoder.uint8(0) + \
                encoder.uint16(section_index)
        else:
            return encoder.uint32(name_index) + \
                encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
                encoder.uint8(0) + \
                encoder.uint16(section_index) + \
                encoder.uint64(self.value) + \
                encoder.uint64(self.size)
Exemplo n.º 6
0
    def as_bytearray(self):
        import peachpy.encoder
        encoder = peachpy.encoder.Encoder(self.abi.endianness)

        if self.abi.pointer_size == 4:
            return encoder.uint32(self.id) + \
                   encoder.uint32(self.size) + \
                   encoder.fixed_string(self.name, 16) + \
                   encoder.uint32(self.address or 0) + \
                   encoder.uint32(self.memory_size) + \
                   encoder.uint32(self.offset) + \
                   encoder.uint32(self.file_size) + \
                   encoder.uint32(MemoryProtection.Default) + \
                   encoder.uint32(MemoryProtection.Default) + \
                   encoder.uint32(self.section_count) + \
                   encoder.uint32(self.flags)
        else:
            return encoder.uint32(self.id) + \
                   encoder.uint32(self.size) + \
                   encoder.fixed_string(self.name, 16) + \
                   encoder.uint64(self.address or 0) + \
                   encoder.uint64(self.memory_size) + \
                   encoder.uint64(self.offset) + \
                   encoder.uint64(self.file_size) + \
                   encoder.uint32(MemoryProtection.Default) + \
                   encoder.uint32(MemoryProtection.Default) + \
                   encoder.uint32(self.section_count) + \
                   encoder.uint32(self.flags)
Exemplo n.º 7
0
    def encode_command(self, encoder, offset, address, relocations_offset):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)

        if len(self.relocations) == 0:
            relocations_offset = 0
        if encoder.bitness == 32:
            return encoder.fixed_string(self.section_name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint32(address) + \
                encoder.uint32(self.content_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(self.log2_alignment) + \
                encoder.uint32(relocations_offset) + \
                encoder.uint32(self.relocations_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(8)
        else:
            return encoder.fixed_string(self.section_name, 16) + \
                encoder.fixed_string(self.segment_name, 16) + \
                encoder.uint64(address) + \
                encoder.uint64(self.content_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(self.log2_alignment) + \
                encoder.uint32(relocations_offset) + \
                encoder.uint32(self.relocations_count) + \
                encoder.uint32(self.type | self.attributes) + \
                bytearray(12)
Exemplo n.º 8
0
    def encode_command(self, encoder, section_offset_map, section_address_map,
                       section_relocations_map):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)

        offset = section_offset_map[self.sections[0]]
        memory_size = section_address_map[
            self.sections[-1]] + self.sections[-1].content_size
        file_size = sum(section.content_size for section in self.sections)

        address = 0
        if self.sections:
            address = section_address_map[self.sections[0]]
        # TODO: combine the two cases
        if encoder.bitness == 32:
            command_id = 0x1
            command_size = 56 + len(self.sections) * 68
            command = encoder.uint32(command_id) + \
                encoder.uint32(command_size) + \
                encoder.fixed_string(self.name, 16) + \
                encoder.uint32(address) + \
                encoder.uint32(memory_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(file_size) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(len(self.sections)) + \
                encoder.uint32(self.flags)
        else:
            command_id = 0x19
            command_size = 72 + len(self.sections) * 80
            command = encoder.uint32(command_id) + \
                encoder.uint32(command_size) + \
                encoder.fixed_string(self.name, 16) + \
                encoder.uint64(address) + \
                encoder.uint64(memory_size) + \
                encoder.uint64(offset) + \
                encoder.uint64(file_size) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(len(self.sections)) + \
                encoder.uint32(self.flags)
        for section in self.sections:
            command += section.encode_command(
                encoder, section_offset_map[section],
                section_address_map[section],
                section_relocations_map.get(section))
        from peachpy.x86_64.abi import system_v_x86_64_abi
        return command
Exemplo n.º 9
0
    def encode_command(self, encoder, section_offset_map, section_address_map, section_relocations_map):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)

        offset = section_offset_map[self.sections[0]]
        memory_size = sum(section.content_size for section in self.sections)
        file_size = sum(section.content_size for section in self.sections)

        address = 0
        if self.sections:
            address = section_address_map[self.sections[0]]
        # TODO: combine the two cases
        if encoder.bitness == 32:
            command_id = 0x1
            command_size = 56 + len(self.sections) * 68
            command = encoder.uint32(command_id) + \
                encoder.uint32(command_size) + \
                encoder.fixed_string(self.name, 16) + \
                encoder.uint32(address) + \
                encoder.uint32(memory_size) + \
                encoder.uint32(offset) + \
                encoder.uint32(file_size) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(len(self.sections)) + \
                encoder.uint32(self.flags)
        else:
            command_id = 0x19
            command_size = 72 + len(self.sections) * 80
            command = encoder.uint32(command_id) + \
                encoder.uint32(command_size) + \
                encoder.fixed_string(self.name, 16) + \
                encoder.uint64(address) + \
                encoder.uint64(memory_size) + \
                encoder.uint64(offset) + \
                encoder.uint64(file_size) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(MemoryProtection.default) + \
                encoder.uint32(len(self.sections)) + \
                encoder.uint32(self.flags)
        for section in self.sections:
            command += section.encode_command(encoder,
                                              section_offset_map[section],
                                              section_address_map[section],
                                              section_relocations_map.get(section))
        from peachpy.x86_64.abi import system_v_x86_64_abi
        return command
Exemplo n.º 10
0
 def as_bytearray(self):
     import peachpy.encoder
     encoder = peachpy.encoder.Encoder(self.abi.endianness)
     if self.abi.elf_class == ElfClass.Class32:
         return encoder.uint32(self.name_index) + \
             encoder.uint32(self.value) + \
             encoder.uint32(self.content_size) + \
             encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
             encoder.uint8(0) + \
             encoder.uint16(self.section_index)
     else:
         return encoder.uint32(self.name_index) + \
             encoder.uint8((self.binding << 4) | (self.type & 0xF)) + \
             encoder.uint8(0) + \
             encoder.uint16(self.section_index) + \
             encoder.uint64(self.value) + \
             encoder.uint64(self.content_size)
Exemplo n.º 11
0
 def as_bytearray(self):
     import peachpy.encoder
     encoder = peachpy.encoder.Encoder(self.abi.endianness)
     if self.abi.elf_class == ElfClass.Class32:
         return encoder.uint32(self.name_index) + \
             encoder.uint32(self.content_type) + \
             encoder.uint32(self.flags) + \
             encoder.uint32(self.address or 0) + \
             encoder.uint32(self.offset or 0) + \
             encoder.uint32(self.content_size) + \
             encoder.uint32(self.link_index) + \
             encoder.uint32(self.info) + \
             encoder.uint32(self.address_alignment) + \
             encoder.uint32(self.entry_size)
     else:
         return encoder.uint32(self.name_index) + \
             encoder.uint32(self.content_type) + \
             encoder.uint64(self.flags) + \
             encoder.uint64(self.address or 0) + \
             encoder.uint64(self.offset or 0) + \
             encoder.uint64(self.content_size) + \
             encoder.uint32(self.link_index or 0) + \
             encoder.uint32(self.info) + \
             encoder.uint64(self.address_alignment) + \
             encoder.uint64(self.entry_size)
Exemplo n.º 12
0
 def as_bytearray(self):
     import peachpy.encoder
     encoder = peachpy.encoder.Encoder(self.abi.endianness)
     if self.abi.elf_class == ElfClass.Class32:
         return encoder.uint32(self.name_index) + \
             encoder.uint32(self.content_type) + \
             encoder.uint32(self.flags) + \
             encoder.uint32(self.address or 0) + \
             encoder.uint32(self.offset or 0) + \
             encoder.uint32(self.content_size) + \
             encoder.uint32(self.link_index) + \
             encoder.uint32(self.info) + \
             encoder.uint32(self.address_alignment) + \
             encoder.uint32(self.entry_size)
     else:
         return encoder.uint32(self.name_index) + \
             encoder.uint32(self.content_type) + \
             encoder.uint64(self.flags) + \
             encoder.uint64(self.address or 0) + \
             encoder.uint64(self.offset or 0) + \
             encoder.uint64(self.content_size) + \
             encoder.uint32(self.link_index or 0) + \
             encoder.uint32(self.info) + \
             encoder.uint64(self.address_alignment) + \
             encoder.uint64(self.entry_size)
Exemplo n.º 13
0
    def as_bytearray(self):
        import peachpy.encoder
        encoder = peachpy.encoder.Encoder(self.abi.endianness)

        if self.abi.pointer_size == 4:
            return encoder.uint32(self.string_index) + \
                encoder.uint8(self.type | self.visibility) + \
                encoder.uint8(self.section_index) + \
                encoder.uint16(self.description | self.flags) + \
                encoder.uint32(self.value)
        else:
            return encoder.uint32(self.string_index) + \
                encoder.uint8(self.type | self.visibility) + \
                encoder.uint8(self.section_index) + \
                encoder.uint16(self.description | self.flags) + \
                encoder.uint64(self.value)
Exemplo n.º 14
0
    def encode(self, encoder, name_index_map, section_index_map, section_address_map):
        import peachpy.encoder
        assert isinstance(encoder, peachpy.encoder.Encoder)
        assert self.name in name_index_map
        assert self.section is None or self.section in section_index_map

        name_index = name_index_map[self.name]
        section_index = 0
        if self.section is not None:
            section_index = section_index_map[self.section]
        if encoder.bitness == 32:
            return encoder.uint32(name_index) + \
                encoder.uint8(self.type | self.visibility) + \
                encoder.uint8(section_index) + \
                encoder.uint16(self.description | self.flags) + \
                encoder.uint32(self.value)
        else:
            return encoder.uint32(name_index) + \
                encoder.uint8(self.type | self.visibility) + \
                encoder.uint8(section_index) + \
                encoder.uint16(self.description | self.flags) + \
                encoder.uint64(self.value + section_address_map[self.section])