From 56ba7527d29060cf6e8693d6e772a9b9b53c1373 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 16 Dec 2022 21:37:29 +1030 Subject: 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. --- bfd/linker.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'bfd/linker.c') diff --git a/bfd/linker.c b/bfd/linker.c index 0c2e3c1..df99ce9 100644 --- a/bfd/linker.c +++ b/bfd/linker.c @@ -2551,9 +2551,8 @@ default_indirect_link_order (bfd *output_bfd, { asection *input_section; bfd *input_bfd; - bfd_byte *contents = NULL; + bfd_byte *alloced = NULL; bfd_byte *new_contents; - bfd_size_type sec_size; file_ptr loc; BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0); @@ -2654,16 +2653,11 @@ default_indirect_link_order (bfd *output_bfd, else { /* Get and relocate the section contents. */ - sec_size = (input_section->rawsize > input_section->size - ? input_section->rawsize - : input_section->size); - contents = (bfd_byte *) bfd_malloc (sec_size); - if (contents == NULL && sec_size != 0) - goto error_return; new_contents = (bfd_get_relocated_section_contents - (output_bfd, info, link_order, contents, + (output_bfd, info, link_order, NULL, bfd_link_relocatable (info), _bfd_generic_link_get_symbols (input_bfd))); + alloced = new_contents; if (!new_contents) goto error_return; } @@ -2675,11 +2669,11 @@ default_indirect_link_order (bfd *output_bfd, new_contents, loc, input_section->size)) goto error_return; - free (contents); + free (alloced); return true; error_return: - free (contents); + free (alloced); return false; } -- cgit v1.1