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