1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCContext.h"
10 #include "llvm/ADT/Optional.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/BinaryFormat/XCOFF.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/MCSectionXCOFF.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCSymbolCOFF.h"
34 #include "llvm/MC/MCSymbolELF.h"
35 #include "llvm/MC/MCSymbolMachO.h"
36 #include "llvm/MC/MCSymbolWasm.h"
37 #include "llvm/MC/MCSymbolXCOFF.h"
38 #include "llvm/MC/SectionKind.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/Signals.h"
45 #include "llvm/Support/SourceMgr.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <cassert>
48 #include <cstdlib>
49 #include <tuple>
50 #include <utility>
51 
52 using namespace llvm;
53 
54 static cl::opt<char*>
55 AsSecureLogFileName("as-secure-log-file-name",
56         cl::desc("As secure log file name (initialized from "
57                  "AS_SECURE_LOG_FILE env variable)"),
58         cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
59 
60 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
61                      const MCObjectFileInfo *mofi, const SourceMgr *mgr,
62                      MCTargetOptions const *TargetOpts, bool DoAutoReset)
63     : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
64       Symbols(Allocator), UsedNames(Allocator),
65       InlineAsmUsedLabelNames(Allocator),
66       CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
67       AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
68   SecureLogFile = AsSecureLogFileName;
69 
70   if (SrcMgr && SrcMgr->getNumBuffers())
71     MainFileName =
72         SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
73 }
74 
75 MCContext::~MCContext() {
76   if (AutoReset)
77     reset();
78 
79   // NOTE: The symbols are all allocated out of a bump pointer allocator,
80   // we don't need to free them here.
81 }
82 
83 //===----------------------------------------------------------------------===//
84 // Module Lifetime Management
85 //===----------------------------------------------------------------------===//
86 
87 void MCContext::reset() {
88   // Call the destructors so the fragments are freed
89   COFFAllocator.DestroyAll();
90   ELFAllocator.DestroyAll();
91   MachOAllocator.DestroyAll();
92   XCOFFAllocator.DestroyAll();
93 
94   MCSubtargetAllocator.DestroyAll();
95   InlineAsmUsedLabelNames.clear();
96   UsedNames.clear();
97   Symbols.clear();
98   Allocator.Reset();
99   Instances.clear();
100   CompilationDir.clear();
101   MainFileName.clear();
102   MCDwarfLineTablesCUMap.clear();
103   SectionsForRanges.clear();
104   MCGenDwarfLabelEntries.clear();
105   DwarfDebugFlags = StringRef();
106   DwarfCompileUnitID = 0;
107   CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
108 
109   CVContext.reset();
110 
111   MachOUniquingMap.clear();
112   ELFUniquingMap.clear();
113   COFFUniquingMap.clear();
114   WasmUniquingMap.clear();
115   XCOFFUniquingMap.clear();
116 
117   NextID.clear();
118   AllowTemporaryLabels = true;
119   DwarfLocSeen = false;
120   GenDwarfForAssembly = false;
121   GenDwarfFileNumber = 0;
122 
123   HadError = false;
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // Symbol Manipulation
128 //===----------------------------------------------------------------------===//
129 
130 MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
131   SmallString<128> NameSV;
132   StringRef NameRef = Name.toStringRef(NameSV);
133 
134   assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
135 
136   MCSymbol *&Sym = Symbols[NameRef];
137   if (!Sym)
138     Sym = createSymbol(NameRef, false, false);
139 
140   return Sym;
141 }
142 
143 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
144                                                  unsigned Idx) {
145   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
146                            "$frame_escape_" + Twine(Idx));
147 }
148 
149 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
150   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
151                            "$parent_frame_offset");
152 }
153 
154 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
155   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
156                            FuncName);
157 }
158 
159 MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
160                                       bool IsTemporary) {
161   if (MOFI) {
162     switch (MOFI->getObjectFileType()) {
163     case MCObjectFileInfo::IsCOFF:
164       return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
165     case MCObjectFileInfo::IsELF:
166       return new (Name, *this) MCSymbolELF(Name, IsTemporary);
167     case MCObjectFileInfo::IsMachO:
168       return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
169     case MCObjectFileInfo::IsWasm:
170       return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
171     case MCObjectFileInfo::IsXCOFF:
172       return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
173     }
174   }
175   return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
176                                     IsTemporary);
177 }
178 
179 MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
180                                   bool CanBeUnnamed) {
181   if (CanBeUnnamed && !UseNamesOnTempLabels)
182     return createSymbolImpl(nullptr, true);
183 
184   // Determine whether this is a user written assembler temporary or normal
185   // label, if used.
186   bool IsTemporary = CanBeUnnamed;
187   if (AllowTemporaryLabels && !IsTemporary)
188     IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
189 
190   SmallString<128> NewName = Name;
191   bool AddSuffix = AlwaysAddSuffix;
192   unsigned &NextUniqueID = NextID[Name];
193   while (true) {
194     if (AddSuffix) {
195       NewName.resize(Name.size());
196       raw_svector_ostream(NewName) << NextUniqueID++;
197     }
198     auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
199     if (NameEntry.second || !NameEntry.first->second) {
200       // Ok, we found a name.
201       // Mark it as used for a non-section symbol.
202       NameEntry.first->second = true;
203       // Have the MCSymbol object itself refer to the copy of the string that is
204       // embedded in the UsedNames entry.
205       return createSymbolImpl(&*NameEntry.first, IsTemporary);
206     }
207     assert(IsTemporary && "Cannot rename non-temporary symbols");
208     AddSuffix = true;
209   }
210   llvm_unreachable("Infinite loop");
211 }
212 
213 MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
214                                       bool CanBeUnnamed) {
215   SmallString<128> NameSV;
216   raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
217   return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
218 }
219 
220 MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
221   SmallString<128> NameSV;
222   raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
223   return createSymbol(NameSV, true, false);
224 }
225 
226 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
227   return createTempSymbol("tmp", true, CanBeUnnamed);
228 }
229 
230 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
231   MCLabel *&Label = Instances[LocalLabelVal];
232   if (!Label)
233     Label = new (*this) MCLabel(0);
234   return Label->incInstance();
235 }
236 
237 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
238   MCLabel *&Label = Instances[LocalLabelVal];
239   if (!Label)
240     Label = new (*this) MCLabel(0);
241   return Label->getInstance();
242 }
243 
244 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
245                                                        unsigned Instance) {
246   MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
247   if (!Sym)
248     Sym = createTempSymbol(false);
249   return Sym;
250 }
251 
252 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
253   unsigned Instance = NextInstance(LocalLabelVal);
254   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
255 }
256 
257 MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
258                                                bool Before) {
259   unsigned Instance = GetInstance(LocalLabelVal);
260   if (!Before)
261     ++Instance;
262   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
263 }
264 
265 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
266   SmallString<128> NameSV;
267   StringRef NameRef = Name.toStringRef(NameSV);
268   return Symbols.lookup(NameRef);
269 }
270 
271 void MCContext::setSymbolValue(MCStreamer &Streamer,
272                               StringRef Sym,
273                               uint64_t Val) {
274   auto Symbol = getOrCreateSymbol(Sym);
275   Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
276 }
277 
278 void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
279   InlineAsmUsedLabelNames[Sym->getName()] = Sym;
280 }
281 
282 //===----------------------------------------------------------------------===//
283 // Section Management
284 //===----------------------------------------------------------------------===//
285 
286 MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
287                                            unsigned TypeAndAttributes,
288                                            unsigned Reserved2, SectionKind Kind,
289                                            const char *BeginSymName) {
290   // We unique sections by their segment/section pair.  The returned section
291   // may not have the same flags as the requested section, if so this should be
292   // diagnosed by the client as an error.
293 
294   // Form the name to look up.
295   SmallString<64> Name;
296   Name += Segment;
297   Name.push_back(',');
298   Name += Section;
299 
300   // Do the lookup, if we have a hit, return it.
301   MCSectionMachO *&Entry = MachOUniquingMap[Name];
302   if (Entry)
303     return Entry;
304 
305   MCSymbol *Begin = nullptr;
306   if (BeginSymName)
307     Begin = createTempSymbol(BeginSymName, false);
308 
309   // Otherwise, return a new section.
310   return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
311              Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
312 }
313 
314 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
315   StringRef GroupName;
316   if (const MCSymbol *Group = Section->getGroup())
317     GroupName = Group->getName();
318 
319   unsigned UniqueID = Section->getUniqueID();
320   ELFUniquingMap.erase(
321       ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
322   auto I = ELFUniquingMap.insert(std::make_pair(
323                                      ELFSectionKey{Name, GroupName, UniqueID},
324                                      Section))
325                .first;
326   StringRef CachedName = I->first.SectionName;
327   const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
328 }
329 
330 MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
331                                               unsigned Flags, SectionKind K,
332                                               unsigned EntrySize,
333                                               const MCSymbolELF *Group,
334                                               unsigned UniqueID,
335                                               const MCSymbolELF *Associated) {
336   MCSymbolELF *R;
337   MCSymbol *&Sym = Symbols[Section];
338   // A section symbol can not redefine regular symbols. There may be multiple
339   // sections with the same name, in which case the first such section wins.
340   if (Sym && Sym->isDefined() &&
341       (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
342     reportError(SMLoc(), "invalid symbol redefinition");
343   if (Sym && Sym->isUndefined()) {
344     R = cast<MCSymbolELF>(Sym);
345   } else {
346     auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
347     R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
348     if (!Sym)
349       Sym = R;
350   }
351   R->setBinding(ELF::STB_LOCAL);
352   R->setType(ELF::STT_SECTION);
353 
354   auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
355       Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
356 
357   auto *F = new MCDataFragment();
358   Ret->getFragmentList().insert(Ret->begin(), F);
359   F->setParent(Ret);
360   R->setFragment(F);
361 
362   return Ret;
363 }
364 
365 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
366                                              unsigned Flags, unsigned EntrySize,
367                                              const MCSymbolELF *Group,
368                                              const MCSectionELF *RelInfoSection) {
369   StringMap<bool>::iterator I;
370   bool Inserted;
371   std::tie(I, Inserted) =
372       RelSecNames.insert(std::make_pair(Name.str(), true));
373 
374   return createELFSectionImpl(
375       I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
376       true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
377 }
378 
379 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
380                                             const Twine &Suffix, unsigned Type,
381                                             unsigned Flags,
382                                             unsigned EntrySize) {
383   return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
384 }
385 
386 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
387                                        unsigned Flags, unsigned EntrySize,
388                                        const Twine &Group, unsigned UniqueID,
389                                        const MCSymbolELF *Associated) {
390   MCSymbolELF *GroupSym = nullptr;
391   if (!Group.isTriviallyEmpty() && !Group.str().empty())
392     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
393 
394   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
395                        Associated);
396 }
397 
398 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
399                                        unsigned Flags, unsigned EntrySize,
400                                        const MCSymbolELF *GroupSym,
401                                        unsigned UniqueID,
402                                        const MCSymbolELF *Associated) {
403   StringRef Group = "";
404   if (GroupSym)
405     Group = GroupSym->getName();
406   // Do the lookup, if we have a hit, return it.
407   auto IterBool = ELFUniquingMap.insert(
408       std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
409   auto &Entry = *IterBool.first;
410   if (!IterBool.second)
411     return Entry.second;
412 
413   StringRef CachedName = Entry.first.SectionName;
414 
415   SectionKind Kind;
416   if (Flags & ELF::SHF_ARM_PURECODE)
417     Kind = SectionKind::getExecuteOnly();
418   else if (Flags & ELF::SHF_EXECINSTR)
419     Kind = SectionKind::getText();
420   else
421     Kind = SectionKind::getReadOnly();
422 
423   MCSectionELF *Result = createELFSectionImpl(
424       CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
425   Entry.second = Result;
426   return Result;
427 }
428 
429 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
430   return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
431                               SectionKind::getReadOnly(), 4, Group, ~0,
432                               nullptr);
433 }
434 
435 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
436                                          unsigned Characteristics,
437                                          SectionKind Kind,
438                                          StringRef COMDATSymName, int Selection,
439                                          unsigned UniqueID,
440                                          const char *BeginSymName) {
441   MCSymbol *COMDATSymbol = nullptr;
442   if (!COMDATSymName.empty()) {
443     COMDATSymbol = getOrCreateSymbol(COMDATSymName);
444     COMDATSymName = COMDATSymbol->getName();
445   }
446 
447 
448   // Do the lookup, if we have a hit, return it.
449   COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
450   auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
451   auto Iter = IterBool.first;
452   if (!IterBool.second)
453     return Iter->second;
454 
455   MCSymbol *Begin = nullptr;
456   if (BeginSymName)
457     Begin = createTempSymbol(BeginSymName, false);
458 
459   StringRef CachedName = Iter->first.SectionName;
460   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
461       CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
462 
463   Iter->second = Result;
464   return Result;
465 }
466 
467 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
468                                          unsigned Characteristics,
469                                          SectionKind Kind,
470                                          const char *BeginSymName) {
471   return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
472                         BeginSymName);
473 }
474 
475 MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
476                                                     const MCSymbol *KeySym,
477                                                     unsigned UniqueID) {
478   // Return the normal section if we don't have to be associative or unique.
479   if (!KeySym && UniqueID == GenericSectionID)
480     return Sec;
481 
482   // If we have a key symbol, make an associative section with the same name and
483   // kind as the normal section.
484   unsigned Characteristics = Sec->getCharacteristics();
485   if (KeySym) {
486     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
487     return getCOFFSection(Sec->getSectionName(), Characteristics,
488                           Sec->getKind(), KeySym->getName(),
489                           COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
490   }
491 
492   return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
493                         "", 0, UniqueID);
494 }
495 
496 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
497                                          const Twine &Group, unsigned UniqueID,
498                                          const char *BeginSymName) {
499   MCSymbolWasm *GroupSym = nullptr;
500   if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
501     GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
502     GroupSym->setComdat(true);
503   }
504 
505   return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
506 }
507 
508 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
509                                          const MCSymbolWasm *GroupSym,
510                                          unsigned UniqueID,
511                                          const char *BeginSymName) {
512   StringRef Group = "";
513   if (GroupSym)
514     Group = GroupSym->getName();
515   // Do the lookup, if we have a hit, return it.
516   auto IterBool = WasmUniquingMap.insert(
517       std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
518   auto &Entry = *IterBool.first;
519   if (!IterBool.second)
520     return Entry.second;
521 
522   StringRef CachedName = Entry.first.SectionName;
523 
524   MCSymbol *Begin = createSymbol(CachedName, false, false);
525   cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
526 
527   MCSectionWasm *Result = new (WasmAllocator.Allocate())
528       MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
529   Entry.second = Result;
530 
531   auto *F = new MCDataFragment();
532   Result->getFragmentList().insert(Result->begin(), F);
533   F->setParent(Result);
534   Begin->setFragment(F);
535 
536   return Result;
537 }
538 
539 MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
540                                            XCOFF::StorageMappingClass SMC,
541                                            XCOFF::SymbolType Type,
542                                            XCOFF::StorageClass SC,
543                                            SectionKind Kind,
544                                            const char *BeginSymName) {
545   // Do the lookup. If we have a hit, return it.
546   auto IterBool = XCOFFUniquingMap.insert(
547       std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
548   auto &Entry = *IterBool.first;
549   if (!IterBool.second)
550     return Entry.second;
551 
552   // Otherwise, return a new section.
553   StringRef CachedName = Entry.first.SectionName;
554   MCSymbol *QualName = getOrCreateSymbol(
555       CachedName + "[" + XCOFF::getMappingClassString(SMC) + "]");
556 
557   MCSymbol *Begin = nullptr;
558   if (BeginSymName)
559     Begin = createTempSymbol(BeginSymName, false);
560 
561   MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate()) MCSectionXCOFF(
562       CachedName, SMC, Type, SC, Kind, cast<MCSymbolXCOFF>(QualName), Begin);
563   Entry.second = Result;
564 
565   auto *F = new MCDataFragment();
566   Result->getFragmentList().insert(Result->begin(), F);
567   F->setParent(Result);
568 
569   if (Begin)
570     Begin->setFragment(F);
571 
572   return Result;
573 }
574 
575 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
576   return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
577 }
578 
579 void MCContext::addDebugPrefixMapEntry(const std::string &From,
580                                        const std::string &To) {
581   DebugPrefixMap.insert(std::make_pair(From, To));
582 }
583 
584 void MCContext::RemapDebugPaths() {
585   const auto &DebugPrefixMap = this->DebugPrefixMap;
586   const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
587     for (const auto &Entry : DebugPrefixMap)
588       if (StringRef(Path).startswith(Entry.first)) {
589         std::string RemappedPath =
590             (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
591         Path.swap(RemappedPath);
592       }
593   };
594 
595   // Remap compilation directory.
596   std::string CompDir = CompilationDir.str();
597   RemapDebugPath(CompDir);
598   CompilationDir = CompDir;
599 
600   // Remap MCDwarfDirs in all compilation units.
601   for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
602     for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
603       RemapDebugPath(Dir);
604 }
605 
606 //===----------------------------------------------------------------------===//
607 // Dwarf Management
608 //===----------------------------------------------------------------------===//
609 
610 void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
611   // MCDwarf needs the root file as well as the compilation directory.
612   // If we find a '.file 0' directive that will supersede these values.
613   Optional<MD5::MD5Result> Cksum;
614   if (getDwarfVersion() >= 5) {
615     MD5 Hash;
616     MD5::MD5Result Sum;
617     Hash.update(Buffer);
618     Hash.final(Sum);
619     Cksum = Sum;
620   }
621   // Canonicalize the root filename. It cannot be empty, and should not
622   // repeat the compilation dir.
623   // The MCContext ctor initializes MainFileName to the name associated with
624   // the SrcMgr's main file ID, which might be the same as InputFileName (and
625   // possibly include directory components).
626   // Or, MainFileName might have been overridden by a -main-file-name option,
627   // which is supposed to be just a base filename with no directory component.
628   // So, if the InputFileName and MainFileName are not equal, assume
629   // MainFileName is a substitute basename and replace the last component.
630   SmallString<1024> FileNameBuf = InputFileName;
631   if (FileNameBuf.empty() || FileNameBuf == "-")
632     FileNameBuf = "<stdin>";
633   if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
634     llvm::sys::path::remove_filename(FileNameBuf);
635     llvm::sys::path::append(FileNameBuf, getMainFileName());
636   }
637   StringRef FileName = FileNameBuf;
638   if (FileName.consume_front(getCompilationDir()))
639     if (llvm::sys::path::is_separator(FileName.front()))
640       FileName = FileName.drop_front();
641   assert(!FileName.empty());
642   setMCLineTableRootFile(
643       /*CUID=*/0, getCompilationDir(), FileName, Cksum, None);
644 }
645 
646 /// getDwarfFile - takes a file name and number to place in the dwarf file and
647 /// directory tables.  If the file number has already been allocated it is an
648 /// error and zero is returned and the client reports the error, else the
649 /// allocated file number is returned.  The file numbers may be in any order.
650 Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
651                                            StringRef FileName,
652                                            unsigned FileNumber,
653                                            Optional<MD5::MD5Result> Checksum,
654                                            Optional<StringRef> Source,
655                                            unsigned CUID) {
656   MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
657   return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
658                           FileNumber);
659 }
660 
661 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
662 /// currently is assigned and false otherwise.
663 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
664   const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
665   if (FileNumber == 0)
666     return getDwarfVersion() >= 5;
667   if (FileNumber >= LineTable.getMCDwarfFiles().size())
668     return false;
669 
670   return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
671 }
672 
673 /// Remove empty sections from SectionsForRanges, to avoid generating
674 /// useless debug info for them.
675 void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
676   SectionsForRanges.remove_if(
677       [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
678 }
679 
680 CodeViewContext &MCContext::getCVContext() {
681   if (!CVContext.get())
682     CVContext.reset(new CodeViewContext);
683   return *CVContext.get();
684 }
685 
686 //===----------------------------------------------------------------------===//
687 // Error Reporting
688 //===----------------------------------------------------------------------===//
689 
690 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
691   HadError = true;
692 
693   // If we have a source manager use it. Otherwise, try using the inline source
694   // manager.
695   // If that fails, use the generic report_fatal_error().
696   if (SrcMgr)
697     SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
698   else if (InlineSrcMgr)
699     InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
700   else
701     report_fatal_error(Msg, false);
702 }
703 
704 void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
705   if (TargetOptions && TargetOptions->MCNoWarn)
706     return;
707   if (TargetOptions && TargetOptions->MCFatalWarnings)
708     reportError(Loc, Msg);
709   else {
710     // If we have a source manager use it. Otherwise, try using the inline
711     // source manager.
712     if (SrcMgr)
713       SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
714     else if (InlineSrcMgr)
715       InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
716   }
717 }
718 
719 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
720   reportError(Loc, Msg);
721 
722   // If we reached here, we are failing ungracefully. Run the interrupt handlers
723   // to make sure any special cleanups get done, in particular that we remove
724   // files registered with RemoveFileOnSignal.
725   sys::RunInterruptHandlers();
726   exit(1);
727 }
728