From 2bb3687ba8720558082d1575823868286d7916b5 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 19 Feb 2020 13:16:01 +1030 Subject: [PATCH] _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. --- bfd/ChangeLog | 52 ++++++++++++++++++++++++++++++++ bfd/aoutx.h | 20 +++---------- bfd/archive.c | 21 ++++++------- bfd/archive64.c | 7 ++--- bfd/coff-rs6000.c | 8 ++--- bfd/coff64-rs6000.c | 4 +-- bfd/coffcode.h | 19 ++++-------- bfd/coffgen.c | 62 +++++++++++--------------------------- bfd/ecoff.c | 69 ++++++++++++++++-------------------------- bfd/elf.c | 45 +++++++++------------------- bfd/elf32-m32c.c | 7 ++--- bfd/elf32-rl78.c | 7 ++--- bfd/elf32-rx.c | 7 ++--- bfd/elf64-alpha.c | 7 ++--- bfd/elf64-mips.c | 9 ++---- bfd/elf64-sparc.c | 10 +++---- bfd/elfcode.h | 18 ++++------- bfd/elflink.c | 19 +++++------- bfd/elfxx-mips.c | 7 ++--- bfd/i386lynx.c | 9 +----- bfd/libbfd-in.h | 26 ++++++++++++++++ bfd/libbfd.h | 26 ++++++++++++++++ bfd/lynx-core.c | 21 ++++--------- bfd/mach-o.c | 46 ++++++++++------------------ bfd/pdp11.c | 26 +++++----------- bfd/pef.c | 40 +++++++++++-------------- bfd/peicode.h | 13 ++------ bfd/som.c | 73 +++++++++++++++------------------------------ bfd/vms-alpha.c | 16 +++++----- bfd/vms-lib.c | 7 +---- bfd/wasm-module.c | 6 ++-- bfd/xcofflink.c | 13 ++++---- bfd/xsym.c | 35 ++++------------------ 33 files changed, 314 insertions(+), 441 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 85747ad64f9..f2ddd86a3c9 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,55 @@ +2020-02-19 Alan Modra + + * 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 * aoutx.h (slurp_reloc_table): Allocate reloc_cache after diff --git a/bfd/aoutx.h b/bfd/aoutx.h index 7cc72b4b677..b843357b091 100644 --- a/bfd/aoutx.h +++ b/bfd/aoutx.h @@ -1303,16 +1303,11 @@ aout_get_external_symbols (bfd *abfd) /* We allocate using malloc to make the values easy to free later on. If we put them on the objalloc it might not be 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) return FALSE; - - if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 - || bfd_bread (syms, amt, abfd) != amt) - { - free (syms); - return FALSE; - } #endif 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) return FALSE; - - relocs = bfd_malloc (reloc_size); + relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size); if (relocs == NULL) return FALSE; - if (bfd_bread (relocs, reloc_size, abfd) != reloc_size) - { - free (relocs); - return FALSE; - } - amt = count * sizeof (arelent); reloc_cache = (arelent *) bfd_zmalloc (amt); if (reloc_cache == NULL) diff --git a/bfd/archive.c b/bfd/archive.c index 91127d7e6be..d2a9a65ea9b 100644 --- a/bfd/archive.c +++ b/bfd/archive.c @@ -968,16 +968,11 @@ do_slurp_bsd_armap (bfd *abfd) if (parsed_size < 4) 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) - return FALSE; - - if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_malformed_archive); - byebye: - bfd_release (abfd, raw_armap); return FALSE; } @@ -987,7 +982,8 @@ do_slurp_bsd_armap (bfd *abfd) { /* Probably we're using the wrong byte ordering. */ bfd_set_error (bfd_error_wrong_format); - goto byebye; + bfd_release (abfd, raw_armap); + return FALSE; } rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE; @@ -997,7 +993,10 @@ do_slurp_bsd_armap (bfd *abfd) amt = ardata->symdef_count * sizeof (carsym); ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt); if (!ardata->symdefs) - return FALSE; + { + bfd_release (abfd, raw_armap); + return FALSE; + } for (counter = 0, set = ardata->symdefs; counter < ardata->symdef_count; @@ -1081,10 +1080,8 @@ do_slurp_coff_armap (bfd *abfd) stringbase = ((char *) ardata->symdefs) + carsym_size; /* Allocate and read in the raw offsets. */ - raw_armap = (int *) bfd_alloc (abfd, ptrsize); - if (raw_armap == NULL) - goto release_symdefs; - if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize + raw_armap = (int *) _bfd_alloc_and_read (abfd, ptrsize, ptrsize); + if (raw_armap == NULL || (bfd_bread (stringbase, stringsize, abfd) != stringsize)) { if (bfd_get_error () != bfd_error_system_call) diff --git a/bfd/archive64.c b/bfd/archive64.c index c693ff9d0b1..5cad723ddbb 100644 --- a/bfd/archive64.c +++ b/bfd/archive64.c @@ -108,11 +108,8 @@ _bfd_archive_64_bit_slurp_armap (bfd *abfd) carsyms = ardata->symdefs; stringbase = ((char *) ardata->symdefs) + carsym_size; - raw_armap = (bfd_byte *) bfd_alloc (abfd, ptrsize); - if (raw_armap == NULL) - goto release_symdefs; - - if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize + raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, ptrsize, ptrsize); + if (raw_armap == NULL || bfd_bread (stringbase, stringsize, abfd) != stringsize) { if (bfd_get_error () != bfd_error_system_call) diff --git a/bfd/coff-rs6000.c b/bfd/coff-rs6000.c index 86b4763a10f..c30b360d83f 100644 --- a/bfd/coff-rs6000.c +++ b/bfd/coff-rs6000.c @@ -1267,11 +1267,9 @@ _bfd_xcoff_slurp_armap (bfd *abfd) } /* 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) return FALSE; - if (bfd_bread (contents, sz, abfd) != sz) - return FALSE; /* Ensure strings are NULL terminated so we don't wander off the end of the buffer. */ @@ -1331,11 +1329,9 @@ _bfd_xcoff_slurp_armap (bfd *abfd) } /* 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) return FALSE; - if (bfd_bread (contents, sz, abfd) != sz) - return FALSE; /* Ensure strings are NULL terminated so we don't wander off the end of the buffer. */ diff --git a/bfd/coff64-rs6000.c b/bfd/coff64-rs6000.c index d39fe4c5900..bb546239fc3 100644 --- a/bfd/coff64-rs6000.c +++ b/bfd/coff64-rs6000.c @@ -1940,11 +1940,9 @@ xcoff64_slurp_armap (bfd *abfd) } /* 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) return FALSE; - if (bfd_bread (contents, sz, abfd) != sz) - return FALSE; /* Ensure strings are NULL terminated so we don't wander off the end of the buffer. */ diff --git a/bfd/coffcode.h b/bfd/coffcode.h index 551105dfb3a..cd495dbe618 100644 --- a/bfd/coffcode.h +++ b/bfd/coffcode.h @@ -2222,15 +2222,11 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr) struct internal_syment sym; 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) 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); if (sym.n_sclass == C_FILE) cputype = sym.n_type & 0xff; @@ -4200,7 +4196,6 @@ static void * buy_and_read (bfd *abfd, file_ptr where, bfd_size_type nmemb, bfd_size_type size) { - void *area; size_t 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); return NULL; } - area = bfd_alloc (abfd, amt); - if (!area) - return NULL; - if (bfd_seek (abfd, where, SEEK_SET) != 0 - || bfd_bread (area, amt, abfd) != amt) + if (bfd_seek (abfd, where, SEEK_SET) != 0) return NULL; - return area; + return _bfd_alloc_and_read (abfd, amt, amt); } /* diff --git a/bfd/coffgen.c b/bfd/coffgen.c index 20cee0a74c5..dda98394f11 100644 --- a/bfd/coffgen.c +++ b/bfd/coffgen.c @@ -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 diff --git a/bfd/ecoff.c b/bfd/ecoff.c index 1d9e385b2b5..9e2efac8e3a 100644 --- a/bfd/ecoff.c +++ b/bfd/ecoff.c @@ -465,13 +465,12 @@ ecoff_slurp_symbolic_header (bfd *abfd) } /* 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) 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; (*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; return TRUE; } - raw = bfd_alloc (abfd, raw_size); - if (raw == NULL) - return FALSE; - pos = ecoff_data (abfd)->sym_filepos; pos += backend->debug_swap.external_hdr_size; - if (bfd_seek (abfd, pos, SEEK_SET) != 0 - || bfd_bread (raw, raw_size, abfd) != raw_size) - { - bfd_release (abfd, raw); - return FALSE; - } + if (bfd_seek (abfd, pos, SEEK_SET) != 0) + return FALSE; + raw = _bfd_alloc_and_read (abfd, raw_size, raw_size); + if (raw == NULL) + return FALSE; ecoff_data (abfd)->raw_syments = raw; @@ -1611,7 +1605,7 @@ ecoff_slurp_reloc_table (bfd *abfd, arelent *internal_relocs; bfd_size_type external_reloc_size; bfd_size_type amt; - char *external_relocs; + bfd_byte *external_relocs; arelent *rptr; unsigned int i; @@ -1625,12 +1619,10 @@ ecoff_slurp_reloc_table (bfd *abfd, external_reloc_size = backend->external_reloc_size; 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) 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; amt = section->reloc_count; @@ -2945,15 +2937,11 @@ _bfd_ecoff_slurp_armap (bfd *abfd) parsed_size = mapdata->parsed_size; 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) - return FALSE; - - if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_malformed_archive); - bfd_release (abfd, (void *) raw_armap); 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; /* 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; 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) goto error_return; - if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0 - || bfd_bread (external_ext, esize, abfd) != esize) + if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0) goto error_return; - - ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax); + ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax, + symhdr->issExtMax); if (ssext == NULL && symhdr->issExtMax != 0) 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); if (ssext != NULL) @@ -3775,14 +3759,13 @@ ecoff_final_link_debug_accumulate (bfd *output_bfd, ret = FALSE; \ goto return_something; \ } \ - debug->ptr = (type) bfd_malloc (amt); \ - if (debug->ptr == NULL) \ + if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0) \ { \ ret = FALSE; \ goto return_something; \ } \ - if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0 \ - || bfd_bread (debug->ptr, amt, input_bfd) != amt) \ + debug->ptr = (type) _bfd_malloc_and_read (input_bfd, amt, amt); \ + if (debug->ptr == NULL) \ { \ ret = FALSE; \ 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_relocs_size = external_reloc_size * input_section->reloc_count; - external_relocs = bfd_malloc (external_relocs_size); - if (external_relocs == NULL && external_relocs_size != 0) + if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0) goto error_return; - - if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 - || (bfd_bread (external_relocs, external_relocs_size, input_bfd) - != external_relocs_size)) + external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size, + external_relocs_size); + if (external_relocs == NULL && external_relocs_size != 0) goto error_return; /* Relocate the section contents. */ diff --git a/bfd/elf.c b/bfd/elf.c index a3af7ef2af7..7eb717b7ffa 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -298,14 +298,9 @@ bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) if (shstrtabsize + 1 <= 1 || shstrtabsize > bfd_get_file_size (abfd) || bfd_seek (abfd, offset, SEEK_SET) != 0 - || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) - shstrtab = 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; + || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1, + shstrtabsize)) == NULL) + { /* Once we've failed to read it, make sure we don't keep trying. Otherwise, we'll keep allocating space for the string table over and over. */ @@ -675,10 +670,9 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) shdr->contents = NULL; if (_bfd_mul_overflow (shdr->sh_size, sizeof (*dest) / 4, &amt) - || (shdr->contents = bfd_alloc (abfd, amt)) == NULL || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 - || (bfd_bread (shdr->contents, shdr->sh_size, abfd) - != shdr->sh_size)) + || !(shdr->contents + = _bfd_alloc_and_read (abfd, amt, shdr->sh_size))) { _bfd_error_handler /* xgettext:c-format */ @@ -687,14 +681,6 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) abfd, (uint64_t) shdr->sh_size); bfd_set_error (bfd_error_bad_value); -- 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; } @@ -8612,12 +8598,11 @@ error_return_verref: abfd, (uint64_t) hdr->sh_size); 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 - || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) + if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) + goto error_return_verref; + contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); + if (contents == NULL) goto error_return_verref; if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) @@ -8747,11 +8732,10 @@ error_return_verref: goto error_return; } - contents = (bfd_byte *) bfd_malloc (hdr->sh_size); - if (contents == NULL) + if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) goto error_return_verdef; - if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 - || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) + contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); + if (contents == NULL) goto error_return_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) return FALSE; - buf = (char *) bfd_malloc (size + 1); + buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); if (buf == NULL) 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. */ buf[size] = 0; - if (bfd_bread (buf, size, abfd) != size - || !elf_parse_notes (abfd, buf, size, offset, align)) + if (!elf_parse_notes (abfd, buf, size, offset, align)) { free (buf); return FALSE; diff --git a/bfd/elf32-m32c.c b/bfd/elf32-m32c.c index a4dfd8d3d4e..a357f62b8c2 100644 --- a/bfd/elf32-m32c.c +++ b/bfd/elf32-m32c.c @@ -1499,11 +1499,10 @@ m32c_elf_relax_section bfd_set_error (bfd_error_file_too_big); goto error_return; } - shndx_buf = bfd_malloc (amt); - if (shndx_buf == NULL) + if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0) goto error_return; - if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 - || bfd_bread (shndx_buf, amt, abfd) != amt) + shndx_buf = _bfd_malloc_and_read (abfd, amt, amt); + if (shndx_buf == NULL) goto error_return; shndx_hdr->contents = shndx_buf; } diff --git a/bfd/elf32-rl78.c b/bfd/elf32-rl78.c index 40f8c5881b1..633350faec4 100644 --- a/bfd/elf32-rl78.c +++ b/bfd/elf32-rl78.c @@ -2129,11 +2129,10 @@ rl78_elf_relax_section bfd_set_error (bfd_error_no_memory); goto error_return; } - shndx_buf = bfd_malloc (amt); - if (shndx_buf == NULL) + if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0) goto error_return; - if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 - || bfd_bread (shndx_buf, amt, abfd) != amt) + shndx_buf = _bfd_malloc_and_read (abfd, amt, amt); + if (shndx_buf == NULL) goto error_return; shndx_hdr->contents = shndx_buf; } diff --git a/bfd/elf32-rx.c b/bfd/elf32-rx.c index 0a9900ff21b..bd08eb47d8a 100644 --- a/bfd/elf32-rx.c +++ b/bfd/elf32-rx.c @@ -2066,11 +2066,10 @@ elf32_rx_relax_section (bfd * abfd, bfd_set_error (bfd_error_file_too_big); goto error_return; } - shndx_buf = bfd_malloc (amt); - if (shndx_buf == NULL) + if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0) goto error_return; - if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 - || bfd_bread (shndx_buf, amt, abfd) != amt) + shndx_buf = _bfd_malloc_and_read (abfd, amt, amt); + if (shndx_buf == NULL) goto error_return; shndx_hdr->contents = shndx_buf; } diff --git a/bfd/elf64-alpha.c b/bfd/elf64-alpha.c index ae4be9f0746..ed18202b718 100644 --- a/bfd/elf64-alpha.c +++ b/bfd/elf64-alpha.c @@ -1398,11 +1398,10 @@ elf64_alpha_read_ecoff_info (bfd *abfd, asection *section, bfd_set_error (bfd_error_file_too_big); \ goto error_return; \ } \ - debug->ptr = (type) bfd_malloc (amt); \ - if (debug->ptr == NULL) \ + if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \ goto error_return; \ - if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \ - || bfd_bread (debug->ptr, amt, abfd) != amt) \ + debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \ + if (debug->ptr == NULL) \ goto error_return; \ } while (0) diff --git a/bfd/elf64-mips.c b/bfd/elf64-mips.c index b674d52f7c6..413018fcf3e 100644 --- a/bfd/elf64-mips.c +++ b/bfd/elf64-mips.c @@ -4010,15 +4010,12 @@ mips_elf64_slurp_one_reloc_table (bfd *abfd, asection *asect, int entsize; 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) 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; entsize = rel_hdr->sh_entsize; diff --git a/bfd/elf64-sparc.c b/bfd/elf64-sparc.c index 4addaf052a2..f07c06222ad 100644 --- a/bfd/elf64-sparc.c +++ b/bfd/elf64-sparc.c @@ -79,13 +79,11 @@ elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect, bfd_size_type count; 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) - goto error_return; - - 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; + return FALSE; native_relocs = (bfd_byte *) allocated; diff --git a/bfd/elfcode.h b/bfd/elfcode.h index e7dfdee95e9..1b2d68ab204 100644 --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -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) goto error_return; - - xverbuf = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size); + xverbuf = (Elf_External_Versym *) + _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size); if (xverbuf == NULL && verhdr->sh_size != 0) 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. */ @@ -1429,14 +1426,11 @@ elf_slurp_reloc_table_from_section (bfd *abfd, int entsize; 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) - goto error_return; - - 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; + return FALSE; native_relocs = (bfd_byte *) allocated; diff --git a/bfd/elflink.c b/bfd/elflink.c index 1656978e07e..8e7ae2a160d 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4332,12 +4332,12 @@ error_free_dyn: Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr; 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) 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); } } @@ -9737,16 +9737,11 @@ elf_link_check_versioned_symbol (struct bfd_link_info *info, /* Read in any version definitions. */ 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 - || (bfd_bread (extversym, versymhdr->sh_size, input) - != versymhdr->sh_size)) + || (extversym = (Elf_External_Versym *) + _bfd_malloc_and_read (input, versymhdr->sh_size, + versymhdr->sh_size)) == NULL) { - free (extversym); - error_ret: free (isymbuf); return FALSE; } diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c index 81ed9b390a0..81af3ef33e5 100644 --- a/bfd/elfxx-mips.c +++ b/bfd/elfxx-mips.c @@ -1424,11 +1424,10 @@ _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section, bfd_set_error (bfd_error_file_too_big); \ goto error_return; \ } \ - debug->ptr = bfd_malloc (amt); \ - if (debug->ptr == NULL) \ + if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \ goto error_return; \ - if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \ - || bfd_bread (debug->ptr, amt, abfd) != amt) \ + debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \ + if (debug->ptr == NULL) \ goto error_return; \ } while (0) diff --git a/bfd/i386lynx.c b/bfd/i386lynx.c index 777fd3d72eb..a4d7cd3228b 100644 --- a/bfd/i386lynx.c +++ b/bfd/i386lynx.c @@ -411,20 +411,13 @@ doit: if (!reloc_cache && count != 0) return FALSE; - relocs = bfd_alloc (abfd, reloc_size); + relocs = _bfd_alloc_and_read (abfd, reloc_size, reloc_size); if (!relocs && reloc_size != 0) { free (reloc_cache); 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) { struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs; diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 00650c3c86d..a8f9bcd47d3 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -903,3 +903,29 @@ extern bfd_vma _bfd_safe_read_leb128 #define _bfd_mul_overflow(a, b, res) \ ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a)) #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; +} diff --git a/bfd/libbfd.h b/bfd/libbfd.h index 7fcd46a38a3..77c66309a25 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -908,6 +908,32 @@ extern bfd_vma _bfd_safe_read_leb128 #define _bfd_mul_overflow(a, b, res) \ ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a)) #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. */ bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int); diff --git a/bfd/lynx-core.c b/bfd/lynx-core.c index ef47c8359c7..66b54a2fd27 100644 --- a/bfd/lynx-core.c +++ b/bfd/lynx-core.c @@ -126,24 +126,13 @@ lynx_core_file_p (bfd *abfd) tcontext_size = pss.threadcnt * sizeof (core_st_t); - /* Allocate space for the thread contexts */ - - threadp = (core_st_t *) bfd_alloc (abfd, tcontext_size); - if (!threadp) - goto fail; - /* Save thread contexts */ - - if (bfd_seek (abfd, (file_ptr) pagesize, SEEK_SET) != 0) + if (bfd_seek (abfd, pagesize, SEEK_SET) != 0) + goto fail; + threadp = (core_st_t *) _bfd_alloc_and_read (abfd, tcontext_size, + tcontext_size); + if (!threadp) 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; diff --git a/bfd/mach-o.c b/bfd/mach-o.c index fd9f480245c..887dfc76ce6 100644 --- a/bfd/mach-o.c +++ b/bfd/mach-o.c @@ -1623,15 +1623,13 @@ bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos, if (native_size < count) goto err; - native_relocs = - (struct mach_o_reloc_info_external *) bfd_malloc (native_size); + if (bfd_seek (abfd, filepos, SEEK_SET) != 0) + return -1; + native_relocs = (struct mach_o_reloc_info_external *) + _bfd_malloc_and_read (abfd, native_size, native_size); if (native_relocs == NULL) 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++) { 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. */ if ((sym->strsize + 1) == 0) 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) 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. */ sym->strtab[sym->strsize] = 0; } @@ -4000,15 +3992,9 @@ bfd_mach_o_ppc_flavour_string (unsigned int flavour) static unsigned char * bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, size_t size) { - unsigned char *buf; - - buf = bfd_alloc (abfd, size); - if (buf == NULL) + if (bfd_seek (abfd, filepos, SEEK_SET) != 0) return NULL; - if (bfd_seek (abfd, filepos, SEEK_SET) != 0 - || bfd_bread (buf, size, abfd) != size) - return NULL; - return buf; + return _bfd_alloc_and_read (abfd, size, size); } static bfd_boolean @@ -4100,11 +4086,9 @@ bfd_mach_o_read_prebound_dylib (bfd *abfd, return FALSE; 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) return FALSE; - if (bfd_bread (str, str_len, abfd) != str_len) - return FALSE; cmd->name_offset = command->offset + nameoff; 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. */ if (cmd->rebase_content == NULL && cmd->rebase_size != 0) { - cmd->rebase_content = - bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size); + cmd->rebase_content + = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size); if (cmd->rebase_content == NULL) return FALSE; } @@ -4590,8 +4574,8 @@ bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) /* Read bind content. */ if (cmd->bind_content == NULL && cmd->bind_size != 0) { - cmd->bind_content = - bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size); + cmd->bind_content + = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size); if (cmd->bind_content == NULL) return FALSE; } diff --git a/bfd/pdp11.c b/bfd/pdp11.c index cd14e87b962..35aefa34047 100644 --- a/bfd/pdp11.c +++ b/bfd/pdp11.c @@ -1182,17 +1182,13 @@ aout_get_external_symbols (bfd *abfd) /* We allocate using malloc to make the values easy to free later on. If we put them on the objalloc it might not be 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) 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 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) return FALSE; - - each_size = obj_reloc_entry_size (abfd); - - relocs = bfd_malloc (reloc_size); + relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size); if (relocs == NULL && reloc_size != 0) return FALSE; - if (bfd_bread (relocs, reloc_size, abfd) != reloc_size) - { - free (relocs); - return FALSE; - } - + each_size = obj_reloc_entry_size (abfd); count = reloc_size / each_size; /* Count the number of NON-ZERO relocs, this is the count we want. */ diff --git a/bfd/pef.c b/bfd/pef.c index 574d9bcb5d6..b0f6c2d2a7c 100644 --- a/bfd/pef.c +++ b/bfd/pef.c @@ -446,14 +446,15 @@ bfd_pef_print_loader_section (bfd *abfd, FILE *file) return -1; 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) return -1; - if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0 - || bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen - || loaderlen < 56 - || bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header) < 0) + if (bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header) < 0) { free (loaderbuf); return -1; @@ -479,17 +480,14 @@ bfd_pef_scan_start_address (bfd *abfd) goto end; loaderlen = loadersec->size; - loaderbuf = bfd_malloc (loaderlen); - if (loaderbuf == NULL) - goto end; - - if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0) + if (loaderlen < 56) goto error; - if (bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen) + if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0) goto error; - - if (loaderlen < 56) + loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen); + if (loaderbuf == NULL) goto error; + ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header); if (ret < 0) goto error; @@ -903,12 +901,10 @@ bfd_pef_parse_symbols (bfd *abfd, asymbol **csym) if (codesec != NULL) { codelen = codesec->size; - codebuf = bfd_malloc (codelen); - if (codebuf == NULL) - goto end; - if (bfd_seek (abfd, codesec->filepos, SEEK_SET) < 0) + if (bfd_seek (abfd, codesec->filepos, SEEK_SET) != 0) goto end; - if (bfd_bread ((void *) codebuf, codelen, abfd) != codelen) + codebuf = _bfd_malloc_and_read (abfd, codelen, codelen); + if (codebuf == NULL) goto end; } @@ -916,12 +912,10 @@ bfd_pef_parse_symbols (bfd *abfd, asymbol **csym) if (loadersec != NULL) { loaderlen = loadersec->size; - loaderbuf = bfd_malloc (loaderlen); - if (loaderbuf == NULL) + if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0) goto end; - if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0) - goto end; - if (bfd_bread ((void *) loaderbuf, loaderlen, abfd) != loaderlen) + loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen); + if (loaderbuf == NULL) goto end; } diff --git a/bfd/peicode.h b/bfd/peicode.h index 3f772a0785a..f569ccf3893 100644 --- a/bfd/peicode.h +++ b/bfd/peicode.h @@ -1273,16 +1273,10 @@ pe_ILF_object_p (bfd * abfd) /* ptr += 2; */ /* 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) return NULL; - if (bfd_bread (ptr, size, abfd) != size) - { - bfd_release (abfd, ptr); - return NULL; - } - symbol_name = (char *) ptr; /* See PR 20905 for an example of where the strnlen is necessary. */ source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1; @@ -1494,12 +1488,9 @@ pe_bfd_object_p (bfd * abfd) if (amt < sizeof (PEAOUTHDR)) amt = sizeof (PEAOUTHDR); - opthdr = bfd_alloc (abfd, amt); + opthdr = _bfd_alloc_and_read (abfd, amt, opt_hdr_size); if (opthdr == NULL) return NULL; - if (bfd_bread (opthdr, opt_hdr_size, abfd) - != (bfd_size_type) opt_hdr_size) - return NULL; if (amt > opt_hdr_size) memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size); diff --git a/bfd/som.c b/bfd/som.c index bd685920ba7..9f37b10f96c 100644 --- a/bfd/som.c +++ b/bfd/som.c @@ -2088,14 +2088,11 @@ setup_sections (bfd *abfd, bfd_set_error (bfd_error_no_memory); 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, SEEK_SET) != 0) 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; /* Make sure that the string table is NUL terminated. */ space_strings[amt] = 0; @@ -4578,15 +4575,11 @@ som_slurp_string_table (bfd *abfd) } /* 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) return FALSE; - - if (bfd_bread (stringtab, amt, abfd) != amt) + amt = obj_som_stringtab_size (abfd); + stringtab = (char *) _bfd_malloc_and_read (abfd, amt, amt); + if (stringtab == NULL) return FALSE; /* Save our results and return success. */ @@ -4681,12 +4674,11 @@ som_slurp_symbol_table (bfd *abfd) bfd_set_error (bfd_error_file_too_big); 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) 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; 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. */ 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. */ - if (bfd_seek (abfd, - obj_som_reloc_filepos (abfd) + section->rel_filepos, - SEEK_SET) - != 0) + if (bfd_seek (abfd, obj_som_reloc_filepos (abfd) + section->rel_filepos, + SEEK_SET) != 0) 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; /* 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); + /* 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)) { bfd_set_error (bfd_error_file_too_big); 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) goto error_return; /* Don't forget to initialize the counter! */ *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 chain. */ 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; 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)) { bfd_set_error (bfd_error_file_too_big); 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) 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 in the carsym's filepos field. */ 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); 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) goto error_return; - if (bfd_bread ((void *) som_dict, amt, abfd) != amt) - goto error_return; - string_loc = lst_header->string_loc; /* 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); goto error_return; } - name = bfd_zalloc (abfd, (bfd_size_type) len + 1); + name = (char *) _bfd_alloc_and_read (abfd, len + 1, len); if (!name) goto error_return; - if (bfd_bread (name, (bfd_size_type) len, abfd) != len) - goto error_return; - name[len] = 0; set->name = name; @@ -6148,12 +6126,9 @@ som_bfd_fill_in_ar_symbols (bfd *abfd, bfd_set_error (bfd_error_no_memory); goto error_return; } - name = bfd_zalloc (abfd, (bfd_size_type) len + 1); + name = (char *) _bfd_alloc_and_read (abfd, len + 1, len); if (!name) goto error_return; - - if (bfd_bread (name, (bfd_size_type) len, abfd) != len) - goto error_return; name[len] = 0; set->name = name; diff --git a/bfd/vms-alpha.c b/bfd/vms-alpha.c index 24842780b6d..5be475a53f3 100644 --- a/bfd/vms-alpha.c +++ b/bfd/vms-alpha.c @@ -4822,10 +4822,10 @@ module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr, { unsigned int size = module->size; 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 - || bfd_bread (buffer, size, abfd) != size) + || (buffer = _bfd_malloc_and_read (abfd, size, size)) == NULL) { bfd_set_error (bfd_error_no_debug_section); return FALSE; @@ -7149,8 +7149,8 @@ evax_bfd_print_dst (struct bfd *abfd, unsigned int dst_size, FILE *file) dst_size -= len; off += len; len -= sizeof (dsth); - buf = bfd_malloc (len); - if (bfd_bread (buf, len, abfd) != len) + buf = _bfd_malloc_and_read (abfd, len, len); + if (buf == NULL) { fprintf (file, _("cannot read DST symbol\n")); return; @@ -8065,14 +8065,12 @@ evax_bfd_print_image (bfd *abfd, FILE *file) unsigned int codeadroff; unsigned int lpfixoff; unsigned int chgprtoff; + file_ptr f_off = (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE; - buf = bfd_malloc (eiaf_size); - - if (bfd_seek (abfd, (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) - || bfd_bread (buf, eiaf_size, abfd) != eiaf_size) + if (bfd_seek (abfd, f_off, SEEK_SET) != 0 + || (buf = _bfd_malloc_and_read (abfd, eiaf_size, eiaf_size)) == NULL) { fprintf (file, _("cannot read EIHA\n")); - free (buf); return; } eiaf = (struct vms_eiaf *)buf; diff --git a/bfd/vms-lib.c b/bfd/vms-lib.c index dc07e79710c..6ae1a7bafbd 100644 --- a/bfd/vms-lib.c +++ b/bfd/vms-lib.c @@ -568,14 +568,9 @@ _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind) != sizeof (buf_reclen)) goto err; reclen = bfd_getl32 (buf_reclen); - buf = bfd_malloc (reclen); + buf = _bfd_malloc_and_read (abfd, reclen, reclen); if (buf == NULL) goto err; - if (bfd_bread (buf, reclen, abfd) != reclen) - { - free (buf); - goto err; - } map = (struct vms_dcxmap *)buf; tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs); sbm_off = bfd_getl16 (map->sub0); diff --git a/bfd/wasm-module.c b/bfd/wasm-module.c index 315d3effb8d..fa8648b9acd 100644 --- a/bfd/wasm-module.c +++ b/bfd/wasm-module.c @@ -452,12 +452,10 @@ wasm_scan (bfd *abfd) 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) goto error_return; - - if (bfd_bread (bfdsec->contents, bfdsec->size, abfd) != bfdsec->size) - goto error_return; } vma += bfdsec->size; diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c index ff4b4837032..2560d2ca427 100644 --- a/bfd/xcofflink.c +++ b/bfd/xcofflink.c @@ -1317,14 +1317,17 @@ xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info) { bfd_byte *linenos; - amt = linesz * o->lineno_count; - linenos = bfd_malloc (amt); + if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0) + 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) goto error_return; 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; } } diff --git a/bfd/xsym.c b/bfd/xsym.c index 9c714c64809..3071a9de3f7 100644 --- a/bfd/xsym.c +++ b/bfd/xsym.c @@ -127,24 +127,12 @@ bfd_sym_valid (bfd *abfd) unsigned char * 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_offset = dshb->dshb_nte.dti_first_page * dshb->dshb_page_size; - rstr = bfd_alloc (abfd, table_size); - if (rstr == NULL) - return rstr; - - 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; + if (bfd_seek (abfd, table_offset, SEEK_SET) != 0) + return FALSE; + return _bfd_alloc_and_read (abfd, table_size, table_size); } void @@ -1808,22 +1796,11 @@ bfd_sym_print_type_information_table_entry (bfd *abfd, fprintf (f, "\n "); - buf = malloc (entry->physical_size); - if (buf == NULL) - { - fprintf (f, "[ERROR]\n"); - 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) + if (bfd_seek (abfd, entry->offset, SEEK_SET) != 0 + || (buf = _bfd_malloc_and_read (abfd, entry->physical_size, + entry->physical_size)) == NULL) { fprintf (f, "[ERROR]\n"); - free (buf); return; } -- 2.39.2