| Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===// |
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCContext.h" |
| Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
| 12 | #include "llvm/ADT/Twine.h" |
| Chandler Carruth | d04a8d4 | 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" |
| 21 | #include "llvm/MC/MCSymbol.h" |
| Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ELF.h" |
| Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| Rafael Espindola | 76858a7 | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
| Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
| Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Signals.h" |
| Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SourceMgr.h" |
| David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 28 | #include <map> |
| 29 | |
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 32 | typedef std::pair<std::string, std::string> SectionGroupPair; |
| 33 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 34 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
| David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 35 | typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy; |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 36 | typedef std::map<SectionGroupPair, const MCSectionCOFF *> COFFUniqueMapTy; |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 37 | |
| Bill Wendling | 99cb622 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 38 | MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 39 | const MCObjectFileInfo *mofi, const SourceMgr *mgr, |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 40 | bool DoAutoReset) |
| 41 | : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(), |
| 42 | Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0), |
| 43 | CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false), |
| 44 | GenDwarfForAssembly(false), GenDwarfFileNumber(0), |
| 45 | AllowTemporaryLabels(true), DwarfCompileUnitID(0), |
| 46 | AutoReset(DoAutoReset) { |
| Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 47 | |
| Rafael Espindola | 76858a7 | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 48 | error_code EC = llvm::sys::fs::current_path(CompilationDir); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 49 | if (EC) |
| 50 | CompilationDir.clear(); |
| Rafael Espindola | 76858a7 | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 51 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 52 | MachOUniquingMap = 0; |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 53 | ELFUniquingMap = 0; |
| Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 54 | COFFUniquingMap = 0; |
| Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 55 | |
| 56 | SecureLogFile = getenv("AS_SECURE_LOG_FILE"); |
| 57 | SecureLog = 0; |
| 58 | SecureLogUsed = false; |
| Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 59 | |
| 60 | if (SrcMgr && SrcMgr->getNumBuffers() > 0) |
| 61 | MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier(); |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | MCContext::~MCContext() { |
| 65 | |
| Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 66 | if (AutoReset) |
| 67 | reset(); |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 68 | |
| 69 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
| 70 | // we don't need to free them here. |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 71 | |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 72 | // If the stream for the .secure_log_unique directive was created free it. |
| 73 | delete (raw_ostream*)SecureLog; |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // Module Lifetime Management |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 80 | void MCContext::reset() { |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 81 | UsedNames.clear(); |
| 82 | Symbols.clear(); |
| 83 | Allocator.Reset(); |
| 84 | Instances.clear(); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 85 | MCDwarfLineTablesCUMap.clear(); |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 86 | MCGenDwarfLabelEntries.clear(); |
| 87 | DwarfDebugFlags = StringRef(); |
| Pedro Artigas | 9e7924d | 2013-02-20 00:10:29 +0000 | [diff] [blame] | 88 | DwarfCompileUnitID = 0; |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 89 | CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 90 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 91 | // If we have the MachO uniquing map, free it. |
| 92 | delete (MachOUniqueMapTy*)MachOUniquingMap; |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 93 | delete (ELFUniqueMapTy*)ELFUniquingMap; |
| Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 94 | delete (COFFUniqueMapTy*)COFFUniquingMap; |
| Pedro Artigas | 873a1dd | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 95 | MachOUniquingMap = 0; |
| 96 | ELFUniquingMap = 0; |
| 97 | COFFUniquingMap = 0; |
| Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 98 | |
| 99 | NextUniqueID = 0; |
| 100 | AllowTemporaryLabels = true; |
| 101 | DwarfLocSeen = false; |
| 102 | GenDwarfForAssembly = false; |
| 103 | GenDwarfFileNumber = 0; |
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | // Symbol Manipulation |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 110 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
| Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 111 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 112 | |
| Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 113 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 114 | // key if we are creating the entry. |
| 115 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 116 | MCSymbol *Sym = Entry.getValue(); |
| 117 | |
| 118 | if (Sym) |
| 119 | return Sym; |
| 120 | |
| 121 | Sym = CreateSymbol(Name); |
| 122 | Entry.setValue(Sym); |
| 123 | return Sym; |
| 124 | } |
| 125 | |
| 126 | MCSymbol *MCContext::CreateSymbol(StringRef Name) { |
| Daniel Dunbar | c6cf43d | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 127 | // Determine whether this is an assembler temporary or normal label, if used. |
| 128 | bool isTemporary = false; |
| 129 | if (AllowTemporaryLabels) |
| Bill Wendling | 99cb622 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 130 | isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 131 | |
| 132 | StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name); |
| 133 | if (NameEntry->getValue()) { |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 134 | assert(isTemporary && "Cannot rename non-temporary symbols"); |
| Benjamin Kramer | c18214a | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 135 | SmallString<128> NewName = Name; |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 136 | do { |
| Benjamin Kramer | c18214a | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 137 | NewName.resize(Name.size()); |
| 138 | raw_svector_ostream(NewName) << NextUniqueID++; |
| 139 | NameEntry = &UsedNames.GetOrCreateValue(NewName); |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 140 | } while (NameEntry->getValue()); |
| 141 | } |
| 142 | NameEntry->setValue(true); |
| Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 143 | |
| Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 144 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 145 | // to the copy of the string that is embedded in the UsedNames entry. |
| 146 | MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary); |
| 147 | |
| 148 | return Result; |
| Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 151 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
| Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 152 | SmallString<128> NameSV; |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 153 | return GetOrCreateSymbol(Name.toStringRef(NameSV)); |
| 154 | } |
| 155 | |
| 156 | MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() { |
| 157 | SmallString<128> NameSV; |
| 158 | raw_svector_ostream(NameSV) |
| 159 | << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++; |
| 160 | return CreateSymbol(NameSV); |
| Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 163 | MCSymbol *MCContext::CreateTempSymbol() { |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 164 | SmallString<128> NameSV; |
| Benjamin Kramer | c18214a | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 165 | raw_svector_ostream(NameSV) |
| Bill Wendling | 99cb622 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 166 | << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++; |
| Rafael Espindola | 9f44724 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 167 | return CreateSymbol(NameSV); |
| Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 170 | unsigned MCContext::NextInstance(unsigned LocalLabelVal) { |
| Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 171 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 172 | if (!Label) |
| 173 | Label = new (*this) MCLabel(0); |
| 174 | return Label->incInstance(); |
| Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 177 | unsigned MCContext::GetInstance(unsigned LocalLabelVal) { |
| Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 178 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 179 | if (!Label) |
| 180 | Label = new (*this) MCLabel(0); |
| 181 | return Label->getInstance(); |
| Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 184 | MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 185 | unsigned Instance) { |
| 186 | MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)]; |
| 187 | if (!Sym) |
| 188 | Sym = CreateTempSymbol(); |
| 189 | return Sym; |
| Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 190 | } |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 191 | |
| 192 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) { |
| 193 | unsigned Instance = NextInstance(LocalLabelVal); |
| 194 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| 195 | } |
| 196 | |
| 197 | MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 198 | bool Before) { |
| 199 | unsigned Instance = GetInstance(LocalLabelVal); |
| 200 | if (!Before) |
| 201 | ++Instance; |
| 202 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 205 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
| Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 206 | return Symbols.lookup(Name); |
| 207 | } |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 208 | |
| Roman Divacky | f145c13 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 209 | MCSymbol *MCContext::LookupSymbol(const Twine &Name) const { |
| 210 | SmallString<128> NameSV; |
| 211 | Name.toVector(NameSV); |
| 212 | return LookupSymbol(NameSV.str()); |
| 213 | } |
| 214 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // Section Management |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
| 219 | const MCSectionMachO *MCContext:: |
| 220 | getMachOSection(StringRef Segment, StringRef Section, |
| 221 | unsigned TypeAndAttributes, |
| 222 | unsigned Reserved2, SectionKind Kind) { |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 223 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 224 | // We unique sections by their segment/section pair. The returned section |
| 225 | // may not have the same flags as the requested section, if so this should be |
| 226 | // diagnosed by the client as an error. |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 227 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 228 | // Create the map if it doesn't already exist. |
| 229 | if (MachOUniquingMap == 0) |
| 230 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 231 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 232 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 233 | // Form the name to look up. |
| 234 | SmallString<64> Name; |
| 235 | Name += Segment; |
| 236 | Name.push_back(','); |
| 237 | Name += Section; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 238 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 239 | // Do the lookup, if we have a hit, return it. |
| 240 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 241 | if (Entry) return Entry; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 242 | |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 243 | // Otherwise, return a new section. |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 244 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 245 | Reserved2, Kind); |
| Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 246 | } |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 247 | |
| Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 248 | const MCSectionELF *MCContext:: |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 249 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 250 | SectionKind Kind) { |
| 251 | return getELFSection(Section, Type, Flags, Kind, 0, ""); |
| 252 | } |
| 253 | |
| 254 | const MCSectionELF *MCContext:: |
| 255 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 256 | SectionKind Kind, unsigned EntrySize, StringRef Group) { |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 257 | if (ELFUniquingMap == 0) |
| 258 | ELFUniquingMap = new ELFUniqueMapTy(); |
| 259 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 260 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 261 | SmallString<32> ZDebugName; |
| 262 | if (MAI->compressDebugSections() && Section.startswith(".debug_") && |
| 263 | Section != ".debug_frame" && Section != ".debug_line") |
| 264 | Section = (".z" + Section.drop_front(1)).toStringRef(ZDebugName); |
| 265 | |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 266 | // Do the lookup, if we have a hit, return it. |
| David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 267 | std::pair<ELFUniqueMapTy::iterator, bool> Entry = Map.insert( |
| 268 | std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0)); |
| 269 | if (!Entry.second) return Entry.first->second; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 270 | |
| Jan Wen Voung | 186e7a0 | 2010-09-30 05:59:22 +0000 | [diff] [blame] | 271 | // Possibly refine the entry size first. |
| 272 | if (!EntrySize) { |
| 273 | EntrySize = MCSectionELF::DetermineEntrySize(Kind); |
| 274 | } |
| Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 275 | |
| 276 | MCSymbol *GroupSym = NULL; |
| 277 | if (!Group.empty()) |
| 278 | GroupSym = GetOrCreateSymbol(Group); |
| 279 | |
| David Blaikie | 2b3ea3c | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 280 | MCSectionELF *Result = new (*this) MCSectionELF( |
| 281 | Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym); |
| 282 | Entry.first->second = Result; |
| Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 283 | return Result; |
| 284 | } |
| 285 | |
| Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 286 | const MCSectionELF *MCContext::CreateELFGroupSection() { |
| 287 | MCSectionELF *Result = |
| Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 288 | new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, |
| Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 289 | SectionKind::getReadOnly(), 4, NULL); |
| 290 | return Result; |
| 291 | } |
| 292 | |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 293 | const MCSectionCOFF * |
| 294 | MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, |
| 295 | SectionKind Kind, StringRef COMDATSymName, |
| 296 | int Selection, const MCSectionCOFF *Assoc) { |
| Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 297 | if (COFFUniquingMap == 0) |
| 298 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 299 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 300 | |
| Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 301 | // Do the lookup, if we have a hit, return it. |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 302 | |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 303 | SectionGroupPair P(Section, COMDATSymName); |
| 304 | std::pair<COFFUniqueMapTy::iterator, bool> Entry = |
| 305 | Map.insert(std::make_pair(P, (MCSectionCOFF *)0)); |
| 306 | COFFUniqueMapTy::iterator Iter = Entry.first; |
| 307 | if (!Entry.second) |
| 308 | return Iter->second; |
| Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 309 | |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 310 | const MCSymbol *COMDATSymbol = NULL; |
| 311 | if (!COMDATSymName.empty()) |
| 312 | COMDATSymbol = GetOrCreateSymbol(COMDATSymName); |
| 313 | |
| 314 | MCSectionCOFF *Result = |
| 315 | new (*this) MCSectionCOFF(Iter->first.first, Characteristics, |
| 316 | COMDATSymbol, Selection, Assoc, Kind); |
| 317 | |
| 318 | Iter->second = Result; |
| Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 319 | return Result; |
| 320 | } |
| Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 321 | |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 322 | const MCSectionCOFF * |
| 323 | MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, |
| 324 | SectionKind Kind) { |
| 325 | return getCOFFSection(Section, Characteristics, Kind, "", 0); |
| 326 | } |
| 327 | |
| Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 328 | const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) { |
| 329 | if (COFFUniquingMap == 0) |
| 330 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 331 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| 332 | |
| Bill Wendling | 0ae0709 | 2013-11-27 06:44:18 +0000 | [diff] [blame] | 333 | SectionGroupPair P(Section, ""); |
| 334 | COFFUniqueMapTy::iterator Iter = Map.find(P); |
| 335 | if (Iter == Map.end()) |
| 336 | return 0; |
| 337 | return Iter->second; |
| Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 340 | //===----------------------------------------------------------------------===// |
| 341 | // Dwarf Management |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | |
| 344 | /// GetDwarfFile - takes a file name an number to place in the dwarf file and |
| 345 | /// directory tables. If the file number has already been allocated it is an |
| 346 | /// error and zero is returned and the client reports the error, else the |
| 347 | /// allocated file number is returned. The file numbers may be in any order. |
| Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 348 | unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName, |
| Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 349 | unsigned FileNumber, unsigned CUID) { |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 350 | MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; |
| 351 | return Table.getFile(Directory, FileName, FileNumber); |
| Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 352 | } |
| Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 353 | |
| Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 354 | /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it |
| Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 355 | /// currently is assigned and false otherwise. |
| Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 356 | bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 357 | const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID); |
| Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 358 | if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size()) |
| 359 | return false; |
| 360 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 361 | return !MCDwarfFiles[FileNumber].Name.empty(); |
| Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 362 | } |
| Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 363 | |
| 364 | void MCContext::FatalError(SMLoc Loc, const Twine &Msg) { |
| 365 | // If we have a source manager and a location, use it. Otherwise just |
| 366 | // use the generic report_fatal_error(). |
| 367 | if (!SrcMgr || Loc == SMLoc()) |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 368 | report_fatal_error(Msg, false); |
| Jim Grosbach | 82f4ce5 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 369 | |
| 370 | // Use the source manager to print the message. |
| 371 | SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); |
| 372 | |
| 373 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 374 | // to make sure any special cleanups get done, in particular that we remove |
| 375 | // files registered with RemoveFileOnSignal. |
| 376 | sys::RunInterruptHandlers(); |
| 377 | exit(1); |
| 378 | } |