blob: 5756da3c6a2ee49b67daf1e836ce3bc3c5abfb88 [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"
Rafael Espindola24d285d2015-05-26 00:32:28 +000013#include "llvm/MC/MCAssembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCDwarf.h"
16#include "llvm/MC/MCLabel.h"
17#include "llvm/MC/MCObjectFileInfo.h"
18#include "llvm/MC/MCRegisterInfo.h"
19#include "llvm/MC/MCSectionCOFF.h"
20#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCSectionMachO.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000022#include "llvm/MC/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSymbol.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000024#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000025#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000026#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000027#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000028#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000030#include <map>
31
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032using namespace llvm;
33
Bill Wendlingbc07a892013-06-18 07:20:20 +000034MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000035 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000036 bool DoAutoReset)
37 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Rafael Espindola9ab09232015-03-17 20:07:06 +000038 Symbols(Allocator), UsedNames(Allocator),
David Blaikie7400a972014-03-27 20:45:58 +000039 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000040 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000041 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
42 AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000043
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000045 if (EC)
46 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000047
Kevin Enderbye233dda2010-06-28 21:45:58 +000048 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000049 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000050 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000051
Alp Tokera55b95b2014-07-06 10:33:31 +000052 if (SrcMgr && SrcMgr->getNumBuffers())
53 MainFileName =
54 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000055}
56
57MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000058 if (AutoReset)
59 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000060
61 // NOTE: The symbols are all allocated out of a bump pointer allocator,
62 // we don't need to free them here.
Andrew Trick1c6a4c32013-12-10 04:39:05 +000063
Pedro Artigase84b13f2012-12-06 22:12:44 +000064 // If the stream for the .secure_log_unique directive was created free it.
Jim Grosbach008359a2015-05-18 18:43:23 +000065 delete (raw_ostream *)SecureLog;
Pedro Artigase84b13f2012-12-06 22:12:44 +000066}
67
68//===----------------------------------------------------------------------===//
69// Module Lifetime Management
70//===----------------------------------------------------------------------===//
71
Pedro Artigas7212ee42012-12-12 22:59:46 +000072void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000073 // Call the destructors so the fragments are freed
74 for (auto &I : ELFUniquingMap)
75 I.second->~MCSectionELF();
76 for (auto &I : COFFUniquingMap)
77 I.second->~MCSectionCOFF();
78 for (auto &I : MachOUniquingMap)
79 I.second->~MCSectionMachO();
80
Pedro Artigase84b13f2012-12-06 22:12:44 +000081 UsedNames.clear();
82 Symbols.clear();
83 Allocator.Reset();
84 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000085 CompilationDir.clear();
86 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000087 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000088 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000089 MCGenDwarfLabelEntries.clear();
90 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000091 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000092 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000093
David Blaikie9ec32122014-04-10 23:55:11 +000094 MachOUniquingMap.clear();
95 ELFUniquingMap.clear();
96 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000097
Rafael Espindola9ab09232015-03-17 20:07:06 +000098 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000099 AllowTemporaryLabels = true;
100 DwarfLocSeen = false;
101 GenDwarfForAssembly = false;
102 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000103}
104
Chris Lattner20731122010-04-08 20:30:37 +0000105//===----------------------------------------------------------------------===//
106// Symbol Manipulation
107//===----------------------------------------------------------------------===//
108
Jim Grosbach6f482002015-05-18 18:43:14 +0000109MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000110 SmallString<128> NameSV;
111 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000112
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000113 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000114
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000115 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000116 if (!Sym)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000117 Sym = CreateSymbol(NameRef, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000118
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000119 return Sym;
120}
121
Rafael Espindolab6613022014-10-17 01:48:58 +0000122MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
123 MCSymbol *&Sym = SectionSymbols[&Section];
124 if (Sym)
125 return Sym;
126
127 StringRef Name = Section.getSectionName();
128
David Blaikie5106ce72014-11-19 05:49:42 +0000129 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000130 if (OldSym && OldSym->isUndefined()) {
131 Sym = OldSym;
132 return OldSym;
133 }
134
David Blaikie5106ce72014-11-19 05:49:42 +0000135 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Duncan P. N. Exon Smith03656162015-05-22 06:04:42 +0000136 Sym = new (*this) MCSymbol(&*NameIter, /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000137
David Blaikie5106ce72014-11-19 05:49:42 +0000138 if (!OldSym)
139 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000140
141 return Sym;
142}
143
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000144MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
145 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000146 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000147 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000148}
149
David Majnemera225a192015-03-31 22:35:44 +0000150MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000151 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000152 "$parent_frame_offset");
153}
154
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000155MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
156 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
157 FuncName);
158}
159
Rafael Espindola9ab09232015-03-17 20:07:06 +0000160MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000161 // Determine whether this is an assembler temporary or normal label, if used.
Rafael Espindola9ab09232015-03-17 20:07:06 +0000162 bool IsTemporary = false;
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000163 if (AllowTemporaryLabels)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000164 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000165
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000166 if (IsTemporary && AlwaysAddSuffix && !UseNamesOnTempLabels)
Duncan P. N. Exon Smith03656162015-05-22 06:04:42 +0000167 return new (*this) MCSymbol(nullptr, true);
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000168
Rafael Espindola9ab09232015-03-17 20:07:06 +0000169 SmallString<128> NewName = Name;
170 bool AddSuffix = AlwaysAddSuffix;
171 unsigned &NextUniqueID = NextID[Name];
172 for (;;) {
173 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000174 NewName.resize(Name.size());
175 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000176 }
177 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
178 if (NameEntry.second) {
179 // Ok, we found a name. Have the MCSymbol object itself refer to the copy
180 // of the string that is embedded in the UsedNames entry.
Duncan P. N. Exon Smith03656162015-05-22 06:04:42 +0000181 MCSymbol *Result = new (*this) MCSymbol(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000182 return Result;
183 }
184 assert(IsTemporary && "Cannot rename non-temporary symbols");
185 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000186 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000187 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000188}
189
Rafael Espindola9ab09232015-03-17 20:07:06 +0000190MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000191 SmallString<128> NameSV;
192 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000193 return CreateSymbol(NameSV, AlwaysAddSuffix);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000194}
195
Jim Grosbach6f482002015-05-18 18:43:14 +0000196MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000197 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000198 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
199 return CreateSymbol(NameSV, true);
Tim Northoverc3988b42014-03-29 07:05:06 +0000200}
201
Jim Grosbach6f482002015-05-18 18:43:14 +0000202MCSymbol *MCContext::createTempSymbol() {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000203 return createTempSymbol("tmp", true);
Chris Lattner073d8172010-03-14 08:23:30 +0000204}
205
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000206unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000207 MCLabel *&Label = Instances[LocalLabelVal];
208 if (!Label)
209 Label = new (*this) MCLabel(0);
210 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000211}
212
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000213unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000214 MCLabel *&Label = Instances[LocalLabelVal];
215 if (!Label)
216 Label = new (*this) MCLabel(0);
217 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000218}
219
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000220MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
221 unsigned Instance) {
222 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
223 if (!Sym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000224 Sym = createTempSymbol();
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000225 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000226}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000227
Jim Grosbach6f482002015-05-18 18:43:14 +0000228MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000229 unsigned Instance = NextInstance(LocalLabelVal);
230 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
231}
232
Jim Grosbach6f482002015-05-18 18:43:14 +0000233MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000234 bool Before) {
235 unsigned Instance = GetInstance(LocalLabelVal);
236 if (!Before)
237 ++Instance;
238 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000239}
240
Jim Grosbach6f482002015-05-18 18:43:14 +0000241MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000242 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000243 StringRef NameRef = Name.toStringRef(NameSV);
244 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000245}
246
Chris Lattner20731122010-04-08 20:30:37 +0000247//===----------------------------------------------------------------------===//
248// Section Management
249//===----------------------------------------------------------------------===//
250
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000251MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
252 unsigned TypeAndAttributes,
253 unsigned Reserved2, SectionKind Kind,
254 const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000255
Chris Lattner20731122010-04-08 20:30:37 +0000256 // We unique sections by their segment/section pair. The returned section
257 // may not have the same flags as the requested section, if so this should be
258 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000259
Chris Lattner20731122010-04-08 20:30:37 +0000260 // Form the name to look up.
261 SmallString<64> Name;
262 Name += Segment;
263 Name.push_back(',');
264 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000265
Chris Lattner20731122010-04-08 20:30:37 +0000266 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000267 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000268 if (Entry)
269 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000270
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000271 MCSymbol *Begin = nullptr;
272 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000273 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000274
Chris Lattner20731122010-04-08 20:30:37 +0000275 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000276 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000277 Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000278}
Chris Lattner5418dd52010-04-08 21:26:26 +0000279
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000280void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000281 StringRef GroupName;
282 if (const MCSymbol *Group = Section->getGroup())
283 GroupName = Group->getName();
284
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000285 unsigned UniqueID = Section->getUniqueID();
286 ELFUniquingMap.erase(
287 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
288 auto I = ELFUniquingMap.insert(std::make_pair(
289 ELFSectionKey{Name, GroupName, UniqueID},
Jim Grosbach008359a2015-05-18 18:43:23 +0000290 Section))
291 .first;
Rafael Espindola44d50572015-03-27 21:34:24 +0000292 StringRef CachedName = I->first.SectionName;
Jim Grosbach008359a2015-05-18 18:43:23 +0000293 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000294}
295
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000296MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
297 unsigned Flags, unsigned EntrySize,
298 const MCSymbol *Group,
299 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000300 StringMap<bool>::iterator I;
301 bool Inserted;
302 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
303
304 return new (*this)
305 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000306 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000307}
308
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000309MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
310 unsigned Flags, unsigned EntrySize,
311 StringRef Group, unsigned UniqueID,
312 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000313 MCSymbol *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000314 if (!Group.empty())
Jim Grosbach6f482002015-05-18 18:43:14 +0000315 GroupSym = getOrCreateSymbol(Group);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000316
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000317 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
318 BeginSymName, nullptr);
319}
320
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000321MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
322 unsigned Flags, unsigned EntrySize,
323 const MCSymbol *GroupSym,
324 unsigned UniqueID,
325 const char *BeginSymName,
326 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000327 StringRef Group = "";
328 if (GroupSym)
329 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000330 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000331 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000332 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000333 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000334 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000335 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000336
Rafael Espindola44d50572015-03-27 21:34:24 +0000337 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000338
339 SectionKind Kind;
340 if (Flags & ELF::SHF_EXECINSTR)
341 Kind = SectionKind::getText();
342 else
343 Kind = SectionKind::getReadOnly();
344
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000345 MCSymbol *Begin = nullptr;
346 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000347 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000348
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000349 MCSectionELF *Result =
350 new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000351 GroupSym, UniqueID, Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000352 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000353 return Result;
354}
355
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000356MCSectionELF *MCContext::createELFGroupSection(const MCSymbol *Group) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000357 MCSectionELF *Result = new (*this)
358 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000359 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000360 return Result;
361}
362
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000363MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
364 unsigned Characteristics,
365 SectionKind Kind,
366 StringRef COMDATSymName, int Selection,
367 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000368 MCSymbol *COMDATSymbol = nullptr;
369 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000370 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000371 COMDATSymName = COMDATSymbol->getName();
372 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000373
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000374 // Do the lookup, if we have a hit, return it.
Rafael Espindola44d50572015-03-27 21:34:24 +0000375 COFFSectionKey T{Section, COMDATSymName, Selection};
David Majnemerc57d0382014-06-27 17:19:44 +0000376 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000377 auto Iter = IterBool.first;
378 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000379 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000380
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000381 MCSymbol *Begin = nullptr;
382 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000383 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000384
Rafael Espindola44d50572015-03-27 21:34:24 +0000385 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000386 MCSectionCOFF *Result = new (*this) MCSectionCOFF(
387 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000388
389 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000390 return Result;
391}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000392
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000393MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
394 unsigned Characteristics,
395 SectionKind Kind,
396 const char *BeginSymName) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000397 return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000398}
399
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000400MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Rafael Espindola44d50572015-03-27 21:34:24 +0000401 COFFSectionKey T{Section, "", 0};
David Majnemerc57d0382014-06-27 17:19:44 +0000402 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000403 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000404 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000405 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000406}
407
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000408MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
409 const MCSymbol *KeySym) {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000410 // Return the normal section if we don't have to be associative.
411 if (!KeySym)
412 return Sec;
413
414 // Make an associative section with the same name and kind as the normal
415 // section.
416 unsigned Characteristics =
417 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
418 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
419 KeySym->getName(),
420 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
421}
422
Kevin Enderbye5930f12010-07-28 20:55:35 +0000423//===----------------------------------------------------------------------===//
424// Dwarf Management
425//===----------------------------------------------------------------------===//
426
Jim Grosbach6f482002015-05-18 18:43:14 +0000427/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000428/// directory tables. If the file number has already been allocated it is an
429/// error and zero is returned and the client reports the error, else the
430/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000431unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000432 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000433 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000434 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000435}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000436
Kevin Enderbya68d0042010-10-04 20:17:24 +0000437/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000438/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000439bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000440 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
441 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000442 return false;
443
David Blaikiea55ddad2014-03-13 18:55:04 +0000444 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000445}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000446
Rafael Espindolae0746792015-05-21 16:52:32 +0000447/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000448/// useless debug info for them.
449void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000450 SectionsForRanges.remove_if(
451 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000452}
453
Jim Grosbach6f482002015-05-18 18:43:14 +0000454void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbachb18b4092012-01-26 23:20:11 +0000455 // If we have a source manager and a location, use it. Otherwise just
456 // use the generic report_fatal_error().
457 if (!SrcMgr || Loc == SMLoc())
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000458 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000459
460 // Use the source manager to print the message.
461 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
462
463 // If we reached here, we are failing ungracefully. Run the interrupt handlers
464 // to make sure any special cleanups get done, in particular that we remove
465 // files registered with RemoveFileOnSignal.
466 sys::RunInterruptHandlers();
467 exit(1);
468}