AArch64/ARM64: remove AArch64 from tree prior to renaming ARM64.
[oota-llvm.git] / lib / Target / ARM64 / MCTargetDesc / ARM64AsmBackend.cpp
1 //===-- ARM64AsmBackend.cpp - ARM64 Assembler Backend ---------------------===//
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 "ARM64.h"
11 #include "ARM64RegisterInfo.h"
12 #include "MCTargetDesc/ARM64FixupKinds.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MachO.h"
22 using namespace llvm;
23
24 namespace {
25
26 class ARM64AsmBackend : public MCAsmBackend {
27   static const unsigned PCRelFlagVal =
28       MCFixupKindInfo::FKF_IsAlignedDownTo32Bits | MCFixupKindInfo::FKF_IsPCRel;
29
30 public:
31   ARM64AsmBackend(const Target &T) : MCAsmBackend() {}
32
33   unsigned getNumFixupKinds() const override {
34     return ARM64::NumTargetFixupKinds;
35   }
36
37   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
38     const static MCFixupKindInfo Infos[ARM64::NumTargetFixupKinds] = {
39       // This table *must* be in the order that the fixup_* kinds are defined in
40       // ARM64FixupKinds.h.
41       //
42       // Name                           Offset (bits) Size (bits)     Flags
43       { "fixup_arm64_pcrel_adr_imm21", 0, 32, PCRelFlagVal },
44       { "fixup_arm64_pcrel_adrp_imm21", 0, 32, PCRelFlagVal },
45       { "fixup_arm64_add_imm12", 10, 12, 0 },
46       { "fixup_arm64_ldst_imm12_scale1", 10, 12, 0 },
47       { "fixup_arm64_ldst_imm12_scale2", 10, 12, 0 },
48       { "fixup_arm64_ldst_imm12_scale4", 10, 12, 0 },
49       { "fixup_arm64_ldst_imm12_scale8", 10, 12, 0 },
50       { "fixup_arm64_ldst_imm12_scale16", 10, 12, 0 },
51       { "fixup_arm64_ldr_pcrel_imm19", 5, 19, PCRelFlagVal },
52       { "fixup_arm64_movw", 5, 16, 0 },
53       { "fixup_arm64_pcrel_branch14", 5, 14, PCRelFlagVal },
54       { "fixup_arm64_pcrel_branch19", 5, 19, PCRelFlagVal },
55       { "fixup_arm64_pcrel_branch26", 0, 26, PCRelFlagVal },
56       { "fixup_arm64_pcrel_call26", 0, 26, PCRelFlagVal },
57       { "fixup_arm64_tlsdesc_call", 0, 0, 0 }
58     };
59
60     if (Kind < FirstTargetFixupKind)
61       return MCAsmBackend::getFixupKindInfo(Kind);
62
63     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
64            "Invalid kind!");
65     return Infos[Kind - FirstTargetFixupKind];
66   }
67
68   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
69                   uint64_t Value, bool IsPCRel) const override;
70
71   bool mayNeedRelaxation(const MCInst &Inst) const override;
72   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
73                             const MCRelaxableFragment *DF,
74                             const MCAsmLayout &Layout) const override;
75   void relaxInstruction(const MCInst &Inst, MCInst &Res) const override;
76   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
77
78   void HandleAssemblerFlag(MCAssemblerFlag Flag) {}
79
80   unsigned getPointerSize() const { return 8; }
81 };
82
83 } // end anonymous namespace
84
85 /// \brief The number of bytes the fixup may change.
86 static unsigned getFixupKindNumBytes(unsigned Kind) {
87   switch (Kind) {
88   default:
89     assert(0 && "Unknown fixup kind!");
90
91   case ARM64::fixup_arm64_tlsdesc_call:
92     return 0;
93
94   case FK_Data_1:
95     return 1;
96
97   case FK_Data_2:
98   case ARM64::fixup_arm64_movw:
99     return 2;
100
101   case ARM64::fixup_arm64_pcrel_branch14:
102   case ARM64::fixup_arm64_add_imm12:
103   case ARM64::fixup_arm64_ldst_imm12_scale1:
104   case ARM64::fixup_arm64_ldst_imm12_scale2:
105   case ARM64::fixup_arm64_ldst_imm12_scale4:
106   case ARM64::fixup_arm64_ldst_imm12_scale8:
107   case ARM64::fixup_arm64_ldst_imm12_scale16:
108   case ARM64::fixup_arm64_ldr_pcrel_imm19:
109   case ARM64::fixup_arm64_pcrel_branch19:
110     return 3;
111
112   case ARM64::fixup_arm64_pcrel_adr_imm21:
113   case ARM64::fixup_arm64_pcrel_adrp_imm21:
114   case ARM64::fixup_arm64_pcrel_branch26:
115   case ARM64::fixup_arm64_pcrel_call26:
116   case FK_Data_4:
117     return 4;
118
119   case FK_Data_8:
120     return 8;
121   }
122 }
123
124 static unsigned AdrImmBits(unsigned Value) {
125   unsigned lo2 = Value & 0x3;
126   unsigned hi19 = (Value & 0x1ffffc) >> 2;
127   return (hi19 << 5) | (lo2 << 29);
128 }
129
130 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
131   int64_t SignedValue = static_cast<int64_t>(Value);
132   switch (Kind) {
133   default:
134     assert(false && "Unknown fixup kind!");
135   case ARM64::fixup_arm64_pcrel_adr_imm21:
136     if (SignedValue > 2097151 || SignedValue < -2097152)
137       report_fatal_error("fixup value out of range");
138     return AdrImmBits(Value & 0x1fffffULL);
139   case ARM64::fixup_arm64_pcrel_adrp_imm21:
140     return AdrImmBits((Value & 0x1fffff000ULL) >> 12);
141   case ARM64::fixup_arm64_ldr_pcrel_imm19:
142   case ARM64::fixup_arm64_pcrel_branch19:
143     // Signed 21-bit immediate
144     if (SignedValue > 2097151 || SignedValue < -2097152)
145       report_fatal_error("fixup value out of range");
146     // Low two bits are not encoded.
147     return (Value >> 2) & 0x7ffff;
148   case ARM64::fixup_arm64_add_imm12:
149   case ARM64::fixup_arm64_ldst_imm12_scale1:
150     // Unsigned 12-bit immediate
151     if (Value >= 0x1000)
152       report_fatal_error("invalid imm12 fixup value");
153     return Value;
154   case ARM64::fixup_arm64_ldst_imm12_scale2:
155     // Unsigned 12-bit immediate which gets multiplied by 2
156     if (Value & 1 || Value >= 0x2000)
157       report_fatal_error("invalid imm12 fixup value");
158     return Value >> 1;
159   case ARM64::fixup_arm64_ldst_imm12_scale4:
160     // Unsigned 12-bit immediate which gets multiplied by 4
161     if (Value & 3 || Value >= 0x4000)
162       report_fatal_error("invalid imm12 fixup value");
163     return Value >> 2;
164   case ARM64::fixup_arm64_ldst_imm12_scale8:
165     // Unsigned 12-bit immediate which gets multiplied by 8
166     if (Value & 7 || Value >= 0x8000)
167       report_fatal_error("invalid imm12 fixup value");
168     return Value >> 3;
169   case ARM64::fixup_arm64_ldst_imm12_scale16:
170     // Unsigned 12-bit immediate which gets multiplied by 16
171     if (Value & 15 || Value >= 0x10000)
172       report_fatal_error("invalid imm12 fixup value");
173     return Value >> 4;
174   case ARM64::fixup_arm64_movw:
175     report_fatal_error("no resolvable MOVZ/MOVK fixups supported yet");
176     return Value;
177   case ARM64::fixup_arm64_pcrel_branch14:
178     // Signed 16-bit immediate
179     if (SignedValue > 32767 || SignedValue < -32768)
180       report_fatal_error("fixup value out of range");
181     // Low two bits are not encoded (4-byte alignment assumed).
182     if (Value & 0x3)
183       report_fatal_error("fixup not sufficiently aligned");
184     return (Value >> 2) & 0x3fff;
185   case ARM64::fixup_arm64_pcrel_branch26:
186   case ARM64::fixup_arm64_pcrel_call26:
187     // Signed 28-bit immediate
188     if (SignedValue > 134217727 || SignedValue < -134217728)
189       report_fatal_error("fixup value out of range");
190     // Low two bits are not encoded (4-byte alignment assumed).
191     if (Value & 0x3)
192       report_fatal_error("fixup not sufficiently aligned");
193     return (Value >> 2) & 0x3ffffff;
194   case FK_Data_1:
195   case FK_Data_2:
196   case FK_Data_4:
197   case FK_Data_8:
198     return Value;
199   }
200 }
201
202 void ARM64AsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
203                                  unsigned DataSize, uint64_t Value,
204                                  bool IsPCRel) const {
205   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
206   if (!Value)
207     return; // Doesn't change encoding.
208   MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
209   // Apply any target-specific value adjustments.
210   Value = adjustFixupValue(Fixup.getKind(), Value);
211
212   // Shift the value into position.
213   Value <<= Info.TargetOffset;
214
215   unsigned Offset = Fixup.getOffset();
216   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
217
218   // For each byte of the fragment that the fixup touches, mask in the
219   // bits from the fixup value.
220   for (unsigned i = 0; i != NumBytes; ++i)
221     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
222 }
223
224 bool ARM64AsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
225   return false;
226 }
227
228 bool ARM64AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
229                                            const MCRelaxableFragment *DF,
230                                            const MCAsmLayout &Layout) const {
231   // FIXME:  This isn't correct for ARM64. Just moving the "generic" logic
232   // into the targets for now.
233   //
234   // Relax if the value is too big for a (signed) i8.
235   return int64_t(Value) != int64_t(int8_t(Value));
236 }
237
238 void ARM64AsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
239   assert(false && "ARM64AsmBackend::relaxInstruction() unimplemented");
240 }
241
242 bool ARM64AsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
243   // If the count is not 4-byte aligned, we must be writing data into the text
244   // section (otherwise we have unaligned instructions, and thus have far
245   // bigger problems), so just write zeros instead.
246   if ((Count & 3) != 0) {
247     for (uint64_t i = 0, e = (Count & 3); i != e; ++i)
248       OW->Write8(0);
249   }
250
251   // We are properly aligned, so write NOPs as requested.
252   Count /= 4;
253   for (uint64_t i = 0; i != Count; ++i)
254     OW->Write32(0xd503201f);
255   return true;
256 }
257
258 namespace {
259
260 namespace CU {
261
262 /// \brief Compact unwind encoding values.
263 enum CompactUnwindEncodings {
264   /// \brief A "frameless" leaf function, where no non-volatile registers are
265   /// saved. The return remains in LR throughout the function.
266   UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
267
268   /// \brief No compact unwind encoding available. Instead the low 23-bits of
269   /// the compact unwind encoding is the offset of the DWARF FDE in the
270   /// __eh_frame section. This mode is never used in object files. It is only
271   /// generated by the linker in final linked images, which have only DWARF info
272   /// for a function.
273   UNWIND_ARM64_MODE_DWARF = 0x03000000,
274
275   /// \brief This is a standard arm64 prologue where FP/LR are immediately
276   /// pushed on the stack, then SP is copied to FP. If there are any
277   /// non-volatile register saved, they are copied into the stack fame in pairs
278   /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
279   /// five X pairs and four D pairs can be saved, but the memory layout must be
280   /// in register number order.
281   UNWIND_ARM64_MODE_FRAME = 0x04000000,
282
283   /// \brief Frame register pair encodings.
284   UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
285   UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
286   UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
287   UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
288   UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
289   UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
290   UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
291   UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
292   UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
293 };
294
295 } // end CU namespace
296
297 // FIXME: This should be in a separate file.
298 class DarwinARM64AsmBackend : public ARM64AsmBackend {
299   const MCRegisterInfo &MRI;
300
301   /// \brief Encode compact unwind stack adjustment for frameless functions.
302   /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
303   /// The stack size always needs to be 16 byte aligned.
304   uint32_t encodeStackAdjustment(uint32_t StackSize) const {
305     return (StackSize / 16) << 12;
306   }
307
308 public:
309   DarwinARM64AsmBackend(const Target &T, const MCRegisterInfo &MRI)
310       : ARM64AsmBackend(T), MRI(MRI) {}
311
312   MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
313     return createARM64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
314                                        MachO::CPU_SUBTYPE_ARM64_ALL);
315   }
316
317   bool doesSectionRequireSymbols(const MCSection &Section) const override {
318     // Any section for which the linker breaks things into atoms needs to
319     // preserve symbols, including assembler local symbols, to identify
320     // those atoms. These sections are:
321     // Sections of type:
322     //
323     //    S_CSTRING_LITERALS  (e.g. __cstring)
324     //    S_LITERAL_POINTERS  (e.g.  objc selector pointers)
325     //    S_16BYTE_LITERALS, S_8BYTE_LITERALS, S_4BYTE_LITERALS
326     //
327     // Sections named:
328     //
329     //    __TEXT,__eh_frame
330     //    __TEXT,__ustring
331     //    __DATA,__cfstring
332     //    __DATA,__objc_classrefs
333     //    __DATA,__objc_catlist
334     //
335     // FIXME: It would be better if the compiler used actual linker local
336     // symbols for each of these sections rather than preserving what
337     // are ostensibly assembler local symbols.
338     const MCSectionMachO &SMO = static_cast<const MCSectionMachO &>(Section);
339     return (SMO.getType() == MachO::S_CSTRING_LITERALS ||
340             SMO.getType() == MachO::S_4BYTE_LITERALS ||
341             SMO.getType() == MachO::S_8BYTE_LITERALS ||
342             SMO.getType() == MachO::S_16BYTE_LITERALS ||
343             SMO.getType() == MachO::S_LITERAL_POINTERS ||
344             (SMO.getSegmentName() == "__TEXT" &&
345              (SMO.getSectionName() == "__eh_frame" ||
346               SMO.getSectionName() == "__ustring")) ||
347             (SMO.getSegmentName() == "__DATA" &&
348              (SMO.getSectionName() == "__cfstring" ||
349               SMO.getSectionName() == "__objc_classrefs" ||
350               SMO.getSectionName() == "__objc_catlist")));
351   }
352
353   /// \brief Generate the compact unwind encoding from the CFI directives.
354   uint32_t generateCompactUnwindEncoding(
355                              ArrayRef<MCCFIInstruction> Instrs) const override {
356     if (Instrs.empty())
357       return CU::UNWIND_ARM64_MODE_FRAMELESS;
358
359     bool HasFP = false;
360     unsigned StackSize = 0;
361
362     uint32_t CompactUnwindEncoding = 0;
363     for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
364       const MCCFIInstruction &Inst = Instrs[i];
365
366       switch (Inst.getOperation()) {
367       default:
368         // Cannot handle this directive:  bail out.
369         return CU::UNWIND_ARM64_MODE_DWARF;
370       case MCCFIInstruction::OpDefCfa: {
371         // Defines a frame pointer.
372         assert(getXRegFromWReg(MRI.getLLVMRegNum(Inst.getRegister(), true)) ==
373                    ARM64::FP &&
374                "Invalid frame pointer!");
375         assert(i + 2 < e && "Insufficient CFI instructions to define a frame!");
376
377         const MCCFIInstruction &LRPush = Instrs[++i];
378         assert(LRPush.getOperation() == MCCFIInstruction::OpOffset &&
379                "Link register not pushed!");
380         const MCCFIInstruction &FPPush = Instrs[++i];
381         assert(FPPush.getOperation() == MCCFIInstruction::OpOffset &&
382                "Frame pointer not pushed!");
383
384         unsigned LRReg = MRI.getLLVMRegNum(LRPush.getRegister(), true);
385         unsigned FPReg = MRI.getLLVMRegNum(FPPush.getRegister(), true);
386
387         LRReg = getXRegFromWReg(LRReg);
388         FPReg = getXRegFromWReg(FPReg);
389
390         assert(LRReg == ARM64::LR && FPReg == ARM64::FP &&
391                "Pushing invalid registers for frame!");
392
393         // Indicate that the function has a frame.
394         CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
395         HasFP = true;
396         break;
397       }
398       case MCCFIInstruction::OpDefCfaOffset: {
399         assert(StackSize == 0 && "We already have the CFA offset!");
400         StackSize = std::abs(Inst.getOffset());
401         break;
402       }
403       case MCCFIInstruction::OpOffset: {
404         // Registers are saved in pairs. We expect there to be two consecutive
405         // `.cfi_offset' instructions with the appropriate registers specified.
406         unsigned Reg1 = MRI.getLLVMRegNum(Inst.getRegister(), true);
407         if (i + 1 == e)
408           return CU::UNWIND_ARM64_MODE_DWARF;
409
410         const MCCFIInstruction &Inst2 = Instrs[++i];
411         if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
412           return CU::UNWIND_ARM64_MODE_DWARF;
413         unsigned Reg2 = MRI.getLLVMRegNum(Inst2.getRegister(), true);
414
415         // N.B. The encodings must be in register number order, and the X
416         // registers before the D registers.
417
418         // X19/X20 pair = 0x00000001,
419         // X21/X22 pair = 0x00000002,
420         // X23/X24 pair = 0x00000004,
421         // X25/X26 pair = 0x00000008,
422         // X27/X28 pair = 0x00000010
423         Reg1 = getXRegFromWReg(Reg1);
424         Reg2 = getXRegFromWReg(Reg2);
425
426         if (Reg1 == ARM64::X19 && Reg2 == ARM64::X20 &&
427             (CompactUnwindEncoding & 0xF1E) == 0)
428           CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
429         else if (Reg1 == ARM64::X21 && Reg2 == ARM64::X22 &&
430                  (CompactUnwindEncoding & 0xF1C) == 0)
431           CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
432         else if (Reg1 == ARM64::X23 && Reg2 == ARM64::X24 &&
433                  (CompactUnwindEncoding & 0xF18) == 0)
434           CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
435         else if (Reg1 == ARM64::X25 && Reg2 == ARM64::X26 &&
436                  (CompactUnwindEncoding & 0xF10) == 0)
437           CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
438         else if (Reg1 == ARM64::X27 && Reg2 == ARM64::X28 &&
439                  (CompactUnwindEncoding & 0xF00) == 0)
440           CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
441         else {
442           Reg1 = getDRegFromBReg(Reg1);
443           Reg2 = getDRegFromBReg(Reg2);
444
445           // D8/D9 pair   = 0x00000100,
446           // D10/D11 pair = 0x00000200,
447           // D12/D13 pair = 0x00000400,
448           // D14/D15 pair = 0x00000800
449           if (Reg1 == ARM64::D8 && Reg2 == ARM64::D9 &&
450               (CompactUnwindEncoding & 0xE00) == 0)
451             CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
452           else if (Reg1 == ARM64::D10 && Reg2 == ARM64::D11 &&
453                    (CompactUnwindEncoding & 0xC00) == 0)
454             CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
455           else if (Reg1 == ARM64::D12 && Reg2 == ARM64::D13 &&
456                    (CompactUnwindEncoding & 0x800) == 0)
457             CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
458           else if (Reg1 == ARM64::D14 && Reg2 == ARM64::D15)
459             CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
460           else
461             // A pair was pushed which we cannot handle.
462             return CU::UNWIND_ARM64_MODE_DWARF;
463         }
464
465         break;
466       }
467       }
468     }
469
470     if (!HasFP) {
471       // With compact unwind info we can only represent stack adjustments of up
472       // to 65520 bytes.
473       if (StackSize > 65520)
474         return CU::UNWIND_ARM64_MODE_DWARF;
475
476       CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
477       CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
478     }
479
480     return CompactUnwindEncoding;
481   }
482 };
483
484 } // end anonymous namespace
485
486 namespace {
487
488 class ELFARM64AsmBackend : public ARM64AsmBackend {
489 public:
490   uint8_t OSABI;
491   bool IsLittleEndian;
492
493   ELFARM64AsmBackend(const Target &T, uint8_t OSABI, bool IsLittleEndian)
494     : ARM64AsmBackend(T), OSABI(OSABI), IsLittleEndian(IsLittleEndian) {}
495
496   MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
497     return createARM64ELFObjectWriter(OS, OSABI, IsLittleEndian);
498   }
499
500   void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
501                          const MCFixup &Fixup, const MCFragment *DF,
502                          const MCValue &Target, uint64_t &Value,
503                          bool &IsResolved) override;
504
505   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
506                   uint64_t Value, bool IsPCRel) const override;
507 };
508
509 void ELFARM64AsmBackend::processFixupValue(const MCAssembler &Asm,
510                                            const MCAsmLayout &Layout,
511                                            const MCFixup &Fixup,
512                                            const MCFragment *DF,
513                                            const MCValue &Target,
514                                            uint64_t &Value, bool &IsResolved) {
515   // The ADRP instruction adds some multiple of 0x1000 to the current PC &
516   // ~0xfff. This means that the required offset to reach a symbol can vary by
517   // up to one step depending on where the ADRP is in memory. For example:
518   //
519   //     ADRP x0, there
520   //  there:
521   //
522   // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
523   // we'll need that as an offset. At any other address "there" will be in the
524   // same page as the ADRP and the instruction should encode 0x0. Assuming the
525   // section isn't 0x1000-aligned, we therefore need to delegate this decision
526   // to the linker -- a relocation!
527   if ((uint32_t)Fixup.getKind() == ARM64::fixup_arm64_pcrel_adrp_imm21)
528     IsResolved = false;
529 }
530
531 void ELFARM64AsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
532                                     unsigned DataSize, uint64_t Value,
533                                     bool IsPCRel) const {
534   // store fixups in .eh_frame section in big endian order
535   if (!IsLittleEndian && Fixup.getKind() == FK_Data_4) {
536     const MCSection *Sec = Fixup.getValue()->FindAssociatedSection();
537     const MCSectionELF *SecELF = static_cast<const MCSectionELF *>(Sec);
538     if (SecELF->getSectionName() == ".eh_frame")
539       Value = ByteSwap_32(unsigned(Value));
540   }
541   ARM64AsmBackend::applyFixup (Fixup, Data, DataSize, Value, IsPCRel);
542 }
543 }
544
545 MCAsmBackend *llvm::createARM64leAsmBackend(const Target &T,
546                                             const MCRegisterInfo &MRI,
547                                             StringRef TT, StringRef CPU) {
548   Triple TheTriple(TT);
549
550   if (TheTriple.isOSDarwin())
551     return new DarwinARM64AsmBackend(T, MRI);
552
553   assert(TheTriple.isOSBinFormatELF() && "Expect either MachO or ELF target");
554   return new ELFARM64AsmBackend(T, TheTriple.getOS(), /*IsLittleEndian=*/true);
555 }
556
557 MCAsmBackend *llvm::createARM64beAsmBackend(const Target &T,
558                                             const MCRegisterInfo &MRI,
559                                             StringRef TT, StringRef CPU) {
560   Triple TheTriple(TT);
561
562   assert(TheTriple.isOSBinFormatELF() && "Big endian is only supported for ELF targets!");
563   return new ELFARM64AsmBackend(T, TheTriple.getOS(), /*IsLittleEndian=*/false);
564 }