blob: f6fab5d6b6fe965ac5af88bff299ef56dbd2c044 [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 Espindolad9c3e302015-01-12 18:13:07 +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 Espindolad9c3e302015-01-12 18:13:07 +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 Espindolad9c3e302015-01-12 18:13:07 +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 cstring.
127 if (!Symbol.isInSection())
128 return true;
129 const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
130 return RefSec.getType() != MachO::S_CSTRING_LITERALS;
131}
132
Tim Northover3b0846e2014-05-24 12:50:23 +0000133void AArch64MachObjectWriter::RecordRelocation(
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000134 MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
Tim Northover3b0846e2014-05-24 12:50:23 +0000135 const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
136 uint64_t &FixedValue) {
137 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
138
139 // See <reloc.h>.
140 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
141 unsigned Log2Size = 0;
142 int64_t Value = 0;
143 unsigned Index = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000144 unsigned Type = 0;
145 unsigned Kind = Fixup.getKind();
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000146 const MCSymbolData *RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000147
148 FixupOffset += Fixup.getOffset();
149
150 // AArch64 pcrel relocation addends do not include the section offset.
151 if (IsPCRel)
152 FixedValue += FixupOffset;
153
154 // ADRP fixups use relocations for the whole symbol value and only
155 // put the addend in the instruction itself. Clear out any value the
156 // generic code figured out from the sybmol definition.
157 if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21)
158 FixedValue = 0;
159
160 // imm19 relocations are for conditional branches, which require
161 // assembler local symbols. If we got here, that's not what we have,
162 // so complain loudly.
163 if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
164 Asm.getContext().FatalError(Fixup.getLoc(),
165 "conditional branch requires assembler-local"
166 " label. '" +
167 Target.getSymA()->getSymbol().getName() +
168 "' is external.");
169 return;
170 }
171
172 // 14-bit branch relocations should only target internal labels, and so
173 // should never get here.
174 if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
175 Asm.getContext().FatalError(Fixup.getLoc(),
176 "Invalid relocation on conditional branch!");
177 return;
178 }
179
180 if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
181 Asm)) {
182 Asm.getContext().FatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
183 return;
184 }
185
186 Value = Target.getConstant();
187
188 if (Target.isAbsolute()) { // constant
189 // FIXME: Should this always be extern?
190 // SymbolNum of 0 indicates the absolute section.
191 Type = MachO::ARM64_RELOC_UNSIGNED;
Tim Northover3b0846e2014-05-24 12:50:23 +0000192
193 if (IsPCRel) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000194 Asm.getContext().FatalError(Fixup.getLoc(),
195 "PC relative absolute relocation!");
196
197 // FIXME: x86_64 sets the type to a branch reloc here. Should we do
198 // something similar?
199 }
200 } else if (Target.getSymB()) { // A - B + constant
201 const MCSymbol *A = &Target.getSymA()->getSymbol();
202 const MCSymbolData &A_SD = Asm.getSymbolData(*A);
203 const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
204
205 const MCSymbol *B = &Target.getSymB()->getSymbol();
206 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
207 const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
208
209 // Check for "_foo@got - .", which comes through here as:
210 // Ltmp0:
211 // ... _foo@got - Ltmp0
212 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
213 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
214 Layout.getSymbolOffset(&B_SD) ==
215 Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
216 // SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
Tim Northover3b0846e2014-05-24 12:50:23 +0000217 Type = MachO::ARM64_RELOC_POINTER_TO_GOT;
218 IsPCRel = 1;
219 MachO::any_relocation_info MRE;
220 MRE.r_word0 = FixupOffset;
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000221 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
222 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000223 return;
224 } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
225 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
226 // Otherwise, neither symbol can be modified.
227 Asm.getContext().FatalError(Fixup.getLoc(),
228 "unsupported relocation of modified symbol");
229
230 // We don't support PCrel relocations of differences.
231 if (IsPCRel)
232 Asm.getContext().FatalError(Fixup.getLoc(),
233 "unsupported pc-relative relocation of "
234 "difference");
235
236 // AArch64 always uses external relocations. If there is no symbol to use as
237 // a base address (a local symbol with no preceding non-local symbol),
238 // error out.
239 //
240 // FIXME: We should probably just synthesize an external symbol and use
241 // that.
242 if (!A_Base)
243 Asm.getContext().FatalError(
244 Fixup.getLoc(),
245 "unsupported relocation of local symbol '" + A->getName() +
246 "'. Must have non-local symbol earlier in section.");
247 if (!B_Base)
248 Asm.getContext().FatalError(
249 Fixup.getLoc(),
250 "unsupported relocation of local symbol '" + B->getName() +
251 "'. Must have non-local symbol earlier in section.");
252
253 if (A_Base == B_Base && A_Base)
254 Asm.getContext().FatalError(Fixup.getLoc(),
255 "unsupported relocation with identical base");
256
257 Value += (!A_SD.getFragment() ? 0
258 : Writer->getSymbolAddress(&A_SD, Layout)) -
259 (!A_Base || !A_Base->getFragment()
260 ? 0
261 : Writer->getSymbolAddress(A_Base, Layout));
262 Value -= (!B_SD.getFragment() ? 0
263 : Writer->getSymbolAddress(&B_SD, Layout)) -
264 (!B_Base || !B_Base->getFragment()
265 ? 0
266 : Writer->getSymbolAddress(B_Base, Layout));
267
Tim Northover3b0846e2014-05-24 12:50:23 +0000268 Type = MachO::ARM64_RELOC_UNSIGNED;
269
270 MachO::any_relocation_info MRE;
271 MRE.r_word0 = FixupOffset;
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000272 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
273 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000274
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000275 RelSymbol = B_Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000276 Type = MachO::ARM64_RELOC_SUBTRACTOR;
277 } else { // A + constant
278 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Lang Hames04b37c42015-01-06 00:54:32 +0000279 const MCSectionMachO &Section = static_cast<const MCSectionMachO &>(
280 Fragment->getParent()->getSection());
Rafael Espindola54b435e2014-12-31 17:19:34 +0000281
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000282 bool CanUseLocalRelocation =
283 canUseLocalRelocation(Section, *Symbol, Log2Size);
284 if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) {
285 const MCSection &Sec = Symbol->getSection();
286 if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
287 Asm.addLocalUsedInReloc(*Symbol);
288 }
289
290 const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
291 const MCSymbolData *Base = Asm.getAtom(&SD);
292
Tim Northover3b0846e2014-05-24 12:50:23 +0000293 // If the symbol is a variable and we weren't able to get a Base for it
294 // (i.e., it's not in the symbol table associated with a section) resolve
295 // the relocation based its expansion instead.
296 if (Symbol->isVariable() && !Base) {
297 // If the evaluation is an absolute value, just use that directly
298 // to keep things easy.
299 int64_t Res;
300 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
301 Res, Layout, Writer->getSectionAddressMap())) {
302 FixedValue = Res;
303 return;
304 }
305
306 // FIXME: Will the Target we already have ever have any data in it
307 // we need to preserve and merge with the new Target? How about
308 // the FixedValue?
Joerg Sonnenberger752b91b2014-08-10 11:35:12 +0000309 if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
310 &Fixup))
Tim Northover3b0846e2014-05-24 12:50:23 +0000311 Asm.getContext().FatalError(Fixup.getLoc(),
312 "unable to resolve variable '" +
313 Symbol->getName() + "'");
314 return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
315 FixedValue);
316 }
317
318 // Relocations inside debug sections always use local relocations when
319 // possible. This seems to be done because the debugger doesn't fully
320 // understand relocation entries and expects to find values that
321 // have already been fixed up.
322 if (Symbol->isInSection()) {
323 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
324 Base = nullptr;
325 }
326
327 // AArch64 uses external relocations as much as possible. For debug
328 // sections, and for pointer-sized relocations (.quad), we allow section
329 // relocations. It's code sections that run into trouble.
330 if (Base) {
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000331 RelSymbol = Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000332
333 // Add the local offset, if needed.
334 if (Base != &SD)
335 Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
336 } else if (Symbol->isInSection()) {
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000337 if (!CanUseLocalRelocation)
Tim Northover3b0846e2014-05-24 12:50:23 +0000338 Asm.getContext().FatalError(
339 Fixup.getLoc(),
340 "unsupported relocation of local symbol '" + Symbol->getName() +
341 "'. Must have non-local symbol earlier in section.");
342 // Adjust the relocation to be section-relative.
343 // The index is the section ordinal (1-based).
344 const MCSectionData &SymSD =
345 Asm.getSectionData(SD.getSymbol().getSection());
346 Index = SymSD.getOrdinal() + 1;
Tim Northover3b0846e2014-05-24 12:50:23 +0000347 Value += Writer->getSymbolAddress(&SD, Layout);
348
349 if (IsPCRel)
350 Value -= Writer->getFragmentAddress(Fragment, Layout) +
351 Fixup.getOffset() + (1ULL << Log2Size);
352 } else {
353 // Resolve constant variables.
354 if (SD.getSymbol().isVariable()) {
355 int64_t Res;
356 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
357 Res, Layout, Writer->getSectionAddressMap())) {
358 FixedValue = Res;
359 return;
360 }
361 }
362 Asm.getContext().FatalError(Fixup.getLoc(),
363 "unsupported relocation of variable '" +
364 Symbol->getName() + "'");
365 }
366 }
367
368 // If the relocation kind is Branch26, Page21, or Pageoff12, any addend
369 // is represented via an Addend relocation, not encoded directly into
370 // the instruction.
371 if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
372 Type == MachO::ARM64_RELOC_PAGE21 ||
373 Type == MachO::ARM64_RELOC_PAGEOFF12) &&
374 Value) {
375 assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
376
377 MachO::any_relocation_info MRE;
378 MRE.r_word0 = FixupOffset;
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000379 MRE.r_word1 =
380 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
381 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000382
383 // Now set up the Addend relocation.
384 Type = MachO::ARM64_RELOC_ADDEND;
385 Index = Value;
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000386 RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000387 IsPCRel = 0;
388 Log2Size = 2;
Tim Northover3b0846e2014-05-24 12:50:23 +0000389
390 // Put zero into the instruction itself. The addend is in the relocation.
391 Value = 0;
392 }
393
394 // If there's any addend left to handle, encode it in the instruction.
395 FixedValue = Value;
396
397 // struct relocation_info (8 bytes)
398 MachO::any_relocation_info MRE;
399 MRE.r_word0 = FixupOffset;
Rafael Espindolad9c3e302015-01-12 18:13:07 +0000400 MRE.r_word1 =
401 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
402 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000403}
404
405MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_ostream &OS,
406 uint32_t CPUType,
407 uint32_t CPUSubtype) {
408 return createMachObjectWriter(
409 new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
410 /*IsLittleEndian=*/true);
411}