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