LCOV - code coverage report
Current view: top level - lib/MC - MCContext.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 264 293 90.1 %
Date: 2017-09-14 15:23:50 Functions: 39 42 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
       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             : #include "llvm/MC/MCContext.h"
      11             : #include "llvm/ADT/SmallString.h"
      12             : #include "llvm/ADT/SmallVector.h"
      13             : #include "llvm/ADT/StringMap.h"
      14             : #include "llvm/ADT/StringRef.h"
      15             : #include "llvm/ADT/Twine.h"
      16             : #include "llvm/BinaryFormat/COFF.h"
      17             : #include "llvm/BinaryFormat/ELF.h"
      18             : #include "llvm/MC/MCAsmInfo.h"
      19             : #include "llvm/MC/MCCodeView.h"
      20             : #include "llvm/MC/MCDwarf.h"
      21             : #include "llvm/MC/MCExpr.h"
      22             : #include "llvm/MC/MCFragment.h"
      23             : #include "llvm/MC/MCLabel.h"
      24             : #include "llvm/MC/MCObjectFileInfo.h"
      25             : #include "llvm/MC/MCSectionCOFF.h"
      26             : #include "llvm/MC/MCSectionELF.h"
      27             : #include "llvm/MC/MCSectionMachO.h"
      28             : #include "llvm/MC/MCSectionWasm.h"
      29             : #include "llvm/MC/MCStreamer.h"
      30             : #include "llvm/MC/MCSymbol.h"
      31             : #include "llvm/MC/MCSymbolCOFF.h"
      32             : #include "llvm/MC/MCSymbolELF.h"
      33             : #include "llvm/MC/MCSymbolMachO.h"
      34             : #include "llvm/MC/MCSymbolWasm.h"
      35             : #include "llvm/MC/SectionKind.h"
      36             : #include "llvm/Support/Casting.h"
      37             : #include "llvm/Support/CommandLine.h"
      38             : #include "llvm/Support/ErrorHandling.h"
      39             : #include "llvm/Support/MemoryBuffer.h"
      40             : #include "llvm/Support/Signals.h"
      41             : #include "llvm/Support/SourceMgr.h"
      42             : #include "llvm/Support/raw_ostream.h"
      43             : #include <cassert>
      44             : #include <cstdlib>
      45             : #include <tuple>
      46             : #include <utility>
      47             : 
      48             : using namespace llvm;
      49             : 
      50             : static cl::opt<char*>
      51       72330 : AsSecureLogFileName("as-secure-log-file-name",
      52      216990 :         cl::desc("As secure log file name (initialized from "
      53             :                  "AS_SECURE_LOG_FILE env variable)"),
      54      289320 :         cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
      55             : 
      56       23941 : MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
      57             :                      const MCObjectFileInfo *mofi, const SourceMgr *mgr,
      58       23941 :                      bool DoAutoReset)
      59             :     : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
      60             :       Symbols(Allocator), UsedNames(Allocator),
      61             :       CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
      62      646407 :       AutoReset(DoAutoReset) {
      63       23941 :   SecureLogFile = AsSecureLogFileName;
      64             : 
      65       28895 :   if (SrcMgr && SrcMgr->getNumBuffers())
      66        4739 :     MainFileName =
      67       18956 :         SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
      68       23941 : }
      69             : 
      70      452062 : MCContext::~MCContext() {
      71       23793 :   if (AutoReset)
      72        6322 :     reset();
      73             : 
      74             :   // NOTE: The symbols are all allocated out of a bump pointer allocator,
      75             :   // we don't need to free them here.
      76       23793 : }
      77             : 
      78             : //===----------------------------------------------------------------------===//
      79             : // Module Lifetime Management
      80             : //===----------------------------------------------------------------------===//
      81             : 
      82       23683 : void MCContext::reset() {
      83             :   // Call the destructors so the fragments are freed
      84       23683 :   COFFAllocator.DestroyAll();
      85       23683 :   ELFAllocator.DestroyAll();
      86       23683 :   MachOAllocator.DestroyAll();
      87             : 
      88       23683 :   MCSubtargetAllocator.DestroyAll();
      89       47366 :   UsedNames.clear();
      90       47366 :   Symbols.clear();
      91       23683 :   Allocator.Reset();
      92       23683 :   Instances.clear();
      93       47364 :   CompilationDir.clear();
      94       47364 :   MainFileName.clear();
      95       47365 :   MCDwarfLineTablesCUMap.clear();
      96       47366 :   SectionsForRanges.clear();
      97       47366 :   MCGenDwarfLabelEntries.clear();
      98       23683 :   DwarfDebugFlags = StringRef();
      99       23683 :   DwarfCompileUnitID = 0;
     100       23683 :   CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
     101             : 
     102       47366 :   CVContext.reset();
     103             : 
     104       23683 :   MachOUniquingMap.clear();
     105       47366 :   ELFUniquingMap.clear();
     106       47366 :   COFFUniquingMap.clear();
     107             : 
     108       23683 :   NextID.clear();
     109       23683 :   AllowTemporaryLabels = true;
     110       23683 :   DwarfLocSeen = false;
     111       23683 :   GenDwarfForAssembly = false;
     112       23683 :   GenDwarfFileNumber = 0;
     113             : 
     114       23683 :   HadError = false;
     115       23683 : }
     116             : 
     117             : //===----------------------------------------------------------------------===//
     118             : // Symbol Manipulation
     119             : //===----------------------------------------------------------------------===//
     120             : 
     121     1257179 : MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
     122     2514358 :   SmallString<128> NameSV;
     123     1257179 :   StringRef NameRef = Name.toStringRef(NameSV);
     124             : 
     125             :   assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
     126             : 
     127     2514358 :   MCSymbol *&Sym = Symbols[NameRef];
     128     1257179 :   if (!Sym)
     129      356639 :     Sym = createSymbol(NameRef, false, false);
     130             : 
     131     2514358 :   return Sym;
     132             : }
     133             : 
     134          28 : MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
     135             :                                                  unsigned Idx) {
     136         140 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
     137         112 :                            "$frame_escape_" + Twine(Idx));
     138             : }
     139             : 
     140          29 : MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
     141         145 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
     142          87 :                            "$parent_frame_offset");
     143             : }
     144             : 
     145          60 : MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
     146         300 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
     147         180 :                            FuncName);
     148             : }
     149             : 
     150     1706232 : MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
     151             :                                       bool IsTemporary) {
     152     1706232 :   if (MOFI) {
     153     1706186 :     switch (MOFI->getObjectFileType()) {
     154       27034 :     case MCObjectFileInfo::IsCOFF:
     155       54068 :       return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
     156     1593992 :     case MCObjectFileInfo::IsELF:
     157     3187984 :       return new (Name, *this) MCSymbolELF(Name, IsTemporary);
     158       85160 :     case MCObjectFileInfo::IsMachO:
     159       85160 :       return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
     160           0 :     case MCObjectFileInfo::IsWasm:
     161           0 :       return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
     162             :     }
     163             :   }
     164             :   return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
     165          46 :                                     IsTemporary);
     166             : }
     167             : 
     168     1706232 : MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
     169             :                                   bool CanBeUnnamed) {
     170     1706232 :   if (CanBeUnnamed && !UseNamesOnTempLabels)
     171     1127323 :     return createSymbolImpl(nullptr, true);
     172             : 
     173             :   // Determine whether this is a user written assembler temporary or normal
     174             :   // label, if used.
     175      578909 :   bool IsTemporary = CanBeUnnamed;
     176      578909 :   if (AllowTemporaryLabels && !IsTemporary)
     177      356870 :     IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
     178             : 
     179      578909 :   SmallString<128> NewName = Name;
     180      578909 :   bool AddSuffix = AlwaysAddSuffix;
     181     1157818 :   unsigned &NextUniqueID = NextID[Name];
     182             :   while (true) {
     183      578909 :     if (AddSuffix) {
     184      175801 :       NewName.resize(Name.size());
     185      527403 :       raw_svector_ostream(NewName) << NextUniqueID++;
     186             :     }
     187     2894905 :     auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
     188      579053 :     if (NameEntry.second || !NameEntry.first->second) {
     189             :       // Ok, we found a name.
     190             :       // Mark it as used for a non-section symbol.
     191      578909 :       NameEntry.first->second = true;
     192             :       // Have the MCSymbol object itself refer to the copy of the string that is
     193             :       // embedded in the UsedNames entry.
     194      578909 :       return createSymbolImpl(&*NameEntry.first, IsTemporary);
     195             :     }
     196             :     assert(IsTemporary && "Cannot rename non-temporary symbols");
     197             :     AddSuffix = true;
     198             :   }
     199             :   llvm_unreachable("Infinite loop");
     200             : }
     201             : 
     202     1349519 : MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
     203             :                                       bool CanBeUnnamed) {
     204     2699039 :   SmallString<128> NameSV;
     205     4048560 :   raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
     206     4048560 :   return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
     207             : }
     208             : 
     209          73 : MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
     210         146 :   SmallString<128> NameSV;
     211         219 :   raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
     212         146 :   return createSymbol(NameSV, true, false);
     213             : }
     214             : 
     215      823648 : MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
     216     1647296 :   return createTempSymbol("tmp", true, CanBeUnnamed);
     217             : }
     218             : 
     219         161 : unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
     220         322 :   MCLabel *&Label = Instances[LocalLabelVal];
     221         161 :   if (!Label)
     222         112 :     Label = new (*this) MCLabel(0);
     223         322 :   return Label->incInstance();
     224             : }
     225             : 
     226         174 : unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
     227         348 :   MCLabel *&Label = Instances[LocalLabelVal];
     228         174 :   if (!Label)
     229         138 :     Label = new (*this) MCLabel(0);
     230         348 :   return Label->getInstance();
     231             : }
     232             : 
     233         335 : MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
     234             :                                                        unsigned Instance) {
     235        1005 :   MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
     236         335 :   if (!Sym)
     237         171 :     Sym = createTempSymbol(false);
     238         335 :   return Sym;
     239             : }
     240             : 
     241         161 : MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
     242         161 :   unsigned Instance = NextInstance(LocalLabelVal);
     243         161 :   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
     244             : }
     245             : 
     246         174 : MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
     247             :                                                bool Before) {
     248         174 :   unsigned Instance = GetInstance(LocalLabelVal);
     249         174 :   if (!Before)
     250         118 :     ++Instance;
     251         174 :   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
     252             : }
     253             : 
     254        1537 : MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
     255        3074 :   SmallString<128> NameSV;
     256        1537 :   StringRef NameRef = Name.toStringRef(NameSV);
     257        3074 :   return Symbols.lookup(NameRef);
     258             : }
     259             : 
     260           2 : void MCContext::setSymbolValue(MCStreamer &Streamer,
     261             :                               StringRef Sym,
     262             :                               uint64_t Val) {
     263           2 :   auto Symbol = getOrCreateSymbol(Sym);
     264           2 :   Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
     265           2 : }
     266             : 
     267             : //===----------------------------------------------------------------------===//
     268             : // Section Management
     269             : //===----------------------------------------------------------------------===//
     270             : 
     271      142321 : MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
     272             :                                            unsigned TypeAndAttributes,
     273             :                                            unsigned Reserved2, SectionKind Kind,
     274             :                                            const char *BeginSymName) {
     275             :   // We unique sections by their segment/section pair.  The returned section
     276             :   // may not have the same flags as the requested section, if so this should be
     277             :   // diagnosed by the client as an error.
     278             : 
     279             :   // Form the name to look up.
     280      284642 :   SmallString<64> Name;
     281      142321 :   Name += Segment;
     282      142321 :   Name.push_back(',');
     283      142321 :   Name += Section;
     284             : 
     285             :   // Do the lookup, if we have a hit, return it.
     286      284642 :   MCSectionMachO *&Entry = MachOUniquingMap[Name];
     287      142321 :   if (Entry)
     288             :     return Entry;
     289             : 
     290      141240 :   MCSymbol *Begin = nullptr;
     291      141240 :   if (BeginSymName)
     292       37452 :     Begin = createTempSymbol(BeginSymName, false);
     293             : 
     294             :   // Otherwise, return a new section.
     295      423720 :   return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
     296      141240 :              Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
     297             : }
     298             : 
     299           5 : void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
     300           5 :   StringRef GroupName;
     301           5 :   if (const MCSymbol *Group = Section->getGroup())
     302             :     GroupName = Group->getName();
     303             : 
     304           5 :   unsigned UniqueID = Section->getUniqueID();
     305          10 :   ELFUniquingMap.erase(
     306          15 :       ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
     307          20 :   auto I = ELFUniquingMap.insert(std::make_pair(
     308          10 :                                      ELFSectionKey{Name, GroupName, UniqueID},
     309           5 :                                      Section))
     310           5 :                .first;
     311          10 :   StringRef CachedName = I->first.SectionName;
     312           5 :   const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
     313           5 : }
     314             : 
     315     1234205 : MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
     316             :                                               unsigned Flags, SectionKind K,
     317             :                                               unsigned EntrySize,
     318             :                                               const MCSymbolELF *Group,
     319             :                                               unsigned UniqueID,
     320             :                                               const MCSymbolELF *Associated) {
     321             :   MCSymbolELF *R;
     322     2468410 :   MCSymbol *&Sym = Symbols[Section];
     323             :   // A section symbol can not redefine regular symbols. There may be multiple
     324             :   // sections with the same name, in which case the first such section wins.
     325     1243102 :   if (Sym && Sym->isDefined() &&
     326       13329 :       (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
     327           1 :     reportError(SMLoc(), "invalid symbol redefinition");
     328     1238659 :   if (Sym && Sym->isUndefined()) {
     329          22 :     R = cast<MCSymbolELF>(Sym);
     330             :   } else {
     331     3702582 :     auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
     332     2468388 :     R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
     333     1234194 :     if (!Sym)
     334     1229751 :       Sym = R;
     335             :   }
     336     1234205 :   R->setBinding(ELF::STB_LOCAL);
     337     1234205 :   R->setType(ELF::STT_SECTION);
     338             : 
     339     1234205 :   auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
     340     3702615 :       Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
     341             : 
     342     2468410 :   auto *F = new MCDataFragment();
     343     3702615 :   Ret->getFragmentList().insert(Ret->begin(), F);
     344     2468410 :   F->setParent(Ret);
     345     2468410 :   R->setFragment(F);
     346             : 
     347     1234205 :   return Ret;
     348             : }
     349             : 
     350       12836 : MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
     351             :                                              unsigned Flags, unsigned EntrySize,
     352             :                                              const MCSymbolELF *Group,
     353             :                                              const MCSectionELF *RelInfoSection) {
     354       12836 :   StringMap<bool>::iterator I;
     355             :   bool Inserted;
     356       12836 :   std::tie(I, Inserted) =
     357      102688 :       RelSecNames.insert(std::make_pair(Name.str(), true));
     358             : 
     359       77016 :   return createELFSectionImpl(
     360             :       I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
     361       12836 :       true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
     362             : }
     363             : 
     364          84 : MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
     365             :                                             const Twine &Suffix, unsigned Type,
     366             :                                             unsigned Flags,
     367             :                                             unsigned EntrySize) {
     368         336 :   return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
     369             : }
     370             : 
     371     1400295 : MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
     372             :                                        unsigned Flags, unsigned EntrySize,
     373             :                                        const Twine &Group, unsigned UniqueID,
     374             :                                        const MCSymbolELF *Associated) {
     375     1400295 :   MCSymbolELF *GroupSym = nullptr;
     376     3825846 :   if (!Group.isTriviallyEmpty() && !Group.str().empty())
     377        9112 :     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
     378             : 
     379             :   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
     380     1400295 :                        Associated);
     381             : }
     382             : 
     383     1401055 : MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
     384             :                                        unsigned Flags, unsigned EntrySize,
     385             :                                        const MCSymbolELF *GroupSym,
     386             :                                        unsigned UniqueID,
     387             :                                        const MCSymbolELF *Associated) {
     388     1401055 :   StringRef Group = "";
     389     1401055 :   if (GroupSym)
     390        4562 :     Group = GroupSym->getName();
     391             :   // Do the lookup, if we have a hit, return it.
     392             :   auto IterBool = ELFUniquingMap.insert(
     393    11208439 :       std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
     394     1401055 :   auto &Entry = *IterBool.first;
     395     1401055 :   if (!IterBool.second)
     396      184095 :     return Entry.second;
     397             : 
     398     2433920 :   StringRef CachedName = Entry.first.SectionName;
     399             : 
     400             :   SectionKind Kind;
     401     1216960 :   if (Flags & ELF::SHF_ARM_PURECODE)
     402         436 :     Kind = SectionKind::getExecuteOnly();
     403     1216524 :   else if (Flags & ELF::SHF_EXECINSTR)
     404       25225 :     Kind = SectionKind::getText();
     405             :   else
     406     1191299 :     Kind = SectionKind::getReadOnly();
     407             : 
     408             :   MCSectionELF *Result = createELFSectionImpl(
     409     1216960 :       CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
     410     1216960 :   Entry.second = Result;
     411     1216960 :   return Result;
     412             : }
     413             : 
     414        4409 : MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
     415       13227 :   return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
     416             :                               SectionKind::getReadOnly(), 4, Group, ~0,
     417        8818 :                               nullptr);
     418             : }
     419             : 
     420       33959 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
     421             :                                          unsigned Characteristics,
     422             :                                          SectionKind Kind,
     423             :                                          StringRef COMDATSymName, int Selection,
     424             :                                          unsigned UniqueID,
     425             :                                          const char *BeginSymName) {
     426       33959 :   MCSymbol *COMDATSymbol = nullptr;
     427       33959 :   if (!COMDATSymName.empty()) {
     428         507 :     COMDATSymbol = getOrCreateSymbol(COMDATSymName);
     429         507 :     COMDATSymName = COMDATSymbol->getName();
     430             :   }
     431             : 
     432             : 
     433             :   // Do the lookup, if we have a hit, return it.
     434       67918 :   COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
     435      135836 :   auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
     436       33959 :   auto Iter = IterBool.first;
     437       33959 :   if (!IterBool.second)
     438         368 :     return Iter->second;
     439             : 
     440       33591 :   MCSymbol *Begin = nullptr;
     441       33591 :   if (BeginSymName)
     442       14630 :     Begin = createTempSymbol(BeginSymName, false);
     443             : 
     444       67182 :   StringRef CachedName = Iter->first.SectionName;
     445       33591 :   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
     446      100773 :       CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
     447             : 
     448       33591 :   Iter->second = Result;
     449       33591 :   return Result;
     450             : }
     451             : 
     452       33160 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
     453             :                                          unsigned Characteristics,
     454             :                                          SectionKind Kind,
     455             :                                          const char *BeginSymName) {
     456       66320 :   return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
     457       66320 :                         BeginSymName);
     458             : }
     459             : 
     460           0 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
     461           0 :   COFFSectionKey T{Section, "", 0, GenericSectionID};
     462           0 :   auto Iter = COFFUniquingMap.find(T);
     463           0 :   if (Iter == COFFUniquingMap.end())
     464             :     return nullptr;
     465           0 :   return Iter->second;
     466             : }
     467             : 
     468         494 : MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
     469             :                                                     const MCSymbol *KeySym,
     470             :                                                     unsigned UniqueID) {
     471             :   // Return the normal section if we don't have to be associative or unique.
     472         494 :   if (!KeySym && UniqueID == GenericSectionID)
     473             :     return Sec;
     474             : 
     475             :   // If we have a key symbol, make an associative section with the same name and
     476             :   // kind as the normal section.
     477         105 :   unsigned Characteristics = Sec->getCharacteristics();
     478         105 :   if (KeySym) {
     479          85 :     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
     480          85 :     return getCOFFSection(Sec->getSectionName(), Characteristics,
     481             :                           Sec->getKind(), KeySym->getName(),
     482          85 :                           COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
     483             :   }
     484             : 
     485          40 :   return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
     486          20 :                         "", 0, UniqueID);
     487             : }
     488             : 
     489           0 : MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
     490             :                                          const Twine &Group, unsigned UniqueID,
     491             :                                          const char *BeginSymName) {
     492           0 :   MCSymbolWasm *GroupSym = nullptr;
     493           0 :   if (!Group.isTriviallyEmpty() && !Group.str().empty())
     494           0 :     GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
     495             : 
     496           0 :   return getWasmSection(Section, Type, GroupSym, UniqueID, BeginSymName);
     497             : }
     498             : 
     499           0 : MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
     500             :                                          const MCSymbolWasm *GroupSym,
     501             :                                          unsigned UniqueID,
     502             :                                          const char *BeginSymName) {
     503           0 :   StringRef Group = "";
     504           0 :   if (GroupSym)
     505           0 :     Group = GroupSym->getName();
     506             :   // Do the lookup, if we have a hit, return it.
     507             :   auto IterBool = WasmUniquingMap.insert(
     508           0 :       std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
     509           0 :   auto &Entry = *IterBool.first;
     510           0 :   if (!IterBool.second)
     511           0 :     return Entry.second;
     512             : 
     513           0 :   StringRef CachedName = Entry.first.SectionName;
     514             : 
     515           0 :   SectionKind Kind = SectionKind::getText();
     516             : 
     517           0 :   MCSymbol *Begin = nullptr;
     518           0 :   if (BeginSymName)
     519           0 :     Begin = createTempSymbol(BeginSymName, false);
     520             : 
     521           0 :   MCSectionWasm *Result = new (WasmAllocator.Allocate())
     522           0 :       MCSectionWasm(CachedName, Type, Kind, GroupSym, UniqueID, Begin);
     523           0 :   Entry.second = Result;
     524           0 :   return Result;
     525             : }
     526             : 
     527         777 : MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
     528        1554 :   return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
     529             : }
     530             : 
     531             : //===----------------------------------------------------------------------===//
     532             : // Dwarf Management
     533             : //===----------------------------------------------------------------------===//
     534             : 
     535             : /// getDwarfFile - takes a file name an number to place in the dwarf file and
     536             : /// directory tables.  If the file number has already been allocated it is an
     537             : /// error and zero is returned and the client reports the error, else the
     538             : /// allocated file number is returned.  The file numbers may be in any order.
     539      550636 : unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
     540             :                                  unsigned FileNumber, unsigned CUID) {
     541      550636 :   MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
     542      550636 :   return Table.getFile(Directory, FileName, FileNumber);
     543             : }
     544             : 
     545             : /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
     546             : /// currently is assigned and false otherwise.
     547          84 : bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
     548          84 :   const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
     549         168 :   if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
     550             :     return false;
     551             : 
     552         252 :   return !MCDwarfFiles[FileNumber].Name.empty();
     553             : }
     554             : 
     555             : /// Remove empty sections from SectionStartEndSyms, to avoid generating
     556             : /// useless debug info for them.
     557          29 : void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
     558          29 :   SectionsForRanges.remove_if(
     559          36 :       [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
     560          29 : }
     561             : 
     562     8338146 : CodeViewContext &MCContext::getCVContext() {
     563    16676292 :   if (!CVContext.get())
     564        3730 :     CVContext.reset(new CodeViewContext);
     565    16676292 :   return *CVContext.get();
     566             : }
     567             : 
     568             : //===----------------------------------------------------------------------===//
     569             : // Error Reporting
     570             : //===----------------------------------------------------------------------===//
     571             : 
     572         302 : void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
     573         302 :   HadError = true;
     574             : 
     575             :   // If we have a source manager use it. Otherwise, try using the inline source
     576             :   // manager.
     577             :   // If that fails, use the generic report_fatal_error().
     578         302 :   if (SrcMgr)
     579         891 :     SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
     580           5 :   else if (InlineSrcMgr)
     581          12 :     InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
     582             :   else
     583           1 :     report_fatal_error(Msg, false);
     584         301 : }
     585             : 
     586           4 : void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
     587           4 :   reportError(Loc, Msg);
     588             : 
     589             :   // If we reached here, we are failing ungracefully. Run the interrupt handlers
     590             :   // to make sure any special cleanups get done, in particular that we remove
     591             :   // files registered with RemoveFileOnSignal.
     592           4 :   sys::RunInterruptHandlers();
     593           4 :   exit(1);
     594      216990 : }

Generated by: LCOV version 1.13