public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections
@ 2023-02-28  0:16 Tom de Vries
  2023-02-28  0:16 ` [PATCH v2 2/3] gas: Handle --compress-debug-sections=subopt1,subopt2 Tom de Vries
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tom de Vries @ 2023-02-28  0:16 UTC (permalink / raw)
  To: binutils

There are three related option forms:
* --compress-debug-sections=<none|zlib|zlib-gnu|zlib-gabi>
* --compress-debug-sections
* --nocompress-debug-sections
where the last two are documented as being equivalent to:
* --compress-debug-section=zlib-gabi
* --compress-debug-section=none

However, the three related option forms are handled independently in
parse_args, and there are minor differences in handling.

For instance, in case !defined OBJ_ELF && !defined OBJ_MAYBE_ELF, using
--compress-debug-section=zlib-gabi or --compress-debug-section=none will
trigger as_fatal ("--compress-debug-sections=%s is unsupported"), while that's
not the case for --compress-debug-sections and --nocompress-debug-sections.

Fix this by:
- factoring out a new function parse_compress_debug_optarg, and
- using it to handle all three options forms.

Also make the code easier to read by not nesting preprocessor conditionals.
---
 gas/as.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/gas/as.c b/gas/as.c
index 598bfd56cf5..40742b13511 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -418,6 +418,32 @@ Options:\n\
     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
 }
 
+static void
+parse_compress_debug_optarg (const char *optarg)
+{
+#if !defined OBJ_ELF && !defined OBJ_MAYBE_ELF
+  as_fatal (_("--compress-debug-sections=%s is unsupported"),
+	    optarg);
+#endif
+
+  gas_assert (optarg != NULL);
+
+  enum compressed_debug_section_type tmp
+    = bfd_get_compression_algorithm (optarg);
+
+#ifndef HAVE_ZSTD
+  if (tmp == COMPRESS_DEBUG_ZSTD)
+    as_fatal (_ ("--compress-debug-sections=zstd: gas is not "
+		 "built with zstd support"));
+#endif
+
+  if (tmp == COMPRESS_UNKNOWN)
+    as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
+	      optarg);
+
+  flag_compress_debug = tmp;
+}
+
 /* Since it is easy to do here we interpret the special arg "-"
    to mean "use stdin" and we set that argv[] pointing to "".
    After we have munged argv[], the only things left are source file
@@ -747,28 +773,13 @@ This program has absolutely no warranty.\n"));
 
 	case OPTION_COMPRESS_DEBUG:
 	  if (optarg)
-	    {
-#if defined OBJ_ELF || defined OBJ_MAYBE_ELF
-	      flag_compress_debug = bfd_get_compression_algorithm (optarg);
-#ifndef HAVE_ZSTD
-	      if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
-		  as_fatal (_ ("--compress-debug-sections=zstd: gas is not "
-			       "built with zstd support"));
-#endif
-	      if (flag_compress_debug == COMPRESS_UNKNOWN)
-		as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
-			  optarg);
-#else
-	      as_fatal (_("--compress-debug-sections=%s is unsupported"),
-			optarg);
-#endif
-	    }
+	    parse_compress_debug_optarg (optarg);
 	  else
-	    flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
+	    parse_compress_debug_optarg ("zlib-gabi");
 	  break;
 
 	case OPTION_NOCOMPRESS_DEBUG:
-	  flag_compress_debug = COMPRESS_DEBUG_NONE;
+	  parse_compress_debug_optarg ("none");
 	  break;
 
 	case OPTION_DEBUG_PREFIX_MAP:

base-commit: d273049e140ef324be2b018e235ae0ba8021a769
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] gas: Handle --compress-debug-sections=subopt1,subopt2
  2023-02-28  0:16 [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
@ 2023-02-28  0:16 ` Tom de Vries
  2023-02-28  0:16 ` [PATCH v2 3/3] gas: Add --compress-debug-sections={heuristic-always,heuristic-sizewin} Tom de Vries
  2023-03-09 15:33 ` [PING] [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2023-02-28  0:16 UTC (permalink / raw)
  To: binutils

Add the possibility to specify more than one --compress-debug-sections subopt
at a time, using a comma separator.

Using --compress-debug-sections=subopt1,subopt2 is equivalent to using
--compress-debug-sections=subopt1 --compress-debug-sections=subopt2.
---
 gas/as.c | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/gas/as.c b/gas/as.c
index 40742b13511..c159c5d0d0a 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -419,13 +419,8 @@ Options:\n\
 }
 
 static void
-parse_compress_debug_optarg (const char *optarg)
+parse_compress_debug_optarg_1 (const char *optarg)
 {
-#if !defined OBJ_ELF && !defined OBJ_MAYBE_ELF
-  as_fatal (_("--compress-debug-sections=%s is unsupported"),
-	    optarg);
-#endif
-
   gas_assert (optarg != NULL);
 
   enum compressed_debug_section_type tmp
@@ -444,6 +439,45 @@ parse_compress_debug_optarg (const char *optarg)
   flag_compress_debug = tmp;
 }
 
+static void
+parse_compress_debug_optarg (const char *optarg)
+{
+#if !defined OBJ_ELF && !defined OBJ_MAYBE_ELF
+  as_fatal (_("--compress-debug-sections=%s is unsupported"),
+	    optarg);
+#endif
+
+  gas_assert (optarg != NULL);
+
+  /* Tokenize subopts pass to parse_compress_debug_optarg_1.  */
+  char sep = ',';
+  while (true)
+    {
+      const char *idx = optarg;
+      while (*idx != '\0' && *idx != sep)
+	idx++;
+
+      size_t len = idx - optarg;
+      if (len == 0)
+	{
+	  /* Generate error.  */
+	  parse_compress_debug_optarg_1 ("");
+	  break;
+	}
+
+      char *tmp = xstrndup (optarg, len);
+      parse_compress_debug_optarg_1 (tmp);
+      free (tmp);
+
+      if (*idx == '\0')
+	break;
+
+      /* Step over separator and continue tokenizing.  */
+      gas_assert (*idx == sep);
+      optarg = idx + 1;
+  }
+}
+
 /* Since it is easy to do here we interpret the special arg "-"
    to mean "use stdin" and we set that argv[] pointing to "".
    After we have munged argv[], the only things left are source file
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] gas: Add --compress-debug-sections={heuristic-always,heuristic-sizewin}
  2023-02-28  0:16 [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
  2023-02-28  0:16 ` [PATCH v2 2/3] gas: Handle --compress-debug-sections=subopt1,subopt2 Tom de Vries
@ 2023-02-28  0:16 ` Tom de Vries
  2023-03-09 15:33 ` [PING] [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2023-02-28  0:16 UTC (permalink / raw)
  To: binutils

Gas has an option --compress-debug-sections that allows it to generate
compressed debug sections.

That does not guarantee that the debug sections are in fact compressed:
...
$ gcc hello.c -Wa,-gdwarf-5 -c -Wa,--compress-debug-sections=zstd
$ readelf -S -W hello.o | grep " .debug"
  [ 9] .debug_line       PROGBITS         0000a8 000053 00      0   0  1
  [11] .debug_line_str   PROGBITS         0000fb 000025 01  MS  0   0  1
  [12] .debug_info       PROGBITS         000120 000039 00      0   0  1
  [14] .debug_abbrev     PROGBITS         000159 000028 00      0   0  1
  [15] .debug_aranges    PROGBITS         000190 000030 00      0   0 16
  [17] .debug_str        PROGBITS         0001c0 000039 01  MS  0   0  1
...

Sensibly so, they're only compressed if that provides a size benefit.

However, for the purpose of testing components consuming dwarf
we may want the sections to be compressed regardless.

Add a new suboption --compress-debug-sections=heuristic-always that ignores
the size heuristic, such that we have instead:
...
$ gcc hello.c -Wa,-gdwarf-5 -c -Xassembler \
    --compress-debug-sections=zstd,heuristic-always
$ readelf -S -W hello.o | grep " .debug"
  [ 9] .debug_line       PROGBITS         0000a8 000064 00   C  0   0  8
  [11] .debug_line_str   PROGBITS         000110 000046 01 MSC  0   0  8
  [12] .debug_info       PROGBITS         000158 000046 00   C  0   0  8
  [14] .debug_abbrev     PROGBITS         0001a0 000049 00   C  0   0  8
  [15] .debug_aranges    PROGBITS         0001f0 000034 00   C  0   0  8
  [17] .debug_str        PROGBITS         000228 00005a 01 MSC  0   0  8
...

Also add an option --compress-debug-sections=heuristic-sizewin that represents
the current default.

Advertised as:
...
$ as --help
  ...
  --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd|heuristic-always|heuristic-sizewin}]
                          compress DWARF debug sections
                            Default: zlib
...

Tested on x86_64-linux.
---
 gas/as.c    | 16 +++++++++++++++-
 gas/as.h    |  8 ++++++++
 gas/write.c |  7 +++++--
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gas/as.c b/gas/as.c
index c159c5d0d0a..a766f864707 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -230,6 +230,8 @@ enum compressed_debug_section_type flag_compress_debug
   = DEFAULT_COMPRESSED_DEBUG_ALGORITHM;
 #endif
 
+enum compress_debug_heuristic compress_debug_heuristic = cdh_sizewin;
+
 static void
 show_usage (FILE * stream)
 {
@@ -252,7 +254,7 @@ Options:\n\
   fprintf (stream, _("\
   --alternate             initially turn on alternate macro syntax\n"));
   fprintf (stream, _("\
-  --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
+  --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd|heuristic-sizewin|heuristic-always}]\n\
                           compress DWARF debug sections\n")),
   fprintf (stream, _("\
 		            Default: %s\n"),
@@ -423,6 +425,18 @@ parse_compress_debug_optarg_1 (const char *optarg)
 {
   gas_assert (optarg != NULL);
 
+  if (strcmp (optarg, "heuristic-sizewin") == 0)
+    {
+      compress_debug_heuristic = cdh_sizewin;
+      return;
+    }
+
+  if (strcmp (optarg, "heuristic-always") == 0)
+    {
+      compress_debug_heuristic = cdh_always;
+      return;
+    }
+
   enum compressed_debug_section_type tmp
     = bfd_get_compression_algorithm (optarg);
 
diff --git a/gas/as.h b/gas/as.h
index 4c5fa9ecf7d..cd852500197 100644
--- a/gas/as.h
+++ b/gas/as.h
@@ -331,6 +331,14 @@ COMMON int flag_traditional_format;
 /* Type of compressed debug sections we should generate.   */
 COMMON enum compressed_debug_section_type flag_compress_debug;
 
+/* Debug sections compression heuristics.   */
+enum compress_debug_heuristic
+{
+  cdh_sizewin,
+  cdh_always
+};
+COMMON enum compress_debug_heuristic compress_debug_heuristic;
+
 /* TRUE if .note.GNU-stack section with SEC_CODE should be created */
 COMMON int flag_execstack;
 
diff --git a/gas/write.c b/gas/write.c
index 8273b7a42f1..34ea5746284 100644
--- a/gas/write.c
+++ b/gas/write.c
@@ -1463,9 +1463,11 @@ compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   segment_info_type *seginfo = seg_info (sec);
   bfd_size_type uncompressed_size = sec->size;
   flagword flags = bfd_section_flags (sec);
+  bool compress_debug_heuristic_sizewin
+    = compress_debug_heuristic == cdh_sizewin;
 
   if (seginfo == NULL
-      || uncompressed_size < 32
+      || (compress_debug_heuristic_sizewin && uncompressed_size < 32)
       || (flags & SEC_HAS_CONTENTS) == 0)
     return;
 
@@ -1582,7 +1584,8 @@ compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
 
   /* PR binutils/18087: If compression didn't make the section smaller,
      just keep it uncompressed.  */
-  if (compressed_size >= uncompressed_size)
+  if (compress_debug_heuristic_sizewin
+      && compressed_size >= uncompressed_size)
     return;
 
   /* Replace the uncompressed frag list with the compressed frag list.  */
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PING] [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections
  2023-02-28  0:16 [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
  2023-02-28  0:16 ` [PATCH v2 2/3] gas: Handle --compress-debug-sections=subopt1,subopt2 Tom de Vries
  2023-02-28  0:16 ` [PATCH v2 3/3] gas: Add --compress-debug-sections={heuristic-always,heuristic-sizewin} Tom de Vries
@ 2023-03-09 15:33 ` Tom de Vries
  2 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2023-03-09 15:33 UTC (permalink / raw)
  To: binutils

On 2/28/23 01:16, Tom de Vries via Binutils wrote:
> There are three related option forms:
> * --compress-debug-sections=<none|zlib|zlib-gnu|zlib-gabi>
> * --compress-debug-sections
> * --nocompress-debug-sections
> where the last two are documented as being equivalent to:
> * --compress-debug-section=zlib-gabi
> * --compress-debug-section=none
> 
> However, the three related option forms are handled independently in
> parse_args, and there are minor differences in handling.
> 
> For instance, in case !defined OBJ_ELF && !defined OBJ_MAYBE_ELF, using
> --compress-debug-section=zlib-gabi or --compress-debug-section=none will
> trigger as_fatal ("--compress-debug-sections=%s is unsupported"), while that's
> not the case for --compress-debug-sections and --nocompress-debug-sections.
> 
> Fix this by:
> - factoring out a new function parse_compress_debug_optarg, and
> - using it to handle all three options forms.
> 
> Also make the code easier to read by not nesting preprocessor conditionals.

Ping for this series.

FWIW, forgot to mention: I've dropped the doc bit in this series because 
it seems a better idea to update that bit once we've settled on the 
naming issue.

Also, the v1 was a patch and the v2 is a series, my hope is that 
splitting it up would make it easier to review.

Thanks,
- Tom

> ---
>   gas/as.c | 47 +++++++++++++++++++++++++++++------------------
>   1 file changed, 29 insertions(+), 18 deletions(-)
> 
> diff --git a/gas/as.c b/gas/as.c
> index 598bfd56cf5..40742b13511 100644
> --- a/gas/as.c
> +++ b/gas/as.c
> @@ -418,6 +418,32 @@ Options:\n\
>       fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
>   }
>   
> +static void
> +parse_compress_debug_optarg (const char *optarg)
> +{
> +#if !defined OBJ_ELF && !defined OBJ_MAYBE_ELF
> +  as_fatal (_("--compress-debug-sections=%s is unsupported"),
> +	    optarg);
> +#endif
> +
> +  gas_assert (optarg != NULL);
> +
> +  enum compressed_debug_section_type tmp
> +    = bfd_get_compression_algorithm (optarg);
> +
> +#ifndef HAVE_ZSTD
> +  if (tmp == COMPRESS_DEBUG_ZSTD)
> +    as_fatal (_ ("--compress-debug-sections=zstd: gas is not "
> +		 "built with zstd support"));
> +#endif
> +
> +  if (tmp == COMPRESS_UNKNOWN)
> +    as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
> +	      optarg);
> +
> +  flag_compress_debug = tmp;
> +}
> +
>   /* Since it is easy to do here we interpret the special arg "-"
>      to mean "use stdin" and we set that argv[] pointing to "".
>      After we have munged argv[], the only things left are source file
> @@ -747,28 +773,13 @@ This program has absolutely no warranty.\n"));
>   
>   	case OPTION_COMPRESS_DEBUG:
>   	  if (optarg)
> -	    {
> -#if defined OBJ_ELF || defined OBJ_MAYBE_ELF
> -	      flag_compress_debug = bfd_get_compression_algorithm (optarg);
> -#ifndef HAVE_ZSTD
> -	      if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
> -		  as_fatal (_ ("--compress-debug-sections=zstd: gas is not "
> -			       "built with zstd support"));
> -#endif
> -	      if (flag_compress_debug == COMPRESS_UNKNOWN)
> -		as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
> -			  optarg);
> -#else
> -	      as_fatal (_("--compress-debug-sections=%s is unsupported"),
> -			optarg);
> -#endif
> -	    }
> +	    parse_compress_debug_optarg (optarg);
>   	  else
> -	    flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
> +	    parse_compress_debug_optarg ("zlib-gabi");
>   	  break;
>   
>   	case OPTION_NOCOMPRESS_DEBUG:
> -	  flag_compress_debug = COMPRESS_DEBUG_NONE;
> +	  parse_compress_debug_optarg ("none");
>   	  break;
>   
>   	case OPTION_DEBUG_PREFIX_MAP:
> 
> base-commit: d273049e140ef324be2b018e235ae0ba8021a769


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-09 15:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28  0:16 [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries
2023-02-28  0:16 ` [PATCH v2 2/3] gas: Handle --compress-debug-sections=subopt1,subopt2 Tom de Vries
2023-02-28  0:16 ` [PATCH v2 3/3] gas: Add --compress-debug-sections={heuristic-always,heuristic-sizewin} Tom de Vries
2023-03-09 15:33 ` [PING] [PATCH v2 1/3] gas: Unify parsing of --compress-debug-sections Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).