blob: 0a13c9e6edb8cd7bef0962732d9940861d6b8c2e [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"
Chandler Carruthed0881b2012-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"
Pete Cooperef21bd42015-03-04 01:24:11 +000021#include "llvm/MC/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MCSymbol.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000023#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000024#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000025#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000026#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000027#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000029#include <map>
30
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000031using namespace llvm;
32
Bill Wendlingbc07a892013-06-18 07:20:20 +000033MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000034 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000035 bool DoAutoReset)
36 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Rafael Espindola9ab09232015-03-17 20:07:06 +000037 Symbols(Allocator), UsedNames(Allocator),
David Blaikie7400a972014-03-27 20:45:58 +000038 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
David Blaikie6f687582014-05-01 19:55:34 +000039 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
David Blaikie7400a972014-03-27 20:45:58 +000040 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
41 AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000042
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000043 std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
Andrew Trick32591d32013-12-10 04:39:09 +000044 if (EC)
45 CompilationDir.clear();
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000046
Kevin Enderbye233dda2010-06-28 21:45:58 +000047 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
Craig Topperbb694de2014-04-13 04:57:38 +000048 SecureLog = nullptr;
Kevin Enderbye233dda2010-06-28 21:45:58 +000049 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000050
Alp Tokera55b95b2014-07-06 10:33:31 +000051 if (SrcMgr && SrcMgr->getNumBuffers())
52 MainFileName =
53 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000054}
55
56MCContext::~MCContext() {
57
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() {
Pedro Artigase84b13f2012-12-06 22:12:44 +000073 UsedNames.clear();
74 Symbols.clear();
75 Allocator.Reset();
76 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000077 CompilationDir.clear();
78 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000079 MCDwarfLineTablesCUMap.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000080 SectionStartEndSyms.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000081 MCGenDwarfLabelEntries.clear();
82 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000083 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000084 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000085
David Blaikie9ec32122014-04-10 23:55:11 +000086 MachOUniquingMap.clear();
87 ELFUniquingMap.clear();
88 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000089
Rafael Espindola9ab09232015-03-17 20:07:06 +000090 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +000091 AllowTemporaryLabels = true;
92 DwarfLocSeen = false;
93 GenDwarfForAssembly = false;
94 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000095}
96
Chris Lattner20731122010-04-08 20:30:37 +000097//===----------------------------------------------------------------------===//
98// Symbol Manipulation
99//===----------------------------------------------------------------------===//
100
Jim Grosbach6f482002015-05-18 18:43:14 +0000101MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000102 SmallString<128> NameSV;
103 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000104
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000105 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000106
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000107 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000108 if (!Sym)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000109 Sym = CreateSymbol(NameRef, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000110
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000111 return Sym;
112}
113
Rafael Espindolab6613022014-10-17 01:48:58 +0000114MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
115 MCSymbol *&Sym = SectionSymbols[&Section];
116 if (Sym)
117 return Sym;
118
119 StringRef Name = Section.getSectionName();
120
David Blaikie5106ce72014-11-19 05:49:42 +0000121 MCSymbol *&OldSym = Symbols[Name];
Rafael Espindolab6613022014-10-17 01:48:58 +0000122 if (OldSym && OldSym->isUndefined()) {
123 Sym = OldSym;
124 return OldSym;
125 }
126
David Blaikie5106ce72014-11-19 05:49:42 +0000127 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Matt Arsenault9897e032015-04-23 23:34:51 +0000128 Sym = new (*this) MCSymbol(NameIter->getKey(), /*isTemporary*/ false);
Rafael Espindolab6613022014-10-17 01:48:58 +0000129
David Blaikie5106ce72014-11-19 05:49:42 +0000130 if (!OldSym)
131 OldSym = Sym;
Rafael Espindolab6613022014-10-17 01:48:58 +0000132
133 return Sym;
134}
135
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000136MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
137 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000138 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000139 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000140}
141
David Majnemera225a192015-03-31 22:35:44 +0000142MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000143 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000144 "$parent_frame_offset");
145}
146
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000147MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
148 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
149 FuncName);
150}
151
Rafael Espindola9ab09232015-03-17 20:07:06 +0000152MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000153 // Determine whether this is an assembler temporary or normal label, if used.
Rafael Espindola9ab09232015-03-17 20:07:06 +0000154 bool IsTemporary = false;
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000155 if (AllowTemporaryLabels)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000156 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000157
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000158 if (IsTemporary && AlwaysAddSuffix && !UseNamesOnTempLabels)
159 return new (*this) MCSymbol("", true);
160
Rafael Espindola9ab09232015-03-17 20:07:06 +0000161 SmallString<128> NewName = Name;
162 bool AddSuffix = AlwaysAddSuffix;
163 unsigned &NextUniqueID = NextID[Name];
164 for (;;) {
165 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000166 NewName.resize(Name.size());
167 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000168 }
169 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
170 if (NameEntry.second) {
171 // Ok, we found a name. Have the MCSymbol object itself refer to the copy
172 // of the string that is embedded in the UsedNames entry.
173 MCSymbol *Result =
Matt Arsenault9897e032015-04-23 23:34:51 +0000174 new (*this) MCSymbol(NameEntry.first->getKey(), IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000175 return Result;
176 }
177 assert(IsTemporary && "Cannot rename non-temporary symbols");
178 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000179 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000180 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000181}
182
Rafael Espindola9ab09232015-03-17 20:07:06 +0000183MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000184 SmallString<128> NameSV;
185 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000186 return CreateSymbol(NameSV, AlwaysAddSuffix);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000187}
188
Jim Grosbach6f482002015-05-18 18:43:14 +0000189MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000190 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000191 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
192 return CreateSymbol(NameSV, true);
Tim Northoverc3988b42014-03-29 07:05:06 +0000193}
194
Jim Grosbach6f482002015-05-18 18:43:14 +0000195MCSymbol *MCContext::createTempSymbol() {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000196 return createTempSymbol("tmp", true);
Chris Lattner073d8172010-03-14 08:23:30 +0000197}
198
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000199unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000200 MCLabel *&Label = Instances[LocalLabelVal];
201 if (!Label)
202 Label = new (*this) MCLabel(0);
203 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000204}
205
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000206unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000207 MCLabel *&Label = Instances[LocalLabelVal];
208 if (!Label)
209 Label = new (*this) MCLabel(0);
210 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000211}
212
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000213MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
214 unsigned Instance) {
215 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
216 if (!Sym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000217 Sym = createTempSymbol();
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000218 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000219}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000220
Jim Grosbach6f482002015-05-18 18:43:14 +0000221MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000222 unsigned Instance = NextInstance(LocalLabelVal);
223 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
224}
225
Jim Grosbach6f482002015-05-18 18:43:14 +0000226MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000227 bool Before) {
228 unsigned Instance = GetInstance(LocalLabelVal);
229 if (!Before)
230 ++Instance;
231 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000232}
233
Jim Grosbach6f482002015-05-18 18:43:14 +0000234MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000235 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000236 StringRef NameRef = Name.toStringRef(NameSV);
237 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000238}
239
Chris Lattner20731122010-04-08 20:30:37 +0000240//===----------------------------------------------------------------------===//
241// Section Management
242//===----------------------------------------------------------------------===//
243
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000244const MCSectionMachO *
245MCContext::getMachOSection(StringRef Segment, StringRef Section,
246 unsigned TypeAndAttributes, unsigned Reserved2,
247 SectionKind Kind, const char *BeginSymName) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000248
Chris Lattner20731122010-04-08 20:30:37 +0000249 // We unique sections by their segment/section pair. The returned section
250 // may not have the same flags as the requested section, if so this should be
251 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000252
Chris Lattner20731122010-04-08 20:30:37 +0000253 // Form the name to look up.
254 SmallString<64> Name;
255 Name += Segment;
256 Name.push_back(',');
257 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000258
Chris Lattner20731122010-04-08 20:30:37 +0000259 // Do the lookup, if we have a hit, return it.
Yaron Kerend7c546c2015-03-17 18:55:30 +0000260 const MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000261 if (Entry)
262 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000263
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000264 MCSymbol *Begin = nullptr;
265 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000266 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000267
Chris Lattner20731122010-04-08 20:30:37 +0000268 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000269 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000270 Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000271}
Chris Lattner5418dd52010-04-08 21:26:26 +0000272
David Blaikie8019bf82014-04-10 21:53:53 +0000273void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) {
David Blaikie8019bf82014-04-10 21:53:53 +0000274 StringRef GroupName;
275 if (const MCSymbol *Group = Section->getGroup())
276 GroupName = Group->getName();
277
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000278 unsigned UniqueID = Section->getUniqueID();
279 ELFUniquingMap.erase(
280 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
281 auto I = ELFUniquingMap.insert(std::make_pair(
282 ELFSectionKey{Name, GroupName, UniqueID},
Jim Grosbach008359a2015-05-18 18:43:23 +0000283 Section))
284 .first;
Rafael Espindola44d50572015-03-27 21:34:24 +0000285 StringRef CachedName = I->first.SectionName;
Jim Grosbach008359a2015-05-18 18:43:23 +0000286 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
David Blaikie8019bf82014-04-10 21:53:53 +0000287}
288
Rafael Espindolac9d06922015-03-30 13:39:16 +0000289const MCSectionELF *
290MCContext::createELFRelSection(StringRef Name, unsigned Type, unsigned Flags,
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000291 unsigned EntrySize, const MCSymbol *Group,
292 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000293 StringMap<bool>::iterator I;
294 bool Inserted;
295 std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
296
297 return new (*this)
298 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000299 EntrySize, Group, true, nullptr, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000300}
301
Rafael Espindolaba31e272015-01-29 17:33:21 +0000302const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
303 unsigned Flags, unsigned EntrySize,
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000304 StringRef Group, unsigned UniqueID,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000305 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000306 MCSymbol *GroupSym = nullptr;
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000307 if (!Group.empty())
Jim Grosbach6f482002015-05-18 18:43:14 +0000308 GroupSym = getOrCreateSymbol(Group);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000309
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000310 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
311 BeginSymName, nullptr);
312}
313
314const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
315 unsigned Flags, unsigned EntrySize,
316 const MCSymbol *GroupSym,
317 unsigned UniqueID,
318 const char *BeginSymName,
319 const MCSectionELF *Associated) {
320 StringRef Group = "";
321 if (GroupSym)
322 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000323 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000324 auto IterBool = ELFUniquingMap.insert(
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000325 std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000326 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000327 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000328 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000329
Rafael Espindola44d50572015-03-27 21:34:24 +0000330 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000331
332 SectionKind Kind;
333 if (Flags & ELF::SHF_EXECINSTR)
334 Kind = SectionKind::getText();
335 else
336 Kind = SectionKind::getReadOnly();
337
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000338 MCSymbol *Begin = nullptr;
339 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000340 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000341
Rafael Espindolab73b70e2015-04-06 03:09:30 +0000342 MCSectionELF *Result =
343 new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000344 GroupSym, UniqueID, Begin, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000345 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000346 return Result;
347}
348
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000349const MCSectionELF *MCContext::createELFGroupSection(const MCSymbol *Group) {
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000350 MCSectionELF *Result = new (*this)
351 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
Rafael Espindola55a3afb2015-04-28 21:07:28 +0000352 Group, ~0, nullptr, nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000353 return Result;
354}
355
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000356const MCSectionCOFF *
357MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
358 SectionKind Kind, StringRef COMDATSymName,
359 int Selection, const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000360 MCSymbol *COMDATSymbol = nullptr;
361 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000362 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000363 COMDATSymName = COMDATSymbol->getName();
364 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000365
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000366 // Do the lookup, if we have a hit, return it.
Rafael Espindola44d50572015-03-27 21:34:24 +0000367 COFFSectionKey T{Section, COMDATSymName, Selection};
David Majnemerc57d0382014-06-27 17:19:44 +0000368 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000369 auto Iter = IterBool.first;
370 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000371 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000372
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000373 MCSymbol *Begin = nullptr;
374 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000375 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000376
Rafael Espindola44d50572015-03-27 21:34:24 +0000377 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000378 MCSectionCOFF *Result = new (*this) MCSectionCOFF(
379 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000380
381 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000382 return Result;
383}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000384
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000385const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
386 unsigned Characteristics,
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000387 SectionKind Kind,
388 const char *BeginSymName) {
389 return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000390}
391
Nico Riecka37acf72013-07-06 12:13:10 +0000392const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Rafael Espindola44d50572015-03-27 21:34:24 +0000393 COFFSectionKey T{Section, "", 0};
David Majnemerc57d0382014-06-27 17:19:44 +0000394 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000395 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000396 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000397 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000398}
399
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000400const MCSectionCOFF *
401MCContext::getAssociativeCOFFSection(const MCSectionCOFF *Sec,
402 const MCSymbol *KeySym) {
403 // Return the normal section if we don't have to be associative.
404 if (!KeySym)
405 return Sec;
406
407 // Make an associative section with the same name and kind as the normal
408 // section.
409 unsigned Characteristics =
410 Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
411 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
412 KeySym->getName(),
413 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
414}
415
Kevin Enderbye5930f12010-07-28 20:55:35 +0000416//===----------------------------------------------------------------------===//
417// Dwarf Management
418//===----------------------------------------------------------------------===//
419
Jim Grosbach6f482002015-05-18 18:43:14 +0000420/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000421/// directory tables. If the file number has already been allocated it is an
422/// error and zero is returned and the client reports the error, else the
423/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000424unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000425 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000426 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000427 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000428}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000429
Kevin Enderbya68d0042010-10-04 20:17:24 +0000430/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000431/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000432bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000433 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
434 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000435 return false;
436
David Blaikiea55ddad2014-03-13 18:55:04 +0000437 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000438}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000439
Oliver Stannard8b273082014-06-19 15:52:37 +0000440/// finalizeDwarfSections - Emit end symbols for each non-empty code section.
441/// Also remove empty sections from SectionStartEndSyms, to avoid generating
442/// useless debug info for them.
443void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
444 MCContext &context = MCOS.getContext();
445
446 auto sec = SectionStartEndSyms.begin();
447 while (sec != SectionStartEndSyms.end()) {
448 assert(sec->second.first && "Start symbol must be set by now");
449 MCOS.SwitchSection(sec->first);
450 if (MCOS.mayHaveInstructions()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000451 MCSymbol *SectionEndSym = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000452 MCOS.EmitLabel(SectionEndSym);
453 sec->second.second = SectionEndSym;
454 ++sec;
455 } else {
Jim Grosbach008359a2015-05-18 18:43:23 +0000456 MapVector<const MCSection *, std::pair<MCSymbol *, MCSymbol *>>::iterator
457 to_erase = sec;
Oliver Stannard8b273082014-06-19 15:52:37 +0000458 sec = SectionStartEndSyms.erase(to_erase);
459 }
460 }
461}
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}