[v2,1/3] gas: Unify parsing of --compress-debug-sections

Message ID 20230228001633.3602-1-tdevries@suse.de
State Unresolved
Headers
Series [v2,1/3] gas: Unify parsing of --compress-debug-sections |

Checks

Context Check Description
snail/binutils-gdb-check warning Git am fail log

Commit Message

Tom de Vries Feb. 28, 2023, 12:16 a.m. UTC
  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(-)


base-commit: d273049e140ef324be2b018e235ae0ba8021a769
  

Comments

Tom de Vries March 9, 2023, 3:33 p.m. UTC | #1
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
  

Patch

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: