Make LTO link pick up compile-time -g

Message ID nycvar.YFH.7.76.1909091355360.5566@zhemvz.fhfr.qr
State New
Headers show
Series
  • Make LTO link pick up compile-time -g
Related show

Commit Message

Richard Biener Sept. 9, 2019, 12:02 p.m.
Currently when you compile with -g -flto and then link without
repeating -g you'll get a binary that has all early debug
but none of the late because the driver doesn't pass -g along
to the LTRANS stage.

This has always been the case and as with other options
"guessing" correctly is hard.  The following goes a very
simple way of recording debug_info_level > DINFO_LEVEL_NONE
at compile-time into lto-opts.c as -g and picking that one
up in lto-wrapper.

So if _any_ object was compiled with debug-info (of any kind)
then we'll now get -g (sic!) enabled at LTRANS time.

The patch also re-instantiates the support for -g0 at link-time,
dropping the early created debug as well.

There are a gazillion different -g options we'd need to handle
if we try to do better like if all compile-time objects had
-gdwarf5 enabled then do so at link time.  But then there's
no change to consistently carry diferent settings to the link
stage if you consider inlining and partitioning.

So this is really a very poor mans solution that also might
uncover issues with -g0 at compile-time vs. -g at link-time
if there are mixed -g0/g TUs in the LTO link.

We've run into this "issue" with openSUSE packages now built
with -flto which in turn means some objects (those linked w/o -g)
ended up without the LTRANS debug part...

Any opinions welcome.

Thanks,
Richard.

2019-09-09  Richard Biener  <rguenther@suse.de>

	* lto-opts.c (lto_write_options): Stream -g when debug is enabled.
	* lto-wrapper.c (merge_and_complain): Pick up -g.
	(append_compiler_options): Likewise.
	(run_gcc): Re-instantiate handling -g0 at link-time.

Comments

Matthias Klose Sept. 9, 2019, 1:03 p.m. | #1
On 09.09.19 14:02, Richard Biener wrote:
> So this is really a very poor mans solution that also might

> uncover issues with -g0 at compile-time vs. -g at link-time

> if there are mixed -g0/g TUs in the LTO link.


Could this be documented, at least in the man page? e.g. invoke.texi.  As a 
bonus I would very much welcome a section in the user manual about LTO building. 
  E.g. what will work, what currently doesn't work, etc.  Happy to review and 
extend such documentation, but I don't feel comfortable to write that on my own.

Matthias
Richard Biener Sept. 9, 2019, 1:51 p.m. | #2
On Mon, 9 Sep 2019, Matthias Klose wrote:

> On 09.09.19 14:02, Richard Biener wrote:

> > So this is really a very poor mans solution that also might

> > uncover issues with -g0 at compile-time vs. -g at link-time

> > if there are mixed -g0/g TUs in the LTO link.

> 

> Could this be documented, at least in the man page? e.g. invoke.texi.  As a

> bonus I would very much welcome a section in the user manual about LTO

> building.  E.g. what will work, what currently doesn't work, etc.  Happy to

> review and extend such documentation, but I don't feel comfortable to write

> that on my own.


Like the following?

Richard.

2019-09-09  Richard Biener  <rguenther@suse.de>

	* lto-opts.c (lto_write_options): Stream -g when debug is enabled.
	* lto-wrapper.c (merge_and_complain): Pick up -g.
	(append_compiler_options): Likewise.
	(run_gcc): Re-instantiate handling -g0 at link-time.
	* doc/invoke.texi (flto): Document debug info generation.

Index: gcc/lto-opts.c
===================================================================
--- gcc/lto-opts.c	(revision 275454)
+++ gcc/lto-opts.c	(working copy)
@@ -94,6 +94,10 @@ lto_write_options (void)
 				      : "-fno-pie");
     }
 
+  /* If debug info is enabled append -g.  */
+  if (debug_info_level > DINFO_LEVEL_NONE)
+    append_to_collect_gcc_options (&temporary_obstack, &first_p, "-g");
+
   /* Append options from target hook and store them to offload_lto section.  */
   if (lto_stream_offload_p)
     {
Index: gcc/lto-wrapper.c
===================================================================
--- gcc/lto-wrapper.c	(revision 275454)
+++ gcc/lto-wrapper.c	(working copy)
@@ -265,6 +265,7 @@ merge_and_complain (struct cl_decoded_op
 	case OPT_fshow_column:
 	case OPT_fcommon:
 	case OPT_fgnu_tm:
+	case OPT_g:
 	  /* Do what the old LTO code did - collect exactly one option
 	     setting per OPT code, we pick the first we encounter.
 	     ???  This doesn't make too much sense, but when it doesn't
@@ -617,6 +618,7 @@ append_compiler_options (obstack *argv_o
 	case OPT_fopenacc:
 	case OPT_fopenacc_dim_:
 	case OPT_foffload_abi_:
+	case OPT_g:
 	case OPT_O:
 	case OPT_Ofast:
 	case OPT_Og:
@@ -1399,6 +1401,10 @@ run_gcc (unsigned argc, char *argv[])
 	  linker_output_rel = !strcmp (option->arg, "rel");
 	  break;
 
+	case OPT_g:
+	  /* Recognize -g0.  */
+	  skip_debug = option->arg && !strcmp (option->arg, "0");
+	  break;
 
 	default:
 	  break;
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 275454)
+++ gcc/doc/invoke.texi	(working copy)
@@ -10335,6 +10335,14 @@ conflicting translation units.  Specific
 precedence; and for example @option{-ffp-contract=off} takes precedence
 over @option{-ffp-contract=fast}.  You can override them at link time.
 
+To enable debug info generation you need to supply @option{-g} at
+compile-time.  If any of the input files at link time were built
+with debug info generation enabled the link will enable debug info
+generation as well.  Any elaborate debug info settings
+like the dwarf level @option{-gdwarf-5} need to be explicitely repeated
+at the linker command line and mixing different settings in different
+translation units is discouraged.
+
 If LTO encounters objects with C linkage declared with incompatible
 types in separate translation units to be linked together (undefined
 behavior according to ISO C99 6.2.7), a non-fatal diagnostic may be
Matthias Klose Sept. 9, 2019, 1:59 p.m. | #3
On 09.09.19 15:51, Richard Biener wrote:
> On Mon, 9 Sep 2019, Matthias Klose wrote:

> 

>> On 09.09.19 14:02, Richard Biener wrote:

>>> So this is really a very poor mans solution that also might

>>> uncover issues with -g0 at compile-time vs. -g at link-time

>>> if there are mixed -g0/g TUs in the LTO link.

>>

>> Could this be documented, at least in the man page? e.g. invoke.texi.  As a

>> bonus I would very much welcome a section in the user manual about LTO

>> building.  E.g. what will work, what currently doesn't work, etc.  Happy to

>> review and extend such documentation, but I don't feel comfortable to write

>> that on my own.

> 

> Like the following?


> Index: gcc/doc/invoke.texi

> ===================================================================

> --- gcc/doc/invoke.texi	(revision 275454)

> +++ gcc/doc/invoke.texi	(working copy)

> @@ -10335,6 +10335,14 @@ conflicting translation units.  Specific

>   precedence; and for example @option{-ffp-contract=off} takes precedence

>   over @option{-ffp-contract=fast}.  You can override them at link time.

>   

> +To enable debug info generation you need to supply @option{-g} at

> +compile-time.  If any of the input files at link time were built

> +with debug info generation enabled the link will enable debug info

> +generation as well.  Any elaborate debug info settings

> +like the dwarf level @option{-gdwarf-5} need to be explicitely repeated

> +at the linker command line and mixing different settings in different

> +translation units is discouraged.

> +

>   If LTO encounters objects with C linkage declared with incompatible

>   types in separate translation units to be linked together (undefined

>   behavior according to ISO C99 6.2.7), a non-fatal diagnostic may be


Thanks!
Gerald Pfeifer Dec. 22, 2019, 12:30 a.m. | #4
On Mon, 9 Sep 2019, Richard Biener wrote:
> Like the following?

> 

> Richard.

> 

> 2019-09-09  Richard Biener  <rguenther@suse.de>

> 

> 	* lto-opts.c (lto_write_options): Stream -g when debug is enabled.

> 	* lto-wrapper.c (merge_and_complain): Pick up -g.

> 	(append_compiler_options): Likewise.

> 	(run_gcc): Re-instantiate handling -g0 at link-time.

> 	* doc/invoke.texi (flto): Document debug info generation.


Thank you!  I committed the minor editorial update below on top.

Gerald


2019-12-22  Gerald Pfeifer  <gerald@pfeifer.com>

	* doc/invoke.texi (-flto): Use "compile time" as a noun.

Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi	(revision 278735)
+++ doc/invoke.texi	(working copy)
@@ -10447,7 +10447,7 @@ precedence; and for example @option{-ffp-contract=
 over @option{-ffp-contract=fast}.  You can override them at link time.
 
 To enable debug info generation you need to supply @option{-g} at
-compile-time.  If any of the input files at link time were built
+compile time.  If any of the input files at link time were built
 with debug info generation enabled the link will enable debug info
 generation as well.  Any elaborate debug info settings
 like the dwarf level @option{-gdwarf-5} need to be explicitly repeated

Patch

Index: gcc/lto-opts.c
===================================================================
--- gcc/lto-opts.c	(revision 275454)
+++ gcc/lto-opts.c	(working copy)
@@ -94,6 +94,10 @@  lto_write_options (void)
 				      : "-fno-pie");
     }
 
+  /* If debug info is enabled append -g.  */
+  if (debug_info_level > DINFO_LEVEL_NONE)
+    append_to_collect_gcc_options (&temporary_obstack, &first_p, "-g");
+
   /* Append options from target hook and store them to offload_lto section.  */
   if (lto_stream_offload_p)
     {
Index: gcc/lto-wrapper.c
===================================================================
--- gcc/lto-wrapper.c	(revision 275454)
+++ gcc/lto-wrapper.c	(working copy)
@@ -265,6 +265,7 @@  merge_and_complain (struct cl_decoded_op
 	case OPT_fshow_column:
 	case OPT_fcommon:
 	case OPT_fgnu_tm:
+	case OPT_g:
 	  /* Do what the old LTO code did - collect exactly one option
 	     setting per OPT code, we pick the first we encounter.
 	     ???  This doesn't make too much sense, but when it doesn't
@@ -617,6 +618,7 @@  append_compiler_options (obstack *argv_o
 	case OPT_fopenacc:
 	case OPT_fopenacc_dim_:
 	case OPT_foffload_abi_:
+	case OPT_g:
 	case OPT_O:
 	case OPT_Ofast:
 	case OPT_Og:
@@ -1399,6 +1401,10 @@  run_gcc (unsigned argc, char *argv[])
 	  linker_output_rel = !strcmp (option->arg, "rel");
 	  break;
 
+	case OPT_g:
+	  /* Recognize -g0.  */
+	  skip_debug = option->arg && !strcmp (option->arg, "0");
+	  break;
 
 	default:
 	  break;