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 static bool isThumb(const MCSubtargetInfo& STI) {
108 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
111 void ARMTargetStreamer::anchor() {}
113 void ARMTargetStreamer::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
114 MCSubtargetInfo *EndInfo) {
115 // If either end mode is unknown (EndInfo == NULL) or different than
116 // the start mode, then restore the start mode.
117 const bool WasThumb = isThumb(StartInfo);
118 if (EndInfo == NULL || WasThumb != isThumb(*EndInfo)) {
120 Streamer->EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
122 EndInfo->ToggleFeature(ARM::ModeThumb);
128 class ARMELFStreamer;
130 class ARMTargetAsmStreamer : public ARMTargetStreamer {
131 formatted_raw_ostream &OS;
132 MCInstPrinter &InstPrinter;
135 virtual void emitFnStart();
136 virtual void emitFnEnd();
137 virtual void emitCantUnwind();
138 virtual void emitPersonality(const MCSymbol *Personality);
139 virtual void emitPersonalityIndex(unsigned Index);
140 virtual void emitHandlerData();
141 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
142 virtual void emitPad(int64_t Offset);
143 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
145 virtual void emitUnwindRaw(int64_t Offset,
146 const SmallVectorImpl<uint8_t> &Opcodes);
148 virtual void switchVendor(StringRef Vendor);
149 virtual void emitAttribute(unsigned Attribute, unsigned Value);
150 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
151 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
152 StringRef StrinValue);
153 virtual void emitArch(unsigned Arch);
154 virtual void emitFPU(unsigned FPU);
155 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
156 virtual void finishAttributeSection();
159 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
163 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
164 MCInstPrinter &InstPrinter,
166 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
167 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
168 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
169 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
170 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
171 OS << "\t.personality " << Personality->getName() << '\n';
173 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
174 OS << "\t.personalityindex " << Index << '\n';
176 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
177 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
180 InstPrinter.printRegName(OS, FpReg);
182 InstPrinter.printRegName(OS, SpReg);
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::emitFPU(unsigned FPU) {
254 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
256 void ARMTargetAsmStreamer::finishAttributeSection() {
259 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
263 OS << "\t0x" << utohexstr(Inst) << "\n";
266 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
267 const SmallVectorImpl<uint8_t> &Opcodes) {
268 OS << "\t.unwind_raw " << Offset;
269 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
272 OS << ", 0x" << utohexstr(*OCI);
276 class ARMTargetELFStreamer : public ARMTargetStreamer {
278 // This structure holds all attributes, accounting for
279 // their string/numeric value, so we can later emmit them
280 // in declaration order, keeping all in the same vector
281 struct AttributeItem {
286 NumericAndTextAttributes
290 StringRef StringValue;
292 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
293 return (LHS.Tag < RHS.Tag);
297 StringRef CurrentVendor;
300 SmallVector<AttributeItem, 64> Contents;
302 const MCSection *AttributeSection;
304 // FIXME: this should be in a more generic place, but
305 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
306 static size_t getULEBSize(int Value) {
310 Size += sizeof(int8_t); // Is this really necessary?
315 AttributeItem *getAttributeItem(unsigned Attribute) {
316 for (size_t i = 0; i < Contents.size(); ++i)
317 if (Contents[i].Tag == Attribute)
322 void setAttributeItem(unsigned Attribute, unsigned Value,
323 bool OverwriteExisting) {
324 // Look for existing attribute item
325 if (AttributeItem *Item = getAttributeItem(Attribute)) {
326 if (!OverwriteExisting)
328 Item->Type = AttributeItem::NumericAttribute;
329 Item->IntValue = Value;
333 // Create new attribute item
334 AttributeItem Item = {
335 AttributeItem::NumericAttribute,
340 Contents.push_back(Item);
343 void setAttributeItem(unsigned Attribute, StringRef Value,
344 bool OverwriteExisting) {
345 // Look for existing attribute item
346 if (AttributeItem *Item = getAttributeItem(Attribute)) {
347 if (!OverwriteExisting)
349 Item->Type = AttributeItem::TextAttribute;
350 Item->StringValue = Value;
354 // Create new attribute item
355 AttributeItem Item = {
356 AttributeItem::TextAttribute,
361 Contents.push_back(Item);
364 void setAttributeItems(unsigned Attribute, unsigned IntValue,
365 StringRef StringValue, bool OverwriteExisting) {
366 // Look for existing attribute item
367 if (AttributeItem *Item = getAttributeItem(Attribute)) {
368 if (!OverwriteExisting)
370 Item->Type = AttributeItem::NumericAndTextAttributes;
371 Item->IntValue = IntValue;
372 Item->StringValue = StringValue;
376 // Create new attribute item
377 AttributeItem Item = {
378 AttributeItem::NumericAndTextAttributes,
383 Contents.push_back(Item);
386 void emitArchDefaultAttributes();
387 void emitFPUDefaultAttributes();
389 ARMELFStreamer &getStreamer();
391 virtual void emitFnStart();
392 virtual void emitFnEnd();
393 virtual void emitCantUnwind();
394 virtual void emitPersonality(const MCSymbol *Personality);
395 virtual void emitPersonalityIndex(unsigned Index);
396 virtual void emitHandlerData();
397 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
398 virtual void emitPad(int64_t Offset);
399 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
401 virtual void emitUnwindRaw(int64_t Offset,
402 const SmallVectorImpl<uint8_t> &Opcodes);
404 virtual void switchVendor(StringRef Vendor);
405 virtual void emitAttribute(unsigned Attribute, unsigned Value);
406 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
407 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
408 StringRef StringValue);
409 virtual void emitArch(unsigned Arch);
410 virtual void emitFPU(unsigned FPU);
411 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
412 virtual void finishAttributeSection();
414 size_t calculateContentSize() const;
417 ARMTargetELFStreamer()
418 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
419 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
423 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
424 /// the appropriate points in the object files. These symbols are defined in the
425 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
427 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
428 /// region of ARM code, Thumb code or data in a section. In practice, this
429 /// emission does not rely on explicit assembler directives but on inherent
430 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
431 /// r0, r0, r0" an instruction).
433 /// As a result this system is orthogonal to the DataRegion infrastructure used
434 /// by MachO. Beware!
435 class ARMELFStreamer : public MCELFStreamer {
437 friend class ARMTargetELFStreamer;
439 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
440 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
442 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
443 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
449 virtual void FinishImpl();
451 // ARM exception handling directives
454 void emitCantUnwind();
455 void emitPersonality(const MCSymbol *Per);
456 void emitPersonalityIndex(unsigned index);
457 void emitHandlerData();
458 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
459 void emitPad(int64_t Offset);
460 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
461 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
463 virtual void ChangeSection(const MCSection *Section,
464 const MCExpr *Subsection) {
465 // We have to keep track of the mapping symbol state of any sections we
466 // use. Each one should start off as EMS_None, which is provided as the
467 // default constructor by DenseMap::lookup.
468 LastMappingSymbols[getPreviousSection().first] = LastEMS;
469 LastEMS = LastMappingSymbols.lookup(Section);
471 MCELFStreamer::ChangeSection(Section, Subsection);
474 /// This function is the one used to emit instruction data into the ELF
475 /// streamer. We override it to add the appropriate mapping symbol if
477 virtual void EmitInstruction(const MCInst& Inst) {
479 EmitThumbMappingSymbol();
481 EmitARMMappingSymbol();
483 MCELFStreamer::EmitInstruction(Inst);
486 virtual void emitInst(uint32_t Inst, char Suffix) {
489 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
496 EmitARMMappingSymbol();
497 for (unsigned II = 0, IE = Size; II != IE; II++) {
498 const unsigned I = LittleEndian ? (Size - II - 1) : II;
499 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
505 Size = (Suffix == 'n' ? 2 : 4);
508 EmitThumbMappingSymbol();
509 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
510 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
511 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
512 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
513 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
518 llvm_unreachable("Invalid Suffix");
521 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
524 /// This is one of the functions used to emit data into an ELF section, so the
525 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
527 virtual void EmitBytes(StringRef Data) {
528 EmitDataMappingSymbol();
529 MCELFStreamer::EmitBytes(Data);
532 /// This is one of the functions used to emit data into an ELF section, so the
533 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
535 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
536 EmitDataMappingSymbol();
537 MCELFStreamer::EmitValueImpl(Value, Size);
540 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
541 MCELFStreamer::EmitAssemblerFlag(Flag);
544 case MCAF_SyntaxUnified:
545 return; // no-op here.
548 return; // Change to Thumb mode
551 return; // Change to ARM mode
554 case MCAF_SubsectionsViaSymbols:
560 enum ElfMappingSymbol {
567 void EmitDataMappingSymbol() {
568 if (LastEMS == EMS_Data) return;
569 EmitMappingSymbol("$d");
573 void EmitThumbMappingSymbol() {
574 if (LastEMS == EMS_Thumb) return;
575 EmitMappingSymbol("$t");
579 void EmitARMMappingSymbol() {
580 if (LastEMS == EMS_ARM) return;
581 EmitMappingSymbol("$a");
585 void EmitMappingSymbol(StringRef Name) {
586 MCSymbol *Start = getContext().CreateTempSymbol();
590 getContext().GetOrCreateSymbol(Name + "." +
591 Twine(MappingSymbolCounter++));
593 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
594 MCELF::SetType(SD, ELF::STT_NOTYPE);
595 MCELF::SetBinding(SD, ELF::STB_LOCAL);
596 SD.setExternal(false);
597 AssignSection(Symbol, getCurrentSection().first);
599 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
600 Symbol->setVariableValue(Value);
603 void EmitThumbFunc(MCSymbol *Func) {
604 // FIXME: Anything needed here to flag the function as thumb?
606 getAssembler().setIsThumbFunc(Func);
608 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
609 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
612 // Helper functions for ARM exception handling directives
615 void EmitPersonalityFixup(StringRef Name);
616 void FlushPendingOffset();
617 void FlushUnwindOpcodes(bool NoHandlerData);
619 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
620 SectionKind Kind, const MCSymbol &Fn);
621 void SwitchToExTabSection(const MCSymbol &FnStart);
622 void SwitchToExIdxSection(const MCSymbol &FnStart);
625 int64_t MappingSymbolCounter;
627 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
628 ElfMappingSymbol LastEMS;
630 // ARM Exception Handling Frame Information
633 const MCSymbol *Personality;
634 unsigned PersonalityIndex;
635 unsigned FPReg; // Frame pointer register
636 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
637 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
638 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
641 SmallVector<uint8_t, 64> Opcodes;
642 UnwindOpcodeAssembler UnwindOpAsm;
644 } // end anonymous namespace
646 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
647 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
651 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
652 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
653 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
654 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
655 getStreamer().emitPersonality(Personality);
657 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
658 getStreamer().emitPersonalityIndex(Index);
660 void ARMTargetELFStreamer::emitHandlerData() {
661 getStreamer().emitHandlerData();
663 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
665 getStreamer().emitSetFP(FpReg, SpReg, Offset);
667 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
668 getStreamer().emitPad(Offset);
670 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
672 getStreamer().emitRegSave(RegList, isVector);
674 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
675 const SmallVectorImpl<uint8_t> &Opcodes) {
676 getStreamer().emitUnwindRaw(Offset, Opcodes);
678 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
679 assert(!Vendor.empty() && "Vendor cannot be empty.");
681 if (CurrentVendor == Vendor)
684 if (!CurrentVendor.empty())
685 finishAttributeSection();
687 assert(Contents.empty() &&
688 ".ARM.attributes should be flushed before changing vendor");
689 CurrentVendor = Vendor;
692 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
693 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
695 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
697 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
699 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
701 StringRef StringValue) {
702 setAttributeItems(Attribute, IntValue, StringValue,
703 /* OverwriteExisting= */ true);
705 void ARMTargetELFStreamer::emitArch(unsigned Value) {
708 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
709 using namespace ARMBuildAttrs;
710 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
711 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
720 setAttributeItem(ARM_ISA_use, Allowed, false);
728 setAttributeItem(ARM_ISA_use, Allowed, false);
729 setAttributeItem(THUMB_ISA_use, Allowed, false);
733 setAttributeItem(ARM_ISA_use, Allowed, false);
734 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
739 setAttributeItem(ARM_ISA_use, Allowed, false);
740 setAttributeItem(THUMB_ISA_use, Allowed, false);
741 setAttributeItem(Virtualization_use, AllowTZ, false);
745 setAttributeItem(THUMB_ISA_use, Allowed, 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);
759 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
760 setAttributeItem(ARM_ISA_use, Allowed, false);
761 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
765 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
766 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
770 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
771 setAttributeItem(ARM_ISA_use, Allowed, false);
772 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
773 setAttributeItem(MPextension_use, Allowed, false);
774 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
778 setAttributeItem(ARM_ISA_use, Allowed, false);
779 setAttributeItem(THUMB_ISA_use, Allowed, false);
780 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
784 setAttributeItem(ARM_ISA_use, Allowed, false);
785 setAttributeItem(THUMB_ISA_use, Allowed, false);
786 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
790 report_fatal_error("Unknown Arch: " + Twine(Arch));
794 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
797 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
801 setAttributeItem(ARMBuildAttrs::FP_arch,
802 ARMBuildAttrs::AllowFPv2,
803 /* OverwriteExisting= */ false);
807 setAttributeItem(ARMBuildAttrs::FP_arch,
808 ARMBuildAttrs::AllowFPv3A,
809 /* OverwriteExisting= */ false);
813 setAttributeItem(ARMBuildAttrs::FP_arch,
814 ARMBuildAttrs::AllowFPv3B,
815 /* OverwriteExisting= */ false);
819 setAttributeItem(ARMBuildAttrs::FP_arch,
820 ARMBuildAttrs::AllowFPv4A,
821 /* OverwriteExisting= */ false);
825 setAttributeItem(ARMBuildAttrs::FP_arch,
826 ARMBuildAttrs::AllowFPv4B,
827 /* OverwriteExisting= */ false);
831 setAttributeItem(ARMBuildAttrs::FP_arch,
832 ARMBuildAttrs::AllowFPARMv8A,
833 /* OverwriteExisting= */ false);
837 setAttributeItem(ARMBuildAttrs::FP_arch,
838 ARMBuildAttrs::AllowFPv3A,
839 /* OverwriteExisting= */ false);
840 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
841 ARMBuildAttrs::AllowNeon,
842 /* OverwriteExisting= */ false);
845 case ARM::NEON_VFPV4:
846 setAttributeItem(ARMBuildAttrs::FP_arch,
847 ARMBuildAttrs::AllowFPv4A,
848 /* OverwriteExisting= */ false);
849 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
850 ARMBuildAttrs::AllowNeon2,
851 /* OverwriteExisting= */ false);
854 case ARM::NEON_FP_ARMV8:
855 case ARM::CRYPTO_NEON_FP_ARMV8:
856 setAttributeItem(ARMBuildAttrs::FP_arch,
857 ARMBuildAttrs::AllowFPARMv8A,
858 /* OverwriteExisting= */ false);
859 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
860 ARMBuildAttrs::AllowNeonARMv8,
861 /* OverwriteExisting= */ false);
868 report_fatal_error("Unknown FPU: " + Twine(FPU));
872 size_t ARMTargetELFStreamer::calculateContentSize() const {
874 for (size_t i = 0; i < Contents.size(); ++i) {
875 AttributeItem item = Contents[i];
877 case AttributeItem::HiddenAttribute:
879 case AttributeItem::NumericAttribute:
880 Result += getULEBSize(item.Tag);
881 Result += getULEBSize(item.IntValue);
883 case AttributeItem::TextAttribute:
884 Result += getULEBSize(item.Tag);
885 Result += item.StringValue.size() + 1; // string + '\0'
887 case AttributeItem::NumericAndTextAttributes:
888 Result += getULEBSize(item.Tag);
889 Result += getULEBSize(item.IntValue);
890 Result += item.StringValue.size() + 1; // string + '\0';
896 void ARMTargetELFStreamer::finishAttributeSection() {
898 // [ <section-length> "vendor-name"
899 // [ <file-tag> <size> <attribute>*
900 // | <section-tag> <size> <section-number>* 0 <attribute>*
901 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
905 if (FPU != ARM::INVALID_FPU)
906 emitFPUDefaultAttributes();
908 if (Arch != ARM::INVALID_ARCH)
909 emitArchDefaultAttributes();
911 if (Contents.empty())
914 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
916 ARMELFStreamer &Streamer = getStreamer();
918 // Switch to .ARM.attributes section
919 if (AttributeSection) {
920 Streamer.SwitchSection(AttributeSection);
923 Streamer.getContext().getELFSection(".ARM.attributes",
924 ELF::SHT_ARM_ATTRIBUTES,
926 SectionKind::getMetadata());
927 Streamer.SwitchSection(AttributeSection);
930 Streamer.EmitIntValue(0x41, 1);
933 // Vendor size + Vendor name + '\0'
934 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
937 const size_t TagHeaderSize = 1 + 4;
939 const size_t ContentsSize = calculateContentSize();
941 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
942 Streamer.EmitBytes(CurrentVendor);
943 Streamer.EmitIntValue(0, 1); // '\0'
945 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
946 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
948 // Size should have been accounted for already, now
949 // emit each field as its type (ULEB or String)
950 for (size_t i = 0; i < Contents.size(); ++i) {
951 AttributeItem item = Contents[i];
952 Streamer.EmitULEB128IntValue(item.Tag);
954 default: llvm_unreachable("Invalid attribute type");
955 case AttributeItem::NumericAttribute:
956 Streamer.EmitULEB128IntValue(item.IntValue);
958 case AttributeItem::TextAttribute:
959 Streamer.EmitBytes(item.StringValue.upper());
960 Streamer.EmitIntValue(0, 1); // '\0'
962 case AttributeItem::NumericAndTextAttributes:
963 Streamer.EmitULEB128IntValue(item.IntValue);
964 Streamer.EmitBytes(item.StringValue.upper());
965 Streamer.EmitIntValue(0, 1); // '\0'
971 FPU = ARM::INVALID_FPU;
973 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
974 getStreamer().emitInst(Inst, Suffix);
977 void ARMELFStreamer::FinishImpl() {
978 MCTargetStreamer &TS = *getTargetStreamer();
979 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
980 ATS.finishAttributeSection();
982 MCELFStreamer::FinishImpl();
985 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
989 const MCSymbol &Fn) {
990 const MCSectionELF &FnSection =
991 static_cast<const MCSectionELF &>(Fn.getSection());
993 // Create the name for new section
994 StringRef FnSecName(FnSection.getSectionName());
995 SmallString<128> EHSecName(Prefix);
996 if (FnSecName != ".text") {
997 EHSecName += FnSecName;
1000 // Get .ARM.extab or .ARM.exidx section
1001 const MCSectionELF *EHSection = NULL;
1002 if (const MCSymbol *Group = FnSection.getGroup()) {
1003 EHSection = getContext().getELFSection(
1004 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1005 FnSection.getEntrySize(), Group->getName());
1007 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1009 assert(EHSection && "Failed to get the required EH section");
1011 // Switch to .ARM.extab or .ARM.exidx section
1012 SwitchSection(EHSection);
1013 EmitCodeAlignment(4, 0);
1016 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1017 SwitchToEHSection(".ARM.extab",
1020 SectionKind::getDataRel(),
1024 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1025 SwitchToEHSection(".ARM.exidx",
1027 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1028 SectionKind::getDataRel(),
1032 void ARMELFStreamer::Reset() {
1036 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1045 UnwindOpAsm.Reset();
1048 void ARMELFStreamer::emitFnStart() {
1049 assert(FnStart == 0);
1050 FnStart = getContext().CreateTempSymbol();
1054 void ARMELFStreamer::emitFnEnd() {
1055 assert(FnStart && ".fnstart must preceeds .fnend");
1057 // Emit unwind opcodes if there is no .handlerdata directive
1058 if (!ExTab && !CantUnwind)
1059 FlushUnwindOpcodes(true);
1061 // Emit the exception index table entry
1062 SwitchToExIdxSection(*FnStart);
1064 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1065 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1067 const MCSymbolRefExpr *FnStartRef =
1068 MCSymbolRefExpr::Create(FnStart,
1069 MCSymbolRefExpr::VK_ARM_PREL31,
1072 EmitValue(FnStartRef, 4);
1075 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1077 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1078 const MCSymbolRefExpr *ExTabEntryRef =
1079 MCSymbolRefExpr::Create(ExTab,
1080 MCSymbolRefExpr::VK_ARM_PREL31,
1082 EmitValue(ExTabEntryRef, 4);
1084 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1085 // the second word of exception index table entry. The size of the unwind
1086 // opcodes should always be 4 bytes.
1087 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1088 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
1089 assert(Opcodes.size() == 4u &&
1090 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
1091 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
1095 // Switch to the section containing FnStart
1096 SwitchSection(&FnStart->getSection());
1098 // Clean exception handling frame information
1102 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1104 // Add the R_ARM_NONE fixup at the same position
1105 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1106 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1108 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1109 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1111 AddValueSymbols(PersonalityRef);
1112 MCDataFragment *DF = getOrCreateDataFragment();
1113 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1115 MCFixup::getKindForSize(4, false)));
1118 void ARMELFStreamer::FlushPendingOffset() {
1119 if (PendingOffset != 0) {
1120 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1125 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1126 // Emit the unwind opcode to restore $sp.
1128 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1129 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1130 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1131 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1133 FlushPendingOffset();
1136 // Finalize the unwind opcode sequence
1137 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1139 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1140 // section. Thus, we don't have to create an entry in the .ARM.extab
1142 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1145 // Switch to .ARM.extab section.
1146 SwitchToExTabSection(*FnStart);
1148 // Create .ARM.extab label for offset in .ARM.exidx
1150 ExTab = getContext().CreateTempSymbol();
1155 const MCSymbolRefExpr *PersonalityRef =
1156 MCSymbolRefExpr::Create(Personality,
1157 MCSymbolRefExpr::VK_ARM_PREL31,
1160 EmitValue(PersonalityRef, 4);
1163 // Emit unwind opcodes
1164 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1167 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1168 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1169 // after the unwind opcodes. The handler data consists of several 32-bit
1170 // words, and should be terminated by zero.
1172 // In case that the .handlerdata directive is not specified by the
1173 // programmer, we should emit zero to terminate the handler data.
1174 if (NoHandlerData && !Personality)
1178 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1180 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1182 UnwindOpAsm.setPersonality(Per);
1185 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1186 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1187 PersonalityIndex = Index;
1190 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1192 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1193 "the operand of .setfp directive should be either $sp or $fp");
1198 if (NewSPReg == ARM::SP)
1199 FPOffset = SPOffset + Offset;
1204 void ARMELFStreamer::emitPad(int64_t Offset) {
1205 // Track the change of the $sp offset
1208 // To squash multiple .pad directives, we should delay the unwind opcode
1209 // until the .save, .vsave, .handlerdata, or .fnend directives.
1210 PendingOffset -= Offset;
1213 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1215 // Collect the registers in the register list
1218 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1219 for (size_t i = 0; i < RegList.size(); ++i) {
1220 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1221 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1222 unsigned Bit = (1u << Reg);
1223 if ((Mask & Bit) == 0) {
1229 // Track the change the $sp offset: For the .save directive, the
1230 // corresponding push instruction will decrease the $sp by (4 * Count).
1231 // For the .vsave directive, the corresponding vpush instruction will
1232 // decrease $sp by (8 * Count).
1233 SPOffset -= Count * (IsVector ? 8 : 4);
1236 FlushPendingOffset();
1238 UnwindOpAsm.EmitVFPRegSave(Mask);
1240 UnwindOpAsm.EmitRegSave(Mask);
1243 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1244 const SmallVectorImpl<uint8_t> &Opcodes) {
1245 FlushPendingOffset();
1246 SPOffset = SPOffset - Offset;
1247 UnwindOpAsm.EmitRaw(Opcodes);
1252 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1253 bool isVerboseAsm, bool useLoc, bool useCFI,
1254 bool useDwarfDirectory,
1255 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1256 MCAsmBackend *TAB, bool ShowInst) {
1257 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1260 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1261 useDwarfDirectory, InstPrint, CE, TAB,
1265 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1266 raw_ostream &OS, MCCodeEmitter *Emitter,
1267 bool RelaxAll, bool NoExecStack,
1269 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1271 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1272 // FIXME: This should eventually end up somewhere else where more
1273 // intelligent flag decisions can be made. For now we are just maintaining
1274 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1275 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1278 S->getAssembler().setRelaxAll(true);
1280 S->getAssembler().setNoExecStack(true);