Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===// |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 2 | // |
| 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 Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCContext.h" |
Scott Linder | 5e4b515 | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Optional.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/COFF.h" |
| 18 | #include "llvm/BinaryFormat/ELF.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.h" |
Reid Kleckner | 1689efb | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCCodeView.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDwarf.h" |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" |
| 23 | #include "llvm/MC/MCFragment.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCLabel.h" |
| 25 | #include "llvm/MC/MCObjectFileInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSectionCOFF.h" |
| 27 | #include "llvm/MC/MCSectionELF.h" |
| 28 | #include "llvm/MC/MCSectionMachO.h" |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSectionWasm.h" |
Pete Cooper | 05be9b1 | 2015-03-04 01:24:11 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSymbol.h" |
Pete Cooper | f560b88 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCSymbolCOFF.h" |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbolELF.h" |
Pete Cooper | cae8bc4 | 2015-06-08 17:17:23 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSymbolMachO.h" |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCSymbolWasm.h" |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 36 | #include "llvm/MC/SectionKind.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Casting.h" |
Igor Laevsky | afaddb4 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Signals.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 44 | #include <cassert> |
| 45 | #include <cstdlib> |
| 46 | #include <tuple> |
| 47 | #include <utility> |
David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 48 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Igor Laevsky | afaddb4 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 51 | static cl::opt<char*> |
| 52 | AsSecureLogFileName("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 Wendling | 99cb622 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 57 | MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 58 | const MCObjectFileInfo *mofi, const SourceMgr *mgr, |
David Blaikie | 0a0c4cc | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 59 | bool DoAutoReset) |
Evgeniy Stepanov | 5982b2c | 2017-02-24 21:44:52 +0000 | [diff] [blame] | 60 | : 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 Friedman | c206978 | 2018-10-12 19:41:05 +0000 | [diff] [blame] | 63 | AutoReset(DoAutoReset) { |
Igor Laevsky | afaddb4 | 2016-06-17 15:19:41 +0000 | [diff] [blame] | 64 | SecureLogFile = AsSecureLogFileName; |
Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 65 | |
Alp Toker | 1508c82 | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 66 | if (SrcMgr && SrcMgr->getNumBuffers()) |
| 67 | MainFileName = |
| 68 | SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier(); |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | MCContext::~MCContext() { |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 72 | if (AutoReset) |
| 73 | reset(); |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 74 | |
| 75 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
| 76 | // we don't need to free them here. |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | // Module Lifetime Management |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 83 | void MCContext::reset() { |
Rafael Espindola | eac71e2 | 2015-05-26 01:52:19 +0000 | [diff] [blame] | 84 | // Call the destructors so the fragments are freed |
Rafael Espindola | d546ca6 | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 85 | COFFAllocator.DestroyAll(); |
| 86 | ELFAllocator.DestroyAll(); |
| 87 | MachOAllocator.DestroyAll(); |
Rafael Espindola | eac71e2 | 2015-05-26 01:52:19 +0000 | [diff] [blame] | 88 | |
Akira Hatanaka | 55c0268 | 2015-11-14 06:35:56 +0000 | [diff] [blame] | 89 | MCSubtargetAllocator.DestroyAll(); |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 90 | UsedNames.clear(); |
| 91 | Symbols.clear(); |
| 92 | Allocator.Reset(); |
| 93 | Instances.clear(); |
Yaron Keren | c63035a | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 94 | CompilationDir.clear(); |
| 95 | MainFileName.clear(); |
David Blaikie | 43d5e07 | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 96 | MCDwarfLineTablesCUMap.clear(); |
Rafael Espindola | 43356a1 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 97 | SectionsForRanges.clear(); |
Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 98 | MCGenDwarfLabelEntries.clear(); |
| 99 | DwarfDebugFlags = StringRef(); |
Pedro Artigas | 9e7924d | 2013-02-20 00:10:29 +0000 | [diff] [blame] | 100 | DwarfCompileUnitID = 0; |
Jim Grosbach | 7b81f73 | 2015-05-18 18:43:23 +0000 | [diff] [blame] | 101 | CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0); |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 102 | |
Reid Kleckner | 1689efb | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 103 | CVContext.reset(); |
| 104 | |
David Blaikie | 42382d3 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 105 | MachOUniquingMap.clear(); |
| 106 | ELFUniquingMap.clear(); |
| 107 | COFFUniquingMap.clear(); |
Sam Clegg | 1838ee9 | 2018-05-30 02:57:20 +0000 | [diff] [blame] | 108 | WasmUniquingMap.clear(); |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 109 | |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 110 | NextID.clear(); |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 111 | AllowTemporaryLabels = true; |
| 112 | DwarfLocSeen = false; |
| 113 | GenDwarfForAssembly = false; |
| 114 | GenDwarfFileNumber = 0; |
Oliver Stannard | 43f1dd7 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 115 | |
| 116 | HadError = false; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
| 120 | // Symbol Manipulation |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 123 | MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) { |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 124 | SmallString<128> NameSV; |
| 125 | StringRef NameRef = Name.toStringRef(NameSV); |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 126 | |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 127 | assert(!NameRef.empty() && "Normal symbols cannot be unnamed!"); |
Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 128 | |
Yaron Keren | 0401f79 | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 129 | MCSymbol *&Sym = Symbols[NameRef]; |
David Blaikie | 1d4f28c | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 130 | if (!Sym) |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 131 | Sym = createSymbol(NameRef, false, false); |
Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 132 | |
Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 133 | return Sym; |
| 134 | } |
| 135 | |
Reid Kleckner | 9f7c861 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 136 | MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName, |
| 137 | unsigned Idx) { |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 138 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + |
Reid Kleckner | 9f7c861 | 2015-03-05 18:26:34 +0000 | [diff] [blame] | 139 | "$frame_escape_" + Twine(Idx)); |
Reid Kleckner | 221a707 | 2015-01-13 00:48:10 +0000 | [diff] [blame] | 140 | } |
| 141 | |
David Majnemer | 6438662 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 142 | MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) { |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 143 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + |
David Majnemer | 6438662 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 144 | "$parent_frame_offset"); |
| 145 | } |
| 146 | |
Reid Kleckner | 7681f6a | 2015-05-20 23:08:04 +0000 | [diff] [blame] | 147 | MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) { |
| 148 | return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" + |
| 149 | FuncName); |
| 150 | } |
| 151 | |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 152 | MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name, |
| 153 | bool IsTemporary) { |
Pete Cooper | f560b88 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 154 | if (MOFI) { |
Rafael Espindola | ea619d0 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 155 | switch (MOFI->getObjectFileType()) { |
| 156 | case MCObjectFileInfo::IsCOFF: |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 157 | return new (Name, *this) MCSymbolCOFF(Name, IsTemporary); |
Rafael Espindola | ea619d0 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 158 | case MCObjectFileInfo::IsELF: |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 159 | return new (Name, *this) MCSymbolELF(Name, IsTemporary); |
Rafael Espindola | ea619d0 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 160 | case MCObjectFileInfo::IsMachO: |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 161 | return new (Name, *this) MCSymbolMachO(Name, IsTemporary); |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 162 | case MCObjectFileInfo::IsWasm: |
| 163 | return new (Name, *this) MCSymbolWasm(Name, IsTemporary); |
Pete Cooper | f560b88 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 166 | return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name, |
| 167 | IsTemporary); |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 170 | MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix, |
| 171 | bool CanBeUnnamed) { |
| 172 | if (CanBeUnnamed && !UseNamesOnTempLabels) |
| 173 | return createSymbolImpl(nullptr, true); |
| 174 | |
Eric Christopher | 32b5cf7 | 2016-09-29 02:03:44 +0000 | [diff] [blame] | 175 | // Determine whether this is a user written assembler temporary or normal |
Rafael Espindola | 0ba7ba4 | 2015-06-02 22:52:13 +0000 | [diff] [blame] | 176 | // label, if used. |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 177 | bool IsTemporary = CanBeUnnamed; |
| 178 | if (AllowTemporaryLabels && !IsTemporary) |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 179 | IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 180 | |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 181 | SmallString<128> NewName = Name; |
| 182 | bool AddSuffix = AlwaysAddSuffix; |
| 183 | unsigned &NextUniqueID = NextID[Name]; |
Eugene Zelenko | 7211c53 | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 184 | while (true) { |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 185 | if (AddSuffix) { |
Benjamin Kramer | c18214a | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 186 | NewName.resize(Name.size()); |
| 187 | raw_svector_ostream(NewName) << NextUniqueID++; |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 188 | } |
| 189 | auto NameEntry = UsedNames.insert(std::make_pair(NewName, true)); |
Evgeniy Stepanov | 3828f99 | 2016-03-28 20:36:28 +0000 | [diff] [blame] | 190 | 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 Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 196 | return createSymbolImpl(&*NameEntry.first, IsTemporary); |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 197 | } |
| 198 | assert(IsTemporary && "Cannot rename non-temporary symbols"); |
| 199 | AddSuffix = true; |
Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 200 | } |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 201 | llvm_unreachable("Infinite loop"); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 204 | MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix, |
| 205 | bool CanBeUnnamed) { |
Rafael Espindola | 150233c | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 206 | SmallString<128> NameSV; |
| 207 | raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name; |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 208 | return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed); |
Rafael Espindola | 150233c | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 211 | MCSymbol *MCContext::createLinkerPrivateTempSymbol() { |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 212 | SmallString<128> NameSV; |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 213 | raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp"; |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 214 | return createSymbol(NameSV, true, false); |
Tim Northover | c5d592d | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 217 | MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) { |
| 218 | return createTempSymbol("tmp", true, CanBeUnnamed); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 221 | unsigned MCContext::NextInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 222 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 223 | if (!Label) |
| 224 | Label = new (*this) MCLabel(0); |
| 225 | return Label->incInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 228 | unsigned MCContext::GetInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 229 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 230 | if (!Label) |
| 231 | Label = new (*this) MCLabel(0); |
| 232 | return Label->getInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 235 | MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 236 | unsigned Instance) { |
| 237 | MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)]; |
| 238 | if (!Sym) |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 239 | Sym = createTempSymbol(false); |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 240 | return Sym; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 241 | } |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 242 | |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 243 | MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) { |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 244 | unsigned Instance = NextInstance(LocalLabelVal); |
| 245 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| 246 | } |
| 247 | |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 248 | MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal, |
Rafael Espindola | 7e9df19 | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 249 | bool Before) { |
| 250 | unsigned Instance = GetInstance(LocalLabelVal); |
| 251 | if (!Before) |
| 252 | ++Instance; |
| 253 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 256 | MCSymbol *MCContext::lookupSymbol(const Twine &Name) const { |
Roman Divacky | f145c13 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 257 | SmallString<128> NameSV; |
Yaron Keren | 6279aae | 2015-03-17 18:55:30 +0000 | [diff] [blame] | 258 | StringRef NameRef = Name.toStringRef(NameSV); |
| 259 | return Symbols.lookup(NameRef); |
Roman Divacky | f145c13 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Mandeep Singh Grang | 9196899 | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 262 | void 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 Grang | 017b7af | 2016-12-01 18:42:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 269 | //===----------------------------------------------------------------------===// |
| 270 | // Section Management |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 273 | MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section, |
| 274 | unsigned TypeAndAttributes, |
| 275 | unsigned Reserved2, SectionKind Kind, |
| 276 | const char *BeginSymName) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 277 | // 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. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 280 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 281 | // Form the name to look up. |
| 282 | SmallString<64> Name; |
| 283 | Name += Segment; |
| 284 | Name.push_back(','); |
| 285 | Name += Section; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 286 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 287 | // Do the lookup, if we have a hit, return it. |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 288 | MCSectionMachO *&Entry = MachOUniquingMap[Name]; |
Rafael Espindola | 5c4720d | 2015-03-10 21:16:18 +0000 | [diff] [blame] | 289 | if (Entry) |
| 290 | return Entry; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 291 | |
Rafael Espindola | f3d745c | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 292 | MCSymbol *Begin = nullptr; |
| 293 | if (BeginSymName) |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 294 | Begin = createTempSymbol(BeginSymName, false); |
Rafael Espindola | f3d745c | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 295 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 296 | // Otherwise, return a new section. |
Rafael Espindola | d546ca6 | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 297 | return Entry = new (MachOAllocator.Allocate()) MCSectionMachO( |
| 298 | Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin); |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 299 | } |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 300 | |
Richard Smith | e89c000 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 301 | void 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 Espindola | 7931a61 | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 317 | MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type, |
| 318 | unsigned Flags, SectionKind K, |
| 319 | unsigned EntrySize, |
| 320 | const MCSymbolELF *Group, |
| 321 | unsigned UniqueID, |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 322 | const MCSymbolELF *Associated) { |
Rafael Espindola | 7931a61 | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 323 | MCSymbolELF *R; |
| 324 | MCSymbol *&Sym = Symbols[Section]; |
Evgeniy Stepanov | 89efd71 | 2017-02-24 21:44:58 +0000 | [diff] [blame] | 325 | // 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 Espindola | 7931a61 | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 330 | 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 Espindola | 7931a61 | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 340 | |
| 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 Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 352 | MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type, |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 353 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 354 | const MCSymbolELF *Group, |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 355 | const MCSectionELF *RelInfoSection) { |
Rafael Espindola | 0bd11cd | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 356 | StringMap<bool>::iterator I; |
| 357 | bool Inserted; |
Eric Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 358 | std::tie(I, Inserted) = |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 359 | RelSecNames.insert(std::make_pair(Name.str(), true)); |
Rafael Espindola | 0bd11cd | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 360 | |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 361 | return createELFSectionImpl( |
| 362 | I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group, |
| 363 | true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol())); |
Rafael Espindola | 0bd11cd | 2015-03-30 13:39:16 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Eric Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 366 | MCSectionELF *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 | |
| 373 | MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 374 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | 5a2d92c | 2017-02-09 14:59:20 +0000 | [diff] [blame] | 375 | const Twine &Group, unsigned UniqueID, |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 376 | const MCSymbolELF *Associated) { |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 377 | MCSymbolELF *GroupSym = nullptr; |
Eric Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 378 | if (!Group.isTriviallyEmpty() && !Group.str().empty()) |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 379 | GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group)); |
Rafael Espindola | fd45ac4 | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 380 | |
Rafael Espindola | 83be442 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 381 | return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID, |
Rafael Espindola | 5a2d92c | 2017-02-09 14:59:20 +0000 | [diff] [blame] | 382 | Associated); |
Rafael Espindola | 83be442 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Eric Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 385 | MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 386 | unsigned Flags, unsigned EntrySize, |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 387 | const MCSymbolELF *GroupSym, |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 388 | unsigned UniqueID, |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 389 | const MCSymbolELF *Associated) { |
Rafael Espindola | 83be442 | 2015-04-06 04:25:18 +0000 | [diff] [blame] | 390 | StringRef Group = ""; |
| 391 | if (GroupSym) |
| 392 | Group = GroupSym->getName(); |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 393 | // Do the lookup, if we have a hit, return it. |
David Blaikie | 42382d3 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 394 | auto IterBool = ELFUniquingMap.insert( |
Eric Christopher | dea5ff1 | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 395 | std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr)); |
David Blaikie | 42382d3 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 396 | auto &Entry = *IterBool.first; |
Rafael Espindola | 903f4a2 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 397 | if (!IterBool.second) |
Rafael Espindola | e0a2541 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 398 | return Entry.second; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | 0ac7591 | 2015-03-27 21:34:24 +0000 | [diff] [blame] | 400 | StringRef CachedName = Entry.first.SectionName; |
Rafael Espindola | 9936b80 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 401 | |
| 402 | SectionKind Kind; |
Prakhar Bahuguna | af672fb | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 403 | if (Flags & ELF::SHF_ARM_PURECODE) |
| 404 | Kind = SectionKind::getExecuteOnly(); |
| 405 | else if (Flags & ELF::SHF_EXECINSTR) |
Rafael Espindola | 9936b80 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 406 | Kind = SectionKind::getText(); |
| 407 | else |
| 408 | Kind = SectionKind::getReadOnly(); |
| 409 | |
George Rimar | 2a751e1 | 2018-08-29 09:04:52 +0000 | [diff] [blame] | 410 | MCSectionELF *Result = createELFSectionImpl( |
| 411 | CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated); |
| 412 | Entry.second = Result; |
| 413 | return Result; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 416 | MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) { |
Rafael Espindola | 7931a61 | 2017-02-02 21:26:06 +0000 | [diff] [blame] | 417 | return createELFSectionImpl(".group", ELF::SHT_GROUP, 0, |
| 418 | SectionKind::getReadOnly(), 4, Group, ~0, |
| 419 | nullptr); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 422 | MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
| 423 | unsigned Characteristics, |
| 424 | SectionKind Kind, |
| 425 | StringRef COMDATSymName, int Selection, |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 426 | unsigned UniqueID, |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 427 | const char *BeginSymName) { |
Rafael Espindola | fd45ac4 | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 428 | MCSymbol *COMDATSymbol = nullptr; |
| 429 | if (!COMDATSymName.empty()) { |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 430 | COMDATSymbol = getOrCreateSymbol(COMDATSymName); |
Rafael Espindola | fd45ac4 | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 431 | COMDATSymName = COMDATSymbol->getName(); |
| 432 | } |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 433 | |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 434 | |
Rafael Espindola | fd45ac4 | 2015-03-30 13:59:06 +0000 | [diff] [blame] | 435 | // Do the lookup, if we have a hit, return it. |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 436 | COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID}; |
David Majnemer | d7d4277 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 437 | auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr)); |
David Blaikie | 42382d3 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 438 | auto Iter = IterBool.first; |
| 439 | if (!IterBool.second) |
Rafael Espindola | 72935dd | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 440 | return Iter->second; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 441 | |
Rafael Espindola | f3d745c | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 442 | MCSymbol *Begin = nullptr; |
| 443 | if (BeginSymName) |
Rafael Espindola | b415e6b | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 444 | Begin = createTempSymbol(BeginSymName, false); |
Rafael Espindola | f3d745c | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 445 | |
Rafael Espindola | 0ac7591 | 2015-03-27 21:34:24 +0000 | [diff] [blame] | 446 | StringRef CachedName = Iter->first.SectionName; |
Rafael Espindola | d546ca6 | 2015-10-07 19:08:19 +0000 | [diff] [blame] | 447 | MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF( |
Rafael Espindola | f3d745c | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 448 | CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin); |
Rafael Espindola | 72935dd | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 449 | |
| 450 | Iter->second = Result; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 451 | return Result; |
| 452 | } |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 453 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 454 | MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
| 455 | unsigned Characteristics, |
| 456 | SectionKind Kind, |
| 457 | const char *BeginSymName) { |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 458 | return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID, |
| 459 | BeginSymName); |
Rafael Espindola | 72935dd | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 462 | MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) { |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 463 | COFFSectionKey T{Section, "", 0, GenericSectionID}; |
David Majnemer | d7d4277 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 464 | auto Iter = COFFUniquingMap.find(T); |
David Blaikie | 42382d3 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 465 | if (Iter == COFFUniquingMap.end()) |
Craig Topper | 4266ae8 | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 466 | return nullptr; |
Rafael Espindola | 72935dd | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 467 | return Iter->second; |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 470 | MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec, |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 471 | 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 Kleckner | b9cb76d | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 475 | return Sec; |
| 476 | |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 477 | // 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 Kleckner | b9cb76d | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 487 | return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(), |
Reid Kleckner | 7f90dd2 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 488 | "", 0, UniqueID); |
Reid Kleckner | b9cb76d | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Sam Clegg | 2b6b6ac | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 491 | MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K, |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 492 | const Twine &Group, unsigned UniqueID, |
| 493 | const char *BeginSymName) { |
| 494 | MCSymbolWasm *GroupSym = nullptr; |
Sam Clegg | 2c77fcc | 2018-01-11 20:35:17 +0000 | [diff] [blame] | 495 | if (!Group.isTriviallyEmpty() && !Group.str().empty()) { |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 496 | GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group)); |
Sam Clegg | 2c77fcc | 2018-01-11 20:35:17 +0000 | [diff] [blame] | 497 | GroupSym->setComdat(true); |
| 498 | } |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 499 | |
Sam Clegg | 2b6b6ac | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 500 | return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName); |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Sam Clegg | 2b6b6ac | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 503 | MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind, |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 504 | 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 Clegg | 70b76a1 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 519 | MCSymbol *Begin = createSymbol(CachedName, false, false); |
| 520 | cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 521 | |
| 522 | MCSectionWasm *Result = new (WasmAllocator.Allocate()) |
Sam Clegg | 2b6b6ac | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 523 | MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin); |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 524 | Entry.second = Result; |
Sam Clegg | 70b76a1 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 525 | |
| 526 | auto *F = new MCDataFragment(); |
| 527 | Result->getFragmentList().insert(Result->begin(), F); |
| 528 | F->setParent(Result); |
| 529 | Begin->setFragment(F); |
| 530 | |
Dan Gohman | d660a5d | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 531 | return Result; |
| 532 | } |
| 533 | |
Akira Hatanaka | 55c0268 | 2015-11-14 06:35:56 +0000 | [diff] [blame] | 534 | MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) { |
| 535 | return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI); |
| 536 | } |
| 537 | |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 538 | void MCContext::addDebugPrefixMapEntry(const std::string &From, |
| 539 | const std::string &To) { |
| 540 | DebugPrefixMap.insert(std::make_pair(From, To)); |
| 541 | } |
| 542 | |
Jonas Devlieghere | f0c06bd | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 543 | void 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 Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 553 | |
Jonas Devlieghere | f0c06bd | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 554 | // Remap compilation directory. |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 555 | std::string CompDir = CompilationDir.str(); |
Jonas Devlieghere | f0c06bd | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 556 | RemapDebugPath(CompDir); |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 557 | CompilationDir = CompDir; |
Jonas Devlieghere | f0c06bd | 2018-07-11 12:30:35 +0000 | [diff] [blame] | 558 | |
| 559 | // Remap MCDwarfDirs in all compilation units. |
| 560 | for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) |
| 561 | for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) |
| 562 | RemapDebugPath(Dir); |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 565 | //===----------------------------------------------------------------------===// |
| 566 | // Dwarf Management |
| 567 | //===----------------------------------------------------------------------===// |
| 568 | |
Scott Linder | 5e4b515 | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 569 | /// getDwarfFile - takes a file name and number to place in the dwarf file and |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 570 | /// 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 Robinson | 1dff32f | 2018-02-22 21:03:33 +0000 | [diff] [blame] | 573 | Expected<unsigned> MCContext::getDwarfFile(StringRef Directory, |
| 574 | StringRef FileName, |
| 575 | unsigned FileNumber, |
| 576 | MD5::MD5Result *Checksum, |
Scott Linder | 5e4b515 | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 577 | Optional<StringRef> Source, |
Paul Robinson | 1dff32f | 2018-02-22 21:03:33 +0000 | [diff] [blame] | 578 | unsigned CUID) { |
David Blaikie | 43d5e07 | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 579 | MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; |
Scott Linder | 5e4b515 | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 580 | return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 581 | } |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 582 | |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 583 | /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 584 | /// currently is assigned and false otherwise. |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 585 | bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { |
Paul Robinson | fe16001 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 586 | const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID); |
| 587 | if (FileNumber == 0) |
| 588 | return getDwarfVersion() >= 5 && LineTable.hasRootFile(); |
| 589 | if (FileNumber >= LineTable.getMCDwarfFiles().size()) |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 590 | return false; |
| 591 | |
Paul Robinson | fe16001 | 2018-06-22 14:16:11 +0000 | [diff] [blame] | 592 | return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty(); |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 593 | } |
Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 594 | |
Fangrui Song | 35bfd59 | 2018-09-08 02:04:20 +0000 | [diff] [blame] | 595 | /// Remove empty sections from SectionsForRanges, to avoid generating |
Oliver Stannard | 591f9ee | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 596 | /// useless debug info for them. |
| 597 | void MCContext::finalizeDwarfSections(MCStreamer &MCOS) { |
Benjamin Kramer | d79915c | 2015-05-31 18:49:28 +0000 | [diff] [blame] | 598 | SectionsForRanges.remove_if( |
| 599 | [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); }); |
Oliver Stannard | 591f9ee | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Reid Kleckner | 1689efb | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 602 | CodeViewContext &MCContext::getCVContext() { |
| 603 | if (!CVContext.get()) |
| 604 | CVContext.reset(new CodeViewContext); |
| 605 | return *CVContext.get(); |
| 606 | } |
| 607 | |
Oliver Stannard | 43f1dd7 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 608 | //===----------------------------------------------------------------------===// |
| 609 | // Error Reporting |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | |
| 612 | void MCContext::reportError(SMLoc Loc, const Twine &Msg) { |
| 613 | HadError = true; |
| 614 | |
Sanne Wouda | 5896003 | 2017-02-08 14:48:05 +0000 | [diff] [blame] | 615 | // 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 Grosbach | 6fb117d | 2014-03-14 22:41:58 +0000 | [diff] [blame] | 623 | report_fatal_error(Msg, false); |
Oliver Stannard | 43f1dd7 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) { |
| 627 | reportError(Loc, Msg); |
Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 628 | |
| 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 | } |