blob: 2ea3826e471af58cc7eb2e619513cc1d10b180c5 [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 Espindolab22d5aa2014-12-30 13:13:27 +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 Espindolab22d5aa2014-12-30 13:13:27 +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
116void AArch64MachObjectWriter::RecordRelocation(
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000117 MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
Tim Northover3b0846e2014-05-24 12:50:23 +0000118 const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
119 uint64_t &FixedValue) {
120 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
121
122 // See <reloc.h>.
123 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
124 unsigned Log2Size = 0;
125 int64_t Value = 0;
126 unsigned Index = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000127 unsigned Type = 0;
128 unsigned Kind = Fixup.getKind();
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000129 const MCSymbolData *RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000130
131 FixupOffset += Fixup.getOffset();
132
133 // AArch64 pcrel relocation addends do not include the section offset.
134 if (IsPCRel)
135 FixedValue += FixupOffset;
136
137 // ADRP fixups use relocations for the whole symbol value and only
138 // put the addend in the instruction itself. Clear out any value the
139 // generic code figured out from the sybmol definition.
140 if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21)
141 FixedValue = 0;
142
143 // imm19 relocations are for conditional branches, which require
144 // assembler local symbols. If we got here, that's not what we have,
145 // so complain loudly.
146 if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
147 Asm.getContext().FatalError(Fixup.getLoc(),
148 "conditional branch requires assembler-local"
149 " label. '" +
150 Target.getSymA()->getSymbol().getName() +
151 "' is external.");
152 return;
153 }
154
155 // 14-bit branch relocations should only target internal labels, and so
156 // should never get here.
157 if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
158 Asm.getContext().FatalError(Fixup.getLoc(),
159 "Invalid relocation on conditional branch!");
160 return;
161 }
162
163 if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
164 Asm)) {
165 Asm.getContext().FatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
166 return;
167 }
168
169 Value = Target.getConstant();
170
171 if (Target.isAbsolute()) { // constant
172 // FIXME: Should this always be extern?
173 // SymbolNum of 0 indicates the absolute section.
174 Type = MachO::ARM64_RELOC_UNSIGNED;
Tim Northover3b0846e2014-05-24 12:50:23 +0000175
176 if (IsPCRel) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000177 Asm.getContext().FatalError(Fixup.getLoc(),
178 "PC relative absolute relocation!");
179
180 // FIXME: x86_64 sets the type to a branch reloc here. Should we do
181 // something similar?
182 }
183 } else if (Target.getSymB()) { // A - B + constant
184 const MCSymbol *A = &Target.getSymA()->getSymbol();
185 const MCSymbolData &A_SD = Asm.getSymbolData(*A);
186 const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
187
188 const MCSymbol *B = &Target.getSymB()->getSymbol();
189 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
190 const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
191
192 // Check for "_foo@got - .", which comes through here as:
193 // Ltmp0:
194 // ... _foo@got - Ltmp0
195 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
196 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
197 Layout.getSymbolOffset(&B_SD) ==
198 Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
199 // SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
Tim Northover3b0846e2014-05-24 12:50:23 +0000200 Type = MachO::ARM64_RELOC_POINTER_TO_GOT;
201 IsPCRel = 1;
202 MachO::any_relocation_info MRE;
203 MRE.r_word0 = FixupOffset;
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000204 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
205 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000206 return;
207 } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
208 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
209 // Otherwise, neither symbol can be modified.
210 Asm.getContext().FatalError(Fixup.getLoc(),
211 "unsupported relocation of modified symbol");
212
213 // We don't support PCrel relocations of differences.
214 if (IsPCRel)
215 Asm.getContext().FatalError(Fixup.getLoc(),
216 "unsupported pc-relative relocation of "
217 "difference");
218
219 // AArch64 always uses external relocations. If there is no symbol to use as
220 // a base address (a local symbol with no preceding non-local symbol),
221 // error out.
222 //
223 // FIXME: We should probably just synthesize an external symbol and use
224 // that.
225 if (!A_Base)
226 Asm.getContext().FatalError(
227 Fixup.getLoc(),
228 "unsupported relocation of local symbol '" + A->getName() +
229 "'. Must have non-local symbol earlier in section.");
230 if (!B_Base)
231 Asm.getContext().FatalError(
232 Fixup.getLoc(),
233 "unsupported relocation of local symbol '" + B->getName() +
234 "'. Must have non-local symbol earlier in section.");
235
236 if (A_Base == B_Base && A_Base)
237 Asm.getContext().FatalError(Fixup.getLoc(),
238 "unsupported relocation with identical base");
239
240 Value += (!A_SD.getFragment() ? 0
241 : Writer->getSymbolAddress(&A_SD, Layout)) -
242 (!A_Base || !A_Base->getFragment()
243 ? 0
244 : Writer->getSymbolAddress(A_Base, Layout));
245 Value -= (!B_SD.getFragment() ? 0
246 : Writer->getSymbolAddress(&B_SD, Layout)) -
247 (!B_Base || !B_Base->getFragment()
248 ? 0
249 : Writer->getSymbolAddress(B_Base, Layout));
250
Tim Northover3b0846e2014-05-24 12:50:23 +0000251 Type = MachO::ARM64_RELOC_UNSIGNED;
252
253 MachO::any_relocation_info MRE;
254 MRE.r_word0 = FixupOffset;
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000255 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
256 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000257
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000258 RelSymbol = B_Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000259 Type = MachO::ARM64_RELOC_SUBTRACTOR;
260 } else { // A + constant
261 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000262 if (Symbol->isTemporary() && Value) {
263 const MCSection &Sec = Symbol->getSection();
264 if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
265 Asm.addLocalUsedInReloc(*Symbol);
266 }
267
Tim Northover3b0846e2014-05-24 12:50:23 +0000268 const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
269 const MCSymbolData *Base = Asm.getAtom(&SD);
270 const MCSectionMachO &Section = static_cast<const MCSectionMachO &>(
271 Fragment->getParent()->getSection());
272
273 // If the symbol is a variable and we weren't able to get a Base for it
274 // (i.e., it's not in the symbol table associated with a section) resolve
275 // the relocation based its expansion instead.
276 if (Symbol->isVariable() && !Base) {
277 // If the evaluation is an absolute value, just use that directly
278 // to keep things easy.
279 int64_t Res;
280 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
281 Res, Layout, Writer->getSectionAddressMap())) {
282 FixedValue = Res;
283 return;
284 }
285
286 // FIXME: Will the Target we already have ever have any data in it
287 // we need to preserve and merge with the new Target? How about
288 // the FixedValue?
Joerg Sonnenberger752b91b2014-08-10 11:35:12 +0000289 if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
290 &Fixup))
Tim Northover3b0846e2014-05-24 12:50:23 +0000291 Asm.getContext().FatalError(Fixup.getLoc(),
292 "unable to resolve variable '" +
293 Symbol->getName() + "'");
294 return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
295 FixedValue);
296 }
297
298 // Relocations inside debug sections always use local relocations when
299 // possible. This seems to be done because the debugger doesn't fully
300 // understand relocation entries and expects to find values that
301 // have already been fixed up.
302 if (Symbol->isInSection()) {
303 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
304 Base = nullptr;
305 }
306
307 // AArch64 uses external relocations as much as possible. For debug
308 // sections, and for pointer-sized relocations (.quad), we allow section
309 // relocations. It's code sections that run into trouble.
310 if (Base) {
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000311 RelSymbol = Base;
Tim Northover3b0846e2014-05-24 12:50:23 +0000312
313 // Add the local offset, if needed.
314 if (Base != &SD)
315 Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
316 } else if (Symbol->isInSection()) {
317 // Pointer-sized relocations can use a local relocation. Otherwise,
318 // we have to be in a debug info section.
319 if (!Section.hasAttribute(MachO::S_ATTR_DEBUG) && Log2Size != 3)
320 Asm.getContext().FatalError(
321 Fixup.getLoc(),
322 "unsupported relocation of local symbol '" + Symbol->getName() +
323 "'. Must have non-local symbol earlier in section.");
324 // Adjust the relocation to be section-relative.
325 // The index is the section ordinal (1-based).
326 const MCSectionData &SymSD =
327 Asm.getSectionData(SD.getSymbol().getSection());
328 Index = SymSD.getOrdinal() + 1;
Tim Northover3b0846e2014-05-24 12:50:23 +0000329 Value += Writer->getSymbolAddress(&SD, Layout);
330
331 if (IsPCRel)
332 Value -= Writer->getFragmentAddress(Fragment, Layout) +
333 Fixup.getOffset() + (1ULL << Log2Size);
334 } else {
335 // Resolve constant variables.
336 if (SD.getSymbol().isVariable()) {
337 int64_t Res;
338 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
339 Res, Layout, Writer->getSectionAddressMap())) {
340 FixedValue = Res;
341 return;
342 }
343 }
344 Asm.getContext().FatalError(Fixup.getLoc(),
345 "unsupported relocation of variable '" +
346 Symbol->getName() + "'");
347 }
348 }
349
350 // If the relocation kind is Branch26, Page21, or Pageoff12, any addend
351 // is represented via an Addend relocation, not encoded directly into
352 // the instruction.
353 if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
354 Type == MachO::ARM64_RELOC_PAGE21 ||
355 Type == MachO::ARM64_RELOC_PAGEOFF12) &&
356 Value) {
357 assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
358
359 MachO::any_relocation_info MRE;
360 MRE.r_word0 = FixupOffset;
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000361 MRE.r_word1 =
362 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
363 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000364
365 // Now set up the Addend relocation.
366 Type = MachO::ARM64_RELOC_ADDEND;
367 Index = Value;
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000368 RelSymbol = nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000369 IsPCRel = 0;
370 Log2Size = 2;
Tim Northover3b0846e2014-05-24 12:50:23 +0000371
372 // Put zero into the instruction itself. The addend is in the relocation.
373 Value = 0;
374 }
375
376 // If there's any addend left to handle, encode it in the instruction.
377 FixedValue = Value;
378
379 // struct relocation_info (8 bytes)
380 MachO::any_relocation_info MRE;
381 MRE.r_word0 = FixupOffset;
Rafael Espindolab22d5aa2014-12-30 13:13:27 +0000382 MRE.r_word1 =
383 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
384 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000385}
386
387MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_ostream &OS,
388 uint32_t CPUType,
389 uint32_t CPUSubtype) {
390 return createMachObjectWriter(
391 new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
392 /*IsLittleEndian=*/true);
393}