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 "ARMArchName.h"
17 #include "ARMFPUName.h"
18 #include "ARMRegisterInfo.h"
19 #include "ARMUnwindOpAsm.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/MC/MCAsmBackend.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCAssembler.h"
26 #include "llvm/MC/MCCodeEmitter.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCELF.h"
29 #include "llvm/MC/MCELFStreamer.h"
30 #include "llvm/MC/MCELFSymbolFlags.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCInstPrinter.h"
34 #include "llvm/MC/MCObjectStreamer.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSection.h"
37 #include "llvm/MC/MCSectionELF.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/MC/MCValue.h"
41 #include "llvm/Support/ARMBuildAttributes.h"
42 #include "llvm/Support/ARMEHABI.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ELF.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include "llvm/Support/raw_ostream.h"
51 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
52 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
53 "Invalid personality index");
54 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
57 static const char *GetFPUName(unsigned ID) {
60 llvm_unreachable("Unknown FPU kind");
62 #define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
63 #include "ARMFPUName.def"
68 static const char *GetArchName(unsigned ID) {
71 llvm_unreachable("Unknown ARCH kind");
73 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
74 case ARM::ID: return NAME;
75 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
76 #include "ARMArchName.def"
81 static const char *GetArchDefaultCPUName(unsigned ID) {
84 llvm_unreachable("Unknown ARCH kind");
86 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
87 case ARM::ID: return DEFAULT_CPU_NAME;
88 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
89 #include "ARMArchName.def"
94 static unsigned GetArchDefaultCPUArch(unsigned ID) {
97 llvm_unreachable("Unknown ARCH kind");
99 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
100 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
101 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
102 #include "ARMArchName.def"
109 class ARMELFStreamer;
111 class ARMTargetAsmStreamer : public ARMTargetStreamer {
112 formatted_raw_ostream &OS;
113 MCInstPrinter &InstPrinter;
116 virtual void emitFnStart();
117 virtual void emitFnEnd();
118 virtual void emitCantUnwind();
119 virtual void emitPersonality(const MCSymbol *Personality);
120 virtual void emitHandlerData();
121 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
122 virtual void emitPad(int64_t Offset);
123 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
126 virtual void switchVendor(StringRef Vendor);
127 virtual void emitAttribute(unsigned Attribute, unsigned Value);
128 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
129 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
130 StringRef StrinValue);
131 virtual void emitArch(unsigned Arch);
132 virtual void emitFPU(unsigned FPU);
133 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
134 virtual void finishAttributeSection();
137 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
141 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
142 MCInstPrinter &InstPrinter,
144 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
145 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
146 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
147 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
148 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
149 OS << "\t.personality " << Personality->getName() << '\n';
151 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
152 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
155 InstPrinter.printRegName(OS, FpReg);
157 InstPrinter.printRegName(OS, SpReg);
159 OS << ", #" << Offset;
162 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
163 OS << "\t.pad\t#" << Offset << '\n';
165 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
167 assert(RegList.size() && "RegList should not be empty");
173 InstPrinter.printRegName(OS, RegList[0]);
175 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
177 InstPrinter.printRegName(OS, RegList[i]);
182 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
184 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
185 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
187 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
189 OS << "\t@ " << Name;
193 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
196 case ARMBuildAttrs::CPU_name:
197 OS << "\t.cpu\t" << String.lower();
200 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
202 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
204 OS << "\t@ " << Name;
210 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
212 StringRef StringValue) {
214 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
215 case ARMBuildAttrs::compatibility:
216 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
217 if (!StringValue.empty())
218 OS << ", \"" << StringValue << "\"";
220 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
225 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
226 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
228 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
229 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
231 void ARMTargetAsmStreamer::finishAttributeSection() {
234 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
238 OS << "\t0x" << utohexstr(Inst) << "\n";
241 class ARMTargetELFStreamer : public ARMTargetStreamer {
243 // This structure holds all attributes, accounting for
244 // their string/numeric value, so we can later emmit them
245 // in declaration order, keeping all in the same vector
246 struct AttributeItem {
251 NumericAndTextAttributes
255 StringRef StringValue;
257 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
258 return (LHS.Tag < RHS.Tag);
262 StringRef CurrentVendor;
265 SmallVector<AttributeItem, 64> Contents;
267 const MCSection *AttributeSection;
269 // FIXME: this should be in a more generic place, but
270 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
271 static size_t getULEBSize(int Value) {
275 Size += sizeof(int8_t); // Is this really necessary?
280 AttributeItem *getAttributeItem(unsigned Attribute) {
281 for (size_t i = 0; i < Contents.size(); ++i)
282 if (Contents[i].Tag == Attribute)
287 void setAttributeItem(unsigned Attribute, unsigned Value,
288 bool OverwriteExisting) {
289 // Look for existing attribute item
290 if (AttributeItem *Item = getAttributeItem(Attribute)) {
291 if (!OverwriteExisting)
293 Item->Type = AttributeItem::NumericAttribute;
294 Item->IntValue = Value;
298 // Create new attribute item
299 AttributeItem Item = {
300 AttributeItem::NumericAttribute,
305 Contents.push_back(Item);
308 void setAttributeItem(unsigned Attribute, StringRef Value,
309 bool OverwriteExisting) {
310 // Look for existing attribute item
311 if (AttributeItem *Item = getAttributeItem(Attribute)) {
312 if (!OverwriteExisting)
314 Item->Type = AttributeItem::TextAttribute;
315 Item->StringValue = Value;
319 // Create new attribute item
320 AttributeItem Item = {
321 AttributeItem::TextAttribute,
326 Contents.push_back(Item);
329 void setAttributeItems(unsigned Attribute, unsigned IntValue,
330 StringRef StringValue, bool OverwriteExisting) {
331 // Look for existing attribute item
332 if (AttributeItem *Item = getAttributeItem(Attribute)) {
333 if (!OverwriteExisting)
335 Item->Type = AttributeItem::NumericAndTextAttributes;
336 Item->IntValue = IntValue;
337 Item->StringValue = StringValue;
341 // Create new attribute item
342 AttributeItem Item = {
343 AttributeItem::NumericAndTextAttributes,
348 Contents.push_back(Item);
351 void emitArchDefaultAttributes();
352 void emitFPUDefaultAttributes();
354 ARMELFStreamer &getStreamer();
356 virtual void emitFnStart();
357 virtual void emitFnEnd();
358 virtual void emitCantUnwind();
359 virtual void emitPersonality(const MCSymbol *Personality);
360 virtual void emitHandlerData();
361 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
362 virtual void emitPad(int64_t Offset);
363 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
366 virtual void switchVendor(StringRef Vendor);
367 virtual void emitAttribute(unsigned Attribute, unsigned Value);
368 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
369 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
370 StringRef StringValue);
371 virtual void emitArch(unsigned Arch);
372 virtual void emitFPU(unsigned FPU);
373 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
374 virtual void finishAttributeSection();
376 size_t calculateContentSize() const;
379 ARMTargetELFStreamer()
380 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
381 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
385 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
386 /// the appropriate points in the object files. These symbols are defined in the
387 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
389 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
390 /// region of ARM code, Thumb code or data in a section. In practice, this
391 /// emission does not rely on explicit assembler directives but on inherent
392 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
393 /// r0, r0, r0" an instruction).
395 /// As a result this system is orthogonal to the DataRegion infrastructure used
396 /// by MachO. Beware!
397 class ARMELFStreamer : public MCELFStreamer {
399 friend class ARMTargetELFStreamer;
401 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
402 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
404 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
405 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
411 virtual void FinishImpl();
413 // ARM exception handling directives
416 void emitCantUnwind();
417 void emitPersonality(const MCSymbol *Per);
418 void emitHandlerData();
419 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
420 void emitPad(int64_t Offset);
421 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
423 virtual void ChangeSection(const MCSection *Section,
424 const MCExpr *Subsection) {
425 // We have to keep track of the mapping symbol state of any sections we
426 // use. Each one should start off as EMS_None, which is provided as the
427 // default constructor by DenseMap::lookup.
428 LastMappingSymbols[getPreviousSection().first] = LastEMS;
429 LastEMS = LastMappingSymbols.lookup(Section);
431 MCELFStreamer::ChangeSection(Section, Subsection);
434 /// This function is the one used to emit instruction data into the ELF
435 /// streamer. We override it to add the appropriate mapping symbol if
437 virtual void EmitInstruction(const MCInst& Inst) {
439 EmitThumbMappingSymbol();
441 EmitARMMappingSymbol();
443 MCELFStreamer::EmitInstruction(Inst);
446 virtual void emitInst(uint32_t Inst, char Suffix) {
449 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
456 EmitARMMappingSymbol();
457 for (unsigned II = 0, IE = Size; II != IE; II++) {
458 const unsigned I = LittleEndian ? (Size - II - 1) : II;
459 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
465 Size = (Suffix == 'n' ? 2 : 4);
468 EmitThumbMappingSymbol();
469 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
470 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
471 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
472 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
473 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
478 llvm_unreachable("Invalid Suffix");
481 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
484 /// This is one of the functions used to emit data into an ELF section, so the
485 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
487 virtual void EmitBytes(StringRef Data) {
488 EmitDataMappingSymbol();
489 MCELFStreamer::EmitBytes(Data);
492 /// This is one of the functions used to emit data into an ELF section, so the
493 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
495 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
496 EmitDataMappingSymbol();
497 MCELFStreamer::EmitValueImpl(Value, Size);
500 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
501 MCELFStreamer::EmitAssemblerFlag(Flag);
504 case MCAF_SyntaxUnified:
505 return; // no-op here.
508 return; // Change to Thumb mode
511 return; // Change to ARM mode
514 case MCAF_SubsectionsViaSymbols:
520 enum ElfMappingSymbol {
527 void EmitDataMappingSymbol() {
528 if (LastEMS == EMS_Data) return;
529 EmitMappingSymbol("$d");
533 void EmitThumbMappingSymbol() {
534 if (LastEMS == EMS_Thumb) return;
535 EmitMappingSymbol("$t");
539 void EmitARMMappingSymbol() {
540 if (LastEMS == EMS_ARM) return;
541 EmitMappingSymbol("$a");
545 void EmitMappingSymbol(StringRef Name) {
546 MCSymbol *Start = getContext().CreateTempSymbol();
550 getContext().GetOrCreateSymbol(Name + "." +
551 Twine(MappingSymbolCounter++));
553 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
554 MCELF::SetType(SD, ELF::STT_NOTYPE);
555 MCELF::SetBinding(SD, ELF::STB_LOCAL);
556 SD.setExternal(false);
557 AssignSection(Symbol, getCurrentSection().first);
559 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
560 Symbol->setVariableValue(Value);
563 void EmitThumbFunc(MCSymbol *Func) {
564 // FIXME: Anything needed here to flag the function as thumb?
566 getAssembler().setIsThumbFunc(Func);
568 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
569 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
572 // Helper functions for ARM exception handling directives
575 void EmitPersonalityFixup(StringRef Name);
576 void FlushPendingOffset();
577 void FlushUnwindOpcodes(bool NoHandlerData);
579 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
580 SectionKind Kind, const MCSymbol &Fn);
581 void SwitchToExTabSection(const MCSymbol &FnStart);
582 void SwitchToExIdxSection(const MCSymbol &FnStart);
585 int64_t MappingSymbolCounter;
587 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
588 ElfMappingSymbol LastEMS;
590 // ARM Exception Handling Frame Information
593 const MCSymbol *Personality;
594 unsigned PersonalityIndex;
595 unsigned FPReg; // Frame pointer register
596 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
597 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
598 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
601 SmallVector<uint8_t, 64> Opcodes;
602 UnwindOpcodeAssembler UnwindOpAsm;
604 } // end anonymous namespace
606 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
607 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
611 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
612 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
613 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
614 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
615 getStreamer().emitPersonality(Personality);
617 void ARMTargetELFStreamer::emitHandlerData() {
618 getStreamer().emitHandlerData();
620 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
622 getStreamer().emitSetFP(FpReg, SpReg, Offset);
624 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
625 getStreamer().emitPad(Offset);
627 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
629 getStreamer().emitRegSave(RegList, isVector);
631 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
632 assert(!Vendor.empty() && "Vendor cannot be empty.");
634 if (CurrentVendor == Vendor)
637 if (!CurrentVendor.empty())
638 finishAttributeSection();
640 assert(Contents.empty() &&
641 ".ARM.attributes should be flushed before changing vendor");
642 CurrentVendor = Vendor;
645 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
646 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
648 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
650 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
652 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
654 StringRef StringValue) {
655 setAttributeItems(Attribute, IntValue, StringValue,
656 /* OverwriteExisting= */ true);
658 void ARMTargetELFStreamer::emitArch(unsigned Value) {
661 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
662 using namespace ARMBuildAttrs;
663 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
664 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
673 setAttributeItem(ARM_ISA_use, Allowed, false);
681 setAttributeItem(ARM_ISA_use, Allowed, false);
682 setAttributeItem(THUMB_ISA_use, Allowed, false);
686 setAttributeItem(ARM_ISA_use, Allowed, false);
687 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
692 setAttributeItem(ARM_ISA_use, Allowed, false);
693 setAttributeItem(THUMB_ISA_use, Allowed, false);
694 setAttributeItem(Virtualization_use, AllowTZ, false);
698 setAttributeItem(THUMB_ISA_use, Allowed, false);
702 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
706 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
707 setAttributeItem(ARM_ISA_use, Allowed, false);
708 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
712 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
713 setAttributeItem(ARM_ISA_use, Allowed, false);
714 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
718 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
719 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
723 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
724 setAttributeItem(ARM_ISA_use, Allowed, false);
725 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
726 setAttributeItem(MPextension_use, Allowed, false);
727 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
731 setAttributeItem(ARM_ISA_use, Allowed, false);
732 setAttributeItem(THUMB_ISA_use, Allowed, false);
733 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
737 setAttributeItem(ARM_ISA_use, Allowed, false);
738 setAttributeItem(THUMB_ISA_use, Allowed, false);
739 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
743 report_fatal_error("Unknown Arch: " + Twine(Arch));
747 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
750 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
754 setAttributeItem(ARMBuildAttrs::FP_arch,
755 ARMBuildAttrs::AllowFPv2,
756 /* OverwriteExisting= */ false);
760 setAttributeItem(ARMBuildAttrs::FP_arch,
761 ARMBuildAttrs::AllowFPv3A,
762 /* OverwriteExisting= */ false);
766 setAttributeItem(ARMBuildAttrs::FP_arch,
767 ARMBuildAttrs::AllowFPv3B,
768 /* OverwriteExisting= */ false);
772 setAttributeItem(ARMBuildAttrs::FP_arch,
773 ARMBuildAttrs::AllowFPv4A,
774 /* OverwriteExisting= */ false);
778 setAttributeItem(ARMBuildAttrs::FP_arch,
779 ARMBuildAttrs::AllowFPv4B,
780 /* OverwriteExisting= */ false);
784 setAttributeItem(ARMBuildAttrs::FP_arch,
785 ARMBuildAttrs::AllowFPARMv8A,
786 /* OverwriteExisting= */ false);
790 setAttributeItem(ARMBuildAttrs::FP_arch,
791 ARMBuildAttrs::AllowFPv3A,
792 /* OverwriteExisting= */ false);
793 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
794 ARMBuildAttrs::AllowNeon,
795 /* OverwriteExisting= */ false);
798 case ARM::NEON_VFPV4:
799 setAttributeItem(ARMBuildAttrs::FP_arch,
800 ARMBuildAttrs::AllowFPv4A,
801 /* OverwriteExisting= */ false);
802 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
803 ARMBuildAttrs::AllowNeon2,
804 /* OverwriteExisting= */ false);
807 case ARM::NEON_FP_ARMV8:
808 case ARM::CRYPTO_NEON_FP_ARMV8:
809 setAttributeItem(ARMBuildAttrs::FP_arch,
810 ARMBuildAttrs::AllowFPARMv8A,
811 /* OverwriteExisting= */ false);
812 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
813 ARMBuildAttrs::AllowNeonARMv8,
814 /* OverwriteExisting= */ false);
821 report_fatal_error("Unknown FPU: " + Twine(FPU));
825 size_t ARMTargetELFStreamer::calculateContentSize() const {
827 for (size_t i = 0; i < Contents.size(); ++i) {
828 AttributeItem item = Contents[i];
830 case AttributeItem::HiddenAttribute:
832 case AttributeItem::NumericAttribute:
833 Result += getULEBSize(item.Tag);
834 Result += getULEBSize(item.IntValue);
836 case AttributeItem::TextAttribute:
837 Result += getULEBSize(item.Tag);
838 Result += item.StringValue.size() + 1; // string + '\0'
840 case AttributeItem::NumericAndTextAttributes:
841 Result += getULEBSize(item.Tag);
842 Result += getULEBSize(item.IntValue);
843 Result += item.StringValue.size() + 1; // string + '\0';
849 void ARMTargetELFStreamer::finishAttributeSection() {
851 // [ <section-length> "vendor-name"
852 // [ <file-tag> <size> <attribute>*
853 // | <section-tag> <size> <section-number>* 0 <attribute>*
854 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
858 if (FPU != ARM::INVALID_FPU)
859 emitFPUDefaultAttributes();
861 if (Arch != ARM::INVALID_ARCH)
862 emitArchDefaultAttributes();
864 if (Contents.empty())
867 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
869 ARMELFStreamer &Streamer = getStreamer();
871 // Switch to .ARM.attributes section
872 if (AttributeSection) {
873 Streamer.SwitchSection(AttributeSection);
876 Streamer.getContext().getELFSection(".ARM.attributes",
877 ELF::SHT_ARM_ATTRIBUTES,
879 SectionKind::getMetadata());
880 Streamer.SwitchSection(AttributeSection);
883 Streamer.EmitIntValue(0x41, 1);
886 // Vendor size + Vendor name + '\0'
887 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
890 const size_t TagHeaderSize = 1 + 4;
892 const size_t ContentsSize = calculateContentSize();
894 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
895 Streamer.EmitBytes(CurrentVendor);
896 Streamer.EmitIntValue(0, 1); // '\0'
898 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
899 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
901 // Size should have been accounted for already, now
902 // emit each field as its type (ULEB or String)
903 for (size_t i = 0; i < Contents.size(); ++i) {
904 AttributeItem item = Contents[i];
905 Streamer.EmitULEB128IntValue(item.Tag);
907 default: llvm_unreachable("Invalid attribute type");
908 case AttributeItem::NumericAttribute:
909 Streamer.EmitULEB128IntValue(item.IntValue);
911 case AttributeItem::TextAttribute:
912 Streamer.EmitBytes(item.StringValue.upper());
913 Streamer.EmitIntValue(0, 1); // '\0'
915 case AttributeItem::NumericAndTextAttributes:
916 Streamer.EmitULEB128IntValue(item.IntValue);
917 Streamer.EmitBytes(item.StringValue.upper());
918 Streamer.EmitIntValue(0, 1); // '\0'
924 FPU = ARM::INVALID_FPU;
926 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
927 getStreamer().emitInst(Inst, Suffix);
930 void ARMELFStreamer::FinishImpl() {
931 MCTargetStreamer &TS = *getTargetStreamer();
932 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
933 ATS.finishAttributeSection();
935 MCELFStreamer::FinishImpl();
938 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
942 const MCSymbol &Fn) {
943 const MCSectionELF &FnSection =
944 static_cast<const MCSectionELF &>(Fn.getSection());
946 // Create the name for new section
947 StringRef FnSecName(FnSection.getSectionName());
948 SmallString<128> EHSecName(Prefix);
949 if (FnSecName != ".text") {
950 EHSecName += FnSecName;
953 // Get .ARM.extab or .ARM.exidx section
954 const MCSectionELF *EHSection = NULL;
955 if (const MCSymbol *Group = FnSection.getGroup()) {
956 EHSection = getContext().getELFSection(
957 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
958 FnSection.getEntrySize(), Group->getName());
960 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
962 assert(EHSection && "Failed to get the required EH section");
964 // Switch to .ARM.extab or .ARM.exidx section
965 SwitchSection(EHSection);
966 EmitCodeAlignment(4, 0);
969 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
970 SwitchToEHSection(".ARM.extab",
973 SectionKind::getDataRel(),
977 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
978 SwitchToEHSection(".ARM.exidx",
980 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
981 SectionKind::getDataRel(),
985 void ARMELFStreamer::Reset() {
989 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1001 void ARMELFStreamer::emitFnStart() {
1002 assert(FnStart == 0);
1003 FnStart = getContext().CreateTempSymbol();
1007 void ARMELFStreamer::emitFnEnd() {
1008 assert(FnStart && ".fnstart must preceeds .fnend");
1010 // Emit unwind opcodes if there is no .handlerdata directive
1011 if (!ExTab && !CantUnwind)
1012 FlushUnwindOpcodes(true);
1014 // Emit the exception index table entry
1015 SwitchToExIdxSection(*FnStart);
1017 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1018 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1020 const MCSymbolRefExpr *FnStartRef =
1021 MCSymbolRefExpr::Create(FnStart,
1022 MCSymbolRefExpr::VK_ARM_PREL31,
1025 EmitValue(FnStartRef, 4);
1028 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1030 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1031 const MCSymbolRefExpr *ExTabEntryRef =
1032 MCSymbolRefExpr::Create(ExTab,
1033 MCSymbolRefExpr::VK_ARM_PREL31,
1035 EmitValue(ExTabEntryRef, 4);
1037 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1038 // the second word of exception index table entry. The size of the unwind
1039 // opcodes should always be 4 bytes.
1040 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1041 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1042 assert(Opcodes.size() == 4u &&
1043 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1044 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1048 // Switch to the section containing FnStart
1049 SwitchSection(&FnStart->getSection());
1051 // Clean exception handling frame information
1055 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1057 // Add the R_ARM_NONE fixup at the same position
1058 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1059 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1061 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1062 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1064 AddValueSymbols(PersonalityRef);
1065 MCDataFragment *DF = getOrCreateDataFragment();
1066 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1068 MCFixup::getKindForSize(4, false)));
1071 void ARMELFStreamer::FlushPendingOffset() {
1072 if (PendingOffset != 0) {
1073 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1078 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1079 // Emit the unwind opcode to restore $sp.
1081 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1082 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1083 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1084 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1086 FlushPendingOffset();
1089 // Finalize the unwind opcode sequence
1090 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1092 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1093 // section. Thus, we don't have to create an entry in the .ARM.extab
1095 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1098 // Switch to .ARM.extab section.
1099 SwitchToExTabSection(*FnStart);
1101 // Create .ARM.extab label for offset in .ARM.exidx
1103 ExTab = getContext().CreateTempSymbol();
1108 const MCSymbolRefExpr *PersonalityRef =
1109 MCSymbolRefExpr::Create(Personality,
1110 MCSymbolRefExpr::VK_ARM_PREL31,
1113 EmitValue(PersonalityRef, 4);
1116 // Emit unwind opcodes
1117 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1120 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1121 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1122 // after the unwind opcodes. The handler data consists of several 32-bit
1123 // words, and should be terminated by zero.
1125 // In case that the .handlerdata directive is not specified by the
1126 // programmer, we should emit zero to terminate the handler data.
1127 if (NoHandlerData && !Personality)
1131 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1133 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1135 UnwindOpAsm.setPersonality(Per);
1138 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1140 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1141 "the operand of .setfp directive should be either $sp or $fp");
1146 if (NewSPReg == ARM::SP)
1147 FPOffset = SPOffset + Offset;
1152 void ARMELFStreamer::emitPad(int64_t Offset) {
1153 // Track the change of the $sp offset
1156 // To squash multiple .pad directives, we should delay the unwind opcode
1157 // until the .save, .vsave, .handlerdata, or .fnend directives.
1158 PendingOffset -= Offset;
1161 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1163 // Collect the registers in the register list
1166 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1167 for (size_t i = 0; i < RegList.size(); ++i) {
1168 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1169 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1170 unsigned Bit = (1u << Reg);
1171 if ((Mask & Bit) == 0) {
1177 // Track the change the $sp offset: For the .save directive, the
1178 // corresponding push instruction will decrease the $sp by (4 * Count).
1179 // For the .vsave directive, the corresponding vpush instruction will
1180 // decrease $sp by (8 * Count).
1181 SPOffset -= Count * (IsVector ? 8 : 4);
1184 FlushPendingOffset();
1186 UnwindOpAsm.EmitVFPRegSave(Mask);
1188 UnwindOpAsm.EmitRegSave(Mask);
1193 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1194 bool isVerboseAsm, bool useLoc, bool useCFI,
1195 bool useDwarfDirectory,
1196 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1197 MCAsmBackend *TAB, bool ShowInst) {
1198 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1201 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1202 useDwarfDirectory, InstPrint, CE, TAB,
1206 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1207 raw_ostream &OS, MCCodeEmitter *Emitter,
1208 bool RelaxAll, bool NoExecStack,
1210 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1212 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1213 // FIXME: This should eventually end up somewhere else where more
1214 // intelligent flag decisions can be made. For now we are just maintaining
1215 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1216 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1219 S->getAssembler().setRelaxAll(true);
1221 S->getAssembler().setNoExecStack(true);