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