Hi Guys,

  The -frecord-gcc-switches option records the gcc command line.  It
  does not however expand options like -O2 into the optimizations that
  this enables.  Thus if a user wants to know if a specific optimization
  was used when creating an object file, (or library or executable),
  they will have to reverse engineer the compilation process.  Which may
  or may not be possible.

  The attached patch is a proposal to address this problem by making
  -frecord-gcc-switches also record all the enabled options.  This does
  make object files bigger, but this cannot be helped.  The enhancement
  is not enabled by default however, instead a second command line
  option must be used.  In a possibly contentious move I chose to reuse
  the -fverbose-asm option, rather than creating a new one.  I did this
  because a) it simplifies the patch, b) we have more than enough switch
  recording options already, c) it does not conflict with the current
  use of -fverbose-asm and d) it ties in nicely with the name of the
  option.

  Tested, with no regressions on an x86_64-pc-linux-gnu target, and
  built for a variety of other targets.

  OK to apply ?

Cheers
  Nick

gcc/ChangeLog
2017-06-08  Nick Clifton  <ni...@redhat.com>

        * varasm.c (dump_enabled_to_asm_out_file): New function.  Prints
        enabled options to the asm_out_file.
        (elf_record_gcc_switches): If verbose-asm is set then also dump
        all enabled options to the asm file.
        * toplec.c (print_switch_values): Convert from static to global.
        * doc/invoke.texi (-fverbose-asm): Mention its effect on the
        -frecord-gcc-switches option.
        (-frecord-gcc-switches): Refactor the description and add details
        of how -fverbose-asm modifies its behaviour.

Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 249007)
+++ gcc/doc/invoke.texi	(working copy)
@@ -12281,10 +12281,13 @@
 who actually need to read the generated assembly code (perhaps while
 debugging the compiler itself).
 
-@option{-fno-verbose-asm}, the default, causes the
-extra information to be omitted and is useful when comparing two assembler
-files.
+This switch can also be used to modify the behaviour of the
+@option{-frecord-gcc-switches} switch, making it record extra
+information in the object file.
 
+@option{-fno-verbose-asm}, the default, causes the extra information
+to be omitted and is useful when comparing two assembler files.
+
 The added comments include:
 
 @itemize @bullet
@@ -12370,17 +12373,26 @@
 
 @item -frecord-gcc-switches
 @opindex frecord-gcc-switches
-This switch causes the command line used to invoke the
-compiler to be recorded into the object file that is being created.
-This switch is only implemented on some targets and the exact format
-of the recording is target and binary file format dependent, but it
-usually takes the form of a section containing ASCII text.  This
-switch is related to the @option{-fverbose-asm} switch, but that
-switch only records information in the assembler output file as
-comments, so it never reaches the object file.
-See also @option{-grecord-gcc-switches} for another
-way of storing compiler options into the object file.
+This switch causes the command line used to invoke the compiler to be
+recorded into the object file that is being created.  This switch is
+only implemented on some targets and the exact format of the recording
+is target and binary file format dependent, but it usually takes the
+form of a section containing ASCII text.
 
+The related switch @option{-fverbose-asm} switch performs a similar
+task but it only records the information in the assembler output file
+as comments, so it never reaches the object file.
+
+If both @option{-fverbose-asm} and @option{-frecord-gcc-switches} are
+enabled together then @option{-frecord-gcc-switches} will record all
+enabled switches, not just those specified on the command line.  Thus
+if the command line includes @option{-O2} then all optimizations
+enabled by that switch will be recorded in the object file, along with
+the presence of @option{-O2} itself.
+
+See also @option{-grecord-gcc-switches} for another way of storing
+compiler options into an object file.
+
 @item -fpic
 @opindex fpic
 @cindex global offset table
Index: gcc/toplev.c
===================================================================
--- gcc/toplev.c	(revision 249007)
+++ gcc/toplev.c	(working copy)
@@ -809,7 +809,9 @@
    Each line begins with INDENT and ends with TERM.
    Each switch is separated from the next by SEP.  */
 
-static void
+void print_switch_values (print_switch_fn_type);
+
+void
 print_switch_values (print_switch_fn_type print_fn)
 {
   int pos = 0;
Index: gcc/varasm.c
===================================================================
--- gcc/varasm.c	(revision 249007)
+++ gcc/varasm.c	(working copy)
@@ -7545,6 +7545,32 @@
   v.release ();
 }
 
+/* Print TEXT to asm_out_file if TYPE is an enabled switch.
+   Returns the number of characters printed or -1 upon failure.  */
+
+static int
+dump_enabled_to_asm_out_file (print_switch_type type, const char * text)
+{
+  switch (type)
+    {
+    case SWITCH_TYPE_LINE_END:
+    case SWITCH_TYPE_LINE_START:
+    case SWITCH_TYPE_DESCRIPTIVE:
+      return 0;
+
+    case SWITCH_TYPE_PASSED:
+    case SWITCH_TYPE_ENABLED:
+      ASM_OUTPUT_ASCII (asm_out_file, text, strlen (text));
+      ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
+       /* No need to return the length here as
+	 print_single_switch has already done it.  */
+      return strlen (text) + 1;
+
+    default:
+      return -1;
+    }
+}
+
 /* This function provides a possible implementation of the
    TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets.  When triggered
    by -frecord-gcc-switches it creates a new mergeable, string section in the
@@ -7586,6 +7612,14 @@
 				 NULL);
 	      switch_to_section (sec);
 	      started = true;
+
+	      if (flag_verbose_asm)
+		{
+		  extern void print_switch_values (print_switch_fn_type);
+
+		  print_switch_values (dump_enabled_to_asm_out_file);
+		  putc ('\n', asm_out_file);
+		}
 	    }
 	}
 

Reply via email to