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