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