1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/MCContext.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/BinaryFormat/COFF.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/BinaryFormat/XCOFF.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCCodeView.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCFragment.h"
24 #include "llvm/MC/MCLabel.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCSectionCOFF.h"
27 #include "llvm/MC/MCSectionELF.h"
28 #include "llvm/MC/MCSectionMachO.h"
29 #include "llvm/MC/MCSectionWasm.h"
30 #include "llvm/MC/MCSectionXCOFF.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCSymbolCOFF.h"
34 #include "llvm/MC/MCSymbolELF.h"
35 #include "llvm/MC/MCSymbolMachO.h"
36 #include "llvm/MC/MCSymbolWasm.h"
37 #include "llvm/MC/MCSymbolXCOFF.h"
38 #include "llvm/MC/SectionKind.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/Signals.h"
45 #include "llvm/Support/SourceMgr.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <cassert>
48 #include <cstdlib>
49 #include <tuple>
50 #include <utility>
51
52 using namespace llvm;
53
54 static cl::opt<char*>
55 AsSecureLogFileName("as-secure-log-file-name",
56 cl::desc("As secure log file name (initialized from "
57 "AS_SECURE_LOG_FILE env variable)"),
58 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
59
MCContext(const MCAsmInfo * mai,const MCRegisterInfo * mri,const MCObjectFileInfo * mofi,const SourceMgr * mgr,MCTargetOptions const * TargetOpts,bool DoAutoReset)60 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
61 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
62 MCTargetOptions const *TargetOpts, bool DoAutoReset)
63 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
64 Symbols(Allocator), UsedNames(Allocator),
65 InlineAsmUsedLabelNames(Allocator),
66 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
67 AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
68 SecureLogFile = AsSecureLogFileName;
69
70 if (SrcMgr && SrcMgr->getNumBuffers())
71 MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())
72 ->getBufferIdentifier());
73 }
74
~MCContext()75 MCContext::~MCContext() {
76 if (AutoReset)
77 reset();
78
79 // NOTE: The symbols are all allocated out of a bump pointer allocator,
80 // we don't need to free them here.
81 }
82
83 //===----------------------------------------------------------------------===//
84 // Module Lifetime Management
85 //===----------------------------------------------------------------------===//
86
reset()87 void MCContext::reset() {
88 // Call the destructors so the fragments are freed
89 COFFAllocator.DestroyAll();
90 ELFAllocator.DestroyAll();
91 MachOAllocator.DestroyAll();
92 XCOFFAllocator.DestroyAll();
93 MCInstAllocator.DestroyAll();
94
95 MCSubtargetAllocator.DestroyAll();
96 InlineAsmUsedLabelNames.clear();
97 UsedNames.clear();
98 Symbols.clear();
99 Allocator.Reset();
100 Instances.clear();
101 CompilationDir.clear();
102 MainFileName.clear();
103 MCDwarfLineTablesCUMap.clear();
104 SectionsForRanges.clear();
105 MCGenDwarfLabelEntries.clear();
106 DwarfDebugFlags = StringRef();
107 DwarfCompileUnitID = 0;
108 CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
109
110 CVContext.reset();
111
112 MachOUniquingMap.clear();
113 ELFUniquingMap.clear();
114 COFFUniquingMap.clear();
115 WasmUniquingMap.clear();
116 XCOFFUniquingMap.clear();
117
118 ELFEntrySizeMap.clear();
119 ELFSeenGenericMergeableSections.clear();
120
121 NextID.clear();
122 AllowTemporaryLabels = true;
123 DwarfLocSeen = false;
124 GenDwarfForAssembly = false;
125 GenDwarfFileNumber = 0;
126
127 HadError = false;
128 }
129
130 //===----------------------------------------------------------------------===//
131 // MCInst Management
132 //===----------------------------------------------------------------------===//
133
createMCInst()134 MCInst *MCContext::createMCInst() {
135 return new (MCInstAllocator.Allocate()) MCInst;
136 }
137
138 //===----------------------------------------------------------------------===//
139 // Symbol Manipulation
140 //===----------------------------------------------------------------------===//
141
getOrCreateSymbol(const Twine & Name)142 MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
143 SmallString<128> NameSV;
144 StringRef NameRef = Name.toStringRef(NameSV);
145
146 assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
147
148 MCSymbol *&Sym = Symbols[NameRef];
149 if (!Sym)
150 Sym = createSymbol(NameRef, false, false);
151
152 return Sym;
153 }
154
getOrCreateFrameAllocSymbol(StringRef FuncName,unsigned Idx)155 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
156 unsigned Idx) {
157 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
158 "$frame_escape_" + Twine(Idx));
159 }
160
getOrCreateParentFrameOffsetSymbol(StringRef FuncName)161 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
162 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
163 "$parent_frame_offset");
164 }
165
getOrCreateLSDASymbol(StringRef FuncName)166 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
167 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
168 FuncName);
169 }
170
createSymbolImpl(const StringMapEntry<bool> * Name,bool IsTemporary)171 MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
172 bool IsTemporary) {
173 static_assert(std::is_trivially_destructible<MCSymbolCOFF>(),
174 "MCSymbol classes must be trivially destructible");
175 static_assert(std::is_trivially_destructible<MCSymbolELF>(),
176 "MCSymbol classes must be trivially destructible");
177 static_assert(std::is_trivially_destructible<MCSymbolMachO>(),
178 "MCSymbol classes must be trivially destructible");
179 static_assert(std::is_trivially_destructible<MCSymbolWasm>(),
180 "MCSymbol classes must be trivially destructible");
181 static_assert(std::is_trivially_destructible<MCSymbolXCOFF>(),
182 "MCSymbol classes must be trivially destructible");
183 if (MOFI) {
184 switch (MOFI->getObjectFileType()) {
185 case MCObjectFileInfo::IsCOFF:
186 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
187 case MCObjectFileInfo::IsELF:
188 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
189 case MCObjectFileInfo::IsMachO:
190 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
191 case MCObjectFileInfo::IsWasm:
192 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
193 case MCObjectFileInfo::IsXCOFF:
194 return createXCOFFSymbolImpl(Name, IsTemporary);
195 }
196 }
197 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
198 IsTemporary);
199 }
200
createSymbol(StringRef Name,bool AlwaysAddSuffix,bool CanBeUnnamed)201 MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
202 bool CanBeUnnamed) {
203 if (CanBeUnnamed && !UseNamesOnTempLabels)
204 return createSymbolImpl(nullptr, true);
205
206 // Determine whether this is a user written assembler temporary or normal
207 // label, if used.
208 bool IsTemporary = CanBeUnnamed;
209 if (AllowTemporaryLabels && !IsTemporary)
210 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
211
212 SmallString<128> NewName = Name;
213 bool AddSuffix = AlwaysAddSuffix;
214 unsigned &NextUniqueID = NextID[Name];
215 while (true) {
216 if (AddSuffix) {
217 NewName.resize(Name.size());
218 raw_svector_ostream(NewName) << NextUniqueID++;
219 }
220 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
221 if (NameEntry.second || !NameEntry.first->second) {
222 // Ok, we found a name.
223 // Mark it as used for a non-section symbol.
224 NameEntry.first->second = true;
225 // Have the MCSymbol object itself refer to the copy of the string that is
226 // embedded in the UsedNames entry.
227 return createSymbolImpl(&*NameEntry.first, IsTemporary);
228 }
229 assert(IsTemporary && "Cannot rename non-temporary symbols");
230 AddSuffix = true;
231 }
232 llvm_unreachable("Infinite loop");
233 }
234
createTempSymbol(const Twine & Name,bool AlwaysAddSuffix,bool CanBeUnnamed)235 MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
236 bool CanBeUnnamed) {
237 SmallString<128> NameSV;
238 raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
239 return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
240 }
241
createLinkerPrivateTempSymbol()242 MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
243 SmallString<128> NameSV;
244 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
245 return createSymbol(NameSV, true, false);
246 }
247
createTempSymbol(bool CanBeUnnamed)248 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
249 return createTempSymbol("tmp", true, CanBeUnnamed);
250 }
251
NextInstance(unsigned LocalLabelVal)252 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
253 MCLabel *&Label = Instances[LocalLabelVal];
254 if (!Label)
255 Label = new (*this) MCLabel(0);
256 return Label->incInstance();
257 }
258
GetInstance(unsigned LocalLabelVal)259 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
260 MCLabel *&Label = Instances[LocalLabelVal];
261 if (!Label)
262 Label = new (*this) MCLabel(0);
263 return Label->getInstance();
264 }
265
getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,unsigned Instance)266 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
267 unsigned Instance) {
268 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
269 if (!Sym)
270 Sym = createTempSymbol(false);
271 return Sym;
272 }
273
createDirectionalLocalSymbol(unsigned LocalLabelVal)274 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
275 unsigned Instance = NextInstance(LocalLabelVal);
276 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
277 }
278
getDirectionalLocalSymbol(unsigned LocalLabelVal,bool Before)279 MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
280 bool Before) {
281 unsigned Instance = GetInstance(LocalLabelVal);
282 if (!Before)
283 ++Instance;
284 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
285 }
286
lookupSymbol(const Twine & Name) const287 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
288 SmallString<128> NameSV;
289 StringRef NameRef = Name.toStringRef(NameSV);
290 return Symbols.lookup(NameRef);
291 }
292
setSymbolValue(MCStreamer & Streamer,StringRef Sym,uint64_t Val)293 void MCContext::setSymbolValue(MCStreamer &Streamer,
294 StringRef Sym,
295 uint64_t Val) {
296 auto Symbol = getOrCreateSymbol(Sym);
297 Streamer.emitAssignment(Symbol, MCConstantExpr::create(Val, *this));
298 }
299
registerInlineAsmLabel(MCSymbol * Sym)300 void MCContext::registerInlineAsmLabel(MCSymbol *Sym) {
301 InlineAsmUsedLabelNames[Sym->getName()] = Sym;
302 }
303
304 MCSymbolXCOFF *
createXCOFFSymbolImpl(const StringMapEntry<bool> * Name,bool IsTemporary)305 MCContext::createXCOFFSymbolImpl(const StringMapEntry<bool> *Name,
306 bool IsTemporary) {
307 if (!Name)
308 return new (nullptr, *this) MCSymbolXCOFF(nullptr, IsTemporary);
309
310 StringRef OriginalName = Name->first();
311 if (OriginalName.startswith("._Renamed..") ||
312 OriginalName.startswith("_Renamed.."))
313 reportError(SMLoc(), "invalid symbol name from source");
314
315 if (MAI->isValidUnquotedName(OriginalName))
316 return new (Name, *this) MCSymbolXCOFF(Name, IsTemporary);
317
318 // Now we have a name that contains invalid character(s) for XCOFF symbol.
319 // Let's replace with something valid, but save the original name so that
320 // we could still use the original name in the symbol table.
321 SmallString<128> InvalidName(OriginalName);
322
323 // If it's an entry point symbol, we will keep the '.'
324 // in front for the convention purpose. Otherwise, add "_Renamed.."
325 // as prefix to signal this is an renamed symbol.
326 const bool IsEntryPoint = !InvalidName.empty() && InvalidName[0] == '.';
327 SmallString<128> ValidName =
328 StringRef(IsEntryPoint ? "._Renamed.." : "_Renamed..");
329
330 // Append the hex values of '_' and invalid characters with "_Renamed..";
331 // at the same time replace invalid characters with '_'.
332 for (size_t I = 0; I < InvalidName.size(); ++I) {
333 if (!MAI->isAcceptableChar(InvalidName[I]) || InvalidName[I] == '_') {
334 raw_svector_ostream(ValidName).write_hex(InvalidName[I]);
335 InvalidName[I] = '_';
336 }
337 }
338
339 // Skip entry point symbol's '.' as we already have a '.' in front of
340 // "_Renamed".
341 if (IsEntryPoint)
342 ValidName.append(InvalidName.substr(1, InvalidName.size() - 1));
343 else
344 ValidName.append(InvalidName);
345
346 auto NameEntry = UsedNames.insert(std::make_pair(ValidName, true));
347 assert((NameEntry.second || !NameEntry.first->second) &&
348 "This name is used somewhere else.");
349 // Mark the name as used for a non-section symbol.
350 NameEntry.first->second = true;
351 // Have the MCSymbol object itself refer to the copy of the string
352 // that is embedded in the UsedNames entry.
353 MCSymbolXCOFF *XSym = new (&*NameEntry.first, *this)
354 MCSymbolXCOFF(&*NameEntry.first, IsTemporary);
355 XSym->setSymbolTableName(MCSymbolXCOFF::getUnqualifiedName(OriginalName));
356 return XSym;
357 }
358
359 //===----------------------------------------------------------------------===//
360 // Section Management
361 //===----------------------------------------------------------------------===//
362
getMachOSection(StringRef Segment,StringRef Section,unsigned TypeAndAttributes,unsigned Reserved2,SectionKind Kind,const char * BeginSymName)363 MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
364 unsigned TypeAndAttributes,
365 unsigned Reserved2, SectionKind Kind,
366 const char *BeginSymName) {
367 // We unique sections by their segment/section pair. The returned section
368 // may not have the same flags as the requested section, if so this should be
369 // diagnosed by the client as an error.
370
371 // Form the name to look up.
372 assert(Section.size() <= 16 && "section name is too long");
373 assert(!memchr(Section.data(), '\0', Section.size()) &&
374 "section name cannot contain NUL");
375
376 // Do the lookup, if we have a hit, return it.
377 auto R = MachOUniquingMap.try_emplace((Segment + Twine(',') + Section).str());
378 if (!R.second)
379 return R.first->second;
380
381 MCSymbol *Begin = nullptr;
382 if (BeginSymName)
383 Begin = createTempSymbol(BeginSymName, false);
384
385 // Otherwise, return a new section.
386 StringRef Name = R.first->first();
387 R.first->second = new (MachOAllocator.Allocate())
388 MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()),
389 TypeAndAttributes, Reserved2, Kind, Begin);
390 return R.first->second;
391 }
392
renameELFSection(MCSectionELF * Section,StringRef Name)393 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
394 StringRef GroupName;
395 if (const MCSymbol *Group = Section->getGroup())
396 GroupName = Group->getName();
397
398 // This function is only used by .debug*, which should not have the
399 // SHF_LINK_ORDER flag.
400 unsigned UniqueID = Section->getUniqueID();
401 ELFUniquingMap.erase(
402 ELFSectionKey{Section->getName(), GroupName, "", UniqueID});
403 auto I = ELFUniquingMap
404 .insert(std::make_pair(
405 ELFSectionKey{Name, GroupName, "", UniqueID}, Section))
406 .first;
407 StringRef CachedName = I->first.SectionName;
408 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
409 }
410
createELFSectionImpl(StringRef Section,unsigned Type,unsigned Flags,SectionKind K,unsigned EntrySize,const MCSymbolELF * Group,unsigned UniqueID,const MCSymbolELF * LinkedToSym)411 MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
412 unsigned Flags, SectionKind K,
413 unsigned EntrySize,
414 const MCSymbolELF *Group,
415 unsigned UniqueID,
416 const MCSymbolELF *LinkedToSym) {
417 MCSymbolELF *R;
418 MCSymbol *&Sym = Symbols[Section];
419 // A section symbol can not redefine regular symbols. There may be multiple
420 // sections with the same name, in which case the first such section wins.
421 if (Sym && Sym->isDefined() &&
422 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
423 reportError(SMLoc(), "invalid symbol redefinition");
424 if (Sym && Sym->isUndefined()) {
425 R = cast<MCSymbolELF>(Sym);
426 } else {
427 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
428 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
429 if (!Sym)
430 Sym = R;
431 }
432 R->setBinding(ELF::STB_LOCAL);
433 R->setType(ELF::STT_SECTION);
434
435 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
436 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, LinkedToSym);
437
438 auto *F = new MCDataFragment();
439 Ret->getFragmentList().insert(Ret->begin(), F);
440 F->setParent(Ret);
441 R->setFragment(F);
442
443 return Ret;
444 }
445
createELFRelSection(const Twine & Name,unsigned Type,unsigned Flags,unsigned EntrySize,const MCSymbolELF * Group,const MCSectionELF * RelInfoSection)446 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
447 unsigned Flags, unsigned EntrySize,
448 const MCSymbolELF *Group,
449 const MCSectionELF *RelInfoSection) {
450 StringMap<bool>::iterator I;
451 bool Inserted;
452 std::tie(I, Inserted) =
453 RelSecNames.insert(std::make_pair(Name.str(), true));
454
455 return createELFSectionImpl(
456 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
457 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
458 }
459
getELFNamedSection(const Twine & Prefix,const Twine & Suffix,unsigned Type,unsigned Flags,unsigned EntrySize)460 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
461 const Twine &Suffix, unsigned Type,
462 unsigned Flags,
463 unsigned EntrySize) {
464 return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
465 }
466
getELFSection(const Twine & Section,unsigned Type,unsigned Flags,unsigned EntrySize,const Twine & Group,unsigned UniqueID,const MCSymbolELF * LinkedToSym)467 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
468 unsigned Flags, unsigned EntrySize,
469 const Twine &Group, unsigned UniqueID,
470 const MCSymbolELF *LinkedToSym) {
471 MCSymbolELF *GroupSym = nullptr;
472 if (!Group.isTriviallyEmpty() && !Group.str().empty())
473 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
474
475 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
476 LinkedToSym);
477 }
478
getELFSection(const Twine & Section,unsigned Type,unsigned Flags,unsigned EntrySize,const MCSymbolELF * GroupSym,unsigned UniqueID,const MCSymbolELF * LinkedToSym)479 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
480 unsigned Flags, unsigned EntrySize,
481 const MCSymbolELF *GroupSym,
482 unsigned UniqueID,
483 const MCSymbolELF *LinkedToSym) {
484 StringRef Group = "";
485 if (GroupSym)
486 Group = GroupSym->getName();
487 assert(!(LinkedToSym && LinkedToSym->getName().empty()));
488 // Do the lookup, if we have a hit, return it.
489 auto IterBool = ELFUniquingMap.insert(std::make_pair(
490 ELFSectionKey{Section.str(), Group,
491 LinkedToSym ? LinkedToSym->getName() : "", UniqueID},
492 nullptr));
493 auto &Entry = *IterBool.first;
494 if (!IterBool.second)
495 return Entry.second;
496
497 StringRef CachedName = Entry.first.SectionName;
498
499 SectionKind Kind;
500 if (Flags & ELF::SHF_ARM_PURECODE)
501 Kind = SectionKind::getExecuteOnly();
502 else if (Flags & ELF::SHF_EXECINSTR)
503 Kind = SectionKind::getText();
504 else
505 Kind = SectionKind::getReadOnly();
506
507 MCSectionELF *Result =
508 createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,
509 UniqueID, LinkedToSym);
510 Entry.second = Result;
511
512 recordELFMergeableSectionInfo(Result->getName(), Result->getFlags(),
513 Result->getUniqueID(), Result->getEntrySize());
514
515 return Result;
516 }
517
createELFGroupSection(const MCSymbolELF * Group)518 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
519 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
520 SectionKind::getReadOnly(), 4, Group,
521 MCSection::NonUniqueID, nullptr);
522 }
523
recordELFMergeableSectionInfo(StringRef SectionName,unsigned Flags,unsigned UniqueID,unsigned EntrySize)524 void MCContext::recordELFMergeableSectionInfo(StringRef SectionName,
525 unsigned Flags, unsigned UniqueID,
526 unsigned EntrySize) {
527 bool IsMergeable = Flags & ELF::SHF_MERGE;
528 if (IsMergeable && (UniqueID == GenericSectionID))
529 ELFSeenGenericMergeableSections.insert(SectionName);
530
531 // For mergeable sections or non-mergeable sections with a generic mergeable
532 // section name we enter their Unique ID into the ELFEntrySizeMap so that
533 // compatible globals can be assigned to the same section.
534 if (IsMergeable || isELFGenericMergeableSection(SectionName)) {
535 ELFEntrySizeMap.insert(std::make_pair(
536 ELFEntrySizeKey{SectionName, Flags, EntrySize}, UniqueID));
537 }
538 }
539
isELFImplicitMergeableSectionNamePrefix(StringRef SectionName)540 bool MCContext::isELFImplicitMergeableSectionNamePrefix(StringRef SectionName) {
541 return SectionName.startswith(".rodata.str") ||
542 SectionName.startswith(".rodata.cst");
543 }
544
isELFGenericMergeableSection(StringRef SectionName)545 bool MCContext::isELFGenericMergeableSection(StringRef SectionName) {
546 return isELFImplicitMergeableSectionNamePrefix(SectionName) ||
547 ELFSeenGenericMergeableSections.count(SectionName);
548 }
549
getELFUniqueIDForEntsize(StringRef SectionName,unsigned Flags,unsigned EntrySize)550 Optional<unsigned> MCContext::getELFUniqueIDForEntsize(StringRef SectionName,
551 unsigned Flags,
552 unsigned EntrySize) {
553 auto I = ELFEntrySizeMap.find(
554 MCContext::ELFEntrySizeKey{SectionName, Flags, EntrySize});
555 return (I != ELFEntrySizeMap.end()) ? Optional<unsigned>(I->second) : None;
556 }
557
getCOFFSection(StringRef Section,unsigned Characteristics,SectionKind Kind,StringRef COMDATSymName,int Selection,unsigned UniqueID,const char * BeginSymName)558 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
559 unsigned Characteristics,
560 SectionKind Kind,
561 StringRef COMDATSymName, int Selection,
562 unsigned UniqueID,
563 const char *BeginSymName) {
564 MCSymbol *COMDATSymbol = nullptr;
565 if (!COMDATSymName.empty()) {
566 COMDATSymbol = getOrCreateSymbol(COMDATSymName);
567 COMDATSymName = COMDATSymbol->getName();
568 }
569
570
571 // Do the lookup, if we have a hit, return it.
572 COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
573 auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
574 auto Iter = IterBool.first;
575 if (!IterBool.second)
576 return Iter->second;
577
578 MCSymbol *Begin = nullptr;
579 if (BeginSymName)
580 Begin = createTempSymbol(BeginSymName, false);
581
582 StringRef CachedName = Iter->first.SectionName;
583 MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
584 CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
585
586 Iter->second = Result;
587 return Result;
588 }
589
getCOFFSection(StringRef Section,unsigned Characteristics,SectionKind Kind,const char * BeginSymName)590 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
591 unsigned Characteristics,
592 SectionKind Kind,
593 const char *BeginSymName) {
594 return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
595 BeginSymName);
596 }
597
getAssociativeCOFFSection(MCSectionCOFF * Sec,const MCSymbol * KeySym,unsigned UniqueID)598 MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
599 const MCSymbol *KeySym,
600 unsigned UniqueID) {
601 // Return the normal section if we don't have to be associative or unique.
602 if (!KeySym && UniqueID == GenericSectionID)
603 return Sec;
604
605 // If we have a key symbol, make an associative section with the same name and
606 // kind as the normal section.
607 unsigned Characteristics = Sec->getCharacteristics();
608 if (KeySym) {
609 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
610 return getCOFFSection(Sec->getName(), Characteristics, Sec->getKind(),
611 KeySym->getName(),
612 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
613 }
614
615 return getCOFFSection(Sec->getName(), Characteristics, Sec->getKind(), "", 0,
616 UniqueID);
617 }
618
getWasmSection(const Twine & Section,SectionKind K,const Twine & Group,unsigned UniqueID,const char * BeginSymName)619 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
620 const Twine &Group, unsigned UniqueID,
621 const char *BeginSymName) {
622 MCSymbolWasm *GroupSym = nullptr;
623 if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
624 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
625 GroupSym->setComdat(true);
626 }
627
628 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
629 }
630
getWasmSection(const Twine & Section,SectionKind Kind,const MCSymbolWasm * GroupSym,unsigned UniqueID,const char * BeginSymName)631 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
632 const MCSymbolWasm *GroupSym,
633 unsigned UniqueID,
634 const char *BeginSymName) {
635 StringRef Group = "";
636 if (GroupSym)
637 Group = GroupSym->getName();
638 // Do the lookup, if we have a hit, return it.
639 auto IterBool = WasmUniquingMap.insert(
640 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
641 auto &Entry = *IterBool.first;
642 if (!IterBool.second)
643 return Entry.second;
644
645 StringRef CachedName = Entry.first.SectionName;
646
647 MCSymbol *Begin = createSymbol(CachedName, true, false);
648 cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
649
650 MCSectionWasm *Result = new (WasmAllocator.Allocate())
651 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
652 Entry.second = Result;
653
654 auto *F = new MCDataFragment();
655 Result->getFragmentList().insert(Result->begin(), F);
656 F->setParent(Result);
657 Begin->setFragment(F);
658
659 return Result;
660 }
661
662 MCSectionXCOFF *
getXCOFFSection(StringRef Section,XCOFF::StorageMappingClass SMC,XCOFF::SymbolType Type,SectionKind Kind,bool MultiSymbolsAllowed,const char * BeginSymName)663 MCContext::getXCOFFSection(StringRef Section, XCOFF::StorageMappingClass SMC,
664 XCOFF::SymbolType Type, SectionKind Kind,
665 bool MultiSymbolsAllowed, const char *BeginSymName) {
666 // Do the lookup. If we have a hit, return it.
667 auto IterBool = XCOFFUniquingMap.insert(
668 std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
669 auto &Entry = *IterBool.first;
670 if (!IterBool.second) {
671 MCSectionXCOFF *ExistedEntry = Entry.second;
672 if (ExistedEntry->isMultiSymbolsAllowed() != MultiSymbolsAllowed)
673 report_fatal_error("section's multiply symbols policy does not match");
674
675 return ExistedEntry;
676 }
677
678 // Otherwise, return a new section.
679 StringRef CachedName = Entry.first.SectionName;
680 MCSymbolXCOFF *QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol(
681 CachedName + "[" + XCOFF::getMappingClassString(SMC) + "]"));
682
683 MCSymbol *Begin = nullptr;
684 if (BeginSymName)
685 Begin = createTempSymbol(BeginSymName, false);
686
687 // QualName->getUnqualifiedName() and CachedName are the same except when
688 // CachedName contains invalid character(s) such as '$' for an XCOFF symbol.
689 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate())
690 MCSectionXCOFF(QualName->getUnqualifiedName(), SMC, Type, Kind, QualName,
691 Begin, CachedName, MultiSymbolsAllowed);
692 Entry.second = Result;
693
694 auto *F = new MCDataFragment();
695 Result->getFragmentList().insert(Result->begin(), F);
696 F->setParent(Result);
697
698 if (Begin)
699 Begin->setFragment(F);
700
701 return Result;
702 }
703
getSubtargetCopy(const MCSubtargetInfo & STI)704 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
705 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
706 }
707
addDebugPrefixMapEntry(const std::string & From,const std::string & To)708 void MCContext::addDebugPrefixMapEntry(const std::string &From,
709 const std::string &To) {
710 DebugPrefixMap.insert(std::make_pair(From, To));
711 }
712
RemapDebugPaths()713 void MCContext::RemapDebugPaths() {
714 const auto &DebugPrefixMap = this->DebugPrefixMap;
715 if (DebugPrefixMap.empty())
716 return;
717
718 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
719 SmallString<256> P(Path);
720 for (const auto &Entry : DebugPrefixMap) {
721 if (llvm::sys::path::replace_path_prefix(P, Entry.first, Entry.second)) {
722 Path = P.str().str();
723 break;
724 }
725 }
726 };
727
728 // Remap compilation directory.
729 std::string CompDir = std::string(CompilationDir.str());
730 RemapDebugPath(CompDir);
731 CompilationDir = CompDir;
732
733 // Remap MCDwarfDirs in all compilation units.
734 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
735 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
736 RemapDebugPath(Dir);
737 }
738
739 //===----------------------------------------------------------------------===//
740 // Dwarf Management
741 //===----------------------------------------------------------------------===//
742
setGenDwarfRootFile(StringRef InputFileName,StringRef Buffer)743 void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
744 // MCDwarf needs the root file as well as the compilation directory.
745 // If we find a '.file 0' directive that will supersede these values.
746 Optional<MD5::MD5Result> Cksum;
747 if (getDwarfVersion() >= 5) {
748 MD5 Hash;
749 MD5::MD5Result Sum;
750 Hash.update(Buffer);
751 Hash.final(Sum);
752 Cksum = Sum;
753 }
754 // Canonicalize the root filename. It cannot be empty, and should not
755 // repeat the compilation dir.
756 // The MCContext ctor initializes MainFileName to the name associated with
757 // the SrcMgr's main file ID, which might be the same as InputFileName (and
758 // possibly include directory components).
759 // Or, MainFileName might have been overridden by a -main-file-name option,
760 // which is supposed to be just a base filename with no directory component.
761 // So, if the InputFileName and MainFileName are not equal, assume
762 // MainFileName is a substitute basename and replace the last component.
763 SmallString<1024> FileNameBuf = InputFileName;
764 if (FileNameBuf.empty() || FileNameBuf == "-")
765 FileNameBuf = "<stdin>";
766 if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
767 llvm::sys::path::remove_filename(FileNameBuf);
768 llvm::sys::path::append(FileNameBuf, getMainFileName());
769 }
770 StringRef FileName = FileNameBuf;
771 if (FileName.consume_front(getCompilationDir()))
772 if (llvm::sys::path::is_separator(FileName.front()))
773 FileName = FileName.drop_front();
774 assert(!FileName.empty());
775 setMCLineTableRootFile(
776 /*CUID=*/0, getCompilationDir(), FileName, Cksum, None);
777 }
778
779 /// getDwarfFile - takes a file name and number to place in the dwarf file and
780 /// directory tables. If the file number has already been allocated it is an
781 /// error and zero is returned and the client reports the error, else the
782 /// allocated file number is returned. The file numbers may be in any order.
getDwarfFile(StringRef Directory,StringRef FileName,unsigned FileNumber,Optional<MD5::MD5Result> Checksum,Optional<StringRef> Source,unsigned CUID)783 Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
784 StringRef FileName,
785 unsigned FileNumber,
786 Optional<MD5::MD5Result> Checksum,
787 Optional<StringRef> Source,
788 unsigned CUID) {
789 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
790 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
791 FileNumber);
792 }
793
794 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
795 /// currently is assigned and false otherwise.
isValidDwarfFileNumber(unsigned FileNumber,unsigned CUID)796 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
797 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
798 if (FileNumber == 0)
799 return getDwarfVersion() >= 5;
800 if (FileNumber >= LineTable.getMCDwarfFiles().size())
801 return false;
802
803 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty();
804 }
805
806 /// Remove empty sections from SectionsForRanges, to avoid generating
807 /// useless debug info for them.
finalizeDwarfSections(MCStreamer & MCOS)808 void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
809 SectionsForRanges.remove_if(
810 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
811 }
812
getCVContext()813 CodeViewContext &MCContext::getCVContext() {
814 if (!CVContext.get())
815 CVContext.reset(new CodeViewContext);
816 return *CVContext.get();
817 }
818
819 //===----------------------------------------------------------------------===//
820 // Error Reporting
821 //===----------------------------------------------------------------------===//
822
reportError(SMLoc Loc,const Twine & Msg)823 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
824 HadError = true;
825
826 // If we have a source manager use it. Otherwise, try using the inline source
827 // manager.
828 // If that fails, use the generic report_fatal_error().
829 if (SrcMgr)
830 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
831 else if (InlineSrcMgr)
832 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
833 else
834 report_fatal_error(Msg, false);
835 }
836
reportWarning(SMLoc Loc,const Twine & Msg)837 void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
838 if (TargetOptions && TargetOptions->MCNoWarn)
839 return;
840 if (TargetOptions && TargetOptions->MCFatalWarnings)
841 reportError(Loc, Msg);
842 else {
843 // If we have a source manager use it. Otherwise, try using the inline
844 // source manager.
845 if (SrcMgr)
846 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
847 else if (InlineSrcMgr)
848 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
849 }
850 }
851
reportFatalError(SMLoc Loc,const Twine & Msg)852 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
853 reportError(Loc, Msg);
854
855 // If we reached here, we are failing ungracefully. Run the interrupt handlers
856 // to make sure any special cleanups get done, in particular that we remove
857 // files registered with RemoveFileOnSignal.
858 sys::RunInterruptHandlers();
859 exit(1);
860 }
861