Skip to content

Commit 9a3e66e

Browse files
authored
[BOLT][DWARF][NFC] Fix DebugStrOffsetsWriter (llvm#100672)
Fix DebugStrOffsetsWriter so updateAddressMap can't be called after it is finalized.
1 parent 558315a commit 9a3e66e

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

bolt/include/bolt/Core/DebugData.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ class DebugStrOffsetsWriter {
475475
}
476476

477477
/// Update Str offset in .debug_str in .debug_str_offsets.
478-
void updateAddressMap(uint32_t Index, uint32_t Address);
478+
void updateAddressMap(uint32_t Index, uint32_t Address,
479+
const DWARFUnit &Unit);
479480

480481
/// Get offset for given index in original .debug_str_offsets section.
481482
uint64_t getOffset(uint32_t Index) const { return StrOffsets[Index]; }
@@ -507,6 +508,8 @@ class DebugStrOffsetsWriter {
507508
std::unique_ptr<DebugStrOffsetsBufferVector> StrOffsetsBuffer;
508509
std::unique_ptr<raw_svector_ostream> StrOffsetsStream;
509510
std::map<uint32_t, uint32_t> IndexToAddressMap;
511+
[[maybe_unused]]
512+
DenseSet<uint64_t> DebugStrOffsetFinalized;
510513
SmallVector<uint32_t, 5> StrOffsets;
511514
std::unordered_map<uint64_t, uint64_t> ProcessedBaseOffsets;
512515
bool StrOffsetSectionWasModified = false;

bolt/lib/Core/DIEBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void addStringHelper(DebugStrOffsetsWriter &StrOffstsWriter,
7878
uint32_t NewOffset = StrWriter.addString(Str);
7979
if (Unit.getVersion() >= 5) {
8080
StrOffstsWriter.updateAddressMap(DIEAttrInfo.getDIEInteger().getValue(),
81-
NewOffset);
81+
NewOffset, Unit);
8282
return;
8383
}
8484
DIEBldr.replaceValue(&Die, DIEAttrInfo.getAttribute(), DIEAttrInfo.getForm(),

bolt/lib/Core/DebugData.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,11 @@ void DebugStrOffsetsWriter::initialize(DWARFUnit &Unit) {
851851
StrOffsetsSection.Data.data() + Contr->Base + Offset));
852852
}
853853

854-
void DebugStrOffsetsWriter::updateAddressMap(uint32_t Index, uint32_t Address) {
854+
void DebugStrOffsetsWriter::updateAddressMap(uint32_t Index, uint32_t Address,
855+
const DWARFUnit &Unit) {
856+
assert(DebugStrOffsetFinalized.count(Unit.getOffset()) == 0 &&
857+
"Cannot update address map since debug_str_offsets was already "
858+
"finalized for this CU.");
855859
IndexToAddressMap[Index] = Address;
856860
StrOffsetSectionWasModified = true;
857861
}
@@ -906,6 +910,8 @@ void DebugStrOffsetsWriter::finalizeSection(DWARFUnit &Unit,
906910
}
907911

908912
StrOffsetSectionWasModified = false;
913+
assert(DebugStrOffsetFinalized.insert(Unit.getOffset()).second &&
914+
"debug_str_offsets was already finalized for this CU.");
909915
clear();
910916
}
911917

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ void DWARFRewriter::updateDebugInfo() {
714714
RangesBase = RangesSectionWriter.getSectionOffset() +
715715
getDWARF5RngListLocListHeaderSize();
716716
RangesSectionWriter.initSection(Unit);
717-
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
717+
if (!SplitCU)
718+
StrOffstsWriter->finalizeSection(Unit, DIEBlder);
718719
} else if (SplitCU) {
719720
RangesBase = LegacyRangesSectionWriter.get()->getSectionOffset();
720721
}
@@ -760,6 +761,8 @@ void DWARFRewriter::updateDebugInfo() {
760761
: std::optional<std::string>(opts::DwarfOutputPath.c_str());
761762
std::string DWOName = DIEBlder.updateDWONameCompDir(
762763
*StrOffstsWriter, *StrWriter, *CU, DwarfOutputPath, std::nullopt);
764+
if (CU->getVersion() >= 5)
765+
StrOffstsWriter->finalizeSection(*CU, DIEBlder);
763766
processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
764767
AddressWriter, DWOName, DwarfOutputPath);
765768
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: rm -rf %t
2+
; RUN: mkdir %t
3+
; RUN: cd %t
4+
; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-input-lowpc-ranges-main.s \
5+
; RUN: -split-dwarf-file=main.dwo -o main.o
6+
; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-input-lowpc-ranges-other.s \
7+
; RUN: -split-dwarf-file=mainOther.dwo -o other.o
8+
; RUN: %clang %cflags main.o other.o -o main.exe
9+
; RUN: llvm-bolt main.exe -o main.exe.bolt --update-debug-sections --cu-processing-batch-size=1
10+
; RUN: llvm-bolt main.exe -o main-batch.exe.bolt --update-debug-sections --cu-processing-batch-size=2
11+
; RUN: llvm-dwarfdump --show-form --verbose --debug-info main.exe.bolt >> %t/foo.txt
12+
; RUN: cat %t/foo.txt | FileCheck -check-prefix=BOLT %s
13+
; RUN: llvm-dwarfdump --show-form --verbose --debug-info main.exe.bolt >> %t/foo-batch.txt
14+
; RUN: cat %t/foo-batch.txt | FileCheck -check-prefix=BOLT-BATCH %s
15+
16+
;; Tests that BOLT correctly handles DWO name strings with larger batch sizes.
17+
18+
; BOLT: DW_TAG_skeleton_unit
19+
; BOLT: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "main.dwo.dwo")
20+
21+
; BOLT: DW_TAG_skeleton_unit
22+
; BOLT: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "mainOther.dwo.dwo")
23+
24+
; BOLT-BATCH: DW_TAG_skeleton_unit
25+
; BOLT-BATCH: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "main.dwo.dwo")
26+
27+
; BOLT-BATCH: DW_TAG_skeleton_unit
28+
; BOLT-BATCH: DW_AT_dwo_name [DW_FORM_strx1] (indexed (00000001) string = "mainOther.dwo.dwo")

0 commit comments

Comments
 (0)