Hi,

I added some fixes to binutils gas to make sure that it always
generates DWARF5 style .debug_rnglists and .debug_line with
.debug_line_str which are more efficient than the older .debug_ranges
and .debug_line data.

There is also a pending patch to make it possible to always pass
--gdwarf-N to gas even if gcc generates its own .debug_info and
.debug_line sections.
https://sourceware.org/pipermail/binutils/2020-September/113220.html

Ideally we have a configure check to make sure that as accepts
--gdwarf-N for N={2,3,4,5} and that when the assembly file already
contains a .debug_info or .debug_line section as doesn't error out. I
haven't written that yet. It requires some way to create target
specific assembler (could be NOPs) to check the .debug_line creation
by as.

Then we can add --gdwarf-N to ASM_SPEC when gcc generates debuginfo
for DWARF version N. The below has part of that, but always uses
--gdwarf-5, and adds it even for targets that don't use DWARF, which
is obviously wrong. But I don't fully understand if I should express
this in the spec or just depend on some configure check conditionals.

To compare the .debug section sizes generated between the current gcc
master default (DWARF4) on x86_64 and using DWARF5 by default I am
using binutils master plus the above unapproved patch plus the
attached patch to gcc to enable DWARF5 by default, pass --gdwarf-5 to
as and adding Jakub's patch to keep the static member variables in C++
classes. It keep locview enabled for now to make the comparison more
fair.

For libstdc++.so we get a 21M file with current master and a 17M when
making DWARF5 the default. The debug sections look as follows:

master lib64/libstdc++.so.6.0.29:
[31] .debug_aranges  PROGBITS 0000000000000000 001da430 00015000 0    0 0  1
[32] .debug_info     PROGBITS 0000000000000000 001ef430 0079f7c3 0    0 0  1
[33] .debug_abbrev   PROGBITS 0000000000000000 0098ebf3 00054e1a 0    0 0  1
[34] .debug_line     PROGBITS 0000000000000000 009e3a0d 001779c0 0    0 0  1
[35] .debug_str      PROGBITS 0000000000000000 00b5b3cd 0012fbb6 1 MS 0 0  1
[36] .debug_loc      PROGBITS 0000000000000000 00c8af83 005c05b0 0    0 0  1
[37] .debug_ranges   PROGBITS 0000000000000000 0124b533 001b1140 0    0 0  1

dwarf5 lib64/libstdc++.so.6.0.29:
[32] .debug_aranges  PROGBITS 0000000000000000 001d9350 00015000 0    0 0  1
[33] .debug_info     PROGBITS 0000000000000000 001ee350 0078b3d1 0    0 0  1
[34] .debug_abbrev   PROGBITS 0000000000000000 00979721 00055972 0    0 0  1
[35] .debug_line     PROGBITS 0000000000000000 009cf093 0015c20b 0    0 0  1
[36] .debug_str      PROGBITS 0000000000000000 00b2b29e 00130b55 1 MS 0 0  1
[37] .debug_loclists PROGBITS 0000000000000000 00c5bdf3 00299d88 0    0 0  1
[38] .debug_rnglists PROGBITS 0000000000000000 00ef5b7b 0009357e 0    0 0  1
[39] .debug_line_str PROGBITS 0000000000000000 00f890f9 00001685 1 MS 0 0  1

master:
.debug_aranges  00015000 0.08M
.debug_info     0079f7c3 7.62M
.debug_abbrev   00054e1a 0.33M
.debug_line     001779c0 1.47M
.debug_str      0012fbb6 1.19M
.debug_loc      005c05b0 5.75M
.debug_ranges   001b1140 1.69M
                        18.13M

dwarf5:
.debug_aranges  00015000 0.08M
.debug_info     0078b3d1 7.54M
.debug_abbrev   00055972 0.33M
.debug_line     0015c20b 1.36M
.debug_str      00130b55 1.19M
.debug_loclists 00299d88 2.60M
.debug_rnglists 0009357e 0.58M
.debug_line_str 00001685 0.01M
                        13.69M

So the total size difference is 4.4MB with the DWARF5 loclists and
rngglists being much smaller than the DWARF5 locs and ranges, the
.debug_line section is slightly smaller because all directory/file
strings are now shared in the .debug_line_str. debug_info is also a
little bit smaller.

Cheers,

Mark
diff --git a/gcc/common.opt b/gcc/common.opt
index dd68c61ae1d2..755df5445905 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -3144,7 +3144,7 @@ Common Driver JoinedOrMissing Negative(gdwarf-)
 Generate debug information in default version of DWARF format.
 
 gdwarf-
-Common Driver Joined UInteger Var(dwarf_version) Init(4) Negative(gstabs)
+Common Driver Joined UInteger Var(dwarf_version) Init(5) Negative(gstabs)
 Generate debug information in DWARF v2 (or later) format.
 
 ggdb
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index bca8c856dc82..d69aa253f72b 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -9057,13 +9057,14 @@ possible.
 @opindex gdwarf
 Produce debugging information in DWARF format (if that is supported).
 The value of @var{version} may be either 2, 3, 4 or 5; the default version
-for most targets is 4.  DWARF Version 5 is only experimental.
+for most targets is 5 (with the exception of vxworks and darwin which
+default to version 2).
 
 Note that with DWARF Version 2, some ports require and always
 use some non-conflicting DWARF 3 extensions in the unwind tables.
 
 Version 4 may require GDB 7.0 and @option{-fvar-tracking-assignments}
-for maximum benefit.
+for maximum benefit. Version 5 requires GDB 8.0 or higher.
 
 GCC no longer supports DWARF Version 1, which is substantially
 different than Version 2 and later.  For historical reasons, some
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 4096c0c0d69f..f3b114b7a4d9 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -29395,6 +29395,13 @@ prune_unused_types_walk (dw_die_ref die)
 	  if (die->die_perennial_p)
 	    break;
 
+	  /* For static data members, the declaration in the class is supposed
+	     to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
+	     DWARF5.  DW_TAG_member will be marked, so mark even such
+	     DW_TAG_variables in DWARF5.  */
+	  if (dwarf_version >= 5 && class_scope_p (die->die_parent))
+	    break;
+
 	  /* premark_used_variables marks external variables --- don't mark
 	     them here.  But function-local externals are always considered
 	     used.  */
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 531f4e02dbdb..0eac2ba03a92 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1882,6 +1882,11 @@ init_spec (void)
   }
 #endif
 
+  static const char dv[] = "%{g*:%{%:debug-level-gt(0):--gdwarf-5}} ";
+  obstack_grow (&obstack, dv, sizeof (dv) - 1);
+  obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
+  asm_spec = XOBFINISH (&obstack, const char *);
+
 #if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC || \
     defined LINKER_HASH_STYLE
 # ifdef LINK_BUILDID_SPEC

Reply via email to