1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "ARMRegisterInfo.h"
17 #include "ARMUnwindOpAsm.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/BinaryFormat/ELF.h"
25 #include "llvm/MC/MCAsmBackend.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCAssembler.h"
28 #include "llvm/MC/MCCodeEmitter.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCELFStreamer.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCFixup.h"
33 #include "llvm/MC/MCFragment.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/MC/MCInstPrinter.h"
36 #include "llvm/MC/MCRegisterInfo.h"
37 #include "llvm/MC/MCSection.h"
38 #include "llvm/MC/MCSectionELF.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSubtargetInfo.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MCSymbolELF.h"
43 #include "llvm/MC/SectionKind.h"
44 #include "llvm/Support/ARMBuildAttributes.h"
45 #include "llvm/Support/ARMEHABI.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FormattedStream.h"
49 #include "llvm/Support/LEB128.h"
50 #include "llvm/Support/TargetParser.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <climits>
55 #include <cstddef>
56 #include <cstdint>
57 #include <string>
58 
59 using namespace llvm;
60 
61 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
62   assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
63          "Invalid personality index");
64   return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
65 }
66 
67 namespace {
68 
69 class ARMELFStreamer;
70 
71 class ARMTargetAsmStreamer : public ARMTargetStreamer {
72   formatted_raw_ostream &OS;
73   MCInstPrinter &InstPrinter;
74   bool IsVerboseAsm;
75 
76   void emitFnStart() override;
77   void emitFnEnd() override;
78   void emitCantUnwind() override;
79   void emitPersonality(const MCSymbol *Personality) override;
80   void emitPersonalityIndex(unsigned Index) override;
81   void emitHandlerData() override;
82   void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
83   void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
84   void emitPad(int64_t Offset) override;
85   void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
86                    bool isVector) override;
87   void emitUnwindRaw(int64_t Offset,
88                      const SmallVectorImpl<uint8_t> &Opcodes) override;
89 
90   void switchVendor(StringRef Vendor) override;
91   void emitAttribute(unsigned Attribute, unsigned Value) override;
92   void emitTextAttribute(unsigned Attribute, StringRef String) override;
93   void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
94                             StringRef StringValue) override;
95   void emitArch(ARM::ArchKind Arch) override;
96   void emitArchExtension(unsigned ArchExt) override;
97   void emitObjectArch(ARM::ArchKind Arch) override;
98   void emitFPU(unsigned FPU) override;
99   void emitInst(uint32_t Inst, char Suffix = '\0') override;
100   void finishAttributeSection() override;
101 
102   void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
103   void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
104 
105 public:
106   ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107                        MCInstPrinter &InstPrinter, bool VerboseAsm);
108 };
109 
110 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
111                                            formatted_raw_ostream &OS,
112                                            MCInstPrinter &InstPrinter,
113                                            bool VerboseAsm)
114     : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
115       IsVerboseAsm(VerboseAsm) {}
116 
117 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
118 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
119 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
120 
121 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122   OS << "\t.personality " << Personality->getName() << '\n';
123 }
124 
125 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126   OS << "\t.personalityindex " << Index << '\n';
127 }
128 
129 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
130 
131 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
132                                      int64_t Offset) {
133   OS << "\t.setfp\t";
134   InstPrinter.printRegName(OS, FpReg);
135   OS << ", ";
136   InstPrinter.printRegName(OS, SpReg);
137   if (Offset)
138     OS << ", #" << Offset;
139   OS << '\n';
140 }
141 
142 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
143   assert((Reg != ARM::SP && Reg != ARM::PC) &&
144          "the operand of .movsp cannot be either sp or pc");
145 
146   OS << "\t.movsp\t";
147   InstPrinter.printRegName(OS, Reg);
148   if (Offset)
149     OS << ", #" << Offset;
150   OS << '\n';
151 }
152 
153 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154   OS << "\t.pad\t#" << Offset << '\n';
155 }
156 
157 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
158                                        bool isVector) {
159   assert(RegList.size() && "RegList should not be empty");
160   if (isVector)
161     OS << "\t.vsave\t{";
162   else
163     OS << "\t.save\t{";
164 
165   InstPrinter.printRegName(OS, RegList[0]);
166 
167   for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
168     OS << ", ";
169     InstPrinter.printRegName(OS, RegList[i]);
170   }
171 
172   OS << "}\n";
173 }
174 
175 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
176 
177 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
178   OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
179   if (IsVerboseAsm) {
180     StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
181     if (!Name.empty())
182       OS << "\t@ " << Name;
183   }
184   OS << "\n";
185 }
186 
187 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
188                                              StringRef String) {
189   switch (Attribute) {
190   case ARMBuildAttrs::CPU_name:
191     OS << "\t.cpu\t" << String.lower();
192     break;
193   default:
194     OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
195     if (IsVerboseAsm) {
196       StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
197       if (!Name.empty())
198         OS << "\t@ " << Name;
199     }
200     break;
201   }
202   OS << "\n";
203 }
204 
205 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
206                                                 unsigned IntValue,
207                                                 StringRef StringValue) {
208   switch (Attribute) {
209   default: llvm_unreachable("unsupported multi-value attribute in asm mode");
210   case ARMBuildAttrs::compatibility:
211     OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
212     if (!StringValue.empty())
213       OS << ", \"" << StringValue << "\"";
214     if (IsVerboseAsm)
215       OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
216     break;
217   }
218   OS << "\n";
219 }
220 
221 void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
222   OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
223 }
224 
225 void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
226   OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
227 }
228 
229 void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
230   OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
231 }
232 
233 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
234   OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
235 }
236 
237 void ARMTargetAsmStreamer::finishAttributeSection() {}
238 
239 void
240 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241   OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
242 }
243 
244 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
245   const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
246 
247   OS << "\t.thumb_set\t";
248   Symbol->print(OS, MAI);
249   OS << ", ";
250   Value->print(OS, MAI);
251   OS << '\n';
252 }
253 
254 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
255   OS << "\t.inst";
256   if (Suffix)
257     OS << "." << Suffix;
258   OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
259 }
260 
261 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
262                                       const SmallVectorImpl<uint8_t> &Opcodes) {
263   OS << "\t.unwind_raw " << Offset;
264   for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
265                                                 OCE = Opcodes.end();
266        OCI != OCE; ++OCI)
267     OS << ", 0x" << Twine::utohexstr(*OCI);
268   OS << '\n';
269 }
270 
271 class ARMTargetELFStreamer : public ARMTargetStreamer {
272 private:
273   // This structure holds all attributes, accounting for
274   // their string/numeric value, so we can later emit them
275   // in declaration order, keeping all in the same vector
276   struct AttributeItem {
277     enum {
278       HiddenAttribute = 0,
279       NumericAttribute,
280       TextAttribute,
281       NumericAndTextAttributes
282     } Type;
283     unsigned Tag;
284     unsigned IntValue;
285     std::string StringValue;
286 
287     static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
288       // The conformance tag must be emitted first when serialised
289       // into an object file. Specifically, the addenda to the ARM ABI
290       // states that (2.3.7.4):
291       //
292       // "To simplify recognition by consumers in the common case of
293       // claiming conformity for the whole file, this tag should be
294       // emitted first in a file-scope sub-subsection of the first
295       // public subsection of the attributes section."
296       //
297       // So it is special-cased in this comparison predicate when the
298       // attributes are sorted in finishAttributeSection().
299       return (RHS.Tag != ARMBuildAttrs::conformance) &&
300              ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
301     }
302   };
303 
304   StringRef CurrentVendor;
305   unsigned FPU = ARM::FK_INVALID;
306   ARM::ArchKind Arch = ARM::ArchKind::INVALID;
307   ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
308   SmallVector<AttributeItem, 64> Contents;
309 
310   MCSection *AttributeSection = nullptr;
311 
312   AttributeItem *getAttributeItem(unsigned Attribute) {
313     for (size_t i = 0; i < Contents.size(); ++i)
314       if (Contents[i].Tag == Attribute)
315         return &Contents[i];
316     return nullptr;
317   }
318 
319   void setAttributeItem(unsigned Attribute, unsigned Value,
320                         bool OverwriteExisting) {
321     // Look for existing attribute item
322     if (AttributeItem *Item = getAttributeItem(Attribute)) {
323       if (!OverwriteExisting)
324         return;
325       Item->Type = AttributeItem::NumericAttribute;
326       Item->IntValue = Value;
327       return;
328     }
329 
330     // Create new attribute item
331     AttributeItem Item = {
332       AttributeItem::NumericAttribute,
333       Attribute,
334       Value,
335       StringRef("")
336     };
337     Contents.push_back(Item);
338   }
339 
340   void setAttributeItem(unsigned Attribute, StringRef Value,
341                         bool OverwriteExisting) {
342     // Look for existing attribute item
343     if (AttributeItem *Item = getAttributeItem(Attribute)) {
344       if (!OverwriteExisting)
345         return;
346       Item->Type = AttributeItem::TextAttribute;
347       Item->StringValue = Value;
348       return;
349     }
350 
351     // Create new attribute item
352     AttributeItem Item = {
353       AttributeItem::TextAttribute,
354       Attribute,
355       0,
356       Value
357     };
358     Contents.push_back(Item);
359   }
360 
361   void setAttributeItems(unsigned Attribute, unsigned IntValue,
362                          StringRef StringValue, bool OverwriteExisting) {
363     // Look for existing attribute item
364     if (AttributeItem *Item = getAttributeItem(Attribute)) {
365       if (!OverwriteExisting)
366         return;
367       Item->Type = AttributeItem::NumericAndTextAttributes;
368       Item->IntValue = IntValue;
369       Item->StringValue = StringValue;
370       return;
371     }
372 
373     // Create new attribute item
374     AttributeItem Item = {
375       AttributeItem::NumericAndTextAttributes,
376       Attribute,
377       IntValue,
378       StringValue
379     };
380     Contents.push_back(Item);
381   }
382 
383   void emitArchDefaultAttributes();
384   void emitFPUDefaultAttributes();
385 
386   ARMELFStreamer &getStreamer();
387 
388   void emitFnStart() override;
389   void emitFnEnd() override;
390   void emitCantUnwind() override;
391   void emitPersonality(const MCSymbol *Personality) override;
392   void emitPersonalityIndex(unsigned Index) override;
393   void emitHandlerData() override;
394   void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
395   void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
396   void emitPad(int64_t Offset) override;
397   void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
398                    bool isVector) override;
399   void emitUnwindRaw(int64_t Offset,
400                      const SmallVectorImpl<uint8_t> &Opcodes) override;
401 
402   void switchVendor(StringRef Vendor) override;
403   void emitAttribute(unsigned Attribute, unsigned Value) override;
404   void emitTextAttribute(unsigned Attribute, StringRef String) override;
405   void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
406                             StringRef StringValue) override;
407   void emitArch(ARM::ArchKind Arch) override;
408   void emitObjectArch(ARM::ArchKind Arch) override;
409   void emitFPU(unsigned FPU) override;
410   void emitInst(uint32_t Inst, char Suffix = '\0') override;
411   void finishAttributeSection() override;
412   void emitLabel(MCSymbol *Symbol) override;
413 
414   void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
415   void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
416 
417   size_t calculateContentSize() const;
418 
419   // Reset state between object emissions
420   void reset() override;
421 
422 public:
423   ARMTargetELFStreamer(MCStreamer &S)
424     : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
425 };
426 
427 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
428 /// the appropriate points in the object files. These symbols are defined in the
429 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
430 ///
431 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
432 /// region of ARM code, Thumb code or data in a section. In practice, this
433 /// emission does not rely on explicit assembler directives but on inherent
434 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
435 /// r0, r0, r0" an instruction).
436 ///
437 /// As a result this system is orthogonal to the DataRegion infrastructure used
438 /// by MachO. Beware!
439 class ARMELFStreamer : public MCELFStreamer {
440 public:
441   friend class ARMTargetELFStreamer;
442 
443   ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
444                  raw_pwrite_stream &OS, std::unique_ptr<MCCodeEmitter> Emitter,
445                  bool IsThumb)
446       : MCELFStreamer(Context, std::move(TAB), OS, std::move(Emitter)),
447         IsThumb(IsThumb) {
448     EHReset();
449   }
450 
451   ~ARMELFStreamer() override = default;
452 
453   void FinishImpl() override;
454 
455   // ARM exception handling directives
456   void emitFnStart();
457   void emitFnEnd();
458   void emitCantUnwind();
459   void emitPersonality(const MCSymbol *Per);
460   void emitPersonalityIndex(unsigned index);
461   void emitHandlerData();
462   void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
463   void emitMovSP(unsigned Reg, int64_t Offset = 0);
464   void emitPad(int64_t Offset);
465   void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
466   void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
467 
468   void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
469     LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
470     MCELFStreamer::ChangeSection(Section, Subsection);
471     auto LastMappingSymbol = LastMappingSymbols.find(Section);
472     if (LastMappingSymbol != LastMappingSymbols.end()) {
473       LastEMSInfo = std::move(LastMappingSymbol->second);
474       return;
475     }
476     LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
477   }
478 
479   /// This function is the one used to emit instruction data into the ELF
480   /// streamer. We override it to add the appropriate mapping symbol if
481   /// necessary.
482   void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
483                        bool) override {
484     if (IsThumb)
485       EmitThumbMappingSymbol();
486     else
487       EmitARMMappingSymbol();
488 
489     MCELFStreamer::EmitInstruction(Inst, STI);
490   }
491 
492   void emitInst(uint32_t Inst, char Suffix) {
493     unsigned Size;
494     char Buffer[4];
495     const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
496 
497     switch (Suffix) {
498     case '\0':
499       Size = 4;
500 
501       assert(!IsThumb);
502       EmitARMMappingSymbol();
503       for (unsigned II = 0, IE = Size; II != IE; II++) {
504         const unsigned I = LittleEndian ? (Size - II - 1) : II;
505         Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
506       }
507 
508       break;
509     case 'n':
510     case 'w':
511       Size = (Suffix == 'n' ? 2 : 4);
512 
513       assert(IsThumb);
514       EmitThumbMappingSymbol();
515       // Thumb wide instructions are emitted as a pair of 16-bit words of the
516       // appropriate endianness.
517       for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
518         const unsigned I0 = LittleEndian ? II + 0 : II + 1;
519         const unsigned I1 = LittleEndian ? II + 1 : II + 0;
520         Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
521         Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
522       }
523 
524       break;
525     default:
526       llvm_unreachable("Invalid Suffix");
527     }
528 
529     MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
530   }
531 
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
534   /// necessary.
535   void EmitBytes(StringRef Data) override {
536     EmitDataMappingSymbol();
537     MCELFStreamer::EmitBytes(Data);
538   }
539 
540   void FlushPendingMappingSymbol() {
541     if (!LastEMSInfo->hasInfo())
542       return;
543     ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
544     EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
545     EMS->resetInfo();
546   }
547 
548   /// This is one of the functions used to emit data into an ELF section, so the
549   /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
550   /// necessary.
551   void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
552     if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
553       if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
554         getContext().reportError(Loc, "relocated expression must be 32-bit");
555         return;
556       }
557       getOrCreateDataFragment();
558     }
559 
560     EmitDataMappingSymbol();
561     MCELFStreamer::EmitValueImpl(Value, Size, Loc);
562   }
563 
564   void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
565     MCELFStreamer::EmitAssemblerFlag(Flag);
566 
567     switch (Flag) {
568     case MCAF_SyntaxUnified:
569       return; // no-op here.
570     case MCAF_Code16:
571       IsThumb = true;
572       return; // Change to Thumb mode
573     case MCAF_Code32:
574       IsThumb = false;
575       return; // Change to ARM mode
576     case MCAF_Code64:
577       return;
578     case MCAF_SubsectionsViaSymbols:
579       return;
580     }
581   }
582 
583 private:
584   enum ElfMappingSymbol {
585     EMS_None,
586     EMS_ARM,
587     EMS_Thumb,
588     EMS_Data
589   };
590 
591   struct ElfMappingSymbolInfo {
592     explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
593         : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
594     void resetInfo() {
595       F = nullptr;
596       Offset = 0;
597     }
598     bool hasInfo() { return F != nullptr; }
599     SMLoc Loc;
600     MCFragment *F;
601     uint64_t Offset;
602     ElfMappingSymbol State;
603   };
604 
605   void EmitDataMappingSymbol() {
606     if (LastEMSInfo->State == EMS_Data)
607       return;
608     else if (LastEMSInfo->State == EMS_None) {
609       // This is a tentative symbol, it won't really be emitted until it's
610       // actually needed.
611       ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
612       auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
613       if (!DF)
614         return;
615       EMS->Loc = SMLoc();
616       EMS->F = getCurrentFragment();
617       EMS->Offset = DF->getContents().size();
618       LastEMSInfo->State = EMS_Data;
619       return;
620     }
621     EmitMappingSymbol("$d");
622     LastEMSInfo->State = EMS_Data;
623   }
624 
625   void EmitThumbMappingSymbol() {
626     if (LastEMSInfo->State == EMS_Thumb)
627       return;
628     FlushPendingMappingSymbol();
629     EmitMappingSymbol("$t");
630     LastEMSInfo->State = EMS_Thumb;
631   }
632 
633   void EmitARMMappingSymbol() {
634     if (LastEMSInfo->State == EMS_ARM)
635       return;
636     FlushPendingMappingSymbol();
637     EmitMappingSymbol("$a");
638     LastEMSInfo->State = EMS_ARM;
639   }
640 
641   void EmitMappingSymbol(StringRef Name) {
642     auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
643         Name + "." + Twine(MappingSymbolCounter++)));
644     EmitLabel(Symbol);
645 
646     Symbol->setType(ELF::STT_NOTYPE);
647     Symbol->setBinding(ELF::STB_LOCAL);
648     Symbol->setExternal(false);
649   }
650 
651   void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
652                          uint64_t Offset) {
653     auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
654         Name + "." + Twine(MappingSymbolCounter++)));
655     EmitLabel(Symbol, Loc, F);
656     Symbol->setType(ELF::STT_NOTYPE);
657     Symbol->setBinding(ELF::STB_LOCAL);
658     Symbol->setExternal(false);
659     Symbol->setOffset(Offset);
660   }
661 
662   void EmitThumbFunc(MCSymbol *Func) override {
663     getAssembler().setIsThumbFunc(Func);
664     EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
665   }
666 
667   // Helper functions for ARM exception handling directives
668   void EHReset();
669 
670   // Reset state between object emissions
671   void reset() override;
672 
673   void EmitPersonalityFixup(StringRef Name);
674   void FlushPendingOffset();
675   void FlushUnwindOpcodes(bool NoHandlerData);
676 
677   void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
678                          SectionKind Kind, const MCSymbol &Fn);
679   void SwitchToExTabSection(const MCSymbol &FnStart);
680   void SwitchToExIdxSection(const MCSymbol &FnStart);
681 
682   void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
683 
684   bool IsThumb;
685   int64_t MappingSymbolCounter = 0;
686 
687   DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
688       LastMappingSymbols;
689 
690   std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
691 
692   // ARM Exception Handling Frame Information
693   MCSymbol *ExTab;
694   MCSymbol *FnStart;
695   const MCSymbol *Personality;
696   unsigned PersonalityIndex;
697   unsigned FPReg; // Frame pointer register
698   int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
699   int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
700   int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
701   bool UsedFP;
702   bool CantUnwind;
703   SmallVector<uint8_t, 64> Opcodes;
704   UnwindOpcodeAssembler UnwindOpAsm;
705 };
706 
707 } // end anonymous namespace
708 
709 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
710   return static_cast<ARMELFStreamer &>(Streamer);
711 }
712 
713 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
714 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
715 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
716 
717 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
718   getStreamer().emitPersonality(Personality);
719 }
720 
721 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
722   getStreamer().emitPersonalityIndex(Index);
723 }
724 
725 void ARMTargetELFStreamer::emitHandlerData() {
726   getStreamer().emitHandlerData();
727 }
728 
729 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
730                                      int64_t Offset) {
731   getStreamer().emitSetFP(FpReg, SpReg, Offset);
732 }
733 
734 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
735   getStreamer().emitMovSP(Reg, Offset);
736 }
737 
738 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
739   getStreamer().emitPad(Offset);
740 }
741 
742 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
743                                        bool isVector) {
744   getStreamer().emitRegSave(RegList, isVector);
745 }
746 
747 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
748                                       const SmallVectorImpl<uint8_t> &Opcodes) {
749   getStreamer().emitUnwindRaw(Offset, Opcodes);
750 }
751 
752 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
753   assert(!Vendor.empty() && "Vendor cannot be empty.");
754 
755   if (CurrentVendor == Vendor)
756     return;
757 
758   if (!CurrentVendor.empty())
759     finishAttributeSection();
760 
761   assert(Contents.empty() &&
762          ".ARM.attributes should be flushed before changing vendor");
763   CurrentVendor = Vendor;
764 
765 }
766 
767 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
768   setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
769 }
770 
771 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
772                                              StringRef Value) {
773   setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
774 }
775 
776 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
777                                                 unsigned IntValue,
778                                                 StringRef StringValue) {
779   setAttributeItems(Attribute, IntValue, StringValue,
780                     /* OverwriteExisting= */ true);
781 }
782 
783 void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
784   Arch = Value;
785 }
786 
787 void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
788   EmittedArch = Value;
789 }
790 
791 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
792   using namespace ARMBuildAttrs;
793 
794   setAttributeItem(CPU_name,
795                    ARM::getCPUAttr(Arch),
796                    false);
797 
798   if (EmittedArch == ARM::ArchKind::INVALID)
799     setAttributeItem(CPU_arch,
800                      ARM::getArchAttr(Arch),
801                      false);
802   else
803     setAttributeItem(CPU_arch,
804                      ARM::getArchAttr(EmittedArch),
805                      false);
806 
807   switch (Arch) {
808   case ARM::ArchKind::ARMV2:
809   case ARM::ArchKind::ARMV2A:
810   case ARM::ArchKind::ARMV3:
811   case ARM::ArchKind::ARMV3M:
812   case ARM::ArchKind::ARMV4:
813     setAttributeItem(ARM_ISA_use, Allowed, false);
814     break;
815 
816   case ARM::ArchKind::ARMV4T:
817   case ARM::ArchKind::ARMV5T:
818   case ARM::ArchKind::ARMV5TE:
819   case ARM::ArchKind::ARMV6:
820     setAttributeItem(ARM_ISA_use, Allowed, false);
821     setAttributeItem(THUMB_ISA_use, Allowed, false);
822     break;
823 
824   case ARM::ArchKind::ARMV6T2:
825     setAttributeItem(ARM_ISA_use, Allowed, false);
826     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
827     break;
828 
829   case ARM::ArchKind::ARMV6K:
830   case ARM::ArchKind::ARMV6KZ:
831     setAttributeItem(ARM_ISA_use, Allowed, false);
832     setAttributeItem(THUMB_ISA_use, Allowed, false);
833     setAttributeItem(Virtualization_use, AllowTZ, false);
834     break;
835 
836   case ARM::ArchKind::ARMV6M:
837     setAttributeItem(THUMB_ISA_use, Allowed, false);
838     break;
839 
840   case ARM::ArchKind::ARMV7A:
841     setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
842     setAttributeItem(ARM_ISA_use, Allowed, false);
843     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
844     break;
845 
846   case ARM::ArchKind::ARMV7R:
847     setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
848     setAttributeItem(ARM_ISA_use, Allowed, false);
849     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
850     break;
851 
852   case ARM::ArchKind::ARMV7EM:
853   case ARM::ArchKind::ARMV7M:
854     setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
855     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
856     break;
857 
858   case ARM::ArchKind::ARMV8A:
859   case ARM::ArchKind::ARMV8_1A:
860   case ARM::ArchKind::ARMV8_2A:
861     setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
862     setAttributeItem(ARM_ISA_use, Allowed, false);
863     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
864     setAttributeItem(MPextension_use, Allowed, false);
865     setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
866     break;
867 
868   case ARM::ArchKind::ARMV8MBaseline:
869   case ARM::ArchKind::ARMV8MMainline:
870     setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
871     setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
872     break;
873 
874   case ARM::ArchKind::IWMMXT:
875     setAttributeItem(ARM_ISA_use, Allowed, false);
876     setAttributeItem(THUMB_ISA_use, Allowed, false);
877     setAttributeItem(WMMX_arch, AllowWMMXv1, false);
878     break;
879 
880   case ARM::ArchKind::IWMMXT2:
881     setAttributeItem(ARM_ISA_use, Allowed, false);
882     setAttributeItem(THUMB_ISA_use, Allowed, false);
883     setAttributeItem(WMMX_arch, AllowWMMXv2, false);
884     break;
885 
886   default:
887     report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
888     break;
889   }
890 }
891 
892 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
893   FPU = Value;
894 }
895 
896 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
897   switch (FPU) {
898   case ARM::FK_VFP:
899   case ARM::FK_VFPV2:
900     setAttributeItem(ARMBuildAttrs::FP_arch,
901                      ARMBuildAttrs::AllowFPv2,
902                      /* OverwriteExisting= */ false);
903     break;
904 
905   case ARM::FK_VFPV3:
906     setAttributeItem(ARMBuildAttrs::FP_arch,
907                      ARMBuildAttrs::AllowFPv3A,
908                      /* OverwriteExisting= */ false);
909     break;
910 
911   case ARM::FK_VFPV3_FP16:
912     setAttributeItem(ARMBuildAttrs::FP_arch,
913                      ARMBuildAttrs::AllowFPv3A,
914                      /* OverwriteExisting= */ false);
915     setAttributeItem(ARMBuildAttrs::FP_HP_extension,
916                      ARMBuildAttrs::AllowHPFP,
917                      /* OverwriteExisting= */ false);
918     break;
919 
920   case ARM::FK_VFPV3_D16:
921     setAttributeItem(ARMBuildAttrs::FP_arch,
922                      ARMBuildAttrs::AllowFPv3B,
923                      /* OverwriteExisting= */ false);
924     break;
925 
926   case ARM::FK_VFPV3_D16_FP16:
927     setAttributeItem(ARMBuildAttrs::FP_arch,
928                      ARMBuildAttrs::AllowFPv3B,
929                      /* OverwriteExisting= */ false);
930     setAttributeItem(ARMBuildAttrs::FP_HP_extension,
931                      ARMBuildAttrs::AllowHPFP,
932                      /* OverwriteExisting= */ false);
933     break;
934 
935   case ARM::FK_VFPV3XD:
936     setAttributeItem(ARMBuildAttrs::FP_arch,
937                      ARMBuildAttrs::AllowFPv3B,
938                      /* OverwriteExisting= */ false);
939     break;
940   case ARM::FK_VFPV3XD_FP16:
941     setAttributeItem(ARMBuildAttrs::FP_arch,
942                      ARMBuildAttrs::AllowFPv3B,
943                      /* OverwriteExisting= */ false);
944     setAttributeItem(ARMBuildAttrs::FP_HP_extension,
945                      ARMBuildAttrs::AllowHPFP,
946                      /* OverwriteExisting= */ false);
947     break;
948 
949   case ARM::FK_VFPV4:
950     setAttributeItem(ARMBuildAttrs::FP_arch,
951                      ARMBuildAttrs::AllowFPv4A,
952                      /* OverwriteExisting= */ false);
953     break;
954 
955   // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
956   // as _D16 here.
957   case ARM::FK_FPV4_SP_D16:
958   case ARM::FK_VFPV4_D16:
959     setAttributeItem(ARMBuildAttrs::FP_arch,
960                      ARMBuildAttrs::AllowFPv4B,
961                      /* OverwriteExisting= */ false);
962     break;
963 
964   case ARM::FK_FP_ARMV8:
965     setAttributeItem(ARMBuildAttrs::FP_arch,
966                      ARMBuildAttrs::AllowFPARMv8A,
967                      /* OverwriteExisting= */ false);
968     break;
969 
970   // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
971   // uses the FP_ARMV8_D16 build attribute.
972   case ARM::FK_FPV5_SP_D16:
973   case ARM::FK_FPV5_D16:
974     setAttributeItem(ARMBuildAttrs::FP_arch,
975                      ARMBuildAttrs::AllowFPARMv8B,
976                      /* OverwriteExisting= */ false);
977     break;
978 
979   case ARM::FK_NEON:
980     setAttributeItem(ARMBuildAttrs::FP_arch,
981                      ARMBuildAttrs::AllowFPv3A,
982                      /* OverwriteExisting= */ false);
983     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
984                      ARMBuildAttrs::AllowNeon,
985                      /* OverwriteExisting= */ false);
986     break;
987 
988   case ARM::FK_NEON_FP16:
989     setAttributeItem(ARMBuildAttrs::FP_arch,
990                      ARMBuildAttrs::AllowFPv3A,
991                      /* OverwriteExisting= */ false);
992     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
993                      ARMBuildAttrs::AllowNeon,
994                      /* OverwriteExisting= */ false);
995     setAttributeItem(ARMBuildAttrs::FP_HP_extension,
996                      ARMBuildAttrs::AllowHPFP,
997                      /* OverwriteExisting= */ false);
998     break;
999 
1000   case ARM::FK_NEON_VFPV4:
1001     setAttributeItem(ARMBuildAttrs::FP_arch,
1002                      ARMBuildAttrs::AllowFPv4A,
1003                      /* OverwriteExisting= */ false);
1004     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1005                      ARMBuildAttrs::AllowNeon2,
1006                      /* OverwriteExisting= */ false);
1007     break;
1008 
1009   case ARM::FK_NEON_FP_ARMV8:
1010   case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1011     setAttributeItem(ARMBuildAttrs::FP_arch,
1012                      ARMBuildAttrs::AllowFPARMv8A,
1013                      /* OverwriteExisting= */ false);
1014     // 'Advanced_SIMD_arch' must be emitted not here, but within
1015     // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1016     break;
1017 
1018   case ARM::FK_SOFTVFP:
1019   case ARM::FK_NONE:
1020     break;
1021 
1022   default:
1023     report_fatal_error("Unknown FPU: " + Twine(FPU));
1024     break;
1025   }
1026 }
1027 
1028 size_t ARMTargetELFStreamer::calculateContentSize() const {
1029   size_t Result = 0;
1030   for (size_t i = 0; i < Contents.size(); ++i) {
1031     AttributeItem item = Contents[i];
1032     switch (item.Type) {
1033     case AttributeItem::HiddenAttribute:
1034       break;
1035     case AttributeItem::NumericAttribute:
1036       Result += getULEB128Size(item.Tag);
1037       Result += getULEB128Size(item.IntValue);
1038       break;
1039     case AttributeItem::TextAttribute:
1040       Result += getULEB128Size(item.Tag);
1041       Result += item.StringValue.size() + 1; // string + '\0'
1042       break;
1043     case AttributeItem::NumericAndTextAttributes:
1044       Result += getULEB128Size(item.Tag);
1045       Result += getULEB128Size(item.IntValue);
1046       Result += item.StringValue.size() + 1; // string + '\0';
1047       break;
1048     }
1049   }
1050   return Result;
1051 }
1052 
1053 void ARMTargetELFStreamer::finishAttributeSection() {
1054   // <format-version>
1055   // [ <section-length> "vendor-name"
1056   // [ <file-tag> <size> <attribute>*
1057   //   | <section-tag> <size> <section-number>* 0 <attribute>*
1058   //   | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1059   //   ]+
1060   // ]*
1061 
1062   if (FPU != ARM::FK_INVALID)
1063     emitFPUDefaultAttributes();
1064 
1065   if (Arch != ARM::ArchKind::INVALID)
1066     emitArchDefaultAttributes();
1067 
1068   if (Contents.empty())
1069     return;
1070 
1071   llvm::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
1072 
1073   ARMELFStreamer &Streamer = getStreamer();
1074 
1075   // Switch to .ARM.attributes section
1076   if (AttributeSection) {
1077     Streamer.SwitchSection(AttributeSection);
1078   } else {
1079     AttributeSection = Streamer.getContext().getELFSection(
1080         ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
1081     Streamer.SwitchSection(AttributeSection);
1082 
1083     // Format version
1084     Streamer.EmitIntValue(0x41, 1);
1085   }
1086 
1087   // Vendor size + Vendor name + '\0'
1088   const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1089 
1090   // Tag + Tag Size
1091   const size_t TagHeaderSize = 1 + 4;
1092 
1093   const size_t ContentsSize = calculateContentSize();
1094 
1095   Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1096   Streamer.EmitBytes(CurrentVendor);
1097   Streamer.EmitIntValue(0, 1); // '\0'
1098 
1099   Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1100   Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1101 
1102   // Size should have been accounted for already, now
1103   // emit each field as its type (ULEB or String)
1104   for (size_t i = 0; i < Contents.size(); ++i) {
1105     AttributeItem item = Contents[i];
1106     Streamer.EmitULEB128IntValue(item.Tag);
1107     switch (item.Type) {
1108     default: llvm_unreachable("Invalid attribute type");
1109     case AttributeItem::NumericAttribute:
1110       Streamer.EmitULEB128IntValue(item.IntValue);
1111       break;
1112     case AttributeItem::TextAttribute:
1113       Streamer.EmitBytes(item.StringValue);
1114       Streamer.EmitIntValue(0, 1); // '\0'
1115       break;
1116     case AttributeItem::NumericAndTextAttributes:
1117       Streamer.EmitULEB128IntValue(item.IntValue);
1118       Streamer.EmitBytes(item.StringValue);
1119       Streamer.EmitIntValue(0, 1); // '\0'
1120       break;
1121     }
1122   }
1123 
1124   Contents.clear();
1125   FPU = ARM::FK_INVALID;
1126 }
1127 
1128 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1129   ARMELFStreamer &Streamer = getStreamer();
1130   if (!Streamer.IsThumb)
1131     return;
1132 
1133   Streamer.getAssembler().registerSymbol(*Symbol);
1134   unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1135   if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
1136     Streamer.EmitThumbFunc(Symbol);
1137 }
1138 
1139 void
1140 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1141   getStreamer().EmitFixup(S, FK_Data_4);
1142 }
1143 
1144 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1145   if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1146     const MCSymbol &Sym = SRE->getSymbol();
1147     if (!Sym.isDefined()) {
1148       getStreamer().EmitAssignment(Symbol, Value);
1149       return;
1150     }
1151   }
1152 
1153   getStreamer().EmitThumbFunc(Symbol);
1154   getStreamer().EmitAssignment(Symbol, Value);
1155 }
1156 
1157 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1158   getStreamer().emitInst(Inst, Suffix);
1159 }
1160 
1161 void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1162 
1163 void ARMELFStreamer::FinishImpl() {
1164   MCTargetStreamer &TS = *getTargetStreamer();
1165   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1166   ATS.finishAttributeSection();
1167 
1168   MCELFStreamer::FinishImpl();
1169 }
1170 
1171 void ARMELFStreamer::reset() {
1172   MCTargetStreamer &TS = *getTargetStreamer();
1173   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1174   ATS.reset();
1175   MappingSymbolCounter = 0;
1176   MCELFStreamer::reset();
1177   LastMappingSymbols.clear();
1178   LastEMSInfo.reset();
1179   // MCELFStreamer clear's the assembler's e_flags. However, for
1180   // arm we manually set the ABI version on streamer creation, so
1181   // do the same here
1182   getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1183 }
1184 
1185 inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1186                                               unsigned Type,
1187                                               unsigned Flags,
1188                                               SectionKind Kind,
1189                                               const MCSymbol &Fn) {
1190   const MCSectionELF &FnSection =
1191     static_cast<const MCSectionELF &>(Fn.getSection());
1192 
1193   // Create the name for new section
1194   StringRef FnSecName(FnSection.getSectionName());
1195   SmallString<128> EHSecName(Prefix);
1196   if (FnSecName != ".text") {
1197     EHSecName += FnSecName;
1198   }
1199 
1200   // Get .ARM.extab or .ARM.exidx section
1201   const MCSymbolELF *Group = FnSection.getGroup();
1202   if (Group)
1203     Flags |= ELF::SHF_GROUP;
1204   MCSectionELF *EHSection = getContext().getELFSection(
1205       EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1206       static_cast<const MCSymbolELF *>(&Fn));
1207 
1208   assert(EHSection && "Failed to get the required EH section");
1209 
1210   // Switch to .ARM.extab or .ARM.exidx section
1211   SwitchSection(EHSection);
1212   EmitCodeAlignment(4);
1213 }
1214 
1215 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1216   SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1217                     SectionKind::getData(), FnStart);
1218 }
1219 
1220 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1221   SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1222                     ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1223                     SectionKind::getData(), FnStart);
1224 }
1225 
1226 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1227   MCDataFragment *Frag = getOrCreateDataFragment();
1228   Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1229                                               Kind));
1230 }
1231 
1232 void ARMELFStreamer::EHReset() {
1233   ExTab = nullptr;
1234   FnStart = nullptr;
1235   Personality = nullptr;
1236   PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1237   FPReg = ARM::SP;
1238   FPOffset = 0;
1239   SPOffset = 0;
1240   PendingOffset = 0;
1241   UsedFP = false;
1242   CantUnwind = false;
1243 
1244   Opcodes.clear();
1245   UnwindOpAsm.Reset();
1246 }
1247 
1248 void ARMELFStreamer::emitFnStart() {
1249   assert(FnStart == nullptr);
1250   FnStart = getContext().createTempSymbol();
1251   EmitLabel(FnStart);
1252 }
1253 
1254 void ARMELFStreamer::emitFnEnd() {
1255   assert(FnStart && ".fnstart must precedes .fnend");
1256 
1257   // Emit unwind opcodes if there is no .handlerdata directive
1258   if (!ExTab && !CantUnwind)
1259     FlushUnwindOpcodes(true);
1260 
1261   // Emit the exception index table entry
1262   SwitchToExIdxSection(*FnStart);
1263 
1264   if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1265     EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1266 
1267   const MCSymbolRefExpr *FnStartRef =
1268     MCSymbolRefExpr::create(FnStart,
1269                             MCSymbolRefExpr::VK_ARM_PREL31,
1270                             getContext());
1271 
1272   EmitValue(FnStartRef, 4);
1273 
1274   if (CantUnwind) {
1275     EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1276   } else if (ExTab) {
1277     // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1278     const MCSymbolRefExpr *ExTabEntryRef =
1279       MCSymbolRefExpr::create(ExTab,
1280                               MCSymbolRefExpr::VK_ARM_PREL31,
1281                               getContext());
1282     EmitValue(ExTabEntryRef, 4);
1283   } else {
1284     // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1285     // the second word of exception index table entry.  The size of the unwind
1286     // opcodes should always be 4 bytes.
1287     assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1288            "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1289     assert(Opcodes.size() == 4u &&
1290            "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1291     uint64_t Intval = Opcodes[0] |
1292                       Opcodes[1] << 8 |
1293                       Opcodes[2] << 16 |
1294                       Opcodes[3] << 24;
1295     EmitIntValue(Intval, Opcodes.size());
1296   }
1297 
1298   // Switch to the section containing FnStart
1299   SwitchSection(&FnStart->getSection());
1300 
1301   // Clean exception handling frame information
1302   EHReset();
1303 }
1304 
1305 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1306 
1307 // Add the R_ARM_NONE fixup at the same position
1308 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1309   const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1310 
1311   const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1312       PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1313 
1314   visitUsedExpr(*PersonalityRef);
1315   MCDataFragment *DF = getOrCreateDataFragment();
1316   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1317                                             PersonalityRef,
1318                                             MCFixup::getKindForSize(4, false)));
1319 }
1320 
1321 void ARMELFStreamer::FlushPendingOffset() {
1322   if (PendingOffset != 0) {
1323     UnwindOpAsm.EmitSPOffset(-PendingOffset);
1324     PendingOffset = 0;
1325   }
1326 }
1327 
1328 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1329   // Emit the unwind opcode to restore $sp.
1330   if (UsedFP) {
1331     const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1332     int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1333     UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1334     UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1335   } else {
1336     FlushPendingOffset();
1337   }
1338 
1339   // Finalize the unwind opcode sequence
1340   UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1341 
1342   // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1343   // section.  Thus, we don't have to create an entry in the .ARM.extab
1344   // section.
1345   if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1346     return;
1347 
1348   // Switch to .ARM.extab section.
1349   SwitchToExTabSection(*FnStart);
1350 
1351   // Create .ARM.extab label for offset in .ARM.exidx
1352   assert(!ExTab);
1353   ExTab = getContext().createTempSymbol();
1354   EmitLabel(ExTab);
1355 
1356   // Emit personality
1357   if (Personality) {
1358     const MCSymbolRefExpr *PersonalityRef =
1359       MCSymbolRefExpr::create(Personality,
1360                               MCSymbolRefExpr::VK_ARM_PREL31,
1361                               getContext());
1362 
1363     EmitValue(PersonalityRef, 4);
1364   }
1365 
1366   // Emit unwind opcodes
1367   assert((Opcodes.size() % 4) == 0 &&
1368          "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1369   for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1370     uint64_t Intval = Opcodes[I] |
1371                       Opcodes[I + 1] << 8 |
1372                       Opcodes[I + 2] << 16 |
1373                       Opcodes[I + 3] << 24;
1374     EmitIntValue(Intval, 4);
1375   }
1376 
1377   // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1378   // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1379   // after the unwind opcodes.  The handler data consists of several 32-bit
1380   // words, and should be terminated by zero.
1381   //
1382   // In case that the .handlerdata directive is not specified by the
1383   // programmer, we should emit zero to terminate the handler data.
1384   if (NoHandlerData && !Personality)
1385     EmitIntValue(0, 4);
1386 }
1387 
1388 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1389 
1390 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1391   Personality = Per;
1392   UnwindOpAsm.setPersonality(Per);
1393 }
1394 
1395 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1396   assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1397   PersonalityIndex = Index;
1398 }
1399 
1400 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1401                                int64_t Offset) {
1402   assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1403          "the operand of .setfp directive should be either $sp or $fp");
1404 
1405   UsedFP = true;
1406   FPReg = NewFPReg;
1407 
1408   if (NewSPReg == ARM::SP)
1409     FPOffset = SPOffset + Offset;
1410   else
1411     FPOffset += Offset;
1412 }
1413 
1414 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1415   assert((Reg != ARM::SP && Reg != ARM::PC) &&
1416          "the operand of .movsp cannot be either sp or pc");
1417   assert(FPReg == ARM::SP && "current FP must be SP");
1418 
1419   FlushPendingOffset();
1420 
1421   FPReg = Reg;
1422   FPOffset = SPOffset + Offset;
1423 
1424   const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1425   UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1426 }
1427 
1428 void ARMELFStreamer::emitPad(int64_t Offset) {
1429   // Track the change of the $sp offset
1430   SPOffset -= Offset;
1431 
1432   // To squash multiple .pad directives, we should delay the unwind opcode
1433   // until the .save, .vsave, .handlerdata, or .fnend directives.
1434   PendingOffset -= Offset;
1435 }
1436 
1437 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1438                                  bool IsVector) {
1439   // Collect the registers in the register list
1440   unsigned Count = 0;
1441   uint32_t Mask = 0;
1442   const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1443   for (size_t i = 0; i < RegList.size(); ++i) {
1444     unsigned Reg = MRI->getEncodingValue(RegList[i]);
1445     assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1446     unsigned Bit = (1u << Reg);
1447     if ((Mask & Bit) == 0) {
1448       Mask |= Bit;
1449       ++Count;
1450     }
1451   }
1452 
1453   // Track the change the $sp offset: For the .save directive, the
1454   // corresponding push instruction will decrease the $sp by (4 * Count).
1455   // For the .vsave directive, the corresponding vpush instruction will
1456   // decrease $sp by (8 * Count).
1457   SPOffset -= Count * (IsVector ? 8 : 4);
1458 
1459   // Emit the opcode
1460   FlushPendingOffset();
1461   if (IsVector)
1462     UnwindOpAsm.EmitVFPRegSave(Mask);
1463   else
1464     UnwindOpAsm.EmitRegSave(Mask);
1465 }
1466 
1467 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1468                                    const SmallVectorImpl<uint8_t> &Opcodes) {
1469   FlushPendingOffset();
1470   SPOffset = SPOffset - Offset;
1471   UnwindOpAsm.EmitRaw(Opcodes);
1472 }
1473 
1474 namespace llvm {
1475 
1476 MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1477                                              formatted_raw_ostream &OS,
1478                                              MCInstPrinter *InstPrint,
1479                                              bool isVerboseAsm) {
1480   return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1481 }
1482 
1483 MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1484   return new ARMTargetStreamer(S);
1485 }
1486 
1487 MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1488                                                 const MCSubtargetInfo &STI) {
1489   const Triple &TT = STI.getTargetTriple();
1490   if (TT.isOSBinFormatELF())
1491     return new ARMTargetELFStreamer(S);
1492   return new ARMTargetStreamer(S);
1493 }
1494 
1495 MCELFStreamer *createARMELFStreamer(MCContext &Context,
1496                                     std::unique_ptr<MCAsmBackend> TAB,
1497                                     raw_pwrite_stream &OS,
1498                                     std::unique_ptr<MCCodeEmitter> Emitter,
1499                                     bool RelaxAll, bool IsThumb) {
1500   ARMELFStreamer *S = new ARMELFStreamer(Context, std::move(TAB), OS,
1501                                          std::move(Emitter), IsThumb);
1502   // FIXME: This should eventually end up somewhere else where more
1503   // intelligent flag decisions can be made. For now we are just maintaining
1504   // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1505   S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1506 
1507   if (RelaxAll)
1508     S->getAssembler().setRelaxAll(true);
1509   return S;
1510 }
1511 
1512 } // end namespace llvm
1513