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" |
| 21 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ELF.h" |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Rafael Espindola | ef03b9f | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Signals.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SourceMgr.h" |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 28 | #include <map> |
| 29 | |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 32 | typedef std::pair<std::string, std::string> SectionGroupPair; |
| 33 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 34 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 35 | typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy; |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 36 | typedef std::map<SectionGroupPair, const MCSectionCOFF *> COFFUniqueMapTy; |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 37 | |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 38 | MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 39 | const MCObjectFileInfo *mofi, const SourceMgr *mgr, |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [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 | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | ef03b9f | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 48 | error_code EC = llvm::sys::fs::current_path(CompilationDir); |
Andrew Trick | 32591d3 | 2013-12-10 04:39:09 +0000 | [diff] [blame] | 49 | if (EC) |
| 50 | CompilationDir.clear(); |
Rafael Espindola | ef03b9f | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 52 | MachOUniquingMap = 0; |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 53 | ELFUniquingMap = 0; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 54 | COFFUniquingMap = 0; |
Kevin Enderby | e233dda | 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 | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 59 | |
| 60 | if (SrcMgr && SrcMgr->getNumBuffers() > 0) |
| 61 | MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | MCContext::~MCContext() { |
| 65 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 66 | if (AutoReset) |
| 67 | reset(); |
Pedro Artigas | e84b13f | 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. |
Andrew Trick | 1c6a4c3 | 2013-12-10 04:39:05 +0000 | [diff] [blame] | 71 | |
Pedro Artigas | e84b13f | 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 | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 80 | void MCContext::reset() { |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 81 | UsedNames.clear(); |
| 82 | Symbols.clear(); |
| 83 | Allocator.Reset(); |
| 84 | Instances.clear(); |
David Blaikie | d9012ba | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 85 | MCDwarfLineTablesCUMap.clear(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 86 | MCGenDwarfLabelEntries.clear(); |
| 87 | DwarfDebugFlags = StringRef(); |
Pedro Artigas | 7ba2edc | 2013-02-20 00:10:29 +0000 | [diff] [blame] | 88 | DwarfCompileUnitID = 0; |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 89 | CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 2073112 | 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 | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 93 | delete (ELFUniqueMapTy*)ELFUniquingMap; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 94 | delete (COFFUniqueMapTy*)COFFUniquingMap; |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 95 | MachOUniquingMap = 0; |
| 96 | ELFUniquingMap = 0; |
| 97 | COFFUniquingMap = 0; |
Pedro Artigas | 7212ee4 | 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 | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | // Symbol Manipulation |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
Chris Lattner | 9897043 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 110 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | b973ea8 | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 111 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 1334876 | 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 | 5fe5f45 | 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 | 4ee0d03 | 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 | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 130 | isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 131 | |
| 132 | StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name); |
| 133 | if (NameEntry->getValue()) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 134 | assert(isTemporary && "Cannot rename non-temporary symbols"); |
Benjamin Kramer | 2b6c96b | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 135 | SmallString<128> NewName = Name; |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 136 | do { |
Benjamin Kramer | 2b6c96b | 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 | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 140 | } while (NameEntry->getValue()); |
| 141 | } |
| 142 | NameEntry->setValue(true); |
Chris Lattner | 3f5738d | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 1334876 | 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 | 5fe5f45 | 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 | 3f5738d | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 9897043 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 151 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 152 | SmallString<128> NameSV; |
David Blaikie | 8e5283a | 2013-12-03 18:18:28 +0000 | [diff] [blame] | 153 | return GetOrCreateSymbol(Name.toStringRef(NameSV)); |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 073d817 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 156 | MCSymbol *MCContext::CreateTempSymbol() { |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 157 | SmallString<128> NameSV; |
Benjamin Kramer | 2b6c96b | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 158 | raw_svector_ostream(NameSV) |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 159 | << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++; |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 160 | return CreateSymbol(NameSV); |
Chris Lattner | 073d817 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 163 | unsigned MCContext::NextInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 164 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 165 | if (!Label) |
| 166 | Label = new (*this) MCLabel(0); |
| 167 | return Label->incInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 170 | unsigned MCContext::GetInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 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->getInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 177 | MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 178 | unsigned Instance) { |
| 179 | MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)]; |
| 180 | if (!Sym) |
| 181 | Sym = CreateTempSymbol(); |
| 182 | return Sym; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 183 | } |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 184 | |
| 185 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) { |
| 186 | unsigned Instance = NextInstance(LocalLabelVal); |
| 187 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| 188 | } |
| 189 | |
| 190 | MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 191 | bool Before) { |
| 192 | unsigned Instance = GetInstance(LocalLabelVal); |
| 193 | if (!Before) |
| 194 | ++Instance; |
| 195 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 198 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 199 | return Symbols.lookup(Name); |
| 200 | } |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 201 | |
Roman Divacky | 0be3359 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 202 | MCSymbol *MCContext::LookupSymbol(const Twine &Name) const { |
| 203 | SmallString<128> NameSV; |
| 204 | Name.toVector(NameSV); |
| 205 | return LookupSymbol(NameSV.str()); |
| 206 | } |
| 207 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 208 | //===----------------------------------------------------------------------===// |
| 209 | // Section Management |
| 210 | //===----------------------------------------------------------------------===// |
| 211 | |
| 212 | const MCSectionMachO *MCContext:: |
| 213 | getMachOSection(StringRef Segment, StringRef Section, |
| 214 | unsigned TypeAndAttributes, |
| 215 | unsigned Reserved2, SectionKind Kind) { |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 217 | // We unique sections by their segment/section pair. The returned section |
| 218 | // may not have the same flags as the requested section, if so this should be |
| 219 | // diagnosed by the client as an error. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 221 | // Create the map if it doesn't already exist. |
| 222 | if (MachOUniquingMap == 0) |
| 223 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 224 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 226 | // Form the name to look up. |
| 227 | SmallString<64> Name; |
| 228 | Name += Segment; |
| 229 | Name.push_back(','); |
| 230 | Name += Section; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 232 | // Do the lookup, if we have a hit, return it. |
| 233 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 234 | if (Entry) return Entry; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 236 | // Otherwise, return a new section. |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 237 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 238 | Reserved2, Kind); |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 240 | |
Rafael Espindola | 36ef57d | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 241 | const MCSectionELF *MCContext:: |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 242 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 243 | SectionKind Kind) { |
| 244 | return getELFSection(Section, Type, Flags, Kind, 0, ""); |
| 245 | } |
| 246 | |
| 247 | const MCSectionELF *MCContext:: |
| 248 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 249 | SectionKind Kind, unsigned EntrySize, StringRef Group) { |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 250 | if (ELFUniquingMap == 0) |
| 251 | ELFUniquingMap = new ELFUniqueMapTy(); |
| 252 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 253 | |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [diff] [blame^] | 254 | SmallString<32> ZDebugName; |
| 255 | if (MAI->compressDebugSections() && Section.startswith(".debug_") && |
| 256 | Section != ".debug_frame") |
| 257 | Section = (".z" + Section.drop_front(1)).toStringRef(ZDebugName); |
| 258 | |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 259 | // Do the lookup, if we have a hit, return it. |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 260 | std::pair<ELFUniqueMapTy::iterator, bool> Entry = Map.insert( |
| 261 | std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0)); |
| 262 | if (!Entry.second) return Entry.first->second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 263 | |
Jan Wen Voung | efbdbe5 | 2010-09-30 05:59:22 +0000 | [diff] [blame] | 264 | // Possibly refine the entry size first. |
| 265 | if (!EntrySize) { |
| 266 | EntrySize = MCSectionELF::DetermineEntrySize(Kind); |
| 267 | } |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 268 | |
| 269 | MCSymbol *GroupSym = NULL; |
| 270 | if (!Group.empty()) |
| 271 | GroupSym = GetOrCreateSymbol(Group); |
| 272 | |
David Blaikie | 15b25df | 2013-10-22 23:41:52 +0000 | [diff] [blame] | 273 | MCSectionELF *Result = new (*this) MCSectionELF( |
| 274 | Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym); |
| 275 | Entry.first->second = Result; |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 276 | return Result; |
| 277 | } |
| 278 | |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 279 | const MCSectionELF *MCContext::CreateELFGroupSection() { |
| 280 | MCSectionELF *Result = |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 281 | new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 282 | SectionKind::getReadOnly(), 4, NULL); |
| 283 | return Result; |
| 284 | } |
| 285 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 286 | const MCSectionCOFF * |
| 287 | MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, |
| 288 | SectionKind Kind, StringRef COMDATSymName, |
| 289 | int Selection, const MCSectionCOFF *Assoc) { |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 290 | if (COFFUniquingMap == 0) |
| 291 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 292 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 294 | // Do the lookup, if we have a hit, return it. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 295 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 296 | SectionGroupPair P(Section, COMDATSymName); |
| 297 | std::pair<COFFUniqueMapTy::iterator, bool> Entry = |
| 298 | Map.insert(std::make_pair(P, (MCSectionCOFF *)0)); |
| 299 | COFFUniqueMapTy::iterator Iter = Entry.first; |
| 300 | if (!Entry.second) |
| 301 | return Iter->second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 302 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 303 | const MCSymbol *COMDATSymbol = NULL; |
| 304 | if (!COMDATSymName.empty()) |
| 305 | COMDATSymbol = GetOrCreateSymbol(COMDATSymName); |
| 306 | |
| 307 | MCSectionCOFF *Result = |
| 308 | new (*this) MCSectionCOFF(Iter->first.first, Characteristics, |
| 309 | COMDATSymbol, Selection, Assoc, Kind); |
| 310 | |
| 311 | Iter->second = Result; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 312 | return Result; |
| 313 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 314 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 315 | const MCSectionCOFF * |
| 316 | MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, |
| 317 | SectionKind Kind) { |
| 318 | return getCOFFSection(Section, Characteristics, Kind, "", 0); |
| 319 | } |
| 320 | |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 321 | const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) { |
| 322 | if (COFFUniquingMap == 0) |
| 323 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 324 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| 325 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 326 | SectionGroupPair P(Section, ""); |
| 327 | COFFUniqueMapTy::iterator Iter = Map.find(P); |
| 328 | if (Iter == Map.end()) |
| 329 | return 0; |
| 330 | return Iter->second; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 333 | //===----------------------------------------------------------------------===// |
| 334 | // Dwarf Management |
| 335 | //===----------------------------------------------------------------------===// |
| 336 | |
| 337 | /// GetDwarfFile - takes a file name an number to place in the dwarf file and |
| 338 | /// directory tables. If the file number has already been allocated it is an |
| 339 | /// error and zero is returned and the client reports the error, else the |
| 340 | /// 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] | 341 | unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName, |
Manman Ren | 1e42720 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 342 | unsigned FileNumber, unsigned CUID) { |
David Blaikie | d9012ba | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 343 | MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; |
David Blaikie | 498589c | 2014-03-13 19:15:04 +0000 | [diff] [blame] | 344 | return Table.getFile(Directory, FileName, FileNumber); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 345 | } |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 346 | |
Kevin Enderby | a68d004 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 347 | /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 348 | /// currently is assigned and false otherwise. |
Manman Ren | 1e42720 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 349 | bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { |
David Blaikie | a55ddad | 2014-03-13 18:55:04 +0000 | [diff] [blame] | 350 | const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID); |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 351 | if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size()) |
| 352 | return false; |
| 353 | |
David Blaikie | a55ddad | 2014-03-13 18:55:04 +0000 | [diff] [blame] | 354 | return !MCDwarfFiles[FileNumber].Name.empty(); |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 355 | } |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 356 | |
| 357 | void MCContext::FatalError(SMLoc Loc, const Twine &Msg) { |
| 358 | // If we have a source manager and a location, use it. Otherwise just |
| 359 | // use the generic report_fatal_error(). |
| 360 | if (!SrcMgr || Loc == SMLoc()) |
Jim Grosbach | 9a3284f | 2014-03-14 22:41:58 +0000 | [diff] [blame] | 361 | report_fatal_error(Msg, false); |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 362 | |
| 363 | // Use the source manager to print the message. |
| 364 | SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); |
| 365 | |
| 366 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 367 | // to make sure any special cleanups get done, in particular that we remove |
| 368 | // files registered with RemoveFileOnSignal. |
| 369 | sys::RunInterruptHandlers(); |
| 370 | exit(1); |
| 371 | } |