2010-07-04 04:52:24 +08:00
|
|
|
/* Compressed section support (intended for debug sections).
|
2017-01-02 11:36:43 +08:00
|
|
|
Copyright (C) 2008-2017 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. */
|
2015-04-25 00:13:22 +08:00
|
|
|
/* PR 18313: The state field in the z_stream structure is supposed
|
|
|
|
to be invisible to the user (ie us), but some compilers will
|
|
|
|
still complain about it being used without initialisation. So
|
|
|
|
we first zero the entire z_stream structure and then set the fields
|
|
|
|
that we need. */
|
|
|
|
memset (& strm, 0, sizeof strm);
|
2015-05-15 06:58:51 +08:00
|
|
|
strm.avail_in = compressed_size;
|
|
|
|
strm.next_in = (Bytef*) compressed_buffer;
|
2010-10-29 20:10:39 +08:00
|
|
|
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-24 07:37:44 +08:00
|
|
|
bfd_size_type uncompressed_size)
|
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
|
|
|
int zlib_size = 0;
|
2015-04-08 22:53:54 +08:00
|
|
|
int orig_compression_header_size;
|
2015-05-15 06:58:51 +08:00
|
|
|
bfd_size_type orig_uncompressed_size;
|
|
|
|
int header_size = bfd_get_compression_header_size (abfd, NULL);
|
2015-04-08 22:53:54 +08:00
|
|
|
bfd_boolean compressed
|
|
|
|
= bfd_is_section_compressed_with_header (abfd, sec,
|
2015-05-15 06:58:51 +08:00
|
|
|
&orig_compression_header_size,
|
|
|
|
&orig_uncompressed_size);
|
|
|
|
|
|
|
|
/* Either ELF compression header or the 12-byte, "ZLIB" + 8-byte size,
|
|
|
|
overhead in .zdebug* section. */
|
|
|
|
if (!header_size)
|
|
|
|
header_size = 12;
|
2015-04-08 22:53:54 +08:00
|
|
|
|
|
|
|
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
|
2015-05-15 06:58:51 +08:00
|
|
|
size first. We need to substract the 12-byte overhead in
|
|
|
|
.zdebug* section. Set orig_compression_header_size to
|
|
|
|
the 12-bye overhead. */
|
|
|
|
orig_compression_header_size = 12;
|
|
|
|
zlib_size = uncompressed_size - 12;
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-15 06:58:51 +08:00
|
|
|
/* Convert it to .zdebug* section. */
|
2015-04-08 22:53:54 +08:00
|
|
|
zlib_size = uncompressed_size - orig_compression_header_size;
|
|
|
|
}
|
2015-05-15 06:58:51 +08:00
|
|
|
|
|
|
|
/* Add the header size. */
|
|
|
|
compressed_size = zlib_size + header_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
|
|
|
else
|
2015-05-15 06:58:51 +08:00
|
|
|
compressed_size = compressBound (uncompressed_size) + header_size;
|
2011-03-07 02:37:07 +08:00
|
|
|
|
2015-05-15 06:58:51 +08:00
|
|
|
/* Uncompress if it leads to smaller size. */
|
|
|
|
if (compressed && compressed_size > orig_uncompressed_size)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-04-08 22:53:54 +08:00
|
|
|
decompress = TRUE;
|
2015-05-15 06:58:51 +08:00
|
|
|
buffer_size = orig_uncompressed_size;
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
2015-04-08 22:53:54 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
decompress = FALSE;
|
2015-05-15 06:58:51 +08:00
|
|
|
buffer_size = compressed_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
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-05-15 06:58:51 +08:00
|
|
|
sec->size = orig_uncompressed_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
if (decompress)
|
|
|
|
{
|
2015-05-15 06:58:51 +08:00
|
|
|
if (!decompress_contents (uncompressed_buffer
|
|
|
|
+ orig_compression_header_size,
|
|
|
|
zlib_size, buffer, buffer_size))
|
2015-04-08 22:53:54 +08:00
|
|
|
{
|
|
|
|
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;
|
2015-05-15 06:58:51 +08:00
|
|
|
return orig_uncompressed_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bfd_update_compression_header (abfd, buffer, sec);
|
2015-05-15 06:58:51 +08:00
|
|
|
memmove (buffer + header_size,
|
2015-04-08 22:53:54 +08:00
|
|
|
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
|
|
|
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-24 07:37:44 +08:00
|
|
|
just keep it uncompressed. */
|
|
|
|
if (compressed_size < uncompressed_size)
|
2015-05-15 06:58:51 +08:00
|
|
|
bfd_update_compression_header (abfd, buffer, sec);
|
2015-04-08 22:53:54 +08:00
|
|
|
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)
|
2016-11-10 20:26:53 +08:00
|
|
|
{
|
|
|
|
/* PR 20801: Provide a more helpful error message. */
|
|
|
|
if (bfd_get_error () == bfd_error_no_memory)
|
|
|
|
_bfd_error_handler
|
|
|
|
/* xgettext:c-format */
|
|
|
|
(_("error: %B(%A) is too large (%#lx bytes)"),
|
|
|
|
abfd, sec, (long) sz);
|
2010-10-29 20:10:39 +08:00
|
|
|
return FALSE;
|
2016-11-10 20:26:53 +08:00
|
|
|
}
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
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);
|
2015-05-15 06:58:51 +08:00
|
|
|
if (compression_header_size == 0)
|
|
|
|
/* Set header size to the zlib header size if it is a
|
|
|
|
SHF_COMPRESSED section. */
|
|
|
|
compression_header_size = 12;
|
2015-04-08 22:53:54 +08:00
|
|
|
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,
|
2015-05-15 06:58:51 +08:00
|
|
|
int *compression_header_size_p,
|
|
|
|
bfd_size_type *uncompressed_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
|
2015-05-15 06:58:51 +08:00
|
|
|
header size is returned in @var{compression_header_size_p} and
|
|
|
|
uncompressed size is returned in @var{uncompressed_size_p}. If
|
2015-04-08 22:53:54 +08:00
|
|
|
compression is unsupported, compression header size is returned
|
2015-05-15 06:58:51 +08:00
|
|
|
with -1 and uncompressed size is returned with 0.
|
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,
|
2015-05-15 06:58:51 +08:00
|
|
|
int *compression_header_size_p,
|
|
|
|
bfd_size_type *uncompressed_size_p)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-05-15 06:58:51 +08:00
|
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
|
2015-04-08 22:53:54 +08:00
|
|
|
int compression_header_size;
|
2015-05-15 06:58:51 +08:00
|
|
|
int header_size;
|
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 ();
|
2015-05-15 06:58:51 +08:00
|
|
|
header_size = compression_header_size ? compression_header_size : 12;
|
2015-04-08 22:53:54 +08:00
|
|
|
|
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
|
|
|
|
2015-05-15 06:58:51 +08:00
|
|
|
/* Read the header. */
|
|
|
|
if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
|
|
|
|
{
|
|
|
|
if (compression_header_size == 0)
|
|
|
|
/* In this case, it should be "ZLIB" followed by the uncompressed
|
|
|
|
section size, 8 bytes in big-endian order. */
|
|
|
|
compressed = CONST_STRNEQ ((char*) header , "ZLIB");
|
|
|
|
else
|
|
|
|
compressed = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
compressed = FALSE;
|
2012-10-25 05:36:50 +08:00
|
|
|
|
2015-05-15 06:58:51 +08:00
|
|
|
*uncompressed_size_p = sec->size;
|
2015-04-08 22:53:54 +08:00
|
|
|
if (compressed)
|
|
|
|
{
|
|
|
|
if (compression_header_size != 0)
|
|
|
|
{
|
|
|
|
if (!bfd_check_compression_header (abfd, header, sec,
|
2015-05-15 06:58:51 +08:00
|
|
|
uncompressed_size_p))
|
2015-04-08 22:53:54 +08:00
|
|
|
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
|
2015-05-15 06:58:51 +08:00
|
|
|
&& ISPRINT (header[4]))
|
2015-04-08 22:53:54 +08:00
|
|
|
compressed = FALSE;
|
2015-05-15 06:58:51 +08:00
|
|
|
else
|
|
|
|
*uncompressed_size_p = bfd_getb64 (header + 4);
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
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;
|
2015-05-15 06:58:51 +08:00
|
|
|
bfd_size_type uncompressed_size;
|
2015-04-08 22:53:54 +08:00
|
|
|
return (bfd_is_section_compressed_with_header (abfd, sec,
|
2015-05-15 06:58:51 +08:00
|
|
|
&compression_header_size,
|
|
|
|
&uncompressed_size)
|
|
|
|
&& compression_header_size >= 0
|
|
|
|
&& uncompressed_size > 0);
|
2015-04-08 22:53:54 +08:00
|
|
|
}
|
|
|
|
|
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-05-15 06:58:51 +08:00
|
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
|
2015-04-08 22:53:54 +08:00
|
|
|
int compression_header_size;
|
2015-05-15 06:58:51 +08:00
|
|
|
int header_size;
|
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 ();
|
2015-05-15 06:58:51 +08:00
|
|
|
header_size = compression_header_size ? compression_header_size : 12;
|
2015-04-08 22:53:54 +08:00
|
|
|
|
2015-05-15 06:58:51 +08:00
|
|
|
/* Read the header. */
|
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
|
|
|
|
2015-05-15 06:58:51 +08:00
|
|
|
if (compression_header_size == 0)
|
2010-10-29 20:10:39 +08:00
|
|
|
{
|
2015-05-15 06:58:51 +08:00
|
|
|
/* In this case, it should be "ZLIB" followed by the uncompressed
|
|
|
|
section size, 8 bytes in big-endian order. */
|
|
|
|
if (! CONST_STRNEQ ((char*) header, "ZLIB"))
|
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_wrong_format);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
uncompressed_size = bfd_getb64 (header + 4);
|
2010-10-29 20:10:39 +08:00
|
|
|
}
|
2015-05-15 06:58:51 +08:00
|
|
|
else if (!bfd_check_compression_header (abfd, header, sec,
|
|
|
|
&uncompressed_size))
|
2015-04-08 22:53:54 +08:00
|
|
|
{
|
|
|
|
bfd_set_error (bfd_error_wrong_format);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-05-15 06:58:51 +08:00
|
|
|
|
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-24 07:37:44 +08:00
|
|
|
uncompressed_size);
|
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,
|
2015-04-24 07:37:44 +08:00
|
|
|
uncompressed_size) != 0;
|
2015-04-15 13:01:25 +08:00
|
|
|
}
|