LCOV - code coverage report
Current view: top level - lib/MC - MCContext.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 218 237 92.0 %
Date: 2018-02-20 16:54:40 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       97326 : AsSecureLogFileName("as-secure-log-file-name",
      52       97326 :         cl::desc("As secure log file name (initialized from "
      53             :                  "AS_SECURE_LOG_FILE env variable)"),
      54      291978 :         cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
      55             : 
      56       29124 : MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
      57             :                      const MCObjectFileInfo *mofi, const SourceMgr *mgr,
      58       29124 :                      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      232992 :       AutoReset(DoAutoReset) {
      63       29123 :   SecureLogFile = AsSecureLogFileName;
      64             : 
      65       34858 :   if (SrcMgr && SrcMgr->getNumBuffers())
      66        5511 :     MainFileName =
      67       11022 :         SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
      68       29123 : }
      69             : 
      70      318416 : MCContext::~MCContext() {
      71       28947 :   if (AutoReset)
      72        7411 :     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       28946 : }
      77             : 
      78             : //===----------------------------------------------------------------------===//
      79             : // Module Lifetime Management
      80             : //===----------------------------------------------------------------------===//
      81             : 
      82       28829 : void MCContext::reset() {
      83             :   // Call the destructors so the fragments are freed
      84       28829 :   COFFAllocator.DestroyAll();
      85       28829 :   ELFAllocator.DestroyAll();
      86       28829 :   MachOAllocator.DestroyAll();
      87             : 
      88       28829 :   MCSubtargetAllocator.DestroyAll();
      89             :   UsedNames.clear();
      90             :   Symbols.clear();
      91       28829 :   Allocator.Reset();
      92       28829 :   Instances.clear();
      93             :   CompilationDir.clear();
      94             :   MainFileName.clear();
      95             :   MCDwarfLineTablesCUMap.clear();
      96             :   SectionsForRanges.clear();
      97             :   MCGenDwarfLabelEntries.clear();
      98       28828 :   DwarfDebugFlags = StringRef();
      99       28828 :   DwarfCompileUnitID = 0;
     100       28828 :   CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
     101             : 
     102             :   CVContext.reset();
     103             : 
     104       28828 :   MachOUniquingMap.clear();
     105             :   ELFUniquingMap.clear();
     106             :   COFFUniquingMap.clear();
     107             : 
     108       28829 :   NextID.clear();
     109       28829 :   AllowTemporaryLabels = true;
     110       28829 :   DwarfLocSeen = false;
     111       28829 :   GenDwarfForAssembly = false;
     112       28829 :   GenDwarfFileNumber = 0;
     113             : 
     114       28829 :   HadError = false;
     115       28829 : }
     116             : 
     117             : //===----------------------------------------------------------------------===//
     118             : // Symbol Manipulation
     119             : //===----------------------------------------------------------------------===//
     120             : 
     121     1619139 : MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
     122             :   SmallString<128> NameSV;
     123     1619139 :   StringRef NameRef = Name.toStringRef(NameSV);
     124             : 
     125             :   assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
     126             : 
     127     1619138 :   MCSymbol *&Sym = Symbols[NameRef];
     128     1619139 :   if (!Sym)
     129      512421 :     Sym = createSymbol(NameRef, false, false);
     130             : 
     131     3238278 :   return Sym;
     132             : }
     133             : 
     134          28 : MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
     135             :                                                  unsigned Idx) {
     136          56 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
     137          56 :                            "$frame_escape_" + Twine(Idx));
     138             : }
     139             : 
     140          29 : MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
     141          58 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
     142          58 :                            "$parent_frame_offset");
     143             : }
     144             : 
     145          64 : MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
     146         128 :   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
     147         128 :                            FuncName);
     148             : }
     149             : 
     150     3274545 : MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
     151             :                                       bool IsTemporary) {
     152     3274545 :   if (MOFI) {
     153     3274498 :     switch (MOFI->getObjectFileType()) {
     154       31262 :     case MCObjectFileInfo::IsCOFF:
     155       62524 :       return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
     156     3149380 :     case MCObjectFileInfo::IsELF:
     157     6298760 :       return new (Name, *this) MCSymbolELF(Name, IsTemporary);
     158       93856 :     case MCObjectFileInfo::IsMachO:
     159       93856 :       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          47 :                                     IsTemporary);
     166             : }
     167             : 
     168     3274545 : MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
     169             :                                   bool CanBeUnnamed) {
     170     3274545 :   if (CanBeUnnamed && !UseNamesOnTempLabels)
     171     2538349 :     return createSymbolImpl(nullptr, true);
     172             : 
     173             :   // Determine whether this is a user written assembler temporary or normal
     174             :   // label, if used.
     175             :   bool IsTemporary = CanBeUnnamed;
     176      736196 :   if (AllowTemporaryLabels && !IsTemporary)
     177      512657 :     IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
     178             : 
     179             :   SmallString<128> NewName = Name;
     180             :   bool AddSuffix = AlwaysAddSuffix;
     181      736196 :   unsigned &NextUniqueID = NextID[Name];
     182             :   while (true) {
     183      736196 :     if (AddSuffix) {
     184      169965 :       NewName.resize(Name.size());
     185      339930 :       raw_svector_ostream(NewName) << NextUniqueID++;
     186             :     }
     187     1473270 :     auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
     188      737074 :     if (NameEntry.second || !NameEntry.first->second) {
     189             :       // Ok, we found a name.
     190             :       // Mark it as used for a non-section symbol.
     191      736196 :       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      736196 :       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     2762045 : MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
     203             :                                       bool CanBeUnnamed) {
     204             :   SmallString<128> NameSV;
     205     5524092 :   raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
     206     8286138 :   return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
     207             : }
     208             : 
     209          78 : MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
     210             :   SmallString<128> NameSV;
     211         234 :   raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
     212         156 :   return createSymbol(NameSV, true, false);
     213             : }
     214             : 
     215     1454395 : MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
     216     2908790 :   return createTempSymbol("tmp", true, CanBeUnnamed);
     217             : }
     218             : 
     219         161 : unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
     220         161 :   MCLabel *&Label = Instances[LocalLabelVal];
     221         161 :   if (!Label)
     222          55 :     Label = new (*this) MCLabel(0);
     223         322 :   return Label->incInstance();
     224             : }
     225             : 
     226         175 : unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
     227         175 :   MCLabel *&Label = Instances[LocalLabelVal];
     228         175 :   if (!Label)
     229          70 :     Label = new (*this) MCLabel(0);
     230         350 :   return Label->getInstance();
     231             : }
     232             : 
     233         336 : MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
     234             :                                                        unsigned Instance) {
     235         672 :   MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
     236         336 :   if (!Sym)
     237         171 :     Sym = createTempSymbol(false);
     238         336 :   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         175 : MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
     247             :                                                bool Before) {
     248         175 :   unsigned Instance = GetInstance(LocalLabelVal);
     249         175 :   if (!Before)
     250         119 :     ++Instance;
     251         175 :   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
     252             : }
     253             : 
     254        1726 : MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
     255             :   SmallString<128> NameSV;
     256        1726 :   StringRef NameRef = Name.toStringRef(NameSV);
     257        3452 :   return Symbols.lookup(NameRef);
     258             : }
     259             : 
     260           4 : void MCContext::setSymbolValue(MCStreamer &Streamer,
     261             :                               StringRef Sym,
     262             :                               uint64_t Val) {
     263           4 :   auto Symbol = getOrCreateSymbol(Sym);
     264           4 :   Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
     265           4 : }
     266             : 
     267             : //===----------------------------------------------------------------------===//
     268             : // Section Management
     269             : //===----------------------------------------------------------------------===//
     270             : 
     271      158344 : 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             :   SmallString<64> Name;
     281             :   Name += Segment;
     282      158345 :   Name.push_back(',');
     283             :   Name += Section;
     284             : 
     285             :   // Do the lookup, if we have a hit, return it.
     286      158344 :   MCSectionMachO *&Entry = MachOUniquingMap[Name];
     287      158345 :   if (Entry)
     288             :     return Entry;
     289             : 
     290             :   MCSymbol *Begin = nullptr;
     291      156867 :   if (BeginSymName)
     292       43199 :     Begin = createTempSymbol(BeginSymName, false);
     293             : 
     294             :   // Otherwise, return a new section.
     295      313734 :   return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
     296      156865 :              Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
     297             : }
     298             : 
     299           7 : void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
     300             :   StringRef GroupName;
     301           7 :   if (const MCSymbol *Group = Section->getGroup())
     302             :     GroupName = Group->getName();
     303             : 
     304           7 :   unsigned UniqueID = Section->getUniqueID();
     305             :   ELFUniquingMap.erase(
     306           7 :       ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
     307           7 :   auto I = ELFUniquingMap.insert(std::make_pair(
     308           7 :                                      ELFSectionKey{Name, GroupName, UniqueID},
     309             :                                      Section))
     310             :                .first;
     311             :   StringRef CachedName = I->first.SectionName;
     312             :   const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
     313           7 : }
     314             : 
     315     1594360 : 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     1594360 :   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     1663046 :   if (Sym && Sym->isDefined() &&
     326      103014 :       (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
     327           1 :     reportError(SMLoc(), "invalid symbol redefinition");
     328     1628708 :   if (Sym && Sym->isUndefined()) {
     329          11 :     R = cast<MCSymbolELF>(Sym);
     330             :   } else {
     331     1594348 :     auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
     332     1594348 :     R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
     333     1594348 :     if (!Sym)
     334     1560011 :       Sym = R;
     335             :   }
     336     1594359 :   R->setBinding(ELF::STB_LOCAL);
     337     1594357 :   R->setType(ELF::STT_SECTION);
     338             : 
     339             :   auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
     340     1594357 :       Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
     341             : 
     342     1594356 :   auto *F = new MCDataFragment();
     343             :   Ret->getFragmentList().insert(Ret->begin(), F);
     344             :   F->setParent(Ret);
     345             :   R->setFragment(F);
     346             : 
     347     1594358 :   return Ret;
     348             : }
     349             : 
     350       48494 : MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
     351             :                                              unsigned Flags, unsigned EntrySize,
     352             :                                              const MCSymbolELF *Group,
     353             :                                              const MCSectionELF *RelInfoSection) {
     354       48494 :   StringMap<bool>::iterator I;
     355             :   bool Inserted;
     356             :   std::tie(I, Inserted) =
     357      145482 :       RelSecNames.insert(std::make_pair(Name.str(), true));
     358             : 
     359             :   return createELFSectionImpl(
     360             :       I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
     361       48494 :       true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
     362             : }
     363             : 
     364          87 : MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
     365             :                                             const Twine &Suffix, unsigned Type,
     366             :                                             unsigned Flags,
     367             :                                             unsigned EntrySize) {
     368          87 :   return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
     369             : }
     370             : 
     371     1750541 : MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
     372             :                                        unsigned Flags, unsigned EntrySize,
     373             :                                        const Twine &Group, unsigned UniqueID,
     374             :                                        const MCSymbolELF *Associated) {
     375             :   MCSymbolELF *GroupSym = nullptr;
     376     2928227 :   if (!Group.isTriviallyEmpty() && !Group.str().empty())
     377       35155 :     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
     378             : 
     379             :   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
     380     1750541 :                        Associated);
     381             : }
     382             : 
     383     1751341 : 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             :   StringRef Group = "";
     389     1751341 :   if (GroupSym)
     390             :     Group = GroupSym->getName();
     391             :   // Do the lookup, if we have a hit, return it.
     392             :   auto IterBool = ELFUniquingMap.insert(
     393     7005367 :       std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
     394             :   auto &Entry = *IterBool.first;
     395     1751342 :   if (!IterBool.second)
     396      240462 :     return Entry.second;
     397             : 
     398             :   StringRef CachedName = Entry.first.SectionName;
     399             : 
     400             :   SectionKind Kind;
     401     1510880 :   if (Flags & ELF::SHF_ARM_PURECODE)
     402         436 :     Kind = SectionKind::getExecuteOnly();
     403     1510444 :   else if (Flags & ELF::SHF_EXECINSTR)
     404       60214 :     Kind = SectionKind::getText();
     405             :   else
     406     1450230 :     Kind = SectionKind::getReadOnly();
     407             : 
     408             :   MCSectionELF *Result = createELFSectionImpl(
     409     1510880 :       CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
     410     1510879 :   Entry.second = Result;
     411     1510879 :   return Result;
     412             : }
     413             : 
     414       34986 : MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
     415       34986 :   return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
     416             :                               SectionKind::getReadOnly(), 4, Group, ~0,
     417       69972 :                               nullptr);
     418             : }
     419             : 
     420       42338 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
     421             :                                          unsigned Characteristics,
     422             :                                          SectionKind Kind,
     423             :                                          StringRef COMDATSymName, int Selection,
     424             :                                          unsigned UniqueID,
     425             :                                          const char *BeginSymName) {
     426             :   MCSymbol *COMDATSymbol = nullptr;
     427       42338 :   if (!COMDATSymName.empty()) {
     428         593 :     COMDATSymbol = getOrCreateSymbol(COMDATSymName);
     429         593 :     COMDATSymName = COMDATSymbol->getName();
     430             :   }
     431             : 
     432             : 
     433             :   // Do the lookup, if we have a hit, return it.
     434             :   COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
     435       42338 :   auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
     436             :   auto Iter = IterBool.first;
     437       42338 :   if (!IterBool.second)
     438         444 :     return Iter->second;
     439             : 
     440             :   MCSymbol *Begin = nullptr;
     441       41894 :   if (BeginSymName)
     442       17980 :     Begin = createTempSymbol(BeginSymName, false);
     443             : 
     444             :   StringRef CachedName = Iter->first.SectionName;
     445             :   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
     446       41894 :       CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
     447             : 
     448       41894 :   Iter->second = Result;
     449       41894 :   return Result;
     450             : }
     451             : 
     452       41392 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
     453             :                                          unsigned Characteristics,
     454             :                                          SectionKind Kind,
     455             :                                          const char *BeginSymName) {
     456       41392 :   return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
     457       82784 :                         BeginSymName);
     458             : }
     459             : 
     460           0 : MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
     461             :   COFFSectionKey T{Section, "", 0, GenericSectionID};
     462             :   auto Iter = COFFUniquingMap.find(T);
     463           0 :   if (Iter == COFFUniquingMap.end())
     464             :     return nullptr;
     465           0 :   return Iter->second;
     466             : }
     467             : 
     468         609 : 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         609 :   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         129 :   unsigned Characteristics = Sec->getCharacteristics();
     478         129 :   if (KeySym) {
     479         104 :     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
     480             :     return getCOFFSection(Sec->getSectionName(), Characteristics,
     481             :                           Sec->getKind(), KeySym->getName(),
     482         104 :                           COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
     483             :   }
     484             : 
     485          25 :   return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
     486          25 :                         "", 0, UniqueID);
     487             : }
     488             : 
     489           0 : MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
     490             :                                          const Twine &Group, unsigned UniqueID,
     491             :                                          const char *BeginSymName) {
     492             :   MCSymbolWasm *GroupSym = nullptr;
     493           0 :   if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
     494           0 :     GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
     495             :     GroupSym->setComdat(true);
     496             :   }
     497             : 
     498           0 :   return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
     499             : }
     500             : 
     501           0 : MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
     502             :                                          const MCSymbolWasm *GroupSym,
     503             :                                          unsigned UniqueID,
     504             :                                          const char *BeginSymName) {
     505             :   StringRef Group = "";
     506           0 :   if (GroupSym)
     507             :     Group = GroupSym->getName();
     508             :   // Do the lookup, if we have a hit, return it.
     509             :   auto IterBool = WasmUniquingMap.insert(
     510           0 :       std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
     511             :   auto &Entry = *IterBool.first;
     512           0 :   if (!IterBool.second)
     513           0 :     return Entry.second;
     514             : 
     515             :   StringRef CachedName = Entry.first.SectionName;
     516             : 
     517             :   MCSymbol *Begin = nullptr;
     518           0 :   if (BeginSymName)
     519           0 :     Begin = createTempSymbol(BeginSymName, false);
     520             : 
     521             :   MCSectionWasm *Result = new (WasmAllocator.Allocate())
     522           0 :       MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
     523           0 :   Entry.second = Result;
     524           0 :   return Result;
     525             : }
     526             : 
     527         893 : MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
     528         893 :   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     1480606 : unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
     540             :                                  unsigned FileNumber, MD5::MD5Result *Checksum,
     541             :                                  unsigned CUID) {
     542     1480606 :   MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
     543     1480606 :   return Table.getFile(Directory, FileName, Checksum, FileNumber);
     544             : }
     545             : 
     546             : /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
     547             : /// currently is assigned and false otherwise.
     548         295 : bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
     549             :   const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
     550         590 :   if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
     551             :     return false;
     552             : 
     553         295 :   return !MCDwarfFiles[FileNumber].Name.empty();
     554             : }
     555             : 
     556             : /// Remove empty sections from SectionStartEndSyms, to avoid generating
     557             : /// useless debug info for them.
     558          28 : void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
     559          28 :   SectionsForRanges.remove_if(
     560          35 :       [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
     561          28 : }
     562             : 
     563    18853959 : CodeViewContext &MCContext::getCVContext() {
     564    18853959 :   if (!CVContext.get())
     565        6487 :     CVContext.reset(new CodeViewContext);
     566    18853958 :   return *CVContext.get();
     567             : }
     568             : 
     569             : //===----------------------------------------------------------------------===//
     570             : // Error Reporting
     571             : //===----------------------------------------------------------------------===//
     572             : 
     573         318 : void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
     574         318 :   HadError = true;
     575             : 
     576             :   // If we have a source manager use it. Otherwise, try using the inline source
     577             :   // manager.
     578             :   // If that fails, use the generic report_fatal_error().
     579         318 :   if (SrcMgr)
     580         313 :     SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
     581           5 :   else if (InlineSrcMgr)
     582           4 :     InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
     583             :   else
     584           1 :     report_fatal_error(Msg, false);
     585         317 : }
     586             : 
     587           4 : void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
     588           4 :   reportError(Loc, Msg);
     589             : 
     590             :   // If we reached here, we are failing ungracefully. Run the interrupt handlers
     591             :   // to make sure any special cleanups get done, in particular that we remove
     592             :   // files registered with RemoveFileOnSignal.
     593           4 :   sys::RunInterruptHandlers();
     594           4 :   exit(1);
     595      291978 : }

Generated by: LCOV version 1.13