Mercurial > lcfOS
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 | 1 |
2 """ | |
3 Object files are used to store assembled code. Information contained | |
4 is code, symbol table and relocation information. | |
5 """ | |
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 | 9 class Symbol: |
335 | 10 def __init__(self, name, value, section): |
334 | 11 self.name = name |
12 self.value = value | |
335 | 13 self.section = section |
14 | |
15 def __repr__(self): | |
16 return 'SYM {}, val={} sec={}'.format(self.name, self.value, self.section) | |
334 | 17 |
18 | |
19 class Relocation: | |
335 | 20 def __init__(self, sym, offset, typ, section): |
21 self.sym = sym | |
22 self.offset = offset | |
23 self.typ = typ | |
24 self.section = section | |
25 | |
26 def __repr__(self): | |
27 return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section) | |
334 | 28 |
29 | |
30 class Section: | |
31 def __init__(self, name): | |
32 self.name = name | |
335 | 33 self.address = 0 |
34 self.data = bytearray() | |
35 | |
36 def add_data(self, data): | |
37 self.data += data | |
38 | |
39 @property | |
40 def Size(self): | |
41 return len(self.data) | |
42 | |
43 def __repr__(self): | |
44 return 'SECTION {}'.format(self.name) | |
334 | 45 |
46 | |
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 | 50 def __init__(self): |
51 self.symbols = {} | |
52 self.sections = {} | |
53 self.relocations = [] | |
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 | 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 | 62 sym = Symbol(name, value, section) |
334 | 63 self.symbols[name] = sym |
64 return sym | |
335 | 65 |
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 | 68 reloc = Relocation(sym_name, offset, typ, section) |
69 self.relocations.append(reloc) | |
70 return reloc | |
71 | |
72 def get_section(self, name): | |
73 if not name in self.sections: | |
74 self.sections[name] = Section(name) | |
75 return self.sections[name] |