From 80425e6c822142cc43999dece179009412b7f190 Mon Sep 17 00:00:00 2001 From: Jim Kingdon Date: Thu, 10 Mar 1994 02:09:10 +0000 Subject: [PATCH] * libbfd-in.h: Remove alloca cruft. It was missing some necessary cruft (like the #pragma alloca for AIX). In addition to that problem, the C alloca calls xmalloc, which means checking for being out of memory can't work right. The following changes remove all uses of alloca from BFD. * hosts/solaris2.h: Remove alloca cruft. * som.c: Replace alloca with a fixed size auto array. * aoutx.h, elfcode.h, nlmcode.h, bout.c, coff-alpha.c, ecoff.c, ecofflink.c, elf32-hppa.c, elf32-mips.c, linker.c, reloc.c, som.c: Replace alloca with malloc and appropriate error checking and freeing. * linker.c: Replace alloca with obstack_alloc. * libbfd.h: Rebuilt. --- bfd/ChangeLog | 16 +++ bfd/aoutx.h | 87 +++++++++---- bfd/bout.c | 51 ++++++-- bfd/ecoff.c | 268 +++++++++++++++++++++++++++----------- bfd/elf32-hppa.c | 40 +++++- bfd/elfcode.h | 40 +++++- bfd/hosts/solaris2.h | 4 - bfd/libbfd-in.h | 17 --- bfd/libbfd.h | 19 +-- bfd/nlmcode.h | 189 +++++++++++++++------------ bfd/reloc.c | 52 +++++--- bfd/som.c | 301 ++++++++++++++++++++++++++++++------------- 12 files changed, 729 insertions(+), 355 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index e1b13203613..50c523d7e2b 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,19 @@ +Wed Mar 9 17:17:53 1994 Jim Kingdon (kingdon@lioth.cygnus.com) + + * libbfd-in.h: Remove alloca cruft. It was missing some necessary + cruft (like the #pragma alloca for AIX). + In addition to that problem, the C alloca calls xmalloc, which + means checking for being out of memory can't work right. The + following changes remove all uses of alloca from BFD. + * hosts/solaris2.h: Remove alloca cruft. + * som.c: Replace alloca with a fixed size auto array. + * aoutx.h, elfcode.h, nlmcode.h, bout.c, coff-alpha.c, ecoff.c, + ecofflink.c, elf32-hppa.c, elf32-mips.c, linker.c, reloc.c, som.c: + Replace alloca with malloc and appropriate error checking and + freeing. + * linker.c: Replace alloca with obstack_alloc. + * libbfd.h: Rebuilt. + Tue Mar 8 12:10:38 1994 Ian Lance Taylor (ian@cygnus.com) * coff-mips.c (mips_relocate_section): Handle MIPS_R_LITERAL like diff --git a/bfd/aoutx.h b/bfd/aoutx.h index aa492392c34..029618c725f 100644 --- a/bfd/aoutx.h +++ b/bfd/aoutx.h @@ -3482,13 +3482,6 @@ NAME(aout,final_link) (abfd, info, callback) p != (struct bfd_link_order *) NULL; p = p->next) { - /* If we might be using the C based alloca function, we need - to dump the memory allocated by aout_link_input_bfd. */ -#ifndef __GNUC__ -#ifndef alloca - (void) alloca (0); -#endif -#endif if (p->type == bfd_indirect_link_order && (bfd_get_flavour (p->u.indirect.section->owner) == bfd_target_aout_flavour)) @@ -3541,7 +3534,7 @@ aout_link_input_bfd (finfo, input_bfd) bfd *input_bfd; { bfd_size_type sym_count; - int *symbol_map; + int *symbol_map = NULL; BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object); @@ -3551,11 +3544,16 @@ aout_link_input_bfd (finfo, input_bfd) return false; sym_count = obj_aout_external_sym_count (input_bfd); - symbol_map = (int *) alloca ((size_t) sym_count * sizeof (int)); + symbol_map = (int *) malloc ((size_t) sym_count * sizeof (int)); + if (symbol_map == NULL) + { + bfd_set_error (bfd_error_no_memory); + return false; + } /* Write out the symbols and get a map of the new indices. */ if (! aout_link_write_symbols (finfo, input_bfd, symbol_map)) - return false; + goto error_return; /* Relocate and write out the sections. */ if (! aout_link_input_section (finfo, input_bfd, @@ -3568,7 +3566,7 @@ aout_link_input_bfd (finfo, input_bfd) &finfo->dreloff, exec_hdr (input_bfd)->a_drsize, symbol_map)) - return false; + goto error_return; /* If we are not keeping memory, we don't need the symbols any longer. We still need them if we are keeping memory, because the @@ -3576,10 +3574,16 @@ aout_link_input_bfd (finfo, input_bfd) if (! finfo->info->keep_memory) { if (! aout_link_free_symbols (input_bfd)) - return false; + goto error_return; } + if (symbol_map != NULL) + free (symbol_map); return true; + error_return: + if (symbol_map != NULL) + free (symbol_map); + return false; } /* Adjust and write out the symbols for an a.out file. Set the new @@ -3596,7 +3600,7 @@ aout_link_write_symbols (finfo, input_bfd, symbol_map) char *strings; enum bfd_link_strip strip; enum bfd_link_discard discard; - struct external_nlist *output_syms; + struct external_nlist *output_syms = NULL; struct external_nlist *outsym; register struct external_nlist *sym; struct external_nlist *sym_end; @@ -3610,7 +3614,12 @@ aout_link_write_symbols (finfo, input_bfd, symbol_map) strip = finfo->info->strip; discard = finfo->info->discard; output_syms = ((struct external_nlist *) - alloca ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE)); + malloc ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE)); + if (output_syms == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } outsym = output_syms; /* First write out a symbol for this object file, unless we are @@ -3876,16 +3885,22 @@ aout_link_write_symbols (finfo, input_bfd, symbol_map) bfd_size_type outsym_count; if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0) - return false; + goto error_return; outsym_count = outsym - output_syms; if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE, (bfd_size_type) outsym_count, output_bfd) != outsym_count * EXTERNAL_NLIST_SIZE) - return false; + goto error_return; finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE; } + if (output_syms != NULL) + free (output_syms); return true; + error_return: + if (output_syms != NULL) + free (output_syms); + return false; } /* Write out a symbol that was not associated with an a.out input @@ -3993,21 +4008,31 @@ aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr, int *symbol_map; { bfd_size_type input_size; - bfd_byte *contents; - PTR relocs; + bfd_byte *contents = NULL; + PTR relocs = NULL; /* Get the section contents. */ input_size = bfd_section_size (input_bfd, input_section); - contents = (bfd_byte *) alloca (input_size); + contents = (bfd_byte *) malloc (input_size); + if (contents == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents, (file_ptr) 0, input_size)) - return false; + goto error_return; /* Read in the relocs. */ - relocs = (PTR) alloca (rel_size); + relocs = (PTR) malloc (rel_size); + if (relocs == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size) - return false; + goto error_return; /* Relocate the section contents. */ if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE) @@ -4015,7 +4040,7 @@ aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr, if (! aout_link_input_section_std (finfo, input_bfd, input_section, (struct reloc_std_external *) relocs, rel_size, contents, symbol_map)) - return false; + goto error_return; } else { @@ -4031,17 +4056,17 @@ aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr, (PTR) contents, input_section->output_offset, input_size)) - return false; + goto error_return; /* If we are producing relocateable output, the relocs were modified, and we now write them out. */ if (finfo->info->relocateable) { if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0) - return false; + goto error_return; if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd) != rel_size) - return false; + goto error_return; *reloff_ptr += rel_size; /* Assert that the relocs have not run into the symbols, and @@ -4053,7 +4078,17 @@ aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr, <= obj_datasec (finfo->output_bfd)->rel_filepos))); } + if (relocs != NULL) + free (relocs); + if (contents != NULL) + free (contents); return true; + error_return: + if (relocs != NULL) + free (relocs); + if (contents != NULL) + free (contents); + return false; } /* Get the section corresponding to a reloc index. */ diff --git a/bfd/bout.c b/bfd/bout.c index fa9bb7cbd0a..fbe51a337ab 100644 --- a/bfd/bout.c +++ b/bfd/bout.c @@ -118,14 +118,14 @@ b_out_object_p (abfd) if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) != EXEC_BYTES_SIZE) { - bfd_error = wrong_format; + bfd_set_error (bfd_error_wrong_format); return 0; } anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); if (N_BADMAG (anexec)) { - bfd_error = wrong_format; + bfd_set_error (bfd_error_wrong_format); return 0; } @@ -199,7 +199,7 @@ b_out_mkobject (abfd) rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct)); if (rawptr == NULL) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } @@ -465,7 +465,7 @@ b_out_slurp_reloc_table (abfd, asect, symbols) goto doit; } - bfd_error = invalid_operation; + bfd_set_error (bfd_error_invalid_operation); return false; doit: @@ -474,18 +474,18 @@ b_out_slurp_reloc_table (abfd, asect, symbols) relocs = (struct relocation_info *) malloc (reloc_size); if (!relocs) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } reloc_cache = (arelent *) malloc ((count+1) * sizeof (arelent)); if (!reloc_cache) { free ((char*)relocs); - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); free (reloc_cache); free (relocs); return false; @@ -670,7 +670,7 @@ b_out_squirt_out_relocs (abfd, section) generic = section->orelocation; native = ((struct relocation_info *) malloc (natsize)); if (!native) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } @@ -821,7 +821,7 @@ b_out_get_reloc_upper_bound (abfd, asect) sec_ptr asect; { if (bfd_get_format (abfd) != bfd_object) { - bfd_error = invalid_operation; + bfd_set_error (bfd_error_invalid_operation); return 0; } @@ -838,7 +838,7 @@ b_out_get_reloc_upper_bound (abfd, asect) if (asect == obj_bsssec (abfd)) return 0; - bfd_error = invalid_operation; + bfd_set_error (bfd_error_invalid_operation); return 0; } @@ -854,7 +854,7 @@ b_out_set_section_contents (abfd, section, location, offset, count) if (abfd->output_has_begun == false) { /* set by bfd.c handler */ if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*|| (obj_textsec (abfd)->_cooked_size == 0) || (obj_datasec (abfd)->_cooked_size == 0)*/) { - bfd_error = invalid_operation; + bfd_set_error (bfd_error_invalid_operation); return false; } @@ -1083,13 +1083,19 @@ b_out_relax_section (abfd, i, link_info, symbols) asection *input_section = i; int shrink = 0 ; boolean new = false; + arelent **reloc_vector = NULL; bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section); if (reloc_size) { - arelent **reloc_vector = (arelent **)alloca(reloc_size); + reloc_vector = (arelent **) malloc (reloc_size); + if (reloc_vector == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } /* Get the relocs and think about them */ if (bfd_canonicalize_reloc(input_bfd, input_section, reloc_vector, @@ -1120,7 +1126,13 @@ b_out_relax_section (abfd, i, link_info, symbols) } input_section->_cooked_size = input_section->_raw_size - shrink; + if (reloc_vector != NULL) + free (reloc_vector); return new; + error_return: + if (reloc_vector != NULL) + free (reloc_vector); + return false; } static bfd_byte * @@ -1138,7 +1150,7 @@ b_out_get_relocated_section_contents (in_abfd, link_info, link_order, data, asection *input_section = link_order->u.indirect.section; bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section); - arelent **reloc_vector = (arelent **)alloca(reloc_size); + arelent **reloc_vector = NULL; /* If producing relocateable output, don't bother to relax. */ if (relocateable) @@ -1147,6 +1159,13 @@ b_out_get_relocated_section_contents (in_abfd, link_info, link_order, data, data, relocateable, symbols); + reloc_vector = (arelent **) malloc (reloc_size); + if (reloc_vector == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + input_section->reloc_done = 1; /* read in the section */ @@ -1280,7 +1299,13 @@ b_out_get_relocated_section_contents (in_abfd, link_info, link_order, data, } } } + if (reloc_vector != NULL) + free (reloc_vector); return data; + error_return: + if (reloc_vector != NULL) + free (reloc_vector); + return NULL; } /***********************************************************************/ diff --git a/bfd/ecoff.c b/bfd/ecoff.c index 552be694206..745d46a33e9 100644 --- a/bfd/ecoff.c +++ b/bfd/ecoff.c @@ -606,7 +606,7 @@ ecoff_slurp_symbolic_header (abfd) { const struct ecoff_backend_data * const backend = ecoff_backend (abfd); bfd_size_type external_hdr_size; - PTR raw; + PTR raw = NULL; HDRR *internal_symhdr; /* See if we've already read it in. */ @@ -633,13 +633,19 @@ ecoff_slurp_symbolic_header (abfd) } /* Read the symbolic information header. */ - raw = (PTR) alloca ((size_t) external_hdr_size); + raw = (PTR) malloc ((size_t) external_hdr_size); + if (raw == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1 || (bfd_read (raw, external_hdr_size, 1, abfd) != external_hdr_size)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr); @@ -647,14 +653,20 @@ ecoff_slurp_symbolic_header (abfd) if (internal_symhdr->magic != backend->debug_swap.sym_magic) { bfd_set_error (bfd_error_bad_value); - return false; + goto error_return; } /* Now we can get the correct number of symbols. */ bfd_get_symcount (abfd) = (internal_symhdr->isymMax + internal_symhdr->iextMax); + if (raw != NULL) + free (raw); return true; + error_return: + if (raw != NULL) + free (raw); + return false; } /* Read in and swap the important symbolic information for an ECOFF @@ -2484,7 +2496,8 @@ ecoff_write_object_contents (abfd) bfd_size_type data_size; bfd_vma data_start; bfd_size_type bss_size; - PTR buff; + PTR buff = NULL; + PTR reloc_buff = NULL; struct internal_filehdr internal_f; struct internal_aouthdr internal_a; int i; @@ -2517,10 +2530,26 @@ ecoff_write_object_contents (abfd) /* Write section headers to the file. */ - buff = (PTR) alloca (scnhsz); + /* Allocate buff big enough to hold a section header, + file header, or a.out header. */ + { + bfd_size_type siz; + siz = scnhsz; + if (siz < filhsz) + siz = filhsz; + if (siz < aoutsz) + siz = aoutsz; + buff = (PTR) malloc (siz); + if (buff == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + } + internal_f.f_nscns = 0; if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0) - return false; + goto error_return; for (current = abfd->sections; current != (asection *) NULL; current = current->next) @@ -2581,7 +2610,7 @@ ecoff_write_object_contents (abfd) bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff); if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz) - return false; + goto error_return; if ((section.s_flags & STYP_TEXT) != 0 || ((section.s_flags & STYP_RDATA) != 0 @@ -2700,17 +2729,15 @@ ecoff_write_object_contents (abfd) /* Write out the file header and the optional header. */ if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) - return false; + goto error_return; - buff = (PTR) alloca (filhsz); bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff); if (bfd_write (buff, 1, filhsz, abfd) != filhsz) - return false; + goto error_return; - buff = (PTR) alloca (aoutsz); bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff); if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz) - return false; + goto error_return; /* Build the external symbol information. This must be done before writing out the relocs so that we know the symbol indices. The @@ -2728,7 +2755,7 @@ ecoff_write_object_contents (abfd) ? true : false), ecoff_get_extr, ecoff_set_index) == false) - return false; + goto error_return; /* Write out the relocs. */ for (current = abfd->sections; @@ -2742,16 +2769,17 @@ ecoff_write_object_contents (abfd) if (current->reloc_count == 0) continue; - buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size); - if (buff == NULL) + reloc_buff = + bfd_alloc (abfd, current->reloc_count * external_reloc_size); + if (reloc_buff == NULL) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } reloc_ptr_ptr = current->orelocation; reloc_end = reloc_ptr_ptr + current->reloc_count; - out_ptr = (char *) buff; + out_ptr = (char *) reloc_buff; for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr += external_reloc_size) @@ -2818,11 +2846,13 @@ ecoff_write_object_contents (abfd) } if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0) - return false; - if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd) + goto error_return; + if (bfd_write (reloc_buff, + external_reloc_size, current->reloc_count, abfd) != external_reloc_size * current->reloc_count) - return false; - bfd_release (abfd, buff); + goto error_return; + bfd_release (abfd, reloc_buff); + reloc_buff = NULL; } /* Write out the symbolic debugging information. */ @@ -2832,7 +2862,7 @@ ecoff_write_object_contents (abfd) if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap, ecoff_data (abfd)->sym_filepos) == false) - return false; + goto error_return; } } @@ -2848,17 +2878,27 @@ ecoff_write_object_contents (abfd) if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0) - return false; + goto error_return; if (bfd_read (&c, 1, 1, abfd) == 0) c = 0; if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0) - return false; + goto error_return; if (bfd_write (&c, 1, 1, abfd) != 1) - return false; + goto error_return; } + if (reloc_buff != NULL) + bfd_release (abfd, reloc_buff); + if (buff != NULL) + free (buff); return true; + error_return: + if (reloc_buff != NULL) + bfd_release (abfd, reloc_buff); + if (buff != NULL) + free (buff); + return false; } /* Archive handling. ECOFF uses what appears to be a unique type of @@ -3589,35 +3629,47 @@ ecoff_link_check_archive_element (abfd, info, pneeded) = backend->debug_swap.swap_ext_in; HDRR *symhdr; bfd_size_type external_ext_size; - PTR external_ext; + PTR external_ext = NULL; size_t esize; - char *ssext; + char *ssext = NULL; char *ext_ptr; char *ext_end; *pneeded = false; if (! ecoff_slurp_symbolic_header (abfd)) - return false; + goto error_return; /* If there are no symbols, we don't want it. */ if (bfd_get_symcount (abfd) == 0) - return true; + goto successful_return; symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; /* Read in the external symbols and external strings. */ external_ext_size = backend->debug_swap.external_ext_size; esize = symhdr->iextMax * external_ext_size; - external_ext = (PTR) alloca (esize); + external_ext = (PTR) malloc (esize); + if (external_ext == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0 || bfd_read (external_ext, 1, esize, abfd) != esize) - return false; + goto error_return; + + ssext = (char *) malloc (symhdr->issExtMax); + if (ssext == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } - ssext = (char *) alloca (symhdr->issExtMax); if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax) - return false; + goto error_return; /* Look through the external symbols to see if they define some symbol that is currently undefined. */ @@ -3672,15 +3724,26 @@ ecoff_link_check_archive_element (abfd, info, pneeded) /* Include this element. */ if (! (*info->callbacks->add_archive_element) (info, abfd, name)) - return false; + goto error_return; if (! ecoff_link_add_externals (abfd, info, external_ext, ssext)) - return false; + goto error_return; *pneeded = true; - return true; + goto successful_return; } + successful_return: + if (external_ext != NULL) + free (external_ext); + if (ssext != NULL) + free (ssext); return true; + error_return: + if (external_ext != NULL) + free (external_ext); + if (ssext != NULL) + free (ssext); + return false; } /* Add symbols from an ECOFF object file to the global linker hash @@ -3693,9 +3756,10 @@ ecoff_link_add_object_symbols (abfd, info) { HDRR *symhdr; bfd_size_type external_ext_size; - PTR external_ext; + PTR external_ext = NULL; size_t esize; - char *ssext; + char *ssext = NULL; + boolean result; if (! ecoff_slurp_symbolic_header (abfd)) return false; @@ -3709,17 +3773,42 @@ ecoff_link_add_object_symbols (abfd, info) /* Read in the external symbols and external strings. */ external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size; esize = symhdr->iextMax * external_ext_size; - external_ext = (PTR) alloca (esize); + external_ext = (PTR) malloc (esize); + if (external_ext == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0 || bfd_read (external_ext, 1, esize, abfd) != esize) - return false; + goto error_return; + + ssext = (char *) malloc (symhdr->issExtMax); + if (ssext == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } - ssext = (char *) alloca (symhdr->issExtMax); if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax) - return false; - - return ecoff_link_add_externals (abfd, info, external_ext, ssext); + goto error_return; + + result = ecoff_link_add_externals (abfd, info, external_ext, ssext); + + if (ssext != NULL) + free (ssext); + if (external_ext != NULL) + free (external_ext); + return result; + + error_return: + if (ssext != NULL) + free (ssext); + if (external_ext != NULL) + free (external_ext); + return false; } /* Add the external symbols of an object file to the global linker @@ -3978,14 +4067,6 @@ ecoff_bfd_final_link (abfd, info) { boolean ret; - /* If we might be using the C based alloca function, dump memory - allocated by ecoff_final_link_debug_accumulate. */ -#ifndef __GNUC__ -#ifndef alloca - (void) alloca (0); -#endif -#endif - if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour) { /* Abitrarily set the symbolic header vstamp to the vstamp @@ -4101,14 +4182,6 @@ ecoff_bfd_final_link (abfd, info) p != (struct bfd_link_order *) NULL; p = p->next) { - /* If we might be using the C based alloca function, we need - to dump the memory allocated by the function - ecoff_indirect_link_order. */ -#ifndef __GNUC__ -#ifndef alloca - (void) alloca (0); -#endif -#endif if (p->type == bfd_indirect_link_order && (bfd_get_flavour (p->u.indirect.section->owner) == bfd_target_ecoff_flavour)) @@ -4151,12 +4224,21 @@ ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle) debug->ptr = NULL; \ else \ { \ - debug->ptr = (type) alloca (size * symhdr->count); \ + debug->ptr = (type) malloc (size * symhdr->count); \ + if (debug->ptr == NULL) \ + { \ + bfd_set_error (bfd_error_no_memory); \ + ret = false; \ + goto return_something; \ + } \ if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \ != 0) \ || (bfd_read (debug->ptr, size, symhdr->count, \ input_bfd) != size * symhdr->count)) \ - return false; \ + { \ + ret = false; \ + goto return_something; \ + } \ } READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *); @@ -4178,6 +4260,26 @@ ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle) &ecoff_backend (output_bfd)->debug_swap, input_bfd, debug, swap, info)); + return_something: + if (debug->line != NULL) + free (debug->line); + if (debug->external_dnr != NULL) + free (debug->external_dnr); + if (debug->external_pdr != NULL) + free (debug->external_pdr); + if (debug->external_sym != NULL) + free (debug->external_sym); + if (debug->external_opt != NULL) + free (debug->external_opt); + if (debug->external_aux != NULL) + free (debug->external_aux); + if (debug->ss != NULL) + free (debug->ss); + if (debug->external_fdr != NULL) + free (debug->external_fdr); + if (debug->external_rfd != NULL) + free (debug->external_rfd); + /* Make sure we don't accidentally follow one of these pointers on to the stack. */ debug->line = NULL; @@ -4293,10 +4395,10 @@ ecoff_indirect_link_order (output_bfd, info, output_section, link_order) asection *input_section; bfd *input_bfd; bfd_size_type input_size; - bfd_byte *contents; + bfd_byte *contents = NULL; bfd_size_type external_reloc_size; bfd_size_type external_relocs_size; - PTR external_relocs; + PTR external_relocs = NULL; BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0); @@ -4312,25 +4414,36 @@ ecoff_indirect_link_order (output_bfd, info, output_section, link_order) /* Get the section contents. */ input_size = bfd_section_size (input_bfd, input_section); - contents = (bfd_byte *) alloca (input_size); + contents = (bfd_byte *) malloc (input_size); + if (contents == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents, (file_ptr) 0, input_size)) - return false; + goto error_return; /* Get the relocs. */ external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size; external_relocs_size = external_reloc_size * input_section->reloc_count; - external_relocs = (PTR) alloca (external_relocs_size); + external_relocs = (PTR) malloc (external_relocs_size); + if (external_relocs == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd) != external_relocs_size)) - return false; + goto error_return; /* Relocate the section contents. */ if (! ((*ecoff_backend (input_bfd)->relocate_section) (output_bfd, info, input_bfd, input_section, contents, external_relocs))) - return false; + goto error_return; /* Write out the relocated section. */ if (! bfd_set_section_contents (output_bfd, @@ -4338,7 +4451,7 @@ ecoff_indirect_link_order (output_bfd, info, output_section, link_order) (PTR) contents, input_section->output_offset, input_size)) - return false; + goto error_return; /* If we are producing relocateable output, the relocs were modified, and we write them out now. We use the reloc_count @@ -4352,9 +4465,20 @@ ecoff_indirect_link_order (output_bfd, info, output_section, link_order) SEEK_SET) != 0 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd) != external_relocs_size)) - return false; + goto error_return; output_section->reloc_count += input_section->reloc_count; } + if (contents != NULL) + free (contents); + if (external_relocs != NULL) + free (external_relocs); return true; + + error_return: + if (contents != NULL) + free (contents); + if (external_relocs != NULL) + free (external_relocs); + return false; } diff --git a/bfd/elf32-hppa.c b/bfd/elf32-hppa.c index 64865651677..4325e26f4b4 100644 --- a/bfd/elf32-hppa.c +++ b/bfd/elf32-hppa.c @@ -1933,7 +1933,13 @@ hppa_elf_stub_finish (output_bfd) /* Make space to hold the relocations for the stub section. */ reloc_size = bfd_get_reloc_upper_bound (stub_bfd, stub_sec); - reloc_vector = (arelent **) alloca (reloc_size); + reloc_vector = (arelent **) malloc (reloc_size); + if (reloc_vector == NULL) + { + /* FIXME: should be returning an error so the caller can + clean up */ + abort (); + } /* If we have relocations, do them. */ if (bfd_canonicalize_reloc (stub_bfd, stub_sec, reloc_vector, @@ -1988,6 +1994,7 @@ hppa_elf_stub_finish (output_bfd) } } } + free (reloc_vector); /* All done with the relocations. Set the final contents of the stub section. FIXME: no check of return value! */ @@ -2704,6 +2711,7 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, asymbol *new_syms = NULL; int new_cnt = 0; int new_max = 0; + arelent **reloc_vector = NULL; /* Relocations are in different places depending on whether this is an output section or an input section. Also, the relocations are @@ -2711,8 +2719,13 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, to straighten this out for us . */ if (asec->reloc_count > 0) { - arelent **reloc_vector - = (arelent **) alloca (asec->reloc_count * (sizeof (arelent *) + 1)); + reloc_vector + = (arelent **) malloc (asec->reloc_count * (sizeof (arelent *) + 1)); + if (reloc_vector == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } /* Make sure the canonical symbols are hanging around in a convient location. */ @@ -2725,7 +2738,7 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, if (!abfd->outsymbols) { bfd_set_error (bfd_error_no_memory); - abort (); + goto error_return; } abfd->symcount = bfd_canonicalize_symtab (abfd, abfd->outsymbols); } @@ -2802,7 +2815,10 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, new_syms = (asymbol *) realloc (new_syms, new_max * sizeof (asymbol)); if (new_syms == NULL) - abort (); + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } } /* Build the argument relocation stub. */ @@ -2827,7 +2843,10 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, new_syms = (asymbol *) realloc (new_syms, (new_max * sizeof (asymbol))); if (! new_syms) - abort (); + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } } /* Build the long-call stub. */ @@ -2910,9 +2929,18 @@ hppa_look_for_stubs_in_section (stub_bfd, abfd, output_bfd, asec, } } + if (reloc_vector != NULL) + free (reloc_vector); /* Return the new symbols and update the counters. */ *new_sym_cnt = new_cnt; return new_syms; + + error_return: + if (reloc_vector != NULL) + free (reloc_vector); + /* FIXME: This is bogus. We should be returning NULL. But do the callers + check for that? */ + abort (); } /* Set the contents of a particular section at a particular location. */ diff --git a/bfd/elfcode.h b/bfd/elfcode.h index c3ff9ff2cfb..e357ee6a9cf 100644 --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -1227,10 +1227,14 @@ fix_up_strtabs (abfd, asect, obj) && !strcmp ("str", asect->name + strlen (asect->name) - 3)) { size_t len = strlen (asect->name) + 1; - char *s = (char *) alloca (len); + char *s = (char *) malloc (len); + if (s == NULL) + /* FIXME: Should deal more gracefully with errors. */ + abort (); strcpy (s, asect->name); s[len - 4] = 0; asect = bfd_get_section_by_name (abfd, s); + free (s); if (!asect) abort (); elf_section_data(asect)->this_hdr.sh_link = this_idx; @@ -1675,12 +1679,17 @@ map_program_segments (abfd) Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); Elf_Internal_Shdr *i_shdrp; Elf_Internal_Phdr *phdr; - char *done; + char *done = NULL; unsigned int i, n_left = 0; file_ptr lowest_offset = 0; struct seg_info *seg = NULL; - done = (char *) alloca (i_ehdrp->e_shnum); + done = (char *) malloc (i_ehdrp->e_shnum); + if (done == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } memset (done, 0, i_ehdrp->e_shnum); for (i = 1; i < i_ehdrp->e_shnum; i++) { @@ -1733,7 +1742,7 @@ map_program_segments (abfd) if (!snew) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } s_ptr = &seg; while (*s_ptr != (struct seg_info *) NULL) @@ -1844,7 +1853,13 @@ map_program_segments (abfd) i_ehdrp->e_phnum = n_segs; } elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum); + if (done != NULL) + free (done); return true; + error_return: + if (done != NULL) + free (done); + return false; } static boolean @@ -2543,7 +2558,7 @@ DEFUN (elf_slurp_symbol_table, (abfd, symptrs), elf_symbol_type *sym; /* Pointer to current bfd symbol */ elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ Elf_Internal_Sym i_sym; - Elf_External_Sym *x_symp; + Elf_External_Sym *x_symp = NULL; /* this is only valid because there is only one symtab... */ /* FIXME: This is incorrect, there may also be a dynamic symbol @@ -2593,13 +2608,18 @@ DEFUN (elf_slurp_symbol_table, (abfd, symptrs), /* Temporarily allocate room for the raw ELF symbols. */ x_symp = ((Elf_External_Sym *) - alloca (symcount * sizeof (Elf_External_Sym))); + malloc (symcount * sizeof (Elf_External_Sym))); + if (x_symp == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) != symcount * sizeof (Elf_External_Sym)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Skip first symbol, which is a null dummy. */ for (i = 1; i < symcount; i++) @@ -2703,7 +2723,13 @@ DEFUN (elf_slurp_symbol_table, (abfd, symptrs), *symptrs = 0; /* Final null pointer */ } + if (x_symp != NULL) + free (x_symp); return true; + error_return: + if (x_symp != NULL) + free (x_symp); + return false; } /* Return the number of bytes required to hold the symtab vector. diff --git a/bfd/hosts/solaris2.h b/bfd/hosts/solaris2.h index ca4f6e662a2..9a7b23f1f66 100644 --- a/bfd/hosts/solaris2.h +++ b/bfd/hosts/solaris2.h @@ -2,10 +2,6 @@ #include "hosts/sysv4.h" -#ifndef __GNUC__ -#include -#endif - #ifndef __GNUC__ /* get around a bug in the Sun C compiler */ #define const diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 3b867e64721..7ed6bc4484e 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -19,13 +19,6 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* Use builtin alloca for gcc. */ -#ifdef __GNUC__ -#ifndef alloca -#define alloca __builtin_alloca -#endif -#endif - /* Align an address upward to a boundary, expressed as a number of bytes. E.g. align to an 8-byte boundary with argument of 8. */ #define BFD_ALIGN(this, boundary) \ @@ -73,17 +66,7 @@ struct areltdata { #define arelt_size(bfd) (((struct areltdata *)((bfd)->arelt_data))->parsed_size) -/* There is major inconsistency in how running out of memory is handled. - Some routines return a NULL, and set bfd_error to no_memory. - However, obstack routines can't do this ... */ - char *bfd_zmalloc PARAMS ((bfd_size_type size)); -/* From libiberty. */ -extern PTR xmalloc PARAMS ((size_t)); -/* SIZE is bfd_size_type. */ -#define bfd_xmalloc(size) xmalloc ((size_t) size) -/* Defined without an argument so its address can be used. */ -#define bfd_xmalloc_by_size_t xmalloc /* These routines allocate and free things on the BFD's obstack. Note that realloc can never occur in place. */ diff --git a/bfd/libbfd.h b/bfd/libbfd.h index ccc21b7a1e8..c8b05325054 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -19,13 +19,6 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* Use builtin alloca for gcc. */ -#ifdef __GNUC__ -#ifndef alloca -#define alloca __builtin_alloca -#endif -#endif - /* Align an address upward to a boundary, expressed as a number of bytes. E.g. align to an 8-byte boundary with argument of 8. */ #define BFD_ALIGN(this, boundary) \ @@ -73,17 +66,7 @@ struct areltdata { #define arelt_size(bfd) (((struct areltdata *)((bfd)->arelt_data))->parsed_size) -/* There is major inconsistency in how running out of memory is handled. - Some routines return a NULL, and set bfd_error to no_memory. - However, obstack routines can't do this ... */ - char *bfd_zmalloc PARAMS ((bfd_size_type size)); -/* From libiberty. */ -extern PTR xmalloc PARAMS ((size_t)); -/* SIZE is bfd_size_type. */ -#define bfd_xmalloc(size) xmalloc ((size_t) size) -/* Defined without an argument so its address can be used. */ -#define bfd_xmalloc_by_size_t xmalloc /* These routines allocate and free things on the BFD's obstack. Note that realloc can never occur in place. */ @@ -292,7 +275,7 @@ bfd_open_file PARAMS ((bfd *abfd)); FILE * bfd_cache_lookup_worker PARAMS ((bfd *abfd)); -void +boolean bfd_constructor_entry PARAMS ((bfd *abfd, asymbol **symbol_ptr_ptr, CONST char*type)); diff --git a/bfd/nlmcode.h b/bfd/nlmcode.h index 43b263efc16..eb07c9a1789 100644 --- a/bfd/nlmcode.h +++ b/bfd/nlmcode.h @@ -95,7 +95,7 @@ DEFUN (nlm_object_p, (abfd), bfd * abfd) { struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd); boolean (*backend_object_p) PARAMS ((bfd *)); - PTR x_fxdhdr; + PTR x_fxdhdr = NULL; Nlm_Internal_Fixed_Header *i_fxdhdrp; const char *signature; enum bfd_architecture arch; @@ -111,7 +111,12 @@ DEFUN (nlm_object_p, (abfd), bfd * abfd) /* Read in the fixed length portion of the NLM header in external format. */ - x_fxdhdr = (PTR) alloca (nlm_fixed_header_size (abfd)); + x_fxdhdr = (PTR) malloc (nlm_fixed_header_size (abfd)); + if (x_fxdhdr == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto got_no_match; + } if (bfd_read ((PTR) x_fxdhdr, nlm_fixed_header_size (abfd), 1, abfd) != nlm_fixed_header_size (abfd)) @@ -124,7 +129,7 @@ DEFUN (nlm_object_p, (abfd), bfd * abfd) bfd_zalloc (abfd, sizeof (struct nlm_obj_tdata)); if (nlm_tdata (abfd) == NULL) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); goto got_no_match; } @@ -187,12 +192,16 @@ DEFUN (nlm_object_p, (abfd), bfd * abfd) if (arch != bfd_arch_unknown) bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0); + if (x_fxdhdr != NULL) + free (x_fxdhdr); return (abfd -> xvec); got_wrong_format_error: - bfd_error = wrong_format; + bfd_set_error (bfd_error_wrong_format); got_no_match: nlm_tdata (abfd) = preserved_tdata; + if (x_fxdhdr != NULL) + free (x_fxdhdr); return (NULL); } @@ -237,7 +246,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> descriptionLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_read ((PTR) nlm_variable_header (abfd) -> descriptionText, @@ -245,7 +254,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != nlm_variable_header (abfd) -> descriptionLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -253,7 +262,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } nlm_variable_header (abfd) -> stackSize = get_word (abfd, (bfd_byte *) temp); @@ -262,7 +271,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } nlm_variable_header (abfd) -> reserved = get_word (abfd, (bfd_byte *) temp); @@ -274,7 +283,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> oldThreadName)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -285,7 +294,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> screenNameLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_read ((PTR) nlm_variable_header (abfd) -> screenName, @@ -293,7 +302,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != nlm_variable_header (abfd) -> screenNameLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -304,7 +313,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> threadNameLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_read ((PTR) nlm_variable_header (abfd) -> threadName, @@ -312,7 +321,7 @@ DEFUN (nlm_swap_variable_header_in, (abfd), 1, abfd) != nlm_variable_header (abfd) -> threadNameLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } return (true); @@ -334,7 +343,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> descriptionLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_write ((PTR) nlm_variable_header (abfd) -> descriptionText, @@ -342,7 +351,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != nlm_variable_header (abfd) -> descriptionLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -352,7 +361,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), (bfd_byte *) temp); if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -362,7 +371,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), (bfd_byte *) temp); if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -373,7 +382,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> oldThreadName)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -384,7 +393,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> screenNameLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_write ((PTR) nlm_variable_header (abfd) -> screenName, @@ -392,7 +401,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != nlm_variable_header (abfd) -> screenNameLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -403,7 +412,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != sizeof (nlm_variable_header (abfd) -> threadNameLength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_write ((PTR) nlm_variable_header (abfd) -> threadName, @@ -411,7 +420,7 @@ DEFUN (nlm_swap_variable_header_out, (abfd), 1, abfd) != nlm_variable_header (abfd) -> threadNameLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } return (true); @@ -439,12 +448,12 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), if (bfd_read ((PTR) tempstr, sizeof (tempstr), 1, abfd) != sizeof (tempstr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_seek (abfd, position, SEEK_SET) == -1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (strncmp (tempstr, "VeRsIoN#", 8) == 0) @@ -452,7 +461,7 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), Nlm_External_Version_Header thdr; if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } memcpy (nlm_version_header (abfd) -> stamp, thdr.stamp, @@ -475,7 +484,7 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), Nlm_External_Extended_Header thdr; if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } memcpy (nlm_extended_header (abfd) -> stamp, thdr.stamp, @@ -544,7 +553,7 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), Nlm_External_Custom_Header thdr; if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } memcpy (nlm_custom_header (abfd) -> stamp, thdr.stamp, @@ -563,14 +572,14 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), 1, abfd) != sizeof (nlm_copyright_header (abfd)->stamp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } if (bfd_read ((PTR) &(nlm_copyright_header (abfd) ->copyrightMessageLength), 1, 1, abfd) != 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } /* The copyright message is a variable length string. */ @@ -579,7 +588,7 @@ DEFUN (nlm_swap_auxiliary_headers_in, (abfd), 1, abfd) != nlm_copyright_header (abfd) -> copyrightMessageLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } } @@ -635,7 +644,7 @@ nlm_swap_auxiliary_headers_out (abfd) (bfd_byte *) thdr.day); if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } } @@ -736,7 +745,7 @@ nlm_swap_auxiliary_headers_out (abfd) (bfd_byte *) thdr.reserved5); if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } } @@ -760,7 +769,7 @@ nlm_swap_auxiliary_headers_out (abfd) (bfd_byte *) thdr.debugRecLength); if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } } @@ -775,14 +784,14 @@ nlm_swap_auxiliary_headers_out (abfd) if (bfd_write ((PTR) thdr.stamp, sizeof (thdr.stamp), 1, abfd) != sizeof (thdr.stamp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } thdr.copyrightMessageLength[0] = nlm_copyright_header (abfd)->copyrightMessageLength; if (bfd_write ((PTR) thdr.copyrightMessageLength, 1, 1, abfd) != 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } /* The copyright message is a variable length string. */ @@ -791,7 +800,7 @@ nlm_swap_auxiliary_headers_out (abfd) 1, abfd) != nlm_copyright_header (abfd) -> copyrightMessageLength + 1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } } @@ -956,7 +965,7 @@ nlm_slurp_symbol_table (abfd) if (bfd_seek (abfd, i_fxdhdrp -> publicsOffset, SEEK_SET) == -1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -964,7 +973,7 @@ nlm_slurp_symbol_table (abfd) bfd_zalloc (abfd, totsymcount * sizeof (nlm_symbol_type))); if (!sym) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } nlm_set_symbols (abfd, sym); @@ -980,27 +989,27 @@ nlm_slurp_symbol_table (abfd) if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd) != sizeof (symlength)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } sym -> symbol.the_bfd = abfd; sym -> symbol.name = bfd_alloc (abfd, symlength + 1); if (!sym -> symbol.name) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd) != symlength) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } /* Cast away const. */ ((char *) (sym -> symbol.name))[symlength] = '\0'; if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } sym -> symbol.flags = BSF_GLOBAL | BSF_EXPORT; @@ -1038,7 +1047,7 @@ nlm_slurp_symbol_table (abfd) { if (bfd_seek (abfd, i_fxdhdrp -> debugInfoOffset, SEEK_SET) == -1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -1051,20 +1060,20 @@ nlm_slurp_symbol_table (abfd) || (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd) != sizeof (symlength))) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } sym -> symbol.the_bfd = abfd; sym -> symbol.name = bfd_alloc (abfd, symlength + 1); if (!sym -> symbol.name) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd) != symlength) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } /* Cast away const. */ @@ -1101,7 +1110,7 @@ nlm_slurp_symbol_table (abfd) if (bfd_seek (abfd, i_fxdhdrp -> externalReferencesOffset, SEEK_SET) == -1) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return (false); } @@ -1150,7 +1159,7 @@ nlm_slurp_reloc_fixups (abfd) if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset, SEEK_SET) != 0) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } @@ -1159,7 +1168,7 @@ nlm_slurp_reloc_fixups (abfd) secs = (asection **) bfd_alloc (abfd, count * sizeof (asection *)); if (rels == NULL || secs == NULL) { - bfd_error = no_memory; + bfd_set_error (bfd_error_no_memory); return false; } nlm_relocation_fixups (abfd) = rels; @@ -1544,7 +1553,7 @@ nlm_set_section_contents (abfd, section, location, offset, count) if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0 || bfd_write (location, 1, count, abfd) != count) { - bfd_error = system_call_error; + bfd_set_error (bfd_error_system_call); return false; } @@ -1613,41 +1622,48 @@ nlm_write_object_contents (abfd) asymbol **sym_ptr_ptr; file_ptr last; boolean (*write_prefix_func) PARAMS ((bfd *)); - unsigned char *fixed_header = (unsigned char *) alloca (nlm_fixed_header_size (abfd)); + unsigned char *fixed_header = NULL; + + fixed_header = (unsigned char *) malloc (nlm_fixed_header_size (abfd)); + if (fixed_header == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (abfd->output_has_begun == false && nlm_compute_section_file_positions (abfd) == false) - return false; + goto error_return; /* Write out the variable length headers. */ if (bfd_seek (abfd, nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd), SEEK_SET) != 0) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } if (nlm_swap_variable_header_out (abfd) == false || nlm_swap_auxiliary_headers_out (abfd) == false) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } /* A weak check on whether the section file positions were reasonable. */ if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset) { - bfd_error = invalid_operation; - return false; + bfd_set_error (bfd_error_invalid_operation); + goto error_return; } /* Advance to the relocs. */ if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset, SEEK_SET) != 0) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } /* The format of the relocation entries is dependent upon the @@ -1675,8 +1691,8 @@ nlm_write_object_contents (abfd) /* We need to know how to write out imports */ if (write_import_func == NULL) { - bfd_error = invalid_operation; - return false; + bfd_set_error (bfd_error_invalid_operation); + goto error_return; } rel_ptr_ptr = sec->orelocation; @@ -1693,7 +1709,7 @@ nlm_write_object_contents (abfd) { ++internal_reloc_count; if ((*write_import_func) (abfd, sec, rel) == false) - return false; + goto error_return; } else ++external_reloc_count; @@ -1711,8 +1727,8 @@ nlm_write_object_contents (abfd) * sizeof (struct reloc_and_sec))); if (external_relocs == (struct reloc_and_sec *) NULL) { - bfd_error = no_memory; - return false; + bfd_set_error (bfd_error_no_memory); + goto error_return; } i = 0; for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next) @@ -1772,7 +1788,7 @@ nlm_write_object_contents (abfd) if ((*nlm_write_external_func (abfd)) (abfd, cnt, sym, &external_relocs[i]) == false) - return false; + goto error_return; i += cnt; } @@ -1832,15 +1848,15 @@ nlm_write_object_contents (abfd) { /* We can't handle an exported symbol that is not in the code or data segment. */ - bfd_error = invalid_operation; - return false; + bfd_set_error (bfd_error_invalid_operation); + goto error_return; } } if (write_export_func) { if ((*write_export_func) (abfd, sym, offset) == false) - return false; + goto error_return; } else { @@ -1849,15 +1865,15 @@ nlm_write_object_contents (abfd) != sizeof (bfd_byte)) || bfd_write (sym->name, len, 1, abfd) != len) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } put_word (abfd, offset, temp); if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } } } @@ -1919,15 +1935,15 @@ nlm_write_object_contents (abfd) if (bfd_write (&type, sizeof (bfd_byte), 1, abfd) != sizeof (bfd_byte)) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } put_word (abfd, offset, temp); if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp)) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } len = strlen (sym->name); @@ -1935,8 +1951,8 @@ nlm_write_object_contents (abfd) != sizeof (bfd_byte)) || bfd_write (sym->name, len, 1, abfd) != len) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } } nlm_fixed_header (abfd)->numberOfDebugRecords = c; @@ -1981,13 +1997,13 @@ nlm_write_object_contents (abfd) nlm_get_text_low (abfd); if (bfd_seek (abfd, 0, SEEK_SET) != 0) - return false; + goto error_return; write_prefix_func = nlm_write_prefix_func (abfd); if (write_prefix_func) { if ((*write_prefix_func) (abfd) == false) - return false; + goto error_return; } BFD_ASSERT (bfd_tell (abfd) == nlm_optional_prefix_size (abfd)); @@ -1996,9 +2012,16 @@ nlm_write_object_contents (abfd) if (bfd_write (fixed_header, nlm_fixed_header_size (abfd), 1, abfd) != nlm_fixed_header_size (abfd)) { - bfd_error = system_call_error; - return false; + bfd_set_error (bfd_error_system_call); + goto error_return; } + if (fixed_header != NULL) + free (fixed_header); return true; + + error_return: + if (fixed_header != NULL) + free (fixed_header); + return false; } diff --git a/bfd/reloc.c b/bfd/reloc.c index 4323d9aaf8a..1c367ccf8f7 100644 --- a/bfd/reloc.c +++ b/bfd/reloc.c @@ -1607,27 +1607,34 @@ bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data, bfd *input_bfd = link_order->u.indirect.section->owner; asection *input_section = link_order->u.indirect.section; - - size_t reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section); - arelent **reloc_vector = (arelent **) alloca(reloc_size); + arelent **reloc_vector = NULL; + + reloc_vector = (arelent **) malloc (reloc_size); + if (reloc_vector == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } /* read in the section */ - bfd_get_section_contents(input_bfd, - input_section, - (PTR) data, - 0, - input_section->_raw_size); - -/* We're not relaxing the section, so just copy the size info */ + if (!bfd_get_section_contents(input_bfd, + input_section, + (PTR) data, + 0, + input_section->_raw_size)) + goto error_return; + + /* We're not relaxing the section, so just copy the size info */ input_section->_cooked_size = input_section->_raw_size; input_section->reloc_done = true; - - if (bfd_canonicalize_reloc(input_bfd, - input_section, - reloc_vector, - symbols) ) + if (!bfd_canonicalize_reloc (input_bfd, + input_section, + reloc_vector, + symbols)) + goto error_return; + { arelent **parent; for (parent = reloc_vector; * parent != (arelent *)NULL; @@ -1659,21 +1666,21 @@ bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data, if (! ((*link_info->callbacks->undefined_symbol) (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr), input_bfd, input_section, (*parent)->address))) - return NULL; + goto error_return; break; case bfd_reloc_dangerous: BFD_ASSERT (error_message != (char *) NULL); if (! ((*link_info->callbacks->reloc_dangerous) (link_info, error_message, input_bfd, input_section, (*parent)->address))) - return NULL; + goto error_return; break; case bfd_reloc_overflow: if (! ((*link_info->callbacks->reloc_overflow) (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr), (*parent)->howto->name, (*parent)->addend, input_bfd, input_section, (*parent)->address))) - return NULL; + goto error_return; break; case bfd_reloc_outofrange: default: @@ -1684,9 +1691,12 @@ bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data, } } } - - + if (reloc_vector != NULL) + free (reloc_vector); return data; - + error_return: + if (reloc_vector != NULL) + free (reloc_vector); + return NULL; } diff --git a/bfd/som.c b/bfd/som.c index daeb9f9e977..7f9d0be9bb8 100644 --- a/bfd/som.c +++ b/bfd/som.c @@ -1624,15 +1624,18 @@ setup_sections (abfd, file_hdr) /* First, read in space names */ - space_strings = alloca (file_hdr->space_strings_size); + space_strings = malloc (file_hdr->space_strings_size); if (!space_strings) - return false; + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } if (bfd_seek (abfd, file_hdr->space_strings_location, SEEK_SET) < 0) - return false; + goto error_return; if (bfd_read (space_strings, 1, file_hdr->space_strings_size, abfd) != file_hdr->space_strings_size) - return false; + goto error_return; /* Loop over all of the space dictionaries, building up sections */ for (space_index = 0; space_index < file_hdr->space_total; space_index++) @@ -1645,9 +1648,9 @@ setup_sections (abfd, file_hdr) /* Read the space dictionary element */ if (bfd_seek (abfd, file_hdr->space_location + space_index * sizeof space, SEEK_SET) < 0) - return false; + goto error_return; if (bfd_read (&space, 1, sizeof space, abfd) != sizeof space) - return false; + goto error_return; /* Setup the space name string */ space.name.n_name = space.name.n_strx + space_strings; @@ -1655,27 +1658,27 @@ setup_sections (abfd, file_hdr) /* Make a section out of it */ space_asect = make_unique_section (abfd, space.name.n_name, space_index); if (!space_asect) - return false; + goto error_return; /* Now, read in the first subspace for this space */ if (bfd_seek (abfd, file_hdr->subspace_location + space.subspace_index * sizeof subspace, SEEK_SET) < 0) - return false; + goto error_return; if (bfd_read (&subspace, 1, sizeof subspace, abfd) != sizeof subspace) - return false; + goto error_return; /* Seek back to the start of the subspaces for loop below */ if (bfd_seek (abfd, file_hdr->subspace_location + space.subspace_index * sizeof subspace, SEEK_SET) < 0) - return false; + goto error_return; /* Setup the start address and file loc from the first subspace record */ space_asect->vma = subspace.subspace_start; space_asect->filepos = subspace.file_loc_init_value; space_asect->alignment_power = log2 (subspace.alignment); if (space_asect->alignment_power == -1) - return false; + goto error_return; /* Initialize save_subspace so we can reliably determine if this loop placed any useful values into it. */ @@ -1690,7 +1693,7 @@ setup_sections (abfd, file_hdr) /* Read in the next subspace */ if (bfd_read (&subspace, 1, sizeof subspace, abfd) != sizeof subspace) - return false; + goto error_return; /* Setup the subspace name string */ subspace.name.n_name = subspace.name.n_strx + space_strings; @@ -1700,7 +1703,7 @@ setup_sections (abfd, file_hdr) space.subspace_index + subspace_index); if (!subspace_asect) - return false; + goto error_return; /* Keep an easy mapping between subspaces and sections. */ som_section_data (subspace_asect)->subspace_index @@ -1777,14 +1780,14 @@ setup_sections (abfd, file_hdr) subspace_asect->filepos = subspace.file_loc_init_value; subspace_asect->alignment_power = log2 (subspace.alignment); if (subspace_asect->alignment_power == -1) - return false; + goto error_return; } /* Yow! there is no subspace within the space which actually has initialized information in it; this should never happen as far as I know. */ if (!save_subspace.file_loc_init_value) - return false; + goto error_return; /* Setup the sizes for the space section based upon the info in the last subspace of the space. */ @@ -1793,7 +1796,14 @@ setup_sections (abfd, file_hdr) space_asect->_raw_size = save_subspace.file_loc_init_value - space_asect->filepos + save_subspace.initialization_length; } + if (space_strings != NULL) + free (space_strings); return true; + + error_return: + if (space_strings != NULL) + free (space_strings); + return false; } /* Read in a SOM object and make it into a BFD. */ @@ -2154,15 +2164,15 @@ som_write_fixups (abfd, current_offset, total_reloc_sizep) unsigned int *total_reloc_sizep; { unsigned int i, j; - unsigned char *tmp_space, *p; + /* Chunk of memory that we can use as buffer space, then throw + away. */ + unsigned char tmp_space[SOM_TMP_BUFSIZE]; + unsigned char *p; unsigned int total_reloc_size = 0; unsigned int subspace_reloc_size = 0; unsigned int num_spaces = obj_som_file_hdr (abfd)->space_total; asection *section = abfd->sections; - /* Get a chunk of memory that we can use as buffer space, then throw - away. */ - tmp_space = alloca (SOM_TMP_BUFSIZE); memset (tmp_space, 0, SOM_TMP_BUFSIZE); p = tmp_space; @@ -2445,13 +2455,13 @@ som_write_space_strings (abfd, current_offset, string_sizep) unsigned long current_offset; unsigned int *string_sizep; { - unsigned char *tmp_space, *p; + /* Chunk of memory that we can use as buffer space, then throw + away. */ + unsigned char tmp_space[SOM_TMP_BUFSIZE]; + unsigned char *p; unsigned int strings_size = 0; asection *section; - /* Get a chunk of memory that we can use as buffer space, then throw - away. */ - tmp_space = alloca (SOM_TMP_BUFSIZE); memset (tmp_space, 0, SOM_TMP_BUFSIZE); p = tmp_space; @@ -2483,7 +2493,7 @@ som_write_space_strings (abfd, current_offset, string_sizep) hold the string length + the string itself + null terminator. */ if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE) { - if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) + if (bfd_write ((PTR) &tmp_space[0], p - tmp_space, 1, abfd) != p - tmp_space) { bfd_set_error (bfd_error_system_call); @@ -2521,7 +2531,7 @@ som_write_space_strings (abfd, current_offset, string_sizep) /* Done with the space/subspace strings. Write out any information contained in a partial block. */ - if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space) + if (bfd_write ((PTR) &tmp_space[0], p - tmp_space, 1, abfd) != p - tmp_space) { bfd_set_error (bfd_error_system_call); return false; @@ -2541,12 +2551,13 @@ som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep) unsigned int *string_sizep; { unsigned int i; - unsigned char *tmp_space, *p; + + /* Chunk of memory that we can use as buffer space, then throw + away. */ + unsigned char tmp_space[SOM_TMP_BUFSIZE]; + unsigned char *p; unsigned int strings_size = 0; - /* Get a chunk of memory that we can use as buffer space, then throw - away. */ - tmp_space = alloca (SOM_TMP_BUFSIZE); memset (tmp_space, 0, SOM_TMP_BUFSIZE); p = tmp_space; @@ -2566,7 +2577,7 @@ som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep) current buffer contents now. */ if (p - tmp_space + 5 + length > SOM_TMP_BUFSIZE) { - if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) + if (bfd_write ((PTR) &tmp_space[0], p - tmp_space, 1, abfd) != p - tmp_space) { bfd_set_error (bfd_error_system_call); @@ -2602,7 +2613,7 @@ som_write_symbol_strings (abfd, current_offset, syms, num_syms, string_sizep) } /* Scribble out any partial block. */ - if (bfd_write ((PTR) tmp_space, p - tmp_space, 1, abfd) != p - tmp_space) + if (bfd_write ((PTR) &tmp_space[0], p - tmp_space, 1, abfd) != p - tmp_space) { bfd_set_error (bfd_error_system_call); return false; @@ -3213,13 +3224,18 @@ som_build_and_write_symbol_table (abfd) unsigned int num_syms = bfd_get_symcount (abfd); file_ptr symtab_location = obj_som_file_hdr (abfd)->symbol_location; asymbol **bfd_syms = bfd_get_outsymbols (abfd); - struct symbol_dictionary_record *som_symtab; + struct symbol_dictionary_record *som_symtab = NULL; int i, symtab_size; /* Compute total symbol table size and allocate a chunk of memory to hold the symbol table as we build it. */ symtab_size = num_syms * sizeof (struct symbol_dictionary_record); - som_symtab = (struct symbol_dictionary_record *) alloca (symtab_size); + som_symtab = (struct symbol_dictionary_record *) malloc (symtab_size); + if (som_symtab == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } memset (som_symtab, 0, symtab_size); /* Walk over each symbol. */ @@ -3248,15 +3264,22 @@ som_build_and_write_symbol_table (abfd) if (bfd_seek (abfd, symtab_location, SEEK_SET) != 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_write ((PTR) som_symtab, symtab_size, 1, abfd) != symtab_size) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } - return true; + + if (som_symtab != NULL) + free (som_symtab); + return true; + error_return: + if (som_symtab != NULL) + free (som_symtab); + return false; } /* Write an object in SOM format. */ @@ -3365,19 +3388,19 @@ som_slurp_symbol_table (abfd) int symbol_count = bfd_get_symcount (abfd); int symsize = sizeof (struct symbol_dictionary_record); char *stringtab; - struct symbol_dictionary_record *buf, *bufp, *endbufp; + struct symbol_dictionary_record *buf = NULL, *bufp, *endbufp; som_symbol_type *sym, *symbase; /* Return saved value if it exists. */ if (obj_som_symtab (abfd) != NULL) - return true; + goto successful_return; /* Special case. This is *not* an error. */ if (symbol_count == 0) - return true; + goto successful_return; if (!som_slurp_string_table (abfd)) - return false; + goto error_return; stringtab = obj_som_stringtab (abfd); @@ -3386,26 +3409,26 @@ som_slurp_symbol_table (abfd) if (symbase == NULL) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } /* Read in the external SOM representation. */ - buf = alloca (symbol_count * symsize); + buf = malloc (symbol_count * symsize); if (buf == NULL) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } if (bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read (buf, symbol_count * symsize, 1, abfd) != symbol_count * symsize) { bfd_set_error (bfd_error_no_symbols); - return (false); + goto error_return; } /* Iterate over all the symbols and internalize them. */ @@ -3514,7 +3537,15 @@ som_slurp_symbol_table (abfd) /* Save our results and return success. */ obj_som_symtab (abfd) = symbase; + successful_return: + if (buf != NULL) + free (buf); return (true); + + error_return: + if (buf != NULL) + free (buf); + return false; } /* Canonicalize a SOM symbol table. Return the number of entries @@ -4251,10 +4282,17 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) symindex *count; { unsigned int i; - unsigned int *hash_table = - (unsigned int *) alloca (lst_header->hash_size * sizeof (unsigned int)); + unsigned int *hash_table = NULL file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header); + hash_table = + (unsigned int *) malloc (lst_header->hash_size * sizeof (unsigned int)); + if (hash_table == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + /* Don't forget to initialize the counter! */ *count = 0; @@ -4264,7 +4302,7 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) != lst_header->hash_size * 4) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Walk each chain counting the number of symbols found on that particular @@ -4281,7 +4319,7 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Read in this symbol and update the counter. */ @@ -4289,7 +4327,7 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) != sizeof (lst_symbol)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } (*count)++; @@ -4302,7 +4340,7 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Read the symbol in and update the counter. */ @@ -4310,12 +4348,19 @@ som_bfd_count_ar_symbols (abfd, lst_header, count) != sizeof (lst_symbol)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } (*count)++; } } + if (hash_table != NULL) + free (hash_table); return true; + + error_return: + if (hash_table != NULL) + free (hash_table); + return false; } /* Fill in the canonical archive symbols (SYMS) from the archive described @@ -4329,20 +4374,34 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) { unsigned int i, len; carsym *set = syms[0]; - unsigned int *hash_table = - (unsigned int *) alloca (lst_header->hash_size * sizeof (unsigned int)); - struct som_entry *som_dict = - (struct som_entry *) alloca (lst_header->module_count - * sizeof (struct som_entry)); + unsigned int *hash_table = NULL; + struct som_entry *som_dict = NULL; file_ptr lst_filepos = bfd_tell (abfd) - sizeof (struct lst_header); + hash_table = + (unsigned int *) malloc (lst_header->hash_size * sizeof (unsigned int)); + if (hash_table == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + + som_dict = + (struct som_entry *) malloc (lst_header->module_count + * sizeof (struct som_entry)); + if (som_dict == NULL) + { + bfd_set_error (bfd_error_no_memory); + 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_read ((PTR) hash_table, lst_header->hash_size, 4, abfd) != lst_header->hash_size * 4) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Seek to and read in the SOM dictionary. We will need this to fill @@ -4350,7 +4409,7 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) if (bfd_seek (abfd, lst_filepos + lst_header->dir_loc, SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read ((PTR) som_dict, lst_header->module_count, @@ -4358,7 +4417,7 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) != lst_header->module_count * sizeof (struct som_entry)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Walk each chain filling in the carsyms as we go along. */ @@ -4374,14 +4433,14 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) if (bfd_seek (abfd, lst_filepos + hash_table[i], SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd) != sizeof (lst_symbol)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Get the name of the symbol, first get the length which is stored @@ -4395,13 +4454,13 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) + lst_symbol.name.n_strx - 4, SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read (&len, 1, 4, abfd) != 4) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Allocate space for the name and null terminate it too. */ @@ -4409,12 +4468,12 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) if (!set->name) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } if (bfd_read (set->name, 1, len, abfd) != len) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } set->name[len] = 0; @@ -4434,14 +4493,14 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read ((PTR) & lst_symbol, 1, sizeof (lst_symbol), abfd) != sizeof (lst_symbol)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Seek to the name length & string and read them in. */ @@ -4449,13 +4508,13 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) + lst_symbol.name.n_strx - 4, SEEK_SET) < 0) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } if (bfd_read (&len, 1, 4, abfd) != 4) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Allocate space for the name and null terminate it too. */ @@ -4463,12 +4522,12 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) if (!set->name) { bfd_set_error (bfd_error_no_memory); - return false; + goto error_return; } if (bfd_read (set->name, 1, len, abfd) != len) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } set->name[len] = 0; @@ -4483,7 +4542,18 @@ som_bfd_fill_in_ar_symbols (abfd, lst_header, syms) } /* If we haven't died by now, then we successfully read the entire archive symbol table. */ + if (hash_table != NULL) + free (hash_table); + if (som_dict != NULL) + free (som_dict); return true; + + error_return: + if (hash_table != NULL) + free (hash_table); + if (som_dict != NULL) + free (som_dict); + return false; } /* Read in the LST from the archive. */ @@ -4687,18 +4757,38 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) struct lst_header lst; { file_ptr lst_filepos; - char *strings, *p; - struct lst_symbol_record *lst_syms, *curr_lst_sym; + char *strings = NULL, *p; + struct lst_symbol_record *lst_syms = NULL, *curr_lst_sym; bfd *curr_bfd = abfd->archive_head; - unsigned int *hash_table = - (unsigned int *) alloca (lst.hash_size * sizeof (unsigned int)); - struct som_entry *som_dict = - (struct som_entry *) alloca (lst.module_count + unsigned int *hash_table = NULL; + struct som_entry *som_dict = NULL; + struct lst_symbol_record **last_hash_entry = NULL; + unsigned int curr_som_offset, som_index; + + hash_table = + (unsigned int *) malloc (lst.hash_size * sizeof (unsigned int)); + if (hash_table == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + som_dict = + (struct som_entry *) malloc (lst.module_count * sizeof (struct som_entry)); - struct lst_symbol_record **last_hash_entry = + if (som_dict == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + + last_hash_entry = ((struct lst_symbol_record **) - alloca (lst.hash_size * sizeof (struct lst_symbol_record *))); - unsigned int curr_som_offset, som_index; + malloc (lst.hash_size * sizeof (struct lst_symbol_record *))); + if (last_hash_entry == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } /* Lots of fields are file positions relative to the start of the lst record. So save its location. */ @@ -4721,8 +4811,19 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) curr_som_offset = 8 + 2 * sizeof (struct ar_hdr) + lst.file_end; /* FIXME should be done with buffers just like everything else... */ - lst_syms = alloca (nsyms * sizeof (struct lst_symbol_record)); - strings = alloca (string_size); + lst_syms = malloc (nsyms * sizeof (struct lst_symbol_record)); + if (lst_syms == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + strings = malloc (string_size); + if (strings == NULL) + { + bfd_set_error (bfd_error_no_memory); + goto error_return; + } + p = strings; curr_lst_sym = lst_syms; @@ -4736,7 +4837,7 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) to it. It's a little slimey to grab the symbols via obj_som_symtab, but doing so avoids allocating lots of extra memory. */ if (som_slurp_symbol_table (curr_bfd) == false) - return false; + goto error_return; sym = obj_som_symtab (curr_bfd); curr_count = bfd_get_symcount (curr_bfd); @@ -4851,7 +4952,7 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) != lst.hash_size * 4) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* Then the SOM dictionary. */ @@ -4860,7 +4961,7 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) != lst.module_count * sizeof (struct som_entry)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* The library symbols. */ @@ -4868,17 +4969,41 @@ som_bfd_ar_write_symbol_stuff (abfd, nsyms, string_size, lst) != nsyms * sizeof (struct lst_symbol_record)) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } /* And finally the strings. */ if (bfd_write ((PTR) strings, string_size, 1, abfd) != string_size) { bfd_set_error (bfd_error_system_call); - return false; + goto error_return; } + if (hash_table != NULL) + free (hash_table); + if (som_dict != NULL) + free (som_dict); + if (last_hash_entry != NULL) + free (last_hash_entry); + if (lst_syms != NULL) + free (lst_syms); + if (strings != NULL) + free (strings); return true; + + error_return: + if (hash_table != NULL) + free (hash_table); + if (som_dict != NULL) + free (som_dict); + if (last_hash_entry != NULL) + free (last_hash_entry); + if (lst_syms != NULL) + free (lst_syms); + if (strings != NULL) + free (strings); + + return false; } /* Write out the LST for the archive. -- 2.30.2