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