Skip to content

Commit 91bd9db

Browse files
committed
[DebugInfo] Allow GNU macro extension to be read
Allow the GNU .debug_macro extension to be parsed and printed by llvm-dwarfdump. In an upcoming patch support will be added for emitting that format also. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D82974
1 parent 2892ed6 commit 91bd9db

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.h

+1
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ StringRef LNStandardString(unsigned Standard);
480480
StringRef LNExtendedString(unsigned Encoding);
481481
StringRef MacinfoString(unsigned Encoding);
482482
StringRef MacroString(unsigned Encoding);
483+
StringRef GnuMacroString(unsigned Encoding);
483484
StringRef RangeListEncodingString(unsigned Encoding);
484485
StringRef LocListEncodingString(unsigned Encoding);
485486
StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);

llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class DWARFDebugMacro {
9696
MacroHeader Header;
9797
SmallVector<Entry, 4> Macros;
9898
uint64_t Offset;
99+
100+
/// Whether or not this is a .debug_macro section.
101+
bool IsDebugMacro;
99102
};
100103

101104
/// A list of all the macro entries in the debug_macinfo section.

llvm/lib/BinaryFormat/Dwarf.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,17 @@ StringRef llvm::dwarf::MacroString(unsigned Encoding) {
488488
}
489489
}
490490

491+
StringRef llvm::dwarf::GnuMacroString(unsigned Encoding) {
492+
switch (Encoding) {
493+
default:
494+
return StringRef();
495+
#define HANDLE_DW_MACRO_GNU(ID, NAME) \
496+
case DW_MACRO_GNU_##NAME: \
497+
return "DW_MACRO_GNU_" #NAME;
498+
#include "llvm/BinaryFormat/Dwarf.def"
499+
}
500+
}
501+
491502
unsigned llvm::dwarf::getMacro(StringRef MacroString) {
492503
return StringSwitch<unsigned>(MacroString)
493504
#define HANDLE_DW_MACRO(ID, NAME) .Case("DW_MACRO_" #NAME, ID)

llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void DWARFDebugMacro::dump(raw_ostream &OS) const {
4040
unsigned IndLevel = 0;
4141
for (const auto &Macros : MacroLists) {
4242
OS << format("0x%08" PRIx64 ":\n", Macros.Offset);
43-
if (Macros.Header.Version >= 5)
43+
if (Macros.IsDebugMacro)
4444
Macros.Header.dumpMacroHeader(OS);
4545
for (const Entry &E : Macros.Macros) {
4646
// There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
@@ -52,8 +52,10 @@ void DWARFDebugMacro::dump(raw_ostream &OS) const {
5252
OS << " ";
5353
IndLevel += (E.Type == DW_MACINFO_start_file);
5454
// Based on which version we are handling choose appropriate macro forms.
55-
if (Macros.Header.Version >= 5)
56-
WithColor(OS, HighlightColor::Macro).get() << MacroString(E.Type);
55+
if (Macros.IsDebugMacro)
56+
WithColor(OS, HighlightColor::Macro).get()
57+
<< (Macros.Header.Version < 5 ? GnuMacroString(E.Type)
58+
: MacroString(E.Type));
5759
else
5860
WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
5961
switch (E.Type) {
@@ -67,6 +69,9 @@ void DWARFDebugMacro::dump(raw_ostream &OS) const {
6769
// DW_MACRO_start_file == DW_MACINFO_start_file
6870
// DW_MACRO_end_file == DW_MACINFO_end_file
6971
// For readability/uniformity we are using DW_MACRO_*.
72+
//
73+
// The GNU .debug_macro extension's entries have the same encoding
74+
// as DWARF 5's DW_MACRO_* entries, so we only use the latter here.
7075
case DW_MACRO_define:
7176
case DW_MACRO_undef:
7277
case DW_MACRO_define_strp:
@@ -118,6 +123,7 @@ Error DWARFDebugMacro::parseImpl(
118123
MacroLists.emplace_back();
119124
M = &MacroLists.back();
120125
M->Offset = Offset;
126+
M->IsDebugMacro = IsMacro;
121127
if (IsMacro) {
122128
auto Err = M->Header.parseMacroHeader(Data, &Offset);
123129
if (Err)
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## This test checks that llvm-dwarfdump can dump a .debug_macro section
2+
## with the GNU extension format.
3+
4+
# RUN: llvm-mc -triple x86_64-unknown-linux -filetype=obj %s -o -| \
5+
# RUN: llvm-dwarfdump -debug-macro - | FileCheck -strict-whitespace -match-full-lines %s
6+
7+
# CHECK:.debug_macro contents:
8+
# CHECK-NEXT:0x00000000:
9+
# CHECK-NEXT:macro header: version = 0x0004, flags = 0x02, format = DWARF32, debug_line_offset = 0x00000000
10+
# CHECK-NEXT:DW_MACRO_GNU_start_file - lineno: 0 filenum: 0
11+
# CHECK-NEXT: DW_MACRO_GNU_start_file - lineno: 1 filenum: 6
12+
# CHECK-NEXT: DW_MACRO_GNU_define_indirect - lineno: 1 macro: FOO 5
13+
# CHECK-NEXT: DW_MACRO_GNU_end_file
14+
# CHECK-NEXT: DW_MACRO_GNU_undef_indirect - lineno: 8 macro: WORLD1
15+
# CHECK-NEXT: DW_MACRO_GNU_transparent_include - import offset: 0x[[OFFSET:[0-9]+]]
16+
# CHECK-NEXT:DW_MACRO_GNU_end_file
17+
18+
# CHECK:0x[[OFFSET]]:
19+
# CHECK-NEXT:macro header: version = 0x0004, flags = 0x00, format = DWARF32
20+
# CHECK-NEXT:DW_MACRO_GNU_define_indirect - lineno: 0 macro: WORLD 2
21+
22+
.section .debug_macro,"",@progbits
23+
.Lcu_macro_begin0:
24+
.short 4 # Macro information version
25+
.byte 2 # Flags: 32 bit, debug_line_offset present
26+
.long 0 # debug_line_offset
27+
.byte 3 # DW_MACRO_GNU_start_file
28+
.byte 0 # Line Number
29+
.byte 0 # File Number
30+
.byte 3 # DW_MACRO_GNU_start_file
31+
.byte 1 # Line Number
32+
.byte 6 # File Number
33+
.byte 5 # DW_MACRO_GNU_define_indirect
34+
.byte 1 # Line Number
35+
.long .Linfo_string0 # Macro String
36+
.byte 4 # DW_MACRO_GNU_end_file
37+
.byte 6 # DW_MACRO_GNU_undef_indirect
38+
.byte 8 # Line Number
39+
.long .Linfo_string1 # Macro String
40+
.byte 7 # DW_MACRO_GNU_transparent_include
41+
.long .Lmacro1 # Macro Unit Offset
42+
.byte 4 # DW_MACRO_GNU_end_file
43+
.byte 0 # End Of Macro List Mark
44+
45+
.Lmacro1:
46+
.short 4 # Macro information version
47+
.byte 0 # Flags: 32 bit
48+
.byte 5 # DW_MACRO_GNU_define_indirect
49+
.byte 0 # Line Number
50+
.long .Linfo_string2 # Macro String
51+
.byte 0 # End Of Macro List Mark
52+
53+
.section .debug_str,"MS",@progbits,1
54+
.Linfo_string0:
55+
.asciz "FOO 5"
56+
.Linfo_string1:
57+
.asciz "WORLD1"
58+
.Linfo_string2:
59+
.asciz "WORLD 2"

0 commit comments

Comments
 (0)