mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-11-21 01:12:32 +08:00
d42c267e9b
This patch uses the new %L _bfd_error_handler support for printing bfd_vma arguments, and fixes a many other format and/or argument errors in error messages. bfd/ * binary.c (binary_set_section_contents): Don't print filepos in error message. (coff_write_object_contents): Cast size_t for error message. (coff_slurp_line_table): Don't use bfd_vma symndx. (coff_slurp_reloc_table): Remove unneeded cast. * dwarf2.c (read_section): Cast bfd_int64_t to long long for error message. (find_abstract_instance_name): Likewise. * elf32-arm.c (arm_type_of_stub): Correct error arg order. (bfd_elf32_arm_stm32l4xx_erratum_scan): Don't cast error arg. (elf32_arm_check_relocs): Make r_symndx an int. * elf32-cris.c (cris_elf_check_relocs): Delete extraneous %s in format string. * elf32-metag.c (elf_metag_relocate_section): Delete extra error message arg. * elf32-nds32.c (nds32_elf_ex9_build_hash_table): Rewrite bogus error message. * elf32-i386.c (elf_i386_check_relocs): Make r_symndx an int. * elf32-s390.c (elf_s390_check_relocs): Likewise. * elf32-tic6x.c (elf32_tic6x_check_relocs): Likewise. * elf32-tilepro.c (tilepro_elf_check_relocs): Likewise. * elf32-xtensa.c (elf_xtensa_check_relocs): Likewise. * elf64-s390.c (elf_s390_check_relocs): Likewise. * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise. * elfnn-aarch64.c (elfNN_aarch64_check_relocs): Likewise. * elfnn-riscv.c (riscv_elf_check_relocs): Likewise. * elfxx-sparc.c (_bfd_sparc_elf_check_relocs): Likewise. * elfxx-tilegx.c (tilegx_elf_check_relocs): Likewise. * elf64-mmix.c (_bfd_mmix_after_linker_allocation): Cast size_t args and use %lu for error message. * elflink.c (elf_link_adjust_relocs): Delete extra error message arg. * mmo.c (mmo_scan): Make stab_loc a file_ptr. Cast expression for error message. * elf32-arm.c (elf32_arm_tls_relax): Correct format string and args in error message. (elf32_arm_final_link_relocate): Likewise. * coff-arm.c (bfd_arm_process_before_allocation): Likewise. * coffcode.h (styp_to_sec_flags): Likewise. * cofflink.c (_bfd_coff_write_global_sym): Likewise. * ecoff.c (_bfd_ecoff_slurp_symbol_table): Likewise. * elf32-arc.c (arc_elf_merge_private_bfd_data): Likewise. * elf32-bfin.c (bfinfdpic_check_relocs): Likewise. (elf32_bfin_merge_private_bfd_data): Likewise. * elf32-cris.c (cris_elf_relocate_section): Likewise. * elf32-frv.c (frv_elf_merge_private_bfd_data): Likewise. * elf32-i370.c (i370_elf_merge_private_bfd_data): Likewise. (i370_elf_relocate_section): Likewise. * elf32-iq2000.c (iq2000_elf_merge_private_bfd_data): Likewise. * elf32-m32c.c (m32c_elf_merge_private_bfd_data): Likewise. * elf32-m68hc1x.c (_bfd_m68hc11_elf_merge_private_bfd_data): Likewise. * elf32-mcore.c (mcore_elf_relocate_section): Likewise. * elf32-mep.c (mep_elf_merge_private_bfd_data): Likewise. * elf32-mt.c (mt_elf_merge_private_bfd_data): Likewise. * elf64-sparc.c (elf64_sparc_merge_private_bfd_data): Likewise. * elfxx-mips.c (mips_elf_merge_obj_e_flags): Likewise. (_bfd_mips_elf_merge_private_bfd_data): Likewise. * ieee.c (ieee_write_id, read_id): Likewise. * mach-o.c (bfd_mach_o_write_contents): Likewise. (bfd_mach_o_layout_commands, bfd_mach_o_read_section_32): Likewise. (bfd_mach_o_read_section_64, bfd_mach_o_read_symtab_symbol): Likewise. (bfd_mach_o_read_command, bfd_mach_o_header_p): Likewise. * peXXigen.c (_bfd_XXi_swap_aouthdr_in): Likewise. * stabs.c (_bfd_link_section_stabs): Likewise. * coff-arm.c (coff_arm_relocate_section): Use L modifier in error format. * coff-mcore.c (coff_mcore_relocate_section): Likewise. * coff-ppc.c (coff_ppc_relocate_section): Likewise. * coff-rs6000.c (xcoff_reloc_type_toc): Likewise. * coff-sh.c (sh_relax_section): Likewise. (sh_relax_delete_bytes, sh_swap_insns): Likewise. * coff-tic80.c (coff_tic80_relocate_section): Likewise. * coffcode.h (coff_slurp_reloc_table): Likewise. * coffgen.c (_bfd_coff_get_external_symbols): Likewise. (_bfd_coff_read_string_table): Likewise. * cofflink.c (_bfd_coff_generic_relocate_section): Likewise. * compress.c (bfd_get_full_section_contents): Likewise. * dwarf2.c (read_formatted_entries, decode_line_info): Likewise. * elf-m10300.c (mn10300_elf_relocate_section): Likewise. * elf.c (bfd_elf_string_from_elf_section): Likewise. * elf32-arc.c (arc_special_overflow_checks): Likewise. * elf32-arm.c (elf32_arm_tls_relax): Likewise. (elf32_arm_final_link_relocate, elf32_arm_relocate_section): Likewise. (elf32_arm_write_section): Likewise. * elf32-bfin.c (bfin_relocate_section): Likewise. (bfinfdpic_relocate_section): Likewise. * elf32-hppa.c (hppa_build_one_stub): Likewise. (final_link_relocate, elf32_hppa_relocate_section): Likewise. * elf32-i386.c (elf_i386_tls_transition): Likewise. (elf_i386_relocate_section): Likewise. * elf32-ip2k.c (ip2k_final_link_relocate): Likewise. * elf32-lm32.c (lm32_elf_finish_dynamic_sections): Likewise. * elf32-m32r.c (m32r_elf_relocate_section): Likewise. * elf32-m68k.c (elf_m68k_relocate_section): Likewise. * elf32-metag.c (elf_metag_relocate_section): Likewise. * elf32-nds32.c (unrecognized_reloc_msg): Likewise. (nds32_elf_relax_longcall1, nds32_elf_relax_longcall2): Likewise. (nds32_elf_relax_longcall3, nds32_elf_relax_longjump1): Likewise. (nds32_elf_relax_longjump2, nds32_elf_relax_longjump3): Likewise. (nds32_elf_relax_longcall4, nds32_elf_relax_longcall5): Likewise. (nds32_elf_relax_longcall6, nds32_elf_relax_longjump4): Likewise. (nds32_elf_relax_longjump5, nds32_elf_relax_longjump6): Likewise. (nds32_elf_relax_longjump7, nds32_elf_relax_loadstore): Likewise. (nds32_elf_relax_ptr, nds32_elf_ex9_build_hash_table): Likewise. * elf32-nios2.c (nios2_elf32_relocate_section): Likewise. * elf32-rx.c (UNSAFE_FOR_PID): Likewise. * elf32-s390.c (invalid_tls_insn, elf_s390_relocate_section): Likewise. * elf32-score.c (s3_bfd_score_elf_check_relocs): Likewise. * elf32-score7.c (s7_bfd_score_elf_check_relocs): Likewise. * elf32-sh.c (sh_elf_relax_section): Likewise. (sh_elf_relax_delete_bytes, sh_elf_swap_insns): Likewise. (sh_elf_relocate_section): Likewise. * elf32-sh64.c (shmedia_prepare_reloc): Likewise. * elf32-spu.c (spu_elf_relocate_section): Likewise. * elf32-tic6x.c (elf32_tic6x_relocate_section): Likewise. * elf32-tilepro.c (tilepro_elf_relocate_section): Likewise. * elf32-v850.c (v850_elf_relax_section): Likewise. * elf32-vax.c (elf_vax_check_relocs): Likewise. (elf_vax_relocate_section): Likewise. * elf32-xtensa.c (elf_xtensa_relocate_section): Likewise. (extend_ebb_bounds_forward, extend_ebb_bounds_backward): Likewise. (compute_text_actions, compute_ebb_proposed_actions): Likewise. (do_fix_for_relocatable_link): Likewise. * elf64-alpha.c (elf64_alpha_relax_got_load): Likewise. (elf64_alpha_relax_with_lituse): Likewise. * elf64-hppa.c (elf64_hppa_finish_dynamic_symbol): Likewise. (elf_hppa_final_link_relocate): Likewise. * elf64-ia64-vms.c (elf64_ia64_relax_section): Likewise. (elf64_ia64_choose_gp, elf64_ia64_relocate_section): Likewise. (elf64_vms_link_add_object_symbols): Likewise. * elf64-mmix.c (mmix_elf_perform_relocation): Likewise. (mmix_final_link_relocate): Likewise. * elf64-s390.c (invalid_tls_insn): Likewise. (elf_s390_relocate_section): Likewise. * elf64-sh64.c (sh_elf64_relocate_section): Likewise. * elf64-x86-64.c (elf_x86_64_tls_transition): Likewise. (elf_x86_64_relocate_section): Likewise. * elfcode.h (elf_slurp_symbol_table): Likewise. * elfcore.h (elf_core_file_p): Likewise. * elflink.c (elf_link_read_relocs_from_section): Likewise. * elfnn-aarch64.c (elfNN_aarch64_final_link_relocate): Likewise. (elfNN_aarch64_relocate_section): Likewise. * elfnn-ia64.c (elfNN_ia64_relax_section): Likewise. (elfNN_ia64_choose_gp, elfNN_ia64_relocate_section): Likewise. * elfnn-riscv.c (riscv_elf_relocate_section): Likewise. * elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise. (_bfd_mips_elf_relocate_section): Likewise. (_bfd_mips_elf_finish_dynamic_symbol, mips_finish_exec_plt): Likewise. * elfxx-sparc.c (_bfd_sparc_elf_relocate_section): Likewise. * elfxx-tilegx.c (tilegx_elf_relocate_section): Likewise. * ieee.c (ieee_slurp_external_symbols): Likewise. * ihex.c (ihex_write_object_content): Likewise. * mach-o.c (bfd_mach_o_build_exec_seg_command): Likewise. * merge.c (_bfd_merged_section_offset): Likewise. * mmo.c (mmo_write_loc_chunk): Likewise. (mmo_write_object_contents): Likewise. * peXXigen.c (_bfd_XX_bfd_copy_private_bfd_data_common): Likewise. * stabs.c (_bfd_link_section_stabs): Likewise. * xcofflink.c (xcoff_link_add_symbols, xcoff_find_tc0): Likewise. ld/ * testsuite/ld-arc/nps-1b.err: Update. * testsuite/ld-x86-64/ilp32-11.d: Update.
611 lines
17 KiB
C
611 lines
17 KiB
C
/* Compressed section support (intended for debug sections).
|
|
Copyright (C) 2008-2017 Free Software Foundation, Inc.
|
|
|
|
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"
|
|
#include <zlib.h>
|
|
#include "bfd.h"
|
|
#include "libbfd.h"
|
|
#include "safe-ctype.h"
|
|
|
|
#define MAX_COMPRESSION_HEADER_SIZE 24
|
|
|
|
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. */
|
|
/* 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);
|
|
strm.avail_in = compressed_size;
|
|
strm.next_in = (Bytef*) compressed_buffer;
|
|
strm.avail_out = uncompressed_size;
|
|
|
|
BFD_ASSERT (Z_OK == 0);
|
|
rc = inflateInit (&strm);
|
|
while (strm.avail_in > 0 && strm.avail_out > 0)
|
|
{
|
|
if (rc != Z_OK)
|
|
break;
|
|
strm.next_out = ((Bytef*) uncompressed_buffer
|
|
+ (uncompressed_size - strm.avail_out));
|
|
rc = inflate (&strm, Z_FINISH);
|
|
if (rc != Z_STREAM_END)
|
|
break;
|
|
rc = inflateReset (&strm);
|
|
}
|
|
rc |= inflateEnd (&strm);
|
|
return rc == Z_OK && strm.avail_out == 0;
|
|
}
|
|
|
|
/* 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
|
|
field was allocated using bfd_malloc() or equivalent.
|
|
|
|
Return the uncompressed size if the full section contents is
|
|
compressed successfully. Otherwise return 0. */
|
|
|
|
static bfd_size_type
|
|
bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
|
|
bfd_byte *uncompressed_buffer,
|
|
bfd_size_type uncompressed_size)
|
|
{
|
|
uLong compressed_size;
|
|
bfd_byte *buffer;
|
|
bfd_size_type buffer_size;
|
|
bfd_boolean decompress;
|
|
int zlib_size = 0;
|
|
int orig_compression_header_size;
|
|
bfd_size_type orig_uncompressed_size;
|
|
int header_size = bfd_get_compression_header_size (abfd, NULL);
|
|
bfd_boolean compressed
|
|
= bfd_is_section_compressed_with_header (abfd, sec,
|
|
&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;
|
|
|
|
if (compressed)
|
|
{
|
|
/* We shouldn't decompress unsupported compressed section. */
|
|
if (orig_compression_header_size < 0)
|
|
abort ();
|
|
|
|
/* 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. 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;
|
|
}
|
|
else
|
|
{
|
|
/* Convert it to .zdebug* section. */
|
|
zlib_size = uncompressed_size - orig_compression_header_size;
|
|
}
|
|
|
|
/* Add the header size. */
|
|
compressed_size = zlib_size + header_size;
|
|
}
|
|
else
|
|
compressed_size = compressBound (uncompressed_size) + header_size;
|
|
|
|
/* Uncompress if it leads to smaller size. */
|
|
if (compressed && compressed_size > orig_uncompressed_size)
|
|
{
|
|
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);
|
|
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;
|
|
}
|
|
}
|
|
|
|
free (uncompressed_buffer);
|
|
sec->contents = buffer;
|
|
sec->size = compressed_size;
|
|
sec->compress_status = COMPRESS_SECTION_DONE;
|
|
|
|
return uncompressed_size;
|
|
}
|
|
|
|
/*
|
|
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,
|
|
return @var{*ptr} with memory malloc'd by this function.
|
|
|
|
Return @code{TRUE} if the full section contents is retrieved
|
|
successfully. If the section has no contents then this function
|
|
returns @code{TRUE} but @var{*ptr} is set to NULL.
|
|
*/
|
|
|
|
bfd_boolean
|
|
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
|
|
{
|
|
bfd_size_type sz;
|
|
bfd_byte *p = *ptr;
|
|
bfd_boolean ret;
|
|
bfd_size_type save_size;
|
|
bfd_size_type save_rawsize;
|
|
bfd_byte *compressed_buffer;
|
|
unsigned int compression_header_size;
|
|
|
|
if (abfd->direction != write_direction && sec->rawsize != 0)
|
|
sz = sec->rawsize;
|
|
else
|
|
sz = sec->size;
|
|
if (sz == 0)
|
|
{
|
|
*ptr = NULL;
|
|
return TRUE;
|
|
}
|
|
|
|
switch (sec->compress_status)
|
|
{
|
|
case COMPRESS_SECTION_NONE:
|
|
if (p == NULL)
|
|
{
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
|
if (p == NULL)
|
|
{
|
|
/* 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, sz);
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
|
|
{
|
|
if (*ptr != p)
|
|
free (p);
|
|
return FALSE;
|
|
}
|
|
*ptr = p;
|
|
return TRUE;
|
|
|
|
case DECOMPRESS_SECTION_SIZED:
|
|
/* Read in the full compressed section contents. */
|
|
compressed_buffer = (bfd_byte *) bfd_malloc (sec->compressed_size);
|
|
if (compressed_buffer == NULL)
|
|
return FALSE;
|
|
save_rawsize = sec->rawsize;
|
|
save_size = sec->size;
|
|
/* 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;
|
|
sec->size = sec->compressed_size;
|
|
sec->compress_status = COMPRESS_SECTION_NONE;
|
|
ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
|
|
0, sec->compressed_size);
|
|
/* Restore rawsize and size. */
|
|
sec->rawsize = save_rawsize;
|
|
sec->size = save_size;
|
|
sec->compress_status = DECOMPRESS_SECTION_SIZED;
|
|
if (!ret)
|
|
goto fail_compressed;
|
|
|
|
if (p == NULL)
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
|
if (p == NULL)
|
|
goto fail_compressed;
|
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
if (compression_header_size == 0)
|
|
/* Set header size to the zlib header size if it is a
|
|
SHF_COMPRESSED section. */
|
|
compression_header_size = 12;
|
|
if (!decompress_contents (compressed_buffer + compression_header_size,
|
|
sec->compressed_size - compression_header_size, p, sz))
|
|
{
|
|
bfd_set_error (bfd_error_bad_value);
|
|
if (p != *ptr)
|
|
free (p);
|
|
fail_compressed:
|
|
free (compressed_buffer);
|
|
return FALSE;
|
|
}
|
|
|
|
free (compressed_buffer);
|
|
*ptr = p;
|
|
return TRUE;
|
|
|
|
case COMPRESS_SECTION_DONE:
|
|
if (sec->contents == NULL)
|
|
return FALSE;
|
|
if (p == NULL)
|
|
{
|
|
p = (bfd_byte *) bfd_malloc (sz);
|
|
if (p == NULL)
|
|
return FALSE;
|
|
*ptr = p;
|
|
}
|
|
/* PR 17512; file: 5bc29788. */
|
|
if (p != sec->contents)
|
|
memcpy (p, sec->contents, sz);
|
|
return TRUE;
|
|
|
|
default:
|
|
abort ();
|
|
}
|
|
}
|
|
|
|
/*
|
|
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;
|
|
}
|
|
|
|
/*
|
|
FUNCTION
|
|
bfd_is_section_compressed_with_header
|
|
|
|
SYNOPSIS
|
|
bfd_boolean bfd_is_section_compressed_with_header
|
|
(bfd *abfd, asection *section,
|
|
int *compression_header_size_p,
|
|
bfd_size_type *uncompressed_size_p);
|
|
|
|
DESCRIPTION
|
|
Return @code{TRUE} if @var{section} is compressed. Compression
|
|
header size is returned in @var{compression_header_size_p} and
|
|
uncompressed size is returned in @var{uncompressed_size_p}. If
|
|
compression is unsupported, compression header size is returned
|
|
with -1 and uncompressed size is returned with 0.
|
|
*/
|
|
|
|
bfd_boolean
|
|
bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
|
|
int *compression_header_size_p,
|
|
bfd_size_type *uncompressed_size_p)
|
|
{
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
|
|
int compression_header_size;
|
|
int header_size;
|
|
unsigned int saved = sec->compress_status;
|
|
bfd_boolean compressed;
|
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
|
|
abort ();
|
|
header_size = compression_header_size ? compression_header_size : 12;
|
|
|
|
/* Don't decompress the section. */
|
|
sec->compress_status = COMPRESS_SECTION_NONE;
|
|
|
|
/* 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;
|
|
|
|
*uncompressed_size_p = sec->size;
|
|
if (compressed)
|
|
{
|
|
if (compression_header_size != 0)
|
|
{
|
|
if (!bfd_check_compression_header (abfd, header, sec,
|
|
uncompressed_size_p))
|
|
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[4]))
|
|
compressed = FALSE;
|
|
else
|
|
*uncompressed_size_p = bfd_getb64 (header + 4);
|
|
}
|
|
|
|
/* Restore compress_status. */
|
|
sec->compress_status = saved;
|
|
*compression_header_size_p = compression_header_size;
|
|
return compressed;
|
|
}
|
|
|
|
/*
|
|
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;
|
|
bfd_size_type uncompressed_size;
|
|
return (bfd_is_section_compressed_with_header (abfd, sec,
|
|
&compression_header_size,
|
|
&uncompressed_size)
|
|
&& compression_header_size >= 0
|
|
&& uncompressed_size > 0);
|
|
}
|
|
|
|
/*
|
|
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
|
|
section. Otherwise, return @code{TRUE}.
|
|
*/
|
|
|
|
bfd_boolean
|
|
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
|
|
{
|
|
bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
|
|
int compression_header_size;
|
|
int header_size;
|
|
bfd_size_type uncompressed_size;
|
|
|
|
compression_header_size = bfd_get_compression_header_size (abfd, sec);
|
|
if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
|
|
abort ();
|
|
header_size = compression_header_size ? compression_header_size : 12;
|
|
|
|
/* Read the header. */
|
|
if (sec->rawsize != 0
|
|
|| sec->contents != NULL
|
|
|| sec->compress_status != COMPRESS_SECTION_NONE
|
|
|| !bfd_get_section_contents (abfd, sec, header, 0, header_size))
|
|
{
|
|
bfd_set_error (bfd_error_invalid_operation);
|
|
return FALSE;
|
|
}
|
|
|
|
if (compression_header_size == 0)
|
|
{
|
|
/* 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);
|
|
}
|
|
else if (!bfd_check_compression_header (abfd, header, sec,
|
|
&uncompressed_size))
|
|
{
|
|
bfd_set_error (bfd_error_wrong_format);
|
|
return FALSE;
|
|
}
|
|
|
|
sec->compressed_size = sec->size;
|
|
sec->size = uncompressed_size;
|
|
sec->compress_status = DECOMPRESS_SECTION_SIZED;
|
|
|
|
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
|
|
section. Otherwise, return @code{TRUE}.
|
|
*/
|
|
|
|
bfd_boolean
|
|
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
|
|
{
|
|
bfd_size_type uncompressed_size;
|
|
bfd_byte *uncompressed_buffer;
|
|
|
|
/* 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)
|
|
{
|
|
bfd_set_error (bfd_error_invalid_operation);
|
|
return FALSE;
|
|
}
|
|
|
|
/* Read in the full section contents and compress it. */
|
|
uncompressed_size = sec->size;
|
|
uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
|
|
/* PR 21431 */
|
|
if (uncompressed_buffer == NULL)
|
|
return FALSE;
|
|
|
|
if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
|
|
0, uncompressed_size))
|
|
return FALSE;
|
|
|
|
uncompressed_size = bfd_compress_section_contents (abfd, sec,
|
|
uncompressed_buffer,
|
|
uncompressed_size);
|
|
return uncompressed_size != 0;
|
|
}
|
|
|
|
/*
|
|
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) != 0;
|
|
}
|