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;
115 virtual void emitFnStart();
116 virtual void emitFnEnd();
117 virtual void emitCantUnwind();
118 virtual void emitPersonality(const MCSymbol *Personality);
119 virtual void emitHandlerData();
120 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
121 virtual void emitPad(int64_t Offset);
122 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
125 virtual void switchVendor(StringRef Vendor);
126 virtual void emitAttribute(unsigned Attribute, unsigned Value);
127 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
128 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
129 StringRef StrinValue);
130 virtual void emitArch(unsigned Arch);
131 virtual void emitFPU(unsigned FPU);
132 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
133 virtual void finishAttributeSection();
136 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter);
139 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
140 MCInstPrinter &InstPrinter)
141 : OS(OS), InstPrinter(InstPrinter) {}
142 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
143 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
144 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
145 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
146 OS << "\t.personality " << Personality->getName() << '\n';
148 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
149 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
152 InstPrinter.printRegName(OS, FpReg);
154 InstPrinter.printRegName(OS, SpReg);
156 OS << ", #" << Offset;
159 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
160 OS << "\t.pad\t#" << Offset << '\n';
162 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
164 assert(RegList.size() && "RegList should not be empty");
170 InstPrinter.printRegName(OS, RegList[0]);
172 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
174 InstPrinter.printRegName(OS, RegList[i]);
179 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
181 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
182 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
184 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
187 case ARMBuildAttrs::CPU_name:
188 OS << "\t.cpu\t" << String.lower();
191 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
196 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
198 StringRef StringValue) {
200 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
201 case ARMBuildAttrs::compatibility:
202 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
203 if (!StringValue.empty())
204 OS << ", \"" << StringValue << "\"";
209 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
210 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
212 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
213 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
215 void ARMTargetAsmStreamer::finishAttributeSection() {
218 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
222 OS << "\t0x" << utohexstr(Inst) << "\n";
225 class ARMTargetELFStreamer : public ARMTargetStreamer {
227 // This structure holds all attributes, accounting for
228 // their string/numeric value, so we can later emmit them
229 // in declaration order, keeping all in the same vector
230 struct AttributeItem {
235 NumericAndTextAttributes
239 StringRef StringValue;
241 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
242 return (LHS.Tag < RHS.Tag);
246 StringRef CurrentVendor;
249 SmallVector<AttributeItem, 64> Contents;
251 const MCSection *AttributeSection;
253 // FIXME: this should be in a more generic place, but
254 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
255 static size_t getULEBSize(int Value) {
259 Size += sizeof(int8_t); // Is this really necessary?
264 AttributeItem *getAttributeItem(unsigned Attribute) {
265 for (size_t i = 0; i < Contents.size(); ++i)
266 if (Contents[i].Tag == Attribute)
271 void setAttributeItem(unsigned Attribute, unsigned Value,
272 bool OverwriteExisting) {
273 // Look for existing attribute item
274 if (AttributeItem *Item = getAttributeItem(Attribute)) {
275 if (!OverwriteExisting)
277 Item->IntValue = Value;
281 // Create new attribute item
282 AttributeItem Item = {
283 AttributeItem::NumericAttribute,
288 Contents.push_back(Item);
291 void setAttributeItem(unsigned Attribute, StringRef Value,
292 bool OverwriteExisting) {
293 // Look for existing attribute item
294 if (AttributeItem *Item = getAttributeItem(Attribute)) {
295 if (!OverwriteExisting)
297 Item->StringValue = Value;
301 // Create new attribute item
302 AttributeItem Item = {
303 AttributeItem::TextAttribute,
308 Contents.push_back(Item);
311 void setAttributeItems(unsigned Attribute, unsigned IntValue,
312 StringRef StringValue, bool OverwriteExisting) {
313 // Look for existing attribute item
314 if (AttributeItem *Item = getAttributeItem(Attribute)) {
315 if (!OverwriteExisting)
317 Item->IntValue = IntValue;
318 Item->StringValue = StringValue;
322 // Create new attribute item
323 AttributeItem Item = {
324 AttributeItem::NumericAndTextAttributes,
329 Contents.push_back(Item);
332 void emitArchDefaultAttributes();
333 void emitFPUDefaultAttributes();
335 ARMELFStreamer &getStreamer();
337 virtual void emitFnStart();
338 virtual void emitFnEnd();
339 virtual void emitCantUnwind();
340 virtual void emitPersonality(const MCSymbol *Personality);
341 virtual void emitHandlerData();
342 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
343 virtual void emitPad(int64_t Offset);
344 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
347 virtual void switchVendor(StringRef Vendor);
348 virtual void emitAttribute(unsigned Attribute, unsigned Value);
349 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
350 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
351 StringRef StringValue);
352 virtual void emitArch(unsigned Arch);
353 virtual void emitFPU(unsigned FPU);
354 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
355 virtual void finishAttributeSection();
357 size_t calculateContentSize() const;
360 ARMTargetELFStreamer()
361 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
362 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
366 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
367 /// the appropriate points in the object files. These symbols are defined in the
368 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
370 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
371 /// region of ARM code, Thumb code or data in a section. In practice, this
372 /// emission does not rely on explicit assembler directives but on inherent
373 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
374 /// r0, r0, r0" an instruction).
376 /// As a result this system is orthogonal to the DataRegion infrastructure used
377 /// by MachO. Beware!
378 class ARMELFStreamer : public MCELFStreamer {
380 friend class ARMTargetELFStreamer;
382 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
383 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
385 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
386 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
392 virtual void FinishImpl();
394 // ARM exception handling directives
397 void emitCantUnwind();
398 void emitPersonality(const MCSymbol *Per);
399 void emitHandlerData();
400 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
401 void emitPad(int64_t Offset);
402 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
404 virtual void ChangeSection(const MCSection *Section,
405 const MCExpr *Subsection) {
406 // We have to keep track of the mapping symbol state of any sections we
407 // use. Each one should start off as EMS_None, which is provided as the
408 // default constructor by DenseMap::lookup.
409 LastMappingSymbols[getPreviousSection().first] = LastEMS;
410 LastEMS = LastMappingSymbols.lookup(Section);
412 MCELFStreamer::ChangeSection(Section, Subsection);
415 /// This function is the one used to emit instruction data into the ELF
416 /// streamer. We override it to add the appropriate mapping symbol if
418 virtual void EmitInstruction(const MCInst& Inst) {
420 EmitThumbMappingSymbol();
422 EmitARMMappingSymbol();
424 MCELFStreamer::EmitInstruction(Inst);
427 virtual void emitInst(uint32_t Inst, char Suffix) {
430 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
437 EmitARMMappingSymbol();
438 for (unsigned II = 0, IE = Size; II != IE; II++) {
439 const unsigned I = LittleEndian ? (Size - II - 1) : II;
440 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
446 Size = (Suffix == 'n' ? 2 : 4);
449 EmitThumbMappingSymbol();
450 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
451 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
452 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
453 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
454 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
459 llvm_unreachable("Invalid Suffix");
462 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
465 /// This is one of the functions used to emit data into an ELF section, so the
466 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
468 virtual void EmitBytes(StringRef Data) {
469 EmitDataMappingSymbol();
470 MCELFStreamer::EmitBytes(Data);
473 /// This is one of the functions used to emit data into an ELF section, so the
474 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
476 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
477 EmitDataMappingSymbol();
478 MCELFStreamer::EmitValueImpl(Value, Size);
481 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
482 MCELFStreamer::EmitAssemblerFlag(Flag);
485 case MCAF_SyntaxUnified:
486 return; // no-op here.
489 return; // Change to Thumb mode
492 return; // Change to ARM mode
495 case MCAF_SubsectionsViaSymbols:
501 enum ElfMappingSymbol {
508 void EmitDataMappingSymbol() {
509 if (LastEMS == EMS_Data) return;
510 EmitMappingSymbol("$d");
514 void EmitThumbMappingSymbol() {
515 if (LastEMS == EMS_Thumb) return;
516 EmitMappingSymbol("$t");
520 void EmitARMMappingSymbol() {
521 if (LastEMS == EMS_ARM) return;
522 EmitMappingSymbol("$a");
526 void EmitMappingSymbol(StringRef Name) {
527 MCSymbol *Start = getContext().CreateTempSymbol();
531 getContext().GetOrCreateSymbol(Name + "." +
532 Twine(MappingSymbolCounter++));
534 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
535 MCELF::SetType(SD, ELF::STT_NOTYPE);
536 MCELF::SetBinding(SD, ELF::STB_LOCAL);
537 SD.setExternal(false);
538 AssignSection(Symbol, getCurrentSection().first);
540 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
541 Symbol->setVariableValue(Value);
544 void EmitThumbFunc(MCSymbol *Func) {
545 // FIXME: Anything needed here to flag the function as thumb?
547 getAssembler().setIsThumbFunc(Func);
549 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
550 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
553 // Helper functions for ARM exception handling directives
556 void EmitPersonalityFixup(StringRef Name);
557 void FlushPendingOffset();
558 void FlushUnwindOpcodes(bool NoHandlerData);
560 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
561 SectionKind Kind, const MCSymbol &Fn);
562 void SwitchToExTabSection(const MCSymbol &FnStart);
563 void SwitchToExIdxSection(const MCSymbol &FnStart);
566 int64_t MappingSymbolCounter;
568 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
569 ElfMappingSymbol LastEMS;
571 // ARM Exception Handling Frame Information
574 const MCSymbol *Personality;
575 unsigned PersonalityIndex;
576 unsigned FPReg; // Frame pointer register
577 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
578 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
579 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
582 SmallVector<uint8_t, 64> Opcodes;
583 UnwindOpcodeAssembler UnwindOpAsm;
585 } // end anonymous namespace
587 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
588 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
592 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
593 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
594 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
595 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
596 getStreamer().emitPersonality(Personality);
598 void ARMTargetELFStreamer::emitHandlerData() {
599 getStreamer().emitHandlerData();
601 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
603 getStreamer().emitSetFP(FpReg, SpReg, Offset);
605 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
606 getStreamer().emitPad(Offset);
608 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
610 getStreamer().emitRegSave(RegList, isVector);
612 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
613 assert(!Vendor.empty() && "Vendor cannot be empty.");
615 if (CurrentVendor == Vendor)
618 if (!CurrentVendor.empty())
619 finishAttributeSection();
621 assert(Contents.empty() &&
622 ".ARM.attributes should be flushed before changing vendor");
623 CurrentVendor = Vendor;
626 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
627 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
629 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
631 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
633 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
635 StringRef StringValue) {
636 setAttributeItems(Attribute, IntValue, StringValue,
637 /* OverwriteExisting= */ true);
639 void ARMTargetELFStreamer::emitArch(unsigned Value) {
642 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
643 using namespace ARMBuildAttrs;
644 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
645 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
654 setAttributeItem(ARM_ISA_use, Allowed, false);
662 setAttributeItem(ARM_ISA_use, Allowed, false);
663 setAttributeItem(THUMB_ISA_use, Allowed, false);
667 setAttributeItem(ARM_ISA_use, Allowed, false);
668 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
673 setAttributeItem(ARM_ISA_use, Allowed, false);
674 setAttributeItem(THUMB_ISA_use, Allowed, false);
675 setAttributeItem(Virtualization_use, AllowTZ, false);
679 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
680 setAttributeItem(THUMB_ISA_use, Allowed, false);
684 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
688 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
689 setAttributeItem(ARM_ISA_use, Allowed, false);
690 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
694 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
695 setAttributeItem(ARM_ISA_use, Allowed, false);
696 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
700 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
701 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
705 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
706 setAttributeItem(ARM_ISA_use, Allowed, false);
707 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
708 setAttributeItem(MPextension_use, Allowed, false);
709 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
713 setAttributeItem(ARM_ISA_use, Allowed, false);
714 setAttributeItem(THUMB_ISA_use, Allowed, false);
715 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
719 setAttributeItem(ARM_ISA_use, Allowed, false);
720 setAttributeItem(THUMB_ISA_use, Allowed, false);
721 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
725 report_fatal_error("Unknown Arch: " + Twine(Arch));
729 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
732 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
736 setAttributeItem(ARMBuildAttrs::FP_arch,
737 ARMBuildAttrs::AllowFPv2,
738 /* OverwriteExisting= */ false);
742 setAttributeItem(ARMBuildAttrs::FP_arch,
743 ARMBuildAttrs::AllowFPv3A,
744 /* OverwriteExisting= */ false);
748 setAttributeItem(ARMBuildAttrs::FP_arch,
749 ARMBuildAttrs::AllowFPv3B,
750 /* OverwriteExisting= */ false);
754 setAttributeItem(ARMBuildAttrs::FP_arch,
755 ARMBuildAttrs::AllowFPv4A,
756 /* OverwriteExisting= */ false);
760 setAttributeItem(ARMBuildAttrs::FP_arch,
761 ARMBuildAttrs::AllowFPv4B,
762 /* OverwriteExisting= */ false);
766 setAttributeItem(ARMBuildAttrs::FP_arch,
767 ARMBuildAttrs::AllowFPARMv8A,
768 /* OverwriteExisting= */ false);
772 setAttributeItem(ARMBuildAttrs::FP_arch,
773 ARMBuildAttrs::AllowFPv3A,
774 /* OverwriteExisting= */ false);
775 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
776 ARMBuildAttrs::AllowNeon,
777 /* OverwriteExisting= */ false);
780 case ARM::NEON_VFPV4:
781 setAttributeItem(ARMBuildAttrs::FP_arch,
782 ARMBuildAttrs::AllowFPv4A,
783 /* OverwriteExisting= */ false);
784 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
785 ARMBuildAttrs::AllowNeon2,
786 /* OverwriteExisting= */ false);
789 case ARM::NEON_FP_ARMV8:
790 case ARM::CRYPTO_NEON_FP_ARMV8:
791 setAttributeItem(ARMBuildAttrs::FP_arch,
792 ARMBuildAttrs::AllowFPARMv8A,
793 /* OverwriteExisting= */ false);
794 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
795 ARMBuildAttrs::AllowNeonARMv8,
796 /* OverwriteExisting= */ false);
803 report_fatal_error("Unknown FPU: " + Twine(FPU));
807 size_t ARMTargetELFStreamer::calculateContentSize() const {
809 for (size_t i = 0; i < Contents.size(); ++i) {
810 AttributeItem item = Contents[i];
812 case AttributeItem::HiddenAttribute:
814 case AttributeItem::NumericAttribute:
815 Result += getULEBSize(item.Tag);
816 Result += getULEBSize(item.IntValue);
818 case AttributeItem::TextAttribute:
819 Result += getULEBSize(item.Tag);
820 Result += item.StringValue.size() + 1; // string + '\0'
822 case AttributeItem::NumericAndTextAttributes:
823 Result += getULEBSize(item.Tag);
824 Result += getULEBSize(item.IntValue);
825 Result += item.StringValue.size() + 1; // string + '\0';
831 void ARMTargetELFStreamer::finishAttributeSection() {
833 // [ <section-length> "vendor-name"
834 // [ <file-tag> <size> <attribute>*
835 // | <section-tag> <size> <section-number>* 0 <attribute>*
836 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
840 if (FPU != ARM::INVALID_FPU)
841 emitFPUDefaultAttributes();
843 if (Arch != ARM::INVALID_ARCH)
844 emitArchDefaultAttributes();
846 if (Contents.empty())
849 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
851 ARMELFStreamer &Streamer = getStreamer();
853 // Switch to .ARM.attributes section
854 if (AttributeSection) {
855 Streamer.SwitchSection(AttributeSection);
858 Streamer.getContext().getELFSection(".ARM.attributes",
859 ELF::SHT_ARM_ATTRIBUTES,
861 SectionKind::getMetadata());
862 Streamer.SwitchSection(AttributeSection);
865 Streamer.EmitIntValue(0x41, 1);
868 // Vendor size + Vendor name + '\0'
869 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
872 const size_t TagHeaderSize = 1 + 4;
874 const size_t ContentsSize = calculateContentSize();
876 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
877 Streamer.EmitBytes(CurrentVendor);
878 Streamer.EmitIntValue(0, 1); // '\0'
880 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
881 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
883 // Size should have been accounted for already, now
884 // emit each field as its type (ULEB or String)
885 for (size_t i = 0; i < Contents.size(); ++i) {
886 AttributeItem item = Contents[i];
887 Streamer.EmitULEB128IntValue(item.Tag);
889 default: llvm_unreachable("Invalid attribute type");
890 case AttributeItem::NumericAttribute:
891 Streamer.EmitULEB128IntValue(item.IntValue);
893 case AttributeItem::TextAttribute:
894 Streamer.EmitBytes(item.StringValue.upper());
895 Streamer.EmitIntValue(0, 1); // '\0'
897 case AttributeItem::NumericAndTextAttributes:
898 Streamer.EmitULEB128IntValue(item.IntValue);
899 Streamer.EmitBytes(item.StringValue.upper());
900 Streamer.EmitIntValue(0, 1); // '\0'
906 FPU = ARM::INVALID_FPU;
908 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
909 getStreamer().emitInst(Inst, Suffix);
912 void ARMELFStreamer::FinishImpl() {
913 MCTargetStreamer &TS = getTargetStreamer();
914 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
915 ATS.finishAttributeSection();
917 MCELFStreamer::FinishImpl();
920 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
924 const MCSymbol &Fn) {
925 const MCSectionELF &FnSection =
926 static_cast<const MCSectionELF &>(Fn.getSection());
928 // Create the name for new section
929 StringRef FnSecName(FnSection.getSectionName());
930 SmallString<128> EHSecName(Prefix);
931 if (FnSecName != ".text") {
932 EHSecName += FnSecName;
935 // Get .ARM.extab or .ARM.exidx section
936 const MCSectionELF *EHSection = NULL;
937 if (const MCSymbol *Group = FnSection.getGroup()) {
938 EHSection = getContext().getELFSection(
939 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
940 FnSection.getEntrySize(), Group->getName());
942 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
944 assert(EHSection && "Failed to get the required EH section");
946 // Switch to .ARM.extab or .ARM.exidx section
947 SwitchSection(EHSection);
948 EmitCodeAlignment(4, 0);
951 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
952 SwitchToEHSection(".ARM.extab",
955 SectionKind::getDataRel(),
959 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
960 SwitchToEHSection(".ARM.exidx",
962 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
963 SectionKind::getDataRel(),
967 void ARMELFStreamer::Reset() {
971 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
983 void ARMELFStreamer::emitFnStart() {
984 assert(FnStart == 0);
985 FnStart = getContext().CreateTempSymbol();
989 void ARMELFStreamer::emitFnEnd() {
990 assert(FnStart && ".fnstart must preceeds .fnend");
992 // Emit unwind opcodes if there is no .handlerdata directive
993 if (!ExTab && !CantUnwind)
994 FlushUnwindOpcodes(true);
996 // Emit the exception index table entry
997 SwitchToExIdxSection(*FnStart);
999 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1000 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1002 const MCSymbolRefExpr *FnStartRef =
1003 MCSymbolRefExpr::Create(FnStart,
1004 MCSymbolRefExpr::VK_ARM_PREL31,
1007 EmitValue(FnStartRef, 4);
1010 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1012 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1013 const MCSymbolRefExpr *ExTabEntryRef =
1014 MCSymbolRefExpr::Create(ExTab,
1015 MCSymbolRefExpr::VK_ARM_PREL31,
1017 EmitValue(ExTabEntryRef, 4);
1019 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1020 // the second word of exception index table entry. The size of the unwind
1021 // opcodes should always be 4 bytes.
1022 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1023 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1024 assert(Opcodes.size() == 4u &&
1025 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1026 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1030 // Switch to the section containing FnStart
1031 SwitchSection(&FnStart->getSection());
1033 // Clean exception handling frame information
1037 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1039 // Add the R_ARM_NONE fixup at the same position
1040 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1041 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1043 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1044 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1046 AddValueSymbols(PersonalityRef);
1047 MCDataFragment *DF = getOrCreateDataFragment();
1048 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1050 MCFixup::getKindForSize(4, false)));
1053 void ARMELFStreamer::FlushPendingOffset() {
1054 if (PendingOffset != 0) {
1055 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1060 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1061 // Emit the unwind opcode to restore $sp.
1063 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1064 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1065 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1066 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1068 FlushPendingOffset();
1071 // Finalize the unwind opcode sequence
1072 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1074 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1075 // section. Thus, we don't have to create an entry in the .ARM.extab
1077 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1080 // Switch to .ARM.extab section.
1081 SwitchToExTabSection(*FnStart);
1083 // Create .ARM.extab label for offset in .ARM.exidx
1085 ExTab = getContext().CreateTempSymbol();
1090 const MCSymbolRefExpr *PersonalityRef =
1091 MCSymbolRefExpr::Create(Personality,
1092 MCSymbolRefExpr::VK_ARM_PREL31,
1095 EmitValue(PersonalityRef, 4);
1098 // Emit unwind opcodes
1099 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1102 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1103 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1104 // after the unwind opcodes. The handler data consists of several 32-bit
1105 // words, and should be terminated by zero.
1107 // In case that the .handlerdata directive is not specified by the
1108 // programmer, we should emit zero to terminate the handler data.
1109 if (NoHandlerData && !Personality)
1113 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1115 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1117 UnwindOpAsm.setPersonality(Per);
1120 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1122 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1123 "the operand of .setfp directive should be either $sp or $fp");
1128 if (NewSPReg == ARM::SP)
1129 FPOffset = SPOffset + Offset;
1134 void ARMELFStreamer::emitPad(int64_t Offset) {
1135 // Track the change of the $sp offset
1138 // To squash multiple .pad directives, we should delay the unwind opcode
1139 // until the .save, .vsave, .handlerdata, or .fnend directives.
1140 PendingOffset -= Offset;
1143 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1145 // Collect the registers in the register list
1148 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1149 for (size_t i = 0; i < RegList.size(); ++i) {
1150 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1151 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1152 unsigned Bit = (1u << Reg);
1153 if ((Mask & Bit) == 0) {
1159 // Track the change the $sp offset: For the .save directive, the
1160 // corresponding push instruction will decrease the $sp by (4 * Count).
1161 // For the .vsave directive, the corresponding vpush instruction will
1162 // decrease $sp by (8 * Count).
1163 SPOffset -= Count * (IsVector ? 8 : 4);
1166 FlushPendingOffset();
1168 UnwindOpAsm.EmitVFPRegSave(Mask);
1170 UnwindOpAsm.EmitRegSave(Mask);
1175 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1176 bool isVerboseAsm, bool useLoc, bool useCFI,
1177 bool useDwarfDirectory,
1178 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1179 MCAsmBackend *TAB, bool ShowInst) {
1180 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint);
1182 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1183 useDwarfDirectory, InstPrint, CE, TAB,
1187 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1188 raw_ostream &OS, MCCodeEmitter *Emitter,
1189 bool RelaxAll, bool NoExecStack,
1191 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1193 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1194 // FIXME: This should eventually end up somewhere else where more
1195 // intelligent flag decisions can be made. For now we are just maintaining
1196 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1197 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1200 S->getAssembler().setRelaxAll(true);
1202 S->getAssembler().setNoExecStack(true);