comparison 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
comparison
equal deleted inserted replaced
334:6f4753202b9a 335:582a1aaa3983
3 Object files are used to store assembled code. Information contained 3 Object files are used to store assembled code. Information contained
4 is code, symbol table and relocation information. 4 is code, symbol table and relocation information.
5 """ 5 """
6 6
7 class Symbol: 7 class Symbol:
8 def __init__(self, name, value): 8 def __init__(self, name, value, section):
9 self.name = name 9 self.name = name
10 self.value = value 10 self.value = value
11 self.section = section
12
13 def __repr__(self):
14 return 'SYM {}, val={} sec={}'.format(self.name, self.value, self.section)
11 15
12 16
13 class Relocation: 17 class Relocation:
14 def __init__(self, typ): 18 def __init__(self, sym, offset, typ, section):
15 pass 19 self.sym = sym
20 self.offset = offset
21 self.typ = typ
22 self.section = section
23
24 def __repr__(self):
25 return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section)
16 26
17 27
18 class Section: 28 class Section:
19 def __init__(self, name): 29 def __init__(self, name):
20 self.name = name 30 self.name = name
31 self.address = 0
32 self.data = bytearray()
33
34 def add_data(self, data):
35 self.data += data
36
37 @property
38 def Size(self):
39 return len(self.data)
40
41 def __repr__(self):
42 return 'SECTION {}'.format(self.name)
21 43
22 44
23 class ObjectFile: 45 class ObjectFile:
24 def __init__(self): 46 def __init__(self):
25 self.symbols = {} 47 self.symbols = {}
26 self.sections = {} 48 self.sections = {}
27 self.relocations = [] 49 self.relocations = []
28 50
29 def add_symbol(self, name, value): 51 def add_symbol(self, name, value, section):
30 sym = Symbol(name, value) 52 sym = Symbol(name, value, section)
31 self.symbols[name] = sym 53 self.symbols[name] = sym
32 return sym 54 return sym
55
56 def add_relocation(self, sym_name, offset, typ, section):
57 reloc = Relocation(sym_name, offset, typ, section)
58 self.relocations.append(reloc)
59 return reloc
60
61 def get_section(self, name):
62 if not name in self.sections:
63 self.sections[name] = Section(name)
64 return self.sections[name]