1 //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===// 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 // This file contains support for writing Microsoft CodeView debug info. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeViewDebug.h" 14 #include "DwarfExpression.h" 15 #include "llvm/ADT/APSInt.h" 16 #include "llvm/ADT/None.h" 17 #include "llvm/ADT/Optional.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/TinyPtrVector.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/BinaryFormat/COFF.h" 25 #include "llvm/BinaryFormat/Dwarf.h" 26 #include "llvm/CodeGen/AsmPrinter.h" 27 #include "llvm/CodeGen/LexicalScopes.h" 28 #include "llvm/CodeGen/MachineFrameInfo.h" 29 #include "llvm/CodeGen/MachineFunction.h" 30 #include "llvm/CodeGen/MachineInstr.h" 31 #include "llvm/CodeGen/MachineModuleInfo.h" 32 #include "llvm/CodeGen/MachineOperand.h" 33 #include "llvm/CodeGen/TargetFrameLowering.h" 34 #include "llvm/CodeGen/TargetRegisterInfo.h" 35 #include "llvm/CodeGen/TargetSubtargetInfo.h" 36 #include "llvm/Config/llvm-config.h" 37 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 38 #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h" 39 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h" 40 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" 41 #include "llvm/DebugInfo/CodeView/EnumTables.h" 42 #include "llvm/DebugInfo/CodeView/Line.h" 43 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 44 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" 45 #include "llvm/DebugInfo/CodeView/TypeRecord.h" 46 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" 47 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" 48 #include "llvm/IR/Constants.h" 49 #include "llvm/IR/DataLayout.h" 50 #include "llvm/IR/DebugInfoMetadata.h" 51 #include "llvm/IR/Function.h" 52 #include "llvm/IR/GlobalValue.h" 53 #include "llvm/IR/GlobalVariable.h" 54 #include "llvm/IR/Metadata.h" 55 #include "llvm/IR/Module.h" 56 #include "llvm/MC/MCAsmInfo.h" 57 #include "llvm/MC/MCContext.h" 58 #include "llvm/MC/MCSectionCOFF.h" 59 #include "llvm/MC/MCStreamer.h" 60 #include "llvm/MC/MCSymbol.h" 61 #include "llvm/Support/BinaryByteStream.h" 62 #include "llvm/Support/BinaryStreamReader.h" 63 #include "llvm/Support/BinaryStreamWriter.h" 64 #include "llvm/Support/Casting.h" 65 #include "llvm/Support/CommandLine.h" 66 #include "llvm/Support/Endian.h" 67 #include "llvm/Support/Error.h" 68 #include "llvm/Support/ErrorHandling.h" 69 #include "llvm/Support/FormatVariadic.h" 70 #include "llvm/Support/Path.h" 71 #include "llvm/Support/Program.h" 72 #include "llvm/Support/SMLoc.h" 73 #include "llvm/Support/ScopedPrinter.h" 74 #include "llvm/Target/TargetLoweringObjectFile.h" 75 #include "llvm/Target/TargetMachine.h" 76 #include <algorithm> 77 #include <cassert> 78 #include <cctype> 79 #include <cstddef> 80 #include <iterator> 81 #include <limits> 82 83 using namespace llvm; 84 using namespace llvm::codeview; 85 86 namespace { 87 class CVMCAdapter : public CodeViewRecordStreamer { 88 public: 89 CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable) 90 : OS(&OS), TypeTable(TypeTable) {} 91 92 void emitBytes(StringRef Data) override { OS->emitBytes(Data); } 93 94 void emitIntValue(uint64_t Value, unsigned Size) override { 95 OS->emitIntValueInHex(Value, Size); 96 } 97 98 void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); } 99 100 void AddComment(const Twine &T) override { OS->AddComment(T); } 101 102 void AddRawComment(const Twine &T) override { OS->emitRawComment(T); } 103 104 bool isVerboseAsm() override { return OS->isVerboseAsm(); } 105 106 std::string getTypeName(TypeIndex TI) override { 107 std::string TypeName; 108 if (!TI.isNoneType()) { 109 if (TI.isSimple()) 110 TypeName = std::string(TypeIndex::simpleTypeName(TI)); 111 else 112 TypeName = std::string(TypeTable.getTypeName(TI)); 113 } 114 return TypeName; 115 } 116 117 private: 118 MCStreamer *OS = nullptr; 119 TypeCollection &TypeTable; 120 }; 121 } // namespace 122 123 static CPUType mapArchToCVCPUType(Triple::ArchType Type) { 124 switch (Type) { 125 case Triple::ArchType::x86: 126 return CPUType::Pentium3; 127 case Triple::ArchType::x86_64: 128 return CPUType::X64; 129 case Triple::ArchType::thumb: 130 // LLVM currently doesn't support Windows CE and so thumb 131 // here is indiscriminately mapped to ARMNT specifically. 132 return CPUType::ARMNT; 133 case Triple::ArchType::aarch64: 134 return CPUType::ARM64; 135 default: 136 report_fatal_error("target architecture doesn't map to a CodeView CPUType"); 137 } 138 } 139 140 CodeViewDebug::CodeViewDebug(AsmPrinter *AP) 141 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {} 142 143 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { 144 std::string &Filepath = FileToFilepathMap[File]; 145 if (!Filepath.empty()) 146 return Filepath; 147 148 StringRef Dir = File->getDirectory(), Filename = File->getFilename(); 149 150 // If this is a Unix-style path, just use it as is. Don't try to canonicalize 151 // it textually because one of the path components could be a symlink. 152 if (Dir.startswith("/") || Filename.startswith("/")) { 153 if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix)) 154 return Filename; 155 Filepath = std::string(Dir); 156 if (Dir.back() != '/') 157 Filepath += '/'; 158 Filepath += Filename; 159 return Filepath; 160 } 161 162 // Clang emits directory and relative filename info into the IR, but CodeView 163 // operates on full paths. We could change Clang to emit full paths too, but 164 // that would increase the IR size and probably not needed for other users. 165 // For now, just concatenate and canonicalize the path here. 166 if (Filename.find(':') == 1) 167 Filepath = std::string(Filename); 168 else 169 Filepath = (Dir + "\\" + Filename).str(); 170 171 // Canonicalize the path. We have to do it textually because we may no longer 172 // have access the file in the filesystem. 173 // First, replace all slashes with backslashes. 174 std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); 175 176 // Remove all "\.\" with "\". 177 size_t Cursor = 0; 178 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) 179 Filepath.erase(Cursor, 2); 180 181 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original 182 // path should be well-formatted, e.g. start with a drive letter, etc. 183 Cursor = 0; 184 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { 185 // Something's wrong if the path starts with "\..\", abort. 186 if (Cursor == 0) 187 break; 188 189 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); 190 if (PrevSlash == std::string::npos) 191 // Something's wrong, abort. 192 break; 193 194 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); 195 // The next ".." might be following the one we've just erased. 196 Cursor = PrevSlash; 197 } 198 199 // Remove all duplicate backslashes. 200 Cursor = 0; 201 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) 202 Filepath.erase(Cursor, 1); 203 204 return Filepath; 205 } 206 207 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { 208 StringRef FullPath = getFullFilepath(F); 209 unsigned NextId = FileIdMap.size() + 1; 210 auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId)); 211 if (Insertion.second) { 212 // We have to compute the full filepath and emit a .cv_file directive. 213 ArrayRef<uint8_t> ChecksumAsBytes; 214 FileChecksumKind CSKind = FileChecksumKind::None; 215 if (F->getChecksum()) { 216 std::string Checksum = fromHex(F->getChecksum()->Value); 217 void *CKMem = OS.getContext().allocate(Checksum.size(), 1); 218 memcpy(CKMem, Checksum.data(), Checksum.size()); 219 ChecksumAsBytes = ArrayRef<uint8_t>( 220 reinterpret_cast<const uint8_t *>(CKMem), Checksum.size()); 221 switch (F->getChecksum()->Kind) { 222 case DIFile::CSK_MD5: 223 CSKind = FileChecksumKind::MD5; 224 break; 225 case DIFile::CSK_SHA1: 226 CSKind = FileChecksumKind::SHA1; 227 break; 228 case DIFile::CSK_SHA256: 229 CSKind = FileChecksumKind::SHA256; 230 break; 231 } 232 } 233 bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes, 234 static_cast<unsigned>(CSKind)); 235 (void)Success; 236 assert(Success && ".cv_file directive failed"); 237 } 238 return Insertion.first->second; 239 } 240 241 CodeViewDebug::InlineSite & 242 CodeViewDebug::getInlineSite(const DILocation *InlinedAt, 243 const DISubprogram *Inlinee) { 244 auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); 245 InlineSite *Site = &SiteInsertion.first->second; 246 if (SiteInsertion.second) { 247 unsigned ParentFuncId = CurFn->FuncId; 248 if (const DILocation *OuterIA = InlinedAt->getInlinedAt()) 249 ParentFuncId = 250 getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram()) 251 .SiteFuncId; 252 253 Site->SiteFuncId = NextFuncId++; 254 OS.EmitCVInlineSiteIdDirective( 255 Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()), 256 InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc()); 257 Site->Inlinee = Inlinee; 258 InlinedSubprograms.insert(Inlinee); 259 getFuncIdForSubprogram(Inlinee); 260 } 261 return *Site; 262 } 263 264 static StringRef getPrettyScopeName(const DIScope *Scope) { 265 StringRef ScopeName = Scope->getName(); 266 if (!ScopeName.empty()) 267 return ScopeName; 268 269 switch (Scope->getTag()) { 270 case dwarf::DW_TAG_enumeration_type: 271 case dwarf::DW_TAG_class_type: 272 case dwarf::DW_TAG_structure_type: 273 case dwarf::DW_TAG_union_type: 274 return "<unnamed-tag>"; 275 case dwarf::DW_TAG_namespace: 276 return "`anonymous namespace'"; 277 default: 278 return StringRef(); 279 } 280 } 281 282 const DISubprogram *CodeViewDebug::collectParentScopeNames( 283 const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) { 284 const DISubprogram *ClosestSubprogram = nullptr; 285 while (Scope != nullptr) { 286 if (ClosestSubprogram == nullptr) 287 ClosestSubprogram = dyn_cast<DISubprogram>(Scope); 288 289 // If a type appears in a scope chain, make sure it gets emitted. The 290 // frontend will be responsible for deciding if this should be a forward 291 // declaration or a complete type. 292 if (const auto *Ty = dyn_cast<DICompositeType>(Scope)) 293 DeferredCompleteTypes.push_back(Ty); 294 295 StringRef ScopeName = getPrettyScopeName(Scope); 296 if (!ScopeName.empty()) 297 QualifiedNameComponents.push_back(ScopeName); 298 Scope = Scope->getScope(); 299 } 300 return ClosestSubprogram; 301 } 302 303 static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents, 304 StringRef TypeName) { 305 std::string FullyQualifiedName; 306 for (StringRef QualifiedNameComponent : 307 llvm::reverse(QualifiedNameComponents)) { 308 FullyQualifiedName.append(std::string(QualifiedNameComponent)); 309 FullyQualifiedName.append("::"); 310 } 311 FullyQualifiedName.append(std::string(TypeName)); 312 return FullyQualifiedName; 313 } 314 315 struct CodeViewDebug::TypeLoweringScope { 316 TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; } 317 ~TypeLoweringScope() { 318 // Don't decrement TypeEmissionLevel until after emitting deferred types, so 319 // inner TypeLoweringScopes don't attempt to emit deferred types. 320 if (CVD.TypeEmissionLevel == 1) 321 CVD.emitDeferredCompleteTypes(); 322 --CVD.TypeEmissionLevel; 323 } 324 CodeViewDebug &CVD; 325 }; 326 327 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope, 328 StringRef Name) { 329 // Ensure types in the scope chain are emitted as soon as possible. 330 // This can create otherwise a situation where S_UDTs are emitted while 331 // looping in emitDebugInfoForUDTs. 332 TypeLoweringScope S(*this); 333 SmallVector<StringRef, 5> QualifiedNameComponents; 334 collectParentScopeNames(Scope, QualifiedNameComponents); 335 return formatNestedName(QualifiedNameComponents, Name); 336 } 337 338 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) { 339 const DIScope *Scope = Ty->getScope(); 340 return getFullyQualifiedName(Scope, getPrettyScopeName(Ty)); 341 } 342 343 TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) { 344 // No scope means global scope and that uses the zero index. 345 // 346 // We also use zero index when the scope is a DISubprogram 347 // to suppress the emission of LF_STRING_ID for the function, 348 // which can trigger a link-time error with the linker in 349 // VS2019 version 16.11.2 or newer. 350 // Note, however, skipping the debug info emission for the DISubprogram 351 // is a temporary fix. The root issue here is that we need to figure out 352 // the proper way to encode a function nested in another function 353 // (as introduced by the Fortran 'contains' keyword) in CodeView. 354 if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope)) 355 return TypeIndex(); 356 357 assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type"); 358 359 // Check if we've already translated this scope. 360 auto I = TypeIndices.find({Scope, nullptr}); 361 if (I != TypeIndices.end()) 362 return I->second; 363 364 // Build the fully qualified name of the scope. 365 std::string ScopeName = getFullyQualifiedName(Scope); 366 StringIdRecord SID(TypeIndex(), ScopeName); 367 auto TI = TypeTable.writeLeafType(SID); 368 return recordTypeIndexForDINode(Scope, TI); 369 } 370 371 static StringRef removeTemplateArgs(StringRef Name) { 372 // Remove template args from the display name. Assume that the template args 373 // are the last thing in the name. 374 if (Name.empty() || Name.back() != '>') 375 return Name; 376 377 int OpenBrackets = 0; 378 for (int i = Name.size() - 1; i >= 0; --i) { 379 if (Name[i] == '>') 380 ++OpenBrackets; 381 else if (Name[i] == '<') { 382 --OpenBrackets; 383 if (OpenBrackets == 0) 384 return Name.substr(0, i); 385 } 386 } 387 return Name; 388 } 389 390 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) { 391 assert(SP); 392 393 // Check if we've already translated this subprogram. 394 auto I = TypeIndices.find({SP, nullptr}); 395 if (I != TypeIndices.end()) 396 return I->second; 397 398 // The display name includes function template arguments. Drop them to match 399 // MSVC. We need to have the template arguments in the DISubprogram name 400 // because they are used in other symbol records, such as S_GPROC32_IDs. 401 StringRef DisplayName = removeTemplateArgs(SP->getName()); 402 403 const DIScope *Scope = SP->getScope(); 404 TypeIndex TI; 405 if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) { 406 // If the scope is a DICompositeType, then this must be a method. Member 407 // function types take some special handling, and require access to the 408 // subprogram. 409 TypeIndex ClassType = getTypeIndex(Class); 410 MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class), 411 DisplayName); 412 TI = TypeTable.writeLeafType(MFuncId); 413 } else { 414 // Otherwise, this must be a free function. 415 TypeIndex ParentScope = getScopeIndex(Scope); 416 FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName); 417 TI = TypeTable.writeLeafType(FuncId); 418 } 419 420 return recordTypeIndexForDINode(SP, TI); 421 } 422 423 static bool isNonTrivial(const DICompositeType *DCTy) { 424 return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial); 425 } 426 427 static FunctionOptions 428 getFunctionOptions(const DISubroutineType *Ty, 429 const DICompositeType *ClassTy = nullptr, 430 StringRef SPName = StringRef("")) { 431 FunctionOptions FO = FunctionOptions::None; 432 const DIType *ReturnTy = nullptr; 433 if (auto TypeArray = Ty->getTypeArray()) { 434 if (TypeArray.size()) 435 ReturnTy = TypeArray[0]; 436 } 437 438 // Add CxxReturnUdt option to functions that return nontrivial record types 439 // or methods that return record types. 440 if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy)) 441 if (isNonTrivial(ReturnDCTy) || ClassTy) 442 FO |= FunctionOptions::CxxReturnUdt; 443 444 // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison. 445 if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) { 446 FO |= FunctionOptions::Constructor; 447 448 // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag. 449 450 } 451 return FO; 452 } 453 454 TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP, 455 const DICompositeType *Class) { 456 // Always use the method declaration as the key for the function type. The 457 // method declaration contains the this adjustment. 458 if (SP->getDeclaration()) 459 SP = SP->getDeclaration(); 460 assert(!SP->getDeclaration() && "should use declaration as key"); 461 462 // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide 463 // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}. 464 auto I = TypeIndices.find({SP, Class}); 465 if (I != TypeIndices.end()) 466 return I->second; 467 468 // Make sure complete type info for the class is emitted *after* the member 469 // function type, as the complete class type is likely to reference this 470 // member function type. 471 TypeLoweringScope S(*this); 472 const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0; 473 474 FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName()); 475 TypeIndex TI = lowerTypeMemberFunction( 476 SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO); 477 return recordTypeIndexForDINode(SP, TI, Class); 478 } 479 480 TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node, 481 TypeIndex TI, 482 const DIType *ClassTy) { 483 auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI}); 484 (void)InsertResult; 485 assert(InsertResult.second && "DINode was already assigned a type index"); 486 return TI; 487 } 488 489 unsigned CodeViewDebug::getPointerSizeInBytes() { 490 return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8; 491 } 492 493 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, 494 const LexicalScope *LS) { 495 if (const DILocation *InlinedAt = LS->getInlinedAt()) { 496 // This variable was inlined. Associate it with the InlineSite. 497 const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); 498 InlineSite &Site = getInlineSite(InlinedAt, Inlinee); 499 Site.InlinedLocals.emplace_back(Var); 500 } else { 501 // This variable goes into the corresponding lexical scope. 502 ScopeVariables[LS].emplace_back(Var); 503 } 504 } 505 506 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, 507 const DILocation *Loc) { 508 if (!llvm::is_contained(Locs, Loc)) 509 Locs.push_back(Loc); 510 } 511 512 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL, 513 const MachineFunction *MF) { 514 // Skip this instruction if it has the same location as the previous one. 515 if (!DL || DL == PrevInstLoc) 516 return; 517 518 const DIScope *Scope = DL.get()->getScope(); 519 if (!Scope) 520 return; 521 522 // Skip this line if it is longer than the maximum we can record. 523 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); 524 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || 525 LI.isNeverStepInto()) 526 return; 527 528 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); 529 if (CI.getStartColumn() != DL.getCol()) 530 return; 531 532 if (!CurFn->HaveLineInfo) 533 CurFn->HaveLineInfo = true; 534 unsigned FileId = 0; 535 if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile()) 536 FileId = CurFn->LastFileId; 537 else 538 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); 539 PrevInstLoc = DL; 540 541 unsigned FuncId = CurFn->FuncId; 542 if (const DILocation *SiteLoc = DL->getInlinedAt()) { 543 const DILocation *Loc = DL.get(); 544 545 // If this location was actually inlined from somewhere else, give it the ID 546 // of the inline call site. 547 FuncId = 548 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId; 549 550 // Ensure we have links in the tree of inline call sites. 551 bool FirstLoc = true; 552 while ((SiteLoc = Loc->getInlinedAt())) { 553 InlineSite &Site = 554 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()); 555 if (!FirstLoc) 556 addLocIfNotPresent(Site.ChildSites, Loc); 557 FirstLoc = false; 558 Loc = SiteLoc; 559 } 560 addLocIfNotPresent(CurFn->ChildSites, Loc); 561 } 562 563 OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), 564 /*PrologueEnd=*/false, /*IsStmt=*/false, 565 DL->getFilename(), SMLoc()); 566 } 567 568 void CodeViewDebug::emitCodeViewMagicVersion() { 569 OS.emitValueToAlignment(4); 570 OS.AddComment("Debug section magic"); 571 OS.emitInt32(COFF::DEBUG_SECTION_MAGIC); 572 } 573 574 static SourceLanguage MapDWLangToCVLang(unsigned DWLang) { 575 switch (DWLang) { 576 case dwarf::DW_LANG_C: 577 case dwarf::DW_LANG_C89: 578 case dwarf::DW_LANG_C99: 579 case dwarf::DW_LANG_C11: 580 case dwarf::DW_LANG_ObjC: 581 return SourceLanguage::C; 582 case dwarf::DW_LANG_C_plus_plus: 583 case dwarf::DW_LANG_C_plus_plus_03: 584 case dwarf::DW_LANG_C_plus_plus_11: 585 case dwarf::DW_LANG_C_plus_plus_14: 586 return SourceLanguage::Cpp; 587 case dwarf::DW_LANG_Fortran77: 588 case dwarf::DW_LANG_Fortran90: 589 case dwarf::DW_LANG_Fortran95: 590 case dwarf::DW_LANG_Fortran03: 591 case dwarf::DW_LANG_Fortran08: 592 return SourceLanguage::Fortran; 593 case dwarf::DW_LANG_Pascal83: 594 return SourceLanguage::Pascal; 595 case dwarf::DW_LANG_Cobol74: 596 case dwarf::DW_LANG_Cobol85: 597 return SourceLanguage::Cobol; 598 case dwarf::DW_LANG_Java: 599 return SourceLanguage::Java; 600 case dwarf::DW_LANG_D: 601 return SourceLanguage::D; 602 case dwarf::DW_LANG_Swift: 603 return SourceLanguage::Swift; 604 case dwarf::DW_LANG_Rust: 605 return SourceLanguage::Rust; 606 default: 607 // There's no CodeView representation for this language, and CV doesn't 608 // have an "unknown" option for the language field, so we'll use MASM, 609 // as it's very low level. 610 return SourceLanguage::Masm; 611 } 612 } 613 614 void CodeViewDebug::beginModule(Module *M) { 615 // If module doesn't have named metadata anchors or COFF debug section 616 // is not available, skip any debug info related stuff. 617 NamedMDNode *CUs = M->getNamedMetadata("llvm.dbg.cu"); 618 if (!CUs || !Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) { 619 Asm = nullptr; 620 return; 621 } 622 // Tell MMI that we have and need debug info. 623 MMI->setDebugInfoAvailability(true); 624 625 TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch()); 626 627 // Get the current source language. 628 const MDNode *Node = *CUs->operands().begin(); 629 const auto *CU = cast<DICompileUnit>(Node); 630 631 CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage()); 632 633 collectGlobalVariableInfo(); 634 635 // Check if we should emit type record hashes. 636 ConstantInt *GH = 637 mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash")); 638 EmitDebugGlobalHashes = GH && !GH->isZero(); 639 } 640 641 void CodeViewDebug::endModule() { 642 if (!Asm || !MMI->hasDebugInfo()) 643 return; 644 645 // The COFF .debug$S section consists of several subsections, each starting 646 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length 647 // of the payload followed by the payload itself. The subsections are 4-byte 648 // aligned. 649 650 // Use the generic .debug$S section, and make a subsection for all the inlined 651 // subprograms. 652 switchToDebugSectionForSymbol(nullptr); 653 654 MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols); 655 emitObjName(); 656 emitCompilerInformation(); 657 endCVSubsection(CompilerInfo); 658 659 emitInlineeLinesSubsection(); 660 661 // Emit per-function debug information. 662 for (auto &P : FnDebugInfo) 663 if (!P.first->isDeclarationForLinker()) 664 emitDebugInfoForFunction(P.first, *P.second); 665 666 // Get types used by globals without emitting anything. 667 // This is meant to collect all static const data members so they can be 668 // emitted as globals. 669 collectDebugInfoForGlobals(); 670 671 // Emit retained types. 672 emitDebugInfoForRetainedTypes(); 673 674 // Emit global variable debug information. 675 setCurrentSubprogram(nullptr); 676 emitDebugInfoForGlobals(); 677 678 // Switch back to the generic .debug$S section after potentially processing 679 // comdat symbol sections. 680 switchToDebugSectionForSymbol(nullptr); 681 682 // Emit UDT records for any types used by global variables. 683 if (!GlobalUDTs.empty()) { 684 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); 685 emitDebugInfoForUDTs(GlobalUDTs); 686 endCVSubsection(SymbolsEnd); 687 } 688 689 // This subsection holds a file index to offset in string table table. 690 OS.AddComment("File index to string table offset subsection"); 691 OS.emitCVFileChecksumsDirective(); 692 693 // This subsection holds the string table. 694 OS.AddComment("String table"); 695 OS.emitCVStringTableDirective(); 696 697 // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol 698 // subsection in the generic .debug$S section at the end. There is no 699 // particular reason for this ordering other than to match MSVC. 700 emitBuildInfo(); 701 702 // Emit type information and hashes last, so that any types we translate while 703 // emitting function info are included. 704 emitTypeInformation(); 705 706 if (EmitDebugGlobalHashes) 707 emitTypeGlobalHashes(); 708 709 clear(); 710 } 711 712 static void 713 emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S, 714 unsigned MaxFixedRecordLength = 0xF00) { 715 // The maximum CV record length is 0xFF00. Most of the strings we emit appear 716 // after a fixed length portion of the record. The fixed length portion should 717 // always be less than 0xF00 (3840) bytes, so truncate the string so that the 718 // overall record size is less than the maximum allowed. 719 SmallString<32> NullTerminatedString( 720 S.take_front(MaxRecordLength - MaxFixedRecordLength - 1)); 721 NullTerminatedString.push_back('\0'); 722 OS.emitBytes(NullTerminatedString); 723 } 724 725 void CodeViewDebug::emitTypeInformation() { 726 if (TypeTable.empty()) 727 return; 728 729 // Start the .debug$T or .debug$P section with 0x4. 730 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); 731 emitCodeViewMagicVersion(); 732 733 TypeTableCollection Table(TypeTable.records()); 734 TypeVisitorCallbackPipeline Pipeline; 735 736 // To emit type record using Codeview MCStreamer adapter 737 CVMCAdapter CVMCOS(OS, Table); 738 TypeRecordMapping typeMapping(CVMCOS); 739 Pipeline.addCallbackToPipeline(typeMapping); 740 741 Optional<TypeIndex> B = Table.getFirst(); 742 while (B) { 743 // This will fail if the record data is invalid. 744 CVType Record = Table.getType(*B); 745 746 Error E = codeview::visitTypeRecord(Record, *B, Pipeline); 747 748 if (E) { 749 logAllUnhandledErrors(std::move(E), errs(), "error: "); 750 llvm_unreachable("produced malformed type record"); 751 } 752 753 B = Table.getNext(*B); 754 } 755 } 756 757 void CodeViewDebug::emitTypeGlobalHashes() { 758 if (TypeTable.empty()) 759 return; 760 761 // Start the .debug$H section with the version and hash algorithm, currently 762 // hardcoded to version 0, SHA1. 763 OS.SwitchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection()); 764 765 OS.emitValueToAlignment(4); 766 OS.AddComment("Magic"); 767 OS.emitInt32(COFF::DEBUG_HASHES_SECTION_MAGIC); 768 OS.AddComment("Section Version"); 769 OS.emitInt16(0); 770 OS.AddComment("Hash Algorithm"); 771 OS.emitInt16(uint16_t(GlobalTypeHashAlg::SHA1_8)); 772 773 TypeIndex TI(TypeIndex::FirstNonSimpleIndex); 774 for (const auto &GHR : TypeTable.hashes()) { 775 if (OS.isVerboseAsm()) { 776 // Emit an EOL-comment describing which TypeIndex this hash corresponds 777 // to, as well as the stringified SHA1 hash. 778 SmallString<32> Comment; 779 raw_svector_ostream CommentOS(Comment); 780 CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR); 781 OS.AddComment(Comment); 782 ++TI; 783 } 784 assert(GHR.Hash.size() == 8); 785 StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()), 786 GHR.Hash.size()); 787 OS.emitBinaryData(S); 788 } 789 } 790 791 void CodeViewDebug::emitObjName() { 792 MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME); 793 794 StringRef PathRef(Asm->TM.Options.ObjectFilenameForDebug); 795 llvm::SmallString<256> PathStore(PathRef); 796 797 if (PathRef.empty() || PathRef == "-") { 798 // Don't emit the filename if we're writing to stdout or to /dev/null. 799 PathRef = {}; 800 } else { 801 llvm::sys::path::remove_dots(PathStore, /*remove_dot_dot=*/true); 802 PathRef = PathStore; 803 } 804 805 OS.AddComment("Signature"); 806 OS.emitIntValue(0, 4); 807 808 OS.AddComment("Object name"); 809 emitNullTerminatedSymbolName(OS, PathRef); 810 811 endSymbolRecord(CompilerEnd); 812 } 813 814 namespace { 815 struct Version { 816 int Part[4]; 817 }; 818 } // end anonymous namespace 819 820 // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out 821 // the version number. 822 static Version parseVersion(StringRef Name) { 823 Version V = {{0}}; 824 int N = 0; 825 for (const char C : Name) { 826 if (isdigit(C)) { 827 V.Part[N] *= 10; 828 V.Part[N] += C - '0'; 829 V.Part[N] = 830 std::min<int>(V.Part[N], std::numeric_limits<uint16_t>::max()); 831 } else if (C == '.') { 832 ++N; 833 if (N >= 4) 834 return V; 835 } else if (N > 0) 836 return V; 837 } 838 return V; 839 } 840 841 void CodeViewDebug::emitCompilerInformation() { 842 MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3); 843 uint32_t Flags = 0; 844 845 // The low byte of the flags indicates the source language. 846 Flags = CurrentSourceLanguage; 847 // TODO: Figure out which other flags need to be set. 848 if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) { 849 Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO); 850 } 851 using ArchType = llvm::Triple::ArchType; 852 ArchType Arch = Triple(MMI->getModule()->getTargetTriple()).getArch(); 853 if (Asm->TM.Options.Hotpatch || Arch == ArchType::thumb || 854 Arch == ArchType::aarch64) { 855 Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch); 856 } 857 858 OS.AddComment("Flags and language"); 859 OS.emitInt32(Flags); 860 861 OS.AddComment("CPUType"); 862 OS.emitInt16(static_cast<uint64_t>(TheCPU)); 863 864 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); 865 const MDNode *Node = *CUs->operands().begin(); 866 const auto *CU = cast<DICompileUnit>(Node); 867 868 StringRef CompilerVersion = CU->getProducer(); 869 Version FrontVer = parseVersion(CompilerVersion); 870 OS.AddComment("Frontend version"); 871 for (int N : FrontVer.Part) { 872 OS.emitInt16(N); 873 } 874 875 // Some Microsoft tools, like Binscope, expect a backend version number of at 876 // least 8.something, so we'll coerce the LLVM version into a form that 877 // guarantees it'll be big enough without really lying about the version. 878 int Major = 1000 * LLVM_VERSION_MAJOR + 879 10 * LLVM_VERSION_MINOR + 880 LLVM_VERSION_PATCH; 881 // Clamp it for builds that use unusually large version numbers. 882 Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max()); 883 Version BackVer = {{ Major, 0, 0, 0 }}; 884 OS.AddComment("Backend version"); 885 for (int N : BackVer.Part) 886 OS.emitInt16(N); 887 888 OS.AddComment("Null-terminated compiler version string"); 889 emitNullTerminatedSymbolName(OS, CompilerVersion); 890 891 endSymbolRecord(CompilerEnd); 892 } 893 894 static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable, 895 StringRef S) { 896 StringIdRecord SIR(TypeIndex(0x0), S); 897 return TypeTable.writeLeafType(SIR); 898 } 899 900 static std::string flattenCommandLine(ArrayRef<std::string> Args, 901 StringRef MainFilename) { 902 std::string FlatCmdLine; 903 raw_string_ostream OS(FlatCmdLine); 904 bool PrintedOneArg = false; 905 if (!StringRef(Args[0]).contains("-cc1")) { 906 llvm::sys::printArg(OS, "-cc1", /*Quote=*/true); 907 PrintedOneArg = true; 908 } 909 for (unsigned i = 0; i < Args.size(); i++) { 910 StringRef Arg = Args[i]; 911 if (Arg.empty()) 912 continue; 913 if (Arg == "-main-file-name" || Arg == "-o") { 914 i++; // Skip this argument and next one. 915 continue; 916 } 917 if (Arg.startswith("-object-file-name") || Arg == MainFilename) 918 continue; 919 if (PrintedOneArg) 920 OS << " "; 921 llvm::sys::printArg(OS, Arg, /*Quote=*/true); 922 PrintedOneArg = true; 923 } 924 OS.flush(); 925 return FlatCmdLine; 926 } 927 928 void CodeViewDebug::emitBuildInfo() { 929 // First, make LF_BUILDINFO. It's a sequence of strings with various bits of 930 // build info. The known prefix is: 931 // - Absolute path of current directory 932 // - Compiler path 933 // - Main source file path, relative to CWD or absolute 934 // - Type server PDB file 935 // - Canonical compiler command line 936 // If frontend and backend compilation are separated (think llc or LTO), it's 937 // not clear if the compiler path should refer to the executable for the 938 // frontend or the backend. Leave it blank for now. 939 TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {}; 940 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); 941 const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs. 942 const auto *CU = cast<DICompileUnit>(Node); 943 const DIFile *MainSourceFile = CU->getFile(); 944 BuildInfoArgs[BuildInfoRecord::CurrentDirectory] = 945 getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory()); 946 BuildInfoArgs[BuildInfoRecord::SourceFile] = 947 getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename()); 948 // FIXME: PDB is intentionally blank unless we implement /Zi type servers. 949 BuildInfoArgs[BuildInfoRecord::TypeServerPDB] = 950 getStringIdTypeIdx(TypeTable, ""); 951 if (Asm->TM.Options.MCOptions.Argv0 != nullptr) { 952 BuildInfoArgs[BuildInfoRecord::BuildTool] = 953 getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0); 954 BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx( 955 TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs, 956 MainSourceFile->getFilename())); 957 } 958 BuildInfoRecord BIR(BuildInfoArgs); 959 TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR); 960 961 // Make a new .debug$S subsection for the S_BUILDINFO record, which points 962 // from the module symbols into the type stream. 963 MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols); 964 MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO); 965 OS.AddComment("LF_BUILDINFO index"); 966 OS.emitInt32(BuildInfoIndex.getIndex()); 967 endSymbolRecord(BIEnd); 968 endCVSubsection(BISubsecEnd); 969 } 970 971 void CodeViewDebug::emitInlineeLinesSubsection() { 972 if (InlinedSubprograms.empty()) 973 return; 974 975 OS.AddComment("Inlinee lines subsection"); 976 MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines); 977 978 // We emit the checksum info for files. This is used by debuggers to 979 // determine if a pdb matches the source before loading it. Visual Studio, 980 // for instance, will display a warning that the breakpoints are not valid if 981 // the pdb does not match the source. 982 OS.AddComment("Inlinee lines signature"); 983 OS.emitInt32(unsigned(InlineeLinesSignature::Normal)); 984 985 for (const DISubprogram *SP : InlinedSubprograms) { 986 assert(TypeIndices.count({SP, nullptr})); 987 TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}]; 988 989 OS.AddBlankLine(); 990 unsigned FileId = maybeRecordFile(SP->getFile()); 991 OS.AddComment("Inlined function " + SP->getName() + " starts at " + 992 SP->getFilename() + Twine(':') + Twine(SP->getLine())); 993 OS.AddBlankLine(); 994 OS.AddComment("Type index of inlined function"); 995 OS.emitInt32(InlineeIdx.getIndex()); 996 OS.AddComment("Offset into filechecksum table"); 997 OS.emitCVFileChecksumOffsetDirective(FileId); 998 OS.AddComment("Starting line number"); 999 OS.emitInt32(SP->getLine()); 1000 } 1001 1002 endCVSubsection(InlineEnd); 1003 } 1004 1005 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, 1006 const DILocation *InlinedAt, 1007 const InlineSite &Site) { 1008 assert(TypeIndices.count({Site.Inlinee, nullptr})); 1009 TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}]; 1010 1011 // SymbolRecord 1012 MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE); 1013 1014 OS.AddComment("PtrParent"); 1015 OS.emitInt32(0); 1016 OS.AddComment("PtrEnd"); 1017 OS.emitInt32(0); 1018 OS.AddComment("Inlinee type index"); 1019 OS.emitInt32(InlineeIdx.getIndex()); 1020 1021 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); 1022 unsigned StartLineNum = Site.Inlinee->getLine(); 1023 1024 OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, 1025 FI.Begin, FI.End); 1026 1027 endSymbolRecord(InlineEnd); 1028 1029 emitLocalVariableList(FI, Site.InlinedLocals); 1030 1031 // Recurse on child inlined call sites before closing the scope. 1032 for (const DILocation *ChildSite : Site.ChildSites) { 1033 auto I = FI.InlineSites.find(ChildSite); 1034 assert(I != FI.InlineSites.end() && 1035 "child site not in function inline site map"); 1036 emitInlinedCallSite(FI, ChildSite, I->second); 1037 } 1038 1039 // Close the scope. 1040 emitEndSymbolRecord(SymbolKind::S_INLINESITE_END); 1041 } 1042 1043 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { 1044 // If we have a symbol, it may be in a section that is COMDAT. If so, find the 1045 // comdat key. A section may be comdat because of -ffunction-sections or 1046 // because it is comdat in the IR. 1047 MCSectionCOFF *GVSec = 1048 GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr; 1049 const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; 1050 1051 MCSectionCOFF *DebugSec = cast<MCSectionCOFF>( 1052 Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); 1053 DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym); 1054 1055 OS.SwitchSection(DebugSec); 1056 1057 // Emit the magic version number if this is the first time we've switched to 1058 // this section. 1059 if (ComdatDebugSections.insert(DebugSec).second) 1060 emitCodeViewMagicVersion(); 1061 } 1062 1063 // Emit an S_THUNK32/S_END symbol pair for a thunk routine. 1064 // The only supported thunk ordinal is currently the standard type. 1065 void CodeViewDebug::emitDebugInfoForThunk(const Function *GV, 1066 FunctionInfo &FI, 1067 const MCSymbol *Fn) { 1068 std::string FuncName = 1069 std::string(GlobalValue::dropLLVMManglingEscape(GV->getName())); 1070 const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind. 1071 1072 OS.AddComment("Symbol subsection for " + Twine(FuncName)); 1073 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); 1074 1075 // Emit S_THUNK32 1076 MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32); 1077 OS.AddComment("PtrParent"); 1078 OS.emitInt32(0); 1079 OS.AddComment("PtrEnd"); 1080 OS.emitInt32(0); 1081 OS.AddComment("PtrNext"); 1082 OS.emitInt32(0); 1083 OS.AddComment("Thunk section relative address"); 1084 OS.EmitCOFFSecRel32(Fn, /*Offset=*/0); 1085 OS.AddComment("Thunk section index"); 1086 OS.EmitCOFFSectionIndex(Fn); 1087 OS.AddComment("Code size"); 1088 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2); 1089 OS.AddComment("Ordinal"); 1090 OS.emitInt8(unsigned(ordinal)); 1091 OS.AddComment("Function name"); 1092 emitNullTerminatedSymbolName(OS, FuncName); 1093 // Additional fields specific to the thunk ordinal would go here. 1094 endSymbolRecord(ThunkRecordEnd); 1095 1096 // Local variables/inlined routines are purposely omitted here. The point of 1097 // marking this as a thunk is so Visual Studio will NOT stop in this routine. 1098 1099 // Emit S_PROC_ID_END 1100 emitEndSymbolRecord(SymbolKind::S_PROC_ID_END); 1101 1102 endCVSubsection(SymbolsEnd); 1103 } 1104 1105 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, 1106 FunctionInfo &FI) { 1107 // For each function there is a separate subsection which holds the PC to 1108 // file:line table. 1109 const MCSymbol *Fn = Asm->getSymbol(GV); 1110 assert(Fn); 1111 1112 // Switch to the to a comdat section, if appropriate. 1113 switchToDebugSectionForSymbol(Fn); 1114 1115 std::string FuncName; 1116 auto *SP = GV->getSubprogram(); 1117 assert(SP); 1118 setCurrentSubprogram(SP); 1119 1120 if (SP->isThunk()) { 1121 emitDebugInfoForThunk(GV, FI, Fn); 1122 return; 1123 } 1124 1125 // If we have a display name, build the fully qualified name by walking the 1126 // chain of scopes. 1127 if (!SP->getName().empty()) 1128 FuncName = getFullyQualifiedName(SP->getScope(), SP->getName()); 1129 1130 // If our DISubprogram name is empty, use the mangled name. 1131 if (FuncName.empty()) 1132 FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName())); 1133 1134 // Emit FPO data, but only on 32-bit x86. No other platforms use it. 1135 if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86) 1136 OS.EmitCVFPOData(Fn); 1137 1138 // Emit a symbol subsection, required by VS2012+ to find function boundaries. 1139 OS.AddComment("Symbol subsection for " + Twine(FuncName)); 1140 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); 1141 { 1142 SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID 1143 : SymbolKind::S_GPROC32_ID; 1144 MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind); 1145 1146 // These fields are filled in by tools like CVPACK which run after the fact. 1147 OS.AddComment("PtrParent"); 1148 OS.emitInt32(0); 1149 OS.AddComment("PtrEnd"); 1150 OS.emitInt32(0); 1151 OS.AddComment("PtrNext"); 1152 OS.emitInt32(0); 1153 // This is the important bit that tells the debugger where the function 1154 // code is located and what's its size: 1155 OS.AddComment("Code size"); 1156 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); 1157 OS.AddComment("Offset after prologue"); 1158 OS.emitInt32(0); 1159 OS.AddComment("Offset before epilogue"); 1160 OS.emitInt32(0); 1161 OS.AddComment("Function type index"); 1162 OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex()); 1163 OS.AddComment("Function section relative address"); 1164 OS.EmitCOFFSecRel32(Fn, /*Offset=*/0); 1165 OS.AddComment("Function section index"); 1166 OS.EmitCOFFSectionIndex(Fn); 1167 OS.AddComment("Flags"); 1168 OS.emitInt8(0); 1169 // Emit the function display name as a null-terminated string. 1170 OS.AddComment("Function name"); 1171 // Truncate the name so we won't overflow the record length field. 1172 emitNullTerminatedSymbolName(OS, FuncName); 1173 endSymbolRecord(ProcRecordEnd); 1174 1175 MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC); 1176 // Subtract out the CSR size since MSVC excludes that and we include it. 1177 OS.AddComment("FrameSize"); 1178 OS.emitInt32(FI.FrameSize - FI.CSRSize); 1179 OS.AddComment("Padding"); 1180 OS.emitInt32(0); 1181 OS.AddComment("Offset of padding"); 1182 OS.emitInt32(0); 1183 OS.AddComment("Bytes of callee saved registers"); 1184 OS.emitInt32(FI.CSRSize); 1185 OS.AddComment("Exception handler offset"); 1186 OS.emitInt32(0); 1187 OS.AddComment("Exception handler section"); 1188 OS.emitInt16(0); 1189 OS.AddComment("Flags (defines frame register)"); 1190 OS.emitInt32(uint32_t(FI.FrameProcOpts)); 1191 endSymbolRecord(FrameProcEnd); 1192 1193 emitLocalVariableList(FI, FI.Locals); 1194 emitGlobalVariableList(FI.Globals); 1195 emitLexicalBlockList(FI.ChildBlocks, FI); 1196 1197 // Emit inlined call site information. Only emit functions inlined directly 1198 // into the parent function. We'll emit the other sites recursively as part 1199 // of their parent inline site. 1200 for (const DILocation *InlinedAt : FI.ChildSites) { 1201 auto I = FI.InlineSites.find(InlinedAt); 1202 assert(I != FI.InlineSites.end() && 1203 "child site not in function inline site map"); 1204 emitInlinedCallSite(FI, InlinedAt, I->second); 1205 } 1206 1207 for (auto Annot : FI.Annotations) { 1208 MCSymbol *Label = Annot.first; 1209 MDTuple *Strs = cast<MDTuple>(Annot.second); 1210 MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION); 1211 OS.EmitCOFFSecRel32(Label, /*Offset=*/0); 1212 // FIXME: Make sure we don't overflow the max record size. 1213 OS.EmitCOFFSectionIndex(Label); 1214 OS.emitInt16(Strs->getNumOperands()); 1215 for (Metadata *MD : Strs->operands()) { 1216 // MDStrings are null terminated, so we can do EmitBytes and get the 1217 // nice .asciz directive. 1218 StringRef Str = cast<MDString>(MD)->getString(); 1219 assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString"); 1220 OS.emitBytes(StringRef(Str.data(), Str.size() + 1)); 1221 } 1222 endSymbolRecord(AnnotEnd); 1223 } 1224 1225 for (auto HeapAllocSite : FI.HeapAllocSites) { 1226 const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite); 1227 const MCSymbol *EndLabel = std::get<1>(HeapAllocSite); 1228 const DIType *DITy = std::get<2>(HeapAllocSite); 1229 MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE); 1230 OS.AddComment("Call site offset"); 1231 OS.EmitCOFFSecRel32(BeginLabel, /*Offset=*/0); 1232 OS.AddComment("Call site section index"); 1233 OS.EmitCOFFSectionIndex(BeginLabel); 1234 OS.AddComment("Call instruction length"); 1235 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2); 1236 OS.AddComment("Type index"); 1237 OS.emitInt32(getCompleteTypeIndex(DITy).getIndex()); 1238 endSymbolRecord(HeapAllocEnd); 1239 } 1240 1241 if (SP != nullptr) 1242 emitDebugInfoForUDTs(LocalUDTs); 1243 1244 // We're done with this function. 1245 emitEndSymbolRecord(SymbolKind::S_PROC_ID_END); 1246 } 1247 endCVSubsection(SymbolsEnd); 1248 1249 // We have an assembler directive that takes care of the whole line table. 1250 OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End); 1251 } 1252 1253 CodeViewDebug::LocalVarDefRange 1254 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { 1255 LocalVarDefRange DR; 1256 DR.InMemory = -1; 1257 DR.DataOffset = Offset; 1258 assert(DR.DataOffset == Offset && "truncation"); 1259 DR.IsSubfield = 0; 1260 DR.StructOffset = 0; 1261 DR.CVRegister = CVRegister; 1262 return DR; 1263 } 1264 1265 void CodeViewDebug::collectVariableInfoFromMFTable( 1266 DenseSet<InlinedEntity> &Processed) { 1267 const MachineFunction &MF = *Asm->MF; 1268 const TargetSubtargetInfo &TSI = MF.getSubtarget(); 1269 const TargetFrameLowering *TFI = TSI.getFrameLowering(); 1270 const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); 1271 1272 for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) { 1273 if (!VI.Var) 1274 continue; 1275 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && 1276 "Expected inlined-at fields to agree"); 1277 1278 Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt())); 1279 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); 1280 1281 // If variable scope is not found then skip this variable. 1282 if (!Scope) 1283 continue; 1284 1285 // If the variable has an attached offset expression, extract it. 1286 // FIXME: Try to handle DW_OP_deref as well. 1287 int64_t ExprOffset = 0; 1288 bool Deref = false; 1289 if (VI.Expr) { 1290 // If there is one DW_OP_deref element, use offset of 0 and keep going. 1291 if (VI.Expr->getNumElements() == 1 && 1292 VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref) 1293 Deref = true; 1294 else if (!VI.Expr->extractIfOffset(ExprOffset)) 1295 continue; 1296 } 1297 1298 // Get the frame register used and the offset. 1299 Register FrameReg; 1300 StackOffset FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); 1301 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg); 1302 1303 assert(!FrameOffset.getScalable() && 1304 "Frame offsets with a scalable component are not supported"); 1305 1306 // Calculate the label ranges. 1307 LocalVarDefRange DefRange = 1308 createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset); 1309 1310 for (const InsnRange &Range : Scope->getRanges()) { 1311 const MCSymbol *Begin = getLabelBeforeInsn(Range.first); 1312 const MCSymbol *End = getLabelAfterInsn(Range.second); 1313 End = End ? End : Asm->getFunctionEnd(); 1314 DefRange.Ranges.emplace_back(Begin, End); 1315 } 1316 1317 LocalVariable Var; 1318 Var.DIVar = VI.Var; 1319 Var.DefRanges.emplace_back(std::move(DefRange)); 1320 if (Deref) 1321 Var.UseReferenceType = true; 1322 1323 recordLocalVariable(std::move(Var), Scope); 1324 } 1325 } 1326 1327 static bool canUseReferenceType(const DbgVariableLocation &Loc) { 1328 return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0; 1329 } 1330 1331 static bool needsReferenceType(const DbgVariableLocation &Loc) { 1332 return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0; 1333 } 1334 1335 void CodeViewDebug::calculateRanges( 1336 LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) { 1337 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); 1338 1339 // Calculate the definition ranges. 1340 for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) { 1341 const auto &Entry = *I; 1342 if (!Entry.isDbgValue()) 1343 continue; 1344 const MachineInstr *DVInst = Entry.getInstr(); 1345 assert(DVInst->isDebugValue() && "Invalid History entry"); 1346 // FIXME: Find a way to represent constant variables, since they are 1347 // relatively common. 1348 Optional<DbgVariableLocation> Location = 1349 DbgVariableLocation::extractFromMachineInstruction(*DVInst); 1350 if (!Location) 1351 continue; 1352 1353 // CodeView can only express variables in register and variables in memory 1354 // at a constant offset from a register. However, for variables passed 1355 // indirectly by pointer, it is common for that pointer to be spilled to a 1356 // stack location. For the special case of one offseted load followed by a 1357 // zero offset load (a pointer spilled to the stack), we change the type of 1358 // the local variable from a value type to a reference type. This tricks the 1359 // debugger into doing the load for us. 1360 if (Var.UseReferenceType) { 1361 // We're using a reference type. Drop the last zero offset load. 1362 if (canUseReferenceType(*Location)) 1363 Location->LoadChain.pop_back(); 1364 else 1365 continue; 1366 } else if (needsReferenceType(*Location)) { 1367 // This location can't be expressed without switching to a reference type. 1368 // Start over using that. 1369 Var.UseReferenceType = true; 1370 Var.DefRanges.clear(); 1371 calculateRanges(Var, Entries); 1372 return; 1373 } 1374 1375 // We can only handle a register or an offseted load of a register. 1376 if (Location->Register == 0 || Location->LoadChain.size() > 1) 1377 continue; 1378 { 1379 LocalVarDefRange DR; 1380 DR.CVRegister = TRI->getCodeViewRegNum(Location->Register); 1381 DR.InMemory = !Location->LoadChain.empty(); 1382 DR.DataOffset = 1383 !Location->LoadChain.empty() ? Location->LoadChain.back() : 0; 1384 if (Location->FragmentInfo) { 1385 DR.IsSubfield = true; 1386 DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8; 1387 } else { 1388 DR.IsSubfield = false; 1389 DR.StructOffset = 0; 1390 } 1391 1392 if (Var.DefRanges.empty() || 1393 Var.DefRanges.back().isDifferentLocation(DR)) { 1394 Var.DefRanges.emplace_back(std::move(DR)); 1395 } 1396 } 1397 1398 // Compute the label range. 1399 const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr()); 1400 const MCSymbol *End; 1401 if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) { 1402 auto &EndingEntry = Entries[Entry.getEndIndex()]; 1403 End = EndingEntry.isDbgValue() 1404 ? getLabelBeforeInsn(EndingEntry.getInstr()) 1405 : getLabelAfterInsn(EndingEntry.getInstr()); 1406 } else 1407 End = Asm->getFunctionEnd(); 1408 1409 // If the last range end is our begin, just extend the last range. 1410 // Otherwise make a new range. 1411 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R = 1412 Var.DefRanges.back().Ranges; 1413 if (!R.empty() && R.back().second == Begin) 1414 R.back().second = End; 1415 else 1416 R.emplace_back(Begin, End); 1417 1418 // FIXME: Do more range combining. 1419 } 1420 } 1421 1422 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { 1423 DenseSet<InlinedEntity> Processed; 1424 // Grab the variable info that was squirreled away in the MMI side-table. 1425 collectVariableInfoFromMFTable(Processed); 1426 1427 for (const auto &I : DbgValues) { 1428 InlinedEntity IV = I.first; 1429 if (Processed.count(IV)) 1430 continue; 1431 const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first); 1432 const DILocation *InlinedAt = IV.second; 1433 1434 // Instruction ranges, specifying where IV is accessible. 1435 const auto &Entries = I.second; 1436 1437 LexicalScope *Scope = nullptr; 1438 if (InlinedAt) 1439 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt); 1440 else 1441 Scope = LScopes.findLexicalScope(DIVar->getScope()); 1442 // If variable scope is not found then skip this variable. 1443 if (!Scope) 1444 continue; 1445 1446 LocalVariable Var; 1447 Var.DIVar = DIVar; 1448 1449 calculateRanges(Var, Entries); 1450 recordLocalVariable(std::move(Var), Scope); 1451 } 1452 } 1453 1454 void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) { 1455 const TargetSubtargetInfo &TSI = MF->getSubtarget(); 1456 const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); 1457 const MachineFrameInfo &MFI = MF->getFrameInfo(); 1458 const Function &GV = MF->getFunction(); 1459 auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()}); 1460 assert(Insertion.second && "function already has info"); 1461 CurFn = Insertion.first->second.get(); 1462 CurFn->FuncId = NextFuncId++; 1463 CurFn->Begin = Asm->getFunctionBegin(); 1464 1465 // The S_FRAMEPROC record reports the stack size, and how many bytes of 1466 // callee-saved registers were used. For targets that don't use a PUSH 1467 // instruction (AArch64), this will be zero. 1468 CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters(); 1469 CurFn->FrameSize = MFI.getStackSize(); 1470 CurFn->OffsetAdjustment = MFI.getOffsetAdjustment(); 1471 CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF); 1472 1473 // For this function S_FRAMEPROC record, figure out which codeview register 1474 // will be the frame pointer. 1475 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None. 1476 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None. 1477 if (CurFn->FrameSize > 0) { 1478 if (!TSI.getFrameLowering()->hasFP(*MF)) { 1479 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr; 1480 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr; 1481 } else { 1482 // If there is an FP, parameters are always relative to it. 1483 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr; 1484 if (CurFn->HasStackRealignment) { 1485 // If the stack needs realignment, locals are relative to SP or VFRAME. 1486 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr; 1487 } else { 1488 // Otherwise, locals are relative to EBP, and we probably have VLAs or 1489 // other stack adjustments. 1490 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr; 1491 } 1492 } 1493 } 1494 1495 // Compute other frame procedure options. 1496 FrameProcedureOptions FPO = FrameProcedureOptions::None; 1497 if (MFI.hasVarSizedObjects()) 1498 FPO |= FrameProcedureOptions::HasAlloca; 1499 if (MF->exposesReturnsTwice()) 1500 FPO |= FrameProcedureOptions::HasSetJmp; 1501 // FIXME: Set HasLongJmp if we ever track that info. 1502 if (MF->hasInlineAsm()) 1503 FPO |= FrameProcedureOptions::HasInlineAssembly; 1504 if (GV.hasPersonalityFn()) { 1505 if (isAsynchronousEHPersonality( 1506 classifyEHPersonality(GV.getPersonalityFn()))) 1507 FPO |= FrameProcedureOptions::HasStructuredExceptionHandling; 1508 else 1509 FPO |= FrameProcedureOptions::HasExceptionHandling; 1510 } 1511 if (GV.hasFnAttribute(Attribute::InlineHint)) 1512 FPO |= FrameProcedureOptions::MarkedInline; 1513 if (GV.hasFnAttribute(Attribute::Naked)) 1514 FPO |= FrameProcedureOptions::Naked; 1515 if (MFI.hasStackProtectorIndex()) 1516 FPO |= FrameProcedureOptions::SecurityChecks; 1517 FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U); 1518 FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U); 1519 if (Asm->TM.getOptLevel() != CodeGenOpt::None && 1520 !GV.hasOptSize() && !GV.hasOptNone()) 1521 FPO |= FrameProcedureOptions::OptimizedForSpeed; 1522 if (GV.hasProfileData()) { 1523 FPO |= FrameProcedureOptions::ValidProfileCounts; 1524 FPO |= FrameProcedureOptions::ProfileGuidedOptimization; 1525 } 1526 // FIXME: Set GuardCfg when it is implemented. 1527 CurFn->FrameProcOpts = FPO; 1528 1529 OS.EmitCVFuncIdDirective(CurFn->FuncId); 1530 1531 // Find the end of the function prolog. First known non-DBG_VALUE and 1532 // non-frame setup location marks the beginning of the function body. 1533 // FIXME: is there a simpler a way to do this? Can we just search 1534 // for the first instruction of the function, not the last of the prolog? 1535 DebugLoc PrologEndLoc; 1536 bool EmptyPrologue = true; 1537 for (const auto &MBB : *MF) { 1538 for (const auto &MI : MBB) { 1539 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && 1540 MI.getDebugLoc()) { 1541 PrologEndLoc = MI.getDebugLoc(); 1542 break; 1543 } else if (!MI.isMetaInstruction()) { 1544 EmptyPrologue = false; 1545 } 1546 } 1547 } 1548 1549 // Record beginning of function if we have a non-empty prologue. 1550 if (PrologEndLoc && !EmptyPrologue) { 1551 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); 1552 maybeRecordLocation(FnStartDL, MF); 1553 } 1554 1555 // Find heap alloc sites and emit labels around them. 1556 for (const auto &MBB : *MF) { 1557 for (const auto &MI : MBB) { 1558 if (MI.getHeapAllocMarker()) { 1559 requestLabelBeforeInsn(&MI); 1560 requestLabelAfterInsn(&MI); 1561 } 1562 } 1563 } 1564 } 1565 1566 static bool shouldEmitUdt(const DIType *T) { 1567 if (!T) 1568 return false; 1569 1570 // MSVC does not emit UDTs for typedefs that are scoped to classes. 1571 if (T->getTag() == dwarf::DW_TAG_typedef) { 1572 if (DIScope *Scope = T->getScope()) { 1573 switch (Scope->getTag()) { 1574 case dwarf::DW_TAG_structure_type: 1575 case dwarf::DW_TAG_class_type: 1576 case dwarf::DW_TAG_union_type: 1577 return false; 1578 default: 1579 // do nothing. 1580 ; 1581 } 1582 } 1583 } 1584 1585 while (true) { 1586 if (!T || T->isForwardDecl()) 1587 return false; 1588 1589 const DIDerivedType *DT = dyn_cast<DIDerivedType>(T); 1590 if (!DT) 1591 return true; 1592 T = DT->getBaseType(); 1593 } 1594 return true; 1595 } 1596 1597 void CodeViewDebug::addToUDTs(const DIType *Ty) { 1598 // Don't record empty UDTs. 1599 if (Ty->getName().empty()) 1600 return; 1601 if (!shouldEmitUdt(Ty)) 1602 return; 1603 1604 SmallVector<StringRef, 5> ParentScopeNames; 1605 const DISubprogram *ClosestSubprogram = 1606 collectParentScopeNames(Ty->getScope(), ParentScopeNames); 1607 1608 std::string FullyQualifiedName = 1609 formatNestedName(ParentScopeNames, getPrettyScopeName(Ty)); 1610 1611 if (ClosestSubprogram == nullptr) { 1612 GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty); 1613 } else if (ClosestSubprogram == CurrentSubprogram) { 1614 LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty); 1615 } 1616 1617 // TODO: What if the ClosestSubprogram is neither null or the current 1618 // subprogram? Currently, the UDT just gets dropped on the floor. 1619 // 1620 // The current behavior is not desirable. To get maximal fidelity, we would 1621 // need to perform all type translation before beginning emission of .debug$S 1622 // and then make LocalUDTs a member of FunctionInfo 1623 } 1624 1625 TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) { 1626 // Generic dispatch for lowering an unknown type. 1627 switch (Ty->getTag()) { 1628 case dwarf::DW_TAG_array_type: 1629 return lowerTypeArray(cast<DICompositeType>(Ty)); 1630 case dwarf::DW_TAG_typedef: 1631 return lowerTypeAlias(cast<DIDerivedType>(Ty)); 1632 case dwarf::DW_TAG_base_type: 1633 return lowerTypeBasic(cast<DIBasicType>(Ty)); 1634 case dwarf::DW_TAG_pointer_type: 1635 if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type") 1636 return lowerTypeVFTableShape(cast<DIDerivedType>(Ty)); 1637 LLVM_FALLTHROUGH; 1638 case dwarf::DW_TAG_reference_type: 1639 case dwarf::DW_TAG_rvalue_reference_type: 1640 return lowerTypePointer(cast<DIDerivedType>(Ty)); 1641 case dwarf::DW_TAG_ptr_to_member_type: 1642 return lowerTypeMemberPointer(cast<DIDerivedType>(Ty)); 1643 case dwarf::DW_TAG_restrict_type: 1644 case dwarf::DW_TAG_const_type: 1645 case dwarf::DW_TAG_volatile_type: 1646 // TODO: add support for DW_TAG_atomic_type here 1647 return lowerTypeModifier(cast<DIDerivedType>(Ty)); 1648 case dwarf::DW_TAG_subroutine_type: 1649 if (ClassTy) { 1650 // The member function type of a member function pointer has no 1651 // ThisAdjustment. 1652 return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy, 1653 /*ThisAdjustment=*/0, 1654 /*IsStaticMethod=*/false); 1655 } 1656 return lowerTypeFunction(cast<DISubroutineType>(Ty)); 1657 case dwarf::DW_TAG_enumeration_type: 1658 return lowerTypeEnum(cast<DICompositeType>(Ty)); 1659 case dwarf::DW_TAG_class_type: 1660 case dwarf::DW_TAG_structure_type: 1661 return lowerTypeClass(cast<DICompositeType>(Ty)); 1662 case dwarf::DW_TAG_union_type: 1663 return lowerTypeUnion(cast<DICompositeType>(Ty)); 1664 case dwarf::DW_TAG_string_type: 1665 return lowerTypeString(cast<DIStringType>(Ty)); 1666 case dwarf::DW_TAG_unspecified_type: 1667 if (Ty->getName() == "decltype(nullptr)") 1668 return TypeIndex::NullptrT(); 1669 return TypeIndex::None(); 1670 default: 1671 // Use the null type index. 1672 return TypeIndex(); 1673 } 1674 } 1675 1676 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) { 1677 TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType()); 1678 StringRef TypeName = Ty->getName(); 1679 1680 addToUDTs(Ty); 1681 1682 if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) && 1683 TypeName == "HRESULT") 1684 return TypeIndex(SimpleTypeKind::HResult); 1685 if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) && 1686 TypeName == "wchar_t") 1687 return TypeIndex(SimpleTypeKind::WideCharacter); 1688 1689 return UnderlyingTypeIndex; 1690 } 1691 1692 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) { 1693 const DIType *ElementType = Ty->getBaseType(); 1694 TypeIndex ElementTypeIndex = getTypeIndex(ElementType); 1695 // IndexType is size_t, which depends on the bitness of the target. 1696 TypeIndex IndexType = getPointerSizeInBytes() == 8 1697 ? TypeIndex(SimpleTypeKind::UInt64Quad) 1698 : TypeIndex(SimpleTypeKind::UInt32Long); 1699 1700 uint64_t ElementSize = getBaseTypeSize(ElementType) / 8; 1701 1702 // Add subranges to array type. 1703 DINodeArray Elements = Ty->getElements(); 1704 for (int i = Elements.size() - 1; i >= 0; --i) { 1705 const DINode *Element = Elements[i]; 1706 assert(Element->getTag() == dwarf::DW_TAG_subrange_type); 1707 1708 const DISubrange *Subrange = cast<DISubrange>(Element); 1709 int64_t Count = -1; 1710 1711 // If Subrange has a Count field, use it. 1712 // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1), 1713 // where lowerbound is from the LowerBound field of the Subrange, 1714 // or the language default lowerbound if that field is unspecified. 1715 if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt *>()) 1716 Count = CI->getSExtValue(); 1717 else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt *>()) { 1718 // Fortran uses 1 as the default lowerbound; other languages use 0. 1719 int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0; 1720 auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>(); 1721 Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound; 1722 Count = UI->getSExtValue() - Lowerbound + 1; 1723 } 1724 1725 // Forward declarations of arrays without a size and VLAs use a count of -1. 1726 // Emit a count of zero in these cases to match what MSVC does for arrays 1727 // without a size. MSVC doesn't support VLAs, so it's not clear what we 1728 // should do for them even if we could distinguish them. 1729 if (Count == -1) 1730 Count = 0; 1731 1732 // Update the element size and element type index for subsequent subranges. 1733 ElementSize *= Count; 1734 1735 // If this is the outermost array, use the size from the array. It will be 1736 // more accurate if we had a VLA or an incomplete element type size. 1737 uint64_t ArraySize = 1738 (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize; 1739 1740 StringRef Name = (i == 0) ? Ty->getName() : ""; 1741 ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name); 1742 ElementTypeIndex = TypeTable.writeLeafType(AR); 1743 } 1744 1745 return ElementTypeIndex; 1746 } 1747 1748 // This function lowers a Fortran character type (DIStringType). 1749 // Note that it handles only the character*n variant (using SizeInBits 1750 // field in DIString to describe the type size) at the moment. 1751 // Other variants (leveraging the StringLength and StringLengthExp 1752 // fields in DIStringType) remain TBD. 1753 TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) { 1754 TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter); 1755 uint64_t ArraySize = Ty->getSizeInBits() >> 3; 1756 StringRef Name = Ty->getName(); 1757 // IndexType is size_t, which depends on the bitness of the target. 1758 TypeIndex IndexType = getPointerSizeInBytes() == 8 1759 ? TypeIndex(SimpleTypeKind::UInt64Quad) 1760 : TypeIndex(SimpleTypeKind::UInt32Long); 1761 1762 // Create a type of character array of ArraySize. 1763 ArrayRecord AR(CharType, IndexType, ArraySize, Name); 1764 1765 return TypeTable.writeLeafType(AR); 1766 } 1767 1768 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { 1769 TypeIndex Index; 1770 dwarf::TypeKind Kind; 1771 uint32_t ByteSize; 1772 1773 Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding()); 1774 ByteSize = Ty->getSizeInBits() / 8; 1775 1776 SimpleTypeKind STK = SimpleTypeKind::None; 1777 switch (Kind) { 1778 case dwarf::DW_ATE_address: 1779 // FIXME: Translate 1780 break; 1781 case dwarf::DW_ATE_boolean: 1782 switch (ByteSize) { 1783 case 1: STK = SimpleTypeKind::Boolean8; break; 1784 case 2: STK = SimpleTypeKind::Boolean16; break; 1785 case 4: STK = SimpleTypeKind::Boolean32; break; 1786 case 8: STK = SimpleTypeKind::Boolean64; break; 1787 case 16: STK = SimpleTypeKind::Boolean128; break; 1788 } 1789 break; 1790 case dwarf::DW_ATE_complex_float: 1791 switch (ByteSize) { 1792 case 2: STK = SimpleTypeKind::Complex16; break; 1793 case 4: STK = SimpleTypeKind::Complex32; break; 1794 case 8: STK = SimpleTypeKind::Complex64; break; 1795 case 10: STK = SimpleTypeKind::Complex80; break; 1796 case 16: STK = SimpleTypeKind::Complex128; break; 1797 } 1798 break; 1799 case dwarf::DW_ATE_float: 1800 switch (ByteSize) { 1801 case 2: STK = SimpleTypeKind::Float16; break; 1802 case 4: STK = SimpleTypeKind::Float32; break; 1803 case 6: STK = SimpleTypeKind::Float48; break; 1804 case 8: STK = SimpleTypeKind::Float64; break; 1805 case 10: STK = SimpleTypeKind::Float80; break; 1806 case 16: STK = SimpleTypeKind::Float128; break; 1807 } 1808 break; 1809 case dwarf::DW_ATE_signed: 1810 switch (ByteSize) { 1811 case 1: STK = SimpleTypeKind::SignedCharacter; break; 1812 case 2: STK = SimpleTypeKind::Int16Short; break; 1813 case 4: STK = SimpleTypeKind::Int32; break; 1814 case 8: STK = SimpleTypeKind::Int64Quad; break; 1815 case 16: STK = SimpleTypeKind::Int128Oct; break; 1816 } 1817 break; 1818 case dwarf::DW_ATE_unsigned: 1819 switch (ByteSize) { 1820 case 1: STK = SimpleTypeKind::UnsignedCharacter; break; 1821 case 2: STK = SimpleTypeKind::UInt16Short; break; 1822 case 4: STK = SimpleTypeKind::UInt32; break; 1823 case 8: STK = SimpleTypeKind::UInt64Quad; break; 1824 case 16: STK = SimpleTypeKind::UInt128Oct; break; 1825 } 1826 break; 1827 case dwarf::DW_ATE_UTF: 1828 switch (ByteSize) { 1829 case 2: STK = SimpleTypeKind::Character16; break; 1830 case 4: STK = SimpleTypeKind::Character32; break; 1831 } 1832 break; 1833 case dwarf::DW_ATE_signed_char: 1834 if (ByteSize == 1) 1835 STK = SimpleTypeKind::SignedCharacter; 1836 break; 1837 case dwarf::DW_ATE_unsigned_char: 1838 if (ByteSize == 1) 1839 STK = SimpleTypeKind::UnsignedCharacter; 1840 break; 1841 default: 1842 break; 1843 } 1844 1845 // Apply some fixups based on the source-level type name. 1846 // Include some amount of canonicalization from an old naming scheme Clang 1847 // used to use for integer types (in an outdated effort to be compatible with 1848 // GCC's debug info/GDB's behavior, which has since been addressed). 1849 if (STK == SimpleTypeKind::Int32 && 1850 (Ty->getName() == "long int" || Ty->getName() == "long")) 1851 STK = SimpleTypeKind::Int32Long; 1852 if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" || 1853 Ty->getName() == "unsigned long")) 1854 STK = SimpleTypeKind::UInt32Long; 1855 if (STK == SimpleTypeKind::UInt16Short && 1856 (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t")) 1857 STK = SimpleTypeKind::WideCharacter; 1858 if ((STK == SimpleTypeKind::SignedCharacter || 1859 STK == SimpleTypeKind::UnsignedCharacter) && 1860 Ty->getName() == "char") 1861 STK = SimpleTypeKind::NarrowCharacter; 1862 1863 return TypeIndex(STK); 1864 } 1865 1866 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty, 1867 PointerOptions PO) { 1868 TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); 1869 1870 // Pointers to simple types without any options can use SimpleTypeMode, rather 1871 // than having a dedicated pointer type record. 1872 if (PointeeTI.isSimple() && PO == PointerOptions::None && 1873 PointeeTI.getSimpleMode() == SimpleTypeMode::Direct && 1874 Ty->getTag() == dwarf::DW_TAG_pointer_type) { 1875 SimpleTypeMode Mode = Ty->getSizeInBits() == 64 1876 ? SimpleTypeMode::NearPointer64 1877 : SimpleTypeMode::NearPointer32; 1878 return TypeIndex(PointeeTI.getSimpleKind(), Mode); 1879 } 1880 1881 PointerKind PK = 1882 Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32; 1883 PointerMode PM = PointerMode::Pointer; 1884 switch (Ty->getTag()) { 1885 default: llvm_unreachable("not a pointer tag type"); 1886 case dwarf::DW_TAG_pointer_type: 1887 PM = PointerMode::Pointer; 1888 break; 1889 case dwarf::DW_TAG_reference_type: 1890 PM = PointerMode::LValueReference; 1891 break; 1892 case dwarf::DW_TAG_rvalue_reference_type: 1893 PM = PointerMode::RValueReference; 1894 break; 1895 } 1896 1897 if (Ty->isObjectPointer()) 1898 PO |= PointerOptions::Const; 1899 1900 PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8); 1901 return TypeTable.writeLeafType(PR); 1902 } 1903 1904 static PointerToMemberRepresentation 1905 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) { 1906 // SizeInBytes being zero generally implies that the member pointer type was 1907 // incomplete, which can happen if it is part of a function prototype. In this 1908 // case, use the unknown model instead of the general model. 1909 if (IsPMF) { 1910 switch (Flags & DINode::FlagPtrToMemberRep) { 1911 case 0: 1912 return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown 1913 : PointerToMemberRepresentation::GeneralFunction; 1914 case DINode::FlagSingleInheritance: 1915 return PointerToMemberRepresentation::SingleInheritanceFunction; 1916 case DINode::FlagMultipleInheritance: 1917 return PointerToMemberRepresentation::MultipleInheritanceFunction; 1918 case DINode::FlagVirtualInheritance: 1919 return PointerToMemberRepresentation::VirtualInheritanceFunction; 1920 } 1921 } else { 1922 switch (Flags & DINode::FlagPtrToMemberRep) { 1923 case 0: 1924 return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown 1925 : PointerToMemberRepresentation::GeneralData; 1926 case DINode::FlagSingleInheritance: 1927 return PointerToMemberRepresentation::SingleInheritanceData; 1928 case DINode::FlagMultipleInheritance: 1929 return PointerToMemberRepresentation::MultipleInheritanceData; 1930 case DINode::FlagVirtualInheritance: 1931 return PointerToMemberRepresentation::VirtualInheritanceData; 1932 } 1933 } 1934 llvm_unreachable("invalid ptr to member representation"); 1935 } 1936 1937 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty, 1938 PointerOptions PO) { 1939 assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type); 1940 bool IsPMF = isa<DISubroutineType>(Ty->getBaseType()); 1941 TypeIndex ClassTI = getTypeIndex(Ty->getClassType()); 1942 TypeIndex PointeeTI = 1943 getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr); 1944 PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 1945 : PointerKind::Near32; 1946 PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction 1947 : PointerMode::PointerToDataMember; 1948 1949 assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big"); 1950 uint8_t SizeInBytes = Ty->getSizeInBits() / 8; 1951 MemberPointerInfo MPI( 1952 ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags())); 1953 PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI); 1954 return TypeTable.writeLeafType(PR); 1955 } 1956 1957 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't 1958 /// have a translation, use the NearC convention. 1959 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) { 1960 switch (DwarfCC) { 1961 case dwarf::DW_CC_normal: return CallingConvention::NearC; 1962 case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast; 1963 case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall; 1964 case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall; 1965 case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal; 1966 case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector; 1967 } 1968 return CallingConvention::NearC; 1969 } 1970 1971 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) { 1972 ModifierOptions Mods = ModifierOptions::None; 1973 PointerOptions PO = PointerOptions::None; 1974 bool IsModifier = true; 1975 const DIType *BaseTy = Ty; 1976 while (IsModifier && BaseTy) { 1977 // FIXME: Need to add DWARF tags for __unaligned and _Atomic 1978 switch (BaseTy->getTag()) { 1979 case dwarf::DW_TAG_const_type: 1980 Mods |= ModifierOptions::Const; 1981 PO |= PointerOptions::Const; 1982 break; 1983 case dwarf::DW_TAG_volatile_type: 1984 Mods |= ModifierOptions::Volatile; 1985 PO |= PointerOptions::Volatile; 1986 break; 1987 case dwarf::DW_TAG_restrict_type: 1988 // Only pointer types be marked with __restrict. There is no known flag 1989 // for __restrict in LF_MODIFIER records. 1990 PO |= PointerOptions::Restrict; 1991 break; 1992 default: 1993 IsModifier = false; 1994 break; 1995 } 1996 if (IsModifier) 1997 BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType(); 1998 } 1999 2000 // Check if the inner type will use an LF_POINTER record. If so, the 2001 // qualifiers will go in the LF_POINTER record. This comes up for types like 2002 // 'int *const' and 'int *__restrict', not the more common cases like 'const 2003 // char *'. 2004 if (BaseTy) { 2005 switch (BaseTy->getTag()) { 2006 case dwarf::DW_TAG_pointer_type: 2007 case dwarf::DW_TAG_reference_type: 2008 case dwarf::DW_TAG_rvalue_reference_type: 2009 return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO); 2010 case dwarf::DW_TAG_ptr_to_member_type: 2011 return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO); 2012 default: 2013 break; 2014 } 2015 } 2016 2017 TypeIndex ModifiedTI = getTypeIndex(BaseTy); 2018 2019 // Return the base type index if there aren't any modifiers. For example, the 2020 // metadata could contain restrict wrappers around non-pointer types. 2021 if (Mods == ModifierOptions::None) 2022 return ModifiedTI; 2023 2024 ModifierRecord MR(ModifiedTI, Mods); 2025 return TypeTable.writeLeafType(MR); 2026 } 2027 2028 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) { 2029 SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices; 2030 for (const DIType *ArgType : Ty->getTypeArray()) 2031 ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType)); 2032 2033 // MSVC uses type none for variadic argument. 2034 if (ReturnAndArgTypeIndices.size() > 1 && 2035 ReturnAndArgTypeIndices.back() == TypeIndex::Void()) { 2036 ReturnAndArgTypeIndices.back() = TypeIndex::None(); 2037 } 2038 TypeIndex ReturnTypeIndex = TypeIndex::Void(); 2039 ArrayRef<TypeIndex> ArgTypeIndices = None; 2040 if (!ReturnAndArgTypeIndices.empty()) { 2041 auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices); 2042 ReturnTypeIndex = ReturnAndArgTypesRef.front(); 2043 ArgTypeIndices = ReturnAndArgTypesRef.drop_front(); 2044 } 2045 2046 ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); 2047 TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec); 2048 2049 CallingConvention CC = dwarfCCToCodeView(Ty->getCC()); 2050 2051 FunctionOptions FO = getFunctionOptions(Ty); 2052 ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(), 2053 ArgListIndex); 2054 return TypeTable.writeLeafType(Procedure); 2055 } 2056 2057 TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty, 2058 const DIType *ClassTy, 2059 int ThisAdjustment, 2060 bool IsStaticMethod, 2061 FunctionOptions FO) { 2062 // Lower the containing class type. 2063 TypeIndex ClassType = getTypeIndex(ClassTy); 2064 2065 DITypeRefArray ReturnAndArgs = Ty->getTypeArray(); 2066 2067 unsigned Index = 0; 2068 SmallVector<TypeIndex, 8> ArgTypeIndices; 2069 TypeIndex ReturnTypeIndex = TypeIndex::Void(); 2070 if (ReturnAndArgs.size() > Index) { 2071 ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]); 2072 } 2073 2074 // If the first argument is a pointer type and this isn't a static method, 2075 // treat it as the special 'this' parameter, which is encoded separately from 2076 // the arguments. 2077 TypeIndex ThisTypeIndex; 2078 if (!IsStaticMethod && ReturnAndArgs.size() > Index) { 2079 if (const DIDerivedType *PtrTy = 2080 dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) { 2081 if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) { 2082 ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty); 2083 Index++; 2084 } 2085 } 2086 } 2087 2088 while (Index < ReturnAndArgs.size()) 2089 ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++])); 2090 2091 // MSVC uses type none for variadic argument. 2092 if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void()) 2093 ArgTypeIndices.back() = TypeIndex::None(); 2094 2095 ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); 2096 TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec); 2097 2098 CallingConvention CC = dwarfCCToCodeView(Ty->getCC()); 2099 2100 MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO, 2101 ArgTypeIndices.size(), ArgListIndex, ThisAdjustment); 2102 return TypeTable.writeLeafType(MFR); 2103 } 2104 2105 TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) { 2106 unsigned VSlotCount = 2107 Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize()); 2108 SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near); 2109 2110 VFTableShapeRecord VFTSR(Slots); 2111 return TypeTable.writeLeafType(VFTSR); 2112 } 2113 2114 static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) { 2115 switch (Flags & DINode::FlagAccessibility) { 2116 case DINode::FlagPrivate: return MemberAccess::Private; 2117 case DINode::FlagPublic: return MemberAccess::Public; 2118 case DINode::FlagProtected: return MemberAccess::Protected; 2119 case 0: 2120 // If there was no explicit access control, provide the default for the tag. 2121 return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private 2122 : MemberAccess::Public; 2123 } 2124 llvm_unreachable("access flags are exclusive"); 2125 } 2126 2127 static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) { 2128 if (SP->isArtificial()) 2129 return MethodOptions::CompilerGenerated; 2130 2131 // FIXME: Handle other MethodOptions. 2132 2133 return MethodOptions::None; 2134 } 2135 2136 static MethodKind translateMethodKindFlags(const DISubprogram *SP, 2137 bool Introduced) { 2138 if (SP->getFlags() & DINode::FlagStaticMember) 2139 return MethodKind::Static; 2140 2141 switch (SP->getVirtuality()) { 2142 case dwarf::DW_VIRTUALITY_none: 2143 break; 2144 case dwarf::DW_VIRTUALITY_virtual: 2145 return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual; 2146 case dwarf::DW_VIRTUALITY_pure_virtual: 2147 return Introduced ? MethodKind::PureIntroducingVirtual 2148 : MethodKind::PureVirtual; 2149 default: 2150 llvm_unreachable("unhandled virtuality case"); 2151 } 2152 2153 return MethodKind::Vanilla; 2154 } 2155 2156 static TypeRecordKind getRecordKind(const DICompositeType *Ty) { 2157 switch (Ty->getTag()) { 2158 case dwarf::DW_TAG_class_type: 2159 return TypeRecordKind::Class; 2160 case dwarf::DW_TAG_structure_type: 2161 return TypeRecordKind::Struct; 2162 default: 2163 llvm_unreachable("unexpected tag"); 2164 } 2165 } 2166 2167 /// Return ClassOptions that should be present on both the forward declaration 2168 /// and the defintion of a tag type. 2169 static ClassOptions getCommonClassOptions(const DICompositeType *Ty) { 2170 ClassOptions CO = ClassOptions::None; 2171 2172 // MSVC always sets this flag, even for local types. Clang doesn't always 2173 // appear to give every type a linkage name, which may be problematic for us. 2174 // FIXME: Investigate the consequences of not following them here. 2175 if (!Ty->getIdentifier().empty()) 2176 CO |= ClassOptions::HasUniqueName; 2177 2178 // Put the Nested flag on a type if it appears immediately inside a tag type. 2179 // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass 2180 // here. That flag is only set on definitions, and not forward declarations. 2181 const DIScope *ImmediateScope = Ty->getScope(); 2182 if (ImmediateScope && isa<DICompositeType>(ImmediateScope)) 2183 CO |= ClassOptions::Nested; 2184 2185 // Put the Scoped flag on function-local types. MSVC puts this flag for enum 2186 // type only when it has an immediate function scope. Clang never puts enums 2187 // inside DILexicalBlock scopes. Enum types, as generated by clang, are 2188 // always in function, class, or file scopes. 2189 if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) { 2190 if (ImmediateScope && isa<DISubprogram>(ImmediateScope)) 2191 CO |= ClassOptions::Scoped; 2192 } else { 2193 for (const DIScope *Scope = ImmediateScope; Scope != nullptr; 2194 Scope = Scope->getScope()) { 2195 if (isa<DISubprogram>(Scope)) { 2196 CO |= ClassOptions::Scoped; 2197 break; 2198 } 2199 } 2200 } 2201 2202 return CO; 2203 } 2204 2205 void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) { 2206 switch (Ty->getTag()) { 2207 case dwarf::DW_TAG_class_type: 2208 case dwarf::DW_TAG_structure_type: 2209 case dwarf::DW_TAG_union_type: 2210 case dwarf::DW_TAG_enumeration_type: 2211 break; 2212 default: 2213 return; 2214 } 2215 2216 if (const auto *File = Ty->getFile()) { 2217 StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File)); 2218 TypeIndex SIDI = TypeTable.writeLeafType(SIDR); 2219 2220 UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine()); 2221 TypeTable.writeLeafType(USLR); 2222 } 2223 } 2224 2225 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) { 2226 ClassOptions CO = getCommonClassOptions(Ty); 2227 TypeIndex FTI; 2228 unsigned EnumeratorCount = 0; 2229 2230 if (Ty->isForwardDecl()) { 2231 CO |= ClassOptions::ForwardReference; 2232 } else { 2233 ContinuationRecordBuilder ContinuationBuilder; 2234 ContinuationBuilder.begin(ContinuationRecordKind::FieldList); 2235 for (const DINode *Element : Ty->getElements()) { 2236 // We assume that the frontend provides all members in source declaration 2237 // order, which is what MSVC does. 2238 if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) { 2239 // FIXME: Is it correct to always emit these as unsigned here? 2240 EnumeratorRecord ER(MemberAccess::Public, 2241 APSInt(Enumerator->getValue(), true), 2242 Enumerator->getName()); 2243 ContinuationBuilder.writeMemberType(ER); 2244 EnumeratorCount++; 2245 } 2246 } 2247 FTI = TypeTable.insertRecord(ContinuationBuilder); 2248 } 2249 2250 std::string FullName = getFullyQualifiedName(Ty); 2251 2252 EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(), 2253 getTypeIndex(Ty->getBaseType())); 2254 TypeIndex EnumTI = TypeTable.writeLeafType(ER); 2255 2256 addUDTSrcLine(Ty, EnumTI); 2257 2258 return EnumTI; 2259 } 2260 2261 //===----------------------------------------------------------------------===// 2262 // ClassInfo 2263 //===----------------------------------------------------------------------===// 2264 2265 struct llvm::ClassInfo { 2266 struct MemberInfo { 2267 const DIDerivedType *MemberTypeNode; 2268 uint64_t BaseOffset; 2269 }; 2270 // [MemberInfo] 2271 using MemberList = std::vector<MemberInfo>; 2272 2273 using MethodsList = TinyPtrVector<const DISubprogram *>; 2274 // MethodName -> MethodsList 2275 using MethodsMap = MapVector<MDString *, MethodsList>; 2276 2277 /// Base classes. 2278 std::vector<const DIDerivedType *> Inheritance; 2279 2280 /// Direct members. 2281 MemberList Members; 2282 // Direct overloaded methods gathered by name. 2283 MethodsMap Methods; 2284 2285 TypeIndex VShapeTI; 2286 2287 std::vector<const DIType *> NestedTypes; 2288 }; 2289 2290 void CodeViewDebug::clear() { 2291 assert(CurFn == nullptr); 2292 FileIdMap.clear(); 2293 FnDebugInfo.clear(); 2294 FileToFilepathMap.clear(); 2295 LocalUDTs.clear(); 2296 GlobalUDTs.clear(); 2297 TypeIndices.clear(); 2298 CompleteTypeIndices.clear(); 2299 ScopeGlobals.clear(); 2300 CVGlobalVariableOffsets.clear(); 2301 } 2302 2303 void CodeViewDebug::collectMemberInfo(ClassInfo &Info, 2304 const DIDerivedType *DDTy) { 2305 if (!DDTy->getName().empty()) { 2306 Info.Members.push_back({DDTy, 0}); 2307 2308 // Collect static const data members with values. 2309 if ((DDTy->getFlags() & DINode::FlagStaticMember) == 2310 DINode::FlagStaticMember) { 2311 if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) || 2312 isa<ConstantFP>(DDTy->getConstant()))) 2313 StaticConstMembers.push_back(DDTy); 2314 } 2315 2316 return; 2317 } 2318 2319 // An unnamed member may represent a nested struct or union. Attempt to 2320 // interpret the unnamed member as a DICompositeType possibly wrapped in 2321 // qualifier types. Add all the indirect fields to the current record if that 2322 // succeeds, and drop the member if that fails. 2323 assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!"); 2324 uint64_t Offset = DDTy->getOffsetInBits(); 2325 const DIType *Ty = DDTy->getBaseType(); 2326 bool FullyResolved = false; 2327 while (!FullyResolved) { 2328 switch (Ty->getTag()) { 2329 case dwarf::DW_TAG_const_type: 2330 case dwarf::DW_TAG_volatile_type: 2331 // FIXME: we should apply the qualifier types to the indirect fields 2332 // rather than dropping them. 2333 Ty = cast<DIDerivedType>(Ty)->getBaseType(); 2334 break; 2335 default: 2336 FullyResolved = true; 2337 break; 2338 } 2339 } 2340 2341 const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty); 2342 if (!DCTy) 2343 return; 2344 2345 ClassInfo NestedInfo = collectClassInfo(DCTy); 2346 for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members) 2347 Info.Members.push_back( 2348 {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset}); 2349 } 2350 2351 ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) { 2352 ClassInfo Info; 2353 // Add elements to structure type. 2354 DINodeArray Elements = Ty->getElements(); 2355 for (auto *Element : Elements) { 2356 // We assume that the frontend provides all members in source declaration 2357 // order, which is what MSVC does. 2358 if (!Element) 2359 continue; 2360 if (auto *SP = dyn_cast<DISubprogram>(Element)) { 2361 Info.Methods[SP->getRawName()].push_back(SP); 2362 } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { 2363 if (DDTy->getTag() == dwarf::DW_TAG_member) { 2364 collectMemberInfo(Info, DDTy); 2365 } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) { 2366 Info.Inheritance.push_back(DDTy); 2367 } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type && 2368 DDTy->getName() == "__vtbl_ptr_type") { 2369 Info.VShapeTI = getTypeIndex(DDTy); 2370 } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) { 2371 Info.NestedTypes.push_back(DDTy); 2372 } else if (DDTy->getTag() == dwarf::DW_TAG_friend) { 2373 // Ignore friend members. It appears that MSVC emitted info about 2374 // friends in the past, but modern versions do not. 2375 } 2376 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) { 2377 Info.NestedTypes.push_back(Composite); 2378 } 2379 // Skip other unrecognized kinds of elements. 2380 } 2381 return Info; 2382 } 2383 2384 static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) { 2385 // This routine is used by lowerTypeClass and lowerTypeUnion to determine 2386 // if a complete type should be emitted instead of a forward reference. 2387 return Ty->getName().empty() && Ty->getIdentifier().empty() && 2388 !Ty->isForwardDecl(); 2389 } 2390 2391 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { 2392 // Emit the complete type for unnamed structs. C++ classes with methods 2393 // which have a circular reference back to the class type are expected to 2394 // be named by the front-end and should not be "unnamed". C unnamed 2395 // structs should not have circular references. 2396 if (shouldAlwaysEmitCompleteClassType(Ty)) { 2397 // If this unnamed complete type is already in the process of being defined 2398 // then the description of the type is malformed and cannot be emitted 2399 // into CodeView correctly so report a fatal error. 2400 auto I = CompleteTypeIndices.find(Ty); 2401 if (I != CompleteTypeIndices.end() && I->second == TypeIndex()) 2402 report_fatal_error("cannot debug circular reference to unnamed type"); 2403 return getCompleteTypeIndex(Ty); 2404 } 2405 2406 // First, construct the forward decl. Don't look into Ty to compute the 2407 // forward decl options, since it might not be available in all TUs. 2408 TypeRecordKind Kind = getRecordKind(Ty); 2409 ClassOptions CO = 2410 ClassOptions::ForwardReference | getCommonClassOptions(Ty); 2411 std::string FullName = getFullyQualifiedName(Ty); 2412 ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0, 2413 FullName, Ty->getIdentifier()); 2414 TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR); 2415 if (!Ty->isForwardDecl()) 2416 DeferredCompleteTypes.push_back(Ty); 2417 return FwdDeclTI; 2418 } 2419 2420 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) { 2421 // Construct the field list and complete type record. 2422 TypeRecordKind Kind = getRecordKind(Ty); 2423 ClassOptions CO = getCommonClassOptions(Ty); 2424 TypeIndex FieldTI; 2425 TypeIndex VShapeTI; 2426 unsigned FieldCount; 2427 bool ContainsNestedClass; 2428 std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) = 2429 lowerRecordFieldList(Ty); 2430 2431 if (ContainsNestedClass) 2432 CO |= ClassOptions::ContainsNestedClass; 2433 2434 // MSVC appears to set this flag by searching any destructor or method with 2435 // FunctionOptions::Constructor among the emitted members. Clang AST has all 2436 // the members, however special member functions are not yet emitted into 2437 // debug information. For now checking a class's non-triviality seems enough. 2438 // FIXME: not true for a nested unnamed struct. 2439 if (isNonTrivial(Ty)) 2440 CO |= ClassOptions::HasConstructorOrDestructor; 2441 2442 std::string FullName = getFullyQualifiedName(Ty); 2443 2444 uint64_t SizeInBytes = Ty->getSizeInBits() / 8; 2445 2446 ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI, 2447 SizeInBytes, FullName, Ty->getIdentifier()); 2448 TypeIndex ClassTI = TypeTable.writeLeafType(CR); 2449 2450 addUDTSrcLine(Ty, ClassTI); 2451 2452 addToUDTs(Ty); 2453 2454 return ClassTI; 2455 } 2456 2457 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { 2458 // Emit the complete type for unnamed unions. 2459 if (shouldAlwaysEmitCompleteClassType(Ty)) 2460 return getCompleteTypeIndex(Ty); 2461 2462 ClassOptions CO = 2463 ClassOptions::ForwardReference | getCommonClassOptions(Ty); 2464 std::string FullName = getFullyQualifiedName(Ty); 2465 UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier()); 2466 TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR); 2467 if (!Ty->isForwardDecl()) 2468 DeferredCompleteTypes.push_back(Ty); 2469 return FwdDeclTI; 2470 } 2471 2472 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) { 2473 ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty); 2474 TypeIndex FieldTI; 2475 unsigned FieldCount; 2476 bool ContainsNestedClass; 2477 std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) = 2478 lowerRecordFieldList(Ty); 2479 2480 if (ContainsNestedClass) 2481 CO |= ClassOptions::ContainsNestedClass; 2482 2483 uint64_t SizeInBytes = Ty->getSizeInBits() / 8; 2484 std::string FullName = getFullyQualifiedName(Ty); 2485 2486 UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName, 2487 Ty->getIdentifier()); 2488 TypeIndex UnionTI = TypeTable.writeLeafType(UR); 2489 2490 addUDTSrcLine(Ty, UnionTI); 2491 2492 addToUDTs(Ty); 2493 2494 return UnionTI; 2495 } 2496 2497 std::tuple<TypeIndex, TypeIndex, unsigned, bool> 2498 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) { 2499 // Manually count members. MSVC appears to count everything that generates a 2500 // field list record. Each individual overload in a method overload group 2501 // contributes to this count, even though the overload group is a single field 2502 // list record. 2503 unsigned MemberCount = 0; 2504 ClassInfo Info = collectClassInfo(Ty); 2505 ContinuationRecordBuilder ContinuationBuilder; 2506 ContinuationBuilder.begin(ContinuationRecordKind::FieldList); 2507 2508 // Create base classes. 2509 for (const DIDerivedType *I : Info.Inheritance) { 2510 if (I->getFlags() & DINode::FlagVirtual) { 2511 // Virtual base. 2512 unsigned VBPtrOffset = I->getVBPtrOffset(); 2513 // FIXME: Despite the accessor name, the offset is really in bytes. 2514 unsigned VBTableIndex = I->getOffsetInBits() / 4; 2515 auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase 2516 ? TypeRecordKind::IndirectVirtualBaseClass 2517 : TypeRecordKind::VirtualBaseClass; 2518 VirtualBaseClassRecord VBCR( 2519 RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()), 2520 getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset, 2521 VBTableIndex); 2522 2523 ContinuationBuilder.writeMemberType(VBCR); 2524 MemberCount++; 2525 } else { 2526 assert(I->getOffsetInBits() % 8 == 0 && 2527 "bases must be on byte boundaries"); 2528 BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()), 2529 getTypeIndex(I->getBaseType()), 2530 I->getOffsetInBits() / 8); 2531 ContinuationBuilder.writeMemberType(BCR); 2532 MemberCount++; 2533 } 2534 } 2535 2536 // Create members. 2537 for (ClassInfo::MemberInfo &MemberInfo : Info.Members) { 2538 const DIDerivedType *Member = MemberInfo.MemberTypeNode; 2539 TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType()); 2540 StringRef MemberName = Member->getName(); 2541 MemberAccess Access = 2542 translateAccessFlags(Ty->getTag(), Member->getFlags()); 2543 2544 if (Member->isStaticMember()) { 2545 StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName); 2546 ContinuationBuilder.writeMemberType(SDMR); 2547 MemberCount++; 2548 continue; 2549 } 2550 2551 // Virtual function pointer member. 2552 if ((Member->getFlags() & DINode::FlagArtificial) && 2553 Member->getName().startswith("_vptr$")) { 2554 VFPtrRecord VFPR(getTypeIndex(Member->getBaseType())); 2555 ContinuationBuilder.writeMemberType(VFPR); 2556 MemberCount++; 2557 continue; 2558 } 2559 2560 // Data member. 2561 uint64_t MemberOffsetInBits = 2562 Member->getOffsetInBits() + MemberInfo.BaseOffset; 2563 if (Member->isBitField()) { 2564 uint64_t StartBitOffset = MemberOffsetInBits; 2565 if (const auto *CI = 2566 dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) { 2567 MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset; 2568 } 2569 StartBitOffset -= MemberOffsetInBits; 2570 BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(), 2571 StartBitOffset); 2572 MemberBaseType = TypeTable.writeLeafType(BFR); 2573 } 2574 uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8; 2575 DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes, 2576 MemberName); 2577 ContinuationBuilder.writeMemberType(DMR); 2578 MemberCount++; 2579 } 2580 2581 // Create methods 2582 for (auto &MethodItr : Info.Methods) { 2583 StringRef Name = MethodItr.first->getString(); 2584 2585 std::vector<OneMethodRecord> Methods; 2586 for (const DISubprogram *SP : MethodItr.second) { 2587 TypeIndex MethodType = getMemberFunctionType(SP, Ty); 2588 bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual; 2589 2590 unsigned VFTableOffset = -1; 2591 if (Introduced) 2592 VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes(); 2593 2594 Methods.push_back(OneMethodRecord( 2595 MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()), 2596 translateMethodKindFlags(SP, Introduced), 2597 translateMethodOptionFlags(SP), VFTableOffset, Name)); 2598 MemberCount++; 2599 } 2600 assert(!Methods.empty() && "Empty methods map entry"); 2601 if (Methods.size() == 1) 2602 ContinuationBuilder.writeMemberType(Methods[0]); 2603 else { 2604 // FIXME: Make this use its own ContinuationBuilder so that 2605 // MethodOverloadList can be split correctly. 2606 MethodOverloadListRecord MOLR(Methods); 2607 TypeIndex MethodList = TypeTable.writeLeafType(MOLR); 2608 2609 OverloadedMethodRecord OMR(Methods.size(), MethodList, Name); 2610 ContinuationBuilder.writeMemberType(OMR); 2611 } 2612 } 2613 2614 // Create nested classes. 2615 for (const DIType *Nested : Info.NestedTypes) { 2616 NestedTypeRecord R(getTypeIndex(Nested), Nested->getName()); 2617 ContinuationBuilder.writeMemberType(R); 2618 MemberCount++; 2619 } 2620 2621 TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder); 2622 return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount, 2623 !Info.NestedTypes.empty()); 2624 } 2625 2626 TypeIndex CodeViewDebug::getVBPTypeIndex() { 2627 if (!VBPType.getIndex()) { 2628 // Make a 'const int *' type. 2629 ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const); 2630 TypeIndex ModifiedTI = TypeTable.writeLeafType(MR); 2631 2632 PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 2633 : PointerKind::Near32; 2634 PointerMode PM = PointerMode::Pointer; 2635 PointerOptions PO = PointerOptions::None; 2636 PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes()); 2637 VBPType = TypeTable.writeLeafType(PR); 2638 } 2639 2640 return VBPType; 2641 } 2642 2643 TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) { 2644 // The null DIType is the void type. Don't try to hash it. 2645 if (!Ty) 2646 return TypeIndex::Void(); 2647 2648 // Check if we've already translated this type. Don't try to do a 2649 // get-or-create style insertion that caches the hash lookup across the 2650 // lowerType call. It will update the TypeIndices map. 2651 auto I = TypeIndices.find({Ty, ClassTy}); 2652 if (I != TypeIndices.end()) 2653 return I->second; 2654 2655 TypeLoweringScope S(*this); 2656 TypeIndex TI = lowerType(Ty, ClassTy); 2657 return recordTypeIndexForDINode(Ty, TI, ClassTy); 2658 } 2659 2660 codeview::TypeIndex 2661 CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy, 2662 const DISubroutineType *SubroutineTy) { 2663 assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type && 2664 "this type must be a pointer type"); 2665 2666 PointerOptions Options = PointerOptions::None; 2667 if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference) 2668 Options = PointerOptions::LValueRefThisPointer; 2669 else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference) 2670 Options = PointerOptions::RValueRefThisPointer; 2671 2672 // Check if we've already translated this type. If there is no ref qualifier 2673 // on the function then we look up this pointer type with no associated class 2674 // so that the TypeIndex for the this pointer can be shared with the type 2675 // index for other pointers to this class type. If there is a ref qualifier 2676 // then we lookup the pointer using the subroutine as the parent type. 2677 auto I = TypeIndices.find({PtrTy, SubroutineTy}); 2678 if (I != TypeIndices.end()) 2679 return I->second; 2680 2681 TypeLoweringScope S(*this); 2682 TypeIndex TI = lowerTypePointer(PtrTy, Options); 2683 return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy); 2684 } 2685 2686 TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) { 2687 PointerRecord PR(getTypeIndex(Ty), 2688 getPointerSizeInBytes() == 8 ? PointerKind::Near64 2689 : PointerKind::Near32, 2690 PointerMode::LValueReference, PointerOptions::None, 2691 Ty->getSizeInBits() / 8); 2692 return TypeTable.writeLeafType(PR); 2693 } 2694 2695 TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) { 2696 // The null DIType is the void type. Don't try to hash it. 2697 if (!Ty) 2698 return TypeIndex::Void(); 2699 2700 // Look through typedefs when getting the complete type index. Call 2701 // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are 2702 // emitted only once. 2703 if (Ty->getTag() == dwarf::DW_TAG_typedef) 2704 (void)getTypeIndex(Ty); 2705 while (Ty->getTag() == dwarf::DW_TAG_typedef) 2706 Ty = cast<DIDerivedType>(Ty)->getBaseType(); 2707 2708 // If this is a non-record type, the complete type index is the same as the 2709 // normal type index. Just call getTypeIndex. 2710 switch (Ty->getTag()) { 2711 case dwarf::DW_TAG_class_type: 2712 case dwarf::DW_TAG_structure_type: 2713 case dwarf::DW_TAG_union_type: 2714 break; 2715 default: 2716 return getTypeIndex(Ty); 2717 } 2718 2719 const auto *CTy = cast<DICompositeType>(Ty); 2720 2721 TypeLoweringScope S(*this); 2722 2723 // Make sure the forward declaration is emitted first. It's unclear if this 2724 // is necessary, but MSVC does it, and we should follow suit until we can show 2725 // otherwise. 2726 // We only emit a forward declaration for named types. 2727 if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) { 2728 TypeIndex FwdDeclTI = getTypeIndex(CTy); 2729 2730 // Just use the forward decl if we don't have complete type info. This 2731 // might happen if the frontend is using modules and expects the complete 2732 // definition to be emitted elsewhere. 2733 if (CTy->isForwardDecl()) 2734 return FwdDeclTI; 2735 } 2736 2737 // Check if we've already translated the complete record type. 2738 // Insert the type with a null TypeIndex to signify that the type is currently 2739 // being lowered. 2740 auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()}); 2741 if (!InsertResult.second) 2742 return InsertResult.first->second; 2743 2744 TypeIndex TI; 2745 switch (CTy->getTag()) { 2746 case dwarf::DW_TAG_class_type: 2747 case dwarf::DW_TAG_structure_type: 2748 TI = lowerCompleteTypeClass(CTy); 2749 break; 2750 case dwarf::DW_TAG_union_type: 2751 TI = lowerCompleteTypeUnion(CTy); 2752 break; 2753 default: 2754 llvm_unreachable("not a record"); 2755 } 2756 2757 // Update the type index associated with this CompositeType. This cannot 2758 // use the 'InsertResult' iterator above because it is potentially 2759 // invalidated by map insertions which can occur while lowering the class 2760 // type above. 2761 CompleteTypeIndices[CTy] = TI; 2762 return TI; 2763 } 2764 2765 /// Emit all the deferred complete record types. Try to do this in FIFO order, 2766 /// and do this until fixpoint, as each complete record type typically 2767 /// references 2768 /// many other record types. 2769 void CodeViewDebug::emitDeferredCompleteTypes() { 2770 SmallVector<const DICompositeType *, 4> TypesToEmit; 2771 while (!DeferredCompleteTypes.empty()) { 2772 std::swap(DeferredCompleteTypes, TypesToEmit); 2773 for (const DICompositeType *RecordTy : TypesToEmit) 2774 getCompleteTypeIndex(RecordTy); 2775 TypesToEmit.clear(); 2776 } 2777 } 2778 2779 void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI, 2780 ArrayRef<LocalVariable> Locals) { 2781 // Get the sorted list of parameters and emit them first. 2782 SmallVector<const LocalVariable *, 6> Params; 2783 for (const LocalVariable &L : Locals) 2784 if (L.DIVar->isParameter()) 2785 Params.push_back(&L); 2786 llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) { 2787 return L->DIVar->getArg() < R->DIVar->getArg(); 2788 }); 2789 for (const LocalVariable *L : Params) 2790 emitLocalVariable(FI, *L); 2791 2792 // Next emit all non-parameters in the order that we found them. 2793 for (const LocalVariable &L : Locals) 2794 if (!L.DIVar->isParameter()) 2795 emitLocalVariable(FI, L); 2796 } 2797 2798 void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI, 2799 const LocalVariable &Var) { 2800 // LocalSym record, see SymbolRecord.h for more info. 2801 MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL); 2802 2803 LocalSymFlags Flags = LocalSymFlags::None; 2804 if (Var.DIVar->isParameter()) 2805 Flags |= LocalSymFlags::IsParameter; 2806 if (Var.DefRanges.empty()) 2807 Flags |= LocalSymFlags::IsOptimizedOut; 2808 2809 OS.AddComment("TypeIndex"); 2810 TypeIndex TI = Var.UseReferenceType 2811 ? getTypeIndexForReferenceTo(Var.DIVar->getType()) 2812 : getCompleteTypeIndex(Var.DIVar->getType()); 2813 OS.emitInt32(TI.getIndex()); 2814 OS.AddComment("Flags"); 2815 OS.emitInt16(static_cast<uint16_t>(Flags)); 2816 // Truncate the name so we won't overflow the record length field. 2817 emitNullTerminatedSymbolName(OS, Var.DIVar->getName()); 2818 endSymbolRecord(LocalEnd); 2819 2820 // Calculate the on disk prefix of the appropriate def range record. The 2821 // records and on disk formats are described in SymbolRecords.h. BytePrefix 2822 // should be big enough to hold all forms without memory allocation. 2823 SmallString<20> BytePrefix; 2824 for (const LocalVarDefRange &DefRange : Var.DefRanges) { 2825 BytePrefix.clear(); 2826 if (DefRange.InMemory) { 2827 int Offset = DefRange.DataOffset; 2828 unsigned Reg = DefRange.CVRegister; 2829 2830 // 32-bit x86 call sequences often use PUSH instructions, which disrupt 2831 // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0, 2832 // instead. In frames without stack realignment, $T0 will be the CFA. 2833 if (RegisterId(Reg) == RegisterId::ESP) { 2834 Reg = unsigned(RegisterId::VFRAME); 2835 Offset += FI.OffsetAdjustment; 2836 } 2837 2838 // If we can use the chosen frame pointer for the frame and this isn't a 2839 // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record. 2840 // Otherwise, use S_DEFRANGE_REGISTER_REL. 2841 EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU); 2842 if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None && 2843 (bool(Flags & LocalSymFlags::IsParameter) 2844 ? (EncFP == FI.EncodedParamFramePtrReg) 2845 : (EncFP == FI.EncodedLocalFramePtrReg))) { 2846 DefRangeFramePointerRelHeader DRHdr; 2847 DRHdr.Offset = Offset; 2848 OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr); 2849 } else { 2850 uint16_t RegRelFlags = 0; 2851 if (DefRange.IsSubfield) { 2852 RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag | 2853 (DefRange.StructOffset 2854 << DefRangeRegisterRelSym::OffsetInParentShift); 2855 } 2856 DefRangeRegisterRelHeader DRHdr; 2857 DRHdr.Register = Reg; 2858 DRHdr.Flags = RegRelFlags; 2859 DRHdr.BasePointerOffset = Offset; 2860 OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr); 2861 } 2862 } else { 2863 assert(DefRange.DataOffset == 0 && "unexpected offset into register"); 2864 if (DefRange.IsSubfield) { 2865 DefRangeSubfieldRegisterHeader DRHdr; 2866 DRHdr.Register = DefRange.CVRegister; 2867 DRHdr.MayHaveNoName = 0; 2868 DRHdr.OffsetInParent = DefRange.StructOffset; 2869 OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr); 2870 } else { 2871 DefRangeRegisterHeader DRHdr; 2872 DRHdr.Register = DefRange.CVRegister; 2873 DRHdr.MayHaveNoName = 0; 2874 OS.emitCVDefRangeDirective(DefRange.Ranges, DRHdr); 2875 } 2876 } 2877 } 2878 } 2879 2880 void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks, 2881 const FunctionInfo& FI) { 2882 for (LexicalBlock *Block : Blocks) 2883 emitLexicalBlock(*Block, FI); 2884 } 2885 2886 /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a 2887 /// lexical block scope. 2888 void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block, 2889 const FunctionInfo& FI) { 2890 MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32); 2891 OS.AddComment("PtrParent"); 2892 OS.emitInt32(0); // PtrParent 2893 OS.AddComment("PtrEnd"); 2894 OS.emitInt32(0); // PtrEnd 2895 OS.AddComment("Code size"); 2896 OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4); // Code Size 2897 OS.AddComment("Function section relative address"); 2898 OS.EmitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset 2899 OS.AddComment("Function section index"); 2900 OS.EmitCOFFSectionIndex(FI.Begin); // Func Symbol 2901 OS.AddComment("Lexical block name"); 2902 emitNullTerminatedSymbolName(OS, Block.Name); // Name 2903 endSymbolRecord(RecordEnd); 2904 2905 // Emit variables local to this lexical block. 2906 emitLocalVariableList(FI, Block.Locals); 2907 emitGlobalVariableList(Block.Globals); 2908 2909 // Emit lexical blocks contained within this block. 2910 emitLexicalBlockList(Block.Children, FI); 2911 2912 // Close the lexical block scope. 2913 emitEndSymbolRecord(SymbolKind::S_END); 2914 } 2915 2916 /// Convenience routine for collecting lexical block information for a list 2917 /// of lexical scopes. 2918 void CodeViewDebug::collectLexicalBlockInfo( 2919 SmallVectorImpl<LexicalScope *> &Scopes, 2920 SmallVectorImpl<LexicalBlock *> &Blocks, 2921 SmallVectorImpl<LocalVariable> &Locals, 2922 SmallVectorImpl<CVGlobalVariable> &Globals) { 2923 for (LexicalScope *Scope : Scopes) 2924 collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals); 2925 } 2926 2927 /// Populate the lexical blocks and local variable lists of the parent with 2928 /// information about the specified lexical scope. 2929 void CodeViewDebug::collectLexicalBlockInfo( 2930 LexicalScope &Scope, 2931 SmallVectorImpl<LexicalBlock *> &ParentBlocks, 2932 SmallVectorImpl<LocalVariable> &ParentLocals, 2933 SmallVectorImpl<CVGlobalVariable> &ParentGlobals) { 2934 if (Scope.isAbstractScope()) 2935 return; 2936 2937 // Gather information about the lexical scope including local variables, 2938 // global variables, and address ranges. 2939 bool IgnoreScope = false; 2940 auto LI = ScopeVariables.find(&Scope); 2941 SmallVectorImpl<LocalVariable> *Locals = 2942 LI != ScopeVariables.end() ? &LI->second : nullptr; 2943 auto GI = ScopeGlobals.find(Scope.getScopeNode()); 2944 SmallVectorImpl<CVGlobalVariable> *Globals = 2945 GI != ScopeGlobals.end() ? GI->second.get() : nullptr; 2946 const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode()); 2947 const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges(); 2948 2949 // Ignore lexical scopes which do not contain variables. 2950 if (!Locals && !Globals) 2951 IgnoreScope = true; 2952 2953 // Ignore lexical scopes which are not lexical blocks. 2954 if (!DILB) 2955 IgnoreScope = true; 2956 2957 // Ignore scopes which have too many address ranges to represent in the 2958 // current CodeView format or do not have a valid address range. 2959 // 2960 // For lexical scopes with multiple address ranges you may be tempted to 2961 // construct a single range covering every instruction where the block is 2962 // live and everything in between. Unfortunately, Visual Studio only 2963 // displays variables from the first matching lexical block scope. If the 2964 // first lexical block contains exception handling code or cold code which 2965 // is moved to the bottom of the routine creating a single range covering 2966 // nearly the entire routine, then it will hide all other lexical blocks 2967 // and the variables they contain. 2968 if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second)) 2969 IgnoreScope = true; 2970 2971 if (IgnoreScope) { 2972 // This scope can be safely ignored and eliminating it will reduce the 2973 // size of the debug information. Be sure to collect any variable and scope 2974 // information from the this scope or any of its children and collapse them 2975 // into the parent scope. 2976 if (Locals) 2977 ParentLocals.append(Locals->begin(), Locals->end()); 2978 if (Globals) 2979 ParentGlobals.append(Globals->begin(), Globals->end()); 2980 collectLexicalBlockInfo(Scope.getChildren(), 2981 ParentBlocks, 2982 ParentLocals, 2983 ParentGlobals); 2984 return; 2985 } 2986 2987 // Create a new CodeView lexical block for this lexical scope. If we've 2988 // seen this DILexicalBlock before then the scope tree is malformed and 2989 // we can handle this gracefully by not processing it a second time. 2990 auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()}); 2991 if (!BlockInsertion.second) 2992 return; 2993 2994 // Create a lexical block containing the variables and collect the the 2995 // lexical block information for the children. 2996 const InsnRange &Range = Ranges.front(); 2997 assert(Range.first && Range.second); 2998 LexicalBlock &Block = BlockInsertion.first->second; 2999 Block.Begin = getLabelBeforeInsn(Range.first); 3000 Block.End = getLabelAfterInsn(Range.second); 3001 assert(Block.Begin && "missing label for scope begin"); 3002 assert(Block.End && "missing label for scope end"); 3003 Block.Name = DILB->getName(); 3004 if (Locals) 3005 Block.Locals = std::move(*Locals); 3006 if (Globals) 3007 Block.Globals = std::move(*Globals); 3008 ParentBlocks.push_back(&Block); 3009 collectLexicalBlockInfo(Scope.getChildren(), 3010 Block.Children, 3011 Block.Locals, 3012 Block.Globals); 3013 } 3014 3015 void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { 3016 const Function &GV = MF->getFunction(); 3017 assert(FnDebugInfo.count(&GV)); 3018 assert(CurFn == FnDebugInfo[&GV].get()); 3019 3020 collectVariableInfo(GV.getSubprogram()); 3021 3022 // Build the lexical block structure to emit for this routine. 3023 if (LexicalScope *CFS = LScopes.getCurrentFunctionScope()) 3024 collectLexicalBlockInfo(*CFS, 3025 CurFn->ChildBlocks, 3026 CurFn->Locals, 3027 CurFn->Globals); 3028 3029 // Clear the scope and variable information from the map which will not be 3030 // valid after we have finished processing this routine. This also prepares 3031 // the map for the subsequent routine. 3032 ScopeVariables.clear(); 3033 3034 // Don't emit anything if we don't have any line tables. 3035 // Thunks are compiler-generated and probably won't have source correlation. 3036 if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) { 3037 FnDebugInfo.erase(&GV); 3038 CurFn = nullptr; 3039 return; 3040 } 3041 3042 // Find heap alloc sites and add to list. 3043 for (const auto &MBB : *MF) { 3044 for (const auto &MI : MBB) { 3045 if (MDNode *MD = MI.getHeapAllocMarker()) { 3046 CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI), 3047 getLabelAfterInsn(&MI), 3048 dyn_cast<DIType>(MD))); 3049 } 3050 } 3051 } 3052 3053 CurFn->Annotations = MF->getCodeViewAnnotations(); 3054 3055 CurFn->End = Asm->getFunctionEnd(); 3056 3057 CurFn = nullptr; 3058 } 3059 3060 // Usable locations are valid with non-zero line numbers. A line number of zero 3061 // corresponds to optimized code that doesn't have a distinct source location. 3062 // In this case, we try to use the previous or next source location depending on 3063 // the context. 3064 static bool isUsableDebugLoc(DebugLoc DL) { 3065 return DL && DL.getLine() != 0; 3066 } 3067 3068 void CodeViewDebug::beginInstruction(const MachineInstr *MI) { 3069 DebugHandlerBase::beginInstruction(MI); 3070 3071 // Ignore DBG_VALUE and DBG_LABEL locations and function prologue. 3072 if (!Asm || !CurFn || MI->isDebugInstr() || 3073 MI->getFlag(MachineInstr::FrameSetup)) 3074 return; 3075 3076 // If the first instruction of a new MBB has no location, find the first 3077 // instruction with a location and use that. 3078 DebugLoc DL = MI->getDebugLoc(); 3079 if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) { 3080 for (const auto &NextMI : *MI->getParent()) { 3081 if (NextMI.isDebugInstr()) 3082 continue; 3083 DL = NextMI.getDebugLoc(); 3084 if (isUsableDebugLoc(DL)) 3085 break; 3086 } 3087 // FIXME: Handle the case where the BB has no valid locations. This would 3088 // probably require doing a real dataflow analysis. 3089 } 3090 PrevInstBB = MI->getParent(); 3091 3092 // If we still don't have a debug location, don't record a location. 3093 if (!isUsableDebugLoc(DL)) 3094 return; 3095 3096 maybeRecordLocation(DL, Asm->MF); 3097 } 3098 3099 MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) { 3100 MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), 3101 *EndLabel = MMI->getContext().createTempSymbol(); 3102 OS.emitInt32(unsigned(Kind)); 3103 OS.AddComment("Subsection size"); 3104 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4); 3105 OS.emitLabel(BeginLabel); 3106 return EndLabel; 3107 } 3108 3109 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) { 3110 OS.emitLabel(EndLabel); 3111 // Every subsection must be aligned to a 4-byte boundary. 3112 OS.emitValueToAlignment(4); 3113 } 3114 3115 static StringRef getSymbolName(SymbolKind SymKind) { 3116 for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames()) 3117 if (EE.Value == SymKind) 3118 return EE.Name; 3119 return ""; 3120 } 3121 3122 MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) { 3123 MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), 3124 *EndLabel = MMI->getContext().createTempSymbol(); 3125 OS.AddComment("Record length"); 3126 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2); 3127 OS.emitLabel(BeginLabel); 3128 if (OS.isVerboseAsm()) 3129 OS.AddComment("Record kind: " + getSymbolName(SymKind)); 3130 OS.emitInt16(unsigned(SymKind)); 3131 return EndLabel; 3132 } 3133 3134 void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) { 3135 // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid 3136 // an extra copy of every symbol record in LLD. This increases object file 3137 // size by less than 1% in the clang build, and is compatible with the Visual 3138 // C++ linker. 3139 OS.emitValueToAlignment(4); 3140 OS.emitLabel(SymEnd); 3141 } 3142 3143 void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) { 3144 OS.AddComment("Record length"); 3145 OS.emitInt16(2); 3146 if (OS.isVerboseAsm()) 3147 OS.AddComment("Record kind: " + getSymbolName(EndKind)); 3148 OS.emitInt16(uint16_t(EndKind)); // Record Kind 3149 } 3150 3151 void CodeViewDebug::emitDebugInfoForUDTs( 3152 const std::vector<std::pair<std::string, const DIType *>> &UDTs) { 3153 #ifndef NDEBUG 3154 size_t OriginalSize = UDTs.size(); 3155 #endif 3156 for (const auto &UDT : UDTs) { 3157 const DIType *T = UDT.second; 3158 assert(shouldEmitUdt(T)); 3159 MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT); 3160 OS.AddComment("Type"); 3161 OS.emitInt32(getCompleteTypeIndex(T).getIndex()); 3162 assert(OriginalSize == UDTs.size() && 3163 "getCompleteTypeIndex found new UDTs!"); 3164 emitNullTerminatedSymbolName(OS, UDT.first); 3165 endSymbolRecord(UDTRecordEnd); 3166 } 3167 } 3168 3169 void CodeViewDebug::collectGlobalVariableInfo() { 3170 DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *> 3171 GlobalMap; 3172 for (const GlobalVariable &GV : MMI->getModule()->globals()) { 3173 SmallVector<DIGlobalVariableExpression *, 1> GVEs; 3174 GV.getDebugInfo(GVEs); 3175 for (const auto *GVE : GVEs) 3176 GlobalMap[GVE] = &GV; 3177 } 3178 3179 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); 3180 for (const MDNode *Node : CUs->operands()) { 3181 const auto *CU = cast<DICompileUnit>(Node); 3182 for (const auto *GVE : CU->getGlobalVariables()) { 3183 const DIGlobalVariable *DIGV = GVE->getVariable(); 3184 const DIExpression *DIE = GVE->getExpression(); 3185 3186 if ((DIE->getNumElements() == 2) && 3187 (DIE->getElement(0) == dwarf::DW_OP_plus_uconst)) 3188 // Record the constant offset for the variable. 3189 // 3190 // A Fortran common block uses this idiom to encode the offset 3191 // of a variable from the common block's starting address. 3192 CVGlobalVariableOffsets.insert( 3193 std::make_pair(DIGV, DIE->getElement(1))); 3194 3195 // Emit constant global variables in a global symbol section. 3196 if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) { 3197 CVGlobalVariable CVGV = {DIGV, DIE}; 3198 GlobalVariables.emplace_back(std::move(CVGV)); 3199 } 3200 3201 const auto *GV = GlobalMap.lookup(GVE); 3202 if (!GV || GV->isDeclarationForLinker()) 3203 continue; 3204 3205 DIScope *Scope = DIGV->getScope(); 3206 SmallVector<CVGlobalVariable, 1> *VariableList; 3207 if (Scope && isa<DILocalScope>(Scope)) { 3208 // Locate a global variable list for this scope, creating one if 3209 // necessary. 3210 auto Insertion = ScopeGlobals.insert( 3211 {Scope, std::unique_ptr<GlobalVariableList>()}); 3212 if (Insertion.second) 3213 Insertion.first->second = std::make_unique<GlobalVariableList>(); 3214 VariableList = Insertion.first->second.get(); 3215 } else if (GV->hasComdat()) 3216 // Emit this global variable into a COMDAT section. 3217 VariableList = &ComdatVariables; 3218 else 3219 // Emit this global variable in a single global symbol section. 3220 VariableList = &GlobalVariables; 3221 CVGlobalVariable CVGV = {DIGV, GV}; 3222 VariableList->emplace_back(std::move(CVGV)); 3223 } 3224 } 3225 } 3226 3227 void CodeViewDebug::collectDebugInfoForGlobals() { 3228 for (const CVGlobalVariable &CVGV : GlobalVariables) { 3229 const DIGlobalVariable *DIGV = CVGV.DIGV; 3230 const DIScope *Scope = DIGV->getScope(); 3231 getCompleteTypeIndex(DIGV->getType()); 3232 getFullyQualifiedName(Scope, DIGV->getName()); 3233 } 3234 3235 for (const CVGlobalVariable &CVGV : ComdatVariables) { 3236 const DIGlobalVariable *DIGV = CVGV.DIGV; 3237 const DIScope *Scope = DIGV->getScope(); 3238 getCompleteTypeIndex(DIGV->getType()); 3239 getFullyQualifiedName(Scope, DIGV->getName()); 3240 } 3241 } 3242 3243 void CodeViewDebug::emitDebugInfoForGlobals() { 3244 // First, emit all globals that are not in a comdat in a single symbol 3245 // substream. MSVC doesn't like it if the substream is empty, so only open 3246 // it if we have at least one global to emit. 3247 switchToDebugSectionForSymbol(nullptr); 3248 if (!GlobalVariables.empty() || !StaticConstMembers.empty()) { 3249 OS.AddComment("Symbol subsection for globals"); 3250 MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols); 3251 emitGlobalVariableList(GlobalVariables); 3252 emitStaticConstMemberList(); 3253 endCVSubsection(EndLabel); 3254 } 3255 3256 // Second, emit each global that is in a comdat into its own .debug$S 3257 // section along with its own symbol substream. 3258 for (const CVGlobalVariable &CVGV : ComdatVariables) { 3259 const GlobalVariable *GV = CVGV.GVInfo.get<const GlobalVariable *>(); 3260 MCSymbol *GVSym = Asm->getSymbol(GV); 3261 OS.AddComment("Symbol subsection for " + 3262 Twine(GlobalValue::dropLLVMManglingEscape(GV->getName()))); 3263 switchToDebugSectionForSymbol(GVSym); 3264 MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols); 3265 // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. 3266 emitDebugInfoForGlobal(CVGV); 3267 endCVSubsection(EndLabel); 3268 } 3269 } 3270 3271 void CodeViewDebug::emitDebugInfoForRetainedTypes() { 3272 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); 3273 for (const MDNode *Node : CUs->operands()) { 3274 for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) { 3275 if (DIType *RT = dyn_cast<DIType>(Ty)) { 3276 getTypeIndex(RT); 3277 // FIXME: Add to global/local DTU list. 3278 } 3279 } 3280 } 3281 } 3282 3283 // Emit each global variable in the specified array. 3284 void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) { 3285 for (const CVGlobalVariable &CVGV : Globals) { 3286 // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. 3287 emitDebugInfoForGlobal(CVGV); 3288 } 3289 } 3290 3291 void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value, 3292 const std::string &QualifiedName) { 3293 MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT); 3294 OS.AddComment("Type"); 3295 OS.emitInt32(getTypeIndex(DTy).getIndex()); 3296 3297 OS.AddComment("Value"); 3298 3299 // Encoded integers shouldn't need more than 10 bytes. 3300 uint8_t Data[10]; 3301 BinaryStreamWriter Writer(Data, llvm::support::endianness::little); 3302 CodeViewRecordIO IO(Writer); 3303 cantFail(IO.mapEncodedInteger(Value)); 3304 StringRef SRef((char *)Data, Writer.getOffset()); 3305 OS.emitBinaryData(SRef); 3306 3307 OS.AddComment("Name"); 3308 emitNullTerminatedSymbolName(OS, QualifiedName); 3309 endSymbolRecord(SConstantEnd); 3310 } 3311 3312 void CodeViewDebug::emitStaticConstMemberList() { 3313 for (const DIDerivedType *DTy : StaticConstMembers) { 3314 const DIScope *Scope = DTy->getScope(); 3315 3316 APSInt Value; 3317 if (const ConstantInt *CI = 3318 dyn_cast_or_null<ConstantInt>(DTy->getConstant())) 3319 Value = APSInt(CI->getValue(), 3320 DebugHandlerBase::isUnsignedDIType(DTy->getBaseType())); 3321 else if (const ConstantFP *CFP = 3322 dyn_cast_or_null<ConstantFP>(DTy->getConstant())) 3323 Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true); 3324 else 3325 llvm_unreachable("cannot emit a constant without a value"); 3326 3327 emitConstantSymbolRecord(DTy->getBaseType(), Value, 3328 getFullyQualifiedName(Scope, DTy->getName())); 3329 } 3330 } 3331 3332 static bool isFloatDIType(const DIType *Ty) { 3333 if (isa<DICompositeType>(Ty)) 3334 return false; 3335 3336 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { 3337 dwarf::Tag T = (dwarf::Tag)Ty->getTag(); 3338 if (T == dwarf::DW_TAG_pointer_type || 3339 T == dwarf::DW_TAG_ptr_to_member_type || 3340 T == dwarf::DW_TAG_reference_type || 3341 T == dwarf::DW_TAG_rvalue_reference_type) 3342 return false; 3343 assert(DTy->getBaseType() && "Expected valid base type"); 3344 return isFloatDIType(DTy->getBaseType()); 3345 } 3346 3347 auto *BTy = cast<DIBasicType>(Ty); 3348 return (BTy->getEncoding() == dwarf::DW_ATE_float); 3349 } 3350 3351 void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) { 3352 const DIGlobalVariable *DIGV = CVGV.DIGV; 3353 3354 const DIScope *Scope = DIGV->getScope(); 3355 // For static data members, get the scope from the declaration. 3356 if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>( 3357 DIGV->getRawStaticDataMemberDeclaration())) 3358 Scope = MemberDecl->getScope(); 3359 // For Fortran, the scoping portion is elided in its name so that we can 3360 // reference the variable in the command line of the VS debugger. 3361 std::string QualifiedName = 3362 (moduleIsInFortran()) ? std::string(DIGV->getName()) 3363 : getFullyQualifiedName(Scope, DIGV->getName()); 3364 3365 if (const GlobalVariable *GV = 3366 CVGV.GVInfo.dyn_cast<const GlobalVariable *>()) { 3367 // DataSym record, see SymbolRecord.h for more info. Thread local data 3368 // happens to have the same format as global data. 3369 MCSymbol *GVSym = Asm->getSymbol(GV); 3370 SymbolKind DataSym = GV->isThreadLocal() 3371 ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32 3372 : SymbolKind::S_GTHREAD32) 3373 : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32 3374 : SymbolKind::S_GDATA32); 3375 MCSymbol *DataEnd = beginSymbolRecord(DataSym); 3376 OS.AddComment("Type"); 3377 OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex()); 3378 OS.AddComment("DataOffset"); 3379 3380 uint64_t Offset = 0; 3381 if (CVGlobalVariableOffsets.find(DIGV) != CVGlobalVariableOffsets.end()) 3382 // Use the offset seen while collecting info on globals. 3383 Offset = CVGlobalVariableOffsets[DIGV]; 3384 OS.EmitCOFFSecRel32(GVSym, Offset); 3385 3386 OS.AddComment("Segment"); 3387 OS.EmitCOFFSectionIndex(GVSym); 3388 OS.AddComment("Name"); 3389 const unsigned LengthOfDataRecord = 12; 3390 emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord); 3391 endSymbolRecord(DataEnd); 3392 } else { 3393 const DIExpression *DIE = CVGV.GVInfo.get<const DIExpression *>(); 3394 assert(DIE->isConstant() && 3395 "Global constant variables must contain a constant expression."); 3396 3397 // Use unsigned for floats. 3398 bool isUnsigned = isFloatDIType(DIGV->getType()) 3399 ? true 3400 : DebugHandlerBase::isUnsignedDIType(DIGV->getType()); 3401 APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned); 3402 emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName); 3403 } 3404 } 3405