annotate python/ppci/objectfile.py @ 336:d1ecc493384e

Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
author Windel Bouwman
date Wed, 19 Feb 2014 22:32:15 +0100
parents 582a1aaa3983
children 86b02c98a717
rev   line source
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
1
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
2 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
3 Object files are used to store assembled code. Information contained
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
4 is code, symbol table and relocation information.
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
5 """
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
6
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
7 from . import CompilerError
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
8
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
9 class Symbol:
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
10 def __init__(self, name, value, section):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
11 self.name = name
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
12 self.value = value
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
13 self.section = section
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
14
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
15 def __repr__(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
16 return 'SYM {}, val={} sec={}'.format(self.name, self.value, self.section)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
17
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
18
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
19 class Relocation:
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
20 def __init__(self, sym, offset, typ, section):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
21 self.sym = sym
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
22 self.offset = offset
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
23 self.typ = typ
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
24 self.section = section
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
25
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
26 def __repr__(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
27 return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
28
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
29
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
30 class Section:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
31 def __init__(self, name):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
32 self.name = name
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
33 self.address = 0
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
34 self.data = bytearray()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
35
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
36 def add_data(self, data):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
37 self.data += data
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
38
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
39 @property
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
40 def Size(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
41 return len(self.data)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
42
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
43 def __repr__(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
44 return 'SECTION {}'.format(self.name)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
45
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
46
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
47 class ObjectFile:
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
48 """ Container for sections with compiled code or data.
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
49 Also contains symbols and relocation entries """
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
50 def __init__(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
51 self.symbols = {}
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
52 self.sections = {}
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
53 self.relocations = []
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
54
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
55 def find_symbol(self, name):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
56 return self.symbols[name]
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
57
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
58 def add_symbol(self, name, value, section):
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
59 if name in self.symbols:
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
60 raise CompilerError('{} already defined'.format(name))
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
61 assert section in self.sections
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
62 sym = Symbol(name, value, section)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
63 self.symbols[name] = sym
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
64 return sym
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
65
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
66 def add_relocation(self, sym_name, offset, typ, section):
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 335
diff changeset
67 assert section in self.sections
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
68 reloc = Relocation(sym_name, offset, typ, section)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
69 self.relocations.append(reloc)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
70 return reloc
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
71
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
72 def get_section(self, name):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
73 if not name in self.sections:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
74 self.sections[name] = Section(name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
75 return self.sections[name]