blob: d7f95df726ff22f1ca71de5ce8632c284ef4ef79 [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"
Pete Cooperef21bd42015-03-04 01:24:11 +000029#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000030#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000031#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000032#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000033#include "llvm/MC/MCSymbolMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000034#include "llvm/MC/MCSymbolWasm.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000035#include "llvm/MC/SectionKind.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000036#include "llvm/Support/Casting.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000037#include "llvm/Support/CommandLine.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000038#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000039#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000040#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/SourceMgr.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000042#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000043#include <cassert>
44#include <cstdlib>
45#include <tuple>
46#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000047
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000048using namespace llvm;
49
Igor Laevsky7b998852016-06-17 15:19:41 +000050static cl::opt<char*>
51AsSecureLogFileName("as-secure-log-file-name",
52 cl::desc("As secure log file name (initialized from "
53 "AS_SECURE_LOG_FILE env variable)"),
54 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
55
Bill Wendlingbc07a892013-06-18 07:20:20 +000056MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000057 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000058 bool DoAutoReset)
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000059 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
60 Symbols(Allocator), UsedNames(Allocator),
61 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Eli Friedmana6e3a822018-10-12 19:41:05 +000062 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000063 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000064
Alp Tokera55b95b2014-07-06 10:33:31 +000065 if (SrcMgr && SrcMgr->getNumBuffers())
66 MainFileName =
67 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000068}
69
70MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000071 if (AutoReset)
72 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000073
74 // NOTE: The symbols are all allocated out of a bump pointer allocator,
75 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000076}
77
78//===----------------------------------------------------------------------===//
79// Module Lifetime Management
80//===----------------------------------------------------------------------===//
81
Pedro Artigas7212ee42012-12-12 22:59:46 +000082void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000083 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000084 COFFAllocator.DestroyAll();
85 ELFAllocator.DestroyAll();
86 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000087
Akira Hatanakab11ef082015-11-14 06:35:56 +000088 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000089 UsedNames.clear();
90 Symbols.clear();
91 Allocator.Reset();
92 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000093 CompilationDir.clear();
94 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000095 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000096 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000097 MCGenDwarfLabelEntries.clear();
98 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000099 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000100 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000101
Reid Kleckner2214ed82016-01-29 00:49:42 +0000102 CVContext.reset();
103
David Blaikie9ec32122014-04-10 23:55:11 +0000104 MachOUniquingMap.clear();
105 ELFUniquingMap.clear();
106 COFFUniquingMap.clear();
Sam Clegg105bdc22018-05-30 02:57:20 +0000107 WasmUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000108
Rafael Espindola9ab09232015-03-17 20:07:06 +0000109 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000110 AllowTemporaryLabels = true;
111 DwarfLocSeen = false;
112 GenDwarfForAssembly = false;
113 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000114
115 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000116}
117
Chris Lattner20731122010-04-08 20:30:37 +0000118//===----------------------------------------------------------------------===//
119// Symbol Manipulation
120//===----------------------------------------------------------------------===//
121
Jim Grosbach6f482002015-05-18 18:43:14 +0000122MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000123 SmallString<128> NameSV;
124 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000125
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000126 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000127
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000128 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000129 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000130 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000131
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000132 return Sym;
133}
134
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000135MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
136 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000137 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000138 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000139}
140
David Majnemera225a192015-03-31 22:35:44 +0000141MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000142 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000143 "$parent_frame_offset");
144}
145
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000146MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
147 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
148 FuncName);
149}
150
Rafael Espindolaa8695762015-06-02 00:25:12 +0000151MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
152 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000153 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000154 switch (MOFI->getObjectFileType()) {
155 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000156 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000157 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000158 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000159 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000160 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000161 case MCObjectFileInfo::IsWasm:
162 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000163 }
164 }
Pete Cooper234b8752015-06-09 18:36:13 +0000165 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
166 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000167}
168
Daniel Jasper41de8022015-06-23 11:31:32 +0000169MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
170 bool CanBeUnnamed) {
171 if (CanBeUnnamed && !UseNamesOnTempLabels)
172 return createSymbolImpl(nullptr, true);
173
Eric Christopher8e948952016-09-29 02:03:44 +0000174 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000175 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000176 bool IsTemporary = CanBeUnnamed;
177 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000178 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000179
Rafael Espindola9ab09232015-03-17 20:07:06 +0000180 SmallString<128> NewName = Name;
181 bool AddSuffix = AlwaysAddSuffix;
182 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000183 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000184 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000185 NewName.resize(Name.size());
186 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000187 }
188 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000189 if (NameEntry.second || !NameEntry.first->second) {
190 // Ok, we found a name.
191 // Mark it as used for a non-section symbol.
192 NameEntry.first->second = true;
193 // Have the MCSymbol object itself refer to the copy of the string that is
194 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000195 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000196 }
197 assert(IsTemporary && "Cannot rename non-temporary symbols");
198 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000199 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000200 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000201}
202
Daniel Jasper41de8022015-06-23 11:31:32 +0000203MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
204 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000205 SmallString<128> NameSV;
206 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000207 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000208}
209
Jim Grosbach6f482002015-05-18 18:43:14 +0000210MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000211 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000212 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000213 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000214}
215
Daniel Jasper41de8022015-06-23 11:31:32 +0000216MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
217 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000218}
219
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000220unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000221 MCLabel *&Label = Instances[LocalLabelVal];
222 if (!Label)
223 Label = new (*this) MCLabel(0);
224 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000225}
226
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000227unsigned MCContext::GetInstance(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->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000232}
233
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000234MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
235 unsigned Instance) {
236 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
237 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000238 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000239 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000240}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000241
Jim Grosbach6f482002015-05-18 18:43:14 +0000242MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000243 unsigned Instance = NextInstance(LocalLabelVal);
244 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
245}
246
Jim Grosbach6f482002015-05-18 18:43:14 +0000247MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000248 bool Before) {
249 unsigned Instance = GetInstance(LocalLabelVal);
250 if (!Before)
251 ++Instance;
252 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000253}
254
Jim Grosbach6f482002015-05-18 18:43:14 +0000255MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000256 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000257 StringRef NameRef = Name.toStringRef(NameSV);
258 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000259}
260
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000261void MCContext::setSymbolValue(MCStreamer &Streamer,
262 StringRef Sym,
263 uint64_t Val) {
264 auto Symbol = getOrCreateSymbol(Sym);
265 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000266}
267
Chris Lattner20731122010-04-08 20:30:37 +0000268//===----------------------------------------------------------------------===//
269// Section Management
270//===----------------------------------------------------------------------===//
271
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000272MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
273 unsigned TypeAndAttributes,
274 unsigned Reserved2, SectionKind Kind,
275 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000276 // We unique sections by their segment/section pair. The returned section
277 // may not have the same flags as the requested section, if so this should be
278 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000279
Chris Lattner20731122010-04-08 20:30:37 +0000280 // Form the name to look up.
281 SmallString<64> Name;
282 Name += Segment;
283 Name.push_back(',');
284 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000285
Chris Lattner20731122010-04-08 20:30:37 +0000286 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000287 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000288 if (Entry)
289 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000290
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000291 MCSymbol *Begin = nullptr;
292 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000293 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000294
Chris Lattner20731122010-04-08 20:30:37 +0000295 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000296 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
297 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000298}
Chris Lattner5418dd52010-04-08 21:26:26 +0000299
Richard Smithb910e562016-05-25 00:14:12 +0000300void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
301 StringRef GroupName;
302 if (const MCSymbol *Group = Section->getGroup())
303 GroupName = Group->getName();
304
305 unsigned UniqueID = Section->getUniqueID();
306 ELFUniquingMap.erase(
307 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
308 auto I = ELFUniquingMap.insert(std::make_pair(
309 ELFSectionKey{Name, GroupName, UniqueID},
310 Section))
311 .first;
312 StringRef CachedName = I->first.SectionName;
313 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
314}
315
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000316MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
317 unsigned Flags, SectionKind K,
318 unsigned EntrySize,
319 const MCSymbolELF *Group,
320 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000321 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000322 MCSymbolELF *R;
323 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000324 // A section symbol can not redefine regular symbols. There may be multiple
325 // sections with the same name, in which case the first such section wins.
326 if (Sym && Sym->isDefined() &&
327 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
328 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000329 if (Sym && Sym->isUndefined()) {
330 R = cast<MCSymbolELF>(Sym);
331 } else {
332 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
333 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
334 if (!Sym)
335 Sym = R;
336 }
337 R->setBinding(ELF::STB_LOCAL);
338 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000339
340 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
341 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
342
343 auto *F = new MCDataFragment();
344 Ret->getFragmentList().insert(Ret->begin(), F);
345 F->setParent(Ret);
346 R->setFragment(F);
347
348 return Ret;
349}
350
Eric Christopher36e601c2016-07-01 06:07:38 +0000351MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000352 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000353 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000354 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000355 StringMap<bool>::iterator I;
356 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000357 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000358 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000359
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000360 return createELFSectionImpl(
361 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
362 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000363}
364
Eric Christopher36e601c2016-07-01 06:07:38 +0000365MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
366 const Twine &Suffix, unsigned Type,
367 unsigned Flags,
368 unsigned EntrySize) {
369 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
370}
371
372MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000373 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000374 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000375 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000376 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000377 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000378 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000379
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000380 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000381 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000382}
383
Eric Christopher36e601c2016-07-01 06:07:38 +0000384MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000385 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000386 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000387 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000388 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000389 StringRef Group = "";
390 if (GroupSym)
391 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000392 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000393 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000394 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000395 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000396 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000397 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000398
Rafael Espindola44d50572015-03-27 21:34:24 +0000399 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000400
401 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000402 if (Flags & ELF::SHF_ARM_PURECODE)
403 Kind = SectionKind::getExecuteOnly();
404 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000405 Kind = SectionKind::getText();
406 else
407 Kind = SectionKind::getReadOnly();
408
George Rimar9fbecc92018-08-29 09:04:52 +0000409 MCSectionELF *Result = createELFSectionImpl(
410 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
411 Entry.second = Result;
412 return Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000413}
414
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000415MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000416 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
417 SectionKind::getReadOnly(), 4, Group, ~0,
418 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000419}
420
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000421MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
422 unsigned Characteristics,
423 SectionKind Kind,
424 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000425 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000426 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000427 MCSymbol *COMDATSymbol = nullptr;
428 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000429 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000430 COMDATSymName = COMDATSymbol->getName();
431 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000432
Reid Kleckner97837b72016-05-02 23:22:18 +0000433
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000434 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000435 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000436 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000437 auto Iter = IterBool.first;
438 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000439 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000440
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000441 MCSymbol *Begin = nullptr;
442 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000443 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000444
Rafael Espindola44d50572015-03-27 21:34:24 +0000445 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000446 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000447 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000448
449 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000450 return Result;
451}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000452
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000453MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
454 unsigned Characteristics,
455 SectionKind Kind,
456 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000457 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
458 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000459}
460
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000461MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000462 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000463 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000464 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000465 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000466 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000467}
468
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000469MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000470 const MCSymbol *KeySym,
471 unsigned UniqueID) {
472 // Return the normal section if we don't have to be associative or unique.
473 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000474 return Sec;
475
Reid Kleckner97837b72016-05-02 23:22:18 +0000476 // If we have a key symbol, make an associative section with the same name and
477 // kind as the normal section.
478 unsigned Characteristics = Sec->getCharacteristics();
479 if (KeySym) {
480 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
481 return getCOFFSection(Sec->getSectionName(), Characteristics,
482 Sec->getKind(), KeySym->getName(),
483 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
484 }
485
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000486 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000487 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000488}
489
Sam Clegg12fd3da2017-10-20 21:28:38 +0000490MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohman18eafb62017-02-22 01:23:18 +0000491 const Twine &Group, unsigned UniqueID,
492 const char *BeginSymName) {
493 MCSymbolWasm *GroupSym = nullptr;
Sam Cleggd423f0d2018-01-11 20:35:17 +0000494 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000495 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Cleggd423f0d2018-01-11 20:35:17 +0000496 GroupSym->setComdat(true);
497 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000498
Sam Clegg12fd3da2017-10-20 21:28:38 +0000499 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohman18eafb62017-02-22 01:23:18 +0000500}
501
Sam Clegg12fd3da2017-10-20 21:28:38 +0000502MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohman18eafb62017-02-22 01:23:18 +0000503 const MCSymbolWasm *GroupSym,
504 unsigned UniqueID,
505 const char *BeginSymName) {
506 StringRef Group = "";
507 if (GroupSym)
508 Group = GroupSym->getName();
509 // Do the lookup, if we have a hit, return it.
510 auto IterBool = WasmUniquingMap.insert(
511 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
512 auto &Entry = *IterBool.first;
513 if (!IterBool.second)
514 return Entry.second;
515
516 StringRef CachedName = Entry.first.SectionName;
517
Sam Cleggb210c642018-05-10 17:38:35 +0000518 MCSymbol *Begin = createSymbol(CachedName, false, false);
519 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000520
521 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg12fd3da2017-10-20 21:28:38 +0000522 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohman18eafb62017-02-22 01:23:18 +0000523 Entry.second = Result;
Sam Cleggb210c642018-05-10 17:38:35 +0000524
525 auto *F = new MCDataFragment();
526 Result->getFragmentList().insert(Result->begin(), F);
527 F->setParent(Result);
528 Begin->setFragment(F);
529
Dan Gohman18eafb62017-02-22 01:23:18 +0000530 return Result;
531}
532
Akira Hatanakab11ef082015-11-14 06:35:56 +0000533MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
534 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
535}
536
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000537void MCContext::addDebugPrefixMapEntry(const std::string &From,
538 const std::string &To) {
539 DebugPrefixMap.insert(std::make_pair(From, To));
540}
541
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000542void MCContext::RemapDebugPaths() {
543 const auto &DebugPrefixMap = this->DebugPrefixMap;
544 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
545 for (const auto &Entry : DebugPrefixMap)
546 if (StringRef(Path).startswith(Entry.first)) {
547 std::string RemappedPath =
548 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
549 Path.swap(RemappedPath);
550 }
551 };
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000552
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000553 // Remap compilation directory.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000554 std::string CompDir = CompilationDir.str();
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000555 RemapDebugPath(CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000556 CompilationDir = CompDir;
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000557
558 // Remap MCDwarfDirs in all compilation units.
559 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
560 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
561 RemapDebugPath(Dir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000562}
563
Kevin Enderbye5930f12010-07-28 20:55:35 +0000564//===----------------------------------------------------------------------===//
565// Dwarf Management
566//===----------------------------------------------------------------------===//
567
Scott Linder16c7bda2018-02-23 23:01:06 +0000568/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000569/// directory tables. If the file number has already been allocated it is an
570/// error and zero is returned and the client reports the error, else the
571/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson70def122018-02-22 21:03:33 +0000572Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
573 StringRef FileName,
574 unsigned FileNumber,
575 MD5::MD5Result *Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000576 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000577 unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000578 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Scott Linder16c7bda2018-02-23 23:01:06 +0000579 return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000580}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000581
Kevin Enderbya68d0042010-10-04 20:17:24 +0000582/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000583/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000584bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinson11539b02018-06-22 14:16:11 +0000585 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
586 if (FileNumber == 0)
587 return getDwarfVersion() >= 5 && LineTable.hasRootFile();
588 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000589 return false;
590
Paul Robinson11539b02018-06-22 14:16:11 +0000591 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000592}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000593
Fangrui Song58963e42018-09-08 02:04:20 +0000594/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000595/// useless debug info for them.
596void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000597 SectionsForRanges.remove_if(
598 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000599}
600
Reid Kleckner2214ed82016-01-29 00:49:42 +0000601CodeViewContext &MCContext::getCVContext() {
602 if (!CVContext.get())
603 CVContext.reset(new CodeViewContext);
604 return *CVContext.get();
605}
606
Oliver Stannard07b43d32015-11-17 09:58:07 +0000607//===----------------------------------------------------------------------===//
608// Error Reporting
609//===----------------------------------------------------------------------===//
610
611void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
612 HadError = true;
613
Sanne Wouda29338752017-02-08 14:48:05 +0000614 // If we have a source manager use it. Otherwise, try using the inline source
615 // manager.
616 // If that fails, use the generic report_fatal_error().
617 if (SrcMgr)
618 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
619 else if (InlineSrcMgr)
620 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
621 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000622 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000623}
624
625void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
626 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000627
628 // If we reached here, we are failing ungracefully. Run the interrupt handlers
629 // to make sure any special cleanups get done, in particular that we remove
630 // files registered with RemoveFileOnSignal.
631 sys::RunInterruptHandlers();
632 exit(1);
633}