annotate python/ppci/objectfile.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents d1ecc493384e
children 9667d78ba79e
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:
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 336
diff changeset
20 """ Represents a relocation entry. A relocation always has a symbol to refer to
86b02c98a717 Moved target directory
Windel Bouwman
parents: 336
diff changeset
21 and a relocation type """
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
22 def __init__(self, sym, offset, typ, section):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
23 self.sym = sym
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
24 self.offset = offset
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
25 self.typ = typ
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
26 self.section = section
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
27
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
28 def __repr__(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
29 return 'RELOC {} off={} t={} sec={}'.format(self.sym, self.offset, self.typ, self.section)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
30
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
31
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
32 class Section:
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
33 def __init__(self, name):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
34 self.name = name
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
35 self.address = 0
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
36 self.data = bytearray()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
37
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
38 def add_data(self, data):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
39 self.data += data
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
40
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
41 @property
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
42 def Size(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
43 return len(self.data)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
44
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
45 def __repr__(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
46 return 'SECTION {}'.format(self.name)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
47
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
48
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
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
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
52 def __init__(self):
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
53 self.symbols = {}
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
54 self.sections = {}
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
55 self.relocations = []
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
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
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
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
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
64 sym = Symbol(name, value, section)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
65 self.symbols[name] = sym
6f4753202b9a Added more recipes
Windel Bouwman
parents:
diff changeset
66 return sym
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
67
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
68 def add_relocation(self, sym_name, offset, typ, section):
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 336
diff changeset
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
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
71 reloc = Relocation(sym_name, offset, typ, section)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
72 self.relocations.append(reloc)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
73 return reloc
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
74
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
75 def get_section(self, name):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
76 if not name in self.sections:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
77 self.sections[name] = Section(name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
78 return self.sections[name]