Fix buffer overflow regression due to minsym malloc-ed instead of obstack-ed.
[deliverable/binutils-gdb.git] / gdb / coffread.c
index 758cd49b3947fdfc4ea0f52ac4c4f04bf8ed2845..4354741ab648948d3526768d235fad354cdf5e56 100644 (file)
@@ -1,5 +1,5 @@
 /* Read coff symbol tables and convert to internal format, for GDB.
-   Copyright (C) 1987-2018 Free Software Foundation, Inc.
+   Copyright (C) 1987-2019 Free Software Foundation, Inc.
    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
 
    This file is part of GDB.
@@ -30,7 +30,7 @@
 #include "coff/internal.h"     /* Internal format of COFF symbols in BFD */
 #include "libcoff.h"           /* FIXME secret internal data from BFD */
 #include "objfiles.h"
-#include "buildsym.h"
+#include "buildsym-legacy.h"
 #include "gdb-stabs.h"
 #include "stabsread.h"
 #include "complaints.h"
@@ -58,7 +58,7 @@ struct coff_symfile_info
 
     CORE_ADDR textaddr;                /* Addr of .text section.  */
     unsigned int textsize;     /* Size of .text section.  */
-    struct stab_section_list *stabsects;       /* .stab sections.  */
+    std::vector<asection *> *stabsects;        /* .stab sections.  */
     asection *stabstrsect;     /* Section pointer for .stab section.  */
     char *stabstrdata;
   };
@@ -155,6 +155,12 @@ static int type_vector_length;
 
 #define INITIAL_TYPE_VECTOR_LENGTH 160
 
+static char *linetab = NULL;
+static long linetab_offset;
+static unsigned long linetab_size;
+
+static char *stringtab = NULL;
+
 extern void stabsread_clear_cache (void);
 
 static struct type *coff_read_struct_type (int, int, int,
@@ -185,21 +191,13 @@ static void patch_opaque_types (struct symtab *);
 
 static void enter_linenos (long, int, int, struct objfile *);
 
-static void free_linetab (void);
-
-static void free_linetab_cleanup (void *ignore);
-
-static int init_lineno (bfd *, long, int);
+static int init_lineno (bfd *, long, int, gdb::unique_xmalloc_ptr<char> *);
 
 static char *getsymname (struct internal_syment *);
 
 static const char *coff_getfilename (union internal_auxent *);
 
-static void free_stringtab (void);
-
-static void free_stringtab_cleanup (void *ignore);
-
-static int init_stringtab (bfd *, long);
+static int init_stringtab (bfd *, long, gdb::unique_xmalloc_ptr<char> *);
 
 static void read_one_sym (struct coff_symbol *,
                          struct internal_syment *,
@@ -249,21 +247,7 @@ coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
        if (!isdigit (*s))
          break;
       if (*s == '\0')
-       {
-         struct stab_section_list *n, **pn;
-
-         n = XNEW (struct stab_section_list);
-         n->section = sectp;
-         n->next = NULL;
-         for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
-           ;
-         *pn = n;
-
-         /* This will be run after coffstab_build_psymtabs is called
-            in coff_symfile_read, at which point we no longer need
-            the information.  */
-         make_cleanup (xfree, n);
-       }
+       csi->stabsects->push_back (sectp);
     }
 }
 
@@ -385,10 +369,7 @@ coff_start_symtab (struct objfile *objfile, const char *name)
 {
   within_function = 0;
   start_symtab (objfile,
-  /* We fill in the filename later.  start_symtab puts this pointer
-     into last_source_file and we put it in subfiles->name, which
-     end_symtab frees; that's why it must be malloc'd.  */
-                xstrdup (name),
+               name,
   /* We never know the directory name for COFF.  */
                 NULL,
   /* The start address is irrelevant, since we call
@@ -482,7 +463,8 @@ record_minimal_symbol (minimal_symbol_reader &reader,
       return NULL;
     }
 
-  return reader.record_with_info (cs->c_name, address, type, section);
+  return reader.record_full (cs->c_name, strlen (cs->c_name), true, address,
+                            type, section);
 }
 \f
 /* coff_symfile_init ()
@@ -565,18 +547,21 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
   struct coff_symfile_info *info;
   bfd *abfd = objfile->obfd;
   coff_data_type *cdata = coff_data (abfd);
-  char *name = bfd_get_filename (abfd);
+  char *filename = bfd_get_filename (abfd);
   int val;
   unsigned int num_symbols;
   int symtab_offset;
   int stringtab_offset;
-  struct cleanup *back_to;
   int stabstrsize;
   
   info = (struct coff_symfile_info *) objfile_data (objfile,
                                                    coff_objfile_data_key);
   symfile_bfd = abfd;          /* Kludge for swap routines.  */
 
+  std::vector<asection *> stabsects;
+  scoped_restore restore_stabsects
+    = make_scoped_restore (&info->stabsects, &stabsects);
+
 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
   num_symbols = bfd_get_symcount (abfd);       /* How many syms */
   symtab_offset = cdata->sym_filepos;  /* Symbol table file offset */
@@ -595,10 +580,10 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
   /* Allocate space for raw symbol and aux entries, based on their
      space requirements as reported by BFD.  */
-  temp_sym = (char *) xmalloc
-    (cdata->local_symesz + cdata->local_auxesz);
+  gdb::def_vector<char> temp_storage (cdata->local_symesz
+                                     + cdata->local_auxesz);
+  temp_sym = temp_storage.data ();
   temp_aux = temp_sym + cdata->local_symesz;
-  back_to = make_cleanup (free_current_contents, &temp_sym);
 
   /* We need to know whether this is a PE file, because in PE files,
      unlike standard COFF files, symbol values are stored as offsets
@@ -628,24 +613,27 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
      can avoid spurious error messages (and maybe run a little
      faster!) by not even reading the line number table unless we have
      symbols.  */
+  scoped_restore restore_linetab = make_scoped_restore (&linetab);
+  gdb::unique_xmalloc_ptr<char> linetab_storage;
   if (num_symbols > 0)
     {
       /* Read the line number table, all at once.  */
       bfd_map_over_sections (abfd, find_linenos, (void *) info);
 
-      make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
       val = init_lineno (abfd, info->min_lineno_offset,
-                         info->max_lineno_offset - info->min_lineno_offset);
+                         info->max_lineno_offset - info->min_lineno_offset,
+                        &linetab_storage);
       if (val < 0)
-        error (_("\"%s\": error reading line numbers."), name);
+        error (_("\"%s\": error reading line numbers."), filename);
     }
 
   /* Now read the string table, all at once.  */
 
-  make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
-  val = init_stringtab (abfd, stringtab_offset);
+  scoped_restore restore_stringtab = make_scoped_restore (&stringtab);
+  gdb::unique_xmalloc_ptr<char> stringtab_storage;
+  val = init_stringtab (abfd, stringtab_offset, &stringtab_storage);
   if (val < 0)
-    error (_("\"%s\": can't get string table"), name);
+    error (_("\"%s\": can't get string table"), filename);
 
   minimal_symbol_reader reader (objfile);
 
@@ -661,9 +649,7 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
   if (pe_file)
     {
-      struct minimal_symbol *msym;
-
-      ALL_OBJFILE_MSYMBOLS (objfile, msym)
+      for (minimal_symbol *msym : objfile->msymbols ())
        {
          const char *name = MSYMBOL_LINKAGE_NAME (msym);
 
@@ -703,13 +689,13 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
   if (!(objfile->flags & OBJF_READNEVER))
     bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
 
-  if (info->stabsects)
+  if (!info->stabsects->empty())
     {
       if (!info->stabstrsect)
        {
          error (_("The debugging information in `%s' is corrupted.\nThe "
                   "file has a `.stabs' section, but no `.stabstr' section."),
-                name);
+                filename);
        }
 
       /* FIXME: dubious.  Why can't we use something normal like
@@ -720,7 +706,7 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
       coffstab_build_psymtabs (objfile,
                               info->textaddr, info->textsize,
-                              info->stabsects,
+                              *info->stabsects,
                               info->stabstrsect->filepos, stabstrsize);
     }
   if (dwarf2_has_info (objfile, NULL))
@@ -741,14 +727,12 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
       if (!debugfile.empty ())
        {
-         gdb_bfd_ref_ptr abfd (symfile_bfd_open (debugfile.c_str ()));
+         gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ()));
 
-         symbol_file_add_separate (abfd.get (), debugfile.c_str (),
+         symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
                                    symfile_flags, objfile);
        }
     }
-
-  do_cleanups (back_to);
 }
 
 static void
@@ -781,7 +765,7 @@ coff_symtab_read (minimal_symbol_reader &reader,
                  struct objfile *objfile)
 {
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
-  struct context_stack *newobj;
+  struct context_stack *newobj = nullptr;
   struct coff_symbol coff_symbol;
   struct coff_symbol *cs = &coff_symbol;
   static struct internal_syment main_sym;
@@ -804,7 +788,6 @@ coff_symtab_read (minimal_symbol_reader &reader,
   CORE_ADDR tmpaddr;
   struct minimal_symbol *msym;
 
-  buildsym_init ();
   scoped_free_pendings free_pending;
 
   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
@@ -878,8 +861,10 @@ coff_symtab_read (minimal_symbol_reader &reader,
          int section = cs_to_section (cs, objfile);
 
          tmpaddr = cs->c_value;
-         record_minimal_symbol (reader, cs, tmpaddr, mst_text,
-                                section, objfile);
+         /* Don't record unresolved symbols.  */
+         if (!(cs->c_secnum <= 0 && cs->c_value == 0))
+           record_minimal_symbol (reader, cs, tmpaddr, mst_text,
+                                  section, objfile);
 
          fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
          fcn_start_addr = tmpaddr;
@@ -1135,7 +1120,7 @@ coff_symtab_read (minimal_symbol_reader &reader,
                enter_linenos (fcn_line_ptr, fcn_first_line,
                               fcn_last_line, objfile);
 
-             finish_block (cstk.name, &local_symbols, cstk.old_blocks,
+             finish_block (cstk.name, cstk.old_blocks,
                            NULL, cstk.start_addr,
                            fcn_cs_saved.c_value
                            + fcn_aux_saved.x_sym.x_misc.x_fsize
@@ -1171,17 +1156,17 @@ coff_symtab_read (minimal_symbol_reader &reader,
                             symnum);
                  break;
                }
-             if (local_symbols && !outermost_context_p ())
+             if (*get_local_symbols () && !outermost_context_p ())
                {
                  tmpaddr =
                    cs->c_value + ANOFFSET (objfile->section_offsets,
                                            SECT_OFF_TEXT (objfile));
                  /* Make a block for the local symbols within.  */
-                 finish_block (0, &local_symbols, cstk.old_blocks, NULL,
+                 finish_block (0, cstk.old_blocks, NULL,
                                cstk.start_addr, tmpaddr);
                }
              /* Now pop locals of block just finished.  */
-             local_symbols = cstk.locals;
+             *get_local_symbols () = cstk.locals;
            }
          break;
 
@@ -1204,11 +1189,11 @@ coff_symtab_read (minimal_symbol_reader &reader,
   /* Patch up any opaque types (references to types that are not defined
      in the file where they are referenced, e.g. "struct foo *bar").  */
   {
-    struct compunit_symtab *cu;
-    struct symtab *s;
-
-    ALL_OBJFILE_FILETABS (objfile, cu, s)
-      patch_opaque_types (s);
+    for (compunit_symtab *cu : objfile->compunits ())
+      {
+       for (symtab *s : compunit_filetabs (cu))
+         patch_opaque_types (s);
+      }
   }
 
   coffread_objfile = NULL;
@@ -1299,17 +1284,13 @@ read_one_sym (struct coff_symbol *cs,
 \f
 /* Support for string table handling.  */
 
-static char *stringtab = NULL;
-
 static int
-init_stringtab (bfd *abfd, long offset)
+init_stringtab (bfd *abfd, long offset, gdb::unique_xmalloc_ptr<char> *storage)
 {
   long length;
   int val;
   unsigned char lengthbuf[4];
 
-  free_stringtab ();
-
   /* If the file is stripped, the offset might be zero, indicating no
      string table.  Just return with `stringtab' set to null.  */
   if (offset == 0)
@@ -1326,7 +1307,8 @@ init_stringtab (bfd *abfd, long offset)
   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
     return 0;
 
-  stringtab = (char *) xmalloc (length);
+  storage->reset ((char *) xmalloc (length));
+  stringtab = storage->get ();
   /* This is in target format (probably not very useful, and not
      currently used), not host format.  */
   memcpy (stringtab, lengthbuf, sizeof lengthbuf);
@@ -1341,20 +1323,6 @@ init_stringtab (bfd *abfd, long offset)
   return 0;
 }
 
-static void
-free_stringtab (void)
-{
-  if (stringtab)
-    xfree (stringtab);
-  stringtab = NULL;
-}
-
-static void
-free_stringtab_cleanup (void *ignore)
-{
-  free_stringtab ();
-}
-
 static char *
 getsymname (struct internal_syment *symbol_entry)
 {
@@ -1408,24 +1376,19 @@ coff_getfilename (union internal_auxent *aux_entry)
 \f
 /* Support for line number handling.  */
 
-static char *linetab = NULL;
-static long linetab_offset;
-static unsigned long linetab_size;
-
 /* Read in all the line numbers for fast lookups later.  Leave them in
    external (unswapped) format in memory; we'll swap them as we enter
    them into GDB's data structures.  */
 
 static int
-init_lineno (bfd *abfd, long offset, int size)
+init_lineno (bfd *abfd, long offset, int size,
+            gdb::unique_xmalloc_ptr<char> *storage)
 {
   int val;
 
   linetab_offset = offset;
   linetab_size = size;
 
-  free_linetab ();
-
   if (size == 0)
     return 0;
 
@@ -1433,9 +1396,10 @@ init_lineno (bfd *abfd, long offset, int size)
     return -1;
 
   /* Allocate the desired table, plus a sentinel.  */
-  linetab = (char *) xmalloc (size + local_linesz);
+  storage->reset ((char *) xmalloc (size + local_linesz));
+  linetab = storage->get ();
 
-  val = bfd_bread (linetab, size, abfd);
+  val = bfd_bread (storage->get (), size, abfd);
   if (val != size)
     return -1;
 
@@ -1445,20 +1409,6 @@ init_lineno (bfd *abfd, long offset, int size)
   return 0;
 }
 
-static void
-free_linetab (void)
-{
-  if (linetab)
-    xfree (linetab);
-  linetab = NULL;
-}
-
-static void
-free_linetab_cleanup (void *ignore)
-{
-  free_linetab ();
-}
-
 #if !defined (L_LNNO32)
 #define L_LNNO32(lp) ((lp)->l_lnno)
 #endif
@@ -1546,7 +1496,7 @@ patch_type (struct type *type, struct type *real_type)
 static void
 patch_opaque_types (struct symtab *s)
 {
-  struct block *b;
+  const struct block *b;
   struct block_iterator iter;
   struct symbol *real_sym;
 
@@ -1647,10 +1597,10 @@ process_coff_symbol (struct coff_symbol *cs,
       SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
       if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
          || cs->c_sclass == C_THUMBSTATFUNC)
-       add_symbol_to_list (sym, &file_symbols);
+       add_symbol_to_list (sym, get_file_symbols ());
       else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
               || cs->c_sclass == C_THUMBEXTFUNC)
-       add_symbol_to_list (sym, &global_symbols);
+       add_symbol_to_list (sym, get_global_symbols ());
     }
   else
     {
@@ -1662,7 +1612,7 @@ process_coff_symbol (struct coff_symbol *cs,
 
        case C_AUTO:
          SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
-         add_symbol_to_list (sym, &local_symbols);
+         add_symbol_to_list (sym, get_local_symbols ());
          break;
 
        case C_THUMBEXT:
@@ -1672,7 +1622,7 @@ process_coff_symbol (struct coff_symbol *cs,
          SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
          SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
                                                  SECT_OFF_TEXT (objfile));
-         add_symbol_to_list (sym, &global_symbols);
+         add_symbol_to_list (sym, get_global_symbols ());
          break;
 
        case C_THUMBSTAT:
@@ -1685,12 +1635,12 @@ process_coff_symbol (struct coff_symbol *cs,
          if (within_function)
            {
              /* Static symbol of local scope.  */
-             add_symbol_to_list (sym, &local_symbols);
+             add_symbol_to_list (sym, get_local_symbols ());
            }
          else
            {
              /* Static symbol at top level of file.  */
-             add_symbol_to_list (sym, &file_symbols);
+             add_symbol_to_list (sym, get_file_symbols ());
            }
          break;
 
@@ -1700,7 +1650,7 @@ process_coff_symbol (struct coff_symbol *cs,
        case C_REG:
          SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
          SYMBOL_VALUE (sym) = cs->c_value;
-         add_symbol_to_list (sym, &local_symbols);
+         add_symbol_to_list (sym, get_local_symbols ());
          break;
 
        case C_THUMBLABEL:
@@ -1710,14 +1660,14 @@ process_coff_symbol (struct coff_symbol *cs,
        case C_ARG:
          SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
          SYMBOL_IS_ARGUMENT (sym) = 1;
-         add_symbol_to_list (sym, &local_symbols);
+         add_symbol_to_list (sym, get_local_symbols ());
          break;
 
        case C_REGPARM:
          SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
          SYMBOL_IS_ARGUMENT (sym) = 1;
          SYMBOL_VALUE (sym) = cs->c_value;
-         add_symbol_to_list (sym, &local_symbols);
+         add_symbol_to_list (sym, get_local_symbols ());
          break;
 
        case C_TPDEF:
@@ -1771,7 +1721,7 @@ process_coff_symbol (struct coff_symbol *cs,
              SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
              opaque_type_chain[i] = sym;
            }
-         add_symbol_to_list (sym, &file_symbols);
+         add_symbol_to_list (sym, get_file_symbols ());
          break;
 
        case C_STRTAG:
@@ -1790,7 +1740,7 @@ process_coff_symbol (struct coff_symbol *cs,
              TYPE_NAME (SYMBOL_TYPE (sym)) =
                concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);
 
-         add_symbol_to_list (sym, &file_symbols);
+         add_symbol_to_list (sym, get_file_symbols ());
          break;
 
        default:
@@ -2154,9 +2104,9 @@ coff_read_enum_type (int index, int length, int lastsym,
 
   type = coff_alloc_type (index);
   if (within_function)
-    symlist = &local_symbols;
+    symlist = get_local_symbols ();
   else
-    symlist = &file_symbols;
+    symlist = get_file_symbols ();
   osyms = *symlist;
   o_nsyms = osyms ? osyms->nsyms : 0;
 
This page took 0.031087 seconds and 4 git commands to generate.