_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

@ -1,3 +1,55 @@
2020-02-19 Alan Modra <amodra@gmail.com>
* 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.
2020-02-19 Alan Modra <amodra@gmail.com> 2020-02-19 Alan Modra <amodra@gmail.com>
* aoutx.h (slurp_reloc_table): Allocate reloc_cache after * aoutx.h (slurp_reloc_table): Allocate reloc_cache after

View file

@ -1303,16 +1303,11 @@ aout_get_external_symbols (bfd *abfd)
/* We allocate using malloc to make the values easy to free /* We allocate using malloc to make the values easy to free
later on. If we put them on the objalloc it might not be later on. If we put them on the objalloc it might not be
possible to free them. */ possible to free them. */
syms = (struct external_nlist *) bfd_malloc (amt); if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
return FALSE;
syms = (struct external_nlist *) _bfd_malloc_and_read (abfd, amt, amt);
if (syms == NULL) if (syms == NULL)
return FALSE; return FALSE;
if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
|| bfd_bread (syms, amt, abfd) != amt)
{
free (syms);
return FALSE;
}
#endif #endif
obj_aout_external_syms (abfd) = syms; obj_aout_external_syms (abfd) = syms;
@ -2323,17 +2318,10 @@ NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
return FALSE; return FALSE;
relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size);
relocs = bfd_malloc (reloc_size);
if (relocs == NULL) if (relocs == NULL)
return FALSE; return FALSE;
if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
{
free (relocs);
return FALSE;
}
amt = count * sizeof (arelent); amt = count * sizeof (arelent);
reloc_cache = (arelent *) bfd_zmalloc (amt); reloc_cache = (arelent *) bfd_zmalloc (amt);
if (reloc_cache == NULL) if (reloc_cache == NULL)

View file

@ -968,16 +968,11 @@ do_slurp_bsd_armap (bfd *abfd)
if (parsed_size < 4) if (parsed_size < 4)
return FALSE; return FALSE;
raw_armap = (bfd_byte *) bfd_alloc (abfd, parsed_size); raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, parsed_size, parsed_size);
if (raw_armap == NULL) if (raw_armap == NULL)
return FALSE;
if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
{ {
if (bfd_get_error () != bfd_error_system_call) if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_malformed_archive); bfd_set_error (bfd_error_malformed_archive);
byebye:
bfd_release (abfd, raw_armap);
return FALSE; return FALSE;
} }
@ -987,7 +982,8 @@ do_slurp_bsd_armap (bfd *abfd)
{ {
/* Probably we're using the wrong byte ordering. */ /* Probably we're using the wrong byte ordering. */
bfd_set_error (bfd_error_wrong_format); bfd_set_error (bfd_error_wrong_format);
goto byebye; bfd_release (abfd, raw_armap);
return FALSE;
} }
rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
@ -997,7 +993,10 @@ do_slurp_bsd_armap (bfd *abfd)
amt = ardata->symdef_count * sizeof (carsym); amt = ardata->symdef_count * sizeof (carsym);
ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
if (!ardata->symdefs) if (!ardata->symdefs)
return FALSE; {
bfd_release (abfd, raw_armap);
return FALSE;
}
for (counter = 0, set = ardata->symdefs; for (counter = 0, set = ardata->symdefs;
counter < ardata->symdef_count; counter < ardata->symdef_count;
@ -1081,10 +1080,8 @@ do_slurp_coff_armap (bfd *abfd)
stringbase = ((char *) ardata->symdefs) + carsym_size; stringbase = ((char *) ardata->symdefs) + carsym_size;
/* Allocate and read in the raw offsets. */ /* Allocate and read in the raw offsets. */
raw_armap = (int *) bfd_alloc (abfd, ptrsize); raw_armap = (int *) _bfd_alloc_and_read (abfd, ptrsize, ptrsize);
if (raw_armap == NULL) if (raw_armap == NULL
goto release_symdefs;
if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
|| (bfd_bread (stringbase, stringsize, abfd) != stringsize)) || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
{ {
if (bfd_get_error () != bfd_error_system_call) if (bfd_get_error () != bfd_error_system_call)

View file

@ -108,11 +108,8 @@ _bfd_archive_64_bit_slurp_armap (bfd *abfd)
carsyms = ardata->symdefs; carsyms = ardata->symdefs;
stringbase = ((char *) ardata->symdefs) + carsym_size; stringbase = ((char *) ardata->symdefs) + carsym_size;
raw_armap = (bfd_byte *) bfd_alloc (abfd, ptrsize); raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, ptrsize, ptrsize);
if (raw_armap == NULL) if (raw_armap == NULL
goto release_symdefs;
if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
|| bfd_bread (stringbase, stringsize, abfd) != stringsize) || bfd_bread (stringbase, stringsize, abfd) != stringsize)
{ {
if (bfd_get_error () != bfd_error_system_call) if (bfd_get_error () != bfd_error_system_call)

View file

@ -1267,11 +1267,9 @@ _bfd_xcoff_slurp_armap (bfd *abfd)
} }
/* Read in the entire symbol table. */ /* Read in the entire symbol table. */
contents = (bfd_byte *) bfd_alloc (abfd, sz + 1); contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
if (contents == NULL) if (contents == NULL)
return FALSE; return FALSE;
if (bfd_bread (contents, sz, abfd) != sz)
return FALSE;
/* Ensure strings are NULL terminated so we don't wander off the /* Ensure strings are NULL terminated so we don't wander off the
end of the buffer. */ end of the buffer. */
@ -1331,11 +1329,9 @@ _bfd_xcoff_slurp_armap (bfd *abfd)
} }
/* Read in the entire symbol table. */ /* Read in the entire symbol table. */
contents = (bfd_byte *) bfd_alloc (abfd, sz + 1); contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
if (contents == NULL) if (contents == NULL)
return FALSE; return FALSE;
if (bfd_bread (contents, sz, abfd) != sz)
return FALSE;
/* Ensure strings are NULL terminated so we don't wander off the /* Ensure strings are NULL terminated so we don't wander off the
end of the buffer. */ end of the buffer. */

View file

@ -1940,11 +1940,9 @@ xcoff64_slurp_armap (bfd *abfd)
} }
/* Read in the entire symbol table. */ /* Read in the entire symbol table. */
contents = (bfd_byte *) bfd_alloc (abfd, sz + 1); contents = (bfd_byte *) _bfd_alloc_and_read (abfd, sz + 1, sz);
if (contents == NULL) if (contents == NULL)
return FALSE; return FALSE;
if (bfd_bread (contents, sz, abfd) != sz)
return FALSE;
/* Ensure strings are NULL terminated so we don't wander off the end /* Ensure strings are NULL terminated so we don't wander off the end
of the buffer. */ of the buffer. */

View file

@ -2222,15 +2222,11 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
struct internal_syment sym; struct internal_syment sym;
bfd_size_type amt = bfd_coff_symesz (abfd); bfd_size_type amt = bfd_coff_symesz (abfd);
buf = bfd_malloc (amt); if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
return FALSE;
buf = _bfd_malloc_and_read (abfd, amt, amt);
if (buf == NULL) if (buf == NULL)
return FALSE; return FALSE;
if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
|| bfd_bread (buf, amt, abfd) != amt)
{
free (buf);
return FALSE;
}
bfd_coff_swap_sym_in (abfd, buf, & sym); bfd_coff_swap_sym_in (abfd, buf, & sym);
if (sym.n_sclass == C_FILE) if (sym.n_sclass == C_FILE)
cputype = sym.n_type & 0xff; cputype = sym.n_type & 0xff;
@ -4200,7 +4196,6 @@ static void *
buy_and_read (bfd *abfd, file_ptr where, buy_and_read (bfd *abfd, file_ptr where,
bfd_size_type nmemb, bfd_size_type size) bfd_size_type nmemb, bfd_size_type size)
{ {
void *area;
size_t amt; size_t amt;
if (_bfd_mul_overflow (nmemb, size, &amt)) if (_bfd_mul_overflow (nmemb, size, &amt))
@ -4208,13 +4203,9 @@ buy_and_read (bfd *abfd, file_ptr where,
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
return NULL; return NULL;
} }
area = bfd_alloc (abfd, amt); if (bfd_seek (abfd, where, SEEK_SET) != 0)
if (!area)
return NULL; return NULL;
if (bfd_seek (abfd, where, SEEK_SET) != 0 return _bfd_alloc_and_read (abfd, amt, amt);
|| bfd_bread (area, amt, abfd) != amt)
return NULL;
return area;
} }
/* /*

View file

@ -275,13 +275,10 @@ coff_real_object_p (bfd *abfd,
scnhsz = bfd_coff_scnhsz (abfd); scnhsz = bfd_coff_scnhsz (abfd);
readsize = (bfd_size_type) nscns * scnhsz; 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) if (!external_sections)
goto fail; goto fail;
if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
goto fail;
/* Set the arch/mach *before* swapping in sections; section header swapping /* Set the arch/mach *before* swapping in sections; section header swapping
may depend on arch/mach info. */ may depend on arch/mach info. */
if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f)) 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); filhsz = bfd_coff_filhsz (abfd);
aoutsz = bfd_coff_aoutsz (abfd); aoutsz = bfd_coff_aoutsz (abfd);
filehdr = bfd_alloc (abfd, filhsz); filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
if (filehdr == NULL) if (filehdr == NULL)
return NULL;
if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
{ {
if (bfd_get_error () != bfd_error_system_call) if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_wrong_format); bfd_set_error (bfd_error_wrong_format);
bfd_release (abfd, filehdr);
return NULL; return NULL;
} }
bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
@ -365,18 +359,13 @@ coff_object_p (bfd *abfd)
{ {
void * opthdr; void * opthdr;
opthdr = bfd_alloc (abfd, aoutsz); opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
if (opthdr == NULL) if (opthdr == NULL)
return 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. */ /* PR 17512: file: 11056-1136-0.004. */
if (internal_f.f_opthdr < aoutsz) 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_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
bfd_release (abfd, opthdr); bfd_release (abfd, opthdr);
@ -1593,19 +1582,20 @@ build_debug_section (bfd *abfd, asection ** sect_return)
return NULL; 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. /* Seek to the beginning of the `.debug' section and read it.
Save the current position first; it is needed by our caller. Save the current position first; it is needed by our caller.
Then read debug section and reset the file pointer. */ Then read debug section and reset the file pointer. */
position = bfd_tell (abfd); position = bfd_tell (abfd);
if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
|| bfd_bread (debug_section, sec_size, abfd) != sec_size return NULL;
|| bfd_seek (abfd, position, SEEK_SET) != 0)
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; return NULL;
* sect_return = sect; * sect_return = sect;
@ -1662,27 +1652,11 @@ _bfd_coff_get_external_symbols (bfd *abfd)
if (size == 0) if (size == 0)
return TRUE; return TRUE;
syms = bfd_malloc (size); if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
if (syms == NULL) return FALSE;
{ syms = _bfd_malloc_and_read (abfd, size, size);
/* 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;
}
obj_coff_external_syms (abfd) = syms; obj_coff_external_syms (abfd) = syms;
return TRUE; return syms != NULL;
} }
/* Read in the external strings. The strings are not loaded until /* Read in the external strings. The strings are not loaded until

View file

@ -465,13 +465,12 @@ ecoff_slurp_symbolic_header (bfd *abfd)
} }
/* Read the symbolic information header. */ /* Read the symbolic information header. */
raw = bfd_malloc (external_hdr_size); if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
goto error_return;
raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
if (raw == NULL) if (raw == NULL)
goto error_return; goto error_return;
if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
|| bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
goto error_return;
internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
(*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr); (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
@ -571,18 +570,13 @@ _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
ecoff_data (abfd)->sym_filepos = 0; ecoff_data (abfd)->sym_filepos = 0;
return TRUE; return TRUE;
} }
raw = bfd_alloc (abfd, raw_size);
if (raw == NULL)
return FALSE;
pos = ecoff_data (abfd)->sym_filepos; pos = ecoff_data (abfd)->sym_filepos;
pos += backend->debug_swap.external_hdr_size; pos += backend->debug_swap.external_hdr_size;
if (bfd_seek (abfd, pos, SEEK_SET) != 0 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
|| bfd_bread (raw, raw_size, abfd) != raw_size) return FALSE;
{ raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
bfd_release (abfd, raw); if (raw == NULL)
return FALSE; return FALSE;
}
ecoff_data (abfd)->raw_syments = raw; ecoff_data (abfd)->raw_syments = raw;
@ -1611,7 +1605,7 @@ ecoff_slurp_reloc_table (bfd *abfd,
arelent *internal_relocs; arelent *internal_relocs;
bfd_size_type external_reloc_size; bfd_size_type external_reloc_size;
bfd_size_type amt; bfd_size_type amt;
char *external_relocs; bfd_byte *external_relocs;
arelent *rptr; arelent *rptr;
unsigned int i; unsigned int i;
@ -1625,12 +1619,10 @@ ecoff_slurp_reloc_table (bfd *abfd,
external_reloc_size = backend->external_reloc_size; external_reloc_size = backend->external_reloc_size;
amt = external_reloc_size * section->reloc_count; amt = external_reloc_size * section->reloc_count;
external_relocs = (char *) bfd_alloc (abfd, amt);
if (external_relocs == NULL)
return FALSE;
if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0) if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
return FALSE; return FALSE;
if (bfd_bread (external_relocs, amt, abfd) != amt) external_relocs = _bfd_alloc_and_read (abfd, amt, amt);
if (external_relocs == NULL)
return FALSE; return FALSE;
amt = section->reloc_count; amt = section->reloc_count;
@ -2945,15 +2937,11 @@ _bfd_ecoff_slurp_armap (bfd *abfd)
parsed_size = mapdata->parsed_size; parsed_size = mapdata->parsed_size;
free (mapdata); free (mapdata);
raw_armap = (char *) bfd_alloc (abfd, parsed_size); raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size, parsed_size);
if (raw_armap == NULL) if (raw_armap == NULL)
return FALSE;
if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size)
{ {
if (bfd_get_error () != bfd_error_system_call) if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_malformed_archive); bfd_set_error (bfd_error_malformed_archive);
bfd_release (abfd, (void *) raw_armap);
return FALSE; return FALSE;
} }
@ -3503,25 +3491,21 @@ ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
/* Read in the external symbols and external strings. */ /* Read in the external symbols and external strings. */
if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0)
return FALSE;
external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size; external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
esize = symhdr->iextMax * external_ext_size; esize = symhdr->iextMax * external_ext_size;
external_ext = bfd_malloc (esize); external_ext = _bfd_malloc_and_read (abfd, esize, esize);
if (external_ext == NULL && esize != 0) if (external_ext == NULL && esize != 0)
goto error_return; goto error_return;
if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0)
|| bfd_bread (external_ext, esize, abfd) != esize)
goto error_return; goto error_return;
ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax,
ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax); symhdr->issExtMax);
if (ssext == NULL && symhdr->issExtMax != 0) if (ssext == NULL && symhdr->issExtMax != 0)
goto error_return; goto error_return;
if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
|| (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
!= (bfd_size_type) symhdr->issExtMax))
goto error_return;
result = ecoff_link_add_externals (abfd, info, external_ext, ssext); result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
if (ssext != NULL) if (ssext != NULL)
@ -3775,14 +3759,13 @@ ecoff_final_link_debug_accumulate (bfd *output_bfd,
ret = FALSE; \ ret = FALSE; \
goto return_something; \ goto return_something; \
} \ } \
debug->ptr = (type) bfd_malloc (amt); \ if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0) \
if (debug->ptr == NULL) \
{ \ { \
ret = FALSE; \ ret = FALSE; \
goto return_something; \ goto return_something; \
} \ } \
if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0 \ debug->ptr = (type) _bfd_malloc_and_read (input_bfd, amt, amt); \
|| bfd_bread (debug->ptr, amt, input_bfd) != amt) \ if (debug->ptr == NULL) \
{ \ { \
ret = FALSE; \ ret = FALSE; \
goto return_something; \ goto return_something; \
@ -3887,13 +3870,11 @@ ecoff_indirect_link_order (bfd *output_bfd,
external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size; external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
external_relocs_size = external_reloc_size * input_section->reloc_count; external_relocs_size = external_reloc_size * input_section->reloc_count;
external_relocs = bfd_malloc (external_relocs_size); if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0)
if (external_relocs == NULL && external_relocs_size != 0)
goto error_return; goto error_return;
external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size,
if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 external_relocs_size);
|| (bfd_bread (external_relocs, external_relocs_size, input_bfd) if (external_relocs == NULL && external_relocs_size != 0)
!= external_relocs_size))
goto error_return; goto error_return;
/* Relocate the section contents. */ /* Relocate the section contents. */

View file

@ -298,14 +298,9 @@ bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
if (shstrtabsize + 1 <= 1 if (shstrtabsize + 1 <= 1
|| shstrtabsize > bfd_get_file_size (abfd) || shstrtabsize > bfd_get_file_size (abfd)
|| bfd_seek (abfd, offset, SEEK_SET) != 0 || bfd_seek (abfd, offset, SEEK_SET) != 0
|| (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
shstrtab = NULL; shstrtabsize)) == NULL)
else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
{ {
if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_file_truncated);
bfd_release (abfd, shstrtab);
shstrtab = NULL;
/* Once we've failed to read it, make sure we don't keep /* Once we've failed to read it, make sure we don't keep
trying. Otherwise, we'll keep allocating space for trying. Otherwise, we'll keep allocating space for
the string table over and over. */ the string table over and over. */
@ -675,10 +670,9 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
shdr->contents = NULL; shdr->contents = NULL;
if (_bfd_mul_overflow (shdr->sh_size, if (_bfd_mul_overflow (shdr->sh_size,
sizeof (*dest) / 4, &amt) sizeof (*dest) / 4, &amt)
|| (shdr->contents = bfd_alloc (abfd, amt)) == NULL
|| bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
|| (bfd_bread (shdr->contents, shdr->sh_size, abfd) || !(shdr->contents
!= shdr->sh_size)) = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
{ {
_bfd_error_handler _bfd_error_handler
/* xgettext:c-format */ /* xgettext:c-format */
@ -687,14 +681,6 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
abfd, (uint64_t) shdr->sh_size); abfd, (uint64_t) shdr->sh_size);
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
-- num_group; -- num_group;
/* PR 17510: If the group contents are even
partially corrupt, do not allow any of the
contents to be used. */
if (shdr->contents != NULL)
{
bfd_release (abfd, shdr->contents);
shdr->contents = NULL;
}
continue; continue;
} }
@ -8612,12 +8598,11 @@ error_return_verref:
abfd, (uint64_t) hdr->sh_size); abfd, (uint64_t) hdr->sh_size);
goto error_return_verref; goto error_return_verref;
} }
contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
if (contents == NULL)
goto error_return_verref;
if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
|| bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) goto error_return_verref;
contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
if (contents == NULL)
goto error_return_verref; goto error_return_verref;
if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
@ -8747,11 +8732,10 @@ error_return_verref:
goto error_return; goto error_return;
} }
contents = (bfd_byte *) bfd_malloc (hdr->sh_size); if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
if (contents == NULL)
goto error_return_verdef; goto error_return_verdef;
if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
|| bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) if (contents == NULL)
goto error_return_verdef; goto error_return_verdef;
BFD_ASSERT (sizeof (Elf_External_Verdef) BFD_ASSERT (sizeof (Elf_External_Verdef)
@ -11937,7 +11921,7 @@ elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
if (bfd_seek (abfd, offset, SEEK_SET) != 0) if (bfd_seek (abfd, offset, SEEK_SET) != 0)
return FALSE; return FALSE;
buf = (char *) bfd_malloc (size + 1); buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
if (buf == NULL) if (buf == NULL)
return FALSE; return FALSE;
@ -11945,8 +11929,7 @@ elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
0-termintate the buffer so that string searches will not overflow. */ 0-termintate the buffer so that string searches will not overflow. */
buf[size] = 0; buf[size] = 0;
if (bfd_bread (buf, size, abfd) != size if (!elf_parse_notes (abfd, buf, size, offset, align))
|| !elf_parse_notes (abfd, buf, size, offset, align))
{ {
free (buf); free (buf);
return FALSE; return FALSE;

View file

@ -1499,11 +1499,10 @@ m32c_elf_relax_section
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
goto error_return; goto error_return;
} }
shndx_buf = bfd_malloc (amt); if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
if (shndx_buf == NULL)
goto error_return; goto error_return;
if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
|| bfd_bread (shndx_buf, amt, abfd) != amt) if (shndx_buf == NULL)
goto error_return; goto error_return;
shndx_hdr->contents = shndx_buf; shndx_hdr->contents = shndx_buf;
} }

View file

@ -2129,11 +2129,10 @@ rl78_elf_relax_section
bfd_set_error (bfd_error_no_memory); bfd_set_error (bfd_error_no_memory);
goto error_return; goto error_return;
} }
shndx_buf = bfd_malloc (amt); if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
if (shndx_buf == NULL)
goto error_return; goto error_return;
if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
|| bfd_bread (shndx_buf, amt, abfd) != amt) if (shndx_buf == NULL)
goto error_return; goto error_return;
shndx_hdr->contents = shndx_buf; shndx_hdr->contents = shndx_buf;
} }

View file

@ -2066,11 +2066,10 @@ elf32_rx_relax_section (bfd * abfd,
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
goto error_return; goto error_return;
} }
shndx_buf = bfd_malloc (amt); if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
if (shndx_buf == NULL)
goto error_return; goto error_return;
if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
|| bfd_bread (shndx_buf, amt, abfd) != amt) if (shndx_buf == NULL)
goto error_return; goto error_return;
shndx_hdr->contents = shndx_buf; shndx_hdr->contents = shndx_buf;
} }

View file

@ -1398,11 +1398,10 @@ elf64_alpha_read_ecoff_info (bfd *abfd, asection *section,
bfd_set_error (bfd_error_file_too_big); \ bfd_set_error (bfd_error_file_too_big); \
goto error_return; \ goto error_return; \
} \ } \
debug->ptr = (type) bfd_malloc (amt); \ if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
if (debug->ptr == NULL) \
goto error_return; \ goto error_return; \
if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \ debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
|| bfd_bread (debug->ptr, amt, abfd) != amt) \ if (debug->ptr == NULL) \
goto error_return; \ goto error_return; \
} while (0) } while (0)

View file

@ -4010,15 +4010,12 @@ mips_elf64_slurp_one_reloc_table (bfd *abfd, asection *asect,
int entsize; int entsize;
bfd_boolean rela_p; bfd_boolean rela_p;
allocated = bfd_malloc (rel_hdr->sh_size); if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
return FALSE;
allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
if (allocated == NULL) if (allocated == NULL)
return FALSE; return FALSE;
if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
|| (bfd_bread (allocated, rel_hdr->sh_size, abfd)
!= rel_hdr->sh_size))
goto error_return;
native_relocs = allocated; native_relocs = allocated;
entsize = rel_hdr->sh_entsize; entsize = rel_hdr->sh_entsize;

View file

@ -79,13 +79,11 @@ elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
bfd_size_type count; bfd_size_type count;
arelent *relents; arelent *relents;
allocated = bfd_malloc (rel_hdr->sh_size); if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
return FALSE;
allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
if (allocated == NULL) if (allocated == NULL)
goto error_return; return FALSE;
if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
|| bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size)
goto error_return;
native_relocs = (bfd_byte *) allocated; native_relocs = (bfd_byte *) allocated;

View file

@ -1234,13 +1234,10 @@ elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
{ {
if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0) if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
goto error_return; goto error_return;
xverbuf = (Elf_External_Versym *)
xverbuf = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size); _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
if (xverbuf == NULL && verhdr->sh_size != 0) if (xverbuf == NULL && verhdr->sh_size != 0)
goto error_return; goto error_return;
if (bfd_bread (xverbuf, verhdr->sh_size, abfd) != verhdr->sh_size)
goto error_return;
} }
/* Skip first symbol, which is a null dummy. */ /* Skip first symbol, which is a null dummy. */
@ -1429,14 +1426,11 @@ elf_slurp_reloc_table_from_section (bfd *abfd,
int entsize; int entsize;
unsigned int symcount; unsigned int symcount;
allocated = bfd_malloc (rel_hdr->sh_size); if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
return FALSE;
allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
if (allocated == NULL) if (allocated == NULL)
goto error_return; return FALSE;
if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
|| (bfd_bread (allocated, rel_hdr->sh_size, abfd)
!= rel_hdr->sh_size))
goto error_return;
native_relocs = (bfd_byte *) allocated; native_relocs = (bfd_byte *) allocated;

View file

@ -4332,12 +4332,12 @@ error_free_dyn:
Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr; Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
bfd_size_type amt = versymhdr->sh_size; bfd_size_type amt = versymhdr->sh_size;
extversym = (Elf_External_Versym *) bfd_malloc (amt); if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
goto error_free_sym;
extversym = (Elf_External_Versym *)
_bfd_malloc_and_read (abfd, amt, amt);
if (extversym == NULL) if (extversym == NULL)
goto error_free_sym; goto error_free_sym;
if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
|| bfd_bread (extversym, amt, abfd) != amt)
goto error_free_vers;
extversym_end = extversym + amt / sizeof (*extversym); extversym_end = extversym + amt / sizeof (*extversym);
} }
} }
@ -9737,16 +9737,11 @@ elf_link_check_versioned_symbol (struct bfd_link_info *info,
/* Read in any version definitions. */ /* Read in any version definitions. */
versymhdr = &elf_tdata (input)->dynversym_hdr; versymhdr = &elf_tdata (input)->dynversym_hdr;
extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
if (extversym == NULL)
goto error_ret;
if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
|| (bfd_bread (extversym, versymhdr->sh_size, input) || (extversym = (Elf_External_Versym *)
!= versymhdr->sh_size)) _bfd_malloc_and_read (input, versymhdr->sh_size,
versymhdr->sh_size)) == NULL)
{ {
free (extversym);
error_ret:
free (isymbuf); free (isymbuf);
return FALSE; return FALSE;
} }

View file

@ -1424,11 +1424,10 @@ _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
bfd_set_error (bfd_error_file_too_big); \ bfd_set_error (bfd_error_file_too_big); \
goto error_return; \ goto error_return; \
} \ } \
debug->ptr = bfd_malloc (amt); \ if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
if (debug->ptr == NULL) \
goto error_return; \ goto error_return; \
if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \ debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
|| bfd_bread (debug->ptr, amt, abfd) != amt) \ if (debug->ptr == NULL) \
goto error_return; \ goto error_return; \
} while (0) } while (0)

View file

@ -411,20 +411,13 @@ doit:
if (!reloc_cache && count != 0) if (!reloc_cache && count != 0)
return FALSE; return FALSE;
relocs = bfd_alloc (abfd, reloc_size); relocs = _bfd_alloc_and_read (abfd, reloc_size, reloc_size);
if (!relocs && reloc_size != 0) if (!relocs && reloc_size != 0)
{ {
free (reloc_cache); free (reloc_cache);
return FALSE; return FALSE;
} }
if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
{
bfd_release (abfd, relocs);
free (reloc_cache);
return FALSE;
}
if (each_size == RELOC_EXT_SIZE) if (each_size == RELOC_EXT_SIZE)
{ {
struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs; struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;

View file

@ -903,3 +903,29 @@ extern bfd_vma _bfd_safe_read_leb128
#define _bfd_mul_overflow(a, b, res) \ #define _bfd_mul_overflow(a, b, res) \
((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a)) ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a))
#endif #endif
static inline bfd_byte *
_bfd_alloc_and_read (bfd *abfd, bfd_size_type asize, bfd_size_type rsize)
{
bfd_byte *mem = bfd_alloc (abfd, asize);
if (mem != NULL)
{
if (bfd_bread (mem, rsize, abfd) == rsize)
return mem;
bfd_release (abfd, mem);
}
return NULL;
}
static inline bfd_byte *
_bfd_malloc_and_read (bfd *abfd, bfd_size_type asize, bfd_size_type rsize)
{
bfd_byte *mem = bfd_malloc (asize);
if (mem != NULL)
{
if (bfd_bread (mem, rsize, abfd) == rsize)
return mem;
free (mem);
}
return NULL;
}

View file

@ -908,6 +908,32 @@ extern bfd_vma _bfd_safe_read_leb128
#define _bfd_mul_overflow(a, b, res) \ #define _bfd_mul_overflow(a, b, res) \
((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a)) ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a))
#endif #endif
static inline bfd_byte *
_bfd_alloc_and_read (bfd *abfd, bfd_size_type asize, bfd_size_type rsize)
{
bfd_byte *mem = bfd_alloc (abfd, asize);
if (mem != NULL)
{
if (bfd_bread (mem, rsize, abfd) == rsize)
return mem;
bfd_release (abfd, mem);
}
return NULL;
}
static inline bfd_byte *
_bfd_malloc_and_read (bfd *abfd, bfd_size_type asize, bfd_size_type rsize)
{
bfd_byte *mem = bfd_malloc (asize);
if (mem != NULL)
{
if (bfd_bread (mem, rsize, abfd) == rsize)
return mem;
free (mem);
}
return NULL;
}
/* Extracted from libbfd.c. */ /* Extracted from libbfd.c. */
bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int); bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);

View file

@ -126,25 +126,14 @@ lynx_core_file_p (bfd *abfd)
tcontext_size = pss.threadcnt * sizeof (core_st_t); tcontext_size = pss.threadcnt * sizeof (core_st_t);
/* Allocate space for the thread contexts */ /* Save thread contexts */
if (bfd_seek (abfd, pagesize, SEEK_SET) != 0)
threadp = (core_st_t *) bfd_alloc (abfd, tcontext_size); goto fail;
threadp = (core_st_t *) _bfd_alloc_and_read (abfd, tcontext_size,
tcontext_size);
if (!threadp) if (!threadp)
goto fail; goto fail;
/* Save thread contexts */
if (bfd_seek (abfd, (file_ptr) pagesize, SEEK_SET) != 0)
goto fail;
if (bfd_bread ((void *) threadp, tcontext_size, abfd) != tcontext_size)
{
/* Probably too small to be a core file */
if (bfd_get_error () != bfd_error_system_call)
bfd_set_error (bfd_error_wrong_format);
goto fail;
}
core_signal (abfd) = threadp->currsig; core_signal (abfd) = threadp->currsig;
newsect = make_bfd_asection (abfd, ".stack", newsect = make_bfd_asection (abfd, ".stack",

View file

@ -1623,15 +1623,13 @@ bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
if (native_size < count) if (native_size < count)
goto err; goto err;
native_relocs = if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
(struct mach_o_reloc_info_external *) bfd_malloc (native_size); return -1;
native_relocs = (struct mach_o_reloc_info_external *)
_bfd_malloc_and_read (abfd, native_size, native_size);
if (native_relocs == NULL) if (native_relocs == NULL)
return -1; return -1;
if (bfd_seek (abfd, filepos, SEEK_SET) != 0
|| bfd_bread (native_relocs, native_size, abfd) != native_size)
goto err;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i], if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
@ -3902,19 +3900,13 @@ bfd_mach_o_read_symtab_strtab (bfd *abfd)
/* See PR 21840 for a reproducer. */ /* See PR 21840 for a reproducer. */
if ((sym->strsize + 1) == 0) if ((sym->strsize + 1) == 0)
return FALSE; return FALSE;
sym->strtab = bfd_alloc (abfd, sym->strsize + 1); if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
return FALSE;
sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1,
sym->strsize);
if (sym->strtab == NULL) if (sym->strtab == NULL)
return FALSE; return FALSE;
if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
|| bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize)
{
/* PR 17512: file: 10888-1609-0.004. */
bfd_release (abfd, sym->strtab);
sym->strtab = NULL;
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
/* Zero terminate the string table. */ /* Zero terminate the string table. */
sym->strtab[sym->strsize] = 0; sym->strtab[sym->strsize] = 0;
} }
@ -4000,15 +3992,9 @@ bfd_mach_o_ppc_flavour_string (unsigned int flavour)
static unsigned char * static unsigned char *
bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size) bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size)
{ {
unsigned char *buf; if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
buf = bfd_alloc (abfd, size);
if (buf == NULL)
return NULL; return NULL;
if (bfd_seek (abfd, filepos, SEEK_SET) != 0 return _bfd_alloc_and_read (abfd, size, size);
|| bfd_bread (buf, size, abfd) != size)
return NULL;
return buf;
} }
static bfd_boolean static bfd_boolean
@ -4100,11 +4086,9 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd,
return FALSE; return FALSE;
str_len = command->len - sizeof (raw); str_len = command->len - sizeof (raw);
str = bfd_alloc (abfd, str_len); str = _bfd_alloc_and_read (abfd, str_len, str_len);
if (str == NULL) if (str == NULL)
return FALSE; return FALSE;
if (bfd_bread (str, str_len, abfd) != str_len)
return FALSE;
cmd->name_offset = command->offset + nameoff; cmd->name_offset = command->offset + nameoff;
cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules); cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
@ -4581,8 +4565,8 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd)
/* Read rebase content. */ /* Read rebase content. */
if (cmd->rebase_content == NULL && cmd->rebase_size != 0) if (cmd->rebase_content == NULL && cmd->rebase_size != 0)
{ {
cmd->rebase_content = cmd->rebase_content
bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size); = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size);
if (cmd->rebase_content == NULL) if (cmd->rebase_content == NULL)
return FALSE; return FALSE;
} }
@ -4590,8 +4574,8 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd)
/* Read bind content. */ /* Read bind content. */
if (cmd->bind_content == NULL && cmd->bind_size != 0) if (cmd->bind_content == NULL && cmd->bind_size != 0)
{ {
cmd->bind_content = cmd->bind_content
bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size); = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size);
if (cmd->bind_content == NULL) if (cmd->bind_content == NULL)
return FALSE; return FALSE;
} }

View file

@ -1182,17 +1182,13 @@ aout_get_external_symbols (bfd *abfd)
/* We allocate using malloc to make the values easy to free /* We allocate using malloc to make the values easy to free
later on. If we put them on the objalloc it might not be later on. If we put them on the objalloc it might not be
possible to free them. */ possible to free them. */
syms = bfd_malloc (count * EXTERNAL_NLIST_SIZE); if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
return FALSE;
syms = (struct external_nlist *)
_bfd_malloc_and_read (abfd, count * EXTERNAL_NLIST_SIZE,
count * EXTERNAL_NLIST_SIZE);
if (syms == NULL && count != 0) if (syms == NULL && count != 0)
return FALSE; return FALSE;
if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
|| (bfd_bread (syms, exec_hdr (abfd)->a_syms, abfd)
!= exec_hdr (abfd)->a_syms))
{
free (syms);
return FALSE;
}
#endif #endif
obj_aout_external_syms (abfd) = syms; obj_aout_external_syms (abfd) = syms;
@ -1829,19 +1825,11 @@ NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
return FALSE; return FALSE;
relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size);
each_size = obj_reloc_entry_size (abfd);
relocs = bfd_malloc (reloc_size);
if (relocs == NULL && reloc_size != 0) if (relocs == NULL && reloc_size != 0)
return FALSE; return FALSE;
if (bfd_bread (relocs, reloc_size, abfd) != reloc_size) each_size = obj_reloc_entry_size (abfd);
{
free (relocs);
return FALSE;
}
count = reloc_size / each_size; count = reloc_size / each_size;
/* Count the number of NON-ZERO relocs, this is the count we want. */ /* Count the number of NON-ZERO relocs, this is the count we want. */

View file

@ -446,14 +446,15 @@ bfd_pef_print_loader_section (bfd *abfd, FILE *file)
return -1; return -1;
loaderlen = loadersec->size; loaderlen = loadersec->size;
loaderbuf = bfd_malloc (loaderlen); if (loaderlen < 56)
return -1;
if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
return -1;
loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
if (loaderbuf == NULL) if (loaderbuf == NULL)
return -1; return -1;
if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0 if (bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header) < 0)
|| bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen
|| loaderlen < 56
|| bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header) < 0)
{ {
free (loaderbuf); free (loaderbuf);
return -1; return -1;
@ -479,17 +480,14 @@ bfd_pef_scan_start_address (bfd *abfd)
goto end; goto end;
loaderlen = loadersec->size; loaderlen = loadersec->size;
loaderbuf = bfd_malloc (loaderlen);
if (loaderbuf == NULL)
goto end;
if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
goto error;
if (bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen)
goto error;
if (loaderlen < 56) if (loaderlen < 56)
goto error; goto error;
if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
goto error;
loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
if (loaderbuf == NULL)
goto error;
ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header); ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
if (ret < 0) if (ret < 0)
goto error; goto error;
@ -903,26 +901,22 @@ bfd_pef_parse_symbols (bfd *abfd, asymbol **csym)
if (codesec != NULL) if (codesec != NULL)
{ {
codelen = codesec->size; codelen = codesec->size;
codebuf = bfd_malloc (codelen); if (bfd_seek (abfd, codesec->filepos, SEEK_SET) != 0)
goto end;
codebuf = _bfd_malloc_and_read (abfd, codelen, codelen);
if (codebuf == NULL) if (codebuf == NULL)
goto end; goto end;
if (bfd_seek (abfd, codesec->filepos, SEEK_SET) < 0)
goto end;
if (bfd_bread ((void *) codebuf, codelen, abfd) != codelen)
goto end;
} }
loadersec = bfd_get_section_by_name (abfd, "loader"); loadersec = bfd_get_section_by_name (abfd, "loader");
if (loadersec != NULL) if (loadersec != NULL)
{ {
loaderlen = loadersec->size; loaderlen = loadersec->size;
loaderbuf = bfd_malloc (loaderlen); if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
goto end;
loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
if (loaderbuf == NULL) if (loaderbuf == NULL)
goto end; goto end;
if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
goto end;
if (bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen)
goto end;
} }
count = 0; count = 0;

View file

@ -1273,16 +1273,10 @@ pe_ILF_object_p (bfd * abfd)
/* ptr += 2; */ /* ptr += 2; */
/* Now read in the two strings that follow. */ /* Now read in the two strings that follow. */
ptr = (bfd_byte *) bfd_alloc (abfd, size); ptr = (bfd_byte *) _bfd_alloc_and_read (abfd, size, size);
if (ptr == NULL) if (ptr == NULL)
return NULL; return NULL;
if (bfd_bread (ptr, size, abfd) != size)
{
bfd_release (abfd, ptr);
return NULL;
}
symbol_name = (char *) ptr; symbol_name = (char *) ptr;
/* See PR 20905 for an example of where the strnlen is necessary. */ /* See PR 20905 for an example of where the strnlen is necessary. */
source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1; source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1;
@ -1494,12 +1488,9 @@ pe_bfd_object_p (bfd * abfd)
if (amt < sizeof (PEAOUTHDR)) if (amt < sizeof (PEAOUTHDR))
amt = sizeof (PEAOUTHDR); amt = sizeof (PEAOUTHDR);
opthdr = bfd_alloc (abfd, amt); opthdr = _bfd_alloc_and_read (abfd, amt, opt_hdr_size);
if (opthdr == NULL) if (opthdr == NULL)
return NULL; return NULL;
if (bfd_bread (opthdr, opt_hdr_size, abfd)
!= (bfd_size_type) opt_hdr_size)
return NULL;
if (amt > opt_hdr_size) if (amt > opt_hdr_size)
memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size); memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size);

View file

@ -2088,14 +2088,11 @@ setup_sections (bfd *abfd,
bfd_set_error (bfd_error_no_memory); bfd_set_error (bfd_error_no_memory);
goto error_return; goto error_return;
} }
space_strings = bfd_malloc (amt + 1);
if (space_strings == NULL)
goto error_return;
if (bfd_seek (abfd, current_offset + file_hdr->space_strings_location, if (bfd_seek (abfd, current_offset + file_hdr->space_strings_location,
SEEK_SET) != 0) SEEK_SET) != 0)
goto error_return; goto error_return;
if (bfd_bread (space_strings, amt, abfd) != amt) space_strings = (char *) _bfd_malloc_and_read (abfd, amt + 1, amt);
if (space_strings == NULL)
goto error_return; goto error_return;
/* Make sure that the string table is NUL terminated. */ /* Make sure that the string table is NUL terminated. */
space_strings[amt] = 0; space_strings[amt] = 0;
@ -4578,15 +4575,11 @@ som_slurp_string_table (bfd *abfd)
} }
/* Allocate and read in the string table. */ /* Allocate and read in the string table. */
amt = obj_som_stringtab_size (abfd);
stringtab = bfd_zmalloc (amt);
if (stringtab == NULL)
return FALSE;
if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) != 0) if (bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET) != 0)
return FALSE; return FALSE;
amt = obj_som_stringtab_size (abfd);
if (bfd_bread (stringtab, amt, abfd) != amt) stringtab = (char *) _bfd_malloc_and_read (abfd, amt, amt);
if (stringtab == NULL)
return FALSE; return FALSE;
/* Save our results and return success. */ /* Save our results and return success. */
@ -4681,12 +4674,11 @@ som_slurp_symbol_table (bfd *abfd)
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
goto error_return; goto error_return;
} }
buf = bfd_malloc (amt);
if (buf == NULL)
goto error_return;
if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) != 0) if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) != 0)
goto error_return; goto error_return;
if (bfd_bread (buf, amt, abfd) != amt) buf = (struct som_external_symbol_dictionary_record *)
_bfd_malloc_and_read (abfd, amt, amt);
if (buf == NULL)
goto error_return; goto error_return;
if (_bfd_mul_overflow (symbol_count, sizeof (som_symbol_type), &amt)) if (_bfd_mul_overflow (symbol_count, sizeof (som_symbol_type), &amt))
@ -5297,17 +5289,13 @@ som_slurp_reloc_table (bfd *abfd,
parsed. We must do so now to know how many relocations exist. */ parsed. We must do so now to know how many relocations exist. */
if (section->reloc_count == (unsigned) -1) if (section->reloc_count == (unsigned) -1)
{ {
amt = fixup_stream_size;
external_relocs = bfd_malloc (amt);
if (external_relocs == NULL)
return FALSE;
/* Read in the external forms. */ /* Read in the external forms. */
if (bfd_seek (abfd, if (bfd_seek (abfd, obj_som_reloc_filepos (abfd) + section->rel_filepos,
obj_som_reloc_filepos (abfd) + section->rel_filepos, SEEK_SET) != 0)
SEEK_SET)
!= 0)
return FALSE; return FALSE;
if (bfd_bread (external_relocs, amt, abfd) != amt) amt = fixup_stream_size;
external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
if (external_relocs == NULL)
return FALSE; return FALSE;
/* Let callers know how many relocations found. /* Let callers know how many relocations found.
@ -5921,23 +5909,20 @@ som_bfd_count_ar_symbols (bfd *abfd,
lst_filepos = bfd_tell (abfd) - sizeof (struct som_external_lst_header); lst_filepos = bfd_tell (abfd) - sizeof (struct som_external_lst_header);
/* Read in the hash table. The hash table is an array of 32-bit
file offsets which point to the hash chains. */
if (_bfd_mul_overflow (lst_header->hash_size, 4, &amt)) if (_bfd_mul_overflow (lst_header->hash_size, 4, &amt))
{ {
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
return FALSE; return FALSE;
} }
hash_table = bfd_malloc (amt); hash_table = _bfd_malloc_and_read (abfd, amt, amt);
if (hash_table == NULL && lst_header->hash_size != 0) if (hash_table == NULL && lst_header->hash_size != 0)
goto error_return; goto error_return;
/* Don't forget to initialize the counter! */ /* Don't forget to initialize the counter! */
*count = 0; *count = 0;
/* Read in the hash table. The hash table is an array of 32-bit
file offsets which point to the hash chains. */
if (bfd_bread ((void *) hash_table, amt, abfd) != amt)
goto error_return;
/* Walk each chain counting the number of symbols found on that particular /* Walk each chain counting the number of symbols found on that particular
chain. */ chain. */
for (i = 0; i < lst_header->hash_size; i++) for (i = 0; i < lst_header->hash_size; i++)
@ -6016,20 +6001,18 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
unsigned int string_loc; unsigned int string_loc;
lst_filepos = bfd_tell (abfd) - sizeof (struct som_external_lst_header); lst_filepos = bfd_tell (abfd) - sizeof (struct som_external_lst_header);
/* Read in the hash table. The has table is an array of 32bit file offsets
which point to the hash chains. */
if (_bfd_mul_overflow (lst_header->hash_size, 4, &amt)) if (_bfd_mul_overflow (lst_header->hash_size, 4, &amt))
{ {
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
return FALSE; return FALSE;
} }
hash_table = bfd_malloc (amt); hash_table = _bfd_malloc_and_read (abfd, amt, amt);
if (hash_table == NULL && lst_header->hash_size != 0) if (hash_table == NULL && lst_header->hash_size != 0)
goto error_return; goto error_return;
/* Read in the hash table. The has table is an array of 32bit file offsets
which point to the hash chains. */
if (bfd_bread ((void *) hash_table, amt, abfd) != amt)
goto error_return;
/* Seek to and read in the SOM dictionary. We will need this to fill /* Seek to and read in the SOM dictionary. We will need this to fill
in the carsym's filepos field. */ in the carsym's filepos field. */
if (bfd_seek (abfd, lst_filepos + lst_header->dir_loc, SEEK_SET) != 0) if (bfd_seek (abfd, lst_filepos + lst_header->dir_loc, SEEK_SET) != 0)
@ -6041,13 +6024,11 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
bfd_set_error (bfd_error_file_too_big); bfd_set_error (bfd_error_file_too_big);
goto error_return; goto error_return;
} }
som_dict = bfd_malloc (amt); som_dict = (struct som_external_som_entry *)
_bfd_malloc_and_read (abfd, amt, amt);
if (som_dict == NULL && lst_header->module_count != 0) if (som_dict == NULL && lst_header->module_count != 0)
goto error_return; goto error_return;
if (bfd_bread ((void *) som_dict, amt, abfd) != amt)
goto error_return;
string_loc = lst_header->string_loc; string_loc = lst_header->string_loc;
/* Walk each chain filling in the carsyms as we go along. */ /* Walk each chain filling in the carsyms as we go along. */
@ -6094,12 +6075,9 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
bfd_set_error (bfd_error_no_memory); bfd_set_error (bfd_error_no_memory);
goto error_return; goto error_return;
} }
name = bfd_zalloc (abfd, (bfd_size_type) len + 1); name = (char *) _bfd_alloc_and_read (abfd, len + 1, len);
if (!name) if (!name)
goto error_return; goto error_return;
if (bfd_bread (name, (bfd_size_type) len, abfd) != len)
goto error_return;
name[len] = 0; name[len] = 0;
set->name = name; set->name = name;
@ -6148,12 +6126,9 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
bfd_set_error (bfd_error_no_memory); bfd_set_error (bfd_error_no_memory);
goto error_return; goto error_return;
} }
name = bfd_zalloc (abfd, (bfd_size_type) len + 1); name = (char *) _bfd_alloc_and_read (abfd, len + 1, len);
if (!name) if (!name)
goto error_return; goto error_return;
if (bfd_bread (name, (bfd_size_type) len, abfd) != len)
goto error_return;
name[len] = 0; name[len] = 0;
set->name = name; set->name = name;

View file

@ -4822,10 +4822,10 @@ module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr,
{ {
unsigned int size = module->size; unsigned int size = module->size;
unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg; unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg;
unsigned char *buffer = (unsigned char *) bfd_malloc (module->size); unsigned char *buffer;
if (bfd_seek (abfd, modbeg, SEEK_SET) != 0 if (bfd_seek (abfd, modbeg, SEEK_SET) != 0
|| bfd_bread (buffer, size, abfd) != size) || (buffer = _bfd_malloc_and_read (abfd, size, size)) == NULL)
{ {
bfd_set_error (bfd_error_no_debug_section); bfd_set_error (bfd_error_no_debug_section);
return FALSE; return FALSE;
@ -7149,8 +7149,8 @@ evax_bfd_print_dst (struct bfd *abfd, unsigned int dst_size, FILE *file)
dst_size -= len; dst_size -= len;
off += len; off += len;
len -= sizeof (dsth); len -= sizeof (dsth);
buf = bfd_malloc (len); buf = _bfd_malloc_and_read (abfd, len, len);
if (bfd_bread (buf, len, abfd) != len) if (buf == NULL)
{ {
fprintf (file, _("cannot read DST symbol\n")); fprintf (file, _("cannot read DST symbol\n"));
return; return;
@ -8065,14 +8065,12 @@ evax_bfd_print_image (bfd *abfd, FILE *file)
unsigned int codeadroff; unsigned int codeadroff;
unsigned int lpfixoff; unsigned int lpfixoff;
unsigned int chgprtoff; unsigned int chgprtoff;
file_ptr f_off = (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE;
buf = bfd_malloc (eiaf_size); if (bfd_seek (abfd, f_off, SEEK_SET) != 0
|| (buf = _bfd_malloc_and_read (abfd, eiaf_size, eiaf_size)) == NULL)
if (bfd_seek (abfd, (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET)
|| bfd_bread (buf, eiaf_size, abfd) != eiaf_size)
{ {
fprintf (file, _("cannot read EIHA\n")); fprintf (file, _("cannot read EIHA\n"));
free (buf);
return; return;
} }
eiaf = (struct vms_eiaf *)buf; eiaf = (struct vms_eiaf *)buf;

View file

@ -568,14 +568,9 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
!= sizeof (buf_reclen)) != sizeof (buf_reclen))
goto err; goto err;
reclen = bfd_getl32 (buf_reclen); reclen = bfd_getl32 (buf_reclen);
buf = bfd_malloc (reclen); buf = _bfd_malloc_and_read (abfd, reclen, reclen);
if (buf == NULL) if (buf == NULL)
goto err; goto err;
if (bfd_bread (buf, reclen, abfd) != reclen)
{
free (buf);
goto err;
}
map = (struct vms_dcxmap *)buf; map = (struct vms_dcxmap *)buf;
tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs); tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
sbm_off = bfd_getl16 (map->sub0); sbm_off = bfd_getl16 (map->sub0);

View file

@ -452,12 +452,10 @@ wasm_scan (bfd *abfd)
if (bfdsec->size != 0) if (bfdsec->size != 0)
{ {
bfdsec->contents = bfd_alloc (abfd, bfdsec->size); bfdsec->contents = _bfd_alloc_and_read (abfd, bfdsec->size,
bfdsec->size);
if (!bfdsec->contents) if (!bfdsec->contents)
goto error_return; goto error_return;
if (bfd_bread (bfdsec->contents, bfdsec->size, abfd) != bfdsec->size)
goto error_return;
} }
vma += bfdsec->size; vma += bfdsec->size;

View file

@ -1317,14 +1317,17 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
{ {
bfd_byte *linenos; bfd_byte *linenos;
amt = linesz * o->lineno_count; if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
linenos = bfd_malloc (amt); goto error_return;
if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
{
bfd_set_error (bfd_error_file_too_big);
goto error_return;
}
linenos = _bfd_malloc_and_read (abfd, amt, amt);
if (linenos == NULL) if (linenos == NULL)
goto error_return; goto error_return;
reloc_info[o->target_index].linenos = linenos; reloc_info[o->target_index].linenos = linenos;
if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
|| bfd_bread (linenos, amt, abfd) != amt)
goto error_return;
} }
} }

View file

@ -127,24 +127,12 @@ bfd_sym_valid (bfd *abfd)
unsigned char * unsigned char *
bfd_sym_read_name_table (bfd *abfd, bfd_sym_header_block *dshb) bfd_sym_read_name_table (bfd *abfd, bfd_sym_header_block *dshb)
{ {
unsigned char *rstr;
long ret;
size_t table_size = dshb->dshb_nte.dti_page_count * dshb->dshb_page_size; size_t table_size = dshb->dshb_nte.dti_page_count * dshb->dshb_page_size;
size_t table_offset = dshb->dshb_nte.dti_first_page * dshb->dshb_page_size; size_t table_offset = dshb->dshb_nte.dti_first_page * dshb->dshb_page_size;
rstr = bfd_alloc (abfd, table_size); if (bfd_seek (abfd, table_offset, SEEK_SET) != 0)
if (rstr == NULL) return FALSE;
return rstr; return _bfd_alloc_and_read (abfd, table_size, table_size);
bfd_seek (abfd, table_offset, SEEK_SET);
ret = bfd_bread (rstr, table_size, abfd);
if (ret < 0 || (unsigned long) ret != table_size)
{
bfd_release (abfd, rstr);
return NULL;
}
return rstr;
} }
void void
@ -1808,24 +1796,13 @@ bfd_sym_print_type_information_table_entry (bfd *abfd,
fprintf (f, "\n "); fprintf (f, "\n ");
buf = malloc (entry->physical_size); if (bfd_seek (abfd, entry->offset, SEEK_SET) != 0
if (buf == NULL) || (buf = _bfd_malloc_and_read (abfd, entry->physical_size,
entry->physical_size)) == NULL)
{ {
fprintf (f, "[ERROR]\n"); fprintf (f, "[ERROR]\n");
return; return;
} }
if (bfd_seek (abfd, entry->offset, SEEK_SET) < 0)
{
fprintf (f, "[ERROR]\n");
free (buf);
return;
}
if (bfd_bread (buf, entry->physical_size, abfd) != entry->physical_size)
{
fprintf (f, "[ERROR]\n");
free (buf);
return;
}
fprintf (f, "["); fprintf (f, "[");
for (i = 0; i < entry->physical_size; i++) for (i = 0; i < entry->physical_size; i++)