blob: b41e6d8201d5f63e10f99b3064959db7a65ff01c [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"
21#include "llvm/MC/MCSymbol.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000022#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000023#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000024#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000025#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000026#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000028#include <map>
29
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000030using namespace llvm;
31
Bill Wendlingbc07a892013-06-18 07:20:20 +000032MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000033 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000034 bool DoAutoReset)
35 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
36 Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0),
37 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000038 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000039 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
40 AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000041
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000042 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000043 if (EC)
44 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000045
Kevin Enderbye233dda2010-06-28 21:45:58 +000046 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000047 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000048 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000049
Alp Tokera55b95b2014-07-06 10:33:31 +000050 if (SrcMgr && SrcMgr->getNumBuffers())
51 MainFileName =
52 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000053}
54
55MCContext::~MCContext() {
56
Pedro Artigas7212ee42012-12-12 22:59:46 +000057 if (AutoReset)
58 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000059
60 // NOTE: The symbols are all allocated out of a bump pointer allocator,
61 // we don't need to free them here.
Andrew Trick1c6a4c32013-12-10 04:39:05 +000062
Pedro Artigase84b13f2012-12-06 22:12:44 +000063 // If the stream for the .secure_log_unique directive was created free it.
64 delete (raw_ostream*)SecureLog;
65}
66
67//===----------------------------------------------------------------------===//
68// Module Lifetime Management
69//===----------------------------------------------------------------------===//
70
Pedro Artigas7212ee42012-12-12 22:59:46 +000071void MCContext::reset() {
Pedro Artigase84b13f2012-12-06 22:12:44 +000072 UsedNames.clear();
73 Symbols.clear();
74 Allocator.Reset();
75 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000076 CompilationDir.clear();
77 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000078 MCDwarfLineTablesCUMap.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000079 SectionStartEndSyms.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000080 MCGenDwarfLabelEntries.clear();
81 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000082 DwarfCompileUnitID = 0;
Pedro Artigase84b13f2012-12-06 22:12:44 +000083 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000084
David Blaikie9ec32122014-04-10 23:55:11 +000085 MachOUniquingMap.clear();
86 ELFUniquingMap.clear();
87 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000088
89 NextUniqueID = 0;
90 AllowTemporaryLabels = true;
91 DwarfLocSeen = false;
92 GenDwarfForAssembly = false;
93 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000094}
95
Chris Lattner20731122010-04-08 20:30:37 +000096//===----------------------------------------------------------------------===//
97// Symbol Manipulation
98//===----------------------------------------------------------------------===//
99
Chris Lattner98970432010-03-30 18:10:53 +0000100MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +0000101 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000102
David Blaikie5106ce72014-11-19 05:49:42 +0000103 MCSymbol *&Sym = Symbols[Name];
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000104
David Blaikie5106ce72014-11-19 05:49:42 +0000105 if (!Sym)
106 Sym = CreateSymbol(Name);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000107
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000108 return Sym;
109}
110
Rafael Espindolab6613022014-10-17 01:48:58 +0000111MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
112 MCSymbol *&Sym = SectionSymbols[&Section];
113 if (Sym)
114 return Sym;
115
116 StringRef Name = Section.getSectionName();
117
David Blaikie5106ce72014-11-19 05:49:42 +0000118 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000119 if (OldSym && OldSym->isUndefined()) {
120 Sym = OldSym;
121 return OldSym;
122 }
123
David Blaikie5106ce72014-11-19 05:49:42 +0000124 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
125 Sym = new (*this) MCSymbol(NameIter->getKey(), /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000126
David Blaikie5106ce72014-11-19 05:49:42 +0000127 if (!OldSym)
128 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000129
130 return Sym;
131}
132
Reid Klecknere9b89312015-01-13 00:48:10 +0000133MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName) {
134 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
135 "frameallocation_" + FuncName);
136}
137
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000138MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000139 // Determine whether this is an assembler temporary or normal label, if used.
140 bool isTemporary = false;
141 if (AllowTemporaryLabels)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000142 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000143
David Blaikie5106ce72014-11-19 05:49:42 +0000144 auto NameEntry = UsedNames.insert(std::make_pair(Name, true));
145 if (!NameEntry.second) {
Alp Tokerf907b892013-12-05 05:44:44 +0000146 assert(isTemporary && "Cannot rename non-temporary symbols");
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000147 SmallString<128> NewName = Name;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000148 do {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000149 NewName.resize(Name.size());
150 raw_svector_ostream(NewName) << NextUniqueID++;
David Blaikie5106ce72014-11-19 05:49:42 +0000151 NameEntry = UsedNames.insert(std::make_pair(NewName, true));
152 } while (!NameEntry.second);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000153 }
Chris Lattner3f5738d2009-06-24 04:31:49 +0000154
Chris Lattner13348762010-03-15 06:15:35 +0000155 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000156 // to the copy of the string that is embedded in the UsedNames entry.
David Blaikie5106ce72014-11-19 05:49:42 +0000157 MCSymbol *Result =
158 new (*this) MCSymbol(NameEntry.first->getKey(), isTemporary);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000159
160 return Result;
Chris Lattner3f5738d2009-06-24 04:31:49 +0000161}
162
Chris Lattner98970432010-03-30 18:10:53 +0000163MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner86dfd732009-10-19 22:49:00 +0000164 SmallString<128> NameSV;
David Blaikie8e5283a2013-12-03 18:18:28 +0000165 return GetOrCreateSymbol(Name.toStringRef(NameSV));
Chris Lattner86dfd732009-10-19 22:49:00 +0000166}
167
Tim Northoverc3988b42014-03-29 07:05:06 +0000168MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000169 SmallString<128> NameSV;
170 raw_svector_ostream(NameSV)
171 << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
172 return CreateSymbol(NameSV);
Tim Northoverc3988b42014-03-29 07:05:06 +0000173}
174
Chris Lattner073d8172010-03-14 08:23:30 +0000175MCSymbol *MCContext::CreateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000176 SmallString<128> NameSV;
177 raw_svector_ostream(NameSV)
178 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
179 return CreateSymbol(NameSV);
Chris Lattner073d8172010-03-14 08:23:30 +0000180}
181
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000182unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000183 MCLabel *&Label = Instances[LocalLabelVal];
184 if (!Label)
185 Label = new (*this) MCLabel(0);
186 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000187}
188
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000189unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000190 MCLabel *&Label = Instances[LocalLabelVal];
191 if (!Label)
192 Label = new (*this) MCLabel(0);
193 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000194}
195
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000196MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
197 unsigned Instance) {
198 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
199 if (!Sym)
200 Sym = CreateTempSymbol();
201 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000202}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000203
204MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) {
205 unsigned Instance = NextInstance(LocalLabelVal);
206 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
207}
208
209MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal,
210 bool Before) {
211 unsigned Instance = GetInstance(LocalLabelVal);
212 if (!Before)
213 ++Instance;
214 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000215}
216
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000217MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000218 return Symbols.lookup(Name);
219}
Chris Lattner20731122010-04-08 20:30:37 +0000220
Roman Divacky0be33592012-09-18 17:10:37 +0000221MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
222 SmallString<128> NameSV;
223 Name.toVector(NameSV);
224 return LookupSymbol(NameSV.str());
225}
226
Chris Lattner20731122010-04-08 20:30:37 +0000227//===----------------------------------------------------------------------===//
228// Section Management
229//===----------------------------------------------------------------------===//
230
231const MCSectionMachO *MCContext::
232getMachOSection(StringRef Segment, StringRef Section,
233 unsigned TypeAndAttributes,
234 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000235
Chris Lattner20731122010-04-08 20:30:37 +0000236 // We unique sections by their segment/section pair. The returned section
237 // may not have the same flags as the requested section, if so this should be
238 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000239
Chris Lattner20731122010-04-08 20:30:37 +0000240 // Form the name to look up.
241 SmallString<64> Name;
242 Name += Segment;
243 Name.push_back(',');
244 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000245
Chris Lattner20731122010-04-08 20:30:37 +0000246 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000247 const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()];
Chris Lattner20731122010-04-08 20:30:37 +0000248 if (Entry) return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000249
Chris Lattner20731122010-04-08 20:30:37 +0000250 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000251 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
252 Reserved2, Kind);
Chris Lattner20731122010-04-08 20:30:37 +0000253}
Chris Lattner5418dd52010-04-08 21:26:26 +0000254
Rafael Espindola36ef57d2010-11-10 19:05:07 +0000255const MCSectionELF *MCContext::
Chris Lattner5418dd52010-04-08 21:26:26 +0000256getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000257 SectionKind Kind) {
258 return getELFSection(Section, Type, Flags, Kind, 0, "");
259}
260
David Blaikie8019bf82014-04-10 21:53:53 +0000261void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000262 StringRef GroupName;
263 if (const MCSymbol *Group = Section->getGroup())
264 GroupName = Group->getName();
265
David Blaikie9ec32122014-04-10 23:55:11 +0000266 ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName));
267 auto I =
268 ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName),
269 Section)).first;
David Blaikieb60e61c2014-04-11 22:49:14 +0000270 StringRef CachedName = I->first.first;
271 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000272}
273
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000274const MCSectionELF *MCContext::
275getELFSection(StringRef Section, unsigned Type, unsigned Flags,
276 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner5418dd52010-04-08 21:26:26 +0000277 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000278 auto IterBool = ELFUniquingMap.insert(
Craig Topperbb694de2014-04-13 04:57:38 +0000279 std::make_pair(SectionGroupPair(Section, Group), nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000280 auto &Entry = *IterBool.first;
281 if (!IterBool.second) return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000282
Jan Wen Voungefbdbe52010-09-30 05:59:22 +0000283 // Possibly refine the entry size first.
284 if (!EntrySize) {
285 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
286 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000287
Craig Topperbb694de2014-04-13 04:57:38 +0000288 MCSymbol *GroupSym = nullptr;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000289 if (!Group.empty())
290 GroupSym = GetOrCreateSymbol(Group);
291
David Blaikieb60e61c2014-04-11 22:49:14 +0000292 StringRef CachedName = Entry.first.first;
293 MCSectionELF *Result = new (*this)
294 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym);
David Blaikie9ec32122014-04-10 23:55:11 +0000295 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000296 return Result;
297}
298
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000299const MCSectionELF *MCContext::CreateELFGroupSection() {
300 MCSectionELF *Result =
Rafael Espindolaaea49582011-01-23 04:28:49 +0000301 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Craig Topperbb694de2014-04-13 04:57:38 +0000302 SectionKind::getReadOnly(), 4, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000303 return Result;
304}
305
Rafael Espindola0766ae02014-06-06 19:26:12 +0000306const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
307 unsigned Characteristics,
308 SectionKind Kind,
309 StringRef COMDATSymName,
310 int Selection) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000311 // Do the lookup, if we have a hit, return it.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000312
David Majnemerc57d0382014-06-27 17:19:44 +0000313 SectionGroupTriple T(Section, COMDATSymName, Selection);
314 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000315 auto Iter = IterBool.first;
316 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000317 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000318
David Majnemer8bce66b2014-07-14 22:57:27 +0000319 MCSymbol *COMDATSymbol = nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000320 if (!COMDATSymName.empty())
321 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
322
David Majnemerc57d0382014-06-27 17:19:44 +0000323 StringRef CachedName = std::get<0>(Iter->first);
Rafael Espindola0766ae02014-06-06 19:26:12 +0000324 MCSectionCOFF *Result = new (*this)
325 MCSectionCOFF(CachedName, Characteristics, COMDATSymbol, Selection, Kind);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000326
327 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000328 return Result;
329}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000330
Rafael Espindola60ec3832013-11-19 19:52:52 +0000331const MCSectionCOFF *
332MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
333 SectionKind Kind) {
334 return getCOFFSection(Section, Characteristics, Kind, "", 0);
335}
336
Nico Riecka37acf72013-07-06 12:13:10 +0000337const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
David Majnemerc57d0382014-06-27 17:19:44 +0000338 SectionGroupTriple T(Section, "", 0);
339 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000340 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000341 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000342 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000343}
344
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000345const MCSectionCOFF *
346MCContext::getAssociativeCOFFSection(const MCSectionCOFF *Sec,
347 const MCSymbol *KeySym) {
348 // Return the normal section if we don't have to be associative.
349 if (!KeySym)
350 return Sec;
351
352 // Make an associative section with the same name and kind as the normal
353 // section.
354 unsigned Characteristics =
355 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
356 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
357 KeySym->getName(),
358 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
359}
360
Kevin Enderbye5930f12010-07-28 20:55:35 +0000361//===----------------------------------------------------------------------===//
362// Dwarf Management
363//===----------------------------------------------------------------------===//
364
365/// GetDwarfFile - takes a file name an number to place in the dwarf file and
366/// directory tables. If the file number has already been allocated it is an
367/// error and zero is returned and the client reports the error, else the
368/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000369unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000370 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000371 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000372 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000373}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000374
Kevin Enderbya68d0042010-10-04 20:17:24 +0000375/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000376/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000377bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000378 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000379 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
380 return false;
381
David Blaikiea55ddad2014-03-13 18:55:04 +0000382 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000383}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000384
Oliver Stannard8b273082014-06-19 15:52:37 +0000385/// finalizeDwarfSections - Emit end symbols for each non-empty code section.
386/// Also remove empty sections from SectionStartEndSyms, to avoid generating
387/// useless debug info for them.
388void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
389 MCContext &context = MCOS.getContext();
390
391 auto sec = SectionStartEndSyms.begin();
392 while (sec != SectionStartEndSyms.end()) {
393 assert(sec->second.first && "Start symbol must be set by now");
394 MCOS.SwitchSection(sec->first);
395 if (MCOS.mayHaveInstructions()) {
396 MCSymbol *SectionEndSym = context.CreateTempSymbol();
397 MCOS.EmitLabel(SectionEndSym);
398 sec->second.second = SectionEndSym;
399 ++sec;
400 } else {
401 MapVector<const MCSection *, std::pair<MCSymbol *, MCSymbol *> >::iterator
402 to_erase = sec;
403 sec = SectionStartEndSyms.erase(to_erase);
404 }
405 }
406}
407
Kevin Enderby7ee97ce2014-04-22 21:42:18 +0000408void MCContext::FatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbachb18b4092012-01-26 23:20:11 +0000409 // If we have a source manager and a location, use it. Otherwise just
410 // use the generic report_fatal_error().
411 if (!SrcMgr || Loc == SMLoc())
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000412 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000413
414 // Use the source manager to print the message.
415 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
416
417 // If we reached here, we are failing ungracefully. Run the interrupt handlers
418 // to make sure any special cleanups get done, in particular that we remove
419 // files registered with RemoveFileOnSignal.
420 sys::RunInterruptHandlers();
421 exit(1);
422}