@@ -620,8 +620,8 @@ void DWARFRewriter::updateDebugInfo() {
620620 uint32_t CUIndex = 0 ;
621621 std::mutex AccessMutex;
622622 // Needs to be invoked in the same order as CUs are processed.
623- auto createRangeLocListAddressWriters =
624- [&](DWARFUnit &CU) -> DebugLocWriter * {
623+ llvm::DenseMap< uint64_t , uint64_t > LocListWritersIndexByCU;
624+ auto createRangeLocListAddressWriters = [&](DWARFUnit &CU) {
625625 std::lock_guard<std::mutex> Lock (AccessMutex);
626626 const uint16_t DwarfVersion = CU.getVersion ();
627627 if (DwarfVersion >= 5 ) {
@@ -641,7 +641,6 @@ void DWARFRewriter::updateDebugInfo() {
641641 RangeListsWritersByCU[*DWOId] = std::move (DWORangeListsSectionWriter);
642642 }
643643 AddressWritersByCU[CU.getOffset ()] = std::move (AddrW);
644-
645644 } else {
646645 auto AddrW =
647646 std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize ());
@@ -657,7 +656,7 @@ void DWARFRewriter::updateDebugInfo() {
657656 std::move (LegacyRangesSectionWriterByCU);
658657 }
659658 }
660- return LocListWritersByCU[CUIndex++]. get () ;
659+ LocListWritersIndexByCU[CU. getOffset ()] = CUIndex++ ;
661660 };
662661
663662 DWARF5AcceleratorTable DebugNamesTable (opts::CreateDebugNames, BC,
@@ -666,74 +665,70 @@ void DWARFRewriter::updateDebugInfo() {
666665 DWPState State;
667666 if (opts::WriteDWP)
668667 initDWPState (State);
669- auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
670- // Check if the unit is a skeleton and we need special updates for it and
671- // its matching split/DWO CU.
668+ auto processSplitCU = [&](DWARFUnit &Unit, DWARFUnit &SplitCU,
669+ DIEBuilder &DIEBlder,
670+ DebugRangesSectionWriter &TempRangesSectionWriter,
671+ DebugAddrWriter &AddressWriter) {
672+ DIEBuilder DWODIEBuilder (BC, &(SplitCU).getContext (), DebugNamesTable,
673+ &Unit);
674+ DWODIEBuilder.buildDWOUnit (SplitCU);
675+ std::string DWOName = " " ;
676+ std::optional<std::string> DwarfOutputPath =
677+ opts::DwarfOutputPath.empty ()
678+ ? std::nullopt
679+ : std::optional<std::string>(opts::DwarfOutputPath.c_str ());
680+ {
681+ std::lock_guard<std::mutex> Lock (AccessMutex);
682+ DWOName = DIEBlder.updateDWONameCompDir (
683+ *StrOffstsWriter, *StrWriter, Unit, DwarfOutputPath, std::nullopt );
684+ }
685+ DebugStrOffsetsWriter DWOStrOffstsWriter (BC);
686+ DebugStrWriter DWOStrWriter ((SplitCU).getContext (), true );
687+ DWODIEBuilder.updateDWONameCompDirForTypes (
688+ DWOStrOffstsWriter, DWOStrWriter, SplitCU, DwarfOutputPath, DWOName);
689+ DebugLoclistWriter DebugLocDWoWriter (Unit, Unit.getVersion (), true ,
690+ AddressWriter);
691+
692+ updateUnitDebugInfo (SplitCU, DWODIEBuilder, DebugLocDWoWriter,
693+ TempRangesSectionWriter, AddressWriter);
694+ DebugLocDWoWriter.finalize (DWODIEBuilder,
695+ *DWODIEBuilder.getUnitDIEbyUnit (SplitCU));
696+ if (Unit.getVersion () >= 5 )
697+ TempRangesSectionWriter.finalizeSection ();
698+
699+ emitDWOBuilder (DWOName, DWODIEBuilder, *this , SplitCU, Unit, State,
700+ DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
701+ GDBIndexSection);
702+ };
703+ auto processMainBinaryCU = [&](DWARFUnit &Unit, DIEBuilder &DIEBlder) {
672704 std::optional<DWARFUnit *> SplitCU;
673705 std::optional<uint64_t > RangesBase;
674- std::optional<uint64_t > DWOId = Unit-> getDWOId ();
706+ std::optional<uint64_t > DWOId = Unit. getDWOId ();
675707 if (DWOId)
676708 SplitCU = BC.getDWOCU (*DWOId);
677- DebugLocWriter *DebugLocWriter = createRangeLocListAddressWriters (*Unit);
678- DebugRangesSectionWriter *RangesSectionWriter =
679- Unit->getVersion () >= 5 ? RangeListsSectionWriter.get ()
680- : LegacyRangesSectionWriter.get ();
681- DebugAddrWriter *AddressWriter =
682- AddressWritersByCU[Unit->getOffset ()].get ();
683- // Skipping CUs that failed to load.
684- if (SplitCU) {
685- DIEBuilder DWODIEBuilder (BC, &(*SplitCU)->getContext (), DebugNamesTable,
686- Unit);
687- DWODIEBuilder.buildDWOUnit (**SplitCU);
688- std::string DWOName = " " ;
689- std::optional<std::string> DwarfOutputPath =
690- opts::DwarfOutputPath.empty ()
691- ? std::nullopt
692- : std::optional<std::string>(opts::DwarfOutputPath.c_str ());
693- {
694- std::lock_guard<std::mutex> Lock (AccessMutex);
695- DWOName = DIEBlder->updateDWONameCompDir (
696- *StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt );
697- }
698- DebugStrOffsetsWriter DWOStrOffstsWriter (BC);
699- DebugStrWriter DWOStrWriter ((*SplitCU)->getContext (), true );
700- DWODIEBuilder.updateDWONameCompDirForTypes (DWOStrOffstsWriter,
701- DWOStrWriter, **SplitCU,
702- DwarfOutputPath, DWOName);
703- DebugLoclistWriter DebugLocDWoWriter (*Unit, Unit->getVersion (), true ,
704- *AddressWriter);
705- DebugRangesSectionWriter *TempRangesSectionWriter = RangesSectionWriter;
706- if (Unit->getVersion () >= 5 ) {
707- TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get ();
708- } else {
709- TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get ();
710- RangesBase = RangesSectionWriter->getSectionOffset ();
711- }
712-
713- updateUnitDebugInfo (*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
714- *TempRangesSectionWriter, *AddressWriter);
715- DebugLocDWoWriter.finalize (DWODIEBuilder,
716- *DWODIEBuilder.getUnitDIEbyUnit (**SplitCU));
717- if (Unit->getVersion () >= 5 )
718- TempRangesSectionWriter->finalizeSection ();
719-
720- emitDWOBuilder (DWOName, DWODIEBuilder, *this , **SplitCU, *Unit, State,
721- DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
722- GDBIndexSection);
723- }
724-
725- if (Unit->getVersion () >= 5 ) {
726- RangesBase = RangesSectionWriter->getSectionOffset () +
709+ DebugLocWriter &DebugLocWriter =
710+ *LocListWritersByCU[LocListWritersIndexByCU[Unit.getOffset ()]].get ();
711+ DebugRangesSectionWriter &RangesSectionWriter =
712+ Unit.getVersion () >= 5 ? *RangeListsSectionWriter.get ()
713+ : *LegacyRangesSectionWriter.get ();
714+ DebugAddrWriter &AddressWriter =
715+ *AddressWritersByCU[Unit.getOffset ()].get ();
716+ if (Unit.getVersion () >= 5 )
717+ RangeListsSectionWriter->setAddressWriter (&AddressWriter);
718+ if (Unit.getVersion () >= 5 ) {
719+ RangesBase = RangesSectionWriter.getSectionOffset () +
727720 getDWARF5RngListLocListHeaderSize ();
728- RangesSectionWriter->initSection (*Unit);
729- StrOffstsWriter->finalizeSection (*Unit, *DIEBlder);
721+ RangesSectionWriter.initSection (Unit);
722+ StrOffstsWriter->finalizeSection (Unit, DIEBlder);
723+ } else if (SplitCU) {
724+ RangesBase = LegacyRangesSectionWriter.get ()->getSectionOffset ();
730725 }
731726
732- updateUnitDebugInfo (* Unit, * DIEBlder, * DebugLocWriter, * RangesSectionWriter,
733- * AddressWriter, RangesBase);
734- DebugLocWriter-> finalize (* DIEBlder, *DIEBlder-> getUnitDIEbyUnit (* Unit));
735- if (Unit-> getVersion () >= 5 )
736- RangesSectionWriter-> finalizeSection ();
727+ updateUnitDebugInfo (Unit, DIEBlder, DebugLocWriter, RangesSectionWriter,
728+ AddressWriter, RangesBase);
729+ DebugLocWriter. finalize (DIEBlder, *DIEBlder. getUnitDIEbyUnit (Unit));
730+ if (Unit. getVersion () >= 5 )
731+ RangesSectionWriter. finalizeSection ();
737732 };
738733
739734 DIEBuilder DIEBlder (BC, BC.DwCtx .get (), DebugNamesTable);
@@ -751,8 +746,24 @@ void DWARFRewriter::updateDebugInfo() {
751746 CUPartitionVector PartVec = partitionCUs (*BC.DwCtx );
752747 for (std::vector<DWARFUnit *> &Vec : PartVec) {
753748 DIEBlder.buildCompileUnits (Vec);
749+ for (DWARFUnit *CU : DIEBlder.getProcessedCUs ()) {
750+ createRangeLocListAddressWriters (*CU);
751+ std::optional<DWARFUnit *> SplitCU;
752+ std::optional<uint64_t > DWOId = CU->getDWOId ();
753+ if (DWOId)
754+ SplitCU = BC.getDWOCU (*DWOId);
755+ if (!SplitCU)
756+ continue ;
757+ DebugAddrWriter &AddressWriter =
758+ *AddressWritersByCU[CU->getOffset ()].get ();
759+ DebugRangesSectionWriter *TempRangesSectionWriter =
760+ CU->getVersion () >= 5 ? RangeListsWritersByCU[*DWOId].get ()
761+ : LegacyRangesWritersByCU[*DWOId].get ();
762+ processSplitCU (*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
763+ AddressWriter);
764+ }
754765 for (DWARFUnit *CU : DIEBlder.getProcessedCUs ())
755- processUnitDIE ( CU, & DIEBlder);
766+ processMainBinaryCU (* CU, DIEBlder);
756767 finalizeCompileUnits (DIEBlder, *Streamer, OffsetMap,
757768 DIEBlder.getProcessedCUs (), *FinalAddrWriter);
758769 }
0 commit comments