blob: ae082bb6074441d2a4449e0299c0d9ed0fb9abad [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"
14#include "llvm/MC/MCDwarf.h"
15#include "llvm/MC/MCLabel.h"
16#include "llvm/MC/MCObjectFileInfo.h"
17#include "llvm/MC/MCRegisterInfo.h"
18#include "llvm/MC/MCSectionCOFF.h"
19#include "llvm/MC/MCSectionELF.h"
20#include "llvm/MC/MCSectionMachO.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000021#include "llvm/MC/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MCSymbol.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000023#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000024#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000025#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000026#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000027#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000029#include <map>
30
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000031using namespace llvm;
32
Bill Wendlingbc07a892013-06-18 07:20:20 +000033MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000034 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000035 bool DoAutoReset)
36 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
37 Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0),
38 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000039 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000040 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
41 AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000042
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000043 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000044 if (EC)
45 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000046
Kevin Enderbye233dda2010-06-28 21:45:58 +000047 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000048 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000049 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000050
Alp Tokera55b95b2014-07-06 10:33:31 +000051 if (SrcMgr && SrcMgr->getNumBuffers())
52 MainFileName =
53 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000054}
55
56MCContext::~MCContext() {
57
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.
65 delete (raw_ostream*)SecureLog;
66}
67
68//===----------------------------------------------------------------------===//
69// Module Lifetime Management
70//===----------------------------------------------------------------------===//
71
Pedro Artigas7212ee42012-12-12 22:59:46 +000072void MCContext::reset() {
Pedro Artigase84b13f2012-12-06 22:12:44 +000073 UsedNames.clear();
74 Symbols.clear();
75 Allocator.Reset();
76 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000077 CompilationDir.clear();
78 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000079 MCDwarfLineTablesCUMap.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000080 SectionStartEndSyms.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000081 MCGenDwarfLabelEntries.clear();
82 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000083 DwarfCompileUnitID = 0;
Pedro Artigase84b13f2012-12-06 22:12:44 +000084 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000085
David Blaikie9ec32122014-04-10 23:55:11 +000086 MachOUniquingMap.clear();
87 ELFUniquingMap.clear();
88 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000089
90 NextUniqueID = 0;
91 AllowTemporaryLabels = true;
92 DwarfLocSeen = false;
93 GenDwarfForAssembly = false;
94 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000095}
96
Chris Lattner20731122010-04-08 20:30:37 +000097//===----------------------------------------------------------------------===//
98// Symbol Manipulation
99//===----------------------------------------------------------------------===//
100
Chris Lattner98970432010-03-30 18:10:53 +0000101MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +0000102 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000103
David Blaikie5106ce72014-11-19 05:49:42 +0000104 MCSymbol *&Sym = Symbols[Name];
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000105
David Blaikie5106ce72014-11-19 05:49:42 +0000106 if (!Sym)
107 Sym = CreateSymbol(Name);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000108
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000109 return Sym;
110}
111
Rafael Espindolab6613022014-10-17 01:48:58 +0000112MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
113 MCSymbol *&Sym = SectionSymbols[&Section];
114 if (Sym)
115 return Sym;
116
117 StringRef Name = Section.getSectionName();
118
David Blaikie5106ce72014-11-19 05:49:42 +0000119 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000120 if (OldSym && OldSym->isUndefined()) {
121 Sym = OldSym;
122 return OldSym;
123 }
124
David Blaikie5106ce72014-11-19 05:49:42 +0000125 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
126 Sym = new (*this) MCSymbol(NameIter->getKey(), /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000127
David Blaikie5106ce72014-11-19 05:49:42 +0000128 if (!OldSym)
129 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000130
131 return Sym;
132}
133
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000134MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
135 unsigned Idx) {
136 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
137 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000138}
139
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000140MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000141 // Determine whether this is an assembler temporary or normal label, if used.
142 bool isTemporary = false;
143 if (AllowTemporaryLabels)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000144 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000145
David Blaikie5106ce72014-11-19 05:49:42 +0000146 auto NameEntry = UsedNames.insert(std::make_pair(Name, true));
147 if (!NameEntry.second) {
Alp Tokerf907b892013-12-05 05:44:44 +0000148 assert(isTemporary && "Cannot rename non-temporary symbols");
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000149 SmallString<128> NewName = Name;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000150 do {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000151 NewName.resize(Name.size());
152 raw_svector_ostream(NewName) << NextUniqueID++;
David Blaikie5106ce72014-11-19 05:49:42 +0000153 NameEntry = UsedNames.insert(std::make_pair(NewName, true));
154 } while (!NameEntry.second);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000155 }
Chris Lattner3f5738d2009-06-24 04:31:49 +0000156
Chris Lattner13348762010-03-15 06:15:35 +0000157 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000158 // to the copy of the string that is embedded in the UsedNames entry.
David Blaikie5106ce72014-11-19 05:49:42 +0000159 MCSymbol *Result =
160 new (*this) MCSymbol(NameEntry.first->getKey(), isTemporary);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000161
162 return Result;
Chris Lattner3f5738d2009-06-24 04:31:49 +0000163}
164
Rafael Espindola629cdba2015-02-27 18:18:39 +0000165MCSymbol *MCContext::createTempSymbol(const Twine &Name) {
166 SmallString<128> NameSV;
167 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
168 return CreateSymbol(NameSV);
169}
170
Chris Lattner98970432010-03-30 18:10:53 +0000171MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner86dfd732009-10-19 22:49:00 +0000172 SmallString<128> NameSV;
David Blaikie8e5283a2013-12-03 18:18:28 +0000173 return GetOrCreateSymbol(Name.toStringRef(NameSV));
Chris Lattner86dfd732009-10-19 22:49:00 +0000174}
175
Tim Northoverc3988b42014-03-29 07:05:06 +0000176MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000177 SmallString<128> NameSV;
178 raw_svector_ostream(NameSV)
179 << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
180 return CreateSymbol(NameSV);
Tim Northoverc3988b42014-03-29 07:05:06 +0000181}
182
Chris Lattner073d8172010-03-14 08:23:30 +0000183MCSymbol *MCContext::CreateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000184 SmallString<128> NameSV;
185 raw_svector_ostream(NameSV)
186 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
187 return CreateSymbol(NameSV);
Chris Lattner073d8172010-03-14 08:23:30 +0000188}
189
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000190unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000191 MCLabel *&Label = Instances[LocalLabelVal];
192 if (!Label)
193 Label = new (*this) MCLabel(0);
194 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000195}
196
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000197unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000198 MCLabel *&Label = Instances[LocalLabelVal];
199 if (!Label)
200 Label = new (*this) MCLabel(0);
201 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000202}
203
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000204MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
205 unsigned Instance) {
206 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
207 if (!Sym)
208 Sym = CreateTempSymbol();
209 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000210}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000211
212MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) {
213 unsigned Instance = NextInstance(LocalLabelVal);
214 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
215}
216
217MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal,
218 bool Before) {
219 unsigned Instance = GetInstance(LocalLabelVal);
220 if (!Before)
221 ++Instance;
222 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000223}
224
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000225MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000226 return Symbols.lookup(Name);
227}
Chris Lattner20731122010-04-08 20:30:37 +0000228
Roman Divacky0be33592012-09-18 17:10:37 +0000229MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
230 SmallString<128> NameSV;
231 Name.toVector(NameSV);
232 return LookupSymbol(NameSV.str());
233}
234
Chris Lattner20731122010-04-08 20:30:37 +0000235//===----------------------------------------------------------------------===//
236// Section Management
237//===----------------------------------------------------------------------===//
238
239const MCSectionMachO *MCContext::
240getMachOSection(StringRef Segment, StringRef Section,
241 unsigned TypeAndAttributes,
242 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000243
Chris Lattner20731122010-04-08 20:30:37 +0000244 // We unique sections by their segment/section pair. The returned section
245 // may not have the same flags as the requested section, if so this should be
246 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000247
Chris Lattner20731122010-04-08 20:30:37 +0000248 // Form the name to look up.
249 SmallString<64> Name;
250 Name += Segment;
251 Name.push_back(',');
252 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000253
Chris Lattner20731122010-04-08 20:30:37 +0000254 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000255 const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()];
Chris Lattner20731122010-04-08 20:30:37 +0000256 if (Entry) return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000257
Chris Lattner20731122010-04-08 20:30:37 +0000258 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000259 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
260 Reserved2, Kind);
Chris Lattner20731122010-04-08 20:30:37 +0000261}
Chris Lattner5418dd52010-04-08 21:26:26 +0000262
Rafael Espindolaba31e272015-01-29 17:33:21 +0000263const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
264 unsigned Flags) {
265 return getELFSection(Section, Type, Flags, 0, "");
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000266}
267
David Blaikie8019bf82014-04-10 21:53:53 +0000268void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000269 StringRef GroupName;
270 if (const MCSymbol *Group = Section->getGroup())
271 GroupName = Group->getName();
272
David Blaikie9ec32122014-04-10 23:55:11 +0000273 ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName));
274 auto I =
275 ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName),
276 Section)).first;
David Blaikieb60e61c2014-04-11 22:49:14 +0000277 StringRef CachedName = I->first.first;
278 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000279}
280
Rafael Espindolaba31e272015-01-29 17:33:21 +0000281const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
282 unsigned Flags, unsigned EntrySize,
Rafael Espindola68fa2492015-02-17 20:48:01 +0000283 StringRef Group, bool Unique) {
Chris Lattner5418dd52010-04-08 21:26:26 +0000284 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000285 auto IterBool = ELFUniquingMap.insert(
Craig Topperbb694de2014-04-13 04:57:38 +0000286 std::make_pair(SectionGroupPair(Section, Group), nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000287 auto &Entry = *IterBool.first;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000288 if (!IterBool.second && !Unique)
289 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000290
Craig Topperbb694de2014-04-13 04:57:38 +0000291 MCSymbol *GroupSym = nullptr;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000292 if (!Group.empty())
293 GroupSym = GetOrCreateSymbol(Group);
294
David Blaikieb60e61c2014-04-11 22:49:14 +0000295 StringRef CachedName = Entry.first.first;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000296
297 SectionKind Kind;
298 if (Flags & ELF::SHF_EXECINSTR)
299 Kind = SectionKind::getText();
300 else
301 Kind = SectionKind::getReadOnly();
302
David Blaikieb60e61c2014-04-11 22:49:14 +0000303 MCSectionELF *Result = new (*this)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000304 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, Unique);
305 if (!Unique)
306 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000307 return Result;
308}
309
Rafael Espindola68fa2492015-02-17 20:48:01 +0000310const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
311 unsigned Flags, unsigned EntrySize,
312 StringRef Group) {
313 return getELFSection(Section, Type, Flags, EntrySize, Group, false);
314}
315
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000316const MCSectionELF *MCContext::CreateELFGroupSection() {
317 MCSectionELF *Result =
Rafael Espindola68fa2492015-02-17 20:48:01 +0000318 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
319 SectionKind::getReadOnly(), 4, nullptr, false);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000320 return Result;
321}
322
Rafael Espindola0766ae02014-06-06 19:26:12 +0000323const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
324 unsigned Characteristics,
325 SectionKind Kind,
326 StringRef COMDATSymName,
327 int Selection) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000328 // Do the lookup, if we have a hit, return it.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000329
David Majnemerc57d0382014-06-27 17:19:44 +0000330 SectionGroupTriple T(Section, COMDATSymName, Selection);
331 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000332 auto Iter = IterBool.first;
333 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000334 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000335
David Majnemer8bce66b2014-07-14 22:57:27 +0000336 MCSymbol *COMDATSymbol = nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000337 if (!COMDATSymName.empty())
338 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
339
David Majnemerc57d0382014-06-27 17:19:44 +0000340 StringRef CachedName = std::get<0>(Iter->first);
Rafael Espindola0766ae02014-06-06 19:26:12 +0000341 MCSectionCOFF *Result = new (*this)
342 MCSectionCOFF(CachedName, Characteristics, COMDATSymbol, Selection, Kind);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000343
344 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000345 return Result;
346}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000347
Rafael Espindola60ec3832013-11-19 19:52:52 +0000348const MCSectionCOFF *
349MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
350 SectionKind Kind) {
351 return getCOFFSection(Section, Characteristics, Kind, "", 0);
352}
353
Nico Riecka37acf72013-07-06 12:13:10 +0000354const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
David Majnemerc57d0382014-06-27 17:19:44 +0000355 SectionGroupTriple T(Section, "", 0);
356 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000357 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000358 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000359 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000360}
361
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000362const MCSectionCOFF *
363MCContext::getAssociativeCOFFSection(const MCSectionCOFF *Sec,
364 const MCSymbol *KeySym) {
365 // Return the normal section if we don't have to be associative.
366 if (!KeySym)
367 return Sec;
368
369 // Make an associative section with the same name and kind as the normal
370 // section.
371 unsigned Characteristics =
372 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
373 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
374 KeySym->getName(),
375 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
376}
377
Kevin Enderbye5930f12010-07-28 20:55:35 +0000378//===----------------------------------------------------------------------===//
379// Dwarf Management
380//===----------------------------------------------------------------------===//
381
382/// GetDwarfFile - takes a file name an number to place in the dwarf file and
383/// directory tables. If the file number has already been allocated it is an
384/// error and zero is returned and the client reports the error, else the
385/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000386unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000387 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000388 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000389 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000390}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000391
Kevin Enderbya68d0042010-10-04 20:17:24 +0000392/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000393/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000394bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000395 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000396 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
397 return false;
398
David Blaikiea55ddad2014-03-13 18:55:04 +0000399 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000400}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000401
Oliver Stannard8b273082014-06-19 15:52:37 +0000402/// finalizeDwarfSections - Emit end symbols for each non-empty code section.
403/// Also remove empty sections from SectionStartEndSyms, to avoid generating
404/// useless debug info for them.
405void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
406 MCContext &context = MCOS.getContext();
407
408 auto sec = SectionStartEndSyms.begin();
409 while (sec != SectionStartEndSyms.end()) {
410 assert(sec->second.first && "Start symbol must be set by now");
411 MCOS.SwitchSection(sec->first);
412 if (MCOS.mayHaveInstructions()) {
413 MCSymbol *SectionEndSym = context.CreateTempSymbol();
414 MCOS.EmitLabel(SectionEndSym);
415 sec->second.second = SectionEndSym;
416 ++sec;
417 } else {
418 MapVector<const MCSection *, std::pair<MCSymbol *, MCSymbol *> >::iterator
419 to_erase = sec;
420 sec = SectionStartEndSyms.erase(to_erase);
421 }
422 }
423}
424
Kevin Enderby7ee97ce2014-04-22 21:42:18 +0000425void MCContext::FatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbachb18b4092012-01-26 23:20:11 +0000426 // If we have a source manager and a location, use it. Otherwise just
427 // use the generic report_fatal_error().
428 if (!SrcMgr || Loc == SMLoc())
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000429 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000430
431 // Use the source manager to print the message.
432 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
433
434 // If we reached here, we are failing ungracefully. Run the interrupt handlers
435 // to make sure any special cleanups get done, in particular that we remove
436 // files registered with RemoveFileOnSignal.
437 sys::RunInterruptHandlers();
438 exit(1);
439}