blob: fab517075c5ac6aef8d7f65fbd94d7910676a205 [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/MC/MCContext.h"
Scott Linder5e4b5152018-02-23 23:01:06 +000011#include "llvm/ADT/Optional.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringRef.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000016#include "llvm/ADT/Twine.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/COFF.h"
18#include "llvm/BinaryFormat/ELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/MC/MCAsmInfo.h"
Reid Kleckner1689efb2016-01-29 00:49:42 +000020#include "llvm/MC/MCCodeView.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/MC/MCDwarf.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFragment.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/MC/MCLabel.h"
25#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSectionELF.h"
28#include "llvm/MC/MCSectionMachO.h"
Dan Gohmand660a5d2017-02-22 01:23:18 +000029#include "llvm/MC/MCSectionWasm.h"
Pete Cooper05be9b12015-03-04 01:24:11 +000030#include "llvm/MC/MCStreamer.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000031#include "llvm/MC/MCSymbol.h"
Pete Cooperf560b882015-06-08 17:17:12 +000032#include "llvm/MC/MCSymbolCOFF.h"
Rafael Espindolaf7e06852015-06-02 00:25:12 +000033#include "llvm/MC/MCSymbolELF.h"
Pete Coopercae8bc42015-06-08 17:17:23 +000034#include "llvm/MC/MCSymbolMachO.h"
Dan Gohmand660a5d2017-02-22 01:23:18 +000035#include "llvm/MC/MCSymbolWasm.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000036#include "llvm/MC/SectionKind.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000037#include "llvm/Support/Casting.h"
Igor Laevskyafaddb42016-06-17 15:19:41 +000038#include "llvm/Support/CommandLine.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000039#include "llvm/Support/ErrorHandling.h"
Eric Christopher6c583142012-12-18 00:31:01 +000040#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000041#include "llvm/Support/Signals.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/Support/SourceMgr.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000043#include "llvm/Support/raw_ostream.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000044#include <cassert>
45#include <cstdlib>
46#include <tuple>
47#include <utility>
David Blaikie2b3ea3c2013-10-22 23:41:52 +000048
Daniel Dunbarecc63f82009-06-23 22:01:43 +000049using namespace llvm;
50
Igor Laevskyafaddb42016-06-17 15:19:41 +000051static cl::opt<char*>
52AsSecureLogFileName("as-secure-log-file-name",
53 cl::desc("As secure log file name (initialized from "
54 "AS_SECURE_LOG_FILE env variable)"),
55 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
56
Bill Wendling99cb6222013-06-18 07:20:20 +000057MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigas873a1dd2012-12-06 22:12:44 +000058 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
David Blaikie0a0c4cc2014-03-27 20:45:58 +000059 bool DoAutoReset)
Evgeniy Stepanov5982b2c2017-02-24 21:44:52 +000060 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
61 Symbols(Allocator), UsedNames(Allocator),
62 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
Eli Friedmanc2069782018-10-12 19:41:05 +000063 AutoReset(DoAutoReset) {
Igor Laevskyafaddb42016-06-17 15:19:41 +000064 SecureLogFile = AsSecureLogFileName;
Eric Christopher6c583142012-12-18 00:31:01 +000065
Alp Toker1508c822014-07-06 10:33:31 +000066 if (SrcMgr && SrcMgr->getNumBuffers())
67 MainFileName =
68 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000069}
70
71MCContext::~MCContext() {
Pedro Artigas5399d252012-12-12 22:59:46 +000072 if (AutoReset)
73 reset();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000074
75 // NOTE: The symbols are all allocated out of a bump pointer allocator,
76 // we don't need to free them here.
Pedro Artigas873a1dd2012-12-06 22:12:44 +000077}
78
79//===----------------------------------------------------------------------===//
80// Module Lifetime Management
81//===----------------------------------------------------------------------===//
82
Pedro Artigas5399d252012-12-12 22:59:46 +000083void MCContext::reset() {
Rafael Espindolaeac71e22015-05-26 01:52:19 +000084 // Call the destructors so the fragments are freed
Rafael Espindolad546ca62015-10-07 19:08:19 +000085 COFFAllocator.DestroyAll();
86 ELFAllocator.DestroyAll();
87 MachOAllocator.DestroyAll();
Rafael Espindolaeac71e22015-05-26 01:52:19 +000088
Akira Hatanaka55c02682015-11-14 06:35:56 +000089 MCSubtargetAllocator.DestroyAll();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000090 UsedNames.clear();
91 Symbols.clear();
92 Allocator.Reset();
93 Instances.clear();
Yaron Kerenc63035a2014-09-17 09:25:36 +000094 CompilationDir.clear();
95 MainFileName.clear();
David Blaikie43d5e072014-03-13 21:59:51 +000096 MCDwarfLineTablesCUMap.clear();
Rafael Espindola43356a12015-05-21 16:52:32 +000097 SectionsForRanges.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000098 MCGenDwarfLabelEntries.clear();
99 DwarfDebugFlags = StringRef();
Pedro Artigas9e7924d2013-02-20 00:10:29 +0000100 DwarfCompileUnitID = 0;
Jim Grosbach7b81f732015-05-18 18:43:23 +0000101 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000102
Reid Kleckner1689efb2016-01-29 00:49:42 +0000103 CVContext.reset();
104
David Blaikie42382d32014-04-10 23:55:11 +0000105 MachOUniquingMap.clear();
106 ELFUniquingMap.clear();
107 COFFUniquingMap.clear();
Sam Clegg1838ee92018-05-30 02:57:20 +0000108 WasmUniquingMap.clear();
Pedro Artigas5399d252012-12-12 22:59:46 +0000109
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000110 NextID.clear();
Pedro Artigas5399d252012-12-12 22:59:46 +0000111 AllowTemporaryLabels = true;
112 DwarfLocSeen = false;
113 GenDwarfForAssembly = false;
114 GenDwarfFileNumber = 0;
Oliver Stannard43f1dd72015-11-17 09:58:07 +0000115
116 HadError = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000117}
118
Chris Lattnerf0559e42010-04-08 20:30:37 +0000119//===----------------------------------------------------------------------===//
120// Symbol Manipulation
121//===----------------------------------------------------------------------===//
122
Jim Grosbach19696da2015-05-18 18:43:14 +0000123MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
Yaron Keren0401f792015-03-17 09:51:17 +0000124 SmallString<128> NameSV;
125 StringRef NameRef = Name.toStringRef(NameSV);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000126
Yaron Keren0401f792015-03-17 09:51:17 +0000127 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
Rafael Espindola9f447242010-12-01 20:46:11 +0000128
Yaron Keren0401f792015-03-17 09:51:17 +0000129 MCSymbol *&Sym = Symbols[NameRef];
David Blaikie1d4f28c2014-11-19 05:49:42 +0000130 if (!Sym)
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000131 Sym = createSymbol(NameRef, false, false);
Rafael Espindola9f447242010-12-01 20:46:11 +0000132
Rafael Espindola9f447242010-12-01 20:46:11 +0000133 return Sym;
134}
135
Reid Kleckner9f7c8612015-03-05 18:26:34 +0000136MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
137 unsigned Idx) {
Jim Grosbach19696da2015-05-18 18:43:14 +0000138 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
Reid Kleckner9f7c8612015-03-05 18:26:34 +0000139 "$frame_escape_" + Twine(Idx));
Reid Kleckner221a7072015-01-13 00:48:10 +0000140}
141
David Majnemer64386622015-03-31 22:35:44 +0000142MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
Jim Grosbach19696da2015-05-18 18:43:14 +0000143 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
David Majnemer64386622015-03-31 22:35:44 +0000144 "$parent_frame_offset");
145}
146
Reid Kleckner7681f6a2015-05-20 23:08:04 +0000147MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
148 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
149 FuncName);
150}
151
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000152MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
153 bool IsTemporary) {
Pete Cooperf560b882015-06-08 17:17:12 +0000154 if (MOFI) {
Rafael Espindolaea619d02015-08-14 15:48:41 +0000155 switch (MOFI->getObjectFileType()) {
156 case MCObjectFileInfo::IsCOFF:
Pete Cooper79e04c52015-06-09 18:36:13 +0000157 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
Rafael Espindolaea619d02015-08-14 15:48:41 +0000158 case MCObjectFileInfo::IsELF:
Pete Cooper79e04c52015-06-09 18:36:13 +0000159 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
Rafael Espindolaea619d02015-08-14 15:48:41 +0000160 case MCObjectFileInfo::IsMachO:
Pete Cooper79e04c52015-06-09 18:36:13 +0000161 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
Dan Gohmand660a5d2017-02-22 01:23:18 +0000162 case MCObjectFileInfo::IsWasm:
163 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
Pete Cooperf560b882015-06-08 17:17:12 +0000164 }
165 }
Pete Cooper79e04c52015-06-09 18:36:13 +0000166 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
167 IsTemporary);
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000168}
169
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000170MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
171 bool CanBeUnnamed) {
172 if (CanBeUnnamed && !UseNamesOnTempLabels)
173 return createSymbolImpl(nullptr, true);
174
Eric Christopher32b5cf72016-09-29 02:03:44 +0000175 // Determine whether this is a user written assembler temporary or normal
Rafael Espindola0ba7ba42015-06-02 22:52:13 +0000176 // label, if used.
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000177 bool IsTemporary = CanBeUnnamed;
178 if (AllowTemporaryLabels && !IsTemporary)
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000179 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +0000180
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000181 SmallString<128> NewName = Name;
182 bool AddSuffix = AlwaysAddSuffix;
183 unsigned &NextUniqueID = NextID[Name];
Eugene Zelenko7211c532017-02-14 00:33:36 +0000184 while (true) {
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000185 if (AddSuffix) {
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000186 NewName.resize(Name.size());
187 raw_svector_ostream(NewName) << NextUniqueID++;
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000188 }
189 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
Evgeniy Stepanov3828f992016-03-28 20:36:28 +0000190 if (NameEntry.second || !NameEntry.first->second) {
191 // Ok, we found a name.
192 // Mark it as used for a non-section symbol.
193 NameEntry.first->second = true;
194 // Have the MCSymbol object itself refer to the copy of the string that is
195 // embedded in the UsedNames entry.
Rafael Espindolaf7e06852015-06-02 00:25:12 +0000196 return createSymbolImpl(&*NameEntry.first, IsTemporary);
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000197 }
198 assert(IsTemporary && "Cannot rename non-temporary symbols");
199 AddSuffix = true;
Rafael Espindola9f447242010-12-01 20:46:11 +0000200 }
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000201 llvm_unreachable("Infinite loop");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000202}
203
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000204MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
205 bool CanBeUnnamed) {
Rafael Espindola150233c2015-02-27 18:18:39 +0000206 SmallString<128> NameSV;
207 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000208 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
Rafael Espindola150233c2015-02-27 18:18:39 +0000209}
210
Jim Grosbach19696da2015-05-18 18:43:14 +0000211MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000212 SmallString<128> NameSV;
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000213 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000214 return createSymbol(NameSV, true, false);
Tim Northoverc5d592d2014-03-29 07:05:06 +0000215}
216
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000217MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
218 return createTempSymbol("tmp", true, CanBeUnnamed);
Chris Lattner1d72a762010-03-14 08:23:30 +0000219}
220
Rafael Espindola7e9df192014-03-13 18:09:26 +0000221unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000222 MCLabel *&Label = Instances[LocalLabelVal];
223 if (!Label)
224 Label = new (*this) MCLabel(0);
225 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000226}
227
Rafael Espindola7e9df192014-03-13 18:09:26 +0000228unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000229 MCLabel *&Label = Instances[LocalLabelVal];
230 if (!Label)
231 Label = new (*this) MCLabel(0);
232 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000233}
234
Rafael Espindola7e9df192014-03-13 18:09:26 +0000235MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
236 unsigned Instance) {
237 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
238 if (!Sym)
Daniel Jasperb7f5b8b2015-06-23 11:31:32 +0000239 Sym = createTempSymbol(false);
Rafael Espindola7e9df192014-03-13 18:09:26 +0000240 return Sym;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000241}
Rafael Espindola7e9df192014-03-13 18:09:26 +0000242
Jim Grosbach19696da2015-05-18 18:43:14 +0000243MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
Rafael Espindola7e9df192014-03-13 18:09:26 +0000244 unsigned Instance = NextInstance(LocalLabelVal);
245 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
246}
247
Jim Grosbach19696da2015-05-18 18:43:14 +0000248MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
Rafael Espindola7e9df192014-03-13 18:09:26 +0000249 bool Before) {
250 unsigned Instance = GetInstance(LocalLabelVal);
251 if (!Before)
252 ++Instance;
253 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000254}
255
Jim Grosbach19696da2015-05-18 18:43:14 +0000256MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
Roman Divackyf145c132012-09-18 17:10:37 +0000257 SmallString<128> NameSV;
Yaron Keren6279aae2015-03-17 18:55:30 +0000258 StringRef NameRef = Name.toStringRef(NameSV);
259 return Symbols.lookup(NameRef);
Roman Divackyf145c132012-09-18 17:10:37 +0000260}
261
Mandeep Singh Grang91968992016-12-06 02:49:17 +0000262void MCContext::setSymbolValue(MCStreamer &Streamer,
263 StringRef Sym,
264 uint64_t Val) {
265 auto Symbol = getOrCreateSymbol(Sym);
266 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
Mandeep Singh Grang017b7af2016-12-01 18:42:04 +0000267}
268
Chris Lattnerf0559e42010-04-08 20:30:37 +0000269//===----------------------------------------------------------------------===//
270// Section Management
271//===----------------------------------------------------------------------===//
272
Rafael Espindola75219642015-05-21 19:20:38 +0000273MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
274 unsigned TypeAndAttributes,
275 unsigned Reserved2, SectionKind Kind,
276 const char *BeginSymName) {
Chris Lattnerf0559e42010-04-08 20:30:37 +0000277 // We unique sections by their segment/section pair. The returned section
278 // may not have the same flags as the requested section, if so this should be
279 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000280
Chris Lattnerf0559e42010-04-08 20:30:37 +0000281 // Form the name to look up.
282 SmallString<64> Name;
283 Name += Segment;
284 Name.push_back(',');
285 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000286
Chris Lattnerf0559e42010-04-08 20:30:37 +0000287 // Do the lookup, if we have a hit, return it.
Rafael Espindola75219642015-05-21 19:20:38 +0000288 MCSectionMachO *&Entry = MachOUniquingMap[Name];
Rafael Espindola5c4720d2015-03-10 21:16:18 +0000289 if (Entry)
290 return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000291
Rafael Espindolaf3d745c2015-03-10 22:00:25 +0000292 MCSymbol *Begin = nullptr;
293 if (BeginSymName)
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000294 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindolaf3d745c2015-03-10 22:00:25 +0000295
Chris Lattnerf0559e42010-04-08 20:30:37 +0000296 // Otherwise, return a new section.
Rafael Espindolad546ca62015-10-07 19:08:19 +0000297 return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
298 Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000299}
Chris Lattner74aae472010-04-08 21:26:26 +0000300
Richard Smithe89c0002016-05-25 00:14:12 +0000301void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
302 StringRef GroupName;
303 if (const MCSymbol *Group = Section->getGroup())
304 GroupName = Group->getName();
305
306 unsigned UniqueID = Section->getUniqueID();
307 ELFUniquingMap.erase(
308 ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
309 auto I = ELFUniquingMap.insert(std::make_pair(
310 ELFSectionKey{Name, GroupName, UniqueID},
311 Section))
312 .first;
313 StringRef CachedName = I->first.SectionName;
314 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
315}
316
Rafael Espindola7931a612017-02-02 21:26:06 +0000317MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
318 unsigned Flags, SectionKind K,
319 unsigned EntrySize,
320 const MCSymbolELF *Group,
321 unsigned UniqueID,
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000322 const MCSymbolELF *Associated) {
Rafael Espindola7931a612017-02-02 21:26:06 +0000323 MCSymbolELF *R;
324 MCSymbol *&Sym = Symbols[Section];
Evgeniy Stepanov89efd712017-02-24 21:44:58 +0000325 // A section symbol can not redefine regular symbols. There may be multiple
326 // sections with the same name, in which case the first such section wins.
327 if (Sym && Sym->isDefined() &&
328 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
329 reportError(SMLoc(), "invalid symbol redefinition");
Rafael Espindola7931a612017-02-02 21:26:06 +0000330 if (Sym && Sym->isUndefined()) {
331 R = cast<MCSymbolELF>(Sym);
332 } else {
333 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
334 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
335 if (!Sym)
336 Sym = R;
337 }
338 R->setBinding(ELF::STB_LOCAL);
339 R->setType(ELF::STT_SECTION);
Rafael Espindola7931a612017-02-02 21:26:06 +0000340
341 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
342 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
343
344 auto *F = new MCDataFragment();
345 Ret->getFragmentList().insert(Ret->begin(), F);
346 F->setParent(Ret);
347 R->setFragment(F);
348
349 return Ret;
350}
351
Eric Christopherdea5ff12016-07-01 06:07:38 +0000352MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
Rafael Espindola75219642015-05-21 19:20:38 +0000353 unsigned Flags, unsigned EntrySize,
Rafael Espindola4cc59a12015-06-02 21:30:13 +0000354 const MCSymbolELF *Group,
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000355 const MCSectionELF *RelInfoSection) {
Rafael Espindola0bd11cd2015-03-30 13:39:16 +0000356 StringMap<bool>::iterator I;
357 bool Inserted;
Eric Christopherdea5ff12016-07-01 06:07:38 +0000358 std::tie(I, Inserted) =
Dan Gohmand660a5d2017-02-22 01:23:18 +0000359 RelSecNames.insert(std::make_pair(Name.str(), true));
Rafael Espindola0bd11cd2015-03-30 13:39:16 +0000360
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000361 return createELFSectionImpl(
362 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
363 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
Rafael Espindola0bd11cd2015-03-30 13:39:16 +0000364}
365
Eric Christopherdea5ff12016-07-01 06:07:38 +0000366MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
367 const Twine &Suffix, unsigned Type,
368 unsigned Flags,
369 unsigned EntrySize) {
370 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
371}
372
373MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola75219642015-05-21 19:20:38 +0000374 unsigned Flags, unsigned EntrySize,
Rafael Espindola5a2d92c2017-02-09 14:59:20 +0000375 const Twine &Group, unsigned UniqueID,
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000376 const MCSymbolELF *Associated) {
Rafael Espindola4cc59a12015-06-02 21:30:13 +0000377 MCSymbolELF *GroupSym = nullptr;
Eric Christopherdea5ff12016-07-01 06:07:38 +0000378 if (!Group.isTriviallyEmpty() && !Group.str().empty())
Rafael Espindola4cc59a12015-06-02 21:30:13 +0000379 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
Rafael Espindolafd45ac42015-03-30 13:59:06 +0000380
Rafael Espindola83be4422015-04-06 04:25:18 +0000381 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
Rafael Espindola5a2d92c2017-02-09 14:59:20 +0000382 Associated);
Rafael Espindola83be4422015-04-06 04:25:18 +0000383}
384
Eric Christopherdea5ff12016-07-01 06:07:38 +0000385MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
Rafael Espindola75219642015-05-21 19:20:38 +0000386 unsigned Flags, unsigned EntrySize,
Rafael Espindola4cc59a12015-06-02 21:30:13 +0000387 const MCSymbolELF *GroupSym,
Rafael Espindola75219642015-05-21 19:20:38 +0000388 unsigned UniqueID,
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000389 const MCSymbolELF *Associated) {
Rafael Espindola83be4422015-04-06 04:25:18 +0000390 StringRef Group = "";
391 if (GroupSym)
392 Group = GroupSym->getName();
Chris Lattner74aae472010-04-08 21:26:26 +0000393 // Do the lookup, if we have a hit, return it.
David Blaikie42382d32014-04-10 23:55:11 +0000394 auto IterBool = ELFUniquingMap.insert(
Eric Christopherdea5ff12016-07-01 06:07:38 +0000395 std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
David Blaikie42382d32014-04-10 23:55:11 +0000396 auto &Entry = *IterBool.first;
Rafael Espindola903f4a22015-04-04 18:02:01 +0000397 if (!IterBool.second)
Rafael Espindolae0a25412015-02-17 20:48:01 +0000398 return Entry.second;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000399
Rafael Espindola0ac75912015-03-27 21:34:24 +0000400 StringRef CachedName = Entry.first.SectionName;
Rafael Espindola9936b802015-01-29 17:33:21 +0000401
402 SectionKind Kind;
Prakhar Bahugunaaf672fb2016-12-15 07:59:08 +0000403 if (Flags & ELF::SHF_ARM_PURECODE)
404 Kind = SectionKind::getExecuteOnly();
405 else if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola9936b802015-01-29 17:33:21 +0000406 Kind = SectionKind::getText();
407 else
408 Kind = SectionKind::getReadOnly();
409
George Rimar2a751e12018-08-29 09:04:52 +0000410 MCSectionELF *Result = createELFSectionImpl(
411 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
412 Entry.second = Result;
413 return Result;
Chris Lattner74aae472010-04-08 21:26:26 +0000414}
415
Rafael Espindola4cc59a12015-06-02 21:30:13 +0000416MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
Rafael Espindola7931a612017-02-02 21:26:06 +0000417 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
418 SectionKind::getReadOnly(), 4, Group, ~0,
419 nullptr);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000420}
421
Rafael Espindola75219642015-05-21 19:20:38 +0000422MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
423 unsigned Characteristics,
424 SectionKind Kind,
425 StringRef COMDATSymName, int Selection,
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000426 unsigned UniqueID,
Rafael Espindola75219642015-05-21 19:20:38 +0000427 const char *BeginSymName) {
Rafael Espindolafd45ac42015-03-30 13:59:06 +0000428 MCSymbol *COMDATSymbol = nullptr;
429 if (!COMDATSymName.empty()) {
Jim Grosbach19696da2015-05-18 18:43:14 +0000430 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
Rafael Espindolafd45ac42015-03-30 13:59:06 +0000431 COMDATSymName = COMDATSymbol->getName();
432 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000433
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000434
Rafael Espindolafd45ac42015-03-30 13:59:06 +0000435 // Do the lookup, if we have a hit, return it.
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000436 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
David Majnemerd7d42772014-06-27 17:19:44 +0000437 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
David Blaikie42382d32014-04-10 23:55:11 +0000438 auto Iter = IterBool.first;
439 if (!IterBool.second)
Rafael Espindola72935dd2013-11-19 19:52:52 +0000440 return Iter->second;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000441
Rafael Espindolaf3d745c2015-03-10 22:00:25 +0000442 MCSymbol *Begin = nullptr;
443 if (BeginSymName)
Rafael Espindolab415e6b2015-03-17 20:07:06 +0000444 Begin = createTempSymbol(BeginSymName, false);
Rafael Espindolaf3d745c2015-03-10 22:00:25 +0000445
Rafael Espindola0ac75912015-03-27 21:34:24 +0000446 StringRef CachedName = Iter->first.SectionName;
Rafael Espindolad546ca62015-10-07 19:08:19 +0000447 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
Rafael Espindolaf3d745c2015-03-10 22:00:25 +0000448 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
Rafael Espindola72935dd2013-11-19 19:52:52 +0000449
450 Iter->second = Result;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000451 return Result;
452}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000453
Rafael Espindola75219642015-05-21 19:20:38 +0000454MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
455 unsigned Characteristics,
456 SectionKind Kind,
457 const char *BeginSymName) {
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000458 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
459 BeginSymName);
Rafael Espindola72935dd2013-11-19 19:52:52 +0000460}
461
Rafael Espindola75219642015-05-21 19:20:38 +0000462MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000463 COFFSectionKey T{Section, "", 0, GenericSectionID};
David Majnemerd7d42772014-06-27 17:19:44 +0000464 auto Iter = COFFUniquingMap.find(T);
David Blaikie42382d32014-04-10 23:55:11 +0000465 if (Iter == COFFUniquingMap.end())
Craig Topper4266ae82014-04-13 04:57:38 +0000466 return nullptr;
Rafael Espindola72935dd2013-11-19 19:52:52 +0000467 return Iter->second;
Nico Rieck80646282013-07-06 12:13:10 +0000468}
469
Rafael Espindola75219642015-05-21 19:20:38 +0000470MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000471 const MCSymbol *KeySym,
472 unsigned UniqueID) {
473 // Return the normal section if we don't have to be associative or unique.
474 if (!KeySym && UniqueID == GenericSectionID)
Reid Klecknerb9cb76d2014-09-04 17:42:03 +0000475 return Sec;
476
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000477 // If we have a key symbol, make an associative section with the same name and
478 // kind as the normal section.
479 unsigned Characteristics = Sec->getCharacteristics();
480 if (KeySym) {
481 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
482 return getCOFFSection(Sec->getSectionName(), Characteristics,
483 Sec->getKind(), KeySym->getName(),
484 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
485 }
486
Reid Klecknerb9cb76d2014-09-04 17:42:03 +0000487 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000488 "", 0, UniqueID);
Reid Klecknerb9cb76d2014-09-04 17:42:03 +0000489}
490
Sam Clegg2b6b6ac2017-10-20 21:28:38 +0000491MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
Dan Gohmand660a5d2017-02-22 01:23:18 +0000492 const Twine &Group, unsigned UniqueID,
493 const char *BeginSymName) {
494 MCSymbolWasm *GroupSym = nullptr;
Sam Clegg2c77fcc2018-01-11 20:35:17 +0000495 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
Dan Gohmand660a5d2017-02-22 01:23:18 +0000496 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
Sam Clegg2c77fcc2018-01-11 20:35:17 +0000497 GroupSym->setComdat(true);
498 }
Dan Gohmand660a5d2017-02-22 01:23:18 +0000499
Sam Clegg2b6b6ac2017-10-20 21:28:38 +0000500 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
Dan Gohmand660a5d2017-02-22 01:23:18 +0000501}
502
Sam Clegg2b6b6ac2017-10-20 21:28:38 +0000503MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
Dan Gohmand660a5d2017-02-22 01:23:18 +0000504 const MCSymbolWasm *GroupSym,
505 unsigned UniqueID,
506 const char *BeginSymName) {
507 StringRef Group = "";
508 if (GroupSym)
509 Group = GroupSym->getName();
510 // Do the lookup, if we have a hit, return it.
511 auto IterBool = WasmUniquingMap.insert(
512 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
513 auto &Entry = *IterBool.first;
514 if (!IterBool.second)
515 return Entry.second;
516
517 StringRef CachedName = Entry.first.SectionName;
518
Sam Clegg70b76a12018-05-10 17:38:35 +0000519 MCSymbol *Begin = createSymbol(CachedName, false, false);
520 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
Dan Gohmand660a5d2017-02-22 01:23:18 +0000521
522 MCSectionWasm *Result = new (WasmAllocator.Allocate())
Sam Clegg2b6b6ac2017-10-20 21:28:38 +0000523 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Dan Gohmand660a5d2017-02-22 01:23:18 +0000524 Entry.second = Result;
Sam Clegg70b76a12018-05-10 17:38:35 +0000525
526 auto *F = new MCDataFragment();
527 Result->getFragmentList().insert(Result->begin(), F);
528 F->setParent(Result);
529 Begin->setFragment(F);
530
Dan Gohmand660a5d2017-02-22 01:23:18 +0000531 return Result;
532}
533
Akira Hatanaka55c02682015-11-14 06:35:56 +0000534MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
535 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
536}
537
Paul Robinson4575d342018-07-10 14:41:54 +0000538void MCContext::addDebugPrefixMapEntry(const std::string &From,
539 const std::string &To) {
540 DebugPrefixMap.insert(std::make_pair(From, To));
541}
542
Jonas Devliegheref0c06bd2018-07-11 12:30:35 +0000543void MCContext::RemapDebugPaths() {
544 const auto &DebugPrefixMap = this->DebugPrefixMap;
545 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
546 for (const auto &Entry : DebugPrefixMap)
547 if (StringRef(Path).startswith(Entry.first)) {
548 std::string RemappedPath =
549 (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
550 Path.swap(RemappedPath);
551 }
552 };
Paul Robinson4575d342018-07-10 14:41:54 +0000553
Jonas Devliegheref0c06bd2018-07-11 12:30:35 +0000554 // Remap compilation directory.
Paul Robinson4575d342018-07-10 14:41:54 +0000555 std::string CompDir = CompilationDir.str();
Jonas Devliegheref0c06bd2018-07-11 12:30:35 +0000556 RemapDebugPath(CompDir);
Paul Robinson4575d342018-07-10 14:41:54 +0000557 CompilationDir = CompDir;
Jonas Devliegheref0c06bd2018-07-11 12:30:35 +0000558
559 // Remap MCDwarfDirs in all compilation units.
560 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
561 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
562 RemapDebugPath(Dir);
Paul Robinson4575d342018-07-10 14:41:54 +0000563}
564
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000565//===----------------------------------------------------------------------===//
566// Dwarf Management
567//===----------------------------------------------------------------------===//
568
Scott Linder5e4b5152018-02-23 23:01:06 +0000569/// getDwarfFile - takes a file name and number to place in the dwarf file and
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000570/// directory tables. If the file number has already been allocated it is an
571/// error and zero is returned and the client reports the error, else the
572/// allocated file number is returned. The file numbers may be in any order.
Paul Robinson1dff32f2018-02-22 21:03:33 +0000573Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
574 StringRef FileName,
575 unsigned FileNumber,
576 MD5::MD5Result *Checksum,
Scott Linder5e4b5152018-02-23 23:01:06 +0000577 Optional<StringRef> Source,
Paul Robinson1dff32f2018-02-22 21:03:33 +0000578 unsigned CUID) {
David Blaikie43d5e072014-03-13 21:59:51 +0000579 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
Scott Linder5e4b5152018-02-23 23:01:06 +0000580 return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000581}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000582
Kevin Enderby3f55c242010-10-04 20:17:24 +0000583/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000584/// currently is assigned and false otherwise.
Manman Ren3de61b42013-03-07 01:42:00 +0000585bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Paul Robinsonfe160012018-06-22 14:16:11 +0000586 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
587 if (FileNumber == 0)
588 return getDwarfVersion() >= 5 && LineTable.hasRootFile();
589 if (FileNumber >= LineTable.getMCDwarfFiles().size())
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000590 return false;
591
Paul Robinsonfe160012018-06-22 14:16:11 +0000592 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000593}
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000594
Fangrui Song35bfd592018-09-08 02:04:20 +0000595/// Remove empty sections from SectionsForRanges, to avoid generating
Oliver Stannard591f9ee2014-06-19 15:52:37 +0000596/// useless debug info for them.
597void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
Benjamin Kramerd79915c2015-05-31 18:49:28 +0000598 SectionsForRanges.remove_if(
599 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
Oliver Stannard591f9ee2014-06-19 15:52:37 +0000600}
601
Reid Kleckner1689efb2016-01-29 00:49:42 +0000602CodeViewContext &MCContext::getCVContext() {
603 if (!CVContext.get())
604 CVContext.reset(new CodeViewContext);
605 return *CVContext.get();
606}
607
Oliver Stannard43f1dd72015-11-17 09:58:07 +0000608//===----------------------------------------------------------------------===//
609// Error Reporting
610//===----------------------------------------------------------------------===//
611
612void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
613 HadError = true;
614
Sanne Wouda58960032017-02-08 14:48:05 +0000615 // If we have a source manager use it. Otherwise, try using the inline source
616 // manager.
617 // If that fails, use the generic report_fatal_error().
618 if (SrcMgr)
619 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
620 else if (InlineSrcMgr)
621 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
622 else
Jim Grosbach6fb117d2014-03-14 22:41:58 +0000623 report_fatal_error(Msg, false);
Oliver Stannard43f1dd72015-11-17 09:58:07 +0000624}
625
626void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
627 reportError(Loc, Msg);
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000628
629 // If we reached here, we are failing ungracefully. Run the interrupt handlers
630 // to make sure any special cleanups get done, in particular that we remove
631 // files registered with RemoveFileOnSignal.
632 sys::RunInterruptHandlers();
633 exit(1);
634}