blob: ff4f636f846f51241b3c47339b4ac85633c00dd1 [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"
Jason Liu0dc05722019-11-08 09:26:28 -050018#include "llvm/BinaryFormat/XCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/MC/MCAsmInfo.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000020#include "llvm/MC/MCCodeView.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCLabel.h"
25#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSectionELF.h"
28#include "llvm/MC/MCSectionMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000029#include "llvm/MC/MCSectionWasm.h"
Sean Fertilef09d54e2019-07-09 19:21:01 +000030#include "llvm/MC/MCSectionXCOFF.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000031#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000033#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000034#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000035#include "llvm/MC/MCSymbolMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000036#include "llvm/MC/MCSymbolWasm.h"
Jason Liu60ec2482019-06-06 19:13:36 +000037#include "llvm/MC/MCSymbolXCOFF.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000038#include "llvm/MC/SectionKind.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000039#include "llvm/Support/Casting.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000040#include "llvm/Support/CommandLine.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000041#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000042#include "llvm/Support/MemoryBuffer.h"
Paul Robinson1ca25762019-03-01 20:58:04 +000043#include "llvm/Support/Path.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000044#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Support/SourceMgr.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000046#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000047#include <cassert>
48#include <cstdlib>
49#include <tuple>
50#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000051
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000052using namespace llvm;
53
Igor Laevsky7b998852016-06-17 15:19:41 +000054static cl::opt<char*>
55AsSecureLogFileName("as-secure-log-file-name",
56 cl::desc("As secure log file name (initialized from "
57 "AS_SECURE_LOG_FILE env variable)"),
58 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
59
Bill Wendlingbc07a892013-06-18 07:20:20 +000060MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000061 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Brian Cain6dbbd0f2019-08-08 19:13:23 +000062 MCTargetOptions const *TargetOpts, bool DoAutoReset)
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000063 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
64 Symbols(Allocator), UsedNames(Allocator),
Bill Wendling1b104382019-08-09 20:16:31 +000065 InlineAsmUsedLabelNames(Allocator),
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000066 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Brian Cain6dbbd0f2019-08-08 19:13:23 +000067 AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
Igor Laevsky7b998852016-06-17 15:19:41 +000068 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000069
Alp Tokera55b95b2014-07-06 10:33:31 +000070 if (SrcMgr && SrcMgr->getNumBuffers())
Benjamin Krameradcd0262020-01-28 20:23:46 +010071 MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())
72 ->getBufferIdentifier());
Pedro Artigase84b13f2012-12-06 22:12:44 +000073}
74
75MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000076 if (AutoReset)
77 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000078
79 // NOTE: The symbols are all allocated out of a bump pointer allocator,
80 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000081}
82
83//===----------------------------------------------------------------------===//
84// Module Lifetime Management
85//===----------------------------------------------------------------------===//
86
Pedro Artigas7212ee42012-12-12 22:59:46 +000087void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000088 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000089 COFFAllocator.DestroyAll();
90 ELFAllocator.DestroyAll();
91 MachOAllocator.DestroyAll();
Sean Fertilef09d54e2019-07-09 19:21:01 +000092 XCOFFAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000093
Akira Hatanakab11ef082015-11-14 06:35:56 +000094 MCSubtargetAllocator.DestroyAll();
Bill Wendling1b104382019-08-09 20:16:31 +000095 InlineAsmUsedLabelNames.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000096 UsedNames.clear();
97 Symbols.clear();
98 Allocator.Reset();
99 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +0000100 CompilationDir.clear();
101 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +0000102 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +0000103 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +0000104 MCGenDwarfLabelEntries.clear();
105 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +0000106 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000107 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000108
Reid Kleckner2214ed82016-01-29 00:49:42 +0000109 CVContext.reset();
110
David Blaikie9ec32122014-04-10 23:55:11 +0000111 MachOUniquingMap.clear();
112 ELFUniquingMap.clear();
113 COFFUniquingMap.clear();
Sam Clegg105bdc22018-05-30 02:57:20 +0000114 WasmUniquingMap.clear();
Sean Fertilef09d54e2019-07-09 19:21:01 +0000115 XCOFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000116
Rafael Espindola9ab09232015-03-17 20:07:06 +0000117 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000118 AllowTemporaryLabels = true;
119 DwarfLocSeen = false;
120 GenDwarfForAssembly = false;
121 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000122
123 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000124}
125
Chris Lattner20731122010-04-08 20:30:37 +0000126//===----------------------------------------------------------------------===//
127// Symbol Manipulation
128//===----------------------------------------------------------------------===//
129
Jim Grosbach6f482002015-05-18 18:43:14 +0000130MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000131 SmallString<128> NameSV;
132 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000133
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000134 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000135
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000136 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000137 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000138 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000139
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000140 return Sym;
141}
142
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000143MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
144 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000145 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000146 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000147}
148
David Majnemera225a192015-03-31 22:35:44 +0000149MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000150 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000151 "$parent_frame_offset");
152}
153
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000154MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
155 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
156 FuncName);
157}
158
Rafael Espindolaa8695762015-06-02 00:25:12 +0000159MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
160 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000161 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000162 switch (MOFI->getObjectFileType()) {
163 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000164 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000165 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000166 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000167 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000168 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000169 case MCObjectFileInfo::IsWasm:
170 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Jason Liua03ae732019-03-12 22:01:10 +0000171 case MCObjectFileInfo::IsXCOFF:
Jason Liu60ec2482019-06-06 19:13:36 +0000172 return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000173 }
174 }
Pete Cooper234b8752015-06-09 18:36:13 +0000175 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
176 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000177}
178
Daniel Jasper41de8022015-06-23 11:31:32 +0000179MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
180 bool CanBeUnnamed) {
181 if (CanBeUnnamed && !UseNamesOnTempLabels)
182 return createSymbolImpl(nullptr, true);
183
Eric Christopher8e948952016-09-29 02:03:44 +0000184 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000185 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000186 bool IsTemporary = CanBeUnnamed;
187 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000188 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000189
Rafael Espindola9ab09232015-03-17 20:07:06 +0000190 SmallString<128> NewName = Name;
191 bool AddSuffix = AlwaysAddSuffix;
192 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000193 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000194 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000195 NewName.resize(Name.size());
196 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000197 }
198 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000199 if (NameEntry.second || !NameEntry.first->second) {
200 // Ok, we found a name.
201 // Mark it as used for a non-section symbol.
202 NameEntry.first->second = true;
203 // Have the MCSymbol object itself refer to the copy of the string that is
204 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000205 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000206 }
207 assert(IsTemporary && "Cannot rename non-temporary symbols");
208 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000209 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000210 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000211}
212
Daniel Jasper41de8022015-06-23 11:31:32 +0000213MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
214 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000215 SmallString<128> NameSV;
216 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000217 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000218}
219
Jim Grosbach6f482002015-05-18 18:43:14 +0000220MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000221 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000222 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000223 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000224}
225
Daniel Jasper41de8022015-06-23 11:31:32 +0000226MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
227 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000228}
229
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000230unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000231 MCLabel *&Label = Instances[LocalLabelVal];
232 if (!Label)
233 Label = new (*this) MCLabel(0);
234 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000235}
236
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000237unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000238 MCLabel *&Label = Instances[LocalLabelVal];
239 if (!Label)
240 Label = new (*this) MCLabel(0);
241 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000242}
243
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000244MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
245 unsigned Instance) {
246 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
247 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000248 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000249 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000250}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000251
Jim Grosbach6f482002015-05-18 18:43:14 +0000252MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000253 unsigned Instance = NextInstance(LocalLabelVal);
254 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
255}
256
Jim Grosbach6f482002015-05-18 18:43:14 +0000257MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000258 bool Before) {
259 unsigned Instance = GetInstance(LocalLabelVal);
260 if (!Before)
261 ++Instance;
262 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000263}
264
Jim Grosbach6f482002015-05-18 18:43:14 +0000265MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000266 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000267 StringRef NameRef = Name.toStringRef(NameSV);
268 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000269}
270
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000271void MCContext::setSymbolValue(MCStreamer &Streamer,
272 StringRef Sym,
273 uint64_t Val) {
274 auto Symbol = getOrCreateSymbol(Sym);
275 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000276}
277
Bill Wendling1b104382019-08-09 20:16:31 +0000278void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
279 InlineAsmUsedLabelNames[Sym->getName()] = Sym;
280}
281
Chris Lattner20731122010-04-08 20:30:37 +0000282//===----------------------------------------------------------------------===//
283// Section Management
284//===----------------------------------------------------------------------===//
285
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000286MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
287 unsigned TypeAndAttributes,
288 unsigned Reserved2, SectionKind Kind,
289 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000290 // We unique sections by their segment/section pair. The returned section
291 // may not have the same flags as the requested section, if so this should be
292 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000293
Chris Lattner20731122010-04-08 20:30:37 +0000294 // Form the name to look up.
295 SmallString<64> Name;
296 Name += Segment;
297 Name.push_back(',');
298 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000299
Chris Lattner20731122010-04-08 20:30:37 +0000300 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000301 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000302 if (Entry)
303 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000304
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000305 MCSymbol *Begin = nullptr;
306 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000307 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000308
Chris Lattner20731122010-04-08 20:30:37 +0000309 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000310 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
311 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000312}
Chris Lattner5418dd52010-04-08 21:26:26 +0000313
Richard Smithb910e562016-05-25 00:14:12 +0000314void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
315 StringRef GroupName;
316 if (const MCSymbol *Group = Section->getGroup())
317 GroupName = Group->getName();
318
319 unsigned UniqueID = Section->getUniqueID();
320 ELFUniquingMap.erase(
321 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
322 auto I = ELFUniquingMap.insert(std::make_pair(
323 ELFSectionKey{Name, GroupName, UniqueID},
324 Section))
325 .first;
326 StringRef CachedName = I->first.SectionName;
327 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
328}
329
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000330MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
331 unsigned Flags, SectionKind K,
332 unsigned EntrySize,
333 const MCSymbolELF *Group,
334 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000335 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000336 MCSymbolELF *R;
337 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000338 // A section symbol can not redefine regular symbols. There may be multiple
339 // sections with the same name, in which case the first such section wins.
340 if (Sym && Sym->isDefined() &&
341 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
342 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000343 if (Sym && Sym->isUndefined()) {
344 R = cast<MCSymbolELF>(Sym);
345 } else {
346 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
347 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
348 if (!Sym)
349 Sym = R;
350 }
351 R->setBinding(ELF::STB_LOCAL);
352 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000353
354 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
355 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
356
357 auto *F = new MCDataFragment();
358 Ret->getFragmentList().insert(Ret->begin(), F);
359 F->setParent(Ret);
360 R->setFragment(F);
361
362 return Ret;
363}
364
Eric Christopher36e601c2016-07-01 06:07:38 +0000365MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000366 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000367 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000368 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000369 StringMap<bool>::iterator I;
370 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000371 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000372 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000373
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000374 return createELFSectionImpl(
375 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
376 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000377}
378
Eric Christopher36e601c2016-07-01 06:07:38 +0000379MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
380 const Twine &Suffix, unsigned Type,
381 unsigned Flags,
382 unsigned EntrySize) {
383 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
384}
385
386MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000387 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000388 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000389 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000390 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000391 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000392 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000393
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000394 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000395 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000396}
397
Eric Christopher36e601c2016-07-01 06:07:38 +0000398MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000399 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000400 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000401 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000402 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000403 StringRef Group = "";
404 if (GroupSym)
405 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000406 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000407 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000408 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000409 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000410 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000411 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000412
Rafael Espindola44d50572015-03-27 21:34:24 +0000413 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000414
415 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000416 if (Flags & ELF::SHF_ARM_PURECODE)
417 Kind = SectionKind::getExecuteOnly();
418 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000419 Kind = SectionKind::getText();
420 else
421 Kind = SectionKind::getReadOnly();
422
George Rimar9fbecc92018-08-29 09:04:52 +0000423 MCSectionELF *Result = createELFSectionImpl(
424 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
425 Entry.second = Result;
426 return Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000427}
428
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000429MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000430 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
431 SectionKind::getReadOnly(), 4, Group, ~0,
432 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000433}
434
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000435MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
436 unsigned Characteristics,
437 SectionKind Kind,
438 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000439 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000440 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000441 MCSymbol *COMDATSymbol = nullptr;
442 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000443 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000444 COMDATSymName = COMDATSymbol->getName();
445 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000446
Reid Kleckner97837b72016-05-02 23:22:18 +0000447
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000448 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000449 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000450 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000451 auto Iter = IterBool.first;
452 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000453 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000454
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000455 MCSymbol *Begin = nullptr;
456 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000457 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000458
Rafael Espindola44d50572015-03-27 21:34:24 +0000459 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000460 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000461 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000462
463 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000464 return Result;
465}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000466
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000467MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
468 unsigned Characteristics,
469 SectionKind Kind,
470 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000471 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
472 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000473}
474
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000475MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000476 const MCSymbol *KeySym,
477 unsigned UniqueID) {
478 // Return the normal section if we don't have to be associative or unique.
479 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000480 return Sec;
481
Reid Kleckner97837b72016-05-02 23:22:18 +0000482 // If we have a key symbol, make an associative section with the same name and
483 // kind as the normal section.
484 unsigned Characteristics = Sec->getCharacteristics();
485 if (KeySym) {
486 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
487 return getCOFFSection(Sec->getSectionName(), Characteristics,
488 Sec->getKind(), KeySym->getName(),
489 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
490 }
491
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000492 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000493 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000494}
495
Sam Clegg12fd3da2017-10-20 21:28:38 +0000496MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohman18eafb62017-02-22 01:23:18 +0000497 const Twine &Group, unsigned UniqueID,
498 const char *BeginSymName) {
499 MCSymbolWasm *GroupSym = nullptr;
Sam Cleggd423f0d2018-01-11 20:35:17 +0000500 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000501 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Cleggd423f0d2018-01-11 20:35:17 +0000502 GroupSym->setComdat(true);
503 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000504
Sam Clegg12fd3da2017-10-20 21:28:38 +0000505 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohman18eafb62017-02-22 01:23:18 +0000506}
507
Sam Clegg12fd3da2017-10-20 21:28:38 +0000508MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohman18eafb62017-02-22 01:23:18 +0000509 const MCSymbolWasm *GroupSym,
510 unsigned UniqueID,
511 const char *BeginSymName) {
512 StringRef Group = "";
513 if (GroupSym)
514 Group = GroupSym->getName();
515 // Do the lookup, if we have a hit, return it.
516 auto IterBool = WasmUniquingMap.insert(
517 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
518 auto &Entry = *IterBool.first;
519 if (!IterBool.second)
520 return Entry.second;
521
522 StringRef CachedName = Entry.first.SectionName;
523
Sam Cleggb210c642018-05-10 17:38:35 +0000524 MCSymbol *Begin = createSymbol(CachedName, false, false);
525 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000526
527 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg12fd3da2017-10-20 21:28:38 +0000528 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohman18eafb62017-02-22 01:23:18 +0000529 Entry.second = Result;
Sam Cleggb210c642018-05-10 17:38:35 +0000530
531 auto *F = new MCDataFragment();
532 Result->getFragmentList().insert(Result->begin(), F);
533 F->setParent(Result);
534 Begin->setFragment(F);
535
Dan Gohman18eafb62017-02-22 01:23:18 +0000536 return Result;
537}
538
Sean Fertilef09d54e2019-07-09 19:21:01 +0000539MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
540 XCOFF::StorageMappingClass SMC,
Sean Fertile942537d2019-07-22 19:15:29 +0000541 XCOFF::SymbolType Type,
Sean Fertile1e46d4c2019-08-20 22:03:18 +0000542 XCOFF::StorageClass SC,
Sean Fertilef09d54e2019-07-09 19:21:01 +0000543 SectionKind Kind,
544 const char *BeginSymName) {
545 // Do the lookup. If we have a hit, return it.
546 auto IterBool = XCOFFUniquingMap.insert(
547 std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
548 auto &Entry = *IterBool.first;
549 if (!IterBool.second)
550 return Entry.second;
551
552 // Otherwise, return a new section.
553 StringRef CachedName = Entry.first.SectionName;
Jason Liu0dc05722019-11-08 09:26:28 -0500554 MCSymbol *QualName = getOrCreateSymbol(
555 CachedName + "[" + XCOFF::getMappingClassString(SMC) + "]");
Sean Fertilef09d54e2019-07-09 19:21:01 +0000556
557 MCSymbol *Begin = nullptr;
558 if (BeginSymName)
559 Begin = createTempSymbol(BeginSymName, false);
560
Jason Liu0dc05722019-11-08 09:26:28 -0500561 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate()) MCSectionXCOFF(
562 CachedName, SMC, Type, SC, Kind, cast<MCSymbolXCOFF>(QualName), Begin);
Sean Fertilef09d54e2019-07-09 19:21:01 +0000563 Entry.second = Result;
564
565 auto *F = new MCDataFragment();
566 Result->getFragmentList().insert(Result->begin(), F);
567 F->setParent(Result);
568
569 if (Begin)
570 Begin->setFragment(F);
571
572 return Result;
573}
574
Akira Hatanakab11ef082015-11-14 06:35:56 +0000575MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
576 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
577}
578
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000579void MCContext::addDebugPrefixMapEntry(const std::string &From,
580 const std::string &To) {
581 DebugPrefixMap.insert(std::make_pair(From, To));
582}
583
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000584void MCContext::RemapDebugPaths() {
585 const auto &DebugPrefixMap = this->DebugPrefixMap;
586 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
587 for (const auto &Entry : DebugPrefixMap)
588 if (StringRef(Path).startswith(Entry.first)) {
589 std::string RemappedPath =
590 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
591 Path.swap(RemappedPath);
592 }
593 };
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000594
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000595 // Remap compilation directory.
Benjamin Krameradcd0262020-01-28 20:23:46 +0100596 std::string CompDir = std::string(CompilationDir.str());
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000597 RemapDebugPath(CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000598 CompilationDir = CompDir;
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000599
600 // Remap MCDwarfDirs in all compilation units.
601 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
602 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
603 RemapDebugPath(Dir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000604}
605
Kevin Enderbye5930f12010-07-28 20:55:35 +0000606//===----------------------------------------------------------------------===//
607// Dwarf Management
608//===----------------------------------------------------------------------===//
609
Paul Robinson1ca25762019-03-01 20:58:04 +0000610void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
611 // MCDwarf needs the root file as well as the compilation directory.
612 // If we find a '.file 0' directive that will supersede these values.
Eric Christopher798e83b2019-04-04 23:34:38 +0000613 Optional<MD5::MD5Result> Cksum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000614 if (getDwarfVersion() >= 5) {
615 MD5 Hash;
Eric Christopher798e83b2019-04-04 23:34:38 +0000616 MD5::MD5Result Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000617 Hash.update(Buffer);
Eric Christopher798e83b2019-04-04 23:34:38 +0000618 Hash.final(Sum);
619 Cksum = Sum;
Paul Robinson1ca25762019-03-01 20:58:04 +0000620 }
621 // Canonicalize the root filename. It cannot be empty, and should not
622 // repeat the compilation dir.
Paul Robinson116e8d42019-05-21 11:52:27 +0000623 // The MCContext ctor initializes MainFileName to the name associated with
624 // the SrcMgr's main file ID, which might be the same as InputFileName (and
625 // possibly include directory components).
626 // Or, MainFileName might have been overridden by a -main-file-name option,
627 // which is supposed to be just a base filename with no directory component.
628 // So, if the InputFileName and MainFileName are not equal, assume
629 // MainFileName is a substitute basename and replace the last component.
630 SmallString<1024> FileNameBuf = InputFileName;
631 if (FileNameBuf.empty() || FileNameBuf == "-")
632 FileNameBuf = "<stdin>";
633 if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
634 llvm::sys::path::remove_filename(FileNameBuf);
635 llvm::sys::path::append(FileNameBuf, getMainFileName());
636 }
637 StringRef FileName = FileNameBuf;
Paul Robinson1ca25762019-03-01 20:58:04 +0000638 if (FileName.consume_front(getCompilationDir()))
Paul Robinsond8632c92019-03-01 22:28:13 +0000639 if (llvm::sys::path::is_separator(FileName.front()))
640 FileName = FileName.drop_front();
Paul Robinson1ca25762019-03-01 20:58:04 +0000641 assert(!FileName.empty());
642 setMCLineTableRootFile(
643 /*CUID=*/0, getCompilationDir(), FileName, Cksum, None);
644}
645
Scott Linder16c7bda2018-02-23 23:01:06 +0000646/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000647/// directory tables. If the file number has already been allocated it is an
648/// error and zero is returned and the client reports the error, else the
649/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson70def122018-02-22 21:03:33 +0000650Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
651 StringRef FileName,
652 unsigned FileNumber,
Eric Christopher798e83b2019-04-04 23:34:38 +0000653 Optional<MD5::MD5Result> Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000654 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000655 unsigned CUID) {
Ali Tamur02e96642019-03-26 20:05:27 +0000656 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Ali Tamur783d84b2019-04-19 02:26:56 +0000657 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
658 FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000659}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000660
Kevin Enderbya68d0042010-10-04 20:17:24 +0000661/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000662/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000663bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinson11539b02018-06-22 14:16:11 +0000664 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
665 if (FileNumber == 0)
Ali Tamur783d84b2019-04-19 02:26:56 +0000666 return getDwarfVersion() >= 5;
Paul Robinson11539b02018-06-22 14:16:11 +0000667 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000668 return false;
669
Paul Robinson11539b02018-06-22 14:16:11 +0000670 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000671}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000672
Fangrui Song58963e42018-09-08 02:04:20 +0000673/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000674/// useless debug info for them.
675void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000676 SectionsForRanges.remove_if(
677 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000678}
679
Reid Kleckner2214ed82016-01-29 00:49:42 +0000680CodeViewContext &MCContext::getCVContext() {
681 if (!CVContext.get())
682 CVContext.reset(new CodeViewContext);
683 return *CVContext.get();
684}
685
Oliver Stannard07b43d32015-11-17 09:58:07 +0000686//===----------------------------------------------------------------------===//
687// Error Reporting
688//===----------------------------------------------------------------------===//
689
690void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
691 HadError = true;
692
Sanne Wouda29338752017-02-08 14:48:05 +0000693 // If we have a source manager use it. Otherwise, try using the inline source
694 // manager.
695 // If that fails, use the generic report_fatal_error().
696 if (SrcMgr)
697 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
698 else if (InlineSrcMgr)
699 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
700 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000701 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000702}
703
Brian Cain6dbbd0f2019-08-08 19:13:23 +0000704void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
705 if (TargetOptions && TargetOptions->MCNoWarn)
706 return;
707 if (TargetOptions && TargetOptions->MCFatalWarnings)
708 reportError(Loc, Msg);
709 else {
710 // If we have a source manager use it. Otherwise, try using the inline
711 // source manager.
712 if (SrcMgr)
713 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
714 else if (InlineSrcMgr)
715 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
716 }
717}
718
Oliver Stannard07b43d32015-11-17 09:58:07 +0000719void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
720 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000721
722 // If we reached here, we are failing ungracefully. Run the interrupt handlers
723 // to make sure any special cleanups get done, in particular that we remove
724 // files registered with RemoveFileOnSignal.
725 sys::RunInterruptHandlers();
726 exit(1);
727}