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