blob: 6ac778e0cecd6fcd9636c83aef63195c72a5fa56 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCMachObjectWriter.h"
Rafael Espindola6e6820a2015-05-25 14:12:48 +000021#include "llvm/MC/MCSection.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,
Tim Northover1bc367a2014-07-04 10:58:05 +000035 unsigned Type,
Jim Grosbach28fcafb2011-06-24 23:44:37 +000036 unsigned Log2Size,
37 uint64_t &FixedValue);
Jim Grosbach997614f2012-03-20 17:25:45 +000038 void RecordARMScatteredHalfRelocation(MachObjectWriter *Writer,
39 const MCAssembler &Asm,
40 const MCAsmLayout &Layout,
41 const MCFragment *Fragment,
42 const MCFixup &Fixup, MCValue Target,
43 uint64_t &FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000044
Jim Grosbachdf8ed712012-09-25 18:07:17 +000045 bool requiresExternRelocation(MachObjectWriter *Writer,
46 const MCAssembler &Asm,
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +000047 const MCFragment &Fragment, unsigned RelocType,
48 const MCSymbol &S, uint64_t FixedValue);
Jim Grosbachdf8ed712012-09-25 18:07:17 +000049
Jim Grosbach2354f872011-06-22 20:14:52 +000050public:
Jim Grosbach7c76b4c2015-06-04 20:27:42 +000051 ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype)
52 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbach28fcafb2011-06-24 23:44:37 +000053
Jim Grosbach36e60e92015-06-04 22:24:41 +000054 void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
Rafael Espindola26585542015-01-19 21:11:14 +000055 const MCAsmLayout &Layout, const MCFragment *Fragment,
56 const MCFixup &Fixup, MCValue Target,
57 uint64_t &FixedValue) override;
Jim Grosbach2354f872011-06-22 20:14:52 +000058};
Alexander Kornienko70bc5f12015-06-19 15:57:42 +000059} // namespace
Jim Grosbach2354f872011-06-22 20:14:52 +000060
Jim Grosbach28fcafb2011-06-24 23:44:37 +000061static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType,
62 unsigned &Log2Size) {
Charles Davis8bdfafd2013-09-01 04:28:48 +000063 RelocType = unsigned(MachO::ARM_RELOC_VANILLA);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000064 Log2Size = ~0U;
65
66 switch (Kind) {
67 default:
68 return false;
69
70 case FK_Data_1:
71 Log2Size = llvm::Log2_32(1);
72 return true;
73 case FK_Data_2:
74 Log2Size = llvm::Log2_32(2);
75 return true;
76 case FK_Data_4:
77 Log2Size = llvm::Log2_32(4);
78 return true;
79 case FK_Data_8:
80 Log2Size = llvm::Log2_32(8);
81 return true;
82
Kevin Enderby651898c2013-12-13 22:46:54 +000083 // These fixups are expected to always be resolvable at assembly time and
84 // have no relocations supported.
Jim Grosbach28fcafb2011-06-24 23:44:37 +000085 case ARM::fixup_arm_ldst_pcrel_12:
86 case ARM::fixup_arm_pcrel_10:
87 case ARM::fixup_arm_adr_pcrel_12:
Tim Northover42335572015-04-06 18:44:42 +000088 case ARM::fixup_arm_thumb_br:
Kevin Enderby651898c2013-12-13 22:46:54 +000089 return false;
90
91 // Handle 24-bit branch kinds.
Jim Grosbach28fcafb2011-06-24 23:44:37 +000092 case ARM::fixup_arm_condbranch:
93 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +000094 case ARM::fixup_arm_uncondbl:
95 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +000096 case ARM::fixup_arm_blx:
Charles Davis8bdfafd2013-09-01 04:28:48 +000097 RelocType = unsigned(MachO::ARM_RELOC_BR24);
Jim Grosbach28fcafb2011-06-24 23:44:37 +000098 // Report as 'long', even though that is not quite accurate.
99 Log2Size = llvm::Log2_32(4);
100 return true;
101
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000102 case ARM::fixup_t2_uncondbranch:
103 case ARM::fixup_arm_thumb_bl:
104 case ARM::fixup_arm_thumb_blx:
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(4);
107 return true;
108
Jim Grosbach997614f2012-03-20 17:25:45 +0000109 // For movw/movt r_type relocations they always have a pair following them and
110 // the r_length bits are used differently. The encoding of the r_length is as
111 // follows:
112 // low bit of r_length:
113 // 0 - :lower16: for movw instructions
114 // 1 - :upper16: for movt instructions
115 // high bit of r_length:
116 // 0 - arm instructions
117 // 1 - thumb instructions
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000118 case ARM::fixup_arm_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000119 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000120 Log2Size = 1;
121 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000122 case ARM::fixup_t2_movt_hi16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000123 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000124 Log2Size = 3;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000125 return true;
126
127 case ARM::fixup_arm_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000128 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000129 Log2Size = 0;
130 return true;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000131 case ARM::fixup_t2_movw_lo16:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000132 RelocType = unsigned(MachO::ARM_RELOC_HALF);
Jim Grosbach997614f2012-03-20 17:25:45 +0000133 Log2Size = 2;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000134 return true;
135 }
136}
137
138void ARMMachObjectWriter::
Jim Grosbach997614f2012-03-20 17:25:45 +0000139RecordARMScatteredHalfRelocation(MachObjectWriter *Writer,
140 const MCAssembler &Asm,
141 const MCAsmLayout &Layout,
142 const MCFragment *Fragment,
143 const MCFixup &Fixup,
144 MCValue Target,
145 uint64_t &FixedValue) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000146 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
147 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Charles Davis8bdfafd2013-09-01 04:28:48 +0000148 unsigned Type = MachO::ARM_RELOC_HALF;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000149
150 // See <reloc.h>.
151 const MCSymbol *A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000152
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000153 if (!A->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000154 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000155 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000156 "' can not be undefined in a subtraction expression");
157
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000158 uint32_t Value = Writer->getSymbolAddress(*A, Layout);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000159 uint32_t Value2 = 0;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000160 uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000161 FixedValue += SecAddr;
162
163 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000164 const MCSymbol *SB = &B->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000165
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000166 if (!SB->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000167 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000168 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000169 "' can not be undefined in a subtraction expression");
170
171 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000172 Type = MachO::ARM_RELOC_HALF_SECTDIFF;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000173 Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000174 FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000175 }
176
177 // Relocations are written out in reverse order, so the PAIR comes first.
178 // ARM_RELOC_HALF and ARM_RELOC_HALF_SECTDIFF abuse the r_length field:
179 //
180 // For these two r_type relocations they always have a pair following them and
181 // the r_length bits are used differently. The encoding of the r_length is as
182 // follows:
183 // low bit of r_length:
184 // 0 - :lower16: for movw instructions
185 // 1 - :upper16: for movt instructions
186 // high bit of r_length:
187 // 0 - arm instructions
188 // 1 - thumb instructions
189 // the other half of the relocated expression is in the following pair
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000190 // relocation entry in the low 16 bits of r_address field.
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000191 unsigned ThumbBit = 0;
192 unsigned MovtBit = 0;
193 switch ((unsigned)Fixup.getKind()) {
194 default: break;
195 case ARM::fixup_arm_movt_hi16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000196 MovtBit = 1;
Jim Grosbachae913222011-11-29 01:15:25 +0000197 // The thumb bit shouldn't be set in the 'other-half' bit of the
198 // relocation, but it will be set in FixedValue if the base symbol
199 // is a thumb function. Clear it out here.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000200 if (Asm.isThumbFunc(A))
Jim Grosbachae913222011-11-29 01:15:25 +0000201 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000202 break;
203 case ARM::fixup_t2_movt_hi16:
Rafael Espindolab60c8292014-04-29 12:46:50 +0000204 if (Asm.isThumbFunc(A))
Jim Grosbachae913222011-11-29 01:15:25 +0000205 FixedValue &= 0xfffffffe;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000206 MovtBit = 1;
207 // Fallthrough
208 case ARM::fixup_t2_movw_lo16:
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000209 ThumbBit = 1;
210 break;
211 }
212
Charles Davis8bdfafd2013-09-01 04:28:48 +0000213 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000214 uint32_t OtherHalf = MovtBit
215 ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16);
216
Charles Davis8bdfafd2013-09-01 04:28:48 +0000217 MachO::any_relocation_info MRE;
218 MRE.r_word0 = ((OtherHalf << 0) |
219 (MachO::ARM_RELOC_PAIR << 24) |
220 (MovtBit << 28) |
221 (ThumbBit << 29) |
222 (IsPCRel << 30) |
223 MachO::R_SCATTERED);
224 MRE.r_word1 = Value2;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000225 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000226 }
227
Charles Davis8bdfafd2013-09-01 04:28:48 +0000228 MachO::any_relocation_info MRE;
229 MRE.r_word0 = ((FixupOffset << 0) |
230 (Type << 24) |
231 (MovtBit << 28) |
232 (ThumbBit << 29) |
233 (IsPCRel << 30) |
234 MachO::R_SCATTERED);
235 MRE.r_word1 = Value;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000236 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000237}
238
239void ARMMachObjectWriter::RecordARMScatteredRelocation(MachObjectWriter *Writer,
240 const MCAssembler &Asm,
241 const MCAsmLayout &Layout,
242 const MCFragment *Fragment,
243 const MCFixup &Fixup,
244 MCValue Target,
Tim Northover1bc367a2014-07-04 10:58:05 +0000245 unsigned Type,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000246 unsigned Log2Size,
247 uint64_t &FixedValue) {
248 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
249 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000250
251 // See <reloc.h>.
252 const MCSymbol *A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000253
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000254 if (!A->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000255 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000256 "symbol '" + A->getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000257 "' can not be undefined in a subtraction expression");
258
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000259 uint32_t Value = Writer->getSymbolAddress(*A, Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000260 uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000261 FixedValue += SecAddr;
262 uint32_t Value2 = 0;
263
264 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Tim Northover1bc367a2014-07-04 10:58:05 +0000265 assert(Type == MachO::ARM_RELOC_VANILLA && "invalid reloc for 2 symbols");
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000266 const MCSymbol *SB = &B->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000267
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000268 if (!SB->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000269 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach20275a82012-01-27 00:37:12 +0000270 "symbol '" + B->getSymbol().getName() +
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000271 "' can not be undefined in a subtraction expression");
272
273 // Select the appropriate difference relocation type.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000274 Type = MachO::ARM_RELOC_SECTDIFF;
Duncan P. N. Exon Smith99d8a8e2015-05-20 00:02:39 +0000275 Value2 = Writer->getSymbolAddress(B->getSymbol(), Layout);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000276 FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000277 }
278
279 // Relocations are written out in reverse order, so the PAIR comes first.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000280 if (Type == MachO::ARM_RELOC_SECTDIFF ||
281 Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) {
282 MachO::any_relocation_info MRE;
283 MRE.r_word0 = ((0 << 0) |
284 (MachO::ARM_RELOC_PAIR << 24) |
285 (Log2Size << 28) |
286 (IsPCRel << 30) |
287 MachO::R_SCATTERED);
288 MRE.r_word1 = Value2;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000289 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000290 }
291
Charles Davis8bdfafd2013-09-01 04:28:48 +0000292 MachO::any_relocation_info MRE;
293 MRE.r_word0 = ((FixupOffset << 0) |
294 (Type << 24) |
295 (Log2Size << 28) |
296 (IsPCRel << 30) |
297 MachO::R_SCATTERED);
298 MRE.r_word1 = Value;
Rafael Espindola61e724a2015-05-26 01:15:30 +0000299 Writer->addRelocation(nullptr, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000300}
301
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000302bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer,
303 const MCAssembler &Asm,
304 const MCFragment &Fragment,
305 unsigned RelocType,
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000306 const MCSymbol &S,
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000307 uint64_t FixedValue) {
308 // Most cases can be identified purely from the symbol.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000309 if (Writer->doesSymbolRequireExternRelocation(S))
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000310 return true;
311 int64_t Value = (int64_t)FixedValue; // The displacement is signed.
312 int64_t Range;
313 switch (RelocType) {
314 default:
315 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000316 case MachO::ARM_RELOC_BR24:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000317 // PC pre-adjustment of 8 for these instructions.
318 Value -= 8;
319 // ARM BL/BLX has a 25-bit offset.
320 Range = 0x1ffffff;
321 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000322 case MachO::ARM_THUMB_RELOC_BR22:
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000323 // PC pre-adjustment of 4 for these instructions.
324 Value -= 4;
325 // Thumb BL/BLX has a 24-bit offset.
326 Range = 0xffffff;
327 }
328 // BL/BLX also use external relocations when an internal relocation
329 // would result in the target being out of range. This gives the linker
330 // enough information to generate a branch island.
Rafael Espindola079027e2015-05-26 00:52:18 +0000331 Value += Writer->getSectionAddress(&S.getSection());
332 Value -= Writer->getSectionAddress(Fragment.getParent());
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000333 // If the resultant value would be out of range for an internal relocation,
334 // use an external instead.
335 if (Value > Range || Value < -(Range + 1))
336 return true;
337 return false;
338}
339
Jim Grosbach36e60e92015-06-04 22:24:41 +0000340void ARMMachObjectWriter::recordRelocation(MachObjectWriter *Writer,
Rafael Espindola26585542015-01-19 21:11:14 +0000341 MCAssembler &Asm,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000342 const MCAsmLayout &Layout,
343 const MCFragment *Fragment,
Rafael Espindola26585542015-01-19 21:11:14 +0000344 const MCFixup &Fixup, MCValue Target,
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000345 uint64_t &FixedValue) {
346 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
347 unsigned Log2Size;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000348 unsigned RelocType = MachO::ARM_RELOC_VANILLA;
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000349 if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size))
350 // If we failed to get fixup kind info, it's because there's no legal
351 // relocation type for the fixup kind. This happens when it's a fixup that's
352 // expected to always be resolvable at assembly time and not have any
353 // relocations needed.
Jim Grosbach6f482002015-05-18 18:43:14 +0000354 Asm.getContext().reportFatalError(Fixup.getLoc(),
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000355 "unsupported relocation on symbol");
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000356
357 // If this is a difference or a defined symbol plus an offset, then we need a
358 // scattered relocation entry. Differences always require scattered
359 // relocations.
360 if (Target.getSymB()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000361 if (RelocType == MachO::ARM_RELOC_HALF)
Jim Grosbach997614f2012-03-20 17:25:45 +0000362 return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment,
363 Fixup, Target, FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000364 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
Tim Northover1bc367a2014-07-04 10:58:05 +0000365 Target, RelocType, Log2Size,
366 FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000367 }
368
369 // Get the symbol data, if any.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000370 const MCSymbol *A = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000371 if (Target.getSymA())
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000372 A = &Target.getSymA()->getSymbol();
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000373
374 // FIXME: For other platforms, we need to use scattered relocations for
375 // internal relocations with offsets. If this is an internal relocation with
376 // an offset, it also needs a scattered relocation entry.
377 //
378 // Is this right for ARM?
379 uint32_t Offset = Target.getConstant();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000380 if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA)
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000381 Offset += 1 << Log2Size;
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000382 if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A))
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000383 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
Tim Northover1bc367a2014-07-04 10:58:05 +0000384 Target, RelocType, Log2Size,
385 FixedValue);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000386
387 // See <reloc.h>.
388 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
389 unsigned Index = 0;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000390 unsigned Type = 0;
Duncan P. N. Exon Smith6e23e5a2015-05-16 01:14:19 +0000391 const MCSymbol *RelSymbol = nullptr;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000392
393 if (Target.isAbsolute()) { // constant
394 // FIXME!
395 report_fatal_error("FIXME: relocations to absolute targets "
396 "not yet implemented");
397 } else {
398 // Resolve constant variables.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000399 if (A->isVariable()) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000400 int64_t Res;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000401 if (A->getVariableValue()->evaluateAsAbsolute(
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000402 Res, Layout, Writer->getSectionAddressMap())) {
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000403 FixedValue = Res;
404 return;
405 }
406 }
407
408 // Check whether we need an external or internal relocation.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000409 if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A,
Jim Grosbachdf8ed712012-09-25 18:07:17 +0000410 FixedValue)) {
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000411 RelSymbol = A;
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000412
413 // For external relocations, make sure to offset the fixup value to
414 // compensate for the addend of the symbol address, if it was
415 // undefined. This occurs with weak definitions, for example.
Duncan P. N. Exon Smith08b87262015-05-20 15:16:14 +0000416 if (!A->isUndefined())
417 FixedValue -= Layout.getSymbolOffset(*A);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000418 } else {
419 // The index is the section ordinal (1-based).
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000420 const MCSection &Sec = A->getSection();
Rafael Espindola6e6820a2015-05-25 14:12:48 +0000421 Index = Sec.getOrdinal() + 1;
Rafael Espindola079027e2015-05-26 00:52:18 +0000422 FixedValue += Writer->getSectionAddress(&Sec);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000423 }
424 if (IsPCRel)
Rafael Espindola079027e2015-05-26 00:52:18 +0000425 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000426
427 // The type is determined by the fixup kind.
428 Type = RelocType;
429 }
430
431 // struct relocation_info (8 bytes)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000432 MachO::any_relocation_info MRE;
433 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000434 MRE.r_word1 =
435 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Jim Grosbach997614f2012-03-20 17:25:45 +0000436
437 // Even when it's not a scattered relocation, movw/movt always uses
438 // a PAIR relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000439 if (Type == MachO::ARM_RELOC_HALF) {
Kevin Enderby5c490f12012-07-30 18:46:15 +0000440 // The other-half value only gets populated for the movt and movw
441 // relocation entries.
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000442 uint32_t Value = 0;
Jim Grosbach997614f2012-03-20 17:25:45 +0000443 switch ((unsigned)Fixup.getKind()) {
444 default: break;
Kevin Enderby5c490f12012-07-30 18:46:15 +0000445 case ARM::fixup_arm_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000446 case ARM::fixup_t2_movw_lo16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000447 Value = (FixedValue >> 16) & 0xffff;
448 break;
Jim Grosbach997614f2012-03-20 17:25:45 +0000449 case ARM::fixup_arm_movt_hi16:
Jim Grosbach997614f2012-03-20 17:25:45 +0000450 case ARM::fixup_t2_movt_hi16:
Kevin Enderby5c490f12012-07-30 18:46:15 +0000451 Value = FixedValue & 0xffff;
Jim Grosbach997614f2012-03-20 17:25:45 +0000452 break;
453 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000454 MachO::any_relocation_info MREPair;
455 MREPair.r_word0 = Value;
456 MREPair.r_word1 = ((0xffffff << 0) |
457 (Log2Size << 25) |
458 (MachO::ARM_RELOC_PAIR << 28));
Jim Grosbach997614f2012-03-20 17:25:45 +0000459
Rafael Espindola61e724a2015-05-26 01:15:30 +0000460 Writer->addRelocation(nullptr, Fragment->getParent(), MREPair);
Jim Grosbach997614f2012-03-20 17:25:45 +0000461 }
462
Rafael Espindola61e724a2015-05-26 01:15:30 +0000463 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Jim Grosbach28fcafb2011-06-24 23:44:37 +0000464}
465
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000466MCObjectWriter *llvm::createARMMachObjectWriter(raw_pwrite_stream &OS,
467 bool Is64Bit, uint32_t CPUType,
Jim Grosbach2354f872011-06-22 20:14:52 +0000468 uint32_t CPUSubtype) {
469 return createMachObjectWriter(new ARMMachObjectWriter(Is64Bit,
470 CPUType,
471 CPUSubtype),
472 OS, /*IsLittleEndian=*/true);
473}