blob: 718c3cefaa3759b3b26ad2199e9f1f7025039a13 [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
David Blaikie15b25df2013-10-22 23:41:52 +000032typedef std::pair<std::string, std::string> SectionGroupPair;
33
Chris Lattner20731122010-04-08 20:30:37 +000034typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
David Blaikie15b25df2013-10-22 23:41:52 +000035typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy;
Rafael Espindola60ec3832013-11-19 19:52:52 +000036typedef std::map<SectionGroupPair, const MCSectionCOFF *> COFFUniqueMapTy;
Chris Lattner20731122010-04-08 20:30:37 +000037
Bill Wendlingbc07a892013-06-18 07:20:20 +000038MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000039 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Eric Christopher79f16562012-12-18 00:42:26 +000040 bool DoAutoReset) :
Jim Grosbachdfc5b552012-01-26 23:20:05 +000041 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedman0e402082011-04-18 05:02:31 +000042 Allocator(), Symbols(Allocator), UsedNames(Allocator),
Chandler Carruth10700aad2012-12-17 21:32:42 +000043 NextUniqueID(0),
Andrew Trick1c6a4c32013-12-10 04:39:05 +000044 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
Pedro Artigas7212ee42012-12-12 22:59:46 +000045 DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
Manman Ren4e042a62013-02-05 21:52:47 +000046 AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000047
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000048 error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000049 if (EC)
50 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000051
Chris Lattner20731122010-04-08 20:30:37 +000052 MachOUniquingMap = 0;
Chris Lattner5418dd52010-04-08 21:26:26 +000053 ELFUniquingMap = 0;
Chris Lattner87cffa92010-05-07 17:17:41 +000054 COFFUniquingMap = 0;
Kevin Enderbye233dda2010-06-28 21:45:58 +000055
56 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
57 SecureLog = 0;
58 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000059
60 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
61 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
62 else
63 MainFileName = "";
Pedro Artigase84b13f2012-12-06 22:12:44 +000064}
65
66MCContext::~MCContext() {
67
Pedro Artigas7212ee42012-12-12 22:59:46 +000068 if (AutoReset)
69 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000070
71 // NOTE: The symbols are all allocated out of a bump pointer allocator,
72 // we don't need to free them here.
Andrew Trick1c6a4c32013-12-10 04:39:05 +000073
Pedro Artigase84b13f2012-12-06 22:12:44 +000074 // If the stream for the .secure_log_unique directive was created free it.
75 delete (raw_ostream*)SecureLog;
76}
77
78//===----------------------------------------------------------------------===//
79// Module Lifetime Management
80//===----------------------------------------------------------------------===//
81
Pedro Artigas7212ee42012-12-12 22:59:46 +000082void MCContext::reset() {
Pedro Artigase84b13f2012-12-06 22:12:44 +000083 UsedNames.clear();
84 Symbols.clear();
85 Allocator.Reset();
86 Instances.clear();
Manman Ren1e427202013-03-07 01:42:00 +000087 MCDwarfFilesCUMap.clear();
88 MCDwarfDirsCUMap.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000089 MCGenDwarfLabelEntries.clear();
90 DwarfDebugFlags = StringRef();
91 MCLineSections.clear();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000092 DwarfCompileUnitID = 0;
93 MCLineTableSymbols.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000094 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000095
Chris Lattner20731122010-04-08 20:30:37 +000096 // If we have the MachO uniquing map, free it.
97 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner5418dd52010-04-08 21:26:26 +000098 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattner87cffa92010-05-07 17:17:41 +000099 delete (COFFUniqueMapTy*)COFFUniquingMap;
Pedro Artigase84b13f2012-12-06 22:12:44 +0000100 MachOUniquingMap = 0;
101 ELFUniquingMap = 0;
102 COFFUniquingMap = 0;
Pedro Artigas7212ee42012-12-12 22:59:46 +0000103
104 NextUniqueID = 0;
105 AllowTemporaryLabels = true;
106 DwarfLocSeen = false;
107 GenDwarfForAssembly = false;
108 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000109}
110
Chris Lattner20731122010-04-08 20:30:37 +0000111//===----------------------------------------------------------------------===//
112// Symbol Manipulation
113//===----------------------------------------------------------------------===//
114
Chris Lattner98970432010-03-30 18:10:53 +0000115MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +0000116 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000117
Chris Lattner13348762010-03-15 06:15:35 +0000118 // Do the lookup and get the entire StringMapEntry. We want access to the
119 // key if we are creating the entry.
120 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000121 MCSymbol *Sym = Entry.getValue();
122
123 if (Sym)
124 return Sym;
125
126 Sym = CreateSymbol(Name);
127 Entry.setValue(Sym);
128 return Sym;
129}
130
131MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000132 // Determine whether this is an assembler temporary or normal label, if used.
133 bool isTemporary = false;
134 if (AllowTemporaryLabels)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000135 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000136
137 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
138 if (NameEntry->getValue()) {
Alp Tokerf907b892013-12-05 05:44:44 +0000139 assert(isTemporary && "Cannot rename non-temporary symbols");
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000140 SmallString<128> NewName = Name;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000141 do {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000142 NewName.resize(Name.size());
143 raw_svector_ostream(NewName) << NextUniqueID++;
144 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000145 } while (NameEntry->getValue());
146 }
147 NameEntry->setValue(true);
Chris Lattner3f5738d2009-06-24 04:31:49 +0000148
Chris Lattner13348762010-03-15 06:15:35 +0000149 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000150 // to the copy of the string that is embedded in the UsedNames entry.
151 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
152
153 return Result;
Chris Lattner3f5738d2009-06-24 04:31:49 +0000154}
155
Chris Lattner98970432010-03-30 18:10:53 +0000156MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner86dfd732009-10-19 22:49:00 +0000157 SmallString<128> NameSV;
David Blaikie8e5283a2013-12-03 18:18:28 +0000158 return GetOrCreateSymbol(Name.toStringRef(NameSV));
Chris Lattner86dfd732009-10-19 22:49:00 +0000159}
160
Chris Lattner073d8172010-03-14 08:23:30 +0000161MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000162 SmallString<128> NameSV;
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000163 raw_svector_ostream(NameSV)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000164 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000165 return CreateSymbol(NameSV);
Chris Lattner073d8172010-03-14 08:23:30 +0000166}
167
Kevin Enderby0510b482010-05-17 23:08:19 +0000168unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000169 MCLabel *&Label = Instances[LocalLabelVal];
170 if (!Label)
171 Label = new (*this) MCLabel(0);
172 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000173}
174
175unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000176 MCLabel *&Label = Instances[LocalLabelVal];
177 if (!Label)
178 Label = new (*this) MCLabel(0);
179 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000180}
181
182MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000183 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderby0510b482010-05-17 23:08:19 +0000184 Twine(LocalLabelVal) +
185 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000186 Twine(NextInstance(LocalLabelVal)));
Kevin Enderby0510b482010-05-17 23:08:19 +0000187}
188MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
189 int bORf) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000190 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderby0510b482010-05-17 23:08:19 +0000191 Twine(LocalLabelVal) +
192 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000193 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderby0510b482010-05-17 23:08:19 +0000194}
195
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000196MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000197 return Symbols.lookup(Name);
198}
Chris Lattner20731122010-04-08 20:30:37 +0000199
Roman Divacky0be33592012-09-18 17:10:37 +0000200MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
201 SmallString<128> NameSV;
202 Name.toVector(NameSV);
203 return LookupSymbol(NameSV.str());
204}
205
Chris Lattner20731122010-04-08 20:30:37 +0000206//===----------------------------------------------------------------------===//
207// Section Management
208//===----------------------------------------------------------------------===//
209
210const MCSectionMachO *MCContext::
211getMachOSection(StringRef Segment, StringRef Section,
212 unsigned TypeAndAttributes,
213 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000214
Chris Lattner20731122010-04-08 20:30:37 +0000215 // We unique sections by their segment/section pair. The returned section
216 // may not have the same flags as the requested section, if so this should be
217 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000218
Chris Lattner20731122010-04-08 20:30:37 +0000219 // Create the map if it doesn't already exist.
220 if (MachOUniquingMap == 0)
221 MachOUniquingMap = new MachOUniqueMapTy();
222 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000223
Chris Lattner20731122010-04-08 20:30:37 +0000224 // Form the name to look up.
225 SmallString<64> Name;
226 Name += Segment;
227 Name.push_back(',');
228 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000229
Chris Lattner20731122010-04-08 20:30:37 +0000230 // Do the lookup, if we have a hit, return it.
231 const MCSectionMachO *&Entry = Map[Name.str()];
232 if (Entry) return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000233
Chris Lattner20731122010-04-08 20:30:37 +0000234 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000235 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
236 Reserved2, Kind);
Chris Lattner20731122010-04-08 20:30:37 +0000237}
Chris Lattner5418dd52010-04-08 21:26:26 +0000238
Rafael Espindola36ef57d2010-11-10 19:05:07 +0000239const MCSectionELF *MCContext::
Chris Lattner5418dd52010-04-08 21:26:26 +0000240getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000241 SectionKind Kind) {
242 return getELFSection(Section, Type, Flags, Kind, 0, "");
243}
244
245const MCSectionELF *MCContext::
246getELFSection(StringRef Section, unsigned Type, unsigned Flags,
247 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner5418dd52010-04-08 21:26:26 +0000248 if (ELFUniquingMap == 0)
249 ELFUniquingMap = new ELFUniqueMapTy();
250 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000251
Chris Lattner5418dd52010-04-08 21:26:26 +0000252 // Do the lookup, if we have a hit, return it.
David Blaikie15b25df2013-10-22 23:41:52 +0000253 std::pair<ELFUniqueMapTy::iterator, bool> Entry = Map.insert(
254 std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0));
255 if (!Entry.second) return Entry.first->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000256
Jan Wen Voungefbdbe52010-09-30 05:59:22 +0000257 // Possibly refine the entry size first.
258 if (!EntrySize) {
259 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
260 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000261
262 MCSymbol *GroupSym = NULL;
263 if (!Group.empty())
264 GroupSym = GetOrCreateSymbol(Group);
265
David Blaikie15b25df2013-10-22 23:41:52 +0000266 MCSectionELF *Result = new (*this) MCSectionELF(
267 Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym);
268 Entry.first->second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000269 return Result;
270}
271
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000272const MCSectionELF *MCContext::CreateELFGroupSection() {
273 MCSectionELF *Result =
Rafael Espindolaaea49582011-01-23 04:28:49 +0000274 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000275 SectionKind::getReadOnly(), 4, NULL);
276 return Result;
277}
278
Rafael Espindola60ec3832013-11-19 19:52:52 +0000279const MCSectionCOFF *
280MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
281 SectionKind Kind, StringRef COMDATSymName,
282 int Selection, const MCSectionCOFF *Assoc) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000283 if (COFFUniquingMap == 0)
284 COFFUniquingMap = new COFFUniqueMapTy();
285 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000286
Chris Lattner87cffa92010-05-07 17:17:41 +0000287 // Do the lookup, if we have a hit, return it.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000288
Rafael Espindola60ec3832013-11-19 19:52:52 +0000289 SectionGroupPair P(Section, COMDATSymName);
290 std::pair<COFFUniqueMapTy::iterator, bool> Entry =
291 Map.insert(std::make_pair(P, (MCSectionCOFF *)0));
292 COFFUniqueMapTy::iterator Iter = Entry.first;
293 if (!Entry.second)
294 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000295
Rafael Espindola60ec3832013-11-19 19:52:52 +0000296 const MCSymbol *COMDATSymbol = NULL;
297 if (!COMDATSymName.empty())
298 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
299
300 MCSectionCOFF *Result =
301 new (*this) MCSectionCOFF(Iter->first.first, Characteristics,
302 COMDATSymbol, Selection, Assoc, Kind);
303
304 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000305 return Result;
306}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000307
Rafael Espindola60ec3832013-11-19 19:52:52 +0000308const MCSectionCOFF *
309MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
310 SectionKind Kind) {
311 return getCOFFSection(Section, Characteristics, Kind, "", 0);
312}
313
Nico Riecka37acf72013-07-06 12:13:10 +0000314const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
315 if (COFFUniquingMap == 0)
316 COFFUniquingMap = new COFFUniqueMapTy();
317 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
318
Rafael Espindola60ec3832013-11-19 19:52:52 +0000319 SectionGroupPair P(Section, "");
320 COFFUniqueMapTy::iterator Iter = Map.find(P);
321 if (Iter == Map.end())
322 return 0;
323 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000324}
325
Kevin Enderbye5930f12010-07-28 20:55:35 +0000326//===----------------------------------------------------------------------===//
327// Dwarf Management
328//===----------------------------------------------------------------------===//
329
330/// GetDwarfFile - takes a file name an number to place in the dwarf file and
331/// directory tables. If the file number has already been allocated it is an
332/// error and zero is returned and the client reports the error, else the
333/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000334unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000335 unsigned FileNumber, unsigned CUID) {
Kevin Enderbye5930f12010-07-28 20:55:35 +0000336 // TODO: a FileNumber of zero says to use the next available file number.
337 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
338 // to not be less than one. This needs to be change to be not less than zero.
339
Manman Ren5ce24ff2013-03-12 20:17:00 +0000340 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
341 SmallVectorImpl<StringRef>& MCDwarfDirs = MCDwarfDirsCUMap[CUID];
Kevin Enderbye5930f12010-07-28 20:55:35 +0000342 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
343 if (FileNumber >= MCDwarfFiles.size()) {
344 MCDwarfFiles.resize(FileNumber + 1);
345 } else {
346 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
347 if (ExistingFile)
348 // It is an error to use see the same number more than once.
349 return 0;
350 }
351
352 // Get the new MCDwarfFile slot for this FileNumber.
353 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
354
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000355 if (Directory.empty()) {
356 // Separate the directory part from the basename of the FileName.
NAKAMURA Takumi30396ba2012-07-03 03:59:29 +0000357 StringRef tFileName = sys::path::filename(FileName);
358 if (!tFileName.empty()) {
359 Directory = sys::path::parent_path(FileName);
360 if (!Directory.empty())
NAKAMURA Takumi09e35102012-07-03 06:01:27 +0000361 FileName = tFileName;
Kevin Enderby82ed3be2011-11-01 23:39:05 +0000362 }
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000363 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000364
David Blaikiec47d0842014-03-12 16:56:05 +0000365 // Find or make an entry in the MCDwarfDirs vector for this Directory.
Kevin Enderbye5930f12010-07-28 20:55:35 +0000366 // Capture directory name.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000367 unsigned DirIndex;
368 if (Directory.empty()) {
369 // For FileNames with no directories a DirIndex of 0 is used.
370 DirIndex = 0;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000371 } else {
Nick Lewyckyf895efa2011-10-12 20:20:48 +0000372 DirIndex = 0;
373 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000374 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderbyd94dacf2010-08-31 22:55:11 +0000375 break;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000376 }
377 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000378 char *Buf = static_cast<char *>(Allocate(Directory.size()));
379 memcpy(Buf, Directory.data(), Directory.size());
380 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderbye5930f12010-07-28 20:55:35 +0000381 }
Kevin Enderby7221b762010-08-09 22:52:14 +0000382 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
383 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000384 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
385 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderby7221b762010-08-09 22:52:14 +0000386 DirIndex++;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000387 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000388
Kevin Enderbye5930f12010-07-28 20:55:35 +0000389 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
390 // vector.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000391 char *Buf = static_cast<char *>(Allocate(FileName.size()));
392 memcpy(Buf, FileName.data(), FileName.size());
393 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000394
395 // return the allocated FileNumber.
396 return FileNumber;
397}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000398
Kevin Enderbya68d0042010-10-04 20:17:24 +0000399/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000400/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000401bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Manman Ren5ce24ff2013-03-12 20:17:00 +0000402 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000403 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
404 return false;
405
Kevin Enderbya68d0042010-10-04 20:17:24 +0000406 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000407}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000408
409void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
410 // If we have a source manager and a location, use it. Otherwise just
411 // use the generic report_fatal_error().
412 if (!SrcMgr || Loc == SMLoc())
413 report_fatal_error(Msg);
414
415 // Use the source manager to print the message.
416 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
417
418 // If we reached here, we are failing ungracefully. Run the interrupt handlers
419 // to make sure any special cleanups get done, in particular that we remove
420 // files registered with RemoveFileOnSignal.
421 sys::RunInterruptHandlers();
422 exit(1);
423}