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