Skip to content

Commit dbea538

Browse files
authored
[DWARFLinker] Support MD5 checksums in the line table (#77151)
Add support to the DWARF linkers for emitting DWARF 5 MD5 checksum in the line table.
1 parent c7e4065 commit dbea538

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

llvm/lib/DWARFLinker/DWARFStreamer.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,18 +859,16 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
859859
for (auto Include : P.IncludeDirectories)
860860
emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool);
861861

862-
bool InlineSources = any_of(P.FileNames, [](auto &File) {
863-
auto s = dwarf::toString(File.Source);
864-
return s && !**s;
865-
});
862+
bool HasChecksums = P.ContentTypes.HasMD5;
863+
bool HasInlineSources = P.ContentTypes.HasSource;
866864

867865
if (P.FileNames.empty()) {
868866
// file_name_entry_format_count (ubyte).
869867
MS->emitInt8(0);
870868
LineSectionSize += 1;
871869
} else {
872870
// file_name_entry_format_count (ubyte).
873-
MS->emitInt8(2 + (InlineSources ? 1 : 0));
871+
MS->emitInt8(2 + (HasChecksums ? 1 : 0) + (HasInlineSources ? 1 : 0));
874872
LineSectionSize += 1;
875873

876874
// file_name_entry_format (sequence of ULEB128 pairs).
@@ -880,7 +878,13 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
880878

881879
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_directory_index);
882880
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_FORM_data1);
883-
if (InlineSources) {
881+
882+
if (HasChecksums) {
883+
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_MD5);
884+
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_FORM_data16);
885+
}
886+
887+
if (HasInlineSources) {
884888
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
885889
LineSectionSize += MS->emitULEB128IntValue(StrForm);
886890
}
@@ -894,7 +898,13 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
894898
emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool);
895899
MS->emitInt8(File.DirIdx);
896900
LineSectionSize += 1;
897-
if (InlineSources)
901+
if (HasChecksums) {
902+
MS->emitBinaryData(
903+
StringRef(reinterpret_cast<const char *>(File.Checksum.data()),
904+
File.Checksum.size()));
905+
LineSectionSize += File.Checksum.size();
906+
}
907+
if (HasInlineSources)
898908
emitLineTableString(P, File.Source, DebugStrPool, DebugLineStrPool);
899909
}
900910
}

llvm/lib/DWARFLinkerParallel/DebugLineSectionEmitter.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,19 @@ class DebugLineSectionEmitter {
197197
Section.emitIntVal(0, 1);
198198
} else {
199199
// file_name_entry_format_count (ubyte).
200-
Section.emitIntVal(2, 1);
200+
Section.emitIntVal(2 + (P.ContentTypes.HasMD5 ? 1 : 0), 1);
201201

202202
// file_name_entry_format (sequence of ULEB128 pairs).
203203
encodeULEB128(dwarf::DW_LNCT_path, Section.OS);
204204
encodeULEB128(P.FileNames[0].Name.getForm(), Section.OS);
205205

206206
encodeULEB128(dwarf::DW_LNCT_directory_index, Section.OS);
207207
encodeULEB128(dwarf::DW_FORM_data1, Section.OS);
208+
209+
if (P.ContentTypes.HasMD5) {
210+
encodeULEB128(dwarf::DW_LNCT_MD5, Section.OS);
211+
encodeULEB128(dwarf::DW_FORM_data16, Section.OS);
212+
}
208213
}
209214

210215
// file_names_count (ULEB128).
@@ -222,6 +227,12 @@ class DebugLineSectionEmitter {
222227
// source file.
223228
Section.emitString(File.Name.getForm(), *FileNameStr);
224229
Section.emitIntVal(File.DirIdx, 1);
230+
231+
if (P.ContentTypes.HasMD5) {
232+
Section.emitBinaryData(
233+
StringRef(reinterpret_cast<const char *>(File.Checksum.data()),
234+
File.Checksum.size()));
235+
}
225236
}
226237
}
227238

llvm/lib/DWARFLinkerParallel/OutputSections.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ void SectionDescriptor::emitIntVal(uint64_t Val, unsigned Size) {
227227
}
228228
}
229229

230+
void SectionDescriptor::emitBinaryData(llvm::StringRef Data) {
231+
OS.write(Data.data(), Data.size());
232+
}
233+
230234
void SectionDescriptor::apply(uint64_t PatchOffset, dwarf::Form AttrForm,
231235
uint64_t Val) {
232236
switch (AttrForm) {

llvm/lib/DWARFLinkerParallel/OutputSections.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ struct SectionDescriptor {
283283

284284
void emitString(dwarf::Form StringForm, const char *StringVal);
285285

286+
void emitBinaryData(llvm::StringRef Data);
287+
286288
/// Emit specified inplace string value into the current section contents.
287289
void emitInplaceString(StringRef String) {
288290
OS << GlobalData.translateString(String);

llvm/test/tools/dsymutil/ARM/dwarf5-dwarf4-combination-macho.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CHECK-NEXT: DW_AT_low_pc [DW_FORM_addrx] (indexed (00000000) address = 0x[[
7373
CHECK: DW_AT_linkage_name [DW_FORM_strx] (indexed (00000005) string = "_Z4foo2i")
7474
CHECK: DW_AT_name [DW_FORM_strx] (indexed (00000006) string = "foo2")
7575
CHECK: DW_TAG_formal_parameter
76-
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOCLIST_OFFSET:[0-9a-f]+]]:
76+
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOCLIST_OFFSET:[0-9a-f]+]]:
7777
CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START:]], 0x[[#%.16x,LOCLIST_PAIR_END:]]): [[LOCLIST_EXPR:.*]]
7878
CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START2:]], 0x[[#%.16x,LOCLIST_PAIR_END2:]]): [[LOCLIST_EXPR2:.*]])
7979
CHECK: DW_AT_name [DW_FORM_strx] (indexed (00000007) string = "a")
@@ -93,7 +93,7 @@ CHECK-NEXT: DW_AT_low_pc [DW_FORM_addr] (0x[[#%.16x,LOC_LOWPC
9393
CHECK: DW_AT_linkage_name [DW_FORM_strp] ( .debug_str[0x000000e6] = "_Z3bari")
9494
CHECK: DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000ee] = "bar")
9595
CHECK: DW_TAG_formal_parameter
96-
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOC_OFFSET:[0-9a-f]+]]:
96+
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOC_OFFSET:[0-9a-f]+]]:
9797
CHECK-NEXT: [0x[[#%.16x,LOC_PAIR_START:]], 0x[[#%.16x,LOC_PAIR_END:]]): [[LOC_EXPR:.*]]
9898
CHECK-NEXT: [0x[[#%.16x,LOC_PAIR_START2:]], 0x[[#%.16x,LOC_PAIR_END2:]]): [[LOC_EXPR2:.*]])
9999
CHECK: DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000f2] = "x")
@@ -105,7 +105,7 @@ CHECK-NEXT: (0x[[#sub(LOC_PAIR_START2,LOC_LOWPC)]], 0x[[#sub(LOC_PAIR
105105

106106
CHECK: .debug_loclists contents:
107107
CHECK-NEXT: 0x00000000: locations list header: length = 0x00000018, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
108-
CHECK-NEXT: 0x[[LOCLIST_OFFSET]]:
108+
CHECK-NEXT: 0x[[LOCLIST_OFFSET]]:
109109
CHECK-NEXT: DW_LLE_base_addressx (0x0000000000000000)
110110
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END,LOCLIST_LOWPC)]])
111111
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START2,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END2,LOCLIST_LOWPC)]])
@@ -114,12 +114,12 @@ CHECK-NEXT: DW_LLE_end_of_list ()
114114
CHECK: .debug_line contents:
115115
CHECK-NEXT: debug_line[0x00000000]
116116
CHECK-NEXT: Line table prologue:
117-
CHECK-NEXT: total_length: 0x00000048
117+
CHECK-NEXT: total_length: 0x0000005a
118118
CHECK-NEXT: format: DWARF32
119119
CHECK-NEXT: version: 5
120120
CHECK-NEXT: address_size: 8
121121
CHECK-NEXT: seg_select_size: 0
122-
CHECK-NEXT: prologue_length: 0x00000025
122+
CHECK-NEXT: prologue_length: 0x00000037
123123
CHECK-NEXT: min_inst_length: 1
124124
CHECK-NEXT: max_ops_per_inst: 1
125125
CHECK-NEXT: default_is_stmt: 1
@@ -143,7 +143,7 @@ CHECK-NEXT: file_names[ 0]:
143143
CHECK-NEXT: name: .debug_line_str[0x00000029] = "a.cpp"
144144
CHECK-NEXT: dir_index: 0
145145

146-
CHECK: debug_line[0x0000004c]
146+
CHECK: debug_line[0x0000005e]
147147
CHECK-NEXT: Line table prologue:
148148
CHECK-NEXT: total_length: 0x0000003b
149149
CHECK-NEXT: format: DWARF32

llvm/test/tools/dsymutil/ARM/dwarf5-macho.test

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ CHECK-NEXT: DW_AT_addr_base [DW_FORM_sec_offset] (0x00000008)
4949
CHECK: DW_TAG_subprogram
5050
CHECK-NEXT: DW_AT_low_pc [DW_FORM_addrx] (indexed (00000000) address = 0x[[#%.16x,LOCLIST_LOWPC:]])
5151
CHECK: DW_TAG_formal_parameter
52-
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOC_OFFSET:[0-9a-f]+]]:
52+
CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset] (0x[[LOC_OFFSET:[0-9a-f]+]]:
5353
CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START:]], 0x[[#%.16x,LOCLIST_PAIR_END:]]): [[LOCLIST_EXPR:.*]]
5454
CHECK-NEXT: [0x[[#%.16x,LOCLIST_PAIR_START2:]], 0x[[#%.16x,LOCLIST_PAIR_END2:]]): [[LOCLIST_EXPR2:.*]])
5555

5656
CHECK: .debug_loclists contents:
5757
CHECK-NEXT: 0x00000000: locations list header: length = 0x00000018, format = DWARF32, version = 0x0005, addr_size = 0x08, seg_size = 0x00, offset_entry_count = 0x00000000
58-
CHECK-NEXT: 0x[[LOC_OFFSET]]:
58+
CHECK-NEXT: 0x[[LOC_OFFSET]]:
5959
CHECK-NEXT: DW_LLE_base_addressx (0x0000000000000000)
6060
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END,LOCLIST_LOWPC)]])
6161
CHECK-NEXT: DW_LLE_offset_pair (0x[[#sub(LOCLIST_PAIR_START2,LOCLIST_LOWPC)]], 0x[[#sub(LOCLIST_PAIR_END2,LOCLIST_LOWPC)]])
@@ -64,12 +64,12 @@ CHECK-NEXT: DW_LLE_end_of_list ()
6464
CHECK: .debug_line contents:
6565
CHECK-NEXT: debug_line[0x00000000]
6666
CHECK-NEXT: Line table prologue:
67-
CHECK-NEXT: total_length: 0x00000048
67+
CHECK-NEXT: total_length: 0x0000005a
6868
CHECK-NEXT: format: DWARF32
6969
CHECK-NEXT: version: 5
7070
CHECK-NEXT: address_size: 8
7171
CHECK-NEXT: seg_select_size: 0
72-
CHECK-NEXT: prologue_length: 0x00000025
72+
CHECK-NEXT: prologue_length: 0x00000037
7373
CHECK-NEXT: min_inst_length: 1
7474
CHECK-NEXT: max_ops_per_inst: 1
7575
CHECK-NEXT: default_is_stmt: 1
@@ -92,6 +92,7 @@ CHECK-NEXT: include_directories[ 0] = .debug_line_str[0x00000000] = "/Users/sh
9292
CHECK-NEXT: file_names[ 0]:
9393
CHECK-NEXT: name: .debug_line_str[0x00000029] = "a.cpp"
9494
CHECK-NEXT: dir_index: 0
95+
CHECK-NEXT: md5_checksum: 2675ab7ce3623b564cfd8a2906a462e5
9596

9697

9798
CHECK: .debug_str contents:

0 commit comments

Comments
 (0)