blob: 3dfc0af1c0370fb8084a3359e94f439df0146f87 [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
Chris Lattner86dfd732009-10-19 22:49:00 +000010#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringRef.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000014#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAsmInfo.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000016#include "llvm/MC/MCCodeView.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000019#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCLabel.h"
22#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSectionCOFF.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSectionMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000026#include "llvm/MC/MCSectionWasm.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000027#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000028#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000029#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000030#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000031#include "llvm/MC/MCSymbolMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000032#include "llvm/MC/MCSymbolWasm.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000033#include "llvm/MC/SectionKind.h"
34#include "llvm/Support/Casting.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000035#include "llvm/Support/COFF.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000036#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000037#include "llvm/Support/ELF.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"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include "llvm/Support/raw_ostream.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"
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)
Eugene Zelenkod96089b2017-02-14 00:33:36 +000059 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Symbols(Allocator),
60 UsedNames(Allocator), CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
61 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000062 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000063
Alp Tokera55b95b2014-07-06 10:33:31 +000064 if (SrcMgr && SrcMgr->getNumBuffers())
65 MainFileName =
66 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000067}
68
69MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000070 if (AutoReset)
71 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000072
73 // NOTE: The symbols are all allocated out of a bump pointer allocator,
74 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000075}
76
77//===----------------------------------------------------------------------===//
78// Module Lifetime Management
79//===----------------------------------------------------------------------===//
80
Pedro Artigas7212ee42012-12-12 22:59:46 +000081void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000082 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000083 COFFAllocator.DestroyAll();
84 ELFAllocator.DestroyAll();
85 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000086
Akira Hatanakab11ef082015-11-14 06:35:56 +000087 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000088 UsedNames.clear();
89 Symbols.clear();
90 Allocator.Reset();
91 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000092 CompilationDir.clear();
93 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000094 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000095 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000096 MCGenDwarfLabelEntries.clear();
97 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000098 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000099 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000100
Reid Kleckner2214ed82016-01-29 00:49:42 +0000101 CVContext.reset();
102
David Blaikie9ec32122014-04-10 23:55:11 +0000103 MachOUniquingMap.clear();
104 ELFUniquingMap.clear();
105 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000106
Rafael Espindola9ab09232015-03-17 20:07:06 +0000107 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000108 AllowTemporaryLabels = true;
109 DwarfLocSeen = false;
110 GenDwarfForAssembly = false;
111 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000112
113 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000114}
115
Chris Lattner20731122010-04-08 20:30:37 +0000116//===----------------------------------------------------------------------===//
117// Symbol Manipulation
118//===----------------------------------------------------------------------===//
119
Jim Grosbach6f482002015-05-18 18:43:14 +0000120MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000121 SmallString<128> NameSV;
122 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000123
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000124 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000125
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000126 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000127 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000128 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000129
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000130 return Sym;
131}
132
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000133MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
134 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000135 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000136 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000137}
138
David Majnemera225a192015-03-31 22:35:44 +0000139MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000140 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000141 "$parent_frame_offset");
142}
143
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000144MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
145 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
146 FuncName);
147}
148
Rafael Espindolaa8695762015-06-02 00:25:12 +0000149MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
150 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000151 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000152 switch (MOFI->getObjectFileType()) {
153 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000154 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000155 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000156 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000157 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000158 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000159 case MCObjectFileInfo::IsWasm:
160 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000161 }
162 }
Pete Cooper234b8752015-06-09 18:36:13 +0000163 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
164 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000165}
166
Daniel Jasper41de8022015-06-23 11:31:32 +0000167MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
168 bool CanBeUnnamed) {
169 if (CanBeUnnamed && !UseNamesOnTempLabels)
170 return createSymbolImpl(nullptr, true);
171
Eric Christopher8e948952016-09-29 02:03:44 +0000172 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000173 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000174 bool IsTemporary = CanBeUnnamed;
175 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000176 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000177
Rafael Espindola9ab09232015-03-17 20:07:06 +0000178 SmallString<128> NewName = Name;
179 bool AddSuffix = AlwaysAddSuffix;
180 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000181 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000182 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000183 NewName.resize(Name.size());
184 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000185 }
186 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000187 if (NameEntry.second || !NameEntry.first->second) {
188 // Ok, we found a name.
189 // Mark it as used for a non-section symbol.
190 NameEntry.first->second = true;
191 // Have the MCSymbol object itself refer to the copy of the string that is
192 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000193 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000194 }
195 assert(IsTemporary && "Cannot rename non-temporary symbols");
196 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000197 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000198 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000199}
200
Daniel Jasper41de8022015-06-23 11:31:32 +0000201MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
202 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000203 SmallString<128> NameSV;
204 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000205 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000206}
207
Jim Grosbach6f482002015-05-18 18:43:14 +0000208MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000209 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000210 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000211 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000212}
213
Daniel Jasper41de8022015-06-23 11:31:32 +0000214MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
215 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000216}
217
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000218unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000219 MCLabel *&Label = Instances[LocalLabelVal];
220 if (!Label)
221 Label = new (*this) MCLabel(0);
222 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000223}
224
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000225unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000226 MCLabel *&Label = Instances[LocalLabelVal];
227 if (!Label)
228 Label = new (*this) MCLabel(0);
229 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000230}
231
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000232MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
233 unsigned Instance) {
234 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
235 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000236 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000237 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000238}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000239
Jim Grosbach6f482002015-05-18 18:43:14 +0000240MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000241 unsigned Instance = NextInstance(LocalLabelVal);
242 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
243}
244
Jim Grosbach6f482002015-05-18 18:43:14 +0000245MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000246 bool Before) {
247 unsigned Instance = GetInstance(LocalLabelVal);
248 if (!Before)
249 ++Instance;
250 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000251}
252
Jim Grosbach6f482002015-05-18 18:43:14 +0000253MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000254 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000255 StringRef NameRef = Name.toStringRef(NameSV);
256 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000257}
258
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000259void MCContext::setSymbolValue(MCStreamer &Streamer,
260 StringRef Sym,
261 uint64_t Val) {
262 auto Symbol = getOrCreateSymbol(Sym);
263 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000264}
265
Chris Lattner20731122010-04-08 20:30:37 +0000266//===----------------------------------------------------------------------===//
267// Section Management
268//===----------------------------------------------------------------------===//
269
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000270MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
271 unsigned TypeAndAttributes,
272 unsigned Reserved2, SectionKind Kind,
273 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000274 // We unique sections by their segment/section pair. The returned section
275 // may not have the same flags as the requested section, if so this should be
276 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000277
Chris Lattner20731122010-04-08 20:30:37 +0000278 // Form the name to look up.
279 SmallString<64> Name;
280 Name += Segment;
281 Name.push_back(',');
282 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000283
Chris Lattner20731122010-04-08 20:30:37 +0000284 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000285 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000286 if (Entry)
287 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000288
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000289 MCSymbol *Begin = nullptr;
290 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000291 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000292
Chris Lattner20731122010-04-08 20:30:37 +0000293 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000294 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
295 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000296}
Chris Lattner5418dd52010-04-08 21:26:26 +0000297
Richard Smithb910e562016-05-25 00:14:12 +0000298void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
299 StringRef GroupName;
300 if (const MCSymbol *Group = Section->getGroup())
301 GroupName = Group->getName();
302
303 unsigned UniqueID = Section->getUniqueID();
304 ELFUniquingMap.erase(
305 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
306 auto I = ELFUniquingMap.insert(std::make_pair(
307 ELFSectionKey{Name, GroupName, UniqueID},
308 Section))
309 .first;
310 StringRef CachedName = I->first.SectionName;
311 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
312}
313
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000314MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
315 unsigned Flags, SectionKind K,
316 unsigned EntrySize,
317 const MCSymbolELF *Group,
318 unsigned UniqueID,
319 const MCSectionELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000320 MCSymbolELF *R;
321 MCSymbol *&Sym = Symbols[Section];
322 if (Sym && Sym->isUndefined()) {
323 R = cast<MCSymbolELF>(Sym);
324 } else {
325 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
326 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
327 if (!Sym)
328 Sym = R;
329 }
330 R->setBinding(ELF::STB_LOCAL);
331 R->setType(ELF::STT_SECTION);
332 R->setRedefinable(true);
333
334 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
335 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
336
337 auto *F = new MCDataFragment();
338 Ret->getFragmentList().insert(Ret->begin(), F);
339 F->setParent(Ret);
340 R->setFragment(F);
341
342 return Ret;
343}
344
Eric Christopher36e601c2016-07-01 06:07:38 +0000345MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000346 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000347 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000348 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000349 StringMap<bool>::iterator I;
350 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000351 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000352 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000353
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000354 return createELFSectionImpl(I->getKey(), Type, Flags,
355 SectionKind::getReadOnly(), EntrySize, Group,
356 true, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000357}
358
Eric Christopher36e601c2016-07-01 06:07:38 +0000359MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
360 const Twine &Suffix, unsigned Type,
361 unsigned Flags,
362 unsigned EntrySize) {
363 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
364}
365
366MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000367 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000368 const Twine &Group, unsigned UniqueID,
369 const MCSectionELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000370 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000371 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000372 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000373
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000374 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000375 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000376}
377
Eric Christopher36e601c2016-07-01 06:07:38 +0000378MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000379 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000380 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000381 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000382 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000383 StringRef Group = "";
384 if (GroupSym)
385 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000386 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000387 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000388 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000389 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000390 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000391 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000392
Rafael Espindola44d50572015-03-27 21:34:24 +0000393 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000394
395 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000396 if (Flags & ELF::SHF_ARM_PURECODE)
397 Kind = SectionKind::getExecuteOnly();
398 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000399 Kind = SectionKind::getText();
400 else
401 Kind = SectionKind::getReadOnly();
402
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000403 MCSectionELF *Result = createELFSectionImpl(
404 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000405 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000406 return Result;
407}
408
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000409MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000410 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
411 SectionKind::getReadOnly(), 4, Group, ~0,
412 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000413}
414
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000415MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
416 unsigned Characteristics,
417 SectionKind Kind,
418 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000419 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000420 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000421 MCSymbol *COMDATSymbol = nullptr;
422 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000423 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000424 COMDATSymName = COMDATSymbol->getName();
425 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000426
Reid Kleckner97837b72016-05-02 23:22:18 +0000427
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000428 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000429 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000430 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000431 auto Iter = IterBool.first;
432 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000433 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000434
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000435 MCSymbol *Begin = nullptr;
436 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000437 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000438
Rafael Espindola44d50572015-03-27 21:34:24 +0000439 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000440 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000441 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000442
443 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000444 return Result;
445}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000446
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000447MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
448 unsigned Characteristics,
449 SectionKind Kind,
450 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000451 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
452 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000453}
454
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000455MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000456 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000457 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000458 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000459 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000460 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000461}
462
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000463MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000464 const MCSymbol *KeySym,
465 unsigned UniqueID) {
466 // Return the normal section if we don't have to be associative or unique.
467 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000468 return Sec;
469
Reid Kleckner97837b72016-05-02 23:22:18 +0000470 // If we have a key symbol, make an associative section with the same name and
471 // kind as the normal section.
472 unsigned Characteristics = Sec->getCharacteristics();
473 if (KeySym) {
474 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
475 return getCOFFSection(Sec->getSectionName(), Characteristics,
476 Sec->getKind(), KeySym->getName(),
477 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
478 }
479
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000480 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000481 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000482}
483
Dan Gohman18eafb62017-02-22 01:23:18 +0000484void MCContext::renameWasmSection(MCSectionWasm *Section, StringRef Name) {
485 StringRef GroupName;
486 assert(!Section->getGroup() && "not yet implemented");
487
488 unsigned UniqueID = Section->getUniqueID();
489 WasmUniquingMap.erase(
490 WasmSectionKey{Section->getSectionName(), GroupName, UniqueID});
491 auto I = WasmUniquingMap.insert(std::make_pair(
492 WasmSectionKey{Name, GroupName, UniqueID},
493 Section))
494 .first;
495 StringRef CachedName = I->first.SectionName;
496 const_cast<MCSectionWasm *>(Section)->setSectionName(CachedName);
497}
498
499MCSectionWasm *MCContext::createWasmRelSection(const Twine &Name, unsigned Type,
500 unsigned Flags,
501 const MCSymbolWasm *Group) {
502 StringMap<bool>::iterator I;
503 bool Inserted;
504 std::tie(I, Inserted) =
505 RelSecNames.insert(std::make_pair(Name.str(), true));
506
507 return new (WasmAllocator.Allocate())
508 MCSectionWasm(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
509 Group, ~0, nullptr);
510}
511
512MCSectionWasm *MCContext::getWasmNamedSection(const Twine &Prefix,
513 const Twine &Suffix, unsigned Type,
514 unsigned Flags) {
515 return getWasmSection(Prefix + "." + Suffix, Type, Flags, Suffix);
516}
517
518MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
519 unsigned Flags,
520 const Twine &Group, unsigned UniqueID,
521 const char *BeginSymName) {
522 MCSymbolWasm *GroupSym = nullptr;
523 if (!Group.isTriviallyEmpty() && !Group.str().empty())
524 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
525
526 return getWasmSection(Section, Type, Flags, GroupSym, UniqueID, BeginSymName);
527}
528
529MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
530 unsigned Flags,
531 const MCSymbolWasm *GroupSym,
532 unsigned UniqueID,
533 const char *BeginSymName) {
534 StringRef Group = "";
535 if (GroupSym)
536 Group = GroupSym->getName();
537 // Do the lookup, if we have a hit, return it.
538 auto IterBool = WasmUniquingMap.insert(
539 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
540 auto &Entry = *IterBool.first;
541 if (!IterBool.second)
542 return Entry.second;
543
544 StringRef CachedName = Entry.first.SectionName;
545
546 SectionKind Kind = SectionKind::getText();
547
548 MCSymbol *Begin = nullptr;
549 if (BeginSymName)
550 Begin = createTempSymbol(BeginSymName, false);
551
552 MCSectionWasm *Result = new (WasmAllocator.Allocate())
553 MCSectionWasm(CachedName, Type, Flags, Kind, GroupSym, UniqueID, Begin);
554 Entry.second = Result;
555 return Result;
556}
557
Akira Hatanakab11ef082015-11-14 06:35:56 +0000558MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
559 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
560}
561
Kevin Enderbye5930f12010-07-28 20:55:35 +0000562//===----------------------------------------------------------------------===//
563// Dwarf Management
564//===----------------------------------------------------------------------===//
565
Jim Grosbach6f482002015-05-18 18:43:14 +0000566/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000567/// directory tables. If the file number has already been allocated it is an
568/// error and zero is returned and the client reports the error, else the
569/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000570unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000571 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000572 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000573 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000574}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000575
Kevin Enderbya68d0042010-10-04 20:17:24 +0000576/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000577/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000578bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000579 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
580 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000581 return false;
582
David Blaikiea55ddad2014-03-13 18:55:04 +0000583 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000584}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000585
Rafael Espindolae0746792015-05-21 16:52:32 +0000586/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000587/// useless debug info for them.
588void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000589 SectionsForRanges.remove_if(
590 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000591}
592
Reid Kleckner2214ed82016-01-29 00:49:42 +0000593CodeViewContext &MCContext::getCVContext() {
594 if (!CVContext.get())
595 CVContext.reset(new CodeViewContext);
596 return *CVContext.get();
597}
598
Oliver Stannard07b43d32015-11-17 09:58:07 +0000599//===----------------------------------------------------------------------===//
600// Error Reporting
601//===----------------------------------------------------------------------===//
602
603void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
604 HadError = true;
605
Sanne Wouda29338752017-02-08 14:48:05 +0000606 // If we have a source manager use it. Otherwise, try using the inline source
607 // manager.
608 // If that fails, use the generic report_fatal_error().
609 if (SrcMgr)
610 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
611 else if (InlineSrcMgr)
612 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
613 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000614 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000615}
616
617void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
618 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000619
620 // If we reached here, we are failing ungracefully. Run the interrupt handlers
621 // to make sure any special cleanups get done, in particular that we remove
622 // files registered with RemoveFileOnSignal.
623 sys::RunInterruptHandlers();
624 exit(1);
625}