blob: c16326829d8aa27b0ab214f5b08edc070156bfa0 [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-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 Lattner7c5b0212009-10-19 22:49:00 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-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 Espindolac85dca62011-01-23 04:28:49 +000022#include "llvm/Support/ELF.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000023#include "llvm/Support/ErrorHandling.h"
Rafael Espindola76858a72013-06-14 20:26:58 +000024#include "llvm/Support/FileSystem.h"
Eric Christopher6c583142012-12-18 00:31:01 +000025#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000026#include "llvm/Support/Signals.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/SourceMgr.h"
David Blaikie2b3ea3c2013-10-22 23:41:52 +000028#include <map>
29
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030using namespace llvm;
31
Bill Wendling99cb6222013-06-18 07:20:20 +000032MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigas873a1dd2012-12-06 22:12:44 +000033 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Stephen Hines36b56882014-04-23 16:57:46 -070034 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),
Stephen Hinesdce4a402014-05-29 02:49:00 -070038 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
Stephen Hines36b56882014-04-23 16:57:46 -070039 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
40 AutoReset(DoAutoReset) {
Pedro Artigas5399d252012-12-12 22:59:46 +000041
Rafael Espindola76858a72013-06-14 20:26:58 +000042 error_code EC = llvm::sys::fs::current_path(CompilationDir);
Stephen Hines36b56882014-04-23 16:57:46 -070043 if (EC)
44 CompilationDir.clear();
Rafael Espindola76858a72013-06-14 20:26:58 +000045
Kevin Enderbyf187ac52010-06-28 21:45:58 +000046 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Stephen Hinesdce4a402014-05-29 02:49:00 -070047 SecureLog = nullptr;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000048 SecureLogUsed = false;
Eric Christopher6c583142012-12-18 00:31:01 +000049
50 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
51 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000052}
53
54MCContext::~MCContext() {
55
Pedro Artigas5399d252012-12-12 22:59:46 +000056 if (AutoReset)
57 reset();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000058
59 // NOTE: The symbols are all allocated out of a bump pointer allocator,
60 // we don't need to free them here.
Stephen Hines36b56882014-04-23 16:57:46 -070061
Pedro Artigas873a1dd2012-12-06 22:12:44 +000062 // If the stream for the .secure_log_unique directive was created free it.
63 delete (raw_ostream*)SecureLog;
64}
65
66//===----------------------------------------------------------------------===//
67// Module Lifetime Management
68//===----------------------------------------------------------------------===//
69
Pedro Artigas5399d252012-12-12 22:59:46 +000070void MCContext::reset() {
Pedro Artigas873a1dd2012-12-06 22:12:44 +000071 UsedNames.clear();
72 Symbols.clear();
73 Allocator.Reset();
74 Instances.clear();
Stephen Hines36b56882014-04-23 16:57:46 -070075 MCDwarfLineTablesCUMap.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000076 MCGenDwarfLabelEntries.clear();
77 DwarfDebugFlags = StringRef();
Pedro Artigas9e7924d2013-02-20 00:10:29 +000078 DwarfCompileUnitID = 0;
Pedro Artigas873a1dd2012-12-06 22:12:44 +000079 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencer326990f2010-11-26 04:16:08 +000080
Stephen Hinesdce4a402014-05-29 02:49:00 -070081 MachOUniquingMap.clear();
82 ELFUniquingMap.clear();
83 COFFUniquingMap.clear();
Pedro Artigas5399d252012-12-12 22:59:46 +000084
85 NextUniqueID = 0;
86 AllowTemporaryLabels = true;
87 DwarfLocSeen = false;
88 GenDwarfForAssembly = false;
89 GenDwarfFileNumber = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000090}
91
Chris Lattnerf0559e42010-04-08 20:30:37 +000092//===----------------------------------------------------------------------===//
93// Symbol Manipulation
94//===----------------------------------------------------------------------===//
95
Chris Lattner9b97a732010-03-30 18:10:53 +000096MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000097 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +000098
Chris Lattnerc28cc092010-03-15 06:15:35 +000099 // Do the lookup and get the entire StringMapEntry. We want access to the
100 // key if we are creating the entry.
101 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +0000102 MCSymbol *Sym = Entry.getValue();
103
104 if (Sym)
105 return Sym;
106
107 Sym = CreateSymbol(Name);
108 Entry.setValue(Sym);
109 return Sym;
110}
111
112MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000113 // Determine whether this is an assembler temporary or normal label, if used.
114 bool isTemporary = false;
115 if (AllowTemporaryLabels)
Bill Wendling99cb6222013-06-18 07:20:20 +0000116 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +0000117
118 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
119 if (NameEntry->getValue()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700120 assert(isTemporary && "Cannot rename non-temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000121 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +0000122 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000123 NewName.resize(Name.size());
124 raw_svector_ostream(NewName) << NextUniqueID++;
125 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +0000126 } while (NameEntry->getValue());
127 }
128 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +0000129
Chris Lattnerc28cc092010-03-15 06:15:35 +0000130 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +0000131 // to the copy of the string that is embedded in the UsedNames entry.
132 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
133
134 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000135}
136
Chris Lattner9b97a732010-03-30 18:10:53 +0000137MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000138 SmallString<128> NameSV;
Stephen Hines36b56882014-04-23 16:57:46 -0700139 return GetOrCreateSymbol(Name.toStringRef(NameSV));
140}
141
142MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() {
143 SmallString<128> NameSV;
144 raw_svector_ostream(NameSV)
145 << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
146 return CreateSymbol(NameSV);
Chris Lattner7c5b0212009-10-19 22:49:00 +0000147}
148
Chris Lattner1d72a762010-03-14 08:23:30 +0000149MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000150 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000151 raw_svector_ostream(NameSV)
Bill Wendling99cb6222013-06-18 07:20:20 +0000152 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000153 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000154}
155
Stephen Hines36b56882014-04-23 16:57:46 -0700156unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000157 MCLabel *&Label = Instances[LocalLabelVal];
158 if (!Label)
159 Label = new (*this) MCLabel(0);
160 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000161}
162
Stephen Hines36b56882014-04-23 16:57:46 -0700163unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000164 MCLabel *&Label = Instances[LocalLabelVal];
165 if (!Label)
166 Label = new (*this) MCLabel(0);
167 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000168}
169
Stephen Hines36b56882014-04-23 16:57:46 -0700170MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
171 unsigned Instance) {
172 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
173 if (!Sym)
174 Sym = CreateTempSymbol();
175 return Sym;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000176}
Stephen Hines36b56882014-04-23 16:57:46 -0700177
178MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) {
179 unsigned Instance = NextInstance(LocalLabelVal);
180 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
181}
182
183MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal,
184 bool Before) {
185 unsigned Instance = GetInstance(LocalLabelVal);
186 if (!Before)
187 ++Instance;
188 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000189}
190
Daniel Dunbar2928c832009-11-06 10:58:06 +0000191MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000192 return Symbols.lookup(Name);
193}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000194
Roman Divackyf145c132012-09-18 17:10:37 +0000195MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
196 SmallString<128> NameSV;
197 Name.toVector(NameSV);
198 return LookupSymbol(NameSV.str());
199}
200
Chris Lattnerf0559e42010-04-08 20:30:37 +0000201//===----------------------------------------------------------------------===//
202// Section Management
203//===----------------------------------------------------------------------===//
204
205const MCSectionMachO *MCContext::
206getMachOSection(StringRef Segment, StringRef Section,
207 unsigned TypeAndAttributes,
208 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000209
Chris Lattnerf0559e42010-04-08 20:30:37 +0000210 // We unique sections by their segment/section pair. The returned section
211 // may not have the same flags as the requested section, if so this should be
212 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000213
Chris Lattnerf0559e42010-04-08 20:30:37 +0000214 // Form the name to look up.
215 SmallString<64> Name;
216 Name += Segment;
217 Name.push_back(',');
218 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000219
Chris Lattnerf0559e42010-04-08 20:30:37 +0000220 // Do the lookup, if we have a hit, return it.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700221 const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()];
Chris Lattnerf0559e42010-04-08 20:30:37 +0000222 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000223
Chris Lattnerf0559e42010-04-08 20:30:37 +0000224 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000225 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
226 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000227}
Chris Lattner74aae472010-04-08 21:26:26 +0000228
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000229const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000230getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000231 SectionKind Kind) {
232 return getELFSection(Section, Type, Flags, Kind, 0, "");
233}
234
Stephen Hinesdce4a402014-05-29 02:49:00 -0700235void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) {
236 StringRef GroupName;
237 if (const MCSymbol *Group = Section->getGroup())
238 GroupName = Group->getName();
239
240 ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName));
241 auto I =
242 ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName),
243 Section)).first;
244 StringRef CachedName = I->first.first;
245 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName);
246}
247
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000248const MCSectionELF *MCContext::
249getELFSection(StringRef Section, unsigned Type, unsigned Flags,
250 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000251 // Do the lookup, if we have a hit, return it.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700252 auto IterBool = ELFUniquingMap.insert(
253 std::make_pair(SectionGroupPair(Section, Group), nullptr));
254 auto &Entry = *IterBool.first;
255 if (!IterBool.second) return Entry.second;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000256
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000257 // Possibly refine the entry size first.
258 if (!EntrySize) {
259 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
260 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000261
Stephen Hinesdce4a402014-05-29 02:49:00 -0700262 MCSymbol *GroupSym = nullptr;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000263 if (!Group.empty())
264 GroupSym = GetOrCreateSymbol(Group);
265
Stephen Hinesdce4a402014-05-29 02:49:00 -0700266 StringRef CachedName = Entry.first.first;
267 MCSectionELF *Result = new (*this)
268 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym);
269 Entry.second = Result;
Chris Lattner74aae472010-04-08 21:26:26 +0000270 return Result;
271}
272
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000273const MCSectionELF *MCContext::CreateELFGroupSection() {
274 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000275 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700276 SectionKind::getReadOnly(), 4, nullptr);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000277 return Result;
278}
279
Bill Wendling0ae07092013-11-27 06:44:18 +0000280const MCSectionCOFF *
281MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
282 SectionKind Kind, StringRef COMDATSymName,
283 int Selection, const MCSectionCOFF *Assoc) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000284 // Do the lookup, if we have a hit, return it.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000285
Bill Wendling0ae07092013-11-27 06:44:18 +0000286 SectionGroupPair P(Section, COMDATSymName);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700287 auto IterBool = COFFUniquingMap.insert(std::make_pair(P, nullptr));
288 auto Iter = IterBool.first;
289 if (!IterBool.second)
Bill Wendling0ae07092013-11-27 06:44:18 +0000290 return Iter->second;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000291
Stephen Hinesdce4a402014-05-29 02:49:00 -0700292 const MCSymbol *COMDATSymbol = nullptr;
Bill Wendling0ae07092013-11-27 06:44:18 +0000293 if (!COMDATSymName.empty())
294 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
295
Stephen Hinesdce4a402014-05-29 02:49:00 -0700296 StringRef CachedName = Iter->first.first;
297 MCSectionCOFF *Result = new (*this) MCSectionCOFF(
298 CachedName, Characteristics, COMDATSymbol, Selection, Assoc, Kind);
Bill Wendling0ae07092013-11-27 06:44:18 +0000299
300 Iter->second = Result;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000301 return Result;
302}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000303
Bill Wendling0ae07092013-11-27 06:44:18 +0000304const MCSectionCOFF *
305MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
306 SectionKind Kind) {
307 return getCOFFSection(Section, Characteristics, Kind, "", 0);
308}
309
Nico Rieck80646282013-07-06 12:13:10 +0000310const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Bill Wendling0ae07092013-11-27 06:44:18 +0000311 SectionGroupPair P(Section, "");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700312 auto Iter = COFFUniquingMap.find(P);
313 if (Iter == COFFUniquingMap.end())
314 return nullptr;
Bill Wendling0ae07092013-11-27 06:44:18 +0000315 return Iter->second;
Nico Rieck80646282013-07-06 12:13:10 +0000316}
317
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000318//===----------------------------------------------------------------------===//
319// Dwarf Management
320//===----------------------------------------------------------------------===//
321
322/// GetDwarfFile - takes a file name an number to place in the dwarf file and
323/// directory tables. If the file number has already been allocated it is an
324/// error and zero is returned and the client reports the error, else the
325/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000326unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren3de61b42013-03-07 01:42:00 +0000327 unsigned FileNumber, unsigned CUID) {
Stephen Hines36b56882014-04-23 16:57:46 -0700328 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
329 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000330}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000331
Kevin Enderby3f55c242010-10-04 20:17:24 +0000332/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000333/// currently is assigned and false otherwise.
Manman Ren3de61b42013-03-07 01:42:00 +0000334bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Stephen Hines36b56882014-04-23 16:57:46 -0700335 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000336 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
337 return false;
338
Stephen Hines36b56882014-04-23 16:57:46 -0700339 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000340}
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000341
Stephen Hinesdce4a402014-05-29 02:49:00 -0700342void MCContext::FatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000343 // If we have a source manager and a location, use it. Otherwise just
344 // use the generic report_fatal_error().
345 if (!SrcMgr || Loc == SMLoc())
Stephen Hines36b56882014-04-23 16:57:46 -0700346 report_fatal_error(Msg, false);
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000347
348 // Use the source manager to print the message.
349 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
350
351 // If we reached here, we are failing ungracefully. Run the interrupt handlers
352 // to make sure any special cleanups get done, in particular that we remove
353 // files registered with RemoveFileOnSignal.
354 sys::RunInterruptHandlers();
355 exit(1);
356}