Skip to content

Commit 786cf76

Browse files
[dsymutil] Add support for inline DWARF source files. (#77016)
There was a strange and seemingly unncessary empty string optimization in NonRelocatableStringPool that I had to remove in order to support empty strings in the line_str string table, without unconditionally forcing an empty string to be added to every debug_line_str table.
1 parent 665e46c commit 786cf76

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

llvm/include/llvm/CodeGen/NonRelocatableStringpool.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class NonRelocatableStringpool {
3232
bool PutEmptyString = false)
3333
: Translator(Translator) {
3434
if (PutEmptyString)
35-
EmptyString = getEntry("");
35+
getEntry("");
3636
}
3737

3838
DwarfStringPoolEntryRef getEntry(StringRef S);
@@ -59,7 +59,6 @@ class NonRelocatableStringpool {
5959
MapTy Strings;
6060
uint64_t CurrentEndOffset = 0;
6161
unsigned NumEntries = 0;
62-
DwarfStringPoolEntryRef EmptyString;
6362
std::function<StringRef(StringRef Input)> Translator;
6463
};
6564

llvm/lib/CodeGen/NonRelocatableStringpool.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
namespace llvm {
1313

1414
DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
15-
if (S.empty() && !Strings.empty())
16-
return EmptyString;
17-
1815
if (Translator)
1916
S = Translator(S);
2017
auto I = Strings.insert({S, DwarfStringPoolEntry()});

llvm/lib/DWARFLinker/DWARFStreamer.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,21 +859,31 @@ 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+
});
866+
862867
if (P.FileNames.empty()) {
863868
// file_name_entry_format_count (ubyte).
864869
MS->emitInt8(0);
865870
LineSectionSize += 1;
866871
} else {
867872
// file_name_entry_format_count (ubyte).
868-
MS->emitInt8(2);
873+
MS->emitInt8(2 + (InlineSources ? 1 : 0));
869874
LineSectionSize += 1;
870875

871876
// file_name_entry_format (sequence of ULEB128 pairs).
877+
auto StrForm = P.FileNames[0].Name.getForm();
872878
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_path);
873-
LineSectionSize += MS->emitULEB128IntValue(P.FileNames[0].Name.getForm());
879+
LineSectionSize += MS->emitULEB128IntValue(StrForm);
874880

875881
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_directory_index);
876882
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_FORM_data1);
883+
if (InlineSources) {
884+
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
885+
LineSectionSize += MS->emitULEB128IntValue(StrForm);
886+
}
877887
}
878888

879889
// file_names_count (ULEB128).
@@ -884,6 +894,8 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
884894
emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool);
885895
MS->emitInt8(File.DirIdx);
886896
LineSectionSize += 1;
897+
if (InlineSources)
898+
emitLineTableString(P, File.Source, DebugStrPool, DebugLineStrPool);
887899
}
888900
}
889901

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# RUN: rm -rf %t
2+
# RUN: mkdir -p %t
3+
# RUN: llc -filetype=obj -mtriple arm64-apple-darwin %p/../Inputs/inline.ll -o %t/inline.o
4+
# RUN: dsymutil -f -oso-prepend-path=%t -y %s -o - | llvm-dwarfdump -debug-line - | FileCheck %s
5+
6+
# Test inline source files.
7+
8+
---
9+
triple: 'arm64-apple-darwin'
10+
objects:
11+
- filename: inline.o
12+
symbols:
13+
- { sym: _f, objAddr: 0x0, binAddr: 0x1000, size: 0x12 }
14+
...
15+
16+
# CHECK: .debug_line contents:
17+
# CHECK: file_names[ 1]:
18+
# CHECK-NEXT: name: "inlined.c"
19+
# CHECK-NEXT: dir_index: 1
20+
# CHECK-NEXT: source: "{{.*}}This is inline source code.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; ModuleID = '/tmp/t.c'
2+
source_filename = "/tmp/t.c"
3+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
4+
5+
; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
6+
define void @f() #0 !dbg !9 {
7+
entry:
8+
ret void, !dbg !14
9+
}
10+
11+
attributes #0 = { noinline nounwind optnone ssp uwtable(sync) }
12+
13+
!llvm.dbg.cu = !{!0}
14+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
15+
!llvm.ident = !{!8}
16+
17+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 18.0.0git ([email protected]:llvm/llvm-project.git 29ee66f4a0967e43a035f147c960743c7b640f2f)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
18+
!1 = !DIFile(filename: "/INLINE/inlined.c", directory: "/Volumes/Data/llvm-project", checksumkind: CSK_MD5, checksum: "3183154a5cb31debe9a8e27ca500bc3c")
19+
!2 = !{i32 7, !"Dwarf Version", i32 5}
20+
!3 = !{i32 2, !"Debug Info Version", i32 3}
21+
!4 = !{i32 1, !"wchar_size", i32 4}
22+
!5 = !{i32 8, !"PIC Level", i32 2}
23+
!6 = !{i32 7, !"uwtable", i32 1}
24+
!7 = !{i32 7, !"frame-pointer", i32 1}
25+
!8 = !{!"clang version 18.0.0git ([email protected]:llvm/llvm-project.git 29ee66f4a0967e43a035f147c960743c7b640f2f)"}
26+
!9 = distinct !DISubprogram(name: "f", scope: !10, file: !10, line: 2, type: !11, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !0)
27+
!10 = !DIFile(filename: "/INLINE/inlined.c", directory: "", source: "void stop();
28+
void f() {
29+
// This is inline source code.
30+
}
31+
")
32+
!11 = !DISubroutineType(types: !12)
33+
!12 = !{null}
34+
!14 = !DILocation(line: 4, column: 1, scope: !9)

0 commit comments

Comments
 (0)