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