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 "ARMBuildAttrs.h"
17 #include "ARMArchName.h"
18 #include "ARMFPUName.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMUnwindOpAsm.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCAssembler.h"
27 #include "llvm/MC/MCCodeEmitter.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCELF.h"
30 #include "llvm/MC/MCELFStreamer.h"
31 #include "llvm/MC/MCELFSymbolFlags.h"
32 #include "llvm/MC/MCExpr.h"
33 #include "llvm/MC/MCInst.h"
34 #include "llvm/MC/MCInstPrinter.h"
35 #include "llvm/MC/MCObjectStreamer.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/MCSymbol.h"
41 #include "llvm/MC/MCValue.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->IntValue = Value;
297 // Create new attribute item
298 AttributeItem Item = {
299 AttributeItem::NumericAttribute,
304 Contents.push_back(Item);
307 void setAttributeItem(unsigned Attribute, StringRef Value,
308 bool OverwriteExisting) {
309 // Look for existing attribute item
310 if (AttributeItem *Item = getAttributeItem(Attribute)) {
311 if (!OverwriteExisting)
313 Item->StringValue = Value;
317 // Create new attribute item
318 AttributeItem Item = {
319 AttributeItem::TextAttribute,
324 Contents.push_back(Item);
327 void setAttributeItems(unsigned Attribute, unsigned IntValue,
328 StringRef StringValue, bool OverwriteExisting) {
329 // Look for existing attribute item
330 if (AttributeItem *Item = getAttributeItem(Attribute)) {
331 if (!OverwriteExisting)
333 Item->IntValue = IntValue;
334 Item->StringValue = StringValue;
338 // Create new attribute item
339 AttributeItem Item = {
340 AttributeItem::NumericAndTextAttributes,
345 Contents.push_back(Item);
348 void emitArchDefaultAttributes();
349 void emitFPUDefaultAttributes();
351 ARMELFStreamer &getStreamer();
353 virtual void emitFnStart();
354 virtual void emitFnEnd();
355 virtual void emitCantUnwind();
356 virtual void emitPersonality(const MCSymbol *Personality);
357 virtual void emitHandlerData();
358 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
359 virtual void emitPad(int64_t Offset);
360 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
363 virtual void switchVendor(StringRef Vendor);
364 virtual void emitAttribute(unsigned Attribute, unsigned Value);
365 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
366 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
367 StringRef StringValue);
368 virtual void emitArch(unsigned Arch);
369 virtual void emitFPU(unsigned FPU);
370 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
371 virtual void finishAttributeSection();
373 size_t calculateContentSize() const;
376 ARMTargetELFStreamer()
377 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
378 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
382 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
383 /// the appropriate points in the object files. These symbols are defined in the
384 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
386 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
387 /// region of ARM code, Thumb code or data in a section. In practice, this
388 /// emission does not rely on explicit assembler directives but on inherent
389 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
390 /// r0, r0, r0" an instruction).
392 /// As a result this system is orthogonal to the DataRegion infrastructure used
393 /// by MachO. Beware!
394 class ARMELFStreamer : public MCELFStreamer {
396 friend class ARMTargetELFStreamer;
398 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
399 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
401 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
402 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
408 virtual void FinishImpl();
410 // ARM exception handling directives
413 void emitCantUnwind();
414 void emitPersonality(const MCSymbol *Per);
415 void emitHandlerData();
416 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
417 void emitPad(int64_t Offset);
418 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
420 virtual void ChangeSection(const MCSection *Section,
421 const MCExpr *Subsection) {
422 // We have to keep track of the mapping symbol state of any sections we
423 // use. Each one should start off as EMS_None, which is provided as the
424 // default constructor by DenseMap::lookup.
425 LastMappingSymbols[getPreviousSection().first] = LastEMS;
426 LastEMS = LastMappingSymbols.lookup(Section);
428 MCELFStreamer::ChangeSection(Section, Subsection);
431 /// This function is the one used to emit instruction data into the ELF
432 /// streamer. We override it to add the appropriate mapping symbol if
434 virtual void EmitInstruction(const MCInst& Inst) {
436 EmitThumbMappingSymbol();
438 EmitARMMappingSymbol();
440 MCELFStreamer::EmitInstruction(Inst);
443 virtual void emitInst(uint32_t Inst, char Suffix) {
446 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
453 EmitARMMappingSymbol();
454 for (unsigned II = 0, IE = Size; II != IE; II++) {
455 const unsigned I = LittleEndian ? (Size - II - 1) : II;
456 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
462 Size = (Suffix == 'n' ? 2 : 4);
465 EmitThumbMappingSymbol();
466 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
467 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
468 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
469 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
470 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
475 llvm_unreachable("Invalid Suffix");
478 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
481 /// This is one of the functions used to emit data into an ELF section, so the
482 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
484 virtual void EmitBytes(StringRef Data) {
485 EmitDataMappingSymbol();
486 MCELFStreamer::EmitBytes(Data);
489 /// This is one of the functions used to emit data into an ELF section, so the
490 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
492 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
493 EmitDataMappingSymbol();
494 MCELFStreamer::EmitValueImpl(Value, Size);
497 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
498 MCELFStreamer::EmitAssemblerFlag(Flag);
501 case MCAF_SyntaxUnified:
502 return; // no-op here.
505 return; // Change to Thumb mode
508 return; // Change to ARM mode
511 case MCAF_SubsectionsViaSymbols:
517 enum ElfMappingSymbol {
524 void EmitDataMappingSymbol() {
525 if (LastEMS == EMS_Data) return;
526 EmitMappingSymbol("$d");
530 void EmitThumbMappingSymbol() {
531 if (LastEMS == EMS_Thumb) return;
532 EmitMappingSymbol("$t");
536 void EmitARMMappingSymbol() {
537 if (LastEMS == EMS_ARM) return;
538 EmitMappingSymbol("$a");
542 void EmitMappingSymbol(StringRef Name) {
543 MCSymbol *Start = getContext().CreateTempSymbol();
547 getContext().GetOrCreateSymbol(Name + "." +
548 Twine(MappingSymbolCounter++));
550 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
551 MCELF::SetType(SD, ELF::STT_NOTYPE);
552 MCELF::SetBinding(SD, ELF::STB_LOCAL);
553 SD.setExternal(false);
554 AssignSection(Symbol, getCurrentSection().first);
556 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
557 Symbol->setVariableValue(Value);
560 void EmitThumbFunc(MCSymbol *Func) {
561 // FIXME: Anything needed here to flag the function as thumb?
563 getAssembler().setIsThumbFunc(Func);
565 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
566 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
569 // Helper functions for ARM exception handling directives
572 void EmitPersonalityFixup(StringRef Name);
573 void FlushPendingOffset();
574 void FlushUnwindOpcodes(bool NoHandlerData);
576 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
577 SectionKind Kind, const MCSymbol &Fn);
578 void SwitchToExTabSection(const MCSymbol &FnStart);
579 void SwitchToExIdxSection(const MCSymbol &FnStart);
582 int64_t MappingSymbolCounter;
584 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
585 ElfMappingSymbol LastEMS;
587 // ARM Exception Handling Frame Information
590 const MCSymbol *Personality;
591 unsigned PersonalityIndex;
592 unsigned FPReg; // Frame pointer register
593 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
594 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
595 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
598 SmallVector<uint8_t, 64> Opcodes;
599 UnwindOpcodeAssembler UnwindOpAsm;
601 } // end anonymous namespace
603 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
604 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
608 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
609 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
610 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
611 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
612 getStreamer().emitPersonality(Personality);
614 void ARMTargetELFStreamer::emitHandlerData() {
615 getStreamer().emitHandlerData();
617 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
619 getStreamer().emitSetFP(FpReg, SpReg, Offset);
621 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
622 getStreamer().emitPad(Offset);
624 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
626 getStreamer().emitRegSave(RegList, isVector);
628 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
629 assert(!Vendor.empty() && "Vendor cannot be empty.");
631 if (CurrentVendor == Vendor)
634 if (!CurrentVendor.empty())
635 finishAttributeSection();
637 assert(Contents.empty() &&
638 ".ARM.attributes should be flushed before changing vendor");
639 CurrentVendor = Vendor;
642 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
643 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
645 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
647 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
649 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
651 StringRef StringValue) {
652 setAttributeItems(Attribute, IntValue, StringValue,
653 /* OverwriteExisting= */ true);
655 void ARMTargetELFStreamer::emitArch(unsigned Value) {
658 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
659 using namespace ARMBuildAttrs;
660 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
661 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
670 setAttributeItem(ARM_ISA_use, Allowed, false);
678 setAttributeItem(ARM_ISA_use, Allowed, false);
679 setAttributeItem(THUMB_ISA_use, Allowed, false);
683 setAttributeItem(ARM_ISA_use, Allowed, false);
684 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
689 setAttributeItem(ARM_ISA_use, Allowed, false);
690 setAttributeItem(THUMB_ISA_use, Allowed, false);
691 setAttributeItem(Virtualization_use, AllowTZ, false);
695 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
696 setAttributeItem(THUMB_ISA_use, Allowed, false);
700 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
704 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
705 setAttributeItem(ARM_ISA_use, Allowed, false);
706 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
710 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
711 setAttributeItem(ARM_ISA_use, Allowed, false);
712 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
716 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
717 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
721 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
722 setAttributeItem(ARM_ISA_use, Allowed, false);
723 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
724 setAttributeItem(MPextension_use, Allowed, false);
725 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
729 setAttributeItem(ARM_ISA_use, Allowed, false);
730 setAttributeItem(THUMB_ISA_use, Allowed, false);
731 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
735 setAttributeItem(ARM_ISA_use, Allowed, false);
736 setAttributeItem(THUMB_ISA_use, Allowed, false);
737 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
741 report_fatal_error("Unknown Arch: " + Twine(Arch));
745 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
748 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
752 setAttributeItem(ARMBuildAttrs::FP_arch,
753 ARMBuildAttrs::AllowFPv2,
754 /* OverwriteExisting= */ false);
758 setAttributeItem(ARMBuildAttrs::FP_arch,
759 ARMBuildAttrs::AllowFPv3A,
760 /* OverwriteExisting= */ false);
764 setAttributeItem(ARMBuildAttrs::FP_arch,
765 ARMBuildAttrs::AllowFPv3B,
766 /* OverwriteExisting= */ false);
770 setAttributeItem(ARMBuildAttrs::FP_arch,
771 ARMBuildAttrs::AllowFPv4A,
772 /* OverwriteExisting= */ false);
776 setAttributeItem(ARMBuildAttrs::FP_arch,
777 ARMBuildAttrs::AllowFPv4B,
778 /* OverwriteExisting= */ false);
782 setAttributeItem(ARMBuildAttrs::FP_arch,
783 ARMBuildAttrs::AllowFPARMv8A,
784 /* OverwriteExisting= */ false);
788 setAttributeItem(ARMBuildAttrs::FP_arch,
789 ARMBuildAttrs::AllowFPv3A,
790 /* OverwriteExisting= */ false);
791 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
792 ARMBuildAttrs::AllowNeon,
793 /* OverwriteExisting= */ false);
796 case ARM::NEON_VFPV4:
797 setAttributeItem(ARMBuildAttrs::FP_arch,
798 ARMBuildAttrs::AllowFPv4A,
799 /* OverwriteExisting= */ false);
800 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
801 ARMBuildAttrs::AllowNeon2,
802 /* OverwriteExisting= */ false);
805 case ARM::NEON_FP_ARMV8:
806 case ARM::CRYPTO_NEON_FP_ARMV8:
807 setAttributeItem(ARMBuildAttrs::FP_arch,
808 ARMBuildAttrs::AllowFPARMv8A,
809 /* OverwriteExisting= */ false);
810 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
811 ARMBuildAttrs::AllowNeonARMv8,
812 /* OverwriteExisting= */ false);
819 report_fatal_error("Unknown FPU: " + Twine(FPU));
823 size_t ARMTargetELFStreamer::calculateContentSize() const {
825 for (size_t i = 0; i < Contents.size(); ++i) {
826 AttributeItem item = Contents[i];
828 case AttributeItem::HiddenAttribute:
830 case AttributeItem::NumericAttribute:
831 Result += getULEBSize(item.Tag);
832 Result += getULEBSize(item.IntValue);
834 case AttributeItem::TextAttribute:
835 Result += getULEBSize(item.Tag);
836 Result += item.StringValue.size() + 1; // string + '\0'
838 case AttributeItem::NumericAndTextAttributes:
839 Result += getULEBSize(item.Tag);
840 Result += getULEBSize(item.IntValue);
841 Result += item.StringValue.size() + 1; // string + '\0';
847 void ARMTargetELFStreamer::finishAttributeSection() {
849 // [ <section-length> "vendor-name"
850 // [ <file-tag> <size> <attribute>*
851 // | <section-tag> <size> <section-number>* 0 <attribute>*
852 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
856 if (FPU != ARM::INVALID_FPU)
857 emitFPUDefaultAttributes();
859 if (Arch != ARM::INVALID_ARCH)
860 emitArchDefaultAttributes();
862 if (Contents.empty())
865 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
867 ARMELFStreamer &Streamer = getStreamer();
869 // Switch to .ARM.attributes section
870 if (AttributeSection) {
871 Streamer.SwitchSection(AttributeSection);
874 Streamer.getContext().getELFSection(".ARM.attributes",
875 ELF::SHT_ARM_ATTRIBUTES,
877 SectionKind::getMetadata());
878 Streamer.SwitchSection(AttributeSection);
881 Streamer.EmitIntValue(0x41, 1);
884 // Vendor size + Vendor name + '\0'
885 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
888 const size_t TagHeaderSize = 1 + 4;
890 const size_t ContentsSize = calculateContentSize();
892 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
893 Streamer.EmitBytes(CurrentVendor);
894 Streamer.EmitIntValue(0, 1); // '\0'
896 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
897 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
899 // Size should have been accounted for already, now
900 // emit each field as its type (ULEB or String)
901 for (size_t i = 0; i < Contents.size(); ++i) {
902 AttributeItem item = Contents[i];
903 Streamer.EmitULEB128IntValue(item.Tag);
905 default: llvm_unreachable("Invalid attribute type");
906 case AttributeItem::NumericAttribute:
907 Streamer.EmitULEB128IntValue(item.IntValue);
909 case AttributeItem::TextAttribute:
910 Streamer.EmitBytes(item.StringValue.upper());
911 Streamer.EmitIntValue(0, 1); // '\0'
913 case AttributeItem::NumericAndTextAttributes:
914 Streamer.EmitULEB128IntValue(item.IntValue);
915 Streamer.EmitBytes(item.StringValue.upper());
916 Streamer.EmitIntValue(0, 1); // '\0'
922 FPU = ARM::INVALID_FPU;
924 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
925 getStreamer().emitInst(Inst, Suffix);
928 void ARMELFStreamer::FinishImpl() {
929 MCTargetStreamer &TS = getTargetStreamer();
930 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
931 ATS.finishAttributeSection();
933 MCELFStreamer::FinishImpl();
936 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
940 const MCSymbol &Fn) {
941 const MCSectionELF &FnSection =
942 static_cast<const MCSectionELF &>(Fn.getSection());
944 // Create the name for new section
945 StringRef FnSecName(FnSection.getSectionName());
946 SmallString<128> EHSecName(Prefix);
947 if (FnSecName != ".text") {
948 EHSecName += FnSecName;
951 // Get .ARM.extab or .ARM.exidx section
952 const MCSectionELF *EHSection = NULL;
953 if (const MCSymbol *Group = FnSection.getGroup()) {
954 EHSection = getContext().getELFSection(
955 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
956 FnSection.getEntrySize(), Group->getName());
958 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
960 assert(EHSection && "Failed to get the required EH section");
962 // Switch to .ARM.extab or .ARM.exidx section
963 SwitchSection(EHSection);
964 EmitCodeAlignment(4, 0);
967 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
968 SwitchToEHSection(".ARM.extab",
971 SectionKind::getDataRel(),
975 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
976 SwitchToEHSection(".ARM.exidx",
978 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
979 SectionKind::getDataRel(),
983 void ARMELFStreamer::Reset() {
987 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
999 void ARMELFStreamer::emitFnStart() {
1000 assert(FnStart == 0);
1001 FnStart = getContext().CreateTempSymbol();
1005 void ARMELFStreamer::emitFnEnd() {
1006 assert(FnStart && ".fnstart must preceeds .fnend");
1008 // Emit unwind opcodes if there is no .handlerdata directive
1009 if (!ExTab && !CantUnwind)
1010 FlushUnwindOpcodes(true);
1012 // Emit the exception index table entry
1013 SwitchToExIdxSection(*FnStart);
1015 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1016 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1018 const MCSymbolRefExpr *FnStartRef =
1019 MCSymbolRefExpr::Create(FnStart,
1020 MCSymbolRefExpr::VK_ARM_PREL31,
1023 EmitValue(FnStartRef, 4);
1026 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1028 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1029 const MCSymbolRefExpr *ExTabEntryRef =
1030 MCSymbolRefExpr::Create(ExTab,
1031 MCSymbolRefExpr::VK_ARM_PREL31,
1033 EmitValue(ExTabEntryRef, 4);
1035 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1036 // the second word of exception index table entry. The size of the unwind
1037 // opcodes should always be 4 bytes.
1038 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1039 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1040 assert(Opcodes.size() == 4u &&
1041 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1042 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1046 // Switch to the section containing FnStart
1047 SwitchSection(&FnStart->getSection());
1049 // Clean exception handling frame information
1053 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1055 // Add the R_ARM_NONE fixup at the same position
1056 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1057 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1059 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1060 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1062 AddValueSymbols(PersonalityRef);
1063 MCDataFragment *DF = getOrCreateDataFragment();
1064 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1066 MCFixup::getKindForSize(4, false)));
1069 void ARMELFStreamer::FlushPendingOffset() {
1070 if (PendingOffset != 0) {
1071 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1076 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1077 // Emit the unwind opcode to restore $sp.
1079 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1080 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1081 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1082 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1084 FlushPendingOffset();
1087 // Finalize the unwind opcode sequence
1088 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1090 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1091 // section. Thus, we don't have to create an entry in the .ARM.extab
1093 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1096 // Switch to .ARM.extab section.
1097 SwitchToExTabSection(*FnStart);
1099 // Create .ARM.extab label for offset in .ARM.exidx
1101 ExTab = getContext().CreateTempSymbol();
1106 const MCSymbolRefExpr *PersonalityRef =
1107 MCSymbolRefExpr::Create(Personality,
1108 MCSymbolRefExpr::VK_ARM_PREL31,
1111 EmitValue(PersonalityRef, 4);
1114 // Emit unwind opcodes
1115 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1118 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1119 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1120 // after the unwind opcodes. The handler data consists of several 32-bit
1121 // words, and should be terminated by zero.
1123 // In case that the .handlerdata directive is not specified by the
1124 // programmer, we should emit zero to terminate the handler data.
1125 if (NoHandlerData && !Personality)
1129 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1131 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1133 UnwindOpAsm.setPersonality(Per);
1136 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1138 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1139 "the operand of .setfp directive should be either $sp or $fp");
1144 if (NewSPReg == ARM::SP)
1145 FPOffset = SPOffset + Offset;
1150 void ARMELFStreamer::emitPad(int64_t Offset) {
1151 // Track the change of the $sp offset
1154 // To squash multiple .pad directives, we should delay the unwind opcode
1155 // until the .save, .vsave, .handlerdata, or .fnend directives.
1156 PendingOffset -= Offset;
1159 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1161 // Collect the registers in the register list
1164 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1165 for (size_t i = 0; i < RegList.size(); ++i) {
1166 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1167 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1168 unsigned Bit = (1u << Reg);
1169 if ((Mask & Bit) == 0) {
1175 // Track the change the $sp offset: For the .save directive, the
1176 // corresponding push instruction will decrease the $sp by (4 * Count).
1177 // For the .vsave directive, the corresponding vpush instruction will
1178 // decrease $sp by (8 * Count).
1179 SPOffset -= Count * (IsVector ? 8 : 4);
1182 FlushPendingOffset();
1184 UnwindOpAsm.EmitVFPRegSave(Mask);
1186 UnwindOpAsm.EmitRegSave(Mask);
1191 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1192 bool isVerboseAsm, bool useLoc, bool useCFI,
1193 bool useDwarfDirectory,
1194 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1195 MCAsmBackend *TAB, bool ShowInst) {
1196 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1199 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1200 useDwarfDirectory, InstPrint, CE, TAB,
1204 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1205 raw_ostream &OS, MCCodeEmitter *Emitter,
1206 bool RelaxAll, bool NoExecStack,
1208 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1210 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1211 // FIXME: This should eventually end up somewhere else where more
1212 // intelligent flag decisions can be made. For now we are just maintaining
1213 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1214 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1217 S->getAssembler().setRelaxAll(true);
1219 S->getAssembler().setNoExecStack(true);