Skip to content

Commit fb9a851

Browse files
authored
[BOLT][DWARF] Fix handling of debug_str_offsets (#75100)
We were not setting size field of .debug_str_offsets correctly. Fixed it, and added a test.
1 parent a43641c commit fb9a851

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

bolt/include/bolt/Core/DebugData.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ class DebugStrOffsetsWriter {
459459
std::unique_ptr<raw_svector_ostream> StrOffsetsStream;
460460
std::map<uint32_t, uint32_t> IndexToAddressMap;
461461
std::unordered_map<uint64_t, uint64_t> ProcessedBaseOffsets;
462-
// Section size not including header.
463-
uint32_t CurrentSectionSize{0};
464462
bool StrOffsetSectionWasModified = false;
465463
};
466464

bolt/lib/Core/DebugData.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,10 @@ void DebugStrOffsetsWriter::finalizeSection(DWARFUnit &Unit,
889889
// Handling re-use of str-offsets section.
890890
if (RetVal == ProcessedBaseOffsets.end() || StrOffsetSectionWasModified) {
891891
// Writing out the header for each section.
892-
support::endian::write(*StrOffsetsStream, CurrentSectionSize + 4,
893-
llvm::endianness::little);
892+
support::endian::write(
893+
*StrOffsetsStream,
894+
static_cast<uint32_t>(IndexToAddressMap.size() * 4 + 4),
895+
llvm::endianness::little);
894896
support::endian::write(*StrOffsetsStream, static_cast<uint16_t>(5),
895897
llvm::endianness::little);
896898
support::endian::write(*StrOffsetsStream, static_cast<uint16_t>(0),

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,9 @@ void DWARFRewriter::updateDebugInfo() {
696696
std::optional<DWARFUnit *> SplitCU;
697697
std::optional<uint64_t> RangesBase;
698698
std::optional<uint64_t> DWOId = Unit->getDWOId();
699-
StrOffstsWriter->initialize(Unit->getStringOffsetSection(),
700-
Unit->getStringOffsetsTableContribution());
699+
if (Unit->getVersion() >= 5)
700+
StrOffstsWriter->initialize(Unit->getStringOffsetSection(),
701+
Unit->getStringOffsetsTableContribution());
701702
if (DWOId)
702703
SplitCU = BC.getDWOCU(*DWOId);
703704
DebugLocWriter *DebugLocWriter = createRangeLocList(*Unit);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# REQUIRES: system-linux
2+
3+
# RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5_main.s -o %tmain.o
4+
# RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5_helper.s -o %thelper.o
5+
# RUN: %clang %cflags -dwarf-5 %tmain.o %thelper.o -o %t.exe -Wl,-q
6+
# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections
7+
# RUN: llvm-dwarfdump --show-form --verbose --debug-str-offsets %t.exe > %t.txt
8+
# RUN: llvm-dwarfdump --show-form --verbose --debug-str-offsets %t.bolt >> %t.txt
9+
# RUN: cat %t.txt | FileCheck --check-prefix=CHECK %s
10+
11+
## This test checks we correclty re-renerate .debug_str_offsets.
12+
13+
# CHECK: .debug_str_offsets contents
14+
# CHECK-NEXT: 0x00000000: Contribution size = 52, Format = DWARF32, Version = 5
15+
# CHECK-NEXT: "clang version 15.0.0"
16+
# CHECK-NEXT: "main.cpp"
17+
# CHECK-NEXT: "/testLocListMultiple"
18+
# CHECK-NEXT: "_Z3usePiS_"
19+
# CHECK-NEXT: "use"
20+
# CHECK-NEXT: "main"
21+
# CHECK-NEXT: "int"
22+
# CHECK-NEXT: "x"
23+
# CHECK-NEXT: "y"
24+
# CHECK-NEXT: "argc"
25+
# CHECK-NEXT: "argv"
26+
# CHECK-NEXT: "char"
27+
# CHECK-NEXT: 0x00000038: Contribution size = 48, Format = DWARF32, Version = 5
28+
# CHECK-NEXT: "clang version 15.0.0)"
29+
# CHECK-NEXT: "foo.cpp"
30+
# CHECK-NEXT: "/testLocListMultiple"
31+
# CHECK-NEXT: "fooVar"
32+
# CHECK-NEXT: "int"
33+
# CHECK-NEXT: "_Z6useFooPi"
34+
# CHECK-NEXT: "useFoo"
35+
# CHECK-NEXT: "x"
36+
# CHECK-NEXT: "_Z3fooi"
37+
# CHECK-NEXT: "foo"
38+
# CHECK-NEXT: "argc"
39+
40+
## Checking post bolt
41+
# CHECK: .debug_str_offsets contents
42+
# CHECK-NEXT: 0x00000000: Contribution size = 52, Format = DWARF32, Version = 5
43+
# CHECK-NEXT: "clang version 15.0.0"
44+
# CHECK-NEXT: "main.cpp"
45+
# CHECK-NEXT: "/testLocListMultiple"
46+
# CHECK-NEXT: "_Z3usePiS_"
47+
# CHECK-NEXT: "use"
48+
# CHECK-NEXT: "main"
49+
# CHECK-NEXT: "int"
50+
# CHECK-NEXT: "x"
51+
# CHECK-NEXT: "y"
52+
# CHECK-NEXT: "argc"
53+
# CHECK-NEXT: "argv"
54+
# CHECK-NEXT: "char"
55+
# CHECK-NEXT: 0x00000038: Contribution size = 48, Format = DWARF32, Version = 5
56+
# CHECK-NEXT: "clang version 15.0.0)"
57+
# CHECK-NEXT: "foo.cpp"
58+
# CHECK-NEXT: "/testLocListMultiple"
59+
# CHECK-NEXT: "fooVar"
60+
# CHECK-NEXT: "int"
61+
# CHECK-NEXT: "_Z6useFooPi"
62+
# CHECK-NEXT: "useFoo"
63+
# CHECK-NEXT: "x"
64+
# CHECK-NEXT: "_Z3fooi"
65+
# CHECK-NEXT: "foo"
66+
# CHECK-NEXT: "argc"

0 commit comments

Comments
 (0)