2010-07-04 04:52:24 +08:00
|
|
|
/* Compressed section support (intended for debug sections).
|
2015-01-01 22:15:26 +08:00
|
|
|
Copyright (C) 2008-2015 Free Software Foundation, Inc.
|
2008-07-10 09:32:23 +08:00
|
|
|
|
|
|
|
This file is part of BFD, the Binary File Descriptor library.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA. */
|
|
|
|
|
|
|
|
#include "sysdep.h"
|
2015-03-29 22:12:38 +08:00
|
|
|
#include <zlib.h>
|
2008-07-10 09:32:23 +08:00
|
|
|
#include "bfd.h"
|
|
|
|
#include "libbfd.h"
|
2014-05-01 00:04:04 +08:00
|
|
|
#include "safe-ctype.h"
|
2008-07-10 09:32:23 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
#define MAX_COMPRESSION_HEADER_SIZE 24
|
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
static bfd_boolean
|
|
|
|
decompress_contents (bfd_byte *compressed_buffer,
|
|
|
|
bfd_size_type compressed_size,
|
|
|
|
bfd_byte *uncompressed_buffer,
|
|
|
|
bfd_size_type uncompressed_size)
|
|
|
|
{
|
|
|
|
z_stream strm;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* It is possible the section consists of several compressed
|
|
|
|
buffers concatenated together, so we uncompress in a loop. */
|
|
|
|
strm.zalloc = NULL;
|
|
|
|
strm.zfree = NULL;
|
|
|
|
strm.opaque = NULL;
|
|
|
|
strm.avail_in = compressed_size - 12;
|
|
|
|
strm.next_in = (Bytef*) compressed_buffer + 12;
|
|
|
|
strm.avail_out = uncompressed_size;
|
|
|
|
|
2013-04-17 22:16:01 +08:00
|
|
|
BFD_ASSERT (Z_OK == 0);
|
2010-10-29 20:10:39 +08:00
|
|
|
rc = inflateInit (&strm);
|
2012-10-19 01:42:29 +08:00
|
|
|
while (strm.avail_in > 0 && strm.avail_out > 0)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
|
|
|
if (rc != Z_OK)
|
2013-04-17 22:16:01 +08:00
|
|
|
break;
|
2010-10-29 20:10:39 +08:00
|
|
|
strm.next_out = ((Bytef*) uncompressed_buffer
|
|
|
|
+ (uncompressed_size - strm.avail_out));
|
|
|
|
rc = inflate (&strm, Z_FINISH);
|
|
|
|
if (rc != Z_STREAM_END)
|
2013-04-17 22:16:01 +08:00
|
|
|
break;
|
2010-10-29 20:10:39 +08:00
|
|
|
rc = inflateReset (&strm);
|
|
|
|
}
|
2013-04-17 22:16:01 +08:00
|
|
|
rc |= inflateEnd (&strm);
|
2010-12-24 18:40:19 +08:00
|
|
|
return rc == Z_OK && strm.avail_out == 0;
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 02:20:06 +08:00
|
|
|
/* Compress data of the size specified in @var{uncompressed_size}
|
|
|
|
and pointed to by @var{uncompressed_buffer} using zlib and store
|
|
|
|
as the contents field. This function assumes the contents
|
2015-04-10 03:48:49 +08:00
|
|
|
field was allocated using bfd_malloc() or equivalent.
|
2008-07-10 09:32:23 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
Return the uncompressed size if the full section contents is
|
|
|
|
compressed successfully. Otherwise return 0. */
|
2008-07-10 09:32:23 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
static bfd_size_type
|
|
|
|
bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
|
2015-03-29 22:12:38 +08:00
|
|
|
bfd_byte *uncompressed_buffer,
|
2015-04-15 13:01:25 +08:00
|
|
|
bfd_size_type uncompressed_size,
|
|
|
|
bfd_boolean write_compress)
|
2008-07-10 09:32:23 +08:00
|
|
|
{
|
2010-10-30 03:56:00 +08:00
|
|
|
uLong compressed_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_byte *buffer;
|
|
|
|
bfd_size_type buffer_size;
|
|
|
|
bfd_boolean decompress;
|
2015-04-09 00:25:08 +08:00
|
|
|
#if defined(__GNUC__) && GCC_VERSION < 4007
|
|
|
|
/* Work around a GCC uninitialized warning bug fixed in GCC 4.7. */
|
|
|
|
int zlib_size = 0;
|
|
|
|
#else
|
2015-04-08 22:53:54 +08:00
|
|
|
int zlib_size;
|
2015-04-09 00:25:08 +08:00
|
|
|
#endif
|
2015-04-08 22:53:54 +08:00
|
|
|
int orig_compression_header_size;
|
|
|
|
int compression_header_size
|
|
|
|
= bfd_get_compression_header_size (abfd, NULL);
|
|
|
|
bfd_boolean compressed
|
|
|
|
= bfd_is_section_compressed_with_header (abfd, sec,
|
|
|
|
&orig_compression_header_size);
|
|
|
|
|
|
|
|
if (compressed)
|
|
|
|
{
|
|
|
|
/* We shouldn't decompress unsupported compressed section. */
|
|
|
|
if (orig_compression_header_size < 0)
|
|
|
|
abort ();
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
/* Different compression schemes. Just move the compressed section
|
|
|
|
contents to the right position. */
|
|
|
|
if (orig_compression_header_size == 0)
|
|
|
|
{
|
|
|
|
/* Convert it from .zdebug* section. Get the uncompressed
|
|
|
|
size first. */
|
|
|
|
zlib_size = uncompressed_size;
|
|
|
|
compressed_size = zlib_size + compression_header_size;
|
|
|
|
uncompressed_size = bfd_getb64 (uncompressed_buffer + 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert it to .zdebug* section. */
|
|
|
|
zlib_size = uncompressed_size - orig_compression_header_size;
|
|
|
|
compressed_size = zlib_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
compressed_size = compressBound (uncompressed_size) + 12;
|
2011-03-07 02:37:07 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
/* When converting from .zdebug* section, uncompress if it leads to
|
|
|
|
smaller size. */
|
|
|
|
if (compressed
|
|
|
|
&& orig_compression_header_size == 0
|
|
|
|
&& compressed_size > uncompressed_size)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
decompress = TRUE;
|
|
|
|
buffer_size = uncompressed_size;
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
2015-04-08 22:53:54 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
decompress = FALSE;
|
|
|
|
buffer_size = compressed_size + compression_header_size;
|
|
|
|
}
|
2015-04-10 18:54:41 +08:00
|
|
|
buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
|
2015-04-08 22:53:54 +08:00
|
|
|
if (buffer == NULL)
|
|
|
|
return 0;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
if (compressed)
|
2015-03-25 04:27:52 +08:00
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
sec->size = uncompressed_size;
|
|
|
|
if (decompress)
|
|
|
|
{
|
|
|
|
if (!decompress_contents (uncompressed_buffer, zlib_size,
|
|
|
|
buffer, uncompressed_size))
|
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_bad_value);
|
2015-04-10 18:54:41 +08:00
|
|
|
bfd_release (abfd, buffer);
|
2015-04-08 22:53:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free (uncompressed_buffer);
|
|
|
|
sec->contents = buffer;
|
|
|
|
sec->compress_status = COMPRESS_SECTION_DONE;
|
|
|
|
return uncompressed_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bfd_update_compression_header (abfd, buffer, sec);
|
|
|
|
memmove (buffer + compression_header_size,
|
|
|
|
uncompressed_buffer + orig_compression_header_size,
|
|
|
|
zlib_size);
|
|
|
|
}
|
2015-03-25 04:27:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_size_type size = uncompressed_size;
|
|
|
|
int header_size = 12 + compression_header_size;
|
|
|
|
if (compress ((Bytef*) buffer + header_size,
|
|
|
|
&compressed_size,
|
|
|
|
(const Bytef*) uncompressed_buffer,
|
|
|
|
uncompressed_size) != Z_OK)
|
|
|
|
{
|
2015-04-10 18:54:41 +08:00
|
|
|
bfd_release (abfd, buffer);
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_set_error (bfd_error_bad_value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
compressed_size += header_size;
|
|
|
|
/* PR binutils/18087: If compression didn't make the section smaller,
|
2015-04-15 13:01:25 +08:00
|
|
|
just keep it uncompressed. We always generate .zdebug_* section
|
|
|
|
when linking since section names have been finalized and they
|
|
|
|
can't be changed easily. */
|
|
|
|
if ((write_compress && compression_header_size == 0)
|
|
|
|
|| compressed_size < uncompressed_size)
|
2015-04-08 22:53:54 +08:00
|
|
|
{
|
|
|
|
bfd_update_compression_header (abfd, buffer, sec);
|
|
|
|
|
|
|
|
/* Write the zlib header. In this case, it should be "ZLIB"
|
|
|
|
followed by the uncompressed section size, 8 bytes in
|
|
|
|
big-endian order. */
|
|
|
|
memcpy (buffer + compression_header_size, "ZLIB", 4);
|
|
|
|
bfd_putb64 (size, buffer + compression_header_size + 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-10 18:54:41 +08:00
|
|
|
/* NOTE: There is a small memory leak here since
|
|
|
|
uncompressed_buffer is malloced and won't be freed. */
|
|
|
|
bfd_release (abfd, buffer);
|
2015-04-08 22:53:54 +08:00
|
|
|
sec->contents = uncompressed_buffer;
|
|
|
|
sec->compress_status = COMPRESS_SECTION_NONE;
|
|
|
|
return uncompressed_size;
|
|
|
|
}
|
2015-03-25 04:27:52 +08:00
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
free (uncompressed_buffer);
|
|
|
|
sec->contents = buffer;
|
|
|
|
sec->size = compressed_size;
|
|
|
|
sec->compress_status = COMPRESS_SECTION_DONE;
|
|
|
|
|
|
|
|
return uncompressed_size;
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_get_full_section_contents
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
bfd_boolean bfd_get_full_section_contents
|
|
|
|
(bfd *abfd, asection *section, bfd_byte **ptr);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Read all data from @var{section} in BFD @var{abfd}, decompress
|
|
|
|
if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
|
2013-01-11 04:03:55 +08:00
|
|
|
return @var{*ptr} with memory malloc'd by this function.
|
2010-10-29 20:10:39 +08:00
|
|
|
|
|
|
|
Return @code{TRUE} if the full section contents is retrieved
|
2014-12-02 00:43:46 +08:00
|
|
|
successfully. If the section has no contents then this function
|
|
|
|
returns @code{TRUE} but @var{*ptr} is set to NULL.
|
2010-10-29 20:10:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
|
|
|
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
|
|
|
|
{
|
2011-04-11 12:08:13 +08:00
|
|
|
bfd_size_type sz;
|
2010-10-29 20:10:39 +08:00
|
|
|
bfd_byte *p = *ptr;
|
2010-12-24 18:40:19 +08:00
|
|
|
bfd_boolean ret;
|
2012-10-20 16:27:13 +08:00
|
|
|
bfd_size_type save_size;
|
|
|
|
bfd_size_type save_rawsize;
|
2010-10-29 20:10:39 +08:00
|
|
|
bfd_byte *compressed_buffer;
|
2015-04-08 22:53:54 +08:00
|
|
|
unsigned int compression_header_size;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2011-04-11 12:08:13 +08:00
|
|
|
if (abfd->direction != write_direction && sec->rawsize != 0)
|
|
|
|
sz = sec->rawsize;
|
|
|
|
else
|
|
|
|
sz = sec->size;
|
2010-10-29 20:10:39 +08:00
|
|
|
if (sz == 0)
|
2014-12-02 00:43:46 +08:00
|
|
|
{
|
|
|
|
*ptr = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
|
|
|
|
switch (sec->compress_status)
|
|
|
|
{
|
|
|
|
case COMPRESS_SECTION_NONE:
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
2011-12-15 00:12:13 +08:00
|
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
2010-10-29 20:10:39 +08:00
|
|
|
if (p == NULL)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-12-02 00:43:46 +08:00
|
|
|
|
2010-12-24 18:40:19 +08:00
|
|
|
if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
|
|
|
|
{
|
|
|
|
if (*ptr != p)
|
|
|
|
free (p);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
*ptr = p;
|
2010-10-29 20:10:39 +08:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case DECOMPRESS_SECTION_SIZED:
|
2010-12-24 18:40:19 +08:00
|
|
|
/* Read in the full compressed section contents. */
|
2012-10-20 16:27:13 +08:00
|
|
|
compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
|
2010-12-24 18:40:19 +08:00
|
|
|
if (compressed_buffer == NULL)
|
|
|
|
return FALSE;
|
2012-10-20 16:27:13 +08:00
|
|
|
save_rawsize = sec->rawsize;
|
|
|
|
save_size = sec->size;
|
2010-12-24 18:40:19 +08:00
|
|
|
/* Clear rawsize, set size to compressed size and set compress_status
|
|
|
|
to COMPRESS_SECTION_NONE. If the compressed size is bigger than
|
|
|
|
the uncompressed size, bfd_get_section_contents will fail. */
|
|
|
|
sec->rawsize = 0;
|
2012-10-20 16:27:13 +08:00
|
|
|
sec->size = sec->compressed_size;
|
2010-12-24 18:40:19 +08:00
|
|
|
sec->compress_status = COMPRESS_SECTION_NONE;
|
|
|
|
ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
|
2012-10-20 16:27:13 +08:00
|
|
|
0, sec->compressed_size);
|
2010-12-24 18:40:19 +08:00
|
|
|
/* Restore rawsize and size. */
|
2012-10-20 16:27:13 +08:00
|
|
|
sec->rawsize = save_rawsize;
|
|
|
|
sec->size = save_size;
|
2010-10-29 20:10:39 +08:00
|
|
|
sec->compress_status = DECOMPRESS_SECTION_SIZED;
|
2010-12-24 18:40:19 +08:00
|
|
|
if (!ret)
|
|
|
|
goto fail_compressed;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2012-10-20 16:27:13 +08:00
|
|
|
if (p == NULL)
|
|
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
|
|
|
if (p == NULL)
|
2010-10-29 20:10:39 +08:00
|
|
|
goto fail_compressed;
|
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
|
|
if (!decompress_contents (compressed_buffer + compression_header_size,
|
|
|
|
sec->compressed_size, p, sz))
|
2010-12-24 18:40:19 +08:00
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_bad_value);
|
2012-10-20 16:27:13 +08:00
|
|
|
if (p != *ptr)
|
|
|
|
free (p);
|
2010-12-24 18:40:19 +08:00
|
|
|
fail_compressed:
|
|
|
|
free (compressed_buffer);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2010-12-24 18:40:19 +08:00
|
|
|
free (compressed_buffer);
|
2012-10-20 16:27:13 +08:00
|
|
|
*ptr = p;
|
|
|
|
return TRUE;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2010-12-24 18:40:19 +08:00
|
|
|
case COMPRESS_SECTION_DONE:
|
2014-12-04 03:50:48 +08:00
|
|
|
if (sec->contents == NULL)
|
|
|
|
return FALSE;
|
2010-12-24 18:40:19 +08:00
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
|
|
|
if (p == NULL)
|
|
|
|
return FALSE;
|
|
|
|
*ptr = p;
|
|
|
|
}
|
2014-12-02 00:43:46 +08:00
|
|
|
/* PR 17512; file: 5bc29788. */
|
|
|
|
if (p != sec->contents)
|
|
|
|
memcpy (p, sec->contents, sz);
|
2010-12-24 18:40:19 +08:00
|
|
|
return TRUE;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
2010-12-24 18:40:19 +08:00
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
|
|
|
|
2012-10-21 17:06:07 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_cache_section_contents
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
void bfd_cache_section_contents
|
|
|
|
(asection *sec, void *contents);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Stash @var(contents) so any following reads of @var(sec) do
|
|
|
|
not need to decompress again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
bfd_cache_section_contents (asection *sec, void *contents)
|
|
|
|
{
|
|
|
|
if (sec->compress_status == DECOMPRESS_SECTION_SIZED)
|
|
|
|
sec->compress_status = COMPRESS_SECTION_DONE;
|
|
|
|
sec->contents = contents;
|
|
|
|
sec->flags |= SEC_IN_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_is_section_compressed_with_header
|
2010-10-29 20:10:39 +08:00
|
|
|
|
|
|
|
SYNOPSIS
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_boolean bfd_is_section_compressed_with_header
|
|
|
|
(bfd *abfd, asection *section,
|
|
|
|
int *compression_header_size_p);
|
2010-10-29 20:10:39 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
2015-04-08 22:53:54 +08:00
|
|
|
Return @code{TRUE} if @var{section} is compressed. Compression
|
|
|
|
header size is returned in @var{compression_header_size_p}. If
|
|
|
|
compression is unsupported, compression header size is returned
|
|
|
|
with -1.
|
2010-10-29 20:10:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
|
|
|
|
int *compression_header_size_p)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
|
|
|
|
int compression_header_size;
|
|
|
|
int header_size = 12;
|
2012-10-25 05:36:50 +08:00
|
|
|
unsigned int saved = sec->compress_status;
|
|
|
|
bfd_boolean compressed;
|
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
|
|
if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
|
|
|
|
abort ();
|
|
|
|
header_size += compression_header_size;
|
|
|
|
|
2012-10-25 05:36:50 +08:00
|
|
|
/* Don't decompress the section. */
|
|
|
|
sec->compress_status = COMPRESS_SECTION_NONE;
|
2010-10-29 20:10:39 +08:00
|
|
|
|
|
|
|
/* Read the zlib header. In this case, it should be "ZLIB" followed
|
|
|
|
by the uncompressed section size, 8 bytes in big-endian order. */
|
2015-04-08 22:53:54 +08:00
|
|
|
compressed = bfd_get_section_contents (abfd, sec, header, 0,
|
|
|
|
header_size)
|
|
|
|
&& CONST_STRNEQ ((char*) header + compression_header_size,
|
|
|
|
"ZLIB");
|
2012-10-25 05:36:50 +08:00
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
if (compressed)
|
|
|
|
{
|
|
|
|
if (compression_header_size != 0)
|
|
|
|
{
|
|
|
|
bfd_size_type uncompressed_size
|
|
|
|
= bfd_getb64 ((bfd_byte *) header
|
|
|
|
+ compression_header_size + 4);
|
|
|
|
if (!bfd_check_compression_header (abfd, header, sec,
|
|
|
|
uncompressed_size))
|
|
|
|
compression_header_size = -1;
|
|
|
|
}
|
|
|
|
/* Check for the pathalogical case of a debug string section that
|
|
|
|
contains the string ZLIB.... as the first entry. We assume that
|
|
|
|
no uncompressed .debug_str section would ever be big enough to
|
|
|
|
have the first byte of its (big-endian) size be non-zero. */
|
|
|
|
else if (strcmp (sec->name, ".debug_str") == 0
|
|
|
|
&& ISPRINT (header[compression_header_size + 4]))
|
|
|
|
compressed = FALSE;
|
|
|
|
}
|
2014-05-01 00:04:04 +08:00
|
|
|
|
2012-10-25 05:36:50 +08:00
|
|
|
/* Restore compress_status. */
|
|
|
|
sec->compress_status = saved;
|
2015-04-08 22:53:54 +08:00
|
|
|
*compression_header_size_p = compression_header_size;
|
2012-10-25 05:36:50 +08:00
|
|
|
return compressed;
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_is_section_compressed
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
bfd_boolean bfd_is_section_compressed
|
|
|
|
(bfd *abfd, asection *section);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Return @code{TRUE} if @var{section} is compressed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
|
|
|
bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
|
|
|
|
{
|
|
|
|
int compression_header_size;
|
|
|
|
return (bfd_is_section_compressed_with_header (abfd, sec,
|
|
|
|
&compression_header_size)
|
|
|
|
&& compression_header_size >= 0);
|
|
|
|
}
|
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_init_section_decompress_status
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
bfd_boolean bfd_init_section_decompress_status
|
|
|
|
(bfd *abfd, asection *section);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Record compressed section size, update section size with
|
|
|
|
decompressed size and set compress_status to
|
|
|
|
DECOMPRESS_SECTION_SIZED.
|
|
|
|
|
|
|
|
Return @code{FALSE} if the section is not a valid compressed
|
2015-04-10 03:48:49 +08:00
|
|
|
section. Otherwise, return @code{TRUE}.
|
2010-10-29 20:10:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
2015-03-29 22:12:38 +08:00
|
|
|
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE + 12];
|
|
|
|
int compression_header_size;
|
|
|
|
int header_size = 12;
|
2010-10-29 20:10:39 +08:00
|
|
|
bfd_size_type uncompressed_size;
|
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
|
|
if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
|
|
|
|
abort ();
|
|
|
|
header_size += compression_header_size;
|
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
if (sec->rawsize != 0
|
|
|
|
|| sec->contents != NULL
|
|
|
|
|| sec->compress_status != COMPRESS_SECTION_NONE
|
2015-04-08 22:53:54 +08:00
|
|
|
|| !bfd_get_section_contents (abfd, sec, header, 0, header_size))
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_invalid_operation);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-07-10 09:32:23 +08:00
|
|
|
|
|
|
|
/* Read the zlib header. In this case, it should be "ZLIB" followed
|
|
|
|
by the uncompressed section size, 8 bytes in big-endian order. */
|
2015-04-08 22:53:54 +08:00
|
|
|
if (! CONST_STRNEQ ((char*) header + compression_header_size, "ZLIB"))
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_wrong_format);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:53:54 +08:00
|
|
|
uncompressed_size = bfd_getb64 (header + compression_header_size + 4);
|
|
|
|
if (compression_header_size != 0
|
|
|
|
&& !bfd_check_compression_header (abfd, header, sec,
|
|
|
|
uncompressed_size))
|
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_wrong_format);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
sec->compressed_size = sec->size;
|
|
|
|
sec->size = uncompressed_size;
|
|
|
|
sec->compress_status = DECOMPRESS_SECTION_SIZED;
|
2008-07-10 09:32:23 +08:00
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_init_section_compress_status
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
bfd_boolean bfd_init_section_compress_status
|
|
|
|
(bfd *abfd, asection *section);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
If open for read, compress section, update section size with
|
|
|
|
compressed size and set compress_status to COMPRESS_SECTION_DONE.
|
|
|
|
|
|
|
|
Return @code{FALSE} if the section is not a valid compressed
|
2015-04-10 03:48:49 +08:00
|
|
|
section. Otherwise, return @code{TRUE}.
|
2010-10-29 20:10:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
2015-03-29 22:12:38 +08:00
|
|
|
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
|
|
|
bfd_size_type uncompressed_size;
|
|
|
|
bfd_byte *uncompressed_buffer;
|
|
|
|
bfd_boolean ret;
|
|
|
|
|
|
|
|
/* Error if not opened for read. */
|
|
|
|
if (abfd->direction != read_direction
|
|
|
|
|| sec->size == 0
|
|
|
|
|| sec->rawsize != 0
|
|
|
|
|| sec->contents != NULL
|
|
|
|
|| sec->compress_status != COMPRESS_SECTION_NONE)
|
2008-07-10 09:32:23 +08:00
|
|
|
{
|
2010-10-29 20:10:39 +08:00
|
|
|
bfd_set_error (bfd_error_invalid_operation);
|
|
|
|
return FALSE;
|
2008-07-10 09:32:23 +08:00
|
|
|
}
|
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
/* Read in the full section contents and compress it. */
|
|
|
|
uncompressed_size = sec->size;
|
|
|
|
uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
|
|
|
|
if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
|
|
|
|
0, uncompressed_size))
|
|
|
|
ret = FALSE;
|
|
|
|
else
|
2015-04-08 22:53:54 +08:00
|
|
|
{
|
|
|
|
uncompressed_size = bfd_compress_section_contents (abfd, sec,
|
|
|
|
uncompressed_buffer,
|
2015-04-15 13:01:25 +08:00
|
|
|
uncompressed_size,
|
|
|
|
FALSE);
|
2015-04-08 22:53:54 +08:00
|
|
|
ret = uncompressed_size != 0;
|
|
|
|
}
|
2008-07-10 09:32:23 +08:00
|
|
|
|
2010-10-29 20:10:39 +08:00
|
|
|
return ret;
|
2008-07-10 09:32:23 +08:00
|
|
|
}
|
2015-04-15 13:01:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
bfd_compress_section
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
bfd_boolean bfd_compress_section
|
|
|
|
(bfd *abfd, asection *section, bfd_byte *uncompressed_buffer);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
If open for write, compress section, update section size with
|
|
|
|
compressed size and set compress_status to COMPRESS_SECTION_DONE.
|
|
|
|
|
|
|
|
Return @code{FALSE} if compression fail. Otherwise, return
|
|
|
|
@code{TRUE}.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bfd_boolean
|
|
|
|
bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer)
|
|
|
|
{
|
|
|
|
bfd_size_type uncompressed_size = sec->size;
|
|
|
|
|
|
|
|
/* Error if not opened for write. */
|
|
|
|
if (abfd->direction != write_direction
|
|
|
|
|| uncompressed_size == 0
|
|
|
|
|| uncompressed_buffer == NULL
|
|
|
|
|| sec->contents != NULL
|
|
|
|
|| sec->compressed_size != 0
|
|
|
|
|| sec->compress_status != COMPRESS_SECTION_NONE)
|
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_invalid_operation);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compress it. */
|
|
|
|
return bfd_compress_section_contents (abfd, sec, uncompressed_buffer,
|
|
|
|
uncompressed_size, TRUE) != 0;
|
|
|
|
}
|