Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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