LLVM  5.0.0
AArch64MachObjectWriter.cpp
Go to the documentation of this file.
1 //===-- 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 
12 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCFragment.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCSectionMachO.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCValue.h"
26 #include "llvm/Support/Casting.h"
28 #include <cassert>
29 #include <cstdint>
30 
31 using namespace llvm;
32 
33 namespace {
34 
35 class AArch64MachObjectWriter : public MCMachObjectTargetWriter {
36  bool getAArch64FixupKindMachOInfo(const MCFixup &Fixup, unsigned &RelocType,
37  const MCSymbolRefExpr *Sym,
38  unsigned &Log2Size, const MCAssembler &Asm);
39 
40 public:
41  AArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype)
42  : MCMachObjectTargetWriter(true /* is64Bit */, CPUType, CPUSubtype) {}
43 
44  void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
45  const MCAsmLayout &Layout, const MCFragment *Fragment,
46  const MCFixup &Fixup, MCValue Target,
47  uint64_t &FixedValue) override;
48 };
49 
50 } // end anonymous namespace
51 
52 bool AArch64MachObjectWriter::getAArch64FixupKindMachOInfo(
53  const MCFixup &Fixup, unsigned &RelocType, const MCSymbolRefExpr *Sym,
54  unsigned &Log2Size, const MCAssembler &Asm) {
56  Log2Size = ~0U;
57 
58  switch ((unsigned)Fixup.getKind()) {
59  default:
60  return false;
61 
62  case FK_Data_1:
63  Log2Size = Log2_32(1);
64  return true;
65  case FK_Data_2:
66  Log2Size = Log2_32(2);
67  return true;
68  case FK_Data_4:
69  Log2Size = Log2_32(4);
70  if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
71  RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
72  return true;
73  case FK_Data_8:
74  Log2Size = Log2_32(8);
75  if (Sym->getKind() == MCSymbolRefExpr::VK_GOT)
76  RelocType = unsigned(MachO::ARM64_RELOC_POINTER_TO_GOT);
77  return true;
84  Log2Size = Log2_32(4);
85  switch (Sym->getKind()) {
86  default:
87  return false;
90  return true;
93  return true;
96  return true;
97  }
99  Log2Size = Log2_32(4);
100  // This encompasses the relocation for the whole 21-bit value.
101  switch (Sym->getKind()) {
102  default:
103  Asm.getContext().reportError(Fixup.getLoc(),
104  "ADR/ADRP relocations must be GOT relative");
105  return false;
107  RelocType = unsigned(MachO::ARM64_RELOC_PAGE21);
108  return true;
111  return true;
114  return true;
115  }
116  return true;
119  Log2Size = Log2_32(4);
121  return true;
122  }
123 }
124 
126  const MCSymbol &Symbol, unsigned Log2Size) {
127  // Debug info sections can use local relocations.
128  if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
129  return true;
130 
131  // Otherwise, only pointer sized relocations are supported.
132  if (Log2Size != 3)
133  return false;
134 
135  // But only if they don't point to a few forbidden sections.
136  if (!Symbol.isInSection())
137  return true;
138  const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
139  if (RefSec.getType() == MachO::S_CSTRING_LITERALS)
140  return false;
141 
142  if (RefSec.getSegmentName() == "__DATA" &&
143  RefSec.getSectionName() == "__objc_classrefs")
144  return false;
145 
146  // FIXME: ld64 currently handles internal pointer-sized relocations
147  // incorrectly (applying the addend twice). We should be able to return true
148  // unconditionally by this point when that's fixed.
149  return false;
150 }
151 
152 void AArch64MachObjectWriter::recordRelocation(
153  MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout,
154  const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target,
155  uint64_t &FixedValue) {
156  unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
157 
158  // See <reloc.h>.
159  uint32_t FixupOffset = Layout.getFragmentOffset(Fragment);
160  unsigned Log2Size = 0;
161  int64_t Value = 0;
162  unsigned Index = 0;
163  unsigned Type = 0;
164  unsigned Kind = Fixup.getKind();
165  const MCSymbol *RelSymbol = nullptr;
166 
167  FixupOffset += Fixup.getOffset();
168 
169  // AArch64 pcrel relocation addends do not include the section offset.
170  if (IsPCRel)
171  FixedValue += FixupOffset;
172 
173  // ADRP fixups use relocations for the whole symbol value and only
174  // put the addend in the instruction itself. Clear out any value the
175  // generic code figured out from the sybmol definition.
177  FixedValue = 0;
178 
179  // imm19 relocations are for conditional branches, which require
180  // assembler local symbols. If we got here, that's not what we have,
181  // so complain loudly.
183  Asm.getContext().reportError(Fixup.getLoc(),
184  "conditional branch requires assembler-local"
185  " label. '" +
186  Target.getSymA()->getSymbol().getName() +
187  "' is external.");
188  return;
189  }
190 
191  // 14-bit branch relocations should only target internal labels, and so
192  // should never get here.
194  Asm.getContext().reportError(Fixup.getLoc(),
195  "Invalid relocation on conditional branch!");
196  return;
197  }
198 
199  if (!getAArch64FixupKindMachOInfo(Fixup, Type, Target.getSymA(), Log2Size,
200  Asm)) {
201  Asm.getContext().reportError(Fixup.getLoc(), "unknown AArch64 fixup kind!");
202  return;
203  }
204 
205  Value = Target.getConstant();
206 
207  if (Target.isAbsolute()) { // constant
208  // FIXME: Should this always be extern?
209  // SymbolNum of 0 indicates the absolute section.
211 
212  if (IsPCRel) {
213  Asm.getContext().reportError(Fixup.getLoc(),
214  "PC relative absolute relocation!");
215  return;
216 
217  // FIXME: x86_64 sets the type to a branch reloc here. Should we do
218  // something similar?
219  }
220  } else if (Target.getSymB()) { // A - B + constant
221  const MCSymbol *A = &Target.getSymA()->getSymbol();
222  const MCSymbol *A_Base = Asm.getAtom(*A);
223 
224  const MCSymbol *B = &Target.getSymB()->getSymbol();
225  const MCSymbol *B_Base = Asm.getAtom(*B);
226 
227  // Check for "_foo@got - .", which comes through here as:
228  // Ltmp0:
229  // ... _foo@got - Ltmp0
230  if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOT &&
231  Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None &&
232  Layout.getSymbolOffset(*B) ==
233  Layout.getFragmentOffset(Fragment) + Fixup.getOffset()) {
234  // SymB is the PC, so use a PC-rel pointer-to-GOT relocation.
236  IsPCRel = 1;
238  MRE.r_word0 = FixupOffset;
239  MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
240  Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
241  return;
242  } else if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
243  Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) {
244  // Otherwise, neither symbol can be modified.
245  Asm.getContext().reportError(Fixup.getLoc(),
246  "unsupported relocation of modified symbol");
247  return;
248  }
249 
250  // We don't support PCrel relocations of differences.
251  if (IsPCRel) {
252  Asm.getContext().reportError(Fixup.getLoc(),
253  "unsupported pc-relative relocation of "
254  "difference");
255  return;
256  }
257 
258  // AArch64 always uses external relocations. If there is no symbol to use as
259  // a base address (a local symbol with no preceding non-local symbol),
260  // error out.
261  //
262  // FIXME: We should probably just synthesize an external symbol and use
263  // that.
264  if (!A_Base) {
265  Asm.getContext().reportError(
266  Fixup.getLoc(),
267  "unsupported relocation of local symbol '" + A->getName() +
268  "'. Must have non-local symbol earlier in section.");
269  return;
270  }
271  if (!B_Base) {
272  Asm.getContext().reportError(
273  Fixup.getLoc(),
274  "unsupported relocation of local symbol '" + B->getName() +
275  "'. Must have non-local symbol earlier in section.");
276  return;
277  }
278 
279  if (A_Base == B_Base && A_Base) {
280  Asm.getContext().reportError(
281  Fixup.getLoc(), "unsupported relocation with identical base");
282  return;
283  }
284 
285  Value += (!A->getFragment() ? 0 : Writer->getSymbolAddress(*A, Layout)) -
286  (!A_Base || !A_Base->getFragment() ? 0 : Writer->getSymbolAddress(
287  *A_Base, Layout));
288  Value -= (!B->getFragment() ? 0 : Writer->getSymbolAddress(*B, Layout)) -
289  (!B_Base || !B_Base->getFragment() ? 0 : Writer->getSymbolAddress(
290  *B_Base, Layout));
291 
293 
295  MRE.r_word0 = FixupOffset;
296  MRE.r_word1 = (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
297  Writer->addRelocation(A_Base, Fragment->getParent(), MRE);
298 
299  RelSymbol = B_Base;
301  } else { // A + constant
302  const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
303  const MCSectionMachO &Section =
304  static_cast<const MCSectionMachO &>(*Fragment->getParent());
305 
306  bool CanUseLocalRelocation =
307  canUseLocalRelocation(Section, *Symbol, Log2Size);
308  if (Symbol->isTemporary() && (Value || !CanUseLocalRelocation)) {
309  const MCSection &Sec = Symbol->getSection();
311  Symbol->setUsedInReloc();
312  }
313 
314  const MCSymbol *Base = Asm.getAtom(*Symbol);
315 
316  // If the symbol is a variable and we weren't able to get a Base for it
317  // (i.e., it's not in the symbol table associated with a section) resolve
318  // the relocation based its expansion instead.
319  if (Symbol->isVariable() && !Base) {
320  // If the evaluation is an absolute value, just use that directly
321  // to keep things easy.
322  int64_t Res;
323  if (Symbol->getVariableValue()->evaluateAsAbsolute(
324  Res, Layout, Writer->getSectionAddressMap())) {
325  FixedValue = Res;
326  return;
327  }
328 
329  // FIXME: Will the Target we already have ever have any data in it
330  // we need to preserve and merge with the new Target? How about
331  // the FixedValue?
332  if (!Symbol->getVariableValue()->evaluateAsRelocatable(Target, &Layout,
333  &Fixup)) {
334  Asm.getContext().reportError(Fixup.getLoc(),
335  "unable to resolve variable '" +
336  Symbol->getName() + "'");
337  return;
338  }
339  return recordRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
340  FixedValue);
341  }
342 
343  // Relocations inside debug sections always use local relocations when
344  // possible. This seems to be done because the debugger doesn't fully
345  // understand relocation entries and expects to find values that
346  // have already been fixed up.
347  if (Symbol->isInSection()) {
348  if (Section.hasAttribute(MachO::S_ATTR_DEBUG))
349  Base = nullptr;
350  }
351 
352  // AArch64 uses external relocations as much as possible. For debug
353  // sections, and for pointer-sized relocations (.quad), we allow section
354  // relocations. It's code sections that run into trouble.
355  if (Base) {
356  RelSymbol = Base;
357 
358  // Add the local offset, if needed.
359  if (Base != Symbol)
360  Value +=
361  Layout.getSymbolOffset(*Symbol) - Layout.getSymbolOffset(*Base);
362  } else if (Symbol->isInSection()) {
363  if (!CanUseLocalRelocation) {
364  Asm.getContext().reportError(
365  Fixup.getLoc(),
366  "unsupported relocation of local symbol '" + Symbol->getName() +
367  "'. Must have non-local symbol earlier in section.");
368  return;
369  }
370  // Adjust the relocation to be section-relative.
371  // The index is the section ordinal (1-based).
372  const MCSection &Sec = Symbol->getSection();
373  Index = Sec.getOrdinal() + 1;
374  Value += Writer->getSymbolAddress(*Symbol, Layout);
375 
376  if (IsPCRel)
377  Value -= Writer->getFragmentAddress(Fragment, Layout) +
378  Fixup.getOffset() + (1ULL << Log2Size);
379  } else {
380  // Resolve constant variables.
381  if (Symbol->isVariable()) {
382  int64_t Res;
383  if (Symbol->getVariableValue()->evaluateAsAbsolute(
384  Res, Layout, Writer->getSectionAddressMap())) {
385  FixedValue = Res;
386  return;
387  }
388  }
389  Asm.getContext().reportError(Fixup.getLoc(),
390  "unsupported relocation of variable '" +
391  Symbol->getName() + "'");
392  return;
393  }
394  }
395 
396  // If the relocation kind is Branch26, Page21, or Pageoff12, any addend
397  // is represented via an Addend relocation, not encoded directly into
398  // the instruction.
399  if ((Type == MachO::ARM64_RELOC_BRANCH26 ||
400  Type == MachO::ARM64_RELOC_PAGE21 ||
401  Type == MachO::ARM64_RELOC_PAGEOFF12) &&
402  Value) {
403  assert((Value & 0xff000000) == 0 && "Added relocation out of range!");
404 
406  MRE.r_word0 = FixupOffset;
407  MRE.r_word1 =
408  (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
409  Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
410 
411  // Now set up the Addend relocation.
413  Index = Value;
414  RelSymbol = nullptr;
415  IsPCRel = 0;
416  Log2Size = 2;
417 
418  // Put zero into the instruction itself. The addend is in the relocation.
419  Value = 0;
420  }
421 
422  // If there's any addend left to handle, encode it in the instruction.
423  FixedValue = Value;
424 
425  // struct relocation_info (8 bytes)
427  MRE.r_word0 = FixupOffset;
428  MRE.r_word1 =
429  (Index << 0) | (IsPCRel << 24) | (Log2Size << 25) | (Type << 28);
430  Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
431 }
432 
435  uint32_t CPUSubtype) {
436  return createMachObjectWriter(
437  new AArch64MachObjectWriter(CPUType, CPUSubtype), OS,
438  /*IsLittleEndian=*/true);
439 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:283
This represents a section on a Mach-O system (used by Mac OS X).
const MCSymbol & getSymbol() const
Definition: MCExpr.h:318
SMLoc getLoc() const
Definition: MCFixup.h:112
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
static bool canUseLocalRelocation(const MCSectionMachO &Section, const MCSymbol &Symbol, unsigned Log2Size)
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
Definition: MCFragment.cpp:129
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:63
MCContext & getContext() const
Definition: MCAssembler.h:257
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
A four-byte fixup.
Definition: MCFixup.h:26
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
StringRef getSectionName() const
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
const MCSymbol * getAtom(const MCSymbol &S) const
Find the symbol which defines the atom containing the given symbol, or null if there is no such symbo...
uint32_t getOffset() const
Definition: MCFixup.h:95
SectionAddrMap & getSectionAddressMap()
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool hasAttribute(unsigned Value) const
void setUsedInReloc() const
Definition: MCSymbol.h:213
#define A
Definition: LargeTest.cpp:12
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:608
unsigned getOrdinal() const
Definition: MCSection.h:120
MCObjectWriter * createAArch64MachObjectWriter(raw_pwrite_stream &OS, uint32_t CPUType, uint32_t CPUSubtype)
bool isInSection(bool SetUsed=true) const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:255
S_CSTRING_LITERALS - Section with literal C strings.
Definition: MachO.h:124
MCFixupKind getKind() const
Definition: MCFixup.h:93
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
A one-byte fixup.
Definition: MCFixup.h:24
S_ATTR_DEBUG - A debug section.
Definition: MachO.h:197
#define B
Definition: LargeTest.cpp:24
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
MachO::SectionType getType() const
uint64_t getFragmentAddress(const MCFragment *Fragment, const MCAsmLayout &Layout) const
MCSection * getParent() const
Definition: MCFragment.h:102
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:531
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:594
Target - Wrapper for Target specific information.
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:300
MCObjectWriter * createMachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:220
StringRef getSegmentName() const
MCSection & getSection(bool SetUsed=true) const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
Basic Alias true
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
A eight-byte fixup.
Definition: MCFixup.h:27
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:77
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:295
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:383
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const unsigned Kind
VariantKind getKind() const
Definition: MCExpr.h:320
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int64_t getConstant() const
Definition: MCValue.h:46
LLVM Value Representation.
Definition: Value.h:73
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const
A two-byte fixup.
Definition: MCFixup.h:25