+ decompress = TRUE;
+ buffer_size = orig_uncompressed_size;
+ }
+ else
+ {
+ decompress = FALSE;
+ buffer_size = compressed_size;
+ }
+ buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
+ if (buffer == NULL)
+ return 0;
+
+ if (compressed)
+ {
+ sec->size = orig_uncompressed_size;
+ if (decompress)
+ {
+ if (!decompress_contents (uncompressed_buffer
+ + orig_compression_header_size,
+ zlib_size, buffer, buffer_size))
+ {
+ bfd_set_error (bfd_error_bad_value);
+ bfd_release (abfd, buffer);
+ return 0;
+ }
+ free (uncompressed_buffer);
+ bfd_set_section_alignment (sec, orig_uncompressed_alignment_pow);
+
+ sec->contents = buffer;
+ sec->compress_status = COMPRESS_SECTION_DONE;
+ return orig_uncompressed_size;
+ }
+ else
+ {
+ bfd_update_compression_header (abfd, buffer, sec);
+ memmove (buffer + header_size,
+ uncompressed_buffer + orig_compression_header_size,
+ zlib_size);
+ }
+ }
+ else
+ {
+ if (compress ((Bytef*) buffer + header_size,
+ &compressed_size,
+ (const Bytef*) uncompressed_buffer,
+ uncompressed_size) != Z_OK)
+ {
+ bfd_release (abfd, buffer);
+ bfd_set_error (bfd_error_bad_value);
+ return 0;
+ }
+
+ compressed_size += header_size;
+ /* PR binutils/18087: If compression didn't make the section smaller,
+ just keep it uncompressed. */
+ if (compressed_size < uncompressed_size)
+ bfd_update_compression_header (abfd, buffer, sec);
+ else
+ {
+ /* NOTE: There is a small memory leak here since
+ uncompressed_buffer is malloced and won't be freed. */
+ bfd_release (abfd, buffer);
+ sec->contents = uncompressed_buffer;
+ sec->compress_status = COMPRESS_SECTION_NONE;
+ return uncompressed_size;
+ }