diff --git a/bolt/include/bolt/Core/DebugData.h b/bolt/include/bolt/Core/DebugData.h index 2000f512f32466..fb9ddc7d17bc2e 100644 --- a/bolt/include/bolt/Core/DebugData.h +++ b/bolt/include/bolt/Core/DebugData.h @@ -336,7 +336,7 @@ using AddressSectionBuffer = SmallVector; class DebugAddrWriter { public: DebugAddrWriter() = delete; - DebugAddrWriter(BinaryContext *BC_) : DebugAddrWriter(BC_, 255) {}; + DebugAddrWriter(BinaryContext *BC_) : DebugAddrWriter(BC_, UCHAR_MAX){}; DebugAddrWriter(BinaryContext *BC_, uint8_t AddressByteSize); virtual ~DebugAddrWriter(){}; /// Given an address returns an index in .debug_addr. @@ -353,10 +353,10 @@ class DebugAddrWriter { } /// Returns buffer size. - virtual size_t getBufferSize() { return Buffer->size(); } + virtual size_t getBufferSize() const { return Buffer->size(); } /// Returns True if Buffer is not empty. - bool isInitialized() { return !Buffer->empty(); } + bool isInitialized() const { return !Buffer->empty(); } /// Updates address base with the given Offset. virtual void updateAddrBase(DIEBuilder &DIEBlder, DWARFUnit &CU, @@ -445,6 +445,7 @@ class DebugAddrWriterDwarf5 : public DebugAddrWriter { /// Write out entries in to .debug_addr section for CUs. virtual std::optional finalize(const size_t BufferSize) override; + /// Updates address base with the given Offset. virtual void updateAddrBase(DIEBuilder &DIEBlder, DWARFUnit &CU, const uint64_t Offset) override; @@ -613,7 +614,7 @@ class DebugLoclistWriter : public DebugLocWriter { DebugLoclistWriter(DWARFUnit &Unit, uint8_t DV, bool SD, DebugAddrWriter &AddrW) : DebugLocWriter(DV, LocWriterKind::DebugLoclistWriter), - AddrWriter(&AddrW), CU(Unit), IsSplitDwarf(SD) { + AddrWriter(AddrW), CU(Unit), IsSplitDwarf(SD) { if (DwarfVersion >= 5) { LocBodyBuffer = std::make_unique(); LocBodyStream = std::make_unique(*LocBodyBuffer); @@ -653,7 +654,7 @@ class DebugLoclistWriter : public DebugLocWriter { /// Writes out locations in to a local buffer and applies debug info patches. void finalizeDWARF5(DIEBuilder &DIEBldr, DIE &Die); - DebugAddrWriter *AddrWriter; + DebugAddrWriter &AddrWriter; DWARFUnit &CU; bool IsSplitDwarf{false}; // Used for DWARF5 to store location lists before being finalized. diff --git a/bolt/include/bolt/Rewrite/DWARFRewriter.h b/bolt/include/bolt/Rewrite/DWARFRewriter.h index ac3b26b08e5937..abd18b56113b62 100644 --- a/bolt/include/bolt/Rewrite/DWARFRewriter.h +++ b/bolt/include/bolt/Rewrite/DWARFRewriter.h @@ -66,10 +66,6 @@ class DWARFRewriter { /// .debug_aranges DWARF section. std::unique_ptr ARangesSectionWriter; - /// Stores and serializes information that will be put into the - /// .debug_addr DWARF section. - std::unique_ptr FinalAddrWriter; - /// Stores and serializes information that will be put in to the /// .debug_addr DWARF section. /// Does not do de-duplication. @@ -146,13 +142,15 @@ class DWARFRewriter { /// Process and write out CUs that are passsed in. void finalizeCompileUnits(DIEBuilder &DIEBlder, DIEStreamer &Streamer, CUOffsetMap &CUMap, - const std::list &CUs); + const std::list &CUs, + DebugAddrWriter &FinalAddrWriter); /// Finalize debug sections in the main binary. void finalizeDebugSections(DIEBuilder &DIEBlder, DWARF5AcceleratorTable &DebugNamesTable, DIEStreamer &Streamer, raw_svector_ostream &ObjOS, - CUOffsetMap &CUMap); + CUOffsetMap &CUMap, + DebugAddrWriter &FinalAddrWriter); /// Patches the binary for DWARF address ranges (e.g. in functions and lexical /// blocks) to be updated. diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp index b5d5c0a62fd2bf..002f58c4743466 100644 --- a/bolt/lib/Core/DebugData.cpp +++ b/bolt/lib/Core/DebugData.cpp @@ -711,11 +711,11 @@ void DebugLoclistWriter::addList(DIEBuilder &DIEBldr, DIE &Die, DIEValue &AttrInfo, DebugLocationsVector &LocList) { if (DwarfVersion < 5) - writeLegacyLocList(AttrInfo, LocList, DIEBldr, Die, *AddrWriter, *LocBuffer, + writeLegacyLocList(AttrInfo, LocList, DIEBldr, Die, AddrWriter, *LocBuffer, CU, *LocStream); else writeDWARF5LocList(NumberOfEntries, AttrInfo, LocList, Die, DIEBldr, - *AddrWriter, *LocBodyBuffer, RelativeLocListOffsets, CU, + AddrWriter, *LocBodyBuffer, RelativeLocListOffsets, CU, *LocBodyStream); } diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp index ccb541c9bb361c..042c39a574561a 100644 --- a/bolt/lib/Rewrite/DWARFRewriter.cpp +++ b/bolt/lib/Rewrite/DWARFRewriter.cpp @@ -612,6 +612,10 @@ void DWARFRewriter::updateDebugInfo() { errs() << "BOLT-WARNING: --deterministic-debuginfo is being deprecated\n"; } + /// Stores and serializes information that will be put into the + /// .debug_addr DWARF section. + std::unique_ptr FinalAddrWriter; + if (BC.isDWARF5Used()) { FinalAddrWriter = std::make_unique(&BC); RangeListsSectionWriter = std::make_unique(); @@ -641,19 +645,18 @@ void DWARFRewriter::updateDebugInfo() { if (std::optional DWOId = CU.getDWOId()) { assert(RangeListsWritersByCU.count(*DWOId) == 0 && "RangeLists writer for DWO unit already exists."); - auto RangeListSectionWriter = + auto DWORangeListsSectionWriter = std::make_unique(); - RangeListSectionWriter->initSection(CU); - RangeListSectionWriter->setAddressWriter(AddrW.get()); - RangeListsWritersByCU.insert( - {*DWOId, std::move(RangeListSectionWriter)}); + DWORangeListsSectionWriter->initSection(CU); + DWORangeListsSectionWriter->setAddressWriter(AddrW.get()); + RangeListsWritersByCU[*DWOId] = std::move(DWORangeListsSectionWriter); } - AddressWritersByCU.insert({CU.getOffset(), std::move(AddrW)}); + AddressWritersByCU[CU.getOffset()] = std::move(AddrW); } else { auto AddrW = std::make_unique(&BC, CU.getAddressByteSize()); - AddressWritersByCU.insert({CU.getOffset(), std::move(AddrW)}); + AddressWritersByCU[CU.getOffset()] = std::move(AddrW); LocListWritersByCU[CUIndex] = std::make_unique(); if (std::optional DWOId = CU.getDWOId()) { assert(LegacyRangesWritersByCU.count(*DWOId) == 0 && @@ -661,8 +664,8 @@ void DWARFRewriter::updateDebugInfo() { auto LegacyRangesSectionWriterByCU = std::make_unique(); LegacyRangesSectionWriterByCU->initSection(CU); - LegacyRangesWritersByCU.insert( - {*DWOId, std::move(LegacyRangesSectionWriterByCU)}); + LegacyRangesWritersByCU[*DWOId] = + std::move(LegacyRangesSectionWriterByCU); } } return LocListWritersByCU[CUIndex++].get(); @@ -768,7 +771,7 @@ void DWARFRewriter::updateDebugInfo() { for (DWARFUnit *CU : DIEBlder.getProcessedCUs()) processUnitDIE(CU, &DIEBlder); finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap, - DIEBlder.getProcessedCUs()); + DIEBlder.getProcessedCUs(), *FinalAddrWriter); } } else { // Update unit debug info in parallel @@ -783,8 +786,8 @@ void DWARFRewriter::updateDebugInfo() { if (opts::WriteDWP) finalizeDWP(State); - finalizeDebugSections(DIEBlder, DebugNamesTable, *Streamer, *ObjOS, - OffsetMap); + finalizeDebugSections(DIEBlder, DebugNamesTable, *Streamer, *ObjOS, OffsetMap, + *FinalAddrWriter); GDBIndexSection.updateGdbIndexSection(OffsetMap, CUIndex, *ARangesSectionWriter); } @@ -1522,7 +1525,8 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder, void DWARFRewriter::finalizeDebugSections( DIEBuilder &DIEBlder, DWARF5AcceleratorTable &DebugNamesTable, - DIEStreamer &Streamer, raw_svector_ostream &ObjOS, CUOffsetMap &CUMap) { + DIEStreamer &Streamer, raw_svector_ostream &ObjOS, CUOffsetMap &CUMap, + DebugAddrWriter &FinalAddrWriter) { if (StrWriter->isInitialized()) { RewriteInstance::addToDebugSectionsToOverwrite(".debug_str"); std::unique_ptr DebugStrSectionContents = @@ -1575,9 +1579,9 @@ void DWARFRewriter::finalizeDebugSections( LocationListSectionContents->size()); } - if (FinalAddrWriter->isInitialized()) { + if (FinalAddrWriter.isInitialized()) { std::unique_ptr AddressSectionContents = - FinalAddrWriter->releaseBuffer(); + FinalAddrWriter.releaseBuffer(); BC.registerOrUpdateNoteSection(".debug_addr", copyByteArray(*AddressSectionContents), AddressSectionContents->size()); @@ -1633,13 +1637,14 @@ void DWARFRewriter::finalizeDebugSections( void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder, DIEStreamer &Streamer, CUOffsetMap &CUMap, - const std::list &CUs) { + const std::list &CUs, + DebugAddrWriter &FinalAddrWriter) { for (DWARFUnit *CU : CUs) { auto AddressWriterIterator = AddressWritersByCU.find(CU->getOffset()); assert(AddressWriterIterator != AddressWritersByCU.end() && "AddressWriter does not exist for CU"); DebugAddrWriter *AddressWriter = AddressWriterIterator->second.get(); - const size_t BufferOffset = FinalAddrWriter->getBufferSize(); + const size_t BufferOffset = FinalAddrWriter.getBufferSize(); std::optional Offset = AddressWriter->finalize(BufferOffset); /// If Offset already exists in UnmodifiedAddressOffsets, then update with /// Offset, else update with BufferOffset. @@ -1650,7 +1655,7 @@ void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder, if (AddressWriter->isInitialized()) { std::unique_ptr AddressSectionContents = AddressWriter->releaseBuffer(); - FinalAddrWriter->appendToAddressBuffer(*AddressSectionContents); + FinalAddrWriter.appendToAddressBuffer(*AddressSectionContents); } if (CU->getVersion() != 4) continue;