1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
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);
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;
70 if (SrcMgr && SrcMgr->getNumBuffers())
72 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
75 MCContext::~MCContext() {
79 // NOTE: The symbols are all allocated out of a bump pointer allocator,
80 // we don't need to free them here.
83 //===----------------------------------------------------------------------===//
84 // Module Lifetime Management
85 //===----------------------------------------------------------------------===//
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();
94 MCSubtargetAllocator.DestroyAll();
95 InlineAsmUsedLabelNames.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);
111 MachOUniquingMap.clear();
112 ELFUniquingMap.clear();
113 COFFUniquingMap.clear();
114 WasmUniquingMap.clear();
115 XCOFFUniquingMap.clear();
118 AllowTemporaryLabels = true;
119 DwarfLocSeen = false;
120 GenDwarfForAssembly = false;
121 GenDwarfFileNumber = 0;
126 //===----------------------------------------------------------------------===//
127 // Symbol Manipulation
128 //===----------------------------------------------------------------------===//
130 MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
131 SmallString<128> NameSV;
132 StringRef NameRef = Name.toStringRef(NameSV);
134 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
136 MCSymbol *&Sym = Symbols[NameRef];
138 Sym = createSymbol(NameRef, false, false);
143 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
145 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
146 "$frame_escape_" + Twine(Idx));
149 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
150 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
151 "$parent_frame_offset");
154 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
155 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
159 MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
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);
175 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
179 MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
181 if (CanBeUnnamed && !UseNamesOnTempLabels)
182 return createSymbolImpl(nullptr, true);
184 // Determine whether this is a user written assembler temporary or normal
186 bool IsTemporary = CanBeUnnamed;
187 if (AllowTemporaryLabels && !IsTemporary)
188 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
190 SmallString<128> NewName = Name;
191 bool AddSuffix = AlwaysAddSuffix;
192 unsigned &NextUniqueID = NextID[Name];
195 NewName.resize(Name.size());
196 raw_svector_ostream(NewName) << NextUniqueID++;
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);
207 assert(IsTemporary && "Cannot rename non-temporary symbols");
210 llvm_unreachable("Infinite loop");
213 MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
215 SmallString<128> NameSV;
216 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
217 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
220 MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
221 SmallString<128> NameSV;
222 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
223 return createSymbol(NameSV, true, false);
226 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
227 return createTempSymbol("tmp", true, CanBeUnnamed);
230 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
231 MCLabel *&Label = Instances[LocalLabelVal];
233 Label = new (*this) MCLabel(0);
234 return Label->incInstance();
237 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
238 MCLabel *&Label = Instances[LocalLabelVal];
240 Label = new (*this) MCLabel(0);
241 return Label->getInstance();
244 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
246 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
248 Sym = createTempSymbol(false);
252 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
253 unsigned Instance = NextInstance(LocalLabelVal);
254 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
257 MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
259 unsigned Instance = GetInstance(LocalLabelVal);
262 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
265 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
266 SmallString<128> NameSV;
267 StringRef NameRef = Name.toStringRef(NameSV);
268 return Symbols.lookup(NameRef);
271 void MCContext::setSymbolValue(MCStreamer &Streamer,
274 auto Symbol = getOrCreateSymbol(Sym);
275 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
278 void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
279 InlineAsmUsedLabelNames[Sym->getName()] = Sym;
282 //===----------------------------------------------------------------------===//
283 // Section Management
284 //===----------------------------------------------------------------------===//
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.
294 // Form the name to look up.
295 SmallString<64> Name;
300 // Do the lookup, if we have a hit, return it.
301 MCSectionMachO *&Entry = MachOUniquingMap[Name];
305 MCSymbol *Begin = nullptr;
307 Begin = createTempSymbol(BeginSymName, false);
309 // Otherwise, return a new section.
310 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
311 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
314 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
316 if (const MCSymbol *Group = Section->getGroup())
317 GroupName = Group->getName();
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},
326 StringRef CachedName = I->first.SectionName;
327 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
330 MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
331 unsigned Flags, SectionKind K,
333 const MCSymbolELF *Group,
335 const MCSymbolELF *Associated) {
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);
346 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
347 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
351 R->setBinding(ELF::STB_LOCAL);
352 R->setType(ELF::STT_SECTION);
354 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
355 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
357 auto *F = new MCDataFragment();
358 Ret->getFragmentList().insert(Ret->begin(), F);
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;
371 std::tie(I, Inserted) =
372 RelSecNames.insert(std::make_pair(Name.str(), true));
374 return createELFSectionImpl(
375 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
376 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
379 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
380 const Twine &Suffix, unsigned Type,
382 unsigned EntrySize) {
383 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
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));
394 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
398 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
399 unsigned Flags, unsigned EntrySize,
400 const MCSymbolELF *GroupSym,
402 const MCSymbolELF *Associated) {
403 StringRef Group = "";
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)
413 StringRef CachedName = Entry.first.SectionName;
416 if (Flags & ELF::SHF_ARM_PURECODE)
417 Kind = SectionKind::getExecuteOnly();
418 else if (Flags & ELF::SHF_EXECINSTR)
419 Kind = SectionKind::getText();
421 Kind = SectionKind::getReadOnly();
423 MCSectionELF *Result = createELFSectionImpl(
424 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
425 Entry.second = Result;
429 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
430 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
431 SectionKind::getReadOnly(), 4, Group, ~0,
435 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
436 unsigned Characteristics,
438 StringRef COMDATSymName, int Selection,
440 const char *BeginSymName) {
441 MCSymbol *COMDATSymbol = nullptr;
442 if (!COMDATSymName.empty()) {
443 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
444 COMDATSymName = COMDATSymbol->getName();
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)
455 MCSymbol *Begin = nullptr;
457 Begin = createTempSymbol(BeginSymName, false);
459 StringRef CachedName = Iter->first.SectionName;
460 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
461 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
463 Iter->second = Result;
467 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
468 unsigned Characteristics,
470 const char *BeginSymName) {
471 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
475 MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
476 const MCSymbol *KeySym,
478 // Return the normal section if we don't have to be associative or unique.
479 if (!KeySym && UniqueID == GenericSectionID)
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();
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);
492 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
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);
505 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
508 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
509 const MCSymbolWasm *GroupSym,
511 const char *BeginSymName) {
512 StringRef Group = "";
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)
522 StringRef CachedName = Entry.first.SectionName;
524 MCSymbol *Begin = createSymbol(CachedName, false, false);
525 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
527 MCSectionWasm *Result = new (WasmAllocator.Allocate())
528 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
529 Entry.second = Result;
531 auto *F = new MCDataFragment();
532 Result->getFragmentList().insert(Result->begin(), F);
533 F->setParent(Result);
534 Begin->setFragment(F);
539 MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
540 XCOFF::StorageMappingClass SMC,
541 XCOFF::SymbolType Type,
542 XCOFF::StorageClass SC,
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)
552 // Otherwise, return a new section.
553 StringRef CachedName = Entry.first.SectionName;
554 MCSymbol *QualName = getOrCreateSymbol(
555 CachedName + "[" + XCOFF::getMappingClassString(SMC) + "]");
557 MCSymbol *Begin = nullptr;
559 Begin = createTempSymbol(BeginSymName, false);
561 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate()) MCSectionXCOFF(
562 CachedName, SMC, Type, SC, Kind, cast<MCSymbolXCOFF>(QualName), Begin);
563 Entry.second = Result;
565 auto *F = new MCDataFragment();
566 Result->getFragmentList().insert(Result->begin(), F);
567 F->setParent(Result);
570 Begin->setFragment(F);
575 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
576 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
579 void MCContext::addDebugPrefixMapEntry(const std::string &From,
580 const std::string &To) {
581 DebugPrefixMap.insert(std::make_pair(From, To));
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);
595 // Remap compilation directory.
596 std::string CompDir = CompilationDir.str();
597 RemapDebugPath(CompDir);
598 CompilationDir = CompDir;
600 // Remap MCDwarfDirs in all compilation units.
601 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
602 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
606 //===----------------------------------------------------------------------===//
608 //===----------------------------------------------------------------------===//
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) {
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());
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);
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,
653 Optional<MD5::MD5Result> Checksum,
654 Optional<StringRef> Source,
656 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
657 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
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);
666 return getDwarfVersion() >= 5;
667 if (FileNumber >= LineTable.getMCDwarfFiles().size())
670 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
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); });
680 CodeViewContext &MCContext::getCVContext() {
681 if (!CVContext.get())
682 CVContext.reset(new CodeViewContext);
683 return *CVContext.get();
686 //===----------------------------------------------------------------------===//
688 //===----------------------------------------------------------------------===//
690 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
693 // If we have a source manager use it. Otherwise, try using the inline source
695 // If that fails, use the generic report_fatal_error().
697 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
698 else if (InlineSrcMgr)
699 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
701 report_fatal_error(Msg, false);
704 void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
705 if (TargetOptions && TargetOptions->MCNoWarn)
707 if (TargetOptions && TargetOptions->MCFatalWarnings)
708 reportError(Loc, Msg);
710 // If we have a source manager use it. Otherwise, try using the inline
713 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
714 else if (InlineSrcMgr)
715 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
719 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
720 reportError(Loc, Msg);
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();