+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>
* aoutx.h (slurp_reloc_table): Allocate reloc_cache after
/* 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;
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)
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;
}
{
/* 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;
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;
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)
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)
}
/* 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. */
}
/* 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. */
}
/* 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. */
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;
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))
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);
}
/*
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))
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);
{
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);
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;
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
}
/* 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);
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;
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;
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;
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;
}
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)
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; \
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. */
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. */
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 */
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;
}
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))
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)
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;
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;
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;
}
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;
}
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;
}
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)
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;
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;
{
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. */
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;
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);
}
}
/* 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;
}
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)
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;
#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;
+}
#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);
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;
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],
/* 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;
}
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
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);
/* 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;
}
/* 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;
}
/* 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;
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. */
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;
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;
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;
}
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;
}
/* 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;
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);
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;
}
/* 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. */
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))
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.
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++)
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)
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. */
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;
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;
{
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;
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;
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;
!= 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);
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;
{
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;
}
}
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
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;
}