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"
107 void ARMTargetStreamer::anchor() {}
108 ARMTargetStreamer::ARMTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
112 class ARMELFStreamer;
114 class ARMTargetAsmStreamer : public ARMTargetStreamer {
115 formatted_raw_ostream &OS;
116 MCInstPrinter &InstPrinter;
119 virtual void emitFnStart();
120 virtual void emitFnEnd();
121 virtual void emitCantUnwind();
122 virtual void emitPersonality(const MCSymbol *Personality);
123 virtual void emitPersonalityIndex(unsigned Index);
124 virtual void emitHandlerData();
125 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
126 virtual void emitPad(int64_t Offset);
127 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
129 virtual void emitUnwindRaw(int64_t Offset,
130 const SmallVectorImpl<uint8_t> &Opcodes);
132 virtual void switchVendor(StringRef Vendor);
133 virtual void emitAttribute(unsigned Attribute, unsigned Value);
134 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
135 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
136 StringRef StrinValue);
137 virtual void emitArch(unsigned Arch);
138 virtual void emitFPU(unsigned FPU);
139 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
140 virtual void finishAttributeSection();
143 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
144 MCInstPrinter &InstPrinter, bool VerboseAsm);
147 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
148 formatted_raw_ostream &OS,
149 MCInstPrinter &InstPrinter,
151 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
152 IsVerboseAsm(VerboseAsm) {}
153 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
154 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
155 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
156 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
157 OS << "\t.personality " << Personality->getName() << '\n';
159 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
160 OS << "\t.personalityindex " << Index << '\n';
162 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
163 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
166 InstPrinter.printRegName(OS, FpReg);
168 InstPrinter.printRegName(OS, SpReg);
170 OS << ", #" << Offset;
173 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
174 OS << "\t.pad\t#" << Offset << '\n';
176 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
178 assert(RegList.size() && "RegList should not be empty");
184 InstPrinter.printRegName(OS, RegList[0]);
186 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
188 InstPrinter.printRegName(OS, RegList[i]);
193 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
195 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
196 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
198 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
200 OS << "\t@ " << Name;
204 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
207 case ARMBuildAttrs::CPU_name:
208 OS << "\t.cpu\t" << String.lower();
211 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
213 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
215 OS << "\t@ " << Name;
221 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
223 StringRef StringValue) {
225 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
226 case ARMBuildAttrs::compatibility:
227 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
228 if (!StringValue.empty())
229 OS << ", \"" << StringValue << "\"";
231 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
236 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
237 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
239 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
240 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
242 void ARMTargetAsmStreamer::finishAttributeSection() {
245 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
249 OS << "\t0x" << utohexstr(Inst) << "\n";
252 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
253 const SmallVectorImpl<uint8_t> &Opcodes) {
254 OS << "\t.unwind_raw " << Offset;
255 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
258 OS << ", 0x" << utohexstr(*OCI);
262 class ARMTargetELFStreamer : public ARMTargetStreamer {
264 // This structure holds all attributes, accounting for
265 // their string/numeric value, so we can later emmit them
266 // in declaration order, keeping all in the same vector
267 struct AttributeItem {
272 NumericAndTextAttributes
276 StringRef StringValue;
278 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
279 return (LHS.Tag < RHS.Tag);
283 StringRef CurrentVendor;
286 SmallVector<AttributeItem, 64> Contents;
288 const MCSection *AttributeSection;
290 // FIXME: this should be in a more generic place, but
291 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
292 static size_t getULEBSize(int Value) {
296 Size += sizeof(int8_t); // Is this really necessary?
301 AttributeItem *getAttributeItem(unsigned Attribute) {
302 for (size_t i = 0; i < Contents.size(); ++i)
303 if (Contents[i].Tag == Attribute)
308 void setAttributeItem(unsigned Attribute, unsigned Value,
309 bool OverwriteExisting) {
310 // Look for existing attribute item
311 if (AttributeItem *Item = getAttributeItem(Attribute)) {
312 if (!OverwriteExisting)
314 Item->Type = AttributeItem::NumericAttribute;
315 Item->IntValue = Value;
319 // Create new attribute item
320 AttributeItem Item = {
321 AttributeItem::NumericAttribute,
326 Contents.push_back(Item);
329 void setAttributeItem(unsigned Attribute, StringRef Value,
330 bool OverwriteExisting) {
331 // Look for existing attribute item
332 if (AttributeItem *Item = getAttributeItem(Attribute)) {
333 if (!OverwriteExisting)
335 Item->Type = AttributeItem::TextAttribute;
336 Item->StringValue = Value;
340 // Create new attribute item
341 AttributeItem Item = {
342 AttributeItem::TextAttribute,
347 Contents.push_back(Item);
350 void setAttributeItems(unsigned Attribute, unsigned IntValue,
351 StringRef StringValue, bool OverwriteExisting) {
352 // Look for existing attribute item
353 if (AttributeItem *Item = getAttributeItem(Attribute)) {
354 if (!OverwriteExisting)
356 Item->Type = AttributeItem::NumericAndTextAttributes;
357 Item->IntValue = IntValue;
358 Item->StringValue = StringValue;
362 // Create new attribute item
363 AttributeItem Item = {
364 AttributeItem::NumericAndTextAttributes,
369 Contents.push_back(Item);
372 void emitArchDefaultAttributes();
373 void emitFPUDefaultAttributes();
375 ARMELFStreamer &getStreamer();
377 virtual void emitFnStart();
378 virtual void emitFnEnd();
379 virtual void emitCantUnwind();
380 virtual void emitPersonality(const MCSymbol *Personality);
381 virtual void emitPersonalityIndex(unsigned Index);
382 virtual void emitHandlerData();
383 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
384 virtual void emitPad(int64_t Offset);
385 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
387 virtual void emitUnwindRaw(int64_t Offset,
388 const SmallVectorImpl<uint8_t> &Opcodes);
390 virtual void switchVendor(StringRef Vendor);
391 virtual void emitAttribute(unsigned Attribute, unsigned Value);
392 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
393 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
394 StringRef StringValue);
395 virtual void emitArch(unsigned Arch);
396 virtual void emitFPU(unsigned FPU);
397 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
398 virtual void finishAttributeSection();
400 size_t calculateContentSize() const;
403 ARMTargetELFStreamer(MCStreamer &S)
404 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
405 Arch(ARM::INVALID_ARCH), AttributeSection(0) {}
408 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
409 /// the appropriate points in the object files. These symbols are defined in the
410 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
412 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
413 /// region of ARM code, Thumb code or data in a section. In practice, this
414 /// emission does not rely on explicit assembler directives but on inherent
415 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
416 /// r0, r0, r0" an instruction).
418 /// As a result this system is orthogonal to the DataRegion infrastructure used
419 /// by MachO. Beware!
420 class ARMELFStreamer : public MCELFStreamer {
422 friend class ARMTargetELFStreamer;
424 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
425 MCCodeEmitter *Emitter, bool IsThumb)
426 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
427 MappingSymbolCounter(0), LastEMS(EMS_None) {
433 virtual void FinishImpl();
435 // ARM exception handling directives
438 void emitCantUnwind();
439 void emitPersonality(const MCSymbol *Per);
440 void emitPersonalityIndex(unsigned index);
441 void emitHandlerData();
442 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
443 void emitPad(int64_t Offset);
444 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
445 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
447 virtual void ChangeSection(const MCSection *Section,
448 const MCExpr *Subsection) {
449 // We have to keep track of the mapping symbol state of any sections we
450 // use. Each one should start off as EMS_None, which is provided as the
451 // default constructor by DenseMap::lookup.
452 LastMappingSymbols[getPreviousSection().first] = LastEMS;
453 LastEMS = LastMappingSymbols.lookup(Section);
455 MCELFStreamer::ChangeSection(Section, Subsection);
458 /// This function is the one used to emit instruction data into the ELF
459 /// streamer. We override it to add the appropriate mapping symbol if
461 virtual void EmitInstruction(const MCInst& Inst) {
463 EmitThumbMappingSymbol();
465 EmitARMMappingSymbol();
467 MCELFStreamer::EmitInstruction(Inst);
470 virtual void emitInst(uint32_t Inst, char Suffix) {
473 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
480 EmitARMMappingSymbol();
481 for (unsigned II = 0, IE = Size; II != IE; II++) {
482 const unsigned I = LittleEndian ? (Size - II - 1) : II;
483 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
489 Size = (Suffix == 'n' ? 2 : 4);
492 EmitThumbMappingSymbol();
493 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
494 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
495 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
496 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
497 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
502 llvm_unreachable("Invalid Suffix");
505 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
508 /// This is one of the functions used to emit data into an ELF section, so the
509 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
511 virtual void EmitBytes(StringRef Data) {
512 EmitDataMappingSymbol();
513 MCELFStreamer::EmitBytes(Data);
516 /// This is one of the functions used to emit data into an ELF section, so the
517 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
519 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
520 EmitDataMappingSymbol();
521 MCELFStreamer::EmitValueImpl(Value, Size);
524 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
525 MCELFStreamer::EmitAssemblerFlag(Flag);
528 case MCAF_SyntaxUnified:
529 return; // no-op here.
532 return; // Change to Thumb mode
535 return; // Change to ARM mode
538 case MCAF_SubsectionsViaSymbols:
544 enum ElfMappingSymbol {
551 void EmitDataMappingSymbol() {
552 if (LastEMS == EMS_Data) return;
553 EmitMappingSymbol("$d");
557 void EmitThumbMappingSymbol() {
558 if (LastEMS == EMS_Thumb) return;
559 EmitMappingSymbol("$t");
563 void EmitARMMappingSymbol() {
564 if (LastEMS == EMS_ARM) return;
565 EmitMappingSymbol("$a");
569 void EmitMappingSymbol(StringRef Name) {
570 MCSymbol *Start = getContext().CreateTempSymbol();
574 getContext().GetOrCreateSymbol(Name + "." +
575 Twine(MappingSymbolCounter++));
577 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
578 MCELF::SetType(SD, ELF::STT_NOTYPE);
579 MCELF::SetBinding(SD, ELF::STB_LOCAL);
580 SD.setExternal(false);
581 AssignSection(Symbol, getCurrentSection().first);
583 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
584 Symbol->setVariableValue(Value);
587 void EmitThumbFunc(MCSymbol *Func) {
588 // FIXME: Anything needed here to flag the function as thumb?
590 getAssembler().setIsThumbFunc(Func);
592 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
593 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
596 // Helper functions for ARM exception handling directives
599 void EmitPersonalityFixup(StringRef Name);
600 void FlushPendingOffset();
601 void FlushUnwindOpcodes(bool NoHandlerData);
603 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
604 SectionKind Kind, const MCSymbol &Fn);
605 void SwitchToExTabSection(const MCSymbol &FnStart);
606 void SwitchToExIdxSection(const MCSymbol &FnStart);
609 int64_t MappingSymbolCounter;
611 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
612 ElfMappingSymbol LastEMS;
614 // ARM Exception Handling Frame Information
617 const MCSymbol *Personality;
618 unsigned PersonalityIndex;
619 unsigned FPReg; // Frame pointer register
620 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
621 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
622 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
625 SmallVector<uint8_t, 64> Opcodes;
626 UnwindOpcodeAssembler UnwindOpAsm;
628 } // end anonymous namespace
630 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
631 return static_cast<ARMELFStreamer &>(Streamer);
634 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
635 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
636 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
637 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
638 getStreamer().emitPersonality(Personality);
640 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
641 getStreamer().emitPersonalityIndex(Index);
643 void ARMTargetELFStreamer::emitHandlerData() {
644 getStreamer().emitHandlerData();
646 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
648 getStreamer().emitSetFP(FpReg, SpReg, Offset);
650 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
651 getStreamer().emitPad(Offset);
653 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
655 getStreamer().emitRegSave(RegList, isVector);
657 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
658 const SmallVectorImpl<uint8_t> &Opcodes) {
659 getStreamer().emitUnwindRaw(Offset, Opcodes);
661 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
662 assert(!Vendor.empty() && "Vendor cannot be empty.");
664 if (CurrentVendor == Vendor)
667 if (!CurrentVendor.empty())
668 finishAttributeSection();
670 assert(Contents.empty() &&
671 ".ARM.attributes should be flushed before changing vendor");
672 CurrentVendor = Vendor;
675 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
676 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
678 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
680 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
682 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
684 StringRef StringValue) {
685 setAttributeItems(Attribute, IntValue, StringValue,
686 /* OverwriteExisting= */ true);
688 void ARMTargetELFStreamer::emitArch(unsigned Value) {
691 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
692 using namespace ARMBuildAttrs;
693 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
694 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
703 setAttributeItem(ARM_ISA_use, Allowed, false);
711 setAttributeItem(ARM_ISA_use, Allowed, false);
712 setAttributeItem(THUMB_ISA_use, Allowed, false);
716 setAttributeItem(ARM_ISA_use, Allowed, false);
717 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
722 setAttributeItem(ARM_ISA_use, Allowed, false);
723 setAttributeItem(THUMB_ISA_use, Allowed, false);
724 setAttributeItem(Virtualization_use, AllowTZ, false);
728 setAttributeItem(THUMB_ISA_use, Allowed, false);
732 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
736 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
737 setAttributeItem(ARM_ISA_use, Allowed, false);
738 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
742 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
743 setAttributeItem(ARM_ISA_use, Allowed, false);
744 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
748 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
749 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
753 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
754 setAttributeItem(ARM_ISA_use, Allowed, false);
755 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
756 setAttributeItem(MPextension_use, Allowed, false);
757 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
761 setAttributeItem(ARM_ISA_use, Allowed, false);
762 setAttributeItem(THUMB_ISA_use, Allowed, false);
763 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
767 setAttributeItem(ARM_ISA_use, Allowed, false);
768 setAttributeItem(THUMB_ISA_use, Allowed, false);
769 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
773 report_fatal_error("Unknown Arch: " + Twine(Arch));
777 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
780 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
784 setAttributeItem(ARMBuildAttrs::FP_arch,
785 ARMBuildAttrs::AllowFPv2,
786 /* OverwriteExisting= */ false);
790 setAttributeItem(ARMBuildAttrs::FP_arch,
791 ARMBuildAttrs::AllowFPv3A,
792 /* OverwriteExisting= */ false);
796 setAttributeItem(ARMBuildAttrs::FP_arch,
797 ARMBuildAttrs::AllowFPv3B,
798 /* OverwriteExisting= */ false);
802 setAttributeItem(ARMBuildAttrs::FP_arch,
803 ARMBuildAttrs::AllowFPv4A,
804 /* OverwriteExisting= */ false);
808 setAttributeItem(ARMBuildAttrs::FP_arch,
809 ARMBuildAttrs::AllowFPv4B,
810 /* OverwriteExisting= */ false);
814 setAttributeItem(ARMBuildAttrs::FP_arch,
815 ARMBuildAttrs::AllowFPARMv8A,
816 /* OverwriteExisting= */ false);
820 setAttributeItem(ARMBuildAttrs::FP_arch,
821 ARMBuildAttrs::AllowFPv3A,
822 /* OverwriteExisting= */ false);
823 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
824 ARMBuildAttrs::AllowNeon,
825 /* OverwriteExisting= */ false);
828 case ARM::NEON_VFPV4:
829 setAttributeItem(ARMBuildAttrs::FP_arch,
830 ARMBuildAttrs::AllowFPv4A,
831 /* OverwriteExisting= */ false);
832 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
833 ARMBuildAttrs::AllowNeon2,
834 /* OverwriteExisting= */ false);
837 case ARM::NEON_FP_ARMV8:
838 case ARM::CRYPTO_NEON_FP_ARMV8:
839 setAttributeItem(ARMBuildAttrs::FP_arch,
840 ARMBuildAttrs::AllowFPARMv8A,
841 /* OverwriteExisting= */ false);
842 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
843 ARMBuildAttrs::AllowNeonARMv8,
844 /* OverwriteExisting= */ false);
851 report_fatal_error("Unknown FPU: " + Twine(FPU));
855 size_t ARMTargetELFStreamer::calculateContentSize() const {
857 for (size_t i = 0; i < Contents.size(); ++i) {
858 AttributeItem item = Contents[i];
860 case AttributeItem::HiddenAttribute:
862 case AttributeItem::NumericAttribute:
863 Result += getULEBSize(item.Tag);
864 Result += getULEBSize(item.IntValue);
866 case AttributeItem::TextAttribute:
867 Result += getULEBSize(item.Tag);
868 Result += item.StringValue.size() + 1; // string + '\0'
870 case AttributeItem::NumericAndTextAttributes:
871 Result += getULEBSize(item.Tag);
872 Result += getULEBSize(item.IntValue);
873 Result += item.StringValue.size() + 1; // string + '\0';
879 void ARMTargetELFStreamer::finishAttributeSection() {
881 // [ <section-length> "vendor-name"
882 // [ <file-tag> <size> <attribute>*
883 // | <section-tag> <size> <section-number>* 0 <attribute>*
884 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
888 if (FPU != ARM::INVALID_FPU)
889 emitFPUDefaultAttributes();
891 if (Arch != ARM::INVALID_ARCH)
892 emitArchDefaultAttributes();
894 if (Contents.empty())
897 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
899 ARMELFStreamer &Streamer = getStreamer();
901 // Switch to .ARM.attributes section
902 if (AttributeSection) {
903 Streamer.SwitchSection(AttributeSection);
906 Streamer.getContext().getELFSection(".ARM.attributes",
907 ELF::SHT_ARM_ATTRIBUTES,
909 SectionKind::getMetadata());
910 Streamer.SwitchSection(AttributeSection);
913 Streamer.EmitIntValue(0x41, 1);
916 // Vendor size + Vendor name + '\0'
917 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
920 const size_t TagHeaderSize = 1 + 4;
922 const size_t ContentsSize = calculateContentSize();
924 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
925 Streamer.EmitBytes(CurrentVendor);
926 Streamer.EmitIntValue(0, 1); // '\0'
928 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
929 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
931 // Size should have been accounted for already, now
932 // emit each field as its type (ULEB or String)
933 for (size_t i = 0; i < Contents.size(); ++i) {
934 AttributeItem item = Contents[i];
935 Streamer.EmitULEB128IntValue(item.Tag);
937 default: llvm_unreachable("Invalid attribute type");
938 case AttributeItem::NumericAttribute:
939 Streamer.EmitULEB128IntValue(item.IntValue);
941 case AttributeItem::TextAttribute:
942 Streamer.EmitBytes(item.StringValue.upper());
943 Streamer.EmitIntValue(0, 1); // '\0'
945 case AttributeItem::NumericAndTextAttributes:
946 Streamer.EmitULEB128IntValue(item.IntValue);
947 Streamer.EmitBytes(item.StringValue.upper());
948 Streamer.EmitIntValue(0, 1); // '\0'
954 FPU = ARM::INVALID_FPU;
956 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
957 getStreamer().emitInst(Inst, Suffix);
960 void ARMELFStreamer::FinishImpl() {
961 MCTargetStreamer &TS = *getTargetStreamer();
962 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
963 ATS.finishAttributeSection();
965 MCELFStreamer::FinishImpl();
968 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
972 const MCSymbol &Fn) {
973 const MCSectionELF &FnSection =
974 static_cast<const MCSectionELF &>(Fn.getSection());
976 // Create the name for new section
977 StringRef FnSecName(FnSection.getSectionName());
978 SmallString<128> EHSecName(Prefix);
979 if (FnSecName != ".text") {
980 EHSecName += FnSecName;
983 // Get .ARM.extab or .ARM.exidx section
984 const MCSectionELF *EHSection = NULL;
985 if (const MCSymbol *Group = FnSection.getGroup()) {
986 EHSection = getContext().getELFSection(
987 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
988 FnSection.getEntrySize(), Group->getName());
990 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
992 assert(EHSection && "Failed to get the required EH section");
994 // Switch to .ARM.extab or .ARM.exidx section
995 SwitchSection(EHSection);
996 EmitCodeAlignment(4, 0);
999 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1000 SwitchToEHSection(".ARM.extab",
1003 SectionKind::getDataRel(),
1007 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1008 SwitchToEHSection(".ARM.exidx",
1010 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1011 SectionKind::getDataRel(),
1015 void ARMELFStreamer::Reset() {
1019 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1028 UnwindOpAsm.Reset();
1031 void ARMELFStreamer::emitFnStart() {
1032 assert(FnStart == 0);
1033 FnStart = getContext().CreateTempSymbol();
1037 void ARMELFStreamer::emitFnEnd() {
1038 assert(FnStart && ".fnstart must precedes .fnend");
1040 // Emit unwind opcodes if there is no .handlerdata directive
1041 if (!ExTab && !CantUnwind)
1042 FlushUnwindOpcodes(true);
1044 // Emit the exception index table entry
1045 SwitchToExIdxSection(*FnStart);
1047 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1048 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1050 const MCSymbolRefExpr *FnStartRef =
1051 MCSymbolRefExpr::Create(FnStart,
1052 MCSymbolRefExpr::VK_ARM_PREL31,
1055 EmitValue(FnStartRef, 4);
1058 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1060 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1061 const MCSymbolRefExpr *ExTabEntryRef =
1062 MCSymbolRefExpr::Create(ExTab,
1063 MCSymbolRefExpr::VK_ARM_PREL31,
1065 EmitValue(ExTabEntryRef, 4);
1067 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1068 // the second word of exception index table entry. The size of the unwind
1069 // opcodes should always be 4 bytes.
1070 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1071 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1072 assert(Opcodes.size() == 4u &&
1073 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1074 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1078 // Switch to the section containing FnStart
1079 SwitchSection(&FnStart->getSection());
1081 // Clean exception handling frame information
1085 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1087 // Add the R_ARM_NONE fixup at the same position
1088 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1089 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1091 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1092 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1094 AddValueSymbols(PersonalityRef);
1095 MCDataFragment *DF = getOrCreateDataFragment();
1096 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1098 MCFixup::getKindForSize(4, false)));
1101 void ARMELFStreamer::FlushPendingOffset() {
1102 if (PendingOffset != 0) {
1103 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1108 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1109 // Emit the unwind opcode to restore $sp.
1111 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1112 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1113 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1114 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1116 FlushPendingOffset();
1119 // Finalize the unwind opcode sequence
1120 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1122 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1123 // section. Thus, we don't have to create an entry in the .ARM.extab
1125 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1128 // Switch to .ARM.extab section.
1129 SwitchToExTabSection(*FnStart);
1131 // Create .ARM.extab label for offset in .ARM.exidx
1133 ExTab = getContext().CreateTempSymbol();
1138 const MCSymbolRefExpr *PersonalityRef =
1139 MCSymbolRefExpr::Create(Personality,
1140 MCSymbolRefExpr::VK_ARM_PREL31,
1143 EmitValue(PersonalityRef, 4);
1146 // Emit unwind opcodes
1147 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1150 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1151 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1152 // after the unwind opcodes. The handler data consists of several 32-bit
1153 // words, and should be terminated by zero.
1155 // In case that the .handlerdata directive is not specified by the
1156 // programmer, we should emit zero to terminate the handler data.
1157 if (NoHandlerData && !Personality)
1161 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1163 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1165 UnwindOpAsm.setPersonality(Per);
1168 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1169 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1170 PersonalityIndex = Index;
1173 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1175 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1176 "the operand of .setfp directive should be either $sp or $fp");
1181 if (NewSPReg == ARM::SP)
1182 FPOffset = SPOffset + Offset;
1187 void ARMELFStreamer::emitPad(int64_t Offset) {
1188 // Track the change of the $sp offset
1191 // To squash multiple .pad directives, we should delay the unwind opcode
1192 // until the .save, .vsave, .handlerdata, or .fnend directives.
1193 PendingOffset -= Offset;
1196 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1198 // Collect the registers in the register list
1201 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1202 for (size_t i = 0; i < RegList.size(); ++i) {
1203 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1204 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1205 unsigned Bit = (1u << Reg);
1206 if ((Mask & Bit) == 0) {
1212 // Track the change the $sp offset: For the .save directive, the
1213 // corresponding push instruction will decrease the $sp by (4 * Count).
1214 // For the .vsave directive, the corresponding vpush instruction will
1215 // decrease $sp by (8 * Count).
1216 SPOffset -= Count * (IsVector ? 8 : 4);
1219 FlushPendingOffset();
1221 UnwindOpAsm.EmitVFPRegSave(Mask);
1223 UnwindOpAsm.EmitRegSave(Mask);
1226 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1227 const SmallVectorImpl<uint8_t> &Opcodes) {
1228 FlushPendingOffset();
1229 SPOffset = SPOffset - Offset;
1230 UnwindOpAsm.EmitRaw(Opcodes);
1235 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1236 bool isVerboseAsm, bool useLoc, bool useCFI,
1237 bool useDwarfDirectory,
1238 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1239 MCAsmBackend *TAB, bool ShowInst) {
1241 llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useLoc, useCFI,
1242 useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
1243 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1247 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1248 raw_ostream &OS, MCCodeEmitter *Emitter,
1249 bool RelaxAll, bool NoExecStack,
1251 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1252 new ARMTargetELFStreamer(*S);
1253 // FIXME: This should eventually end up somewhere else where more
1254 // intelligent flag decisions can be made. For now we are just maintaining
1255 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1256 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1259 S->getAssembler().setRelaxAll(true);
1261 S->getAssembler().setNoExecStack(true);