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