blob: a69ee19e1a1aeeb47bf6263379d97140f3784a83 [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,
Brian Cain6dbbd0f2019-08-08 19:13:23 +000061 MCTargetOptions const *TargetOpts, 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),
Bill Wendling1b104382019-08-09 20:16:31 +000064 InlineAsmUsedLabelNames(Allocator),
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000065 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Brian Cain6dbbd0f2019-08-08 19:13:23 +000066 AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
Igor Laevsky7b998852016-06-17 15:19:41 +000067 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000068
Alp Tokera55b95b2014-07-06 10:33:31 +000069 if (SrcMgr && SrcMgr->getNumBuffers())
70 MainFileName =
71 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000072}
73
74MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000075 if (AutoReset)
76 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000077
78 // NOTE: The symbols are all allocated out of a bump pointer allocator,
79 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000080}
81
82//===----------------------------------------------------------------------===//
83// Module Lifetime Management
84//===----------------------------------------------------------------------===//
85
Pedro Artigas7212ee42012-12-12 22:59:46 +000086void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000087 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000088 COFFAllocator.DestroyAll();
89 ELFAllocator.DestroyAll();
90 MachOAllocator.DestroyAll();
Sean Fertilef09d54e2019-07-09 19:21:01 +000091 XCOFFAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000092
Akira Hatanakab11ef082015-11-14 06:35:56 +000093 MCSubtargetAllocator.DestroyAll();
Bill Wendling1b104382019-08-09 20:16:31 +000094 InlineAsmUsedLabelNames.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000095 UsedNames.clear();
96 Symbols.clear();
97 Allocator.Reset();
98 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000099 CompilationDir.clear();
100 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +0000101 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +0000102 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +0000103 MCGenDwarfLabelEntries.clear();
104 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +0000105 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000106 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000107
Reid Kleckner2214ed82016-01-29 00:49:42 +0000108 CVContext.reset();
109
David Blaikie9ec32122014-04-10 23:55:11 +0000110 MachOUniquingMap.clear();
111 ELFUniquingMap.clear();
112 COFFUniquingMap.clear();
Sam Clegg105bdc22018-05-30 02:57:20 +0000113 WasmUniquingMap.clear();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000114 XCOFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000115
Rafael Espindola9ab09232015-03-17 20:07:06 +0000116 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000117 AllowTemporaryLabels = true;
118 DwarfLocSeen = false;
119 GenDwarfForAssembly = false;
120 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000121
122 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000123}
124
Chris Lattner20731122010-04-08 20:30:37 +0000125//===----------------------------------------------------------------------===//
126// Symbol Manipulation
127//===----------------------------------------------------------------------===//
128
Jim Grosbach6f482002015-05-18 18:43:14 +0000129MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000130 SmallString<128> NameSV;
131 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000132
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000133 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000134
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000135 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000136 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000137 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000138
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000139 return Sym;
140}
141
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000142MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
143 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000144 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000145 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000146}
147
David Majnemera225a192015-03-31 22:35:44 +0000148MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000149 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000150 "$parent_frame_offset");
151}
152
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000153MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
154 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
155 FuncName);
156}
157
Rafael Espindolaa8695762015-06-02 00:25:12 +0000158MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
159 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000160 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000161 switch (MOFI->getObjectFileType()) {
162 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000163 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000164 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000165 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000166 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000167 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000168 case MCObjectFileInfo::IsWasm:
169 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Jason Liua03ae732019-03-12 22:01:10 +0000170 case MCObjectFileInfo::IsXCOFF:
Jason Liu60ec2482019-06-06 19:13:36 +0000171 return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000172 }
173 }
Pete Cooper234b8752015-06-09 18:36:13 +0000174 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
175 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000176}
177
Daniel Jasper41de8022015-06-23 11:31:32 +0000178MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
179 bool CanBeUnnamed) {
180 if (CanBeUnnamed && !UseNamesOnTempLabels)
181 return createSymbolImpl(nullptr, true);
182
Eric Christopher8e948952016-09-29 02:03:44 +0000183 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000184 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000185 bool IsTemporary = CanBeUnnamed;
186 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000187 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000188
Rafael Espindola9ab09232015-03-17 20:07:06 +0000189 SmallString<128> NewName = Name;
190 bool AddSuffix = AlwaysAddSuffix;
191 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000192 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000193 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000194 NewName.resize(Name.size());
195 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000196 }
197 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000198 if (NameEntry.second || !NameEntry.first->second) {
199 // Ok, we found a name.
200 // Mark it as used for a non-section symbol.
201 NameEntry.first->second = true;
202 // Have the MCSymbol object itself refer to the copy of the string that is
203 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000204 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000205 }
206 assert(IsTemporary && "Cannot rename non-temporary symbols");
207 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000208 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000209 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000210}
211
Daniel Jasper41de8022015-06-23 11:31:32 +0000212MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
213 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000214 SmallString<128> NameSV;
215 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000216 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000217}
218
Jim Grosbach6f482002015-05-18 18:43:14 +0000219MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000220 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000221 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000222 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000223}
224
Daniel Jasper41de8022015-06-23 11:31:32 +0000225MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
226 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000227}
228
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000229unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000230 MCLabel *&Label = Instances[LocalLabelVal];
231 if (!Label)
232 Label = new (*this) MCLabel(0);
233 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000234}
235
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000236unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000237 MCLabel *&Label = Instances[LocalLabelVal];
238 if (!Label)
239 Label = new (*this) MCLabel(0);
240 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000241}
242
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000243MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
244 unsigned Instance) {
245 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
246 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000247 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000248 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000249}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000250
Jim Grosbach6f482002015-05-18 18:43:14 +0000251MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000252 unsigned Instance = NextInstance(LocalLabelVal);
253 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
254}
255
Jim Grosbach6f482002015-05-18 18:43:14 +0000256MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000257 bool Before) {
258 unsigned Instance = GetInstance(LocalLabelVal);
259 if (!Before)
260 ++Instance;
261 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000262}
263
Jim Grosbach6f482002015-05-18 18:43:14 +0000264MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000265 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000266 StringRef NameRef = Name.toStringRef(NameSV);
267 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000268}
269
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000270void MCContext::setSymbolValue(MCStreamer &Streamer,
271 StringRef Sym,
272 uint64_t Val) {
273 auto Symbol = getOrCreateSymbol(Sym);
274 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000275}
276
Bill Wendling1b104382019-08-09 20:16:31 +0000277void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
278 InlineAsmUsedLabelNames[Sym->getName()] = Sym;
279}
280
Chris Lattner20731122010-04-08 20:30:37 +0000281//===----------------------------------------------------------------------===//
282// Section Management
283//===----------------------------------------------------------------------===//
284
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000285MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
286 unsigned TypeAndAttributes,
287 unsigned Reserved2, SectionKind Kind,
288 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000289 // We unique sections by their segment/section pair. The returned section
290 // may not have the same flags as the requested section, if so this should be
291 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000292
Chris Lattner20731122010-04-08 20:30:37 +0000293 // Form the name to look up.
294 SmallString<64> Name;
295 Name += Segment;
296 Name.push_back(',');
297 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000298
Chris Lattner20731122010-04-08 20:30:37 +0000299 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000300 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000301 if (Entry)
302 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000303
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000304 MCSymbol *Begin = nullptr;
305 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000306 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000307
Chris Lattner20731122010-04-08 20:30:37 +0000308 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000309 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
310 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000311}
Chris Lattner5418dd52010-04-08 21:26:26 +0000312
Richard Smithb910e562016-05-25 00:14:12 +0000313void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
314 StringRef GroupName;
315 if (const MCSymbol *Group = Section->getGroup())
316 GroupName = Group->getName();
317
318 unsigned UniqueID = Section->getUniqueID();
319 ELFUniquingMap.erase(
320 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
321 auto I = ELFUniquingMap.insert(std::make_pair(
322 ELFSectionKey{Name, GroupName, UniqueID},
323 Section))
324 .first;
325 StringRef CachedName = I->first.SectionName;
326 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
327}
328
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000329MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
330 unsigned Flags, SectionKind K,
331 unsigned EntrySize,
332 const MCSymbolELF *Group,
333 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000334 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000335 MCSymbolELF *R;
336 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000337 // A section symbol can not redefine regular symbols. There may be multiple
338 // sections with the same name, in which case the first such section wins.
339 if (Sym && Sym->isDefined() &&
340 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
341 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000342 if (Sym && Sym->isUndefined()) {
343 R = cast<MCSymbolELF>(Sym);
344 } else {
345 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
346 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
347 if (!Sym)
348 Sym = R;
349 }
350 R->setBinding(ELF::STB_LOCAL);
351 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000352
353 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
354 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
355
356 auto *F = new MCDataFragment();
357 Ret->getFragmentList().insert(Ret->begin(), F);
358 F->setParent(Ret);
359 R->setFragment(F);
360
361 return Ret;
362}
363
Eric Christopher36e601c2016-07-01 06:07:38 +0000364MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000365 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000366 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000367 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000368 StringMap<bool>::iterator I;
369 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000370 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000371 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000372
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000373 return createELFSectionImpl(
374 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
375 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000376}
377
Eric Christopher36e601c2016-07-01 06:07:38 +0000378MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
379 const Twine &Suffix, unsigned Type,
380 unsigned Flags,
381 unsigned EntrySize) {
382 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
383}
384
385MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000386 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000387 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000388 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000389 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000390 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000391 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000392
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000393 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000394 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000395}
396
Eric Christopher36e601c2016-07-01 06:07:38 +0000397MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000398 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000399 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000400 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000401 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000402 StringRef Group = "";
403 if (GroupSym)
404 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000405 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000406 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000407 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000408 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000409 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000410 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000411
Rafael Espindola44d50572015-03-27 21:34:24 +0000412 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000413
414 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000415 if (Flags & ELF::SHF_ARM_PURECODE)
416 Kind = SectionKind::getExecuteOnly();
417 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000418 Kind = SectionKind::getText();
419 else
420 Kind = SectionKind::getReadOnly();
421
George Rimar9fbecc92018-08-29 09:04:52 +0000422 MCSectionELF *Result = createELFSectionImpl(
423 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
424 Entry.second = Result;
425 return Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000426}
427
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000428MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000429 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
430 SectionKind::getReadOnly(), 4, Group, ~0,
431 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000432}
433
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000434MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
435 unsigned Characteristics,
436 SectionKind Kind,
437 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000438 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000439 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000440 MCSymbol *COMDATSymbol = nullptr;
441 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000442 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000443 COMDATSymName = COMDATSymbol->getName();
444 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000445
Reid Kleckner97837b72016-05-02 23:22:18 +0000446
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000447 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000448 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000449 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000450 auto Iter = IterBool.first;
451 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000452 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000453
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000454 MCSymbol *Begin = nullptr;
455 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000456 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000457
Rafael Espindola44d50572015-03-27 21:34:24 +0000458 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000459 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000460 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000461
462 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000463 return Result;
464}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000465
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000466MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
467 unsigned Characteristics,
468 SectionKind Kind,
469 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000470 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
471 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000472}
473
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000474MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000475 const MCSymbol *KeySym,
476 unsigned UniqueID) {
477 // Return the normal section if we don't have to be associative or unique.
478 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000479 return Sec;
480
Reid Kleckner97837b72016-05-02 23:22:18 +0000481 // If we have a key symbol, make an associative section with the same name and
482 // kind as the normal section.
483 unsigned Characteristics = Sec->getCharacteristics();
484 if (KeySym) {
485 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
486 return getCOFFSection(Sec->getSectionName(), Characteristics,
487 Sec->getKind(), KeySym->getName(),
488 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
489 }
490
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000491 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000492 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000493}
494
Sam Clegg12fd3da2017-10-20 21:28:38 +0000495MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohman18eafb62017-02-22 01:23:18 +0000496 const Twine &Group, unsigned UniqueID,
497 const char *BeginSymName) {
498 MCSymbolWasm *GroupSym = nullptr;
Sam Cleggd423f0d2018-01-11 20:35:17 +0000499 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000500 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Cleggd423f0d2018-01-11 20:35:17 +0000501 GroupSym->setComdat(true);
502 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000503
Sam Clegg12fd3da2017-10-20 21:28:38 +0000504 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohman18eafb62017-02-22 01:23:18 +0000505}
506
Sam Clegg12fd3da2017-10-20 21:28:38 +0000507MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohman18eafb62017-02-22 01:23:18 +0000508 const MCSymbolWasm *GroupSym,
509 unsigned UniqueID,
510 const char *BeginSymName) {
511 StringRef Group = "";
512 if (GroupSym)
513 Group = GroupSym->getName();
514 // Do the lookup, if we have a hit, return it.
515 auto IterBool = WasmUniquingMap.insert(
516 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
517 auto &Entry = *IterBool.first;
518 if (!IterBool.second)
519 return Entry.second;
520
521 StringRef CachedName = Entry.first.SectionName;
522
Sam Cleggb210c642018-05-10 17:38:35 +0000523 MCSymbol *Begin = createSymbol(CachedName, false, false);
524 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000525
526 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg12fd3da2017-10-20 21:28:38 +0000527 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohman18eafb62017-02-22 01:23:18 +0000528 Entry.second = Result;
Sam Cleggb210c642018-05-10 17:38:35 +0000529
530 auto *F = new MCDataFragment();
531 Result->getFragmentList().insert(Result->begin(), F);
532 F->setParent(Result);
533 Begin->setFragment(F);
534
Dan Gohman18eafb62017-02-22 01:23:18 +0000535 return Result;
536}
537
Sean Fertilef09d54e2019-07-09 19:21:01 +0000538MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
539 XCOFF::StorageMappingClass SMC,
Sean Fertile942537d2019-07-22 19:15:29 +0000540 XCOFF::SymbolType Type,
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000541 XCOFF::StorageClass SC,
Sean Fertilef09d54e2019-07-09 19:21:01 +0000542 SectionKind Kind,
543 const char *BeginSymName) {
544 // Do the lookup. If we have a hit, return it.
545 auto IterBool = XCOFFUniquingMap.insert(
546 std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
547 auto &Entry = *IterBool.first;
548 if (!IterBool.second)
549 return Entry.second;
550
551 // Otherwise, return a new section.
552 StringRef CachedName = Entry.first.SectionName;
553
554 MCSymbol *Begin = nullptr;
555 if (BeginSymName)
556 Begin = createTempSymbol(BeginSymName, false);
557
558 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate())
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000559 MCSectionXCOFF(CachedName, SMC, Type, SC, Kind, Begin);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000560 Entry.second = Result;
561
562 auto *F = new MCDataFragment();
563 Result->getFragmentList().insert(Result->begin(), F);
564 F->setParent(Result);
565
566 if (Begin)
567 Begin->setFragment(F);
568
569 return Result;
570}
571
Akira Hatanakab11ef082015-11-14 06:35:56 +0000572MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
573 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
574}
575
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000576void MCContext::addDebugPrefixMapEntry(const std::string &From,
577 const std::string &To) {
578 DebugPrefixMap.insert(std::make_pair(From, To));
579}
580
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000581void MCContext::RemapDebugPaths() {
582 const auto &DebugPrefixMap = this->DebugPrefixMap;
583 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
584 for (const auto &Entry : DebugPrefixMap)
585 if (StringRef(Path).startswith(Entry.first)) {
586 std::string RemappedPath =
587 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
588 Path.swap(RemappedPath);
589 }
590 };
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000591
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000592 // Remap compilation directory.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000593 std::string CompDir = CompilationDir.str();
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000594 RemapDebugPath(CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000595 CompilationDir = CompDir;
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000596
597 // Remap MCDwarfDirs in all compilation units.
598 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
599 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
600 RemapDebugPath(Dir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000601}
602
Kevin Enderbye5930f12010-07-28 20:55:35 +0000603//===----------------------------------------------------------------------===//
604// Dwarf Management
605//===----------------------------------------------------------------------===//
606
Paul Robinson1ca25762019-03-01 20:58:04 +0000607void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
608 // MCDwarf needs the root file as well as the compilation directory.
609 // If we find a '.file 0' directive that will supersede these values.
Eric Christopher798e83b2019-04-04 23:34:38 +0000610 Optional<MD5::MD5Result> Cksum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000611 if (getDwarfVersion() >= 5) {
612 MD5 Hash;
Eric Christopher798e83b2019-04-04 23:34:38 +0000613 MD5::MD5Result Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000614 Hash.update(Buffer);
Eric Christopher798e83b2019-04-04 23:34:38 +0000615 Hash.final(Sum);
616 Cksum = Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000617 }
618 // Canonicalize the root filename. It cannot be empty, and should not
619 // repeat the compilation dir.
Paul Robinson116e8d42019-05-21 11:52:27 +0000620 // The MCContext ctor initializes MainFileName to the name associated with
621 // the SrcMgr's main file ID, which might be the same as InputFileName (and
622 // possibly include directory components).
623 // Or, MainFileName might have been overridden by a -main-file-name option,
624 // which is supposed to be just a base filename with no directory component.
625 // So, if the InputFileName and MainFileName are not equal, assume
626 // MainFileName is a substitute basename and replace the last component.
627 SmallString<1024> FileNameBuf = InputFileName;
628 if (FileNameBuf.empty() || FileNameBuf == "-")
629 FileNameBuf = "<stdin>";
630 if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
631 llvm::sys::path::remove_filename(FileNameBuf);
632 llvm::sys::path::append(FileNameBuf, getMainFileName());
633 }
634 StringRef FileName = FileNameBuf;
Paul Robinson1ca25762019-03-01 20:58:04 +0000635 if (FileName.consume_front(getCompilationDir()))
Paul Robinsond8632c92019-03-01 22:28:13 +0000636 if (llvm::sys::path::is_separator(FileName.front()))
637 FileName = FileName.drop_front();
Paul Robinson1ca25762019-03-01 20:58:04 +0000638 assert(!FileName.empty());
639 setMCLineTableRootFile(
640 /*CUID=*/0, getCompilationDir(), FileName, Cksum, None);
641}
642
Scott Linder16c7bda2018-02-23 23:01:06 +0000643/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000644/// directory tables. If the file number has already been allocated it is an
645/// error and zero is returned and the client reports the error, else the
646/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson70def122018-02-22 21:03:33 +0000647Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
648 StringRef FileName,
649 unsigned FileNumber,
Eric Christopher798e83b2019-04-04 23:34:38 +0000650 Optional<MD5::MD5Result> Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000651 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000652 unsigned CUID) {
Ali Tamur02e96642019-03-26 20:05:27 +0000653 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Ali Tamur783d84b2019-04-19 02:26:56 +0000654 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
655 FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000657
Kevin Enderbya68d0042010-10-04 20:17:24 +0000658/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000659/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000660bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinson11539b02018-06-22 14:16:11 +0000661 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
662 if (FileNumber == 0)
Ali Tamur783d84b2019-04-19 02:26:56 +0000663 return getDwarfVersion() >= 5;
Paul Robinson11539b02018-06-22 14:16:11 +0000664 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000665 return false;
666
Paul Robinson11539b02018-06-22 14:16:11 +0000667 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000668}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000669
Fangrui Song58963e42018-09-08 02:04:20 +0000670/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000671/// useless debug info for them.
672void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000673 SectionsForRanges.remove_if(
674 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000675}
676
Reid Kleckner2214ed82016-01-29 00:49:42 +0000677CodeViewContext &MCContext::getCVContext() {
678 if (!CVContext.get())
679 CVContext.reset(new CodeViewContext);
680 return *CVContext.get();
681}
682
Oliver Stannard07b43d32015-11-17 09:58:07 +0000683//===----------------------------------------------------------------------===//
684// Error Reporting
685//===----------------------------------------------------------------------===//
686
687void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
688 HadError = true;
689
Sanne Wouda29338752017-02-08 14:48:05 +0000690 // If we have a source manager use it. Otherwise, try using the inline source
691 // manager.
692 // If that fails, use the generic report_fatal_error().
693 if (SrcMgr)
694 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
695 else if (InlineSrcMgr)
696 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
697 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000698 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000699}
700
Brian Cain6dbbd0f2019-08-08 19:13:23 +0000701void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
702 if (TargetOptions && TargetOptions->MCNoWarn)
703 return;
704 if (TargetOptions && TargetOptions->MCFatalWarnings)
705 reportError(Loc, Msg);
706 else {
707 // If we have a source manager use it. Otherwise, try using the inline
708 // source manager.
709 if (SrcMgr)
710 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
711 else if (InlineSrcMgr)
712 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
713 }
714}
715
Oliver Stannard07b43d32015-11-17 09:58:07 +0000716void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
717 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000718
719 // If we reached here, we are failing ungracefully. Run the interrupt handlers
720 // to make sure any special cleanups get done, in particular that we remove
721 // files registered with RemoveFileOnSignal.
722 sys::RunInterruptHandlers();
723 exit(1);
724}