Skip to content

[dsymutil] Add support for inline DWARF source files. #77016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions llvm/include/llvm/CodeGen/NonRelocatableStringpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NonRelocatableStringpool {
bool PutEmptyString = false)
: Translator(Translator) {
if (PutEmptyString)
EmptyString = getEntry("");
getEntry("");
}

DwarfStringPoolEntryRef getEntry(StringRef S);
Expand All @@ -59,7 +59,6 @@ class NonRelocatableStringpool {
MapTy Strings;
uint64_t CurrentEndOffset = 0;
unsigned NumEntries = 0;
DwarfStringPoolEntryRef EmptyString;
std::function<StringRef(StringRef Input)> Translator;
};

Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/CodeGen/NonRelocatableStringpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
namespace llvm {

DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
if (S.empty() && !Strings.empty())
return EmptyString;

if (Translator)
S = Translator(S);
auto I = Strings.insert({S, DwarfStringPoolEntry()});
Expand Down
16 changes: 14 additions & 2 deletions llvm/lib/DWARFLinker/DWARFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,21 +859,31 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
for (auto Include : P.IncludeDirectories)
emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool);

bool InlineSources = any_of(P.FileNames, [](auto &File) {
auto s = dwarf::toString(File.Source);
return s && !**s;
});

if (P.FileNames.empty()) {
// file_name_entry_format_count (ubyte).
MS->emitInt8(0);
LineSectionSize += 1;
} else {
// file_name_entry_format_count (ubyte).
MS->emitInt8(2);
MS->emitInt8(2 + (InlineSources ? 1 : 0));
LineSectionSize += 1;

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

LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_directory_index);
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_FORM_data1);
if (InlineSources) {
LineSectionSize += MS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
LineSectionSize += MS->emitULEB128IntValue(StrForm);
}
}

// file_names_count (ULEB128).
Expand All @@ -884,6 +894,8 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool);
MS->emitInt8(File.DirIdx);
LineSectionSize += 1;
if (InlineSources)
emitLineTableString(P, File.Source, DebugStrPool, DebugLineStrPool);
}
}

Expand Down
20 changes: 20 additions & 0 deletions llvm/test/tools/dsymutil/ARM/inline-source.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RUN: rm -rf %t
# RUN: mkdir -p %t
# RUN: llc -filetype=obj -mtriple arm64-apple-darwin %p/../Inputs/inline.ll -o %t/inline.o
# RUN: dsymutil -f -oso-prepend-path=%t -y %s -o - | llvm-dwarfdump -debug-line - | FileCheck %s

# Test inline source files.

---
triple: 'arm64-apple-darwin'
objects:
- filename: inline.o
symbols:
- { sym: _f, objAddr: 0x0, binAddr: 0x1000, size: 0x12 }
...

# CHECK: .debug_line contents:
# CHECK: file_names[ 1]:
# CHECK-NEXT: name: "inlined.c"
# CHECK-NEXT: dir_index: 1
# CHECK-NEXT: source: "{{.*}}This is inline source code.
34 changes: 34 additions & 0 deletions llvm/test/tools/dsymutil/Inputs/inline.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; ModuleID = '/tmp/t.c'
source_filename = "/tmp/t.c"
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"

; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
define void @f() #0 !dbg !9 {
entry:
ret void, !dbg !14
}

attributes #0 = { noinline nounwind optnone ssp uwtable(sync) }

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
!llvm.ident = !{!8}

!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: "/")
!1 = !DIFile(filename: "/INLINE/inlined.c", directory: "/Volumes/Data/llvm-project", checksumkind: CSK_MD5, checksum: "3183154a5cb31debe9a8e27ca500bc3c")
!2 = !{i32 7, !"Dwarf Version", i32 5}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !{i32 1, !"wchar_size", i32 4}
!5 = !{i32 8, !"PIC Level", i32 2}
!6 = !{i32 7, !"uwtable", i32 1}
!7 = !{i32 7, !"frame-pointer", i32 1}
!8 = !{!"clang version 18.0.0git ([email protected]:llvm/llvm-project.git 29ee66f4a0967e43a035f147c960743c7b640f2f)"}
!9 = distinct !DISubprogram(name: "f", scope: !10, file: !10, line: 2, type: !11, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !0)
!10 = !DIFile(filename: "/INLINE/inlined.c", directory: "", source: "void stop();
void f() {
// This is inline source code.
}
")
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!14 = !DILocation(line: 4, column: 1, scope: !9)