aboutsummaryrefslogtreecommitdiff
path: root/bfd/compress.c
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2022-12-16 21:37:29 +1030
committerAlan Modra <amodra@gmail.com>2022-12-17 18:44:42 +1030
commit56ba7527d29060cf6e8693d6e772a9b9b53c1373 (patch)
tree793cd025443d0fc7d91b52f7e50746ce9c814740 /bfd/compress.c
parent6f00d50e2b6445f88b5f362dbbd982f387b6354f (diff)
downloadbinutils-56ba7527d29060cf6e8693d6e772a9b9b53c1373.zip
binutils-56ba7527d29060cf6e8693d6e772a9b9b53c1373.tar.gz
binutils-56ba7527d29060cf6e8693d6e772a9b9b53c1373.tar.bz2
bfd_get_relocated_section_contents allow NULL data buffer
This patch removes the bfd_malloc in default_indirect_link_order and bfd_simple_get_relocated_section_contents, pushing the allocation down to bfd_get_relocated_section_contents. The idea is to make use of the allocation done with sanity checking in bfd_get_full_section_contents, which is called by bfd_generic_get_relocated_section_contents. Doing this exposed a bug in bfd_get_full_section_contents. With relaxation it is possible that an input section rawsize is different to the section size. In that case we want to use the larger of rawsize (the on-disk size for input sections) and size. * reloc.c (bfd_generic_get_relocated_section_contents), * reloc16.c (bfd_coff_reloc16_get_relocated_section_contents), * coff-alpha.c (alpha_ecoff_get_relocated_section_contents), * coff-sh.c (sh_coff_get_relocated_section_contents), * elf-m10200.c (mn10200_elf_get_relocated_section_contents), * elf-m10300.c (mn10300_elf_get_relocated_section_contents), * elf32-avr.c (elf32_avr_get_relocated_section_contents), * elf32-cr16.c (elf32_cr16_get_relocated_section_contents), * elf32-crx.c (elf32_crx_get_relocated_section_contents), * elf32-h8300.c (elf32_h8_get_relocated_section_contents), * elf32-nds32.c (nds32_elf_get_relocated_section_contents), * elf32-sh.c (sh_elf_get_relocated_section_contents), * elfxx-mips.c (_bfd_elf_mips_get_relocated_section_contents): Handle NULL data buffer. * bfd.c (bfd_get_section_alloc_size): New function. * bfd-in2.h: Regenerate. * compress.c (bfd_get_full_section_contents): Correct section malloc size. * linker.c (default_indirect_link_order): Don't malloc memory here before calling bfd_get_relocated_section_contents. * simple.c (bfd_simple_get_relocated_section_contents): Likewise.
Diffstat (limited to 'bfd/compress.c')
-rw-r--r--bfd/compress.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/bfd/compress.c b/bfd/compress.c
index 6a30af5..86422ba 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -720,7 +720,8 @@ DESCRIPTION
bool
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
{
- bfd_size_type sz = bfd_get_section_limit_octets (abfd, sec);
+ bfd_size_type readsz = bfd_get_section_limit_octets (abfd, sec);
+ bfd_size_type allocsz = bfd_get_section_alloc_size (abfd, sec);
bfd_byte *p = *ptr;
bool ret;
bfd_size_type save_size;
@@ -729,7 +730,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
unsigned int compression_header_size;
const unsigned int compress_status = sec->compress_status;
- if (sz == 0)
+ if (allocsz == 0)
{
*ptr = NULL;
return true;
@@ -744,7 +745,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
_bfd_error_handler
/* xgettext:c-format */
(_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
- abfd, sec, (uint64_t) sz);
+ abfd, sec, (uint64_t) readsz);
return false;
}
@@ -753,7 +754,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
case COMPRESS_SECTION_NONE:
if (p == NULL)
{
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
{
/* PR 20801: Provide a more helpful error message. */
@@ -761,12 +762,12 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
_bfd_error_handler
/* xgettext:c-format */
(_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
- abfd, sec, (uint64_t) sz);
+ abfd, sec, (uint64_t) allocsz);
return false;
}
}
- if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
+ if (!bfd_get_section_contents (abfd, sec, p, 0, readsz))
{
if (*ptr != p)
free (p);
@@ -799,7 +800,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
goto fail_compressed;
if (p == NULL)
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
goto fail_compressed;
@@ -811,7 +812,7 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
if (!decompress_contents (
is_zstd, compressed_buffer + compression_header_size,
- sec->compressed_size - compression_header_size, p, sz))
+ sec->compressed_size - compression_header_size, p, readsz))
{
bfd_set_error (bfd_error_bad_value);
if (p != *ptr)
@@ -830,14 +831,14 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
return false;
if (p == NULL)
{
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
return false;
*ptr = p;
}
/* PR 17512; file: 5bc29788. */
if (p != sec->contents)
- memcpy (p, sec->contents, sz);
+ memcpy (p, sec->contents, readsz);
return true;
default: