blob: f2e4532611c1c24d3dfc1c84e525373758af14ce [file] [log] [blame]
Jim Grosbach2354f872011-06-22 20:14:52 +00001//===-- ARMMachObjectWriter.cpp - ARM Mach Object Writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruthed0881b2012-12-03 16:50:05 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Evan Chengad5f4852011-07-23 00:00:19 +000011#include "MCTargetDesc/ARMBaseInfo.h"
12#include "MCTargetDesc/ARMFixupKinds.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000013#include "llvm/ADT/Twine.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000014#include "llvm/MC/MCAsmLayout.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAssembler.h"
Jim Grosbach5e5eabb2012-01-26 23:20:15 +000016#include "llvm/MC/MCContext.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixup.h"
19#include "llvm/MC/MCFixupKindInfo.h"
Jim Grosbachae913222011-11-29 01:15:25 +000020#include "llvm/MC/MCMachOSymbolFlags.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCMachObjectWriter.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000022#include "llvm/MC/MCValue.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000023#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000024#include "llvm/Support/MachO.h"
Jim Grosbach2354f872011-06-22 20:14:52 +000025using namespace llvm;
26
27namespace {
28class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Jim Grosbach28fcafb2011-06-24 23:44:37 +000029 void RecordARMScatteredRelocation(MachObjectWriter *Writer,
30 const MCAssembler &Asm,
31 const MCAsmLayout &Layout,
32 const MCFragment *Fragment,
33 const MCFixup &Fixup,
34 MCValue Target,
35 unsigned Log2Size,
36 uint64_t &FixedValue);
Jim Grosbach997614f2012-03-20 17:25:45 +000037 void RecordARMScatteredHalfRelocation(MachObjectWriter *Writer,
38 const MCAssembler &Asm,
39 const MCAsmLayout &Layout,
40 const MCFragment *Fragment,
41 const MCFixup &Fixup, MCValue Target,
42 uint64_t &FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000043
Jim Grosbachdf8ed712012-09-25 18:07:17 +000044 bool requiresExternRelocation(MachObjectWriter *Writer,
45 const MCAssembler &Asm,
46 const MCFragment &Fragment,
47 unsigned RelocType, const MCSymbolData *SD,
48 uint64_t FixedValue);
49
Jim Grosbach2354f872011-06-22 20:14:52 +000050public:
51 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType,
52 uint32_t CPUSubtype)
53 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype,
54 /*UseAggressiveSymbolFolding=*/true) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000055
56 void RecordRelocation(MachObjectWriter *Writer,
57 const MCAssembler &Asm, const MCAsmLayout &Layout,
58 const MCFragment *Fragment, const MCFixup &Fixup,
Craig Topperca7e3e52014-03-10 03:19:03 +000059 MCValue Target, uint64_t &FixedValue) override;
Jim Grosbach2354f872011-06-22 20:14:52 +000060};
61}
62
Jim Grosbach28fcafb2011-06-24 23:44:37 +000063static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType,
64 unsigned &Log2Size) {
Charles Davis8bdfafd2013-09-01 04:28:48 +000065 RelocType = unsigned(MachO::ARM_RELOC_VANILLA);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000066 Log2Size = ~0U;
67
68 switch (Kind) {
69 default:
70 return false;
71
72 case FK_Data_1:
73 Log2Size = llvm::Log2_32(1);
74 return true;
75 case FK_Data_2:
76 Log2Size = llvm::Log2_32(2);
77 return true;
78 case FK_Data_4:
79 Log2Size = llvm::Log2_32(4);
80 return true;
81 case FK_Data_8:
82 Log2Size = llvm::Log2_32(8);
83 return true;
84
Kevin Enderby651898c2013-12-13 22:46:54 +000085 // These fixups are expected to always be resolvable at assembly time and
86 // have no relocations supported.
Jim Grosbach28fcafb2011-06-24 23:44:37 +000087 case ARM::fixup_arm_ldst_pcrel_12:
88 case ARM::fixup_arm_pcrel_10:
89 case ARM::fixup_arm_adr_pcrel_12:
Kevin Enderby651898c2013-12-13 22:46:54 +000090 return false;
91
92 // Handle 24-bit branch kinds.
Jim Grosbach28fcafb2011-06-24 23:44:37 +000093 case ARM::fixup_arm_condbranch:
94 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +000095 case ARM::fixup_arm_uncondbl:
96 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +000097 case ARM::fixup_arm_blx:
Charles Davis8bdfafd2013-09-01 04:28:48 +000098 RelocType = unsigned(MachO::ARM_RELOC_BR24);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000099 // Report as 'long', even though that is not quite accurate.
100 Log2Size = llvm::Log2_32(4);
101 return true;
102
103 // Handle Thumb branches.
104 case ARM::fixup_arm_thumb_br:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000105 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000106 Log2Size = llvm::Log2_32(2);
107 return true;
108
109 case ARM::fixup_t2_uncondbranch:
110 case ARM::fixup_arm_thumb_bl:
111 case ARM::fixup_arm_thumb_blx:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000112 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000113 Log2Size = llvm::Log2_32(4);
114 return true;
115
Jim Grosbach997614f2012-03-20 17:25:45 +0000116 // For movw/movt r_type relocations they always have a pair following them and
117 // the r_length bits are used differently. The encoding of the r_length is as
118 // follows:
119 // low bit of r_length:
120 // 0 - :lower16: for movw instructions
121 // 1 - :upper16: for movt instructions
122 // high bit of r_length:
123 // 0 - arm instructions
124 // 1 - thumb instructions
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000125 case ARM::fixup_arm_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000126 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000127 Log2Size = 1;
128 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000129 case ARM::fixup_t2_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000130 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000131 Log2Size = 3;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000132 return true;
133
134 case ARM::fixup_arm_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000135 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000136 Log2Size = 0;
137 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000138 case ARM::fixup_t2_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000139 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000140 Log2Size = 2;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000141 return true;
142 }
143}
144
145void ARMMachObjectWriter::
Jim Grosbach997614f2012-03-20 17:25:45 +0000146RecordARMScatteredHalfRelocation(MachObjectWriter *Writer,
147 const MCAssembler &Asm,
148 const MCAsmLayout &Layout,
149 const MCFragment *Fragment,
150 const MCFixup &Fixup,
151 MCValue Target,
152 uint64_t &FixedValue) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000153 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
154 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Charles Davis8bdfafd2013-09-01 04:28:48 +0000155 unsigned Type = MachO::ARM_RELOC_HALF;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000156
157 // See <reloc.h>.
158 const MCSymbol *A = &Target.getSymA()->getSymbol();
David Blaikie908f4d42014-04-24 16:59:40 +0000159 const MCSymbolData *A_SD = &Asm.getSymbolData(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000160
161 if (!A_SD->getFragment())
Jim Grosbach20275a82012-01-27 00:37:12 +0000162 Asm.getContext().FatalError(Fixup.getLoc(),
163 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000164 "' can not be undefined in a subtraction expression");
165
166 uint32_t Value = Writer->getSymbolAddress(A_SD, Layout);
167 uint32_t Value2 = 0;
168 uint64_t SecAddr =
169 Writer->getSectionAddress(A_SD->getFragment()->getParent());
170 FixedValue += SecAddr;
171
172 if (const MCSymbolRefExpr *B = Target.getSymB()) {
David Blaikie908f4d42014-04-24 16:59:40 +0000173 const MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000174
175 if (!B_SD->getFragment())
Jim Grosbach20275a82012-01-27 00:37:12 +0000176 Asm.getContext().FatalError(Fixup.getLoc(),
177 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000178 "' can not be undefined in a subtraction expression");
179
180 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000181 Type = MachO::ARM_RELOC_HALF_SECTDIFF;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000182 Value2 = Writer->getSymbolAddress(B_SD, Layout);
183 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
184 }
185
186 // Relocations are written out in reverse order, so the PAIR comes first.
187 // ARM_RELOC_HALF and ARM_RELOC_HALF_SECTDIFF abuse the r_length field:
188 //
189 // For these two r_type relocations they always have a pair following them and
190 // the r_length bits are used differently. The encoding of the r_length is as
191 // follows:
192 // low bit of r_length:
193 // 0 - :lower16: for movw instructions
194 // 1 - :upper16: for movt instructions
195 // high bit of r_length:
196 // 0 - arm instructions
197 // 1 - thumb instructions
198 // the other half of the relocated expression is in the following pair
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000199 // relocation entry in the low 16 bits of r_address field.
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000200 unsigned ThumbBit = 0;
201 unsigned MovtBit = 0;
202 switch ((unsigned)Fixup.getKind()) {
203 default: break;
204 case ARM::fixup_arm_movt_hi16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000205 MovtBit = 1;
Jim Grosbachae913222011-11-29 01:15:25 +0000206 // The thumb bit shouldn't be set in the 'other-half' bit of the
207 // relocation, but it will be set in FixedValue if the base symbol
208 // is a thumb function. Clear it out here.
209 if (A_SD->getFlags() & SF_ThumbFunc)
210 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000211 break;
212 case ARM::fixup_t2_movt_hi16:
Jim Grosbachae913222011-11-29 01:15:25 +0000213 if (A_SD->getFlags() & SF_ThumbFunc)
214 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000215 MovtBit = 1;
216 // Fallthrough
217 case ARM::fixup_t2_movw_lo16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000218 ThumbBit = 1;
219 break;
220 }
221
Charles Davis8bdfafd2013-09-01 04:28:48 +0000222 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000223 uint32_t OtherHalf = MovtBit
224 ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16);
225
Charles Davis8bdfafd2013-09-01 04:28:48 +0000226 MachO::any_relocation_info MRE;
227 MRE.r_word0 = ((OtherHalf << 0) |
228 (MachO::ARM_RELOC_PAIR << 24) |
229 (MovtBit << 28) |
230 (ThumbBit << 29) |
231 (IsPCRel << 30) |
232 MachO::R_SCATTERED);
233 MRE.r_word1 = Value2;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000234 Writer->addRelocation(Fragment->getParent(), MRE);
235 }
236
Charles Davis8bdfafd2013-09-01 04:28:48 +0000237 MachO::any_relocation_info MRE;
238 MRE.r_word0 = ((FixupOffset << 0) |
239 (Type << 24) |
240 (MovtBit << 28) |
241 (ThumbBit << 29) |
242 (IsPCRel << 30) |
243 MachO::R_SCATTERED);
244 MRE.r_word1 = Value;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000245 Writer->addRelocation(Fragment->getParent(), MRE);
246}
247
248void ARMMachObjectWriter::RecordARMScatteredRelocation(MachObjectWriter *Writer,
249 const MCAssembler &Asm,
250 const MCAsmLayout &Layout,
251 const MCFragment *Fragment,
252 const MCFixup &Fixup,
253 MCValue Target,
254 unsigned Log2Size,
255 uint64_t &FixedValue) {
256 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
257 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Charles Davis8bdfafd2013-09-01 04:28:48 +0000258 unsigned Type = MachO::ARM_RELOC_VANILLA;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000259
260 // See <reloc.h>.
261 const MCSymbol *A = &Target.getSymA()->getSymbol();
David Blaikie908f4d42014-04-24 16:59:40 +0000262 const MCSymbolData *A_SD = &Asm.getSymbolData(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000263
264 if (!A_SD->getFragment())
Jim Grosbach20275a82012-01-27 00:37:12 +0000265 Asm.getContext().FatalError(Fixup.getLoc(),
266 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000267 "' can not be undefined in a subtraction expression");
268
269 uint32_t Value = Writer->getSymbolAddress(A_SD, Layout);
270 uint64_t SecAddr = Writer->getSectionAddress(A_SD->getFragment()->getParent());
271 FixedValue += SecAddr;
272 uint32_t Value2 = 0;
273
274 if (const MCSymbolRefExpr *B = Target.getSymB()) {
David Blaikie908f4d42014-04-24 16:59:40 +0000275 const MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000276
277 if (!B_SD->getFragment())
Jim Grosbach20275a82012-01-27 00:37:12 +0000278 Asm.getContext().FatalError(Fixup.getLoc(),
279 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000280 "' can not be undefined in a subtraction expression");
281
282 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000283 Type = MachO::ARM_RELOC_SECTDIFF;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000284 Value2 = Writer->getSymbolAddress(B_SD, Layout);
285 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
286 }
287
288 // Relocations are written out in reverse order, so the PAIR comes first.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000289 if (Type == MachO::ARM_RELOC_SECTDIFF ||
290 Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) {
291 MachO::any_relocation_info MRE;
292 MRE.r_word0 = ((0 << 0) |
293 (MachO::ARM_RELOC_PAIR << 24) |
294 (Log2Size << 28) |
295 (IsPCRel << 30) |
296 MachO::R_SCATTERED);
297 MRE.r_word1 = Value2;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000298 Writer->addRelocation(Fragment->getParent(), MRE);
299 }
300
Charles Davis8bdfafd2013-09-01 04:28:48 +0000301 MachO::any_relocation_info MRE;
302 MRE.r_word0 = ((FixupOffset << 0) |
303 (Type << 24) |
304 (Log2Size << 28) |
305 (IsPCRel << 30) |
306 MachO::R_SCATTERED);
307 MRE.r_word1 = Value;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000308 Writer->addRelocation(Fragment->getParent(), MRE);
309}
310
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000311bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer,
312 const MCAssembler &Asm,
313 const MCFragment &Fragment,
314 unsigned RelocType,
315 const MCSymbolData *SD,
316 uint64_t FixedValue) {
317 // Most cases can be identified purely from the symbol.
318 if (Writer->doesSymbolRequireExternRelocation(SD))
319 return true;
320 int64_t Value = (int64_t)FixedValue; // The displacement is signed.
321 int64_t Range;
322 switch (RelocType) {
323 default:
324 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000325 case MachO::ARM_RELOC_BR24:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000326 // PC pre-adjustment of 8 for these instructions.
327 Value -= 8;
328 // ARM BL/BLX has a 25-bit offset.
329 Range = 0x1ffffff;
330 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000331 case MachO::ARM_THUMB_RELOC_BR22:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000332 // PC pre-adjustment of 4 for these instructions.
333 Value -= 4;
334 // Thumb BL/BLX has a 24-bit offset.
335 Range = 0xffffff;
336 }
337 // BL/BLX also use external relocations when an internal relocation
338 // would result in the target being out of range. This gives the linker
339 // enough information to generate a branch island.
340 const MCSectionData &SymSD = Asm.getSectionData(
341 SD->getSymbol().getSection());
342 Value += Writer->getSectionAddress(&SymSD);
343 Value -= Writer->getSectionAddress(Fragment.getParent());
344 // If the resultant value would be out of range for an internal relocation,
345 // use an external instead.
346 if (Value > Range || Value < -(Range + 1))
347 return true;
348 return false;
349}
350
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000351void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer,
352 const MCAssembler &Asm,
353 const MCAsmLayout &Layout,
354 const MCFragment *Fragment,
355 const MCFixup &Fixup,
356 MCValue Target,
357 uint64_t &FixedValue) {
358 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
359 unsigned Log2Size;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000360 unsigned RelocType = MachO::ARM_RELOC_VANILLA;
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000361 if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size))
362 // If we failed to get fixup kind info, it's because there's no legal
363 // relocation type for the fixup kind. This happens when it's a fixup that's
364 // expected to always be resolvable at assembly time and not have any
365 // relocations needed.
366 Asm.getContext().FatalError(Fixup.getLoc(),
367 "unsupported relocation on symbol");
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000368
369 // If this is a difference or a defined symbol plus an offset, then we need a
370 // scattered relocation entry. Differences always require scattered
371 // relocations.
372 if (Target.getSymB()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000373 if (RelocType == MachO::ARM_RELOC_HALF)
Jim Grosbach997614f2012-03-20 17:25:45 +0000374 return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment,
375 Fixup, Target, FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000376 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
377 Target, Log2Size, FixedValue);
378 }
379
380 // Get the symbol data, if any.
David Blaikie908f4d42014-04-24 16:59:40 +0000381 const MCSymbolData *SD = 0;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000382 if (Target.getSymA())
383 SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
384
385 // FIXME: For other platforms, we need to use scattered relocations for
386 // internal relocations with offsets. If this is an internal relocation with
387 // an offset, it also needs a scattered relocation entry.
388 //
389 // Is this right for ARM?
390 uint32_t Offset = Target.getConstant();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000391 if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA)
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000392 Offset += 1 << Log2Size;
393 if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD))
394 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
395 Target, Log2Size, FixedValue);
396
397 // See <reloc.h>.
398 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
399 unsigned Index = 0;
400 unsigned IsExtern = 0;
401 unsigned Type = 0;
402
403 if (Target.isAbsolute()) { // constant
404 // FIXME!
405 report_fatal_error("FIXME: relocations to absolute targets "
406 "not yet implemented");
407 } else {
408 // Resolve constant variables.
409 if (SD->getSymbol().isVariable()) {
410 int64_t Res;
411 if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute(
412 Res, Layout, Writer->getSectionAddressMap())) {
413 FixedValue = Res;
414 return;
415 }
416 }
417
418 // Check whether we need an external or internal relocation.
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000419 if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, SD,
420 FixedValue)) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000421 IsExtern = 1;
422 Index = SD->getIndex();
423
424 // For external relocations, make sure to offset the fixup value to
425 // compensate for the addend of the symbol address, if it was
426 // undefined. This occurs with weak definitions, for example.
427 if (!SD->Symbol->isUndefined())
428 FixedValue -= Layout.getSymbolOffset(SD);
429 } else {
430 // The index is the section ordinal (1-based).
431 const MCSectionData &SymSD = Asm.getSectionData(
432 SD->getSymbol().getSection());
433 Index = SymSD.getOrdinal() + 1;
434 FixedValue += Writer->getSectionAddress(&SymSD);
435 }
436 if (IsPCRel)
437 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
438
439 // The type is determined by the fixup kind.
440 Type = RelocType;
441 }
442
443 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000444 MachO::any_relocation_info MRE;
445 MRE.r_word0 = FixupOffset;
446 MRE.r_word1 = ((Index << 0) |
447 (IsPCRel << 24) |
448 (Log2Size << 25) |
449 (IsExtern << 27) |
450 (Type << 28));
Jim Grosbach997614f2012-03-20 17:25:45 +0000451
452 // Even when it's not a scattered relocation, movw/movt always uses
453 // a PAIR relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000454 if (Type == MachO::ARM_RELOC_HALF) {
Kevin Enderby5c490f12012-07-30 18:46:15 +0000455 // The other-half value only gets populated for the movt and movw
456 // relocation entries.
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000457 uint32_t Value = 0;
Jim Grosbach997614f2012-03-20 17:25:45 +0000458 switch ((unsigned)Fixup.getKind()) {
459 default: break;
Kevin Enderby5c490f12012-07-30 18:46:15 +0000460 case ARM::fixup_arm_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000461 case ARM::fixup_t2_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000462 Value = (FixedValue >> 16) & 0xffff;
463 break;
Jim Grosbach997614f2012-03-20 17:25:45 +0000464 case ARM::fixup_arm_movt_hi16:
Jim Grosbach997614f2012-03-20 17:25:45 +0000465 case ARM::fixup_t2_movt_hi16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000466 Value = FixedValue & 0xffff;
Jim Grosbach997614f2012-03-20 17:25:45 +0000467 break;
468 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000469 MachO::any_relocation_info MREPair;
470 MREPair.r_word0 = Value;
471 MREPair.r_word1 = ((0xffffff << 0) |
472 (Log2Size << 25) |
473 (MachO::ARM_RELOC_PAIR << 28));
Jim Grosbach997614f2012-03-20 17:25:45 +0000474
475 Writer->addRelocation(Fragment->getParent(), MREPair);
476 }
477
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000478 Writer->addRelocation(Fragment->getParent(), MRE);
479}
480
Jim Grosbach2354f872011-06-22 20:14:52 +0000481MCObjectWriter *llvm::createARMMachObjectWriter(raw_ostream &OS,
482 bool Is64Bit,
483 uint32_t CPUType,
484 uint32_t CPUSubtype) {
485 return createMachObjectWriter(new ARMMachObjectWriter(Is64Bit,
486 CPUType,
487 CPUSubtype),
488 OS, /*IsLittleEndian=*/true);
489}