blob: 79f9bdae46316b8c1d223aaa851ea62c598f2fbf [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"
Rafael Espindolaa8695762015-06-02 00:25:12 +000023#include "llvm/MC/MCSymbolELF.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000024#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000025#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000026#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000027#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000028#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000030#include <map>
31
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032using namespace llvm;
33
Bill Wendlingbc07a892013-06-18 07:20:20 +000034MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000035 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000036 bool DoAutoReset)
37 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Rafael Espindola9ab09232015-03-17 20:07:06 +000038 Symbols(Allocator), UsedNames(Allocator),
David Blaikie7400a972014-03-27 20:45:58 +000039 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000040 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000041 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
42 AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000043
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000045 if (EC)
46 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000047
Kevin Enderbye233dda2010-06-28 21:45:58 +000048 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000049 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000050 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000051
Alp Tokera55b95b2014-07-06 10:33:31 +000052 if (SrcMgr && SrcMgr->getNumBuffers())
53 MainFileName =
54 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000055}
56
57MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000058 if (AutoReset)
59 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000060
61 // NOTE: The symbols are all allocated out of a bump pointer allocator,
62 // we don't need to free them here.
Andrew Trick1c6a4c32013-12-10 04:39:05 +000063
Pedro Artigase84b13f2012-12-06 22:12:44 +000064 // If the stream for the .secure_log_unique directive was created free it.
Jim Grosbach008359a2015-05-18 18:43:23 +000065 delete (raw_ostream *)SecureLog;
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
74 for (auto &I : ELFUniquingMap)
75 I.second->~MCSectionELF();
76 for (auto &I : COFFUniquingMap)
77 I.second->~MCSectionCOFF();
78 for (auto &I : MachOUniquingMap)
79 I.second->~MCSectionMachO();
80
Pedro Artigase84b13f2012-12-06 22:12:44 +000081 UsedNames.clear();
82 Symbols.clear();
83 Allocator.Reset();
84 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000085 CompilationDir.clear();
86 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000087 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000088 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000089 MCGenDwarfLabelEntries.clear();
90 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000091 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000092 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000093
David Blaikie9ec32122014-04-10 23:55:11 +000094 MachOUniquingMap.clear();
95 ELFUniquingMap.clear();
96 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000097
Rafael Espindola9ab09232015-03-17 20:07:06 +000098 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000099 AllowTemporaryLabels = true;
100 DwarfLocSeen = false;
101 GenDwarfForAssembly = false;
102 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000103}
104
Chris Lattner20731122010-04-08 20:30:37 +0000105//===----------------------------------------------------------------------===//
106// Symbol Manipulation
107//===----------------------------------------------------------------------===//
108
Jim Grosbach6f482002015-05-18 18:43:14 +0000109MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000110 SmallString<128> NameSV;
111 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000112
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000113 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000114
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000115 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000116 if (!Sym)
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000117 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000118
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000119 return Sym;
120}
121
Rafael Espindolaa8695762015-06-02 00:25:12 +0000122MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
123 MCSymbolELF *&Sym = SectionSymbols[&Section];
Rafael Espindolab6613022014-10-17 01:48:58 +0000124 if (Sym)
125 return Sym;
126
127 StringRef Name = Section.getSectionName();
128
David Blaikie5106ce72014-11-19 05:49:42 +0000129 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000130 if (OldSym && OldSym->isUndefined()) {
Rafael Espindolaa8695762015-06-02 00:25:12 +0000131 Sym = cast<MCSymbolELF>(OldSym);
132 return Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000133 }
134
David Blaikie5106ce72014-11-19 05:49:42 +0000135 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Rafael Espindolaa8695762015-06-02 00:25:12 +0000136 Sym = new (*this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000137
David Blaikie5106ce72014-11-19 05:49:42 +0000138 if (!OldSym)
139 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000140
141 return Sym;
142}
143
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000144MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
145 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000146 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000147 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000148}
149
David Majnemera225a192015-03-31 22:35:44 +0000150MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000151 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000152 "$parent_frame_offset");
153}
154
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000155MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
156 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
157 FuncName);
158}
159
Rafael Espindolaa8695762015-06-02 00:25:12 +0000160MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
161 bool IsTemporary) {
162 bool IsELF = MOFI && MOFI->getObjectFileType() == MCObjectFileInfo::IsELF;
163 if (IsELF)
164 return new (*this) MCSymbolELF(Name, IsTemporary);
165 return new (*this) MCSymbol(false, Name, IsTemporary);
166}
167
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000168MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
169 bool IsTemporary) {
170 if (IsTemporary && !UseNamesOnTempLabels)
171 return createSymbolImpl(nullptr, true);
172
173 // Determine whether this is an user writter assembler temporary or normal
174 // label, if used.
175 IsTemporary = false;
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000176 if (AllowTemporaryLabels)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000177 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000178
Rafael Espindola9ab09232015-03-17 20:07:06 +0000179 SmallString<128> NewName = Name;
180 bool AddSuffix = AlwaysAddSuffix;
181 unsigned &NextUniqueID = NextID[Name];
182 for (;;) {
183 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000184 NewName.resize(Name.size());
185 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000186 }
187 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
188 if (NameEntry.second) {
189 // Ok, we found a name. Have the MCSymbol object itself refer to the copy
190 // of the string that is embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000191 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000192 }
193 assert(IsTemporary && "Cannot rename non-temporary symbols");
194 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000195 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000196 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000197}
198
Rafael Espindola9ab09232015-03-17 20:07:06 +0000199MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000200 SmallString<128> NameSV;
201 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000202 return createSymbol(NameSV, AlwaysAddSuffix, true);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000203}
204
Jim Grosbach6f482002015-05-18 18:43:14 +0000205MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000206 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000207 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000208 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000209}
210
Jim Grosbach6f482002015-05-18 18:43:14 +0000211MCSymbol *MCContext::createTempSymbol() {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000212 return createTempSymbol("tmp", true);
Chris Lattner073d8172010-03-14 08:23:30 +0000213}
214
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000215unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000216 MCLabel *&Label = Instances[LocalLabelVal];
217 if (!Label)
218 Label = new (*this) MCLabel(0);
219 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000220}
221
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000222unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000223 MCLabel *&Label = Instances[LocalLabelVal];
224 if (!Label)
225 Label = new (*this) MCLabel(0);
226 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000227}
228
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000229MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
230 unsigned Instance) {
231 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
232 if (!Sym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000233 Sym = createTempSymbol();
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000234 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000235}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000236
Jim Grosbach6f482002015-05-18 18:43:14 +0000237MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000238 unsigned Instance = NextInstance(LocalLabelVal);
239 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
240}
241
Jim Grosbach6f482002015-05-18 18:43:14 +0000242MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000243 bool Before) {
244 unsigned Instance = GetInstance(LocalLabelVal);
245 if (!Before)
246 ++Instance;
247 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000248}
249
Jim Grosbach6f482002015-05-18 18:43:14 +0000250MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000251 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000252 StringRef NameRef = Name.toStringRef(NameSV);
253 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000254}
255
Chris Lattner20731122010-04-08 20:30:37 +0000256//===----------------------------------------------------------------------===//
257// Section Management
258//===----------------------------------------------------------------------===//
259
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000260MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
261 unsigned TypeAndAttributes,
262 unsigned Reserved2, SectionKind Kind,
263 const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000264
Chris Lattner20731122010-04-08 20:30:37 +0000265 // We unique sections by their segment/section pair. The returned section
266 // may not have the same flags as the requested section, if so this should be
267 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000268
Chris Lattner20731122010-04-08 20:30:37 +0000269 // Form the name to look up.
270 SmallString<64> Name;
271 Name += Segment;
272 Name.push_back(',');
273 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000274
Chris Lattner20731122010-04-08 20:30:37 +0000275 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000276 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000277 if (Entry)
278 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000279
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000280 MCSymbol *Begin = nullptr;
281 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000282 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000283
Chris Lattner20731122010-04-08 20:30:37 +0000284 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000285 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000286 Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000287}
Chris Lattner5418dd52010-04-08 21:26:26 +0000288
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000289void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000290 StringRef GroupName;
291 if (const MCSymbol *Group = Section->getGroup())
292 GroupName = Group->getName();
293
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000294 unsigned UniqueID = Section->getUniqueID();
295 ELFUniquingMap.erase(
296 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
297 auto I = ELFUniquingMap.insert(std::make_pair(
298 ELFSectionKey{Name, GroupName, UniqueID},
Jim Grosbach008359a2015-05-18 18:43:23 +0000299 Section))
300 .first;
Rafael Espindola44d50572015-03-27 21:34:24 +0000301 StringRef CachedName = I->first.SectionName;
Jim Grosbach008359a2015-05-18 18:43:23 +0000302 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000303}
304
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000305MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
306 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000307 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000308 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000309 StringMap<bool>::iterator I;
310 bool Inserted;
311 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
312
313 return new (*this)
314 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000315 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000316}
317
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000318MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
319 unsigned Flags, unsigned EntrySize,
320 StringRef Group, unsigned UniqueID,
321 const char *BeginSymName) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000322 MCSymbolELF *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000323 if (!Group.empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000324 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000325
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000326 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
327 BeginSymName, nullptr);
328}
329
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000330MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
331 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000332 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000333 unsigned UniqueID,
334 const char *BeginSymName,
335 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000336 StringRef Group = "";
337 if (GroupSym)
338 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000339 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000340 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000341 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000342 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000343 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000344 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000345
Rafael Espindola44d50572015-03-27 21:34:24 +0000346 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000347
348 SectionKind Kind;
349 if (Flags & ELF::SHF_EXECINSTR)
350 Kind = SectionKind::getText();
351 else
352 Kind = SectionKind::getReadOnly();
353
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000354 MCSymbol *Begin = nullptr;
355 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000356 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000357
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000358 MCSectionELF *Result =
359 new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000360 GroupSym, UniqueID, Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000361 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000362 return Result;
363}
364
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000365MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000366 MCSectionELF *Result = new (*this)
367 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000368 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000369 return Result;
370}
371
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000372MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
373 unsigned Characteristics,
374 SectionKind Kind,
375 StringRef COMDATSymName, int Selection,
376 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000377 MCSymbol *COMDATSymbol = nullptr;
378 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000379 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000380 COMDATSymName = COMDATSymbol->getName();
381 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000382
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000383 // Do the lookup, if we have a hit, return it.
Rafael Espindola44d50572015-03-27 21:34:24 +0000384 COFFSectionKey T{Section, COMDATSymName, Selection};
David Majnemerc57d0382014-06-27 17:19:44 +0000385 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000386 auto Iter = IterBool.first;
387 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000388 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000389
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000390 MCSymbol *Begin = nullptr;
391 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000392 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000393
Rafael Espindola44d50572015-03-27 21:34:24 +0000394 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000395 MCSectionCOFF *Result = new (*this) MCSectionCOFF(
396 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000397
398 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000399 return Result;
400}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000401
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000402MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
403 unsigned Characteristics,
404 SectionKind Kind,
405 const char *BeginSymName) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000406 return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000407}
408
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000409MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Rafael Espindola44d50572015-03-27 21:34:24 +0000410 COFFSectionKey T{Section, "", 0};
David Majnemerc57d0382014-06-27 17:19:44 +0000411 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000412 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000413 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000414 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000415}
416
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000417MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
418 const MCSymbol *KeySym) {
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000419 // Return the normal section if we don't have to be associative.
420 if (!KeySym)
421 return Sec;
422
423 // Make an associative section with the same name and kind as the normal
424 // section.
425 unsigned Characteristics =
426 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
427 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
428 KeySym->getName(),
429 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
430}
431
Kevin Enderbye5930f12010-07-28 20:55:35 +0000432//===----------------------------------------------------------------------===//
433// Dwarf Management
434//===----------------------------------------------------------------------===//
435
Jim Grosbach6f482002015-05-18 18:43:14 +0000436/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000437/// directory tables. If the file number has already been allocated it is an
438/// error and zero is returned and the client reports the error, else the
439/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000440unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000441 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000442 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000443 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000444}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000445
Kevin Enderbya68d0042010-10-04 20:17:24 +0000446/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000447/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000448bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000449 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
450 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000451 return false;
452
David Blaikiea55ddad2014-03-13 18:55:04 +0000453 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000454}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000455
Rafael Espindolae0746792015-05-21 16:52:32 +0000456/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000457/// useless debug info for them.
458void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000459 SectionsForRanges.remove_if(
460 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000461}
462
Jim Grosbach6f482002015-05-18 18:43:14 +0000463void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const {
Jim Grosbachb18b4092012-01-26 23:20:11 +0000464 // If we have a source manager and a location, use it. Otherwise just
465 // use the generic report_fatal_error().
466 if (!SrcMgr || Loc == SMLoc())
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000467 report_fatal_error(Msg, false);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000468
469 // Use the source manager to print the message.
470 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
471
472 // If we reached here, we are failing ungracefully. Run the interrupt handlers
473 // to make sure any special cleanups get done, in particular that we remove
474 // files registered with RemoveFileOnSignal.
475 sys::RunInterruptHandlers();
476 exit(1);
477}