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