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

Generated by: LCOV version 1.13