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 emitMovSP(unsigned Reg, int64_t Offset = 0);
127 virtual void emitPad(int64_t Offset);
128 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
130 virtual void emitUnwindRaw(int64_t Offset,
131 const SmallVectorImpl<uint8_t> &Opcodes);
133 virtual void switchVendor(StringRef Vendor);
134 virtual void emitAttribute(unsigned Attribute, unsigned Value);
135 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
136 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
137 StringRef StrinValue);
138 virtual void emitArch(unsigned Arch);
139 virtual void emitObjectArch(unsigned Arch);
140 virtual void emitFPU(unsigned FPU);
141 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
142 virtual void finishAttributeSection();
144 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
147 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
148 MCInstPrinter &InstPrinter, bool VerboseAsm);
151 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
152 formatted_raw_ostream &OS,
153 MCInstPrinter &InstPrinter,
155 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
156 IsVerboseAsm(VerboseAsm) {}
157 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
158 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
159 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
160 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
161 OS << "\t.personality " << Personality->getName() << '\n';
163 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
164 OS << "\t.personalityindex " << Index << '\n';
166 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
167 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
170 InstPrinter.printRegName(OS, FpReg);
172 InstPrinter.printRegName(OS, SpReg);
174 OS << ", #" << Offset;
177 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
178 assert((Reg != ARM::SP && Reg != ARM::PC) &&
179 "the operand of .movsp cannot be either sp or pc");
182 InstPrinter.printRegName(OS, Reg);
184 OS << ", #" << Offset;
187 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
188 OS << "\t.pad\t#" << Offset << '\n';
190 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
192 assert(RegList.size() && "RegList should not be empty");
198 InstPrinter.printRegName(OS, RegList[0]);
200 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
202 InstPrinter.printRegName(OS, RegList[i]);
207 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
209 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
210 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
212 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
214 OS << "\t@ " << Name;
218 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
221 case ARMBuildAttrs::CPU_name:
222 OS << "\t.cpu\t" << String.lower();
225 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
227 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
229 OS << "\t@ " << Name;
235 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
237 StringRef StringValue) {
239 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
240 case ARMBuildAttrs::compatibility:
241 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
242 if (!StringValue.empty())
243 OS << ", \"" << StringValue << "\"";
245 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
250 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
251 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
253 void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
254 OS << "\t.object_arch\t" << GetArchName(Arch) << '\n';
256 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
257 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
259 void ARMTargetAsmStreamer::finishAttributeSection() {
262 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
263 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
266 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
270 OS << "\t0x" << utohexstr(Inst) << "\n";
273 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
274 const SmallVectorImpl<uint8_t> &Opcodes) {
275 OS << "\t.unwind_raw " << Offset;
276 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
279 OS << ", 0x" << utohexstr(*OCI);
283 class ARMTargetELFStreamer : public ARMTargetStreamer {
285 // This structure holds all attributes, accounting for
286 // their string/numeric value, so we can later emmit them
287 // in declaration order, keeping all in the same vector
288 struct AttributeItem {
293 NumericAndTextAttributes
297 StringRef StringValue;
299 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
300 return (LHS.Tag < RHS.Tag);
304 StringRef CurrentVendor;
307 unsigned EmittedArch;
308 SmallVector<AttributeItem, 64> Contents;
310 const MCSection *AttributeSection;
312 // FIXME: this should be in a more generic place, but
313 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
314 static size_t getULEBSize(int Value) {
318 Size += sizeof(int8_t); // Is this really necessary?
323 AttributeItem *getAttributeItem(unsigned Attribute) {
324 for (size_t i = 0; i < Contents.size(); ++i)
325 if (Contents[i].Tag == Attribute)
330 void setAttributeItem(unsigned Attribute, unsigned Value,
331 bool OverwriteExisting) {
332 // Look for existing attribute item
333 if (AttributeItem *Item = getAttributeItem(Attribute)) {
334 if (!OverwriteExisting)
336 Item->Type = AttributeItem::NumericAttribute;
337 Item->IntValue = Value;
341 // Create new attribute item
342 AttributeItem Item = {
343 AttributeItem::NumericAttribute,
348 Contents.push_back(Item);
351 void setAttributeItem(unsigned Attribute, StringRef Value,
352 bool OverwriteExisting) {
353 // Look for existing attribute item
354 if (AttributeItem *Item = getAttributeItem(Attribute)) {
355 if (!OverwriteExisting)
357 Item->Type = AttributeItem::TextAttribute;
358 Item->StringValue = Value;
362 // Create new attribute item
363 AttributeItem Item = {
364 AttributeItem::TextAttribute,
369 Contents.push_back(Item);
372 void setAttributeItems(unsigned Attribute, unsigned IntValue,
373 StringRef StringValue, bool OverwriteExisting) {
374 // Look for existing attribute item
375 if (AttributeItem *Item = getAttributeItem(Attribute)) {
376 if (!OverwriteExisting)
378 Item->Type = AttributeItem::NumericAndTextAttributes;
379 Item->IntValue = IntValue;
380 Item->StringValue = StringValue;
384 // Create new attribute item
385 AttributeItem Item = {
386 AttributeItem::NumericAndTextAttributes,
391 Contents.push_back(Item);
394 void emitArchDefaultAttributes();
395 void emitFPUDefaultAttributes();
397 ARMELFStreamer &getStreamer();
399 virtual void emitFnStart();
400 virtual void emitFnEnd();
401 virtual void emitCantUnwind();
402 virtual void emitPersonality(const MCSymbol *Personality);
403 virtual void emitPersonalityIndex(unsigned Index);
404 virtual void emitHandlerData();
405 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
406 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
407 virtual void emitPad(int64_t Offset);
408 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
410 virtual void emitUnwindRaw(int64_t Offset,
411 const SmallVectorImpl<uint8_t> &Opcodes);
413 virtual void switchVendor(StringRef Vendor);
414 virtual void emitAttribute(unsigned Attribute, unsigned Value);
415 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
416 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
417 StringRef StringValue);
418 virtual void emitArch(unsigned Arch);
419 virtual void emitObjectArch(unsigned Arch);
420 virtual void emitFPU(unsigned FPU);
421 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
422 virtual void finishAttributeSection();
424 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
426 size_t calculateContentSize() const;
429 ARMTargetELFStreamer(MCStreamer &S)
430 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
431 Arch(ARM::INVALID_ARCH), EmittedArch(ARM::INVALID_ARCH),
432 AttributeSection(0) {}
435 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
436 /// the appropriate points in the object files. These symbols are defined in the
437 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
439 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
440 /// region of ARM code, Thumb code or data in a section. In practice, this
441 /// emission does not rely on explicit assembler directives but on inherent
442 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
443 /// r0, r0, r0" an instruction).
445 /// As a result this system is orthogonal to the DataRegion infrastructure used
446 /// by MachO. Beware!
447 class ARMELFStreamer : public MCELFStreamer {
449 friend class ARMTargetELFStreamer;
451 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
452 MCCodeEmitter *Emitter, bool IsThumb)
453 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
454 MappingSymbolCounter(0), LastEMS(EMS_None) {
460 virtual void FinishImpl();
462 // ARM exception handling directives
465 void emitCantUnwind();
466 void emitPersonality(const MCSymbol *Per);
467 void emitPersonalityIndex(unsigned index);
468 void emitHandlerData();
469 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
470 void emitMovSP(unsigned Reg, int64_t Offset = 0);
471 void emitPad(int64_t Offset);
472 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
473 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
475 virtual void ChangeSection(const MCSection *Section,
476 const MCExpr *Subsection) {
477 // We have to keep track of the mapping symbol state of any sections we
478 // use. Each one should start off as EMS_None, which is provided as the
479 // default constructor by DenseMap::lookup.
480 LastMappingSymbols[getPreviousSection().first] = LastEMS;
481 LastEMS = LastMappingSymbols.lookup(Section);
483 MCELFStreamer::ChangeSection(Section, Subsection);
486 /// This function is the one used to emit instruction data into the ELF
487 /// streamer. We override it to add the appropriate mapping symbol if
489 virtual void EmitInstruction(const MCInst& Inst, const MCSubtargetInfo &STI) {
491 EmitThumbMappingSymbol();
493 EmitARMMappingSymbol();
495 MCELFStreamer::EmitInstruction(Inst, STI);
498 virtual void emitInst(uint32_t Inst, char Suffix) {
501 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
508 EmitARMMappingSymbol();
509 for (unsigned II = 0, IE = Size; II != IE; II++) {
510 const unsigned I = LittleEndian ? (Size - II - 1) : II;
511 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
517 Size = (Suffix == 'n' ? 2 : 4);
520 EmitThumbMappingSymbol();
521 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
522 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
523 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
524 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
525 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
530 llvm_unreachable("Invalid Suffix");
533 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
536 /// This is one of the functions used to emit data into an ELF section, so the
537 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
539 virtual void EmitBytes(StringRef Data) {
540 EmitDataMappingSymbol();
541 MCELFStreamer::EmitBytes(Data);
544 /// This is one of the functions used to emit data into an ELF section, so the
545 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
547 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
548 EmitDataMappingSymbol();
549 MCELFStreamer::EmitValueImpl(Value, Size);
552 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
553 MCELFStreamer::EmitAssemblerFlag(Flag);
556 case MCAF_SyntaxUnified:
557 return; // no-op here.
560 return; // Change to Thumb mode
563 return; // Change to ARM mode
566 case MCAF_SubsectionsViaSymbols:
572 enum ElfMappingSymbol {
579 void EmitDataMappingSymbol() {
580 if (LastEMS == EMS_Data) return;
581 EmitMappingSymbol("$d");
585 void EmitThumbMappingSymbol() {
586 if (LastEMS == EMS_Thumb) return;
587 EmitMappingSymbol("$t");
591 void EmitARMMappingSymbol() {
592 if (LastEMS == EMS_ARM) return;
593 EmitMappingSymbol("$a");
597 void EmitMappingSymbol(StringRef Name) {
598 MCSymbol *Start = getContext().CreateTempSymbol();
602 getContext().GetOrCreateSymbol(Name + "." +
603 Twine(MappingSymbolCounter++));
605 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
606 MCELF::SetType(SD, ELF::STT_NOTYPE);
607 MCELF::SetBinding(SD, ELF::STB_LOCAL);
608 SD.setExternal(false);
609 AssignSection(Symbol, getCurrentSection().first);
611 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
612 Symbol->setVariableValue(Value);
615 void EmitThumbFunc(MCSymbol *Func) {
616 // FIXME: Anything needed here to flag the function as thumb?
618 getAssembler().setIsThumbFunc(Func);
620 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
621 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
624 // Helper functions for ARM exception handling directives
627 void EmitPersonalityFixup(StringRef Name);
628 void FlushPendingOffset();
629 void FlushUnwindOpcodes(bool NoHandlerData);
631 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
632 SectionKind Kind, const MCSymbol &Fn);
633 void SwitchToExTabSection(const MCSymbol &FnStart);
634 void SwitchToExIdxSection(const MCSymbol &FnStart);
636 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
639 int64_t MappingSymbolCounter;
641 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
642 ElfMappingSymbol LastEMS;
644 // ARM Exception Handling Frame Information
647 const MCSymbol *Personality;
648 unsigned PersonalityIndex;
649 unsigned FPReg; // Frame pointer register
650 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
651 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
652 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
655 SmallVector<uint8_t, 64> Opcodes;
656 UnwindOpcodeAssembler UnwindOpAsm;
658 } // end anonymous namespace
660 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
661 return static_cast<ARMELFStreamer &>(Streamer);
664 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
665 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
666 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
667 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
668 getStreamer().emitPersonality(Personality);
670 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
671 getStreamer().emitPersonalityIndex(Index);
673 void ARMTargetELFStreamer::emitHandlerData() {
674 getStreamer().emitHandlerData();
676 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
678 getStreamer().emitSetFP(FpReg, SpReg, Offset);
680 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
681 getStreamer().emitMovSP(Reg, Offset);
683 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
684 getStreamer().emitPad(Offset);
686 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
688 getStreamer().emitRegSave(RegList, isVector);
690 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
691 const SmallVectorImpl<uint8_t> &Opcodes) {
692 getStreamer().emitUnwindRaw(Offset, Opcodes);
694 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
695 assert(!Vendor.empty() && "Vendor cannot be empty.");
697 if (CurrentVendor == Vendor)
700 if (!CurrentVendor.empty())
701 finishAttributeSection();
703 assert(Contents.empty() &&
704 ".ARM.attributes should be flushed before changing vendor");
705 CurrentVendor = Vendor;
708 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
709 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
711 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
713 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
715 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
717 StringRef StringValue) {
718 setAttributeItems(Attribute, IntValue, StringValue,
719 /* OverwriteExisting= */ true);
721 void ARMTargetELFStreamer::emitArch(unsigned Value) {
724 void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
727 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
728 using namespace ARMBuildAttrs;
730 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
731 if (EmittedArch == ARM::INVALID_ARCH)
732 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
734 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(EmittedArch), false);
743 setAttributeItem(ARM_ISA_use, Allowed, false);
751 setAttributeItem(ARM_ISA_use, Allowed, false);
752 setAttributeItem(THUMB_ISA_use, Allowed, false);
756 setAttributeItem(ARM_ISA_use, Allowed, false);
757 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
762 setAttributeItem(ARM_ISA_use, Allowed, false);
763 setAttributeItem(THUMB_ISA_use, Allowed, false);
764 setAttributeItem(Virtualization_use, AllowTZ, false);
768 setAttributeItem(THUMB_ISA_use, Allowed, false);
772 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
776 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
777 setAttributeItem(ARM_ISA_use, Allowed, false);
778 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
782 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
783 setAttributeItem(ARM_ISA_use, Allowed, false);
784 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
788 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
789 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
793 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
794 setAttributeItem(ARM_ISA_use, Allowed, false);
795 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
796 setAttributeItem(MPextension_use, Allowed, false);
797 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
801 setAttributeItem(ARM_ISA_use, Allowed, false);
802 setAttributeItem(THUMB_ISA_use, Allowed, false);
803 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
807 setAttributeItem(ARM_ISA_use, Allowed, false);
808 setAttributeItem(THUMB_ISA_use, Allowed, false);
809 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
813 report_fatal_error("Unknown Arch: " + Twine(Arch));
817 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
820 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
824 setAttributeItem(ARMBuildAttrs::FP_arch,
825 ARMBuildAttrs::AllowFPv2,
826 /* OverwriteExisting= */ false);
830 setAttributeItem(ARMBuildAttrs::FP_arch,
831 ARMBuildAttrs::AllowFPv3A,
832 /* OverwriteExisting= */ false);
836 setAttributeItem(ARMBuildAttrs::FP_arch,
837 ARMBuildAttrs::AllowFPv3B,
838 /* OverwriteExisting= */ false);
842 setAttributeItem(ARMBuildAttrs::FP_arch,
843 ARMBuildAttrs::AllowFPv4A,
844 /* OverwriteExisting= */ false);
848 setAttributeItem(ARMBuildAttrs::FP_arch,
849 ARMBuildAttrs::AllowFPv4B,
850 /* OverwriteExisting= */ false);
854 setAttributeItem(ARMBuildAttrs::FP_arch,
855 ARMBuildAttrs::AllowFPARMv8A,
856 /* OverwriteExisting= */ false);
860 setAttributeItem(ARMBuildAttrs::FP_arch,
861 ARMBuildAttrs::AllowFPv3A,
862 /* OverwriteExisting= */ false);
863 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
864 ARMBuildAttrs::AllowNeon,
865 /* OverwriteExisting= */ false);
868 case ARM::NEON_VFPV4:
869 setAttributeItem(ARMBuildAttrs::FP_arch,
870 ARMBuildAttrs::AllowFPv4A,
871 /* OverwriteExisting= */ false);
872 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
873 ARMBuildAttrs::AllowNeon2,
874 /* OverwriteExisting= */ false);
877 case ARM::NEON_FP_ARMV8:
878 case ARM::CRYPTO_NEON_FP_ARMV8:
879 setAttributeItem(ARMBuildAttrs::FP_arch,
880 ARMBuildAttrs::AllowFPARMv8A,
881 /* OverwriteExisting= */ false);
882 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
883 ARMBuildAttrs::AllowNeonARMv8,
884 /* OverwriteExisting= */ false);
891 report_fatal_error("Unknown FPU: " + Twine(FPU));
895 size_t ARMTargetELFStreamer::calculateContentSize() const {
897 for (size_t i = 0; i < Contents.size(); ++i) {
898 AttributeItem item = Contents[i];
900 case AttributeItem::HiddenAttribute:
902 case AttributeItem::NumericAttribute:
903 Result += getULEBSize(item.Tag);
904 Result += getULEBSize(item.IntValue);
906 case AttributeItem::TextAttribute:
907 Result += getULEBSize(item.Tag);
908 Result += item.StringValue.size() + 1; // string + '\0'
910 case AttributeItem::NumericAndTextAttributes:
911 Result += getULEBSize(item.Tag);
912 Result += getULEBSize(item.IntValue);
913 Result += item.StringValue.size() + 1; // string + '\0';
919 void ARMTargetELFStreamer::finishAttributeSection() {
921 // [ <section-length> "vendor-name"
922 // [ <file-tag> <size> <attribute>*
923 // | <section-tag> <size> <section-number>* 0 <attribute>*
924 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
928 if (FPU != ARM::INVALID_FPU)
929 emitFPUDefaultAttributes();
931 if (Arch != ARM::INVALID_ARCH)
932 emitArchDefaultAttributes();
934 if (Contents.empty())
937 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
939 ARMELFStreamer &Streamer = getStreamer();
941 // Switch to .ARM.attributes section
942 if (AttributeSection) {
943 Streamer.SwitchSection(AttributeSection);
946 Streamer.getContext().getELFSection(".ARM.attributes",
947 ELF::SHT_ARM_ATTRIBUTES,
949 SectionKind::getMetadata());
950 Streamer.SwitchSection(AttributeSection);
953 Streamer.EmitIntValue(0x41, 1);
956 // Vendor size + Vendor name + '\0'
957 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
960 const size_t TagHeaderSize = 1 + 4;
962 const size_t ContentsSize = calculateContentSize();
964 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
965 Streamer.EmitBytes(CurrentVendor);
966 Streamer.EmitIntValue(0, 1); // '\0'
968 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
969 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
971 // Size should have been accounted for already, now
972 // emit each field as its type (ULEB or String)
973 for (size_t i = 0; i < Contents.size(); ++i) {
974 AttributeItem item = Contents[i];
975 Streamer.EmitULEB128IntValue(item.Tag);
977 default: llvm_unreachable("Invalid attribute type");
978 case AttributeItem::NumericAttribute:
979 Streamer.EmitULEB128IntValue(item.IntValue);
981 case AttributeItem::TextAttribute:
982 Streamer.EmitBytes(item.StringValue.upper());
983 Streamer.EmitIntValue(0, 1); // '\0'
985 case AttributeItem::NumericAndTextAttributes:
986 Streamer.EmitULEB128IntValue(item.IntValue);
987 Streamer.EmitBytes(item.StringValue.upper());
988 Streamer.EmitIntValue(0, 1); // '\0'
994 FPU = ARM::INVALID_FPU;
997 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
998 getStreamer().EmitFixup(S, FK_Data_4);
1000 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1001 getStreamer().emitInst(Inst, Suffix);
1004 void ARMELFStreamer::FinishImpl() {
1005 MCTargetStreamer &TS = *getTargetStreamer();
1006 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1007 ATS.finishAttributeSection();
1009 MCELFStreamer::FinishImpl();
1012 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1016 const MCSymbol &Fn) {
1017 const MCSectionELF &FnSection =
1018 static_cast<const MCSectionELF &>(Fn.getSection());
1020 // Create the name for new section
1021 StringRef FnSecName(FnSection.getSectionName());
1022 SmallString<128> EHSecName(Prefix);
1023 if (FnSecName != ".text") {
1024 EHSecName += FnSecName;
1027 // Get .ARM.extab or .ARM.exidx section
1028 const MCSectionELF *EHSection = NULL;
1029 if (const MCSymbol *Group = FnSection.getGroup()) {
1030 EHSection = getContext().getELFSection(
1031 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1032 FnSection.getEntrySize(), Group->getName());
1034 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1036 assert(EHSection && "Failed to get the required EH section");
1038 // Switch to .ARM.extab or .ARM.exidx section
1039 SwitchSection(EHSection);
1040 EmitCodeAlignment(4, 0);
1043 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1044 SwitchToEHSection(".ARM.extab",
1047 SectionKind::getDataRel(),
1051 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1052 SwitchToEHSection(".ARM.exidx",
1054 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1055 SectionKind::getDataRel(),
1058 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1059 MCDataFragment *Frag = getOrCreateDataFragment();
1060 Frag->getFixups().push_back(MCFixup::Create(Frag->getContents().size(), Expr,
1064 void ARMELFStreamer::Reset() {
1068 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1077 UnwindOpAsm.Reset();
1080 void ARMELFStreamer::emitFnStart() {
1081 assert(FnStart == 0);
1082 FnStart = getContext().CreateTempSymbol();
1086 void ARMELFStreamer::emitFnEnd() {
1087 assert(FnStart && ".fnstart must precedes .fnend");
1089 // Emit unwind opcodes if there is no .handlerdata directive
1090 if (!ExTab && !CantUnwind)
1091 FlushUnwindOpcodes(true);
1093 // Emit the exception index table entry
1094 SwitchToExIdxSection(*FnStart);
1096 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1097 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1099 const MCSymbolRefExpr *FnStartRef =
1100 MCSymbolRefExpr::Create(FnStart,
1101 MCSymbolRefExpr::VK_ARM_PREL31,
1104 EmitValue(FnStartRef, 4);
1107 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1109 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1110 const MCSymbolRefExpr *ExTabEntryRef =
1111 MCSymbolRefExpr::Create(ExTab,
1112 MCSymbolRefExpr::VK_ARM_PREL31,
1114 EmitValue(ExTabEntryRef, 4);
1116 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1117 // the second word of exception index table entry. The size of the unwind
1118 // opcodes should always be 4 bytes.
1119 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1120 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1121 assert(Opcodes.size() == 4u &&
1122 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1123 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1127 // Switch to the section containing FnStart
1128 SwitchSection(&FnStart->getSection());
1130 // Clean exception handling frame information
1134 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1136 // Add the R_ARM_NONE fixup at the same position
1137 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1138 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1140 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1141 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1143 AddValueSymbols(PersonalityRef);
1144 MCDataFragment *DF = getOrCreateDataFragment();
1145 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1147 MCFixup::getKindForSize(4, false)));
1150 void ARMELFStreamer::FlushPendingOffset() {
1151 if (PendingOffset != 0) {
1152 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1157 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1158 // Emit the unwind opcode to restore $sp.
1160 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1161 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1162 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1163 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1165 FlushPendingOffset();
1168 // Finalize the unwind opcode sequence
1169 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1171 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1172 // section. Thus, we don't have to create an entry in the .ARM.extab
1174 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1177 // Switch to .ARM.extab section.
1178 SwitchToExTabSection(*FnStart);
1180 // Create .ARM.extab label for offset in .ARM.exidx
1182 ExTab = getContext().CreateTempSymbol();
1187 const MCSymbolRefExpr *PersonalityRef =
1188 MCSymbolRefExpr::Create(Personality,
1189 MCSymbolRefExpr::VK_ARM_PREL31,
1192 EmitValue(PersonalityRef, 4);
1195 // Emit unwind opcodes
1196 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1199 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1200 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1201 // after the unwind opcodes. The handler data consists of several 32-bit
1202 // words, and should be terminated by zero.
1204 // In case that the .handlerdata directive is not specified by the
1205 // programmer, we should emit zero to terminate the handler data.
1206 if (NoHandlerData && !Personality)
1210 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1212 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1214 UnwindOpAsm.setPersonality(Per);
1217 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1218 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1219 PersonalityIndex = Index;
1222 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1224 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1225 "the operand of .setfp directive should be either $sp or $fp");
1230 if (NewSPReg == ARM::SP)
1231 FPOffset = SPOffset + Offset;
1236 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1237 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1238 "the operand of .movsp cannot be either sp or pc");
1239 assert(FPReg == ARM::SP && "current FP must be SP");
1241 FlushPendingOffset();
1244 FPOffset = SPOffset + Offset;
1246 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1247 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1250 void ARMELFStreamer::emitPad(int64_t Offset) {
1251 // Track the change of the $sp offset
1254 // To squash multiple .pad directives, we should delay the unwind opcode
1255 // until the .save, .vsave, .handlerdata, or .fnend directives.
1256 PendingOffset -= Offset;
1259 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1261 // Collect the registers in the register list
1264 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1265 for (size_t i = 0; i < RegList.size(); ++i) {
1266 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1267 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1268 unsigned Bit = (1u << Reg);
1269 if ((Mask & Bit) == 0) {
1275 // Track the change the $sp offset: For the .save directive, the
1276 // corresponding push instruction will decrease the $sp by (4 * Count).
1277 // For the .vsave directive, the corresponding vpush instruction will
1278 // decrease $sp by (8 * Count).
1279 SPOffset -= Count * (IsVector ? 8 : 4);
1282 FlushPendingOffset();
1284 UnwindOpAsm.EmitVFPRegSave(Mask);
1286 UnwindOpAsm.EmitRegSave(Mask);
1289 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1290 const SmallVectorImpl<uint8_t> &Opcodes) {
1291 FlushPendingOffset();
1292 SPOffset = SPOffset - Offset;
1293 UnwindOpAsm.EmitRaw(Opcodes);
1298 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1299 bool isVerboseAsm, bool useLoc, bool useCFI,
1300 bool useDwarfDirectory,
1301 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1302 MCAsmBackend *TAB, bool ShowInst) {
1304 llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useLoc, useCFI,
1305 useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
1306 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1310 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1311 raw_ostream &OS, MCCodeEmitter *Emitter,
1312 bool RelaxAll, bool NoExecStack,
1314 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1315 new ARMTargetELFStreamer(*S);
1316 // FIXME: This should eventually end up somewhere else where more
1317 // intelligent flag decisions can be made. For now we are just maintaining
1318 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1319 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1322 S->getAssembler().setRelaxAll(true);
1324 S->getAssembler().setNoExecStack(true);