Skip to content

Commit 9f71a6b

Browse files
authored
[DWARFLinker] Preserve DWARF discriminators while linking (llvm#96124)
DWARF-4 introduced DWARF discriminators but both, the classic and the parallel DWARF linkers discarded them so far. After applying this patch dsymutil, which uses the DWARF linkers, correctly preserves discriminator information.
1 parent 225d8fc commit 9f71a6b

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ void DwarfStreamer::emitLineTableRows(
10601060
unsigned FileNum = 1;
10611061
unsigned LastLine = 1;
10621062
unsigned Column = 0;
1063+
unsigned Discriminator = 0;
10631064
unsigned IsStatement = 1;
10641065
unsigned Isa = 0;
10651066
uint64_t Address = -1ULL;
@@ -1098,9 +1099,17 @@ void DwarfStreamer::emitLineTableRows(
10981099
MS->emitULEB128IntValue(Column);
10991100
LineSectionSize += 1 + getULEB128Size(Column);
11001101
}
1101-
1102-
// FIXME: We should handle the discriminator here, but dsymutil doesn't
1103-
// consider it, thus ignore it for now.
1102+
if (Discriminator != Row.Discriminator &&
1103+
MS->getContext().getDwarfVersion() >= 4) {
1104+
Discriminator = Row.Discriminator;
1105+
unsigned Size = getULEB128Size(Discriminator);
1106+
MS->emitIntValue(dwarf::DW_LNS_extended_op, 1);
1107+
MS->emitULEB128IntValue(Size + 1);
1108+
MS->emitIntValue(dwarf::DW_LNE_set_discriminator, 1);
1109+
MS->emitULEB128IntValue(Discriminator);
1110+
LineSectionSize += /* extended op */ 1 + getULEB128Size(Size + 1) +
1111+
/* discriminator */ 1 + Size;
1112+
}
11041113

11051114
if (Isa != Row.Isa) {
11061115
Isa = Row.Isa;
@@ -1156,7 +1165,7 @@ void DwarfStreamer::emitLineTableRows(
11561165
EncodingBuffer.resize(0);
11571166
Address = -1ULL;
11581167
LastLine = FileNum = IsStatement = 1;
1159-
RowsSinceLastSequence = Column = Isa = 0;
1168+
RowsSinceLastSequence = Column = Discriminator = Isa = 0;
11601169
}
11611170
}
11621171

llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class DebugLineSectionEmitter {
315315
unsigned FileNum = 1;
316316
unsigned LastLine = 1;
317317
unsigned Column = 0;
318+
unsigned Discriminator = 0;
318319
unsigned IsStatement = 1;
319320
unsigned Isa = 0;
320321
uint64_t Address = -1ULL;
@@ -350,9 +351,14 @@ class DebugLineSectionEmitter {
350351
Section.emitIntVal(dwarf::DW_LNS_set_column, 1);
351352
encodeULEB128(Column, Section.OS);
352353
}
353-
354-
// FIXME: We should handle the discriminator here, but dsymutil doesn't
355-
// consider it, thus ignore it for now.
354+
if (Discriminator != Row.Discriminator && MC->getDwarfVersion() >= 4) {
355+
Discriminator = Row.Discriminator;
356+
unsigned Size = getULEB128Size(Discriminator);
357+
Section.emitIntVal(dwarf::DW_LNS_extended_op, 1);
358+
encodeULEB128(Size + 1, Section.OS);
359+
Section.emitIntVal(dwarf::DW_LNE_set_discriminator, 1);
360+
encodeULEB128(Discriminator, Section.OS);
361+
}
356362

357363
if (Isa != Row.Isa) {
358364
Isa = Row.Isa;
@@ -397,7 +403,7 @@ class DebugLineSectionEmitter {
397403
EncodingBuffer.resize(0);
398404
Address = -1ULL;
399405
LastLine = FileNum = IsStatement = 1;
400-
RowsSinceLastSequence = Column = Isa = 0;
406+
RowsSinceLastSequence = Column = Discriminator = Isa = 0;
401407
}
402408
}
403409

Binary file not shown.
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; The input at Inputs/discriminator.arm64.dylib[.o] produced by compiling
2+
;
3+
; int foo(int i, int a, int b) {
4+
; return i ? a : a * b;
5+
; }
6+
;
7+
; with -g -fdebug-info-for-profiling -O2.
8+
9+
RUN: dsymutil --flat --linker=classic -oso-prepend-path %p -o - \
10+
RUN: --oso-prepend-path %p/Inputs --verify-dwarf=none \
11+
RUN: %p/Inputs/discriminator.arm64.dylib | llvm-dwarfdump -debug-line - \
12+
RUN: | FileCheck %s
13+
14+
RUN: dsymutil --flat --linker=parallel -oso-prepend-path %p -o - \
15+
RUN: --oso-prepend-path %p/Inputs --verify-dwarf=none \
16+
RUN: %p/Inputs/discriminator.arm64.dylib | llvm-dwarfdump -debug-line - \
17+
RUN: | FileCheck %s
18+
19+
CHECK: Address Line Column File ISA Discriminator OpIndex Flags
20+
CHECK-NEXT: ------------------ ------ ------ ------ --- ------------- ------- -------------
21+
CHECK-NEXT: 0x0000000000003f98 2 10 0 0 0 0 is_stmt prologue_end
22+
CHECK-NEXT: 0x0000000000003fa4 2 3 0 0 6 0
23+
CHECK-NEXT: 0x0000000000003fa8 2 3 0 0 0 0 end_sequence

0 commit comments

Comments
 (0)