@@ -125,6 +125,8 @@ static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
125125    return  CPUType::ARM64;
126126  case  Triple::ArchType::mipsel:
127127    return  CPUType::MIPS;
128+   case  Triple::ArchType::UnknownArch:
129+     return  CPUType::Unknown;
128130  default :
129131    report_fatal_error (" target architecture doesn't map to a CodeView CPUType" 
130132  }
@@ -611,21 +613,33 @@ static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
611613}
612614
613615void  CodeViewDebug::beginModule (Module *M) {
614-   //  If module doesn't have named metadata anchors or COFF debug section
615-   //  is not available, skip any debug info related stuff.
616-   if  (!Asm->hasDebugInfo () ||
617-       !Asm->getObjFileLowering ().getCOFFDebugSymbolsSection ()) {
616+   //  If COFF debug section is not available, skip any debug info related stuff.
617+   if  (!Asm->getObjFileLowering ().getCOFFDebugSymbolsSection ()) {
618618    Asm = nullptr ;
619619    return ;
620620  }
621621
622+   CompilerInfoAsm = Asm;
622623  TheCPU = mapArchToCVCPUType (M->getTargetTriple ().getArch ());
623624
624625  //  Get the current source language.
625-   const  MDNode *Node = *M->debug_compile_units_begin ();
626+   const  MDNode *Node;
627+   if  (Asm->hasDebugInfo ()) {
628+     Node = *M->debug_compile_units_begin ();
629+   } else  {
630+     //  When emitting only compiler information, we may have only NoDebug CUs,
631+     //  which would be skipped by debug_compile_units_begin.
632+     NamedMDNode *CUs = MMI->getModule ()->getNamedMetadata (" llvm.dbg.cu" 
633+     Node = *CUs->operands ().begin ();
634+   }
626635  const  auto  *CU = cast<DICompileUnit>(Node);
627636
628637  CurrentSourceLanguage = MapDWLangToCVLang (CU->getSourceLanguage ());
638+   if  (!M->getCodeViewFlag () ||
639+       CU->getEmissionKind () == DICompileUnit::NoDebug) {
640+     Asm = nullptr ;
641+     return ;
642+   }
629643
630644  collectGlobalVariableInfo ();
631645
@@ -636,7 +650,7 @@ void CodeViewDebug::beginModule(Module *M) {
636650}
637651
638652void  CodeViewDebug::endModule () {
639-   if  (!Asm || !Asm-> hasDebugInfo () )
653+   if  (!CompilerInfoAsm )
640654    return ;
641655
642656  //  The COFF .debug$S section consists of several subsections, each starting
@@ -652,6 +666,8 @@ void CodeViewDebug::endModule() {
652666  emitObjName ();
653667  emitCompilerInformation ();
654668  endCVSubsection (CompilerInfo);
669+   if  (!Asm)
670+     return ;
655671
656672  emitInlineeLinesSubsection ();
657673
@@ -788,7 +804,7 @@ void CodeViewDebug::emitTypeGlobalHashes() {
788804void  CodeViewDebug::emitObjName () {
789805  MCSymbol *CompilerEnd = beginSymbolRecord (SymbolKind::S_OBJNAME);
790806
791-   StringRef PathRef (Asm ->TM .Options .ObjectFilenameForDebug );
807+   StringRef PathRef (CompilerInfoAsm ->TM .Options .ObjectFilenameForDebug );
792808  llvm::SmallString<256 > PathStore (PathRef);
793809
794810  if  (PathRef.empty () || PathRef == " -" 
@@ -846,7 +862,7 @@ void CodeViewDebug::emitCompilerInformation() {
846862  }
847863  using  ArchType = llvm::Triple::ArchType;
848864  ArchType Arch = MMI->getModule ()->getTargetTriple ().getArch ();
849-   if  (Asm ->TM .Options .Hotpatch  || Arch == ArchType::thumb ||
865+   if  (CompilerInfoAsm ->TM .Options .Hotpatch  || Arch == ArchType::thumb ||
850866      Arch == ArchType::aarch64) {
851867    Flags |= static_cast <uint32_t >(CompileSym3Flags::HotPatch);
852868  }
@@ -1015,7 +1031,7 @@ void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
10151031  const  MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol () : nullptr ;
10161032
10171033  MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
1018-       Asm ->getObjFileLowering ().getCOFFDebugSymbolsSection ());
1034+       CompilerInfoAsm ->getObjFileLowering ().getCOFFDebugSymbolsSection ());
10191035  DebugSec = OS.getContext ().getAssociativeCOFFSection (DebugSec, KeySym);
10201036
10211037  OS.switchSection (DebugSec);
0 commit comments