Skip to content

Commit fb0e512

Browse files
committed
CodeGen: Fix CodeView crashes with empty llvm.dbg.cu
1 parent 5ba0b91 commit fb0e512

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,13 @@ void CodeViewDebug::beginModule(Module *M) {
625625
if (Asm->hasDebugInfo()) {
626626
Node = *M->debug_compile_units_begin();
627627
} else {
628+
auto DebugCompileUnits = MMI->getModule()->debug_compile_units();
629+
if (DebugCompileUnits.empty())
630+
return;
631+
628632
// When emitting only compiler information, we may have only NoDebug CUs,
629633
// which would be skipped by debug_compile_units_begin.
630-
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
631-
Node = *CUs->operands().begin();
634+
Node = *DebugCompileUnits.begin();
632635
}
633636
const auto *CU = cast<DICompileUnit>(Node);
634637
DISourceLanguageName Lang = CU->getSourceLanguage();
@@ -900,8 +903,11 @@ void CodeViewDebug::emitCompilerInformation() {
900903
OS.AddComment("CPUType");
901904
OS.emitInt16(static_cast<uint64_t>(TheCPU));
902905

903-
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
904-
const MDNode *Node = *CUs->operands().begin();
906+
auto CUs = MMI->getModule()->debug_compile_units();
907+
if (CUs.empty())
908+
return;
909+
910+
const MDNode *Node = *CUs.begin();
905911
const auto *CU = cast<DICompileUnit>(Node);
906912

907913
StringRef CompilerVersion = CU->getProducer();
@@ -948,8 +954,11 @@ void CodeViewDebug::emitBuildInfo() {
948954
// not clear if the compiler path should refer to the executable for the
949955
// frontend or the backend. Leave it blank for now.
950956
TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
951-
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
952-
const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
957+
auto CUs = MMI->getModule()->debug_compile_units();
958+
if (CUs.empty())
959+
return;
960+
961+
const MDNode *Node = *CUs.begin(); // FIXME: Multiple CUs.
953962
const auto *CU = cast<DICompileUnit>(Node);
954963
const DIFile *MainSourceFile = CU->getFile();
955964
BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
2+
3+
; CHECK: .file "<stdin>"
4+
; CHECK-NEXT: .section .debug$S,"dr"
5+
; CHECK-NEXT: .p2align 2, 0x0
6+
; CHECK-NEXT: .long 4 # Debug section magic
7+
; CHECK-NEXT: .long 241
8+
; CHECK-NEXT: .long .Ltmp1-.Ltmp0 # Subsection size
9+
; CHECK-NEXT: .Ltmp0:
10+
; CHECK-NEXT: .short .Ltmp3-.Ltmp2 # Record length
11+
; CHECK-NEXT: .Ltmp2:
12+
; CHECK-NEXT: .short 4353 # Record kind: S_OBJNAME
13+
; CHECK-NEXT: .long 0 # Signature
14+
; CHECK-NEXT: .byte 0 # Object name
15+
; CHECK-NEXT: .p2align 2, 0x0
16+
; CHECK-NEXT: .Ltmp3:
17+
; CHECK-NEXT: .short .Ltmp5-.Ltmp4 # Record length
18+
; CHECK-NEXT: .Ltmp4:
19+
; CHECK-NEXT: .short 4412 # Record kind: S_COMPILE3
20+
; CHECK-NEXT: .long 3 # Flags and language
21+
; CHECK-NEXT: .short 208 # CPUType
22+
; CHECK-NEXT: .Ltmp1:
23+
; CHECK-NEXT: .p2align 2, 0x0
24+
; CHECK-NEXT: .cv_filechecksums # File index to string table offset subsection
25+
; CHECK-NEXT: .cv_stringtable # String table
26+
27+
!llvm.dbg.cu = !{}
28+
!llvm.module.flags = !{!0}
29+
30+
!0 = !{i32 2, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)