blob: e6fda3dbeb19fe3f83193db7ca1a33b86ef2435f [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
Chris Lattner86dfd732009-10-19 22:49:00 +000010#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringRef.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000014#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAsmInfo.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000016#include "llvm/MC/MCCodeView.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000019#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCLabel.h"
22#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSectionCOFF.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSectionMachO.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000027#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000028#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000029#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000030#include "llvm/MC/MCSymbolMachO.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/MC/SectionKind.h"
32#include "llvm/Support/Casting.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000033#include "llvm/Support/COFF.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000034#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000035#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000036#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000037#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000038#include "llvm/Support/raw_ostream.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000039#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Support/SourceMgr.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000041#include <cassert>
42#include <cstdlib>
43#include <tuple>
44#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000045
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000046using namespace llvm;
47
Igor Laevsky7b998852016-06-17 15:19:41 +000048static cl::opt<char*>
49AsSecureLogFileName("as-secure-log-file-name",
50 cl::desc("As secure log file name (initialized from "
51 "AS_SECURE_LOG_FILE env variable)"),
52 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
53
Bill Wendlingbc07a892013-06-18 07:20:20 +000054MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000055 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000056 bool DoAutoReset)
Eugene Zelenkod96089b2017-02-14 00:33:36 +000057 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Symbols(Allocator),
58 UsedNames(Allocator), CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
59 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000060 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000061
Alp Tokera55b95b2014-07-06 10:33:31 +000062 if (SrcMgr && SrcMgr->getNumBuffers())
63 MainFileName =
64 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000065}
66
67MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000068 if (AutoReset)
69 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000070
71 // NOTE: The symbols are all allocated out of a bump pointer allocator,
72 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000073}
74
75//===----------------------------------------------------------------------===//
76// Module Lifetime Management
77//===----------------------------------------------------------------------===//
78
Pedro Artigas7212ee42012-12-12 22:59:46 +000079void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000080 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000081 COFFAllocator.DestroyAll();
82 ELFAllocator.DestroyAll();
83 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000084
Akira Hatanakab11ef082015-11-14 06:35:56 +000085 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000086 UsedNames.clear();
87 Symbols.clear();
88 Allocator.Reset();
89 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000090 CompilationDir.clear();
91 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000092 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000093 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000094 MCGenDwarfLabelEntries.clear();
95 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000096 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +000097 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000098
Reid Kleckner2214ed82016-01-29 00:49:42 +000099 CVContext.reset();
100
David Blaikie9ec32122014-04-10 23:55:11 +0000101 MachOUniquingMap.clear();
102 ELFUniquingMap.clear();
103 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000104
Rafael Espindola9ab09232015-03-17 20:07:06 +0000105 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000106 AllowTemporaryLabels = true;
107 DwarfLocSeen = false;
108 GenDwarfForAssembly = false;
109 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000110
111 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000112}
113
Chris Lattner20731122010-04-08 20:30:37 +0000114//===----------------------------------------------------------------------===//
115// Symbol Manipulation
116//===----------------------------------------------------------------------===//
117
Jim Grosbach6f482002015-05-18 18:43:14 +0000118MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000119 SmallString<128> NameSV;
120 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000121
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000122 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000123
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000124 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000125 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000126 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000127
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000128 return Sym;
129}
130
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000131MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
132 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000133 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000134 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000135}
136
David Majnemera225a192015-03-31 22:35:44 +0000137MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000138 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000139 "$parent_frame_offset");
140}
141
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000142MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
143 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
144 FuncName);
145}
146
Rafael Espindolaa8695762015-06-02 00:25:12 +0000147MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
148 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000149 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000150 switch (MOFI->getObjectFileType()) {
151 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000152 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000153 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000154 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000155 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000156 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000157 }
158 }
Pete Cooper234b8752015-06-09 18:36:13 +0000159 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
160 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000161}
162
Daniel Jasper41de8022015-06-23 11:31:32 +0000163MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
164 bool CanBeUnnamed) {
165 if (CanBeUnnamed && !UseNamesOnTempLabels)
166 return createSymbolImpl(nullptr, true);
167
Eric Christopher8e948952016-09-29 02:03:44 +0000168 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000169 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000170 bool IsTemporary = CanBeUnnamed;
171 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000172 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000173
Rafael Espindola9ab09232015-03-17 20:07:06 +0000174 SmallString<128> NewName = Name;
175 bool AddSuffix = AlwaysAddSuffix;
176 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000177 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000178 if (AddSuffix) {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000179 NewName.resize(Name.size());
180 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000181 }
182 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000183 if (NameEntry.second || !NameEntry.first->second) {
184 // Ok, we found a name.
185 // Mark it as used for a non-section symbol.
186 NameEntry.first->second = true;
187 // Have the MCSymbol object itself refer to the copy of the string that is
188 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000189 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000190 }
191 assert(IsTemporary && "Cannot rename non-temporary symbols");
192 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000193 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000194 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000195}
196
Daniel Jasper41de8022015-06-23 11:31:32 +0000197MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
198 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000199 SmallString<128> NameSV;
200 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000201 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000202}
203
Jim Grosbach6f482002015-05-18 18:43:14 +0000204MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000205 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000206 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000207 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000208}
209
Daniel Jasper41de8022015-06-23 11:31:32 +0000210MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
211 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000212}
213
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000214unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000215 MCLabel *&Label = Instances[LocalLabelVal];
216 if (!Label)
217 Label = new (*this) MCLabel(0);
218 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000219}
220
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000221unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000222 MCLabel *&Label = Instances[LocalLabelVal];
223 if (!Label)
224 Label = new (*this) MCLabel(0);
225 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000226}
227
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000228MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
229 unsigned Instance) {
230 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
231 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000232 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000233 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000234}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000235
Jim Grosbach6f482002015-05-18 18:43:14 +0000236MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000237 unsigned Instance = NextInstance(LocalLabelVal);
238 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
239}
240
Jim Grosbach6f482002015-05-18 18:43:14 +0000241MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000242 bool Before) {
243 unsigned Instance = GetInstance(LocalLabelVal);
244 if (!Before)
245 ++Instance;
246 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000247}
248
Jim Grosbach6f482002015-05-18 18:43:14 +0000249MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000250 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000251 StringRef NameRef = Name.toStringRef(NameSV);
252 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000253}
254
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000255void MCContext::setSymbolValue(MCStreamer &Streamer,
256 StringRef Sym,
257 uint64_t Val) {
258 auto Symbol = getOrCreateSymbol(Sym);
259 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000260}
261
Chris Lattner20731122010-04-08 20:30:37 +0000262//===----------------------------------------------------------------------===//
263// Section Management
264//===----------------------------------------------------------------------===//
265
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000266MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
267 unsigned TypeAndAttributes,
268 unsigned Reserved2, SectionKind Kind,
269 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000270 // We unique sections by their segment/section pair. The returned section
271 // may not have the same flags as the requested section, if so this should be
272 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000273
Chris Lattner20731122010-04-08 20:30:37 +0000274 // Form the name to look up.
275 SmallString<64> Name;
276 Name += Segment;
277 Name.push_back(',');
278 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000279
Chris Lattner20731122010-04-08 20:30:37 +0000280 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000281 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000282 if (Entry)
283 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000284
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000285 MCSymbol *Begin = nullptr;
286 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000287 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000288
Chris Lattner20731122010-04-08 20:30:37 +0000289 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000290 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
291 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000292}
Chris Lattner5418dd52010-04-08 21:26:26 +0000293
Richard Smithb910e562016-05-25 00:14:12 +0000294void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
295 StringRef GroupName;
296 if (const MCSymbol *Group = Section->getGroup())
297 GroupName = Group->getName();
298
299 unsigned UniqueID = Section->getUniqueID();
300 ELFUniquingMap.erase(
301 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
302 auto I = ELFUniquingMap.insert(std::make_pair(
303 ELFSectionKey{Name, GroupName, UniqueID},
304 Section))
305 .first;
306 StringRef CachedName = I->first.SectionName;
307 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
308}
309
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000310MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
311 unsigned Flags, SectionKind K,
312 unsigned EntrySize,
313 const MCSymbolELF *Group,
314 unsigned UniqueID,
315 const MCSectionELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000316 MCSymbolELF *R;
317 MCSymbol *&Sym = Symbols[Section];
318 if (Sym && Sym->isUndefined()) {
319 R = cast<MCSymbolELF>(Sym);
320 } else {
321 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
322 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
323 if (!Sym)
324 Sym = R;
325 }
326 R->setBinding(ELF::STB_LOCAL);
327 R->setType(ELF::STT_SECTION);
328 R->setRedefinable(true);
329
330 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
331 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
332
333 auto *F = new MCDataFragment();
334 Ret->getFragmentList().insert(Ret->begin(), F);
335 F->setParent(Ret);
336 R->setFragment(F);
337
338 return Ret;
339}
340
Eric Christopher36e601c2016-07-01 06:07:38 +0000341MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000342 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000343 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000344 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000345 StringMap<bool>::iterator I;
346 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000347 std::tie(I, Inserted) =
348 ELFRelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000349
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000350 return createELFSectionImpl(I->getKey(), Type, Flags,
351 SectionKind::getReadOnly(), EntrySize, Group,
352 true, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000353}
354
Eric Christopher36e601c2016-07-01 06:07:38 +0000355MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
356 const Twine &Suffix, unsigned Type,
357 unsigned Flags,
358 unsigned EntrySize) {
359 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
360}
361
362MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000363 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000364 const Twine &Group, unsigned UniqueID,
365 const MCSectionELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000366 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000367 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000368 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000369
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000370 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000371 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000372}
373
Eric Christopher36e601c2016-07-01 06:07:38 +0000374MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000375 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000376 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000377 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000378 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000379 StringRef Group = "";
380 if (GroupSym)
381 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000382 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000383 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000384 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000385 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000386 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000387 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000388
Rafael Espindola44d50572015-03-27 21:34:24 +0000389 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000390
391 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000392 if (Flags & ELF::SHF_ARM_PURECODE)
393 Kind = SectionKind::getExecuteOnly();
394 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000395 Kind = SectionKind::getText();
396 else
397 Kind = SectionKind::getReadOnly();
398
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000399 MCSectionELF *Result = createELFSectionImpl(
400 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000401 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000402 return Result;
403}
404
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000405MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000406 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
407 SectionKind::getReadOnly(), 4, Group, ~0,
408 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000409}
410
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000411MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
412 unsigned Characteristics,
413 SectionKind Kind,
414 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000415 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000416 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000417 MCSymbol *COMDATSymbol = nullptr;
418 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000419 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000420 COMDATSymName = COMDATSymbol->getName();
421 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000422
Reid Kleckner97837b72016-05-02 23:22:18 +0000423
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000424 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000425 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000426 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000427 auto Iter = IterBool.first;
428 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000429 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000430
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000431 MCSymbol *Begin = nullptr;
432 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000433 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000434
Rafael Espindola44d50572015-03-27 21:34:24 +0000435 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000436 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000437 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000438
439 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000440 return Result;
441}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000442
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000443MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
444 unsigned Characteristics,
445 SectionKind Kind,
446 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000447 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
448 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000449}
450
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000451MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000452 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000453 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000454 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000455 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000456 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000457}
458
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000459MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000460 const MCSymbol *KeySym,
461 unsigned UniqueID) {
462 // Return the normal section if we don't have to be associative or unique.
463 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000464 return Sec;
465
Reid Kleckner97837b72016-05-02 23:22:18 +0000466 // If we have a key symbol, make an associative section with the same name and
467 // kind as the normal section.
468 unsigned Characteristics = Sec->getCharacteristics();
469 if (KeySym) {
470 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
471 return getCOFFSection(Sec->getSectionName(), Characteristics,
472 Sec->getKind(), KeySym->getName(),
473 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
474 }
475
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000476 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000477 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000478}
479
Akira Hatanakab11ef082015-11-14 06:35:56 +0000480MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
481 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
482}
483
Kevin Enderbye5930f12010-07-28 20:55:35 +0000484//===----------------------------------------------------------------------===//
485// Dwarf Management
486//===----------------------------------------------------------------------===//
487
Jim Grosbach6f482002015-05-18 18:43:14 +0000488/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000489/// directory tables. If the file number has already been allocated it is an
490/// error and zero is returned and the client reports the error, else the
491/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000492unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000493 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000494 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000495 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000496}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000497
Kevin Enderbya68d0042010-10-04 20:17:24 +0000498/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000499/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000500bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000501 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
502 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000503 return false;
504
David Blaikiea55ddad2014-03-13 18:55:04 +0000505 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000506}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000507
Rafael Espindolae0746792015-05-21 16:52:32 +0000508/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000509/// useless debug info for them.
510void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000511 SectionsForRanges.remove_if(
512 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000513}
514
Reid Kleckner2214ed82016-01-29 00:49:42 +0000515CodeViewContext &MCContext::getCVContext() {
516 if (!CVContext.get())
517 CVContext.reset(new CodeViewContext);
518 return *CVContext.get();
519}
520
Oliver Stannard07b43d32015-11-17 09:58:07 +0000521//===----------------------------------------------------------------------===//
522// Error Reporting
523//===----------------------------------------------------------------------===//
524
525void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
526 HadError = true;
527
Sanne Wouda29338752017-02-08 14:48:05 +0000528 // If we have a source manager use it. Otherwise, try using the inline source
529 // manager.
530 // If that fails, use the generic report_fatal_error().
531 if (SrcMgr)
532 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
533 else if (InlineSrcMgr)
534 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
535 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000536 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000537}
538
539void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
540 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000541
542 // If we reached here, we are failing ungracefully. Run the interrupt handlers
543 // to make sure any special cleanups get done, in particular that we remove
544 // files registered with RemoveFileOnSignal.
545 sys::RunInterruptHandlers();
546 exit(1);
547}