1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | #include "MCTargetDesc/ARMMCTargetDesc.h" |
11 | #include "MCTargetDesc/ARMBaseInfo.h" |
12 | #include "MCTargetDesc/ARMFixupKinds.h" |
13 | #include "llvm/ADT/Twine.h" |
14 | #include "llvm/MC/MCAsmLayout.h" |
15 | #include "llvm/MC/MCAssembler.h" |
16 | #include "llvm/MC/MCContext.h" |
17 | #include "llvm/MC/MCExpr.h" |
18 | #include "llvm/MC/MCFixup.h" |
19 | #include "llvm/MC/MCFixupKindInfo.h" |
20 | #include "llvm/MC/MCMachOSymbolFlags.h" |
21 | #include "llvm/MC/MCMachObjectWriter.h" |
22 | #include "llvm/MC/MCSection.h" |
23 | #include "llvm/MC/MCValue.h" |
24 | #include "llvm/Support/ErrorHandling.h" |
25 | #include "llvm/Support/MachO.h" |
26 | using namespace llvm; |
27 | |
28 | namespace { |
29 | class ARMMachObjectWriter : public MCMachObjectTargetWriter { |
30 | void RecordARMScatteredRelocation(MachObjectWriter *Writer, |
31 | const MCAssembler &Asm, |
32 | const MCAsmLayout &Layout, |
33 | const MCFragment *Fragment, |
34 | const MCFixup &Fixup, |
35 | MCValue Target, |
36 | unsigned Type, |
37 | unsigned Log2Size, |
38 | uint64_t &FixedValue); |
39 | void RecordARMScatteredHalfRelocation(MachObjectWriter *Writer, |
40 | const MCAssembler &Asm, |
41 | const MCAsmLayout &Layout, |
42 | const MCFragment *Fragment, |
43 | const MCFixup &Fixup, MCValue Target, |
44 | uint64_t &FixedValue); |
45 | |
46 | bool requiresExternRelocation(MachObjectWriter *Writer, |
47 | const MCAssembler &Asm, |
48 | const MCFragment &Fragment, unsigned RelocType, |
49 | const MCSymbol &S, uint64_t FixedValue); |
50 | |
51 | public: |
52 | ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType, |
53 | uint32_t CPUSubtype) |
54 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype, |
55 | true) {} |
56 | |
57 | void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, |
58 | const MCAsmLayout &Layout, const MCFragment *Fragment, |
59 | const MCFixup &Fixup, MCValue Target, |
60 | uint64_t &FixedValue) override; |
61 | }; |
62 | } |
63 | |
64 | static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType, |
65 | unsigned &Log2Size) { |
66 | RelocType = unsigned(MachO::ARM_RELOC_VANILLA); |
67 | Log2Size = ~0U; |
68 | |
69 | switch (Kind) { |
70 | default: |
71 | return false; |
72 | |
73 | case FK_Data_1: |
74 | Log2Size = llvm::Log2_32(1); |
75 | return true; |
76 | case FK_Data_2: |
77 | Log2Size = llvm::Log2_32(2); |
78 | return true; |
79 | case FK_Data_4: |
80 | Log2Size = llvm::Log2_32(4); |
81 | return true; |
82 | case FK_Data_8: |
83 | Log2Size = llvm::Log2_32(8); |
84 | return true; |
85 | |
86 | |
87 | |
88 | case ARM::fixup_arm_ldst_pcrel_12: |
89 | case ARM::fixup_arm_pcrel_10: |
90 | case ARM::fixup_arm_adr_pcrel_12: |
91 | case ARM::fixup_arm_thumb_br: |
92 | return false; |
93 | |
94 | |
95 | case ARM::fixup_arm_condbranch: |
96 | case ARM::fixup_arm_uncondbranch: |
97 | case ARM::fixup_arm_uncondbl: |
98 | case ARM::fixup_arm_condbl: |
99 | case ARM::fixup_arm_blx: |
100 | RelocType = unsigned(MachO::ARM_RELOC_BR24); |
101 | |
102 | Log2Size = llvm::Log2_32(4); |
103 | return true; |
104 | |
105 | case ARM::fixup_t2_uncondbranch: |
106 | case ARM::fixup_arm_thumb_bl: |
107 | case ARM::fixup_arm_thumb_blx: |
108 | RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22); |
109 | Log2Size = llvm::Log2_32(4); |
110 | return true; |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | case ARM::fixup_arm_movt_hi16: |
122 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
123 | Log2Size = 1; |
124 | return true; |
125 | case ARM::fixup_t2_movt_hi16: |
126 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
127 | Log2Size = 3; |
128 | return true; |
129 | |
130 | case ARM::fixup_arm_movw_lo16: |
131 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
132 | Log2Size = 0; |
133 | return true; |
134 | case ARM::fixup_t2_movw_lo16: |
135 | RelocType = unsigned(MachO::ARM_RELOC_HALF); |
136 | Log2Size = 2; |
137 | return true; |
138 | } |
139 | } |
140 | |
141 | void ARMMachObjectWriter:: |
142 | RecordARMScatteredHalfRelocation(MachObjectWriter *Writer, |
143 | const MCAssembler &Asm, |
144 | const MCAsmLayout &Layout, |
145 | const MCFragment *Fragment, |
146 | const MCFixup &Fixup, |
147 | MCValue Target, |
148 | uint64_t &FixedValue) { |
149 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
150 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
151 | unsigned Type = MachO::ARM_RELOC_HALF; |
152 | |
153 | |
154 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
155 | |
156 | if (!A->getFragment()) |
157 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
158 | "symbol '" + A->getName() + |
159 | "' can not be undefined in a subtraction expression"); |
160 | |
161 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
162 | uint32_t Value2 = 0; |
163 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
164 | FixedValue += SecAddr; |
165 | |
166 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
167 | const MCSymbol *SB = &B->getSymbol(); |
168 | |
169 | if (!SB->getFragment()) |
170 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
171 | "symbol '" + B->getSymbol().getName() + |
172 | "' can not be undefined in a subtraction expression"); |
173 | |
174 | |
175 | Type = MachO::ARM_RELOC_HALF_SECTDIFF; |
176 | Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout); |
177 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
178 | } |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | unsigned ThumbBit = 0; |
195 | unsigned MovtBit = 0; |
196 | switch ((unsigned)Fixup.getKind()) { |
197 | default: break; |
198 | case ARM::fixup_arm_movt_hi16: |
199 | MovtBit = 1; |
200 | |
201 | |
202 | |
203 | if (Asm.isThumbFunc(A)) |
204 | FixedValue &= 0xfffffffe; |
205 | break; |
206 | case ARM::fixup_t2_movt_hi16: |
207 | if (Asm.isThumbFunc(A)) |
208 | FixedValue &= 0xfffffffe; |
209 | MovtBit = 1; |
210 | |
211 | case ARM::fixup_t2_movw_lo16: |
212 | ThumbBit = 1; |
213 | break; |
214 | } |
215 | |
216 | if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
217 | uint32_t OtherHalf = MovtBit |
218 | ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16); |
219 | |
220 | MachO::any_relocation_info MRE; |
221 | MRE.r_word0 = ((OtherHalf << 0) | |
222 | (MachO::ARM_RELOC_PAIR << 24) | |
223 | (MovtBit << 28) | |
224 | (ThumbBit << 29) | |
225 | (IsPCRel << 30) | |
226 | MachO::R_SCATTERED); |
227 | MRE.r_word1 = Value2; |
228 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
229 | } |
230 | |
231 | MachO::any_relocation_info MRE; |
232 | MRE.r_word0 = ((FixupOffset << 0) | |
233 | (Type << 24) | |
234 | (MovtBit << 28) | |
235 | (ThumbBit << 29) | |
236 | (IsPCRel << 30) | |
237 | MachO::R_SCATTERED); |
238 | MRE.r_word1 = Value; |
239 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
240 | } |
241 | |
242 | void ARMMachObjectWriter::RecordARMScatteredRelocation(MachObjectWriter *Writer, |
243 | const MCAssembler &Asm, |
244 | const MCAsmLayout &Layout, |
245 | const MCFragment *Fragment, |
246 | const MCFixup &Fixup, |
247 | MCValue Target, |
248 | unsigned Type, |
249 | unsigned Log2Size, |
250 | uint64_t &FixedValue) { |
251 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
252 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
253 | |
254 | |
255 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
256 | |
257 | if (!A->getFragment()) |
258 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
259 | "symbol '" + A->getName() + |
260 | "' can not be undefined in a subtraction expression"); |
261 | |
262 | uint32_t Value = Writer->getSymbolAddress(*A, Layout); |
263 | uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent()); |
264 | FixedValue += SecAddr; |
265 | uint32_t Value2 = 0; |
266 | |
267 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
268 | assert(Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols")((Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols" ) ? static_cast<void> (0) : __assert_fail ("Type == MachO::ARM_RELOC_VANILLA && \"invalid reloc for 2 symbols\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.7~svn239079/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp" , 268, __PRETTY_FUNCTION__)); |
269 | const MCSymbol *SB = &B->getSymbol(); |
270 | |
271 | if (!SB->getFragment()) |
272 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
273 | "symbol '" + B->getSymbol().getName() + |
274 | "' can not be undefined in a subtraction expression"); |
275 | |
276 | |
277 | Type = MachO::ARM_RELOC_SECTDIFF; |
278 | Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout); |
279 | FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent()); |
280 | } |
281 | |
282 | |
283 | if (Type == MachO::ARM_RELOC_SECTDIFF || |
284 | Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) { |
285 | MachO::any_relocation_info MRE; |
286 | MRE.r_word0 = ((0 << 0) | |
287 | (MachO::ARM_RELOC_PAIR << 24) | |
288 | (Log2Size << 28) | |
289 | (IsPCRel << 30) | |
290 | MachO::R_SCATTERED); |
291 | MRE.r_word1 = Value2; |
292 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
293 | } |
294 | |
295 | MachO::any_relocation_info MRE; |
296 | MRE.r_word0 = ((FixupOffset << 0) | |
297 | (Type << 24) | |
298 | (Log2Size << 28) | |
299 | (IsPCRel << 30) | |
300 | MachO::R_SCATTERED); |
301 | MRE.r_word1 = Value; |
302 | Writer->addRelocation(nullptr, Fragment->getParent(), MRE); |
303 | } |
304 | |
305 | bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer, |
306 | const MCAssembler &Asm, |
307 | const MCFragment &Fragment, |
308 | unsigned RelocType, |
309 | const MCSymbol &S, |
310 | uint64_t FixedValue) { |
311 | |
312 | if (Writer->doesSymbolRequireExternRelocation(S)) |
313 | return true; |
314 | int64_t Value = (int64_t)FixedValue; |
315 | int64_t Range; |
316 | switch (RelocType) { |
317 | default: |
318 | return false; |
319 | case MachO::ARM_RELOC_BR24: |
320 | |
321 | Value -= 8; |
322 | |
323 | Range = 0x1ffffff; |
324 | break; |
325 | case MachO::ARM_THUMB_RELOC_BR22: |
326 | |
327 | Value -= 4; |
328 | |
329 | Range = 0xffffff; |
330 | } |
331 | |
332 | |
333 | |
334 | Value += Writer->getSectionAddress(&S.getSection()); |
335 | Value -= Writer->getSectionAddress(Fragment.getParent()); |
336 | |
337 | |
338 | if (Value > Range || Value < -(Range + 1)) |
339 | return true; |
340 | return false; |
341 | } |
342 | |
343 | void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer, |
344 | MCAssembler &Asm, |
345 | const MCAsmLayout &Layout, |
346 | const MCFragment *Fragment, |
347 | const MCFixup &Fixup, MCValue Target, |
348 | uint64_t &FixedValue) { |
349 | unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind()); |
350 | unsigned Log2Size; |
351 | unsigned RelocType = MachO::ARM_RELOC_VANILLA; |
352 | if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size)) |
| |
353 | |
354 | |
355 | |
356 | |
357 | Asm.getContext().reportFatalError(Fixup.getLoc(), |
358 | "unsupported relocation on symbol"); |
359 | |
360 | |
361 | |
362 | |
363 | if (Target.getSymB()) { |
| |
364 | if (RelocType == MachO::ARM_RELOC_HALF) |
365 | return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment, |
366 | Fixup, Target, FixedValue); |
367 | return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, |
368 | Target, RelocType, Log2Size, |
369 | FixedValue); |
370 | } |
371 | |
372 | |
373 | const MCSymbol *A = nullptr; |
| 3 | | 'A' initialized to a null pointer value | |
|
374 | if (Target.getSymA()) |
| |
375 | A = &Target.getSymA()->getSymbol(); |
376 | |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | uint32_t Offset = Target.getConstant(); |
383 | if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA) |
384 | Offset += 1 << Log2Size; |
385 | if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A)) |
386 | return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, |
387 | Target, RelocType, Log2Size, |
388 | FixedValue); |
389 | |
390 | |
391 | uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset(); |
392 | unsigned Index = 0; |
393 | unsigned Type = 0; |
394 | const MCSymbol *RelSymbol = nullptr; |
395 | |
396 | if (Target.isAbsolute()) { |
| |
397 | |
398 | report_fatal_error("FIXME: relocations to absolute targets " |
399 | "not yet implemented"); |
400 | } else { |
401 | |
402 | if (A->isVariable()) { |
| 6 | | Called C++ object pointer is null |
|
403 | int64_t Res; |
404 | if (A->getVariableValue()->evaluateAsAbsolute( |
405 | Res, Layout, Writer->getSectionAddressMap())) { |
406 | FixedValue = Res; |
407 | return; |
408 | } |
409 | } |
410 | |
411 | |
412 | if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A, |
413 | FixedValue)) { |
414 | RelSymbol = A; |
415 | |
416 | |
417 | |
418 | |
419 | if (!A->isUndefined()) |
420 | FixedValue -= Layout.getSymbolOffset(*A); |
421 | } else { |
422 | |
423 | const MCSection &Sec = A->getSection(); |
424 | Index = Sec.getOrdinal() + 1; |
425 | FixedValue += Writer->getSectionAddress(&Sec); |
426 | } |
427 | if (IsPCRel) |
428 | FixedValue -= Writer->getSectionAddress(Fragment->getParent()); |
429 | |
430 | |
431 | Type = RelocType; |
432 | } |
433 | |
434 | |
435 | MachO::any_relocation_info MRE; |
436 | MRE.r_word0 = FixupOffset; |
437 | MRE.r_word1 = |
438 | (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28); |
439 | |
440 | |
441 | |
442 | if (Type == MachO::ARM_RELOC_HALF) { |
443 | |
444 | |
445 | uint32_t Value = 0; |
446 | switch ((unsigned)Fixup.getKind()) { |
447 | default: break; |
448 | case ARM::fixup_arm_movw_lo16: |
449 | case ARM::fixup_t2_movw_lo16: |
450 | Value = (FixedValue >> 16) & 0xffff; |
451 | break; |
452 | case ARM::fixup_arm_movt_hi16: |
453 | case ARM::fixup_t2_movt_hi16: |
454 | Value = FixedValue & 0xffff; |
455 | break; |
456 | } |
457 | MachO::any_relocation_info MREPair; |
458 | MREPair.r_word0 = Value; |
459 | MREPair.r_word1 = ((0xffffff << 0) | |
460 | (Log2Size << 25) | |
461 | (MachO::ARM_RELOC_PAIR << 28)); |
462 | |
463 | Writer->addRelocation(nullptr, Fragment->getParent(), MREPair); |
464 | } |
465 | |
466 | Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE); |
467 | } |
468 | |
469 | MCObjectWriter *llvm::createARMMachObjectWriter(raw_pwrite_stream &OS, |
470 | bool Is64Bit, uint32_t CPUType, |
471 | uint32_t CPUSubtype) { |
472 | return createMachObjectWriter(new ARMMachObjectWriter(Is64Bit, |
473 | CPUType, |
474 | CPUSubtype), |
475 | OS, true); |
476 | } |