[PATCH] [PR binutils/15350] Fix compressed debug sections for PE targets

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] [PR binutils/15350] Fix compressed debug sections for PE targets

Sourceware - binutils list mailing list
When objcopy --compress-debug-sections was used, it actually did the
compression, but the zlib header was not written, so the resulting debug info
was invalid.

bfd/ChangeLog:

2019-12-27  Hannes Domani  <[hidden email]>

        * bfd.c (bfd_update_compression_header):
        Write zlib header for compressed debug sections of PE files.
---
 bfd/bfd.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/bfd/bfd.c b/bfd/bfd.c
index a3c522cbf8..a935def35f 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -2542,9 +2542,11 @@ bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
 {
   if ((abfd->flags & BFD_COMPRESS) != 0)
     {
-      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+  || bfd_get_flavour (abfd) == bfd_target_coff_flavour)
  {
-  if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
+  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+      && (abfd->flags & BFD_COMPRESS_GABI) != 0)
     {
       const struct elf_backend_data *bed
  = get_elf_backend_data (abfd);
@@ -2579,7 +2581,8 @@ bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
   else
     {
       /* Clear the SHF_COMPRESSED bit.  */
-      elf_section_flags (sec) &= ~SHF_COMPRESSED;
+      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ elf_section_flags (sec) &= ~SHF_COMPRESSED;
 
       /* Write the zlib header.  It should be "ZLIB" followed by
  the uncompressed section size, 8 bytes in big-endian
--
2.24.1

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] [PR binutils/15350] Fix compressed debug sections for PE targets

Alan Modra-3
On Fri, Dec 27, 2019 at 07:47:44PM +0100, Hannes Domani via binutils wrote:

> When objcopy --compress-debug-sections was used, it actually did the
> compression, but the zlib header was not written, so the resulting debug info
> was invalid.
>
> bfd/ChangeLog:
>
> 2019-12-27  Hannes Domani  <[hidden email]>
>
> * bfd.c (bfd_update_compression_header):
> Write zlib header for compressed debug sections of PE files.

Thanks!  I'm going to commit the following variation on your patch.
diff -w shown to omit indentation changes.

        PR 15350
        * bfd.c (bfd_update_compression_header): Write zlib header for
        formats other than ELF too.

diff --git a/bfd/bfd.c b/bfd/bfd.c
index a3c522cbf8..b6821fa20f 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -2540,22 +2540,22 @@ void
 bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
        asection *sec)
 {
-  if ((abfd->flags & BFD_COMPRESS) != 0)
-    {
-      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+  if ((abfd->flags & BFD_COMPRESS) == 0)
+    abort ();
+
+  switch (bfd_get_flavour (abfd))
     {
+    case bfd_target_elf_flavour:
       if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
  {
-      const struct elf_backend_data *bed
- = get_elf_backend_data (abfd);
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   /* Set the SHF_COMPRESSED bit.  */
   elf_section_flags (sec) |= SHF_COMPRESSED;
 
   if (bed->s->elfclass == ELFCLASS32)
     {
-  Elf32_External_Chdr *echdr
-    = (Elf32_External_Chdr *) contents;
+      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
       bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
       bfd_put_32 (abfd, sec->size, &echdr->ch_size);
       bfd_put_32 (abfd, 1 << sec->alignment_power,
@@ -2565,8 +2565,7 @@ bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
     }
   else
     {
-  Elf64_External_Chdr *echdr
-    = (Elf64_External_Chdr *) contents;
+      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
       bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
       bfd_put_32 (abfd, 0, &echdr->ch_reserved);
       bfd_put_64 (abfd, sec->size, &echdr->ch_size);
@@ -2575,12 +2574,14 @@ bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
       /* bfd_log2 (alignof (Elf64_Chdr)) */
       bfd_set_section_alignment (sec, 3);
     }
+  break;
  }
-  else
-    {
+
       /* Clear the SHF_COMPRESSED bit.  */
       elf_section_flags (sec) &= ~SHF_COMPRESSED;
+      /* Fall through.  */
 
+    default:
       /* Write the zlib header.  It should be "ZLIB" followed by
  the uncompressed section size, 8 bytes in big-endian
  order.  */
@@ -2588,12 +2589,9 @@ bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
       bfd_putb64 (sec->size, contents + 4);
       /* No way to keep the original alignment, just use 1 always. */
       bfd_set_section_alignment (sec, 0);
+      break;
     }
 }
-    }
-  else
-    abort ();
-}
 
 /*
    FUNCTION

--
Alan Modra
Australia Development Lab, IBM