LCOV - code coverage report
Current view: top level - lib/MC - MCELFStreamer.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 274 301 91.0 %
Date: 2017-09-14 15:23:50 Functions: 23 27 85.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
       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 ELF .o object files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/MC/MCELFStreamer.h"
      15             : #include "llvm/ADT/SmallString.h"
      16             : #include "llvm/ADT/SmallVector.h"
      17             : #include "llvm/BinaryFormat/ELF.h"
      18             : #include "llvm/MC/MCAsmBackend.h"
      19             : #include "llvm/MC/MCAsmInfo.h"
      20             : #include "llvm/MC/MCAssembler.h"
      21             : #include "llvm/MC/MCCodeEmitter.h"
      22             : #include "llvm/MC/MCContext.h"
      23             : #include "llvm/MC/MCExpr.h"
      24             : #include "llvm/MC/MCFixup.h"
      25             : #include "llvm/MC/MCFragment.h"
      26             : #include "llvm/MC/MCObjectFileInfo.h"
      27             : #include "llvm/MC/MCObjectWriter.h"
      28             : #include "llvm/MC/MCSection.h"
      29             : #include "llvm/MC/MCSectionELF.h"
      30             : #include "llvm/MC/MCStreamer.h"
      31             : #include "llvm/MC/MCSymbol.h"
      32             : #include "llvm/MC/MCSymbolELF.h"
      33             : #include "llvm/Support/Casting.h"
      34             : #include "llvm/Support/ErrorHandling.h"
      35             : #include "llvm/Support/TargetRegistry.h"
      36             : #include "llvm/Support/raw_ostream.h"
      37             : #include <cassert>
      38             : #include <cstdint>
      39             : 
      40             : using namespace llvm;
      41             : 
      42     1161987 : bool MCELFStreamer::isBundleLocked() const {
      43     3485961 :   return getCurrentSectionOnly()->isBundleLocked();
      44             : }
      45             : 
      46         129 : void MCELFStreamer::mergeFragment(MCDataFragment *DF,
      47             :                                   MCDataFragment *EF) {
      48         129 :   MCAssembler &Assembler = getAssembler();
      49             : 
      50         258 :   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
      51         258 :     uint64_t FSize = EF->getContents().size();
      52             : 
      53         129 :     if (FSize > Assembler.getBundleAlignSize())
      54           1 :       report_fatal_error("Fragment can't be larger than a bundle size");
      55             : 
      56         256 :     uint64_t RequiredBundlePadding = computeBundlePadding(
      57         256 :         Assembler, EF, DF->getContents().size(), FSize);
      58             : 
      59         128 :     if (RequiredBundlePadding > UINT8_MAX)
      60           0 :       report_fatal_error("Padding cannot exceed 255 bytes");
      61             : 
      62         128 :     if (RequiredBundlePadding > 0) {
      63          46 :       SmallString<256> Code;
      64          46 :       raw_svector_ostream VecOS(Code);
      65          23 :       MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
      66             : 
      67          46 :       EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
      68             : 
      69          23 :       Assembler.writeFragmentPadding(*EF, FSize, OW);
      70          23 :       delete OW;
      71             : 
      72          92 :       DF->getContents().append(Code.begin(), Code.end());
      73             :     }
      74             :   }
      75             : 
      76         256 :   flushPendingLabels(DF, DF->getContents().size());
      77             : 
      78         308 :   for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
      79         260 :     EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
      80         104 :                                  DF->getContents().size());
      81         104 :     DF->getFixups().push_back(EF->getFixups()[i]);
      82             :   }
      83         128 :   DF->setHasInstructions(true);
      84         512 :   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
      85         128 : }
      86             : 
      87        3286 : void MCELFStreamer::InitSections(bool NoExecStack) {
      88        3286 :   MCContext &Ctx = getContext();
      89        3286 :   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
      90        3286 :   EmitCodeAlignment(4);
      91             : 
      92        3286 :   if (NoExecStack)
      93           1 :     SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
      94        3286 : }
      95             : 
      96     1251930 : void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
      97     1251930 :   auto *Symbol = cast<MCSymbolELF>(S);
      98     1251930 :   MCObjectStreamer::EmitLabel(Symbol, Loc);
      99             : 
     100             :   const MCSectionELF &Section =
     101     2503860 :       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
     102     1251930 :   if (Section.getFlags() & ELF::SHF_TLS)
     103         154 :     Symbol->setType(ELF::STT_TLS);
     104     1251930 : }
     105             : 
     106          10 : void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc, MCFragment *F) {
     107          10 :   auto *Symbol = cast<MCSymbolELF>(S);
     108          10 :   MCObjectStreamer::EmitLabel(Symbol, Loc, F);
     109             : 
     110             :   const MCSectionELF &Section =
     111          20 :       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
     112          10 :   if (Section.getFlags() & ELF::SHF_TLS)
     113           0 :     Symbol->setType(ELF::STT_TLS);
     114          10 : }
     115             : 
     116         591 : void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
     117             :   // Let the target do whatever target specific stuff it needs to do.
     118         591 :   getAssembler().getBackend().handleAssemblerFlag(Flag);
     119             :   // Do any generic stuff we need to do.
     120         591 :   switch (Flag) {
     121             :   case MCAF_SyntaxUnified: return; // no-op here.
     122             :   case MCAF_Code16: return; // Change parsing mode; no-op here.
     123             :   case MCAF_Code32: return; // Change parsing mode; no-op here.
     124             :   case MCAF_Code64: return; // Change parsing mode; no-op here.
     125           0 :   case MCAF_SubsectionsViaSymbols:
     126           0 :     getAssembler().setSubsectionsViaSymbols(true);
     127             :     return;
     128             :   }
     129             : 
     130           0 :   llvm_unreachable("invalid assembler flag!");
     131             : }
     132             : 
     133             : // If bundle alignment is used and there are any instructions in the section, it
     134             : // needs to be aligned to at least the bundle size.
     135             : static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
     136             :                                            MCSection *Section) {
     137      781747 :   if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
     138          18 :       Section->getAlignment() < Assembler.getBundleAlignSize())
     139          22 :     Section->setAlignment(Assembler.getBundleAlignSize());
     140             : }
     141             : 
     142      390885 : void MCELFStreamer::ChangeSection(MCSection *Section,
     143             :                                   const MCExpr *Subsection) {
     144      781771 :   MCSection *CurSection = getCurrentSectionOnly();
     145      390886 :   if (CurSection && isBundleLocked())
     146           1 :     report_fatal_error("Unterminated .bundle_lock when changing a section");
     147             : 
     148      390884 :   MCAssembler &Asm = getAssembler();
     149             :   // Ensure the previous section gets aligned if necessary.
     150      390884 :   setSectionAlignmentForBundling(Asm, CurSection);
     151      390884 :   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
     152      390884 :   const MCSymbol *Grp = SectionELF->getGroup();
     153      390884 :   if (Grp)
     154        8345 :     Asm.registerSymbol(*Grp);
     155             : 
     156      390884 :   changeSectionImpl(Section, Subsection);
     157      390884 :   Asm.registerSymbol(*Section->getBeginSymbol());
     158      390885 : }
     159             : 
     160          20 : void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
     161          20 :   getAssembler().registerSymbol(*Symbol);
     162          40 :   const MCExpr *Value = MCSymbolRefExpr::create(
     163          40 :       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
     164          20 :   Alias->setVariableValue(Value);
     165          20 : }
     166             : 
     167             : // When GNU as encounters more than one .type declaration for an object it seems
     168             : // to use a mechanism similar to the one below to decide which type is actually
     169             : // used in the object file.  The greater of T1 and T2 is selected based on the
     170             : // following ordering:
     171             : //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
     172             : // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
     173             : // provided type).
     174             : static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
     175       65767 :   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
     176       63462 :                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
     177       64614 :     if (T1 == Type)
     178             :       return T2;
     179        1209 :     if (T2 == Type)
     180             :       return T1;
     181             :   }
     182             : 
     183             :   return T2;
     184             : }
     185             : 
     186       77488 : bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
     187       77488 :   auto *Symbol = cast<MCSymbolELF>(S);
     188             :   // Indirect symbols are handled differently, to match how 'as' handles
     189             :   // them. This makes writing matching .o files easier.
     190       77488 :   if (Attribute == MCSA_IndirectSymbol) {
     191             :     // Note that we intentionally cannot use the symbol data here; this is
     192             :     // important for matching the string table that 'as' generates.
     193             :     IndirectSymbolData ISD;
     194           0 :     ISD.Symbol = Symbol;
     195           0 :     ISD.Section = getCurrentSectionOnly();
     196           0 :     getAssembler().getIndirectSymbols().push_back(ISD);
     197             :     return true;
     198             :   }
     199             : 
     200             :   // Adding a symbol attribute always introduces the symbol, note that an
     201             :   // important side effect of calling registerSymbol here is to register
     202             :   // the symbol with the assembler.
     203       77488 :   getAssembler().registerSymbol(*Symbol);
     204             : 
     205             :   // The implementation of symbol attributes is designed to match 'as', but it
     206             :   // leaves much to desired. It doesn't really make sense to arbitrarily add and
     207             :   // remove flags, but 'as' allows this (in particular, see .desc).
     208             :   //
     209             :   // In the future it might be worth trying to make these operations more well
     210             :   // defined.
     211       77488 :   switch (Attribute) {
     212             :   case MCSA_LazyReference:
     213             :   case MCSA_Reference:
     214             :   case MCSA_SymbolResolver:
     215             :   case MCSA_PrivateExtern:
     216             :   case MCSA_WeakDefinition:
     217             :   case MCSA_WeakDefAutoPrivate:
     218             :   case MCSA_Invalid:
     219             :   case MCSA_IndirectSymbol:
     220             :     return false;
     221             : 
     222             :   case MCSA_NoDeadStrip:
     223             :     // Ignore for now.
     224             :     break;
     225             : 
     226           2 :   case MCSA_ELF_TypeGnuUniqueObject:
     227           4 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     228           2 :     Symbol->setBinding(ELF::STB_GNU_UNIQUE);
     229           2 :     Symbol->setExternal(true);
     230             :     break;
     231             : 
     232        6233 :   case MCSA_Global:
     233        6233 :     Symbol->setBinding(ELF::STB_GLOBAL);
     234        6232 :     Symbol->setExternal(true);
     235             :     break;
     236             : 
     237        4569 :   case MCSA_WeakReference:
     238             :   case MCSA_Weak:
     239        4569 :     Symbol->setBinding(ELF::STB_WEAK);
     240        4569 :     Symbol->setExternal(true);
     241             :     break;
     242             : 
     243          58 :   case MCSA_Local:
     244          58 :     Symbol->setBinding(ELF::STB_LOCAL);
     245          58 :     Symbol->setExternal(false);
     246             :     break;
     247             : 
     248        8954 :   case MCSA_ELF_TypeFunction:
     249       17907 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
     250        8954 :     break;
     251             : 
     252          42 :   case MCSA_ELF_TypeIndFunction:
     253          84 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
     254          42 :     break;
     255             : 
     256       54448 :   case MCSA_ELF_TypeObject:
     257      108896 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     258       54448 :     break;
     259             : 
     260          11 :   case MCSA_ELF_TypeTLS:
     261          22 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
     262          11 :     break;
     263             : 
     264           0 :   case MCSA_ELF_TypeCommon:
     265             :     // TODO: Emit these as a common symbol.
     266           0 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     267           0 :     break;
     268             : 
     269           5 :   case MCSA_ELF_TypeNoType:
     270          10 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
     271           5 :     break;
     272             : 
     273          25 :   case MCSA_Protected:
     274          25 :     Symbol->setVisibility(ELF::STV_PROTECTED);
     275          25 :     break;
     276             : 
     277        3063 :   case MCSA_Hidden:
     278        3063 :     Symbol->setVisibility(ELF::STV_HIDDEN);
     279        3063 :     break;
     280             : 
     281           4 :   case MCSA_Internal:
     282           4 :     Symbol->setVisibility(ELF::STV_INTERNAL);
     283           4 :     break;
     284             : 
     285           0 :   case MCSA_AltEntry:
     286           0 :     llvm_unreachable("ELF doesn't support the .alt_entry attribute");
     287             :   }
     288             : 
     289             :   return true;
     290             : }
     291             : 
     292         234 : void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
     293             :                                      unsigned ByteAlignment) {
     294         234 :   auto *Symbol = cast<MCSymbolELF>(S);
     295         234 :   getAssembler().registerSymbol(*Symbol);
     296             : 
     297         234 :   if (!Symbol->isBindingSet()) {
     298         161 :     Symbol->setBinding(ELF::STB_GLOBAL);
     299         161 :     Symbol->setExternal(true);
     300             :   }
     301             : 
     302         234 :   Symbol->setType(ELF::STT_OBJECT);
     303             : 
     304         234 :   if (Symbol->getBinding() == ELF::STB_LOCAL) {
     305         212 :     MCSection &Section = *getAssembler().getContext().getELFSection(
     306          53 :         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
     307         106 :     MCSectionSubPair P = getCurrentSection();
     308          53 :     SwitchSection(&Section);
     309             : 
     310          53 :     EmitValueToAlignment(ByteAlignment, 0, 1, 0);
     311          53 :     EmitLabel(Symbol);
     312          53 :     EmitZeros(Size);
     313             : 
     314             :     // Update the maximum alignment of the section if necessary.
     315          53 :     if (ByteAlignment > Section.getAlignment())
     316           0 :       Section.setAlignment(ByteAlignment);
     317             : 
     318          53 :     SwitchSection(P.first, P.second);
     319             :   } else {
     320         181 :     if(Symbol->declareCommon(Size, ByteAlignment))
     321           4 :       report_fatal_error("Symbol: " + Symbol->getName() +
     322           1 :                          " redeclared as different type");
     323             :   }
     324             : 
     325             :   cast<MCSymbolELF>(Symbol)
     326         466 :       ->setSize(MCConstantExpr::create(Size, getContext()));
     327         233 : }
     328             : 
     329       62051 : void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
     330      124102 :   cast<MCSymbolELF>(Symbol)->setSize(Value);
     331       62051 : }
     332             : 
     333           9 : void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
     334             :                                           unsigned ByteAlignment) {
     335           9 :   auto *Symbol = cast<MCSymbolELF>(S);
     336             :   // FIXME: Should this be caught and done earlier?
     337           9 :   getAssembler().registerSymbol(*Symbol);
     338           9 :   Symbol->setBinding(ELF::STB_LOCAL);
     339          18 :   Symbol->setExternal(false);
     340           9 :   EmitCommonSymbol(Symbol, Size, ByteAlignment);
     341           9 : }
     342             : 
     343      689839 : void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
     344             :                                   SMLoc Loc) {
     345      689839 :   if (isBundleLocked())
     346           0 :     report_fatal_error("Emitting values inside a locked bundle is forbidden");
     347      689839 :   fixSymbolsInTLSFixups(Value);
     348      689839 :   MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
     349      689839 : }
     350             : 
     351       77614 : void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
     352             :                                          int64_t Value,
     353             :                                          unsigned ValueSize,
     354             :                                          unsigned MaxBytesToEmit) {
     355       77614 :   if (isBundleLocked())
     356           0 :     report_fatal_error("Emitting values inside a locked bundle is forbidden");
     357       77614 :   MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
     358             :                                          ValueSize, MaxBytesToEmit);
     359       77614 : }
     360             : 
     361         337 : void MCELFStreamer::EmitIdent(StringRef IdentString) {
     362        1348 :   MCSection *Comment = getAssembler().getContext().getELFSection(
     363         337 :       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
     364         337 :   PushSection();
     365         337 :   SwitchSection(Comment);
     366         337 :   if (!SeenIdent) {
     367         315 :     EmitIntValue(0, 1);
     368         315 :     SeenIdent = true;
     369             :   }
     370         337 :   EmitBytes(IdentString);
     371         337 :   EmitIntValue(0, 1);
     372         337 :   PopSection();
     373         337 : }
     374             : 
     375     2229563 : void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
     376     3044595 :   switch (expr->getKind()) {
     377        1534 :   case MCExpr::Target:
     378        1534 :     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
     379        1534 :     break;
     380             :   case MCExpr::Constant:
     381             :     break;
     382             : 
     383      815017 :   case MCExpr::Binary: {
     384      815017 :     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
     385      815017 :     fixSymbolsInTLSFixups(be->getLHS());
     386             :     fixSymbolsInTLSFixups(be->getRHS());
     387      815017 :     break;
     388             :   }
     389             : 
     390     1507149 :   case MCExpr::SymbolRef: {
     391     1507149 :     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
     392     1507149 :     switch (symRef.getKind()) {
     393             :     default:
     394             :       return;
     395             :     case MCSymbolRefExpr::VK_GOTTPOFF:
     396             :     case MCSymbolRefExpr::VK_INDNTPOFF:
     397             :     case MCSymbolRefExpr::VK_NTPOFF:
     398             :     case MCSymbolRefExpr::VK_GOTNTPOFF:
     399             :     case MCSymbolRefExpr::VK_TLSGD:
     400             :     case MCSymbolRefExpr::VK_TLSLD:
     401             :     case MCSymbolRefExpr::VK_TLSLDM:
     402             :     case MCSymbolRefExpr::VK_TPOFF:
     403             :     case MCSymbolRefExpr::VK_TPREL:
     404             :     case MCSymbolRefExpr::VK_DTPOFF:
     405             :     case MCSymbolRefExpr::VK_DTPREL:
     406             :     case MCSymbolRefExpr::VK_PPC_DTPMOD:
     407             :     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
     408             :     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
     409             :     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
     410             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
     411             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
     412             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
     413             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
     414             :     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
     415             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
     416             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
     417             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
     418             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
     419             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
     420             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
     421             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
     422             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
     423             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
     424             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
     425             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
     426             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
     427             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
     428             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
     429             :     case MCSymbolRefExpr::VK_PPC_TLS:
     430             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
     431             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
     432             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
     433             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
     434             :     case MCSymbolRefExpr::VK_PPC_TLSGD:
     435             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
     436             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
     437             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
     438             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
     439             :     case MCSymbolRefExpr::VK_PPC_TLSLD:
     440             :       break;
     441             :     }
     442         353 :     getAssembler().registerSymbol(symRef.getSymbol());
     443         706 :     cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
     444         353 :     break;
     445             :   }
     446             : 
     447          15 :   case MCExpr::Unary:
     448          15 :     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
     449          15 :     break;
     450             :   }
     451             : }
     452             : 
     453       57024 : void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
     454             :                                        const MCSubtargetInfo &STI) {
     455       57024 :   this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
     456      114048 :   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
     457             : 
     458      168896 :   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
     459      109696 :     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
     460       57024 : }
     461             : 
     462     1407537 : void MCELFStreamer::EmitInstToData(const MCInst &Inst,
     463             :                                    const MCSubtargetInfo &STI) {
     464     1407537 :   MCAssembler &Assembler = getAssembler();
     465     2814938 :   SmallVector<MCFixup, 4> Fixups;
     466     2814938 :   SmallString<256> Code;
     467     2814938 :   raw_svector_ostream VecOS(Code);
     468     1407537 :   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
     469             : 
     470     3484933 :   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
     471     1339718 :     fixSymbolsInTLSFixups(Fixups[i].getValue());
     472             : 
     473             :   // There are several possibilities here:
     474             :   //
     475             :   // If bundling is disabled, append the encoded instruction to the current data
     476             :   // fragment (or create a new such fragment if the current fragment is not a
     477             :   // data fragment).
     478             :   //
     479             :   // If bundling is enabled:
     480             :   // - If we're not in a bundle-locked group, emit the instruction into a
     481             :   //   fragment of its own. If there are no fixups registered for the
     482             :   //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
     483             :   //   MCDataFragment.
     484             :   // - If we're in a bundle-locked group, append the instruction to the current
     485             :   //   data fragment because we want all the instructions in a group to get into
     486             :   //   the same fragment. Be careful not to do that for the first instruction in
     487             :   //   the group, though.
     488             :   MCDataFragment *DF;
     489             : 
     490     1407537 :   if (Assembler.isBundlingEnabled()) {
     491        9544 :     MCSection &Sec = *getCurrentSectionOnly();
     492        4772 :     if (Assembler.getRelaxAll() && isBundleLocked())
     493             :       // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
     494             :       // the current bundle group.
     495          62 :       DF = BundleGroups.back();
     496        4741 :     else if (Assembler.getRelaxAll() && !isBundleLocked())
     497             :       // When not in a bundle-locked group and the -mc-relax-all flag is used,
     498             :       // we create a new temporary fragment which will be later merged into
     499             :       // the current fragment.
     500         226 :       DF = new MCDataFragment();
     501        9092 :     else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
     502             :       // If we are bundle-locked, we re-use the current fragment.
     503             :       // The bundle-locking directive ensures this is a new data fragment.
     504        7792 :       DF = cast<MCDataFragment>(getCurrentFragment());
     505         896 :     else if (!isBundleLocked() && Fixups.size() == 0) {
     506             :       // Optimize memory usage by emitting the instruction to a
     507             :       // MCCompactEncodedInstFragment when not in a bundle-locked group and
     508             :       // there are no fixups registered.
     509         270 :       MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
     510         135 :       insert(CEIF);
     511         540 :       CEIF->getContents().append(Code.begin(), Code.end());
     512         135 :       return;
     513             :     } else {
     514        1194 :       DF = new MCDataFragment();
     515         597 :       insert(DF);
     516             :     }
     517        4637 :     if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
     518             :       // If this fragment is for a group marked "align_to_end", set a flag
     519             :       // in the fragment. This can happen after the fragment has already been
     520             :       // created if there are nested bundle_align groups and an inner one
     521             :       // is the one marked align_to_end.
     522        2223 :       DF->setAlignToBundleEnd(true);
     523             :     }
     524             : 
     525             :     // We're now emitting an instruction in a bundle group, so this flag has
     526             :     // to be turned off.
     527             :     Sec.setBundleGroupBeforeFirstInst(false);
     528             :   } else {
     529     1402765 :     DF = getOrCreateDataFragment();
     530             :   }
     531             : 
     532             :   // Add the fixups and data.
     533     3484663 :   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
     534     3349295 :     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
     535     1339718 :     DF->getFixups().push_back(Fixups[i]);
     536             :   }
     537     1407402 :   DF->setHasInstructions(true);
     538     5629608 :   DF->getContents().append(Code.begin(), Code.end());
     539             : 
     540     1412039 :   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
     541         144 :     if (!isBundleLocked()) {
     542         113 :       mergeFragment(getOrCreateDataFragment(), DF);
     543         113 :       delete DF;
     544             :     }
     545             :   }
     546             : }
     547             : 
     548          38 : void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
     549             :   assert(AlignPow2 <= 30 && "Invalid bundle alignment");
     550          38 :   MCAssembler &Assembler = getAssembler();
     551          40 :   if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
     552           2 :                         Assembler.getBundleAlignSize() == 1U << AlignPow2))
     553          76 :     Assembler.setBundleAlignSize(1U << AlignPow2);
     554             :   else
     555           0 :     report_fatal_error(".bundle_align_mode cannot be changed once set");
     556          38 : }
     557             : 
     558         593 : void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
     559        1186 :   MCSection &Sec = *getCurrentSectionOnly();
     560             : 
     561             :   // Sanity checks
     562             :   //
     563         593 :   if (!getAssembler().isBundlingEnabled())
     564           1 :     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
     565             : 
     566         592 :   if (!isBundleLocked())
     567             :     Sec.setBundleGroupBeforeFirstInst(true);
     568             : 
     569        1184 :   if (getAssembler().getRelaxAll() && !isBundleLocked()) {
     570             :     // TODO: drop the lock state and set directly in the fragment
     571          32 :     MCDataFragment *DF = new MCDataFragment();
     572          16 :     BundleGroups.push_back(DF);
     573             :   }
     574             : 
     575         592 :   Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
     576             :                                     : MCSection::BundleLocked);
     577         592 : }
     578             : 
     579         593 : void MCELFStreamer::EmitBundleUnlock() {
     580        1186 :   MCSection &Sec = *getCurrentSectionOnly();
     581             : 
     582             :   // Sanity checks
     583         593 :   if (!getAssembler().isBundlingEnabled())
     584           0 :     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
     585         593 :   else if (!isBundleLocked())
     586           2 :     report_fatal_error(".bundle_unlock without matching lock");
     587         591 :   else if (Sec.isBundleGroupBeforeFirstInst())
     588           0 :     report_fatal_error("Empty bundle-locked group is forbidden");
     589             : 
     590             :   // When the -mc-relax-all flag is used, we emit instructions to fragments
     591             :   // stored on a stack. When the bundle unlock is emitted, we pop a fragment
     592             :   // from the stack a merge it to the one below.
     593        1182 :   if (getAssembler().getRelaxAll()) {
     594             :     assert(!BundleGroups.empty() && "There are no bundle groups");
     595          40 :     MCDataFragment *DF = BundleGroups.back();
     596             : 
     597             :     // FIXME: Use BundleGroups to track the lock state instead.
     598          20 :     Sec.setBundleLockState(MCSection::NotBundleLocked);
     599             : 
     600             :     // FIXME: Use more separate fragments for nested groups.
     601          20 :     if (!isBundleLocked()) {
     602          16 :       mergeFragment(getOrCreateDataFragment(), DF);
     603          30 :       BundleGroups.pop_back();
     604          30 :       delete DF;
     605             :     }
     606             : 
     607          19 :     if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
     608          17 :       getOrCreateDataFragment()->setAlignToBundleEnd(false);
     609             :   } else
     610         571 :     Sec.setBundleLockState(MCSection::NotBundleLocked);
     611         590 : }
     612             : 
     613        3298 : void MCELFStreamer::FinishImpl() {
     614             :   // Ensure the last section gets aligned if necessary.
     615        6595 :   MCSection *CurSection = getCurrentSectionOnly();
     616        6595 :   setSectionAlignmentForBundling(getAssembler(), CurSection);
     617             : 
     618        3298 :   EmitFrames(nullptr);
     619             : 
     620        3297 :   this->MCObjectStreamer::FinishImpl();
     621        3288 : }
     622             : 
     623           0 : void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
     624           0 :   llvm_unreachable("Generic ELF doesn't support this directive");
     625             : }
     626             : 
     627           0 : void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
     628           0 :   llvm_unreachable("ELF doesn't support this directive");
     629             : }
     630             : 
     631           0 : void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
     632             :                                  uint64_t Size, unsigned ByteAlignment) {
     633           0 :   llvm_unreachable("ELF doesn't support this directive");
     634             : }
     635             : 
     636           0 : void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
     637             :                                    uint64_t Size, unsigned ByteAlignment) {
     638           0 :   llvm_unreachable("ELF doesn't support this directive");
     639             : }
     640             : 
     641        2268 : MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
     642             :                                     raw_pwrite_stream &OS, MCCodeEmitter *CE,
     643             :                                     bool RelaxAll) {
     644        4536 :   MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
     645        2268 :   if (RelaxAll)
     646          33 :     S->getAssembler().setRelaxAll(true);
     647        2268 :   return S;
     648             : }

Generated by: LCOV version 1.13