Fix address violation parsing a corrupt SOM binary.
[deliverable/binutils-gdb.git] / bfd / som.c
index a18c8691a49420e23c9725c834259dbf1ea9c5eb..98c4124bbadd0c4c39a015cb7185db8b54d61937 100644 (file)
--- a/bfd/som.c
+++ b/bfd/som.c
@@ -1,7 +1,5 @@
 /* bfd back-end for HP PA-RISC SOM objects.
-   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 1990-2017 Free Software Foundation, Inc.
 
    Contributed by the Center for Software Science at the
    University of Utah.
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
    02110-1301, USA.  */
 
-#include "alloca-conf.h"
 #include "sysdep.h"
+#include "alloca-conf.h"
 #include "bfd.h"
-
+#include "libiberty.h"
 #include "libbfd.h"
 #include "som.h"
 #include "safe-ctype.h"
@@ -2085,8 +2083,8 @@ setup_sections (bfd *abfd,
 
   /* First, read in space names.  */
   amt = file_hdr->space_strings_size;
-  space_strings = bfd_malloc (amt);
-  if (!space_strings && amt != 0)
+  space_strings = bfd_malloc (amt + 1);
+  if (space_strings == NULL && amt != 0)
     goto error_return;
 
   if (bfd_seek (abfd, current_offset + file_hdr->space_strings_location,
@@ -2094,6 +2092,8 @@ setup_sections (bfd *abfd,
     goto error_return;
   if (bfd_bread (space_strings, amt, abfd) != amt)
     goto error_return;
+  /* Make sure that the string table is NUL terminated.  */
+  space_strings[amt] = 0;
 
   /* Loop over all of the space dictionaries, building up sections.  */
   for (space_index = 0; space_index < file_hdr->space_total; space_index++)
@@ -2121,6 +2121,9 @@ setup_sections (bfd *abfd,
       som_swap_space_dictionary_in (&ext_space, &space);
 
       /* Setup the space name string.  */
+      if (space.name >= file_hdr->space_strings_size)
+       goto error_return;
+
       space_name = space.name + space_strings;
 
       /* Make a section out of it.  */
@@ -2496,7 +2499,7 @@ som_object_p (bfd *abfd)
     {
       struct som_external_exec_auxhdr ext_exec_auxhdr;
 
-      aux_hdr_ptr = bfd_zalloc (abfd, 
+      aux_hdr_ptr = bfd_zalloc (abfd,
                                (bfd_size_type) sizeof (*aux_hdr_ptr));
       if (aux_hdr_ptr == NULL)
        return NULL;
@@ -3306,11 +3309,12 @@ som_write_space_strings (bfd *abfd,
   /* Chunk of memory that we can use as buffer space, then throw
      away.  */
   size_t tmp_space_size = SOM_TMP_BUFSIZE;
-  char *tmp_space = alloca (tmp_space_size);
+  char *tmp_space = xmalloc (tmp_space_size);
   char *p = tmp_space;
   unsigned int strings_size = 0;
   asection *section;
   bfd_size_type amt;
+  bfd_size_type res;
 
   /* Seek to the start of the space strings in preparation for writing
      them out.  */
@@ -3357,7 +3361,7 @@ som_write_space_strings (bfd *abfd,
                 tmp_space_size = length + 5;
               else
                 tmp_space_size = 2 * tmp_space_size;
-             tmp_space = alloca (tmp_space_size);
+             tmp_space = xrealloc (tmp_space, tmp_space_size);
            }
 
          /* Reset to beginning of the (possibly new) buffer space.  */
@@ -3393,7 +3397,9 @@ som_write_space_strings (bfd *abfd,
   /* Done with the space/subspace strings.  Write out any information
      contained in a partial block.  */
   amt = p - tmp_space;
-  if (bfd_bwrite ((void *) &tmp_space[0], amt, abfd) != amt)
+  res = bfd_bwrite ((void *) &tmp_space[0], amt, abfd);
+  free (tmp_space);
+  if (res != amt)
     return FALSE;
   *string_sizep = strings_size;
   return TRUE;
@@ -3410,15 +3416,14 @@ som_write_symbol_strings (bfd *abfd,
                          struct som_compilation_unit *compilation_unit)
 {
   unsigned int i;
-
   /* Chunk of memory that we can use as buffer space, then throw
      away.  */
   size_t tmp_space_size = SOM_TMP_BUFSIZE;
-  char *tmp_space = alloca (tmp_space_size);
+  char *tmp_space = xmalloc (tmp_space_size);
   char *p = tmp_space;
-
   unsigned int strings_size = 0;
   bfd_size_type amt;
+  bfd_size_type res;
 
   /* This gets a bit gruesome because of the compilation unit.  The
      strings within the compilation unit are part of the symbol
@@ -3477,7 +3482,7 @@ som_write_symbol_strings (bfd *abfd,
                     tmp_space_size = 5 + length;
                   else
                     tmp_space_size = 2 * tmp_space_size;
-                 tmp_space = alloca (tmp_space_size);
+                 tmp_space = xrealloc (tmp_space, tmp_space_size);
                }
 
              /* Reset to beginning of the (possibly new) buffer
@@ -3532,7 +3537,7 @@ som_write_symbol_strings (bfd *abfd,
                 tmp_space_size = 5 + length;
               else
                 tmp_space_size = 2 * tmp_space_size;
-             tmp_space = alloca (tmp_space_size);
+             tmp_space = xrealloc (tmp_space, tmp_space_size);
            }
 
          /* Reset to beginning of the (possibly new) buffer space.  */
@@ -3565,7 +3570,9 @@ som_write_symbol_strings (bfd *abfd,
 
   /* Scribble out any partial block.  */
   amt = p - tmp_space;
-  if (bfd_bwrite ((void *) &tmp_space[0], amt, abfd) != amt)
+  res = bfd_bwrite ((void *) &tmp_space[0], amt, abfd);
+  free (tmp_space);
+  if (res != amt)
     return FALSE;
 
   *string_sizep = strings_size;
@@ -4572,7 +4579,7 @@ som_get_symtab_upper_bound (bfd *abfd)
 
 /* Convert from a SOM subspace index to a BFD section.  */
 
-static asection *
+asection *
 bfd_section_from_som_symbol
   (bfd *abfd, struct som_external_symbol_dictionary_record *symbol)
 {
@@ -5351,7 +5358,7 @@ som_canonicalize_reloc (bfd *abfd,
   return section->reloc_count;
 }
 
-extern const bfd_target som_vec;
+extern const bfd_target hppa_som_vec;
 
 /* A hook to set up object file dependent section information.  */
 
@@ -5717,18 +5724,22 @@ som_set_arch_mach (bfd *abfd,
 
 static bfd_boolean
 som_find_nearest_line (bfd *abfd,
-                      asection *section,
                       asymbol **symbols,
+                      asection *section,
                       bfd_vma offset,
                       const char **filename_ptr,
                       const char **functionname_ptr,
-                      unsigned int *line_ptr)
+                      unsigned int *line_ptr,
+                      unsigned int *discriminator_ptr)
 {
   bfd_boolean found;
   asymbol *func;
   bfd_vma low_func;
   asymbol **p;
 
+  if (discriminator_ptr)
+    *discriminator_ptr = 0;
+
   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
                                              & found, filename_ptr,
                                              functionname_ptr, line_ptr,
@@ -5746,9 +5757,9 @@ som_find_nearest_line (bfd *abfd,
   low_func = 0;
 
   for (p = symbols; *p != NULL; p++)
-    { 
+    {
       som_symbol_type *q = (som_symbol_type *) *p;
-  
+
       if (q->som_type == SYMBOL_TYPE_ENTRY
          && q->symbol.section == section
          && q->symbol.value >= low_func
@@ -5773,7 +5784,7 @@ static int
 som_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
-  (*_bfd_error_handler) (_("som_sizeof_headers unimplemented"));
+  _bfd_error_handler (_("som_sizeof_headers unimplemented"));
   abort ();
   return 0;
 }
@@ -6715,6 +6726,8 @@ som_bfd_link_split_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
   return som_is_subspace (sec) && sec->size > 240000;
 }
 
+#define som_find_line                          _bfd_nosymbols_find_line
+#define som_get_symbol_version_string          _bfd_nosymbols_get_symbol_version_string
 #define        som_close_and_cleanup                   som_bfd_free_cached_info
 #define som_read_ar_hdr                                _bfd_generic_read_ar_hdr
 #define som_write_ar_hdr                       _bfd_generic_write_ar_hdr
@@ -6734,24 +6747,27 @@ som_bfd_link_split_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
 #define som_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
 #define som_bfd_relax_section                   bfd_generic_relax_section
 #define som_bfd_link_hash_table_create          _bfd_generic_link_hash_table_create
-#define som_bfd_link_hash_table_free            _bfd_generic_link_hash_table_free
 #define som_bfd_link_add_symbols                _bfd_generic_link_add_symbols
 #define som_bfd_link_just_syms                  _bfd_generic_link_just_syms
 #define som_bfd_copy_link_hash_symbol_type \
   _bfd_generic_copy_link_hash_symbol_type
 #define som_bfd_final_link                      _bfd_generic_final_link
 #define som_bfd_gc_sections                    bfd_generic_gc_sections
+#define som_bfd_lookup_section_flags            bfd_generic_lookup_section_flags
 #define som_bfd_merge_sections                 bfd_generic_merge_sections
 #define som_bfd_is_group_section               bfd_generic_is_group_section
 #define som_bfd_discard_group                  bfd_generic_discard_group
 #define som_section_already_linked              _bfd_generic_section_already_linked
 #define som_bfd_define_common_symbol            bfd_generic_define_common_symbol
+#define som_bfd_define_start_stop               bfd_generic_define_start_stop
 #define som_bfd_merge_private_bfd_data         _bfd_generic_bfd_merge_private_bfd_data
 #define som_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
 #define som_bfd_set_private_flags              _bfd_generic_bfd_set_private_flags
 #define som_find_inliner_info                  _bfd_nosymbols_find_inliner_info
+#define som_bfd_link_check_relocs               _bfd_generic_link_check_relocs
+#define som_set_reloc                          _bfd_generic_set_reloc
 
-const bfd_target som_vec =
+const bfd_target hppa_som_vec =
 {
   "som",                       /* Name.  */
   bfd_target_som_flavour,
@@ -6768,6 +6784,7 @@ const bfd_target som_vec =
   0,
   '/',                         /* AR_pad_char.  */
   14,                          /* AR_max_namelen.  */
+  0,                           /* match priority.  */
   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
   bfd_getb16, bfd_getb_signed_16, bfd_putb16,  /* Data.  */
This page took 0.02716 seconds and 4 git commands to generate.