blob: 0dc2e2d37caf674222b56defc6a071c83de8c837 [file] [log] [blame]
Daniel Dunbarbadeace2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarca29e4d2009-06-23 22:01:43 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbarca29e4d2009-06-23 22:01:43 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCContext.h"
Scott Linder16c7bda2018-02-23 23:01:06 +000010#include "llvm/ADT/Optional.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000015#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/COFF.h"
17#include "llvm/BinaryFormat/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCAsmInfo.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000019#include "llvm/MC/MCCodeView.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCLabel.h"
24#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/MC/MCSectionCOFF.h"
26#include "llvm/MC/MCSectionELF.h"
27#include "llvm/MC/MCSectionMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000028#include "llvm/MC/MCSectionWasm.h"
Sean Fertilef09d54e2019-07-09 19:21:01 +000029#include "llvm/MC/MCSectionXCOFF.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000030#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000032#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000033#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000034#include "llvm/MC/MCSymbolMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000035#include "llvm/MC/MCSymbolWasm.h"
Jason Liu60ec2482019-06-06 19:13:36 +000036#include "llvm/MC/MCSymbolXCOFF.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000037#include "llvm/MC/SectionKind.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/Support/Casting.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000039#include "llvm/Support/CommandLine.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000040#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000041#include "llvm/Support/MemoryBuffer.h"
Paul Robinson1ca25762019-03-01 20:58:04 +000042#include "llvm/Support/Path.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000043#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Support/SourceMgr.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000046#include <cassert>
47#include <cstdlib>
48#include <tuple>
49#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000050
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000051using namespace llvm;
52
Igor Laevsky7b998852016-06-17 15:19:41 +000053static cl::opt<char*>
54AsSecureLogFileName("as-secure-log-file-name",
55 cl::desc("As secure log file name (initialized from "
56 "AS_SECURE_LOG_FILE env variable)"),
57 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
58
Bill Wendlingbc07a892013-06-18 07:20:20 +000059MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000060 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000061 bool DoAutoReset)
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000062 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
63 Symbols(Allocator), UsedNames(Allocator),
64 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Eli Friedmana6e3a822018-10-12 19:41:05 +000065 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000066 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000067
Alp Tokera55b95b2014-07-06 10:33:31 +000068 if (SrcMgr && SrcMgr->getNumBuffers())
69 MainFileName =
70 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000071}
72
73MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000074 if (AutoReset)
75 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000076
77 // NOTE: The symbols are all allocated out of a bump pointer allocator,
78 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000079}
80
81//===----------------------------------------------------------------------===//
82// Module Lifetime Management
83//===----------------------------------------------------------------------===//
84
Pedro Artigas7212ee42012-12-12 22:59:46 +000085void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000086 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000087 COFFAllocator.DestroyAll();
88 ELFAllocator.DestroyAll();
89 MachOAllocator.DestroyAll();
Sean Fertilef09d54e2019-07-09 19:21:01 +000090 XCOFFAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000091
Akira Hatanakab11ef082015-11-14 06:35:56 +000092 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000093 UsedNames.clear();
94 Symbols.clear();
95 Allocator.Reset();
96 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000097 CompilationDir.clear();
98 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000099 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +0000100 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +0000101 MCGenDwarfLabelEntries.clear();
102 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +0000103 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000104 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000105
Reid Kleckner2214ed82016-01-29 00:49:42 +0000106 CVContext.reset();
107
David Blaikie9ec32122014-04-10 23:55:11 +0000108 MachOUniquingMap.clear();
109 ELFUniquingMap.clear();
110 COFFUniquingMap.clear();
Sam Clegg105bdc22018-05-30 02:57:20 +0000111 WasmUniquingMap.clear();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000112 XCOFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000113
Rafael Espindola9ab09232015-03-17 20:07:06 +0000114 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000115 AllowTemporaryLabels = true;
116 DwarfLocSeen = false;
117 GenDwarfForAssembly = false;
118 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000119
120 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000121}
122
Chris Lattner20731122010-04-08 20:30:37 +0000123//===----------------------------------------------------------------------===//
124// Symbol Manipulation
125//===----------------------------------------------------------------------===//
126
Jim Grosbach6f482002015-05-18 18:43:14 +0000127MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000128 SmallString<128> NameSV;
129 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000130
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000131 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000132
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000133 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000134 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000135 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000136
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000137 return Sym;
138}
139
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000140MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
141 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000142 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000143 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000144}
145
David Majnemera225a192015-03-31 22:35:44 +0000146MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000147 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000148 "$parent_frame_offset");
149}
150
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000151MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
152 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
153 FuncName);
154}
155
Rafael Espindolaa8695762015-06-02 00:25:12 +0000156MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
157 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000158 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000159 switch (MOFI->getObjectFileType()) {
160 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000161 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000162 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000163 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000164 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000165 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000166 case MCObjectFileInfo::IsWasm:
167 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Jason Liua03ae732019-03-12 22:01:10 +0000168 case MCObjectFileInfo::IsXCOFF:
Jason Liu60ec2482019-06-06 19:13:36 +0000169 return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000170 }
171 }
Pete Cooper234b8752015-06-09 18:36:13 +0000172 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
173 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000174}
175
Daniel Jasper41de8022015-06-23 11:31:32 +0000176MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
177 bool CanBeUnnamed) {
178 if (CanBeUnnamed && !UseNamesOnTempLabels)
179 return createSymbolImpl(nullptr, true);
180
Eric Christopher8e948952016-09-29 02:03:44 +0000181 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000182 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000183 bool IsTemporary = CanBeUnnamed;
184 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000185 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000186
Rafael Espindola9ab09232015-03-17 20:07:06 +0000187 SmallString<128> NewName = Name;
188 bool AddSuffix = AlwaysAddSuffix;
189 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000190 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000191 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000192 NewName.resize(Name.size());
193 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000194 }
195 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000196 if (NameEntry.second || !NameEntry.first->second) {
197 // Ok, we found a name.
198 // Mark it as used for a non-section symbol.
199 NameEntry.first->second = true;
200 // Have the MCSymbol object itself refer to the copy of the string that is
201 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000202 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000203 }
204 assert(IsTemporary && "Cannot rename non-temporary symbols");
205 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000206 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000207 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000208}
209
Daniel Jasper41de8022015-06-23 11:31:32 +0000210MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
211 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000212 SmallString<128> NameSV;
213 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000214 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000215}
216
Jim Grosbach6f482002015-05-18 18:43:14 +0000217MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000218 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000219 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000220 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000221}
222
Daniel Jasper41de8022015-06-23 11:31:32 +0000223MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
224 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000225}
226
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000227unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000228 MCLabel *&Label = Instances[LocalLabelVal];
229 if (!Label)
230 Label = new (*this) MCLabel(0);
231 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000232}
233
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000234unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000235 MCLabel *&Label = Instances[LocalLabelVal];
236 if (!Label)
237 Label = new (*this) MCLabel(0);
238 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000239}
240
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000241MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
242 unsigned Instance) {
243 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
244 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000245 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000246 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000247}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000248
Jim Grosbach6f482002015-05-18 18:43:14 +0000249MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000250 unsigned Instance = NextInstance(LocalLabelVal);
251 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
252}
253
Jim Grosbach6f482002015-05-18 18:43:14 +0000254MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000255 bool Before) {
256 unsigned Instance = GetInstance(LocalLabelVal);
257 if (!Before)
258 ++Instance;
259 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000260}
261
Jim Grosbach6f482002015-05-18 18:43:14 +0000262MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000263 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000264 StringRef NameRef = Name.toStringRef(NameSV);
265 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000266}
267
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000268void MCContext::setSymbolValue(MCStreamer &Streamer,
269 StringRef Sym,
270 uint64_t Val) {
271 auto Symbol = getOrCreateSymbol(Sym);
272 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000273}
274
Chris Lattner20731122010-04-08 20:30:37 +0000275//===----------------------------------------------------------------------===//
276// Section Management
277//===----------------------------------------------------------------------===//
278
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000279MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
280 unsigned TypeAndAttributes,
281 unsigned Reserved2, SectionKind Kind,
282 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000283 // We unique sections by their segment/section pair. The returned section
284 // may not have the same flags as the requested section, if so this should be
285 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000286
Chris Lattner20731122010-04-08 20:30:37 +0000287 // Form the name to look up.
288 SmallString<64> Name;
289 Name += Segment;
290 Name.push_back(',');
291 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000292
Chris Lattner20731122010-04-08 20:30:37 +0000293 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000294 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000295 if (Entry)
296 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000297
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000298 MCSymbol *Begin = nullptr;
299 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000300 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000301
Chris Lattner20731122010-04-08 20:30:37 +0000302 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000303 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
304 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000305}
Chris Lattner5418dd52010-04-08 21:26:26 +0000306
Richard Smithb910e562016-05-25 00:14:12 +0000307void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
308 StringRef GroupName;
309 if (const MCSymbol *Group = Section->getGroup())
310 GroupName = Group->getName();
311
312 unsigned UniqueID = Section->getUniqueID();
313 ELFUniquingMap.erase(
314 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
315 auto I = ELFUniquingMap.insert(std::make_pair(
316 ELFSectionKey{Name, GroupName, UniqueID},
317 Section))
318 .first;
319 StringRef CachedName = I->first.SectionName;
320 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
321}
322
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000323MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
324 unsigned Flags, SectionKind K,
325 unsigned EntrySize,
326 const MCSymbolELF *Group,
327 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000328 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000329 MCSymbolELF *R;
330 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000331 // A section symbol can not redefine regular symbols. There may be multiple
332 // sections with the same name, in which case the first such section wins.
333 if (Sym && Sym->isDefined() &&
334 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
335 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000336 if (Sym && Sym->isUndefined()) {
337 R = cast<MCSymbolELF>(Sym);
338 } else {
339 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
340 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
341 if (!Sym)
342 Sym = R;
343 }
344 R->setBinding(ELF::STB_LOCAL);
345 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000346
347 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
348 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
349
350 auto *F = new MCDataFragment();
351 Ret->getFragmentList().insert(Ret->begin(), F);
352 F->setParent(Ret);
353 R->setFragment(F);
354
355 return Ret;
356}
357
Eric Christopher36e601c2016-07-01 06:07:38 +0000358MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000359 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000360 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000361 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000362 StringMap<bool>::iterator I;
363 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000364 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000365 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000366
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000367 return createELFSectionImpl(
368 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
369 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000370}
371
Eric Christopher36e601c2016-07-01 06:07:38 +0000372MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
373 const Twine &Suffix, unsigned Type,
374 unsigned Flags,
375 unsigned EntrySize) {
376 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
377}
378
379MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000380 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000381 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000382 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000383 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000384 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000385 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000386
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000387 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000388 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000389}
390
Eric Christopher36e601c2016-07-01 06:07:38 +0000391MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000392 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000393 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000394 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000395 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000396 StringRef Group = "";
397 if (GroupSym)
398 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000399 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000400 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000401 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000402 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000403 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000404 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000405
Rafael Espindola44d50572015-03-27 21:34:24 +0000406 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000407
408 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000409 if (Flags & ELF::SHF_ARM_PURECODE)
410 Kind = SectionKind::getExecuteOnly();
411 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000412 Kind = SectionKind::getText();
413 else
414 Kind = SectionKind::getReadOnly();
415
George Rimar9fbecc92018-08-29 09:04:52 +0000416 MCSectionELF *Result = createELFSectionImpl(
417 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
418 Entry.second = Result;
419 return Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000420}
421
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000422MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000423 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
424 SectionKind::getReadOnly(), 4, Group, ~0,
425 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000426}
427
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000428MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
429 unsigned Characteristics,
430 SectionKind Kind,
431 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000432 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000433 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000434 MCSymbol *COMDATSymbol = nullptr;
435 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000436 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000437 COMDATSymName = COMDATSymbol->getName();
438 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000439
Reid Kleckner97837b72016-05-02 23:22:18 +0000440
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000441 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000442 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000443 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000444 auto Iter = IterBool.first;
445 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000446 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000447
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000448 MCSymbol *Begin = nullptr;
449 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000450 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000451
Rafael Espindola44d50572015-03-27 21:34:24 +0000452 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000453 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000454 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000455
456 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000457 return Result;
458}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000459
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000460MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
461 unsigned Characteristics,
462 SectionKind Kind,
463 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000464 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
465 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000466}
467
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000468MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000469 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)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000473 return Sec;
474
Reid Kleckner97837b72016-05-02 23:22:18 +0000475 // 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
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000485 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000486 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000487}
488
Sam Clegg12fd3da2017-10-20 21:28:38 +0000489MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohman18eafb62017-02-22 01:23:18 +0000490 const Twine &Group, unsigned UniqueID,
491 const char *BeginSymName) {
492 MCSymbolWasm *GroupSym = nullptr;
Sam Cleggd423f0d2018-01-11 20:35:17 +0000493 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000494 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Cleggd423f0d2018-01-11 20:35:17 +0000495 GroupSym->setComdat(true);
496 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000497
Sam Clegg12fd3da2017-10-20 21:28:38 +0000498 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohman18eafb62017-02-22 01:23:18 +0000499}
500
Sam Clegg12fd3da2017-10-20 21:28:38 +0000501MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohman18eafb62017-02-22 01:23:18 +0000502 const MCSymbolWasm *GroupSym,
503 unsigned UniqueID,
504 const char *BeginSymName) {
505 StringRef Group = "";
506 if (GroupSym)
507 Group = GroupSym->getName();
508 // Do the lookup, if we have a hit, return it.
509 auto IterBool = WasmUniquingMap.insert(
510 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
511 auto &Entry = *IterBool.first;
512 if (!IterBool.second)
513 return Entry.second;
514
515 StringRef CachedName = Entry.first.SectionName;
516
Sam Cleggb210c642018-05-10 17:38:35 +0000517 MCSymbol *Begin = createSymbol(CachedName, false, false);
518 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000519
520 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg12fd3da2017-10-20 21:28:38 +0000521 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohman18eafb62017-02-22 01:23:18 +0000522 Entry.second = Result;
Sam Cleggb210c642018-05-10 17:38:35 +0000523
524 auto *F = new MCDataFragment();
525 Result->getFragmentList().insert(Result->begin(), F);
526 F->setParent(Result);
527 Begin->setFragment(F);
528
Dan Gohman18eafb62017-02-22 01:23:18 +0000529 return Result;
530}
531
Sean Fertilef09d54e2019-07-09 19:21:01 +0000532MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
533 XCOFF::StorageMappingClass SMC,
534 SectionKind Kind,
535 const char *BeginSymName) {
536 // Do the lookup. If we have a hit, return it.
537 auto IterBool = XCOFFUniquingMap.insert(
538 std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
539 auto &Entry = *IterBool.first;
540 if (!IterBool.second)
541 return Entry.second;
542
543 // Otherwise, return a new section.
544 StringRef CachedName = Entry.first.SectionName;
545
546 MCSymbol *Begin = nullptr;
547 if (BeginSymName)
548 Begin = createTempSymbol(BeginSymName, false);
549
550 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate())
551 MCSectionXCOFF(CachedName, SMC, Kind, Begin);
552 Entry.second = Result;
553
554 auto *F = new MCDataFragment();
555 Result->getFragmentList().insert(Result->begin(), F);
556 F->setParent(Result);
557
558 if (Begin)
559 Begin->setFragment(F);
560
561 return Result;
562}
563
Akira Hatanakab11ef082015-11-14 06:35:56 +0000564MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
565 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
566}
567
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000568void MCContext::addDebugPrefixMapEntry(const std::string &From,
569 const std::string &To) {
570 DebugPrefixMap.insert(std::make_pair(From, To));
571}
572
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000573void MCContext::RemapDebugPaths() {
574 const auto &DebugPrefixMap = this->DebugPrefixMap;
575 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
576 for (const auto &Entry : DebugPrefixMap)
577 if (StringRef(Path).startswith(Entry.first)) {
578 std::string RemappedPath =
579 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
580 Path.swap(RemappedPath);
581 }
582 };
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000583
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000584 // Remap compilation directory.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000585 std::string CompDir = CompilationDir.str();
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000586 RemapDebugPath(CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000587 CompilationDir = CompDir;
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000588
589 // Remap MCDwarfDirs in all compilation units.
590 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
591 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
592 RemapDebugPath(Dir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000593}
594
Kevin Enderbye5930f12010-07-28 20:55:35 +0000595//===----------------------------------------------------------------------===//
596// Dwarf Management
597//===----------------------------------------------------------------------===//
598
Paul Robinson1ca25762019-03-01 20:58:04 +0000599void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
600 // MCDwarf needs the root file as well as the compilation directory.
601 // If we find a '.file 0' directive that will supersede these values.
Eric Christopher798e83b2019-04-04 23:34:38 +0000602 Optional<MD5::MD5Result> Cksum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000603 if (getDwarfVersion() >= 5) {
604 MD5 Hash;
Eric Christopher798e83b2019-04-04 23:34:38 +0000605 MD5::MD5Result Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000606 Hash.update(Buffer);
Eric Christopher798e83b2019-04-04 23:34:38 +0000607 Hash.final(Sum);
608 Cksum = Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000609 }
610 // Canonicalize the root filename. It cannot be empty, and should not
611 // repeat the compilation dir.
Paul Robinson116e8d42019-05-21 11:52:27 +0000612 // The MCContext ctor initializes MainFileName to the name associated with
613 // the SrcMgr's main file ID, which might be the same as InputFileName (and
614 // possibly include directory components).
615 // Or, MainFileName might have been overridden by a -main-file-name option,
616 // which is supposed to be just a base filename with no directory component.
617 // So, if the InputFileName and MainFileName are not equal, assume
618 // MainFileName is a substitute basename and replace the last component.
619 SmallString<1024> FileNameBuf = InputFileName;
620 if (FileNameBuf.empty() || FileNameBuf == "-")
621 FileNameBuf = "<stdin>";
622 if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
623 llvm::sys::path::remove_filename(FileNameBuf);
624 llvm::sys::path::append(FileNameBuf, getMainFileName());
625 }
626 StringRef FileName = FileNameBuf;
Paul Robinson1ca25762019-03-01 20:58:04 +0000627 if (FileName.consume_front(getCompilationDir()))
Paul Robinsond8632c92019-03-01 22:28:13 +0000628 if (llvm::sys::path::is_separator(FileName.front()))
629 FileName = FileName.drop_front();
Paul Robinson1ca25762019-03-01 20:58:04 +0000630 assert(!FileName.empty());
631 setMCLineTableRootFile(
632 /*CUID=*/0, getCompilationDir(), FileName, Cksum, None);
633}
634
Scott Linder16c7bda2018-02-23 23:01:06 +0000635/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000636/// directory tables. If the file number has already been allocated it is an
637/// error and zero is returned and the client reports the error, else the
638/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson70def122018-02-22 21:03:33 +0000639Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
640 StringRef FileName,
641 unsigned FileNumber,
Eric Christopher798e83b2019-04-04 23:34:38 +0000642 Optional<MD5::MD5Result> Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000643 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000644 unsigned CUID) {
Ali Tamur02e96642019-03-26 20:05:27 +0000645 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Ali Tamur783d84b2019-04-19 02:26:56 +0000646 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
647 FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000648}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000649
Kevin Enderbya68d0042010-10-04 20:17:24 +0000650/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000651/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000652bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinson11539b02018-06-22 14:16:11 +0000653 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
654 if (FileNumber == 0)
Ali Tamur783d84b2019-04-19 02:26:56 +0000655 return getDwarfVersion() >= 5;
Paul Robinson11539b02018-06-22 14:16:11 +0000656 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000657 return false;
658
Paul Robinson11539b02018-06-22 14:16:11 +0000659 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000660}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000661
Fangrui Song58963e42018-09-08 02:04:20 +0000662/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000663/// useless debug info for them.
664void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000665 SectionsForRanges.remove_if(
666 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000667}
668
Reid Kleckner2214ed82016-01-29 00:49:42 +0000669CodeViewContext &MCContext::getCVContext() {
670 if (!CVContext.get())
671 CVContext.reset(new CodeViewContext);
672 return *CVContext.get();
673}
674
Oliver Stannard07b43d32015-11-17 09:58:07 +0000675//===----------------------------------------------------------------------===//
676// Error Reporting
677//===----------------------------------------------------------------------===//
678
679void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
680 HadError = true;
681
Sanne Wouda29338752017-02-08 14:48:05 +0000682 // If we have a source manager use it. Otherwise, try using the inline source
683 // manager.
684 // If that fails, use the generic report_fatal_error().
685 if (SrcMgr)
686 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
687 else if (InlineSrcMgr)
688 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
689 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000690 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000691}
692
693void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
694 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000695
696 // If we reached here, we are failing ungracefully. Run the interrupt handlers
697 // to make sure any special cleanups get done, in particular that we remove
698 // files registered with RemoveFileOnSignal.
699 sys::RunInterruptHandlers();
700 exit(1);
701}