_bfd_alloc_and_read

This patch provides two new inline functions that are then used in
places that allocate memory, read from file, and then deallocate on a
read failure.

	* libbfd-in.h (_bfd_alloc_and_read, _bfd_malloc_and_read): New.
	* aoutx.h (aout_get_external_symbols): Replace calls to
	bfd_[m]alloc and bfd_bread with call to _bfd_[m]alloc_and_read.
	(slurp_reloc_table): Likewise.
	* archive.c (do_slurp_bsd_armap): Likewise.
	(do_slurp_coff_armap): Likewise.
	* archive64.c (_bfd_archive_64_bit_slurp_armap): Likewise.
	* coff-rs6000.c (_bfd_xcoff_slurp_armap): Likewise.
	* coff64-rs6000.c (xcoff64_slurp_armap): Likewise.
	* coffcode.h (coff_set_arch_mach_hook, buy_and_read): Likewise.
	* coffgen.c (coff_real_object_p, coff_object_p, build_debug_section),
	(_bfd_coff_get_external_symbols): Likewise.
	* ecoff.c (ecoff_slurp_symbolic_header),
	(_bfd_ecoff_slurp_symbolic_info, ecoff_slurp_reloc_table),
	(_bfd_ecoff_slurp_armap, ecoff_link_add_object_symbols, READ),
	(ecoff_indirect_link_order): Likewise.
	* elf.c (bfd_elf_get_str_section, setup_group, elf_read_notes),
	(_bfd_elf_slurp_version_tables): Likewise.
	* elf32-m32c.c (m32c_elf_relax_section): Likewise.
	* elf32-rl78.c (rl78_elf_relax_section): Likewise.
	* elf32-rx.c (elf32_rx_relax_section): Likewise.
	* elf64-alpha.c (READ): Likewise.
	* elf64-mips.c (mips_elf64_slurp_one_reloc_table): Likewise.
	* elf64-sparc.c (elf64_sparc_slurp_one_reloc_table): Likewise.
	* elfcode.h (elf_slurp_symbol_table),
	(elf_slurp_reloc_table_from_section): Likewise.
	* elflink.c (elf_link_add_object_symbols),
	(elf_link_check_versioned_symbol): Likewise.
	* elfxx-mips.c (READ): Likewise.
	* i386lynx.c (slurp_reloc_table): Likewise.
	* lynx-core.c (lynx_core_file_p): Likewise.
	* mach-o.c (bfd_mach_o_canonicalize_relocs),
	(bfd_mach_o_read_symtab_strtab, bfd_mach_o_alloc_and_read),
	(bfd_mach_o_read_prebound_dylib, bfd_mach_o_read_dyld_content
	* pdp11.c (aout_get_external_symbols, slurp_reloc_table
	* pef.c (bfd_pef_print_loader_section, bfd_pef_scan_start_address),
	(bfd_pef_parse_symbols): Likewise.
	* peicode.h (pe_ILF_object_p, pe_bfd_object_p
	* som.c (setup_sections, som_slurp_string_table),
	(som_slurp_reloc_table, som_bfd_count_ar_symbols),
	(som_bfd_fill_in_ar_symbols): Likewise.
	* vms-alpha.c (module_find_nearest_line, evax_bfd_print_dst),
	(evax_bfd_print_image): Likewise.
	* vms-lib.c (_bfd_vms_lib_archive_p): Likewise.
	* wasm-module.c (wasm_scan): Likewise.
	* xcofflink.c (xcoff_link_add_symbols): Likewise.
	* xsym.c (bfd_sym_read_name_table),
	(bfd_sym_print_type_information_table_entry): Likewise.
	* libbfd.h: Regenerate.
This commit is contained in:
Alan Modra
2020-02-19 13:16:01 +10:30
parent 806470a219
commit 2bb3687ba8
33 changed files with 314 additions and 441 deletions

View File

@@ -275,13 +275,10 @@ coff_real_object_p (bfd *abfd,
scnhsz = bfd_coff_scnhsz (abfd);
readsize = (bfd_size_type) nscns * scnhsz;
external_sections = (char *) bfd_alloc (abfd, readsize);
external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
if (!external_sections)
goto fail;
if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
goto fail;
/* Set the arch/mach *before* swapping in sections; section header swapping
may depend on arch/mach info. */
if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
@@ -332,14 +329,11 @@ coff_object_p (bfd *abfd)
filhsz = bfd_coff_filhsz (abfd);
aoutsz = bfd_coff_aoutsz (abfd);
filehdr = bfd_alloc (abfd, filhsz);
filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
if (filehdr == NULL)
return NULL;
if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
{
if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_wrong_format);
bfd_release (abfd, filehdr);
return NULL;
}
bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
@@ -365,18 +359,13 @@ coff_object_p (bfd *abfd)
{
void * opthdr;
opthdr = bfd_alloc (abfd, aoutsz);
opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
if (opthdr == NULL)
return NULL;
if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
!= internal_f.f_opthdr)
{
bfd_release (abfd, opthdr);
return NULL;
}
/* PR 17512: file: 11056-1136-0.004. */
if (internal_f.f_opthdr < aoutsz)
memset (((char *) opthdr) + internal_f.f_opthdr, 0, aoutsz - internal_f.f_opthdr);
memset (((char *) opthdr) + internal_f.f_opthdr, 0,
aoutsz - internal_f.f_opthdr);
bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
bfd_release (abfd, opthdr);
@@ -1593,19 +1582,20 @@ build_debug_section (bfd *abfd, asection ** sect_return)
return NULL;
}
sec_size = sect->size;
debug_section = (char *) bfd_alloc (abfd, sec_size);
if (debug_section == NULL)
return NULL;
/* Seek to the beginning of the `.debug' section and read it.
Save the current position first; it is needed by our caller.
Then read debug section and reset the file pointer. */
position = bfd_tell (abfd);
if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
|| bfd_bread (debug_section, sec_size, abfd) != sec_size
|| bfd_seek (abfd, position, SEEK_SET) != 0)
if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
return NULL;
sec_size = sect->size;
debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size, sec_size);
if (debug_section == NULL)
return NULL;
if (bfd_seek (abfd, position, SEEK_SET) != 0)
return NULL;
* sect_return = sect;
@@ -1662,27 +1652,11 @@ _bfd_coff_get_external_symbols (bfd *abfd)
if (size == 0)
return TRUE;
syms = bfd_malloc (size);
if (syms == NULL)
{
/* PR 21013: Provide an error message when the alloc fails. */
_bfd_error_handler (_("%pB: not enough memory to allocate space "
"for %#" PRIx64 " symbols of size %#" PRIx64),
abfd, (uint64_t) obj_raw_syment_count (abfd),
(uint64_t) symesz);
return FALSE;
}
if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
|| bfd_bread (syms, size, abfd) != size)
{
if (syms != NULL)
free (syms);
return FALSE;
}
if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
return FALSE;
syms = _bfd_malloc_and_read (abfd, size, size);
obj_coff_external_syms (abfd) = syms;
return TRUE;
return syms != NULL;
}
/* Read in the external strings. The strings are not loaded until