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 "ARMUnwindOp.h"
21 #include "ARMUnwindOpAsm.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/MC/MCAsmBackend.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/Debug.h"
42 #include "llvm/Support/ELF.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/raw_ostream.h"
49 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
50 assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
51 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
54 static const char *GetFPUName(unsigned ID) {
57 llvm_unreachable("Unknown FPU kind");
59 #define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
60 #include "ARMFPUName.def"
65 static const char *GetArchName(unsigned ID) {
68 llvm_unreachable("Unknown ARCH kind");
70 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
71 case ARM::ID: return NAME;
72 #include "ARMArchName.def"
77 static const char *GetArchDefaultCPUName(unsigned ID) {
80 llvm_unreachable("Unknown ARCH kind");
82 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
83 case ARM::ID: return DEFAULT_CPU_NAME;
84 #include "ARMArchName.def"
89 static unsigned GetArchDefaultCPUArch(unsigned ID) {
92 llvm_unreachable("Unknown ARCH kind");
94 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
95 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
96 #include "ARMArchName.def"
103 class ARMELFStreamer;
105 class ARMTargetAsmStreamer : public ARMTargetStreamer {
106 formatted_raw_ostream &OS;
107 MCInstPrinter &InstPrinter;
109 virtual void emitFnStart();
110 virtual void emitFnEnd();
111 virtual void emitCantUnwind();
112 virtual void emitPersonality(const MCSymbol *Personality);
113 virtual void emitHandlerData();
114 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
115 virtual void emitPad(int64_t Offset);
116 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
119 virtual void switchVendor(StringRef Vendor);
120 virtual void emitAttribute(unsigned Attribute, unsigned Value);
121 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
122 virtual void emitArch(unsigned Arch);
123 virtual void emitFPU(unsigned FPU);
124 virtual void finishAttributeSection();
127 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter);
130 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
131 MCInstPrinter &InstPrinter)
132 : OS(OS), InstPrinter(InstPrinter) {}
133 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
134 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
135 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
136 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
137 OS << "\t.personality " << Personality->getName() << '\n';
139 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
140 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
143 InstPrinter.printRegName(OS, FpReg);
145 InstPrinter.printRegName(OS, SpReg);
147 OS << ", #" << Offset;
150 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
151 OS << "\t.pad\t#" << Offset << '\n';
153 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
155 assert(RegList.size() && "RegList should not be empty");
161 InstPrinter.printRegName(OS, RegList[0]);
163 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
165 InstPrinter.printRegName(OS, RegList[i]);
170 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
172 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
173 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
175 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
178 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
179 case ARMBuildAttrs::CPU_name:
180 OS << "\t.cpu\t" << String.lower() << "\n";
184 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
185 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
187 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
188 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
190 void ARMTargetAsmStreamer::finishAttributeSection() {
193 class ARMTargetELFStreamer : public ARMTargetStreamer {
195 // This structure holds all attributes, accounting for
196 // their string/numeric value, so we can later emmit them
197 // in declaration order, keeping all in the same vector
198 struct AttributeItem {
206 StringRef StringValue;
208 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
209 return (LHS.Tag < RHS.Tag);
213 StringRef CurrentVendor;
216 SmallVector<AttributeItem, 64> Contents;
218 const MCSection *AttributeSection;
220 // FIXME: this should be in a more generic place, but
221 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
222 static size_t getULEBSize(int Value) {
226 Size += sizeof(int8_t); // Is this really necessary?
231 AttributeItem *getAttributeItem(unsigned Attribute) {
232 for (size_t i = 0; i < Contents.size(); ++i)
233 if (Contents[i].Tag == Attribute)
238 void setAttributeItem(unsigned Attribute, unsigned Value,
239 bool OverwriteExisting) {
240 // Look for existing attribute item
241 if (AttributeItem *Item = getAttributeItem(Attribute)) {
242 if (!OverwriteExisting)
244 Item->IntValue = Value;
248 // Create new attribute item
249 AttributeItem Item = {
250 AttributeItem::NumericAttribute,
255 Contents.push_back(Item);
258 void setAttributeItem(unsigned Attribute, StringRef Value,
259 bool OverwriteExisting) {
260 // Look for existing attribute item
261 if (AttributeItem *Item = getAttributeItem(Attribute)) {
262 if (!OverwriteExisting)
264 Item->StringValue = Value;
268 // Create new attribute item
269 AttributeItem Item = {
270 AttributeItem::TextAttribute,
275 Contents.push_back(Item);
278 void emitArchDefaultAttributes();
279 void emitFPUDefaultAttributes();
281 ARMELFStreamer &getStreamer();
283 virtual void emitFnStart();
284 virtual void emitFnEnd();
285 virtual void emitCantUnwind();
286 virtual void emitPersonality(const MCSymbol *Personality);
287 virtual void emitHandlerData();
288 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
289 virtual void emitPad(int64_t Offset);
290 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
293 virtual void switchVendor(StringRef Vendor);
294 virtual void emitAttribute(unsigned Attribute, unsigned Value);
295 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
296 virtual void emitArch(unsigned Arch);
297 virtual void emitFPU(unsigned FPU);
298 virtual void finishAttributeSection();
300 size_t calculateContentSize() const;
303 ARMTargetELFStreamer()
304 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
305 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
309 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
310 /// the appropriate points in the object files. These symbols are defined in the
311 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
313 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
314 /// region of ARM code, Thumb code or data in a section. In practice, this
315 /// emission does not rely on explicit assembler directives but on inherent
316 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
317 /// r0, r0, r0" an instruction).
319 /// As a result this system is orthogonal to the DataRegion infrastructure used
320 /// by MachO. Beware!
321 class ARMELFStreamer : public MCELFStreamer {
323 friend class ARMTargetELFStreamer;
325 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
326 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
328 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
329 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
335 virtual void FinishImpl();
337 // ARM exception handling directives
340 void emitCantUnwind();
341 void emitPersonality(const MCSymbol *Per);
342 void emitHandlerData();
343 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
344 void emitPad(int64_t Offset);
345 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
347 virtual void ChangeSection(const MCSection *Section,
348 const MCExpr *Subsection) {
349 // We have to keep track of the mapping symbol state of any sections we
350 // use. Each one should start off as EMS_None, which is provided as the
351 // default constructor by DenseMap::lookup.
352 LastMappingSymbols[getPreviousSection().first] = LastEMS;
353 LastEMS = LastMappingSymbols.lookup(Section);
355 MCELFStreamer::ChangeSection(Section, Subsection);
358 /// This function is the one used to emit instruction data into the ELF
359 /// streamer. We override it to add the appropriate mapping symbol if
361 virtual void EmitInstruction(const MCInst& Inst) {
363 EmitThumbMappingSymbol();
365 EmitARMMappingSymbol();
367 MCELFStreamer::EmitInstruction(Inst);
370 /// This is one of the functions used to emit data into an ELF section, so the
371 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
373 virtual void EmitBytes(StringRef Data) {
374 EmitDataMappingSymbol();
375 MCELFStreamer::EmitBytes(Data);
378 /// This is one of the functions used to emit data into an ELF section, so the
379 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
381 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
382 EmitDataMappingSymbol();
383 MCELFStreamer::EmitValueImpl(Value, Size);
386 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
387 MCELFStreamer::EmitAssemblerFlag(Flag);
390 case MCAF_SyntaxUnified:
391 return; // no-op here.
394 return; // Change to Thumb mode
397 return; // Change to ARM mode
400 case MCAF_SubsectionsViaSymbols:
406 enum ElfMappingSymbol {
413 void EmitDataMappingSymbol() {
414 if (LastEMS == EMS_Data) return;
415 EmitMappingSymbol("$d");
419 void EmitThumbMappingSymbol() {
420 if (LastEMS == EMS_Thumb) return;
421 EmitMappingSymbol("$t");
425 void EmitARMMappingSymbol() {
426 if (LastEMS == EMS_ARM) return;
427 EmitMappingSymbol("$a");
431 void EmitMappingSymbol(StringRef Name) {
432 MCSymbol *Start = getContext().CreateTempSymbol();
436 getContext().GetOrCreateSymbol(Name + "." +
437 Twine(MappingSymbolCounter++));
439 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
440 MCELF::SetType(SD, ELF::STT_NOTYPE);
441 MCELF::SetBinding(SD, ELF::STB_LOCAL);
442 SD.setExternal(false);
443 AssignSection(Symbol, getCurrentSection().first);
445 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
446 Symbol->setVariableValue(Value);
449 void EmitThumbFunc(MCSymbol *Func) {
450 // FIXME: Anything needed here to flag the function as thumb?
452 getAssembler().setIsThumbFunc(Func);
454 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
455 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
458 // Helper functions for ARM exception handling directives
461 void EmitPersonalityFixup(StringRef Name);
462 void FlushPendingOffset();
463 void FlushUnwindOpcodes(bool NoHandlerData);
465 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
466 SectionKind Kind, const MCSymbol &Fn);
467 void SwitchToExTabSection(const MCSymbol &FnStart);
468 void SwitchToExIdxSection(const MCSymbol &FnStart);
471 int64_t MappingSymbolCounter;
473 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
474 ElfMappingSymbol LastEMS;
476 // ARM Exception Handling Frame Information
479 const MCSymbol *Personality;
480 unsigned PersonalityIndex;
481 unsigned FPReg; // Frame pointer register
482 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
483 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
484 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
487 SmallVector<uint8_t, 64> Opcodes;
488 UnwindOpcodeAssembler UnwindOpAsm;
490 } // end anonymous namespace
492 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
493 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
497 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
498 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
499 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
500 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
501 getStreamer().emitPersonality(Personality);
503 void ARMTargetELFStreamer::emitHandlerData() {
504 getStreamer().emitHandlerData();
506 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
508 getStreamer().emitSetFP(FpReg, SpReg, Offset);
510 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
511 getStreamer().emitPad(Offset);
513 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
515 getStreamer().emitRegSave(RegList, isVector);
517 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
518 assert(!Vendor.empty() && "Vendor cannot be empty.");
520 if (CurrentVendor == Vendor)
523 if (!CurrentVendor.empty())
524 finishAttributeSection();
526 assert(Contents.empty() &&
527 ".ARM.attributes should be flushed before changing vendor");
528 CurrentVendor = Vendor;
531 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
532 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
534 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
536 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
538 void ARMTargetELFStreamer::emitArch(unsigned Value) {
541 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
542 using namespace ARMBuildAttrs;
543 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
544 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
553 setAttributeItem(ARM_ISA_use, Allowed, false);
561 setAttributeItem(ARM_ISA_use, Allowed, false);
562 setAttributeItem(THUMB_ISA_use, Allowed, false);
566 setAttributeItem(ARM_ISA_use, Allowed, false);
567 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
572 setAttributeItem(ARM_ISA_use, Allowed, false);
573 setAttributeItem(THUMB_ISA_use, Allowed, false);
574 setAttributeItem(Virtualization_use, AllowTZ, false);
578 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
579 setAttributeItem(THUMB_ISA_use, Allowed, false);
583 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
587 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
588 setAttributeItem(ARM_ISA_use, Allowed, false);
589 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
593 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
594 setAttributeItem(ARM_ISA_use, Allowed, false);
595 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
599 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
600 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
604 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
605 setAttributeItem(ARM_ISA_use, Allowed, false);
606 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
607 setAttributeItem(MPextension_use, Allowed, false);
608 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
612 setAttributeItem(ARM_ISA_use, Allowed, false);
613 setAttributeItem(THUMB_ISA_use, Allowed, false);
614 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
618 setAttributeItem(ARM_ISA_use, Allowed, false);
619 setAttributeItem(THUMB_ISA_use, Allowed, false);
620 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
624 report_fatal_error("Unknown Arch: " + Twine(Arch));
628 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
631 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
635 setAttributeItem(ARMBuildAttrs::FP_arch,
636 ARMBuildAttrs::AllowFPv2,
637 /* OverwriteExisting= */ false);
641 setAttributeItem(ARMBuildAttrs::FP_arch,
642 ARMBuildAttrs::AllowFPv3A,
643 /* OverwriteExisting= */ false);
647 setAttributeItem(ARMBuildAttrs::FP_arch,
648 ARMBuildAttrs::AllowFPv3B,
649 /* OverwriteExisting= */ false);
653 setAttributeItem(ARMBuildAttrs::FP_arch,
654 ARMBuildAttrs::AllowFPv4A,
655 /* OverwriteExisting= */ false);
659 setAttributeItem(ARMBuildAttrs::FP_arch,
660 ARMBuildAttrs::AllowFPv4B,
661 /* OverwriteExisting= */ false);
665 setAttributeItem(ARMBuildAttrs::FP_arch,
666 ARMBuildAttrs::AllowFPARMv8A,
667 /* OverwriteExisting= */ false);
671 setAttributeItem(ARMBuildAttrs::FP_arch,
672 ARMBuildAttrs::AllowFPv3A,
673 /* OverwriteExisting= */ false);
674 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
675 ARMBuildAttrs::AllowNeon,
676 /* OverwriteExisting= */ false);
679 case ARM::NEON_VFPV4:
680 setAttributeItem(ARMBuildAttrs::FP_arch,
681 ARMBuildAttrs::AllowFPv4A,
682 /* OverwriteExisting= */ false);
683 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
684 ARMBuildAttrs::AllowNeon2,
685 /* OverwriteExisting= */ false);
688 case ARM::NEON_FP_ARMV8:
689 case ARM::CRYPTO_NEON_FP_ARMV8:
690 setAttributeItem(ARMBuildAttrs::FP_arch,
691 ARMBuildAttrs::AllowFPARMv8A,
692 /* OverwriteExisting= */ false);
693 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
694 ARMBuildAttrs::AllowNeonARMv8,
695 /* OverwriteExisting= */ false);
699 report_fatal_error("Unknown FPU: " + Twine(FPU));
703 size_t ARMTargetELFStreamer::calculateContentSize() const {
705 for (size_t i = 0; i < Contents.size(); ++i) {
706 AttributeItem item = Contents[i];
708 case AttributeItem::HiddenAttribute:
710 case AttributeItem::NumericAttribute:
711 Result += getULEBSize(item.Tag);
712 Result += getULEBSize(item.IntValue);
714 case AttributeItem::TextAttribute:
715 Result += getULEBSize(item.Tag);
716 Result += item.StringValue.size() + 1; // string + '\0'
722 void ARMTargetELFStreamer::finishAttributeSection() {
724 // [ <section-length> "vendor-name"
725 // [ <file-tag> <size> <attribute>*
726 // | <section-tag> <size> <section-number>* 0 <attribute>*
727 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
731 if (FPU != ARM::INVALID_FPU)
732 emitFPUDefaultAttributes();
734 if (Arch != ARM::INVALID_ARCH)
735 emitArchDefaultAttributes();
737 if (Contents.empty())
740 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
742 ARMELFStreamer &Streamer = getStreamer();
744 // Switch to .ARM.attributes section
745 if (AttributeSection) {
746 Streamer.SwitchSection(AttributeSection);
749 Streamer.getContext().getELFSection(".ARM.attributes",
750 ELF::SHT_ARM_ATTRIBUTES,
752 SectionKind::getMetadata());
753 Streamer.SwitchSection(AttributeSection);
756 Streamer.EmitIntValue(0x41, 1);
759 // Vendor size + Vendor name + '\0'
760 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
763 const size_t TagHeaderSize = 1 + 4;
765 const size_t ContentsSize = calculateContentSize();
767 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
768 Streamer.EmitBytes(CurrentVendor);
769 Streamer.EmitIntValue(0, 1); // '\0'
771 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
772 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
774 // Size should have been accounted for already, now
775 // emit each field as its type (ULEB or String)
776 for (size_t i = 0; i < Contents.size(); ++i) {
777 AttributeItem item = Contents[i];
778 Streamer.EmitULEB128IntValue(item.Tag);
780 default: llvm_unreachable("Invalid attribute type");
781 case AttributeItem::NumericAttribute:
782 Streamer.EmitULEB128IntValue(item.IntValue);
784 case AttributeItem::TextAttribute:
785 Streamer.EmitBytes(item.StringValue.upper());
786 Streamer.EmitIntValue(0, 1); // '\0'
792 FPU = ARM::INVALID_FPU;
795 void ARMELFStreamer::FinishImpl() {
796 MCTargetStreamer &TS = getTargetStreamer();
797 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
798 ATS.finishAttributeSection();
800 MCELFStreamer::FinishImpl();
803 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
807 const MCSymbol &Fn) {
808 const MCSectionELF &FnSection =
809 static_cast<const MCSectionELF &>(Fn.getSection());
811 // Create the name for new section
812 StringRef FnSecName(FnSection.getSectionName());
813 SmallString<128> EHSecName(Prefix);
814 if (FnSecName != ".text") {
815 EHSecName += FnSecName;
818 // Get .ARM.extab or .ARM.exidx section
819 const MCSectionELF *EHSection = NULL;
820 if (const MCSymbol *Group = FnSection.getGroup()) {
821 EHSection = getContext().getELFSection(
822 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
823 FnSection.getEntrySize(), Group->getName());
825 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
827 assert(EHSection && "Failed to get the required EH section");
829 // Switch to .ARM.extab or .ARM.exidx section
830 SwitchSection(EHSection);
831 EmitCodeAlignment(4, 0);
834 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
835 SwitchToEHSection(".ARM.extab",
838 SectionKind::getDataRel(),
842 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
843 SwitchToEHSection(".ARM.exidx",
845 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
846 SectionKind::getDataRel(),
850 void ARMELFStreamer::Reset() {
854 PersonalityIndex = NUM_PERSONALITY_INDEX;
866 void ARMELFStreamer::emitFnStart() {
867 assert(FnStart == 0);
868 FnStart = getContext().CreateTempSymbol();
872 void ARMELFStreamer::emitFnEnd() {
873 assert(FnStart && ".fnstart must preceeds .fnend");
875 // Emit unwind opcodes if there is no .handlerdata directive
876 if (!ExTab && !CantUnwind)
877 FlushUnwindOpcodes(true);
879 // Emit the exception index table entry
880 SwitchToExIdxSection(*FnStart);
882 if (PersonalityIndex < NUM_PERSONALITY_INDEX)
883 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
885 const MCSymbolRefExpr *FnStartRef =
886 MCSymbolRefExpr::Create(FnStart,
887 MCSymbolRefExpr::VK_ARM_PREL31,
890 EmitValue(FnStartRef, 4);
893 EmitIntValue(EXIDX_CANTUNWIND, 4);
895 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
896 const MCSymbolRefExpr *ExTabEntryRef =
897 MCSymbolRefExpr::Create(ExTab,
898 MCSymbolRefExpr::VK_ARM_PREL31,
900 EmitValue(ExTabEntryRef, 4);
902 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
903 // the second word of exception index table entry. The size of the unwind
904 // opcodes should always be 4 bytes.
905 assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
906 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
907 assert(Opcodes.size() == 4u &&
908 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
909 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
913 // Switch to the section containing FnStart
914 SwitchSection(&FnStart->getSection());
916 // Clean exception handling frame information
920 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
922 // Add the R_ARM_NONE fixup at the same position
923 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
924 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
926 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
927 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
929 AddValueSymbols(PersonalityRef);
930 MCDataFragment *DF = getOrCreateDataFragment();
931 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
933 MCFixup::getKindForSize(4, false)));
936 void ARMELFStreamer::FlushPendingOffset() {
937 if (PendingOffset != 0) {
938 UnwindOpAsm.EmitSPOffset(-PendingOffset);
943 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
944 // Emit the unwind opcode to restore $sp.
946 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
947 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
948 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
949 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
951 FlushPendingOffset();
954 // Finalize the unwind opcode sequence
955 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
957 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
958 // section. Thus, we don't have to create an entry in the .ARM.extab
960 if (NoHandlerData && PersonalityIndex == AEABI_UNWIND_CPP_PR0)
963 // Switch to .ARM.extab section.
964 SwitchToExTabSection(*FnStart);
966 // Create .ARM.extab label for offset in .ARM.exidx
968 ExTab = getContext().CreateTempSymbol();
973 const MCSymbolRefExpr *PersonalityRef =
974 MCSymbolRefExpr::Create(Personality,
975 MCSymbolRefExpr::VK_ARM_PREL31,
978 EmitValue(PersonalityRef, 4);
981 // Emit unwind opcodes
982 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
985 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
986 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
987 // after the unwind opcodes. The handler data consists of several 32-bit
988 // words, and should be terminated by zero.
990 // In case that the .handlerdata directive is not specified by the
991 // programmer, we should emit zero to terminate the handler data.
992 if (NoHandlerData && !Personality)
996 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
998 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1000 UnwindOpAsm.setPersonality(Per);
1003 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1005 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1006 "the operand of .setfp directive should be either $sp or $fp");
1011 if (NewSPReg == ARM::SP)
1012 FPOffset = SPOffset + Offset;
1017 void ARMELFStreamer::emitPad(int64_t Offset) {
1018 // Track the change of the $sp offset
1021 // To squash multiple .pad directives, we should delay the unwind opcode
1022 // until the .save, .vsave, .handlerdata, or .fnend directives.
1023 PendingOffset -= Offset;
1026 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1028 // Collect the registers in the register list
1031 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1032 for (size_t i = 0; i < RegList.size(); ++i) {
1033 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1034 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1035 unsigned Bit = (1u << Reg);
1036 if ((Mask & Bit) == 0) {
1042 // Track the change the $sp offset: For the .save directive, the
1043 // corresponding push instruction will decrease the $sp by (4 * Count).
1044 // For the .vsave directive, the corresponding vpush instruction will
1045 // decrease $sp by (8 * Count).
1046 SPOffset -= Count * (IsVector ? 8 : 4);
1049 FlushPendingOffset();
1051 UnwindOpAsm.EmitVFPRegSave(Mask);
1053 UnwindOpAsm.EmitRegSave(Mask);
1058 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1059 bool isVerboseAsm, bool useLoc, bool useCFI,
1060 bool useDwarfDirectory,
1061 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1062 MCAsmBackend *TAB, bool ShowInst) {
1063 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint);
1065 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1066 useDwarfDirectory, InstPrint, CE, TAB,
1070 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1071 raw_ostream &OS, MCCodeEmitter *Emitter,
1072 bool RelaxAll, bool NoExecStack,
1074 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1076 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1077 // FIXME: This should eventually end up somewhere else where more
1078 // intelligent flag decisions can be made. For now we are just maintaining
1079 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1080 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1083 S->getAssembler().setRelaxAll(true);
1085 S->getAssembler().setNoExecStack(true);