aboutsummaryrefslogtreecommitdiff
path: root/bfd/som.c
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2020-02-19 13:16:01 +1030
committerAlan Modra <amodra@gmail.com>2020-02-19 14:00:55 +1030
commit2bb3687ba8720558082d1575823868286d7916b5 (patch)
tree45bb12014f7fabd8963cc4b58bd537768d2dbbca /bfd/som.c
parent806470a219e84665a59fc6be632d4ed6a4ad908b (diff)
downloadgdb-2bb3687ba8720558082d1575823868286d7916b5.zip
gdb-2bb3687ba8720558082d1575823868286d7916b5.tar.gz
gdb-2bb3687ba8720558082d1575823868286d7916b5.tar.bz2
_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.
Diffstat (limited to 'bfd/som.c')
-rw-r--r--bfd/som.c73
1 files changed, 24 insertions, 49 deletions
diff --git a/bfd/som.c b/bfd/som.c
index bd68592..9f37b10 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;