]> git.proxmox.com Git - rustc.git/blame - src/llvm/lib/MC/MCContext.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / lib / MC / MCContext.cpp
CommitLineData
223e47cc
LB
1//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2//
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"
970d7e83
LB
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
223e47cc 13#include "llvm/MC/MCAsmInfo.h"
970d7e83
LB
14#include "llvm/MC/MCDwarf.h"
15#include "llvm/MC/MCLabel.h"
223e47cc
LB
16#include "llvm/MC/MCObjectFileInfo.h"
17#include "llvm/MC/MCRegisterInfo.h"
223e47cc 18#include "llvm/MC/MCSectionCOFF.h"
970d7e83
LB
19#include "llvm/MC/MCSectionELF.h"
20#include "llvm/MC/MCSectionMachO.h"
223e47cc 21#include "llvm/MC/MCSymbol.h"
223e47cc
LB
22#include "llvm/Support/ELF.h"
23#include "llvm/Support/ErrorHandling.h"
1a4d82fc 24#include "llvm/Support/FileSystem.h"
970d7e83 25#include "llvm/Support/MemoryBuffer.h"
223e47cc 26#include "llvm/Support/Signals.h"
970d7e83 27#include "llvm/Support/SourceMgr.h"
1a4d82fc 28#include <map>
223e47cc 29
1a4d82fc 30using namespace llvm;
223e47cc 31
1a4d82fc 32MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
970d7e83 33 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
1a4d82fc
JJ
34 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),
38 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
39 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
40 AutoReset(DoAutoReset) {
41
42 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
43 if (EC)
44 CompilationDir.clear();
223e47cc
LB
45
46 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
1a4d82fc 47 SecureLog = nullptr;
223e47cc
LB
48 SecureLogUsed = false;
49
1a4d82fc
JJ
50 if (SrcMgr && SrcMgr->getNumBuffers())
51 MainFileName =
52 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
223e47cc
LB
53}
54
55MCContext::~MCContext() {
970d7e83
LB
56
57 if (AutoReset)
58 reset();
59
223e47cc
LB
60 // NOTE: The symbols are all allocated out of a bump pointer allocator,
61 // we don't need to free them here.
1a4d82fc 62
970d7e83
LB
63 // 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
71void MCContext::reset() {
72 UsedNames.clear();
73 Symbols.clear();
74 Allocator.Reset();
75 Instances.clear();
1a4d82fc
JJ
76 CompilationDir.clear();
77 MainFileName.clear();
78 MCDwarfLineTablesCUMap.clear();
79 SectionStartEndSyms.clear();
970d7e83
LB
80 MCGenDwarfLabelEntries.clear();
81 DwarfDebugFlags = StringRef();
970d7e83 82 DwarfCompileUnitID = 0;
970d7e83 83 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
223e47cc 84
1a4d82fc
JJ
85 MachOUniquingMap.clear();
86 ELFUniquingMap.clear();
87 COFFUniquingMap.clear();
223e47cc 88
970d7e83
LB
89 NextUniqueID = 0;
90 AllowTemporaryLabels = true;
91 DwarfLocSeen = false;
92 GenDwarfForAssembly = false;
93 GenDwarfFileNumber = 0;
223e47cc
LB
94}
95
96//===----------------------------------------------------------------------===//
97// Symbol Manipulation
98//===----------------------------------------------------------------------===//
99
100MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
101 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
102
85aaf69f 103 MCSymbol *&Sym = Symbols[Name];
223e47cc 104
85aaf69f
SL
105 if (!Sym)
106 Sym = CreateSymbol(Name);
107
108 return Sym;
109}
110
111MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
112 MCSymbol *&Sym = SectionSymbols[&Section];
223e47cc
LB
113 if (Sym)
114 return Sym;
115
85aaf69f
SL
116 StringRef Name = Section.getSectionName();
117
118 MCSymbol *&OldSym = Symbols[Name];
119 if (OldSym && OldSym->isUndefined()) {
120 Sym = OldSym;
121 return OldSym;
122 }
123
124 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
125 Sym = new (*this) MCSymbol(NameIter->getKey(), /*isTemporary*/ false);
126
127 if (!OldSym)
128 OldSym = Sym;
129
223e47cc
LB
130 return Sym;
131}
132
85aaf69f
SL
133MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName) {
134 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
135 "frameallocation_" + FuncName);
136}
137
223e47cc
LB
138MCSymbol *MCContext::CreateSymbol(StringRef Name) {
139 // Determine whether this is an assembler temporary or normal label, if used.
140 bool isTemporary = false;
141 if (AllowTemporaryLabels)
1a4d82fc 142 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
223e47cc 143
85aaf69f
SL
144 auto NameEntry = UsedNames.insert(std::make_pair(Name, true));
145 if (!NameEntry.second) {
1a4d82fc 146 assert(isTemporary && "Cannot rename non-temporary symbols");
223e47cc
LB
147 SmallString<128> NewName = Name;
148 do {
149 NewName.resize(Name.size());
150 raw_svector_ostream(NewName) << NextUniqueID++;
85aaf69f
SL
151 NameEntry = UsedNames.insert(std::make_pair(NewName, true));
152 } while (!NameEntry.second);
223e47cc 153 }
223e47cc
LB
154
155 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
156 // to the copy of the string that is embedded in the UsedNames entry.
85aaf69f
SL
157 MCSymbol *Result =
158 new (*this) MCSymbol(NameEntry.first->getKey(), isTemporary);
223e47cc
LB
159
160 return Result;
161}
162
163MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
164 SmallString<128> NameSV;
1a4d82fc
JJ
165 return GetOrCreateSymbol(Name.toStringRef(NameSV));
166}
167
168MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() {
169 SmallString<128> NameSV;
170 raw_svector_ostream(NameSV)
171 << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
172 return CreateSymbol(NameSV);
223e47cc
LB
173}
174
175MCSymbol *MCContext::CreateTempSymbol() {
176 SmallString<128> NameSV;
177 raw_svector_ostream(NameSV)
1a4d82fc 178 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
223e47cc
LB
179 return CreateSymbol(NameSV);
180}
181
1a4d82fc 182unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
223e47cc
LB
183 MCLabel *&Label = Instances[LocalLabelVal];
184 if (!Label)
185 Label = new (*this) MCLabel(0);
186 return Label->incInstance();
187}
188
1a4d82fc 189unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
223e47cc
LB
190 MCLabel *&Label = Instances[LocalLabelVal];
191 if (!Label)
192 Label = new (*this) MCLabel(0);
193 return Label->getInstance();
194}
195
1a4d82fc
JJ
196MCSymbol *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;
223e47cc 202}
1a4d82fc
JJ
203
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);
223e47cc
LB
215}
216
217MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
218 return Symbols.lookup(Name);
219}
220
221MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
222 SmallString<128> NameSV;
223 Name.toVector(NameSV);
224 return LookupSymbol(NameSV.str());
225}
226
227//===----------------------------------------------------------------------===//
228// Section Management
229//===----------------------------------------------------------------------===//
230
231const MCSectionMachO *MCContext::
232getMachOSection(StringRef Segment, StringRef Section,
233 unsigned TypeAndAttributes,
234 unsigned Reserved2, SectionKind Kind) {
235
236 // 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.
239
223e47cc
LB
240 // Form the name to look up.
241 SmallString<64> Name;
242 Name += Segment;
243 Name.push_back(',');
244 Name += Section;
245
246 // Do the lookup, if we have a hit, return it.
1a4d82fc 247 const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()];
223e47cc
LB
248 if (Entry) return Entry;
249
250 // Otherwise, return a new section.
251 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
252 Reserved2, Kind);
253}
254
255const MCSectionELF *MCContext::
256getELFSection(StringRef Section, unsigned Type, unsigned Flags,
257 SectionKind Kind) {
258 return getELFSection(Section, Type, Flags, Kind, 0, "");
259}
260
1a4d82fc
JJ
261void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) {
262 StringRef GroupName;
263 if (const MCSymbol *Group = Section->getGroup())
264 GroupName = Group->getName();
265
266 ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName));
267 auto I =
268 ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName),
269 Section)).first;
270 StringRef CachedName = I->first.first;
271 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName);
272}
273
223e47cc
LB
274const MCSectionELF *MCContext::
275getELFSection(StringRef Section, unsigned Type, unsigned Flags,
276 SectionKind Kind, unsigned EntrySize, StringRef Group) {
223e47cc 277 // Do the lookup, if we have a hit, return it.
1a4d82fc
JJ
278 auto IterBool = ELFUniquingMap.insert(
279 std::make_pair(SectionGroupPair(Section, Group), nullptr));
280 auto &Entry = *IterBool.first;
281 if (!IterBool.second) return Entry.second;
223e47cc
LB
282
283 // Possibly refine the entry size first.
284 if (!EntrySize) {
285 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
286 }
287
1a4d82fc 288 MCSymbol *GroupSym = nullptr;
223e47cc
LB
289 if (!Group.empty())
290 GroupSym = GetOrCreateSymbol(Group);
291
1a4d82fc
JJ
292 StringRef CachedName = Entry.first.first;
293 MCSectionELF *Result = new (*this)
294 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym);
295 Entry.second = Result;
223e47cc
LB
296 return Result;
297}
298
299const MCSectionELF *MCContext::CreateELFGroupSection() {
300 MCSectionELF *Result =
301 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
1a4d82fc 302 SectionKind::getReadOnly(), 4, nullptr);
223e47cc
LB
303 return Result;
304}
305
1a4d82fc
JJ
306const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
307 unsigned Characteristics,
308 SectionKind Kind,
309 StringRef COMDATSymName,
310 int Selection) {
223e47cc 311 // Do the lookup, if we have a hit, return it.
223e47cc 312
1a4d82fc
JJ
313 SectionGroupTriple T(Section, COMDATSymName, Selection);
314 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
315 auto Iter = IterBool.first;
316 if (!IterBool.second)
317 return Iter->second;
318
319 MCSymbol *COMDATSymbol = nullptr;
320 if (!COMDATSymName.empty())
321 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
322
323 StringRef CachedName = std::get<0>(Iter->first);
324 MCSectionCOFF *Result = new (*this)
325 MCSectionCOFF(CachedName, Characteristics, COMDATSymbol, Selection, Kind);
223e47cc 326
1a4d82fc 327 Iter->second = Result;
223e47cc
LB
328 return Result;
329}
330
1a4d82fc
JJ
331const MCSectionCOFF *
332MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
333 SectionKind Kind) {
334 return getCOFFSection(Section, Characteristics, Kind, "", 0);
335}
336
337const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
338 SectionGroupTriple T(Section, "", 0);
339 auto Iter = COFFUniquingMap.find(T);
340 if (Iter == COFFUniquingMap.end())
341 return nullptr;
342 return Iter->second;
343}
344
345const 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
223e47cc
LB
361//===----------------------------------------------------------------------===//
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.
369unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
970d7e83 370 unsigned FileNumber, unsigned CUID) {
1a4d82fc
JJ
371 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
372 return Table.getFile(Directory, FileName, FileNumber);
223e47cc
LB
373}
374
375/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
376/// currently is assigned and false otherwise.
970d7e83 377bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
1a4d82fc 378 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
223e47cc
LB
379 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
380 return false;
381
1a4d82fc
JJ
382 return !MCDwarfFiles[FileNumber].Name.empty();
383}
384
385/// 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 }
223e47cc
LB
406}
407
1a4d82fc 408void MCContext::FatalError(SMLoc Loc, const Twine &Msg) const {
223e47cc
LB
409 // 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())
1a4d82fc 412 report_fatal_error(Msg, false);
223e47cc
LB
413
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}