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