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