]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/MC/MCContext.cpp
Upgrade Unbound to 1.6.2. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / MC / MCContext.cpp
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 AsSecureLogFileName("as-secure-log-file-name",
52         cl::desc("As secure log file name (initialized from "
53                  "AS_SECURE_LOG_FILE env variable)"),
54         cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
55
56 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
57                      const MCObjectFileInfo *mofi, const SourceMgr *mgr,
58                      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       AutoReset(DoAutoReset) {
63   SecureLogFile = AsSecureLogFileName;
64
65   if (SrcMgr && SrcMgr->getNumBuffers())
66     MainFileName =
67         SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
68 }
69
70 MCContext::~MCContext() {
71   if (AutoReset)
72     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 }
77
78 //===----------------------------------------------------------------------===//
79 // Module Lifetime Management
80 //===----------------------------------------------------------------------===//
81
82 void MCContext::reset() {
83   // Call the destructors so the fragments are freed
84   COFFAllocator.DestroyAll();
85   ELFAllocator.DestroyAll();
86   MachOAllocator.DestroyAll();
87
88   MCSubtargetAllocator.DestroyAll();
89   UsedNames.clear();
90   Symbols.clear();
91   Allocator.Reset();
92   Instances.clear();
93   CompilationDir.clear();
94   MainFileName.clear();
95   MCDwarfLineTablesCUMap.clear();
96   SectionsForRanges.clear();
97   MCGenDwarfLabelEntries.clear();
98   DwarfDebugFlags = StringRef();
99   DwarfCompileUnitID = 0;
100   CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
101
102   CVContext.reset();
103
104   MachOUniquingMap.clear();
105   ELFUniquingMap.clear();
106   COFFUniquingMap.clear();
107
108   NextID.clear();
109   AllowTemporaryLabels = true;
110   DwarfLocSeen = false;
111   GenDwarfForAssembly = false;
112   GenDwarfFileNumber = 0;
113
114   HadError = false;
115 }
116
117 //===----------------------------------------------------------------------===//
118 // Symbol Manipulation
119 //===----------------------------------------------------------------------===//
120
121 MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
122   SmallString<128> NameSV;
123   StringRef NameRef = Name.toStringRef(NameSV);
124
125   assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
126
127   MCSymbol *&Sym = Symbols[NameRef];
128   if (!Sym)
129     Sym = createSymbol(NameRef, false, false);
130
131   return Sym;
132 }
133
134 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
135                                                  unsigned Idx) {
136   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
137                            "$frame_escape_" + Twine(Idx));
138 }
139
140 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
141   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
142                            "$parent_frame_offset");
143 }
144
145 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
146   return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
147                            FuncName);
148 }
149
150 MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
151                                       bool IsTemporary) {
152   if (MOFI) {
153     switch (MOFI->getObjectFileType()) {
154     case MCObjectFileInfo::IsCOFF:
155       return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
156     case MCObjectFileInfo::IsELF:
157       return new (Name, *this) MCSymbolELF(Name, IsTemporary);
158     case MCObjectFileInfo::IsMachO:
159       return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
160     case MCObjectFileInfo::IsWasm:
161       return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
162     }
163   }
164   return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
165                                     IsTemporary);
166 }
167
168 MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
169                                   bool CanBeUnnamed) {
170   if (CanBeUnnamed && !UseNamesOnTempLabels)
171     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   if (AllowTemporaryLabels && !IsTemporary)
177     IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
178
179   SmallString<128> NewName = Name;
180   bool AddSuffix = AlwaysAddSuffix;
181   unsigned &NextUniqueID = NextID[Name];
182   while (true) {
183     if (AddSuffix) {
184       NewName.resize(Name.size());
185       raw_svector_ostream(NewName) << NextUniqueID++;
186     }
187     auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
188     if (NameEntry.second || !NameEntry.first->second) {
189       // Ok, we found a name.
190       // Mark it as used for a non-section symbol.
191       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       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 MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
203                                       bool CanBeUnnamed) {
204   SmallString<128> NameSV;
205   raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
206   return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
207 }
208
209 MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
210   SmallString<128> NameSV;
211   raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
212   return createSymbol(NameSV, true, false);
213 }
214
215 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
216   return createTempSymbol("tmp", true, CanBeUnnamed);
217 }
218
219 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
220   MCLabel *&Label = Instances[LocalLabelVal];
221   if (!Label)
222     Label = new (*this) MCLabel(0);
223   return Label->incInstance();
224 }
225
226 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
227   MCLabel *&Label = Instances[LocalLabelVal];
228   if (!Label)
229     Label = new (*this) MCLabel(0);
230   return Label->getInstance();
231 }
232
233 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
234                                                        unsigned Instance) {
235   MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
236   if (!Sym)
237     Sym = createTempSymbol(false);
238   return Sym;
239 }
240
241 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
242   unsigned Instance = NextInstance(LocalLabelVal);
243   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
244 }
245
246 MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
247                                                bool Before) {
248   unsigned Instance = GetInstance(LocalLabelVal);
249   if (!Before)
250     ++Instance;
251   return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
252 }
253
254 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
255   SmallString<128> NameSV;
256   StringRef NameRef = Name.toStringRef(NameSV);
257   return Symbols.lookup(NameRef);
258 }
259
260 void MCContext::setSymbolValue(MCStreamer &Streamer,
261                               StringRef Sym,
262                               uint64_t Val) {
263   auto Symbol = getOrCreateSymbol(Sym);
264   Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
265 }
266
267 //===----------------------------------------------------------------------===//
268 // Section Management
269 //===----------------------------------------------------------------------===//
270
271 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   Name.push_back(',');
283   Name += Section;
284
285   // Do the lookup, if we have a hit, return it.
286   MCSectionMachO *&Entry = MachOUniquingMap[Name];
287   if (Entry)
288     return Entry;
289
290   MCSymbol *Begin = nullptr;
291   if (BeginSymName)
292     Begin = createTempSymbol(BeginSymName, false);
293
294   // Otherwise, return a new section.
295   return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
296              Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
297 }
298
299 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
300   StringRef GroupName;
301   if (const MCSymbol *Group = Section->getGroup())
302     GroupName = Group->getName();
303
304   unsigned UniqueID = Section->getUniqueID();
305   ELFUniquingMap.erase(
306       ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
307   auto I = ELFUniquingMap.insert(std::make_pair(
308                                      ELFSectionKey{Name, GroupName, UniqueID},
309                                      Section))
310                .first;
311   StringRef CachedName = I->first.SectionName;
312   const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
313 }
314
315 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   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   if (Sym && Sym->isDefined() &&
326       (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
327     reportError(SMLoc(), "invalid symbol redefinition");
328   if (Sym && Sym->isUndefined()) {
329     R = cast<MCSymbolELF>(Sym);
330   } else {
331     auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
332     R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
333     if (!Sym)
334       Sym = R;
335   }
336   R->setBinding(ELF::STB_LOCAL);
337   R->setType(ELF::STT_SECTION);
338
339   auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
340       Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
341
342   auto *F = new MCDataFragment();
343   Ret->getFragmentList().insert(Ret->begin(), F);
344   F->setParent(Ret);
345   R->setFragment(F);
346
347   return Ret;
348 }
349
350 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
351                                              unsigned Flags, unsigned EntrySize,
352                                              const MCSymbolELF *Group,
353                                              const MCSectionELF *RelInfoSection) {
354   StringMap<bool>::iterator I;
355   bool Inserted;
356   std::tie(I, Inserted) =
357       RelSecNames.insert(std::make_pair(Name.str(), true));
358
359   return createELFSectionImpl(
360       I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
361       true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
362 }
363
364 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
365                                             const Twine &Suffix, unsigned Type,
366                                             unsigned Flags,
367                                             unsigned EntrySize) {
368   return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
369 }
370
371 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   if (!Group.isTriviallyEmpty() && !Group.str().empty())
377     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
378
379   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
380                        Associated);
381 }
382
383 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   if (GroupSym)
390     Group = GroupSym->getName();
391   // Do the lookup, if we have a hit, return it.
392   auto IterBool = ELFUniquingMap.insert(
393       std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
394   auto &Entry = *IterBool.first;
395   if (!IterBool.second)
396     return Entry.second;
397
398   StringRef CachedName = Entry.first.SectionName;
399
400   SectionKind Kind;
401   if (Flags & ELF::SHF_ARM_PURECODE)
402     Kind = SectionKind::getExecuteOnly();
403   else if (Flags & ELF::SHF_EXECINSTR)
404     Kind = SectionKind::getText();
405   else
406     Kind = SectionKind::getReadOnly();
407
408   MCSectionELF *Result = createELFSectionImpl(
409       CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
410   Entry.second = Result;
411   return Result;
412 }
413
414 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
415   return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
416                               SectionKind::getReadOnly(), 4, Group, ~0,
417                               nullptr);
418 }
419
420 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   if (!COMDATSymName.empty()) {
428     COMDATSymbol = getOrCreateSymbol(COMDATSymName);
429     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   auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
436   auto Iter = IterBool.first;
437   if (!IterBool.second)
438     return Iter->second;
439
440   MCSymbol *Begin = nullptr;
441   if (BeginSymName)
442     Begin = createTempSymbol(BeginSymName, false);
443
444   StringRef CachedName = Iter->first.SectionName;
445   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
446       CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
447
448   Iter->second = Result;
449   return Result;
450 }
451
452 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
453                                          unsigned Characteristics,
454                                          SectionKind Kind,
455                                          const char *BeginSymName) {
456   return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
457                         BeginSymName);
458 }
459
460 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
461   COFFSectionKey T{Section, "", 0, GenericSectionID};
462   auto Iter = COFFUniquingMap.find(T);
463   if (Iter == COFFUniquingMap.end())
464     return nullptr;
465   return Iter->second;
466 }
467
468 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   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   unsigned Characteristics = Sec->getCharacteristics();
478   if (KeySym) {
479     Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
480     return getCOFFSection(Sec->getSectionName(), Characteristics,
481                           Sec->getKind(), KeySym->getName(),
482                           COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
483   }
484
485   return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
486                         "", 0, UniqueID);
487 }
488
489 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
490                                          const Twine &Group, unsigned UniqueID,
491                                          const char *BeginSymName) {
492   MCSymbolWasm *GroupSym = nullptr;
493   if (!Group.isTriviallyEmpty() && !Group.str().empty())
494     GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
495
496   return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
497 }
498
499 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
500                                          const MCSymbolWasm *GroupSym,
501                                          unsigned UniqueID,
502                                          const char *BeginSymName) {
503   StringRef Group = "";
504   if (GroupSym)
505     Group = GroupSym->getName();
506   // Do the lookup, if we have a hit, return it.
507   auto IterBool = WasmUniquingMap.insert(
508       std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
509   auto &Entry = *IterBool.first;
510   if (!IterBool.second)
511     return Entry.second;
512
513   StringRef CachedName = Entry.first.SectionName;
514
515   MCSymbol *Begin = nullptr;
516   if (BeginSymName)
517     Begin = createTempSymbol(BeginSymName, false);
518
519   MCSectionWasm *Result = new (WasmAllocator.Allocate())
520       MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
521   Entry.second = Result;
522   return Result;
523 }
524
525 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
526   return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
527 }
528
529 //===----------------------------------------------------------------------===//
530 // Dwarf Management
531 //===----------------------------------------------------------------------===//
532
533 /// getDwarfFile - takes a file name an number to place in the dwarf file and
534 /// directory tables.  If the file number has already been allocated it is an
535 /// error and zero is returned and the client reports the error, else the
536 /// allocated file number is returned.  The file numbers may be in any order.
537 unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
538                                  unsigned FileNumber, unsigned CUID) {
539   MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
540   return Table.getFile(Directory, FileName, FileNumber);
541 }
542
543 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
544 /// currently is assigned and false otherwise.
545 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
546   const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
547   if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
548     return false;
549
550   return !MCDwarfFiles[FileNumber].Name.empty();
551 }
552
553 /// Remove empty sections from SectionStartEndSyms, to avoid generating
554 /// useless debug info for them.
555 void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
556   SectionsForRanges.remove_if(
557       [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
558 }
559
560 CodeViewContext &MCContext::getCVContext() {
561   if (!CVContext.get())
562     CVContext.reset(new CodeViewContext);
563   return *CVContext.get();
564 }
565
566 //===----------------------------------------------------------------------===//
567 // Error Reporting
568 //===----------------------------------------------------------------------===//
569
570 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
571   HadError = true;
572
573   // If we have a source manager use it. Otherwise, try using the inline source
574   // manager.
575   // If that fails, use the generic report_fatal_error().
576   if (SrcMgr)
577     SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
578   else if (InlineSrcMgr)
579     InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
580   else
581     report_fatal_error(Msg, false);
582 }
583
584 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
585   reportError(Loc, Msg);
586
587   // If we reached here, we are failing ungracefully. Run the interrupt handlers
588   // to make sure any special cleanups get done, in particular that we remove
589   // files registered with RemoveFileOnSignal.
590   sys::RunInterruptHandlers();
591   exit(1);
592 }