1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file assembles .s files and emits ARM ELF .o object files. Different
11 // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
12 // delimit regions of data and code.
14 //===----------------------------------------------------------------------===//
16 #include "ARMRegisterInfo.h"
17 #include "ARMUnwindOpAsm.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/BinaryFormat/ELF.h"
25 #include "llvm/MC/MCAsmBackend.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCAssembler.h"
28 #include "llvm/MC/MCCodeEmitter.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCELFStreamer.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"
35 #include "llvm/MC/MCInstPrinter.h"
36 #include "llvm/MC/MCRegisterInfo.h"
37 #include "llvm/MC/MCSection.h"
38 #include "llvm/MC/MCSectionELF.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSubtargetInfo.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MCSymbolELF.h"
43 #include "llvm/MC/SectionKind.h"
44 #include "llvm/Support/ARMBuildAttributes.h"
45 #include "llvm/Support/ARMEHABI.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FormattedStream.h"
49 #include "llvm/Support/LEB128.h"
50 #include "llvm/Support/TargetParser.h"
51 #include "llvm/Support/raw_ostream.h"
61 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
62 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
63 "Invalid personality index");
64 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
71 class ARMTargetAsmStreamer : public ARMTargetStreamer {
72 formatted_raw_ostream &OS;
73 MCInstPrinter &InstPrinter;
76 void emitFnStart() override;
77 void emitFnEnd() override;
78 void emitCantUnwind() override;
79 void emitPersonality(const MCSymbol *Personality) override;
80 void emitPersonalityIndex(unsigned Index) override;
81 void emitHandlerData() override;
82 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
83 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
84 void emitPad(int64_t Offset) override;
85 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
86 bool isVector) override;
87 void emitUnwindRaw(int64_t Offset,
88 const SmallVectorImpl<uint8_t> &Opcodes) override;
90 void switchVendor(StringRef Vendor) override;
91 void emitAttribute(unsigned Attribute, unsigned Value) override;
92 void emitTextAttribute(unsigned Attribute, StringRef String) override;
93 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
94 StringRef StringValue) override;
95 void emitArch(ARM::ArchKind Arch) override;
96 void emitArchExtension(unsigned ArchExt) override;
97 void emitObjectArch(ARM::ArchKind Arch) override;
98 void emitFPU(unsigned FPU) override;
99 void emitInst(uint32_t Inst, char Suffix = '\0') override;
100 void finishAttributeSection() override;
102 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
103 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
106 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107 MCInstPrinter &InstPrinter, bool VerboseAsm);
110 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
111 formatted_raw_ostream &OS,
112 MCInstPrinter &InstPrinter,
114 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
115 IsVerboseAsm(VerboseAsm) {}
117 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
118 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
119 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
121 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122 OS << "\t.personality " << Personality->getName() << '\n';
125 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126 OS << "\t.personalityindex " << Index << '\n';
129 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
131 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
134 InstPrinter.printRegName(OS, FpReg);
136 InstPrinter.printRegName(OS, SpReg);
138 OS << ", #" << Offset;
142 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
143 assert((Reg != ARM::SP && Reg != ARM::PC) &&
144 "the operand of .movsp cannot be either sp or pc");
147 InstPrinter.printRegName(OS, Reg);
149 OS << ", #" << Offset;
153 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154 OS << "\t.pad\t#" << Offset << '\n';
157 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
159 assert(RegList.size() && "RegList should not be empty");
165 InstPrinter.printRegName(OS, RegList[0]);
167 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
169 InstPrinter.printRegName(OS, RegList[i]);
175 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
177 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
178 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
180 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
182 OS << "\t@ " << Name;
187 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
190 case ARMBuildAttrs::CPU_name:
191 OS << "\t.cpu\t" << String.lower();
194 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
196 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
198 OS << "\t@ " << Name;
205 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
207 StringRef StringValue) {
209 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
210 case ARMBuildAttrs::compatibility:
211 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
212 if (!StringValue.empty())
213 OS << ", \"" << StringValue << "\"";
215 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
221 void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
222 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
225 void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
226 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
229 void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
230 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
233 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
234 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
237 void ARMTargetAsmStreamer::finishAttributeSection() {}
240 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
244 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
245 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
247 OS << "\t.thumb_set\t";
248 Symbol->print(OS, MAI);
250 Value->print(OS, MAI);
254 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
258 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
261 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
262 const SmallVectorImpl<uint8_t> &Opcodes) {
263 OS << "\t.unwind_raw " << Offset;
264 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
267 OS << ", 0x" << Twine::utohexstr(*OCI);
271 class ARMTargetELFStreamer : public ARMTargetStreamer {
273 // This structure holds all attributes, accounting for
274 // their string/numeric value, so we can later emit them
275 // in declaration order, keeping all in the same vector
276 struct AttributeItem {
281 NumericAndTextAttributes
285 std::string StringValue;
287 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
288 // The conformance tag must be emitted first when serialised
289 // into an object file. Specifically, the addenda to the ARM ABI
290 // states that (2.3.7.4):
292 // "To simplify recognition by consumers in the common case of
293 // claiming conformity for the whole file, this tag should be
294 // emitted first in a file-scope sub-subsection of the first
295 // public subsection of the attributes section."
297 // So it is special-cased in this comparison predicate when the
298 // attributes are sorted in finishAttributeSection().
299 return (RHS.Tag != ARMBuildAttrs::conformance) &&
300 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
304 StringRef CurrentVendor;
305 unsigned FPU = ARM::FK_INVALID;
306 ARM::ArchKind Arch = ARM::ArchKind::INVALID;
307 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
308 SmallVector<AttributeItem, 64> Contents;
310 MCSection *AttributeSection = nullptr;
312 AttributeItem *getAttributeItem(unsigned Attribute) {
313 for (size_t i = 0; i < Contents.size(); ++i)
314 if (Contents[i].Tag == Attribute)
319 void setAttributeItem(unsigned Attribute, unsigned Value,
320 bool OverwriteExisting) {
321 // Look for existing attribute item
322 if (AttributeItem *Item = getAttributeItem(Attribute)) {
323 if (!OverwriteExisting)
325 Item->Type = AttributeItem::NumericAttribute;
326 Item->IntValue = Value;
330 // Create new attribute item
331 AttributeItem Item = {
332 AttributeItem::NumericAttribute,
337 Contents.push_back(Item);
340 void setAttributeItem(unsigned Attribute, StringRef Value,
341 bool OverwriteExisting) {
342 // Look for existing attribute item
343 if (AttributeItem *Item = getAttributeItem(Attribute)) {
344 if (!OverwriteExisting)
346 Item->Type = AttributeItem::TextAttribute;
347 Item->StringValue = Value;
351 // Create new attribute item
352 AttributeItem Item = {
353 AttributeItem::TextAttribute,
358 Contents.push_back(Item);
361 void setAttributeItems(unsigned Attribute, unsigned IntValue,
362 StringRef StringValue, bool OverwriteExisting) {
363 // Look for existing attribute item
364 if (AttributeItem *Item = getAttributeItem(Attribute)) {
365 if (!OverwriteExisting)
367 Item->Type = AttributeItem::NumericAndTextAttributes;
368 Item->IntValue = IntValue;
369 Item->StringValue = StringValue;
373 // Create new attribute item
374 AttributeItem Item = {
375 AttributeItem::NumericAndTextAttributes,
380 Contents.push_back(Item);
383 void emitArchDefaultAttributes();
384 void emitFPUDefaultAttributes();
386 ARMELFStreamer &getStreamer();
388 void emitFnStart() override;
389 void emitFnEnd() override;
390 void emitCantUnwind() override;
391 void emitPersonality(const MCSymbol *Personality) override;
392 void emitPersonalityIndex(unsigned Index) override;
393 void emitHandlerData() override;
394 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
395 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
396 void emitPad(int64_t Offset) override;
397 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
398 bool isVector) override;
399 void emitUnwindRaw(int64_t Offset,
400 const SmallVectorImpl<uint8_t> &Opcodes) override;
402 void switchVendor(StringRef Vendor) override;
403 void emitAttribute(unsigned Attribute, unsigned Value) override;
404 void emitTextAttribute(unsigned Attribute, StringRef String) override;
405 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
406 StringRef StringValue) override;
407 void emitArch(ARM::ArchKind Arch) override;
408 void emitObjectArch(ARM::ArchKind Arch) override;
409 void emitFPU(unsigned FPU) override;
410 void emitInst(uint32_t Inst, char Suffix = '\0') override;
411 void finishAttributeSection() override;
412 void emitLabel(MCSymbol *Symbol) override;
414 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
415 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
417 size_t calculateContentSize() const;
419 // Reset state between object emissions
420 void reset() override;
423 ARMTargetELFStreamer(MCStreamer &S)
424 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
427 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
428 /// the appropriate points in the object files. These symbols are defined in the
429 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
431 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
432 /// region of ARM code, Thumb code or data in a section. In practice, this
433 /// emission does not rely on explicit assembler directives but on inherent
434 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
435 /// r0, r0, r0" an instruction).
437 /// As a result this system is orthogonal to the DataRegion infrastructure used
438 /// by MachO. Beware!
439 class ARMELFStreamer : public MCELFStreamer {
441 friend class ARMTargetELFStreamer;
443 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
444 raw_pwrite_stream &OS, std::unique_ptr<MCCodeEmitter> Emitter,
446 : MCELFStreamer(Context, std::move(TAB), OS, std::move(Emitter)),
451 ~ARMELFStreamer() override = default;
453 void FinishImpl() override;
455 // ARM exception handling directives
458 void emitCantUnwind();
459 void emitPersonality(const MCSymbol *Per);
460 void emitPersonalityIndex(unsigned index);
461 void emitHandlerData();
462 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
463 void emitMovSP(unsigned Reg, int64_t Offset = 0);
464 void emitPad(int64_t Offset);
465 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
466 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
468 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
469 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
470 MCELFStreamer::ChangeSection(Section, Subsection);
471 auto LastMappingSymbol = LastMappingSymbols.find(Section);
472 if (LastMappingSymbol != LastMappingSymbols.end()) {
473 LastEMSInfo = std::move(LastMappingSymbol->second);
476 LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
479 /// This function is the one used to emit instruction data into the ELF
480 /// streamer. We override it to add the appropriate mapping symbol if
482 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
485 EmitThumbMappingSymbol();
487 EmitARMMappingSymbol();
489 MCELFStreamer::EmitInstruction(Inst, STI);
492 void emitInst(uint32_t Inst, char Suffix) {
495 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
502 EmitARMMappingSymbol();
503 for (unsigned II = 0, IE = Size; II != IE; II++) {
504 const unsigned I = LittleEndian ? (Size - II - 1) : II;
505 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
511 Size = (Suffix == 'n' ? 2 : 4);
514 EmitThumbMappingSymbol();
515 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
516 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
517 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
518 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
519 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
524 llvm_unreachable("Invalid Suffix");
527 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
530 /// This is one of the functions used to emit data into an ELF section, so the
531 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
533 void EmitBytes(StringRef Data) override {
534 EmitDataMappingSymbol();
535 MCELFStreamer::EmitBytes(Data);
538 void FlushPendingMappingSymbol() {
539 if (!LastEMSInfo->hasInfo())
541 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
542 EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
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
549 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
550 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
551 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
552 getContext().reportError(Loc, "relocated expression must be 32-bit");
555 getOrCreateDataFragment();
558 EmitDataMappingSymbol();
559 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
562 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
563 MCELFStreamer::EmitAssemblerFlag(Flag);
566 case MCAF_SyntaxUnified:
567 return; // no-op here.
570 return; // Change to Thumb mode
573 return; // Change to ARM mode
576 case MCAF_SubsectionsViaSymbols:
582 enum ElfMappingSymbol {
589 struct ElfMappingSymbolInfo {
590 explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
591 : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
596 bool hasInfo() { return F != nullptr; }
600 ElfMappingSymbol State;
603 void EmitDataMappingSymbol() {
604 if (LastEMSInfo->State == EMS_Data)
606 else if (LastEMSInfo->State == EMS_None) {
607 // This is a tentative symbol, it won't really be emitted until it's
609 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
610 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
614 EMS->F = getCurrentFragment();
615 EMS->Offset = DF->getContents().size();
616 LastEMSInfo->State = EMS_Data;
619 EmitMappingSymbol("$d");
620 LastEMSInfo->State = EMS_Data;
623 void EmitThumbMappingSymbol() {
624 if (LastEMSInfo->State == EMS_Thumb)
626 FlushPendingMappingSymbol();
627 EmitMappingSymbol("$t");
628 LastEMSInfo->State = EMS_Thumb;
631 void EmitARMMappingSymbol() {
632 if (LastEMSInfo->State == EMS_ARM)
634 FlushPendingMappingSymbol();
635 EmitMappingSymbol("$a");
636 LastEMSInfo->State = EMS_ARM;
639 void EmitMappingSymbol(StringRef Name) {
640 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
641 Name + "." + Twine(MappingSymbolCounter++)));
644 Symbol->setType(ELF::STT_NOTYPE);
645 Symbol->setBinding(ELF::STB_LOCAL);
646 Symbol->setExternal(false);
649 void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
651 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
652 Name + "." + Twine(MappingSymbolCounter++)));
653 EmitLabel(Symbol, Loc, F);
654 Symbol->setType(ELF::STT_NOTYPE);
655 Symbol->setBinding(ELF::STB_LOCAL);
656 Symbol->setExternal(false);
657 Symbol->setOffset(Offset);
660 void EmitThumbFunc(MCSymbol *Func) override {
661 getAssembler().setIsThumbFunc(Func);
662 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
665 // Helper functions for ARM exception handling directives
668 // Reset state between object emissions
669 void reset() override;
671 void EmitPersonalityFixup(StringRef Name);
672 void FlushPendingOffset();
673 void FlushUnwindOpcodes(bool NoHandlerData);
675 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
676 SectionKind Kind, const MCSymbol &Fn);
677 void SwitchToExTabSection(const MCSymbol &FnStart);
678 void SwitchToExIdxSection(const MCSymbol &FnStart);
680 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
683 int64_t MappingSymbolCounter = 0;
685 DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
688 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
690 // ARM Exception Handling Frame Information
693 const MCSymbol *Personality;
694 unsigned PersonalityIndex;
695 unsigned FPReg; // Frame pointer register
696 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
697 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
698 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
701 SmallVector<uint8_t, 64> Opcodes;
702 UnwindOpcodeAssembler UnwindOpAsm;
705 } // end anonymous namespace
707 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
708 return static_cast<ARMELFStreamer &>(Streamer);
711 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
712 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
713 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
715 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
716 getStreamer().emitPersonality(Personality);
719 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
720 getStreamer().emitPersonalityIndex(Index);
723 void ARMTargetELFStreamer::emitHandlerData() {
724 getStreamer().emitHandlerData();
727 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
729 getStreamer().emitSetFP(FpReg, SpReg, Offset);
732 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
733 getStreamer().emitMovSP(Reg, Offset);
736 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
737 getStreamer().emitPad(Offset);
740 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
742 getStreamer().emitRegSave(RegList, isVector);
745 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
746 const SmallVectorImpl<uint8_t> &Opcodes) {
747 getStreamer().emitUnwindRaw(Offset, Opcodes);
750 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
751 assert(!Vendor.empty() && "Vendor cannot be empty.");
753 if (CurrentVendor == Vendor)
756 if (!CurrentVendor.empty())
757 finishAttributeSection();
759 assert(Contents.empty() &&
760 ".ARM.attributes should be flushed before changing vendor");
761 CurrentVendor = Vendor;
765 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
766 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
769 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
771 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
774 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
776 StringRef StringValue) {
777 setAttributeItems(Attribute, IntValue, StringValue,
778 /* OverwriteExisting= */ true);
781 void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
785 void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
789 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
790 using namespace ARMBuildAttrs;
792 setAttributeItem(CPU_name,
793 ARM::getCPUAttr(Arch),
796 if (EmittedArch == ARM::ArchKind::INVALID)
797 setAttributeItem(CPU_arch,
798 ARM::getArchAttr(Arch),
801 setAttributeItem(CPU_arch,
802 ARM::getArchAttr(EmittedArch),
806 case ARM::ArchKind::ARMV2:
807 case ARM::ArchKind::ARMV2A:
808 case ARM::ArchKind::ARMV3:
809 case ARM::ArchKind::ARMV3M:
810 case ARM::ArchKind::ARMV4:
811 setAttributeItem(ARM_ISA_use, Allowed, false);
814 case ARM::ArchKind::ARMV4T:
815 case ARM::ArchKind::ARMV5T:
816 case ARM::ArchKind::ARMV5TE:
817 case ARM::ArchKind::ARMV6:
818 setAttributeItem(ARM_ISA_use, Allowed, false);
819 setAttributeItem(THUMB_ISA_use, Allowed, false);
822 case ARM::ArchKind::ARMV6T2:
823 setAttributeItem(ARM_ISA_use, Allowed, false);
824 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
827 case ARM::ArchKind::ARMV6K:
828 case ARM::ArchKind::ARMV6KZ:
829 setAttributeItem(ARM_ISA_use, Allowed, false);
830 setAttributeItem(THUMB_ISA_use, Allowed, false);
831 setAttributeItem(Virtualization_use, AllowTZ, false);
834 case ARM::ArchKind::ARMV6M:
835 setAttributeItem(THUMB_ISA_use, Allowed, false);
838 case ARM::ArchKind::ARMV7A:
839 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
840 setAttributeItem(ARM_ISA_use, Allowed, false);
841 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
844 case ARM::ArchKind::ARMV7R:
845 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
846 setAttributeItem(ARM_ISA_use, Allowed, false);
847 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
850 case ARM::ArchKind::ARMV7EM:
851 case ARM::ArchKind::ARMV7M:
852 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
853 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
856 case ARM::ArchKind::ARMV8A:
857 case ARM::ArchKind::ARMV8_1A:
858 case ARM::ArchKind::ARMV8_2A:
859 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
860 setAttributeItem(ARM_ISA_use, Allowed, false);
861 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
862 setAttributeItem(MPextension_use, Allowed, false);
863 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
866 case ARM::ArchKind::ARMV8MBaseline:
867 case ARM::ArchKind::ARMV8MMainline:
868 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
869 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
872 case ARM::ArchKind::IWMMXT:
873 setAttributeItem(ARM_ISA_use, Allowed, false);
874 setAttributeItem(THUMB_ISA_use, Allowed, false);
875 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
878 case ARM::ArchKind::IWMMXT2:
879 setAttributeItem(ARM_ISA_use, Allowed, false);
880 setAttributeItem(THUMB_ISA_use, Allowed, false);
881 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
885 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
890 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
894 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
898 setAttributeItem(ARMBuildAttrs::FP_arch,
899 ARMBuildAttrs::AllowFPv2,
900 /* OverwriteExisting= */ false);
904 setAttributeItem(ARMBuildAttrs::FP_arch,
905 ARMBuildAttrs::AllowFPv3A,
906 /* OverwriteExisting= */ false);
909 case ARM::FK_VFPV3_FP16:
910 setAttributeItem(ARMBuildAttrs::FP_arch,
911 ARMBuildAttrs::AllowFPv3A,
912 /* OverwriteExisting= */ false);
913 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
914 ARMBuildAttrs::AllowHPFP,
915 /* OverwriteExisting= */ false);
918 case ARM::FK_VFPV3_D16:
919 setAttributeItem(ARMBuildAttrs::FP_arch,
920 ARMBuildAttrs::AllowFPv3B,
921 /* OverwriteExisting= */ false);
924 case ARM::FK_VFPV3_D16_FP16:
925 setAttributeItem(ARMBuildAttrs::FP_arch,
926 ARMBuildAttrs::AllowFPv3B,
927 /* OverwriteExisting= */ false);
928 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
929 ARMBuildAttrs::AllowHPFP,
930 /* OverwriteExisting= */ false);
933 case ARM::FK_VFPV3XD:
934 setAttributeItem(ARMBuildAttrs::FP_arch,
935 ARMBuildAttrs::AllowFPv3B,
936 /* OverwriteExisting= */ false);
938 case ARM::FK_VFPV3XD_FP16:
939 setAttributeItem(ARMBuildAttrs::FP_arch,
940 ARMBuildAttrs::AllowFPv3B,
941 /* OverwriteExisting= */ false);
942 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
943 ARMBuildAttrs::AllowHPFP,
944 /* OverwriteExisting= */ false);
948 setAttributeItem(ARMBuildAttrs::FP_arch,
949 ARMBuildAttrs::AllowFPv4A,
950 /* OverwriteExisting= */ false);
953 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
955 case ARM::FK_FPV4_SP_D16:
956 case ARM::FK_VFPV4_D16:
957 setAttributeItem(ARMBuildAttrs::FP_arch,
958 ARMBuildAttrs::AllowFPv4B,
959 /* OverwriteExisting= */ false);
962 case ARM::FK_FP_ARMV8:
963 setAttributeItem(ARMBuildAttrs::FP_arch,
964 ARMBuildAttrs::AllowFPARMv8A,
965 /* OverwriteExisting= */ false);
968 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
969 // uses the FP_ARMV8_D16 build attribute.
970 case ARM::FK_FPV5_SP_D16:
971 case ARM::FK_FPV5_D16:
972 setAttributeItem(ARMBuildAttrs::FP_arch,
973 ARMBuildAttrs::AllowFPARMv8B,
974 /* OverwriteExisting= */ false);
978 setAttributeItem(ARMBuildAttrs::FP_arch,
979 ARMBuildAttrs::AllowFPv3A,
980 /* OverwriteExisting= */ false);
981 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
982 ARMBuildAttrs::AllowNeon,
983 /* OverwriteExisting= */ false);
986 case ARM::FK_NEON_FP16:
987 setAttributeItem(ARMBuildAttrs::FP_arch,
988 ARMBuildAttrs::AllowFPv3A,
989 /* OverwriteExisting= */ false);
990 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
991 ARMBuildAttrs::AllowNeon,
992 /* OverwriteExisting= */ false);
993 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
994 ARMBuildAttrs::AllowHPFP,
995 /* OverwriteExisting= */ false);
998 case ARM::FK_NEON_VFPV4:
999 setAttributeItem(ARMBuildAttrs::FP_arch,
1000 ARMBuildAttrs::AllowFPv4A,
1001 /* OverwriteExisting= */ false);
1002 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1003 ARMBuildAttrs::AllowNeon2,
1004 /* OverwriteExisting= */ false);
1007 case ARM::FK_NEON_FP_ARMV8:
1008 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1009 setAttributeItem(ARMBuildAttrs::FP_arch,
1010 ARMBuildAttrs::AllowFPARMv8A,
1011 /* OverwriteExisting= */ false);
1012 // 'Advanced_SIMD_arch' must be emitted not here, but within
1013 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1016 case ARM::FK_SOFTVFP:
1021 report_fatal_error("Unknown FPU: " + Twine(FPU));
1026 size_t ARMTargetELFStreamer::calculateContentSize() const {
1028 for (size_t i = 0; i < Contents.size(); ++i) {
1029 AttributeItem item = Contents[i];
1030 switch (item.Type) {
1031 case AttributeItem::HiddenAttribute:
1033 case AttributeItem::NumericAttribute:
1034 Result += getULEB128Size(item.Tag);
1035 Result += getULEB128Size(item.IntValue);
1037 case AttributeItem::TextAttribute:
1038 Result += getULEB128Size(item.Tag);
1039 Result += item.StringValue.size() + 1; // string + '\0'
1041 case AttributeItem::NumericAndTextAttributes:
1042 Result += getULEB128Size(item.Tag);
1043 Result += getULEB128Size(item.IntValue);
1044 Result += item.StringValue.size() + 1; // string + '\0';
1051 void ARMTargetELFStreamer::finishAttributeSection() {
1053 // [ <section-length> "vendor-name"
1054 // [ <file-tag> <size> <attribute>*
1055 // | <section-tag> <size> <section-number>* 0 <attribute>*
1056 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1060 if (FPU != ARM::FK_INVALID)
1061 emitFPUDefaultAttributes();
1063 if (Arch != ARM::ArchKind::INVALID)
1064 emitArchDefaultAttributes();
1066 if (Contents.empty())
1069 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
1071 ARMELFStreamer &Streamer = getStreamer();
1073 // Switch to .ARM.attributes section
1074 if (AttributeSection) {
1075 Streamer.SwitchSection(AttributeSection);
1077 AttributeSection = Streamer.getContext().getELFSection(
1078 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
1079 Streamer.SwitchSection(AttributeSection);
1082 Streamer.EmitIntValue(0x41, 1);
1085 // Vendor size + Vendor name + '\0'
1086 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1089 const size_t TagHeaderSize = 1 + 4;
1091 const size_t ContentsSize = calculateContentSize();
1093 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1094 Streamer.EmitBytes(CurrentVendor);
1095 Streamer.EmitIntValue(0, 1); // '\0'
1097 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1098 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1100 // Size should have been accounted for already, now
1101 // emit each field as its type (ULEB or String)
1102 for (size_t i = 0; i < Contents.size(); ++i) {
1103 AttributeItem item = Contents[i];
1104 Streamer.EmitULEB128IntValue(item.Tag);
1105 switch (item.Type) {
1106 default: llvm_unreachable("Invalid attribute type");
1107 case AttributeItem::NumericAttribute:
1108 Streamer.EmitULEB128IntValue(item.IntValue);
1110 case AttributeItem::TextAttribute:
1111 Streamer.EmitBytes(item.StringValue);
1112 Streamer.EmitIntValue(0, 1); // '\0'
1114 case AttributeItem::NumericAndTextAttributes:
1115 Streamer.EmitULEB128IntValue(item.IntValue);
1116 Streamer.EmitBytes(item.StringValue);
1117 Streamer.EmitIntValue(0, 1); // '\0'
1123 FPU = ARM::FK_INVALID;
1126 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1127 ARMELFStreamer &Streamer = getStreamer();
1128 if (!Streamer.IsThumb)
1131 Streamer.getAssembler().registerSymbol(*Symbol);
1132 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1133 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
1134 Streamer.EmitThumbFunc(Symbol);
1138 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1139 getStreamer().EmitFixup(S, FK_Data_4);
1142 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1143 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1144 const MCSymbol &Sym = SRE->getSymbol();
1145 if (!Sym.isDefined()) {
1146 getStreamer().EmitAssignment(Symbol, Value);
1151 getStreamer().EmitThumbFunc(Symbol);
1152 getStreamer().EmitAssignment(Symbol, Value);
1155 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1156 getStreamer().emitInst(Inst, Suffix);
1159 void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1161 void ARMELFStreamer::FinishImpl() {
1162 MCTargetStreamer &TS = *getTargetStreamer();
1163 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1164 ATS.finishAttributeSection();
1166 MCELFStreamer::FinishImpl();
1169 void ARMELFStreamer::reset() {
1170 MCTargetStreamer &TS = *getTargetStreamer();
1171 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1173 MappingSymbolCounter = 0;
1174 MCELFStreamer::reset();
1175 LastMappingSymbols.clear();
1176 LastEMSInfo.reset();
1177 // MCELFStreamer clear's the assembler's e_flags. However, for
1178 // arm we manually set the ABI version on streamer creation, so
1180 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1183 inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1187 const MCSymbol &Fn) {
1188 const MCSectionELF &FnSection =
1189 static_cast<const MCSectionELF &>(Fn.getSection());
1191 // Create the name for new section
1192 StringRef FnSecName(FnSection.getSectionName());
1193 SmallString<128> EHSecName(Prefix);
1194 if (FnSecName != ".text") {
1195 EHSecName += FnSecName;
1198 // Get .ARM.extab or .ARM.exidx section
1199 const MCSymbolELF *Group = FnSection.getGroup();
1201 Flags |= ELF::SHF_GROUP;
1202 MCSectionELF *EHSection = getContext().getELFSection(
1203 EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1204 static_cast<const MCSymbolELF *>(&Fn));
1206 assert(EHSection && "Failed to get the required EH section");
1208 // Switch to .ARM.extab or .ARM.exidx section
1209 SwitchSection(EHSection);
1210 EmitCodeAlignment(4);
1213 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1214 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1215 SectionKind::getData(), FnStart);
1218 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1219 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1220 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1221 SectionKind::getData(), FnStart);
1224 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1225 MCDataFragment *Frag = getOrCreateDataFragment();
1226 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1230 void ARMELFStreamer::EHReset() {
1233 Personality = nullptr;
1234 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1243 UnwindOpAsm.Reset();
1246 void ARMELFStreamer::emitFnStart() {
1247 assert(FnStart == nullptr);
1248 FnStart = getContext().createTempSymbol();
1252 void ARMELFStreamer::emitFnEnd() {
1253 assert(FnStart && ".fnstart must precedes .fnend");
1255 // Emit unwind opcodes if there is no .handlerdata directive
1256 if (!ExTab && !CantUnwind)
1257 FlushUnwindOpcodes(true);
1259 // Emit the exception index table entry
1260 SwitchToExIdxSection(*FnStart);
1262 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1263 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1265 const MCSymbolRefExpr *FnStartRef =
1266 MCSymbolRefExpr::create(FnStart,
1267 MCSymbolRefExpr::VK_ARM_PREL31,
1270 EmitValue(FnStartRef, 4);
1273 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1275 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1276 const MCSymbolRefExpr *ExTabEntryRef =
1277 MCSymbolRefExpr::create(ExTab,
1278 MCSymbolRefExpr::VK_ARM_PREL31,
1280 EmitValue(ExTabEntryRef, 4);
1282 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1283 // the second word of exception index table entry. The size of the unwind
1284 // opcodes should always be 4 bytes.
1285 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1286 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1287 assert(Opcodes.size() == 4u &&
1288 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1289 uint64_t Intval = Opcodes[0] |
1293 EmitIntValue(Intval, Opcodes.size());
1296 // Switch to the section containing FnStart
1297 SwitchSection(&FnStart->getSection());
1299 // Clean exception handling frame information
1303 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1305 // Add the R_ARM_NONE fixup at the same position
1306 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1307 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1309 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1310 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1312 visitUsedExpr(*PersonalityRef);
1313 MCDataFragment *DF = getOrCreateDataFragment();
1314 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1316 MCFixup::getKindForSize(4, false)));
1319 void ARMELFStreamer::FlushPendingOffset() {
1320 if (PendingOffset != 0) {
1321 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1326 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1327 // Emit the unwind opcode to restore $sp.
1329 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1330 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1331 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1332 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1334 FlushPendingOffset();
1337 // Finalize the unwind opcode sequence
1338 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1340 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1341 // section. Thus, we don't have to create an entry in the .ARM.extab
1343 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1346 // Switch to .ARM.extab section.
1347 SwitchToExTabSection(*FnStart);
1349 // Create .ARM.extab label for offset in .ARM.exidx
1351 ExTab = getContext().createTempSymbol();
1356 const MCSymbolRefExpr *PersonalityRef =
1357 MCSymbolRefExpr::create(Personality,
1358 MCSymbolRefExpr::VK_ARM_PREL31,
1361 EmitValue(PersonalityRef, 4);
1364 // Emit unwind opcodes
1365 assert((Opcodes.size() % 4) == 0 &&
1366 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1367 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1368 uint64_t Intval = Opcodes[I] |
1369 Opcodes[I + 1] << 8 |
1370 Opcodes[I + 2] << 16 |
1371 Opcodes[I + 3] << 24;
1372 EmitIntValue(Intval, 4);
1375 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1376 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1377 // after the unwind opcodes. The handler data consists of several 32-bit
1378 // words, and should be terminated by zero.
1380 // In case that the .handlerdata directive is not specified by the
1381 // programmer, we should emit zero to terminate the handler data.
1382 if (NoHandlerData && !Personality)
1386 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1388 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1390 UnwindOpAsm.setPersonality(Per);
1393 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1394 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1395 PersonalityIndex = Index;
1398 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1400 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1401 "the operand of .setfp directive should be either $sp or $fp");
1406 if (NewSPReg == ARM::SP)
1407 FPOffset = SPOffset + Offset;
1412 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1413 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1414 "the operand of .movsp cannot be either sp or pc");
1415 assert(FPReg == ARM::SP && "current FP must be SP");
1417 FlushPendingOffset();
1420 FPOffset = SPOffset + Offset;
1422 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1423 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1426 void ARMELFStreamer::emitPad(int64_t Offset) {
1427 // Track the change of the $sp offset
1430 // To squash multiple .pad directives, we should delay the unwind opcode
1431 // until the .save, .vsave, .handlerdata, or .fnend directives.
1432 PendingOffset -= Offset;
1435 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1437 // Collect the registers in the register list
1440 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1441 for (size_t i = 0; i < RegList.size(); ++i) {
1442 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1443 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1444 unsigned Bit = (1u << Reg);
1445 if ((Mask & Bit) == 0) {
1451 // Track the change the $sp offset: For the .save directive, the
1452 // corresponding push instruction will decrease the $sp by (4 * Count).
1453 // For the .vsave directive, the corresponding vpush instruction will
1454 // decrease $sp by (8 * Count).
1455 SPOffset -= Count * (IsVector ? 8 : 4);
1458 FlushPendingOffset();
1460 UnwindOpAsm.EmitVFPRegSave(Mask);
1462 UnwindOpAsm.EmitRegSave(Mask);
1465 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1466 const SmallVectorImpl<uint8_t> &Opcodes) {
1467 FlushPendingOffset();
1468 SPOffset = SPOffset - Offset;
1469 UnwindOpAsm.EmitRaw(Opcodes);
1474 MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1475 formatted_raw_ostream &OS,
1476 MCInstPrinter *InstPrint,
1477 bool isVerboseAsm) {
1478 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1481 MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1482 return new ARMTargetStreamer(S);
1485 MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1486 const MCSubtargetInfo &STI) {
1487 const Triple &TT = STI.getTargetTriple();
1488 if (TT.isOSBinFormatELF())
1489 return new ARMTargetELFStreamer(S);
1490 return new ARMTargetStreamer(S);
1493 MCELFStreamer *createARMELFStreamer(MCContext &Context,
1494 std::unique_ptr<MCAsmBackend> TAB,
1495 raw_pwrite_stream &OS,
1496 std::unique_ptr<MCCodeEmitter> Emitter,
1497 bool RelaxAll, bool IsThumb) {
1498 ARMELFStreamer *S = new ARMELFStreamer(Context, std::move(TAB), OS,
1499 std::move(Emitter), IsThumb);
1500 // FIXME: This should eventually end up somewhere else where more
1501 // intelligent flag decisions can be made. For now we are just maintaining
1502 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1503 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1506 S->getAssembler().setRelaxAll(true);
1510 } // end namespace llvm