Mercurial > lcfOS
annotate python/ppci/linker.py @ 377:9667d78ba79e
Switched to xml for project description
author | Windel Bouwman |
---|---|
date | Fri, 11 Apr 2014 15:47:50 +0200 |
parents | 98ff43cfdd36 |
children | 6df89163e114 |
rev | line source |
---|---|
348 | 1 import logging |
335 | 2 import struct |
334 | 3 from .objectfile import ObjectFile |
335 | 4 from . import CompilerError |
365 | 5 from .bitfun import encode_imm32 |
335 | 6 |
7 def align(x, m): | |
8 while ((x % m) != 0): | |
9 x = x + 1 | |
10 return x | |
11 | |
12 def wrap_negative(x, bits): | |
13 b = struct.unpack('<I', struct.pack('<i', x))[0] | |
14 mask = (1 << bits) - 1 | |
15 return b & mask | |
16 | |
17 reloc_map = {} | |
18 | |
19 def reloc(t): | |
20 def f(c): | |
21 reloc_map[t] = c | |
22 return f | |
23 | |
24 | |
25 @reloc('lit_add_8') | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
26 def apply_lit8(reloc, sym, section, reloc_value): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
27 assert sym.value % 4 == 0 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
28 offset = (sym.value - (align(reloc_value + 2, 4))) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
29 assert offset in range(0, 1024, 4), str(offset)+str( self.dst.sections) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
30 rel8 = offset >> 2 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
31 section.data[reloc.offset] = rel8 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
32 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
33 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
34 @reloc('wrap_new11') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
35 def apply_wrap_new11(reloc, sym, section, reloc_value): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
36 offset = sym.value - (align(reloc_value, 2) + 4) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
37 assert offset in range(-2048, 2046, 2) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
38 imm11 = wrap_negative(offset >> 1, 11) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
39 section.data[reloc.offset] = (imm11 & 0xff) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
40 section.data[reloc.offset + 1] |= (imm11 >> 8) & 0x7 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
41 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
42 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
43 @reloc('rel8') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
44 def apply_rel8(reloc, sym, section, reloc_value): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
45 assert sym.value % 2 == 0 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
46 offset = sym.value - (align(reloc_value, 2) + 4) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
47 assert offset in range(-256, 254, 2), str(offset) + str(reloc) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
48 imm8 = wrap_negative(offset >> 1, 8) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
49 section.data[reloc.offset] = imm8 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
50 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
51 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
52 @reloc('bl_imm11_imm10') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
53 def apply_bl_imm11(reloc, sym, section, reloc_value): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
54 assert sym.value % 2 == 0 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
55 offset = sym.value - (align(reloc_value, 2) + 4) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
56 assert offset in range(-16777216, 16777214, 2), str(offset) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
57 imm32 = wrap_negative(offset >> 1, 32) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
58 imm11 = imm32 & 0x7FF |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
59 imm10 = (imm32 >> 11) & 0x3FF |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
60 s = (imm32 >> 24) & 0x1 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
61 section.data[reloc.offset + 2] = imm11 & 0xFF |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
62 section.data[reloc.offset + 3] |= (imm11 >> 8) & 0x7 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
63 section.data[reloc.offset] = imm10 & 0xff |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
64 section.data[reloc.offset + 1] |= ((imm10 >> 8) & 0x3) | (s << 2) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
65 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
66 @reloc('b_imm11_imm6') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
67 def apply_b_imm11_imm6(reloc, sym, section, reloc_value): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
68 assert sym.value % 2 == 0 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
69 offset = sym.value - (align(reloc_value, 2) + 4) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
70 assert offset in range(-1048576, 1048574, 2), str(offset) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
71 imm32 = wrap_negative(offset >> 1, 32) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
72 imm11 = imm32 & 0x7FF |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
73 imm6 = (imm32 >> 11) & 0x3F |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
74 s = (imm32 >> 24) & 0x1 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
75 section.data[reloc.offset + 2] = imm11 & 0xFF |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
76 section.data[reloc.offset + 3] |= (imm11 >> 8) & 0x7 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
77 section.data[reloc.offset] |= imm6 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
78 section.data[reloc.offset + 1] |= (s << 2) |
335 | 79 |
345 | 80 # ARM reloc!! |
81 # TODO: move to target classes??? | |
82 @reloc('b_imm24') | |
83 def apply_b_imm24(reloc, sym, section, reloc_value): | |
84 assert sym.value % 4 == 0 | |
85 assert reloc_value % 4 == 0 | |
86 offset = (sym.value - (reloc_value + 8)) | |
87 rel24 = wrap_negative(offset >> 2, 24) | |
88 section.data[reloc.offset+2] = (rel24 >> 16) & 0xFF | |
89 section.data[reloc.offset+1] = (rel24 >> 8) & 0xFF | |
90 section.data[reloc.offset+0] = rel24 & 0xFF | |
91 | |
334 | 92 |
350 | 93 @reloc('ldr_imm12') |
94 def apply_ldr_imm12(reloc, sym, section, reloc_value): | |
95 assert sym.value % 4 == 0 | |
96 assert reloc_value % 4 == 0 | |
97 offset = (sym.value - (reloc_value + 8)) | |
98 U = 1 | |
99 if offset < 0: | |
100 offset = -offset | |
101 U = 0 | |
102 assert offset < 4096 | |
365 | 103 section.data[reloc.offset+2] |= (U << 7) |
350 | 104 section.data[reloc.offset+1] |= (offset >> 8) & 0xF |
105 section.data[reloc.offset+0] = offset & 0xFF | |
106 | |
354 | 107 @reloc('adr_imm12') |
108 def apply_adr_imm12(reloc, sym, section, reloc_value): | |
109 assert sym.value % 4 == 0 | |
110 assert reloc_value % 4 == 0 | |
111 offset = (sym.value - (reloc_value + 8)) | |
112 U = 2 | |
113 if offset < 0: | |
114 offset = -offset | |
115 U = 1 | |
116 assert offset < 4096 | |
365 | 117 offset = encode_imm32(offset) |
364 | 118 section.data[reloc.offset+2] |= (U << 6) |
354 | 119 section.data[reloc.offset+1] |= (offset >> 8) & 0xF |
120 section.data[reloc.offset+0] = offset & 0xFF | |
121 | |
364 | 122 @reloc('absaddr32') |
123 def apply_absaddr32(reloc, sym, section, reloc_value): | |
124 assert sym.value % 4 == 0 | |
125 assert reloc_value % 4 == 0 | |
126 offset = sym.value | |
127 section.data[reloc.offset+3] = (offset >> 24) & 0xFF | |
128 section.data[reloc.offset+2] = (offset >> 16) & 0xFF | |
129 section.data[reloc.offset+1] = (offset >> 8) & 0xFF | |
130 section.data[reloc.offset+0] = offset & 0xFF | |
131 | |
354 | 132 |
377 | 133 class Layout: |
134 pass | |
135 | |
334 | 136 class Linker: |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
137 """ Merges the sections of several object files and |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
138 performs relocation """ |
348 | 139 def __init__(self): |
140 self.logger = logging.getLogger('Linker') | |
141 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
142 def link(self, objs, layout={}): |
377 | 143 assert type(objs) is list |
335 | 144 # Create new object file to store output: |
334 | 145 self.dst = ObjectFile() |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
146 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
147 # Create sections with address: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
148 for section_name, address in layout.items(): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
149 self.dst.get_section(section_name).address = address |
335 | 150 |
334 | 151 # First copy all sections into output sections: |
152 for iobj in objs: | |
335 | 153 offsets = {} |
154 # Merge sections: | |
155 for in_s in iobj.sections.values(): | |
156 out_s = self.dst.get_section(in_s.name) | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
157 # TODO: align section in other way: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
158 while out_s.Size % 4 != 0: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
159 out_s.add_data(bytes([0])) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
160 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
161 # Add new section: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
162 offsets[in_s.name] = out_s.Size |
335 | 163 out_s.add_data(in_s.data) |
348 | 164 self.logger.debug('{} {}({})'.format(offsets[in_s.name], iobj, in_s.name)) |
335 | 165 |
166 # Merge symbols: | |
167 for sym in iobj.symbols.values(): | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
168 out_s = self.dst.get_section(sym.section) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
169 value = offsets[sym.section] + out_s.address + sym.value |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
170 self.dst.add_symbol(sym.name, value, sym.section) |
363 | 171 self.logger.debug('{} at 0x{:08X} in section {}'.format(sym.name, value, sym.section)) |
335 | 172 |
173 # Merge relocations: | |
174 for reloc in iobj.relocations: | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
175 offset = offsets[reloc.section] + reloc.offset |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
176 self.dst.add_relocation(reloc.sym, offset, reloc.typ, reloc.section) |
335 | 177 |
178 # Perform relocations: | |
179 for reloc in self.dst.relocations: | |
180 # Lookup symbol: | |
181 if reloc.sym not in self.dst.symbols: | |
182 raise CompilerError('Undefined reference "{}"'.format(reloc.sym)) | |
183 sym = self.dst.symbols[reloc.sym] | |
184 # patch up: | |
185 section = self.dst.get_section(reloc.section) | |
186 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
187 # Determine location in memory of reloc patchup position: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
188 reloc_value = section.address + reloc.offset |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
189 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
190 if reloc.typ in reloc_map: |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
191 f = reloc_map[reloc.typ] |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
335
diff
changeset
|
192 f(reloc, sym, section, reloc_value) |
335 | 193 else: |
194 raise NotImplementedError('Unknown relocation type {}'.format(reloc.typ)) | |
195 | |
334 | 196 return self.dst |