LLVM 19.0.0git
ARMELFStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
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// This file assembles .s files and emits ARM ELF .o object files. Different
10// from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
11// delimit regions of data and code.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARMMCTargetDesc.h"
16#include "ARMUnwindOpAsm.h"
17#include "Utils/ARMBaseInfo.h"
18#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCAssembler.h"
29#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCFixup.h"
33#include "llvm/MC/MCFragment.h"
34#include "llvm/MC/MCInst.h"
38#include "llvm/MC/MCSection.h"
40#include "llvm/MC/MCStreamer.h"
42#include "llvm/MC/MCSymbol.h"
43#include "llvm/MC/MCSymbolELF.h"
44#include "llvm/MC/SectionKind.h"
51#include <algorithm>
52#include <cassert>
53#include <climits>
54#include <cstddef>
55#include <cstdint>
56#include <string>
57
58using namespace llvm;
59
60static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
62 "Invalid personality index");
63 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
64}
65
66namespace {
67
68class ARMELFStreamer;
69
70class ARMTargetAsmStreamer : public ARMTargetStreamer {
72 MCInstPrinter &InstPrinter;
73 bool IsVerboseAsm;
74
75 void emitFnStart() override;
76 void emitFnEnd() override;
77 void emitCantUnwind() override;
78 void emitPersonality(const MCSymbol *Personality) override;
79 void emitPersonalityIndex(unsigned Index) override;
80 void emitHandlerData() override;
81 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
82 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
83 void emitPad(int64_t Offset) override;
84 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
85 bool isVector) override;
86 void emitUnwindRaw(int64_t Offset,
87 const SmallVectorImpl<uint8_t> &Opcodes) override;
88
89 void switchVendor(StringRef Vendor) override;
90 void emitAttribute(unsigned Attribute, unsigned Value) override;
91 void emitTextAttribute(unsigned Attribute, StringRef String) override;
92 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
93 StringRef StringValue) override;
94 void emitArch(ARM::ArchKind Arch) override;
95 void emitArchExtension(uint64_t ArchExt) override;
96 void emitObjectArch(ARM::ArchKind Arch) override;
97 void emitFPU(ARM::FPUKind FPU) override;
98 void emitInst(uint32_t Inst, char Suffix = '\0') override;
99 void finishAttributeSection() override;
100
101 void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
102 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
103
104 void emitARMWinCFIAllocStack(unsigned Size, bool Wide) override;
105 void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide) override;
106 void emitARMWinCFISaveSP(unsigned Reg) override;
107 void emitARMWinCFISaveFRegs(unsigned First, unsigned Last) override;
108 void emitARMWinCFISaveLR(unsigned Offset) override;
109 void emitARMWinCFIPrologEnd(bool Fragment) override;
110 void emitARMWinCFINop(bool Wide) override;
111 void emitARMWinCFIEpilogStart(unsigned Condition) override;
112 void emitARMWinCFIEpilogEnd() override;
113 void emitARMWinCFICustom(unsigned Opcode) override;
114
115public:
116 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
117 MCInstPrinter &InstPrinter, bool VerboseAsm);
118};
119
120ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
122 MCInstPrinter &InstPrinter,
123 bool VerboseAsm)
124 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
125 IsVerboseAsm(VerboseAsm) {}
126
127void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
128void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
129void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
130
131void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
132 OS << "\t.personality " << Personality->getName() << '\n';
133}
134
135void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
136 OS << "\t.personalityindex " << Index << '\n';
137}
138
139void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
140
141void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
142 int64_t Offset) {
143 OS << "\t.setfp\t";
144 InstPrinter.printRegName(OS, FpReg);
145 OS << ", ";
146 InstPrinter.printRegName(OS, SpReg);
147 if (Offset)
148 OS << ", #" << Offset;
149 OS << '\n';
150}
151
152void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
153 assert((Reg != ARM::SP && Reg != ARM::PC) &&
154 "the operand of .movsp cannot be either sp or pc");
155
156 OS << "\t.movsp\t";
157 InstPrinter.printRegName(OS, Reg);
158 if (Offset)
159 OS << ", #" << Offset;
160 OS << '\n';
161}
162
163void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
164 OS << "\t.pad\t#" << Offset << '\n';
165}
166
167void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
168 bool isVector) {
169 assert(RegList.size() && "RegList should not be empty");
170 if (isVector)
171 OS << "\t.vsave\t{";
172 else
173 OS << "\t.save\t{";
174
175 InstPrinter.printRegName(OS, RegList[0]);
176
177 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
178 OS << ", ";
179 InstPrinter.printRegName(OS, RegList[i]);
180 }
181
182 OS << "}\n";
183}
184
185void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
186
187void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
188 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
189 if (IsVerboseAsm) {
192 if (!Name.empty())
193 OS << "\t@ " << Name;
194 }
195 OS << "\n";
196}
197
198void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
200 switch (Attribute) {
202 OS << "\t.cpu\t" << String.lower();
203 break;
204 default:
205 OS << "\t.eabi_attribute\t" << Attribute << ", \"";
208 else
209 OS << String;
210 OS << "\"";
211 if (IsVerboseAsm) {
214 if (!Name.empty())
215 OS << "\t@ " << Name;
216 }
217 break;
218 }
219 OS << "\n";
220}
221
222void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
223 unsigned IntValue,
224 StringRef StringValue) {
225 switch (Attribute) {
226 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
228 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
229 if (!StringValue.empty())
230 OS << ", \"" << StringValue << "\"";
231 if (IsVerboseAsm)
232 OS << "\t@ "
235 break;
236 }
237 OS << "\n";
238}
239
240void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
241 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
242}
243
244void ARMTargetAsmStreamer::emitArchExtension(uint64_t ArchExt) {
245 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
246}
247
248void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
249 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
250}
251
252void ARMTargetAsmStreamer::emitFPU(ARM::FPUKind FPU) {
253 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
254}
255
256void ARMTargetAsmStreamer::finishAttributeSection() {}
257
258void ARMTargetAsmStreamer::annotateTLSDescriptorSequence(
259 const MCSymbolRefExpr *S) {
260 OS << "\t.tlsdescseq\t" << S->getSymbol().getName() << "\n";
261}
262
263void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
264 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
265
266 OS << "\t.thumb_set\t";
267 Symbol->print(OS, MAI);
268 OS << ", ";
269 Value->print(OS, MAI);
270 OS << '\n';
271}
272
273void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
274 OS << "\t.inst";
275 if (Suffix)
276 OS << "." << Suffix;
277 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
278}
279
280void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
281 const SmallVectorImpl<uint8_t> &Opcodes) {
282 OS << "\t.unwind_raw " << Offset;
283 for (uint8_t Opcode : Opcodes)
284 OS << ", 0x" << Twine::utohexstr(Opcode);
285 OS << '\n';
286}
287
288void ARMTargetAsmStreamer::emitARMWinCFIAllocStack(unsigned Size, bool Wide) {
289 if (Wide)
290 OS << "\t.seh_stackalloc_w\t" << Size << "\n";
291 else
292 OS << "\t.seh_stackalloc\t" << Size << "\n";
293}
294
295static void printRegs(formatted_raw_ostream &OS, ListSeparator &LS, int First,
296 int Last) {
297 if (First != Last)
298 OS << LS << "r" << First << "-r" << Last;
299 else
300 OS << LS << "r" << First;
301}
302
303void ARMTargetAsmStreamer::emitARMWinCFISaveRegMask(unsigned Mask, bool Wide) {
304 if (Wide)
305 OS << "\t.seh_save_regs_w\t";
306 else
307 OS << "\t.seh_save_regs\t";
308 ListSeparator LS;
309 int First = -1;
310 OS << "{";
311 for (int I = 0; I <= 12; I++) {
312 if (Mask & (1 << I)) {
313 if (First < 0)
314 First = I;
315 } else {
316 if (First >= 0) {
317 printRegs(OS, LS, First, I - 1);
318 First = -1;
319 }
320 }
321 }
322 if (First >= 0)
323 printRegs(OS, LS, First, 12);
324 if (Mask & (1 << 14))
325 OS << LS << "lr";
326 OS << "}\n";
327}
328
329void ARMTargetAsmStreamer::emitARMWinCFISaveSP(unsigned Reg) {
330 OS << "\t.seh_save_sp\tr" << Reg << "\n";
331}
332
333void ARMTargetAsmStreamer::emitARMWinCFISaveFRegs(unsigned First,
334 unsigned Last) {
335 if (First != Last)
336 OS << "\t.seh_save_fregs\t{d" << First << "-d" << Last << "}\n";
337 else
338 OS << "\t.seh_save_fregs\t{d" << First << "}\n";
339}
340
341void ARMTargetAsmStreamer::emitARMWinCFISaveLR(unsigned Offset) {
342 OS << "\t.seh_save_lr\t" << Offset << "\n";
343}
344
345void ARMTargetAsmStreamer::emitARMWinCFIPrologEnd(bool Fragment) {
346 if (Fragment)
347 OS << "\t.seh_endprologue_fragment\n";
348 else
349 OS << "\t.seh_endprologue\n";
350}
351
352void ARMTargetAsmStreamer::emitARMWinCFINop(bool Wide) {
353 if (Wide)
354 OS << "\t.seh_nop_w\n";
355 else
356 OS << "\t.seh_nop\n";
357}
358
359void ARMTargetAsmStreamer::emitARMWinCFIEpilogStart(unsigned Condition) {
360 if (Condition == ARMCC::AL)
361 OS << "\t.seh_startepilogue\n";
362 else
363 OS << "\t.seh_startepilogue_cond\t"
364 << ARMCondCodeToString(static_cast<ARMCC::CondCodes>(Condition)) << "\n";
365}
366
367void ARMTargetAsmStreamer::emitARMWinCFIEpilogEnd() {
368 OS << "\t.seh_endepilogue\n";
369}
370
371void ARMTargetAsmStreamer::emitARMWinCFICustom(unsigned Opcode) {
372 int I;
373 for (I = 3; I > 0; I--)
374 if (Opcode & (0xffu << (8 * I)))
375 break;
376 ListSeparator LS;
377 OS << "\t.seh_custom\t";
378 for (; I >= 0; I--)
379 OS << LS << ((Opcode >> (8 * I)) & 0xff);
380 OS << "\n";
381}
382
383class ARMTargetELFStreamer : public ARMTargetStreamer {
384private:
385 StringRef CurrentVendor;
386 ARM::FPUKind FPU = ARM::FK_INVALID;
387 ARM::ArchKind Arch = ARM::ArchKind::INVALID;
388 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
389
390 MCSection *AttributeSection = nullptr;
391
392 void emitArchDefaultAttributes();
393 void emitFPUDefaultAttributes();
394
395 ARMELFStreamer &getStreamer();
396
397 void emitFnStart() override;
398 void emitFnEnd() override;
399 void emitCantUnwind() override;
400 void emitPersonality(const MCSymbol *Personality) override;
401 void emitPersonalityIndex(unsigned Index) override;
402 void emitHandlerData() override;
403 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
404 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
405 void emitPad(int64_t Offset) override;
406 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
407 bool isVector) override;
408 void emitUnwindRaw(int64_t Offset,
409 const SmallVectorImpl<uint8_t> &Opcodes) override;
410
411 void switchVendor(StringRef Vendor) override;
412 void emitAttribute(unsigned Attribute, unsigned Value) override;
413 void emitTextAttribute(unsigned Attribute, StringRef String) override;
414 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
415 StringRef StringValue) override;
416 void emitArch(ARM::ArchKind Arch) override;
417 void emitObjectArch(ARM::ArchKind Arch) override;
418 void emitFPU(ARM::FPUKind FPU) override;
419 void emitInst(uint32_t Inst, char Suffix = '\0') override;
420 void finishAttributeSection() override;
421 void emitLabel(MCSymbol *Symbol) override;
422
423 void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
424 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
425
426 // Reset state between object emissions
427 void reset() override;
428
429 void finish() override;
430
431public:
432 ARMTargetELFStreamer(MCStreamer &S)
433 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
434};
435
436/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
437/// the appropriate points in the object files. These symbols are defined in the
438/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
439///
440/// In brief: $a, $t or $d should be emitted at the start of each contiguous
441/// region of ARM code, Thumb code or data in a section. In practice, this
442/// emission does not rely on explicit assembler directives but on inherent
443/// properties of the directives doing the emission (e.g. ".byte" is data, "add
444/// r0, r0, r0" an instruction).
445///
446/// As a result this system is orthogonal to the DataRegion infrastructure used
447/// by MachO. Beware!
448class ARMELFStreamer : public MCELFStreamer {
449public:
450 friend class ARMTargetELFStreamer;
451
452 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
453 std::unique_ptr<MCObjectWriter> OW,
454 std::unique_ptr<MCCodeEmitter> Emitter, bool IsThumb,
455 bool IsAndroid)
456 : MCELFStreamer(Context, std::move(TAB), std::move(OW),
457 std::move(Emitter)),
458 IsThumb(IsThumb), IsAndroid(IsAndroid) {
459 EHReset();
460 }
461
462 ~ARMELFStreamer() override = default;
463
464 // ARM exception handling directives
465 void emitFnStart();
466 void emitFnEnd();
467 void emitCantUnwind();
468 void emitPersonality(const MCSymbol *Per);
469 void emitPersonalityIndex(unsigned index);
470 void emitHandlerData();
471 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
472 void emitMovSP(unsigned Reg, int64_t Offset = 0);
473 void emitPad(int64_t Offset);
474 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
475 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
476 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
477 SMLoc Loc) override {
478 emitDataMappingSymbol();
479 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
480 }
481
482 void changeSection(MCSection *Section, uint32_t Subsection) override {
483 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
484 MCELFStreamer::changeSection(Section, Subsection);
485 auto LastMappingSymbol = LastMappingSymbols.find(Section);
486 if (LastMappingSymbol != LastMappingSymbols.end()) {
487 LastEMSInfo = std::move(LastMappingSymbol->second);
488 return;
489 }
490 LastEMSInfo.reset(new ElfMappingSymbolInfo);
491 }
492
493 /// This function is the one used to emit instruction data into the ELF
494 /// streamer. We override it to add the appropriate mapping symbol if
495 /// necessary.
496 void emitInstruction(const MCInst &Inst,
497 const MCSubtargetInfo &STI) override {
498 if (IsThumb)
499 EmitThumbMappingSymbol();
500 else
501 EmitARMMappingSymbol();
502
504 }
505
506 void emitInst(uint32_t Inst, char Suffix) {
507 unsigned Size;
508 char Buffer[4];
509 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
510
511 switch (Suffix) {
512 case '\0':
513 Size = 4;
514
515 assert(!IsThumb);
516 EmitARMMappingSymbol();
517 for (unsigned II = 0, IE = Size; II != IE; II++) {
518 const unsigned I = LittleEndian ? (Size - II - 1) : II;
519 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
520 }
521
522 break;
523 case 'n':
524 case 'w':
525 Size = (Suffix == 'n' ? 2 : 4);
526
527 assert(IsThumb);
528 EmitThumbMappingSymbol();
529 // Thumb wide instructions are emitted as a pair of 16-bit words of the
530 // appropriate endianness.
531 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
532 const unsigned I0 = LittleEndian ? II + 0 : II + 1;
533 const unsigned I1 = LittleEndian ? II + 1 : II + 0;
534 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
535 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
536 }
537
538 break;
539 default:
540 llvm_unreachable("Invalid Suffix");
541 }
542
544 }
545
546 /// This is one of the functions used to emit data into an ELF section, so the
547 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
548 /// necessary.
549 void emitBytes(StringRef Data) override {
550 emitDataMappingSymbol();
552 }
553
554 void FlushPendingMappingSymbol() {
555 if (!LastEMSInfo->hasInfo())
556 return;
557 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
558 emitMappingSymbol("$d", *EMS->F, EMS->Offset);
559 EMS->resetInfo();
560 }
561
562 /// This is one of the functions used to emit data into an ELF section, so the
563 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
564 /// necessary.
565 void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
566 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
567 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
568 getContext().reportError(Loc, "relocated expression must be 32-bit");
569 return;
570 }
572 }
573
574 emitDataMappingSymbol();
576 }
577
578 void emitAssemblerFlag(MCAssemblerFlag Flag) override {
580
581 switch (Flag) {
583 return; // no-op here.
584 case MCAF_Code16:
585 IsThumb = true;
586 return; // Change to Thumb mode
587 case MCAF_Code32:
588 IsThumb = false;
589 return; // Change to ARM mode
590 case MCAF_Code64:
591 return;
593 return;
594 }
595 }
596
597 /// If a label is defined before the .type directive sets the label's type
598 /// then the label can't be recorded as thumb function when the label is
599 /// defined. We override emitSymbolAttribute() which is called as part of the
600 /// parsing of .type so that if the symbol has already been defined we can
601 /// record the label as Thumb. FIXME: there is a corner case where the state
602 /// is changed in between the label definition and the .type directive, this
603 /// is not expected to occur in practice and handling it would require the
604 /// backend to track IsThumb for every label.
605 bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
607
608 if (!IsThumb)
609 return Val;
610
611 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
612 if ((Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC) &&
613 Symbol->isDefined())
615
616 return Val;
617 };
618
619private:
620 enum ElfMappingSymbol {
621 EMS_None,
622 EMS_ARM,
623 EMS_Thumb,
624 EMS_Data
625 };
626
627 struct ElfMappingSymbolInfo {
628 void resetInfo() {
629 F = nullptr;
630 Offset = 0;
631 }
632 bool hasInfo() { return F != nullptr; }
633 MCDataFragment *F = nullptr;
634 uint64_t Offset = 0;
635 ElfMappingSymbol State = EMS_None;
636 };
637
638 void emitDataMappingSymbol() {
639 if (LastEMSInfo->State == EMS_Data)
640 return;
641 else if (LastEMSInfo->State == EMS_None) {
642 // This is a tentative symbol, it won't really be emitted until it's
643 // actually needed.
644 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
645 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
646 if (!DF)
647 return;
648 EMS->F = DF;
649 EMS->Offset = DF->getContents().size();
650 LastEMSInfo->State = EMS_Data;
651 return;
652 }
653 EmitMappingSymbol("$d");
654 LastEMSInfo->State = EMS_Data;
655 }
656
657 void EmitThumbMappingSymbol() {
658 if (LastEMSInfo->State == EMS_Thumb)
659 return;
660 FlushPendingMappingSymbol();
661 EmitMappingSymbol("$t");
662 LastEMSInfo->State = EMS_Thumb;
663 }
664
665 void EmitARMMappingSymbol() {
666 if (LastEMSInfo->State == EMS_ARM)
667 return;
668 FlushPendingMappingSymbol();
669 EmitMappingSymbol("$a");
670 LastEMSInfo->State = EMS_ARM;
671 }
672
673 void EmitMappingSymbol(StringRef Name) {
674 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
675 Name + "." + Twine(MappingSymbolCounter++)));
676 emitLabel(Symbol);
677
678 Symbol->setType(ELF::STT_NOTYPE);
679 Symbol->setBinding(ELF::STB_LOCAL);
680 }
681
682 void emitMappingSymbol(StringRef Name, MCDataFragment &F, uint64_t Offset) {
683 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
684 Name + "." + Twine(MappingSymbolCounter++)));
685 emitLabelAtPos(Symbol, SMLoc(), F, Offset);
686 Symbol->setType(ELF::STT_NOTYPE);
687 Symbol->setBinding(ELF::STB_LOCAL);
688 }
689
690 void emitThumbFunc(MCSymbol *Func) override {
693 }
694
695 // Helper functions for ARM exception handling directives
696 void EHReset();
697
698 // Reset state between object emissions
699 void reset() override;
700
701 void EmitPersonalityFixup(StringRef Name);
702 void FlushPendingOffset();
703 void FlushUnwindOpcodes(bool NoHandlerData);
704
705 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
706 SectionKind Kind, const MCSymbol &Fn);
707 void SwitchToExTabSection(const MCSymbol &FnStart);
708 void SwitchToExIdxSection(const MCSymbol &FnStart);
709
710 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
711
712 bool IsThumb;
713 bool IsAndroid;
714 int64_t MappingSymbolCounter = 0;
715
717 LastMappingSymbols;
718
719 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
720
721 // ARM Exception Handling Frame Information
722 MCSymbol *ExTab;
723 MCSymbol *FnStart;
724 const MCSymbol *Personality;
725 unsigned PersonalityIndex;
726 unsigned FPReg; // Frame pointer register
727 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
728 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
729 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
730 bool UsedFP;
731 bool CantUnwind;
733 UnwindOpcodeAssembler UnwindOpAsm;
734};
735
736} // end anonymous namespace
737
738ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
739 return static_cast<ARMELFStreamer &>(Streamer);
740}
741
742void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
743void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
744void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
745
746void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
747 getStreamer().emitPersonality(Personality);
748}
749
750void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
751 getStreamer().emitPersonalityIndex(Index);
752}
753
754void ARMTargetELFStreamer::emitHandlerData() {
755 getStreamer().emitHandlerData();
756}
757
758void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
759 int64_t Offset) {
760 getStreamer().emitSetFP(FpReg, SpReg, Offset);
761}
762
763void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
764 getStreamer().emitMovSP(Reg, Offset);
765}
766
767void ARMTargetELFStreamer::emitPad(int64_t Offset) {
768 getStreamer().emitPad(Offset);
769}
770
771void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
772 bool isVector) {
773 getStreamer().emitRegSave(RegList, isVector);
774}
775
776void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
777 const SmallVectorImpl<uint8_t> &Opcodes) {
778 getStreamer().emitUnwindRaw(Offset, Opcodes);
779}
780
781void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
782 assert(!Vendor.empty() && "Vendor cannot be empty.");
783
784 if (CurrentVendor == Vendor)
785 return;
786
787 if (!CurrentVendor.empty())
788 finishAttributeSection();
789
790 assert(getStreamer().Contents.empty() &&
791 ".ARM.attributes should be flushed before changing vendor");
792 CurrentVendor = Vendor;
793
794}
795
796void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
797 getStreamer().setAttributeItem(Attribute, Value,
798 /* OverwriteExisting= */ true);
799}
800
801void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
803 getStreamer().setAttributeItem(Attribute, Value,
804 /* OverwriteExisting= */ true);
805}
806
807void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
808 unsigned IntValue,
809 StringRef StringValue) {
810 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
811 /* OverwriteExisting= */ true);
812}
813
814void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
815 Arch = Value;
816}
817
818void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
819 EmittedArch = Value;
820}
821
822void ARMTargetELFStreamer::emitArchDefaultAttributes() {
823 using namespace ARMBuildAttrs;
824 ARMELFStreamer &S = getStreamer();
825
826 S.setAttributeItem(CPU_name, ARM::getCPUAttr(Arch), false);
827
828 if (EmittedArch == ARM::ArchKind::INVALID)
829 S.setAttributeItem(CPU_arch, ARM::getArchAttr(Arch), false);
830 else
831 S.setAttributeItem(CPU_arch, ARM::getArchAttr(EmittedArch), false);
832
833 switch (Arch) {
834 case ARM::ArchKind::ARMV4:
835 S.setAttributeItem(ARM_ISA_use, Allowed, false);
836 break;
837
838 case ARM::ArchKind::ARMV4T:
839 case ARM::ArchKind::ARMV5T:
840 case ARM::ArchKind::XSCALE:
841 case ARM::ArchKind::ARMV5TE:
842 case ARM::ArchKind::ARMV6:
843 S.setAttributeItem(ARM_ISA_use, Allowed, false);
844 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
845 break;
846
847 case ARM::ArchKind::ARMV6T2:
848 S.setAttributeItem(ARM_ISA_use, Allowed, false);
849 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
850 break;
851
852 case ARM::ArchKind::ARMV6K:
853 case ARM::ArchKind::ARMV6KZ:
854 S.setAttributeItem(ARM_ISA_use, Allowed, false);
855 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
856 S.setAttributeItem(Virtualization_use, AllowTZ, false);
857 break;
858
859 case ARM::ArchKind::ARMV6M:
860 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
861 break;
862
863 case ARM::ArchKind::ARMV7A:
864 S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
865 S.setAttributeItem(ARM_ISA_use, Allowed, false);
866 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
867 break;
868
869 case ARM::ArchKind::ARMV7R:
870 S.setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
871 S.setAttributeItem(ARM_ISA_use, Allowed, false);
872 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
873 break;
874
875 case ARM::ArchKind::ARMV7EM:
876 case ARM::ArchKind::ARMV7M:
877 S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
878 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
879 break;
880
881 case ARM::ArchKind::ARMV8A:
882 case ARM::ArchKind::ARMV8_1A:
883 case ARM::ArchKind::ARMV8_2A:
884 case ARM::ArchKind::ARMV8_3A:
885 case ARM::ArchKind::ARMV8_4A:
886 case ARM::ArchKind::ARMV8_5A:
887 case ARM::ArchKind::ARMV8_6A:
888 case ARM::ArchKind::ARMV8_7A:
889 case ARM::ArchKind::ARMV8_8A:
890 case ARM::ArchKind::ARMV8_9A:
891 case ARM::ArchKind::ARMV9A:
892 case ARM::ArchKind::ARMV9_1A:
893 case ARM::ArchKind::ARMV9_2A:
894 case ARM::ArchKind::ARMV9_3A:
895 case ARM::ArchKind::ARMV9_4A:
896 case ARM::ArchKind::ARMV9_5A:
897 S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
898 S.setAttributeItem(ARM_ISA_use, Allowed, false);
899 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
900 S.setAttributeItem(MPextension_use, Allowed, false);
901 S.setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
902 break;
903
904 case ARM::ArchKind::ARMV8MBaseline:
905 case ARM::ArchKind::ARMV8MMainline:
906 S.setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
907 S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
908 break;
909
910 case ARM::ArchKind::IWMMXT:
911 S.setAttributeItem(ARM_ISA_use, Allowed, false);
912 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
913 S.setAttributeItem(WMMX_arch, AllowWMMXv1, false);
914 break;
915
916 case ARM::ArchKind::IWMMXT2:
917 S.setAttributeItem(ARM_ISA_use, Allowed, false);
918 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
919 S.setAttributeItem(WMMX_arch, AllowWMMXv2, false);
920 break;
921
922 default:
923 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
924 break;
925 }
926}
927
928void ARMTargetELFStreamer::emitFPU(ARM::FPUKind Value) { FPU = Value; }
929
930void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
931 ARMELFStreamer &S = getStreamer();
932
933 switch (FPU) {
934 case ARM::FK_VFP:
935 case ARM::FK_VFPV2:
937 /* OverwriteExisting= */ false);
938 break;
939
940 case ARM::FK_VFPV3:
942 /* OverwriteExisting= */ false);
943 break;
944
945 case ARM::FK_VFPV3_FP16:
947 /* OverwriteExisting= */ false);
949 /* OverwriteExisting= */ false);
950 break;
951
952 case ARM::FK_VFPV3_D16:
954 /* OverwriteExisting= */ false);
955 break;
956
957 case ARM::FK_VFPV3_D16_FP16:
959 /* OverwriteExisting= */ false);
961 /* OverwriteExisting= */ false);
962 break;
963
964 case ARM::FK_VFPV3XD:
966 /* OverwriteExisting= */ false);
967 break;
968 case ARM::FK_VFPV3XD_FP16:
970 /* OverwriteExisting= */ false);
972 /* OverwriteExisting= */ false);
973 break;
974
975 case ARM::FK_VFPV4:
977 /* OverwriteExisting= */ false);
978 break;
979
980 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
981 // as _D16 here.
982 case ARM::FK_FPV4_SP_D16:
983 case ARM::FK_VFPV4_D16:
985 /* OverwriteExisting= */ false);
986 break;
987
988 case ARM::FK_FP_ARMV8:
990 /* OverwriteExisting= */ false);
991 break;
992
993 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
994 // uses the FP_ARMV8_D16 build attribute.
995 case ARM::FK_FPV5_SP_D16:
996 case ARM::FK_FPV5_D16:
998 /* OverwriteExisting= */ false);
999 break;
1000
1001 case ARM::FK_NEON:
1003 /* OverwriteExisting= */ false);
1004 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1006 /* OverwriteExisting= */ false);
1007 break;
1008
1009 case ARM::FK_NEON_FP16:
1011 /* OverwriteExisting= */ false);
1012 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1014 /* OverwriteExisting= */ false);
1016 /* OverwriteExisting= */ false);
1017 break;
1018
1019 case ARM::FK_NEON_VFPV4:
1021 /* OverwriteExisting= */ false);
1022 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1024 /* OverwriteExisting= */ false);
1025 break;
1026
1027 case ARM::FK_NEON_FP_ARMV8:
1028 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1030 /* OverwriteExisting= */ false);
1031 // 'Advanced_SIMD_arch' must be emitted not here, but within
1032 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1033 break;
1034
1035 case ARM::FK_SOFTVFP:
1036 case ARM::FK_NONE:
1037 break;
1038
1039 default:
1040 report_fatal_error("Unknown FPU: " + Twine(FPU));
1041 break;
1042 }
1043}
1044
1045void ARMTargetELFStreamer::finishAttributeSection() {
1046 ARMELFStreamer &S = getStreamer();
1047
1048 if (FPU != ARM::FK_INVALID)
1049 emitFPUDefaultAttributes();
1050
1051 if (Arch != ARM::ArchKind::INVALID)
1052 emitArchDefaultAttributes();
1053
1054 if (S.Contents.empty())
1055 return;
1056
1057 auto LessTag = [](const MCELFStreamer::AttributeItem &LHS,
1058 const MCELFStreamer::AttributeItem &RHS) -> bool {
1059 // The conformance tag must be emitted first when serialised into an
1060 // object file. Specifically, the addenda to the ARM ABI states that
1061 // (2.3.7.4):
1062 //
1063 // "To simplify recognition by consumers in the common case of claiming
1064 // conformity for the whole file, this tag should be emitted first in a
1065 // file-scope sub-subsection of the first public subsection of the
1066 // attributes section."
1067 //
1068 // So it is special-cased in this comparison predicate when the
1069 // attributes are sorted in finishAttributeSection().
1070 return (RHS.Tag != ARMBuildAttrs::conformance) &&
1071 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
1072 };
1073 llvm::sort(S.Contents, LessTag);
1074
1075 S.emitAttributesSection(CurrentVendor, ".ARM.attributes",
1076 ELF::SHT_ARM_ATTRIBUTES, AttributeSection);
1077
1078 FPU = ARM::FK_INVALID;
1079}
1080
1081void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1082 ARMELFStreamer &Streamer = getStreamer();
1083 if (!Streamer.IsThumb)
1084 return;
1085
1086 Streamer.getAssembler().registerSymbol(*Symbol);
1087 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1089 Streamer.emitThumbFunc(Symbol);
1090}
1091
1092void ARMTargetELFStreamer::annotateTLSDescriptorSequence(
1093 const MCSymbolRefExpr *S) {
1094 getStreamer().EmitFixup(S, FK_Data_4);
1095}
1096
1097void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1098 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1099 const MCSymbol &Sym = SRE->getSymbol();
1100 if (!Sym.isDefined()) {
1101 getStreamer().emitAssignment(Symbol, Value);
1102 return;
1103 }
1104 }
1105
1106 getStreamer().emitThumbFunc(Symbol);
1107 getStreamer().emitAssignment(Symbol, Value);
1108}
1109
1110void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1111 getStreamer().emitInst(Inst, Suffix);
1112}
1113
1114void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1115
1116void ARMTargetELFStreamer::finish() {
1117 ARMTargetStreamer::finish();
1118 finishAttributeSection();
1119}
1120
1121void ARMELFStreamer::reset() {
1122 MCTargetStreamer &TS = *getTargetStreamer();
1123 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1124 ATS.reset();
1125 MappingSymbolCounter = 0;
1127 LastMappingSymbols.clear();
1128 LastEMSInfo.reset();
1129 // MCELFStreamer clear's the assembler's e_flags. However, for
1130 // arm we manually set the ABI version on streamer creation, so
1131 // do the same here
1132 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1133}
1134
1135inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1136 unsigned Type,
1137 unsigned Flags,
1138 SectionKind Kind,
1139 const MCSymbol &Fn) {
1140 const MCSectionELF &FnSection =
1141 static_cast<const MCSectionELF &>(Fn.getSection());
1142
1143 // Create the name for new section
1144 StringRef FnSecName(FnSection.getName());
1145 SmallString<128> EHSecName(Prefix);
1146 if (FnSecName != ".text") {
1147 EHSecName += FnSecName;
1148 }
1149
1150 // Get .ARM.extab or .ARM.exidx section
1151 const MCSymbolELF *Group = FnSection.getGroup();
1152 if (Group)
1154 MCSectionELF *EHSection = getContext().getELFSection(
1155 EHSecName, Type, Flags, 0, Group, /*IsComdat=*/true,
1156 FnSection.getUniqueID(),
1157 static_cast<const MCSymbolELF *>(FnSection.getBeginSymbol()));
1158
1159 assert(EHSection && "Failed to get the required EH section");
1160
1161 // Switch to .ARM.extab or .ARM.exidx section
1162 switchSection(EHSection);
1163 emitValueToAlignment(Align(4), 0, 1, 0);
1164}
1165
1166inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1167 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1168 SectionKind::getData(), FnStart);
1169}
1170
1171inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1172 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1174 SectionKind::getData(), FnStart);
1175}
1176
1177void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1178 MCDataFragment *Frag = getOrCreateDataFragment();
1179 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1180 Kind));
1181}
1182
1183void ARMELFStreamer::EHReset() {
1184 ExTab = nullptr;
1185 FnStart = nullptr;
1186 Personality = nullptr;
1187 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1188 FPReg = ARM::SP;
1189 FPOffset = 0;
1190 SPOffset = 0;
1191 PendingOffset = 0;
1192 UsedFP = false;
1193 CantUnwind = false;
1194
1195 Opcodes.clear();
1196 UnwindOpAsm.Reset();
1197}
1198
1199void ARMELFStreamer::emitFnStart() {
1200 assert(FnStart == nullptr);
1201 FnStart = getContext().createTempSymbol();
1202 emitLabel(FnStart);
1203}
1204
1205void ARMELFStreamer::emitFnEnd() {
1206 assert(FnStart && ".fnstart must precedes .fnend");
1207
1208 // Emit unwind opcodes if there is no .handlerdata directive
1209 if (!ExTab && !CantUnwind)
1210 FlushUnwindOpcodes(true);
1211
1212 // Emit the exception index table entry
1213 SwitchToExIdxSection(*FnStart);
1214
1215 // The EHABI requires a dependency preserving R_ARM_NONE relocation to the
1216 // personality routine to protect it from an arbitrary platform's static
1217 // linker garbage collection. We disable this for Android where the unwinder
1218 // is either dynamically linked or directly references the personality
1219 // routine.
1220 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX && !IsAndroid)
1221 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1222
1223 const MCSymbolRefExpr *FnStartRef =
1226 getContext());
1227
1228 emitValue(FnStartRef, 4);
1229
1230 if (CantUnwind) {
1232 } else if (ExTab) {
1233 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1234 const MCSymbolRefExpr *ExTabEntryRef =
1237 getContext());
1238 emitValue(ExTabEntryRef, 4);
1239 } else {
1240 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1241 // the second word of exception index table entry. The size of the unwind
1242 // opcodes should always be 4 bytes.
1243 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1244 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1245 assert(Opcodes.size() == 4u &&
1246 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1247 uint64_t Intval = Opcodes[0] |
1248 Opcodes[1] << 8 |
1249 Opcodes[2] << 16 |
1250 Opcodes[3] << 24;
1251 emitIntValue(Intval, Opcodes.size());
1252 }
1253
1254 // Switch to the section containing FnStart
1255 switchSection(&FnStart->getSection());
1256
1257 // Clean exception handling frame information
1258 EHReset();
1259}
1260
1261void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1262
1263// Add the R_ARM_NONE fixup at the same position
1264void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1265 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1266
1267 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1268 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1269
1270 visitUsedExpr(*PersonalityRef);
1271 MCDataFragment *DF = getOrCreateDataFragment();
1272 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1273 PersonalityRef,
1274 MCFixup::getKindForSize(4, false)));
1275}
1276
1277void ARMELFStreamer::FlushPendingOffset() {
1278 if (PendingOffset != 0) {
1279 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1280 PendingOffset = 0;
1281 }
1282}
1283
1284void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1285 // Emit the unwind opcode to restore $sp.
1286 if (UsedFP) {
1287 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1288 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1289 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1290 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1291 } else {
1292 FlushPendingOffset();
1293 }
1294
1295 // Finalize the unwind opcode sequence
1296 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1297
1298 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1299 // section. Thus, we don't have to create an entry in the .ARM.extab
1300 // section.
1301 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1302 return;
1303
1304 // Switch to .ARM.extab section.
1305 SwitchToExTabSection(*FnStart);
1306
1307 // Create .ARM.extab label for offset in .ARM.exidx
1308 assert(!ExTab);
1309 ExTab = getContext().createTempSymbol();
1310 emitLabel(ExTab);
1311
1312 // Emit personality
1313 if (Personality) {
1314 const MCSymbolRefExpr *PersonalityRef =
1315 MCSymbolRefExpr::create(Personality,
1317 getContext());
1318
1319 emitValue(PersonalityRef, 4);
1320 }
1321
1322 // Emit unwind opcodes
1323 assert((Opcodes.size() % 4) == 0 &&
1324 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1325 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1326 uint64_t Intval = Opcodes[I] |
1327 Opcodes[I + 1] << 8 |
1328 Opcodes[I + 2] << 16 |
1329 Opcodes[I + 3] << 24;
1330 emitInt32(Intval);
1331 }
1332
1333 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1334 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1335 // after the unwind opcodes. The handler data consists of several 32-bit
1336 // words, and should be terminated by zero.
1337 //
1338 // In case that the .handlerdata directive is not specified by the
1339 // programmer, we should emit zero to terminate the handler data.
1340 if (NoHandlerData && !Personality)
1341 emitInt32(0);
1342}
1343
1344void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1345
1346void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1347 Personality = Per;
1348 UnwindOpAsm.setPersonality(Per);
1349}
1350
1351void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1352 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1353 PersonalityIndex = Index;
1354}
1355
1356void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1357 int64_t Offset) {
1358 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1359 "the operand of .setfp directive should be either $sp or $fp");
1360
1361 UsedFP = true;
1362 FPReg = NewFPReg;
1363
1364 if (NewSPReg == ARM::SP)
1365 FPOffset = SPOffset + Offset;
1366 else
1367 FPOffset += Offset;
1368}
1369
1370void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1371 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1372 "the operand of .movsp cannot be either sp or pc");
1373 assert(FPReg == ARM::SP && "current FP must be SP");
1374
1375 FlushPendingOffset();
1376
1377 FPReg = Reg;
1378 FPOffset = SPOffset + Offset;
1379
1380 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1381 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1382}
1383
1384void ARMELFStreamer::emitPad(int64_t Offset) {
1385 // Track the change of the $sp offset
1386 SPOffset -= Offset;
1387
1388 // To squash multiple .pad directives, we should delay the unwind opcode
1389 // until the .save, .vsave, .handlerdata, or .fnend directives.
1390 PendingOffset -= Offset;
1391}
1392
1393static std::pair<unsigned, unsigned>
1395 const SmallVectorImpl<unsigned> &RegList, bool IsVector,
1396 uint32_t &Mask_) {
1397 uint32_t Mask = 0;
1398 unsigned Count = 0;
1399 while (Idx > 0) {
1400 unsigned Reg = RegList[Idx - 1];
1401 if (Reg == ARM::RA_AUTH_CODE)
1402 break;
1403 Reg = MRI.getEncodingValue(Reg);
1404 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1405 unsigned Bit = (1u << Reg);
1406 if ((Mask & Bit) == 0) {
1407 Mask |= Bit;
1408 ++Count;
1409 }
1410 --Idx;
1411 }
1412
1413 Mask_ = Mask;
1414 return {Idx, Count};
1415}
1416
1417void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1418 bool IsVector) {
1419 uint32_t Mask;
1420 unsigned Idx, Count;
1421 const MCRegisterInfo &MRI = *getContext().getRegisterInfo();
1422
1423 // Collect the registers in the register list. Issue unwinding instructions in
1424 // three parts: ordinary hardware registers, return address authentication
1425 // code pseudo register, the rest of the registers. The RA PAC is kept in an
1426 // architectural register (usually r12), but we treat it as a special case in
1427 // order to distinguish between that register containing RA PAC or a general
1428 // value.
1429 Idx = RegList.size();
1430 while (Idx > 0) {
1431 std::tie(Idx, Count) = collectHWRegs(MRI, Idx, RegList, IsVector, Mask);
1432 if (Count) {
1433 // Track the change the $sp offset: For the .save directive, the
1434 // corresponding push instruction will decrease the $sp by (4 * Count).
1435 // For the .vsave directive, the corresponding vpush instruction will
1436 // decrease $sp by (8 * Count).
1437 SPOffset -= Count * (IsVector ? 8 : 4);
1438
1439 // Emit the opcode
1440 FlushPendingOffset();
1441 if (IsVector)
1442 UnwindOpAsm.EmitVFPRegSave(Mask);
1443 else
1444 UnwindOpAsm.EmitRegSave(Mask);
1445 } else if (Idx > 0 && RegList[Idx - 1] == ARM::RA_AUTH_CODE) {
1446 --Idx;
1447 SPOffset -= 4;
1448 FlushPendingOffset();
1449 UnwindOpAsm.EmitRegSave(0);
1450 }
1451 }
1452}
1453
1454void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1455 const SmallVectorImpl<uint8_t> &Opcodes) {
1456 FlushPendingOffset();
1457 SPOffset = SPOffset - Offset;
1458 UnwindOpAsm.EmitRaw(Opcodes);
1459}
1460
1461namespace llvm {
1462
1465 MCInstPrinter *InstPrint,
1466 bool isVerboseAsm) {
1467 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1468}
1469
1471 return new ARMTargetStreamer(S);
1472}
1473
1475 return new ARMTargetELFStreamer(S);
1476}
1477
1479 std::unique_ptr<MCAsmBackend> TAB,
1480 std::unique_ptr<MCObjectWriter> OW,
1481 std::unique_ptr<MCCodeEmitter> Emitter,
1482 bool IsThumb, bool IsAndroid) {
1483 ARMELFStreamer *S =
1484 new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
1485 std::move(Emitter), IsThumb, IsAndroid);
1486 // FIXME: This should eventually end up somewhere else where more
1487 // intelligent flag decisions can be made. For now we are just maintaining
1488 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1489 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1490
1491 return S;
1492}
1493
1494} // end namespace llvm
unsigned const MachineRegisterInfo * MRI
static std::string GetAEABIUnwindPersonalityName(unsigned Index)
static std::pair< unsigned, unsigned > collectHWRegs(const MCRegisterInfo &MRI, unsigned Idx, const SmallVectorImpl< unsigned > &RegList, bool IsVector, uint32_t &Mask_)
dxil DXContainer Global Emitter
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the DenseMap class.
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned Reg
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Value * RHS
Value * LHS
virtual void emitUnwindRaw(int64_t StackOffset, const SmallVectorImpl< uint8_t > &Opcodes)
virtual void reset()
Reset any state between object emissions, i.e.
virtual void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE)
virtual void finishAttributeSection()
virtual void emitARMWinCFISaveSP(unsigned Reg)
virtual void emitPersonalityIndex(unsigned Index)
virtual void emitInst(uint32_t Inst, char Suffix='\0')
virtual void emitARMWinCFISaveLR(unsigned Offset)
virtual void emitArchExtension(uint64_t ArchExt)
virtual void emitRegSave(const SmallVectorImpl< unsigned > &RegList, bool isVector)
virtual void emitTextAttribute(unsigned Attribute, StringRef String)
virtual void emitARMWinCFIAllocStack(unsigned Size, bool Wide)
virtual void emitMovSP(unsigned Reg, int64_t Offset=0)
virtual void emitARMWinCFICustom(unsigned Opcode)
virtual void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide)
virtual void emitARMWinCFIEpilogEnd()
virtual void emitARMWinCFIPrologEnd(bool Fragment)
virtual void switchVendor(StringRef Vendor)
virtual void emitPersonality(const MCSymbol *Personality)
virtual void emitObjectArch(ARM::ArchKind Arch)
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, StringRef StringValue="")
virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value)
virtual void emitFPU(ARM::FPUKind FPU)
virtual void emitARMWinCFISaveFRegs(unsigned First, unsigned Last)
virtual void emitARMWinCFIEpilogStart(unsigned Condition)
virtual void emitPad(int64_t Offset)
virtual void emitAttribute(unsigned Attribute, unsigned Value)
virtual void emitARMWinCFINop(bool Wide)
virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset=0)
virtual void emitArch(ARM::ArchKind Arch)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:555
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:229
Context object for machine code objects.
Definition: MCContext.h:83
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:412
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1069
Fragment for data and encoded instructions.
Definition: MCFragment.h:231
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void emitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
void emitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
void reset() override
state management
Definition: MCELFStreamer.h:40
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F, uint64_t Offset) override
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:188
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:109
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:45
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
MCAssembler & getAssembler()
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
unsigned getUniqueID() const
Definition: MCSectionELF.h:92
const MCSymbolELF * getGroup() const
Definition: MCSectionELF.h:82
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
StringRef getName() const
Definition: MCSection.h:130
MCSymbol * getBeginSymbol()
Definition: MCSection.h:135
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCFragment * getCurrentFragment() const
Definition: MCStreamer.h:409
MCContext & getContext() const
Definition: MCStreamer.h:300
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:393
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:269
Target specific streamer interface.
Definition: MCStreamer.h:94
virtual void finish()
Definition: MCStreamer.cpp:55
MCStreamer & getStreamer()
Definition: MCStreamer.h:102
virtual void emitLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:53
Represents a location in source code.
Definition: SMLoc.h:23
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
static SectionKind getData()
Definition: SectionKind.h:213
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:5022
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const TagNameMap & getARMAttributeTags()
@ EXIDX_CANTUNWIND
Special entry for the function never unwind.
Definition: ARMEHABI.h:35
StringRef getArchExtName(uint64_t ArchExtKind)
StringRef getCPUAttr(ArchKind AK)
StringRef getArchName(ArchKind AK)
unsigned getArchAttr(ArchKind AK)
StringRef getFPUName(FPUKind FPUKind)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
StringRef attrTypeAsString(unsigned attr, TagNameMap tagNameMap, bool hasTagPrefix=true)
@ STB_LOCAL
Definition: ELF.h:1318
@ SHT_PROGBITS
Definition: ELF.h:1068
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1127
@ SHT_ARM_EXIDX
Definition: ELF.h:1123
@ EF_ARM_EABI_VER5
Definition: ELF.h:448
@ SHF_ALLOC
Definition: ELF.h:1165
@ SHF_LINK_ORDER
Definition: ELF.h:1180
@ SHF_GROUP
Definition: ELF.h:1187
@ STT_FUNC
Definition: ELF.h:1332
@ STT_NOTYPE
Definition: ELF.h:1330
@ STT_GNU_IFUNC
Definition: ELF.h:1337
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MCELFStreamer * createARMELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool IsThumb, bool IsAndroid)
MCTargetStreamer * createARMObjectTargetELFStreamer(MCStreamer &S)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
MCAssemblerFlag
Definition: MCDirectives.h:53
@ MCAF_SyntaxUnified
.syntax (ARM/ELF)
Definition: MCDirectives.h:54
@ MCAF_Code64
.code64 (X86)
Definition: MCDirectives.h:58
@ MCAF_Code16
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:56
@ MCAF_Code32
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:57
@ MCAF_SubsectionsViaSymbols
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:55
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
static const char * ARMCondCodeToString(ARMCC::CondCodes CC)
Definition: ARMBaseInfo.h:146
MCTargetStreamer * createARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
MCTargetStreamer * createARMNullTargetStreamer(MCStreamer &S)
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
ELF object attributes section emission support.
Definition: MCELFStreamer.h:92