]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/rdcoff.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / rdcoff.c
index ee68bc6d449f39f152c89531ae68ed4c1effbdba..69fa5706c07dd5354690eeaecd3dc148dfdffa85 100644 (file)
@@ -1,12 +1,12 @@
 /* stabs.c -- Parse COFF debugging information
-   Copyright (C) 1996, 1998 Free Software Foundation, Inc.
+   Copyright (C) 1996-2022 Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@cygnus.com>.
 
    This file is part of GNU Binutils.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
 
    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., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
 
 /* This file contains code which parses COFF debugging information.  */
 
+#include "sysdep.h"
 #include "bfd.h"
 #include "coff/internal.h"
-#include "bucomm.h"
 #include "libiberty.h"
-#include "demangle.h"
+#include "bucomm.h"
 #include "debug.h"
 #include "budbg.h"
 
@@ -83,34 +83,38 @@ struct coff_types
   debug_type basic[T_MAX + 1];
 };
 
-static debug_type *coff_get_slot PARAMS ((struct coff_types *, int));
+static debug_type *coff_get_slot (struct coff_types *, long);
 static debug_type parse_coff_type
-  PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, long, int,
-          union internal_auxent *, boolean, PTR));
+  (bfd *, struct coff_symbols *, struct coff_types *, long, int,
+   union internal_auxent *, bool, void *);
 static debug_type parse_coff_base_type
-  PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, long, int,
-          union internal_auxent *, PTR));
+  (bfd *, struct coff_symbols *, struct coff_types *, long, int,
+   union internal_auxent *, void *);
 static debug_type parse_coff_struct_type
-  PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, int,
-          union internal_auxent *, PTR));
+  (bfd *, struct coff_symbols *, struct coff_types *, int,
+   union internal_auxent *, void *);
 static debug_type parse_coff_enum_type
-  PARAMS ((bfd *, struct coff_symbols *, struct coff_types *,
-          union internal_auxent *, PTR));
-static boolean parse_coff_symbol
-  PARAMS ((bfd *, struct coff_types *, asymbol *, long,
-          struct internal_syment *, PTR, debug_type, boolean));
+  (bfd *, struct coff_symbols *, struct coff_types *,
+   union internal_auxent *, void *);
+static bool parse_coff_symbol
+  (bfd *, struct coff_types *, asymbol *, long, struct internal_syment *,
+   void *, debug_type, bool);
+static bool external_coff_symbol_p (int sym_class);
 \f
 /* Return the slot for a type.  */
 
 static debug_type *
-coff_get_slot (types, indx)
-     struct coff_types *types;
-     int indx;
+coff_get_slot (struct coff_types *types, long indx)
 {
   struct coff_slots **pps;
 
   pps = &types->slots;
 
+  /* PR 17512: file: 078-18333-0.001:0.1.
+     FIXME: The value of 1000 is a guess.  Maybe a better heuristic is needed.  */
+  if (indx / COFF_SLOTS > 1000)
+    fatal (_("Excessively large slot index: %lx"), indx);
+
   while (indx >= COFF_SLOTS)
     {
       if (*pps == NULL)
@@ -134,16 +138,10 @@ coff_get_slot (types, indx)
 /* Parse a COFF type code in NTYPE.  */
 
 static debug_type
-parse_coff_type (abfd, symbols, types, coff_symno, ntype, pauxent, useaux,
-                dhandle)
-     bfd *abfd;
-     struct coff_symbols *symbols;
-     struct coff_types *types;
-     long coff_symno;
-     int ntype;
-     union internal_auxent *pauxent;
-     boolean useaux;
-     PTR dhandle;
+parse_coff_type (bfd *abfd, struct coff_symbols *symbols,
+                struct coff_types *types, long coff_symno, int ntype,
+                union internal_auxent *pauxent, bool useaux,
+                void *dhandle)
 {
   debug_type type;
 
@@ -201,8 +199,7 @@ parse_coff_type (abfd, symbols, types, coff_symno, ntype, pauxent, useaux,
        }
       else
        {
-         fprintf (stderr, _("%s: parse_coff_type: Bad type code 0x%x\n"),
-                  program_name, ntype);
+         non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype);
          return DEBUG_TYPE_NULL;
        }
 
@@ -238,18 +235,12 @@ parse_coff_type (abfd, symbols, types, coff_symno, ntype, pauxent, useaux,
 /* Parse a basic COFF type in NTYPE.  */
 
 static debug_type
-parse_coff_base_type (abfd, symbols, types, coff_symno, ntype, pauxent,
-                     dhandle)
-     bfd *abfd;
-     struct coff_symbols *symbols;
-     struct coff_types *types;
-     long coff_symno;
-     int ntype;
-     union internal_auxent *pauxent;
-     PTR dhandle;
+parse_coff_base_type (bfd *abfd, struct coff_symbols *symbols,
+                     struct coff_types *types, long coff_symno, int ntype,
+                     union internal_auxent *pauxent, void *dhandle)
 {
   debug_type ret;
-  boolean set_basic;
+  bool set_basic;
   const char *name;
   debug_type *slot;
 
@@ -384,19 +375,15 @@ parse_coff_base_type (abfd, symbols, types, coff_symno, ntype, pauxent,
 /* Parse a struct type.  */
 
 static debug_type
-parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, dhandle)
-     bfd *abfd;
-     struct coff_symbols *symbols;
-     struct coff_types *types;
-     int ntype;
-     union internal_auxent *pauxent;
-     PTR dhandle;
+parse_coff_struct_type (bfd *abfd, struct coff_symbols *symbols,
+                       struct coff_types *types, int ntype,
+                       union internal_auxent *pauxent, void *dhandle)
 {
   long symend;
   int alloc;
   debug_field *fields;
   int count;
-  boolean done;
+  bool done;
 
   symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
 
@@ -420,8 +407,9 @@ parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, dhandle)
 
       if (! bfd_coff_get_syment (abfd, sym, &syment))
        {
-         fprintf (stderr, _("%s: bfd_coff_get_syment failed: %s\n"),
-                  program_name, bfd_errmsg (bfd_get_error ()));
+         non_fatal (_("bfd_coff_get_syment failed: %s"),
+                    bfd_errmsg (bfd_get_error ()));
+         free (fields);
          return DEBUG_TYPE_NULL;
        }
 
@@ -436,8 +424,9 @@ parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, dhandle)
        {
          if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
            {
-             fprintf (stderr, _("%s: bfd_coff_get_auxent failed: %s\n"),
-                      program_name, bfd_errmsg (bfd_get_error ()));
+             non_fatal (_("bfd_coff_get_auxent failed: %s"),
+                        bfd_errmsg (bfd_get_error ()));
+             free (fields);
              return DEBUG_TYPE_NULL;
            }
          psubaux = &auxent;
@@ -495,19 +484,16 @@ parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, dhandle)
 /* Parse an enum type.  */
 
 static debug_type
-parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle)
-     bfd *abfd;
-     struct coff_symbols *symbols;
-     struct coff_types *types;
-     union internal_auxent *pauxent;
-     PTR dhandle;
+parse_coff_enum_type (bfd *abfd, struct coff_symbols *symbols,
+                     struct coff_types *types ATTRIBUTE_UNUSED,
+                     union internal_auxent *pauxent, void *dhandle)
 {
   long symend;
   int alloc;
   const char **names;
   bfd_signed_vma *vals;
   int count;
-  boolean done;
+  bool done;
 
   symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
 
@@ -528,8 +514,10 @@ parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle)
 
       if (! bfd_coff_get_syment (abfd, sym, &syment))
        {
-         fprintf (stderr, _("%s: bfd_coff_get_syment failed: %s\n"),
-                  program_name, bfd_errmsg (bfd_get_error ()));
+         non_fatal (_("bfd_coff_get_syment failed: %s"),
+                    bfd_errmsg (bfd_get_error ()));
+         free (names);
+         free (vals);
          return DEBUG_TYPE_NULL;
        }
 
@@ -566,17 +554,11 @@ parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle)
 
 /* Handle a single COFF symbol.  */
 
-static boolean
-parse_coff_symbol (abfd, types, sym, coff_symno, psyment, dhandle, type,
-                  within_function)
-     bfd *abfd;
-     struct coff_types *types;
-     asymbol *sym;
-     long coff_symno;
-     struct internal_syment *psyment;
-     PTR dhandle;
-     debug_type type;
-     boolean within_function;
+static bool
+parse_coff_symbol (bfd *abfd ATTRIBUTE_UNUSED, struct coff_types *types,
+                  asymbol *sym, long coff_symno,
+                  struct internal_syment *psyment, void *dhandle,
+                  debug_type type, bool within_function)
 {
   switch (psyment->n_sclass)
     {
@@ -589,6 +571,7 @@ parse_coff_symbol (abfd, types, sym, coff_symno, psyment, dhandle, type,
        return false;
       break;
 
+    case C_WEAKEXT:
     case C_EXT:
       if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
                                   DEBUG_GLOBAL, bfd_asymbol_value (sym)))
@@ -654,18 +637,30 @@ parse_coff_symbol (abfd, types, sym, coff_symno, psyment, dhandle, type,
       break;
     }
 
-  return true;                            
+  return true;
+}
+
+/* Determine if a symbol has external visibility.  */
+
+static bool
+external_coff_symbol_p (int sym_class)
+{
+  switch (sym_class)
+    {
+    case C_EXT:
+    case C_WEAKEXT:
+      return true;
+    default:
+      break;
+    }
+  return false;
 }
 
 /* This is the main routine.  It looks through all the symbols and
    handles them.  */
 
-boolean
-parse_coff (abfd, syms, symcount, dhandle)
-     bfd *abfd;
-     asymbol **syms;
-     long symcount;
-     PTR dhandle;
+bool
+parse_coff (bfd *abfd, asymbol **syms, long symcount, void *dhandle)
 {
   struct coff_symbols symbols;
   struct coff_types types;
@@ -676,7 +671,7 @@ parse_coff (abfd, syms, symcount, dhandle)
   int fntype;
   bfd_vma fnend;
   alent *linenos;
-  boolean within_function;
+  bool within_function;
   long this_coff_symno;
 
   symbols.syms = syms;
@@ -709,8 +704,8 @@ parse_coff (abfd, syms, symcount, dhandle)
 
       if (! bfd_coff_get_syment (abfd, sym, &syment))
        {
-         fprintf (stderr, _("%s: bfd_coff_get_syment failed: %s\n"),
-                  program_name, bfd_errmsg (bfd_get_error ()));
+         non_fatal (_("bfd_coff_get_syment failed: %s"),
+                    bfd_errmsg (bfd_get_error ()));
          return false;
        }
 
@@ -729,8 +724,8 @@ parse_coff (abfd, syms, symcount, dhandle)
        {
          if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
            {
-             fprintf (stderr, _("%s: bfd_coff_get_auxent failed: %s\n"),
-                      program_name, bfd_errmsg (bfd_get_error ()));
+             non_fatal (_("bfd_coff_get_auxent failed: %s"),
+                        bfd_errmsg (bfd_get_error ()));
              return false;
            }
          paux = &auxent;
@@ -768,6 +763,7 @@ parse_coff (abfd, syms, symcount, dhandle)
          if (syment.n_type == T_NULL)
            break;
          /* Fall through.  */
+       case C_WEAKEXT:
        case C_EXT:
          if (ISFCN (syment.n_type))
            {
@@ -795,8 +791,8 @@ parse_coff (abfd, syms, symcount, dhandle)
            {
              if (fnname == NULL)
                {
-                 fprintf (stderr, _("%s: %ld: .bf without preceding function\n"),
-                          program_name, this_coff_symno);
+                 non_fatal (_("%ld: .bf without preceding function"),
+                            this_coff_symno);
                  return false;
                }
 
@@ -806,7 +802,7 @@ parse_coff (abfd, syms, symcount, dhandle)
                return false;
 
              if (! debug_record_function (dhandle, fnname, type,
-                                          fnclass == C_EXT,
+                                          external_coff_symbol_p (fnclass),
                                           bfd_asymbol_value (sym)))
                return false;
 
@@ -820,7 +816,7 @@ parse_coff (abfd, syms, symcount, dhandle)
                  else
                    base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1;
 
-                 addr = bfd_get_section_vma (abfd, bfd_get_section (sym));
+                 addr = bfd_section_vma (bfd_asymbol_section (sym));
 
                  ++linenos;
 
@@ -845,8 +841,7 @@ parse_coff (abfd, syms, symcount, dhandle)
            {
              if (! within_function)
                {
-                 fprintf (stderr, _("%s: %ld: unexpected .ef\n"),
-                          program_name, this_coff_symno);
+                 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno);
                  return false;
                }