blob: fab517075c5ac6aef8d7f65fbd94d7910676a205 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCContext.h"
Scott Linder16c7bda2018-02-23 23:01:06 +000011#include "llvm/ADT/Optional.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000016#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/COFF.h"
18#include "llvm/BinaryFormat/ELF.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"
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"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000036#include "llvm/MC/SectionKind.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000037#include "llvm/Support/Casting.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000038#include "llvm/Support/CommandLine.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000039#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000040#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000041#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/SourceMgr.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000043#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000044#include <cassert>
45#include <cstdlib>
46#include <tuple>
47#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000048
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000049using namespace llvm;
50
Igor Laevsky7b998852016-06-17 15:19:41 +000051static cl::opt<char*>
52AsSecureLogFileName("as-secure-log-file-name",
53 cl::desc("As secure log file name (initialized from "
54 "AS_SECURE_LOG_FILE env variable)"),
55 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
56
Bill Wendlingbc07a892013-06-18 07:20:20 +000057MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000058 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000059 bool DoAutoReset)
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000060 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
61 Symbols(Allocator), UsedNames(Allocator),
62 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Eugene Zelenkod96089b2017-02-14 00:33:36 +000063 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000064 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000065
Alp Tokera55b95b2014-07-06 10:33:31 +000066 if (SrcMgr && SrcMgr->getNumBuffers())
67 MainFileName =
68 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000069}
70
71MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000072 if (AutoReset)
73 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000074
75 // NOTE: The symbols are all allocated out of a bump pointer allocator,
76 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000077}
78
79//===----------------------------------------------------------------------===//
80// Module Lifetime Management
81//===----------------------------------------------------------------------===//
82
Pedro Artigas7212ee42012-12-12 22:59:46 +000083void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000084 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000085 COFFAllocator.DestroyAll();
86 ELFAllocator.DestroyAll();
87 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000088
Akira Hatanakab11ef082015-11-14 06:35:56 +000089 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000090 UsedNames.clear();
91 Symbols.clear();
92 Allocator.Reset();
93 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000094 CompilationDir.clear();
95 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000096 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000097 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000098 MCGenDwarfLabelEntries.clear();
99 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +0000100 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000101 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000102
Reid Kleckner2214ed82016-01-29 00:49:42 +0000103 CVContext.reset();
104
David Blaikie9ec32122014-04-10 23:55:11 +0000105 MachOUniquingMap.clear();
106 ELFUniquingMap.clear();
107 COFFUniquingMap.clear();
Sam Clegg105bdc22018-05-30 02:57:20 +0000108 WasmUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000109
Rafael Espindola9ab09232015-03-17 20:07:06 +0000110 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000111 AllowTemporaryLabels = true;
112 DwarfLocSeen = false;
113 GenDwarfForAssembly = false;
114 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000115
116 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000117}
118
Chris Lattner20731122010-04-08 20:30:37 +0000119//===----------------------------------------------------------------------===//
120// Symbol Manipulation
121//===----------------------------------------------------------------------===//
122
Jim Grosbach6f482002015-05-18 18:43:14 +0000123MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000124 SmallString<128> NameSV;
125 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000126
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000127 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000128
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000129 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000130 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000131 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000132
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000133 return Sym;
134}
135
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000136MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
137 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000138 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000139 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000140}
141
David Majnemera225a192015-03-31 22:35:44 +0000142MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000143 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000144 "$parent_frame_offset");
145}
146
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000147MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
148 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
149 FuncName);
150}
151
Rafael Espindolaa8695762015-06-02 00:25:12 +0000152MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
153 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000154 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000155 switch (MOFI->getObjectFileType()) {
156 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000157 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000158 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000159 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000160 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000161 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000162 case MCObjectFileInfo::IsWasm:
163 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000164 }
165 }
Pete Cooper234b8752015-06-09 18:36:13 +0000166 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
167 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000168}
169
Daniel Jasper41de8022015-06-23 11:31:32 +0000170MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
171 bool CanBeUnnamed) {
172 if (CanBeUnnamed && !UseNamesOnTempLabels)
173 return createSymbolImpl(nullptr, true);
174
Eric Christopher8e948952016-09-29 02:03:44 +0000175 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000176 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000177 bool IsTemporary = CanBeUnnamed;
178 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000179 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000180
Rafael Espindola9ab09232015-03-17 20:07:06 +0000181 SmallString<128> NewName = Name;
182 bool AddSuffix = AlwaysAddSuffix;
183 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000184 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000185 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000186 NewName.resize(Name.size());
187 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000188 }
189 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000190 if (NameEntry.second || !NameEntry.first->second) {
191 // Ok, we found a name.
192 // Mark it as used for a non-section symbol.
193 NameEntry.first->second = true;
194 // Have the MCSymbol object itself refer to the copy of the string that is
195 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000196 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000197 }
198 assert(IsTemporary && "Cannot rename non-temporary symbols");
199 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000200 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000201 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000202}
203
Daniel Jasper41de8022015-06-23 11:31:32 +0000204MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
205 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000206 SmallString<128> NameSV;
207 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000208 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000209}
210
Jim Grosbach6f482002015-05-18 18:43:14 +0000211MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000212 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000213 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000214 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000215}
216
Daniel Jasper41de8022015-06-23 11:31:32 +0000217MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
218 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000219}
220
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000221unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000222 MCLabel *&Label = Instances[LocalLabelVal];
223 if (!Label)
224 Label = new (*this) MCLabel(0);
225 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000226}
227
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000228unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000229 MCLabel *&Label = Instances[LocalLabelVal];
230 if (!Label)
231 Label = new (*this) MCLabel(0);
232 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000233}
234
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000235MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
236 unsigned Instance) {
237 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
238 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000239 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000240 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000241}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000242
Jim Grosbach6f482002015-05-18 18:43:14 +0000243MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000244 unsigned Instance = NextInstance(LocalLabelVal);
245 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
246}
247
Jim Grosbach6f482002015-05-18 18:43:14 +0000248MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000249 bool Before) {
250 unsigned Instance = GetInstance(LocalLabelVal);
251 if (!Before)
252 ++Instance;
253 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000254}
255
Jim Grosbach6f482002015-05-18 18:43:14 +0000256MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000257 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000258 StringRef NameRef = Name.toStringRef(NameSV);
259 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000260}
261
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000262void MCContext::setSymbolValue(MCStreamer &Streamer,
263 StringRef Sym,
264 uint64_t Val) {
265 auto Symbol = getOrCreateSymbol(Sym);
266 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000267}
268
Chris Lattner20731122010-04-08 20:30:37 +0000269//===----------------------------------------------------------------------===//
270// Section Management
271//===----------------------------------------------------------------------===//
272
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000273MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
274 unsigned TypeAndAttributes,
275 unsigned Reserved2, SectionKind Kind,
276 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000277 // We unique sections by their segment/section pair. The returned section
278 // may not have the same flags as the requested section, if so this should be
279 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000280
Chris Lattner20731122010-04-08 20:30:37 +0000281 // Form the name to look up.
282 SmallString<64> Name;
283 Name += Segment;
284 Name.push_back(',');
285 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000286
Chris Lattner20731122010-04-08 20:30:37 +0000287 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000288 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000289 if (Entry)
290 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000291
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000292 MCSymbol *Begin = nullptr;
293 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000294 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000295
Chris Lattner20731122010-04-08 20:30:37 +0000296 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000297 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
298 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000299}
Chris Lattner5418dd52010-04-08 21:26:26 +0000300
Richard Smithb910e562016-05-25 00:14:12 +0000301void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
302 StringRef GroupName;
303 if (const MCSymbol *Group = Section->getGroup())
304 GroupName = Group->getName();
305
306 unsigned UniqueID = Section->getUniqueID();
307 ELFUniquingMap.erase(
308 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
309 auto I = ELFUniquingMap.insert(std::make_pair(
310 ELFSectionKey{Name, GroupName, UniqueID},
311 Section))
312 .first;
313 StringRef CachedName = I->first.SectionName;
314 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
315}
316
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000317MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
318 unsigned Flags, SectionKind K,
319 unsigned EntrySize,
320 const MCSymbolELF *Group,
321 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000322 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000323 MCSymbolELF *R;
324 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000325 // A section symbol can not redefine regular symbols. There may be multiple
326 // sections with the same name, in which case the first such section wins.
327 if (Sym && Sym->isDefined() &&
328 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
329 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000330 if (Sym && Sym->isUndefined()) {
331 R = cast<MCSymbolELF>(Sym);
332 } else {
333 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
334 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
335 if (!Sym)
336 Sym = R;
337 }
338 R->setBinding(ELF::STB_LOCAL);
339 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000340
341 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
342 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
343
344 auto *F = new MCDataFragment();
345 Ret->getFragmentList().insert(Ret->begin(), F);
346 F->setParent(Ret);
347 R->setFragment(F);
348
349 return Ret;
350}
351
Eric Christopher36e601c2016-07-01 06:07:38 +0000352MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000353 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000354 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000355 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000356 StringMap<bool>::iterator I;
357 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000358 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000359 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000360
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000361 return createELFSectionImpl(
362 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
363 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000364}
365
Eric Christopher36e601c2016-07-01 06:07:38 +0000366MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
367 const Twine &Suffix, unsigned Type,
368 unsigned Flags,
369 unsigned EntrySize) {
370 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
371}
372
373MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000374 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000375 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000376 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000377 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000378 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000379 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000380
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000381 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000382 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000383}
384
Eric Christopher36e601c2016-07-01 06:07:38 +0000385MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000386 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000387 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000388 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000389 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000390 StringRef Group = "";
391 if (GroupSym)
392 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000393 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000394 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000395 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000396 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000397 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000398 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000399
Rafael Espindola44d50572015-03-27 21:34:24 +0000400 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000401
402 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000403 if (Flags & ELF::SHF_ARM_PURECODE)
404 Kind = SectionKind::getExecuteOnly();
405 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000406 Kind = SectionKind::getText();
407 else
408 Kind = SectionKind::getReadOnly();
409
George Rimar9fbecc92018-08-29 09:04:52 +0000410 MCSectionELF *Result = createELFSectionImpl(
411 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
412 Entry.second = Result;
413 return Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000414}
415
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000416MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000417 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
418 SectionKind::getReadOnly(), 4, Group, ~0,
419 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000420}
421
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000422MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
423 unsigned Characteristics,
424 SectionKind Kind,
425 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000426 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000427 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000428 MCSymbol *COMDATSymbol = nullptr;
429 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000430 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000431 COMDATSymName = COMDATSymbol->getName();
432 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000433
Reid Kleckner97837b72016-05-02 23:22:18 +0000434
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000435 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000436 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000437 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000438 auto Iter = IterBool.first;
439 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000440 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000441
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000442 MCSymbol *Begin = nullptr;
443 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000444 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000445
Rafael Espindola44d50572015-03-27 21:34:24 +0000446 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000447 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000448 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000449
450 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000451 return Result;
452}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000453
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000454MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
455 unsigned Characteristics,
456 SectionKind Kind,
457 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000458 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
459 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000460}
461
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000462MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000463 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000464 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000465 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000466 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000467 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000468}
469
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000470MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000471 const MCSymbol *KeySym,
472 unsigned UniqueID) {
473 // Return the normal section if we don't have to be associative or unique.
474 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000475 return Sec;
476
Reid Kleckner97837b72016-05-02 23:22:18 +0000477 // If we have a key symbol, make an associative section with the same name and
478 // kind as the normal section.
479 unsigned Characteristics = Sec->getCharacteristics();
480 if (KeySym) {
481 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
482 return getCOFFSection(Sec->getSectionName(), Characteristics,
483 Sec->getKind(), KeySym->getName(),
484 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
485 }
486
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000487 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000488 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000489}
490
Sam Clegg12fd3da2017-10-20 21:28:38 +0000491MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohman18eafb62017-02-22 01:23:18 +0000492 const Twine &Group, unsigned UniqueID,
493 const char *BeginSymName) {
494 MCSymbolWasm *GroupSym = nullptr;
Sam Cleggd423f0d2018-01-11 20:35:17 +0000495 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000496 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Cleggd423f0d2018-01-11 20:35:17 +0000497 GroupSym->setComdat(true);
498 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000499
Sam Clegg12fd3da2017-10-20 21:28:38 +0000500 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohman18eafb62017-02-22 01:23:18 +0000501}
502
Sam Clegg12fd3da2017-10-20 21:28:38 +0000503MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohman18eafb62017-02-22 01:23:18 +0000504 const MCSymbolWasm *GroupSym,
505 unsigned UniqueID,
506 const char *BeginSymName) {
507 StringRef Group = "";
508 if (GroupSym)
509 Group = GroupSym->getName();
510 // Do the lookup, if we have a hit, return it.
511 auto IterBool = WasmUniquingMap.insert(
512 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
513 auto &Entry = *IterBool.first;
514 if (!IterBool.second)
515 return Entry.second;
516
517 StringRef CachedName = Entry.first.SectionName;
518
Sam Cleggb210c642018-05-10 17:38:35 +0000519 MCSymbol *Begin = createSymbol(CachedName, false, false);
520 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000521
522 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg12fd3da2017-10-20 21:28:38 +0000523 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohman18eafb62017-02-22 01:23:18 +0000524 Entry.second = Result;
Sam Cleggb210c642018-05-10 17:38:35 +0000525
526 auto *F = new MCDataFragment();
527 Result->getFragmentList().insert(Result->begin(), F);
528 F->setParent(Result);
529 Begin->setFragment(F);
530
Dan Gohman18eafb62017-02-22 01:23:18 +0000531 return Result;
532}
533
Akira Hatanakab11ef082015-11-14 06:35:56 +0000534MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
535 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
536}
537
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000538void MCContext::addDebugPrefixMapEntry(const std::string &From,
539 const std::string &To) {
540 DebugPrefixMap.insert(std::make_pair(From, To));
541}
542
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000543void MCContext::RemapDebugPaths() {
544 const auto &DebugPrefixMap = this->DebugPrefixMap;
545 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
546 for (const auto &Entry : DebugPrefixMap)
547 if (StringRef(Path).startswith(Entry.first)) {
548 std::string RemappedPath =
549 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
550 Path.swap(RemappedPath);
551 }
552 };
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000553
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000554 // Remap compilation directory.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000555 std::string CompDir = CompilationDir.str();
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000556 RemapDebugPath(CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000557 CompilationDir = CompDir;
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000558
559 // Remap MCDwarfDirs in all compilation units.
560 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
561 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
562 RemapDebugPath(Dir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000563}
564
Kevin Enderbye5930f12010-07-28 20:55:35 +0000565//===----------------------------------------------------------------------===//
566// Dwarf Management
567//===----------------------------------------------------------------------===//
568
Scott Linder16c7bda2018-02-23 23:01:06 +0000569/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000570/// directory tables. If the file number has already been allocated it is an
571/// error and zero is returned and the client reports the error, else the
572/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson70def122018-02-22 21:03:33 +0000573Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
574 StringRef FileName,
575 unsigned FileNumber,
576 MD5::MD5Result *Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000577 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000578 unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000579 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Scott Linder16c7bda2018-02-23 23:01:06 +0000580 return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000581}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000582
Kevin Enderbya68d0042010-10-04 20:17:24 +0000583/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000584/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000585bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinson11539b02018-06-22 14:16:11 +0000586 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
587 if (FileNumber == 0)
588 return getDwarfVersion() >= 5 && LineTable.hasRootFile();
589 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000590 return false;
591
Paul Robinson11539b02018-06-22 14:16:11 +0000592 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000593}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000594
Fangrui Song58963e42018-09-08 02:04:20 +0000595/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000596/// useless debug info for them.
597void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000598 SectionsForRanges.remove_if(
599 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000600}
601
Reid Kleckner2214ed82016-01-29 00:49:42 +0000602CodeViewContext &MCContext::getCVContext() {
603 if (!CVContext.get())
604 CVContext.reset(new CodeViewContext);
605 return *CVContext.get();
606}
607
Oliver Stannard07b43d32015-11-17 09:58:07 +0000608//===----------------------------------------------------------------------===//
609// Error Reporting
610//===----------------------------------------------------------------------===//
611
612void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
613 HadError = true;
614
Sanne Wouda29338752017-02-08 14:48:05 +0000615 // If we have a source manager use it. Otherwise, try using the inline source
616 // manager.
617 // If that fails, use the generic report_fatal_error().
618 if (SrcMgr)
619 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
620 else if (InlineSrcMgr)
621 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
622 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000623 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000624}
625
626void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
627 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000628
629 // If we reached here, we are failing ungracefully. Run the interrupt handlers
630 // to make sure any special cleanups get done, in particular that we remove
631 // files registered with RemoveFileOnSignal.
632 sys::RunInterruptHandlers();
633 exit(1);
634}