LCOV - code coverage report
Current view: top level - lib/MC - MCELFStreamer.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 259 279 92.8 %
Date: 2018-09-23 13:06:45 Functions: 28 32 87.5 %
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       12362 : MCELFStreamer::MCELFStreamer(MCContext &Context,
      43             :                              std::unique_ptr<MCAsmBackend> TAB,
      44             :                              std::unique_ptr<MCObjectWriter> OW,
      45       12362 :                              std::unique_ptr<MCCodeEmitter> Emitter)
      46             :     : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
      47       37086 :                        std::move(Emitter)) {}
      48             : 
      49     4619201 : bool MCELFStreamer::isBundleLocked() const {
      50     9238402 :   return getCurrentSectionOnly()->isBundleLocked();
      51             : }
      52             : 
      53         130 : void MCELFStreamer::mergeFragment(MCDataFragment *DF,
      54             :                                   MCDataFragment *EF) {
      55             :   MCAssembler &Assembler = getAssembler();
      56             : 
      57         130 :   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
      58         130 :     uint64_t FSize = EF->getContents().size();
      59             : 
      60         130 :     if (FSize > Assembler.getBundleAlignSize())
      61           1 :       report_fatal_error("Fragment can't be larger than a bundle size");
      62             : 
      63         258 :     uint64_t RequiredBundlePadding = computeBundlePadding(
      64             :         Assembler, EF, DF->getContents().size(), FSize);
      65             : 
      66         129 :     if (RequiredBundlePadding > UINT8_MAX)
      67           0 :       report_fatal_error("Padding cannot exceed 255 bytes");
      68             : 
      69         129 :     if (RequiredBundlePadding > 0) {
      70             :       SmallString<256> Code;
      71             :       raw_svector_ostream VecOS(Code);
      72          23 :       EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
      73          23 :       Assembler.writeFragmentPadding(VecOS, *EF, FSize);
      74             : 
      75          23 :       DF->getContents().append(Code.begin(), Code.end());
      76             :     }
      77             :   }
      78             : 
      79         258 :   flushPendingLabels(DF, DF->getContents().size());
      80             : 
      81         181 :   for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
      82          52 :     EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
      83          52 :                                  DF->getContents().size());
      84          52 :     DF->getFixups().push_back(EF->getFixups()[i]);
      85             :   }
      86         129 :   if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
      87             :     DF->setHasInstructions(*EF->getSubtargetInfo());
      88         129 :   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
      89         129 : }
      90             : 
      91       12280 : void MCELFStreamer::InitSections(bool NoExecStack) {
      92       12280 :   MCContext &Ctx = getContext();
      93       12280 :   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
      94       12280 :   EmitCodeAlignment(4);
      95             : 
      96       12280 :   if (NoExecStack)
      97           1 :     SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
      98       12280 : }
      99             : 
     100     8928839 : void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
     101             :   auto *Symbol = cast<MCSymbolELF>(S);
     102     8928839 :   MCObjectStreamer::EmitLabel(Symbol, Loc);
     103             : 
     104             :   const MCSectionELF &Section =
     105             :       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
     106     8928840 :   if (Section.getFlags() & ELF::SHF_TLS)
     107         248 :     Symbol->setType(ELF::STT_TLS);
     108     8928840 : }
     109             : 
     110          10 : void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc, MCFragment *F) {
     111             :   auto *Symbol = cast<MCSymbolELF>(S);
     112          10 :   MCObjectStreamer::EmitLabel(Symbol, Loc, F);
     113             : 
     114             :   const MCSectionELF &Section =
     115             :       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
     116          10 :   if (Section.getFlags() & ELF::SHF_TLS)
     117           0 :     Symbol->setType(ELF::STT_TLS);
     118          10 : }
     119             : 
     120         941 : void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
     121             :   // Let the target do whatever target specific stuff it needs to do.
     122         941 :   getAssembler().getBackend().handleAssemblerFlag(Flag);
     123             :   // Do any generic stuff we need to do.
     124         941 :   switch (Flag) {
     125             :   case MCAF_SyntaxUnified: return; // no-op here.
     126             :   case MCAF_Code16: return; // Change parsing mode; no-op here.
     127             :   case MCAF_Code32: return; // Change parsing mode; no-op here.
     128             :   case MCAF_Code64: return; // Change parsing mode; no-op here.
     129           0 :   case MCAF_SubsectionsViaSymbols:
     130             :     getAssembler().setSubsectionsViaSymbols(true);
     131             :     return;
     132             :   }
     133             : 
     134           0 :   llvm_unreachable("invalid assembler flag!");
     135             : }
     136             : 
     137             : // If bundle alignment is used and there are any instructions in the section, it
     138             : // needs to be aligned to at least the bundle size.
     139             : static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
     140             :                                            MCSection *Section) {
     141      969235 :   if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
     142          46 :       Section->getAlignment() < Assembler.getBundleAlignSize())
     143             :     Section->setAlignment(Assembler.getBundleAlignSize());
     144             : }
     145             : 
     146      956924 : void MCELFStreamer::ChangeSection(MCSection *Section,
     147             :                                   const MCExpr *Subsection) {
     148             :   MCSection *CurSection = getCurrentSectionOnly();
     149      956924 :   if (CurSection && isBundleLocked())
     150           1 :     report_fatal_error("Unterminated .bundle_lock when changing a section");
     151             : 
     152             :   MCAssembler &Asm = getAssembler();
     153             :   // Ensure the previous section gets aligned if necessary.
     154             :   setSectionAlignmentForBundling(Asm, CurSection);
     155             :   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
     156      956924 :   const MCSymbol *Grp = SectionELF->getGroup();
     157      956924 :   if (Grp)
     158      269646 :     Asm.registerSymbol(*Grp);
     159             : 
     160      956924 :   changeSectionImpl(Section, Subsection);
     161      956924 :   Asm.registerSymbol(*Section->getBeginSymbol());
     162      956924 : }
     163             : 
     164          20 : void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
     165          20 :   getAssembler().registerSymbol(*Symbol);
     166          40 :   const MCExpr *Value = MCSymbolRefExpr::create(
     167          20 :       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
     168          20 :   Alias->setVariableValue(Value);
     169          20 : }
     170             : 
     171             : // When GNU as encounters more than one .type declaration for an object it seems
     172             : // to use a mechanism similar to the one below to decide which type is actually
     173             : // used in the object file.  The greater of T1 and T2 is selected based on the
     174             : // following ordering:
     175             : //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
     176             : // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
     177             : // provided type).
     178             : static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
     179        1785 :   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
     180      501885 :                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
     181      501890 :     if (T1 == Type)
     182             :       return T2;
     183        1899 :     if (T2 == Type)
     184             :       return T1;
     185             :   }
     186             : 
     187             :   return T2;
     188             : }
     189             : 
     190      790835 : bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
     191             :   auto *Symbol = cast<MCSymbolELF>(S);
     192             : 
     193             :   // Adding a symbol attribute always introduces the symbol, note that an
     194             :   // important side effect of calling registerSymbol here is to register
     195             :   // the symbol with the assembler.
     196      790835 :   getAssembler().registerSymbol(*Symbol);
     197             : 
     198             :   // The implementation of symbol attributes is designed to match 'as', but it
     199             :   // leaves much to desired. It doesn't really make sense to arbitrarily add and
     200             :   // remove flags, but 'as' allows this (in particular, see .desc).
     201             :   //
     202             :   // In the future it might be worth trying to make these operations more well
     203             :   // defined.
     204      790835 :   switch (Attribute) {
     205             :   case MCSA_LazyReference:
     206             :   case MCSA_Reference:
     207             :   case MCSA_SymbolResolver:
     208             :   case MCSA_PrivateExtern:
     209             :   case MCSA_WeakDefinition:
     210             :   case MCSA_WeakDefAutoPrivate:
     211             :   case MCSA_Invalid:
     212             :   case MCSA_IndirectSymbol:
     213             :     return false;
     214             : 
     215             :   case MCSA_NoDeadStrip:
     216             :     // Ignore for now.
     217             :     break;
     218             : 
     219           2 :   case MCSA_ELF_TypeGnuUniqueObject:
     220           4 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     221           2 :     Symbol->setBinding(ELF::STB_GNU_UNIQUE);
     222             :     Symbol->setExternal(true);
     223             :     break;
     224             : 
     225       39683 :   case MCSA_Global:
     226       39683 :     Symbol->setBinding(ELF::STB_GLOBAL);
     227             :     Symbol->setExternal(true);
     228             :     break;
     229             : 
     230      237946 :   case MCSA_WeakReference:
     231             :   case MCSA_Weak:
     232      237946 :     Symbol->setBinding(ELF::STB_WEAK);
     233             :     Symbol->setExternal(true);
     234             :     break;
     235             : 
     236        3055 :   case MCSA_Local:
     237        3055 :     Symbol->setBinding(ELF::STB_LOCAL);
     238             :     Symbol->setExternal(false);
     239             :     break;
     240             : 
     241      239489 :   case MCSA_ELF_TypeFunction:
     242      478978 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
     243      239489 :     break;
     244             : 
     245          46 :   case MCSA_ELF_TypeIndFunction:
     246          92 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
     247          46 :     break;
     248             : 
     249      260552 :   case MCSA_ELF_TypeObject:
     250      521104 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     251      260552 :     break;
     252             : 
     253          11 :   case MCSA_ELF_TypeTLS:
     254          22 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
     255          11 :     break;
     256             : 
     257           0 :   case MCSA_ELF_TypeCommon:
     258             :     // TODO: Emit these as a common symbol.
     259           0 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
     260           0 :     break;
     261             : 
     262           5 :   case MCSA_ELF_TypeNoType:
     263          10 :     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
     264           5 :     break;
     265             : 
     266          33 :   case MCSA_Protected:
     267          33 :     Symbol->setVisibility(ELF::STV_PROTECTED);
     268          33 :     break;
     269             : 
     270        9925 :   case MCSA_Hidden:
     271        9925 :     Symbol->setVisibility(ELF::STV_HIDDEN);
     272        9925 :     break;
     273             : 
     274           4 :   case MCSA_Internal:
     275           4 :     Symbol->setVisibility(ELF::STV_INTERNAL);
     276           4 :     break;
     277             : 
     278             :   case MCSA_AltEntry:
     279             :     llvm_unreachable("ELF doesn't support the .alt_entry attribute");
     280             :   }
     281             : 
     282             :   return true;
     283             : }
     284             : 
     285        3589 : void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
     286             :                                      unsigned ByteAlignment) {
     287             :   auto *Symbol = cast<MCSymbolELF>(S);
     288        3589 :   getAssembler().registerSymbol(*Symbol);
     289             : 
     290        3589 :   if (!Symbol->isBindingSet()) {
     291         514 :     Symbol->setBinding(ELF::STB_GLOBAL);
     292             :     Symbol->setExternal(true);
     293             :   }
     294             : 
     295        3589 :   Symbol->setType(ELF::STT_OBJECT);
     296             : 
     297        3589 :   if (Symbol->getBinding() == ELF::STB_LOCAL) {
     298        3055 :     MCSection &Section = *getAssembler().getContext().getELFSection(
     299        3055 :         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
     300             :     MCSectionSubPair P = getCurrentSection();
     301        3055 :     SwitchSection(&Section);
     302             : 
     303        3055 :     EmitValueToAlignment(ByteAlignment, 0, 1, 0);
     304        3055 :     EmitLabel(Symbol);
     305        3055 :     EmitZeros(Size);
     306             : 
     307             :     // Update the maximum alignment of the section if necessary.
     308        3055 :     if (ByteAlignment > Section.getAlignment())
     309             :       Section.setAlignment(ByteAlignment);
     310             : 
     311        3055 :     SwitchSection(P.first, P.second);
     312             :   } else {
     313         534 :     if(Symbol->declareCommon(Size, ByteAlignment))
     314           1 :       report_fatal_error("Symbol: " + Symbol->getName() +
     315             :                          " redeclared as different type");
     316             :   }
     317             : 
     318             :   cast<MCSymbolELF>(Symbol)
     319        3588 :       ->setSize(MCConstantExpr::create(Size, getContext()));
     320        3588 : }
     321             : 
     322      494116 : void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
     323             :   cast<MCSymbolELF>(Symbol)->setSize(Value);
     324      494116 : }
     325             : 
     326          68 : void MCELFStreamer::emitELFSymverDirective(StringRef AliasName,
     327             :                                            const MCSymbol *Aliasee) {
     328          68 :   getAssembler().Symvers.push_back({AliasName, Aliasee});
     329          68 : }
     330             : 
     331          19 : void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
     332             :                                           unsigned ByteAlignment) {
     333             :   auto *Symbol = cast<MCSymbolELF>(S);
     334             :   // FIXME: Should this be caught and done earlier?
     335          19 :   getAssembler().registerSymbol(*Symbol);
     336          19 :   Symbol->setBinding(ELF::STB_LOCAL);
     337             :   Symbol->setExternal(false);
     338          19 :   EmitCommonSymbol(Symbol, Size, ByteAlignment);
     339          19 : }
     340             : 
     341     2964026 : void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
     342             :                                   SMLoc Loc) {
     343     2964026 :   if (isBundleLocked())
     344           0 :     report_fatal_error("Emitting values inside a locked bundle is forbidden");
     345     2964026 :   fixSymbolsInTLSFixups(Value);
     346     2964026 :   MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
     347     2964026 : }
     348             : 
     349      703587 : void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
     350             :                                          int64_t Value,
     351             :                                          unsigned ValueSize,
     352             :                                          unsigned MaxBytesToEmit) {
     353      703587 :   if (isBundleLocked())
     354           0 :     report_fatal_error("Emitting values inside a locked bundle is forbidden");
     355      703587 :   MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
     356             :                                          ValueSize, MaxBytesToEmit);
     357      703587 : }
     358             : 
     359           9 : void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
     360             :                                        const MCSymbolRefExpr *To,
     361             :                                        uint64_t Count) {
     362           9 :   getAssembler().CGProfile.push_back({From, To, Count});
     363           9 : }
     364             : 
     365        7509 : void MCELFStreamer::EmitIdent(StringRef IdentString) {
     366        7509 :   MCSection *Comment = getAssembler().getContext().getELFSection(
     367             :       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
     368        7509 :   PushSection();
     369        7509 :   SwitchSection(Comment);
     370        7509 :   if (!SeenIdent) {
     371        7479 :     EmitIntValue(0, 1);
     372        7479 :     SeenIdent = true;
     373             :   }
     374        7509 :   EmitBytes(IdentString);
     375        7509 :   EmitIntValue(0, 1);
     376        7509 :   PopSection();
     377        7509 : }
     378             : 
     379    15312779 : void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
     380    21971261 :   switch (expr->getKind()) {
     381             :   case MCExpr::Target:
     382       24378 :     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
     383       12189 :     break;
     384             :   case MCExpr::Constant:
     385             :     break;
     386             : 
     387             :   case MCExpr::Binary: {
     388             :     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
     389     6658465 :     fixSymbolsInTLSFixups(be->getLHS());
     390     6658465 :     fixSymbolsInTLSFixups(be->getRHS());
     391     6658465 :     break;
     392             :   }
     393             : 
     394             :   case MCExpr::SymbolRef: {
     395             :     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
     396     9626539 :     switch (symRef.getKind()) {
     397             :     default:
     398             :       return;
     399             :     case MCSymbolRefExpr::VK_GOTTPOFF:
     400             :     case MCSymbolRefExpr::VK_INDNTPOFF:
     401             :     case MCSymbolRefExpr::VK_NTPOFF:
     402             :     case MCSymbolRefExpr::VK_GOTNTPOFF:
     403             :     case MCSymbolRefExpr::VK_TLSGD:
     404             :     case MCSymbolRefExpr::VK_TLSLD:
     405             :     case MCSymbolRefExpr::VK_TLSLDM:
     406             :     case MCSymbolRefExpr::VK_TPOFF:
     407             :     case MCSymbolRefExpr::VK_TPREL:
     408             :     case MCSymbolRefExpr::VK_DTPOFF:
     409             :     case MCSymbolRefExpr::VK_DTPREL:
     410             :     case MCSymbolRefExpr::VK_PPC_DTPMOD:
     411             :     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
     412             :     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
     413             :     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
     414             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
     415             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
     416             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
     417             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
     418             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
     419             :     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
     420             :     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
     421             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
     422             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
     423             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
     424             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
     425             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
     426             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
     427             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
     428             :     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
     429             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
     430             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
     431             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
     432             :     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
     433             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
     434             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
     435             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
     436             :     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
     437             :     case MCSymbolRefExpr::VK_PPC_TLS:
     438             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
     439             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
     440             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
     441             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
     442             :     case MCSymbolRefExpr::VK_PPC_TLSGD:
     443             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
     444             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
     445             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
     446             :     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
     447             :     case MCSymbolRefExpr::VK_PPC_TLSLD:
     448             :       break;
     449             :     }
     450         724 :     getAssembler().registerSymbol(symRef.getSymbol());
     451         724 :     cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
     452         724 :     break;
     453             :   }
     454             : 
     455             :   case MCExpr::Unary:
     456          17 :     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
     457          17 :     break;
     458             :   }
     459             : }
     460             : 
     461          18 : void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
     462          18 :   const MCSymbol *S = &SRE->getSymbol();
     463          18 :   if (S->isTemporary()) {
     464           2 :     if (!S->isInSection()) {
     465           2 :       getContext().reportError(
     466           1 :           SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
     467           1 :                              "`" + S->getName() + "`");
     468           2 :       return;
     469             :     }
     470           1 :     S = S->getSection().getBeginSymbol();
     471             :     S->setUsedInReloc();
     472           1 :     SRE =
     473           1 :         MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc());
     474           1 :     return;
     475             :   }
     476             :   // Not a temporary, referece it as a weak undefined.
     477             :   bool Created;
     478          16 :   getAssembler().registerSymbol(*S, &Created);
     479          16 :   if (Created) {
     480           2 :     cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK);
     481             :     cast<MCSymbolELF>(S)->setExternal(true);
     482             :   }
     483             : }
     484             : 
     485       12311 : void MCELFStreamer::finalizeCGProfile() {
     486       12320 :   for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
     487           9 :     finalizeCGProfileEntry(E.From);
     488           9 :     finalizeCGProfileEntry(E.To);
     489             :   }
     490       12311 : }
     491             : 
     492      129405 : void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
     493             :                                        const MCSubtargetInfo &STI) {
     494      129405 :   this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
     495      129405 :   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
     496             : 
     497      256465 :   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
     498      254120 :     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
     499      129405 : }
     500             : 
     501             : // A fragment can only have one Subtarget, and when bundling is enabled we
     502             : // sometimes need to use the same fragment. We give an error if there
     503             : // are conflicting Subtargets.
     504             : static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
     505             :                                   const MCSubtargetInfo *NewSTI) {
     506        3916 :   if (OldSTI && NewSTI && OldSTI != NewSTI)
     507           3 :     report_fatal_error("A Bundle can only have one Subtarget.");
     508             : }
     509             : 
     510    30427177 : void MCELFStreamer::EmitInstToData(const MCInst &Inst,
     511             :                                    const MCSubtargetInfo &STI) {
     512             :   MCAssembler &Assembler = getAssembler();
     513             :   SmallVector<MCFixup, 4> Fixups;
     514             :   SmallString<256> Code;
     515             :   raw_svector_ostream VecOS(Code);
     516    30427177 :   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
     517             : 
     518    35990405 :   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
     519    11126456 :     fixSymbolsInTLSFixups(Fixups[i].getValue());
     520             : 
     521             :   // There are several possibilities here:
     522             :   //
     523             :   // If bundling is disabled, append the encoded instruction to the current data
     524             :   // fragment (or create a new such fragment if the current fragment is not a
     525             :   // data fragment, or the Subtarget has changed).
     526             :   //
     527             :   // If bundling is enabled:
     528             :   // - If we're not in a bundle-locked group, emit the instruction into a
     529             :   //   fragment of its own. If there are no fixups registered for the
     530             :   //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
     531             :   //   MCDataFragment.
     532             :   // - If we're in a bundle-locked group, append the instruction to the current
     533             :   //   data fragment because we want all the instructions in a group to get into
     534             :   //   the same fragment. Be careful not to do that for the first instruction in
     535             :   //   the group, though.
     536             :   MCDataFragment *DF;
     537             : 
     538    30427177 :   if (Assembler.isBundlingEnabled()) {
     539             :     MCSection &Sec = *getCurrentSectionOnly();
     540        4789 :     if (Assembler.getRelaxAll() && isBundleLocked()) {
     541             :       // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
     542             :       // the current bundle group.
     543          33 :       DF = BundleGroups.back();
     544          33 :       CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
     545             :     }
     546        4756 :     else if (Assembler.getRelaxAll() && !isBundleLocked())
     547             :       // When not in a bundle-locked group and the -mc-relax-all flag is used,
     548             :       // we create a new temporary fragment which will be later merged into
     549             :       // the current fragment.
     550         114 :       DF = new MCDataFragment();
     551        4642 :     else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
     552             :       // If we are bundle-locked, we re-use the current fragment.
     553             :       // The bundle-locking directive ensures this is a new data fragment.
     554        3900 :       DF = cast<MCDataFragment>(getCurrentFragment());
     555        3900 :       CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
     556             :     }
     557         742 :     else if (!isBundleLocked() && Fixups.size() == 0) {
     558             :       // Optimize memory usage by emitting the instruction to a
     559             :       // MCCompactEncodedInstFragment when not in a bundle-locked group and
     560             :       // there are no fixups registered.
     561         141 :       MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
     562         141 :       insert(CEIF);
     563         141 :       CEIF->getContents().append(Code.begin(), Code.end());
     564             :       CEIF->setHasInstructions(STI);
     565             :       return;
     566             :     } else {
     567         601 :       DF = new MCDataFragment();
     568         601 :       insert(DF);
     569             :     }
     570        4645 :     if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
     571             :       // If this fragment is for a group marked "align_to_end", set a flag
     572             :       // in the fragment. This can happen after the fragment has already been
     573             :       // created if there are nested bundle_align groups and an inner one
     574             :       // is the one marked align_to_end.
     575             :       DF->setAlignToBundleEnd(true);
     576             :     }
     577             : 
     578             :     // We're now emitting an instruction in a bundle group, so this flag has
     579             :     // to be turned off.
     580             :     Sec.setBundleGroupBeforeFirstInst(false);
     581             :   } else {
     582    30422388 :     DF = getOrCreateDataFragment(&STI);
     583             :   }
     584             : 
     585             :   // Add the fixups and data.
     586    35990261 :   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
     587    11126456 :     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
     588     5563228 :     DF->getFixups().push_back(Fixups[i]);
     589             :   }
     590             :   DF->setHasInstructions(STI);
     591    30427033 :   DF->getContents().append(Code.begin(), Code.end());
     592             : 
     593    30427032 :   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
     594         146 :     if (!isBundleLocked()) {
     595         114 :       mergeFragment(getOrCreateDataFragment(&STI), DF);
     596         114 :       delete DF;
     597             :     }
     598             :   }
     599             : }
     600             : 
     601          42 : void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
     602             :   assert(AlignPow2 <= 30 && "Invalid bundle alignment");
     603             :   MCAssembler &Assembler = getAssembler();
     604          42 :   if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
     605           2 :                         Assembler.getBundleAlignSize() == 1U << AlignPow2))
     606          42 :     Assembler.setBundleAlignSize(1U << AlignPow2);
     607             :   else
     608           0 :     report_fatal_error(".bundle_align_mode cannot be changed once set");
     609          42 : }
     610             : 
     611         598 : void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
     612             :   MCSection &Sec = *getCurrentSectionOnly();
     613             : 
     614             :   // Sanity checks
     615             :   //
     616         598 :   if (!getAssembler().isBundlingEnabled())
     617           1 :     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
     618             : 
     619         597 :   if (!isBundleLocked())
     620             :     Sec.setBundleGroupBeforeFirstInst(true);
     621             : 
     622         597 :   if (getAssembler().getRelaxAll() && !isBundleLocked()) {
     623             :     // TODO: drop the lock state and set directly in the fragment
     624          17 :     MCDataFragment *DF = new MCDataFragment();
     625          17 :     BundleGroups.push_back(DF);
     626             :   }
     627             : 
     628         906 :   Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
     629             :                                     : MCSection::BundleLocked);
     630         597 : }
     631             : 
     632         595 : void MCELFStreamer::EmitBundleUnlock() {
     633             :   MCSection &Sec = *getCurrentSectionOnly();
     634             : 
     635             :   // Sanity checks
     636         595 :   if (!getAssembler().isBundlingEnabled())
     637           0 :     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
     638         595 :   else if (!isBundleLocked())
     639           2 :     report_fatal_error(".bundle_unlock without matching lock");
     640         593 :   else if (Sec.isBundleGroupBeforeFirstInst())
     641           0 :     report_fatal_error("Empty bundle-locked group is forbidden");
     642             : 
     643             :   // When the -mc-relax-all flag is used, we emit instructions to fragments
     644             :   // stored on a stack. When the bundle unlock is emitted, we pop a fragment
     645             :   // from the stack a merge it to the one below.
     646         593 :   if (getAssembler().getRelaxAll()) {
     647             :     assert(!BundleGroups.empty() && "There are no bundle groups");
     648          20 :     MCDataFragment *DF = BundleGroups.back();
     649             : 
     650             :     // FIXME: Use BundleGroups to track the lock state instead.
     651          20 :     Sec.setBundleLockState(MCSection::NotBundleLocked);
     652             : 
     653             :     // FIXME: Use more separate fragments for nested groups.
     654          20 :     if (!isBundleLocked()) {
     655          16 :       mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF);
     656             :       BundleGroups.pop_back();
     657          15 :       delete DF;
     658             :     }
     659             : 
     660          19 :     if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
     661          17 :       getOrCreateDataFragment()->setAlignToBundleEnd(false);
     662             :   } else
     663         573 :     Sec.setBundleLockState(MCSection::NotBundleLocked);
     664         592 : }
     665             : 
     666       12310 : void MCELFStreamer::FinishImpl() {
     667             :   // Ensure the last section gets aligned if necessary.
     668             :   MCSection *CurSection = getCurrentSectionOnly();
     669             :   setSectionAlignmentForBundling(getAssembler(), CurSection);
     670             : 
     671       12310 :   finalizeCGProfile();
     672       12311 :   EmitFrames(nullptr);
     673             : 
     674       12311 :   this->MCObjectStreamer::FinishImpl();
     675       12299 : }
     676             : 
     677           0 : void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
     678           0 :   llvm_unreachable("Generic ELF doesn't support this directive");
     679             : }
     680             : 
     681           0 : void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
     682           0 :   llvm_unreachable("ELF doesn't support this directive");
     683             : }
     684             : 
     685           0 : void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
     686             :                                  uint64_t Size, unsigned ByteAlignment,
     687             :                                  SMLoc Loc) {
     688           0 :   llvm_unreachable("ELF doesn't support this directive");
     689             : }
     690             : 
     691           0 : void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
     692             :                                    uint64_t Size, unsigned ByteAlignment) {
     693           0 :   llvm_unreachable("ELF doesn't support this directive");
     694             : }
     695             : 
     696       10336 : MCStreamer *llvm::createELFStreamer(MCContext &Context,
     697             :                                     std::unique_ptr<MCAsmBackend> &&MAB,
     698             :                                     std::unique_ptr<MCObjectWriter> &&OW,
     699             :                                     std::unique_ptr<MCCodeEmitter> &&CE,
     700             :                                     bool RelaxAll) {
     701             :   MCELFStreamer *S =
     702       20672 :       new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
     703       10336 :   if (RelaxAll)
     704             :     S->getAssembler().setRelaxAll(true);
     705       10336 :   return S;
     706             : }

Generated by: LCOV version 1.13