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 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 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 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 134 MCInst *MCContext::createMCInst() { 135 return new (MCInstAllocator.Allocate()) MCInst; 136 } 137 138 //===----------------------------------------------------------------------===// 139 // Symbol Manipulation 140 //===----------------------------------------------------------------------===// 141 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 155 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName, 156 unsigned Idx) { 157 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + 158 "$frame_escape_" + Twine(Idx)); 159 } 160 161 MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) { 162 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + 163 "$parent_frame_offset"); 164 } 165 166 MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) { 167 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" + 168 FuncName); 169 } 170 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 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 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 242 MCSymbol *MCContext::createLinkerPrivateTempSymbol() { 243 SmallString<128> NameSV; 244 raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp"; 245 return createSymbol(NameSV, true, false); 246 } 247 248 MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) { 249 return createTempSymbol("tmp", true, CanBeUnnamed); 250 } 251 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 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 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 274 MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) { 275 unsigned Instance = NextInstance(LocalLabelVal); 276 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); 277 } 278 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 287 MCSymbol *MCContext::lookupSymbol(const Twine &Name) const { 288 SmallString<128> NameSV; 289 StringRef NameRef = Name.toStringRef(NameSV); 290 return Symbols.lookup(NameRef); 291 } 292 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 300 void MCContext::registerInlineAsmLabel(MCSymbol *Sym) { 301 InlineAsmUsedLabelNames[Sym->getName()] = Sym; 302 } 303 304 MCSymbolXCOFF * 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 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 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 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 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 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 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 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 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 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 540 bool MCContext::isELFImplicitMergeableSectionNamePrefix(StringRef SectionName) { 541 return SectionName.startswith(".rodata.str") || 542 SectionName.startswith(".rodata.cst"); 543 } 544 545 bool MCContext::isELFGenericMergeableSection(StringRef SectionName) { 546 return isELFImplicitMergeableSectionNamePrefix(SectionName) || 547 ELFSeenGenericMergeableSections.count(SectionName); 548 } 549 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 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 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 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 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 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, false, 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 *MCContext::getXCOFFSection(StringRef Section, 663 XCOFF::StorageMappingClass SMC, 664 XCOFF::SymbolType Type, 665 SectionKind Kind, 666 const char *BeginSymName) { 667 // Do the lookup. If we have a hit, return it. 668 auto IterBool = XCOFFUniquingMap.insert( 669 std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr)); 670 auto &Entry = *IterBool.first; 671 if (!IterBool.second) 672 return Entry.second; 673 674 // Otherwise, return a new section. 675 StringRef CachedName = Entry.first.SectionName; 676 MCSymbolXCOFF *QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol( 677 CachedName + "[" + XCOFF::getMappingClassString(SMC) + "]")); 678 679 MCSymbol *Begin = nullptr; 680 if (BeginSymName) 681 Begin = createTempSymbol(BeginSymName, false); 682 683 // QualName->getUnqualifiedName() and CachedName are the same except when 684 // CachedName contains invalid character(s) such as '$' for an XCOFF symbol. 685 MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate()) 686 MCSectionXCOFF(QualName->getUnqualifiedName(), SMC, Type, Kind, QualName, 687 Begin, CachedName); 688 Entry.second = Result; 689 690 auto *F = new MCDataFragment(); 691 Result->getFragmentList().insert(Result->begin(), F); 692 F->setParent(Result); 693 694 if (Begin) 695 Begin->setFragment(F); 696 697 return Result; 698 } 699 700 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) { 701 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI); 702 } 703 704 void MCContext::addDebugPrefixMapEntry(const std::string &From, 705 const std::string &To) { 706 DebugPrefixMap.insert(std::make_pair(From, To)); 707 } 708 709 void MCContext::RemapDebugPaths() { 710 const auto &DebugPrefixMap = this->DebugPrefixMap; 711 if (DebugPrefixMap.empty()) 712 return; 713 714 const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) { 715 SmallString<256> P(Path); 716 for (const auto &Entry : DebugPrefixMap) { 717 if (llvm::sys::path::replace_path_prefix(P, Entry.first, Entry.second)) { 718 Path = P.str().str(); 719 break; 720 } 721 } 722 }; 723 724 // Remap compilation directory. 725 std::string CompDir = std::string(CompilationDir.str()); 726 RemapDebugPath(CompDir); 727 CompilationDir = CompDir; 728 729 // Remap MCDwarfDirs in all compilation units. 730 for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) 731 for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) 732 RemapDebugPath(Dir); 733 } 734 735 //===----------------------------------------------------------------------===// 736 // Dwarf Management 737 //===----------------------------------------------------------------------===// 738 739 void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) { 740 // MCDwarf needs the root file as well as the compilation directory. 741 // If we find a '.file 0' directive that will supersede these values. 742 Optional<MD5::MD5Result> Cksum; 743 if (getDwarfVersion() >= 5) { 744 MD5 Hash; 745 MD5::MD5Result Sum; 746 Hash.update(Buffer); 747 Hash.final(Sum); 748 Cksum = Sum; 749 } 750 // Canonicalize the root filename. It cannot be empty, and should not 751 // repeat the compilation dir. 752 // The MCContext ctor initializes MainFileName to the name associated with 753 // the SrcMgr's main file ID, which might be the same as InputFileName (and 754 // possibly include directory components). 755 // Or, MainFileName might have been overridden by a -main-file-name option, 756 // which is supposed to be just a base filename with no directory component. 757 // So, if the InputFileName and MainFileName are not equal, assume 758 // MainFileName is a substitute basename and replace the last component. 759 SmallString<1024> FileNameBuf = InputFileName; 760 if (FileNameBuf.empty() || FileNameBuf == "-") 761 FileNameBuf = "<stdin>"; 762 if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) { 763 llvm::sys::path::remove_filename(FileNameBuf); 764 llvm::sys::path::append(FileNameBuf, getMainFileName()); 765 } 766 StringRef FileName = FileNameBuf; 767 if (FileName.consume_front(getCompilationDir())) 768 if (llvm::sys::path::is_separator(FileName.front())) 769 FileName = FileName.drop_front(); 770 assert(!FileName.empty()); 771 setMCLineTableRootFile( 772 /*CUID=*/0, getCompilationDir(), FileName, Cksum, None); 773 } 774 775 /// getDwarfFile - takes a file name and number to place in the dwarf file and 776 /// directory tables. If the file number has already been allocated it is an 777 /// error and zero is returned and the client reports the error, else the 778 /// allocated file number is returned. The file numbers may be in any order. 779 Expected<unsigned> MCContext::getDwarfFile(StringRef Directory, 780 StringRef FileName, 781 unsigned FileNumber, 782 Optional<MD5::MD5Result> Checksum, 783 Optional<StringRef> Source, 784 unsigned CUID) { 785 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; 786 return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion, 787 FileNumber); 788 } 789 790 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it 791 /// currently is assigned and false otherwise. 792 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) { 793 const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID); 794 if (FileNumber == 0) 795 return getDwarfVersion() >= 5; 796 if (FileNumber >= LineTable.getMCDwarfFiles().size()) 797 return false; 798 799 return !LineTable.getMCDwarfFiles()[FileNumber].Name.empty(); 800 } 801 802 /// Remove empty sections from SectionsForRanges, to avoid generating 803 /// useless debug info for them. 804 void MCContext::finalizeDwarfSections(MCStreamer &MCOS) { 805 SectionsForRanges.remove_if( 806 [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); }); 807 } 808 809 CodeViewContext &MCContext::getCVContext() { 810 if (!CVContext.get()) 811 CVContext.reset(new CodeViewContext); 812 return *CVContext.get(); 813 } 814 815 //===----------------------------------------------------------------------===// 816 // Error Reporting 817 //===----------------------------------------------------------------------===// 818 819 void MCContext::reportError(SMLoc Loc, const Twine &Msg) { 820 HadError = true; 821 822 // If we have a source manager use it. Otherwise, try using the inline source 823 // manager. 824 // If that fails, use the generic report_fatal_error(). 825 if (SrcMgr) 826 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); 827 else if (InlineSrcMgr) 828 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); 829 else 830 report_fatal_error(Msg, false); 831 } 832 833 void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) { 834 if (TargetOptions && TargetOptions->MCNoWarn) 835 return; 836 if (TargetOptions && TargetOptions->MCFatalWarnings) 837 reportError(Loc, Msg); 838 else { 839 // If we have a source manager use it. Otherwise, try using the inline 840 // source manager. 841 if (SrcMgr) 842 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg); 843 else if (InlineSrcMgr) 844 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Warning, Msg); 845 } 846 } 847 848 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) { 849 reportError(Loc, Msg); 850 851 // If we reached here, we are failing ungracefully. Run the interrupt handlers 852 // to make sure any special cleanups get done, in particular that we remove 853 // files registered with RemoveFileOnSignal. 854 sys::RunInterruptHandlers(); 855 exit(1); 856 } 857