aboutsummaryrefslogtreecommitdiff
path: root/bfd/reloc16.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/reloc16.c
parent6f00d50e2b6445f88b5f362dbbd982f387b6354f (diff)
downloadgdb-56ba7527d29060cf6e8693d6e772a9b9b53c1373.zip
gdb-56ba7527d29060cf6e8693d6e772a9b9b53c1373.tar.gz
gdb-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/reloc16.c')
-rw-r--r--bfd/reloc16.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/bfd/reloc16.c b/bfd/reloc16.c
index 3ae8492..7c8586c 100644
--- a/bfd/reloc16.c
+++ b/bfd/reloc16.c
@@ -251,11 +251,11 @@ bfd_coff_reloc16_get_relocated_section_contents
/* Get enough memory to hold the stuff. */
bfd *input_bfd = link_order->u.indirect.section->owner;
asection *input_section = link_order->u.indirect.section;
- long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
+ long reloc_size;
arelent **reloc_vector;
long reloc_count;
- bfd_size_type sz;
+ reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
if (reloc_size < 0)
return NULL;
@@ -267,23 +267,26 @@ bfd_coff_reloc16_get_relocated_section_contents
symbols);
/* Read in the section. */
- sz = input_section->rawsize ? input_section->rawsize : input_section->size;
- if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
+ bfd_byte *orig_data = data;
+ if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
return NULL;
- reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
- if (!reloc_vector && reloc_size != 0)
+ if (data == NULL)
return NULL;
+ if (reloc_size == 0)
+ return data;
+
+ reloc_vector = (arelent **) bfd_malloc (reloc_size);
+ if (reloc_vector == NULL)
+ goto error_return;
+
reloc_count = bfd_canonicalize_reloc (input_bfd,
input_section,
reloc_vector,
symbols);
if (reloc_count < 0)
- {
- free (reloc_vector);
- return NULL;
- }
+ goto error_return;
if (reloc_count > 0)
{
@@ -324,6 +327,12 @@ bfd_coff_reloc16_get_relocated_section_contents
}
}
}
- free ((char *) reloc_vector);
+ free (reloc_vector);
return data;
+
+ error_return:
+ free (reloc_vector);
+ if (orig_data == NULL)
+ free (data);
+ return NULL;
}