[1/2] refactor usage of compressed_debug_section_type

Message ID 597ed978-d121-220c-b56b-2ffb94bd091c@suse.cz
State Accepted, archived
Headers
Series [1/2] refactor usage of compressed_debug_section_type |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Martin Liška Oct. 3, 2022, 7:50 a.m. UTC
  bfd/ChangeLog:

	* bfd-in.h (bfd_hash_set_default_size): Add COMPRESS_UNKNOWN
	  enum value.
	(struct compressed_type_tuple): New.
	* bfd-in2.h (bfd_hash_set_default_size): Regenerate.
	(struct compressed_type_tuple): Likewise.
	* libbfd.c (ARRAY_SIZE): New macro.
	(bfd_get_compression_algorithm): New function.
	(bfd_get_compression_algorithm_name): Likewise.

gas/ChangeLog:

	* as.c: Do not special-case, use the new functions.

ld/ChangeLog:

	* emultempl/elf.em: Do not special-case, use the new functions.
	* lexsup.c (elf_static_list_options): Likewise.
---
 bfd/bfd-in.h        | 20 +++++++++++++++++---
 bfd/bfd-in2.h       | 20 +++++++++++++++++---
 bfd/libbfd.c        | 36 ++++++++++++++++++++++++++++++++++++
 gas/as.c            | 34 +++++++++-------------------------
 ld/emultempl/elf.em | 22 +++++++---------------
 ld/lexsup.c         |  8 ++------
 6 files changed, 88 insertions(+), 52 deletions(-)
  

Comments

Martin Liška Oct. 11, 2022, 7:14 a.m. UTC | #1
PING^1

On 10/3/22 09:50, Martin Liška wrote:
> bfd/ChangeLog:
> 
> 	* bfd-in.h (bfd_hash_set_default_size): Add COMPRESS_UNKNOWN
> 	  enum value.
> 	(struct compressed_type_tuple): New.
> 	* bfd-in2.h (bfd_hash_set_default_size): Regenerate.
> 	(struct compressed_type_tuple): Likewise.
> 	* libbfd.c (ARRAY_SIZE): New macro.
> 	(bfd_get_compression_algorithm): New function.
> 	(bfd_get_compression_algorithm_name): Likewise.
> 
> gas/ChangeLog:
> 
> 	* as.c: Do not special-case, use the new functions.
> 
> ld/ChangeLog:
> 
> 	* emultempl/elf.em: Do not special-case, use the new functions.
> 	* lexsup.c (elf_static_list_options): Likewise.
> ---
>  bfd/bfd-in.h        | 20 +++++++++++++++++---
>  bfd/bfd-in2.h       | 20 +++++++++++++++++---
>  bfd/libbfd.c        | 36 ++++++++++++++++++++++++++++++++++++
>  gas/as.c            | 34 +++++++++-------------------------
>  ld/emultempl/elf.em | 22 +++++++---------------
>  ld/lexsup.c         |  8 ++------
>  6 files changed, 88 insertions(+), 52 deletions(-)
> 
> diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h
> index 4765ea80536..82e33d400f3 100644
> --- a/bfd/bfd-in.h
> +++ b/bfd/bfd-in.h
> @@ -335,15 +335,23 @@ extern void bfd_hash_traverse
>     this size.  */
>  extern unsigned long bfd_hash_set_default_size (unsigned long);
>  
> -/* Types of compressed DWARF debug sections.  We currently support
> -   zlib.  */
> +/* Types of compressed DWARF debug sections.  */
>  enum compressed_debug_section_type
>  {
>    COMPRESS_DEBUG_NONE = 0,
>    COMPRESS_DEBUG = 1 << 0,
>    COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 1,
>    COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 2,
> -  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3
> +  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3,
> +  COMPRESS_UNKNOWN = 1 << 4
> +};
> +
> +/* Tuple for compressed_debug_section_type and their name.  */
> +
> +struct compressed_type_tuple
> +{
> +  enum compressed_debug_section_type type;
> +  const char *name;
>  };
>  
>  /* This structure is used to keep track of stabs in sections
> @@ -456,6 +464,12 @@ extern void bfd_free_window
>    (bfd_window *);
>  extern bool bfd_get_file_window
>    (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
> +
> +
> +extern enum compressed_debug_section_type bfd_get_compression_algorithm
> +  (const char *);
> +extern const char *bfd_get_compression_algorithm_name
> +  (enum compressed_debug_section_type);
>  
>  /* Externally visible ELF routines.  */
>  
> diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
> index 5c80956c79c..39f925362e0 100644
> --- a/bfd/bfd-in2.h
> +++ b/bfd/bfd-in2.h
> @@ -342,15 +342,23 @@ extern void bfd_hash_traverse
>     this size.  */
>  extern unsigned long bfd_hash_set_default_size (unsigned long);
>  
> -/* Types of compressed DWARF debug sections.  We currently support
> -   zlib.  */
> +/* Types of compressed DWARF debug sections.  */
>  enum compressed_debug_section_type
>  {
>    COMPRESS_DEBUG_NONE = 0,
>    COMPRESS_DEBUG = 1 << 0,
>    COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 1,
>    COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 2,
> -  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3
> +  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3,
> +  COMPRESS_UNKNOWN = 1 << 4
> +};
> +
> +/* Tuple for compressed_debug_section_type and their name.  */
> +
> +struct compressed_type_tuple
> +{
> +  enum compressed_debug_section_type type;
> +  const char *name;
>  };
>  
>  /* This structure is used to keep track of stabs in sections
> @@ -463,6 +471,12 @@ extern void bfd_free_window
>    (bfd_window *);
>  extern bool bfd_get_file_window
>    (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
> +
> +
> +extern enum compressed_debug_section_type bfd_get_compression_algorithm
> +  (const char *);
> +extern const char *bfd_get_compression_algorithm_name
> +  (enum compressed_debug_section_type);
>  
>  /* Externally visible ELF routines.  */
>  
> diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> index d33f3416206..14e7d8ef34f 100644
> --- a/bfd/libbfd.c
> +++ b/bfd/libbfd.c
> @@ -1244,3 +1244,39 @@ _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
>  {
>    return true;
>  }
> +
> +/* Display texts for type of compressed DWARF debug sections.  */
> +static const struct compressed_type_tuple compressed_debug_section_names[] =
> +{
> +  { COMPRESS_DEBUG_NONE, "none" },
> +  { COMPRESS_DEBUG, "zlib" },
> +  { COMPRESS_DEBUG_GNU_ZLIB, "zlib-gnu" },
> +  { COMPRESS_DEBUG_GABI_ZLIB, "zlib-gabi" },
> +  { COMPRESS_DEBUG_ZSTD, "zstd" },
> +};
> +
> +#ifndef ARRAY_SIZE
> +#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
> +#endif
> +
> +/* Return compressed_debug_section_type from a string representation.  */
> +enum compressed_debug_section_type
> +bfd_get_compression_algorithm (const char *name)
> +{
> +  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
> +    if (strcasecmp (compressed_debug_section_names[i].name, name) == 0)
> +      return compressed_debug_section_names[i].type;
> +
> +  return COMPRESS_UNKNOWN;
> +}
> +
> +/* Return compression algorithm name based on the type.  */
> +const char *
> +bfd_get_compression_algorithm_name (enum compressed_debug_section_type type)
> +{
> +  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
> +    if (type == compressed_debug_section_names[i].type)
> +      return compressed_debug_section_names[i].name;
> +
> +  return NULL;
> +}
> diff --git a/gas/as.c b/gas/as.c
> index 9ce3d622f95..a5c2d2459f7 100644
> --- a/gas/as.c
> +++ b/gas/as.c
> @@ -250,21 +250,16 @@ Options:\n\
>  
>    fprintf (stream, _("\
>    --alternate             initially turn on alternate macro syntax\n"));
> -#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
>    fprintf (stream, _("\
>    --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
> -                          compress DWARF debug sections using zlib [default]\n"));
> +                          compress DWARF debug sections\n")),
>    fprintf (stream, _("\
> -  --nocompress-debug-sections\n\
> -                          don't compress DWARF debug sections\n"));
> -#else
> -  fprintf (stream, _("\
> -  --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
> -                          compress DWARF debug sections using zlib\n"));
> +		            Default: %s\n"),
> +	   bfd_get_compression_algorithm_name (flag_compress_debug));
> +
>    fprintf (stream, _("\
>    --nocompress-debug-sections\n\
> -                          don't compress DWARF debug sections [default]\n"));
> -#endif
> +                          don't compress DWARF debug sections\n"));
>    fprintf (stream, _("\
>    -D                      produce assembler debugging messages\n"));
>    fprintf (stream, _("\
> @@ -741,24 +736,13 @@ This program has absolutely no warranty.\n"));
>  	  if (optarg)
>  	    {
>  #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
> -	      if (strcasecmp (optarg, "none") == 0)
> -		flag_compress_debug = COMPRESS_DEBUG_NONE;
> -	      else if (strcasecmp (optarg, "zlib") == 0)
> -		flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
> -	      else if (strcasecmp (optarg, "zlib-gnu") == 0)
> -		flag_compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
> -	      else if (strcasecmp (optarg, "zlib-gabi") == 0)
> -		flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
> -	      else if (strcasecmp (optarg, "zstd") == 0)
> -		{
> -#ifdef HAVE_ZSTD
> -		  flag_compress_debug = COMPRESS_DEBUG_ZSTD;
> -#else
> +	      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
> -		}
> -	      else
> +	      if (flag_compress_debug == COMPRESS_UNKNOWN)
>  		as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
>  			  optarg);
>  #else
> diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
> index acd66f907d1..5dfc03a740c 100644
> --- a/ld/emultempl/elf.em
> +++ b/ld/emultempl/elf.em
> @@ -660,24 +660,16 @@ gld${EMULATION_NAME}_handle_option (int optc)
>        break;
>  
>      case OPTION_COMPRESS_DEBUG:
> -      if (strcasecmp (optarg, "none") == 0)
> -	link_info.compress_debug = COMPRESS_DEBUG_NONE;
> -      else if (strcasecmp (optarg, "zlib") == 0)
> -	link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
> -      else if (strcasecmp (optarg, "zlib-gnu") == 0)
> -	link_info.compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
> -      else if (strcasecmp (optarg, "zlib-gabi") == 0)
> -	link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
> -      else if (strcasecmp (optarg, "zstd") == 0)
> +      link_info.compress_debug = bfd_get_compression_algorithm (optarg);
> +      if (strcasecmp (optarg, "zstd") == 0)
>  	{
> -#ifdef HAVE_ZSTD
> -	  link_info.compress_debug = COMPRESS_DEBUG_ZSTD;
> -#else
> -	  einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
> -		    "with zstd support\n"));
> +#ifndef HAVE_ZSTD
> +	  if (link_info.compress_debug == COMPRESS_DEBUG_ZSTD)
> +	    einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
> +		  "with zstd support\n"));
>  #endif
>  	}
> -      else
> +      if (link_info.compress_debug == COMPRESS_UNKNOWN)
>  	einfo (_("%F%P: invalid --compress-debug-sections option: \`%s'\n"),
>  	       optarg);
>        break;
> diff --git a/ld/lexsup.c b/ld/lexsup.c
> index 299371fb775..0c01c9966e0 100644
> --- a/ld/lexsup.c
> +++ b/ld/lexsup.c
> @@ -2148,13 +2148,9 @@ elf_static_list_options (FILE *file)
>    fprintf (file, _("\
>    --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi|zstd]\n\
>  			      Compress DWARF debug sections\n"));
> -#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
>    fprintf (file, _("\
> -                                Default: zlib-gabi\n"));
> -#else
> -  fprintf (file, _("\
> -                                Default: none\n"));
> -#endif
> +                                Default: %s\n"),
> +	   bfd_get_compression_algorithm_name (link_info.compress_debug));
>    fprintf (file, _("\
>    -z common-page-size=SIZE    Set common page size to SIZE\n"));
>    fprintf (file, _("\
  
Nick Clifton Oct. 11, 2022, 12:06 p.m. UTC | #2
Hi Martin,

> PING^1
Sorry!


> On 10/3/22 09:50, Martin Liška wrote:
>> bfd/ChangeLog:
>>
>> 	* bfd-in.h (bfd_hash_set_default_size): Add COMPRESS_UNKNOWN
>> 	  enum value.
>> 	(struct compressed_type_tuple): New.
>> 	* bfd-in2.h (bfd_hash_set_default_size): Regenerate.
>> 	(struct compressed_type_tuple): Likewise.
>> 	* libbfd.c (ARRAY_SIZE): New macro.
>> 	(bfd_get_compression_algorithm): New function.
>> 	(bfd_get_compression_algorithm_name): Likewise.
>>
>> gas/ChangeLog:
>>
>> 	* as.c: Do not special-case, use the new functions.
>>
>> ld/ChangeLog:
>>
>> 	* emultempl/elf.em: Do not special-case, use the new functions.
>> 	* lexsup.c (elf_static_list_options): Likewise.

Approved - please apply.

Cheers
   Nick
  
Martin Liška Oct. 11, 2022, 1:27 p.m. UTC | #3
On 10/11/22 14:06, Nick Clifton wrote:
> Hi Martin,
> 
>> PING^1
> Sorry!
> 
> 
>> On 10/3/22 09:50, Martin Liška wrote:
>>> bfd/ChangeLog:
>>>
>>>     * bfd-in.h (bfd_hash_set_default_size): Add COMPRESS_UNKNOWN
>>>       enum value.
>>>     (struct compressed_type_tuple): New.
>>>     * bfd-in2.h (bfd_hash_set_default_size): Regenerate.
>>>     (struct compressed_type_tuple): Likewise.
>>>     * libbfd.c (ARRAY_SIZE): New macro.
>>>     (bfd_get_compression_algorithm): New function.
>>>     (bfd_get_compression_algorithm_name): Likewise.
>>>
>>> gas/ChangeLog:
>>>
>>>     * as.c: Do not special-case, use the new functions.
>>>
>>> ld/ChangeLog:
>>>
>>>     * emultempl/elf.em: Do not special-case, use the new functions.
>>>     * lexsup.c (elf_static_list_options): Likewise.
> 
> Approved - please apply.
> 
> Cheers
>   Nick
> 
> 

Hello.

Thanks, I've installed the patches and noticed the following obvious fix is needed.
I'm going to install it.

Cheers,
Martin
  

Patch

diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h
index 4765ea80536..82e33d400f3 100644
--- a/bfd/bfd-in.h
+++ b/bfd/bfd-in.h
@@ -335,15 +335,23 @@  extern void bfd_hash_traverse
    this size.  */
 extern unsigned long bfd_hash_set_default_size (unsigned long);
 
-/* Types of compressed DWARF debug sections.  We currently support
-   zlib.  */
+/* Types of compressed DWARF debug sections.  */
 enum compressed_debug_section_type
 {
   COMPRESS_DEBUG_NONE = 0,
   COMPRESS_DEBUG = 1 << 0,
   COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 1,
   COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 2,
-  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3
+  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3,
+  COMPRESS_UNKNOWN = 1 << 4
+};
+
+/* Tuple for compressed_debug_section_type and their name.  */
+
+struct compressed_type_tuple
+{
+  enum compressed_debug_section_type type;
+  const char *name;
 };
 
 /* This structure is used to keep track of stabs in sections
@@ -456,6 +464,12 @@  extern void bfd_free_window
   (bfd_window *);
 extern bool bfd_get_file_window
   (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
+
+
+extern enum compressed_debug_section_type bfd_get_compression_algorithm
+  (const char *);
+extern const char *bfd_get_compression_algorithm_name
+  (enum compressed_debug_section_type);
 
 /* Externally visible ELF routines.  */
 
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 5c80956c79c..39f925362e0 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -342,15 +342,23 @@  extern void bfd_hash_traverse
    this size.  */
 extern unsigned long bfd_hash_set_default_size (unsigned long);
 
-/* Types of compressed DWARF debug sections.  We currently support
-   zlib.  */
+/* Types of compressed DWARF debug sections.  */
 enum compressed_debug_section_type
 {
   COMPRESS_DEBUG_NONE = 0,
   COMPRESS_DEBUG = 1 << 0,
   COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 1,
   COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 2,
-  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3
+  COMPRESS_DEBUG_ZSTD = COMPRESS_DEBUG | 1 << 3,
+  COMPRESS_UNKNOWN = 1 << 4
+};
+
+/* Tuple for compressed_debug_section_type and their name.  */
+
+struct compressed_type_tuple
+{
+  enum compressed_debug_section_type type;
+  const char *name;
 };
 
 /* This structure is used to keep track of stabs in sections
@@ -463,6 +471,12 @@  extern void bfd_free_window
   (bfd_window *);
 extern bool bfd_get_file_window
   (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
+
+
+extern enum compressed_debug_section_type bfd_get_compression_algorithm
+  (const char *);
+extern const char *bfd_get_compression_algorithm_name
+  (enum compressed_debug_section_type);
 
 /* Externally visible ELF routines.  */
 
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index d33f3416206..14e7d8ef34f 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -1244,3 +1244,39 @@  _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
 {
   return true;
 }
+
+/* Display texts for type of compressed DWARF debug sections.  */
+static const struct compressed_type_tuple compressed_debug_section_names[] =
+{
+  { COMPRESS_DEBUG_NONE, "none" },
+  { COMPRESS_DEBUG, "zlib" },
+  { COMPRESS_DEBUG_GNU_ZLIB, "zlib-gnu" },
+  { COMPRESS_DEBUG_GABI_ZLIB, "zlib-gabi" },
+  { COMPRESS_DEBUG_ZSTD, "zstd" },
+};
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
+#endif
+
+/* Return compressed_debug_section_type from a string representation.  */
+enum compressed_debug_section_type
+bfd_get_compression_algorithm (const char *name)
+{
+  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
+    if (strcasecmp (compressed_debug_section_names[i].name, name) == 0)
+      return compressed_debug_section_names[i].type;
+
+  return COMPRESS_UNKNOWN;
+}
+
+/* Return compression algorithm name based on the type.  */
+const char *
+bfd_get_compression_algorithm_name (enum compressed_debug_section_type type)
+{
+  for (unsigned i = 0; i < ARRAY_SIZE (compressed_debug_section_names); ++i)
+    if (type == compressed_debug_section_names[i].type)
+      return compressed_debug_section_names[i].name;
+
+  return NULL;
+}
diff --git a/gas/as.c b/gas/as.c
index 9ce3d622f95..a5c2d2459f7 100644
--- a/gas/as.c
+++ b/gas/as.c
@@ -250,21 +250,16 @@  Options:\n\
 
   fprintf (stream, _("\
   --alternate             initially turn on alternate macro syntax\n"));
-#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
   fprintf (stream, _("\
   --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
-                          compress DWARF debug sections using zlib [default]\n"));
+                          compress DWARF debug sections\n")),
   fprintf (stream, _("\
-  --nocompress-debug-sections\n\
-                          don't compress DWARF debug sections\n"));
-#else
-  fprintf (stream, _("\
-  --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
-                          compress DWARF debug sections using zlib\n"));
+		            Default: %s\n"),
+	   bfd_get_compression_algorithm_name (flag_compress_debug));
+
   fprintf (stream, _("\
   --nocompress-debug-sections\n\
-                          don't compress DWARF debug sections [default]\n"));
-#endif
+                          don't compress DWARF debug sections\n"));
   fprintf (stream, _("\
   -D                      produce assembler debugging messages\n"));
   fprintf (stream, _("\
@@ -741,24 +736,13 @@  This program has absolutely no warranty.\n"));
 	  if (optarg)
 	    {
 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
-	      if (strcasecmp (optarg, "none") == 0)
-		flag_compress_debug = COMPRESS_DEBUG_NONE;
-	      else if (strcasecmp (optarg, "zlib") == 0)
-		flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
-	      else if (strcasecmp (optarg, "zlib-gnu") == 0)
-		flag_compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
-	      else if (strcasecmp (optarg, "zlib-gabi") == 0)
-		flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
-	      else if (strcasecmp (optarg, "zstd") == 0)
-		{
-#ifdef HAVE_ZSTD
-		  flag_compress_debug = COMPRESS_DEBUG_ZSTD;
-#else
+	      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
-		}
-	      else
+	      if (flag_compress_debug == COMPRESS_UNKNOWN)
 		as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
 			  optarg);
 #else
diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index acd66f907d1..5dfc03a740c 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -660,24 +660,16 @@  gld${EMULATION_NAME}_handle_option (int optc)
       break;
 
     case OPTION_COMPRESS_DEBUG:
-      if (strcasecmp (optarg, "none") == 0)
-	link_info.compress_debug = COMPRESS_DEBUG_NONE;
-      else if (strcasecmp (optarg, "zlib") == 0)
-	link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
-      else if (strcasecmp (optarg, "zlib-gnu") == 0)
-	link_info.compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
-      else if (strcasecmp (optarg, "zlib-gabi") == 0)
-	link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
-      else if (strcasecmp (optarg, "zstd") == 0)
+      link_info.compress_debug = bfd_get_compression_algorithm (optarg);
+      if (strcasecmp (optarg, "zstd") == 0)
 	{
-#ifdef HAVE_ZSTD
-	  link_info.compress_debug = COMPRESS_DEBUG_ZSTD;
-#else
-	  einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
-		    "with zstd support\n"));
+#ifndef HAVE_ZSTD
+	  if (link_info.compress_debug == COMPRESS_DEBUG_ZSTD)
+	    einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
+		  "with zstd support\n"));
 #endif
 	}
-      else
+      if (link_info.compress_debug == COMPRESS_UNKNOWN)
 	einfo (_("%F%P: invalid --compress-debug-sections option: \`%s'\n"),
 	       optarg);
       break;
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 299371fb775..0c01c9966e0 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -2148,13 +2148,9 @@  elf_static_list_options (FILE *file)
   fprintf (file, _("\
   --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi|zstd]\n\
 			      Compress DWARF debug sections\n"));
-#ifdef DEFAULT_FLAG_COMPRESS_DEBUG
   fprintf (file, _("\
-                                Default: zlib-gabi\n"));
-#else
-  fprintf (file, _("\
-                                Default: none\n"));
-#endif
+                                Default: %s\n"),
+	   bfd_get_compression_algorithm_name (link_info.compress_debug));
   fprintf (file, _("\
   -z common-page-size=SIZE    Set common page size to SIZE\n"));
   fprintf (file, _("\