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

Generated by: LCOV version 1.13