Skip to content

Commit 8b03e64

Browse files
molarAlexisPerry
authored andcommitted
[llvm-dwp] Fix merging of debug_str_offsets with multiple contributions (llvm#90461)
This pull request will change the merging of ``debug_str_offset`` to merge per contribution and correctly copy over each contribution header to the merged section. I have added some test data which is in dwarf5 format as this is where the section contribution header was introduced, as far as i can tell.
1 parent 47cbb26 commit 8b03e64

File tree

3 files changed

+190
-9
lines changed

3 files changed

+190
-9
lines changed

llvm/lib/DWP/DWP.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,17 @@ Expected<InfoSectionUnitHeader> parseInfoSectionUnitHeader(StringRef Info) {
415415
return Header;
416416
}
417417

418+
static void writeNewOffsetsTo(MCStreamer &Out, DataExtractor &Data,
419+
DenseMap<uint64_t, uint32_t> &OffsetRemapping,
420+
uint64_t &Offset, uint64_t &Size) {
421+
422+
while (Offset < Size) {
423+
auto OldOffset = Data.getU32(&Offset);
424+
auto NewOffset = OffsetRemapping[OldOffset];
425+
Out.emitIntValue(NewOffset, 4);
426+
}
427+
}
428+
418429
void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
419430
MCSection *StrOffsetSection,
420431
StringRef CurStrSection,
@@ -439,17 +450,30 @@ void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
439450

440451
Out.switchSection(StrOffsetSection);
441452

442-
uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version);
443453
uint64_t Offset = 0;
444454
uint64_t Size = CurStrOffsetSection.size();
445-
// FIXME: This can be caused by bad input and should be handled as such.
446-
assert(HeaderSize <= Size && "StrOffsetSection size is less than its header");
447-
// Copy the header to the output.
448-
Out.emitBytes(Data.getBytes(&Offset, HeaderSize));
449-
while (Offset < Size) {
450-
auto OldOffset = Data.getU32(&Offset);
451-
auto NewOffset = OffsetRemapping[OldOffset];
452-
Out.emitIntValue(NewOffset, 4);
455+
if (Version > 4) {
456+
while (Offset < Size) {
457+
uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version);
458+
assert(HeaderSize <= Size - Offset &&
459+
"StrOffsetSection size is less than its header");
460+
461+
uint64_t ContributionEnd = 0;
462+
uint64_t ContributionSize = 0;
463+
uint64_t HeaderLengthOffset = Offset;
464+
if (HeaderSize == 8) {
465+
ContributionSize = Data.getU32(&HeaderLengthOffset);
466+
} else if (HeaderSize == 16) {
467+
HeaderLengthOffset += 4; // skip the dwarf64 marker
468+
ContributionSize = Data.getU64(&HeaderLengthOffset);
469+
}
470+
ContributionEnd = ContributionSize + HeaderLengthOffset;
471+
Out.emitBytes(Data.getBytes(&Offset, HeaderSize));
472+
writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, ContributionEnd);
473+
}
474+
475+
} else {
476+
writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size);
453477
}
454478
}
455479

llvm/test/tools/llvm-dwp/X86/merge.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ CHECK: Index Signature INFO ABBREV
4444
CHECK-DAG: [[DWOC]] [0x00000000[[#COFF]], 0x00000000[[#AOFF]]) [0x0000[[CAOFF]], 0x0000[[AAOFF]]) [0x00000000, 0x00000011) [0x00000000, 0x00000018)
4545
CHECK-DAG: [[DWOA]] [0x00000000[[#AOFF]], 0x00000000[[#BOFF]]) [0x0000[[AAOFF]], 0x0000[[BAOFF]]) [0x00000011, 0x00000022) [0x00000018, 0x00000028)
4646
CHECK-DAG: [[DWOB]] [0x00000000[[#BOFF]], 0x00000000[[#XOFF]]) [0x0000[[BAOFF]], 0x000000c3) [0x00000022, 0x00000033) [0x00000028, 0x0000003c)
47+
48+
CHECK-LABEL: .debug_str_offsets.dwo contents:
49+
CHECK: Contribution size = 24, Format = DWARF32, Version = 4
50+
CHECK: Contribution size = 16, Format = DWARF32, Version = 4
51+
CHECK: Contribution size = 20, Format = DWARF32, Version = 4
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
RUN: rm -rf %t && split-file %s %t && cd %t
2+
RUN: llc a.ll -o a.o -filetype=obj --split-dwarf-output=a.dwo --split-dwarf-file=a.dwo
3+
RUN: llc b.ll -o b.o -filetype=obj --split-dwarf-output=b.dwo --split-dwarf-file=b.dwo
4+
RUN: llc c.ll -o c.o -filetype=obj --split-dwarf-output=c.dwo --split-dwarf-file=c.dwo
5+
RUN: llvm-dwp a.dwo b.dwo -o ab.dwp
6+
RUN: llvm-dwp c.dwo ab.dwp -o merged.dwp
7+
RUN: llvm-dwarfdump -v merged.dwp | FileCheck --check-prefix=CHECK %s
8+
9+
10+
CHECK-LABEL: .debug_str_offsets.dwo contents:
11+
CHECK: Contribution size = 32, Format = DWARF32, Version = 5
12+
CHECK: 0x00000008: 00000000 "_Z1cv"
13+
CHECK: 0x0000000c: 00000006 "c"
14+
CHECK: 0x00000010: 00000008 "int"
15+
CHECK: 0x00000014: 0000000c "baz"
16+
CHECK: 0x00000018: 00000010 ""
17+
CHECK: 0x0000001c: 00000011 "c.cpp"
18+
CHECK: 0x00000020: 00000017 "c.dwo"
19+
CHECK: Contribution size = 24, Format = DWARF32, Version = 5
20+
CHECK: 0x0000002c: 0000001d "a"
21+
CHECK: 0x00000030: 0000001f "foo"
22+
CHECK: 0x00000034: 00000010 ""
23+
CHECK: 0x00000038: 00000023 "a.cpp"
24+
CHECK: 0x0000003c: 00000029 "a.dwo"
25+
CHECK: Contribution size = 28, Format = DWARF32, Version = 5
26+
CHECK: 0x00000048: 0000002f "_Z1b3bar"
27+
CHECK: 0x0000004c: 00000038 "b"
28+
CHECK: 0x00000050: 0000003a "bar"
29+
CHECK: 0x00000054: 00000010 ""
30+
CHECK: 0x00000058: 0000003e "b.cpp"
31+
CHECK: 0x0000005c: 00000044 "b.dwo"
32+
;--- a.cpp
33+
struct foo { };
34+
foo a;
35+
;--- b.cpp
36+
struct bar { };
37+
void b(bar) {
38+
}
39+
;--- c.cpp
40+
typedef int baz;
41+
baz c() {
42+
}
43+
;--- gen
44+
clang --target=x86_64-linux -g3 -S -emit-llvm -gsplit-dwarf a.cpp -o -
45+
echo '#--- b.ll'
46+
clang --target=x86_64-linux -g3 -S -emit-llvm -gsplit-dwarf b.cpp -o -
47+
echo '#--- c.ll'
48+
clang --target=x86_64-linux -g3 -S -emit-llvm -gsplit-dwarf c.cpp -o -
49+
;--- a.ll
50+
; ModuleID = 'a.cpp'
51+
source_filename = "a.cpp"
52+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
53+
target triple = "x86_64-unknown-linux"
54+
55+
%struct.foo = type { i8 }
56+
57+
@a = dso_local global %struct.foo zeroinitializer, align 1, !dbg !0
58+
59+
!llvm.dbg.cu = !{!2}
60+
!llvm.module.flags = !{!7, !8, !9, !10, !11, !12, !13}
61+
62+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
63+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 2, type: !5, isLocal: false, isDefinition: true)
64+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, isOptimized: false, runtimeVersion: 0, splitDebugFilename: "a.dwo", emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: GNU)
65+
!3 = !DIFile(filename: "a.cpp", directory: "/proc/self/cwd", checksumkind: CSK_MD5, checksum: "394299a94a96cb48c0c9c95d7baf01f5")
66+
!4 = !{!0}
67+
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "foo", file: !3, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !6, identifier: "_ZTS3foo")
68+
!6 = !{}
69+
!7 = !{i32 7, !"Dwarf Version", i32 5}
70+
!8 = !{i32 2, !"Debug Info Version", i32 3}
71+
!9 = !{i32 1, !"wchar_size", i32 4}
72+
!10 = !{i32 8, !"PIC Level", i32 2}
73+
!11 = !{i32 7, !"PIE Level", i32 2}
74+
!12 = !{i32 7, !"uwtable", i32 2}
75+
!13 = !{i32 7, !"frame-pointer", i32 2}
76+
#--- b.ll
77+
; ModuleID = 'b.cpp'
78+
source_filename = "b.cpp"
79+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
80+
target triple = "x86_64-unknown-linux"
81+
82+
%struct.bar = type { i8 }
83+
84+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
85+
define dso_local void @_Z1b3bar() #0 !dbg !9 {
86+
%1 = alloca %struct.bar, align 1
87+
call void @llvm.dbg.declare(metadata ptr %1, metadata !14, metadata !DIExpression()), !dbg !15
88+
ret void, !dbg !16
89+
}
90+
91+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
92+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
93+
94+
attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
95+
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
96+
97+
!llvm.dbg.cu = !{!0}
98+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
99+
100+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: false, runtimeVersion: 0, splitDebugFilename: "b.dwo", emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: GNU)
101+
!1 = !DIFile(filename: "b.cpp", directory: "/proc/self/cwd", checksumkind: CSK_MD5, checksum: "8195382ac12baa5edfe47e2e4725f4a7")
102+
!2 = !{i32 7, !"Dwarf Version", i32 5}
103+
!3 = !{i32 2, !"Debug Info Version", i32 3}
104+
!4 = !{i32 1, !"wchar_size", i32 4}
105+
!5 = !{i32 8, !"PIC Level", i32 2}
106+
!6 = !{i32 7, !"PIE Level", i32 2}
107+
!7 = !{i32 7, !"uwtable", i32 2}
108+
!8 = !{i32 7, !"frame-pointer", i32 2}
109+
!9 = distinct !DISubprogram(name: "b", linkageName: "_Z1b3bar", scope: !1, file: !1, line: 2, type: !10, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !13)
110+
!10 = !DISubroutineType(types: !11)
111+
!11 = !{null, !12}
112+
!12 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "bar", file: !1, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !13, identifier: "_ZTS3bar")
113+
!13 = !{}
114+
!14 = !DILocalVariable(arg: 1, scope: !9, file: !1, line: 2, type: !12)
115+
!15 = !DILocation(line: 2, column: 13, scope: !9)
116+
!16 = !DILocation(line: 3, column: 3, scope: !9)
117+
#--- c.ll
118+
; ModuleID = 'c.cpp'
119+
source_filename = "c.cpp"
120+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
121+
target triple = "x86_64-unknown-linux"
122+
123+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
124+
define dso_local noundef i32 @_Z1cv() #0 !dbg !9 {
125+
call void @llvm.trap(), !dbg !14
126+
unreachable, !dbg !14
127+
}
128+
129+
; Function Attrs: cold noreturn nounwind memory(inaccessiblemem: write)
130+
declare void @llvm.trap() #1
131+
132+
attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
133+
attributes #1 = { cold noreturn nounwind memory(inaccessiblemem: write) }
134+
135+
!llvm.dbg.cu = !{!0}
136+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
137+
138+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: false, runtimeVersion: 0, splitDebugFilename: "c.dwo", emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: GNU)
139+
!1 = !DIFile(filename: "c.cpp", directory: "/proc/self/cwd", checksumkind: CSK_MD5, checksum: "e508eeb01e2e608fe8713f9132696ef5")
140+
!2 = !{i32 7, !"Dwarf Version", i32 5}
141+
!3 = !{i32 2, !"Debug Info Version", i32 3}
142+
!4 = !{i32 1, !"wchar_size", i32 4}
143+
!5 = !{i32 8, !"PIC Level", i32 2}
144+
!6 = !{i32 7, !"PIE Level", i32 2}
145+
!7 = !{i32 7, !"uwtable", i32 2}
146+
!8 = !{i32 7, !"frame-pointer", i32 2}
147+
!9 = distinct !DISubprogram(name: "c", linkageName: "_Z1cv", scope: !1, file: !1, line: 2, type: !10, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
148+
!10 = !DISubroutineType(types: !11)
149+
!11 = !{!12}
150+
!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "baz", file: !1, line: 1, baseType: !13)
151+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
152+
!14 = !DILocation(line: 2, column: 11, scope: !9)

0 commit comments

Comments
 (0)