LLVM 24.0.0git
AArch64AsmBackend.cpp
Go to the documentation of this file.
1//===-- AArch64AsmBackend.cpp - AArch64 Assembler Backend -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13#include "llvm/ADT/Enum.h"
16#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCValue.h"
28using namespace llvm;
29
30namespace {
31
32class AArch64AsmBackend : public MCAsmBackend {
33protected:
34 Triple TheTriple;
35
36public:
37 AArch64AsmBackend(const Target &T, const Triple &TT, bool IsLittleEndian)
38 : MCAsmBackend(IsLittleEndian ? llvm::endianness::little
39 : llvm::endianness::big),
40 TheTriple(TT) {}
41
42
43 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
44
45 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override {
46 const static MCFixupKindInfo Infos[AArch64::NumTargetFixupKinds] = {
47 // This table *must* be in the order that the fixup_* kinds are defined
48 // in AArch64FixupKinds.h.
49 //
50 // Name Offset (bits) Size (bits) Flags
51 {"fixup_aarch64_pcrel_adr_imm21", 0, 32, 0},
52 {"fixup_aarch64_pcrel_adrp_imm21", 0, 32, 0},
53 {"fixup_aarch64_add_imm12", 10, 12, 0},
54 {"fixup_aarch64_ldst_imm12_scale1", 10, 12, 0},
55 {"fixup_aarch64_ldst_imm12_scale2", 10, 12, 0},
56 {"fixup_aarch64_ldst_imm12_scale4", 10, 12, 0},
57 {"fixup_aarch64_ldst_imm12_scale8", 10, 12, 0},
58 {"fixup_aarch64_ldst_imm12_scale16", 10, 12, 0},
59 {"fixup_aarch64_ldr_pcrel_imm19", 5, 19, 0},
60 {"fixup_aarch64_movw", 5, 16, 0},
61 {"fixup_aarch64_pcrel_branch9", 5, 9, 0},
62 {"fixup_aarch64_pcrel_branch14", 5, 14, 0},
63 {"fixup_aarch64_pcrel_branch16", 5, 16, 0},
64 {"fixup_aarch64_pcrel_branch19", 5, 19, 0},
65 {"fixup_aarch64_pcrel_branch26", 0, 26, 0},
66 {"fixup_aarch64_pcrel_call26", 0, 26, 0}};
67
68 // Fixup kinds from raw relocation types and .reloc directives force
69 // relocations and do not need these fields.
70 if (mc::isRelocation(Kind))
71 return {};
72
73 if (Kind < FirstTargetFixupKind)
75
76 assert(unsigned(Kind - FirstTargetFixupKind) <
78 "Invalid kind!");
79 return Infos[Kind - FirstTargetFixupKind];
80 }
81
82 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
83 uint8_t *Data, uint64_t Value, bool IsResolved) override;
84
85 bool writeNopData(raw_ostream &OS, uint64_t Count,
86 const MCSubtargetInfo *STI) const override;
87
88 unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
89};
90
91} // end anonymous namespace
92
93/// The number of bytes the fixup may change.
132
133static unsigned AdrImmBits(unsigned Value) {
134 unsigned lo2 = Value & 0x3;
135 unsigned hi19 = (Value & 0x1ffffc) >> 2;
136 return (hi19 << 5) | (lo2 << 29);
137}
138
141 const Triple &TheTriple, bool IsResolved) {
142 int64_t SignedValue = static_cast<int64_t>(Value);
143 switch (Fixup.getKind()) {
144 default:
145 llvm_unreachable("Unknown fixup kind!");
147 if (!isInt<21>(SignedValue))
148 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
149 return AdrImmBits(Value & 0x1fffffULL);
151 assert(!IsResolved);
152 if (TheTriple.isOSBinFormatCOFF()) {
153 if (!isInt<21>(SignedValue))
154 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
155 return AdrImmBits(Value & 0x1fffffULL);
156 }
157 return AdrImmBits((Value & 0x1fffff000ULL) >> 12);
160 // Signed 19-bit immediate which gets multiplied by 4
161 if (!isInt<21>(SignedValue))
162 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
163 if (Value & 0x3)
164 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
165 // Low two bits are not encoded.
166 return (Value >> 2) & 0x7ffff;
169 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
170 Value &= 0xfff;
171 // Unsigned 12-bit immediate
172 if (!isUInt<12>(Value))
173 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
174 return Value;
176 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
177 Value &= 0xfff;
178 // Unsigned 12-bit immediate which gets multiplied by 2
179 if (!isUInt<13>(Value))
180 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
181 if (Value & 0x1)
182 Ctx.reportError(Fixup.getLoc(), "fixup must be 2-byte aligned");
183 return Value >> 1;
185 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
186 Value &= 0xfff;
187 // Unsigned 12-bit immediate which gets multiplied by 4
188 if (!isUInt<14>(Value))
189 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
190 if (Value & 0x3)
191 Ctx.reportError(Fixup.getLoc(), "fixup must be 4-byte aligned");
192 return Value >> 2;
194 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
195 Value &= 0xfff;
196 // Unsigned 12-bit immediate which gets multiplied by 8
197 if (!isUInt<15>(Value))
198 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
199 if (Value & 0x7)
200 Ctx.reportError(Fixup.getLoc(), "fixup must be 8-byte aligned");
201 return Value >> 3;
203 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
204 Value &= 0xfff;
205 // Unsigned 12-bit immediate which gets multiplied by 16
206 if (!isUInt<16>(Value))
207 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
208 if (Value & 0xf)
209 Ctx.reportError(Fixup.getLoc(), "fixup must be 16-byte aligned");
210 return Value >> 4;
212 AArch64::Specifier RefKind =
213 static_cast<AArch64::Specifier>(Target.getSpecifier());
214 if (AArch64::getSymbolLoc(RefKind) != AArch64::S_ABS &&
216 if (!RefKind) {
217 // The fixup is an expression
218 if (SignedValue > 0xFFFF || SignedValue < -0xFFFF)
219 Ctx.reportError(Fixup.getLoc(),
220 "fixup value out of range [-0xFFFF, 0xFFFF]");
221
222 // Invert the negative immediate because it will feed into a MOVN.
223 if (SignedValue < 0)
224 SignedValue = ~SignedValue;
225 Value = static_cast<uint64_t>(SignedValue);
226 } else
227 // VK_GOTTPREL, VK_TPREL, VK_DTPREL are movw fixups, but they can't
228 // ever be resolved in the assembler.
229 Ctx.reportError(Fixup.getLoc(),
230 "relocation for a thread-local variable points to an "
231 "absolute symbol");
232 return Value;
233 }
234
235 if (!IsResolved) {
236 // FIXME: Figure out when this can actually happen, and verify our
237 // behavior.
238 Ctx.reportError(Fixup.getLoc(), "unresolved movw fixup not yet "
239 "implemented");
240 return Value;
241 }
242
243 if (AArch64::getSymbolLoc(RefKind) == AArch64::S_SABS) {
244 switch (AArch64::getAddressFrag(RefKind)) {
245 case AArch64::S_G0:
246 break;
247 case AArch64::S_G1:
248 SignedValue = SignedValue >> 16;
249 break;
250 case AArch64::S_G2:
251 SignedValue = SignedValue >> 32;
252 break;
253 case AArch64::S_G3:
254 SignedValue = SignedValue >> 48;
255 break;
256 default:
257 llvm_unreachable("Variant kind doesn't correspond to fixup");
258 }
259
260 } else {
261 switch (AArch64::getAddressFrag(RefKind)) {
262 case AArch64::S_G0:
263 break;
264 case AArch64::S_G1:
265 Value = Value >> 16;
266 break;
267 case AArch64::S_G2:
268 Value = Value >> 32;
269 break;
270 case AArch64::S_G3:
271 Value = Value >> 48;
272 break;
273 default:
274 llvm_unreachable("Variant kind doesn't correspond to fixup");
275 }
276 }
277
278 if (RefKind & AArch64::S_NC) {
279 Value &= 0xFFFF;
280 } else if (AArch64::getSymbolLoc(RefKind) == AArch64::S_SABS) {
281 if (SignedValue > 0xFFFF || SignedValue < -0xFFFF)
282 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
283
284 // Invert the negative immediate because it will feed into a MOVN.
285 if (SignedValue < 0)
286 SignedValue = ~SignedValue;
287 Value = static_cast<uint64_t>(SignedValue);
288 } else if (Value > 0xFFFF) {
289 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
290 }
291 return Value;
292 }
294 // Signed 11-bit(9bits + 2 shifts) label
295 if (!isInt<11>(SignedValue))
296 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
297 // Low two bits are not encoded (4-byte alignment assumed).
298 if (Value & 0b11)
299 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
300 return (Value >> 2) & 0x1ff;
302 // Signed 16-bit immediate
303 if (!isInt<16>(SignedValue))
304 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
305 // Low two bits are not encoded (4-byte alignment assumed).
306 if (Value & 0x3)
307 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
308 return (Value >> 2) & 0x3fff;
310 // Unsigned PC-relative offset, so invert the negative immediate.
311 SignedValue = -SignedValue;
312 Value = static_cast<uint64_t>(SignedValue);
313 // Check valid 18-bit unsigned range.
314 if (SignedValue < 0 || SignedValue > ((1 << 18) - 1))
315 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
316 // Low two bits are not encoded (4-byte alignment assumed).
317 if (Value & 0b11)
318 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
319 return (Value >> 2) & 0xffff;
322 if (TheTriple.isOSBinFormatCOFF() && !IsResolved && SignedValue != 0) {
323 // MSVC link.exe and lld do not support this relocation type
324 // with a non-zero offset
325 Ctx.reportError(Fixup.getLoc(),
326 "cannot perform a PC-relative fixup with a non-zero "
327 "symbol offset");
328 }
329 // Signed 28-bit immediate
330 if (!isInt<28>(SignedValue))
331 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
332 // Low two bits are not encoded (4-byte alignment assumed).
333 if (Value & 0x3)
334 Ctx.reportError(Fixup.getLoc(), "fixup not sufficiently aligned");
335 return (Value >> 2) & 0x3ffffff;
336 case FK_Data_1:
337 case FK_Data_2:
338 case FK_Data_4:
339 case FK_Data_8:
340 case FK_SecRel_2:
341 case FK_SecRel_4:
342 return Value;
343 }
344}
345
346std::optional<MCFixupKind>
347AArch64AsmBackend::getFixupKind(StringRef Name) const {
348 if (!TheTriple.isOSBinFormatELF())
349 return std::nullopt;
350
351 constexpr llvm::EnumStringDef<uint16_t> TypesDefs[] = {
352#define ELF_RELOC(X, Y) {{#X}, Y},
353#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
354#undef ELF_RELOC
355 {{"BFD_RELOC_NONE"}, ELF::R_AARCH64_NONE},
356 {{"BFD_RELOC_16"}, ELF::R_AARCH64_ABS16},
357 {{"BFD_RELOC_32"}, ELF::R_AARCH64_ABS32},
358 {{"BFD_RELOC_64"}, ELF::R_AARCH64_ABS64},
359 };
360 static constexpr auto Types = BUILD_ENUM_STRINGS(TypesDefs);
361 if (const auto *E = llvm::EnumStrings(Types).fromString(Name))
362 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + E->value());
363 return std::nullopt;
364}
365
366/// getFixupKindContainereSizeInBytes - The number of bytes of the
367/// container involved in big endian or 0 if the item is little endian
368unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) const {
369 if (Endian == llvm::endianness::little)
370 return 0;
371
372 switch (Kind) {
373 default:
374 llvm_unreachable("Unknown fixup kind!");
375
376 case FK_Data_1:
377 return 1;
378 case FK_Data_2:
379 return 2;
380 case FK_Data_4:
381 return 4;
382 case FK_Data_8:
383 return 8;
384
401 // Instructions are always little endian
402 return 0;
403 }
404}
405
407 // The ADRP instruction adds some multiple of 0x1000 to the current PC &
408 // ~0xfff. This means that the required offset to reach a symbol can vary by
409 // up to one step depending on where the ADRP is in memory. For example:
410 //
411 // ADRP x0, there
412 // there:
413 //
414 // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
415 // we'll need that as an offset. At any other address "there" will be in the
416 // same page as the ADRP and the instruction should encode 0x0. Assuming the
417 // section isn't 0x1000-aligned, we therefore need to delegate this decision
418 // to the linker -- a relocation!
420}
421
422void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
423 const MCValue &Target, uint8_t *Data,
424 uint64_t Value, bool IsResolved) {
426 IsResolved = false;
427 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
428 MCFixupKind Kind = Fixup.getKind();
429 if (mc::isRelocation(Kind))
430 return;
431
432 if (Fixup.getKind() == FK_Data_8 && TheTriple.isOSBinFormatELF()) {
433 auto RefKind = static_cast<AArch64::Specifier>(Target.getSpecifier());
435 if (SymLoc == AArch64::S_AUTH || SymLoc == AArch64::S_AUTHADDR) {
436 const auto *Expr = dyn_cast<AArch64AuthMCExpr>(Fixup.getValue());
437 if (!Expr) {
438 getContext().reportError(Fixup.getValue()->getLoc(),
439 "expected relocatable expression");
440 return;
441 }
442 assert(Value == 0);
443 Value = (uint64_t(Expr->getDiscriminator()) << 32) |
444 (uint64_t(Expr->getKey()) << 60) |
445 (uint64_t(Expr->hasAddressDiversity()) << 63);
446 }
447 }
448
449 if (!Value)
450 return; // Doesn't change encoding.
451 unsigned NumBytes = getFixupKindNumBytes(Kind);
452 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
453 MCContext &Ctx = getContext();
454 int64_t SignedValue = static_cast<int64_t>(Value);
455 // Apply any target-specific value adjustments.
456 Value = adjustFixupValue(Fixup, Target, Value, Ctx, TheTriple, IsResolved);
457
458 // Shift the value into position.
459 Value <<= Info.TargetOffset;
460
461 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
462 "Invalid fixup offset!");
463
464 // Used to point to big endian bytes.
465 unsigned FulleSizeInBytes = getFixupKindContainereSizeInBytes(Fixup.getKind());
466
467 // For each byte of the fragment that the fixup touches, mask in the
468 // bits from the fixup value.
469 if (FulleSizeInBytes == 0) {
470 // Handle as little-endian
471 for (unsigned i = 0; i != NumBytes; ++i) {
472 Data[i] |= uint8_t((Value >> (i * 8)) & 0xff);
473 }
474 } else {
475 // Handle as big-endian
476 assert(Fixup.getOffset() + FulleSizeInBytes <= F.getSize() &&
477 "Invalid fixup size!");
478 assert(NumBytes <= FulleSizeInBytes && "Invalid fixup size!");
479 for (unsigned i = 0; i != NumBytes; ++i) {
480 unsigned Idx = FulleSizeInBytes - 1 - i;
481 Data[Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
482 }
483 }
484
485 // FIXME: getFixupKindInfo() and getFixupKindNumBytes() could be fixed to
486 // handle this more cleanly. This may affect the output of -show-mc-encoding.
487 AArch64::Specifier RefKind =
488 static_cast<AArch64::Specifier>(Target.getSpecifier());
489 if (AArch64::getSymbolLoc(RefKind) == AArch64::S_SABS ||
490 (!RefKind && Fixup.getKind() == AArch64::fixup_aarch64_movw)) {
491 // If the immediate is negative, generate MOVN else MOVZ.
492 // (Bit 30 = 0) ==> MOVN, (Bit 30 = 1) ==> MOVZ.
493 if (SignedValue < 0)
494 Data[3] &= ~(1 << 6);
495 else
496 Data[3] |= (1 << 6);
497 }
498}
499
500bool AArch64AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
501 const MCSubtargetInfo *STI) const {
502 // If the count is not 4-byte aligned, we must be writing data into the text
503 // section (otherwise we have unaligned instructions, and thus have far
504 // bigger problems), so just write zeros instead.
505 OS.write_zeros(Count % 4);
506
507 // We are properly aligned, so write NOPs as requested.
508 Count /= 4;
509 for (uint64_t i = 0; i != Count; ++i)
510 OS.write("\x1f\x20\x03\xd5", 4);
511 return true;
512}
513
514namespace {
515
516namespace CU {
517
518/// Compact unwind encoding values.
520 /// A "frameless" leaf function, where no non-volatile registers are
521 /// saved. The return remains in LR throughout the function.
522 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000,
523
524 /// No compact unwind encoding available. Instead the low 23-bits of
525 /// the compact unwind encoding is the offset of the DWARF FDE in the
526 /// __eh_frame section. This mode is never used in object files. It is only
527 /// generated by the linker in final linked images, which have only DWARF info
528 /// for a function.
529 UNWIND_ARM64_MODE_DWARF = 0x03000000,
530
531 /// This is a standard arm64 prologue where FP/LR are immediately
532 /// pushed on the stack, then SP is copied to FP. If there are any
533 /// non-volatile register saved, they are copied into the stack fame in pairs
534 /// in a contiguous ranger right below the saved FP/LR pair. Any subset of the
535 /// five X pairs and four D pairs can be saved, but the memory layout must be
536 /// in register number order.
537 UNWIND_ARM64_MODE_FRAME = 0x04000000,
538
539 /// Frame register pair encodings.
540 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001,
541 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002,
542 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004,
543 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008,
544 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010,
545 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100,
546 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200,
547 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400,
548 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800
549};
550
551} // end CU namespace
552
553// FIXME: This should be in a separate file.
554class DarwinAArch64AsmBackend : public AArch64AsmBackend {
555 const MCRegisterInfo &MRI;
556
557 /// Encode compact unwind stack adjustment for frameless functions.
558 /// See UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK in compact_unwind_encoding.h.
559 /// The stack size always needs to be 16 byte aligned.
560 uint32_t encodeStackAdjustment(uint32_t StackSize) const {
561 return (StackSize / 16) << 12;
562 }
563
564public:
565 DarwinAArch64AsmBackend(const Target &T, const Triple &TT,
566 const MCRegisterInfo &MRI)
567 : AArch64AsmBackend(T, TT, /*IsLittleEndian*/ true), MRI(MRI) {}
568
569 std::unique_ptr<MCObjectTargetWriter>
570 createObjectTargetWriter() const override {
571 uint32_t CPUType = cantFail(MachO::getCPUType(TheTriple));
572 uint32_t CPUSubType = cantFail(MachO::getCPUSubType(TheTriple));
573 return createAArch64MachObjectWriter(CPUType, CPUSubType,
574 TheTriple.isArch32Bit());
575 }
576
577 /// Generate the compact unwind encoding from the CFI directives.
578 uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI,
579 const MCContext *Ctxt) const override {
580 if (Ctxt->emitDwarfUnwindInfo() == EmitDwarfUnwindType::DwarfOnly)
581 return CU::UNWIND_ARM64_MODE_DWARF;
582
583 // MTE-tagged frames must use DWARF unwinding because compact unwind
584 // doesn't handle MTE tags
585 if (FI->IsMTETaggedFrame)
586 return CU::UNWIND_ARM64_MODE_DWARF;
587
588 // Signal frames cannot be encoded in compact unwind.
589 if (FI->IsSignalFrame)
590 return CU::UNWIND_ARM64_MODE_DWARF;
591
593 if (Instrs.empty())
594 return CU::UNWIND_ARM64_MODE_FRAMELESS;
595 if (!isDarwinCanonicalPersonality(FI->Personality) &&
597 return CU::UNWIND_ARM64_MODE_DWARF;
598
599 bool HasFP = false;
600 uint64_t StackSize = 0;
601
602 uint64_t CompactUnwindEncoding = 0;
603 int64_t CurOffset = 0;
604 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
605 const MCCFIInstruction &Inst = Instrs[i];
606
607 switch (Inst.getOperation()) {
608 default:
609 // Cannot handle this directive: bail out.
610 return CU::UNWIND_ARM64_MODE_DWARF;
612 // Defines a frame pointer.
613 MCRegister XReg =
614 getXRegFromWReg(*MRI.getLLVMRegNum(Inst.getRegister(), true));
615
616 // Other CFA registers than FP are not supported by compact unwind.
617 // Fallback on DWARF.
618 // FIXME: When opt-remarks are supported in MC, add a remark to notify
619 // the user.
620 if (XReg != AArch64::FP)
621 return CU::UNWIND_ARM64_MODE_DWARF;
622
623 if (i + 2 >= e)
624 return CU::UNWIND_ARM64_MODE_DWARF;
625
626 const MCCFIInstruction &LRPush = Instrs[++i];
628 return CU::UNWIND_ARM64_MODE_DWARF;
629 const MCCFIInstruction &FPPush = Instrs[++i];
631 return CU::UNWIND_ARM64_MODE_DWARF;
632
633 if (FPPush.getOffset() + 8 != LRPush.getOffset())
634 return CU::UNWIND_ARM64_MODE_DWARF;
635 CurOffset = FPPush.getOffset();
636
637 MCRegister LRReg = *MRI.getLLVMRegNum(LRPush.getRegister(), true);
638 MCRegister FPReg = *MRI.getLLVMRegNum(FPPush.getRegister(), true);
639
640 LRReg = getXRegFromWReg(LRReg);
642
643 if (LRReg != AArch64::LR || FPReg != AArch64::FP)
644 return CU::UNWIND_ARM64_MODE_DWARF;
645
646 // Indicate that the function has a frame.
647 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAME;
648 HasFP = true;
649 break;
650 }
652 if (StackSize != 0)
653 return CU::UNWIND_ARM64_MODE_DWARF;
654 StackSize = std::abs(Inst.getOffset());
655 break;
656 }
658 // Registers are saved in pairs. We expect there to be two consecutive
659 // `.cfi_offset' instructions with the appropriate registers specified.
660 MCRegister Reg1 = *MRI.getLLVMRegNum(Inst.getRegister(), true);
661 if (i + 1 == e)
662 return CU::UNWIND_ARM64_MODE_DWARF;
663
664 if (CurOffset != 0 && Inst.getOffset() != CurOffset - 8)
665 return CU::UNWIND_ARM64_MODE_DWARF;
666 CurOffset = Inst.getOffset();
667
668 const MCCFIInstruction &Inst2 = Instrs[++i];
670 return CU::UNWIND_ARM64_MODE_DWARF;
671 MCRegister Reg2 = *MRI.getLLVMRegNum(Inst2.getRegister(), true);
672
673 if (Inst2.getOffset() != CurOffset - 8)
674 return CU::UNWIND_ARM64_MODE_DWARF;
675 CurOffset = Inst2.getOffset();
676
677 // N.B. The encodings must be in register number order, and the X
678 // registers before the D registers.
679
680 // X19/X20 pair = 0x00000001,
681 // X21/X22 pair = 0x00000002,
682 // X23/X24 pair = 0x00000004,
683 // X25/X26 pair = 0x00000008,
684 // X27/X28 pair = 0x00000010
685 Reg1 = getXRegFromWReg(Reg1);
686 Reg2 = getXRegFromWReg(Reg2);
687
688 if (Reg1 == AArch64::X19 && Reg2 == AArch64::X20 &&
689 (CompactUnwindEncoding & 0xF1E) == 0)
690 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X19_X20_PAIR;
691 else if (Reg1 == AArch64::X21 && Reg2 == AArch64::X22 &&
692 (CompactUnwindEncoding & 0xF1C) == 0)
693 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X21_X22_PAIR;
694 else if (Reg1 == AArch64::X23 && Reg2 == AArch64::X24 &&
695 (CompactUnwindEncoding & 0xF18) == 0)
696 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X23_X24_PAIR;
697 else if (Reg1 == AArch64::X25 && Reg2 == AArch64::X26 &&
698 (CompactUnwindEncoding & 0xF10) == 0)
699 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X25_X26_PAIR;
700 else if (Reg1 == AArch64::X27 && Reg2 == AArch64::X28 &&
701 (CompactUnwindEncoding & 0xF00) == 0)
702 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_X27_X28_PAIR;
703 else {
704 Reg1 = getDRegFromBReg(Reg1);
705 Reg2 = getDRegFromBReg(Reg2);
706
707 // D8/D9 pair = 0x00000100,
708 // D10/D11 pair = 0x00000200,
709 // D12/D13 pair = 0x00000400,
710 // D14/D15 pair = 0x00000800
711 if (Reg1 == AArch64::D8 && Reg2 == AArch64::D9 &&
712 (CompactUnwindEncoding & 0xE00) == 0)
713 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D8_D9_PAIR;
714 else if (Reg1 == AArch64::D10 && Reg2 == AArch64::D11 &&
715 (CompactUnwindEncoding & 0xC00) == 0)
716 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D10_D11_PAIR;
717 else if (Reg1 == AArch64::D12 && Reg2 == AArch64::D13 &&
718 (CompactUnwindEncoding & 0x800) == 0)
719 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D12_D13_PAIR;
720 else if (Reg1 == AArch64::D14 && Reg2 == AArch64::D15)
721 CompactUnwindEncoding |= CU::UNWIND_ARM64_FRAME_D14_D15_PAIR;
722 else
723 // A pair was pushed which we cannot handle.
724 return CU::UNWIND_ARM64_MODE_DWARF;
725 }
726
727 break;
728 }
730 // .cfi_set_ra_state is not yet supported in Compact Unwind.
731 return CU::UNWIND_ARM64_MODE_DWARF;
732 }
733 }
734
735 if (!HasFP) {
736 // With compact unwind info we can only represent stack adjustments of up
737 // to 65520 bytes.
738 if (StackSize > 65520)
739 return CU::UNWIND_ARM64_MODE_DWARF;
740
741 CompactUnwindEncoding |= CU::UNWIND_ARM64_MODE_FRAMELESS;
742 CompactUnwindEncoding |= encodeStackAdjustment(StackSize);
743 }
744
745 return CompactUnwindEncoding;
746 }
747};
748
749} // end anonymous namespace
750
751namespace {
752
753class ELFAArch64AsmBackend : public AArch64AsmBackend {
754public:
755 uint8_t OSABI;
756 bool IsILP32;
757
758 ELFAArch64AsmBackend(const Target &T, const Triple &TT, uint8_t OSABI,
759 bool IsLittleEndian, bool IsILP32)
760 : AArch64AsmBackend(T, TT, IsLittleEndian), OSABI(OSABI),
761 IsILP32(IsILP32) {}
762
763 std::unique_ptr<MCObjectTargetWriter>
764 createObjectTargetWriter() const override {
765 return createAArch64ELFObjectWriter(OSABI, IsILP32);
766 }
767};
768
769}
770
771namespace {
772class COFFAArch64AsmBackend : public AArch64AsmBackend {
773public:
774 COFFAArch64AsmBackend(const Target &T, const Triple &TheTriple)
775 : AArch64AsmBackend(T, TheTriple, /*IsLittleEndian*/ true) {}
776
777 std::unique_ptr<MCObjectTargetWriter>
778 createObjectTargetWriter() const override {
779 return createAArch64WinCOFFObjectWriter(TheTriple);
780 }
781};
782}
783
785 const MCSubtargetInfo &STI,
786 const MCRegisterInfo &MRI,
787 const MCTargetOptions &Options) {
788 const Triple &TheTriple = STI.getTargetTriple();
789 if (TheTriple.isOSBinFormatMachO()) {
790 return new DarwinAArch64AsmBackend(T, TheTriple, MRI);
791 }
792
793 if (TheTriple.isOSBinFormatCOFF())
794 return new COFFAArch64AsmBackend(T, TheTriple);
795
796 assert(TheTriple.isOSBinFormatELF() && "Invalid target");
797
799 bool IsILP32 = STI.getTargetTriple().getEnvironment() == Triple::GNUILP32;
800 return new ELFAArch64AsmBackend(T, TheTriple, OSABI, /*IsLittleEndian=*/true,
801 IsILP32);
802}
803
805 const MCSubtargetInfo &STI,
806 const MCRegisterInfo &MRI,
807 const MCTargetOptions &Options) {
808 const Triple &TheTriple = STI.getTargetTriple();
809 assert(TheTriple.isOSBinFormatELF() &&
810 "Big endian is only supported for ELF targets!");
812 bool IsILP32 = STI.getTargetTriple().getEnvironment() == Triple::GNUILP32;
813 return new ELFAArch64AsmBackend(T, TheTriple, OSABI, /*IsLittleEndian=*/false,
814 IsILP32);
815}
static unsigned AdrImmBits(unsigned Value)
static unsigned getFixupKindNumBytes(unsigned Kind)
The number of bytes the fixup may change.
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
static bool shouldForceRelocation(const MCFixup &Fixup)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
basic Basic Alias true
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define BUILD_ENUM_STRINGS(Tab)
Definition Enum.h:120
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define T
PowerPC TLS Dynamic Call Fixup
static constexpr MCPhysReg FPReg
MachineRegisterInfo & MRI
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
Generic interface to target specific assembler backends.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
unsigned getRegister() const
Definition MCDwarf.h:836
OpType getOperation() const
Definition MCDwarf.h:833
int64_t getOffset() const
Definition MCDwarf.h:855
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI bool emitCompactUnwindNonCanonical() const
LLVM_ABI EmitDwarfUnwindType emitDwarfUnwindInfo() const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition Triple.h:876
OSType getOS() const
Get the parsed operating system type of this triple.
Definition Triple.h:524
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition Triple.h:870
EnvironmentType getEnvironment() const
Get the parsed environment type of this triple.
Definition Triple.h:530
LLVM_ABI bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition Triple.cpp:1833
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:867
LLVM Value Representation.
Definition Value.h:75
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & write(unsigned char C)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CompactUnwindEncodings
Compact unwind encoding values.
Specifier getSymbolLoc(Specifier S)
Specifier getAddressFrag(Specifier S)
LLVM_ABI std::error_code fromString(StringRef String, Metadata &HSAMetadata)
Converts String to HSAMetadata.
LLVM_ABI Expected< uint32_t > getCPUSubType(const Triple &T)
Definition MachO.cpp:109
LLVM_ABI Expected< uint32_t > getCPUType(const Triple &T)
Definition MachO.cpp:89
VE::Fixups getFixupKind(uint8_t S)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
std::unique_ptr< MCObjectTargetWriter > createAArch64WinCOFFObjectWriter(const Triple &TheTriple)
EnumStrings(const EnumStringsStorage< T, NumStrs, N, StrLen > &) -> EnumStrings< T, NumStrs >
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
MCAsmBackend * createAArch64leAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ FK_SecRel_2
A two-byte section relative fixup.
Definition MCFixup.h:44
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:45
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
static MCRegister getXRegFromWReg(MCRegister Reg)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::unique_ptr< MCObjectTargetWriter > createAArch64MachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype, bool IsILP32)
MCAsmBackend * createAArch64beAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
static MCRegister getDRegFromBReg(MCRegister Reg)
endianness
Definition bit.h:71
std::unique_ptr< MCObjectTargetWriter > createAArch64ELFObjectWriter(uint8_t OSABI, bool IsILP32)
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32
const MCSymbol * Personality
Definition MCDwarf.h:904
std::vector< MCCFIInstruction > Instructions
Definition MCDwarf.h:906