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 emitPersonalityIndex(unsigned Index);
121 virtual void emitHandlerData();
122 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
123 virtual void emitPad(int64_t Offset);
124 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
127 virtual void switchVendor(StringRef Vendor);
128 virtual void emitAttribute(unsigned Attribute, unsigned Value);
129 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
130 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
131 StringRef StrinValue);
132 virtual void emitArch(unsigned Arch);
133 virtual void emitFPU(unsigned FPU);
134 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
135 virtual void finishAttributeSection();
138 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
142 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
143 MCInstPrinter &InstPrinter,
145 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
146 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
147 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
148 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
149 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
150 OS << "\t.personality " << Personality->getName() << '\n';
152 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
153 OS << "\t.personalityindex " << Index << '\n';
155 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
156 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
159 InstPrinter.printRegName(OS, FpReg);
161 InstPrinter.printRegName(OS, SpReg);
163 OS << ", #" << Offset;
166 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
167 OS << "\t.pad\t#" << Offset << '\n';
169 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
171 assert(RegList.size() && "RegList should not be empty");
177 InstPrinter.printRegName(OS, RegList[0]);
179 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
181 InstPrinter.printRegName(OS, RegList[i]);
186 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
188 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
189 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
191 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
193 OS << "\t@ " << Name;
197 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
200 case ARMBuildAttrs::CPU_name:
201 OS << "\t.cpu\t" << String.lower();
204 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
206 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
208 OS << "\t@ " << Name;
214 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
216 StringRef StringValue) {
218 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
219 case ARMBuildAttrs::compatibility:
220 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
221 if (!StringValue.empty())
222 OS << ", \"" << StringValue << "\"";
224 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
229 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
230 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
232 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
233 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
235 void ARMTargetAsmStreamer::finishAttributeSection() {
238 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
242 OS << "\t0x" << utohexstr(Inst) << "\n";
245 class ARMTargetELFStreamer : public ARMTargetStreamer {
247 // This structure holds all attributes, accounting for
248 // their string/numeric value, so we can later emmit them
249 // in declaration order, keeping all in the same vector
250 struct AttributeItem {
255 NumericAndTextAttributes
259 StringRef StringValue;
261 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
262 return (LHS.Tag < RHS.Tag);
266 StringRef CurrentVendor;
269 SmallVector<AttributeItem, 64> Contents;
271 const MCSection *AttributeSection;
273 // FIXME: this should be in a more generic place, but
274 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
275 static size_t getULEBSize(int Value) {
279 Size += sizeof(int8_t); // Is this really necessary?
284 AttributeItem *getAttributeItem(unsigned Attribute) {
285 for (size_t i = 0; i < Contents.size(); ++i)
286 if (Contents[i].Tag == Attribute)
291 void setAttributeItem(unsigned Attribute, unsigned Value,
292 bool OverwriteExisting) {
293 // Look for existing attribute item
294 if (AttributeItem *Item = getAttributeItem(Attribute)) {
295 if (!OverwriteExisting)
297 Item->Type = AttributeItem::NumericAttribute;
298 Item->IntValue = Value;
302 // Create new attribute item
303 AttributeItem Item = {
304 AttributeItem::NumericAttribute,
309 Contents.push_back(Item);
312 void setAttributeItem(unsigned Attribute, StringRef Value,
313 bool OverwriteExisting) {
314 // Look for existing attribute item
315 if (AttributeItem *Item = getAttributeItem(Attribute)) {
316 if (!OverwriteExisting)
318 Item->Type = AttributeItem::TextAttribute;
319 Item->StringValue = Value;
323 // Create new attribute item
324 AttributeItem Item = {
325 AttributeItem::TextAttribute,
330 Contents.push_back(Item);
333 void setAttributeItems(unsigned Attribute, unsigned IntValue,
334 StringRef StringValue, bool OverwriteExisting) {
335 // Look for existing attribute item
336 if (AttributeItem *Item = getAttributeItem(Attribute)) {
337 if (!OverwriteExisting)
339 Item->Type = AttributeItem::NumericAndTextAttributes;
340 Item->IntValue = IntValue;
341 Item->StringValue = StringValue;
345 // Create new attribute item
346 AttributeItem Item = {
347 AttributeItem::NumericAndTextAttributes,
352 Contents.push_back(Item);
355 void emitArchDefaultAttributes();
356 void emitFPUDefaultAttributes();
358 ARMELFStreamer &getStreamer();
360 virtual void emitFnStart();
361 virtual void emitFnEnd();
362 virtual void emitCantUnwind();
363 virtual void emitPersonality(const MCSymbol *Personality);
364 virtual void emitPersonalityIndex(unsigned Index);
365 virtual void emitHandlerData();
366 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
367 virtual void emitPad(int64_t Offset);
368 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
371 virtual void switchVendor(StringRef Vendor);
372 virtual void emitAttribute(unsigned Attribute, unsigned Value);
373 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
374 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
375 StringRef StringValue);
376 virtual void emitArch(unsigned Arch);
377 virtual void emitFPU(unsigned FPU);
378 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
379 virtual void finishAttributeSection();
381 size_t calculateContentSize() const;
384 ARMTargetELFStreamer()
385 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
386 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
390 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
391 /// the appropriate points in the object files. These symbols are defined in the
392 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
394 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
395 /// region of ARM code, Thumb code or data in a section. In practice, this
396 /// emission does not rely on explicit assembler directives but on inherent
397 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
398 /// r0, r0, r0" an instruction).
400 /// As a result this system is orthogonal to the DataRegion infrastructure used
401 /// by MachO. Beware!
402 class ARMELFStreamer : public MCELFStreamer {
404 friend class ARMTargetELFStreamer;
406 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
407 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
409 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
410 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
416 virtual void FinishImpl();
418 // ARM exception handling directives
421 void emitCantUnwind();
422 void emitPersonality(const MCSymbol *Per);
423 void emitPersonalityIndex(unsigned index);
424 void emitHandlerData();
425 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
426 void emitPad(int64_t Offset);
427 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
429 virtual void ChangeSection(const MCSection *Section,
430 const MCExpr *Subsection) {
431 // We have to keep track of the mapping symbol state of any sections we
432 // use. Each one should start off as EMS_None, which is provided as the
433 // default constructor by DenseMap::lookup.
434 LastMappingSymbols[getPreviousSection().first] = LastEMS;
435 LastEMS = LastMappingSymbols.lookup(Section);
437 MCELFStreamer::ChangeSection(Section, Subsection);
440 /// This function is the one used to emit instruction data into the ELF
441 /// streamer. We override it to add the appropriate mapping symbol if
443 virtual void EmitInstruction(const MCInst& Inst) {
445 EmitThumbMappingSymbol();
447 EmitARMMappingSymbol();
449 MCELFStreamer::EmitInstruction(Inst);
452 virtual void emitInst(uint32_t Inst, char Suffix) {
455 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
462 EmitARMMappingSymbol();
463 for (unsigned II = 0, IE = Size; II != IE; II++) {
464 const unsigned I = LittleEndian ? (Size - II - 1) : II;
465 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
471 Size = (Suffix == 'n' ? 2 : 4);
474 EmitThumbMappingSymbol();
475 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
476 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
477 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
478 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
479 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
484 llvm_unreachable("Invalid Suffix");
487 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
490 /// This is one of the functions used to emit data into an ELF section, so the
491 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
493 virtual void EmitBytes(StringRef Data) {
494 EmitDataMappingSymbol();
495 MCELFStreamer::EmitBytes(Data);
498 /// This is one of the functions used to emit data into an ELF section, so the
499 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
501 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
502 EmitDataMappingSymbol();
503 MCELFStreamer::EmitValueImpl(Value, Size);
506 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
507 MCELFStreamer::EmitAssemblerFlag(Flag);
510 case MCAF_SyntaxUnified:
511 return; // no-op here.
514 return; // Change to Thumb mode
517 return; // Change to ARM mode
520 case MCAF_SubsectionsViaSymbols:
526 enum ElfMappingSymbol {
533 void EmitDataMappingSymbol() {
534 if (LastEMS == EMS_Data) return;
535 EmitMappingSymbol("$d");
539 void EmitThumbMappingSymbol() {
540 if (LastEMS == EMS_Thumb) return;
541 EmitMappingSymbol("$t");
545 void EmitARMMappingSymbol() {
546 if (LastEMS == EMS_ARM) return;
547 EmitMappingSymbol("$a");
551 void EmitMappingSymbol(StringRef Name) {
552 MCSymbol *Start = getContext().CreateTempSymbol();
556 getContext().GetOrCreateSymbol(Name + "." +
557 Twine(MappingSymbolCounter++));
559 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
560 MCELF::SetType(SD, ELF::STT_NOTYPE);
561 MCELF::SetBinding(SD, ELF::STB_LOCAL);
562 SD.setExternal(false);
563 AssignSection(Symbol, getCurrentSection().first);
565 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
566 Symbol->setVariableValue(Value);
569 void EmitThumbFunc(MCSymbol *Func) {
570 // FIXME: Anything needed here to flag the function as thumb?
572 getAssembler().setIsThumbFunc(Func);
574 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
575 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
578 // Helper functions for ARM exception handling directives
581 void EmitPersonalityFixup(StringRef Name);
582 void FlushPendingOffset();
583 void FlushUnwindOpcodes(bool NoHandlerData);
585 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
586 SectionKind Kind, const MCSymbol &Fn);
587 void SwitchToExTabSection(const MCSymbol &FnStart);
588 void SwitchToExIdxSection(const MCSymbol &FnStart);
591 int64_t MappingSymbolCounter;
593 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
594 ElfMappingSymbol LastEMS;
596 // ARM Exception Handling Frame Information
599 const MCSymbol *Personality;
600 unsigned PersonalityIndex;
601 unsigned FPReg; // Frame pointer register
602 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
603 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
604 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
607 SmallVector<uint8_t, 64> Opcodes;
608 UnwindOpcodeAssembler UnwindOpAsm;
610 } // end anonymous namespace
612 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
613 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
617 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
618 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
619 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
620 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
621 getStreamer().emitPersonality(Personality);
623 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
624 getStreamer().emitPersonalityIndex(Index);
626 void ARMTargetELFStreamer::emitHandlerData() {
627 getStreamer().emitHandlerData();
629 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
631 getStreamer().emitSetFP(FpReg, SpReg, Offset);
633 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
634 getStreamer().emitPad(Offset);
636 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
638 getStreamer().emitRegSave(RegList, isVector);
640 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
641 assert(!Vendor.empty() && "Vendor cannot be empty.");
643 if (CurrentVendor == Vendor)
646 if (!CurrentVendor.empty())
647 finishAttributeSection();
649 assert(Contents.empty() &&
650 ".ARM.attributes should be flushed before changing vendor");
651 CurrentVendor = Vendor;
654 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
655 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
657 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
659 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
661 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
663 StringRef StringValue) {
664 setAttributeItems(Attribute, IntValue, StringValue,
665 /* OverwriteExisting= */ true);
667 void ARMTargetELFStreamer::emitArch(unsigned Value) {
670 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
671 using namespace ARMBuildAttrs;
672 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
673 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
682 setAttributeItem(ARM_ISA_use, Allowed, false);
690 setAttributeItem(ARM_ISA_use, Allowed, false);
691 setAttributeItem(THUMB_ISA_use, Allowed, false);
695 setAttributeItem(ARM_ISA_use, Allowed, false);
696 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
701 setAttributeItem(ARM_ISA_use, Allowed, false);
702 setAttributeItem(THUMB_ISA_use, Allowed, false);
703 setAttributeItem(Virtualization_use, AllowTZ, false);
707 setAttributeItem(THUMB_ISA_use, Allowed, false);
711 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
715 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
716 setAttributeItem(ARM_ISA_use, Allowed, false);
717 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
721 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
722 setAttributeItem(ARM_ISA_use, Allowed, false);
723 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
727 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
728 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
732 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
733 setAttributeItem(ARM_ISA_use, Allowed, false);
734 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
735 setAttributeItem(MPextension_use, Allowed, false);
736 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
740 setAttributeItem(ARM_ISA_use, Allowed, false);
741 setAttributeItem(THUMB_ISA_use, Allowed, false);
742 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
746 setAttributeItem(ARM_ISA_use, Allowed, false);
747 setAttributeItem(THUMB_ISA_use, Allowed, false);
748 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
752 report_fatal_error("Unknown Arch: " + Twine(Arch));
756 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
759 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
763 setAttributeItem(ARMBuildAttrs::FP_arch,
764 ARMBuildAttrs::AllowFPv2,
765 /* OverwriteExisting= */ false);
769 setAttributeItem(ARMBuildAttrs::FP_arch,
770 ARMBuildAttrs::AllowFPv3A,
771 /* OverwriteExisting= */ false);
775 setAttributeItem(ARMBuildAttrs::FP_arch,
776 ARMBuildAttrs::AllowFPv3B,
777 /* OverwriteExisting= */ false);
781 setAttributeItem(ARMBuildAttrs::FP_arch,
782 ARMBuildAttrs::AllowFPv4A,
783 /* OverwriteExisting= */ false);
787 setAttributeItem(ARMBuildAttrs::FP_arch,
788 ARMBuildAttrs::AllowFPv4B,
789 /* OverwriteExisting= */ false);
793 setAttributeItem(ARMBuildAttrs::FP_arch,
794 ARMBuildAttrs::AllowFPARMv8A,
795 /* OverwriteExisting= */ false);
799 setAttributeItem(ARMBuildAttrs::FP_arch,
800 ARMBuildAttrs::AllowFPv3A,
801 /* OverwriteExisting= */ false);
802 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
803 ARMBuildAttrs::AllowNeon,
804 /* OverwriteExisting= */ false);
807 case ARM::NEON_VFPV4:
808 setAttributeItem(ARMBuildAttrs::FP_arch,
809 ARMBuildAttrs::AllowFPv4A,
810 /* OverwriteExisting= */ false);
811 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
812 ARMBuildAttrs::AllowNeon2,
813 /* OverwriteExisting= */ false);
816 case ARM::NEON_FP_ARMV8:
817 case ARM::CRYPTO_NEON_FP_ARMV8:
818 setAttributeItem(ARMBuildAttrs::FP_arch,
819 ARMBuildAttrs::AllowFPARMv8A,
820 /* OverwriteExisting= */ false);
821 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
822 ARMBuildAttrs::AllowNeonARMv8,
823 /* OverwriteExisting= */ false);
830 report_fatal_error("Unknown FPU: " + Twine(FPU));
834 size_t ARMTargetELFStreamer::calculateContentSize() const {
836 for (size_t i = 0; i < Contents.size(); ++i) {
837 AttributeItem item = Contents[i];
839 case AttributeItem::HiddenAttribute:
841 case AttributeItem::NumericAttribute:
842 Result += getULEBSize(item.Tag);
843 Result += getULEBSize(item.IntValue);
845 case AttributeItem::TextAttribute:
846 Result += getULEBSize(item.Tag);
847 Result += item.StringValue.size() + 1; // string + '\0'
849 case AttributeItem::NumericAndTextAttributes:
850 Result += getULEBSize(item.Tag);
851 Result += getULEBSize(item.IntValue);
852 Result += item.StringValue.size() + 1; // string + '\0';
858 void ARMTargetELFStreamer::finishAttributeSection() {
860 // [ <section-length> "vendor-name"
861 // [ <file-tag> <size> <attribute>*
862 // | <section-tag> <size> <section-number>* 0 <attribute>*
863 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
867 if (FPU != ARM::INVALID_FPU)
868 emitFPUDefaultAttributes();
870 if (Arch != ARM::INVALID_ARCH)
871 emitArchDefaultAttributes();
873 if (Contents.empty())
876 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
878 ARMELFStreamer &Streamer = getStreamer();
880 // Switch to .ARM.attributes section
881 if (AttributeSection) {
882 Streamer.SwitchSection(AttributeSection);
885 Streamer.getContext().getELFSection(".ARM.attributes",
886 ELF::SHT_ARM_ATTRIBUTES,
888 SectionKind::getMetadata());
889 Streamer.SwitchSection(AttributeSection);
892 Streamer.EmitIntValue(0x41, 1);
895 // Vendor size + Vendor name + '\0'
896 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
899 const size_t TagHeaderSize = 1 + 4;
901 const size_t ContentsSize = calculateContentSize();
903 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
904 Streamer.EmitBytes(CurrentVendor);
905 Streamer.EmitIntValue(0, 1); // '\0'
907 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
908 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
910 // Size should have been accounted for already, now
911 // emit each field as its type (ULEB or String)
912 for (size_t i = 0; i < Contents.size(); ++i) {
913 AttributeItem item = Contents[i];
914 Streamer.EmitULEB128IntValue(item.Tag);
916 default: llvm_unreachable("Invalid attribute type");
917 case AttributeItem::NumericAttribute:
918 Streamer.EmitULEB128IntValue(item.IntValue);
920 case AttributeItem::TextAttribute:
921 Streamer.EmitBytes(item.StringValue.upper());
922 Streamer.EmitIntValue(0, 1); // '\0'
924 case AttributeItem::NumericAndTextAttributes:
925 Streamer.EmitULEB128IntValue(item.IntValue);
926 Streamer.EmitBytes(item.StringValue.upper());
927 Streamer.EmitIntValue(0, 1); // '\0'
933 FPU = ARM::INVALID_FPU;
935 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
936 getStreamer().emitInst(Inst, Suffix);
939 void ARMELFStreamer::FinishImpl() {
940 MCTargetStreamer &TS = *getTargetStreamer();
941 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
942 ATS.finishAttributeSection();
944 MCELFStreamer::FinishImpl();
947 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
951 const MCSymbol &Fn) {
952 const MCSectionELF &FnSection =
953 static_cast<const MCSectionELF &>(Fn.getSection());
955 // Create the name for new section
956 StringRef FnSecName(FnSection.getSectionName());
957 SmallString<128> EHSecName(Prefix);
958 if (FnSecName != ".text") {
959 EHSecName += FnSecName;
962 // Get .ARM.extab or .ARM.exidx section
963 const MCSectionELF *EHSection = NULL;
964 if (const MCSymbol *Group = FnSection.getGroup()) {
965 EHSection = getContext().getELFSection(
966 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
967 FnSection.getEntrySize(), Group->getName());
969 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
971 assert(EHSection && "Failed to get the required EH section");
973 // Switch to .ARM.extab or .ARM.exidx section
974 SwitchSection(EHSection);
975 EmitCodeAlignment(4, 0);
978 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
979 SwitchToEHSection(".ARM.extab",
982 SectionKind::getDataRel(),
986 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
987 SwitchToEHSection(".ARM.exidx",
989 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
990 SectionKind::getDataRel(),
994 void ARMELFStreamer::Reset() {
998 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1007 UnwindOpAsm.Reset();
1010 void ARMELFStreamer::emitFnStart() {
1011 assert(FnStart == 0);
1012 FnStart = getContext().CreateTempSymbol();
1016 void ARMELFStreamer::emitFnEnd() {
1017 assert(FnStart && ".fnstart must preceeds .fnend");
1019 // Emit unwind opcodes if there is no .handlerdata directive
1020 if (!ExTab && !CantUnwind)
1021 FlushUnwindOpcodes(true);
1023 // Emit the exception index table entry
1024 SwitchToExIdxSection(*FnStart);
1026 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1027 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1029 const MCSymbolRefExpr *FnStartRef =
1030 MCSymbolRefExpr::Create(FnStart,
1031 MCSymbolRefExpr::VK_ARM_PREL31,
1034 EmitValue(FnStartRef, 4);
1037 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1039 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1040 const MCSymbolRefExpr *ExTabEntryRef =
1041 MCSymbolRefExpr::Create(ExTab,
1042 MCSymbolRefExpr::VK_ARM_PREL31,
1044 EmitValue(ExTabEntryRef, 4);
1046 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1047 // the second word of exception index table entry. The size of the unwind
1048 // opcodes should always be 4 bytes.
1049 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1050 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1051 assert(Opcodes.size() == 4u &&
1052 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1053 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1057 // Switch to the section containing FnStart
1058 SwitchSection(&FnStart->getSection());
1060 // Clean exception handling frame information
1064 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1066 // Add the R_ARM_NONE fixup at the same position
1067 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1068 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1070 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1071 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1073 AddValueSymbols(PersonalityRef);
1074 MCDataFragment *DF = getOrCreateDataFragment();
1075 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1077 MCFixup::getKindForSize(4, false)));
1080 void ARMELFStreamer::FlushPendingOffset() {
1081 if (PendingOffset != 0) {
1082 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1087 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1088 // Emit the unwind opcode to restore $sp.
1090 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1091 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1092 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1093 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1095 FlushPendingOffset();
1098 // Finalize the unwind opcode sequence
1099 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1101 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1102 // section. Thus, we don't have to create an entry in the .ARM.extab
1104 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1107 // Switch to .ARM.extab section.
1108 SwitchToExTabSection(*FnStart);
1110 // Create .ARM.extab label for offset in .ARM.exidx
1112 ExTab = getContext().CreateTempSymbol();
1117 const MCSymbolRefExpr *PersonalityRef =
1118 MCSymbolRefExpr::Create(Personality,
1119 MCSymbolRefExpr::VK_ARM_PREL31,
1122 EmitValue(PersonalityRef, 4);
1125 // Emit unwind opcodes
1126 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1129 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1130 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1131 // after the unwind opcodes. The handler data consists of several 32-bit
1132 // words, and should be terminated by zero.
1134 // In case that the .handlerdata directive is not specified by the
1135 // programmer, we should emit zero to terminate the handler data.
1136 if (NoHandlerData && !Personality)
1140 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1142 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1144 UnwindOpAsm.setPersonality(Per);
1147 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1148 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1149 PersonalityIndex = Index;
1152 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1154 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1155 "the operand of .setfp directive should be either $sp or $fp");
1160 if (NewSPReg == ARM::SP)
1161 FPOffset = SPOffset + Offset;
1166 void ARMELFStreamer::emitPad(int64_t Offset) {
1167 // Track the change of the $sp offset
1170 // To squash multiple .pad directives, we should delay the unwind opcode
1171 // until the .save, .vsave, .handlerdata, or .fnend directives.
1172 PendingOffset -= Offset;
1175 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1177 // Collect the registers in the register list
1180 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1181 for (size_t i = 0; i < RegList.size(); ++i) {
1182 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1183 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1184 unsigned Bit = (1u << Reg);
1185 if ((Mask & Bit) == 0) {
1191 // Track the change the $sp offset: For the .save directive, the
1192 // corresponding push instruction will decrease the $sp by (4 * Count).
1193 // For the .vsave directive, the corresponding vpush instruction will
1194 // decrease $sp by (8 * Count).
1195 SPOffset -= Count * (IsVector ? 8 : 4);
1198 FlushPendingOffset();
1200 UnwindOpAsm.EmitVFPRegSave(Mask);
1202 UnwindOpAsm.EmitRegSave(Mask);
1207 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1208 bool isVerboseAsm, bool useLoc, bool useCFI,
1209 bool useDwarfDirectory,
1210 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1211 MCAsmBackend *TAB, bool ShowInst) {
1212 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1215 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1216 useDwarfDirectory, InstPrint, CE, TAB,
1220 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1221 raw_ostream &OS, MCCodeEmitter *Emitter,
1222 bool RelaxAll, bool NoExecStack,
1224 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1226 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1227 // FIXME: This should eventually end up somewhere else where more
1228 // intelligent flag decisions can be made. For now we are just maintaining
1229 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1230 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1233 S->getAssembler().setRelaxAll(true);
1235 S->getAssembler().setNoExecStack(true);