1 //===-- AArch64MachObjectWriter.cpp - ARM Mach Object Writer --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/AArch64FixupKinds.h"
11 #include "MCTargetDesc/AArch64MCTargetDesc.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #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"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MachO.h"
27 class AArch64MachObjectWriter : public MCMachObjectTargetWriter {
28 bool getAArch64FixupKindMachOInfo(const MCFixup &Fixup, unsigned &RelocType,
29 const MCSymbolRefExpr *Sym,
30 unsigned &Log2Size, const MCAssembler &Asm);
33 AArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype)
34 : MCMachObjectTargetWriter(true /* is64Bit */, CPUType, CPUSubtype,
35 /*UseAggressiveSymbolFolding=*/true) {}
37 void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
38 const MCAsmLayout &Layout, const MCFragment *Fragment,
39 const MCFixup &Fixup, MCValue Target,
40 uint64_t &FixedValue) override;
44 bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo(
45 const MCFixup &Fixup, unsigned &RelocType, const MCSymbolRefExpr *Sym,
46 unsigned &Log2Size, const MCAssembler &Asm) {
47 RelocType = unsigned(MachO::ARM64_RELOC_UNSIGNED);
50 switch ((unsigned)Fixup.getKind()) {
55 Log2Size = llvm::Log2_32(1);
58 Log2Size = llvm::Log2_32(2);
61 Log2Size = llvm::Log2_32(4);
62 if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
63 RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
66 Log2Size = llvm::Log2_32(8);
67 if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
68 RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
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()) {
79 llvm_unreachable("Unexpected symbol reference variant kind!");
80 case MCSymbolRefExpr::VK_PAGEOFF:
81 RelocType = unsigned(MachO::ARM64_RELOC_PAGEOFF12);
83 case MCSymbolRefExpr::VK_GOTPAGEOFF:
84 RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12);
86 case MCSymbolRefExpr::VK_TLVPPAGEOFF:
87 RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12);
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()) {
95 Asm.getContext().reportFatalError(Fixup.getLoc(),
96 "ADR/ADRP relocations must be GOT relative");
97 case MCSymbolRefExpr::VK_PAGE:
98 RelocType = unsigned(MachO::ARM64_RELOC_PAGE21);
100 case MCSymbolRefExpr::VK_GOTPAGE:
101 RelocType = unsigned(MachO::ARM64_RELOC_GOT_LOAD_PAGE21);
103 case MCSymbolRefExpr::VK_TLVPPAGE:
104 RelocType = unsigned(MachO::ARM64_RELOC_TLVP_LOAD_PAGE21);
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);
116 static 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))
122 // Otherwise, only pointer sized relocations are supported.
126 // But only if they don't point to a few forbidden sections.
127 if (!Symbol.isInSection())
129 const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
130 if (RefSec.getType() == MachO::S_CSTRING_LITERALS)
133 if (RefSec.getSegmentName() == "__DATA" &&
134 RefSec.getSectionName() == "__objc_classrefs")
137 // FIXME: ld64 currently handles internal pointer-sized relocations
138 // incorrectly (applying the addend twice). We should be able to return true
139 // unconditionally by this point when that's fixed.
143 void AArch64MachObjectWriter::RecordRelocation(
144 MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
145 const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
146 uint64_t &FixedValue) {
147 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
150 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
151 unsigned Log2Size = 0;
155 unsigned Kind = Fixup.getKind();
156 const MCSymbol *RelSymbol = nullptr;
158 FixupOffset += Fixup.getOffset();
160 // AArch64 pcrel relocation addends do not include the section offset.
162 FixedValue += FixupOffset;
164 // ADRP fixups use relocations for the whole symbol value and only
165 // put the addend in the instruction itself. Clear out any value the
166 // generic code figured out from the sybmol definition.
167 if (Kind == AArch64::fixup_aarch64_pcrel_adrp_imm21)
170 // imm19 relocations are for conditional branches, which require
171 // assembler local symbols. If we got here, that's not what we have,
172 // so complain loudly.
173 if (Kind == AArch64::fixup_aarch64_pcrel_branch19) {
174 Asm.getContext().reportFatalError(Fixup.getLoc(),
175 "conditional branch requires assembler-local"
177 Target.getSymA()->getSymbol().getName() +
182 // 14-bit branch relocations should only target internal labels, and so
183 // should never get here.
184 if (Kind == AArch64::fixup_aarch64_pcrel_branch14) {
185 Asm.getContext().reportFatalError(Fixup.getLoc(),
186 "Invalid relocation on conditional branch!");
190 if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
192 Asm.getContext().reportFatalError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
196 Value = Target.getConstant();
198 if (Target.isAbsolute()) { // constant
199 // FIXME: Should this always be extern?
200 // SymbolNum of 0 indicates the absolute section.
201 Type = MachO::ARM64_RELOC_UNSIGNED;
204 Asm.getContext().reportFatalError(Fixup.getLoc(),
205 "PC relative absolute relocation!");
207 // FIXME: x86_64 sets the type to a branch reloc here. Should we do
208 // something similar?
210 } else if (Target.getSymB()) { // A - B + constant
211 const MCSymbol *A = &Target.getSymA()->getSymbol();
212 const MCSymbolData &A_SD = Asm.getSymbolData(*A);
213 const MCSymbol *A_Base = Asm.getAtom(&A_SD);
215 const MCSymbol *B = &Target.getSymB()->getSymbol();
216 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
217 const MCSymbol *B_Base = Asm.getAtom(&B_SD);
219 // Check for "_foo@got - .", which comes through here as:
221 // ... _foo@got - Ltmp0
222 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
223 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
224 Layout.getSymbolOffset(*B) ==
225 Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
226 // SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
227 Type = MachO::ARM64_RELOC_POINTER_TO_GOT;
229 MachO::any_relocation_info MRE;
230 MRE.r_word0 = FixupOffset;
231 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
232 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
234 } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
235 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
236 // Otherwise, neither symbol can be modified.
237 Asm.getContext().reportFatalError(Fixup.getLoc(),
238 "unsupported relocation of modified symbol");
240 // We don't support PCrel relocations of differences.
242 Asm.getContext().reportFatalError(Fixup.getLoc(),
243 "unsupported pc-relative relocation of "
246 // AArch64 always uses external relocations. If there is no symbol to use as
247 // a base address (a local symbol with no preceding non-local symbol),
250 // FIXME: We should probably just synthesize an external symbol and use
253 Asm.getContext().reportFatalError(
255 "unsupported relocation of local symbol '" + A->getName() +
256 "'. Must have non-local symbol earlier in section.");
258 Asm.getContext().reportFatalError(
260 "unsupported relocation of local symbol '" + B->getName() +
261 "'. Must have non-local symbol earlier in section.");
263 if (A_Base == B_Base && A_Base)
264 Asm.getContext().reportFatalError(Fixup.getLoc(),
265 "unsupported relocation with identical base");
267 Value += (!A_SD.getFragment() ? 0 : Writer->getSymbolAddress(*A, Layout)) -
268 (!A_Base || !A_Base->getData().getFragment()
270 : Writer->getSymbolAddress(*A_Base, Layout));
271 Value -= (!B_SD.getFragment() ? 0 : Writer->getSymbolAddress(*B, Layout)) -
272 (!B_Base || !B_Base->getData().getFragment()
274 : Writer->getSymbolAddress(*B_Base, Layout));
276 Type = MachO::ARM64_RELOC_UNSIGNED;
278 MachO::any_relocation_info MRE;
279 MRE.r_word0 = FixupOffset;
280 MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
281 Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
284 Type = MachO::ARM64_RELOC_SUBTRACTOR;
285 } else { // A + constant
286 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
287 const MCSectionMachO &Section = static_cast<const MCSectionMachO &>(
288 Fragment->getParent()->getSection());
290 bool CanUseLocalRelocation =
291 canUseLocalRelocation(Section, *Symbol, Log2Size);
292 if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) {
293 const MCSection &Sec = Symbol->getSection();
294 if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
295 Asm.addLocalUsedInReloc(*Symbol);
298 const MCSymbolData &SD = Asm.getSymbolData(*Symbol);
299 const MCSymbol *Base = Asm.getAtom(&SD);
301 // If the symbol is a variable and we weren't able to get a Base for it
302 // (i.e., it's not in the symbol table associated with a section) resolve
303 // the relocation based its expansion instead.
304 if (Symbol->isVariable() && !Base) {
305 // If the evaluation is an absolute value, just use that directly
306 // to keep things easy.
308 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
309 Res, Layout, Writer->getSectionAddressMap())) {
314 // FIXME: Will the Target we already have ever have any data in it
315 // we need to preserve and merge with the new Target? How about
317 if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
319 Asm.getContext().reportFatalError(Fixup.getLoc(),
320 "unable to resolve variable '" +
321 Symbol->getName() + "'");
322 return RecordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
326 // Relocations inside debug sections always use local relocations when
327 // possible. This seems to be done because the debugger doesn't fully
328 // understand relocation entries and expects to find values that
329 // have already been fixed up.
330 if (Symbol->isInSection()) {
331 if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
335 // AArch64 uses external relocations as much as possible. For debug
336 // sections, and for pointer-sized relocations (.quad), we allow section
337 // relocations. It's code sections that run into trouble.
341 // Add the local offset, if needed.
344 Layout.getSymbolOffset(*Symbol) - Layout.getSymbolOffset(*Base);
345 } else if (Symbol->isInSection()) {
346 if (!CanUseLocalRelocation)
347 Asm.getContext().reportFatalError(
349 "unsupported relocation of local symbol '" + Symbol->getName() +
350 "'. Must have non-local symbol earlier in section.");
351 // Adjust the relocation to be section-relative.
352 // The index is the section ordinal (1-based).
353 const MCSectionData &SymSD =
354 Asm.getSectionData(SD.getSymbol().getSection());
355 Index = SymSD.getOrdinal() + 1;
356 Value += Writer->getSymbolAddress(SD.getSymbol(), Layout);
359 Value -= Writer->getFragmentAddress(Fragment, Layout) +
360 Fixup.getOffset() + (1ULL << Log2Size);
362 // Resolve constant variables.
363 if (SD.getSymbol().isVariable()) {
365 if (SD.getSymbol().getVariableValue()->EvaluateAsAbsolute(
366 Res, Layout, Writer->getSectionAddressMap())) {
371 Asm.getContext().reportFatalError(Fixup.getLoc(),
372 "unsupported relocation of variable '" +
373 Symbol->getName() + "'");
377 // If the relocation kind is Branch26, Page21, or Pageoff12, any addend
378 // is represented via an Addend relocation, not encoded directly into
380 if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
381 Type == MachO::ARM64_RELOC_PAGE21 ||
382 Type == MachO::ARM64_RELOC_PAGEOFF12) &&
384 assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
386 MachO::any_relocation_info MRE;
387 MRE.r_word0 = FixupOffset;
389 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
390 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
392 // Now set up the Addend relocation.
393 Type = MachO::ARM64_RELOC_ADDEND;
399 // Put zero into the instruction itself. The addend is in the relocation.
403 // If there's any addend left to handle, encode it in the instruction.
406 // struct relocation_info (8 bytes)
407 MachO::any_relocation_info MRE;
408 MRE.r_word0 = FixupOffset;
410 (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
411 Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
414 MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS,
416 uint32_t CPUSubtype) {
417 return createMachObjectWriter(
418 new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
419 /*IsLittleEndian=*/true);