Home | History | Annotate | Download | only in binutils
      1 /* stabs.c -- Parse stabs debugging information
      2    Copyright (C) 1995-2016 Free Software Foundation, Inc.
      3    Written by Ian Lance Taylor <ian (at) cygnus.com>.
      4 
      5    This file is part of GNU Binutils.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
     20    02110-1301, USA.  */
     21 
     22 /* This file contains code which parses stabs debugging information.
     23    The organization of this code is based on the gdb stabs reading
     24    code.  The job it does is somewhat different, because it is not
     25    trying to identify the correct address for anything.  */
     26 
     27 #include "sysdep.h"
     28 #include "bfd.h"
     29 #include "libiberty.h"
     30 #include "safe-ctype.h"
     31 #include "demangle.h"
     32 #include "debug.h"
     33 #include "budbg.h"
     34 #include "filenames.h"
     35 #include "aout/aout64.h"
     36 #include "aout/stab_gnu.h"
     37 
     38 /* The number of predefined XCOFF types.  */
     39 
     40 #define XCOFF_TYPE_COUNT 34
     41 
     42 /* This structure is used as a handle so that the stab parsing doesn't
     43    need to use any static variables.  */
     44 
     45 struct stab_handle
     46 {
     47   /* The BFD.  */
     48   bfd *abfd;
     49   /* TRUE if this is stabs in sections.  */
     50   bfd_boolean sections;
     51   /* The symbol table.  */
     52   asymbol **syms;
     53   /* The number of symbols.  */
     54   long symcount;
     55   /* The accumulated file name string.  */
     56   char *so_string;
     57   /* The value of the last N_SO symbol.  */
     58   bfd_vma so_value;
     59   /* The value of the start of the file, so that we can handle file
     60      relative N_LBRAC and N_RBRAC symbols.  */
     61   bfd_vma file_start_offset;
     62   /* The offset of the start of the function, so that we can handle
     63      function relative N_LBRAC and N_RBRAC symbols.  */
     64   bfd_vma function_start_offset;
     65   /* The version number of gcc which compiled the current compilation
     66      unit, 0 if not compiled by gcc.  */
     67   int gcc_compiled;
     68   /* Whether an N_OPT symbol was seen that was not generated by gcc,
     69      so that we can detect the SunPRO compiler.  */
     70   bfd_boolean n_opt_found;
     71   /* The main file name.  */
     72   char *main_filename;
     73   /* A stack of unfinished N_BINCL files.  */
     74   struct bincl_file *bincl_stack;
     75   /* A list of finished N_BINCL files.  */
     76   struct bincl_file *bincl_list;
     77   /* Whether we are inside a function or not.  */
     78   bfd_boolean within_function;
     79   /* The address of the end of the function, used if we have seen an
     80      N_FUN symbol while in a function.  This is -1 if we have not seen
     81      an N_FUN (the normal case).  */
     82   bfd_vma function_end;
     83   /* The depth of block nesting.  */
     84   int block_depth;
     85   /* List of pending variable definitions.  */
     86   struct stab_pending_var *pending;
     87   /* Number of files for which we have types.  */
     88   unsigned int files;
     89   /* Lists of types per file.  */
     90   struct stab_types **file_types;
     91   /* Predefined XCOFF types.  */
     92   debug_type xcoff_types[XCOFF_TYPE_COUNT];
     93   /* Undefined tags.  */
     94   struct stab_tag *tags;
     95   /* Set by parse_stab_type if it sees a structure defined as a cross
     96      reference to itself.  Reset by parse_stab_type otherwise.  */
     97   bfd_boolean self_crossref;
     98 };
     99 
    100 /* A list of these structures is used to hold pending variable
    101    definitions seen before the N_LBRAC of a block.  */
    102 
    103 struct stab_pending_var
    104 {
    105   /* Next pending variable definition.  */
    106   struct stab_pending_var *next;
    107   /* Name.  */
    108   const char *name;
    109   /* Type.  */
    110   debug_type type;
    111   /* Kind.  */
    112   enum debug_var_kind kind;
    113   /* Value.  */
    114   bfd_vma val;
    115 };
    116 
    117 /* A list of these structures is used to hold the types for a single
    118    file.  */
    119 
    120 struct stab_types
    121 {
    122   /* Next set of slots for this file.  */
    123   struct stab_types *next;
    124   /* Types indexed by type number.  */
    125 #define STAB_TYPES_SLOTS (16)
    126   debug_type types[STAB_TYPES_SLOTS];
    127 };
    128 
    129 /* We keep a list of undefined tags that we encounter, so that we can
    130    fill them in if the tag is later defined.  */
    131 
    132 struct stab_tag
    133 {
    134   /* Next undefined tag.  */
    135   struct stab_tag *next;
    136   /* Tag name.  */
    137   const char *name;
    138   /* Type kind.  */
    139   enum debug_type_kind kind;
    140   /* Slot to hold real type when we discover it.  If we don't, we fill
    141      in an undefined tag type.  */
    142   debug_type slot;
    143   /* Indirect type we have created to point at slot.  */
    144   debug_type type;
    145 };
    146 
    147 static char *savestring (const char *, int);
    148 static bfd_vma parse_number (const char **, bfd_boolean *);
    149 static void bad_stab (const char *);
    150 static void warn_stab (const char *, const char *);
    151 static bfd_boolean parse_stab_string
    152   (void *, struct stab_handle *, int, int, bfd_vma, const char *);
    153 static debug_type parse_stab_type
    154   (void *, struct stab_handle *, const char *, const char **, debug_type **);
    155 static bfd_boolean parse_stab_type_number (const char **, int *);
    156 static debug_type parse_stab_range_type
    157   (void *, struct stab_handle *, const char *, const char **, const int *);
    158 static debug_type parse_stab_sun_builtin_type (void *, const char **);
    159 static debug_type parse_stab_sun_floating_type (void *, const char **);
    160 static debug_type parse_stab_enum_type (void *, const char **);
    161 static debug_type parse_stab_struct_type
    162   (void *, struct stab_handle *, const char *, const char **,
    163    bfd_boolean, const int *);
    164 static bfd_boolean parse_stab_baseclasses
    165   (void *, struct stab_handle *, const char **, debug_baseclass **);
    166 static bfd_boolean parse_stab_struct_fields
    167   (void *, struct stab_handle *, const char **, debug_field **, bfd_boolean *);
    168 static bfd_boolean parse_stab_cpp_abbrev
    169   (void *, struct stab_handle *, const char **, debug_field *);
    170 static bfd_boolean parse_stab_one_struct_field
    171   (void *, struct stab_handle *, const char **, const char *,
    172    debug_field *, bfd_boolean *);
    173 static bfd_boolean parse_stab_members
    174   (void *, struct stab_handle *, const char *, const char **, const int *,
    175    debug_method **);
    176 static debug_type parse_stab_argtypes
    177   (void *, struct stab_handle *, debug_type, const char *, const char *,
    178    debug_type, const char *, bfd_boolean, bfd_boolean, const char **);
    179 static bfd_boolean parse_stab_tilde_field
    180   (void *, struct stab_handle *, const char **, const int *, debug_type *,
    181    bfd_boolean *);
    182 static debug_type parse_stab_array_type
    183   (void *, struct stab_handle *, const char **, bfd_boolean);
    184 static void push_bincl (struct stab_handle *, const char *, bfd_vma);
    185 static const char *pop_bincl (struct stab_handle *);
    186 static bfd_boolean find_excl (struct stab_handle *, const char *, bfd_vma);
    187 static bfd_boolean stab_record_variable
    188   (void *, struct stab_handle *, const char *, debug_type,
    189    enum debug_var_kind, bfd_vma);
    190 static bfd_boolean stab_emit_pending_vars (void *, struct stab_handle *);
    191 static debug_type *stab_find_slot (struct stab_handle *, const int *);
    192 static debug_type stab_find_type (void *, struct stab_handle *, const int *);
    193 static bfd_boolean stab_record_type
    194   (void *, struct stab_handle *, const int *, debug_type);
    195 static debug_type stab_xcoff_builtin_type
    196   (void *, struct stab_handle *, int);
    197 static debug_type stab_find_tagged_type
    198   (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
    199 static debug_type *stab_demangle_argtypes
    200   (void *, struct stab_handle *, const char *, bfd_boolean *, unsigned int);
    201 static debug_type *stab_demangle_v3_argtypes
    202   (void *, struct stab_handle *, const char *, bfd_boolean *);
    203 static debug_type *stab_demangle_v3_arglist
    204   (void *, struct stab_handle *, struct demangle_component *, bfd_boolean *);
    205 static debug_type stab_demangle_v3_arg
    206   (void *, struct stab_handle *, struct demangle_component *, debug_type,
    207    bfd_boolean *);
    208 
    209 /* Save a string in memory.  */
    210 
    211 static char *
    212 savestring (const char *start, int len)
    213 {
    214   char *ret;
    215 
    216   ret = (char *) xmalloc (len + 1);
    217   memcpy (ret, start, len);
    218   ret[len] = '\0';
    219   return ret;
    220 }
    221 
    222 /* Read a number from a string.  */
    223 
    224 static bfd_vma
    225 parse_number (const char **pp, bfd_boolean *poverflow)
    226 {
    227   unsigned long ul;
    228   const char *orig;
    229 
    230   if (poverflow != NULL)
    231     *poverflow = FALSE;
    232 
    233   orig = *pp;
    234 
    235   errno = 0;
    236   ul = strtoul (*pp, (char **) pp, 0);
    237   if (ul + 1 != 0 || errno == 0)
    238     {
    239       /* If bfd_vma is larger than unsigned long, and the number is
    240          meant to be negative, we have to make sure that we sign
    241          extend properly.  */
    242       if (*orig == '-')
    243 	return (bfd_vma) (bfd_signed_vma) (long) ul;
    244       return (bfd_vma) ul;
    245     }
    246 
    247   /* Note that even though strtoul overflowed, it should have set *pp
    248      to the end of the number, which is where we want it.  */
    249   if (sizeof (bfd_vma) > sizeof (unsigned long))
    250     {
    251       const char *p;
    252       bfd_boolean neg;
    253       int base;
    254       bfd_vma over, lastdig;
    255       bfd_boolean overflow;
    256       bfd_vma v;
    257 
    258       /* Our own version of strtoul, for a bfd_vma.  */
    259       p = orig;
    260 
    261       neg = FALSE;
    262       if (*p == '+')
    263 	++p;
    264       else if (*p == '-')
    265 	{
    266 	  neg = TRUE;
    267 	  ++p;
    268 	}
    269 
    270       base = 10;
    271       if (*p == '0')
    272 	{
    273 	  if (p[1] == 'x' || p[1] == 'X')
    274 	    {
    275 	      base = 16;
    276 	      p += 2;
    277 	    }
    278 	  else
    279 	    {
    280 	      base = 8;
    281 	      ++p;
    282 	    }
    283 	}
    284 
    285       over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
    286       lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
    287 
    288       overflow = FALSE;
    289       v = 0;
    290       while (1)
    291 	{
    292 	  int d;
    293 
    294 	  d = *p++;
    295 	  if (ISDIGIT (d))
    296 	    d -= '0';
    297 	  else if (ISUPPER (d))
    298 	    d -= 'A';
    299 	  else if (ISLOWER (d))
    300 	    d -= 'a';
    301 	  else
    302 	    break;
    303 
    304 	  if (d >= base)
    305 	    break;
    306 
    307 	  if (v > over || (v == over && (bfd_vma) d > lastdig))
    308 	    {
    309 	      overflow = TRUE;
    310 	      break;
    311 	    }
    312 	}
    313 
    314       if (! overflow)
    315 	{
    316 	  if (neg)
    317 	    v = - v;
    318 	  return v;
    319 	}
    320     }
    321 
    322   /* If we get here, the number is too large to represent in a
    323      bfd_vma.  */
    324   if (poverflow != NULL)
    325     *poverflow = TRUE;
    326   else
    327     warn_stab (orig, _("numeric overflow"));
    328 
    329   return 0;
    330 }
    331 
    332 /* Give an error for a bad stab string.  */
    333 
    334 static void
    335 bad_stab (const char *p)
    336 {
    337   fprintf (stderr, _("Bad stab: %s\n"), p);
    338 }
    339 
    340 /* Warn about something in a stab string.  */
    341 
    342 static void
    343 warn_stab (const char *p, const char *err)
    344 {
    345   fprintf (stderr, _("Warning: %s: %s\n"), err, p);
    346 }
    347 
    348 /* Create a handle to parse stabs symbols with.  */
    349 
    350 void *
    351 start_stab (void *dhandle ATTRIBUTE_UNUSED, bfd *abfd, bfd_boolean sections,
    352 	    asymbol **syms, long symcount)
    353 {
    354   struct stab_handle *ret;
    355 
    356   ret = (struct stab_handle *) xmalloc (sizeof *ret);
    357   memset (ret, 0, sizeof *ret);
    358   ret->abfd = abfd;
    359   ret->sections = sections;
    360   ret->syms = syms;
    361   ret->symcount = symcount;
    362   ret->files = 1;
    363   ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
    364   ret->file_types[0] = NULL;
    365   ret->function_end = (bfd_vma) -1;
    366   return (void *) ret;
    367 }
    368 
    369 /* When we have processed all the stabs information, we need to go
    370    through and fill in all the undefined tags.  */
    371 
    372 bfd_boolean
    373 finish_stab (void *dhandle, void *handle)
    374 {
    375   struct stab_handle *info = (struct stab_handle *) handle;
    376   struct stab_tag *st;
    377 
    378   if (info->within_function)
    379     {
    380       if (! stab_emit_pending_vars (dhandle, info)
    381 	  || ! debug_end_function (dhandle, info->function_end))
    382 	return FALSE;
    383       info->within_function = FALSE;
    384       info->function_end = (bfd_vma) -1;
    385     }
    386 
    387   for (st = info->tags; st != NULL; st = st->next)
    388     {
    389       enum debug_type_kind kind;
    390 
    391       kind = st->kind;
    392       if (kind == DEBUG_KIND_ILLEGAL)
    393 	kind = DEBUG_KIND_STRUCT;
    394       st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
    395       if (st->slot == DEBUG_TYPE_NULL)
    396 	return FALSE;
    397     }
    398 
    399   return TRUE;
    400 }
    401 
    402 /* Handle a single stabs symbol.  */
    403 
    404 bfd_boolean
    405 parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value,
    406 	    const char *string)
    407 {
    408   struct stab_handle *info = (struct stab_handle *) handle;
    409 
    410   /* gcc will emit two N_SO strings per compilation unit, one for the
    411      directory name and one for the file name.  We just collect N_SO
    412      strings as we see them, and start the new compilation unit when
    413      we see a non N_SO symbol.  */
    414   if (info->so_string != NULL
    415       && (type != N_SO || *string == '\0' || value != info->so_value))
    416     {
    417       if (! debug_set_filename (dhandle, info->so_string))
    418 	return FALSE;
    419       info->main_filename = info->so_string;
    420 
    421       info->gcc_compiled = 0;
    422       info->n_opt_found = FALSE;
    423 
    424       /* Generally, for stabs in the symbol table, the N_LBRAC and
    425 	 N_RBRAC symbols are relative to the N_SO symbol value.  */
    426       if (! info->sections)
    427 	info->file_start_offset = info->so_value;
    428 
    429       /* We need to reset the mapping from type numbers to types.  We
    430 	 can't free the old mapping, because of the use of
    431 	 debug_make_indirect_type.  */
    432       info->files = 1;
    433       info->file_types = ((struct stab_types **)
    434 			  xmalloc (sizeof *info->file_types));
    435       info->file_types[0] = NULL;
    436 
    437       info->so_string = NULL;
    438 
    439       /* Now process whatever type we just got.  */
    440     }
    441 
    442   switch (type)
    443     {
    444     case N_FN:
    445     case N_FN_SEQ:
    446       break;
    447 
    448     case N_LBRAC:
    449       /* Ignore extra outermost context from SunPRO cc and acc.  */
    450       if (info->n_opt_found && desc == 1)
    451 	break;
    452 
    453       if (! info->within_function)
    454 	{
    455 	  fprintf (stderr, _("N_LBRAC not within function\n"));
    456 	  return FALSE;
    457 	}
    458 
    459       /* Start an inner lexical block.  */
    460       if (! debug_start_block (dhandle,
    461 			       (value
    462 				+ info->file_start_offset
    463 				+ info->function_start_offset)))
    464 	return FALSE;
    465 
    466       /* Emit any pending variable definitions.  */
    467       if (! stab_emit_pending_vars (dhandle, info))
    468 	return FALSE;
    469 
    470       ++info->block_depth;
    471       break;
    472 
    473     case N_RBRAC:
    474       /* Ignore extra outermost context from SunPRO cc and acc.  */
    475       if (info->n_opt_found && desc == 1)
    476 	break;
    477 
    478       /* We shouldn't have any pending variable definitions here, but,
    479          if we do, we probably need to emit them before closing the
    480          block.  */
    481       if (! stab_emit_pending_vars (dhandle, info))
    482 	return FALSE;
    483 
    484       /* End an inner lexical block.  */
    485       if (! debug_end_block (dhandle,
    486 			     (value
    487 			      + info->file_start_offset
    488 			      + info->function_start_offset)))
    489 	return FALSE;
    490 
    491       --info->block_depth;
    492       if (info->block_depth < 0)
    493 	{
    494 	  fprintf (stderr, _("Too many N_RBRACs\n"));
    495 	  return FALSE;
    496 	}
    497       break;
    498 
    499     case N_SO:
    500       /* This always ends a function.  */
    501       if (info->within_function)
    502 	{
    503 	  bfd_vma endval;
    504 
    505 	  endval = value;
    506 	  if (*string != '\0'
    507 	      && info->function_end != (bfd_vma) -1
    508 	      && info->function_end < endval)
    509 	    endval = info->function_end;
    510 	  if (! stab_emit_pending_vars (dhandle, info)
    511 	      || ! debug_end_function (dhandle, endval))
    512 	    return FALSE;
    513 	  info->within_function = FALSE;
    514 	  info->function_end = (bfd_vma) -1;
    515 	}
    516 
    517       /* An empty string is emitted by gcc at the end of a compilation
    518          unit.  */
    519       if (*string == '\0')
    520 	return TRUE;
    521 
    522       /* Just accumulate strings until we see a non N_SO symbol.  If
    523          the string starts with a directory separator or some other
    524 	 form of absolute path specification, we discard the previously
    525          accumulated strings.  */
    526       if (info->so_string == NULL)
    527 	info->so_string = xstrdup (string);
    528       else
    529 	{
    530 	  char *f;
    531 
    532 	  f = info->so_string;
    533 
    534 	  if (IS_ABSOLUTE_PATH (string))
    535 	    info->so_string = xstrdup (string);
    536 	  else
    537 	    info->so_string = concat (info->so_string, string,
    538 				      (const char *) NULL);
    539 	  free (f);
    540 	}
    541 
    542       info->so_value = value;
    543 
    544       break;
    545 
    546     case N_SOL:
    547       /* Start an include file.  */
    548       if (! debug_start_source (dhandle, string))
    549 	return FALSE;
    550       break;
    551 
    552     case N_BINCL:
    553       /* Start an include file which may be replaced.  */
    554       push_bincl (info, string, value);
    555       if (! debug_start_source (dhandle, string))
    556 	return FALSE;
    557       break;
    558 
    559     case N_EINCL:
    560       /* End an N_BINCL include.  */
    561       if (! debug_start_source (dhandle, pop_bincl (info)))
    562 	return FALSE;
    563       break;
    564 
    565     case N_EXCL:
    566       /* This is a duplicate of a header file named by N_BINCL which
    567          was eliminated by the linker.  */
    568       if (! find_excl (info, string, value))
    569 	return FALSE;
    570       break;
    571 
    572     case N_SLINE:
    573       if (! debug_record_line (dhandle, desc,
    574 			       value + (info->within_function
    575 					? info->function_start_offset : 0)))
    576 	return FALSE;
    577       break;
    578 
    579     case N_BCOMM:
    580       if (! debug_start_common_block (dhandle, string))
    581 	return FALSE;
    582       break;
    583 
    584     case N_ECOMM:
    585       if (! debug_end_common_block (dhandle, string))
    586 	return FALSE;
    587       break;
    588 
    589     case N_FUN:
    590       if (*string == '\0')
    591 	{
    592 	  if (info->within_function)
    593 	    {
    594 	      /* This always marks the end of a function; we don't
    595                  need to worry about info->function_end.  */
    596 	      if (info->sections)
    597 		value += info->function_start_offset;
    598 	      if (! stab_emit_pending_vars (dhandle, info)
    599 		  || ! debug_end_function (dhandle, value))
    600 		return FALSE;
    601 	      info->within_function = FALSE;
    602 	      info->function_end = (bfd_vma) -1;
    603 	    }
    604 	  break;
    605 	}
    606 
    607       /* A const static symbol in the .text section will have an N_FUN
    608          entry.  We need to use these to mark the end of the function,
    609          in case we are looking at gcc output before it was changed to
    610          always emit an empty N_FUN.  We can't call debug_end_function
    611          here, because it might be a local static symbol.  */
    612       if (info->within_function
    613 	  && (info->function_end == (bfd_vma) -1
    614 	      || value < info->function_end))
    615 	info->function_end = value;
    616 
    617       /* Fall through.  */
    618       /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
    619          symbols, and if it does not start with :S, gdb relocates the
    620          value to the start of the section.  gcc always seems to use
    621          :S, so we don't worry about this.  */
    622       /* Fall through.  */
    623     default:
    624       {
    625 	const char *colon;
    626 
    627 	colon = strchr (string, ':');
    628 	if (colon != NULL
    629 	    && (colon[1] == 'f' || colon[1] == 'F'))
    630 	  {
    631 	    if (info->within_function)
    632 	      {
    633 		bfd_vma endval;
    634 
    635 		endval = value;
    636 		if (info->function_end != (bfd_vma) -1
    637 		    && info->function_end < endval)
    638 		  endval = info->function_end;
    639 		if (! stab_emit_pending_vars (dhandle, info)
    640 		    || ! debug_end_function (dhandle, endval))
    641 		  return FALSE;
    642 		info->function_end = (bfd_vma) -1;
    643 	      }
    644 	    /* For stabs in sections, line numbers and block addresses
    645                are offsets from the start of the function.  */
    646 	    if (info->sections)
    647 	      info->function_start_offset = value;
    648 	    info->within_function = TRUE;
    649 	  }
    650 
    651 	if (! parse_stab_string (dhandle, info, type, desc, value, string))
    652 	  return FALSE;
    653       }
    654       break;
    655 
    656     case N_OPT:
    657       if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
    658 	info->gcc_compiled = 2;
    659       else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
    660 	info->gcc_compiled = 1;
    661       else
    662 	info->n_opt_found = TRUE;
    663       break;
    664 
    665     case N_OBJ:
    666     case N_ENDM:
    667     case N_MAIN:
    668     case N_WARNING:
    669       break;
    670     }
    671 
    672   return TRUE;
    673 }
    674 
    675 /* Parse the stabs string.  */
    676 
    677 static bfd_boolean
    678 parse_stab_string (void *dhandle, struct stab_handle *info, int stabtype,
    679 		   int desc ATTRIBUTE_UNUSED, bfd_vma value, const char *string)
    680 {
    681   const char *p;
    682   char *name;
    683   int type;
    684   debug_type dtype;
    685   bfd_boolean synonym;
    686   bfd_boolean self_crossref;
    687   debug_type *slot;
    688 
    689   p = strchr (string, ':');
    690   if (p == NULL)
    691     return TRUE;
    692 
    693   while (p[1] == ':')
    694     {
    695       p += 2;
    696       p = strchr (p, ':');
    697       if (p == NULL)
    698 	{
    699 	  bad_stab (string);
    700 	  return FALSE;
    701 	}
    702     }
    703 
    704   /* FIXME: Sometimes the special C++ names start with '.'.  */
    705   name = NULL;
    706   if (string[0] == '$')
    707     {
    708       switch (string[1])
    709 	{
    710 	case 't':
    711 	  name = "this";
    712 	  break;
    713 	case 'v':
    714 	  /* Was: name = "vptr"; */
    715 	  break;
    716 	case 'e':
    717 	  name = "eh_throw";
    718 	  break;
    719 	case '_':
    720 	  /* This was an anonymous type that was never fixed up.  */
    721 	  break;
    722 	case 'X':
    723 	  /* SunPRO (3.0 at least) static variable encoding.  */
    724 	  break;
    725 	default:
    726 	  warn_stab (string, _("unknown C++ encoded name"));
    727 	  break;
    728 	}
    729     }
    730 
    731   if (name == NULL)
    732     {
    733       if (p == string || (string[0] == ' ' && p == string + 1))
    734 	name = NULL;
    735       else
    736 	name = savestring (string, p - string);
    737     }
    738 
    739   ++p;
    740   if (ISDIGIT (*p) || *p == '(' || *p == '-')
    741     type = 'l';
    742   else
    743     type = *p++;
    744 
    745   switch (type)
    746     {
    747     case 'c':
    748       /* c is a special case, not followed by a type-number.
    749 	 SYMBOL:c=iVALUE for an integer constant symbol.
    750 	 SYMBOL:c=rVALUE for a floating constant symbol.
    751 	 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
    752 	 e.g. "b:c=e6,0" for "const b = blob1"
    753 	 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
    754       if (*p != '=')
    755 	{
    756 	  bad_stab (string);
    757 	  return FALSE;
    758 	}
    759       ++p;
    760       switch (*p++)
    761 	{
    762 	case 'r':
    763 	  /* Floating point constant.  */
    764 	  if (! debug_record_float_const (dhandle, name, atof (p)))
    765 	    return FALSE;
    766 	  break;
    767 	case 'i':
    768 	  /* Integer constant.  */
    769 	  /* Defining integer constants this way is kind of silly,
    770 	     since 'e' constants allows the compiler to give not only
    771 	     the value, but the type as well.  C has at least int,
    772 	     long, unsigned int, and long long as constant types;
    773 	     other languages probably should have at least unsigned as
    774 	     well as signed constants.  */
    775 	  if (! debug_record_int_const (dhandle, name, atoi (p)))
    776 	    return FALSE;
    777 	  break;
    778 	case 'e':
    779 	  /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
    780 	     can be represented as integral.
    781 	     e.g. "b:c=e6,0" for "const b = blob1"
    782 	     (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
    783 	  dtype = parse_stab_type (dhandle, info, (const char *) NULL,
    784 				   &p, (debug_type **) NULL);
    785 	  if (dtype == DEBUG_TYPE_NULL)
    786 	    return FALSE;
    787 	  if (*p != ',')
    788 	    {
    789 	      bad_stab (string);
    790 	      return FALSE;
    791 	    }
    792 	  if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
    793 	    return FALSE;
    794 	  break;
    795 	default:
    796 	  bad_stab (string);
    797 	  return FALSE;
    798 	}
    799 
    800       break;
    801 
    802     case 'C':
    803       /* The name of a caught exception.  */
    804       dtype = parse_stab_type (dhandle, info, (const char *) NULL,
    805 			       &p, (debug_type **) NULL);
    806       if (dtype == DEBUG_TYPE_NULL)
    807 	return FALSE;
    808       if (! debug_record_label (dhandle, name, dtype, value))
    809 	return FALSE;
    810       break;
    811 
    812     case 'f':
    813     case 'F':
    814       /* A function definition.  */
    815       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    816 			       (debug_type **) NULL);
    817       if (dtype == DEBUG_TYPE_NULL)
    818 	return FALSE;
    819       if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
    820 	return FALSE;
    821 
    822       /* Sun acc puts declared types of arguments here.  We don't care
    823 	 about their actual types (FIXME -- we should remember the whole
    824 	 function prototype), but the list may define some new types
    825 	 that we have to remember, so we must scan it now.  */
    826       while (*p == ';')
    827 	{
    828 	  ++p;
    829 	  if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
    830 			       (debug_type **) NULL)
    831 	      == DEBUG_TYPE_NULL)
    832 	    return FALSE;
    833 	}
    834 
    835       break;
    836 
    837     case 'G':
    838       {
    839 	asymbol **ps;
    840 
    841 	/* A global symbol.  The value must be extracted from the
    842 	   symbol table.  */
    843 	dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    844 				 (debug_type **) NULL);
    845 	if (dtype == DEBUG_TYPE_NULL)
    846 	  return FALSE;
    847 	if (name != NULL)
    848 	  {
    849 	    char leading;
    850 	    long c;
    851 
    852 	    leading = bfd_get_symbol_leading_char (info->abfd);
    853 	    for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
    854 	      {
    855 		const char *n;
    856 
    857 		n = bfd_asymbol_name (*ps);
    858 		if (leading != '\0' && *n == leading)
    859 		  ++n;
    860 		if (*n == *name && strcmp (n, name) == 0)
    861 		  break;
    862 	      }
    863 
    864 	    if (c > 0)
    865 	      value = bfd_asymbol_value (*ps);
    866 	  }
    867 
    868 	if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
    869 				    value))
    870 	  return FALSE;
    871       }
    872       break;
    873 
    874       /* This case is faked by a conditional above, when there is no
    875 	 code letter in the dbx data.  Dbx data never actually
    876 	 contains 'l'.  */
    877     case 'l':
    878     case 's':
    879       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    880 			       (debug_type **) NULL);
    881       if (dtype == DEBUG_TYPE_NULL)
    882 	return FALSE;
    883       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
    884 				  value))
    885 	return FALSE;
    886       break;
    887 
    888     case 'p':
    889       /* A function parameter.  */
    890       if (*p != 'F')
    891 	dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    892 				 (debug_type **) NULL);
    893       else
    894 	{
    895 	/* pF is a two-letter code that means a function parameter in
    896 	   Fortran.  The type-number specifies the type of the return
    897 	   value.  Translate it into a pointer-to-function type.  */
    898 	  ++p;
    899 	  dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    900 				   (debug_type **) NULL);
    901 	  if (dtype != DEBUG_TYPE_NULL)
    902 	    {
    903 	      debug_type ftype;
    904 
    905 	      ftype = debug_make_function_type (dhandle, dtype,
    906 						(debug_type *) NULL, FALSE);
    907 	      dtype = debug_make_pointer_type (dhandle, ftype);
    908 	    }
    909 	}
    910       if (dtype == DEBUG_TYPE_NULL)
    911 	return FALSE;
    912       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
    913 				    value))
    914 	return FALSE;
    915 
    916       /* FIXME: At this point gdb considers rearranging the parameter
    917 	 address on a big endian machine if it is smaller than an int.
    918 	 We have no way to do that, since we don't really know much
    919 	 about the target.  */
    920       break;
    921 
    922     case 'P':
    923       if (stabtype == N_FUN)
    924 	{
    925 	  /* Prototype of a function referenced by this file.  */
    926 	  while (*p == ';')
    927 	    {
    928 	      ++p;
    929 	      if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
    930 				   (debug_type **) NULL)
    931 		  == DEBUG_TYPE_NULL)
    932 		return FALSE;
    933 	    }
    934 	  break;
    935 	}
    936       /* Fall through.  */
    937     case 'R':
    938       /* Parameter which is in a register.  */
    939       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    940 			       (debug_type **) NULL);
    941       if (dtype == DEBUG_TYPE_NULL)
    942 	return FALSE;
    943       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
    944 				    value))
    945 	return FALSE;
    946       break;
    947 
    948     case 'r':
    949       /* Register variable (either global or local).  */
    950       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    951 			       (debug_type **) NULL);
    952       if (dtype == DEBUG_TYPE_NULL)
    953 	return FALSE;
    954       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
    955 				  value))
    956 	return FALSE;
    957 
    958       /* FIXME: At this point gdb checks to combine pairs of 'p' and
    959 	 'r' stabs into a single 'P' stab.  */
    960       break;
    961 
    962     case 'S':
    963       /* Static symbol at top level of file.  */
    964       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
    965 			       (debug_type **) NULL);
    966       if (dtype == DEBUG_TYPE_NULL)
    967 	return FALSE;
    968       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
    969 				  value))
    970 	return FALSE;
    971       break;
    972 
    973     case 't':
    974       /* A typedef.  */
    975       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
    976       if (dtype == DEBUG_TYPE_NULL)
    977 	return FALSE;
    978       if (name == NULL)
    979 	{
    980 	  /* A nameless type.  Nothing to do.  */
    981 	  return TRUE;
    982 	}
    983 
    984       dtype = debug_name_type (dhandle, name, dtype);
    985       if (dtype == DEBUG_TYPE_NULL)
    986 	return FALSE;
    987 
    988       if (slot != NULL)
    989 	*slot = dtype;
    990 
    991       break;
    992 
    993     case 'T':
    994       /* Struct, union, or enum tag.  For GNU C++, this can be be followed
    995 	 by 't' which means we are typedef'ing it as well.  */
    996       if (*p != 't')
    997 	{
    998 	  synonym = FALSE;
    999 	  /* FIXME: gdb sets synonym to TRUE if the current language
   1000              is C++.  */
   1001 	}
   1002       else
   1003 	{
   1004 	  synonym = TRUE;
   1005 	  ++p;
   1006 	}
   1007 
   1008       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
   1009       if (dtype == DEBUG_TYPE_NULL)
   1010 	return FALSE;
   1011       if (name == NULL)
   1012 	return TRUE;
   1013 
   1014       /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
   1015          a cross reference to itself.  These are generated by some
   1016          versions of g++.  */
   1017       self_crossref = info->self_crossref;
   1018 
   1019       dtype = debug_tag_type (dhandle, name, dtype);
   1020       if (dtype == DEBUG_TYPE_NULL)
   1021 	return FALSE;
   1022       if (slot != NULL)
   1023 	*slot = dtype;
   1024 
   1025       /* See if we have a cross reference to this tag which we can now
   1026          fill in.  Avoid filling in a cross reference to ourselves,
   1027          because that would lead to circular debugging information.  */
   1028       if (! self_crossref)
   1029 	{
   1030 	  register struct stab_tag **pst;
   1031 
   1032 	  for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
   1033 	    {
   1034 	      if ((*pst)->name[0] == name[0]
   1035 		  && strcmp ((*pst)->name, name) == 0)
   1036 		{
   1037 		  (*pst)->slot = dtype;
   1038 		  *pst = (*pst)->next;
   1039 		  break;
   1040 		}
   1041 	    }
   1042 	}
   1043 
   1044       if (synonym)
   1045 	{
   1046 	  dtype = debug_name_type (dhandle, name, dtype);
   1047 	  if (dtype == DEBUG_TYPE_NULL)
   1048 	    return FALSE;
   1049 
   1050 	  if (slot != NULL)
   1051 	    *slot = dtype;
   1052 	}
   1053 
   1054       break;
   1055 
   1056     case 'V':
   1057       /* Static symbol of local scope */
   1058       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
   1059 			       (debug_type **) NULL);
   1060       if (dtype == DEBUG_TYPE_NULL)
   1061 	return FALSE;
   1062       /* FIXME: gdb checks os9k_stabs here.  */
   1063       if (! stab_record_variable (dhandle, info, name, dtype,
   1064 				  DEBUG_LOCAL_STATIC, value))
   1065 	return FALSE;
   1066       break;
   1067 
   1068     case 'v':
   1069       /* Reference parameter.  */
   1070       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
   1071 			       (debug_type **) NULL);
   1072       if (dtype == DEBUG_TYPE_NULL)
   1073 	return FALSE;
   1074       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
   1075 				    value))
   1076 	return FALSE;
   1077       break;
   1078 
   1079     case 'a':
   1080       /* Reference parameter which is in a register.  */
   1081       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
   1082 			       (debug_type **) NULL);
   1083       if (dtype == DEBUG_TYPE_NULL)
   1084 	return FALSE;
   1085       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
   1086 				    value))
   1087 	return FALSE;
   1088       break;
   1089 
   1090     case 'X':
   1091       /* This is used by Sun FORTRAN for "function result value".
   1092 	 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
   1093 	 that Pascal uses it too, but when I tried it Pascal used
   1094 	 "x:3" (local symbol) instead.  */
   1095       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
   1096 			       (debug_type **) NULL);
   1097       if (dtype == DEBUG_TYPE_NULL)
   1098 	return FALSE;
   1099       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
   1100 				  value))
   1101 	return FALSE;
   1102       break;
   1103 
   1104     case 'Y':
   1105       /* SUNPro C++ Namespace =Yn0.  */
   1106       /* Skip the namespace mapping, as it is not used now.  */
   1107       if (*(++p) == 'n' && *(++p) == '0')
   1108 	{
   1109 	  /* =Yn0name; */
   1110 	  while (*p != ';')
   1111 	    ++p;
   1112 	  ++p;
   1113 	  return TRUE;
   1114 	}
   1115       /* TODO SUNPro C++ support:
   1116          Support default arguments after F,P parameters
   1117          Ya = Anonymous unions
   1118          YM,YD = Pointers to class members
   1119          YT,YI = Templates
   1120          YR = Run-time type information (RTTI)  */
   1121 
   1122       /* Fall through.  */
   1123 
   1124     default:
   1125       bad_stab (string);
   1126       return FALSE;
   1127     }
   1128 
   1129   /* FIXME: gdb converts structure values to structure pointers in a
   1130      couple of cases, depending upon the target.  */
   1131 
   1132   return TRUE;
   1133 }
   1134 
   1135 /* Parse a stabs type.  The typename argument is non-NULL if this is a
   1136    typedef or a tag definition.  The pp argument points to the stab
   1137    string, and is updated.  The slotp argument points to a place to
   1138    store the slot used if the type is being defined.  */
   1139 
   1140 static debug_type
   1141 parse_stab_type (void *dhandle, struct stab_handle *info, const char *type_name, const char **pp, debug_type **slotp)
   1142 {
   1143   const char *orig;
   1144   int typenums[2];
   1145   int size;
   1146   bfd_boolean stringp;
   1147   int descriptor;
   1148   debug_type dtype;
   1149 
   1150   if (slotp != NULL)
   1151     *slotp = NULL;
   1152 
   1153   orig = *pp;
   1154 
   1155   size = -1;
   1156   stringp = FALSE;
   1157 
   1158   info->self_crossref = FALSE;
   1159 
   1160   /* Read type number if present.  The type number may be omitted.
   1161      for instance in a two-dimensional array declared with type
   1162      "ar1;1;10;ar1;1;10;4".  */
   1163   if (! ISDIGIT (**pp) && **pp != '(' && **pp != '-')
   1164     {
   1165       /* 'typenums=' not present, type is anonymous.  Read and return
   1166 	 the definition, but don't put it in the type vector.  */
   1167       typenums[0] = typenums[1] = -1;
   1168     }
   1169   else
   1170     {
   1171       if (! parse_stab_type_number (pp, typenums))
   1172 	return DEBUG_TYPE_NULL;
   1173 
   1174       if (**pp != '=')
   1175 	/* Type is not being defined here.  Either it already
   1176 	   exists, or this is a forward reference to it.  */
   1177 	return stab_find_type (dhandle, info, typenums);
   1178 
   1179       /* Only set the slot if the type is being defined.  This means
   1180          that the mapping from type numbers to types will only record
   1181          the name of the typedef which defines a type.  If we don't do
   1182          this, then something like
   1183 	     typedef int foo;
   1184 	     int i;
   1185 	 will record that i is of type foo.  Unfortunately, stabs
   1186 	 information is ambiguous about variable types.  For this code,
   1187 	     typedef int foo;
   1188 	     int i;
   1189 	     foo j;
   1190 	 the stabs information records both i and j as having the same
   1191 	 type.  This could be fixed by patching the compiler.  */
   1192       if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
   1193 	*slotp = stab_find_slot (info, typenums);
   1194 
   1195       /* Type is being defined here.  */
   1196       /* Skip the '='.  */
   1197       ++*pp;
   1198 
   1199       while (**pp == '@')
   1200 	{
   1201 	  const char *p = *pp + 1;
   1202 	  const char *attr;
   1203 
   1204 	  if (ISDIGIT (*p) || *p == '(' || *p == '-')
   1205 	    /* Member type.  */
   1206 	    break;
   1207 
   1208 	  /* Type attributes.  */
   1209 	  attr = p;
   1210 
   1211 	  for (; *p != ';'; ++p)
   1212 	    {
   1213 	      if (*p == '\0')
   1214 		{
   1215 		  bad_stab (orig);
   1216 		  return DEBUG_TYPE_NULL;
   1217 		}
   1218 	    }
   1219 	  *pp = p + 1;
   1220 
   1221 	  switch (*attr)
   1222 	    {
   1223 	    case 's':
   1224 	      size = atoi (attr + 1);
   1225 	      size /= 8;  /* Size is in bits.  We store it in bytes.  */
   1226 	      if (size <= 0)
   1227 		size = -1;
   1228 	      break;
   1229 
   1230 	    case 'S':
   1231 	      stringp = TRUE;
   1232 	      break;
   1233 
   1234 	    default:
   1235 	      /* Ignore unrecognized type attributes, so future
   1236 		 compilers can invent new ones.  */
   1237 	      break;
   1238 	    }
   1239 	}
   1240     }
   1241 
   1242   descriptor = **pp;
   1243   ++*pp;
   1244 
   1245   switch (descriptor)
   1246     {
   1247     case 'x':
   1248       {
   1249 	enum debug_type_kind code;
   1250 	const char *q1, *q2, *p;
   1251 
   1252 	/* A cross reference to another type.  */
   1253 	switch (**pp)
   1254 	  {
   1255 	  case 's':
   1256 	    code = DEBUG_KIND_STRUCT;
   1257 	    break;
   1258 	  case 'u':
   1259 	    code = DEBUG_KIND_UNION;
   1260 	    break;
   1261 	  case 'e':
   1262 	    code = DEBUG_KIND_ENUM;
   1263 	    break;
   1264 	  default:
   1265 	    /* Complain and keep going, so compilers can invent new
   1266 	       cross-reference types.  */
   1267 	    warn_stab (orig, _("unrecognized cross reference type"));
   1268 	    code = DEBUG_KIND_STRUCT;
   1269 	    break;
   1270 	  }
   1271 	++*pp;
   1272 
   1273 	q1 = strchr (*pp, '<');
   1274 	p = strchr (*pp, ':');
   1275 	if (p == NULL)
   1276 	  {
   1277 	    bad_stab (orig);
   1278 	    return DEBUG_TYPE_NULL;
   1279 	  }
   1280 	if (q1 != NULL && p > q1 && p[1] == ':')
   1281 	  {
   1282 	    int nest = 0;
   1283 
   1284 	    for (q2 = q1; *q2 != '\0'; ++q2)
   1285 	      {
   1286 		if (*q2 == '<')
   1287 		  ++nest;
   1288 		else if (*q2 == '>')
   1289 		  --nest;
   1290 		else if (*q2 == ':' && nest == 0)
   1291 		  break;
   1292 	      }
   1293 	    p = q2;
   1294 	    if (*p != ':')
   1295 	      {
   1296 		bad_stab (orig);
   1297 		return DEBUG_TYPE_NULL;
   1298 	      }
   1299 	  }
   1300 
   1301 	/* Some versions of g++ can emit stabs like
   1302 	       fleep:T20=xsfleep:
   1303 	   which define structures in terms of themselves.  We need to
   1304 	   tell the caller to avoid building a circular structure.  */
   1305 	if (type_name != NULL
   1306 	    && strncmp (type_name, *pp, p - *pp) == 0
   1307 	    && type_name[p - *pp] == '\0')
   1308 	  info->self_crossref = TRUE;
   1309 
   1310 	dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
   1311 
   1312 	*pp = p + 1;
   1313       }
   1314       break;
   1315 
   1316     case '-':
   1317     case '0':
   1318     case '1':
   1319     case '2':
   1320     case '3':
   1321     case '4':
   1322     case '5':
   1323     case '6':
   1324     case '7':
   1325     case '8':
   1326     case '9':
   1327     case '(':
   1328       {
   1329 	const char *hold;
   1330 	int xtypenums[2];
   1331 
   1332 	/* This type is defined as another type.  */
   1333 	(*pp)--;
   1334 	hold = *pp;
   1335 
   1336 	/* Peek ahead at the number to detect void.  */
   1337 	if (! parse_stab_type_number (pp, xtypenums))
   1338 	  return DEBUG_TYPE_NULL;
   1339 
   1340 	if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
   1341 	  {
   1342 	    /* This type is being defined as itself, which means that
   1343                it is void.  */
   1344 	    dtype = debug_make_void_type (dhandle);
   1345 	  }
   1346 	else
   1347 	  {
   1348 	    *pp = hold;
   1349 
   1350 	    /* Go back to the number and have parse_stab_type get it.
   1351 	       This means that we can deal with something like
   1352 	       t(1,2)=(3,4)=... which the Lucid compiler uses.  */
   1353 	    dtype = parse_stab_type (dhandle, info, (const char *) NULL,
   1354 				     pp, (debug_type **) NULL);
   1355 	    if (dtype == DEBUG_TYPE_NULL)
   1356 	      return DEBUG_TYPE_NULL;
   1357 	  }
   1358 
   1359 	if (typenums[0] != -1)
   1360 	  {
   1361 	    if (! stab_record_type (dhandle, info, typenums, dtype))
   1362 	      return DEBUG_TYPE_NULL;
   1363 	  }
   1364 
   1365 	break;
   1366       }
   1367 
   1368     case '*':
   1369       dtype = debug_make_pointer_type (dhandle,
   1370 				       parse_stab_type (dhandle, info,
   1371 							(const char *) NULL,
   1372 							pp,
   1373 							(debug_type **) NULL));
   1374       break;
   1375 
   1376     case '&':
   1377       /* Reference to another type.  */
   1378       dtype = (debug_make_reference_type
   1379 	       (dhandle,
   1380 		parse_stab_type (dhandle, info, (const char *) NULL, pp,
   1381 				 (debug_type **) NULL)));
   1382       break;
   1383 
   1384     case 'f':
   1385       /* Function returning another type.  */
   1386       /* FIXME: gdb checks os9k_stabs here.  */
   1387       dtype = (debug_make_function_type
   1388 	       (dhandle,
   1389 		parse_stab_type (dhandle, info, (const char *) NULL, pp,
   1390 				 (debug_type **) NULL),
   1391 		(debug_type *) NULL, FALSE));
   1392       break;
   1393 
   1394     case 'k':
   1395       /* Const qualifier on some type (Sun).  */
   1396       /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
   1397       dtype = debug_make_const_type (dhandle,
   1398 				     parse_stab_type (dhandle, info,
   1399 						      (const char *) NULL,
   1400 						      pp,
   1401 						      (debug_type **) NULL));
   1402       break;
   1403 
   1404     case 'B':
   1405       /* Volatile qual on some type (Sun).  */
   1406       /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
   1407       dtype = (debug_make_volatile_type
   1408 	       (dhandle,
   1409 		parse_stab_type (dhandle, info, (const char *) NULL, pp,
   1410 				 (debug_type **) NULL)));
   1411       break;
   1412 
   1413     case '@':
   1414       /* Offset (class & variable) type.  This is used for a pointer
   1415          relative to an object.  */
   1416       {
   1417 	debug_type domain;
   1418 	debug_type memtype;
   1419 
   1420 	/* Member type.  */
   1421 
   1422 	domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   1423 				  (debug_type **) NULL);
   1424 	if (domain == DEBUG_TYPE_NULL)
   1425 	  return DEBUG_TYPE_NULL;
   1426 
   1427 	if (**pp != ',')
   1428 	  {
   1429 	    bad_stab (orig);
   1430 	    return DEBUG_TYPE_NULL;
   1431 	  }
   1432 	++*pp;
   1433 
   1434 	memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   1435 				   (debug_type **) NULL);
   1436 	if (memtype == DEBUG_TYPE_NULL)
   1437 	  return DEBUG_TYPE_NULL;
   1438 
   1439 	dtype = debug_make_offset_type (dhandle, domain, memtype);
   1440       }
   1441       break;
   1442 
   1443     case '#':
   1444       /* Method (class & fn) type.  */
   1445       if (**pp == '#')
   1446 	{
   1447 	  debug_type return_type;
   1448 
   1449 	  ++*pp;
   1450 	  return_type = parse_stab_type (dhandle, info, (const char *) NULL,
   1451 					 pp, (debug_type **) NULL);
   1452 	  if (return_type == DEBUG_TYPE_NULL)
   1453 	    return DEBUG_TYPE_NULL;
   1454 	  if (**pp != ';')
   1455 	    {
   1456 	      bad_stab (orig);
   1457 	      return DEBUG_TYPE_NULL;
   1458 	    }
   1459 	  ++*pp;
   1460 	  dtype = debug_make_method_type (dhandle, return_type,
   1461 					  DEBUG_TYPE_NULL,
   1462 					  (debug_type *) NULL, FALSE);
   1463 	}
   1464       else
   1465 	{
   1466 	  debug_type domain;
   1467 	  debug_type return_type;
   1468 	  debug_type *args;
   1469 	  unsigned int n;
   1470 	  unsigned int alloc;
   1471 	  bfd_boolean varargs;
   1472 
   1473 	  domain = parse_stab_type (dhandle, info, (const char *) NULL,
   1474 				    pp, (debug_type **) NULL);
   1475 	  if (domain == DEBUG_TYPE_NULL)
   1476 	    return DEBUG_TYPE_NULL;
   1477 
   1478 	  if (**pp != ',')
   1479 	    {
   1480 	      bad_stab (orig);
   1481 	      return DEBUG_TYPE_NULL;
   1482 	    }
   1483 	  ++*pp;
   1484 
   1485 	  return_type = parse_stab_type (dhandle, info, (const char *) NULL,
   1486 					 pp, (debug_type **) NULL);
   1487 	  if (return_type == DEBUG_TYPE_NULL)
   1488 	    return DEBUG_TYPE_NULL;
   1489 
   1490 	  alloc = 10;
   1491 	  args = (debug_type *) xmalloc (alloc * sizeof *args);
   1492 	  n = 0;
   1493 	  while (**pp != ';')
   1494 	    {
   1495 	      if (**pp != ',')
   1496 		{
   1497 		  bad_stab (orig);
   1498 		  return DEBUG_TYPE_NULL;
   1499 		}
   1500 	      ++*pp;
   1501 
   1502 	      if (n + 1 >= alloc)
   1503 		{
   1504 		  alloc += 10;
   1505 		  args = ((debug_type *)
   1506 			  xrealloc (args, alloc * sizeof *args));
   1507 		}
   1508 
   1509 	      args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
   1510 					 pp, (debug_type **) NULL);
   1511 	      if (args[n] == DEBUG_TYPE_NULL)
   1512 		return DEBUG_TYPE_NULL;
   1513 	      ++n;
   1514 	    }
   1515 	  ++*pp;
   1516 
   1517 	  /* If the last type is not void, then this function takes a
   1518 	     variable number of arguments.  Otherwise, we must strip
   1519 	     the void type.  */
   1520 	  if (n == 0
   1521 	      || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
   1522 	    varargs = TRUE;
   1523 	  else
   1524 	    {
   1525 	      --n;
   1526 	      varargs = FALSE;
   1527 	    }
   1528 
   1529 	  args[n] = DEBUG_TYPE_NULL;
   1530 
   1531 	  dtype = debug_make_method_type (dhandle, return_type, domain, args,
   1532 					  varargs);
   1533 	}
   1534       break;
   1535 
   1536     case 'r':
   1537       /* Range type.  */
   1538       dtype = parse_stab_range_type (dhandle, info, type_name, pp, typenums);
   1539       break;
   1540 
   1541     case 'b':
   1542       /* FIXME: gdb checks os9k_stabs here.  */
   1543       /* Sun ACC builtin int type.  */
   1544       dtype = parse_stab_sun_builtin_type (dhandle, pp);
   1545       break;
   1546 
   1547     case 'R':
   1548       /* Sun ACC builtin float type.  */
   1549       dtype = parse_stab_sun_floating_type (dhandle, pp);
   1550       break;
   1551 
   1552     case 'e':
   1553       /* Enumeration type.  */
   1554       dtype = parse_stab_enum_type (dhandle, pp);
   1555       break;
   1556 
   1557     case 's':
   1558     case 'u':
   1559       /* Struct or union type.  */
   1560       dtype = parse_stab_struct_type (dhandle, info, type_name, pp,
   1561 				      descriptor == 's', typenums);
   1562       break;
   1563 
   1564     case 'a':
   1565       /* Array type.  */
   1566       if (**pp != 'r')
   1567 	{
   1568 	  bad_stab (orig);
   1569 	  return DEBUG_TYPE_NULL;
   1570 	}
   1571       ++*pp;
   1572 
   1573       dtype = parse_stab_array_type (dhandle, info, pp, stringp);
   1574       break;
   1575 
   1576     case 'S':
   1577       dtype = debug_make_set_type (dhandle,
   1578 				   parse_stab_type (dhandle, info,
   1579 						    (const char *) NULL,
   1580 						    pp,
   1581 						    (debug_type **) NULL),
   1582 				   stringp);
   1583       break;
   1584 
   1585     default:
   1586       bad_stab (orig);
   1587       return DEBUG_TYPE_NULL;
   1588     }
   1589 
   1590   if (dtype == DEBUG_TYPE_NULL)
   1591     return DEBUG_TYPE_NULL;
   1592 
   1593   if (typenums[0] != -1)
   1594     {
   1595       if (! stab_record_type (dhandle, info, typenums, dtype))
   1596 	return DEBUG_TYPE_NULL;
   1597     }
   1598 
   1599   if (size != -1)
   1600     {
   1601       if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
   1602 	return DEBUG_TYPE_NULL;
   1603     }
   1604 
   1605   return dtype;
   1606 }
   1607 
   1608 /* Read a number by which a type is referred to in dbx data, or
   1609    perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
   1610    single number N is equivalent to (0,N).  Return the two numbers by
   1611    storing them in the vector TYPENUMS.  */
   1612 
   1613 static bfd_boolean
   1614 parse_stab_type_number (const char **pp, int *typenums)
   1615 {
   1616   const char *orig;
   1617 
   1618   orig = *pp;
   1619 
   1620   if (**pp != '(')
   1621     {
   1622       typenums[0] = 0;
   1623       typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
   1624     }
   1625   else
   1626     {
   1627       ++*pp;
   1628       typenums[0] = (int) parse_number (pp, (bfd_boolean *) NULL);
   1629       if (**pp != ',')
   1630 	{
   1631 	  bad_stab (orig);
   1632 	  return FALSE;
   1633 	}
   1634       ++*pp;
   1635       typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
   1636       if (**pp != ')')
   1637 	{
   1638 	  bad_stab (orig);
   1639 	  return FALSE;
   1640 	}
   1641       ++*pp;
   1642     }
   1643 
   1644   return TRUE;
   1645 }
   1646 
   1647 /* Parse a range type.  */
   1648 
   1649 static debug_type
   1650 parse_stab_range_type (void *dhandle, struct stab_handle *info, const char *type_name, const char **pp, const int *typenums)
   1651 {
   1652   const char *orig;
   1653   int rangenums[2];
   1654   bfd_boolean self_subrange;
   1655   debug_type index_type;
   1656   const char *s2, *s3;
   1657   bfd_signed_vma n2, n3;
   1658   bfd_boolean ov2, ov3;
   1659 
   1660   orig = *pp;
   1661 
   1662   index_type = DEBUG_TYPE_NULL;
   1663 
   1664   /* First comes a type we are a subrange of.
   1665      In C it is usually 0, 1 or the type being defined.  */
   1666   if (! parse_stab_type_number (pp, rangenums))
   1667     return DEBUG_TYPE_NULL;
   1668 
   1669   self_subrange = (rangenums[0] == typenums[0]
   1670 		   && rangenums[1] == typenums[1]);
   1671 
   1672   if (**pp == '=')
   1673     {
   1674       *pp = orig;
   1675       index_type = parse_stab_type (dhandle, info, (const char *) NULL,
   1676 				    pp, (debug_type **) NULL);
   1677       if (index_type == DEBUG_TYPE_NULL)
   1678 	return DEBUG_TYPE_NULL;
   1679     }
   1680 
   1681   if (**pp == ';')
   1682     ++*pp;
   1683 
   1684   /* The remaining two operands are usually lower and upper bounds of
   1685      the range.  But in some special cases they mean something else.  */
   1686   s2 = *pp;
   1687   n2 = parse_number (pp, &ov2);
   1688   if (**pp != ';')
   1689     {
   1690       bad_stab (orig);
   1691       return DEBUG_TYPE_NULL;
   1692     }
   1693   ++*pp;
   1694 
   1695   s3 = *pp;
   1696   n3 = parse_number (pp, &ov3);
   1697   if (**pp != ';')
   1698     {
   1699       bad_stab (orig);
   1700       return DEBUG_TYPE_NULL;
   1701     }
   1702   ++*pp;
   1703 
   1704   if (ov2 || ov3)
   1705     {
   1706       /* gcc will emit range stabs for long long types.  Handle this
   1707          as a special case.  FIXME: This needs to be more general.  */
   1708 #define LLLOW   "01000000000000000000000;"
   1709 #define LLHIGH   "0777777777777777777777;"
   1710 #define ULLHIGH "01777777777777777777777;"
   1711       if (index_type == DEBUG_TYPE_NULL)
   1712 	{
   1713 	  if (CONST_STRNEQ (s2, LLLOW)
   1714 	      && CONST_STRNEQ (s3, LLHIGH))
   1715 	    return debug_make_int_type (dhandle, 8, FALSE);
   1716 	  if (! ov2
   1717 	      && n2 == 0
   1718 	      && CONST_STRNEQ (s3, ULLHIGH))
   1719 	    return debug_make_int_type (dhandle, 8, TRUE);
   1720 	}
   1721 
   1722       warn_stab (orig, _("numeric overflow"));
   1723     }
   1724 
   1725   if (index_type == DEBUG_TYPE_NULL)
   1726     {
   1727       /* A type defined as a subrange of itself, with both bounds 0,
   1728          is void.  */
   1729       if (self_subrange && n2 == 0 && n3 == 0)
   1730 	return debug_make_void_type (dhandle);
   1731 
   1732       /* A type defined as a subrange of itself, with n2 positive and
   1733 	 n3 zero, is a complex type, and n2 is the number of bytes.  */
   1734       if (self_subrange && n3 == 0 && n2 > 0)
   1735 	return debug_make_complex_type (dhandle, n2);
   1736 
   1737       /* If n3 is zero and n2 is positive, this is a floating point
   1738          type, and n2 is the number of bytes.  */
   1739       if (n3 == 0 && n2 > 0)
   1740 	return debug_make_float_type (dhandle, n2);
   1741 
   1742       /* If the upper bound is -1, this is an unsigned int.  */
   1743       if (n2 == 0 && n3 == -1)
   1744 	{
   1745 	  /* When gcc is used with -gstabs, but not -gstabs+, it will emit
   1746 	         long long int:t6=r1;0;-1;
   1747 		 long long unsigned int:t7=r1;0;-1;
   1748 	     We hack here to handle this reasonably.  */
   1749 	  if (type_name != NULL)
   1750 	    {
   1751 	      if (strcmp (type_name, "long long int") == 0)
   1752 		return debug_make_int_type (dhandle, 8, FALSE);
   1753 	      else if (strcmp (type_name, "long long unsigned int") == 0)
   1754 		return debug_make_int_type (dhandle, 8, TRUE);
   1755 	    }
   1756 	  /* FIXME: The size here really depends upon the target.  */
   1757 	  return debug_make_int_type (dhandle, 4, TRUE);
   1758 	}
   1759 
   1760       /* A range of 0 to 127 is char.  */
   1761       if (self_subrange && n2 == 0 && n3 == 127)
   1762 	return debug_make_int_type (dhandle, 1, FALSE);
   1763 
   1764       /* FIXME: gdb checks for the language CHILL here.  */
   1765 
   1766       if (n2 == 0)
   1767 	{
   1768 	  if (n3 < 0)
   1769 	    return debug_make_int_type (dhandle, - n3, TRUE);
   1770 	  else if (n3 == 0xff)
   1771 	    return debug_make_int_type (dhandle, 1, TRUE);
   1772 	  else if (n3 == 0xffff)
   1773 	    return debug_make_int_type (dhandle, 2, TRUE);
   1774 	  else if (n3 == (bfd_signed_vma) 0xffffffff)
   1775 	    return debug_make_int_type (dhandle, 4, TRUE);
   1776 #ifdef BFD64
   1777 	  else if (n3 == (bfd_signed_vma) 0xffffffffffffffffLL)
   1778 	    return debug_make_int_type (dhandle, 8, TRUE);
   1779 #endif
   1780 	}
   1781       else if (n3 == 0
   1782 	       && n2 < 0
   1783 	       && (self_subrange || n2 == -8))
   1784 	return debug_make_int_type (dhandle, - n2, TRUE);
   1785       else if (n2 == - n3 - 1 || n2 == n3 + 1)
   1786 	{
   1787 	  if (n3 == 0x7f)
   1788 	    return debug_make_int_type (dhandle, 1, FALSE);
   1789 	  else if (n3 == 0x7fff)
   1790 	    return debug_make_int_type (dhandle, 2, FALSE);
   1791 	  else if (n3 == 0x7fffffff)
   1792 	    return debug_make_int_type (dhandle, 4, FALSE);
   1793 #ifdef BFD64
   1794 	  else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
   1795 	    return debug_make_int_type (dhandle, 8, FALSE);
   1796 #endif
   1797 	}
   1798     }
   1799 
   1800   /* At this point I don't have the faintest idea how to deal with a
   1801      self_subrange type; I'm going to assume that this is used as an
   1802      idiom, and that all of them are special cases.  So . . .  */
   1803   if (self_subrange)
   1804     {
   1805       bad_stab (orig);
   1806       return DEBUG_TYPE_NULL;
   1807     }
   1808 
   1809   index_type = stab_find_type (dhandle, info, rangenums);
   1810   if (index_type == DEBUG_TYPE_NULL)
   1811     {
   1812       /* Does this actually ever happen?  Is that why we are worrying
   1813          about dealing with it rather than just calling error_type?  */
   1814       warn_stab (orig, _("missing index type"));
   1815       index_type = debug_make_int_type (dhandle, 4, FALSE);
   1816     }
   1817 
   1818   return debug_make_range_type (dhandle, index_type, n2, n3);
   1819 }
   1820 
   1821 /* Sun's ACC uses a somewhat saner method for specifying the builtin
   1822    typedefs in every file (for int, long, etc):
   1823 
   1824 	type = b <signed> <width>; <offset>; <nbits>
   1825 	signed = u or s.  Possible c in addition to u or s (for char?).
   1826 	offset = offset from high order bit to start bit of type.
   1827 	width is # bytes in object of this type, nbits is # bits in type.
   1828 
   1829    The width/offset stuff appears to be for small objects stored in
   1830    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
   1831    FIXME.  */
   1832 
   1833 static debug_type
   1834 parse_stab_sun_builtin_type (void *dhandle, const char **pp)
   1835 {
   1836   const char *orig;
   1837   bfd_boolean unsignedp;
   1838   bfd_vma bits;
   1839 
   1840   orig = *pp;
   1841 
   1842   switch (**pp)
   1843     {
   1844     case 's':
   1845       unsignedp = FALSE;
   1846       break;
   1847     case 'u':
   1848       unsignedp = TRUE;
   1849       break;
   1850     default:
   1851       bad_stab (orig);
   1852       return DEBUG_TYPE_NULL;
   1853     }
   1854   ++*pp;
   1855 
   1856   /* OpenSolaris source code indicates that one of "cbv" characters
   1857      can come next and specify the intrinsic 'iformat' encoding.
   1858      'c' is character encoding, 'b' is boolean encoding, and 'v' is
   1859      varargs encoding.  This field can be safely ignored because
   1860      the type of the field is determined from the bitwidth extracted
   1861      below.  */
   1862   if (**pp == 'c' || **pp == 'b' || **pp == 'v')
   1863     ++*pp;
   1864 
   1865   /* The first number appears to be the number of bytes occupied
   1866      by this type, except that unsigned short is 4 instead of 2.
   1867      Since this information is redundant with the third number,
   1868      we will ignore it.  */
   1869   (void) parse_number (pp, (bfd_boolean *) NULL);
   1870   if (**pp != ';')
   1871     {
   1872       bad_stab (orig);
   1873       return DEBUG_TYPE_NULL;
   1874     }
   1875   ++*pp;
   1876 
   1877   /* The second number is always 0, so ignore it too.  */
   1878   (void) parse_number (pp, (bfd_boolean *) NULL);
   1879   if (**pp != ';')
   1880     {
   1881       bad_stab (orig);
   1882       return DEBUG_TYPE_NULL;
   1883     }
   1884   ++*pp;
   1885 
   1886   /* The third number is the number of bits for this type.  */
   1887   bits = parse_number (pp, (bfd_boolean *) NULL);
   1888 
   1889   /* The type *should* end with a semicolon.  If it are embedded
   1890      in a larger type the semicolon may be the only way to know where
   1891      the type ends.  If this type is at the end of the stabstring we
   1892      can deal with the omitted semicolon (but we don't have to like
   1893      it).  Don't bother to complain(), Sun's compiler omits the semicolon
   1894      for "void".  */
   1895   if (**pp == ';')
   1896     ++*pp;
   1897 
   1898   if (bits == 0)
   1899     return debug_make_void_type (dhandle);
   1900 
   1901   return debug_make_int_type (dhandle, bits / 8, unsignedp);
   1902 }
   1903 
   1904 /* Parse a builtin floating type generated by the Sun compiler.  */
   1905 
   1906 static debug_type
   1907 parse_stab_sun_floating_type (void *dhandle, const char **pp)
   1908 {
   1909   const char *orig;
   1910   bfd_vma details;
   1911   bfd_vma bytes;
   1912 
   1913   orig = *pp;
   1914 
   1915   /* The first number has more details about the type, for example
   1916      FN_COMPLEX.  */
   1917   details = parse_number (pp, (bfd_boolean *) NULL);
   1918   if (**pp != ';')
   1919     {
   1920       bad_stab (orig);
   1921       return DEBUG_TYPE_NULL;
   1922     }
   1923 
   1924   /* The second number is the number of bytes occupied by this type */
   1925   bytes = parse_number (pp, (bfd_boolean *) NULL);
   1926   if (**pp != ';')
   1927     {
   1928       bad_stab (orig);
   1929       return DEBUG_TYPE_NULL;
   1930     }
   1931 
   1932   if (details == NF_COMPLEX
   1933       || details == NF_COMPLEX16
   1934       || details == NF_COMPLEX32)
   1935     return debug_make_complex_type (dhandle, bytes);
   1936 
   1937   return debug_make_float_type (dhandle, bytes);
   1938 }
   1939 
   1940 /* Handle an enum type.  */
   1941 
   1942 static debug_type
   1943 parse_stab_enum_type (void *dhandle, const char **pp)
   1944 {
   1945   const char *orig;
   1946   const char **names;
   1947   bfd_signed_vma *values;
   1948   unsigned int n;
   1949   unsigned int alloc;
   1950 
   1951   orig = *pp;
   1952 
   1953   /* FIXME: gdb checks os9k_stabs here.  */
   1954 
   1955   /* The aix4 compiler emits an extra field before the enum members;
   1956      my guess is it's a type of some sort.  Just ignore it.  */
   1957   if (**pp == '-')
   1958     {
   1959       while (**pp != ':')
   1960 	++*pp;
   1961       ++*pp;
   1962     }
   1963 
   1964   /* Read the value-names and their values.
   1965      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
   1966      A semicolon or comma instead of a NAME means the end.  */
   1967   alloc = 10;
   1968   names = (const char **) xmalloc (alloc * sizeof *names);
   1969   values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
   1970   n = 0;
   1971   while (**pp != '\0' && **pp != ';' && **pp != ',')
   1972     {
   1973       const char *p;
   1974       char *name;
   1975       bfd_signed_vma val;
   1976 
   1977       p = *pp;
   1978       while (*p != ':')
   1979 	++p;
   1980 
   1981       name = savestring (*pp, p - *pp);
   1982 
   1983       *pp = p + 1;
   1984       val = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
   1985       if (**pp != ',')
   1986 	{
   1987 	  bad_stab (orig);
   1988 	  free (name);
   1989 	  free (names);
   1990 	  free (values);
   1991 	  return DEBUG_TYPE_NULL;
   1992 	}
   1993       ++*pp;
   1994 
   1995       if (n + 1 >= alloc)
   1996 	{
   1997 	  alloc += 10;
   1998 	  names = ((const char **)
   1999 		   xrealloc (names, alloc * sizeof *names));
   2000 	  values = ((bfd_signed_vma *)
   2001 		    xrealloc (values, alloc * sizeof *values));
   2002 	}
   2003 
   2004       names[n] = name;
   2005       values[n] = val;
   2006       ++n;
   2007     }
   2008 
   2009   names[n] = NULL;
   2010   values[n] = 0;
   2011 
   2012   if (**pp == ';')
   2013     ++*pp;
   2014 
   2015   return debug_make_enum_type (dhandle, names, values);
   2016 }
   2017 
   2018 /* Read the description of a structure (or union type) and return an object
   2019    describing the type.
   2020 
   2021    PP points to a character pointer that points to the next unconsumed token
   2022    in the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
   2023    *PP will point to "4a:1,0,32;;".  */
   2024 
   2025 static debug_type
   2026 parse_stab_struct_type (void *dhandle, struct stab_handle *info,
   2027 			const char *tagname, const char **pp,
   2028 			bfd_boolean structp, const int *typenums)
   2029 {
   2030   bfd_vma size;
   2031   debug_baseclass *baseclasses;
   2032   debug_field *fields = NULL;
   2033   bfd_boolean statics;
   2034   debug_method *methods;
   2035   debug_type vptrbase;
   2036   bfd_boolean ownvptr;
   2037 
   2038   /* Get the size.  */
   2039   size = parse_number (pp, (bfd_boolean *) NULL);
   2040 
   2041   /* Get the other information.  */
   2042   if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
   2043       || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
   2044       || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
   2045       || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
   2046 				   &ownvptr))
   2047     {
   2048       if (fields != NULL)
   2049 	free (fields);
   2050       return DEBUG_TYPE_NULL;
   2051     }
   2052 
   2053   if (! statics
   2054       && baseclasses == NULL
   2055       && methods == NULL
   2056       && vptrbase == DEBUG_TYPE_NULL
   2057       && ! ownvptr)
   2058     return debug_make_struct_type (dhandle, structp, size, fields);
   2059 
   2060   return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
   2061 				 methods, vptrbase, ownvptr);
   2062 }
   2063 
   2064 /* The stabs for C++ derived classes contain baseclass information which
   2065    is marked by a '!' character after the total size.  This function is
   2066    called when we encounter the baseclass marker, and slurps up all the
   2067    baseclass information.
   2068 
   2069    Immediately following the '!' marker is the number of base classes that
   2070    the class is derived from, followed by information for each base class.
   2071    For each base class, there are two visibility specifiers, a bit offset
   2072    to the base class information within the derived class, a reference to
   2073    the type for the base class, and a terminating semicolon.
   2074 
   2075    A typical example, with two base classes, would be "!2,020,19;0264,21;".
   2076 						       ^^ ^ ^ ^  ^ ^  ^
   2077 	Baseclass information marker __________________|| | | |  | |  |
   2078 	Number of baseclasses __________________________| | | |  | |  |
   2079 	Visibility specifiers (2) ________________________| | |  | |  |
   2080 	Offset in bits from start of class _________________| |  | |  |
   2081 	Type number for base class ___________________________|  | |  |
   2082 	Visibility specifiers (2) _______________________________| |  |
   2083 	Offset in bits from start of class ________________________|  |
   2084 	Type number of base class ____________________________________|
   2085 
   2086   Return TRUE for success, FALSE for failure.  */
   2087 
   2088 static bfd_boolean
   2089 parse_stab_baseclasses (void *dhandle, struct stab_handle *info,
   2090 			const char **pp, debug_baseclass **retp)
   2091 {
   2092   const char *orig;
   2093   unsigned int c, i;
   2094   debug_baseclass *classes;
   2095 
   2096   *retp = NULL;
   2097 
   2098   orig = *pp;
   2099 
   2100   if (**pp != '!')
   2101     {
   2102       /* No base classes.  */
   2103       return TRUE;
   2104     }
   2105   ++*pp;
   2106 
   2107   c = (unsigned int) parse_number (pp, (bfd_boolean *) NULL);
   2108 
   2109   if (**pp != ',')
   2110     {
   2111       bad_stab (orig);
   2112       return FALSE;
   2113     }
   2114   ++*pp;
   2115 
   2116   classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
   2117 
   2118   for (i = 0; i < c; i++)
   2119     {
   2120       bfd_boolean is_virtual;
   2121       enum debug_visibility visibility;
   2122       bfd_vma bitpos;
   2123       debug_type type;
   2124 
   2125       switch (**pp)
   2126 	{
   2127 	case '0':
   2128 	  is_virtual = FALSE;
   2129 	  break;
   2130 	case '1':
   2131 	  is_virtual = TRUE;
   2132 	  break;
   2133 	default:
   2134 	  warn_stab (orig, _("unknown virtual character for baseclass"));
   2135 	  is_virtual = FALSE;
   2136 	  break;
   2137 	}
   2138       ++*pp;
   2139 
   2140       switch (**pp)
   2141 	{
   2142 	case '0':
   2143 	  visibility = DEBUG_VISIBILITY_PRIVATE;
   2144 	  break;
   2145 	case '1':
   2146 	  visibility = DEBUG_VISIBILITY_PROTECTED;
   2147 	  break;
   2148 	case '2':
   2149 	  visibility = DEBUG_VISIBILITY_PUBLIC;
   2150 	  break;
   2151 	default:
   2152 	  warn_stab (orig, _("unknown visibility character for baseclass"));
   2153 	  visibility = DEBUG_VISIBILITY_PUBLIC;
   2154 	  break;
   2155 	}
   2156       ++*pp;
   2157 
   2158       /* The remaining value is the bit offset of the portion of the
   2159 	 object corresponding to this baseclass.  Always zero in the
   2160 	 absence of multiple inheritance.  */
   2161       bitpos = parse_number (pp, (bfd_boolean *) NULL);
   2162       if (**pp != ',')
   2163 	{
   2164 	  bad_stab (orig);
   2165 	  return FALSE;
   2166 	}
   2167       ++*pp;
   2168 
   2169       type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   2170 			      (debug_type **) NULL);
   2171       if (type == DEBUG_TYPE_NULL)
   2172 	return FALSE;
   2173 
   2174       classes[i] = debug_make_baseclass (dhandle, type, bitpos, is_virtual,
   2175 					 visibility);
   2176       if (classes[i] == DEBUG_BASECLASS_NULL)
   2177 	return FALSE;
   2178 
   2179       if (**pp != ';')
   2180 	return FALSE;
   2181       ++*pp;
   2182     }
   2183 
   2184   classes[i] = DEBUG_BASECLASS_NULL;
   2185 
   2186   *retp = classes;
   2187 
   2188   return TRUE;
   2189 }
   2190 
   2191 /* Read struct or class data fields.  They have the form:
   2192 
   2193 	NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
   2194 
   2195    At the end, we see a semicolon instead of a field.
   2196 
   2197    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
   2198    a static field.
   2199 
   2200    The optional VISIBILITY is one of:
   2201 
   2202 	'/0'	(VISIBILITY_PRIVATE)
   2203 	'/1'	(VISIBILITY_PROTECTED)
   2204 	'/2'	(VISIBILITY_PUBLIC)
   2205 	'/9'	(VISIBILITY_IGNORE)
   2206 
   2207    or nothing, for C style fields with public visibility.
   2208 
   2209    Returns 1 for success, 0 for failure.  */
   2210 
   2211 static bfd_boolean
   2212 parse_stab_struct_fields (void *dhandle, struct stab_handle *info,
   2213 			  const char **pp, debug_field **retp,
   2214 			  bfd_boolean *staticsp)
   2215 {
   2216   const char *orig;
   2217   const char *p;
   2218   debug_field *fields;
   2219   unsigned int c;
   2220   unsigned int alloc;
   2221 
   2222   *retp = NULL;
   2223   *staticsp = FALSE;
   2224 
   2225   orig = *pp;
   2226 
   2227   c = 0;
   2228   alloc = 10;
   2229   fields = (debug_field *) xmalloc (alloc * sizeof *fields);
   2230   while (**pp != ';')
   2231     {
   2232       /* FIXME: gdb checks os9k_stabs here.  */
   2233 
   2234       p = *pp;
   2235 
   2236       /* Add 1 to c to leave room for NULL pointer at end.  */
   2237       if (c + 1 >= alloc)
   2238 	{
   2239 	  alloc += 10;
   2240 	  fields = ((debug_field *)
   2241 		    xrealloc (fields, alloc * sizeof *fields));
   2242 	}
   2243 
   2244       /* If it starts with CPLUS_MARKER it is a special abbreviation,
   2245 	 unless the CPLUS_MARKER is followed by an underscore, in
   2246 	 which case it is just the name of an anonymous type, which we
   2247 	 should handle like any other type name.  We accept either '$'
   2248 	 or '.', because a field name can never contain one of these
   2249 	 characters except as a CPLUS_MARKER.  */
   2250 
   2251       if ((*p == '$' || *p == '.') && p[1] != '_')
   2252 	{
   2253 	  ++*pp;
   2254 	  if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
   2255 	    {
   2256 	      free (fields);
   2257 	      return FALSE;
   2258 	    }
   2259 	  ++c;
   2260 	  continue;
   2261 	}
   2262 
   2263       /* Look for the ':' that separates the field name from the field
   2264 	 values.  Data members are delimited by a single ':', while member
   2265 	 functions are delimited by a pair of ':'s.  When we hit the member
   2266 	 functions (if any), terminate scan loop and return.  */
   2267 
   2268       p = strchr (p, ':');
   2269       if (p == NULL)
   2270 	{
   2271 	  bad_stab (orig);
   2272 	  free (fields);
   2273 	  return FALSE;
   2274 	}
   2275 
   2276       if (p[1] == ':')
   2277 	break;
   2278 
   2279       if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
   2280 					 staticsp))
   2281 	return FALSE;
   2282 
   2283       ++c;
   2284     }
   2285 
   2286   fields[c] = DEBUG_FIELD_NULL;
   2287 
   2288   *retp = fields;
   2289 
   2290   return TRUE;
   2291 }
   2292 
   2293 /* Special GNU C++ name.  */
   2294 
   2295 static bfd_boolean
   2296 parse_stab_cpp_abbrev (void *dhandle, struct stab_handle *info,
   2297 		       const char **pp, debug_field *retp)
   2298 {
   2299   const char *orig;
   2300   int cpp_abbrev;
   2301   debug_type context;
   2302   const char *name;
   2303   const char *type_name;
   2304   debug_type type;
   2305   bfd_vma bitpos;
   2306 
   2307   *retp = DEBUG_FIELD_NULL;
   2308 
   2309   orig = *pp;
   2310 
   2311   if (**pp != 'v')
   2312     {
   2313       bad_stab (*pp);
   2314       return FALSE;
   2315     }
   2316   ++*pp;
   2317 
   2318   cpp_abbrev = **pp;
   2319   ++*pp;
   2320 
   2321   /* At this point, *pp points to something like "22:23=*22...", where
   2322      the type number before the ':' is the "context" and everything
   2323      after is a regular type definition.  Lookup the type, find it's
   2324      name, and construct the field name.  */
   2325 
   2326   context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   2327 			     (debug_type **) NULL);
   2328   if (context == DEBUG_TYPE_NULL)
   2329     return FALSE;
   2330 
   2331   switch (cpp_abbrev)
   2332     {
   2333     case 'f':
   2334       /* $vf -- a virtual function table pointer.  */
   2335       name = "_vptr$";
   2336       break;
   2337     case 'b':
   2338       /* $vb -- a virtual bsomethingorother */
   2339       type_name = debug_get_type_name (dhandle, context);
   2340       if (type_name == NULL)
   2341 	{
   2342 	  warn_stab (orig, _("unnamed $vb type"));
   2343 	  type_name = "FOO";
   2344 	}
   2345       name = concat ("_vb$", type_name, (const char *) NULL);
   2346       break;
   2347     default:
   2348       warn_stab (orig, _("unrecognized C++ abbreviation"));
   2349       name = "INVALID_CPLUSPLUS_ABBREV";
   2350       break;
   2351     }
   2352 
   2353   if (**pp != ':')
   2354     {
   2355       bad_stab (orig);
   2356       return FALSE;
   2357     }
   2358   ++*pp;
   2359 
   2360   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   2361 			  (debug_type **) NULL);
   2362   if (**pp != ',')
   2363     {
   2364       bad_stab (orig);
   2365       return FALSE;
   2366     }
   2367   ++*pp;
   2368 
   2369   bitpos = parse_number (pp, (bfd_boolean *) NULL);
   2370   if (**pp != ';')
   2371     {
   2372       bad_stab (orig);
   2373       return FALSE;
   2374     }
   2375   ++*pp;
   2376 
   2377   *retp = debug_make_field (dhandle, name, type, bitpos, 0,
   2378 			    DEBUG_VISIBILITY_PRIVATE);
   2379   if (*retp == DEBUG_FIELD_NULL)
   2380     return FALSE;
   2381 
   2382   return TRUE;
   2383 }
   2384 
   2385 /* Parse a single field in a struct or union.  */
   2386 
   2387 static bfd_boolean
   2388 parse_stab_one_struct_field (void *dhandle, struct stab_handle *info,
   2389 			     const char **pp, const char *p,
   2390 			     debug_field *retp, bfd_boolean *staticsp)
   2391 {
   2392   const char *orig;
   2393   char *name;
   2394   enum debug_visibility visibility;
   2395   debug_type type;
   2396   bfd_vma bitpos;
   2397   bfd_vma bitsize;
   2398 
   2399   orig = *pp;
   2400 
   2401   /* FIXME: gdb checks ARM_DEMANGLING here.  */
   2402 
   2403   name = savestring (*pp, p - *pp);
   2404 
   2405   *pp = p + 1;
   2406 
   2407   if (**pp != '/')
   2408     visibility = DEBUG_VISIBILITY_PUBLIC;
   2409   else
   2410     {
   2411       ++*pp;
   2412       switch (**pp)
   2413 	{
   2414 	case '0':
   2415 	  visibility = DEBUG_VISIBILITY_PRIVATE;
   2416 	  break;
   2417 	case '1':
   2418 	  visibility = DEBUG_VISIBILITY_PROTECTED;
   2419 	  break;
   2420 	case '2':
   2421 	  visibility = DEBUG_VISIBILITY_PUBLIC;
   2422 	  break;
   2423 	default:
   2424 	  warn_stab (orig, _("unknown visibility character for field"));
   2425 	  visibility = DEBUG_VISIBILITY_PUBLIC;
   2426 	  break;
   2427 	}
   2428       ++*pp;
   2429     }
   2430 
   2431   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   2432 			  (debug_type **) NULL);
   2433   if (type == DEBUG_TYPE_NULL)
   2434     {
   2435       free (name);
   2436       return FALSE;
   2437     }
   2438 
   2439   if (**pp == ':')
   2440     {
   2441       char *varname;
   2442 
   2443       /* This is a static class member.  */
   2444       ++*pp;
   2445       p = strchr (*pp, ';');
   2446       if (p == NULL)
   2447 	{
   2448 	  bad_stab (orig);
   2449 	  free (name);
   2450 	  return FALSE;
   2451 	}
   2452 
   2453       varname = savestring (*pp, p - *pp);
   2454 
   2455       *pp = p + 1;
   2456 
   2457       *retp = debug_make_static_member (dhandle, name, type, varname,
   2458 					visibility);
   2459       *staticsp = TRUE;
   2460 
   2461       return TRUE;
   2462     }
   2463 
   2464   if (**pp != ',')
   2465     {
   2466       bad_stab (orig);
   2467       free (name);
   2468       return FALSE;
   2469     }
   2470   ++*pp;
   2471 
   2472   bitpos = parse_number (pp, (bfd_boolean *) NULL);
   2473   if (**pp != ',')
   2474     {
   2475       bad_stab (orig);
   2476       free (name);
   2477       return FALSE;
   2478     }
   2479   ++*pp;
   2480 
   2481   bitsize = parse_number (pp, (bfd_boolean *) NULL);
   2482   if (**pp != ';')
   2483     {
   2484       bad_stab (orig);
   2485       free (name);
   2486       return FALSE;
   2487     }
   2488   ++*pp;
   2489 
   2490   if (bitpos == 0 && bitsize == 0)
   2491     {
   2492       /* This can happen in two cases: (1) at least for gcc 2.4.5 or
   2493 	 so, it is a field which has been optimized out.  The correct
   2494 	 stab for this case is to use VISIBILITY_IGNORE, but that is a
   2495 	 recent invention.  (2) It is a 0-size array.  For example
   2496 	 union { int num; char str[0]; } foo.  Printing "<no value>"
   2497 	 for str in "p foo" is OK, since foo.str (and thus foo.str[3])
   2498 	 will continue to work, and a 0-size array as a whole doesn't
   2499 	 have any contents to print.
   2500 
   2501 	 I suspect this probably could also happen with gcc -gstabs
   2502 	 (not -gstabs+) for static fields, and perhaps other C++
   2503 	 extensions.  Hopefully few people use -gstabs with gdb, since
   2504 	 it is intended for dbx compatibility.  */
   2505       visibility = DEBUG_VISIBILITY_IGNORE;
   2506     }
   2507 
   2508   /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
   2509 
   2510   *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
   2511 
   2512   return TRUE;
   2513 }
   2514 
   2515 /* Read member function stabs info for C++ classes.  The form of each member
   2516    function data is:
   2517 
   2518 	NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
   2519 
   2520    An example with two member functions is:
   2521 
   2522 	afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
   2523 
   2524    For the case of overloaded operators, the format is op$::*.funcs, where
   2525    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
   2526    name (such as `+=') and `.' marks the end of the operator name.  */
   2527 
   2528 static bfd_boolean
   2529 parse_stab_members (void *dhandle, struct stab_handle *info,
   2530 		    const char *tagname, const char **pp,
   2531 		    const int *typenums, debug_method **retp)
   2532 {
   2533   const char *orig;
   2534   debug_method *methods;
   2535   unsigned int c;
   2536   unsigned int alloc;
   2537   char *name = NULL;
   2538   debug_method_variant *variants = NULL;
   2539   char *argtypes = NULL;
   2540 
   2541   *retp = NULL;
   2542 
   2543   orig = *pp;
   2544 
   2545   alloc = 0;
   2546   methods = NULL;
   2547   c = 0;
   2548 
   2549   while (**pp != ';')
   2550     {
   2551       const char *p;
   2552       unsigned int cvars;
   2553       unsigned int allocvars;
   2554       debug_type look_ahead_type;
   2555 
   2556       p = strchr (*pp, ':');
   2557       if (p == NULL || p[1] != ':')
   2558 	break;
   2559 
   2560       /* FIXME: Some systems use something other than '$' here.  */
   2561       if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
   2562 	{
   2563 	  name = savestring (*pp, p - *pp);
   2564 	  *pp = p + 2;
   2565 	}
   2566       else
   2567 	{
   2568 	  /* This is a completely weird case.  In order to stuff in the
   2569 	     names that might contain colons (the usual name delimiter),
   2570 	     Mike Tiemann defined a different name format which is
   2571 	     signalled if the identifier is "op$".  In that case, the
   2572 	     format is "op$::XXXX." where XXXX is the name.  This is
   2573 	     used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
   2574 	  *pp = p + 2;
   2575 	  for (p = *pp; *p != '.' && *p != '\0'; p++)
   2576 	    ;
   2577 	  if (*p != '.')
   2578 	    {
   2579 	      bad_stab (orig);
   2580 	      goto fail;
   2581 	    }
   2582 	  name = savestring (*pp, p - *pp);
   2583 	  *pp = p + 1;
   2584 	}
   2585 
   2586       allocvars = 10;
   2587       variants = ((debug_method_variant *)
   2588 		  xmalloc (allocvars * sizeof *variants));
   2589       cvars = 0;
   2590 
   2591       look_ahead_type = DEBUG_TYPE_NULL;
   2592 
   2593       do
   2594 	{
   2595 	  debug_type type;
   2596 	  bfd_boolean stub;
   2597 	  enum debug_visibility visibility;
   2598 	  bfd_boolean constp, volatilep, staticp;
   2599 	  bfd_vma voffset;
   2600 	  debug_type context;
   2601 	  const char *physname;
   2602 	  bfd_boolean varargs;
   2603 
   2604 	  if (look_ahead_type != DEBUG_TYPE_NULL)
   2605 	    {
   2606 	      /* g++ version 1 kludge */
   2607 	      type = look_ahead_type;
   2608 	      look_ahead_type = DEBUG_TYPE_NULL;
   2609 	    }
   2610 	  else
   2611 	    {
   2612 	      type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   2613 				      (debug_type **) NULL);
   2614 	      if (type == DEBUG_TYPE_NULL)
   2615 		goto fail;
   2616 
   2617 	      if (**pp != ':')
   2618 		{
   2619 		  bad_stab (orig);
   2620 		  goto fail;
   2621 		}
   2622 	    }
   2623 
   2624 	  ++*pp;
   2625 	  p = strchr (*pp, ';');
   2626 	  if (p == NULL)
   2627 	    {
   2628 	      bad_stab (orig);
   2629 	      goto fail;
   2630 	    }
   2631 
   2632 	  stub = FALSE;
   2633 	  if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
   2634 	      && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
   2635 	    stub = TRUE;
   2636 
   2637 	  argtypes = savestring (*pp, p - *pp);
   2638 	  *pp = p + 1;
   2639 
   2640 	  switch (**pp)
   2641 	    {
   2642 	    case '0':
   2643 	      visibility = DEBUG_VISIBILITY_PRIVATE;
   2644 	      break;
   2645 	    case '1':
   2646 	      visibility = DEBUG_VISIBILITY_PROTECTED;
   2647 	      break;
   2648 	    default:
   2649 	      visibility = DEBUG_VISIBILITY_PUBLIC;
   2650 	      break;
   2651 	    }
   2652 	  ++*pp;
   2653 
   2654 	  constp = FALSE;
   2655 	  volatilep = FALSE;
   2656 	  switch (**pp)
   2657 	    {
   2658 	    case 'A':
   2659 	      /* Normal function.  */
   2660 	      ++*pp;
   2661 	      break;
   2662 	    case 'B':
   2663 	      /* const member function.  */
   2664 	      constp = TRUE;
   2665 	      ++*pp;
   2666 	      break;
   2667 	    case 'C':
   2668 	      /* volatile member function.  */
   2669 	      volatilep = TRUE;
   2670 	      ++*pp;
   2671 	      break;
   2672 	    case 'D':
   2673 	      /* const volatile member function.  */
   2674 	      constp = TRUE;
   2675 	      volatilep = TRUE;
   2676 	      ++*pp;
   2677 	      break;
   2678 	    case '*':
   2679 	    case '?':
   2680 	    case '.':
   2681 	      /* File compiled with g++ version 1; no information.  */
   2682 	      break;
   2683 	    default:
   2684 	      warn_stab (orig, _("const/volatile indicator missing"));
   2685 	      break;
   2686 	    }
   2687 
   2688 	  staticp = FALSE;
   2689 	  switch (**pp)
   2690 	    {
   2691 	    case '*':
   2692 	      /* virtual member function, followed by index.  The sign
   2693 		 bit is supposedly set to distinguish
   2694 		 pointers-to-methods from virtual function indicies.  */
   2695 	      ++*pp;
   2696 	      voffset = parse_number (pp, (bfd_boolean *) NULL);
   2697 	      if (**pp != ';')
   2698 		{
   2699 		  bad_stab (orig);
   2700 		  goto fail;
   2701 		}
   2702 	      ++*pp;
   2703 	      voffset &= 0x7fffffff;
   2704 
   2705 	      if (**pp == ';' || *pp == '\0')
   2706 		{
   2707 		  /* Must be g++ version 1.  */
   2708 		  context = DEBUG_TYPE_NULL;
   2709 		}
   2710 	      else
   2711 		{
   2712 		  /* Figure out from whence this virtual function
   2713 		     came.  It may belong to virtual function table of
   2714 		     one of its baseclasses.  */
   2715 		  look_ahead_type = parse_stab_type (dhandle, info,
   2716 						     (const char *) NULL,
   2717 						     pp,
   2718 						     (debug_type **) NULL);
   2719 		  if (**pp == ':')
   2720 		    {
   2721 		      /* g++ version 1 overloaded methods.  */
   2722 		      context = DEBUG_TYPE_NULL;
   2723 		    }
   2724 		  else
   2725 		    {
   2726 		      context = look_ahead_type;
   2727 		      look_ahead_type = DEBUG_TYPE_NULL;
   2728 		      if (**pp != ';')
   2729 			{
   2730 			  bad_stab (orig);
   2731 			  goto fail;
   2732 			}
   2733 		      ++*pp;
   2734 		    }
   2735 		}
   2736 	      break;
   2737 
   2738 	    case '?':
   2739 	      /* static member function.  */
   2740 	      ++*pp;
   2741 	      staticp = TRUE;
   2742 	      voffset = 0;
   2743 	      context = DEBUG_TYPE_NULL;
   2744 	      if (strncmp (argtypes, name, strlen (name)) != 0)
   2745 		stub = TRUE;
   2746 	      break;
   2747 
   2748 	    default:
   2749 	      warn_stab (orig, "member function type missing");
   2750 	      voffset = 0;
   2751 	      context = DEBUG_TYPE_NULL;
   2752 	      break;
   2753 
   2754 	    case '.':
   2755 	      ++*pp;
   2756 	      voffset = 0;
   2757 	      context = DEBUG_TYPE_NULL;
   2758 	      break;
   2759 	    }
   2760 
   2761 	  /* If the type is not a stub, then the argtypes string is
   2762              the physical name of the function.  Otherwise the
   2763              argtypes string is the mangled form of the argument
   2764              types, and the full type and the physical name must be
   2765              extracted from them.  */
   2766 	  physname = argtypes;
   2767 	  if (stub)
   2768 	    {
   2769 	      debug_type class_type, return_type;
   2770 
   2771 	      class_type = stab_find_type (dhandle, info, typenums);
   2772 	      if (class_type == DEBUG_TYPE_NULL)
   2773 		goto fail;
   2774 	      return_type = debug_get_return_type (dhandle, type);
   2775 	      if (return_type == DEBUG_TYPE_NULL)
   2776 		{
   2777 		  bad_stab (orig);
   2778 		  goto fail;
   2779 		}
   2780 	      type = parse_stab_argtypes (dhandle, info, class_type, name,
   2781 					  tagname, return_type, argtypes,
   2782 					  constp, volatilep, &physname);
   2783 	      if (type == DEBUG_TYPE_NULL)
   2784 		goto fail;
   2785 	    }
   2786 
   2787 	  if (cvars + 1 >= allocvars)
   2788 	    {
   2789 	      allocvars += 10;
   2790 	      variants = ((debug_method_variant *)
   2791 			  xrealloc (variants,
   2792 				    allocvars * sizeof *variants));
   2793 	    }
   2794 
   2795 	  if (! staticp)
   2796 	    variants[cvars] = debug_make_method_variant (dhandle, physname,
   2797 							 type, visibility,
   2798 							 constp, volatilep,
   2799 							 voffset, context);
   2800 	  else
   2801 	    variants[cvars] = debug_make_static_method_variant (dhandle,
   2802 								physname,
   2803 								type,
   2804 								visibility,
   2805 								constp,
   2806 								volatilep);
   2807 	  if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
   2808 	    goto fail;
   2809 
   2810 	  ++cvars;
   2811 	}
   2812       while (**pp != ';' && **pp != '\0');
   2813 
   2814       variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
   2815 
   2816       if (**pp != '\0')
   2817 	++*pp;
   2818 
   2819       if (c + 1 >= alloc)
   2820 	{
   2821 	  alloc += 10;
   2822 	  methods = ((debug_method *)
   2823 		     xrealloc (methods, alloc * sizeof *methods));
   2824 	}
   2825 
   2826       methods[c] = debug_make_method (dhandle, name, variants);
   2827 
   2828       ++c;
   2829     }
   2830 
   2831   if (methods != NULL)
   2832     methods[c] = DEBUG_METHOD_NULL;
   2833 
   2834   *retp = methods;
   2835 
   2836   return TRUE;
   2837 
   2838  fail:
   2839   if (name != NULL)
   2840     free (name);
   2841   if (variants != NULL)
   2842     free (variants);
   2843   if (argtypes != NULL)
   2844     free (argtypes);
   2845   return FALSE;
   2846 }
   2847 
   2848 /* Parse a string representing argument types for a method.  Stabs
   2849    tries to save space by packing argument types into a mangled
   2850    string.  This string should give us enough information to extract
   2851    both argument types and the physical name of the function, given
   2852    the tag name.  */
   2853 
   2854 static debug_type
   2855 parse_stab_argtypes (void *dhandle, struct stab_handle *info,
   2856 		     debug_type class_type, const char *fieldname,
   2857 		     const char *tagname, debug_type return_type,
   2858 		     const char *argtypes, bfd_boolean constp,
   2859 		     bfd_boolean volatilep, const char **pphysname)
   2860 {
   2861   bfd_boolean is_full_physname_constructor;
   2862   bfd_boolean is_constructor;
   2863   bfd_boolean is_destructor;
   2864   bfd_boolean is_v3;
   2865   debug_type *args;
   2866   bfd_boolean varargs;
   2867   unsigned int physname_len = 0;
   2868 
   2869   /* Constructors are sometimes handled specially.  */
   2870   is_full_physname_constructor = ((argtypes[0] == '_'
   2871 				   && argtypes[1] == '_'
   2872 				   && (ISDIGIT (argtypes[2])
   2873 				       || argtypes[2] == 'Q'
   2874 				       || argtypes[2] == 't'))
   2875 				  || CONST_STRNEQ (argtypes, "__ct"));
   2876 
   2877   is_constructor = (is_full_physname_constructor
   2878 		    || (tagname != NULL
   2879 			&& strcmp (fieldname, tagname) == 0));
   2880   is_destructor = ((argtypes[0] == '_'
   2881 		    && (argtypes[1] == '$' || argtypes[1] == '.')
   2882 		    && argtypes[2] == '_')
   2883 		   || CONST_STRNEQ (argtypes, "__dt"));
   2884   is_v3 = argtypes[0] == '_' && argtypes[1] == 'Z';
   2885 
   2886   if (!(is_destructor || is_full_physname_constructor || is_v3))
   2887     {
   2888       unsigned int len;
   2889       const char *const_prefix;
   2890       const char *volatile_prefix;
   2891       char buf[20];
   2892       unsigned int mangled_name_len;
   2893       char *physname;
   2894 
   2895       len = tagname == NULL ? 0 : strlen (tagname);
   2896       const_prefix = constp ? "C" : "";
   2897       volatile_prefix = volatilep ? "V" : "";
   2898 
   2899       if (len == 0)
   2900 	sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
   2901       else if (tagname != NULL && strchr (tagname, '<') != NULL)
   2902 	{
   2903 	  /* Template methods are fully mangled.  */
   2904 	  sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
   2905 	  tagname = NULL;
   2906 	  len = 0;
   2907 	}
   2908       else
   2909 	sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
   2910 
   2911       mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
   2912 			  + strlen (buf)
   2913 			  + len
   2914 			  + strlen (argtypes)
   2915 			  + 1);
   2916 
   2917       if (fieldname[0] == 'o'
   2918 	  && fieldname[1] == 'p'
   2919 	  && (fieldname[2] == '$' || fieldname[2] == '.'))
   2920 	{
   2921 	  const char *opname;
   2922 
   2923 	  opname = cplus_mangle_opname (fieldname + 3, 0);
   2924 	  if (opname == NULL)
   2925 	    {
   2926 	      fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
   2927 	      return DEBUG_TYPE_NULL;
   2928 	    }
   2929 	  mangled_name_len += strlen (opname);
   2930 	  physname = (char *) xmalloc (mangled_name_len);
   2931 	  strncpy (physname, fieldname, 3);
   2932 	  strcpy (physname + 3, opname);
   2933 	}
   2934       else
   2935 	{
   2936 	  physname = (char *) xmalloc (mangled_name_len);
   2937 	  if (is_constructor)
   2938 	    physname[0] = '\0';
   2939 	  else
   2940 	    strcpy (physname, fieldname);
   2941 	}
   2942 
   2943       physname_len = strlen (physname);
   2944       strcat (physname, buf);
   2945       if (tagname != NULL)
   2946 	strcat (physname, tagname);
   2947       strcat (physname, argtypes);
   2948 
   2949       *pphysname = physname;
   2950     }
   2951 
   2952   if (*argtypes == '\0' || is_destructor)
   2953     {
   2954       args = (debug_type *) xmalloc (sizeof *args);
   2955       *args = NULL;
   2956       return debug_make_method_type (dhandle, return_type, class_type, args,
   2957 				     FALSE);
   2958     }
   2959 
   2960   args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs, physname_len);
   2961   if (args == NULL)
   2962     return DEBUG_TYPE_NULL;
   2963 
   2964   return debug_make_method_type (dhandle, return_type, class_type, args,
   2965 				 varargs);
   2966 }
   2967 
   2968 /* The tail end of stabs for C++ classes that contain a virtual function
   2969    pointer contains a tilde, a %, and a type number.
   2970    The type number refers to the base class (possibly this class itself) which
   2971    contains the vtable pointer for the current class.
   2972 
   2973    This function is called when we have parsed all the method declarations,
   2974    so we can look for the vptr base class info.  */
   2975 
   2976 static bfd_boolean
   2977 parse_stab_tilde_field (void *dhandle, struct stab_handle *info,
   2978 			const char **pp, const int *typenums,
   2979 			debug_type *retvptrbase, bfd_boolean *retownvptr)
   2980 {
   2981   const char *orig;
   2982   const char *hold;
   2983   int vtypenums[2];
   2984 
   2985   *retvptrbase = DEBUG_TYPE_NULL;
   2986   *retownvptr = FALSE;
   2987 
   2988   orig = *pp;
   2989 
   2990   /* If we are positioned at a ';', then skip it.  */
   2991   if (**pp == ';')
   2992     ++*pp;
   2993 
   2994   if (**pp != '~')
   2995     return TRUE;
   2996 
   2997   ++*pp;
   2998 
   2999   if (**pp == '=' || **pp == '+' || **pp == '-')
   3000     {
   3001       /* Obsolete flags that used to indicate the presence of
   3002 	 constructors and/or destructors.  */
   3003       ++*pp;
   3004     }
   3005 
   3006   if (**pp != '%')
   3007     return TRUE;
   3008 
   3009   ++*pp;
   3010 
   3011   hold = *pp;
   3012 
   3013   /* The next number is the type number of the base class (possibly
   3014      our own class) which supplies the vtable for this class.  */
   3015   if (! parse_stab_type_number (pp, vtypenums))
   3016     return FALSE;
   3017 
   3018   if (vtypenums[0] == typenums[0]
   3019       && vtypenums[1] == typenums[1])
   3020     *retownvptr = TRUE;
   3021   else
   3022     {
   3023       debug_type vtype;
   3024       const char *p;
   3025 
   3026       *pp = hold;
   3027 
   3028       vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   3029 			       (debug_type **) NULL);
   3030       for (p = *pp; *p != ';' && *p != '\0'; p++)
   3031 	;
   3032       if (*p != ';')
   3033 	{
   3034 	  bad_stab (orig);
   3035 	  return FALSE;
   3036 	}
   3037 
   3038       *retvptrbase = vtype;
   3039 
   3040       *pp = p + 1;
   3041     }
   3042 
   3043   return TRUE;
   3044 }
   3045 
   3046 /* Read a definition of an array type.  */
   3047 
   3048 static debug_type
   3049 parse_stab_array_type (void *dhandle, struct stab_handle *info,
   3050 		       const char **pp, bfd_boolean stringp)
   3051 {
   3052   const char *orig;
   3053   const char *p;
   3054   int typenums[2];
   3055   debug_type index_type;
   3056   bfd_boolean adjustable;
   3057   bfd_signed_vma lower, upper;
   3058   debug_type element_type;
   3059 
   3060   /* Format of an array type:
   3061      "ar<index type>;lower;upper;<array_contents_type>".
   3062      OS9000: "arlower,upper;<array_contents_type>".
   3063 
   3064      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
   3065      for these, produce a type like float[][].  */
   3066 
   3067   orig = *pp;
   3068 
   3069   /* FIXME: gdb checks os9k_stabs here.  */
   3070 
   3071   /* If the index type is type 0, we take it as int.  */
   3072   p = *pp;
   3073   if (! parse_stab_type_number (&p, typenums))
   3074     return DEBUG_TYPE_NULL;
   3075   if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
   3076     {
   3077       index_type = debug_find_named_type (dhandle, "int");
   3078       if (index_type == DEBUG_TYPE_NULL)
   3079 	{
   3080 	  index_type = debug_make_int_type (dhandle, 4, FALSE);
   3081 	  if (index_type == DEBUG_TYPE_NULL)
   3082 	    return DEBUG_TYPE_NULL;
   3083 	}
   3084       *pp = p;
   3085     }
   3086   else
   3087     {
   3088       index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   3089 				    (debug_type **) NULL);
   3090     }
   3091 
   3092   if (**pp != ';')
   3093     {
   3094       bad_stab (orig);
   3095       return DEBUG_TYPE_NULL;
   3096     }
   3097   ++*pp;
   3098 
   3099   adjustable = FALSE;
   3100 
   3101   if (! ISDIGIT (**pp) && **pp != '-')
   3102     {
   3103       ++*pp;
   3104       adjustable = TRUE;
   3105     }
   3106 
   3107   lower = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
   3108   if (**pp != ';')
   3109     {
   3110       bad_stab (orig);
   3111       return DEBUG_TYPE_NULL;
   3112     }
   3113   ++*pp;
   3114 
   3115   if (! ISDIGIT (**pp) && **pp != '-')
   3116     {
   3117       ++*pp;
   3118       adjustable = TRUE;
   3119     }
   3120 
   3121   upper = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
   3122   if (**pp != ';')
   3123     {
   3124       bad_stab (orig);
   3125       return DEBUG_TYPE_NULL;
   3126     }
   3127   ++*pp;
   3128 
   3129   element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
   3130 				  (debug_type **) NULL);
   3131   if (element_type == DEBUG_TYPE_NULL)
   3132     return DEBUG_TYPE_NULL;
   3133 
   3134   if (adjustable)
   3135     {
   3136       lower = 0;
   3137       upper = -1;
   3138     }
   3139 
   3140   return debug_make_array_type (dhandle, element_type, index_type, lower,
   3141 				upper, stringp);
   3142 }
   3143 
   3144 /* This struct holds information about files we have seen using
   3145    N_BINCL.  */
   3146 
   3147 struct bincl_file
   3148 {
   3149   /* The next N_BINCL file.  */
   3150   struct bincl_file *next;
   3151   /* The next N_BINCL on the stack.  */
   3152   struct bincl_file *next_stack;
   3153   /* The file name.  */
   3154   const char *name;
   3155   /* The hash value.  */
   3156   bfd_vma hash;
   3157   /* The file index.  */
   3158   unsigned int file;
   3159   /* The list of types defined in this file.  */
   3160   struct stab_types *file_types;
   3161 };
   3162 
   3163 /* Start a new N_BINCL file, pushing it onto the stack.  */
   3164 
   3165 static void
   3166 push_bincl (struct stab_handle *info, const char *name, bfd_vma hash)
   3167 {
   3168   struct bincl_file *n;
   3169 
   3170   n = (struct bincl_file *) xmalloc (sizeof *n);
   3171   n->next = info->bincl_list;
   3172   n->next_stack = info->bincl_stack;
   3173   n->name = name;
   3174   n->hash = hash;
   3175   n->file = info->files;
   3176   n->file_types = NULL;
   3177   info->bincl_list = n;
   3178   info->bincl_stack = n;
   3179 
   3180   ++info->files;
   3181   info->file_types = ((struct stab_types **)
   3182 		      xrealloc (info->file_types,
   3183 				(info->files
   3184 				 * sizeof *info->file_types)));
   3185   info->file_types[n->file] = NULL;
   3186 }
   3187 
   3188 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
   3189    stack.  */
   3190 
   3191 static const char *
   3192 pop_bincl (struct stab_handle *info)
   3193 {
   3194   struct bincl_file *o;
   3195 
   3196   o = info->bincl_stack;
   3197   if (o == NULL)
   3198     return info->main_filename;
   3199   info->bincl_stack = o->next_stack;
   3200 
   3201   o->file_types = info->file_types[o->file];
   3202 
   3203   if (info->bincl_stack == NULL)
   3204     return info->main_filename;
   3205   return info->bincl_stack->name;
   3206 }
   3207 
   3208 /* Handle an N_EXCL: get the types from the corresponding N_BINCL.  */
   3209 
   3210 static bfd_boolean
   3211 find_excl (struct stab_handle *info, const char *name, bfd_vma hash)
   3212 {
   3213   struct bincl_file *l;
   3214 
   3215   ++info->files;
   3216   info->file_types = ((struct stab_types **)
   3217 		      xrealloc (info->file_types,
   3218 				(info->files
   3219 				 * sizeof *info->file_types)));
   3220 
   3221   for (l = info->bincl_list; l != NULL; l = l->next)
   3222     if (l->hash == hash && strcmp (l->name, name) == 0)
   3223       break;
   3224   if (l == NULL)
   3225     {
   3226       warn_stab (name, _("Undefined N_EXCL"));
   3227       info->file_types[info->files - 1] = NULL;
   3228       return TRUE;
   3229     }
   3230 
   3231   info->file_types[info->files - 1] = l->file_types;
   3232 
   3233   return TRUE;
   3234 }
   3235 
   3236 /* Handle a variable definition.  gcc emits variable definitions for a
   3237    block before the N_LBRAC, so we must hold onto them until we see
   3238    it.  The SunPRO compiler emits variable definitions after the
   3239    N_LBRAC, so we can call debug_record_variable immediately.  */
   3240 
   3241 static bfd_boolean
   3242 stab_record_variable (void *dhandle, struct stab_handle *info,
   3243 		      const char *name, debug_type type,
   3244 		      enum debug_var_kind kind, bfd_vma val)
   3245 {
   3246   struct stab_pending_var *v;
   3247 
   3248   if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
   3249       || ! info->within_function
   3250       || (info->gcc_compiled == 0 && info->n_opt_found))
   3251     return debug_record_variable (dhandle, name, type, kind, val);
   3252 
   3253   v = (struct stab_pending_var *) xmalloc (sizeof *v);
   3254   memset (v, 0, sizeof *v);
   3255 
   3256   v->next = info->pending;
   3257   v->name = name;
   3258   v->type = type;
   3259   v->kind = kind;
   3260   v->val = val;
   3261   info->pending = v;
   3262 
   3263   return TRUE;
   3264 }
   3265 
   3266 /* Emit pending variable definitions.  This is called after we see the
   3267    N_LBRAC that starts the block.  */
   3268 
   3269 static bfd_boolean
   3270 stab_emit_pending_vars (void *dhandle, struct stab_handle *info)
   3271 {
   3272   struct stab_pending_var *v;
   3273 
   3274   v = info->pending;
   3275   while (v != NULL)
   3276     {
   3277       struct stab_pending_var *next;
   3278 
   3279       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
   3280 	return FALSE;
   3281 
   3282       next = v->next;
   3283       free (v);
   3284       v = next;
   3285     }
   3286 
   3287   info->pending = NULL;
   3288 
   3289   return TRUE;
   3290 }
   3291 
   3292 /* Find the slot for a type in the database.  */
   3293 
   3294 static debug_type *
   3295 stab_find_slot (struct stab_handle *info, const int *typenums)
   3296 {
   3297   int filenum;
   3298   int tindex;
   3299   struct stab_types **ps;
   3300 
   3301   filenum = typenums[0];
   3302   tindex = typenums[1];
   3303 
   3304   if (filenum < 0 || (unsigned int) filenum >= info->files)
   3305     {
   3306       fprintf (stderr, _("Type file number %d out of range\n"), filenum);
   3307       return NULL;
   3308     }
   3309   if (tindex < 0)
   3310     {
   3311       fprintf (stderr, _("Type index number %d out of range\n"), tindex);
   3312       return NULL;
   3313     }
   3314 
   3315   ps = info->file_types + filenum;
   3316 
   3317   while (tindex >= STAB_TYPES_SLOTS)
   3318     {
   3319       if (*ps == NULL)
   3320 	{
   3321 	  *ps = (struct stab_types *) xmalloc (sizeof **ps);
   3322 	  memset (*ps, 0, sizeof **ps);
   3323 	}
   3324       ps = &(*ps)->next;
   3325       tindex -= STAB_TYPES_SLOTS;
   3326     }
   3327   if (*ps == NULL)
   3328     {
   3329       *ps = (struct stab_types *) xmalloc (sizeof **ps);
   3330       memset (*ps, 0, sizeof **ps);
   3331     }
   3332 
   3333   return (*ps)->types + tindex;
   3334 }
   3335 
   3336 /* Find a type given a type number.  If the type has not been
   3337    allocated yet, create an indirect type.  */
   3338 
   3339 static debug_type
   3340 stab_find_type (void *dhandle, struct stab_handle *info, const int *typenums)
   3341 {
   3342   debug_type *slot;
   3343 
   3344   if (typenums[0] == 0 && typenums[1] < 0)
   3345     {
   3346       /* A negative type number indicates an XCOFF builtin type.  */
   3347       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
   3348     }
   3349 
   3350   slot = stab_find_slot (info, typenums);
   3351   if (slot == NULL)
   3352     return DEBUG_TYPE_NULL;
   3353 
   3354   if (*slot == DEBUG_TYPE_NULL)
   3355     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
   3356 
   3357   return *slot;
   3358 }
   3359 
   3360 /* Record that a given type number refers to a given type.  */
   3361 
   3362 static bfd_boolean
   3363 stab_record_type (void *dhandle ATTRIBUTE_UNUSED, struct stab_handle *info,
   3364 		  const int *typenums, debug_type type)
   3365 {
   3366   debug_type *slot;
   3367 
   3368   slot = stab_find_slot (info, typenums);
   3369   if (slot == NULL)
   3370     return FALSE;
   3371 
   3372   /* gdb appears to ignore type redefinitions, so we do as well.  */
   3373 
   3374   *slot = type;
   3375 
   3376   return TRUE;
   3377 }
   3378 
   3379 /* Return an XCOFF builtin type.  */
   3380 
   3381 static debug_type
   3382 stab_xcoff_builtin_type (void *dhandle, struct stab_handle *info,
   3383 			 int typenum)
   3384 {
   3385   debug_type rettype;
   3386   const char *name;
   3387 
   3388   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
   3389     {
   3390       fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
   3391       return DEBUG_TYPE_NULL;
   3392     }
   3393   if (info->xcoff_types[-typenum] != NULL)
   3394     return info->xcoff_types[-typenum];
   3395 
   3396   switch (-typenum)
   3397     {
   3398     case 1:
   3399       /* The size of this and all the other types are fixed, defined
   3400 	 by the debugging format.  */
   3401       name = "int";
   3402       rettype = debug_make_int_type (dhandle, 4, FALSE);
   3403       break;
   3404     case 2:
   3405       name = "char";
   3406       rettype = debug_make_int_type (dhandle, 1, FALSE);
   3407       break;
   3408     case 3:
   3409       name = "short";
   3410       rettype = debug_make_int_type (dhandle, 2, FALSE);
   3411       break;
   3412     case 4:
   3413       name = "long";
   3414       rettype = debug_make_int_type (dhandle, 4, FALSE);
   3415       break;
   3416     case 5:
   3417       name = "unsigned char";
   3418       rettype = debug_make_int_type (dhandle, 1, TRUE);
   3419       break;
   3420     case 6:
   3421       name = "signed char";
   3422       rettype = debug_make_int_type (dhandle, 1, FALSE);
   3423       break;
   3424     case 7:
   3425       name = "unsigned short";
   3426       rettype = debug_make_int_type (dhandle, 2, TRUE);
   3427       break;
   3428     case 8:
   3429       name = "unsigned int";
   3430       rettype = debug_make_int_type (dhandle, 4, TRUE);
   3431       break;
   3432     case 9:
   3433       name = "unsigned";
   3434       rettype = debug_make_int_type (dhandle, 4, TRUE);
   3435     case 10:
   3436       name = "unsigned long";
   3437       rettype = debug_make_int_type (dhandle, 4, TRUE);
   3438       break;
   3439     case 11:
   3440       name = "void";
   3441       rettype = debug_make_void_type (dhandle);
   3442       break;
   3443     case 12:
   3444       /* IEEE single precision (32 bit).  */
   3445       name = "float";
   3446       rettype = debug_make_float_type (dhandle, 4);
   3447       break;
   3448     case 13:
   3449       /* IEEE double precision (64 bit).  */
   3450       name = "double";
   3451       rettype = debug_make_float_type (dhandle, 8);
   3452       break;
   3453     case 14:
   3454       /* This is an IEEE double on the RS/6000, and different machines
   3455 	 with different sizes for "long double" should use different
   3456 	 negative type numbers.  See stabs.texinfo.  */
   3457       name = "long double";
   3458       rettype = debug_make_float_type (dhandle, 8);
   3459       break;
   3460     case 15:
   3461       name = "integer";
   3462       rettype = debug_make_int_type (dhandle, 4, FALSE);
   3463       break;
   3464     case 16:
   3465       name = "boolean";
   3466       rettype = debug_make_bool_type (dhandle, 4);
   3467       break;
   3468     case 17:
   3469       name = "short real";
   3470       rettype = debug_make_float_type (dhandle, 4);
   3471       break;
   3472     case 18:
   3473       name = "real";
   3474       rettype = debug_make_float_type (dhandle, 8);
   3475       break;
   3476     case 19:
   3477       /* FIXME */
   3478       name = "stringptr";
   3479       rettype = NULL;
   3480       break;
   3481     case 20:
   3482       /* FIXME */
   3483       name = "character";
   3484       rettype = debug_make_int_type (dhandle, 1, TRUE);
   3485       break;
   3486     case 21:
   3487       name = "logical*1";
   3488       rettype = debug_make_bool_type (dhandle, 1);
   3489       break;
   3490     case 22:
   3491       name = "logical*2";
   3492       rettype = debug_make_bool_type (dhandle, 2);
   3493       break;
   3494     case 23:
   3495       name = "logical*4";
   3496       rettype = debug_make_bool_type (dhandle, 4);
   3497       break;
   3498     case 24:
   3499       name = "logical";
   3500       rettype = debug_make_bool_type (dhandle, 4);
   3501       break;
   3502     case 25:
   3503       /* Complex type consisting of two IEEE single precision values.  */
   3504       name = "complex";
   3505       rettype = debug_make_complex_type (dhandle, 8);
   3506       break;
   3507     case 26:
   3508       /* Complex type consisting of two IEEE double precision values.  */
   3509       name = "double complex";
   3510       rettype = debug_make_complex_type (dhandle, 16);
   3511       break;
   3512     case 27:
   3513       name = "integer*1";
   3514       rettype = debug_make_int_type (dhandle, 1, FALSE);
   3515       break;
   3516     case 28:
   3517       name = "integer*2";
   3518       rettype = debug_make_int_type (dhandle, 2, FALSE);
   3519       break;
   3520     case 29:
   3521       name = "integer*4";
   3522       rettype = debug_make_int_type (dhandle, 4, FALSE);
   3523       break;
   3524     case 30:
   3525       /* FIXME */
   3526       name = "wchar";
   3527       rettype = debug_make_int_type (dhandle, 2, FALSE);
   3528       break;
   3529     case 31:
   3530       name = "long long";
   3531       rettype = debug_make_int_type (dhandle, 8, FALSE);
   3532       break;
   3533     case 32:
   3534       name = "unsigned long long";
   3535       rettype = debug_make_int_type (dhandle, 8, TRUE);
   3536       break;
   3537     case 33:
   3538       name = "logical*8";
   3539       rettype = debug_make_bool_type (dhandle, 8);
   3540       break;
   3541     case 34:
   3542       name = "integer*8";
   3543       rettype = debug_make_int_type (dhandle, 8, FALSE);
   3544       break;
   3545     default:
   3546       abort ();
   3547     }
   3548 
   3549   rettype = debug_name_type (dhandle, name, rettype);
   3550 
   3551   info->xcoff_types[-typenum] = rettype;
   3552 
   3553   return rettype;
   3554 }
   3555 
   3556 /* Find or create a tagged type.  */
   3557 
   3558 static debug_type
   3559 stab_find_tagged_type (void *dhandle, struct stab_handle *info,
   3560 		       const char *p, int len, enum debug_type_kind kind)
   3561 {
   3562   char *name;
   3563   debug_type dtype;
   3564   struct stab_tag *st;
   3565 
   3566   name = savestring (p, len);
   3567 
   3568   /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
   3569      namespace.  This is right for C, and I don't know how to handle
   3570      other languages.  FIXME.  */
   3571   dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
   3572   if (dtype != DEBUG_TYPE_NULL)
   3573     {
   3574       free (name);
   3575       return dtype;
   3576     }
   3577 
   3578   /* We need to allocate an entry on the undefined tag list.  */
   3579   for (st = info->tags; st != NULL; st = st->next)
   3580     {
   3581       if (st->name[0] == name[0]
   3582 	  && strcmp (st->name, name) == 0)
   3583 	{
   3584 	  if (st->kind == DEBUG_KIND_ILLEGAL)
   3585 	    st->kind = kind;
   3586 	  free (name);
   3587 	  break;
   3588 	}
   3589     }
   3590   if (st == NULL)
   3591     {
   3592       st = (struct stab_tag *) xmalloc (sizeof *st);
   3593       memset (st, 0, sizeof *st);
   3594 
   3595       st->next = info->tags;
   3596       st->name = name;
   3597       st->kind = kind;
   3598       st->slot = DEBUG_TYPE_NULL;
   3599       st->type = debug_make_indirect_type (dhandle, &st->slot, name);
   3600       info->tags = st;
   3601     }
   3602 
   3603   return st->type;
   3604 }
   3605 
   3606 /* In order to get the correct argument types for a stubbed method, we
   3608    need to extract the argument types from a C++ mangled string.
   3609    Since the argument types can refer back to the return type, this
   3610    means that we must demangle the entire physical name.  In gdb this
   3611    is done by calling cplus_demangle and running the results back
   3612    through the C++ expression parser.  Since we have no expression
   3613    parser, we must duplicate much of the work of cplus_demangle here.
   3614 
   3615    We assume that GNU style demangling is used, since this is only
   3616    done for method stubs, and only g++ should output that form of
   3617    debugging information.  */
   3618 
   3619 /* This structure is used to hold a pointer to type information which
   3620    demangling a string.  */
   3621 
   3622 struct stab_demangle_typestring
   3623 {
   3624   /* The start of the type.  This is not null terminated.  */
   3625   const char *typestring;
   3626   /* The length of the type.  */
   3627   unsigned int len;
   3628 };
   3629 
   3630 /* This structure is used to hold information while demangling a
   3631    string.  */
   3632 
   3633 struct stab_demangle_info
   3634 {
   3635   /* The debugging information handle.  */
   3636   void *dhandle;
   3637   /* The stab information handle.  */
   3638   struct stab_handle *info;
   3639   /* The array of arguments we are building.  */
   3640   debug_type *args;
   3641   /* Whether the method takes a variable number of arguments.  */
   3642   bfd_boolean varargs;
   3643   /* The array of types we have remembered.  */
   3644   struct stab_demangle_typestring *typestrings;
   3645   /* The number of typestrings.  */
   3646   unsigned int typestring_count;
   3647   /* The number of typestring slots we have allocated.  */
   3648   unsigned int typestring_alloc;
   3649 };
   3650 
   3651 static void stab_bad_demangle (const char *);
   3652 static unsigned int stab_demangle_count (const char **);
   3653 static bfd_boolean stab_demangle_get_count (const char **, unsigned int *);
   3654 static bfd_boolean stab_demangle_prefix
   3655   (struct stab_demangle_info *, const char **, unsigned int);
   3656 static bfd_boolean stab_demangle_function_name
   3657   (struct stab_demangle_info *, const char **, const char *);
   3658 static bfd_boolean stab_demangle_signature
   3659   (struct stab_demangle_info *, const char **);
   3660 static bfd_boolean stab_demangle_qualified
   3661   (struct stab_demangle_info *, const char **, debug_type *);
   3662 static bfd_boolean stab_demangle_template
   3663   (struct stab_demangle_info *, const char **, char **);
   3664 static bfd_boolean stab_demangle_class
   3665   (struct stab_demangle_info *, const char **, const char **);
   3666 static bfd_boolean stab_demangle_args
   3667   (struct stab_demangle_info *, const char **, debug_type **, bfd_boolean *);
   3668 static bfd_boolean stab_demangle_arg
   3669   (struct stab_demangle_info *, const char **, debug_type **,
   3670    unsigned int *, unsigned int *);
   3671 static bfd_boolean stab_demangle_type
   3672   (struct stab_demangle_info *, const char **, debug_type *);
   3673 static bfd_boolean stab_demangle_fund_type
   3674   (struct stab_demangle_info *, const char **, debug_type *);
   3675 static bfd_boolean stab_demangle_remember_type
   3676   (struct stab_demangle_info *, const char *, int);
   3677 
   3678 /* Warn about a bad demangling.  */
   3679 
   3680 static void
   3681 stab_bad_demangle (const char *s)
   3682 {
   3683   fprintf (stderr, _("bad mangled name `%s'\n"), s);
   3684 }
   3685 
   3686 /* Get a count from a stab string.  */
   3687 
   3688 static unsigned int
   3689 stab_demangle_count (const char **pp)
   3690 {
   3691   unsigned int count;
   3692 
   3693   count = 0;
   3694   while (ISDIGIT (**pp))
   3695     {
   3696       count *= 10;
   3697       count += **pp - '0';
   3698       ++*pp;
   3699     }
   3700   return count;
   3701 }
   3702 
   3703 /* Require a count in a string.  The count may be multiple digits, in
   3704    which case it must end in an underscore.  */
   3705 
   3706 static bfd_boolean
   3707 stab_demangle_get_count (const char **pp, unsigned int *pi)
   3708 {
   3709   if (! ISDIGIT (**pp))
   3710     return FALSE;
   3711 
   3712   *pi = **pp - '0';
   3713   ++*pp;
   3714   if (ISDIGIT (**pp))
   3715     {
   3716       unsigned int count;
   3717       const char *p;
   3718 
   3719       count = *pi;
   3720       p = *pp;
   3721       do
   3722 	{
   3723 	  count *= 10;
   3724 	  count += *p - '0';
   3725 	  ++p;
   3726 	}
   3727       while (ISDIGIT (*p));
   3728       if (*p == '_')
   3729 	{
   3730 	  *pp = p + 1;
   3731 	  *pi = count;
   3732 	}
   3733     }
   3734 
   3735   return TRUE;
   3736 }
   3737 
   3738 /* This function demangles a physical name, returning a NULL
   3739    terminated array of argument types.  */
   3740 
   3741 static debug_type *
   3742 stab_demangle_argtypes (void *dhandle, struct stab_handle *info,
   3743 			const char *physname, bfd_boolean *pvarargs,
   3744 			unsigned int physname_len)
   3745 {
   3746   struct stab_demangle_info minfo;
   3747 
   3748   /* Check for the g++ V3 ABI.  */
   3749   if (physname[0] == '_' && physname[1] == 'Z')
   3750     return stab_demangle_v3_argtypes (dhandle, info, physname, pvarargs);
   3751 
   3752   minfo.dhandle = dhandle;
   3753   minfo.info = info;
   3754   minfo.args = NULL;
   3755   minfo.varargs = FALSE;
   3756   minfo.typestring_alloc = 10;
   3757   minfo.typestrings = ((struct stab_demangle_typestring *)
   3758 		       xmalloc (minfo.typestring_alloc
   3759 				* sizeof *minfo.typestrings));
   3760   minfo.typestring_count = 0;
   3761 
   3762   /* cplus_demangle checks for special GNU mangled forms, but we can't
   3763      see any of them in mangled method argument types.  */
   3764 
   3765   if (! stab_demangle_prefix (&minfo, &physname, physname_len))
   3766     goto error_return;
   3767 
   3768   if (*physname != '\0')
   3769     {
   3770       if (! stab_demangle_signature (&minfo, &physname))
   3771 	goto error_return;
   3772     }
   3773 
   3774   free (minfo.typestrings);
   3775   minfo.typestrings = NULL;
   3776 
   3777   if (minfo.args == NULL)
   3778     fprintf (stderr, _("no argument types in mangled string\n"));
   3779 
   3780   *pvarargs = minfo.varargs;
   3781   return minfo.args;
   3782 
   3783  error_return:
   3784   if (minfo.typestrings != NULL)
   3785     free (minfo.typestrings);
   3786   return NULL;
   3787 }
   3788 
   3789 /* Demangle the prefix of the mangled name.  */
   3790 
   3791 static bfd_boolean
   3792 stab_demangle_prefix (struct stab_demangle_info *minfo, const char **pp,
   3793 		      unsigned int physname_len)
   3794 {
   3795   const char *scan;
   3796   unsigned int i;
   3797 
   3798   /* cplus_demangle checks for global constructors and destructors,
   3799      but we can't see them in mangled argument types.  */
   3800 
   3801   if (physname_len)
   3802     scan = *pp + physname_len;
   3803   else
   3804     {
   3805       /* Look for `__'.  */
   3806       scan = *pp;
   3807       do
   3808 	scan = strchr (scan, '_');
   3809       while (scan != NULL && *++scan != '_');
   3810 
   3811       if (scan == NULL)
   3812 	{
   3813 	  stab_bad_demangle (*pp);
   3814 	  return FALSE;
   3815 	}
   3816 
   3817       --scan;
   3818 
   3819       /* We found `__'; move ahead to the last contiguous `__' pair.  */
   3820       i = strspn (scan, "_");
   3821       if (i > 2)
   3822 	scan += i - 2;
   3823     }
   3824 
   3825   if (scan == *pp
   3826       && (ISDIGIT (scan[2])
   3827 	  || scan[2] == 'Q'
   3828 	  || scan[2] == 't'))
   3829     {
   3830       /* This is a GNU style constructor name.  */
   3831       *pp = scan + 2;
   3832       return TRUE;
   3833     }
   3834   else if (scan == *pp
   3835 	   && ! ISDIGIT (scan[2])
   3836 	   && scan[2] != 't')
   3837     {
   3838       /* Look for the `__' that separates the prefix from the
   3839          signature.  */
   3840       while (*scan == '_')
   3841 	++scan;
   3842       scan = strstr (scan, "__");
   3843       if (scan == NULL || scan[2] == '\0')
   3844 	{
   3845 	  stab_bad_demangle (*pp);
   3846 	  return FALSE;
   3847 	}
   3848 
   3849       return stab_demangle_function_name (minfo, pp, scan);
   3850     }
   3851   else if (scan[2] != '\0')
   3852     {
   3853       /* The name doesn't start with `__', but it does contain `__'.  */
   3854       return stab_demangle_function_name (minfo, pp, scan);
   3855     }
   3856   else
   3857     {
   3858       stab_bad_demangle (*pp);
   3859       return FALSE;
   3860     }
   3861   /*NOTREACHED*/
   3862 }
   3863 
   3864 /* Demangle a function name prefix.  The scan argument points to the
   3865    double underscore which separates the function name from the
   3866    signature.  */
   3867 
   3868 static bfd_boolean
   3869 stab_demangle_function_name (struct stab_demangle_info *minfo,
   3870 			     const char **pp, const char *scan)
   3871 {
   3872   const char *name;
   3873 
   3874   /* The string from *pp to scan is the name of the function.  We
   3875      don't care about the name, since we just looking for argument
   3876      types.  However, for conversion operators, the name may include a
   3877      type which we must remember in order to handle backreferences.  */
   3878 
   3879   name = *pp;
   3880   *pp = scan + 2;
   3881 
   3882   if (*pp - name >= 5
   3883 	   && CONST_STRNEQ (name, "type")
   3884 	   && (name[4] == '$' || name[4] == '.'))
   3885     {
   3886       const char *tem;
   3887 
   3888       /* This is a type conversion operator.  */
   3889       tem = name + 5;
   3890       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
   3891 	return FALSE;
   3892     }
   3893   else if (name[0] == '_'
   3894 	   && name[1] == '_'
   3895 	   && name[2] == 'o'
   3896 	   && name[3] == 'p')
   3897     {
   3898       const char *tem;
   3899 
   3900       /* This is a type conversion operator.  */
   3901       tem = name + 4;
   3902       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
   3903 	return FALSE;
   3904     }
   3905 
   3906   return TRUE;
   3907 }
   3908 
   3909 /* Demangle the signature.  This is where the argument types are
   3910    found.  */
   3911 
   3912 static bfd_boolean
   3913 stab_demangle_signature (struct stab_demangle_info *minfo, const char **pp)
   3914 {
   3915   const char *orig;
   3916   bfd_boolean expect_func, func_done;
   3917   const char *hold;
   3918 
   3919   orig = *pp;
   3920 
   3921   expect_func = FALSE;
   3922   func_done = FALSE;
   3923   hold = NULL;
   3924 
   3925   while (**pp != '\0')
   3926     {
   3927       switch (**pp)
   3928 	{
   3929 	case 'Q':
   3930 	  hold = *pp;
   3931 	  if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
   3932 	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
   3933 	    return FALSE;
   3934 	  expect_func = TRUE;
   3935 	  hold = NULL;
   3936 	  break;
   3937 
   3938 	case 'S':
   3939 	  /* Static member function.  FIXME: Can this happen?  */
   3940 	  if (hold == NULL)
   3941 	    hold = *pp;
   3942 	  ++*pp;
   3943 	  break;
   3944 
   3945 	case 'C':
   3946 	  /* Const member function.  */
   3947 	  if (hold == NULL)
   3948 	    hold = *pp;
   3949 	  ++*pp;
   3950 	  break;
   3951 
   3952 	case '0': case '1': case '2': case '3': case '4':
   3953 	case '5': case '6': case '7': case '8': case '9':
   3954 	  if (hold == NULL)
   3955 	    hold = *pp;
   3956 	  if (! stab_demangle_class (minfo, pp, (const char **) NULL)
   3957 	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
   3958 	    return FALSE;
   3959 	  expect_func = TRUE;
   3960 	  hold = NULL;
   3961 	  break;
   3962 
   3963 	case 'F':
   3964 	  /* Function.  I don't know if this actually happens with g++
   3965              output.  */
   3966 	  hold = NULL;
   3967 	  func_done = TRUE;
   3968 	  ++*pp;
   3969 	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
   3970 	    return FALSE;
   3971 	  break;
   3972 
   3973 	case 't':
   3974 	  /* Template.  */
   3975 	  if (hold == NULL)
   3976 	    hold = *pp;
   3977 	  if (! stab_demangle_template (minfo, pp, (char **) NULL)
   3978 	      || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
   3979 	    return FALSE;
   3980 	  hold = NULL;
   3981 	  expect_func = TRUE;
   3982 	  break;
   3983 
   3984 	case '_':
   3985 	  /* At the outermost level, we cannot have a return type
   3986 	     specified, so if we run into another '_' at this point we
   3987 	     are dealing with a mangled name that is either bogus, or
   3988 	     has been mangled by some algorithm we don't know how to
   3989 	     deal with.  So just reject the entire demangling.  */
   3990 	  stab_bad_demangle (orig);
   3991 	  return FALSE;
   3992 
   3993 	default:
   3994 	  /* Assume we have stumbled onto the first outermost function
   3995 	     argument token, and start processing args.  */
   3996 	  func_done = TRUE;
   3997 	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
   3998 	    return FALSE;
   3999 	  break;
   4000 	}
   4001 
   4002       if (expect_func)
   4003 	{
   4004 	  func_done = TRUE;
   4005 	  if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
   4006 	    return FALSE;
   4007 	}
   4008     }
   4009 
   4010   if (! func_done)
   4011     {
   4012       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
   4013 	 bar__3fooi is 'foo::bar(int)'.  We get here when we find the
   4014 	 first case, and need to ensure that the '(void)' gets added
   4015 	 to the current declp.  */
   4016       if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
   4017 	return FALSE;
   4018     }
   4019 
   4020   return TRUE;
   4021 }
   4022 
   4023 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
   4024    mangled form of "Outer::Inner".  */
   4025 
   4026 static bfd_boolean
   4027 stab_demangle_qualified (struct stab_demangle_info *minfo, const char **pp,
   4028 			 debug_type *ptype)
   4029 {
   4030   const char *orig;
   4031   const char *p;
   4032   unsigned int qualifiers;
   4033   debug_type context;
   4034 
   4035   orig = *pp;
   4036 
   4037   switch ((*pp)[1])
   4038     {
   4039     case '_':
   4040       /* GNU mangled name with more than 9 classes.  The count is
   4041 	 preceded by an underscore (to distinguish it from the <= 9
   4042 	 case) and followed by an underscore.  */
   4043       p = *pp + 2;
   4044       if (! ISDIGIT (*p) || *p == '0')
   4045 	{
   4046 	  stab_bad_demangle (orig);
   4047 	  return FALSE;
   4048 	}
   4049       qualifiers = atoi (p);
   4050       while (ISDIGIT (*p))
   4051 	++p;
   4052       if (*p != '_')
   4053 	{
   4054 	  stab_bad_demangle (orig);
   4055 	  return FALSE;
   4056 	}
   4057       *pp = p + 1;
   4058       break;
   4059 
   4060     case '1': case '2': case '3': case '4': case '5':
   4061     case '6': case '7': case '8': case '9':
   4062       qualifiers = (*pp)[1] - '0';
   4063       /* Skip an optional underscore after the count.  */
   4064       if ((*pp)[2] == '_')
   4065 	++*pp;
   4066       *pp += 2;
   4067       break;
   4068 
   4069     case '0':
   4070     default:
   4071       stab_bad_demangle (orig);
   4072       return FALSE;
   4073     }
   4074 
   4075   context = DEBUG_TYPE_NULL;
   4076 
   4077   /* Pick off the names.  */
   4078   while (qualifiers-- > 0)
   4079     {
   4080       if (**pp == '_')
   4081 	++*pp;
   4082       if (**pp == 't')
   4083 	{
   4084 	  char *name;
   4085 
   4086 	  if (! stab_demangle_template (minfo, pp,
   4087 					ptype != NULL ? &name : NULL))
   4088 	    return FALSE;
   4089 
   4090 	  if (ptype != NULL)
   4091 	    {
   4092 	      context = stab_find_tagged_type (minfo->dhandle, minfo->info,
   4093 					       name, strlen (name),
   4094 					       DEBUG_KIND_CLASS);
   4095 	      free (name);
   4096 	      if (context == DEBUG_TYPE_NULL)
   4097 		return FALSE;
   4098 	    }
   4099 	}
   4100       else
   4101 	{
   4102 	  unsigned int len;
   4103 
   4104 	  len = stab_demangle_count (pp);
   4105 	  if (strlen (*pp) < len)
   4106 	    {
   4107 	      stab_bad_demangle (orig);
   4108 	      return FALSE;
   4109 	    }
   4110 
   4111 	  if (ptype != NULL)
   4112 	    {
   4113 	      const debug_field *fields;
   4114 
   4115 	      fields = NULL;
   4116 	      if (context != DEBUG_TYPE_NULL)
   4117 		fields = debug_get_fields (minfo->dhandle, context);
   4118 
   4119 	      context = DEBUG_TYPE_NULL;
   4120 
   4121 	      if (fields != NULL)
   4122 		{
   4123 		  char *name;
   4124 
   4125 		  /* Try to find the type by looking through the
   4126                      fields of context until we find a field with the
   4127                      same type.  This ought to work for a class
   4128                      defined within a class, but it won't work for,
   4129                      e.g., an enum defined within a class.  stabs does
   4130                      not give us enough information to figure out the
   4131                      latter case.  */
   4132 
   4133 		  name = savestring (*pp, len);
   4134 
   4135 		  for (; *fields != DEBUG_FIELD_NULL; fields++)
   4136 		    {
   4137 		      debug_type ft;
   4138 		      const char *dn;
   4139 
   4140 		      ft = debug_get_field_type (minfo->dhandle, *fields);
   4141 		      if (ft == NULL)
   4142 			{
   4143 			  free (name);
   4144 			  return FALSE;
   4145 			}
   4146 		      dn = debug_get_type_name (minfo->dhandle, ft);
   4147 		      if (dn != NULL && strcmp (dn, name) == 0)
   4148 			{
   4149 			  context = ft;
   4150 			  break;
   4151 			}
   4152 		    }
   4153 
   4154 		  free (name);
   4155 		}
   4156 
   4157 	      if (context == DEBUG_TYPE_NULL)
   4158 		{
   4159 		  /* We have to fall back on finding the type by name.
   4160                      If there are more types to come, then this must
   4161                      be a class.  Otherwise, it could be anything.  */
   4162 
   4163 		  if (qualifiers == 0)
   4164 		    {
   4165 		      char *name;
   4166 
   4167 		      name = savestring (*pp, len);
   4168 		      context = debug_find_named_type (minfo->dhandle,
   4169 						       name);
   4170 		      free (name);
   4171 		    }
   4172 
   4173 		  if (context == DEBUG_TYPE_NULL)
   4174 		    {
   4175 		      context = stab_find_tagged_type (minfo->dhandle,
   4176 						       minfo->info,
   4177 						       *pp, len,
   4178 						       (qualifiers == 0
   4179 							? DEBUG_KIND_ILLEGAL
   4180 							: DEBUG_KIND_CLASS));
   4181 		      if (context == DEBUG_TYPE_NULL)
   4182 			return FALSE;
   4183 		    }
   4184 		}
   4185 	    }
   4186 
   4187 	  *pp += len;
   4188 	}
   4189     }
   4190 
   4191   if (ptype != NULL)
   4192     *ptype = context;
   4193 
   4194   return TRUE;
   4195 }
   4196 
   4197 /* Demangle a template.  If PNAME is not NULL, this sets *PNAME to a
   4198    string representation of the template.  */
   4199 
   4200 static bfd_boolean
   4201 stab_demangle_template (struct stab_demangle_info *minfo, const char **pp,
   4202 			char **pname)
   4203 {
   4204   const char *orig;
   4205   unsigned int r, i;
   4206 
   4207   orig = *pp;
   4208 
   4209   ++*pp;
   4210 
   4211   /* Skip the template name.  */
   4212   r = stab_demangle_count (pp);
   4213   if (r == 0 || strlen (*pp) < r)
   4214     {
   4215       stab_bad_demangle (orig);
   4216       return FALSE;
   4217     }
   4218   *pp += r;
   4219 
   4220   /* Get the size of the parameter list.  */
   4221   if (stab_demangle_get_count (pp, &r) == 0)
   4222     {
   4223       stab_bad_demangle (orig);
   4224       return FALSE;
   4225     }
   4226 
   4227   for (i = 0; i < r; i++)
   4228     {
   4229       if (**pp == 'Z')
   4230 	{
   4231 	  /* This is a type parameter.  */
   4232 	  ++*pp;
   4233 	  if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
   4234 	    return FALSE;
   4235 	}
   4236       else
   4237 	{
   4238 	  const char *old_p;
   4239 	  bfd_boolean pointerp, realp, integralp, charp, boolp;
   4240 	  bfd_boolean done;
   4241 
   4242 	  old_p = *pp;
   4243 	  pointerp = FALSE;
   4244 	  realp = FALSE;
   4245 	  integralp = FALSE;
   4246 	  charp = FALSE;
   4247 	  boolp = FALSE;
   4248 	  done = FALSE;
   4249 
   4250 	  /* This is a value parameter.  */
   4251 
   4252 	  if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
   4253 	    return FALSE;
   4254 
   4255 	  while (*old_p != '\0' && ! done)
   4256 	    {
   4257 	      switch (*old_p)
   4258 		{
   4259 		case 'P':
   4260 		case 'p':
   4261 		case 'R':
   4262 		  pointerp = TRUE;
   4263 		  done = TRUE;
   4264 		  break;
   4265 		case 'C':	/* Const.  */
   4266 		case 'S':	/* Signed.  */
   4267 		case 'U':	/* Unsigned.  */
   4268 		case 'V':	/* Volatile.  */
   4269 		case 'F':	/* Function.  */
   4270 		case 'M':	/* Member function.  */
   4271 		case 'O':	/* ??? */
   4272 		  ++old_p;
   4273 		  break;
   4274 		case 'Q':	/* Qualified name.  */
   4275 		  integralp = TRUE;
   4276 		  done = TRUE;
   4277 		  break;
   4278 		case 'T':	/* Remembered type.  */
   4279 		  abort ();
   4280 		case 'v':	/* Void.  */
   4281 		  abort ();
   4282 		case 'x':	/* Long long.  */
   4283 		case 'l':	/* Long.  */
   4284 		case 'i':	/* Int.  */
   4285 		case 's':	/* Short.  */
   4286 		case 'w':	/* Wchar_t.  */
   4287 		  integralp = TRUE;
   4288 		  done = TRUE;
   4289 		  break;
   4290 		case 'b':	/* Bool.  */
   4291 		  boolp = TRUE;
   4292 		  done = TRUE;
   4293 		  break;
   4294 		case 'c':	/* Char.  */
   4295 		  charp = TRUE;
   4296 		  done = TRUE;
   4297 		  break;
   4298 		case 'r':	/* Long double.  */
   4299 		case 'd':	/* Double.  */
   4300 		case 'f':	/* Float.  */
   4301 		  realp = TRUE;
   4302 		  done = TRUE;
   4303 		  break;
   4304 		default:
   4305 		  /* Assume it's a user defined integral type.  */
   4306 		  integralp = TRUE;
   4307 		  done = TRUE;
   4308 		  break;
   4309 		}
   4310 	    }
   4311 
   4312 	  if (integralp)
   4313 	    {
   4314 	      if (**pp == 'm')
   4315 		++*pp;
   4316 	      while (ISDIGIT (**pp))
   4317 		++*pp;
   4318 	    }
   4319 	  else if (charp)
   4320 	    {
   4321 	      unsigned int val;
   4322 
   4323 	      if (**pp == 'm')
   4324 		++*pp;
   4325 	      val = stab_demangle_count (pp);
   4326 	      if (val == 0)
   4327 		{
   4328 		  stab_bad_demangle (orig);
   4329 		  return FALSE;
   4330 		}
   4331 	    }
   4332 	  else if (boolp)
   4333 	    {
   4334 	      unsigned int val;
   4335 
   4336 	      val = stab_demangle_count (pp);
   4337 	      if (val != 0 && val != 1)
   4338 		{
   4339 		  stab_bad_demangle (orig);
   4340 		  return FALSE;
   4341 		}
   4342 	    }
   4343 	  else if (realp)
   4344 	    {
   4345 	      if (**pp == 'm')
   4346 		++*pp;
   4347 	      while (ISDIGIT (**pp))
   4348 		++*pp;
   4349 	      if (**pp == '.')
   4350 		{
   4351 		  ++*pp;
   4352 		  while (ISDIGIT (**pp))
   4353 		    ++*pp;
   4354 		}
   4355 	      if (**pp == 'e')
   4356 		{
   4357 		  ++*pp;
   4358 		  while (ISDIGIT (**pp))
   4359 		    ++*pp;
   4360 		}
   4361 	    }
   4362 	  else if (pointerp)
   4363 	    {
   4364 	      unsigned int len;
   4365 
   4366 	      len = stab_demangle_count (pp);
   4367 	      if (len == 0)
   4368 		{
   4369 		  stab_bad_demangle (orig);
   4370 		  return FALSE;
   4371 		}
   4372 	      *pp += len;
   4373 	    }
   4374 	}
   4375     }
   4376 
   4377   /* We can translate this to a string fairly easily by invoking the
   4378      regular demangling routine.  */
   4379   if (pname != NULL)
   4380     {
   4381       char *s1, *s2, *s3, *s4 = NULL;
   4382       char *from, *to;
   4383 
   4384       s1 = savestring (orig, *pp - orig);
   4385 
   4386       s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
   4387 
   4388       free (s1);
   4389 
   4390       s3 = cplus_demangle (s2, DMGL_ANSI);
   4391 
   4392       free (s2);
   4393 
   4394       if (s3 != NULL)
   4395 	s4 = strstr (s3, "::NoSuchStrinG");
   4396       if (s3 == NULL || s4 == NULL)
   4397 	{
   4398 	  stab_bad_demangle (orig);
   4399 	  if (s3 != NULL)
   4400 	    free (s3);
   4401 	  return FALSE;
   4402 	}
   4403 
   4404       /* Eliminating all spaces, except those between > characters,
   4405          makes it more likely that the demangled name will match the
   4406          name which g++ used as the structure name.  */
   4407       for (from = to = s3; from != s4; ++from)
   4408 	if (*from != ' '
   4409 	    || (from[1] == '>' && from > s3 && from[-1] == '>'))
   4410 	  *to++ = *from;
   4411 
   4412       *pname = savestring (s3, to - s3);
   4413 
   4414       free (s3);
   4415     }
   4416 
   4417   return TRUE;
   4418 }
   4419 
   4420 /* Demangle a class name.  */
   4421 
   4422 static bfd_boolean
   4423 stab_demangle_class (struct stab_demangle_info *minfo ATTRIBUTE_UNUSED,
   4424 		     const char **pp, const char **pstart)
   4425 {
   4426   const char *orig;
   4427   unsigned int n;
   4428 
   4429   orig = *pp;
   4430 
   4431   n = stab_demangle_count (pp);
   4432   if (strlen (*pp) < n)
   4433     {
   4434       stab_bad_demangle (orig);
   4435       return FALSE;
   4436     }
   4437 
   4438   if (pstart != NULL)
   4439     *pstart = *pp;
   4440 
   4441   *pp += n;
   4442 
   4443   return TRUE;
   4444 }
   4445 
   4446 /* Demangle function arguments.  If the pargs argument is not NULL, it
   4447    is set to a NULL terminated array holding the arguments.  */
   4448 
   4449 static bfd_boolean
   4450 stab_demangle_args (struct stab_demangle_info *minfo, const char **pp,
   4451 		    debug_type **pargs, bfd_boolean *pvarargs)
   4452 {
   4453   const char *orig;
   4454   unsigned int alloc, count;
   4455 
   4456   orig = *pp;
   4457 
   4458   alloc = 10;
   4459   if (pargs != NULL)
   4460     {
   4461       *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
   4462       *pvarargs = FALSE;
   4463     }
   4464   count = 0;
   4465 
   4466   while (**pp != '_' && **pp != '\0' && **pp != 'e')
   4467     {
   4468       if (**pp == 'N' || **pp == 'T')
   4469 	{
   4470 	  char temptype;
   4471 	  unsigned int r, t;
   4472 
   4473 	  temptype = **pp;
   4474 	  ++*pp;
   4475 
   4476 	  if (temptype == 'T')
   4477 	    r = 1;
   4478 	  else
   4479 	    {
   4480 	      if (! stab_demangle_get_count (pp, &r))
   4481 		{
   4482 		  stab_bad_demangle (orig);
   4483 		  return FALSE;
   4484 		}
   4485 	    }
   4486 
   4487 	  if (! stab_demangle_get_count (pp, &t))
   4488 	    {
   4489 	      stab_bad_demangle (orig);
   4490 	      return FALSE;
   4491 	    }
   4492 
   4493 	  if (t >= minfo->typestring_count)
   4494 	    {
   4495 	      stab_bad_demangle (orig);
   4496 	      return FALSE;
   4497 	    }
   4498 	  while (r-- > 0)
   4499 	    {
   4500 	      const char *tem;
   4501 
   4502 	      tem = minfo->typestrings[t].typestring;
   4503 	      if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
   4504 		return FALSE;
   4505 	    }
   4506 	}
   4507       else
   4508 	{
   4509 	  if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
   4510 	    return FALSE;
   4511 	}
   4512     }
   4513 
   4514   if (pargs != NULL)
   4515     (*pargs)[count] = DEBUG_TYPE_NULL;
   4516 
   4517   if (**pp == 'e')
   4518     {
   4519       if (pargs != NULL)
   4520 	*pvarargs = TRUE;
   4521       ++*pp;
   4522     }
   4523 
   4524   return TRUE;
   4525 }
   4526 
   4527 /* Demangle a single argument.  */
   4528 
   4529 static bfd_boolean
   4530 stab_demangle_arg (struct stab_demangle_info *minfo, const char **pp,
   4531 		   debug_type **pargs, unsigned int *pcount,
   4532 		   unsigned int *palloc)
   4533 {
   4534   const char *start;
   4535   debug_type type;
   4536 
   4537   start = *pp;
   4538   if (! stab_demangle_type (minfo, pp,
   4539 			    pargs == NULL ? (debug_type *) NULL : &type)
   4540       || ! stab_demangle_remember_type (minfo, start, *pp - start))
   4541     return FALSE;
   4542 
   4543   if (pargs != NULL)
   4544     {
   4545       if (type == DEBUG_TYPE_NULL)
   4546 	return FALSE;
   4547 
   4548       if (*pcount + 1 >= *palloc)
   4549 	{
   4550 	  *palloc += 10;
   4551 	  *pargs = ((debug_type *)
   4552 		    xrealloc (*pargs, *palloc * sizeof **pargs));
   4553 	}
   4554       (*pargs)[*pcount] = type;
   4555       ++*pcount;
   4556     }
   4557 
   4558   return TRUE;
   4559 }
   4560 
   4561 /* Demangle a type.  If the ptype argument is not NULL, *ptype is set
   4562    to the newly allocated type.  */
   4563 
   4564 static bfd_boolean
   4565 stab_demangle_type (struct stab_demangle_info *minfo, const char **pp,
   4566 		    debug_type *ptype)
   4567 {
   4568   const char *orig;
   4569 
   4570   orig = *pp;
   4571 
   4572   switch (**pp)
   4573     {
   4574     case 'P':
   4575     case 'p':
   4576       /* A pointer type.  */
   4577       ++*pp;
   4578       if (! stab_demangle_type (minfo, pp, ptype))
   4579 	return FALSE;
   4580       if (ptype != NULL)
   4581 	*ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
   4582       break;
   4583 
   4584     case 'R':
   4585       /* A reference type.  */
   4586       ++*pp;
   4587       if (! stab_demangle_type (minfo, pp, ptype))
   4588 	return FALSE;
   4589       if (ptype != NULL)
   4590 	*ptype = debug_make_reference_type (minfo->dhandle, *ptype);
   4591       break;
   4592 
   4593     case 'A':
   4594       /* An array.  */
   4595       {
   4596 	unsigned long high;
   4597 
   4598 	++*pp;
   4599 	high = 0;
   4600 	while (**pp != '\0' && **pp != '_')
   4601 	  {
   4602 	    if (! ISDIGIT (**pp))
   4603 	      {
   4604 		stab_bad_demangle (orig);
   4605 		return FALSE;
   4606 	      }
   4607 	    high *= 10;
   4608 	    high += **pp - '0';
   4609 	    ++*pp;
   4610 	  }
   4611 	if (**pp != '_')
   4612 	  {
   4613 	    stab_bad_demangle (orig);
   4614 	    return FALSE;
   4615 	  }
   4616 	++*pp;
   4617 
   4618 	if (! stab_demangle_type (minfo, pp, ptype))
   4619 	  return FALSE;
   4620 	if (ptype != NULL)
   4621 	  {
   4622 	    debug_type int_type;
   4623 
   4624 	    int_type = debug_find_named_type (minfo->dhandle, "int");
   4625 	    if (int_type == NULL)
   4626 	      int_type = debug_make_int_type (minfo->dhandle, 4, FALSE);
   4627 	    *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
   4628 					    0, high, FALSE);
   4629 	  }
   4630       }
   4631       break;
   4632 
   4633     case 'T':
   4634       /* A back reference to a remembered type.  */
   4635       {
   4636 	unsigned int i;
   4637 	const char *p;
   4638 
   4639 	++*pp;
   4640 	if (! stab_demangle_get_count (pp, &i))
   4641 	  {
   4642 	    stab_bad_demangle (orig);
   4643 	    return FALSE;
   4644 	  }
   4645 	if (i >= minfo->typestring_count)
   4646 	  {
   4647 	    stab_bad_demangle (orig);
   4648 	    return FALSE;
   4649 	  }
   4650 	p = minfo->typestrings[i].typestring;
   4651 	if (! stab_demangle_type (minfo, &p, ptype))
   4652 	  return FALSE;
   4653       }
   4654       break;
   4655 
   4656     case 'F':
   4657       /* A function.  */
   4658       {
   4659 	debug_type *args;
   4660 	bfd_boolean varargs;
   4661 
   4662 	++*pp;
   4663 	if (! stab_demangle_args (minfo, pp,
   4664 				  (ptype == NULL
   4665 				   ? (debug_type **) NULL
   4666 				   : &args),
   4667 				  (ptype == NULL
   4668 				   ? (bfd_boolean *) NULL
   4669 				   : &varargs)))
   4670 	  return FALSE;
   4671 	if (**pp != '_')
   4672 	  {
   4673 	    /* cplus_demangle will accept a function without a return
   4674 	       type, but I don't know when that will happen, or what
   4675 	       to do if it does.  */
   4676 	    stab_bad_demangle (orig);
   4677 	    return FALSE;
   4678 	  }
   4679 	++*pp;
   4680 	if (! stab_demangle_type (minfo, pp, ptype))
   4681 	  return FALSE;
   4682 	if (ptype != NULL)
   4683 	  *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
   4684 					     varargs);
   4685 
   4686       }
   4687       break;
   4688 
   4689     case 'M':
   4690     case 'O':
   4691       {
   4692 	bfd_boolean memberp;
   4693 	debug_type class_type = DEBUG_TYPE_NULL;
   4694 	debug_type *args;
   4695 	bfd_boolean varargs;
   4696 	unsigned int n;
   4697 	const char *name;
   4698 
   4699 	memberp = **pp == 'M';
   4700 	args = NULL;
   4701 	varargs = FALSE;
   4702 
   4703 	++*pp;
   4704 	if (ISDIGIT (**pp))
   4705 	  {
   4706 	    n = stab_demangle_count (pp);
   4707 	    if (strlen (*pp) < n)
   4708 	      {
   4709 		stab_bad_demangle (orig);
   4710 		return FALSE;
   4711 	      }
   4712 	    name = *pp;
   4713 	    *pp += n;
   4714 
   4715 	    if (ptype != NULL)
   4716 	      {
   4717 		class_type = stab_find_tagged_type (minfo->dhandle,
   4718 						    minfo->info,
   4719 						    name, (int) n,
   4720 						    DEBUG_KIND_CLASS);
   4721 		if (class_type == DEBUG_TYPE_NULL)
   4722 		  return FALSE;
   4723 	      }
   4724 	  }
   4725 	else if (**pp == 'Q')
   4726 	  {
   4727 	    if (! stab_demangle_qualified (minfo, pp,
   4728 					   (ptype == NULL
   4729 					    ? (debug_type *) NULL
   4730 					    : &class_type)))
   4731 	      return FALSE;
   4732 	  }
   4733 	else
   4734 	  {
   4735 	    stab_bad_demangle (orig);
   4736 	    return FALSE;
   4737 	  }
   4738 
   4739 	if (memberp)
   4740 	  {
   4741 	    if (**pp == 'C')
   4742 	      {
   4743 		++*pp;
   4744 	      }
   4745 	    else if (**pp == 'V')
   4746 	      {
   4747 		++*pp;
   4748 	      }
   4749 	    if (**pp != 'F')
   4750 	      {
   4751 		stab_bad_demangle (orig);
   4752 		return FALSE;
   4753 	      }
   4754 	    ++*pp;
   4755 	    if (! stab_demangle_args (minfo, pp,
   4756 				      (ptype == NULL
   4757 				       ? (debug_type **) NULL
   4758 				       : &args),
   4759 				      (ptype == NULL
   4760 				       ? (bfd_boolean *) NULL
   4761 				       : &varargs)))
   4762 	      return FALSE;
   4763 	  }
   4764 
   4765 	if (**pp != '_')
   4766 	  {
   4767 	    stab_bad_demangle (orig);
   4768 	    return FALSE;
   4769 	  }
   4770 	++*pp;
   4771 
   4772 	if (! stab_demangle_type (minfo, pp, ptype))
   4773 	  return FALSE;
   4774 
   4775 	if (ptype != NULL)
   4776 	  {
   4777 	    if (! memberp)
   4778 	      *ptype = debug_make_offset_type (minfo->dhandle, class_type,
   4779 					       *ptype);
   4780 	    else
   4781 	      {
   4782 		/* FIXME: We have no way to record constp or
   4783                    volatilep.  */
   4784 		*ptype = debug_make_method_type (minfo->dhandle, *ptype,
   4785 						 class_type, args, varargs);
   4786 	      }
   4787 	  }
   4788       }
   4789       break;
   4790 
   4791     case 'G':
   4792       ++*pp;
   4793       if (! stab_demangle_type (minfo, pp, ptype))
   4794 	return FALSE;
   4795       break;
   4796 
   4797     case 'C':
   4798       ++*pp;
   4799       if (! stab_demangle_type (minfo, pp, ptype))
   4800 	return FALSE;
   4801       if (ptype != NULL)
   4802 	*ptype = debug_make_const_type (minfo->dhandle, *ptype);
   4803       break;
   4804 
   4805     case 'Q':
   4806       {
   4807 	if (! stab_demangle_qualified (minfo, pp, ptype))
   4808 	  return FALSE;
   4809       }
   4810       break;
   4811 
   4812     default:
   4813       if (! stab_demangle_fund_type (minfo, pp, ptype))
   4814 	return FALSE;
   4815       break;
   4816     }
   4817 
   4818   return TRUE;
   4819 }
   4820 
   4821 /* Demangle a fundamental type.  If the ptype argument is not NULL,
   4822    *ptype is set to the newly allocated type.  */
   4823 
   4824 static bfd_boolean
   4825 stab_demangle_fund_type (struct stab_demangle_info *minfo, const char **pp,
   4826 			 debug_type *ptype)
   4827 {
   4828   const char *orig;
   4829   bfd_boolean constp, volatilep, unsignedp, signedp;
   4830   bfd_boolean done;
   4831 
   4832   orig = *pp;
   4833 
   4834   constp = FALSE;
   4835   volatilep = FALSE;
   4836   unsignedp = FALSE;
   4837   signedp = FALSE;
   4838 
   4839   done = FALSE;
   4840   while (! done)
   4841     {
   4842       switch (**pp)
   4843 	{
   4844 	case 'C':
   4845 	  constp = TRUE;
   4846 	  ++*pp;
   4847 	  break;
   4848 
   4849 	case 'U':
   4850 	  unsignedp = TRUE;
   4851 	  ++*pp;
   4852 	  break;
   4853 
   4854 	case 'S':
   4855 	  signedp = TRUE;
   4856 	  ++*pp;
   4857 	  break;
   4858 
   4859 	case 'V':
   4860 	  volatilep = TRUE;
   4861 	  ++*pp;
   4862 	  break;
   4863 
   4864 	default:
   4865 	  done = TRUE;
   4866 	  break;
   4867 	}
   4868     }
   4869 
   4870   switch (**pp)
   4871     {
   4872     case '\0':
   4873     case '_':
   4874       /* cplus_demangle permits this, but I don't know what it means.  */
   4875       stab_bad_demangle (orig);
   4876       break;
   4877 
   4878     case 'v': /* void */
   4879       if (ptype != NULL)
   4880 	{
   4881 	  *ptype = debug_find_named_type (minfo->dhandle, "void");
   4882 	  if (*ptype == DEBUG_TYPE_NULL)
   4883 	    *ptype = debug_make_void_type (minfo->dhandle);
   4884 	}
   4885       ++*pp;
   4886       break;
   4887 
   4888     case 'x': /* long long */
   4889       if (ptype != NULL)
   4890 	{
   4891 	  *ptype = debug_find_named_type (minfo->dhandle,
   4892 					  (unsignedp
   4893 					   ? "long long unsigned int"
   4894 					   : "long long int"));
   4895 	  if (*ptype == DEBUG_TYPE_NULL)
   4896 	    *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
   4897 	}
   4898       ++*pp;
   4899       break;
   4900 
   4901     case 'l': /* long */
   4902       if (ptype != NULL)
   4903 	{
   4904 	  *ptype = debug_find_named_type (minfo->dhandle,
   4905 					  (unsignedp
   4906 					   ? "long unsigned int"
   4907 					   : "long int"));
   4908 	  if (*ptype == DEBUG_TYPE_NULL)
   4909 	    *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
   4910 	}
   4911       ++*pp;
   4912       break;
   4913 
   4914     case 'i': /* int */
   4915       if (ptype != NULL)
   4916 	{
   4917 	  *ptype = debug_find_named_type (minfo->dhandle,
   4918 					  (unsignedp
   4919 					   ? "unsigned int"
   4920 					   : "int"));
   4921 	  if (*ptype == DEBUG_TYPE_NULL)
   4922 	    *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
   4923 	}
   4924       ++*pp;
   4925       break;
   4926 
   4927     case 's': /* short */
   4928       if (ptype != NULL)
   4929 	{
   4930 	  *ptype = debug_find_named_type (minfo->dhandle,
   4931 					  (unsignedp
   4932 					   ? "short unsigned int"
   4933 					   : "short int"));
   4934 	  if (*ptype == DEBUG_TYPE_NULL)
   4935 	    *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
   4936 	}
   4937       ++*pp;
   4938       break;
   4939 
   4940     case 'b': /* bool */
   4941       if (ptype != NULL)
   4942 	{
   4943 	  *ptype = debug_find_named_type (minfo->dhandle, "bool");
   4944 	  if (*ptype == DEBUG_TYPE_NULL)
   4945 	    *ptype = debug_make_bool_type (minfo->dhandle, 4);
   4946 	}
   4947       ++*pp;
   4948       break;
   4949 
   4950     case 'c': /* char */
   4951       if (ptype != NULL)
   4952 	{
   4953 	  *ptype = debug_find_named_type (minfo->dhandle,
   4954 					  (unsignedp
   4955 					   ? "unsigned char"
   4956 					   : (signedp
   4957 					      ? "signed char"
   4958 					      : "char")));
   4959 	  if (*ptype == DEBUG_TYPE_NULL)
   4960 	    *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
   4961 	}
   4962       ++*pp;
   4963       break;
   4964 
   4965     case 'w': /* wchar_t */
   4966       if (ptype != NULL)
   4967 	{
   4968 	  *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
   4969 	  if (*ptype == DEBUG_TYPE_NULL)
   4970 	    *ptype = debug_make_int_type (minfo->dhandle, 2, TRUE);
   4971 	}
   4972       ++*pp;
   4973       break;
   4974 
   4975     case 'r': /* long double */
   4976       if (ptype != NULL)
   4977 	{
   4978 	  *ptype = debug_find_named_type (minfo->dhandle, "long double");
   4979 	  if (*ptype == DEBUG_TYPE_NULL)
   4980 	    *ptype = debug_make_float_type (minfo->dhandle, 8);
   4981 	}
   4982       ++*pp;
   4983       break;
   4984 
   4985     case 'd': /* double */
   4986       if (ptype != NULL)
   4987 	{
   4988 	  *ptype = debug_find_named_type (minfo->dhandle, "double");
   4989 	  if (*ptype == DEBUG_TYPE_NULL)
   4990 	    *ptype = debug_make_float_type (minfo->dhandle, 8);
   4991 	}
   4992       ++*pp;
   4993       break;
   4994 
   4995     case 'f': /* float */
   4996       if (ptype != NULL)
   4997 	{
   4998 	  *ptype = debug_find_named_type (minfo->dhandle, "float");
   4999 	  if (*ptype == DEBUG_TYPE_NULL)
   5000 	    *ptype = debug_make_float_type (minfo->dhandle, 4);
   5001 	}
   5002       ++*pp;
   5003       break;
   5004 
   5005     case 'G':
   5006       ++*pp;
   5007       if (! ISDIGIT (**pp))
   5008 	{
   5009 	  stab_bad_demangle (orig);
   5010 	  return FALSE;
   5011 	}
   5012       /* Fall through.  */
   5013     case '0': case '1': case '2': case '3': case '4':
   5014     case '5': case '6': case '7': case '8': case '9':
   5015       {
   5016 	const char *hold;
   5017 
   5018 	if (! stab_demangle_class (minfo, pp, &hold))
   5019 	  return FALSE;
   5020 	if (ptype != NULL)
   5021 	  {
   5022 	    char *name;
   5023 
   5024 	    name = savestring (hold, *pp - hold);
   5025 	    *ptype = debug_find_named_type (minfo->dhandle, name);
   5026 	    free (name);
   5027 	    if (*ptype == DEBUG_TYPE_NULL)
   5028 	      {
   5029 		/* FIXME: It is probably incorrect to assume that
   5030                    undefined types are tagged types.  */
   5031 		*ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
   5032 						hold, *pp - hold,
   5033 						DEBUG_KIND_ILLEGAL);
   5034 		if (*ptype == DEBUG_TYPE_NULL)
   5035 		  return FALSE;
   5036 	      }
   5037 	  }
   5038       }
   5039       break;
   5040 
   5041     case 't':
   5042       {
   5043 	char *name;
   5044 
   5045 	if (! stab_demangle_template (minfo, pp,
   5046 				      ptype != NULL ? &name : NULL))
   5047 	  return FALSE;
   5048 	if (ptype != NULL)
   5049 	  {
   5050 	    *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
   5051 					    name, strlen (name),
   5052 					    DEBUG_KIND_CLASS);
   5053 	    free (name);
   5054 	    if (*ptype == DEBUG_TYPE_NULL)
   5055 	      return FALSE;
   5056 	  }
   5057       }
   5058       break;
   5059 
   5060     default:
   5061       stab_bad_demangle (orig);
   5062       return FALSE;
   5063     }
   5064 
   5065   if (ptype != NULL)
   5066     {
   5067       if (constp)
   5068 	*ptype = debug_make_const_type (minfo->dhandle, *ptype);
   5069       if (volatilep)
   5070 	*ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
   5071     }
   5072 
   5073   return TRUE;
   5074 }
   5075 
   5076 /* Remember a type string in a demangled string.  */
   5077 
   5078 static bfd_boolean
   5079 stab_demangle_remember_type (struct stab_demangle_info *minfo,
   5080 			     const char *p, int len)
   5081 {
   5082   if (minfo->typestring_count >= minfo->typestring_alloc)
   5083     {
   5084       minfo->typestring_alloc += 10;
   5085       minfo->typestrings = ((struct stab_demangle_typestring *)
   5086 			    xrealloc (minfo->typestrings,
   5087 				      (minfo->typestring_alloc
   5088 				       * sizeof *minfo->typestrings)));
   5089     }
   5090 
   5091   minfo->typestrings[minfo->typestring_count].typestring = p;
   5092   minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
   5093   ++minfo->typestring_count;
   5094 
   5095   return TRUE;
   5096 }
   5097 
   5098 /* Demangle names encoded using the g++ V3 ABI.  The newer versions of
   5100    g++ which use this ABI do not encode ordinary method argument types
   5101    in a mangled name; they simply output the argument types.  However,
   5102    for a static method, g++ simply outputs the return type and the
   5103    physical name.  So in that case we need to demangle the name here.
   5104    Here PHYSNAME is the physical name of the function, and we set the
   5105    variable pointed at by PVARARGS to indicate whether this function
   5106    is varargs.  This returns NULL, or a NULL terminated array of
   5107    argument types.  */
   5108 
   5109 static debug_type *
   5110 stab_demangle_v3_argtypes (void *dhandle, struct stab_handle *info,
   5111 			   const char *physname, bfd_boolean *pvarargs)
   5112 {
   5113   struct demangle_component *dc;
   5114   void *mem;
   5115   debug_type *pargs;
   5116 
   5117   dc = cplus_demangle_v3_components (physname, DMGL_PARAMS | DMGL_ANSI, &mem);
   5118   if (dc == NULL)
   5119     {
   5120       stab_bad_demangle (physname);
   5121       return NULL;
   5122     }
   5123 
   5124   /* We expect to see TYPED_NAME, and the right subtree describes the
   5125      function type.  */
   5126   if (dc->type != DEMANGLE_COMPONENT_TYPED_NAME
   5127       || dc->u.s_binary.right->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
   5128     {
   5129       fprintf (stderr, _("Demangled name is not a function\n"));
   5130       free (mem);
   5131       return NULL;
   5132     }
   5133 
   5134   pargs = stab_demangle_v3_arglist (dhandle, info,
   5135 				    dc->u.s_binary.right->u.s_binary.right,
   5136 				    pvarargs);
   5137 
   5138   free (mem);
   5139 
   5140   return pargs;
   5141 }
   5142 
   5143 /* Demangle an argument list in a struct demangle_component tree.
   5144    Returns a DEBUG_TYPE_NULL terminated array of argument types, and
   5145    sets *PVARARGS to indicate whether this is a varargs function.  */
   5146 
   5147 static debug_type *
   5148 stab_demangle_v3_arglist (void *dhandle, struct stab_handle *info,
   5149 			  struct demangle_component *arglist,
   5150 			  bfd_boolean *pvarargs)
   5151 {
   5152   struct demangle_component *dc;
   5153   unsigned int alloc, count;
   5154   debug_type *pargs;
   5155 
   5156   alloc = 10;
   5157   pargs = (debug_type *) xmalloc (alloc * sizeof *pargs);
   5158   *pvarargs = FALSE;
   5159 
   5160   count = 0;
   5161 
   5162   for (dc = arglist;
   5163        dc != NULL;
   5164        dc = dc->u.s_binary.right)
   5165     {
   5166       debug_type arg;
   5167       bfd_boolean varargs;
   5168 
   5169       if (dc->type != DEMANGLE_COMPONENT_ARGLIST)
   5170 	{
   5171 	  fprintf (stderr, _("Unexpected type in v3 arglist demangling\n"));
   5172 	  free (pargs);
   5173 	  return NULL;
   5174 	}
   5175 
   5176       /* PR 13925: Cope if the demangler returns an empty
   5177 	 context for a function with no arguments.  */
   5178       if (dc->u.s_binary.left == NULL)
   5179 	break;
   5180 
   5181       arg = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
   5182 				  NULL, &varargs);
   5183       if (arg == NULL)
   5184 	{
   5185 	  if (varargs)
   5186 	    {
   5187 	      *pvarargs = TRUE;
   5188 	      continue;
   5189 	    }
   5190 	  free (pargs);
   5191 	  return NULL;
   5192 	}
   5193 
   5194       if (count + 1 >= alloc)
   5195 	{
   5196 	  alloc += 10;
   5197 	  pargs = (debug_type *) xrealloc (pargs, alloc * sizeof *pargs);
   5198 	}
   5199 
   5200       pargs[count] = arg;
   5201       ++count;
   5202     }
   5203 
   5204   pargs[count] = DEBUG_TYPE_NULL;
   5205 
   5206   return pargs;
   5207 }
   5208 
   5209 /* Convert a struct demangle_component tree describing an argument
   5210    type into a debug_type.  */
   5211 
   5212 static debug_type
   5213 stab_demangle_v3_arg (void *dhandle, struct stab_handle *info,
   5214 		      struct demangle_component *dc, debug_type context,
   5215 		      bfd_boolean *pvarargs)
   5216 {
   5217   debug_type dt;
   5218 
   5219   if (pvarargs != NULL)
   5220     *pvarargs = FALSE;
   5221 
   5222   switch (dc->type)
   5223     {
   5224       /* FIXME: These are demangle component types which we probably
   5225 	 need to handle one way or another.  */
   5226     case DEMANGLE_COMPONENT_LOCAL_NAME:
   5227     case DEMANGLE_COMPONENT_TYPED_NAME:
   5228     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   5229     case DEMANGLE_COMPONENT_CTOR:
   5230     case DEMANGLE_COMPONENT_DTOR:
   5231     case DEMANGLE_COMPONENT_JAVA_CLASS:
   5232     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   5233     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   5234     case DEMANGLE_COMPONENT_CONST_THIS:
   5235     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   5236     case DEMANGLE_COMPONENT_COMPLEX:
   5237     case DEMANGLE_COMPONENT_IMAGINARY:
   5238     case DEMANGLE_COMPONENT_VENDOR_TYPE:
   5239     case DEMANGLE_COMPONENT_ARRAY_TYPE:
   5240     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   5241     case DEMANGLE_COMPONENT_ARGLIST:
   5242     default:
   5243       fprintf (stderr, _("Unrecognized demangle component %d\n"),
   5244 	       (int) dc->type);
   5245       return NULL;
   5246 
   5247     case DEMANGLE_COMPONENT_NAME:
   5248       if (context != NULL)
   5249 	{
   5250 	  const debug_field *fields;
   5251 
   5252 	  fields = debug_get_fields (dhandle, context);
   5253 	  if (fields != NULL)
   5254 	    {
   5255 	      /* Try to find this type by looking through the context
   5256 		 class.  */
   5257 	      for (; *fields != DEBUG_FIELD_NULL; fields++)
   5258 		{
   5259 		  debug_type ft;
   5260 		  const char *dn;
   5261 
   5262 		  ft = debug_get_field_type (dhandle, *fields);
   5263 		  if (ft == NULL)
   5264 		    return NULL;
   5265 		  dn = debug_get_type_name (dhandle, ft);
   5266 		  if (dn != NULL
   5267 		      && (int) strlen (dn) == dc->u.s_name.len
   5268 		      && strncmp (dn, dc->u.s_name.s, dc->u.s_name.len) == 0)
   5269 		    return ft;
   5270 		}
   5271 	    }
   5272 	}
   5273       return stab_find_tagged_type (dhandle, info, dc->u.s_name.s,
   5274 				    dc->u.s_name.len, DEBUG_KIND_ILLEGAL);
   5275 
   5276     case DEMANGLE_COMPONENT_QUAL_NAME:
   5277       context = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
   5278 				      context, NULL);
   5279       if (context == NULL)
   5280 	return NULL;
   5281       return stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.right,
   5282 				   context, NULL);
   5283 
   5284     case DEMANGLE_COMPONENT_TEMPLATE:
   5285       {
   5286 	char *p;
   5287 	size_t alc;
   5288 
   5289 	/* We print this component to get a class name which we can
   5290 	   use.  FIXME: This probably won't work if the template uses
   5291 	   template parameters which refer to an outer template.  */
   5292 	p = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, dc, 20, &alc);
   5293 	if (p == NULL)
   5294 	  {
   5295 	    fprintf (stderr, _("Failed to print demangled template\n"));
   5296 	    return NULL;
   5297 	  }
   5298 	dt = stab_find_tagged_type (dhandle, info, p, strlen (p),
   5299 				    DEBUG_KIND_CLASS);
   5300 	free (p);
   5301 	return dt;
   5302       }
   5303 
   5304     case DEMANGLE_COMPONENT_SUB_STD:
   5305       return stab_find_tagged_type (dhandle, info, dc->u.s_string.string,
   5306 				    dc->u.s_string.len, DEBUG_KIND_ILLEGAL);
   5307 
   5308     case DEMANGLE_COMPONENT_RESTRICT:
   5309     case DEMANGLE_COMPONENT_VOLATILE:
   5310     case DEMANGLE_COMPONENT_CONST:
   5311     case DEMANGLE_COMPONENT_POINTER:
   5312     case DEMANGLE_COMPONENT_REFERENCE:
   5313       dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
   5314 				 NULL);
   5315       if (dt == NULL)
   5316 	return NULL;
   5317 
   5318       switch (dc->type)
   5319 	{
   5320 	default:
   5321 	  abort ();
   5322 	case DEMANGLE_COMPONENT_RESTRICT:
   5323 	  /* FIXME: We have no way to represent restrict.  */
   5324 	  return dt;
   5325 	case DEMANGLE_COMPONENT_VOLATILE:
   5326 	  return debug_make_volatile_type (dhandle, dt);
   5327 	case DEMANGLE_COMPONENT_CONST:
   5328 	  return debug_make_const_type (dhandle, dt);
   5329 	case DEMANGLE_COMPONENT_POINTER:
   5330 	  return debug_make_pointer_type (dhandle, dt);
   5331 	case DEMANGLE_COMPONENT_REFERENCE:
   5332 	  return debug_make_reference_type (dhandle, dt);
   5333 	}
   5334 
   5335     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
   5336       {
   5337 	debug_type *pargs;
   5338 	bfd_boolean varargs;
   5339 
   5340 	if (dc->u.s_binary.left == NULL)
   5341 	  {
   5342 	    /* In this case the return type is actually unknown.
   5343 	       However, I'm not sure this will ever arise in practice;
   5344 	       normally an unknown return type would only appear at
   5345 	       the top level, which is handled above.  */
   5346 	    dt = debug_make_void_type (dhandle);
   5347 	  }
   5348 	else
   5349 	  dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
   5350 				     NULL);
   5351 	if (dt == NULL)
   5352 	  return NULL;
   5353 
   5354 	pargs = stab_demangle_v3_arglist (dhandle, info,
   5355 					  dc->u.s_binary.right,
   5356 					  &varargs);
   5357 	if (pargs == NULL)
   5358 	  return NULL;
   5359 
   5360 	return debug_make_function_type (dhandle, dt, pargs, varargs);
   5361       }
   5362 
   5363     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   5364       {
   5365 	char *p;
   5366 	size_t alc;
   5367 	debug_type ret;
   5368 
   5369 	/* We print this component in order to find out the type name.
   5370 	   FIXME: Should we instead expose the
   5371 	   demangle_builtin_type_info structure?  */
   5372 	p = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, dc, 20, &alc);
   5373 	if (p == NULL)
   5374 	  {
   5375 	    fprintf (stderr, _("Couldn't get demangled builtin type\n"));
   5376 	    return NULL;
   5377 	  }
   5378 
   5379 	/* The mangling is based on the type, but does not itself
   5380 	   indicate what the sizes are.  So we have to guess.  */
   5381 	if (strcmp (p, "signed char") == 0)
   5382 	  ret = debug_make_int_type (dhandle, 1, FALSE);
   5383 	else if (strcmp (p, "bool") == 0)
   5384 	  ret = debug_make_bool_type (dhandle, 1);
   5385 	else if (strcmp (p, "char") == 0)
   5386 	  ret = debug_make_int_type (dhandle, 1, FALSE);
   5387 	else if (strcmp (p, "double") == 0)
   5388 	  ret = debug_make_float_type (dhandle, 8);
   5389 	else if (strcmp (p, "long double") == 0)
   5390 	  ret = debug_make_float_type (dhandle, 8);
   5391 	else if (strcmp (p, "float") == 0)
   5392 	  ret = debug_make_float_type (dhandle, 4);
   5393 	else if (strcmp (p, "__float128") == 0)
   5394 	  ret = debug_make_float_type (dhandle, 16);
   5395 	else if (strcmp (p, "unsigned char") == 0)
   5396 	  ret = debug_make_int_type (dhandle, 1, TRUE);
   5397 	else if (strcmp (p, "int") == 0)
   5398 	  ret = debug_make_int_type (dhandle, 4, FALSE);
   5399 	else if (strcmp (p, "unsigned int") == 0)
   5400 	  ret = debug_make_int_type (dhandle, 4, TRUE);
   5401 	else if (strcmp (p, "long") == 0)
   5402 	  ret = debug_make_int_type (dhandle, 4, FALSE);
   5403 	else if (strcmp (p, "unsigned long") == 0)
   5404 	  ret = debug_make_int_type (dhandle, 4, TRUE);
   5405 	else if (strcmp (p, "__int128") == 0)
   5406 	  ret = debug_make_int_type (dhandle, 16, FALSE);
   5407 	else if (strcmp (p, "unsigned __int128") == 0)
   5408 	  ret = debug_make_int_type (dhandle, 16, TRUE);
   5409 	else if (strcmp (p, "short") == 0)
   5410 	  ret = debug_make_int_type (dhandle, 2, FALSE);
   5411 	else if (strcmp (p, "unsigned short") == 0)
   5412 	  ret = debug_make_int_type (dhandle, 2, TRUE);
   5413 	else if (strcmp (p, "void") == 0)
   5414 	  ret = debug_make_void_type (dhandle);
   5415 	else if (strcmp (p, "wchar_t") == 0)
   5416 	  ret = debug_make_int_type (dhandle, 4, TRUE);
   5417 	else if (strcmp (p, "long long") == 0)
   5418 	  ret = debug_make_int_type (dhandle, 8, FALSE);
   5419 	else if (strcmp (p, "unsigned long long") == 0)
   5420 	  ret = debug_make_int_type (dhandle, 8, TRUE);
   5421 	else if (strcmp (p, "...") == 0)
   5422 	  {
   5423 	    if (pvarargs == NULL)
   5424 	      fprintf (stderr, _("Unexpected demangled varargs\n"));
   5425 	    else
   5426 	      *pvarargs = TRUE;
   5427 	    ret = NULL;
   5428 	  }
   5429 	else
   5430 	  {
   5431 	    fprintf (stderr, _("Unrecognized demangled builtin type\n"));
   5432 	    ret = NULL;
   5433 	  }
   5434 
   5435 	free (p);
   5436 
   5437 	return ret;
   5438       }
   5439     }
   5440 }
   5441