#!/usr/bin/env python
# I, Danny Milosavljevic, hereby place this file into the public domain

import elf
import struct
import symbols
assert(struct.calcsize("Q") == 8)
class ELFWriter(elf.ELFWriter):
    offFormat = "Q"
    addrFormat = "Q"
    bigFormat = "Q"
    bigSignedFormat = "q"
    typeMachineVersion = (1, 0x3e, 1)
    ELFMagic = "\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    def writeRelTextEntry(self, addr, symbolIndex, offset, typ):
        assert(offset == 0)
        stream = self.stream
        stream.write(struct.pack(self.__class__.addrFormat, addr))
        stream.write(struct.pack(self.__class__.bigFormat, (symbolIndex << 32) | typ)) # symbol = value >> 8, type = value & 0xFF
    def writeRelaTextEntry(self, addr, symbolIndex, offset, typ):
        stream = self.stream
        stream.write(struct.pack(self.__class__.addrFormat, addr))
        stream.write(struct.pack(self.__class__.bigFormat, (symbolIndex << 32) | typ)) # symbol = value >> 8, type = value & 0xFF
        stream.write(struct.pack(self.__class__.bigSignedFormat, offset))
    