view python/ppci/objectfile.py @ 335:582a1aaa3983

Added long branch format
author Windel Bouwman
date Mon, 17 Feb 2014 20:41:30 +0100
parents 6f4753202b9a
children d1ecc493384e
line wrap: on
line source


"""
Object files are used to store assembled code. Information contained
is code, symbol table and relocation information.
"""

class Symbol:
    def __init__(self, name, value, section):
        self.name = name
        self.value = value
        self.section = section

    def __repr__(self):
        return 'SYM {}, val={} sec={}'.format(self.name, self.value, self.section)


class Relocation:
    def __init__(self, sym, offset, typ, section):
        self.sym = sym
        self.offset = offset
        self.typ = typ
        self.section = section

    def __repr__(self):
        return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section)


class Section:
    def __init__(self, name):
        self.name = name
        self.address = 0
        self.data = bytearray()

    def add_data(self, data):
        self.data += data

    @property
    def Size(self):
        return len(self.data)

    def __repr__(self):
        return 'SECTION {}'.format(self.name)


class ObjectFile:
    def __init__(self):
        self.symbols = {}
        self.sections = {}
        self.relocations = []

    def add_symbol(self, name, value, section):
        sym = Symbol(name, value, section)
        self.symbols[name] = sym
        return sym

    def add_relocation(self, sym_name, offset, typ, section):
        reloc = Relocation(sym_name, offset, typ, section)
        self.relocations.append(reloc)
        return reloc

    def get_section(self, name):
        if not name in self.sections:
            self.sections[name] = Section(name)
        return self.sections[name]