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
9#include "MCTargetDesc/AArch64FixupKinds.h"
10#include "MCTargetDesc/AArch64MCAsmInfo.h"
11#include "MCTargetDesc/AArch64MCTargetDesc.h"
12#include "Utils/AArch64BaseInfo.h"
13#include "llvm/ADT/Enum.h"
14#include "llvm/BinaryFormat/MachO.h"
15#include "llvm/MC/MCAsmBackend.h"
16#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCELFObjectWriter.h"
19#include "llvm/MC/MCObjectWriter.h"
20#include "llvm/MC/MCRegisterInfo.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/MC/MCTargetOptions.h"
23#include "llvm/MC/MCValue.h"
24#include "llvm/MC/TargetRegistry.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/TargetParser/Triple.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 {.Name: "fixup_aarch64_pcrel_adr_imm21", .TargetOffset: 0, .TargetSize: 32, .Flags: 0},
52 {.Name: "fixup_aarch64_pcrel_adrp_imm21", .TargetOffset: 0, .TargetSize: 32, .Flags: 0},
53 {.Name: "fixup_aarch64_add_imm12", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
54 {.Name: "fixup_aarch64_ldst_imm12_scale1", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
55 {.Name: "fixup_aarch64_ldst_imm12_scale2", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
56 {.Name: "fixup_aarch64_ldst_imm12_scale4", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
57 {.Name: "fixup_aarch64_ldst_imm12_scale8", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
58 {.Name: "fixup_aarch64_ldst_imm12_scale16", .TargetOffset: 10, .TargetSize: 12, .Flags: 0},
59 {.Name: "fixup_aarch64_ldr_pcrel_imm19", .TargetOffset: 5, .TargetSize: 19, .Flags: 0},
60 {.Name: "fixup_aarch64_movw", .TargetOffset: 5, .TargetSize: 16, .Flags: 0},
61 {.Name: "fixup_aarch64_pcrel_branch9", .TargetOffset: 5, .TargetSize: 9, .Flags: 0},
62 {.Name: "fixup_aarch64_pcrel_branch14", .TargetOffset: 5, .TargetSize: 14, .Flags: 0},
63 {.Name: "fixup_aarch64_pcrel_branch16", .TargetOffset: 5, .TargetSize: 16, .Flags: 0},
64 {.Name: "fixup_aarch64_pcrel_branch19", .TargetOffset: 5, .TargetSize: 19, .Flags: 0},
65 {.Name: "fixup_aarch64_pcrel_branch26", .TargetOffset: 0, .TargetSize: 26, .Flags: 0},
66 {.Name: "fixup_aarch64_pcrel_call26", .TargetOffset: 0, .TargetSize: 26, .Flags: 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(FixupKind: Kind))
71 return {};
72
73 if (Kind < FirstTargetFixupKind)
74 return MCAsmBackend::getFixupKindInfo(Kind);
75
76 assert(unsigned(Kind - FirstTargetFixupKind) <
77 AArch64::NumTargetFixupKinds &&
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.
94static unsigned getFixupKindNumBytes(unsigned Kind) {
95 switch (Kind) {
96 default:
97 llvm_unreachable("Unknown fixup kind!");
98
99 case FK_Data_1:
100 return 1;
101
102 case FK_Data_2:
103 case FK_SecRel_2:
104 return 2;
105
106 case AArch64::fixup_aarch64_movw:
107 case AArch64::fixup_aarch64_pcrel_branch9:
108 case AArch64::fixup_aarch64_pcrel_branch14:
109 case AArch64::fixup_aarch64_pcrel_branch16:
110 case AArch64::fixup_aarch64_add_imm12:
111 case AArch64::fixup_aarch64_ldst_imm12_scale1:
112 case AArch64::fixup_aarch64_ldst_imm12_scale2:
113 case AArch64::fixup_aarch64_ldst_imm12_scale4:
114 case AArch64::fixup_aarch64_ldst_imm12_scale8:
115 case AArch64::fixup_aarch64_ldst_imm12_scale16:
116 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
117 case AArch64::fixup_aarch64_pcrel_branch19:
118 return 3;
119
120 case AArch64::fixup_aarch64_pcrel_adr_imm21:
121 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
122 case AArch64::fixup_aarch64_pcrel_branch26:
123 case AArch64::fixup_aarch64_pcrel_call26:
124 case FK_Data_4:
125 case FK_SecRel_4:
126 return 4;
127
128 case FK_Data_8:
129 return 8;
130 }
131}
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
139static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
140 uint64_t Value, MCContext &Ctx,
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!");
146 case AArch64::fixup_aarch64_pcrel_adr_imm21:
147 if (!isInt<21>(x: SignedValue))
148 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
149 return AdrImmBits(Value: Value & 0x1fffffULL);
150 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
151 assert(!IsResolved);
152 if (TheTriple.isOSBinFormatCOFF()) {
153 if (!isInt<21>(x: SignedValue))
154 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
155 return AdrImmBits(Value: Value & 0x1fffffULL);
156 }
157 return AdrImmBits(Value: (Value & 0x1fffff000ULL) >> 12);
158 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
159 case AArch64::fixup_aarch64_pcrel_branch19:
160 // Signed 19-bit immediate which gets multiplied by 4
161 if (!isInt<21>(x: SignedValue))
162 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
163 if (Value & 0x3)
164 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup not sufficiently aligned");
165 // Low two bits are not encoded.
166 return (Value >> 2) & 0x7ffff;
167 case AArch64::fixup_aarch64_add_imm12:
168 case AArch64::fixup_aarch64_ldst_imm12_scale1:
169 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
170 Value &= 0xfff;
171 // Unsigned 12-bit immediate
172 if (!isUInt<12>(x: Value))
173 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
174 return Value;
175 case AArch64::fixup_aarch64_ldst_imm12_scale2:
176 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
177 Value &= 0xfff;
178 // Unsigned 12-bit immediate which gets multiplied by 2
179 if (!isUInt<13>(x: Value))
180 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
181 if (Value & 0x1)
182 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup must be 2-byte aligned");
183 return Value >> 1;
184 case AArch64::fixup_aarch64_ldst_imm12_scale4:
185 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
186 Value &= 0xfff;
187 // Unsigned 12-bit immediate which gets multiplied by 4
188 if (!isUInt<14>(x: Value))
189 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
190 if (Value & 0x3)
191 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup must be 4-byte aligned");
192 return Value >> 2;
193 case AArch64::fixup_aarch64_ldst_imm12_scale8:
194 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
195 Value &= 0xfff;
196 // Unsigned 12-bit immediate which gets multiplied by 8
197 if (!isUInt<15>(x: Value))
198 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
199 if (Value & 0x7)
200 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup must be 8-byte aligned");
201 return Value >> 3;
202 case AArch64::fixup_aarch64_ldst_imm12_scale16:
203 if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
204 Value &= 0xfff;
205 // Unsigned 12-bit immediate which gets multiplied by 16
206 if (!isUInt<16>(x: Value))
207 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
208 if (Value & 0xf)
209 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup must be 16-byte aligned");
210 return Value >> 4;
211 case AArch64::fixup_aarch64_movw: {
212 AArch64::Specifier RefKind =
213 static_cast<AArch64::Specifier>(Target.getSpecifier());
214 if (AArch64::getSymbolLoc(S: RefKind) != AArch64::S_ABS &&
215 AArch64::getSymbolLoc(S: RefKind) != AArch64::S_SABS) {
216 if (!RefKind) {
217 // The fixup is an expression
218 if (SignedValue > 0xFFFF || SignedValue < -0xFFFF)
219 Ctx.reportError(L: Fixup.getLoc(),
220 Msg: "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(L: Fixup.getLoc(),
230 Msg: "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(L: Fixup.getLoc(), Msg: "unresolved movw fixup not yet "
239 "implemented");
240 return Value;
241 }
242
243 if (AArch64::getSymbolLoc(S: RefKind) == AArch64::S_SABS) {
244 switch (AArch64::getAddressFrag(S: 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(S: 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(S: RefKind) == AArch64::S_SABS) {
281 if (SignedValue > 0xFFFF || SignedValue < -0xFFFF)
282 Ctx.reportError(L: Fixup.getLoc(), Msg: "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(L: Fixup.getLoc(), Msg: "fixup value out of range");
290 }
291 return Value;
292 }
293 case AArch64::fixup_aarch64_pcrel_branch9:
294 // Signed 11-bit(9bits + 2 shifts) label
295 if (!isInt<11>(x: SignedValue))
296 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
297 // Low two bits are not encoded (4-byte alignment assumed).
298 if (Value & 0b11)
299 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup not sufficiently aligned");
300 return (Value >> 2) & 0x1ff;
301 case AArch64::fixup_aarch64_pcrel_branch14:
302 // Signed 16-bit immediate
303 if (!isInt<16>(x: SignedValue))
304 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
305 // Low two bits are not encoded (4-byte alignment assumed).
306 if (Value & 0x3)
307 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup not sufficiently aligned");
308 return (Value >> 2) & 0x3fff;
309 case AArch64::fixup_aarch64_pcrel_branch16:
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(L: Fixup.getLoc(), Msg: "fixup value out of range");
316 // Low two bits are not encoded (4-byte alignment assumed).
317 if (Value & 0b11)
318 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup not sufficiently aligned");
319 return (Value >> 2) & 0xffff;
320 case AArch64::fixup_aarch64_pcrel_branch26:
321 case AArch64::fixup_aarch64_pcrel_call26:
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(L: Fixup.getLoc(),
326 Msg: "cannot perform a PC-relative fixup with a non-zero "
327 "symbol offset");
328 }
329 // Signed 28-bit immediate
330 if (!isInt<28>(x: SignedValue))
331 Ctx.reportError(L: Fixup.getLoc(), Msg: "fixup value out of range");
332 // Low two bits are not encoded (4-byte alignment assumed).
333 if (Value & 0x3)
334 Ctx.reportError(L: Fixup.getLoc(), Msg: "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 {.Names: {"BFD_RELOC_NONE"}, .Value: ELF::R_AARCH64_NONE},
356 {.Names: {"BFD_RELOC_16"}, .Value: ELF::R_AARCH64_ABS16},
357 {.Names: {"BFD_RELOC_32"}, .Value: ELF::R_AARCH64_ABS32},
358 {.Names: {"BFD_RELOC_64"}, .Value: ELF::R_AARCH64_ABS64},
359 };
360 static constexpr auto Types = BUILD_ENUM_STRINGS(TypesDefs);
361 if (const auto *E = llvm::EnumStrings(Types).fromString(Str: 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
385 case AArch64::fixup_aarch64_movw:
386 case AArch64::fixup_aarch64_pcrel_branch9:
387 case AArch64::fixup_aarch64_pcrel_branch14:
388 case AArch64::fixup_aarch64_pcrel_branch16:
389 case AArch64::fixup_aarch64_add_imm12:
390 case AArch64::fixup_aarch64_ldst_imm12_scale1:
391 case AArch64::fixup_aarch64_ldst_imm12_scale2:
392 case AArch64::fixup_aarch64_ldst_imm12_scale4:
393 case AArch64::fixup_aarch64_ldst_imm12_scale8:
394 case AArch64::fixup_aarch64_ldst_imm12_scale16:
395 case AArch64::fixup_aarch64_ldr_pcrel_imm19:
396 case AArch64::fixup_aarch64_pcrel_branch19:
397 case AArch64::fixup_aarch64_pcrel_adr_imm21:
398 case AArch64::fixup_aarch64_pcrel_adrp_imm21:
399 case AArch64::fixup_aarch64_pcrel_branch26:
400 case AArch64::fixup_aarch64_pcrel_call26:
401 // Instructions are always little endian
402 return 0;
403 }
404}
405
406static bool shouldForceRelocation(const MCFixup &Fixup) {
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!
419 return Fixup.getKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21;
420}
421
422void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
423 const MCValue &Target, uint8_t *Data,
424 uint64_t Value, bool IsResolved) {
425 if (shouldForceRelocation(Fixup))
426 IsResolved = false;
427 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
428 MCFixupKind Kind = Fixup.getKind();
429 if (mc::isRelocation(FixupKind: Kind))
430 return;
431
432 if (Fixup.getKind() == FK_Data_8 && TheTriple.isOSBinFormatELF()) {
433 auto RefKind = static_cast<AArch64::Specifier>(Target.getSpecifier());
434 AArch64::Specifier SymLoc = AArch64::getSymbolLoc(S: RefKind);
435 if (SymLoc == AArch64::S_AUTH || SymLoc == AArch64::S_AUTHADDR) {
436 const auto *Expr = dyn_cast<AArch64AuthMCExpr>(Val: Fixup.getValue());
437 if (!Expr) {
438 getContext().reportError(L: Fixup.getValue()->getLoc(),
439 Msg: "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(Kind: 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(Kind: 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(S: 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(NumZeros: 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(Ptr: "\x1f\x20\x03\xd5", Size: 4);
511 return true;
512}
513
514namespace {
515
516namespace CU {
517
518/// Compact unwind encoding values.
519enum CompactUnwindEncodings {
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(ValOrErr: MachO::getCPUType(T: TheTriple));
572 uint32_t CPUSubType = cantFail(ValOrErr: MachO::getCPUSubType(T: TheTriple));
573 return createAArch64MachObjectWriter(CPUType, CPUSubtype: CPUSubType,
574 IsILP32: 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
592 ArrayRef<MCCFIInstruction> Instrs = FI->Instructions;
593 if (Instrs.empty())
594 return CU::UNWIND_ARM64_MODE_FRAMELESS;
595 if (!isDarwinCanonicalPersonality(Sym: FI->Personality) &&
596 !Ctxt->emitCompactUnwindNonCanonical())
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;
611 case MCCFIInstruction::OpDefCfa: {
612 // Defines a frame pointer.
613 MCRegister XReg =
614 getXRegFromWReg(Reg: *MRI.getLLVMRegNum(RegNum: Inst.getRegister(), isEH: 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];
627 if (LRPush.getOperation() != MCCFIInstruction::OpOffset)
628 return CU::UNWIND_ARM64_MODE_DWARF;
629 const MCCFIInstruction &FPPush = Instrs[++i];
630 if (FPPush.getOperation() != MCCFIInstruction::OpOffset)
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(RegNum: LRPush.getRegister(), isEH: true);
638 MCRegister FPReg = *MRI.getLLVMRegNum(RegNum: FPPush.getRegister(), isEH: true);
639
640 LRReg = getXRegFromWReg(Reg: LRReg);
641 FPReg = getXRegFromWReg(Reg: FPReg);
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 }
651 case MCCFIInstruction::OpDefCfaOffset: {
652 if (StackSize != 0)
653 return CU::UNWIND_ARM64_MODE_DWARF;
654 StackSize = std::abs(i: Inst.getOffset());
655 break;
656 }
657 case MCCFIInstruction::OpOffset: {
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(RegNum: Inst.getRegister(), isEH: 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];
669 if (Inst2.getOperation() != MCCFIInstruction::OpOffset)
670 return CU::UNWIND_ARM64_MODE_DWARF;
671 MCRegister Reg2 = *MRI.getLLVMRegNum(RegNum: Inst2.getRegister(), isEH: 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(Reg: Reg1);
686 Reg2 = getXRegFromWReg(Reg: 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(Reg: Reg1);
705 Reg2 = getDRegFromBReg(Reg: 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 }
729 case MCCFIInstruction::OpLLVMSetRAState:
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
784MCAsmBackend *llvm::createAArch64leAsmBackend(const Target &T,
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
798 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType: TheTriple.getOS());
799 bool IsILP32 = STI.getTargetTriple().getEnvironment() == Triple::GNUILP32;
800 return new ELFAArch64AsmBackend(T, TheTriple, OSABI, /*IsLittleEndian=*/true,
801 IsILP32);
802}
803
804MCAsmBackend *llvm::createAArch64beAsmBackend(const Target &T,
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!");
811 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType: TheTriple.getOS());
812 bool IsILP32 = STI.getTargetTriple().getEnvironment() == Triple::GNUILP32;
813 return new ELFAArch64AsmBackend(T, TheTriple, OSABI, /*IsLittleEndian=*/false,
814 IsILP32);
815}
816