blob: cc7533f87b7c59036036c019cc56ca3e6b6d66e1 [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"
Dan Gohman18eafb62017-02-22 01:23:18 +000026#include "llvm/MC/MCSectionWasm.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000027#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000028#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000029#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000030#include "llvm/MC/MCSymbolELF.h"
Pete Coopereb012fa2015-06-08 17:17:23 +000031#include "llvm/MC/MCSymbolMachO.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000032#include "llvm/MC/MCSymbolWasm.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000033#include "llvm/MC/SectionKind.h"
34#include "llvm/Support/Casting.h"
Reid Kleckner1f13d472015-09-03 16:41:50 +000035#include "llvm/Support/COFF.h"
Igor Laevsky7b998852016-06-17 15:19:41 +000036#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000037#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000038#include "llvm/Support/ErrorHandling.h"
Eric Christopher906da232012-12-18 00:31:01 +000039#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include "llvm/Support/raw_ostream.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000041#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/SourceMgr.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000043#include <cassert>
44#include <cstdlib>
45#include <tuple>
46#include <utility>
David Blaikie15b25df2013-10-22 23:41:52 +000047
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000048using namespace llvm;
49
Igor Laevsky7b998852016-06-17 15:19:41 +000050static cl::opt<char*>
51AsSecureLogFileName("as-secure-log-file-name",
52 cl::desc("As secure log file name (initialized from "
53 "AS_SECURE_LOG_FILE env variable)"),
54 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
55
Bill Wendlingbc07a892013-06-18 07:20:20 +000056MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000057 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie7400a972014-03-27 20:45:58 +000058 bool DoAutoReset)
Evgeniy Stepanov0338ce82017-02-24 21:44:52 +000059 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
60 Symbols(Allocator), UsedNames(Allocator),
61 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Eugene Zelenkod96089b2017-02-14 00:33:36 +000062 AutoReset(DoAutoReset) {
Igor Laevsky7b998852016-06-17 15:19:41 +000063 SecureLogFile = AsSecureLogFileName;
Eric Christopher906da232012-12-18 00:31:01 +000064
Alp Tokera55b95b2014-07-06 10:33:31 +000065 if (SrcMgr && SrcMgr->getNumBuffers())
66 MainFileName =
67 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigase84b13f2012-12-06 22:12:44 +000068}
69
70MCContext::~MCContext() {
Pedro Artigas7212ee42012-12-12 22:59:46 +000071 if (AutoReset)
72 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000073
74 // NOTE: The symbols are all allocated out of a bump pointer allocator,
75 // we don't need to free them here.
Pedro Artigase84b13f2012-12-06 22:12:44 +000076}
77
78//===----------------------------------------------------------------------===//
79// Module Lifetime Management
80//===----------------------------------------------------------------------===//
81
Pedro Artigas7212ee42012-12-12 22:59:46 +000082void MCContext::reset() {
Rafael Espindolaed34d582015-05-26 01:52:19 +000083 // Call the destructors so the fragments are freed
Rafael Espindola4264e2d2015-10-07 19:08:19 +000084 COFFAllocator.DestroyAll();
85 ELFAllocator.DestroyAll();
86 MachOAllocator.DestroyAll();
Rafael Espindolaed34d582015-05-26 01:52:19 +000087
Akira Hatanakab11ef082015-11-14 06:35:56 +000088 MCSubtargetAllocator.DestroyAll();
Pedro Artigase84b13f2012-12-06 22:12:44 +000089 UsedNames.clear();
90 Symbols.clear();
91 Allocator.Reset();
92 Instances.clear();
Yaron Keren559b47d2014-09-17 09:25:36 +000093 CompilationDir.clear();
94 MainFileName.clear();
David Blaikied9012ba2014-03-13 21:59:51 +000095 MCDwarfLineTablesCUMap.clear();
Rafael Espindolae0746792015-05-21 16:52:32 +000096 SectionsForRanges.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000097 MCGenDwarfLabelEntries.clear();
98 DwarfDebugFlags = StringRef();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000099 DwarfCompileUnitID = 0;
Jim Grosbach008359a2015-05-18 18:43:23 +0000100 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000101
Reid Kleckner2214ed82016-01-29 00:49:42 +0000102 CVContext.reset();
103
David Blaikie9ec32122014-04-10 23:55:11 +0000104 MachOUniquingMap.clear();
105 ELFUniquingMap.clear();
106 COFFUniquingMap.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000107
Rafael Espindola9ab09232015-03-17 20:07:06 +0000108 NextID.clear();
Pedro Artigas7212ee42012-12-12 22:59:46 +0000109 AllowTemporaryLabels = true;
110 DwarfLocSeen = false;
111 GenDwarfForAssembly = false;
112 GenDwarfFileNumber = 0;
Oliver Stannard07b43d32015-11-17 09:58:07 +0000113
114 HadError = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000115}
116
Chris Lattner20731122010-04-08 20:30:37 +0000117//===----------------------------------------------------------------------===//
118// Symbol Manipulation
119//===----------------------------------------------------------------------===//
120
Jim Grosbach6f482002015-05-18 18:43:14 +0000121MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000122 SmallString<128> NameSV;
123 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000124
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000125 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000126
Yaron Keren1ee89fc2015-03-17 09:51:17 +0000127 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie5106ce72014-11-19 05:49:42 +0000128 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000129 Sym = createSymbol(NameRef, false, false);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000130
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000131 return Sym;
132}
133
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000134MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
135 unsigned Idx) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000136 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000137 "$frame_escape_" + Twine(Idx));
Reid Klecknere9b89312015-01-13 00:48:10 +0000138}
139
David Majnemera225a192015-03-31 22:35:44 +0000140MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000141 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemera225a192015-03-31 22:35:44 +0000142 "$parent_frame_offset");
143}
144
Reid Kleckner2632f0d2015-05-20 23:08:04 +0000145MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
146 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
147 FuncName);
148}
149
Rafael Espindolaa8695762015-06-02 00:25:12 +0000150MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
151 bool IsTemporary) {
Pete Cooperad9f9c32015-06-08 17:17:12 +0000152 if (MOFI) {
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000153 switch (MOFI->getObjectFileType()) {
154 case MCObjectFileInfo::IsCOFF:
Pete Cooper234b8752015-06-09 18:36:13 +0000155 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000156 case MCObjectFileInfo::IsELF:
Pete Cooper234b8752015-06-09 18:36:13 +0000157 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindoladbaf0492015-08-14 15:48:41 +0000158 case MCObjectFileInfo::IsMachO:
Pete Cooper234b8752015-06-09 18:36:13 +0000159 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohman18eafb62017-02-22 01:23:18 +0000160 case MCObjectFileInfo::IsWasm:
161 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperad9f9c32015-06-08 17:17:12 +0000162 }
163 }
Pete Cooper234b8752015-06-09 18:36:13 +0000164 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
165 IsTemporary);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000166}
167
Daniel Jasper41de8022015-06-23 11:31:32 +0000168MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
169 bool CanBeUnnamed) {
170 if (CanBeUnnamed && !UseNamesOnTempLabels)
171 return createSymbolImpl(nullptr, true);
172
Eric Christopher8e948952016-09-29 02:03:44 +0000173 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola3e9e72a2015-06-02 22:52:13 +0000174 // label, if used.
Daniel Jasper41de8022015-06-23 11:31:32 +0000175 bool IsTemporary = CanBeUnnamed;
176 if (AllowTemporaryLabels && !IsTemporary)
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];
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000182 while (true) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000183 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));
Evgeniy Stepanova023f792016-03-28 20:36:28 +0000188 if (NameEntry.second || !NameEntry.first->second) {
189 // Ok, we found a name.
190 // Mark it as used for a non-section symbol.
191 NameEntry.first->second = true;
192 // Have the MCSymbol object itself refer to the copy of the string that is
193 // embedded in the UsedNames entry.
Rafael Espindolaa8695762015-06-02 00:25:12 +0000194 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000195 }
196 assert(IsTemporary && "Cannot rename non-temporary symbols");
197 AddSuffix = true;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000198 }
Rafael Espindola9ab09232015-03-17 20:07:06 +0000199 llvm_unreachable("Infinite loop");
Chris Lattner3f5738d2009-06-24 04:31:49 +0000200}
201
Daniel Jasper41de8022015-06-23 11:31:32 +0000202MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
203 bool CanBeUnnamed) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000204 SmallString<128> NameSV;
205 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasper41de8022015-06-23 11:31:32 +0000206 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000207}
208
Jim Grosbach6f482002015-05-18 18:43:14 +0000209MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Tokere69170a2014-06-26 22:52:05 +0000210 SmallString<128> NameSV;
Rafael Espindola9ab09232015-03-17 20:07:06 +0000211 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasper41de8022015-06-23 11:31:32 +0000212 return createSymbol(NameSV, true, false);
Tim Northoverc3988b42014-03-29 07:05:06 +0000213}
214
Daniel Jasper41de8022015-06-23 11:31:32 +0000215MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
216 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner073d8172010-03-14 08:23:30 +0000217}
218
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000219unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000220 MCLabel *&Label = Instances[LocalLabelVal];
221 if (!Label)
222 Label = new (*this) MCLabel(0);
223 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000224}
225
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000226unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000227 MCLabel *&Label = Instances[LocalLabelVal];
228 if (!Label)
229 Label = new (*this) MCLabel(0);
230 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000231}
232
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000233MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
234 unsigned Instance) {
235 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
236 if (!Sym)
Daniel Jasper41de8022015-06-23 11:31:32 +0000237 Sym = createTempSymbol(false);
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000238 return Sym;
Kevin Enderby0510b482010-05-17 23:08:19 +0000239}
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000240
Jim Grosbach6f482002015-05-18 18:43:14 +0000241MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000242 unsigned Instance = NextInstance(LocalLabelVal);
243 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
244}
245
Jim Grosbach6f482002015-05-18 18:43:14 +0000246MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000247 bool Before) {
248 unsigned Instance = GetInstance(LocalLabelVal);
249 if (!Before)
250 ++Instance;
251 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderby0510b482010-05-17 23:08:19 +0000252}
253
Jim Grosbach6f482002015-05-18 18:43:14 +0000254MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divacky0be33592012-09-18 17:10:37 +0000255 SmallString<128> NameSV;
Yaron Kerend7c546c2015-03-17 18:55:30 +0000256 StringRef NameRef = Name.toStringRef(NameSV);
257 return Symbols.lookup(NameRef);
Roman Divacky0be33592012-09-18 17:10:37 +0000258}
259
Mandeep Singh Grang9a561aa2016-12-06 02:49:17 +0000260void MCContext::setSymbolValue(MCStreamer &Streamer,
261 StringRef Sym,
262 uint64_t Val) {
263 auto Symbol = getOrCreateSymbol(Sym);
264 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang32360072016-12-01 18:42:04 +0000265}
266
Chris Lattner20731122010-04-08 20:30:37 +0000267//===----------------------------------------------------------------------===//
268// Section Management
269//===----------------------------------------------------------------------===//
270
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000271MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
272 unsigned TypeAndAttributes,
273 unsigned Reserved2, SectionKind Kind,
274 const char *BeginSymName) {
Chris Lattner20731122010-04-08 20:30:37 +0000275 // We unique sections by their segment/section pair. The returned section
276 // may not have the same flags as the requested section, if so this should be
277 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000278
Chris Lattner20731122010-04-08 20:30:37 +0000279 // Form the name to look up.
280 SmallString<64> Name;
281 Name += Segment;
282 Name.push_back(',');
283 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000284
Chris Lattner20731122010-04-08 20:30:37 +0000285 // Do the lookup, if we have a hit, return it.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000286 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola6ed58a22015-03-10 21:16:18 +0000287 if (Entry)
288 return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000289
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000290 MCSymbol *Begin = nullptr;
291 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000292 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000293
Chris Lattner20731122010-04-08 20:30:37 +0000294 // Otherwise, return a new section.
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000295 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
296 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattner20731122010-04-08 20:30:37 +0000297}
Chris Lattner5418dd52010-04-08 21:26:26 +0000298
Richard Smithb910e562016-05-25 00:14:12 +0000299void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
300 StringRef GroupName;
301 if (const MCSymbol *Group = Section->getGroup())
302 GroupName = Group->getName();
303
304 unsigned UniqueID = Section->getUniqueID();
305 ELFUniquingMap.erase(
306 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
307 auto I = ELFUniquingMap.insert(std::make_pair(
308 ELFSectionKey{Name, GroupName, UniqueID},
309 Section))
310 .first;
311 StringRef CachedName = I->first.SectionName;
312 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
313}
314
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000315MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
316 unsigned Flags, SectionKind K,
317 unsigned EntrySize,
318 const MCSymbolELF *Group,
319 unsigned UniqueID,
320 const MCSectionELF *Associated) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000321 MCSymbolELF *R;
322 MCSymbol *&Sym = Symbols[Section];
323 if (Sym && Sym->isUndefined()) {
324 R = cast<MCSymbolELF>(Sym);
325 } else {
326 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
327 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
328 if (!Sym)
329 Sym = R;
330 }
331 R->setBinding(ELF::STB_LOCAL);
332 R->setType(ELF::STT_SECTION);
333 R->setRedefinable(true);
334
335 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
336 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
337
338 auto *F = new MCDataFragment();
339 Ret->getFragmentList().insert(Ret->begin(), F);
340 F->setParent(Ret);
341 R->setFragment(F);
342
343 return Ret;
344}
345
Eric Christopher36e601c2016-07-01 06:07:38 +0000346MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000347 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000348 const MCSymbolELF *Group,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000349 const MCSectionELF *Associated) {
Rafael Espindolac9d06922015-03-30 13:39:16 +0000350 StringMap<bool>::iterator I;
351 bool Inserted;
Eric Christopher36e601c2016-07-01 06:07:38 +0000352 std::tie(I, Inserted) =
Dan Gohman18eafb62017-02-22 01:23:18 +0000353 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindolac9d06922015-03-30 13:39:16 +0000354
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000355 return createELFSectionImpl(I->getKey(), Type, Flags,
356 SectionKind::getReadOnly(), EntrySize, Group,
357 true, Associated);
Rafael Espindolac9d06922015-03-30 13:39:16 +0000358}
359
Eric Christopher36e601c2016-07-01 06:07:38 +0000360MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
361 const Twine &Suffix, unsigned Type,
362 unsigned Flags,
363 unsigned EntrySize) {
364 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
365}
366
367MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000368 unsigned Flags, unsigned EntrySize,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000369 const Twine &Group, unsigned UniqueID,
370 const MCSectionELF *Associated) {
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000371 MCSymbolELF *GroupSym = nullptr;
Eric Christopher36e601c2016-07-01 06:07:38 +0000372 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000373 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000374
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000375 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000376 Associated);
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000377}
378
Eric Christopher36e601c2016-07-01 06:07:38 +0000379MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000380 unsigned Flags, unsigned EntrySize,
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000381 const MCSymbolELF *GroupSym,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000382 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000383 const MCSectionELF *Associated) {
Rafael Espindola61e8ce32015-04-06 04:25:18 +0000384 StringRef Group = "";
385 if (GroupSym)
386 Group = GroupSym->getName();
Chris Lattner5418dd52010-04-08 21:26:26 +0000387 // Do the lookup, if we have a hit, return it.
David Blaikie9ec32122014-04-10 23:55:11 +0000388 auto IterBool = ELFUniquingMap.insert(
Eric Christopher36e601c2016-07-01 06:07:38 +0000389 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000390 auto &Entry = *IterBool.first;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000391 if (!IterBool.second)
Rafael Espindola68fa2492015-02-17 20:48:01 +0000392 return Entry.second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000393
Rafael Espindola44d50572015-03-27 21:34:24 +0000394 StringRef CachedName = Entry.first.SectionName;
Rafael Espindolaba31e272015-01-29 17:33:21 +0000395
396 SectionKind Kind;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000397 if (Flags & ELF::SHF_ARM_PURECODE)
398 Kind = SectionKind::getExecuteOnly();
399 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindolaba31e272015-01-29 17:33:21 +0000400 Kind = SectionKind::getText();
401 else
402 Kind = SectionKind::getReadOnly();
403
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000404 MCSectionELF *Result = createELFSectionImpl(
405 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000406 Entry.second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000407 return Result;
408}
409
Rafael Espindola0ccf9b72015-06-02 21:30:13 +0000410MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola13a79bb2017-02-02 21:26:06 +0000411 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
412 SectionKind::getReadOnly(), 4, Group, ~0,
413 nullptr);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000414}
415
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000416MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
417 unsigned Characteristics,
418 SectionKind Kind,
419 StringRef COMDATSymName, int Selection,
Reid Kleckner97837b72016-05-02 23:22:18 +0000420 unsigned UniqueID,
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000421 const char *BeginSymName) {
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000422 MCSymbol *COMDATSymbol = nullptr;
423 if (!COMDATSymName.empty()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000424 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000425 COMDATSymName = COMDATSymbol->getName();
426 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000427
Reid Kleckner97837b72016-05-02 23:22:18 +0000428
Rafael Espindolad3ac79b2015-03-30 13:59:06 +0000429 // Do the lookup, if we have a hit, return it.
Reid Kleckner97837b72016-05-02 23:22:18 +0000430 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerc57d0382014-06-27 17:19:44 +0000431 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie9ec32122014-04-10 23:55:11 +0000432 auto Iter = IterBool.first;
433 if (!IterBool.second)
Rafael Espindola60ec3832013-11-19 19:52:52 +0000434 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000435
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000436 MCSymbol *Begin = nullptr;
437 if (BeginSymName)
Rafael Espindola9ab09232015-03-17 20:07:06 +0000438 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000439
Rafael Espindola44d50572015-03-27 21:34:24 +0000440 StringRef CachedName = Iter->first.SectionName;
Rafael Espindola4264e2d2015-10-07 19:08:19 +0000441 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindola6b9998b2015-03-10 22:00:25 +0000442 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000443
444 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000445 return Result;
446}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000447
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000448MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
449 unsigned Characteristics,
450 SectionKind Kind,
451 const char *BeginSymName) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000452 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
453 BeginSymName);
Rafael Espindola60ec3832013-11-19 19:52:52 +0000454}
455
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000456MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner97837b72016-05-02 23:22:18 +0000457 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerc57d0382014-06-27 17:19:44 +0000458 auto Iter = COFFUniquingMap.find(T);
David Blaikie9ec32122014-04-10 23:55:11 +0000459 if (Iter == COFFUniquingMap.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000460 return nullptr;
Rafael Espindola60ec3832013-11-19 19:52:52 +0000461 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000462}
463
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000464MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner97837b72016-05-02 23:22:18 +0000465 const MCSymbol *KeySym,
466 unsigned UniqueID) {
467 // Return the normal section if we don't have to be associative or unique.
468 if (!KeySym && UniqueID == GenericSectionID)
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000469 return Sec;
470
Reid Kleckner97837b72016-05-02 23:22:18 +0000471 // If we have a key symbol, make an associative section with the same name and
472 // kind as the normal section.
473 unsigned Characteristics = Sec->getCharacteristics();
474 if (KeySym) {
475 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
476 return getCOFFSection(Sec->getSectionName(), Characteristics,
477 Sec->getKind(), KeySym->getName(),
478 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
479 }
480
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000481 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner97837b72016-05-02 23:22:18 +0000482 "", 0, UniqueID);
Reid Kleckner7c4059e2014-09-04 17:42:03 +0000483}
484
Dan Gohman18eafb62017-02-22 01:23:18 +0000485void MCContext::renameWasmSection(MCSectionWasm *Section, StringRef Name) {
486 StringRef GroupName;
487 assert(!Section->getGroup() && "not yet implemented");
488
489 unsigned UniqueID = Section->getUniqueID();
490 WasmUniquingMap.erase(
491 WasmSectionKey{Section->getSectionName(), GroupName, UniqueID});
492 auto I = WasmUniquingMap.insert(std::make_pair(
493 WasmSectionKey{Name, GroupName, UniqueID},
494 Section))
495 .first;
496 StringRef CachedName = I->first.SectionName;
497 const_cast<MCSectionWasm *>(Section)->setSectionName(CachedName);
498}
499
500MCSectionWasm *MCContext::createWasmRelSection(const Twine &Name, unsigned Type,
501 unsigned Flags,
502 const MCSymbolWasm *Group) {
503 StringMap<bool>::iterator I;
504 bool Inserted;
505 std::tie(I, Inserted) =
506 RelSecNames.insert(std::make_pair(Name.str(), true));
507
508 return new (WasmAllocator.Allocate())
509 MCSectionWasm(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
510 Group, ~0, nullptr);
511}
512
513MCSectionWasm *MCContext::getWasmNamedSection(const Twine &Prefix,
514 const Twine &Suffix, unsigned Type,
515 unsigned Flags) {
516 return getWasmSection(Prefix + "." + Suffix, Type, Flags, Suffix);
517}
518
519MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
520 unsigned Flags,
521 const Twine &Group, unsigned UniqueID,
522 const char *BeginSymName) {
523 MCSymbolWasm *GroupSym = nullptr;
524 if (!Group.isTriviallyEmpty() && !Group.str().empty())
525 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
526
527 return getWasmSection(Section, Type, Flags, GroupSym, UniqueID, BeginSymName);
528}
529
530MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
531 unsigned Flags,
532 const MCSymbolWasm *GroupSym,
533 unsigned UniqueID,
534 const char *BeginSymName) {
535 StringRef Group = "";
536 if (GroupSym)
537 Group = GroupSym->getName();
538 // Do the lookup, if we have a hit, return it.
539 auto IterBool = WasmUniquingMap.insert(
540 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
541 auto &Entry = *IterBool.first;
542 if (!IterBool.second)
543 return Entry.second;
544
545 StringRef CachedName = Entry.first.SectionName;
546
547 SectionKind Kind = SectionKind::getText();
548
549 MCSymbol *Begin = nullptr;
550 if (BeginSymName)
551 Begin = createTempSymbol(BeginSymName, false);
552
553 MCSectionWasm *Result = new (WasmAllocator.Allocate())
554 MCSectionWasm(CachedName, Type, Flags, Kind, GroupSym, UniqueID, Begin);
555 Entry.second = Result;
556 return Result;
557}
558
Akira Hatanakab11ef082015-11-14 06:35:56 +0000559MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
560 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
561}
562
Kevin Enderbye5930f12010-07-28 20:55:35 +0000563//===----------------------------------------------------------------------===//
564// Dwarf Management
565//===----------------------------------------------------------------------===//
566
Jim Grosbach6f482002015-05-18 18:43:14 +0000567/// getDwarfFile - takes a file name an number to place in the dwarf file and
Kevin Enderbye5930f12010-07-28 20:55:35 +0000568/// directory tables. If the file number has already been allocated it is an
569/// error and zero is returned and the client reports the error, else the
570/// allocated file number is returned. The file numbers may be in any order.
Jim Grosbach6f482002015-05-18 18:43:14 +0000571unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000572 unsigned FileNumber, unsigned CUID) {
David Blaikied9012ba2014-03-13 21:59:51 +0000573 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
David Blaikie498589c2014-03-13 19:15:04 +0000574 return Table.getFile(Directory, FileName, FileNumber);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000575}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000576
Kevin Enderbya68d0042010-10-04 20:17:24 +0000577/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000578/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000579bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Jim Grosbach008359a2015-05-18 18:43:23 +0000580 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
581 if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000582 return false;
583
David Blaikiea55ddad2014-03-13 18:55:04 +0000584 return !MCDwarfFiles[FileNumber].Name.empty();
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000585}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000586
Rafael Espindolae0746792015-05-21 16:52:32 +0000587/// Remove empty sections from SectionStartEndSyms, to avoid generating
Oliver Stannard8b273082014-06-19 15:52:37 +0000588/// useless debug info for them.
589void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramer412c4db2015-05-31 18:49:28 +0000590 SectionsForRanges.remove_if(
591 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard8b273082014-06-19 15:52:37 +0000592}
593
Reid Kleckner2214ed82016-01-29 00:49:42 +0000594CodeViewContext &MCContext::getCVContext() {
595 if (!CVContext.get())
596 CVContext.reset(new CodeViewContext);
597 return *CVContext.get();
598}
599
Oliver Stannard07b43d32015-11-17 09:58:07 +0000600//===----------------------------------------------------------------------===//
601// Error Reporting
602//===----------------------------------------------------------------------===//
603
604void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
605 HadError = true;
606
Sanne Wouda29338752017-02-08 14:48:05 +0000607 // If we have a source manager use it. Otherwise, try using the inline source
608 // manager.
609 // If that fails, use the generic report_fatal_error().
610 if (SrcMgr)
611 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
612 else if (InlineSrcMgr)
613 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
614 else
Jim Grosbach9a3284f2014-03-14 22:41:58 +0000615 report_fatal_error(Msg, false);
Oliver Stannard07b43d32015-11-17 09:58:07 +0000616}
617
618void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
619 reportError(Loc, Msg);
Jim Grosbachb18b4092012-01-26 23:20:11 +0000620
621 // If we reached here, we are failing ungracefully. Run the interrupt handlers
622 // to make sure any special cleanups get done, in particular that we remove
623 // files registered with RemoveFileOnSignal.
624 sys::RunInterruptHandlers();
625 exit(1);
626}