[BOLT][DWARF][NFC] Fix DebugStrOffsetsWriter (#100672)

Fix DebugStrOffsetsWriter so updateAddressMap can't be called after it
is finalized.
This commit is contained in:
Sayhaan Siddiqui 2024-07-26 18:58:25 -07:00 committed by GitHub
parent 558315a0ea
commit 9a3e66e314
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 4 deletions

View File

@ -475,7 +475,8 @@ public:
}
/// Update Str offset in .debug_str in .debug_str_offsets.
void updateAddressMap(uint32_t Index, uint32_t Address);
void updateAddressMap(uint32_t Index, uint32_t Address,
const DWARFUnit &Unit);
/// Get offset for given index in original .debug_str_offsets section.
uint64_t getOffset(uint32_t Index) const { return StrOffsets[Index]; }
@ -507,6 +508,8 @@ private:
std::unique_ptr<DebugStrOffsetsBufferVector> StrOffsetsBuffer;
std::unique_ptr<raw_svector_ostream> StrOffsetsStream;
std::map<uint32_t, uint32_t> IndexToAddressMap;
[[maybe_unused]]
DenseSet<uint64_t> DebugStrOffsetFinalized;
SmallVector<uint32_t, 5> StrOffsets;
std::unordered_map<uint64_t, uint64_t> ProcessedBaseOffsets;
bool StrOffsetSectionWasModified = false;

View File

@ -78,7 +78,7 @@ static void addStringHelper(DebugStrOffsetsWriter &StrOffstsWriter,
uint32_t NewOffset = StrWriter.addString(Str);
if (Unit.getVersion() >= 5) {
StrOffstsWriter.updateAddressMap(DIEAttrInfo.getDIEInteger().getValue(),
NewOffset);
NewOffset, Unit);
return;
}
DIEBldr.replaceValue(&Die, DIEAttrInfo.getAttribute(), DIEAttrInfo.getForm(),

View File

@ -851,7 +851,11 @@ void DebugStrOffsetsWriter::initialize(DWARFUnit &Unit) {
StrOffsetsSection.Data.data() + Contr->Base + Offset));
}
void DebugStrOffsetsWriter::updateAddressMap(uint32_t Index, uint32_t Address) {
void DebugStrOffsetsWriter::updateAddressMap(uint32_t Index, uint32_t Address,
const DWARFUnit &Unit) {
assert(DebugStrOffsetFinalized.count(Unit.getOffset()) == 0 &&
"Cannot update address map since debug_str_offsets was already "
"finalized for this CU.");
IndexToAddressMap[Index] = Address;
StrOffsetSectionWasModified = true;
}
@ -906,6 +910,8 @@ void DebugStrOffsetsWriter::finalizeSection(DWARFUnit &Unit,
}
StrOffsetSectionWasModified = false;
assert(DebugStrOffsetFinalized.insert(Unit.getOffset()).second &&
"debug_str_offsets was already finalized for this CU.");
clear();
}

View File

@ -714,7 +714,8 @@ void DWARFRewriter::updateDebugInfo() {
RangesBase = RangesSectionWriter.getSectionOffset() +
getDWARF5RngListLocListHeaderSize();
RangesSectionWriter.initSection(Unit);
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
if (!SplitCU)
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
} else if (SplitCU) {
RangesBase = LegacyRangesSectionWriter.get()->getSectionOffset();
}
@ -760,6 +761,8 @@ void DWARFRewriter::updateDebugInfo() {
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
std::string DWOName = DIEBlder.updateDWONameCompDir(
*StrOffstsWriter, *StrWriter, *CU, DwarfOutputPath, std::nullopt);
if (CU->getVersion() >= 5)
StrOffstsWriter->finalizeSection(*CU, DIEBlder);
processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
AddressWriter, DWOName, DwarfOutputPath);
}

View File

@ -0,0 +1,28 @@
; RUN: rm -rf %t
; RUN: mkdir %t
; RUN: cd %t
; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-input-lowpc-ranges-main.s \
; RUN: -split-dwarf-file=main.dwo -o main.o
; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-input-lowpc-ranges-other.s \
; RUN: -split-dwarf-file=mainOther.dwo -o other.o
; RUN: %clang %cflags main.o other.o -o main.exe
; RUN: llvm-bolt main.exe -o main.exe.bolt --update-debug-sections --cu-processing-batch-size=1
; RUN: llvm-bolt main.exe -o main-batch.exe.bolt --update-debug-sections --cu-processing-batch-size=2
; RUN: llvm-dwarfdump --show-form --verbose --debug-info main.exe.bolt >> %t/foo.txt
; RUN: cat %t/foo.txt | FileCheck -check-prefix=BOLT %s
; RUN: llvm-dwarfdump --show-form --verbose --debug-info main.exe.bolt >> %t/foo-batch.txt
; RUN: cat %t/foo-batch.txt | FileCheck -check-prefix=BOLT-BATCH %s
;; Tests that BOLT correctly handles DWO name strings with larger batch sizes.
; BOLT: DW_TAG_skeleton_unit
; BOLT: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "main.dwo.dwo")
; BOLT: DW_TAG_skeleton_unit
; BOLT: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "mainOther.dwo.dwo")
; BOLT-BATCH: DW_TAG_skeleton_unit
; BOLT-BATCH: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "main.dwo.dwo")
; BOLT-BATCH: DW_TAG_skeleton_unit
; BOLT-BATCH: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "mainOther.dwo.dwo")