Fix debug section compression so that it is only performed if it would make the secti...
authorJon Turney <jon.turney@dronecode.org.uk>
Wed, 18 Mar 2015 15:47:13 +0000 (15:47 +0000)
committerNick Clifton <nickc@redhat.com>
Wed, 18 Mar 2015 15:47:13 +0000 (15:47 +0000)
PR binutils/18087
gas * doc/as.texinfo: Note that when gas compresses debug sections the
compression is only performed if it makes the section smaller.
* write.c (compress_debug): Do not compress a debug section if
doing so would make it larger.

tests * gas/i386/dw2-compress-1.d: Do not expect the .debug_abbrev or
.debug_info sections to be compressed.

binu * doc/binutils.texi: Note that when objcopy compresses debug
sections the compression is only performed if it makes the section
smaller.

bfd * coffgen.c (make_a_section_from_file): Only prepend a z to a
debug section's name if the section was actually compressed.
* elf.c (_bfd_elf_make_section_from_shdr): Likewise.
* compress.c (bfd_init_section_compress_status): Do not compress
the section if doing so would make it bigger.  In such cases leave
the section alone and return COMPRESS_SECTION_NONE.

bfd/ChangeLog
bfd/coffgen.c
bfd/compress.c
bfd/elf.c
binutils/ChangeLog
binutils/doc/binutils.texi
gas/ChangeLog
gas/doc/as.texinfo
gas/testsuite/ChangeLog
gas/testsuite/gas/i386/dw2-compress-1.d
gas/write.c

index 9173e17fcb6cd7c9b1606ecd9601e67fa2b8eb48..c5f109e1618408afd16391b2caf284923ebf6037 100644 (file)
@@ -1,3 +1,14 @@
+2015-03-18  Jon Turney  <jon.turney@dronecode.org.uk>
+           Nick Clifton  <nickc@redhat.com>
+
+       PR binutils/18087
+       * coffgen.c (make_a_section_from_file): Only prepend a z to a
+       debug section's name if the section was actually compressed.
+       * elf.c (_bfd_elf_make_section_from_shdr): Likewise.
+       * compress.c (bfd_init_section_compress_status): Do not compress
+       the section if doing so would make it bigger.  In such cases leave
+       the section alone and return COMPRESS_SECTION_NONE.
+
 2015-03-17  Alan Modra  <amodra@gmail.com>
 
        * elf64-ppc.c (ppc64_elf_get_synthetic_symtab): Return count of 0
index 5c664a49f2bbff0011db6d33b8695b93d21e1a01..b1ab56e2e14f3b57ce254942a2f276510c9443e9 100644 (file)
@@ -178,18 +178,21 @@ make_a_section_from_file (bfd *abfd,
                 abfd, name);
              return FALSE;
            }
-         if (name[1] != 'z')
+         if (return_section->compress_status == COMPRESS_SECTION_DONE)
            {
-             unsigned int len = strlen (name);
+             if (name[1] != 'z')
+               {
+                 unsigned int len = strlen (name);
 
-             new_name = bfd_alloc (abfd, len + 2);
-             if (new_name == NULL)
-               return FALSE;
-             new_name[0] = '.';
-             new_name[1] = 'z';
-             memcpy (new_name + 2, name + 1, len);
+                 new_name = bfd_alloc (abfd, len + 2);
+                 if (new_name == NULL)
+                   return FALSE;
+                 new_name[0] = '.';
+                 new_name[1] = 'z';
+                 memcpy (new_name + 2, name + 1, len);
+               }
            }
-         break;
+         break;
        case decompress:
          if (!bfd_init_section_decompress_status (abfd, return_section))
            {
index 0087a665d87b3357e0ba190acdc347892bd944ca..ad1fbee0133d93d289b59a62f7473171fdc937bc 100644 (file)
@@ -441,7 +441,18 @@ bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
                                         uncompressed_buffer,
                                         uncompressed_size);
 
-  free (uncompressed_buffer);
+  /* PR binutils/18087: If compression didn't make
+     the section smaller, just keep it uncompressed.  */
+  if (ret && uncompressed_size < sec->size)
+    {
+      free (sec->contents);
+      sec->contents = uncompressed_buffer;
+      sec->size = uncompressed_size;
+      sec->compress_status = COMPRESS_SECTION_NONE;
+    }
+  else
+    free (uncompressed_buffer);
+
   return ret;
 #endif
 }
index 13d4272c93620442d15d24fd612af16179a32510..c4defda728dadbaa13086a13324a71efdfd03e35 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -1069,16 +1069,22 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
                 abfd, name);
              return FALSE;
            }
-         if (name[1] != 'z')
+         /* PR binutils/18087: Compression does not always make a section
+            smaller.  So only rename the section when compression has
+            actually taken place.  */
+         if (newsect->compress_status == COMPRESS_SECTION_DONE)
            {
-             unsigned int len = strlen (name);
-
-             new_name = bfd_alloc (abfd, len + 2);
-             if (new_name == NULL)
-               return FALSE;
-             new_name[0] = '.';
-             new_name[1] = 'z';
-             memcpy (new_name + 2, name + 1, len);
+             if (name[1] != 'z')
+               {
+                 unsigned int len = strlen (name);
+
+                 new_name = bfd_alloc (abfd, len + 2);
+                 if (new_name == NULL)
+                   return FALSE;
+                 new_name[0] = '.';
+                 new_name[1] = 'z';
+                 memcpy (new_name + 2, name + 1, len);
+               }
            }
          break;
        case decompress:
index f59c0d135905d2fc4d88467495cc0cf9acebf908..5c0efb115fa2ae2f341ba883573f74ae81e9d51f 100644 (file)
@@ -1,3 +1,11 @@
+2015-03-18  Jon Turney  <jon.turney@dronecode.org.uk>
+           Nick Clifton  <nickc@redhat.com>
+
+       PR binutils/18087
+       * doc/binutils.texi: Note that when objcopy compresses debug
+       sections the compression is only performed if it makes the section
+       smaller.
+
 2015-03-10  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR binutils/18101
index 22d54bebfede058bf0021e6df30f9a625d65867e..da50163ce4252bc7990b8ff70d881dbb95677dce 100644 (file)
@@ -1844,10 +1844,14 @@ It can also be a useful way of reducing the size of a @option{--just-symbols}
 linker input file.
 
 @item --compress-debug-sections
-Compress DWARF debug sections using zlib.
+Compress DWARF debug sections using zlib.  The debug sections are
+renamed to begin with @samp{.zdebug} instead of @samp{.debug}.  Note -
+if compression would actually make a section @emph{larger} then it is
+not compressed or renamed. 
 
 @item --decompress-debug-sections
-Decompress DWARF debug sections using zlib.
+Decompress DWARF debug sections using zlib.  The original section
+names of the compressed sections are restored.
 
 @item -V
 @itemx --version
index bccf7b30d68ffd00c397a80801682323fc8fb0c1..7bf7794f5db54db11f51640e343925f7f41e5546 100644 (file)
@@ -1,3 +1,12 @@
+2015-03-18  Jon Turney  <jon.turney@dronecode.org.uk>
+           Nick Clifton  <nickc@redhat.com>
+
+       PR binutils/18087
+       * doc/as.texinfo: Note that when gas compresses debug sections the
+       compression is only performed if it makes the section smaller.
+       * write.c (compress_debug): Do not compress a debug section if
+       doing so would make it larger.
+
 2015-03-17  Ganesh Gopalasubramanian  <Ganesh.Gopalasubramanian@amd.com>
 
        * config/tc-i386.c (cpu_arch): Add PROCESSOR_ZNVER flags.
index 36c2207c3e00acd775a4ac51394098fd75790e41..bedb4d5a54a4f214c9b7755a5ef44028e0d0ed40 100644 (file)
@@ -627,7 +627,8 @@ Begin in alternate macro mode.
 @item --compress-debug-sections
 Compress DWARF debug sections using zlib.  The debug sections are renamed
 to begin with @samp{.zdebug}, and the resulting object file may not be
-compatible with older linkers and object file utilities.
+compatible with older linkers and object file utilities.  Note if compression
+would make a given section @emph{larger} then it is not compressed or renamed.
 
 @item --nocompress-debug-sections
 Do not compress DWARF debug sections.  This is the default.
index 3b58c97f6111cd5b9536c4f3440e5d1686fc5937..5b1608521ef03f365b8f4d2d1b840d5e9ded72d3 100644 (file)
@@ -1,3 +1,10 @@
+2015-03-18  Jon Turney  <jon.turney@dronecode.org.uk>
+           Nick Clifton  <nickc@redhat.com>
+
+       PR binutils/18087
+       * gas/i386/dw2-compress-1.d: Do not expect the .debug_abbrev or
+       .debug_info sections to be compressed.
+
 2015-03-17  Ganesh Gopalasubramanian  <Ganesh.Gopalasubramanian@amd.com>
 
        * gas/i386/i386.exp: Add new znver1 test cases.
index 4d1a393a2a816551a06bb022d38605f851c7c0c3..2b2fd8d92843a35ef7ed954c28761d9eaebb5d58 100644 (file)
@@ -2,7 +2,7 @@
 #readelf: -w
 #name: DWARF2 debugging information 1
 
-Contents of the .zdebug_info section:
+Contents of the .debug_info section:
 
   Compilation Unit @ offset 0x0:
    Length:        0x4e \(32-bit\)
@@ -31,7 +31,7 @@ Contents of the .zdebug_info section:
     <50>   DW_AT_encoding    : 5       \(signed\)
  <1><51>: Abbrev Number: 0
 
-Contents of the .zdebug_abbrev section:
+Contents of the .debug_abbrev section:
 
   Number TAG \(0x0\)
    1      DW_TAG_compile_unit    \[has children\]
index 949ae921dfe39c364795f4d5f3b307941f44841c..e3570acace90aabd9ab2ece58d6a78bf62f3f85f 100644 (file)
@@ -1526,6 +1526,11 @@ compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
        break;
     }
 
+  /* PR binutils/18087: If compression didn't make
+     the section smaller, just keep it uncompressed.  */
+  if (compressed_size > sec->size)
+    return;
+  
   /* Replace the uncompressed frag list with the compressed frag list.  */
   seginfo->frchainP->frch_root = first_newf;
   seginfo->frchainP->frch_last = last_newf;