blob: f48ab2f8e349caeb314e640317cb9e527a824df2 [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
10#include "llvm/MC/MCContext.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/MC/MCAsmInfo.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000014#include "llvm/MC/MCAssembler.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000015#include "llvm/MC/MCCodeView.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/MC/MCDwarf.h"
17#include "llvm/MC/MCLabel.h"
18#include "llvm/MC/MCObjectFileInfo.h"
19#include "llvm/MC/MCRegisterInfo.h"
20#include "llvm/MC/MCSectionCOFF.h"
21#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCSectionMachO.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000023#include "llvm/MC/MCStreamer.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000024#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000025#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000026#include "llvm/MC/MCSymbolMachO.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000027#include "llvm/Support/COFF.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000028#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000029#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000030#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000031#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000032#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000034
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000035using namespace llvm;
36
Igor Laevsky7b998852016-06-17 15:19:41 +000037static cl::opt<char*>
38AsSecureLogFileName("as-secure-log-file-name",
39 cl::desc("As secure log file name (initialized from "
40 "AS_SECURE_LOG_FILE env variable)"),
41 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
42
43
Bill Wendlingbc07a892013-06-18 07:20:20 +000044MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000045 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000046 bool DoAutoReset)
47 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Rafael Espindola9ab09232015-03-17 20:07:06 +000048 Symbols(Allocator), UsedNames(Allocator),
David Blaikie7400a972014-03-27 20:45:58 +000049 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000050 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000051 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
Oliver Stannard07b43d32015-11-17 09:58:07 +000052 AutoReset(DoAutoReset), HadError(false) {
Igor Laevsky7b998852016-06-17 15:19:41 +000053 SecureLogFile = AsSecureLogFileName;
Craig Topperbb694de2014-04-13 04:57:38 +000054 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000055 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000056
Alp Tokera55b95b2014-07-06 10:33:31 +000057 if (SrcMgr && SrcMgr->getNumBuffers())
58 MainFileName =
59 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000060}
61
62MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000063 if (AutoReset)
64 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000065
66 // NOTE: The symbols are all allocated out of a bump pointer allocator,
67 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000068}
69
70//===----------------------------------------------------------------------===//
71// Module Lifetime Management
72//===----------------------------------------------------------------------===//
73
Pedro Artigas7212ee42012-12-12 22:59:46 +000074void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000075 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000076 COFFAllocator.DestroyAll();
77 ELFAllocator.DestroyAll();
78 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000079
Akira Hatanakab11ef082015-11-14 06:35:56 +000080 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000081 UsedNames.clear();
82 Symbols.clear();
Keno Fischer21a7f232015-10-09 17:24:54 +000083 SectionSymbols.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000084 Allocator.Reset();
85 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000086 CompilationDir.clear();
87 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000088 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000089 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000090 MCGenDwarfLabelEntries.clear();
91 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000092 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000093 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000094
Reid Kleckner2214ed82016-01-29 00:49:42 +000095 CVContext.reset();
96
David Blaikie9ec32122014-04-10 23:55:11 +000097 MachOUniquingMap.clear();
98 ELFUniquingMap.clear();
99 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000100
Rafael Espindola9ab09232015-03-17 20:07:06 +0000101 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000102 AllowTemporaryLabels = true;
103 DwarfLocSeen = false;
104 GenDwarfForAssembly = false;
105 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000106
107 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000108}
109
Chris Lattner20731122010-04-08 20:30:37 +0000110//===----------------------------------------------------------------------===//
111// Symbol Manipulation
112//===----------------------------------------------------------------------===//
113
Jim Grosbach6f482002015-05-18 18:43:14 +0000114MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000115 SmallString<128> NameSV;
116 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000117
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000118 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000119
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000120 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000121 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000122 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000123
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000124 return Sym;
125}
126
Rafael Espindolaa8695762015-06-02 00:25:12 +0000127MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
128 MCSymbolELF *&Sym = SectionSymbols[&Section];
Rafael Espindolab6613022014-10-17 01:48:58 +0000129 if (Sym)
130 return Sym;
131
132 StringRef Name = Section.getSectionName();
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000133 auto NameIter = UsedNames.insert(std::make_pair(Name, false)).first;
Pete Cooper234b8752015-06-09 18:36:13 +0000134 Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000135
Rafael Espindolab6613022014-10-17 01:48:58 +0000136 return Sym;
137}
138
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000139MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
140 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000141 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000142 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000143}
144
David Majnemera225a192015-03-31 22:35:44 +0000145MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000146 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000147 "$parent_frame_offset");
148}
149
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000150MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
151 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
152 FuncName);
153}
154
Rafael Espindolaa8695762015-06-02 00:25:12 +0000155MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
156 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000157 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000158 switch (MOFI->getObjectFileType()) {
159 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000160 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000161 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000162 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000163 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000164 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000165 }
166 }
Pete Cooper234b8752015-06-09 18:36:13 +0000167 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
168 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000169}
170
Daniel Jasper41de8022015-06-23 11:31:32 +0000171MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
172 bool CanBeUnnamed) {
173 if (CanBeUnnamed && !UseNamesOnTempLabels)
174 return createSymbolImpl(nullptr, true);
175
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000176 // Determine whether this is an user writter assembler temporary or normal
177 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000178 bool IsTemporary = CanBeUnnamed;
179 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000180 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000181
Rafael Espindola9ab09232015-03-17 20:07:06 +0000182 SmallString<128> NewName = Name;
183 bool AddSuffix = AlwaysAddSuffix;
184 unsigned &NextUniqueID = NextID[Name];
185 for (;;) {
186 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000187 NewName.resize(Name.size());
188 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000189 }
190 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000191 if (NameEntry.second || !NameEntry.first->second) {
192 // Ok, we found a name.
193 // Mark it as used for a non-section symbol.
194 NameEntry.first->second = true;
195 // Have the MCSymbol object itself refer to the copy of the string that is
196 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000197 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000198 }
199 assert(IsTemporary && "Cannot rename non-temporary symbols");
200 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000201 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000202 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000203}
204
Daniel Jasper41de8022015-06-23 11:31:32 +0000205MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
206 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000207 SmallString<128> NameSV;
208 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000209 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000210}
211
Jim Grosbach6f482002015-05-18 18:43:14 +0000212MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000213 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000214 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000215 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000216}
217
Daniel Jasper41de8022015-06-23 11:31:32 +0000218MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
219 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000220}
221
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000222unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000223 MCLabel *&Label = Instances[LocalLabelVal];
224 if (!Label)
225 Label = new (*this) MCLabel(0);
226 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000227}
228
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000229unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000230 MCLabel *&Label = Instances[LocalLabelVal];
231 if (!Label)
232 Label = new (*this) MCLabel(0);
233 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000234}
235
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000236MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
237 unsigned Instance) {
238 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
239 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000240 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000241 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000242}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000243
Jim Grosbach6f482002015-05-18 18:43:14 +0000244MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000245 unsigned Instance = NextInstance(LocalLabelVal);
246 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
247}
248
Jim Grosbach6f482002015-05-18 18:43:14 +0000249MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000250 bool Before) {
251 unsigned Instance = GetInstance(LocalLabelVal);
252 if (!Before)
253 ++Instance;
254 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000255}
256
Jim Grosbach6f482002015-05-18 18:43:14 +0000257MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000258 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000259 StringRef NameRef = Name.toStringRef(NameSV);
260 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000261}
262
Chris Lattner20731122010-04-08 20:30:37 +0000263//===----------------------------------------------------------------------===//
264// Section Management
265//===----------------------------------------------------------------------===//
266
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000267MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
268 unsigned TypeAndAttributes,
269 unsigned Reserved2, SectionKind Kind,
270 const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000271
Chris Lattner20731122010-04-08 20:30:37 +0000272 // We unique sections by their segment/section pair. The returned section
273 // may not have the same flags as the requested section, if so this should be
274 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000275
Chris Lattner20731122010-04-08 20:30:37 +0000276 // Form the name to look up.
277 SmallString<64> Name;
278 Name += Segment;
279 Name.push_back(',');
280 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000281
Chris Lattner20731122010-04-08 20:30:37 +0000282 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000283 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000284 if (Entry)
285 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000286
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000287 MCSymbol *Begin = nullptr;
288 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000289 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000290
Chris Lattner20731122010-04-08 20:30:37 +0000291 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000292 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
293 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000294}
Chris Lattner5418dd52010-04-08 21:26:26 +0000295
Richard Smithb910e562016-05-25 00:14:12 +0000296void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
297 StringRef GroupName;
298 if (const MCSymbol *Group = Section->getGroup())
299 GroupName = Group->getName();
300
301 unsigned UniqueID = Section->getUniqueID();
302 ELFUniquingMap.erase(
303 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
304 auto I = ELFUniquingMap.insert(std::make_pair(
305 ELFSectionKey{Name, GroupName, UniqueID},
306 Section))
307 .first;
308 StringRef CachedName = I->first.SectionName;
309 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
310}
311
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000312MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
313 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000314 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000315 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000316 StringMap<bool>::iterator I;
317 bool Inserted;
318 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
319
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000320 return new (ELFAllocator.Allocate())
Rafael Espindolac9d06922015-03-30 13:39:16 +0000321 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000322 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000323}
324
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000325MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
326 unsigned Flags, unsigned EntrySize,
327 StringRef Group, unsigned UniqueID,
328 const char *BeginSymName) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000329 MCSymbolELF *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000330 if (!Group.empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000331 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000332
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000333 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
334 BeginSymName, nullptr);
335}
336
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000337MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
338 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000339 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000340 unsigned UniqueID,
341 const char *BeginSymName,
342 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000343 StringRef Group = "";
344 if (GroupSym)
345 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000346 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000347 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000348 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000349 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000350 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000351 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000352
Rafael Espindola44d50572015-03-27 21:34:24 +0000353 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000354
355 SectionKind Kind;
356 if (Flags & ELF::SHF_EXECINSTR)
357 Kind = SectionKind::getText();
358 else
359 Kind = SectionKind::getReadOnly();
360
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000361 MCSymbol *Begin = nullptr;
362 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000363 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000364
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000365 MCSectionELF *Result = new (ELFAllocator.Allocate())
366 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID,
367 Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000368 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000369 return Result;
370}
371
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000372MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000373 MCSectionELF *Result = new (ELFAllocator.Allocate())
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000374 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000375 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000376 return Result;
377}
378
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000379MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
380 unsigned Characteristics,
381 SectionKind Kind,
382 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000383 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000384 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000385 MCSymbol *COMDATSymbol = nullptr;
386 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000387 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000388 COMDATSymName = COMDATSymbol->getName();
389 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000390
Reid Kleckner97837b72016-05-02 23:22:18 +0000391
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000392 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000393 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000394 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000395 auto Iter = IterBool.first;
396 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000397 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000398
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000399 MCSymbol *Begin = nullptr;
400 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000401 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000402
Rafael Espindola44d50572015-03-27 21:34:24 +0000403 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000404 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000405 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000406
407 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000408 return Result;
409}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000410
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000411MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
412 unsigned Characteristics,
413 SectionKind Kind,
414 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000415 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
416 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000417}
418
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000419MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000420 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000421 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000422 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000423 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000424 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000425}
426
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000427MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000428 const MCSymbol *KeySym,
429 unsigned UniqueID) {
430 // Return the normal section if we don't have to be associative or unique.
431 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000432 return Sec;
433
Reid Kleckner97837b72016-05-02 23:22:18 +0000434 // If we have a key symbol, make an associative section with the same name and
435 // kind as the normal section.
436 unsigned Characteristics = Sec->getCharacteristics();
437 if (KeySym) {
438 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
439 return getCOFFSection(Sec->getSectionName(), Characteristics,
440 Sec->getKind(), KeySym->getName(),
441 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
442 }
443
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000444 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000445 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000446}
447
Akira Hatanakab11ef082015-11-14 06:35:56 +0000448MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
449 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
450}
451
Kevin Enderbye5930f12010-07-28 20:55:35 +0000452//===----------------------------------------------------------------------===//
453// Dwarf Management
454//===----------------------------------------------------------------------===//
455
Jim Grosbach6f482002015-05-18 18:43:14 +0000456/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000457/// directory tables. If the file number has already been allocated it is an
458/// error and zero is returned and the client reports the error, else the
459/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000460unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000461 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000462 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000463 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000464}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000465
Kevin Enderbya68d0042010-10-04 20:17:24 +0000466/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000467/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000468bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000469 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
470 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000471 return false;
472
David Blaikiea55ddad2014-03-13 18:55:04 +0000473 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000474}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000475
Rafael Espindolae0746792015-05-21 16:52:32 +0000476/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000477/// useless debug info for them.
478void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000479 SectionsForRanges.remove_if(
480 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000481}
482
Reid Kleckner2214ed82016-01-29 00:49:42 +0000483CodeViewContext &MCContext::getCVContext() {
484 if (!CVContext.get())
485 CVContext.reset(new CodeViewContext);
486 return *CVContext.get();
487}
488
489unsigned MCContext::getCVFile(StringRef FileName, unsigned FileNumber) {
490 return getCVContext().addFile(FileNumber, FileName) ? FileNumber : 0;
491}
492
493bool MCContext::isValidCVFileNumber(unsigned FileNumber) {
494 return getCVContext().isValidFileNumber(FileNumber);
495}
496
Oliver Stannard07b43d32015-11-17 09:58:07 +0000497//===----------------------------------------------------------------------===//
498// Error Reporting
499//===----------------------------------------------------------------------===//
500
501void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
502 HadError = true;
503
504 // If we have a source manager use it. Otherwise just use the generic
505 // report_fatal_error().
506 if (!SrcMgr)
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000507 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000508
509 // Use the source manager to print the message.
510 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000511}
512
513void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
514 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000515
516 // If we reached here, we are failing ungracefully. Run the interrupt handlers
517 // to make sure any special cleanups get done, in particular that we remove
518 // files registered with RemoveFileOnSignal.
519 sys::RunInterruptHandlers();
520 exit(1);
521}