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 | |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 32 | MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 33 | const MCObjectFileInfo *mofi, const SourceMgr *mgr, |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 34 | bool DoAutoReset) |
| 35 | : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(), |
| 36 | Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0), |
| 37 | CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false), |
David Blaikie | 6f68758 | 2014-05-01 19:55:34 +0000 | [diff] [blame] | 38 | GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4), |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 39 | AllowTemporaryLabels(true), DwarfCompileUnitID(0), |
| 40 | AutoReset(DoAutoReset) { |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 41 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 42 | std::error_code EC = llvm::sys::fs::current_path(CompilationDir); |
Andrew Trick | 32591d3 | 2013-12-10 04:39:09 +0000 | [diff] [blame] | 43 | if (EC) |
| 44 | CompilationDir.clear(); |
Rafael Espindola | ef03b9f | 2013-06-14 20:26:58 +0000 | [diff] [blame] | 45 | |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 46 | SecureLogFile = getenv("AS_SECURE_LOG_FILE"); |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 47 | SecureLog = nullptr; |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 48 | SecureLogUsed = false; |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 49 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 50 | if (SrcMgr && SrcMgr->getNumBuffers()) |
| 51 | MainFileName = |
| 52 | SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | MCContext::~MCContext() { |
| 56 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 57 | if (AutoReset) |
| 58 | reset(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 59 | |
| 60 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
| 61 | // we don't need to free them here. |
Andrew Trick | 1c6a4c3 | 2013-12-10 04:39:05 +0000 | [diff] [blame] | 62 | |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 63 | // If the stream for the .secure_log_unique directive was created free it. |
| 64 | delete (raw_ostream*)SecureLog; |
| 65 | } |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | // Module Lifetime Management |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 71 | void MCContext::reset() { |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 72 | UsedNames.clear(); |
| 73 | Symbols.clear(); |
| 74 | Allocator.Reset(); |
| 75 | Instances.clear(); |
David Blaikie | d9012ba | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 76 | MCDwarfLineTablesCUMap.clear(); |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 77 | MCGenDwarfLabelEntries.clear(); |
| 78 | DwarfDebugFlags = StringRef(); |
Pedro Artigas | 7ba2edc | 2013-02-20 00:10:29 +0000 | [diff] [blame] | 79 | DwarfCompileUnitID = 0; |
Pedro Artigas | e84b13f | 2012-12-06 22:12:44 +0000 | [diff] [blame] | 80 | CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 81 | |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 82 | MachOUniquingMap.clear(); |
| 83 | ELFUniquingMap.clear(); |
| 84 | COFFUniquingMap.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 85 | |
| 86 | NextUniqueID = 0; |
| 87 | AllowTemporaryLabels = true; |
| 88 | DwarfLocSeen = false; |
| 89 | GenDwarfForAssembly = false; |
| 90 | GenDwarfFileNumber = 0; |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | // Symbol Manipulation |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
Chris Lattner | 9897043 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 97 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | b973ea8 | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 98 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 1334876 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 100 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 101 | // key if we are creating the entry. |
| 102 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 103 | MCSymbol *Sym = Entry.getValue(); |
| 104 | |
| 105 | if (Sym) |
| 106 | return Sym; |
| 107 | |
| 108 | Sym = CreateSymbol(Name); |
| 109 | Entry.setValue(Sym); |
| 110 | return Sym; |
| 111 | } |
| 112 | |
| 113 | MCSymbol *MCContext::CreateSymbol(StringRef Name) { |
Daniel Dunbar | 4ee0d03 | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 114 | // Determine whether this is an assembler temporary or normal label, if used. |
| 115 | bool isTemporary = false; |
| 116 | if (AllowTemporaryLabels) |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 117 | isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 118 | |
| 119 | StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name); |
| 120 | if (NameEntry->getValue()) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 121 | assert(isTemporary && "Cannot rename non-temporary symbols"); |
Benjamin Kramer | 2b6c96b | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 122 | SmallString<128> NewName = Name; |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 123 | do { |
Benjamin Kramer | 2b6c96b | 2011-04-09 11:26:27 +0000 | [diff] [blame] | 124 | NewName.resize(Name.size()); |
| 125 | raw_svector_ostream(NewName) << NextUniqueID++; |
| 126 | NameEntry = &UsedNames.GetOrCreateValue(NewName); |
Rafael Espindola | 5fe5f45 | 2010-12-01 20:46:11 +0000 | [diff] [blame] | 127 | } while (NameEntry->getValue()); |
| 128 | } |
| 129 | NameEntry->setValue(true); |
Chris Lattner | 3f5738d | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 1334876 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 131 | // 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] | 132 | // to the copy of the string that is embedded in the UsedNames entry. |
| 133 | MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary); |
| 134 | |
| 135 | return Result; |
Chris Lattner | 3f5738d | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 9897043 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 138 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 139 | SmallString<128> NameSV; |
David Blaikie | 8e5283a | 2013-12-03 18:18:28 +0000 | [diff] [blame] | 140 | return GetOrCreateSymbol(Name.toStringRef(NameSV)); |
Chris Lattner | 86dfd73 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 143 | MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 144 | SmallString<128> NameSV; |
| 145 | raw_svector_ostream(NameSV) |
| 146 | << MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++; |
| 147 | return CreateSymbol(NameSV); |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 073d817 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 150 | MCSymbol *MCContext::CreateTempSymbol() { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 151 | SmallString<128> NameSV; |
| 152 | raw_svector_ostream(NameSV) |
| 153 | << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++; |
| 154 | return CreateSymbol(NameSV); |
Chris Lattner | 073d817 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 157 | unsigned MCContext::NextInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 158 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 159 | if (!Label) |
| 160 | Label = new (*this) MCLabel(0); |
| 161 | return Label->incInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 164 | unsigned MCContext::GetInstance(unsigned LocalLabelVal) { |
Benjamin Kramer | ab7be75 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 165 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 166 | if (!Label) |
| 167 | Label = new (*this) MCLabel(0); |
| 168 | return Label->getInstance(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 171 | MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 172 | unsigned Instance) { |
| 173 | MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)]; |
| 174 | if (!Sym) |
| 175 | Sym = CreateTempSymbol(); |
| 176 | return Sym; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 177 | } |
Rafael Espindola | 4269b9e | 2014-03-13 18:09:26 +0000 | [diff] [blame] | 178 | |
| 179 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) { |
| 180 | unsigned Instance = NextInstance(LocalLabelVal); |
| 181 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
| 182 | } |
| 183 | |
| 184 | MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 185 | bool Before) { |
| 186 | unsigned Instance = GetInstance(LocalLabelVal); |
| 187 | if (!Before) |
| 188 | ++Instance; |
| 189 | return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 192 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 193 | return Symbols.lookup(Name); |
| 194 | } |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 195 | |
Roman Divacky | 0be3359 | 2012-09-18 17:10:37 +0000 | [diff] [blame] | 196 | MCSymbol *MCContext::LookupSymbol(const Twine &Name) const { |
| 197 | SmallString<128> NameSV; |
| 198 | Name.toVector(NameSV); |
| 199 | return LookupSymbol(NameSV.str()); |
| 200 | } |
| 201 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Section Management |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
| 206 | const MCSectionMachO *MCContext:: |
| 207 | getMachOSection(StringRef Segment, StringRef Section, |
| 208 | unsigned TypeAndAttributes, |
| 209 | unsigned Reserved2, SectionKind Kind) { |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 211 | // We unique sections by their segment/section pair. The returned section |
| 212 | // may not have the same flags as the requested section, if so this should be |
| 213 | // diagnosed by the client as an error. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 215 | // Form the name to look up. |
| 216 | SmallString<64> Name; |
| 217 | Name += Segment; |
| 218 | Name.push_back(','); |
| 219 | Name += Section; |
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 | // Do the lookup, if we have a hit, return it. |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 222 | const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()]; |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 223 | if (Entry) return Entry; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 225 | // Otherwise, return a new section. |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 226 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 227 | Reserved2, Kind); |
Chris Lattner | 2073112 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 229 | |
Rafael Espindola | 36ef57d | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 230 | const MCSectionELF *MCContext:: |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 231 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 232 | SectionKind Kind) { |
| 233 | return getELFSection(Section, Type, Flags, Kind, 0, ""); |
| 234 | } |
| 235 | |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 236 | void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 237 | StringRef GroupName; |
| 238 | if (const MCSymbol *Group = Section->getGroup()) |
| 239 | GroupName = Group->getName(); |
| 240 | |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 241 | ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName)); |
| 242 | auto I = |
| 243 | ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName), |
| 244 | Section)).first; |
David Blaikie | b60e61c | 2014-04-11 22:49:14 +0000 | [diff] [blame] | 245 | StringRef CachedName = I->first.first; |
| 246 | const_cast<MCSectionELF*>(Section)->setSectionName(CachedName); |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 249 | const MCSectionELF *MCContext:: |
| 250 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 251 | SectionKind Kind, unsigned EntrySize, StringRef Group) { |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 252 | // Do the lookup, if we have a hit, return it. |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 253 | auto IterBool = ELFUniquingMap.insert( |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 254 | std::make_pair(SectionGroupPair(Section, Group), nullptr)); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 255 | auto &Entry = *IterBool.first; |
| 256 | if (!IterBool.second) return Entry.second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 257 | |
Jan Wen Voung | efbdbe5 | 2010-09-30 05:59:22 +0000 | [diff] [blame] | 258 | // Possibly refine the entry size first. |
| 259 | if (!EntrySize) { |
| 260 | EntrySize = MCSectionELF::DetermineEntrySize(Kind); |
| 261 | } |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 262 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 263 | MCSymbol *GroupSym = nullptr; |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 264 | if (!Group.empty()) |
| 265 | GroupSym = GetOrCreateSymbol(Group); |
| 266 | |
David Blaikie | b60e61c | 2014-04-11 22:49:14 +0000 | [diff] [blame] | 267 | StringRef CachedName = Entry.first.first; |
| 268 | MCSectionELF *Result = new (*this) |
| 269 | MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 270 | Entry.second = Result; |
Chris Lattner | 5418dd5 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 271 | return Result; |
| 272 | } |
| 273 | |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 274 | const MCSectionELF *MCContext::CreateELFGroupSection() { |
| 275 | MCSectionELF *Result = |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 276 | new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 277 | SectionKind::getReadOnly(), 4, nullptr); |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 278 | return Result; |
| 279 | } |
| 280 | |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 281 | const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
| 282 | unsigned Characteristics, |
| 283 | SectionKind Kind, |
| 284 | StringRef COMDATSymName, |
| 285 | int Selection) { |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 286 | // Do the lookup, if we have a hit, return it. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 287 | |
David Majnemer | c57d038 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 288 | SectionGroupTriple T(Section, COMDATSymName, Selection); |
| 289 | auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr)); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 290 | auto Iter = IterBool.first; |
| 291 | if (!IterBool.second) |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 292 | return Iter->second; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 293 | |
David Majnemer | 8bce66b | 2014-07-14 22:57:27 +0000 | [diff] [blame^] | 294 | MCSymbol *COMDATSymbol = nullptr; |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 295 | if (!COMDATSymName.empty()) |
| 296 | COMDATSymbol = GetOrCreateSymbol(COMDATSymName); |
| 297 | |
David Majnemer | c57d038 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 298 | StringRef CachedName = std::get<0>(Iter->first); |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 299 | MCSectionCOFF *Result = new (*this) |
| 300 | MCSectionCOFF(CachedName, Characteristics, COMDATSymbol, Selection, Kind); |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 301 | |
| 302 | Iter->second = Result; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 303 | return Result; |
| 304 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 305 | |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 306 | const MCSectionCOFF * |
| 307 | MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, |
| 308 | SectionKind Kind) { |
| 309 | return getCOFFSection(Section, Characteristics, Kind, "", 0); |
| 310 | } |
| 311 | |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 312 | const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) { |
David Majnemer | c57d038 | 2014-06-27 17:19:44 +0000 | [diff] [blame] | 313 | SectionGroupTriple T(Section, "", 0); |
| 314 | auto Iter = COFFUniquingMap.find(T); |
David Blaikie | 9ec3212 | 2014-04-10 23:55:11 +0000 | [diff] [blame] | 315 | if (Iter == COFFUniquingMap.end()) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 316 | return nullptr; |
Rafael Espindola | 60ec383 | 2013-11-19 19:52:52 +0000 | [diff] [blame] | 317 | return Iter->second; |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 320 | //===----------------------------------------------------------------------===// |
| 321 | // Dwarf Management |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | |
| 324 | /// GetDwarfFile - takes a file name an number to place in the dwarf file and |
| 325 | /// directory tables. If the file number has already been allocated it is an |
| 326 | /// error and zero is returned and the client reports the error, else the |
| 327 | /// 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] | 328 | unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName, |
Manman Ren | 1e42720 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 329 | unsigned FileNumber, unsigned CUID) { |
David Blaikie | d9012ba | 2014-03-13 21:59:51 +0000 | [diff] [blame] | 330 | MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; |
David Blaikie | 498589c | 2014-03-13 19:15:04 +0000 | [diff] [blame] | 331 | return Table.getFile(Directory, FileName, FileNumber); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 332 | } |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 333 | |
Kevin Enderby | a68d004 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 334 | /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 335 | /// currently is assigned and false otherwise. |
Manman Ren | 1e42720 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 336 | bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { |
David Blaikie | a55ddad | 2014-03-13 18:55:04 +0000 | [diff] [blame] | 337 | const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID); |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 338 | if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size()) |
| 339 | return false; |
| 340 | |
David Blaikie | a55ddad | 2014-03-13 18:55:04 +0000 | [diff] [blame] | 341 | return !MCDwarfFiles[FileNumber].Name.empty(); |
Kevin Enderby | 1264b7c | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 342 | } |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 343 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 344 | /// finalizeDwarfSections - Emit end symbols for each non-empty code section. |
| 345 | /// Also remove empty sections from SectionStartEndSyms, to avoid generating |
| 346 | /// useless debug info for them. |
| 347 | void MCContext::finalizeDwarfSections(MCStreamer &MCOS) { |
| 348 | MCContext &context = MCOS.getContext(); |
| 349 | |
| 350 | auto sec = SectionStartEndSyms.begin(); |
| 351 | while (sec != SectionStartEndSyms.end()) { |
| 352 | assert(sec->second.first && "Start symbol must be set by now"); |
| 353 | MCOS.SwitchSection(sec->first); |
| 354 | if (MCOS.mayHaveInstructions()) { |
| 355 | MCSymbol *SectionEndSym = context.CreateTempSymbol(); |
| 356 | MCOS.EmitLabel(SectionEndSym); |
| 357 | sec->second.second = SectionEndSym; |
| 358 | ++sec; |
| 359 | } else { |
| 360 | MapVector<const MCSection *, std::pair<MCSymbol *, MCSymbol *> >::iterator |
| 361 | to_erase = sec; |
| 362 | sec = SectionStartEndSyms.erase(to_erase); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
Kevin Enderby | 7ee97ce | 2014-04-22 21:42:18 +0000 | [diff] [blame] | 367 | void MCContext::FatalError(SMLoc Loc, const Twine &Msg) const { |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 368 | // If we have a source manager and a location, use it. Otherwise just |
| 369 | // use the generic report_fatal_error(). |
| 370 | if (!SrcMgr || Loc == SMLoc()) |
Jim Grosbach | 9a3284f | 2014-03-14 22:41:58 +0000 | [diff] [blame] | 371 | report_fatal_error(Msg, false); |
Jim Grosbach | b18b409 | 2012-01-26 23:20:11 +0000 | [diff] [blame] | 372 | |
| 373 | // Use the source manager to print the message. |
| 374 | SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); |
| 375 | |
| 376 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 377 | // to make sure any special cleanups get done, in particular that we remove |
| 378 | // files registered with RemoveFileOnSignal. |
| 379 | sys::RunInterruptHandlers(); |
| 380 | exit(1); |
| 381 | } |