blob: faaf33a9710d65fd335472d70501395f5e4356c6 [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- AArch64MachObjectWriter.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
10#include "MCTargetDesc/AArch64FixupKinds.h"
11#include "MCTargetDesc/AArch64MCTargetDesc.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000012#include "llvm/ADT/Twine.h"
Rafael Espindola26585542015-01-19 21:11:14 +000013#include "llvm/MC/MCAsmInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000014#include "llvm/MC/MCAsmLayout.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000015#include "llvm/MC/MCAssembler.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixup.h"
19#include "llvm/MC/MCMachObjectWriter.h"
20#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCValue.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/MachO.h"
24using namespace llvm;
25
26namespace {
27class AArch64MachObjectWriter : public MCMachObjectTargetWriter {
28 bool getAArch64FixupKindMachOInfo(const MCFixup &Fixup, unsigned &RelocType,
29 const MCSymbolRefExpr *Sym,
30 unsigned &Log2Size, const MCAssembler &Asm);
31
32public:
33 AArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype)
34 : MCMachObjectTargetWriter(true /* is64Bit */, CPUType, CPUSubtype,
35 /*UseAggressiveSymbolFolding=*/true) {}
36
Rafael Espindola26585542015-01-19 21:11:14 +000037 void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
Tim Northover3b0846e2014-05-24 12:50:23 +000038 const MCAsmLayout &Layout, const MCFragment *Fragment,
39 const MCFixup &Fixup, MCValue Target,
40 uint64_t &FixedValue) override;
41};
42}
43
44bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo(
45 const MCFixup &Fixup, unsigned &RelocType, const MCSymbolRefExpr *Sym,
46 unsigned &Log2Size, const MCAssembler &Asm) {
47 RelocType = unsigned(MachO::ARM64_RELOC_UNSIGNED);
48 Log2Size = ~0U;
49
50 switch ((unsigned)Fixup.getKind()) {
51 default:
52 return false;
53
54 case FK_Data_1:
55 Log2Size = llvm::Log2_32(1);
56 return true;
57 case FK_Data_2:
58 Log2Size = llvm::Log2_32(2);
59 return true;
60 case FK_Data_4:
61 Log2Size = llvm::Log2_32(4);
62 if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
63 RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
64 return true;
65 case FK_Data_8:
66 Log2Size = llvm::Log2_32(8);
67 if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
68 RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
69 return true;
70 case AArch64::fixup_aarch64_add_imm12:
71 case AArch64::fixup_aarch64_ldst_imm12_scale1:
72 case AArch64::fixup_aarch64_ldst_imm12_scale2:
73 case AArch64::fixup_aarch64_ldst_imm12_scale4:
74 case AArch64::fixup_aarch64_ldst_imm12_scale8:
75 case AArch64::fixup_aarch64_ldst_imm12_scale16:
76 Log2Size = llvm::Log2_32(4);
77 switch (Sym->getKind()) {
78 default:
Craig Topper2a30d782014-06-18 05:05:13 +000079 llvm_unreachable("Unexpected symbol reference variant kind!");
Tim Northover3b0846e2014-05-24 12:50:23 +000080 case MCSymbolRefExpr::VK_PAGEOFF:
81 RelocType = unsigned(MachO::ARM64_RELOC_PAGEOFF12);
82 return true;
83 case MCSymbolRefExpr::VK_GOTPAGEOFF:
84 RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12);
85 return true;
86 case MCSymbolRefExpr::VK_TLVPPAGEOFF:
87 RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12);
88 return true;
89 }
90 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
91 Log2Size = llvm::Log2_32(4);
92 // This encompasses the relocation for the whole 21-bit value.
93 switch (Sym->getKind()) {
94 default:
95 Asm.getContext().FatalError(Fixup.getLoc(),
96 "ADR/ADRP relocations must be GOT relative");
97 case MCSymbolRefExpr::VK_PAGE:
98 RelocType = unsigned(MachO::ARM64_RELOC_PAGE21);
99 return true;
100 case MCSymbolRefExpr::VK_GOTPAGE:
101 RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGE21);
102 return true;
103 case MCSymbolRefExpr::VK_TLVPPAGE:
104 RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGE21);
105 return true;
106 }
107 return true;
108 case AArch64::fixup_aarch64_pcrel_branch26:
109 case AArch64::fixup_aarch64_pcrel_call26:
110 Log2Size = llvm::Log2_32(4);
111 RelocType = unsigned(MachO::ARM64_RELOC_BRANCH26);
112 return true;
113 }
114}
115
Rafael Espindola26585542015-01-19 21:11:14 +0000116static bool canUseLocalRelocation(const MCSectionMachO &Section,
117 const MCSymbol &Symbol, unsigned Log2Size) {
118 // Debug info sections can use local relocations.
119 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
120 return true;
121
122 // Otherwise, only pointer sized relocations are supported.
123 if (Log2Size != 3)
124 return false;
125
126 // But only if they don't point to a few forbidden sections.
127 if (!Symbol.isInSection())
128 return true;
129 const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
130 if (RefSec.getType() == MachO::S_CSTRING_LITERALS)
131 return false;
132
133 if (RefSec.getSegmentName() == "__DATA" &&
134 RefSec.getSectionName() == "__cfstring")
135 return false;
136
Rafael Espindolae4bcad42015-02-12 23:11:59 +0000137 if (RefSec.getSegmentName() == "__DATA" &&
138 RefSec.getSectionName() == "__objc_classrefs")
139 return false;
140
Rafael Espindola26585542015-01-19 21:11:14 +0000141 return true;
142}
143
Tim Northover3b0846e2014-05-24 12:50:23 +0000144void AArch64MachObjectWriter::RecordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000145 MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
Tim Northover3b0846e2014-05-24 12:50:23 +0000146 const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
147 uint64_t &FixedValue) {
148 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
149
150 // See <reloc.h>.
151 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
152 unsigned Log2Size = 0;
153 int64_t Value = 0;
154 unsigned Index = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 unsigned Type = 0;
156 unsigned Kind = Fixup.getKind();
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000157 const MCSymbol *RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000158
159 FixupOffset += Fixup.getOffset();
160
161 // AArch64 pcrel relocation addends do not include the section offset.
162 if (IsPCRel)
163 FixedValue += FixupOffset;
164
165 // ADRP fixups use relocations for the whole symbol value and only
166 // put the addend in the instruction itself. Clear out any value the
167 // generic code figured out from the sybmol definition.
168 if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21)
169 FixedValue = 0;
170
171 // imm19 relocations are for conditional branches, which require
172 // assembler local symbols. If we got here, that's not what we have,
173 // so complain loudly.
174 if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
175 Asm.getContext().FatalError(Fixup.getLoc(),
176 "conditional branch requires assembler-local"
177 " label. '" +
178 Target.getSymA()->getSymbol().getName() +
179 "' is external.");
180 return;
181 }
182
183 // 14-bit branch relocations should only target internal labels, and so
184 // should never get here.
185 if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
186 Asm.getContext().FatalError(Fixup.getLoc(),
187 "Invalid relocation on conditional branch!");
188 return;
189 }
190
191 if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
192 Asm)) {
193 Asm.getContext().FatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
194 return;
195 }
196
197 Value = Target.getConstant();
198
199 if (Target.isAbsolute()) { // constant
200 // FIXME: Should this always be extern?
201 // SymbolNum of 0 indicates the absolute section.
202 Type = MachO::ARM64_RELOC_UNSIGNED;
Tim Northover3b0846e2014-05-24 12:50:23 +0000203
204 if (IsPCRel) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000205 Asm.getContext().FatalError(Fixup.getLoc(),
206 "PC relative absolute relocation!");
207
208 // FIXME: x86_64 sets the type to a branch reloc here. Should we do
209 // something similar?
210 }
211 } else if (Target.getSymB()) { // A - B + constant
212 const MCSymbol *A = &Target.getSymA()->getSymbol();
213 const MCSymbolData &A_SD = Asm.getSymbolData(*A);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000214 const MCSymbol *A_Base = Asm.getAtom(&A_SD);
Tim Northover3b0846e2014-05-24 12:50:23 +0000215
216 const MCSymbol *B = &Target.getSymB()->getSymbol();
217 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000218 const MCSymbol *B_Base = Asm.getAtom(&B_SD);
Tim Northover3b0846e2014-05-24 12:50:23 +0000219
220 // Check for "_foo@got - .", which comes through here as:
221 // Ltmp0:
222 // ... _foo@got - Ltmp0
223 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
224 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
225 Layout.getSymbolOffset(&B_SD) ==
226 Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
227 // SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
Tim Northover3b0846e2014-05-24 12:50:23 +0000228 Type = MachO::ARM64_RELOC_POINTER_TO_GOT;
229 IsPCRel = 1;
230 MachO::any_relocation_info MRE;
231 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000232 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000233 Writer->addRelocation(A_Base ? &A_Base->getData() : nullptr,
234 Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000235 return;
236 } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
237 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
238 // Otherwise, neither symbol can be modified.
239 Asm.getContext().FatalError(Fixup.getLoc(),
240 "unsupported relocation of modified symbol");
241
242 // We don't support PCrel relocations of differences.
243 if (IsPCRel)
244 Asm.getContext().FatalError(Fixup.getLoc(),
245 "unsupported pc-relative relocation of "
246 "difference");
247
248 // AArch64 always uses external relocations. If there is no symbol to use as
249 // a base address (a local symbol with no preceding non-local symbol),
250 // error out.
251 //
252 // FIXME: We should probably just synthesize an external symbol and use
253 // that.
254 if (!A_Base)
255 Asm.getContext().FatalError(
256 Fixup.getLoc(),
257 "unsupported relocation of local symbol '" + A->getName() +
258 "'. Must have non-local symbol earlier in section.");
259 if (!B_Base)
260 Asm.getContext().FatalError(
261 Fixup.getLoc(),
262 "unsupported relocation of local symbol '" + B->getName() +
263 "'. Must have non-local symbol earlier in section.");
264
265 if (A_Base == B_Base && A_Base)
266 Asm.getContext().FatalError(Fixup.getLoc(),
267 "unsupported relocation with identical base");
268
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000269 Value +=
270 (!A_SD.getFragment() ? 0 : Writer->getSymbolAddress(&A_SD, Layout)) -
271 (!A_Base || !A_Base->getData().getFragment()
272 ? 0
273 : Writer->getSymbolAddress(&A_Base->getData(), Layout));
274 Value -=
275 (!B_SD.getFragment() ? 0 : Writer->getSymbolAddress(&B_SD, Layout)) -
276 (!B_Base || !B_Base->getData().getFragment()
277 ? 0
278 : Writer->getSymbolAddress(&B_Base->getData(), Layout));
Tim Northover3b0846e2014-05-24 12:50:23 +0000279
Tim Northover3b0846e2014-05-24 12:50:23 +0000280 Type = MachO::ARM64_RELOC_UNSIGNED;
281
282 MachO::any_relocation_info MRE;
283 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000284 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000285 Writer->addRelocation(A_Base ? &A_Base->getData() : nullptr,
286 Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000287
Rafael Espindola26585542015-01-19 21:11:14 +0000288 RelSymbol = B_Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000289 Type = MachO::ARM64_RELOC_SUBTRACTOR;
290 } else { // A + constant
291 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Rafael Espindola7244bb32015-01-14 19:07:23 +0000292 const MCSectionMachO &Section = static_cast<const MCSectionMachO &>(
293 Fragment->getParent()->getSection());
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000294
Rafael Espindola26585542015-01-19 21:11:14 +0000295 bool CanUseLocalRelocation =
296 canUseLocalRelocation(Section, *Symbol, Log2Size);
297 if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) {
298 const MCSection &Sec = Symbol->getSection();
299 if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
300 Asm.addLocalUsedInReloc(*Symbol);
301 }
302
303 const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000304 const MCSymbol *Base = Asm.getAtom(&SD);
Rafael Espindola26585542015-01-19 21:11:14 +0000305
Tim Northover3b0846e2014-05-24 12:50:23 +0000306 // If the symbol is a variable and we weren't able to get a Base for it
307 // (i.e., it's not in the symbol table associated with a section) resolve
308 // the relocation based its expansion instead.
309 if (Symbol->isVariable() && !Base) {
310 // If the evaluation is an absolute value, just use that directly
311 // to keep things easy.
312 int64_t Res;
313 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
314 Res, Layout, Writer->getSectionAddressMap())) {
315 FixedValue = Res;
316 return;
317 }
318
319 // FIXME: Will the Target we already have ever have any data in it
320 // we need to preserve and merge with the new Target? How about
321 // the FixedValue?
Joerg Sonnenberger752b91b2014-08-10 11:35:12 +0000322 if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
323 &Fixup))
Tim Northover3b0846e2014-05-24 12:50:23 +0000324 Asm.getContext().FatalError(Fixup.getLoc(),
325 "unable to resolve variable '" +
326 Symbol->getName() + "'");
327 return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
328 FixedValue);
329 }
330
331 // Relocations inside debug sections always use local relocations when
332 // possible. This seems to be done because the debugger doesn't fully
333 // understand relocation entries and expects to find values that
334 // have already been fixed up.
335 if (Symbol->isInSection()) {
336 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
337 Base = nullptr;
338 }
339
340 // AArch64 uses external relocations as much as possible. For debug
341 // sections, and for pointer-sized relocations (.quad), we allow section
342 // relocations. It's code sections that run into trouble.
343 if (Base) {
Rafael Espindola26585542015-01-19 21:11:14 +0000344 RelSymbol = Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000345
346 // Add the local offset, if needed.
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000347 if (&Base->getData() != &SD)
348 Value += Layout.getSymbolOffset(&SD) -
349 Layout.getSymbolOffset(&Base->getData());
Tim Northover3b0846e2014-05-24 12:50:23 +0000350 } else if (Symbol->isInSection()) {
Rafael Espindola26585542015-01-19 21:11:14 +0000351 if (!CanUseLocalRelocation)
Tim Northover3b0846e2014-05-24 12:50:23 +0000352 Asm.getContext().FatalError(
353 Fixup.getLoc(),
354 "unsupported relocation of local symbol '" + Symbol->getName() +
355 "'. Must have non-local symbol earlier in section.");
356 // Adjust the relocation to be section-relative.
357 // The index is the section ordinal (1-based).
358 const MCSectionData &SymSD =
359 Asm.getSectionData(SD.getSymbol().getSection());
360 Index = SymSD.getOrdinal() + 1;
Tim Northover3b0846e2014-05-24 12:50:23 +0000361 Value += Writer->getSymbolAddress(&SD, Layout);
362
363 if (IsPCRel)
364 Value -= Writer->getFragmentAddress(Fragment, Layout) +
365 Fixup.getOffset() + (1ULL << Log2Size);
366 } else {
367 // Resolve constant variables.
368 if (SD.getSymbol().isVariable()) {
369 int64_t Res;
370 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
371 Res, Layout, Writer->getSectionAddressMap())) {
372 FixedValue = Res;
373 return;
374 }
375 }
376 Asm.getContext().FatalError(Fixup.getLoc(),
377 "unsupported relocation of variable '" +
378 Symbol->getName() + "'");
379 }
380 }
381
382 // If the relocation kind is Branch26, Page21, or Pageoff12, any addend
383 // is represented via an Addend relocation, not encoded directly into
384 // the instruction.
385 if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
386 Type == MachO::ARM64_RELOC_PAGE21 ||
387 Type == MachO::ARM64_RELOC_PAGEOFF12) &&
388 Value) {
389 assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
390
391 MachO::any_relocation_info MRE;
392 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000393 MRE.r_word1 =
394 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000395 Writer->addRelocation(RelSymbol ? &RelSymbol->getData() : nullptr,
396 Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000397
398 // Now set up the Addend relocation.
399 Type = MachO::ARM64_RELOC_ADDEND;
400 Index = Value;
Rafael Espindola26585542015-01-19 21:11:14 +0000401 RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000402 IsPCRel = 0;
403 Log2Size = 2;
Tim Northover3b0846e2014-05-24 12:50:23 +0000404
405 // Put zero into the instruction itself. The addend is in the relocation.
406 Value = 0;
407 }
408
409 // If there's any addend left to handle, encode it in the instruction.
410 FixedValue = Value;
411
412 // struct relocation_info (8 bytes)
413 MachO::any_relocation_info MRE;
414 MRE.r_word0 = FixupOffset;
Rafael Espindola26585542015-01-19 21:11:14 +0000415 MRE.r_word1 =
416 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000417 Writer->addRelocation(RelSymbol ? &RelSymbol->getData() : nullptr,
418 Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000419}
420
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000421MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS,
Rafael Espindola49286e92015-04-09 18:32:58 +0000422 uint32_t CPUType,
423 uint32_t CPUSubtype) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000424 return createMachObjectWriter(
425 new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
426 /*IsLittleEndian=*/true);
427}