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