blob: 48ee84edb096b0cfe2dc9524c3309ddb4aa9b7df [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"
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),
Eugene Zelenkod96089b2017-02-14 00:33:36 +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();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000107
Rafael Espindola9ab09232015-03-17 20:07:06 +0000108 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000109 AllowTemporaryLabels = true;
110 DwarfLocSeen = false;
111 GenDwarfForAssembly = false;
112 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000113
114 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000115}
116
Chris Lattner20731122010-04-08 20:30:37 +0000117//===----------------------------------------------------------------------===//
118// Symbol Manipulation
119//===----------------------------------------------------------------------===//
120
Jim Grosbach6f482002015-05-18 18:43:14 +0000121MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000122 SmallString<128> NameSV;
123 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000124
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000125 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000126
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000127 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000128 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000129 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000130
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000131 return Sym;
132}
133
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000134MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
135 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000136 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000137 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000138}
139
David Majnemera225a192015-03-31 22:35:44 +0000140MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000141 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000142 "$parent_frame_offset");
143}
144
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000145MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
146 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
147 FuncName);
148}
149
Rafael Espindolaa8695762015-06-02 00:25:12 +0000150MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
151 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000152 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000153 switch (MOFI->getObjectFileType()) {
154 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000155 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000156 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000157 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000158 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000159 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000160 case MCObjectFileInfo::IsWasm:
161 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000162 }
163 }
Pete Cooper234b8752015-06-09 18:36:13 +0000164 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
165 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000166}
167
Daniel Jasper41de8022015-06-23 11:31:32 +0000168MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
169 bool CanBeUnnamed) {
170 if (CanBeUnnamed && !UseNamesOnTempLabels)
171 return createSymbolImpl(nullptr, true);
172
Eric Christopher8e948952016-09-29 02:03:44 +0000173 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000174 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000175 bool IsTemporary = CanBeUnnamed;
176 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000177 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000178
Rafael Espindola9ab09232015-03-17 20:07:06 +0000179 SmallString<128> NewName = Name;
180 bool AddSuffix = AlwaysAddSuffix;
181 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000182 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000183 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000184 NewName.resize(Name.size());
185 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000186 }
187 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000188 if (NameEntry.second || !NameEntry.first->second) {
189 // Ok, we found a name.
190 // Mark it as used for a non-section symbol.
191 NameEntry.first->second = true;
192 // Have the MCSymbol object itself refer to the copy of the string that is
193 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000194 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000195 }
196 assert(IsTemporary && "Cannot rename non-temporary symbols");
197 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000198 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000199 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000200}
201
Daniel Jasper41de8022015-06-23 11:31:32 +0000202MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
203 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000204 SmallString<128> NameSV;
205 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000206 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000207}
208
Jim Grosbach6f482002015-05-18 18:43:14 +0000209MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000210 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000211 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000212 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000213}
214
Daniel Jasper41de8022015-06-23 11:31:32 +0000215MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
216 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000217}
218
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000219unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000220 MCLabel *&Label = Instances[LocalLabelVal];
221 if (!Label)
222 Label = new (*this) MCLabel(0);
223 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000224}
225
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000226unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000227 MCLabel *&Label = Instances[LocalLabelVal];
228 if (!Label)
229 Label = new (*this) MCLabel(0);
230 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000231}
232
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000233MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
234 unsigned Instance) {
235 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
236 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000237 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000238 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000239}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000240
Jim Grosbach6f482002015-05-18 18:43:14 +0000241MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000242 unsigned Instance = NextInstance(LocalLabelVal);
243 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
244}
245
Jim Grosbach6f482002015-05-18 18:43:14 +0000246MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000247 bool Before) {
248 unsigned Instance = GetInstance(LocalLabelVal);
249 if (!Before)
250 ++Instance;
251 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000252}
253
Jim Grosbach6f482002015-05-18 18:43:14 +0000254MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000255 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000256 StringRef NameRef = Name.toStringRef(NameSV);
257 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000258}
259
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000260void MCContext::setSymbolValue(MCStreamer &Streamer,
261 StringRef Sym,
262 uint64_t Val) {
263 auto Symbol = getOrCreateSymbol(Sym);
264 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000265}
266
Chris Lattner20731122010-04-08 20:30:37 +0000267//===----------------------------------------------------------------------===//
268// Section Management
269//===----------------------------------------------------------------------===//
270
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000271MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
272 unsigned TypeAndAttributes,
273 unsigned Reserved2, SectionKind Kind,
274 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000275 // We unique sections by their segment/section pair. The returned section
276 // may not have the same flags as the requested section, if so this should be
277 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000278
Chris Lattner20731122010-04-08 20:30:37 +0000279 // Form the name to look up.
280 SmallString<64> Name;
281 Name += Segment;
282 Name.push_back(',');
283 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000284
Chris Lattner20731122010-04-08 20:30:37 +0000285 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000286 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000287 if (Entry)
288 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000289
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000290 MCSymbol *Begin = nullptr;
291 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000292 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000293
Chris Lattner20731122010-04-08 20:30:37 +0000294 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000295 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
296 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000297}
Chris Lattner5418dd52010-04-08 21:26:26 +0000298
Richard Smithb910e562016-05-25 00:14:12 +0000299void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
300 StringRef GroupName;
301 if (const MCSymbol *Group = Section->getGroup())
302 GroupName = Group->getName();
303
304 unsigned UniqueID = Section->getUniqueID();
305 ELFUniquingMap.erase(
306 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
307 auto I = ELFUniquingMap.insert(std::make_pair(
308 ELFSectionKey{Name, GroupName, UniqueID},
309 Section))
310 .first;
311 StringRef CachedName = I->first.SectionName;
312 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
313}
314
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000315MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
316 unsigned Flags, SectionKind K,
317 unsigned EntrySize,
318 const MCSymbolELF *Group,
319 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000320 const MCSymbolELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000321 MCSymbolELF *R;
322 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov00400d32017-02-24 21:44:58 +0000323 // A section symbol can not redefine regular symbols. There may be multiple
324 // sections with the same name, in which case the first such section wins.
325 if (Sym && Sym->isDefined() &&
326 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
327 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000328 if (Sym && Sym->isUndefined()) {
329 R = cast<MCSymbolELF>(Sym);
330 } else {
331 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
332 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
333 if (!Sym)
334 Sym = R;
335 }
336 R->setBinding(ELF::STB_LOCAL);
337 R->setType(ELF::STT_SECTION);
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000338
339 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
340 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
341
342 auto *F = new MCDataFragment();
343 Ret->getFragmentList().insert(Ret->begin(), F);
344 F->setParent(Ret);
345 R->setFragment(F);
346
347 return Ret;
348}
349
Eric Christopher36e601c2016-07-01 06:07:38 +0000350MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000351 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000352 const MCSymbolELF *Group,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000353 const MCSectionELF *RelInfoSection) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000354 StringMap<bool>::iterator I;
355 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000356 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000357 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000358
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000359 return createELFSectionImpl(
360 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
361 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000362}
363
Eric Christopher36e601c2016-07-01 06:07:38 +0000364MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
365 const Twine &Suffix, unsigned Type,
366 unsigned Flags,
367 unsigned EntrySize) {
368 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
369}
370
371MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000372 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000373 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000374 const MCSymbolELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000375 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000376 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000377 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000378
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000379 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000380 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000381}
382
Eric Christopher36e601c2016-07-01 06:07:38 +0000383MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000384 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000385 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000386 unsigned UniqueID,
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000387 const MCSymbolELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000388 StringRef Group = "";
389 if (GroupSym)
390 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000391 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000392 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000393 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000394 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000395 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000396 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000397
Rafael Espindola44d50572015-03-27 21:34:24 +0000398 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000399
400 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000401 if (Flags & ELF::SHF_ARM_PURECODE)
402 Kind = SectionKind::getExecuteOnly();
403 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000404 Kind = SectionKind::getText();
405 else
406 Kind = SectionKind::getReadOnly();
407
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000408 MCSectionELF *Result = createELFSectionImpl(
409 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000410 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000411 return Result;
412}
413
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000414MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000415 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
416 SectionKind::getReadOnly(), 4, Group, ~0,
417 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000418}
419
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000420MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
421 unsigned Characteristics,
422 SectionKind Kind,
423 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000424 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000425 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000426 MCSymbol *COMDATSymbol = nullptr;
427 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000428 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000429 COMDATSymName = COMDATSymbol->getName();
430 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000431
Reid Kleckner97837b72016-05-02 23:22:18 +0000432
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000433 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000434 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000435 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000436 auto Iter = IterBool.first;
437 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000438 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000439
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000440 MCSymbol *Begin = nullptr;
441 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000442 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000443
Rafael Espindola44d50572015-03-27 21:34:24 +0000444 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000445 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000446 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000447
448 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000449 return Result;
450}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000451
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000452MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
453 unsigned Characteristics,
454 SectionKind Kind,
455 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000456 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
457 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000458}
459
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000460MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000461 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000462 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000463 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000464 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000465 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000466}
467
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000468MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000469 const MCSymbol *KeySym,
470 unsigned UniqueID) {
471 // Return the normal section if we don't have to be associative or unique.
472 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000473 return Sec;
474
Reid Kleckner97837b72016-05-02 23:22:18 +0000475 // If we have a key symbol, make an associative section with the same name and
476 // kind as the normal section.
477 unsigned Characteristics = Sec->getCharacteristics();
478 if (KeySym) {
479 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
480 return getCOFFSection(Sec->getSectionName(), Characteristics,
481 Sec->getKind(), KeySym->getName(),
482 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
483 }
484
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000485 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000486 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000487}
488
Dan Gohman18eafb62017-02-22 01:23:18 +0000489void MCContext::renameWasmSection(MCSectionWasm *Section, StringRef Name) {
490 StringRef GroupName;
491 assert(!Section->getGroup() && "not yet implemented");
492
493 unsigned UniqueID = Section->getUniqueID();
494 WasmUniquingMap.erase(
495 WasmSectionKey{Section->getSectionName(), GroupName, UniqueID});
496 auto I = WasmUniquingMap.insert(std::make_pair(
497 WasmSectionKey{Name, GroupName, UniqueID},
498 Section))
499 .first;
500 StringRef CachedName = I->first.SectionName;
501 const_cast<MCSectionWasm *>(Section)->setSectionName(CachedName);
502}
503
504MCSectionWasm *MCContext::createWasmRelSection(const Twine &Name, unsigned Type,
505 unsigned Flags,
506 const MCSymbolWasm *Group) {
507 StringMap<bool>::iterator I;
508 bool Inserted;
509 std::tie(I, Inserted) =
510 RelSecNames.insert(std::make_pair(Name.str(), true));
511
512 return new (WasmAllocator.Allocate())
513 MCSectionWasm(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
514 Group, ~0, nullptr);
515}
516
517MCSectionWasm *MCContext::getWasmNamedSection(const Twine &Prefix,
518 const Twine &Suffix, unsigned Type,
519 unsigned Flags) {
520 return getWasmSection(Prefix + "." + Suffix, Type, Flags, Suffix);
521}
522
523MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
524 unsigned Flags,
525 const Twine &Group, unsigned UniqueID,
526 const char *BeginSymName) {
527 MCSymbolWasm *GroupSym = nullptr;
528 if (!Group.isTriviallyEmpty() && !Group.str().empty())
529 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
530
531 return getWasmSection(Section, Type, Flags, GroupSym, UniqueID, BeginSymName);
532}
533
534MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
535 unsigned Flags,
536 const MCSymbolWasm *GroupSym,
537 unsigned UniqueID,
538 const char *BeginSymName) {
539 StringRef Group = "";
540 if (GroupSym)
541 Group = GroupSym->getName();
542 // Do the lookup, if we have a hit, return it.
543 auto IterBool = WasmUniquingMap.insert(
544 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
545 auto &Entry = *IterBool.first;
546 if (!IterBool.second)
547 return Entry.second;
548
549 StringRef CachedName = Entry.first.SectionName;
550
551 SectionKind Kind = SectionKind::getText();
552
553 MCSymbol *Begin = nullptr;
554 if (BeginSymName)
555 Begin = createTempSymbol(BeginSymName, false);
556
557 MCSectionWasm *Result = new (WasmAllocator.Allocate())
558 MCSectionWasm(CachedName, Type, Flags, Kind, GroupSym, UniqueID, Begin);
559 Entry.second = Result;
560 return Result;
561}
562
Akira Hatanakab11ef082015-11-14 06:35:56 +0000563MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
564 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
565}
566
Kevin Enderbye5930f12010-07-28 20:55:35 +0000567//===----------------------------------------------------------------------===//
568// Dwarf Management
569//===----------------------------------------------------------------------===//
570
Jim Grosbach6f482002015-05-18 18:43:14 +0000571/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000572/// directory tables. If the file number has already been allocated it is an
573/// error and zero is returned and the client reports the error, else the
574/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000575unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000576 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000577 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000578 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000579}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000580
Kevin Enderbya68d0042010-10-04 20:17:24 +0000581/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000582/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000583bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000584 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
585 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000586 return false;
587
David Blaikiea55ddad2014-03-13 18:55:04 +0000588 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000589}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000590
Rafael Espindolae0746792015-05-21 16:52:32 +0000591/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000592/// useless debug info for them.
593void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000594 SectionsForRanges.remove_if(
595 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000596}
597
Reid Kleckner2214ed82016-01-29 00:49:42 +0000598CodeViewContext &MCContext::getCVContext() {
599 if (!CVContext.get())
600 CVContext.reset(new CodeViewContext);
601 return *CVContext.get();
602}
603
Oliver Stannard07b43d32015-11-17 09:58:07 +0000604//===----------------------------------------------------------------------===//
605// Error Reporting
606//===----------------------------------------------------------------------===//
607
608void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
609 HadError = true;
610
Sanne Wouda29338752017-02-08 14:48:05 +0000611 // If we have a source manager use it. Otherwise, try using the inline source
612 // manager.
613 // If that fails, use the generic report_fatal_error().
614 if (SrcMgr)
615 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
616 else if (InlineSrcMgr)
617 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
618 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000619 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000620}
621
622void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
623 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000624
625 // If we reached here, we are failing ungracefully. Run the interrupt handlers
626 // to make sure any special cleanups get done, in particular that we remove
627 // files registered with RemoveFileOnSignal.
628 sys::RunInterruptHandlers();
629 exit(1);
630}