Esempio n. 1
0
    def encode_header(self,
                      encoder,
                      name_index_map,
                      section_index_map,
                      offset,
                      address=None,
                      link_section=None,
                      info=None,
                      content_size=0,
                      entry_size=0):
        import peachpy.encoder
        from peachpy.util import is_uint64, is_uint32
        assert isinstance(encoder, peachpy.encoder.Encoder)
        assert isinstance(name_index_map, dict)
        assert section_index_map is None or isinstance(section_index_map, dict)
        assert offset is None or is_uint64(offset)
        assert address is None or is_uint64(address)
        assert link_section is None or isinstance(link_section, Section)
        assert info is None or is_uint64(info)
        assert is_uint64(content_size)
        assert is_uint32(entry_size)

        assert encoder.bitness in [32, 64]
        if encoder.bitness == 32:
            assert offset is None or is_uint32(offset)
            assert address is None or is_uint32(address)
        assert self.name is None or self.name in name_index_map
        assert section_index_map is not None or link_section is None

        name_index = name_index_map.get(self.name, 0)
        if address is None:
            address = 0
        if offset is None:
            offset = 0
        link = 0
        if link_section is not None:
            link = section_index_map[link_section]
        if info is None:
            info = 0
        return encoder.uint32(name_index) + \
            encoder.uint32(self.type) + \
            encoder.unsigned_offset(self.flags) + \
            encoder.unsigned_offset(address) + \
            encoder.unsigned_offset(offset) + \
            encoder.unsigned_offset(content_size) + \
            encoder.uint32(link) + \
            encoder.uint32(info) + \
            encoder.unsigned_offset(self.alignment) + \
            encoder.unsigned_offset(entry_size)
Esempio n. 2
0
 def alignment(self, alignment):
     from peachpy.util import is_uint32
     if not is_uint32(alignment):
         raise TypeError("Section alignment %s is not representable as a 32-bit unsigned integer" % str(alignment))
     if alignment & (alignment - 1) != 0:
         raise ValueError("Section alignment %d is not a power of 2" % alignment)
     if alignment == 0:
         alignment = 1
     self._alignment = alignment
Esempio n. 3
0
    def encode_header(self, encoder, name_index_map, section_index_map, offset,
                      address=None, link_section=None, info=None,
                      content_size=0, entry_size=0):
        import peachpy.encoder
        from peachpy.util import is_uint64, is_uint32
        assert isinstance(encoder, peachpy.encoder.Encoder)
        assert isinstance(name_index_map, dict)
        assert section_index_map is None or isinstance(section_index_map, dict)
        assert offset is None or is_uint64(offset)
        assert address is None or is_uint64(address)
        assert link_section is None or isinstance(link_section, Section)
        assert info is None or is_uint64(info)
        assert is_uint64(content_size)
        assert is_uint32(entry_size)

        assert encoder.bitness in [32, 64]
        if encoder.bitness == 32:
            assert offset is None or is_uint32(offset)
            assert address is None or is_uint32(address)
        assert self.name is None or self.name in name_index_map
        assert section_index_map is not None or link_section is None

        name_index = name_index_map.get(self.name, 0)
        if address is None:
            address = 0
        if offset is None:
            offset = 0
        link = 0
        if link_section is not None:
            link = section_index_map[link_section]
        if info is None:
            info = 0
        return encoder.uint32(name_index) + \
            encoder.uint32(self.type) + \
            encoder.unsigned_offset(self.flags) + \
            encoder.unsigned_offset(address) + \
            encoder.unsigned_offset(offset) + \
            encoder.unsigned_offset(content_size) + \
            encoder.uint32(link) + \
            encoder.uint32(info) + \
            encoder.unsigned_offset(self.alignment) + \
            encoder.unsigned_offset(entry_size)
Esempio n. 4
0
    def __init__(self, type, offset, symbol):
        from peachpy.util import is_int, is_uint32
        if not isinstance(type, RelocationType):
            raise TypeError("Relocation type %s is not in RelocationType enumeration" % str(type))
        if not is_int(offset):
            raise TypeError("Offset %s is not an integer" % str(offset))
        if not is_uint32(offset):
            raise ValueError("Offset %d can not be represented as a 32-bit unsigned integer" % offset)
        if not isinstance(symbol, Symbol):
            raise TypeError("Symbol %s is not an instance of Symbol type" % str(symbol))

        self.type = type
        self.offset = offset
        self.symbol = symbol
Esempio n. 5
0
    def __init__(self, name, flags, alignment=None):
        from peachpy.util import is_uint32
        if not isinstance(name, str):
            raise TypeError("Section name %s is not a string" % str(name))
        if not is_uint32(flags):
            raise TypeError("Flags %s are not representable as a 32-bit unsigned integer" % str(flags))

        super(Section, self).__init__()
        # Section name
        self.name = name
        # Flags for the section
        self.flags = (flags & ~Section._alignment_mask) | Section._alignment_flag_map[1]
        if alignment is not None:
            self.alignment = alignment

        self.relocations = list()
        self.content = bytearray()
Esempio n. 6
0
    def __init__(self, type, offset, symbol):
        from peachpy.util import is_int, is_uint32
        if not isinstance(type, RelocationType):
            raise TypeError(
                "Relocation type %s is not in RelocationType enumeration" %
                str(type))
        if not is_int(offset):
            raise TypeError("Offset %s is not an integer" % str(offset))
        if not is_uint32(offset):
            raise ValueError(
                "Offset %d can not be represented as a 32-bit unsigned integer"
                % offset)
        if not isinstance(symbol, Symbol):
            raise TypeError("Symbol %s is not an instance of Symbol type" %
                            str(symbol))

        self.type = type
        self.offset = offset
        self.symbol = symbol
Esempio n. 7
0
    def __init__(self, name, flags, alignment=None):
        from peachpy.util import is_uint32
        if not isinstance(name, str):
            raise TypeError("Section name %s is not a string" % str(name))
        if not is_uint32(flags):
            raise TypeError(
                "Flags %s are not representable as a 32-bit unsigned integer" %
                str(flags))

        super(Section, self).__init__()
        # Section name
        self.name = name
        # Flags for the section
        self.flags = (
            flags & ~Section._alignment_mask) | Section._alignment_flag_map[1]
        if alignment is not None:
            self.alignment = alignment

        self.relocations = list()
        self.content = bytearray()