Mercurial > lcfOS
annotate python/ppci/objectfile.py @ 343:11c5a8a70c02 devel
Fix ide
author | Windel Bouwman |
---|---|
date | Sat, 01 Mar 2014 16:27:52 +0100 |
parents | 86b02c98a717 |
children | 9667d78ba79e |
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: | |
342 | 20 """ Represents a relocation entry. A relocation always has a symbol to refer to |
21 and a relocation type """ | |
335 | 22 def __init__(self, sym, offset, typ, section): |
23 self.sym = sym | |
24 self.offset = offset | |
25 self.typ = typ | |
26 self.section = section | |
27 | |
28 def __repr__(self): | |
29 return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section) | |
334 | 30 |
31 | |
32 class Section: | |
33 def __init__(self, name): | |
34 self.name = name | |
335 | 35 self.address = 0 |
36 self.data = bytearray() | |
37 | |
38 def add_data(self, data): | |
39 self.data += data | |
40 | |
41 @property | |
42 def Size(self): | |
43 return len(self.data) | |
44 | |
45 def __repr__(self): | |
46 return 'SECTION {}'.format(self.name) | |
334 | 47 |
48 | |
49 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
|
50 """ 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
|
51 Also contains symbols and relocation entries """ |
334 | 52 def __init__(self): |
53 self.symbols = {} | |
54 self.sections = {} | |
55 self.relocations = [] | |
56 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
57 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
|
58 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
|
59 |
335 | 60 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
|
61 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
|
62 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
|
63 assert section in self.sections |
335 | 64 sym = Symbol(name, value, section) |
334 | 65 self.symbols[name] = sym |
66 return sym | |
335 | 67 |
68 def add_relocation(self, sym_name, offset, typ, section): | |
342 | 69 assert type(sym_name) is str, str(sym_name) |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
70 assert section in self.sections |
335 | 71 reloc = Relocation(sym_name, offset, typ, section) |
72 self.relocations.append(reloc) | |
73 return reloc | |
74 | |
75 def get_section(self, name): | |
76 if not name in self.sections: | |
77 self.sections[name] = Section(name) | |
78 return self.sections[name] |