blob: 5d9b32779813e87e18419d9ae39529c8ce6b4962 [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"
Rafael Espindola6e6820a2015-05-25 14:12:48 +000022#include "llvm/MC/MCSection.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000023#include "llvm/MC/MCValue.h"
Jim Grosbach28fcafb2011-06-24 23:44:37 +000024#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000025#include "llvm/Support/MachO.h"
Jim Grosbach2354f872011-06-22 20:14:52 +000026using namespace llvm;
27
28namespace {
29class ARMMachObjectWriter : public MCMachObjectTargetWriter {
Jim Grosbach28fcafb2011-06-24 23:44:37 +000030 void RecordARMScatteredRelocation(MachObjectWriter *Writer,
31 const MCAssembler &Asm,
32 const MCAsmLayout &Layout,
33 const MCFragment *Fragment,
34 const MCFixup &Fixup,
35 MCValue Target,
Tim Northover1bc367a2014-07-04 10:58:05 +000036 unsigned Type,
Jim Grosbach28fcafb2011-06-24 23:44:37 +000037 unsigned Log2Size,
38 uint64_t &FixedValue);
Jim Grosbach997614f2012-03-20 17:25:45 +000039 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);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000045
Jim Grosbachdf8ed712012-09-25 18:07:17 +000046 bool requiresExternRelocation(MachObjectWriter *Writer,
47 const MCAssembler &Asm,
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +000048 const MCFragment &Fragment, unsigned RelocType,
49 const MCSymbol &S, uint64_t FixedValue);
Jim Grosbachdf8ed712012-09-25 18:07:17 +000050
Jim Grosbach2354f872011-06-22 20:14:52 +000051public:
Jim Grosbach7c76b4c2015-06-04 20:27:42 +000052 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
53 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000054
Rafael Espindola26585542015-01-19 21:11:14 +000055 void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
56 const MCAsmLayout &Layout, const MCFragment *Fragment,
57 const MCFixup &Fixup, MCValue Target,
58 uint64_t &FixedValue) override;
Jim Grosbach2354f872011-06-22 20:14:52 +000059};
60}
61
Jim Grosbach28fcafb2011-06-24 23:44:37 +000062static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType,
63 unsigned &Log2Size) {
Charles Davis8bdfafd2013-09-01 04:28:48 +000064 RelocType = unsigned(MachO::ARM_RELOC_VANILLA);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000065 Log2Size = ~0U;
66
67 switch (Kind) {
68 default:
69 return false;
70
71 case FK_Data_1:
72 Log2Size = llvm::Log2_32(1);
73 return true;
74 case FK_Data_2:
75 Log2Size = llvm::Log2_32(2);
76 return true;
77 case FK_Data_4:
78 Log2Size = llvm::Log2_32(4);
79 return true;
80 case FK_Data_8:
81 Log2Size = llvm::Log2_32(8);
82 return true;
83
Kevin Enderby651898c2013-12-13 22:46:54 +000084 // These fixups are expected to always be resolvable at assembly time and
85 // have no relocations supported.
Jim Grosbach28fcafb2011-06-24 23:44:37 +000086 case ARM::fixup_arm_ldst_pcrel_12:
87 case ARM::fixup_arm_pcrel_10:
88 case ARM::fixup_arm_adr_pcrel_12:
Tim Northover42335572015-04-06 18:44:42 +000089 case ARM::fixup_arm_thumb_br:
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
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000103 case ARM::fixup_t2_uncondbranch:
104 case ARM::fixup_arm_thumb_bl:
105 case ARM::fixup_arm_thumb_blx:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000106 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000107 Log2Size = llvm::Log2_32(4);
108 return true;
109
Jim Grosbach997614f2012-03-20 17:25:45 +0000110 // For movw/movt r_type relocations they always have a pair following them and
111 // the r_length bits are used differently. The encoding of the r_length is as
112 // follows:
113 // low bit of r_length:
114 // 0 - :lower16: for movw instructions
115 // 1 - :upper16: for movt instructions
116 // high bit of r_length:
117 // 0 - arm instructions
118 // 1 - thumb instructions
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000119 case ARM::fixup_arm_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000120 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000121 Log2Size = 1;
122 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000123 case ARM::fixup_t2_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000124 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000125 Log2Size = 3;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000126 return true;
127
128 case ARM::fixup_arm_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000129 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000130 Log2Size = 0;
131 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000132 case ARM::fixup_t2_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000133 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000134 Log2Size = 2;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000135 return true;
136 }
137}
138
139void ARMMachObjectWriter::
Jim Grosbach997614f2012-03-20 17:25:45 +0000140RecordARMScatteredHalfRelocation(MachObjectWriter *Writer,
141 const MCAssembler &Asm,
142 const MCAsmLayout &Layout,
143 const MCFragment *Fragment,
144 const MCFixup &Fixup,
145 MCValue Target,
146 uint64_t &FixedValue) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000147 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
148 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Charles Davis8bdfafd2013-09-01 04:28:48 +0000149 unsigned Type = MachO::ARM_RELOC_HALF;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000150
151 // See <reloc.h>.
152 const MCSymbol *A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000153
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000154 if (!A->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000155 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000156 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000157 "' can not be undefined in a subtraction expression");
158
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000159 uint32_t Value = Writer->getSymbolAddress(*A, Layout);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000160 uint32_t Value2 = 0;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000161 uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000162 FixedValue += SecAddr;
163
164 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000165 const MCSymbol *SB = &B->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000166
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000167 if (!SB->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000168 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000169 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000170 "' can not be undefined in a subtraction expression");
171
172 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000173 Type = MachO::ARM_RELOC_HALF_SECTDIFF;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000174 Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000175 FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000176 }
177
178 // Relocations are written out in reverse order, so the PAIR comes first.
179 // ARM_RELOC_HALF and ARM_RELOC_HALF_SECTDIFF abuse the r_length field:
180 //
181 // For these two r_type relocations they always have a pair following them and
182 // the r_length bits are used differently. The encoding of the r_length is as
183 // follows:
184 // low bit of r_length:
185 // 0 - :lower16: for movw instructions
186 // 1 - :upper16: for movt instructions
187 // high bit of r_length:
188 // 0 - arm instructions
189 // 1 - thumb instructions
190 // the other half of the relocated expression is in the following pair
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000191 // relocation entry in the low 16 bits of r_address field.
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000192 unsigned ThumbBit = 0;
193 unsigned MovtBit = 0;
194 switch ((unsigned)Fixup.getKind()) {
195 default: break;
196 case ARM::fixup_arm_movt_hi16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000197 MovtBit = 1;
Jim Grosbachae913222011-11-29 01:15:25 +0000198 // The thumb bit shouldn't be set in the 'other-half' bit of the
199 // relocation, but it will be set in FixedValue if the base symbol
200 // is a thumb function. Clear it out here.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000201 if (Asm.isThumbFunc(A))
Jim Grosbachae913222011-11-29 01:15:25 +0000202 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000203 break;
204 case ARM::fixup_t2_movt_hi16:
Rafael Espindolab60c8292014-04-29 12:46:50 +0000205 if (Asm.isThumbFunc(A))
Jim Grosbachae913222011-11-29 01:15:25 +0000206 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000207 MovtBit = 1;
208 // Fallthrough
209 case ARM::fixup_t2_movw_lo16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000210 ThumbBit = 1;
211 break;
212 }
213
Charles Davis8bdfafd2013-09-01 04:28:48 +0000214 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000215 uint32_t OtherHalf = MovtBit
216 ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16);
217
Charles Davis8bdfafd2013-09-01 04:28:48 +0000218 MachO::any_relocation_info MRE;
219 MRE.r_word0 = ((OtherHalf << 0) |
220 (MachO::ARM_RELOC_PAIR << 24) |
221 (MovtBit << 28) |
222 (ThumbBit << 29) |
223 (IsPCRel << 30) |
224 MachO::R_SCATTERED);
225 MRE.r_word1 = Value2;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000226 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000227 }
228
Charles Davis8bdfafd2013-09-01 04:28:48 +0000229 MachO::any_relocation_info MRE;
230 MRE.r_word0 = ((FixupOffset << 0) |
231 (Type << 24) |
232 (MovtBit << 28) |
233 (ThumbBit << 29) |
234 (IsPCRel << 30) |
235 MachO::R_SCATTERED);
236 MRE.r_word1 = Value;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000237 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000238}
239
240void ARMMachObjectWriter::RecordARMScatteredRelocation(MachObjectWriter *Writer,
241 const MCAssembler &Asm,
242 const MCAsmLayout &Layout,
243 const MCFragment *Fragment,
244 const MCFixup &Fixup,
245 MCValue Target,
Tim Northover1bc367a2014-07-04 10:58:05 +0000246 unsigned Type,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000247 unsigned Log2Size,
248 uint64_t &FixedValue) {
249 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
250 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000251
252 // See <reloc.h>.
253 const MCSymbol *A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000254
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000255 if (!A->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000256 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000257 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000258 "' can not be undefined in a subtraction expression");
259
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000260 uint32_t Value = Writer->getSymbolAddress(*A, Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000261 uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000262 FixedValue += SecAddr;
263 uint32_t Value2 = 0;
264
265 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Tim Northover1bc367a2014-07-04 10:58:05 +0000266 assert(Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols");
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000267 const MCSymbol *SB = &B->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000268
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000269 if (!SB->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000270 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000271 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000272 "' can not be undefined in a subtraction expression");
273
274 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000275 Type = MachO::ARM_RELOC_SECTDIFF;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000276 Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000277 FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000278 }
279
280 // Relocations are written out in reverse order, so the PAIR comes first.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000281 if (Type == MachO::ARM_RELOC_SECTDIFF ||
282 Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) {
283 MachO::any_relocation_info MRE;
284 MRE.r_word0 = ((0 << 0) |
285 (MachO::ARM_RELOC_PAIR << 24) |
286 (Log2Size << 28) |
287 (IsPCRel << 30) |
288 MachO::R_SCATTERED);
289 MRE.r_word1 = Value2;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000290 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000291 }
292
Charles Davis8bdfafd2013-09-01 04:28:48 +0000293 MachO::any_relocation_info MRE;
294 MRE.r_word0 = ((FixupOffset << 0) |
295 (Type << 24) |
296 (Log2Size << 28) |
297 (IsPCRel << 30) |
298 MachO::R_SCATTERED);
299 MRE.r_word1 = Value;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000300 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000301}
302
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000303bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer,
304 const MCAssembler &Asm,
305 const MCFragment &Fragment,
306 unsigned RelocType,
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000307 const MCSymbol &S,
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000308 uint64_t FixedValue) {
309 // Most cases can be identified purely from the symbol.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000310 if (Writer->doesSymbolRequireExternRelocation(S))
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000311 return true;
312 int64_t Value = (int64_t)FixedValue; // The displacement is signed.
313 int64_t Range;
314 switch (RelocType) {
315 default:
316 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000317 case MachO::ARM_RELOC_BR24:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000318 // PC pre-adjustment of 8 for these instructions.
319 Value -= 8;
320 // ARM BL/BLX has a 25-bit offset.
321 Range = 0x1ffffff;
322 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000323 case MachO::ARM_THUMB_RELOC_BR22:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000324 // PC pre-adjustment of 4 for these instructions.
325 Value -= 4;
326 // Thumb BL/BLX has a 24-bit offset.
327 Range = 0xffffff;
328 }
329 // BL/BLX also use external relocations when an internal relocation
330 // would result in the target being out of range. This gives the linker
331 // enough information to generate a branch island.
Rafael Espindola079027e2015-05-26 00:52:18 +0000332 Value += Writer->getSectionAddress(&S.getSection());
333 Value -= Writer->getSectionAddress(Fragment.getParent());
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000334 // If the resultant value would be out of range for an internal relocation,
335 // use an external instead.
336 if (Value > Range || Value < -(Range + 1))
337 return true;
338 return false;
339}
340
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000341void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer,
Rafael Espindola26585542015-01-19 21:11:14 +0000342 MCAssembler &Asm,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000343 const MCAsmLayout &Layout,
344 const MCFragment *Fragment,
Rafael Espindola26585542015-01-19 21:11:14 +0000345 const MCFixup &Fixup, MCValue Target,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000346 uint64_t &FixedValue) {
347 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
348 unsigned Log2Size;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000349 unsigned RelocType = MachO::ARM_RELOC_VANILLA;
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000350 if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size))
351 // If we failed to get fixup kind info, it's because there's no legal
352 // relocation type for the fixup kind. This happens when it's a fixup that's
353 // expected to always be resolvable at assembly time and not have any
354 // relocations needed.
Jim Grosbach6f482002015-05-18 18:43:14 +0000355 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000356 "unsupported relocation on symbol");
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000357
358 // If this is a difference or a defined symbol plus an offset, then we need a
359 // scattered relocation entry. Differences always require scattered
360 // relocations.
361 if (Target.getSymB()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000362 if (RelocType == MachO::ARM_RELOC_HALF)
Jim Grosbach997614f2012-03-20 17:25:45 +0000363 return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment,
364 Fixup, Target, FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000365 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
Tim Northover1bc367a2014-07-04 10:58:05 +0000366 Target, RelocType, Log2Size,
367 FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000368 }
369
370 // Get the symbol data, if any.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000371 const MCSymbol *A = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000372 if (Target.getSymA())
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000373 A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000374
375 // FIXME: For other platforms, we need to use scattered relocations for
376 // internal relocations with offsets. If this is an internal relocation with
377 // an offset, it also needs a scattered relocation entry.
378 //
379 // Is this right for ARM?
380 uint32_t Offset = Target.getConstant();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000381 if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA)
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000382 Offset += 1 << Log2Size;
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000383 if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A))
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000384 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
Tim Northover1bc367a2014-07-04 10:58:05 +0000385 Target, RelocType, Log2Size,
386 FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000387
388 // See <reloc.h>.
389 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
390 unsigned Index = 0;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000391 unsigned Type = 0;
Duncan P. N. Exon Smith6e23e5a2015-05-16 01:14:19 +0000392 const MCSymbol *RelSymbol = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000393
394 if (Target.isAbsolute()) { // constant
395 // FIXME!
396 report_fatal_error("FIXME: relocations to absolute targets "
397 "not yet implemented");
398 } else {
399 // Resolve constant variables.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000400 if (A->isVariable()) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000401 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000402 if (A->getVariableValue()->evaluateAsAbsolute(
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000403 Res, Layout, Writer->getSectionAddressMap())) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000404 FixedValue = Res;
405 return;
406 }
407 }
408
409 // Check whether we need an external or internal relocation.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000410 if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A,
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000411 FixedValue)) {
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000412 RelSymbol = A;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000413
414 // For external relocations, make sure to offset the fixup value to
415 // compensate for the addend of the symbol address, if it was
416 // undefined. This occurs with weak definitions, for example.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000417 if (!A->isUndefined())
418 FixedValue -= Layout.getSymbolOffset(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000419 } else {
420 // The index is the section ordinal (1-based).
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000421 const MCSection &Sec = A->getSection();
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000422 Index = Sec.getOrdinal() + 1;
Rafael Espindola079027e2015-05-26 00:52:18 +0000423 FixedValue += Writer->getSectionAddress(&Sec);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000424 }
425 if (IsPCRel)
Rafael Espindola079027e2015-05-26 00:52:18 +0000426 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000427
428 // The type is determined by the fixup kind.
429 Type = RelocType;
430 }
431
432 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000433 MachO::any_relocation_info MRE;
434 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000435 MRE.r_word1 =
436 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Jim Grosbach997614f2012-03-20 17:25:45 +0000437
438 // Even when it's not a scattered relocation, movw/movt always uses
439 // a PAIR relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000440 if (Type == MachO::ARM_RELOC_HALF) {
Kevin Enderby5c490f12012-07-30 18:46:15 +0000441 // The other-half value only gets populated for the movt and movw
442 // relocation entries.
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000443 uint32_t Value = 0;
Jim Grosbach997614f2012-03-20 17:25:45 +0000444 switch ((unsigned)Fixup.getKind()) {
445 default: break;
Kevin Enderby5c490f12012-07-30 18:46:15 +0000446 case ARM::fixup_arm_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000447 case ARM::fixup_t2_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000448 Value = (FixedValue >> 16) & 0xffff;
449 break;
Jim Grosbach997614f2012-03-20 17:25:45 +0000450 case ARM::fixup_arm_movt_hi16:
Jim Grosbach997614f2012-03-20 17:25:45 +0000451 case ARM::fixup_t2_movt_hi16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000452 Value = FixedValue & 0xffff;
Jim Grosbach997614f2012-03-20 17:25:45 +0000453 break;
454 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000455 MachO::any_relocation_info MREPair;
456 MREPair.r_word0 = Value;
457 MREPair.r_word1 = ((0xffffff << 0) |
458 (Log2Size << 25) |
459 (MachO::ARM_RELOC_PAIR << 28));
Jim Grosbach997614f2012-03-20 17:25:45 +0000460
Rafael Espindola61e724a2015-05-26 01:15:30 +0000461 Writer->addRelocation(nullptr, Fragment->getParent(), MREPair);
Jim Grosbach997614f2012-03-20 17:25:45 +0000462 }
463
Rafael Espindola61e724a2015-05-26 01:15:30 +0000464 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000465}
466
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000467MCObjectWriter *llvm::createARMMachObjectWriter(raw_pwrite_stream &OS,
468 bool Is64Bit, uint32_t CPUType,
Jim Grosbach2354f872011-06-22 20:14:52 +0000469 uint32_t CPUSubtype) {
470 return createMachObjectWriter(new ARMMachObjectWriter(Is64Bit,
471 CPUType,
472 CPUSubtype),
473 OS, /*IsLittleEndian=*/true);
474}