Skip to content

Commit e68d505

Browse files
authored
[BOLT][DWARF] Refactor helper functions that update DW_AT_comp_dir/DW_AT_dwo_name (#91237)
We need to update DW_AT_comp_dir/DW_AT_dwo_name TU in the .debug_info.dwo section so that the path is correct. Refactored helper functions to make it easier for next step.
1 parent d3e77f5 commit e68d505

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

bolt/include/bolt/Rewrite/DWARFRewriter.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,6 @@ class DWARFRewriter {
177177
DIEValue &HighPCAttrInfo,
178178
std::optional<uint64_t> RangesBase = std::nullopt);
179179

180-
/// Adds a \p Str to .debug_str section.
181-
/// Uses \p AttrInfoVal to either update entry in a DIE for legacy DWARF using
182-
/// \p DebugInfoPatcher, or for DWARF5 update an index in .debug_str_offsets
183-
/// for this contribution of \p Unit.
184-
void addStringHelper(DIEBuilder &DIEBldr, DIE &Die, const DWARFUnit &Unit,
185-
DIEValue &DIEAttrInfo, StringRef Str);
186-
187180
public:
188181
DWARFRewriter(BinaryContext &BC) : BC(BC) {}
189182

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -582,19 +582,51 @@ static void emitDWOBuilder(const std::string &DWOName,
582582
Rewriter.writeDWOFiles(CU, OverriddenSections, DWOName, LocWriter);
583583
}
584584

585-
void DWARFRewriter::addStringHelper(DIEBuilder &DIEBldr, DIE &Die,
586-
const DWARFUnit &Unit,
587-
DIEValue &DIEAttrInfo, StringRef Str) {
588-
uint32_t NewOffset = StrWriter->addString(Str);
585+
/// Adds a \p Str to .debug_str section.
586+
/// Uses \p AttrInfoVal to either update entry in a DIE for legacy DWARF using
587+
/// \p DebugInfoPatcher, or for DWARF5 update an index in .debug_str_offsets
588+
/// for this contribution of \p Unit.
589+
static void addStringHelper(DebugStrOffsetsWriter &StrOffstsWriter,
590+
DebugStrWriter &StrWriter, DIEBuilder &DIEBldr,
591+
DIE &Die, const DWARFUnit &Unit,
592+
DIEValue &DIEAttrInfo, StringRef Str) {
593+
uint32_t NewOffset = StrWriter.addString(Str);
589594
if (Unit.getVersion() >= 5) {
590-
StrOffstsWriter->updateAddressMap(DIEAttrInfo.getDIEInteger().getValue(),
591-
NewOffset);
595+
StrOffstsWriter.updateAddressMap(DIEAttrInfo.getDIEInteger().getValue(),
596+
NewOffset);
592597
return;
593598
}
594599
DIEBldr.replaceValue(&Die, DIEAttrInfo.getAttribute(), DIEAttrInfo.getForm(),
595600
DIEInteger(NewOffset));
596601
}
597602

603+
static std::string
604+
updateDWONameCompDir(DebugStrOffsetsWriter &StrOffstsWriter,
605+
DebugStrWriter &StrWriter,
606+
std::unordered_map<std::string, uint32_t> &NameToIndexMap,
607+
DWARFUnit &Unit, DIEBuilder &DIEBldr, DIE &UnitDIE) {
608+
DIEValue DWONameAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_dwo_name);
609+
if (!DWONameAttrInfo)
610+
DWONameAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_GNU_dwo_name);
611+
assert(DWONameAttrInfo && "DW_AT_dwo_name is not in Skeleton CU.");
612+
std::string ObjectName;
613+
614+
ObjectName = getDWOName(Unit, NameToIndexMap);
615+
addStringHelper(StrOffstsWriter, StrWriter, DIEBldr, UnitDIE, Unit,
616+
DWONameAttrInfo, ObjectName.c_str());
617+
618+
DIEValue CompDirAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_comp_dir);
619+
assert(CompDirAttrInfo && "DW_AT_comp_dir is not in Skeleton CU.");
620+
621+
if (!opts::DwarfOutputPath.empty()) {
622+
if (!sys::fs::exists(opts::DwarfOutputPath))
623+
sys::fs::create_directory(opts::DwarfOutputPath);
624+
addStringHelper(StrOffstsWriter, StrWriter, DIEBldr, UnitDIE, Unit,
625+
CompDirAttrInfo, opts::DwarfOutputPath.c_str());
626+
}
627+
return ObjectName;
628+
}
629+
598630
using DWARFUnitVec = std::vector<DWARFUnit *>;
599631
using CUPartitionVector = std::vector<DWARFUnitVec>;
600632
/// Partitions CUs in to buckets. Bucket size is controlled by
@@ -692,33 +724,6 @@ void DWARFRewriter::updateDebugInfo() {
692724
// specified.
693725
std::unordered_map<std::string, uint32_t> NameToIndexMap;
694726

695-
auto updateDWONameCompDir = [&](DWARFUnit &Unit, DIEBuilder &DIEBldr,
696-
DIE &UnitDIE) -> std::string {
697-
DIEValue DWONameAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_dwo_name);
698-
if (!DWONameAttrInfo)
699-
DWONameAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_GNU_dwo_name);
700-
assert(DWONameAttrInfo && "DW_AT_dwo_name is not in Skeleton CU.");
701-
std::string ObjectName;
702-
703-
{
704-
std::lock_guard<std::mutex> Lock(AccessMutex);
705-
ObjectName = getDWOName(Unit, NameToIndexMap);
706-
}
707-
addStringHelper(DIEBldr, UnitDIE, Unit, DWONameAttrInfo,
708-
ObjectName.c_str());
709-
710-
DIEValue CompDirAttrInfo = UnitDIE.findAttribute(dwarf::DW_AT_comp_dir);
711-
assert(CompDirAttrInfo && "DW_AT_comp_dir is not in Skeleton CU.");
712-
713-
if (!opts::DwarfOutputPath.empty()) {
714-
if (!sys::fs::exists(opts::DwarfOutputPath))
715-
sys::fs::create_directory(opts::DwarfOutputPath);
716-
addStringHelper(DIEBldr, UnitDIE, Unit, CompDirAttrInfo,
717-
opts::DwarfOutputPath.c_str());
718-
}
719-
return ObjectName;
720-
};
721-
722727
DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
723728
*StrWriter);
724729
DWPState State;
@@ -741,8 +746,13 @@ void DWARFRewriter::updateDebugInfo() {
741746
DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
742747
Unit);
743748
DWODIEBuilder.buildDWOUnit(**SplitCU);
744-
std::string DWOName = updateDWONameCompDir(
745-
*Unit, *DIEBlder, *DIEBlder->getUnitDIEbyUnit(*Unit));
749+
std::string DWOName = "";
750+
{
751+
std::lock_guard<std::mutex> Lock(AccessMutex);
752+
DWOName = updateDWONameCompDir(*StrOffstsWriter, *StrWriter,
753+
NameToIndexMap, *Unit, *DIEBlder,
754+
*DIEBlder->getUnitDIEbyUnit(*Unit));
755+
}
746756

747757
DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true);
748758
DebugRangesSectionWriter *TempRangesSectionWriter = RangesSectionWriter;

0 commit comments

Comments
 (0)