blob: b5ad518d0330e89f6846cee1fe0dcffc6469836d [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"
Rafael Espindola24d285d2015-05-26 00:32:28 +000013#include "llvm/MC/MCAssembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCDwarf.h"
16#include "llvm/MC/MCLabel.h"
17#include "llvm/MC/MCObjectFileInfo.h"
18#include "llvm/MC/MCRegisterInfo.h"
19#include "llvm/MC/MCSectionCOFF.h"
20#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCSectionMachO.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000022#include "llvm/MC/MCStreamer.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000023#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000024#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000025#include "llvm/MC/MCSymbolMachO.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000026#include "llvm/Support/COFF.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000027#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000028#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000029#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000030#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000031#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000033#include <map>
34
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000035using namespace llvm;
36
Bill Wendlingbc07a892013-06-18 07:20:20 +000037MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000038 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000039 bool DoAutoReset)
40 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Rafael Espindola9ab09232015-03-17 20:07:06 +000041 Symbols(Allocator), UsedNames(Allocator),
David Blaikie7400a972014-03-27 20:45:58 +000042 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000043 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000044 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
Oliver Stannard07b43d32015-11-17 09:58:07 +000045 AutoReset(DoAutoReset), HadError(false) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000046
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000047 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000048 if (EC)
49 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000050
Kevin Enderbye233dda2010-06-28 21:45:58 +000051 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000052 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000053 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000054
Alp Tokera55b95b2014-07-06 10:33:31 +000055 if (SrcMgr && SrcMgr->getNumBuffers())
56 MainFileName =
57 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000058}
59
60MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000061 if (AutoReset)
62 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000063
64 // NOTE: The symbols are all allocated out of a bump pointer allocator,
65 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000066}
67
68//===----------------------------------------------------------------------===//
69// Module Lifetime Management
70//===----------------------------------------------------------------------===//
71
Pedro Artigas7212ee42012-12-12 22:59:46 +000072void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000073 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000074 COFFAllocator.DestroyAll();
75 ELFAllocator.DestroyAll();
76 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000077
Akira Hatanakab11ef082015-11-14 06:35:56 +000078 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000079 UsedNames.clear();
80 Symbols.clear();
Keno Fischer21a7f232015-10-09 17:24:54 +000081 SectionSymbols.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000082 Allocator.Reset();
83 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000084 CompilationDir.clear();
85 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000086 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000087 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000088 MCGenDwarfLabelEntries.clear();
89 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000090 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000091 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000092
David Blaikie9ec32122014-04-10 23:55:11 +000093 MachOUniquingMap.clear();
94 ELFUniquingMap.clear();
95 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000096
Rafael Espindola9ab09232015-03-17 20:07:06 +000097 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000098 AllowTemporaryLabels = true;
99 DwarfLocSeen = false;
100 GenDwarfForAssembly = false;
101 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000102
103 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000104}
105
Chris Lattner20731122010-04-08 20:30:37 +0000106//===----------------------------------------------------------------------===//
107// Symbol Manipulation
108//===----------------------------------------------------------------------===//
109
Jim Grosbach6f482002015-05-18 18:43:14 +0000110MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000111 SmallString<128> NameSV;
112 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000113
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000114 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000115
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000116 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000117 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000118 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000119
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000120 return Sym;
121}
122
Rafael Espindolaa8695762015-06-02 00:25:12 +0000123MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
124 MCSymbolELF *&Sym = SectionSymbols[&Section];
Rafael Espindolab6613022014-10-17 01:48:58 +0000125 if (Sym)
126 return Sym;
127
128 StringRef Name = Section.getSectionName();
129
David Blaikie5106ce72014-11-19 05:49:42 +0000130 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000131 if (OldSym && OldSym->isUndefined()) {
Rafael Espindolaa8695762015-06-02 00:25:12 +0000132 Sym = cast<MCSymbolELF>(OldSym);
133 return Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000134 }
135
David Blaikie5106ce72014-11-19 05:49:42 +0000136 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Pete Cooper234b8752015-06-09 18:36:13 +0000137 Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000138
David Blaikie5106ce72014-11-19 05:49:42 +0000139 if (!OldSym)
140 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000141
142 return Sym;
143}
144
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000145MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
146 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000147 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000148 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000149}
150
David Majnemera225a192015-03-31 22:35:44 +0000151MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000152 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000153 "$parent_frame_offset");
154}
155
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000156MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
157 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
158 FuncName);
159}
160
Rafael Espindolaa8695762015-06-02 00:25:12 +0000161MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
162 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000163 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000164 switch (MOFI->getObjectFileType()) {
165 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000166 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000167 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000168 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000169 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000170 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000171 }
172 }
Pete Cooper234b8752015-06-09 18:36:13 +0000173 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
174 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000175}
176
Daniel Jasper41de8022015-06-23 11:31:32 +0000177MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
178 bool CanBeUnnamed) {
179 if (CanBeUnnamed && !UseNamesOnTempLabels)
180 return createSymbolImpl(nullptr, true);
181
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000182 // Determine whether this is an user writter assembler temporary or normal
183 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000184 bool IsTemporary = CanBeUnnamed;
185 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000186 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000187
Rafael Espindola9ab09232015-03-17 20:07:06 +0000188 SmallString<128> NewName = Name;
189 bool AddSuffix = AlwaysAddSuffix;
190 unsigned &NextUniqueID = NextID[Name];
191 for (;;) {
192 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000193 NewName.resize(Name.size());
194 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000195 }
196 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
197 if (NameEntry.second) {
198 // Ok, we found a name. Have the MCSymbol object itself refer to the copy
199 // of the string that is embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000200 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000201 }
202 assert(IsTemporary && "Cannot rename non-temporary symbols");
203 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000204 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000205 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000206}
207
Daniel Jasper41de8022015-06-23 11:31:32 +0000208MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
209 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000210 SmallString<128> NameSV;
211 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000212 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000213}
214
Jim Grosbach6f482002015-05-18 18:43:14 +0000215MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000216 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000217 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000218 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000219}
220
Daniel Jasper41de8022015-06-23 11:31:32 +0000221MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
222 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000223}
224
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000225unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000226 MCLabel *&Label = Instances[LocalLabelVal];
227 if (!Label)
228 Label = new (*this) MCLabel(0);
229 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000230}
231
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000232unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000233 MCLabel *&Label = Instances[LocalLabelVal];
234 if (!Label)
235 Label = new (*this) MCLabel(0);
236 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000237}
238
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000239MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
240 unsigned Instance) {
241 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
242 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000243 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000244 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000245}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000246
Jim Grosbach6f482002015-05-18 18:43:14 +0000247MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000248 unsigned Instance = NextInstance(LocalLabelVal);
249 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
250}
251
Jim Grosbach6f482002015-05-18 18:43:14 +0000252MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000253 bool Before) {
254 unsigned Instance = GetInstance(LocalLabelVal);
255 if (!Before)
256 ++Instance;
257 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000258}
259
Jim Grosbach6f482002015-05-18 18:43:14 +0000260MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000261 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000262 StringRef NameRef = Name.toStringRef(NameSV);
263 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000264}
265
Chris Lattner20731122010-04-08 20:30:37 +0000266//===----------------------------------------------------------------------===//
267// Section Management
268//===----------------------------------------------------------------------===//
269
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000270MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
271 unsigned TypeAndAttributes,
272 unsigned Reserved2, SectionKind Kind,
273 const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000274
Chris Lattner20731122010-04-08 20:30:37 +0000275 // We unique sections by their segment/section pair. The returned section
276 // may not have the same flags as the requested section, if so this should be
277 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000278
Chris Lattner20731122010-04-08 20:30:37 +0000279 // Form the name to look up.
280 SmallString<64> Name;
281 Name += Segment;
282 Name.push_back(',');
283 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000284
Chris Lattner20731122010-04-08 20:30:37 +0000285 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000286 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000287 if (Entry)
288 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000289
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000290 MCSymbol *Begin = nullptr;
291 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000292 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000293
Chris Lattner20731122010-04-08 20:30:37 +0000294 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000295 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
296 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000297}
Chris Lattner5418dd52010-04-08 21:26:26 +0000298
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000299void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000300 StringRef GroupName;
301 if (const MCSymbol *Group = Section->getGroup())
302 GroupName = Group->getName();
303
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000304 unsigned UniqueID = Section->getUniqueID();
305 ELFUniquingMap.erase(
306 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
307 auto I = ELFUniquingMap.insert(std::make_pair(
308 ELFSectionKey{Name, GroupName, UniqueID},
Jim Grosbach008359a2015-05-18 18:43:23 +0000309 Section))
310 .first;
Rafael Espindola44d50572015-03-27 21:34:24 +0000311 StringRef CachedName = I->first.SectionName;
Jim Grosbach008359a2015-05-18 18:43:23 +0000312 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000313}
314
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000315MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
316 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000317 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000318 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000319 StringMap<bool>::iterator I;
320 bool Inserted;
321 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
322
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000323 return new (ELFAllocator.Allocate())
Rafael Espindolac9d06922015-03-30 13:39:16 +0000324 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000325 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000326}
327
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000328MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
329 unsigned Flags, unsigned EntrySize,
330 StringRef Group, unsigned UniqueID,
331 const char *BeginSymName) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000332 MCSymbolELF *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000333 if (!Group.empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000334 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000335
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000336 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
337 BeginSymName, nullptr);
338}
339
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000340MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
341 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000342 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000343 unsigned UniqueID,
344 const char *BeginSymName,
345 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000346 StringRef Group = "";
347 if (GroupSym)
348 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000349 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000350 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000351 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000352 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000353 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000354 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000355
Rafael Espindola44d50572015-03-27 21:34:24 +0000356 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000357
358 SectionKind Kind;
359 if (Flags & ELF::SHF_EXECINSTR)
360 Kind = SectionKind::getText();
361 else
362 Kind = SectionKind::getReadOnly();
363
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000364 MCSymbol *Begin = nullptr;
365 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000366 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000367
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000368 MCSectionELF *Result = new (ELFAllocator.Allocate())
369 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID,
370 Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000371 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000372 return Result;
373}
374
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000375MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000376 MCSectionELF *Result = new (ELFAllocator.Allocate())
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000377 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000378 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000379 return Result;
380}
381
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000382MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
383 unsigned Characteristics,
384 SectionKind Kind,
385 StringRef COMDATSymName, int Selection,
386 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000387 MCSymbol *COMDATSymbol = nullptr;
388 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000389 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000390 COMDATSymName = COMDATSymbol->getName();
391 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000392
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000393 // Do the lookup, if we have a hit, return it.
Rafael Espindola44d50572015-03-27 21:34:24 +0000394 COFFSectionKey T{Section, COMDATSymName, Selection};
David Majnemerc57d0382014-06-27 17:19:44 +0000395 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000396 auto Iter = IterBool.first;
397 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000398 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000399
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000400 MCSymbol *Begin = nullptr;
401 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000402 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000403
Rafael Espindola44d50572015-03-27 21:34:24 +0000404 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000405 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000406 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000407
408 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000409 return Result;
410}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000411
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000412MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
413 unsigned Characteristics,
414 SectionKind Kind,
415 const char *BeginSymName) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000416 return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000417}
418
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000419MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Rafael Espindola44d50572015-03-27 21:34:24 +0000420 COFFSectionKey T{Section, "", 0};
David Majnemerc57d0382014-06-27 17:19:44 +0000421 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000422 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000423 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000424 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000425}
426
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000427MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
428 const MCSymbol *KeySym) {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000429 // Return the normal section if we don't have to be associative.
430 if (!KeySym)
431 return Sec;
432
433 // Make an associative section with the same name and kind as the normal
434 // section.
435 unsigned Characteristics =
436 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
437 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
438 KeySym->getName(),
439 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
440}
441
Akira Hatanakab11ef082015-11-14 06:35:56 +0000442MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
443 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
444}
445
Kevin Enderbye5930f12010-07-28 20:55:35 +0000446//===----------------------------------------------------------------------===//
447// Dwarf Management
448//===----------------------------------------------------------------------===//
449
Jim Grosbach6f482002015-05-18 18:43:14 +0000450/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000451/// directory tables. If the file number has already been allocated it is an
452/// error and zero is returned and the client reports the error, else the
453/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000454unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000455 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000456 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000457 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000458}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000459
Kevin Enderbya68d0042010-10-04 20:17:24 +0000460/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000461/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000462bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000463 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
464 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000465 return false;
466
David Blaikiea55ddad2014-03-13 18:55:04 +0000467 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000468}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000469
Rafael Espindolae0746792015-05-21 16:52:32 +0000470/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000471/// useless debug info for them.
472void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000473 SectionsForRanges.remove_if(
474 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000475}
476
Oliver Stannard07b43d32015-11-17 09:58:07 +0000477//===----------------------------------------------------------------------===//
478// Error Reporting
479//===----------------------------------------------------------------------===//
480
481void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
482 HadError = true;
483
484 // If we have a source manager use it. Otherwise just use the generic
485 // report_fatal_error().
486 if (!SrcMgr)
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000487 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000488
489 // Use the source manager to print the message.
490 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000491}
492
493void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
494 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000495
496 // If we reached here, we are failing ungracefully. Run the interrupt handlers
497 // to make sure any special cleanups get done, in particular that we remove
498 // files registered with RemoveFileOnSignal.
499 sys::RunInterruptHandlers();
500 exit(1);
501}