Daniel Dunbar | badeace | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===// |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 9 | #include "llvm/MC/MCContext.h" |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Optional.h" |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/ADT/StringMap.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/COFF.h" |
| 17 | #include "llvm/BinaryFormat/ELF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCCodeView.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDwarf.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCFragment.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCLabel.h" |
| 24 | #include "llvm/MC/MCObjectFileInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSectionCOFF.h" |
| 26 | #include "llvm/MC/MCSectionELF.h" |
| 27 | #include "llvm/MC/MCSectionMachO.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSectionWasm.h" |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSectionXCOFF.h" |
Pete Cooper | ef21bd4 | 2015-03-04 01:24:11 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSymbol.h" |
Pete Cooper | ad9f9c3 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCSymbolCOFF.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbolELF.h" |
Pete Cooper | eb012fa | 2015-06-08 17:17:23 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSymbolMachO.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCSymbolWasm.h" |
Jason Liu | 60ec248 | 2019-06-06 19:13:36 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCSymbolXCOFF.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 37 | #include "llvm/MC/SectionKind.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Casting.h" |
Igor Laevsky | 7b99885 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MemoryBuffer.h" |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Path.h" |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Signals.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 46 | #include <cassert> |
| 47 | #include <cstdlib> |
| 48 | #include <tuple> |
| 49 | #include <utility> |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 51 | using namespace llvm; |
| 52 | |
Igor Laevsky | 7b99885 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 53 | static cl::opt<char*> |
| 54 | AsSecureLogFileName("as-secure-log-file-name", |
| 55 | cl::desc("As secure log file name (initialized from " |
| 56 | "AS_SECURE_LOG_FILE env variable)"), |
| 57 | cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden); |
| 58 | |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 59 | MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 60 | const MCObjectFileInfo *mofi, const SourceMgr *mgr, |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 61 | MCTargetOptions const *TargetOpts, bool DoAutoReset) |
Evgeniy Stepanov | 0338ce8 | 2017-02-24 21:44:52 +0000 | [diff] [blame] | 62 | : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi), |
| 63 | Symbols(Allocator), UsedNames(Allocator), |
Bill Wendling | 1b10438 | 2019-08-09 20:16:31 +0000 | [diff] [blame] | 64 | InlineAsmUsedLabelNames(Allocator), |
Evgeniy Stepanov | 0338ce8 | 2017-02-24 21:44:52 +0000 | [diff] [blame] | 65 | CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 66 | AutoReset(DoAutoReset), TargetOptions(TargetOpts) { |
Igor Laevsky | 7b99885 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 67 | SecureLogFile = AsSecureLogFileName; |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 68 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 69 | if (SrcMgr && SrcMgr->getNumBuffers()) |
| 70 | MainFileName = |
| 71 | SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | MCContext::~MCContext() { |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 75 | if (AutoReset) |
| 76 | reset(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 77 | |
| 78 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
| 79 | // we don't need to free them here. |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // Module Lifetime Management |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 86 | void MCContext::reset() { |
Rafael Espindola | ed34d58 | 2015-05-26 01:52:19 +0000 | [diff] [blame] | 87 | // Call the destructors so the fragments are freed |
Rafael Espindola | 4264e2d | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 88 | COFFAllocator.DestroyAll(); |
| 89 | ELFAllocator.DestroyAll(); |
| 90 | MachOAllocator.DestroyAll(); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 91 | XCOFFAllocator.DestroyAll(); |
Rafael Espindola | ed34d58 | 2015-05-26 01:52:19 +0000 | [diff] [blame] | 92 | |
Akira Hatanaka | b11ef08 | 2015-11-14 06:35:56 +0000 | [diff] [blame] | 93 | MCSubtargetAllocator.DestroyAll(); |
Bill Wendling | 1b10438 | 2019-08-09 20:16:31 +0000 | [diff] [blame] | 94 | InlineAsmUsedLabelNames.clear(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 95 | UsedNames.clear(); |
| 96 | Symbols.clear(); |
| 97 | Allocator.Reset(); |
| 98 | Instances.clear(); |
Yaron Keren | 559b47d | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 99 | CompilationDir.clear(); |
| 100 | MainFileName.clear(); |
David Blaikie | d9012ba | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 101 | MCDwarfLineTablesCUMap.clear(); |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 102 | SectionsForRanges.clear(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 103 | MCGenDwarfLabelEntries.clear(); |
| 104 | DwarfDebugFlags = StringRef(); |
Pedro Artigas | 7ba2edc | 2013-02-20 00:10:29 +0000 | [diff] [blame] | 105 | DwarfCompileUnitID = 0; |
Jim Grosbach | 008359a | 2015-05-18 18:43:23 +0000 | [diff] [blame] | 106 | CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 107 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 108 | CVContext.reset(); |
| 109 | |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 110 | MachOUniquingMap.clear(); |
| 111 | ELFUniquingMap.clear(); |
| 112 | COFFUniquingMap.clear(); |
Sam Clegg | 105bdc2 | 2018-05-30 02:57:20 +0000 | [diff] [blame] | 113 | WasmUniquingMap.clear(); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 114 | XCOFFUniquingMap.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 115 | |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 116 | NextID.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 117 | AllowTemporaryLabels = true; |
| 118 | DwarfLocSeen = false; |
| 119 | GenDwarfForAssembly = false; |
| 120 | GenDwarfFileNumber = 0; |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 121 | |
| 122 | HadError = false; |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
| 126 | // Symbol Manipulation |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 129 | MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) { |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 130 | SmallString<128> NameSV; |
| 131 | StringRef NameRef = Name.toStringRef(NameSV); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 132 | |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 133 | assert(!NameRef.empty() && "Normal symbols cannot be unnamed!"); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 134 | |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 135 | MCSymbol *&Sym = Symbols[NameRef]; |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 136 | if (!Sym) |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 137 | Sym = createSymbol(NameRef, false, false); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 138 | |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 139 | return Sym; |
| 140 | } |
| 141 | |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 142 | MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName, |
| 143 | unsigned Idx) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 144 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + |
Reid Kleckner | cfb9ce5 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 145 | "$frame_escape_" + Twine(Idx)); |
Reid Kleckner | e9b8931 | 2015-01-13 00:48:10 +0000 | [diff] [blame] | 146 | } |
| 147 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 148 | MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 149 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 150 | "$parent_frame_offset"); |
| 151 | } |
| 152 | |
Reid Kleckner | 2632f0d | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 153 | MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) { |
| 154 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" + |
| 155 | FuncName); |
| 156 | } |
| 157 | |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 158 | MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name, |
| 159 | bool IsTemporary) { |
Pete Cooper | ad9f9c3 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 160 | if (MOFI) { |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 161 | switch (MOFI->getObjectFileType()) { |
| 162 | case MCObjectFileInfo::IsCOFF: |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 163 | return new (Name, *this) MCSymbolCOFF(Name, IsTemporary); |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 164 | case MCObjectFileInfo::IsELF: |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 165 | return new (Name, *this) MCSymbolELF(Name, IsTemporary); |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 166 | case MCObjectFileInfo::IsMachO: |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 167 | return new (Name, *this) MCSymbolMachO(Name, IsTemporary); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 168 | case MCObjectFileInfo::IsWasm: |
| 169 | return new (Name, *this) MCSymbolWasm(Name, IsTemporary); |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 170 | case MCObjectFileInfo::IsXCOFF: |
Jason Liu | 60ec248 | 2019-06-06 19:13:36 +0000 | [diff] [blame] | 171 | return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary); |
Pete Cooper | ad9f9c3 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 174 | return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name, |
| 175 | IsTemporary); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 178 | MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix, |
| 179 | bool CanBeUnnamed) { |
| 180 | if (CanBeUnnamed && !UseNamesOnTempLabels) |
| 181 | return createSymbolImpl(nullptr, true); |
| 182 | |
Eric Christopher | 8e94895 | 2016-09-29 02:03:44 +0000 | [diff] [blame] | 183 | // Determine whether this is a user written assembler temporary or normal |
Rafael Espindola | 3e9e72a | 2015-06-02 22:52:13 +0000 | [diff] [blame] | 184 | // label, if used. |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 185 | bool IsTemporary = CanBeUnnamed; |
| 186 | if (AllowTemporaryLabels && !IsTemporary) |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 187 | IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 188 | |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 189 | SmallString<128> NewName = Name; |
| 190 | bool AddSuffix = AlwaysAddSuffix; |
| 191 | unsigned &NextUniqueID = NextID[Name]; |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 192 | while (true) { |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 193 | if (AddSuffix) { |
Benjamin Kramer | 2b6c96b | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 194 | NewName.resize(Name.size()); |
| 195 | raw_svector_ostream(NewName) << NextUniqueID++; |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 196 | } |
| 197 | auto NameEntry = UsedNames.insert(std::make_pair(NewName, true)); |
Evgeniy Stepanov | a023f79 | 2016-03-28 20:36:28 +0000 | [diff] [blame] | 198 | if (NameEntry.second || !NameEntry.first->second) { |
| 199 | // Ok, we found a name. |
| 200 | // Mark it as used for a non-section symbol. |
| 201 | NameEntry.first->second = true; |
| 202 | // Have the MCSymbol object itself refer to the copy of the string that is |
| 203 | // embedded in the UsedNames entry. |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 204 | return createSymbolImpl(&*NameEntry.first, IsTemporary); |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 205 | } |
| 206 | assert(IsTemporary && "Cannot rename non-temporary symbols"); |
| 207 | AddSuffix = true; |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 208 | } |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 209 | llvm_unreachable("Infinite loop"); |
Chris Lattner | 3f5738d | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 212 | MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix, |
| 213 | bool CanBeUnnamed) { |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 214 | SmallString<128> NameSV; |
| 215 | raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name; |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 216 | return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed); |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 219 | MCSymbol *MCContext::createLinkerPrivateTempSymbol() { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 220 | SmallString<128> NameSV; |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 221 | raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp"; |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 222 | return createSymbol(NameSV, true, false); |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 225 | MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) { |
| 226 | return createTempSymbol("tmp", true, CanBeUnnamed); |
Chris Lattner | 073d817 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 229 | unsigned MCContext::NextInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 230 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 231 | if (!Label) |
| 232 | Label = new (*this) MCLabel(0); |
| 233 | return Label->incInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 236 | unsigned MCContext::GetInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 237 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 238 | if (!Label) |
| 239 | Label = new (*this) MCLabel(0); |
| 240 | return Label->getInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 243 | MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 244 | unsigned Instance) { |
| 245 | MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)]; |
| 246 | if (!Sym) |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 247 | Sym = createTempSymbol(false); |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 248 | return Sym; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 249 | } |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 250 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 251 | MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) { |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 252 | unsigned Instance = NextInstance(LocalLabelVal); |
| 253 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| 254 | } |
| 255 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 256 | MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal, |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 257 | bool Before) { |
| 258 | unsigned Instance = GetInstance(LocalLabelVal); |
| 259 | if (!Before) |
| 260 | ++Instance; |
| 261 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 264 | MCSymbol *MCContext::lookupSymbol(const Twine &Name) const { |
Roman Divacky | 0be3359 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 265 | SmallString<128> NameSV; |
Yaron Keren | d7c546c | 2015-03-17 18:55:30 +0000 | [diff] [blame] | 266 | StringRef NameRef = Name.toStringRef(NameSV); |
| 267 | return Symbols.lookup(NameRef); |
Roman Divacky | 0be3359 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Mandeep Singh Grang | 9a561aa | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 270 | void MCContext::setSymbolValue(MCStreamer &Streamer, |
| 271 | StringRef Sym, |
| 272 | uint64_t Val) { |
| 273 | auto Symbol = getOrCreateSymbol(Sym); |
| 274 | Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this)); |
Mandeep Singh Grang | 3236007 | 2016-12-01 18:42:04 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Bill Wendling | 1b10438 | 2019-08-09 20:16:31 +0000 | [diff] [blame] | 277 | void MCContext::registerInlineAsmLabel(MCSymbol *Sym) { |
| 278 | InlineAsmUsedLabelNames[Sym->getName()] = Sym; |
| 279 | } |
| 280 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | // Section Management |
| 283 | //===----------------------------------------------------------------------===// |
| 284 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 285 | MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section, |
| 286 | unsigned TypeAndAttributes, |
| 287 | unsigned Reserved2, SectionKind Kind, |
| 288 | const char *BeginSymName) { |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 289 | // We unique sections by their segment/section pair. The returned section |
| 290 | // may not have the same flags as the requested section, if so this should be |
| 291 | // diagnosed by the client as an error. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 293 | // Form the name to look up. |
| 294 | SmallString<64> Name; |
| 295 | Name += Segment; |
| 296 | Name.push_back(','); |
| 297 | Name += Section; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 299 | // Do the lookup, if we have a hit, return it. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 300 | MCSectionMachO *&Entry = MachOUniquingMap[Name]; |
Rafael Espindola | 6ed58a2 | 2015-03-10 21:16:18 +0000 | [diff] [blame] | 301 | if (Entry) |
| 302 | return Entry; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 303 | |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 304 | MCSymbol *Begin = nullptr; |
| 305 | if (BeginSymName) |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 306 | Begin = createTempSymbol(BeginSymName, false); |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 308 | // Otherwise, return a new section. |
Rafael Espindola | 4264e2d | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 309 | return Entry = new (MachOAllocator.Allocate()) MCSectionMachO( |
| 310 | Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin); |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 312 | |
Richard Smith | b910e56 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 313 | void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) { |
| 314 | StringRef GroupName; |
| 315 | if (const MCSymbol *Group = Section->getGroup()) |
| 316 | GroupName = Group->getName(); |
| 317 | |
| 318 | unsigned UniqueID = Section->getUniqueID(); |
| 319 | ELFUniquingMap.erase( |
| 320 | ELFSectionKey{Section->getSectionName(), GroupName, UniqueID}); |
| 321 | auto I = ELFUniquingMap.insert(std::make_pair( |
| 322 | ELFSectionKey{Name, GroupName, UniqueID}, |
| 323 | Section)) |
| 324 | .first; |
| 325 | StringRef CachedName = I->first.SectionName; |
| 326 | const_cast<MCSectionELF *>(Section)->setSectionName(CachedName); |
| 327 | } |
| 328 | |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 329 | MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type, |
| 330 | unsigned Flags, SectionKind K, |
| 331 | unsigned EntrySize, |
| 332 | const MCSymbolELF *Group, |
| 333 | unsigned UniqueID, |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 334 | const MCSymbolELF *Associated) { |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 335 | MCSymbolELF *R; |
| 336 | MCSymbol *&Sym = Symbols[Section]; |
Evgeniy Stepanov | 00400d3 | 2017-02-24 21:44:58 +0000 | [diff] [blame] | 337 | // A section symbol can not redefine regular symbols. There may be multiple |
| 338 | // sections with the same name, in which case the first such section wins. |
| 339 | if (Sym && Sym->isDefined() && |
| 340 | (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym)) |
| 341 | reportError(SMLoc(), "invalid symbol redefinition"); |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 342 | if (Sym && Sym->isUndefined()) { |
| 343 | R = cast<MCSymbolELF>(Sym); |
| 344 | } else { |
| 345 | auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first; |
| 346 | R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false); |
| 347 | if (!Sym) |
| 348 | Sym = R; |
| 349 | } |
| 350 | R->setBinding(ELF::STB_LOCAL); |
| 351 | R->setType(ELF::STT_SECTION); |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 352 | |
| 353 | auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF( |
| 354 | Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated); |
| 355 | |
| 356 | auto *F = new MCDataFragment(); |
| 357 | Ret->getFragmentList().insert(Ret->begin(), F); |
| 358 | F->setParent(Ret); |
| 359 | R->setFragment(F); |
| 360 | |
| 361 | return Ret; |
| 362 | } |
| 363 | |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 364 | MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type, |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 365 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 366 | const MCSymbolELF *Group, |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 367 | const MCSectionELF *RelInfoSection) { |
Rafael Espindola | c9d0692 | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 368 | StringMap<bool>::iterator I; |
| 369 | bool Inserted; |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 370 | std::tie(I, Inserted) = |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 371 | RelSecNames.insert(std::make_pair(Name.str(), true)); |
Rafael Espindola | c9d0692 | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 372 | |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 373 | return createELFSectionImpl( |
| 374 | I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group, |
| 375 | true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol())); |
Rafael Espindola | c9d0692 | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 378 | MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix, |
| 379 | const Twine &Suffix, unsigned Type, |
| 380 | unsigned Flags, |
| 381 | unsigned EntrySize) { |
| 382 | return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix); |
| 383 | } |
| 384 | |
| 385 | MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 386 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | dc1c301 | 2017-02-09 14:59:20 +0000 | [diff] [blame] | 387 | const Twine &Group, unsigned UniqueID, |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 388 | const MCSymbolELF *Associated) { |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 389 | MCSymbolELF *GroupSym = nullptr; |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 390 | if (!Group.isTriviallyEmpty() && !Group.str().empty()) |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 391 | GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group)); |
Rafael Espindola | d3ac79b | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 392 | |
Rafael Espindola | 61e8ce3 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 393 | return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID, |
Rafael Espindola | dc1c301 | 2017-02-09 14:59:20 +0000 | [diff] [blame] | 394 | Associated); |
Rafael Espindola | 61e8ce3 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 397 | MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 398 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 399 | const MCSymbolELF *GroupSym, |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 400 | unsigned UniqueID, |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 401 | const MCSymbolELF *Associated) { |
Rafael Espindola | 61e8ce3 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 402 | StringRef Group = ""; |
| 403 | if (GroupSym) |
| 404 | Group = GroupSym->getName(); |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 405 | // Do the lookup, if we have a hit, return it. |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 406 | auto IterBool = ELFUniquingMap.insert( |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 407 | std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr)); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 408 | auto &Entry = *IterBool.first; |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 409 | if (!IterBool.second) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 410 | return Entry.second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 411 | |
Rafael Espindola | 44d5057 | 2015-03-27 21:34:24 +0000 | [diff] [blame] | 412 | StringRef CachedName = Entry.first.SectionName; |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 413 | |
| 414 | SectionKind Kind; |
Prakhar Bahuguna | 52a7dd7 | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 415 | if (Flags & ELF::SHF_ARM_PURECODE) |
| 416 | Kind = SectionKind::getExecuteOnly(); |
| 417 | else if (Flags & ELF::SHF_EXECINSTR) |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 418 | Kind = SectionKind::getText(); |
| 419 | else |
| 420 | Kind = SectionKind::getReadOnly(); |
| 421 | |
George Rimar | 9fbecc9 | 2018-08-29 09:04:52 +0000 | [diff] [blame] | 422 | MCSectionELF *Result = createELFSectionImpl( |
| 423 | CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated); |
| 424 | Entry.second = Result; |
| 425 | return Result; |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 428 | MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) { |
Rafael Espindola | 13a79bb | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 429 | return createELFSectionImpl(".group", ELF::SHT_GROUP, 0, |
| 430 | SectionKind::getReadOnly(), 4, Group, ~0, |
| 431 | nullptr); |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 434 | MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
| 435 | unsigned Characteristics, |
| 436 | SectionKind Kind, |
| 437 | StringRef COMDATSymName, int Selection, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 438 | unsigned UniqueID, |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 439 | const char *BeginSymName) { |
Rafael Espindola | d3ac79b | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 440 | MCSymbol *COMDATSymbol = nullptr; |
| 441 | if (!COMDATSymName.empty()) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 442 | COMDATSymbol = getOrCreateSymbol(COMDATSymName); |
Rafael Espindola | d3ac79b | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 443 | COMDATSymName = COMDATSymbol->getName(); |
| 444 | } |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 445 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 446 | |
Rafael Espindola | d3ac79b | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 447 | // Do the lookup, if we have a hit, return it. |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 448 | COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID}; |
David Majnemer | c57d038 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 449 | auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr)); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 450 | auto Iter = IterBool.first; |
| 451 | if (!IterBool.second) |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 452 | return Iter->second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 453 | |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 454 | MCSymbol *Begin = nullptr; |
| 455 | if (BeginSymName) |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 456 | Begin = createTempSymbol(BeginSymName, false); |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 457 | |
Rafael Espindola | 44d5057 | 2015-03-27 21:34:24 +0000 | [diff] [blame] | 458 | StringRef CachedName = Iter->first.SectionName; |
Rafael Espindola | 4264e2d | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 459 | MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF( |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 460 | CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin); |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 461 | |
| 462 | Iter->second = Result; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 463 | return Result; |
| 464 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 465 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 466 | MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
| 467 | unsigned Characteristics, |
| 468 | SectionKind Kind, |
| 469 | const char *BeginSymName) { |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 470 | return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID, |
| 471 | BeginSymName); |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 474 | MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 475 | const MCSymbol *KeySym, |
| 476 | unsigned UniqueID) { |
| 477 | // Return the normal section if we don't have to be associative or unique. |
| 478 | if (!KeySym && UniqueID == GenericSectionID) |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 479 | return Sec; |
| 480 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 481 | // If we have a key symbol, make an associative section with the same name and |
| 482 | // kind as the normal section. |
| 483 | unsigned Characteristics = Sec->getCharacteristics(); |
| 484 | if (KeySym) { |
| 485 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 486 | return getCOFFSection(Sec->getSectionName(), Characteristics, |
| 487 | Sec->getKind(), KeySym->getName(), |
| 488 | COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); |
| 489 | } |
| 490 | |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 491 | return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(), |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 492 | "", 0, UniqueID); |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 495 | MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K, |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 496 | const Twine &Group, unsigned UniqueID, |
| 497 | const char *BeginSymName) { |
| 498 | MCSymbolWasm *GroupSym = nullptr; |
Sam Clegg | d423f0d | 2018-01-11 20:35:17 +0000 | [diff] [blame] | 499 | if (!Group.isTriviallyEmpty() && !Group.str().empty()) { |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 500 | GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group)); |
Sam Clegg | d423f0d | 2018-01-11 20:35:17 +0000 | [diff] [blame] | 501 | GroupSym->setComdat(true); |
| 502 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 503 | |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 504 | return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 507 | MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind, |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 508 | const MCSymbolWasm *GroupSym, |
| 509 | unsigned UniqueID, |
| 510 | const char *BeginSymName) { |
| 511 | StringRef Group = ""; |
| 512 | if (GroupSym) |
| 513 | Group = GroupSym->getName(); |
| 514 | // Do the lookup, if we have a hit, return it. |
| 515 | auto IterBool = WasmUniquingMap.insert( |
| 516 | std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr)); |
| 517 | auto &Entry = *IterBool.first; |
| 518 | if (!IterBool.second) |
| 519 | return Entry.second; |
| 520 | |
| 521 | StringRef CachedName = Entry.first.SectionName; |
| 522 | |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 523 | MCSymbol *Begin = createSymbol(CachedName, false, false); |
| 524 | cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 525 | |
| 526 | MCSectionWasm *Result = new (WasmAllocator.Allocate()) |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 527 | MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 528 | Entry.second = Result; |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 529 | |
| 530 | auto *F = new MCDataFragment(); |
| 531 | Result->getFragmentList().insert(Result->begin(), F); |
| 532 | F->setParent(Result); |
| 533 | Begin->setFragment(F); |
| 534 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 535 | return Result; |
| 536 | } |
| 537 | |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 538 | MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section, |
| 539 | XCOFF::StorageMappingClass SMC, |
Sean Fertile | 942537d | 2019-07-22 19:15:29 +0000 | [diff] [blame] | 540 | XCOFF::SymbolType Type, |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 541 | XCOFF::StorageClass SC, |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 542 | SectionKind Kind, |
| 543 | const char *BeginSymName) { |
| 544 | // Do the lookup. If we have a hit, return it. |
| 545 | auto IterBool = XCOFFUniquingMap.insert( |
| 546 | std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr)); |
| 547 | auto &Entry = *IterBool.first; |
| 548 | if (!IterBool.second) |
| 549 | return Entry.second; |
| 550 | |
| 551 | // Otherwise, return a new section. |
| 552 | StringRef CachedName = Entry.first.SectionName; |
| 553 | |
| 554 | MCSymbol *Begin = nullptr; |
| 555 | if (BeginSymName) |
| 556 | Begin = createTempSymbol(BeginSymName, false); |
| 557 | |
| 558 | MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate()) |
Sean Fertile | 1e46d4c | 2019-08-20 22:03:18 +0000 | [diff] [blame] | 559 | MCSectionXCOFF(CachedName, SMC, Type, SC, Kind, Begin); |
Sean Fertile | f09d54e | 2019-07-09 19:21:01 +0000 | [diff] [blame] | 560 | Entry.second = Result; |
| 561 | |
| 562 | auto *F = new MCDataFragment(); |
| 563 | Result->getFragmentList().insert(Result->begin(), F); |
| 564 | F->setParent(Result); |
| 565 | |
| 566 | if (Begin) |
| 567 | Begin->setFragment(F); |
| 568 | |
| 569 | return Result; |
| 570 | } |
| 571 | |
Akira Hatanaka | b11ef08 | 2015-11-14 06:35:56 +0000 | [diff] [blame] | 572 | MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) { |
| 573 | return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI); |
| 574 | } |
| 575 | |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 576 | void MCContext::addDebugPrefixMapEntry(const std::string &From, |
| 577 | const std::string &To) { |
| 578 | DebugPrefixMap.insert(std::make_pair(From, To)); |
| 579 | } |
| 580 | |
Jonas Devlieghere | 26ddf27 | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 581 | void MCContext::RemapDebugPaths() { |
| 582 | const auto &DebugPrefixMap = this->DebugPrefixMap; |
| 583 | const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) { |
| 584 | for (const auto &Entry : DebugPrefixMap) |
| 585 | if (StringRef(Path).startswith(Entry.first)) { |
| 586 | std::string RemappedPath = |
| 587 | (Twine(Entry.second) + Path.substr(Entry.first.size())).str(); |
| 588 | Path.swap(RemappedPath); |
| 589 | } |
| 590 | }; |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 591 | |
Jonas Devlieghere | 26ddf27 | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 592 | // Remap compilation directory. |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 593 | std::string CompDir = CompilationDir.str(); |
Jonas Devlieghere | 26ddf27 | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 594 | RemapDebugPath(CompDir); |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 595 | CompilationDir = CompDir; |
Jonas Devlieghere | 26ddf27 | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 596 | |
| 597 | // Remap MCDwarfDirs in all compilation units. |
| 598 | for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) |
| 599 | for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) |
| 600 | RemapDebugPath(Dir); |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 603 | //===----------------------------------------------------------------------===// |
| 604 | // Dwarf Management |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 607 | void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) { |
| 608 | // MCDwarf needs the root file as well as the compilation directory. |
| 609 | // If we find a '.file 0' directive that will supersede these values. |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 610 | Optional<MD5::MD5Result> Cksum; |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 611 | if (getDwarfVersion() >= 5) { |
| 612 | MD5 Hash; |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 613 | MD5::MD5Result Sum; |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 614 | Hash.update(Buffer); |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 615 | Hash.final(Sum); |
| 616 | Cksum = Sum; |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 617 | } |
| 618 | // Canonicalize the root filename. It cannot be empty, and should not |
| 619 | // repeat the compilation dir. |
Paul Robinson | 116e8d4 | 2019-05-21 11:52:27 +0000 | [diff] [blame] | 620 | // The MCContext ctor initializes MainFileName to the name associated with |
| 621 | // the SrcMgr's main file ID, which might be the same as InputFileName (and |
| 622 | // possibly include directory components). |
| 623 | // Or, MainFileName might have been overridden by a -main-file-name option, |
| 624 | // which is supposed to be just a base filename with no directory component. |
| 625 | // So, if the InputFileName and MainFileName are not equal, assume |
| 626 | // MainFileName is a substitute basename and replace the last component. |
| 627 | SmallString<1024> FileNameBuf = InputFileName; |
| 628 | if (FileNameBuf.empty() || FileNameBuf == "-") |
| 629 | FileNameBuf = "<stdin>"; |
| 630 | if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) { |
| 631 | llvm::sys::path::remove_filename(FileNameBuf); |
| 632 | llvm::sys::path::append(FileNameBuf, getMainFileName()); |
| 633 | } |
| 634 | StringRef FileName = FileNameBuf; |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 635 | if (FileName.consume_front(getCompilationDir())) |
Paul Robinson | d8632c9 | 2019-03-01 22:28:13 +0000 | [diff] [blame] | 636 | if (llvm::sys::path::is_separator(FileName.front())) |
| 637 | FileName = FileName.drop_front(); |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 638 | assert(!FileName.empty()); |
| 639 | setMCLineTableRootFile( |
| 640 | /*CUID=*/0, getCompilationDir(), FileName, Cksum, None); |
| 641 | } |
| 642 | |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 643 | /// getDwarfFile - takes a file name and number to place in the dwarf file and |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 644 | /// directory tables. If the file number has already been allocated it is an |
| 645 | /// error and zero is returned and the client reports the error, else the |
| 646 | /// allocated file number is returned. The file numbers may be in any order. |
Paul Robinson | 70def12 | 2018-02-22 21:03:33 +0000 | [diff] [blame] | 647 | Expected<unsigned> MCContext::getDwarfFile(StringRef Directory, |
| 648 | StringRef FileName, |
| 649 | unsigned FileNumber, |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 650 | Optional<MD5::MD5Result> Checksum, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 651 | Optional<StringRef> Source, |
Paul Robinson | 70def12 | 2018-02-22 21:03:33 +0000 | [diff] [blame] | 652 | unsigned CUID) { |
Ali Tamur | 02e9664 | 2019-03-26 20:05:27 +0000 | [diff] [blame] | 653 | MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; |
Ali Tamur | 783d84b | 2019-04-19 02:26:56 +0000 | [diff] [blame] | 654 | return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion, |
| 655 | FileNumber); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 656 | } |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 657 | |
Kevin Enderby | a68d004 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 658 | /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 659 | /// currently is assigned and false otherwise. |
Manman Ren | 1e42720 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 660 | bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { |
Paul Robinson | 11539b0 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 661 | const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID); |
| 662 | if (FileNumber == 0) |
Ali Tamur | 783d84b | 2019-04-19 02:26:56 +0000 | [diff] [blame] | 663 | return getDwarfVersion() >= 5; |
Paul Robinson | 11539b0 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 664 | if (FileNumber >= LineTable.getMCDwarfFiles().size()) |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 665 | return false; |
| 666 | |
Paul Robinson | 11539b0 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 667 | return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty(); |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 668 | } |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 669 | |
Fangrui Song | 58963e4 | 2018-09-08 02:04:20 +0000 | [diff] [blame] | 670 | /// Remove empty sections from SectionsForRanges, to avoid generating |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 671 | /// useless debug info for them. |
| 672 | void MCContext::finalizeDwarfSections(MCStreamer &MCOS) { |
Benjamin Kramer | 412c4db | 2015-05-31 18:49:28 +0000 | [diff] [blame] | 673 | SectionsForRanges.remove_if( |
| 674 | [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); }); |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 677 | CodeViewContext &MCContext::getCVContext() { |
| 678 | if (!CVContext.get()) |
| 679 | CVContext.reset(new CodeViewContext); |
| 680 | return *CVContext.get(); |
| 681 | } |
| 682 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 683 | //===----------------------------------------------------------------------===// |
| 684 | // Error Reporting |
| 685 | //===----------------------------------------------------------------------===// |
| 686 | |
| 687 | void MCContext::reportError(SMLoc Loc, const Twine &Msg) { |
| 688 | HadError = true; |
| 689 | |
Sanne Wouda | 2933875 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 690 | // If we have a source manager use it. Otherwise, try using the inline source |
| 691 | // manager. |
| 692 | // If that fails, use the generic report_fatal_error(). |
| 693 | if (SrcMgr) |
| 694 | SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); |
| 695 | else if (InlineSrcMgr) |
| 696 | InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); |
| 697 | else |
Jim Grosbach | 9a3284f | 2014-03-14 22:41:58 +0000 | [diff] [blame] | 698 | report_fatal_error(Msg, false); |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 701 | void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) { |
| 702 | if (TargetOptions && TargetOptions->MCNoWarn) |
| 703 | return; |
| 704 | if (TargetOptions && TargetOptions->MCFatalWarnings) |
| 705 | reportError(Loc, Msg); |
| 706 | else { |
| 707 | // If we have a source manager use it. Otherwise, try using the inline |
| 708 | // source manager. |
| 709 | if (SrcMgr) |
| 710 | SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg); |
| 711 | else if (InlineSrcMgr) |
| 712 | InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg); |
| 713 | } |
| 714 | } |
| 715 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 716 | void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) { |
| 717 | reportError(Loc, Msg); |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 718 | |
| 719 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 720 | // to make sure any special cleanups get done, in particular that we remove |
| 721 | // files registered with RemoveFileOnSignal. |
| 722 | sys::RunInterruptHandlers(); |
| 723 | exit(1); |
| 724 | } |