blob: 9149f89504083e5adab541ab4d201e154a92220e [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),
45 AutoReset(DoAutoReset) {
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.
Andrew Trick1c6a4c32013-12-10 04:39:05 +000066
Pedro Artigase84b13f2012-12-06 22:12:44 +000067 // If the stream for the .secure_log_unique directive was created free it.
Jim Grosbach008359a2015-05-18 18:43:23 +000068 delete (raw_ostream *)SecureLog;
Pedro Artigase84b13f2012-12-06 22:12:44 +000069}
70
71//===----------------------------------------------------------------------===//
72// Module Lifetime Management
73//===----------------------------------------------------------------------===//
74
Pedro Artigas7212ee42012-12-12 22:59:46 +000075void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000076 // Call the destructors so the fragments are freed
77 for (auto &I : ELFUniquingMap)
78 I.second->~MCSectionELF();
79 for (auto &I : COFFUniquingMap)
80 I.second->~MCSectionCOFF();
81 for (auto &I : MachOUniquingMap)
82 I.second->~MCSectionMachO();
83
Pedro Artigase84b13f2012-12-06 22:12:44 +000084 UsedNames.clear();
85 Symbols.clear();
86 Allocator.Reset();
87 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000088 CompilationDir.clear();
89 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000090 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000091 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000092 MCGenDwarfLabelEntries.clear();
93 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000094 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000095 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000096
David Blaikie9ec32122014-04-10 23:55:11 +000097 MachOUniquingMap.clear();
98 ELFUniquingMap.clear();
99 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000100
Rafael Espindola9ab09232015-03-17 20:07:06 +0000101 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000102 AllowTemporaryLabels = true;
103 DwarfLocSeen = false;
104 GenDwarfForAssembly = false;
105 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000106}
107
Chris Lattner20731122010-04-08 20:30:37 +0000108//===----------------------------------------------------------------------===//
109// Symbol Manipulation
110//===----------------------------------------------------------------------===//
111
Jim Grosbach6f482002015-05-18 18:43:14 +0000112MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000113 SmallString<128> NameSV;
114 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000115
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000116 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000117
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000118 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000119 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000120 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000121
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000122 return Sym;
123}
124
Rafael Espindolaa8695762015-06-02 00:25:12 +0000125MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
126 MCSymbolELF *&Sym = SectionSymbols[&Section];
Rafael Espindolab6613022014-10-17 01:48:58 +0000127 if (Sym)
128 return Sym;
129
130 StringRef Name = Section.getSectionName();
131
David Blaikie5106ce72014-11-19 05:49:42 +0000132 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000133 if (OldSym && OldSym->isUndefined()) {
Rafael Espindolaa8695762015-06-02 00:25:12 +0000134 Sym = cast<MCSymbolELF>(OldSym);
135 return Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000136 }
137
David Blaikie5106ce72014-11-19 05:49:42 +0000138 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Pete Cooper234b8752015-06-09 18:36:13 +0000139 Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000140
David Blaikie5106ce72014-11-19 05:49:42 +0000141 if (!OldSym)
142 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000143
144 return Sym;
145}
146
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000147MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
148 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000149 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000150 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000151}
152
David Majnemera225a192015-03-31 22:35:44 +0000153MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000154 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000155 "$parent_frame_offset");
156}
157
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000158MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
159 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
160 FuncName);
161}
162
Rafael Espindolaa8695762015-06-02 00:25:12 +0000163MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
164 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000165 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000166 switch (MOFI->getObjectFileType()) {
167 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000168 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000169 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000170 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000171 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000172 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000173 }
174 }
Pete Cooper234b8752015-06-09 18:36:13 +0000175 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
176 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000177}
178
Daniel Jasper41de8022015-06-23 11:31:32 +0000179MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
180 bool CanBeUnnamed) {
181 if (CanBeUnnamed && !UseNamesOnTempLabels)
182 return createSymbolImpl(nullptr, true);
183
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000184 // Determine whether this is an user writter assembler temporary or normal
185 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000186 bool IsTemporary = CanBeUnnamed;
187 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000188 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000189
Rafael Espindola9ab09232015-03-17 20:07:06 +0000190 SmallString<128> NewName = Name;
191 bool AddSuffix = AlwaysAddSuffix;
192 unsigned &NextUniqueID = NextID[Name];
193 for (;;) {
194 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000195 NewName.resize(Name.size());
196 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000197 }
198 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
199 if (NameEntry.second) {
200 // Ok, we found a name. Have the MCSymbol object itself refer to the copy
201 // of the string that is embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000202 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000203 }
204 assert(IsTemporary && "Cannot rename non-temporary symbols");
205 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000206 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000207 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000208}
209
Daniel Jasper41de8022015-06-23 11:31:32 +0000210MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
211 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000212 SmallString<128> NameSV;
213 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000214 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000215}
216
Jim Grosbach6f482002015-05-18 18:43:14 +0000217MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000218 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000219 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000220 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000221}
222
Daniel Jasper41de8022015-06-23 11:31:32 +0000223MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
224 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000225}
226
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000227unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000228 MCLabel *&Label = Instances[LocalLabelVal];
229 if (!Label)
230 Label = new (*this) MCLabel(0);
231 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000232}
233
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000234unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000235 MCLabel *&Label = Instances[LocalLabelVal];
236 if (!Label)
237 Label = new (*this) MCLabel(0);
238 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000239}
240
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000241MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
242 unsigned Instance) {
243 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
244 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000245 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000246 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000247}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000248
Jim Grosbach6f482002015-05-18 18:43:14 +0000249MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000250 unsigned Instance = NextInstance(LocalLabelVal);
251 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
252}
253
Jim Grosbach6f482002015-05-18 18:43:14 +0000254MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000255 bool Before) {
256 unsigned Instance = GetInstance(LocalLabelVal);
257 if (!Before)
258 ++Instance;
259 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000260}
261
Jim Grosbach6f482002015-05-18 18:43:14 +0000262MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000263 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000264 StringRef NameRef = Name.toStringRef(NameSV);
265 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000266}
267
Chris Lattner20731122010-04-08 20:30:37 +0000268//===----------------------------------------------------------------------===//
269// Section Management
270//===----------------------------------------------------------------------===//
271
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000272MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
273 unsigned TypeAndAttributes,
274 unsigned Reserved2, SectionKind Kind,
275 const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000276
Chris Lattner20731122010-04-08 20:30:37 +0000277 // We unique sections by their segment/section pair. The returned section
278 // may not have the same flags as the requested section, if so this should be
279 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000280
Chris Lattner20731122010-04-08 20:30:37 +0000281 // Form the name to look up.
282 SmallString<64> Name;
283 Name += Segment;
284 Name.push_back(',');
285 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000286
Chris Lattner20731122010-04-08 20:30:37 +0000287 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000288 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000289 if (Entry)
290 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000291
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000292 MCSymbol *Begin = nullptr;
293 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000294 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000295
Chris Lattner20731122010-04-08 20:30:37 +0000296 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000297 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000298 Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000299}
Chris Lattner5418dd52010-04-08 21:26:26 +0000300
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000301void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000302 StringRef GroupName;
303 if (const MCSymbol *Group = Section->getGroup())
304 GroupName = Group->getName();
305
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000306 unsigned UniqueID = Section->getUniqueID();
307 ELFUniquingMap.erase(
308 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
309 auto I = ELFUniquingMap.insert(std::make_pair(
310 ELFSectionKey{Name, GroupName, UniqueID},
Jim Grosbach008359a2015-05-18 18:43:23 +0000311 Section))
312 .first;
Rafael Espindola44d50572015-03-27 21:34:24 +0000313 StringRef CachedName = I->first.SectionName;
Jim Grosbach008359a2015-05-18 18:43:23 +0000314 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000315}
316
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000317MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
318 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000319 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000320 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000321 StringMap<bool>::iterator I;
322 bool Inserted;
323 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
324
325 return new (*this)
326 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000327 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000328}
329
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000330MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
331 unsigned Flags, unsigned EntrySize,
332 StringRef Group, unsigned UniqueID,
333 const char *BeginSymName) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000334 MCSymbolELF *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000335 if (!Group.empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000336 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000337
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000338 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
339 BeginSymName, nullptr);
340}
341
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000342MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
343 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000344 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000345 unsigned UniqueID,
346 const char *BeginSymName,
347 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000348 StringRef Group = "";
349 if (GroupSym)
350 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000351 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000352 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000353 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000354 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000355 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000356 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000357
Rafael Espindola44d50572015-03-27 21:34:24 +0000358 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000359
360 SectionKind Kind;
361 if (Flags & ELF::SHF_EXECINSTR)
362 Kind = SectionKind::getText();
363 else
364 Kind = SectionKind::getReadOnly();
365
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000366 MCSymbol *Begin = nullptr;
367 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000368 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000369
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000370 MCSectionELF *Result =
371 new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000372 GroupSym, UniqueID, Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000373 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000374 return Result;
375}
376
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000377MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000378 MCSectionELF *Result = new (*this)
379 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000380 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000381 return Result;
382}
383
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000384MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
385 unsigned Characteristics,
386 SectionKind Kind,
387 StringRef COMDATSymName, int Selection,
388 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000389 MCSymbol *COMDATSymbol = nullptr;
390 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000391 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000392 COMDATSymName = COMDATSymbol->getName();
393 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000394
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000395 // Do the lookup, if we have a hit, return it.
Rafael Espindola44d50572015-03-27 21:34:24 +0000396 COFFSectionKey T{Section, COMDATSymName, Selection};
David Majnemerc57d0382014-06-27 17:19:44 +0000397 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000398 auto Iter = IterBool.first;
399 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000400 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000401
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000402 MCSymbol *Begin = nullptr;
403 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000404 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000405
Rafael Espindola44d50572015-03-27 21:34:24 +0000406 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000407 MCSectionCOFF *Result = new (*this) MCSectionCOFF(
408 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000409
410 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000411 return Result;
412}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000413
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000414MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
415 unsigned Characteristics,
416 SectionKind Kind,
417 const char *BeginSymName) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000418 return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000419}
420
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000421MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Rafael Espindola44d50572015-03-27 21:34:24 +0000422 COFFSectionKey T{Section, "", 0};
David Majnemerc57d0382014-06-27 17:19:44 +0000423 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000424 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000425 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000426 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000427}
428
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000429MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
430 const MCSymbol *KeySym) {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000431 // Return the normal section if we don't have to be associative.
432 if (!KeySym)
433 return Sec;
434
435 // Make an associative section with the same name and kind as the normal
436 // section.
437 unsigned Characteristics =
438 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
439 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
440 KeySym->getName(),
441 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
442}
443
Kevin Enderbye5930f12010-07-28 20:55:35 +0000444//===----------------------------------------------------------------------===//
445// Dwarf Management
446//===----------------------------------------------------------------------===//
447
Jim Grosbach6f482002015-05-18 18:43:14 +0000448/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000449/// directory tables. If the file number has already been allocated it is an
450/// error and zero is returned and the client reports the error, else the
451/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000452unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000453 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000454 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000455 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000456}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000457
Kevin Enderbya68d0042010-10-04 20:17:24 +0000458/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000459/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000460bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000461 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
462 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000463 return false;
464
David Blaikiea55ddad2014-03-13 18:55:04 +0000465 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000466}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000467
Rafael Espindolae0746792015-05-21 16:52:32 +0000468/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000469/// useless debug info for them.
470void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000471 SectionsForRanges.remove_if(
472 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000473}
474
Jim Grosbach6f482002015-05-18 18:43:14 +0000475void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbachb18b4092012-01-26 23:20:11 +0000476 // If we have a source manager and a location, use it. Otherwise just
477 // use the generic report_fatal_error().
478 if (!SrcMgr || Loc == SMLoc())
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000479 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000480
481 // Use the source manager to print the message.
482 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
483
484 // If we reached here, we are failing ungracefully. Run the interrupt handlers
485 // to make sure any special cleanups get done, in particular that we remove
486 // files registered with RemoveFileOnSignal.
487 sys::RunInterruptHandlers();
488 exit(1);
489}