Update Spanish and Swedish translations
[binutils.git] / binutils / stabs.c
blob087358289261e202cb7993096d707f7dbeaa13e2
1 /* stabs.c -- Parse stabs debugging information
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This file contains code which parses stabs debugging information.
24 The organization of this code is based on the gdb stabs reading
25 code. The job it does is somewhat different, because it is not
26 trying to identify the correct address for anything. */
28 #include <stdio.h>
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "libiberty.h"
33 #include "safe-ctype.h"
34 #include "demangle.h"
35 #include "debug.h"
36 #include "budbg.h"
37 #include "filenames.h"
39 /* Meaningless definition needs by aout64.h. FIXME. */
40 #define BYTES_IN_WORD 4
42 #include "aout/aout64.h"
43 #include "aout/stab_gnu.h"
45 /* The number of predefined XCOFF types. */
47 #define XCOFF_TYPE_COUNT 34
49 /* This structure is used as a handle so that the stab parsing doesn't
50 need to use any static variables. */
52 struct stab_handle
54 /* The BFD. */
55 bfd *abfd;
56 /* True if this is stabs in sections. */
57 boolean sections;
58 /* The symbol table. */
59 asymbol **syms;
60 /* The number of symbols. */
61 long symcount;
62 /* The accumulated file name string. */
63 char *so_string;
64 /* The value of the last N_SO symbol. */
65 bfd_vma so_value;
66 /* The value of the start of the file, so that we can handle file
67 relative N_LBRAC and N_RBRAC symbols. */
68 bfd_vma file_start_offset;
69 /* The offset of the start of the function, so that we can handle
70 function relative N_LBRAC and N_RBRAC symbols. */
71 bfd_vma function_start_offset;
72 /* The version number of gcc which compiled the current compilation
73 unit, 0 if not compiled by gcc. */
74 int gcc_compiled;
75 /* Whether an N_OPT symbol was seen that was not generated by gcc,
76 so that we can detect the SunPRO compiler. */
77 boolean n_opt_found;
78 /* The main file name. */
79 char *main_filename;
80 /* A stack of unfinished N_BINCL files. */
81 struct bincl_file *bincl_stack;
82 /* A list of finished N_BINCL files. */
83 struct bincl_file *bincl_list;
84 /* Whether we are inside a function or not. */
85 boolean within_function;
86 /* The address of the end of the function, used if we have seen an
87 N_FUN symbol while in a function. This is -1 if we have not seen
88 an N_FUN (the normal case). */
89 bfd_vma function_end;
90 /* The depth of block nesting. */
91 int block_depth;
92 /* List of pending variable definitions. */
93 struct stab_pending_var *pending;
94 /* Number of files for which we have types. */
95 unsigned int files;
96 /* Lists of types per file. */
97 struct stab_types **file_types;
98 /* Predefined XCOFF types. */
99 debug_type xcoff_types[XCOFF_TYPE_COUNT];
100 /* Undefined tags. */
101 struct stab_tag *tags;
102 /* Set by parse_stab_type if it sees a structure defined as a cross
103 reference to itself. Reset by parse_stab_type otherwise. */
104 boolean self_crossref;
107 /* A list of these structures is used to hold pending variable
108 definitions seen before the N_LBRAC of a block. */
110 struct stab_pending_var
112 /* Next pending variable definition. */
113 struct stab_pending_var *next;
114 /* Name. */
115 const char *name;
116 /* Type. */
117 debug_type type;
118 /* Kind. */
119 enum debug_var_kind kind;
120 /* Value. */
121 bfd_vma val;
124 /* A list of these structures is used to hold the types for a single
125 file. */
127 struct stab_types
129 /* Next set of slots for this file. */
130 struct stab_types *next;
131 /* Types indexed by type number. */
132 #define STAB_TYPES_SLOTS (16)
133 debug_type types[STAB_TYPES_SLOTS];
136 /* We keep a list of undefined tags that we encounter, so that we can
137 fill them in if the tag is later defined. */
139 struct stab_tag
141 /* Next undefined tag. */
142 struct stab_tag *next;
143 /* Tag name. */
144 const char *name;
145 /* Type kind. */
146 enum debug_type_kind kind;
147 /* Slot to hold real type when we discover it. If we don't, we fill
148 in an undefined tag type. */
149 debug_type slot;
150 /* Indirect type we have created to point at slot. */
151 debug_type type;
154 static char *savestring PARAMS ((const char *, int));
155 static bfd_vma parse_number PARAMS ((const char **, boolean *));
156 static void bad_stab PARAMS ((const char *));
157 static void warn_stab PARAMS ((const char *, const char *));
158 static boolean parse_stab_string
159 PARAMS ((PTR, struct stab_handle *, int, int, bfd_vma, const char *));
160 static debug_type parse_stab_type
161 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
162 debug_type **));
163 static boolean parse_stab_type_number
164 PARAMS ((const char **, int *));
165 static debug_type parse_stab_range_type
166 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
167 const int *));
168 static debug_type parse_stab_sun_builtin_type PARAMS ((PTR, const char **));
169 static debug_type parse_stab_sun_floating_type
170 PARAMS ((PTR, const char **));
171 static debug_type parse_stab_enum_type PARAMS ((PTR, const char **));
172 static debug_type parse_stab_struct_type
173 PARAMS ((PTR, struct stab_handle *, const char *, const char **, boolean,
174 const int *));
175 static boolean parse_stab_baseclasses
176 PARAMS ((PTR, struct stab_handle *, const char **, debug_baseclass **));
177 static boolean parse_stab_struct_fields
178 PARAMS ((PTR, struct stab_handle *, const char **, debug_field **,
179 boolean *));
180 static boolean parse_stab_cpp_abbrev
181 PARAMS ((PTR, struct stab_handle *, const char **, debug_field *));
182 static boolean parse_stab_one_struct_field
183 PARAMS ((PTR, struct stab_handle *, const char **, const char *,
184 debug_field *, boolean *));
185 static boolean parse_stab_members
186 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
187 const int *, debug_method **));
188 static debug_type parse_stab_argtypes
189 PARAMS ((PTR, struct stab_handle *, debug_type, const char *, const char *,
190 debug_type, const char *, boolean, boolean, const char **));
191 static boolean parse_stab_tilde_field
192 PARAMS ((PTR, struct stab_handle *, const char **, const int *,
193 debug_type *, boolean *));
194 static debug_type parse_stab_array_type
195 PARAMS ((PTR, struct stab_handle *, const char **, boolean));
196 static void push_bincl PARAMS ((struct stab_handle *, const char *, bfd_vma));
197 static const char *pop_bincl PARAMS ((struct stab_handle *));
198 static boolean find_excl
199 PARAMS ((struct stab_handle *, const char *, bfd_vma));
200 static boolean stab_record_variable
201 PARAMS ((PTR, struct stab_handle *, const char *, debug_type,
202 enum debug_var_kind, bfd_vma));
203 static boolean stab_emit_pending_vars PARAMS ((PTR, struct stab_handle *));
204 static debug_type *stab_find_slot
205 PARAMS ((struct stab_handle *, const int *));
206 static debug_type stab_find_type
207 PARAMS ((PTR, struct stab_handle *, const int *));
208 static boolean stab_record_type
209 PARAMS ((PTR, struct stab_handle *, const int *, debug_type));
210 static debug_type stab_xcoff_builtin_type
211 PARAMS ((PTR, struct stab_handle *, int));
212 static debug_type stab_find_tagged_type
213 PARAMS ((PTR, struct stab_handle *, const char *, int,
214 enum debug_type_kind));
215 static debug_type *stab_demangle_argtypes
216 PARAMS ((PTR, struct stab_handle *, const char *, boolean *));
218 /* Save a string in memory. */
220 static char *
221 savestring (start, len)
222 const char *start;
223 int len;
225 char *ret;
227 ret = (char *) xmalloc (len + 1);
228 memcpy (ret, start, len);
229 ret[len] = '\0';
230 return ret;
233 /* Read a number from a string. */
235 static bfd_vma
236 parse_number (pp, poverflow)
237 const char **pp;
238 boolean *poverflow;
240 unsigned long ul;
241 const char *orig;
243 if (poverflow != NULL)
244 *poverflow = false;
246 orig = *pp;
248 errno = 0;
249 ul = strtoul (*pp, (char **) pp, 0);
250 if (ul + 1 != 0 || errno == 0)
252 /* If bfd_vma is larger than unsigned long, and the number is
253 meant to be negative, we have to make sure that we sign
254 extend properly. */
255 if (*orig == '-')
256 return (bfd_vma) (bfd_signed_vma) (long) ul;
257 return (bfd_vma) ul;
260 /* Note that even though strtoul overflowed, it should have set *pp
261 to the end of the number, which is where we want it. */
263 if (sizeof (bfd_vma) > sizeof (unsigned long))
265 const char *p;
266 boolean neg;
267 int base;
268 bfd_vma over, lastdig;
269 boolean overflow;
270 bfd_vma v;
272 /* Our own version of strtoul, for a bfd_vma. */
274 p = orig;
276 neg = false;
277 if (*p == '+')
278 ++p;
279 else if (*p == '-')
281 neg = true;
282 ++p;
285 base = 10;
286 if (*p == '0')
288 if (p[1] == 'x' || p[1] == 'X')
290 base = 16;
291 p += 2;
293 else
295 base = 8;
296 ++p;
300 over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
301 lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
303 overflow = false;
304 v = 0;
305 while (1)
307 int d;
309 d = *p++;
310 if (ISDIGIT (d))
311 d -= '0';
312 else if (ISUPPER (d))
313 d -= 'A';
314 else if (ISLOWER (d))
315 d -= 'a';
316 else
317 break;
319 if (d >= base)
320 break;
322 if (v > over || (v == over && (bfd_vma) d > lastdig))
324 overflow = true;
325 break;
329 if (! overflow)
331 if (neg)
332 v = - v;
333 return v;
337 /* If we get here, the number is too large to represent in a
338 bfd_vma. */
340 if (poverflow != NULL)
341 *poverflow = true;
342 else
343 warn_stab (orig, _("numeric overflow"));
345 return 0;
348 /* Give an error for a bad stab string. */
350 static void
351 bad_stab (p)
352 const char *p;
354 fprintf (stderr, _("Bad stab: %s\n"), p);
357 /* Warn about something in a stab string. */
359 static void
360 warn_stab (p, err)
361 const char *p;
362 const char *err;
364 fprintf (stderr, _("Warning: %s: %s\n"), err, p);
367 /* Create a handle to parse stabs symbols with. */
370 start_stab (dhandle, abfd, sections, syms, symcount)
371 PTR dhandle ATTRIBUTE_UNUSED;
372 bfd *abfd;
373 boolean sections;
374 asymbol **syms;
375 long symcount;
377 struct stab_handle *ret;
379 ret = (struct stab_handle *) xmalloc (sizeof *ret);
380 memset (ret, 0, sizeof *ret);
381 ret->abfd = abfd;
382 ret->sections = sections;
383 ret->syms = syms;
384 ret->symcount = symcount;
385 ret->files = 1;
386 ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
387 ret->file_types[0] = NULL;
388 ret->function_end = (bfd_vma) -1;
389 return (PTR) ret;
392 /* When we have processed all the stabs information, we need to go
393 through and fill in all the undefined tags. */
395 boolean
396 finish_stab (dhandle, handle)
397 PTR dhandle;
398 PTR handle;
400 struct stab_handle *info = (struct stab_handle *) handle;
401 struct stab_tag *st;
403 if (info->within_function)
405 if (! stab_emit_pending_vars (dhandle, info)
406 || ! debug_end_function (dhandle, info->function_end))
407 return false;
408 info->within_function = false;
409 info->function_end = (bfd_vma) -1;
412 for (st = info->tags; st != NULL; st = st->next)
414 enum debug_type_kind kind;
416 kind = st->kind;
417 if (kind == DEBUG_KIND_ILLEGAL)
418 kind = DEBUG_KIND_STRUCT;
419 st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
420 if (st->slot == DEBUG_TYPE_NULL)
421 return false;
424 return true;
427 /* Handle a single stabs symbol. */
429 boolean
430 parse_stab (dhandle, handle, type, desc, value, string)
431 PTR dhandle;
432 PTR handle;
433 int type;
434 int desc;
435 bfd_vma value;
436 const char *string;
438 struct stab_handle *info = (struct stab_handle *) handle;
440 /* gcc will emit two N_SO strings per compilation unit, one for the
441 directory name and one for the file name. We just collect N_SO
442 strings as we see them, and start the new compilation unit when
443 we see a non N_SO symbol. */
444 if (info->so_string != NULL
445 && (type != N_SO || *string == '\0' || value != info->so_value))
447 if (! debug_set_filename (dhandle, info->so_string))
448 return false;
449 info->main_filename = info->so_string;
451 info->gcc_compiled = 0;
452 info->n_opt_found = false;
454 /* Generally, for stabs in the symbol table, the N_LBRAC and
455 N_RBRAC symbols are relative to the N_SO symbol value. */
456 if (! info->sections)
457 info->file_start_offset = info->so_value;
459 /* We need to reset the mapping from type numbers to types. We
460 can't free the old mapping, because of the use of
461 debug_make_indirect_type. */
462 info->files = 1;
463 info->file_types = ((struct stab_types **)
464 xmalloc (sizeof *info->file_types));
465 info->file_types[0] = NULL;
467 info->so_string = NULL;
469 /* Now process whatever type we just got. */
472 switch (type)
474 case N_FN:
475 case N_FN_SEQ:
476 break;
478 case N_LBRAC:
479 /* Ignore extra outermost context from SunPRO cc and acc. */
480 if (info->n_opt_found && desc == 1)
481 break;
483 if (! info->within_function)
485 fprintf (stderr, _("N_LBRAC not within function\n"));
486 return false;
489 /* Start an inner lexical block. */
490 if (! debug_start_block (dhandle,
491 (value
492 + info->file_start_offset
493 + info->function_start_offset)))
494 return false;
496 /* Emit any pending variable definitions. */
497 if (! stab_emit_pending_vars (dhandle, info))
498 return false;
500 ++info->block_depth;
501 break;
503 case N_RBRAC:
504 /* Ignore extra outermost context from SunPRO cc and acc. */
505 if (info->n_opt_found && desc == 1)
506 break;
508 /* We shouldn't have any pending variable definitions here, but,
509 if we do, we probably need to emit them before closing the
510 block. */
511 if (! stab_emit_pending_vars (dhandle, info))
512 return false;
514 /* End an inner lexical block. */
515 if (! debug_end_block (dhandle,
516 (value
517 + info->file_start_offset
518 + info->function_start_offset)))
519 return false;
521 --info->block_depth;
522 if (info->block_depth < 0)
524 fprintf (stderr, _("Too many N_RBRACs\n"));
525 return false;
527 break;
529 case N_SO:
530 /* This always ends a function. */
531 if (info->within_function)
533 bfd_vma endval;
535 endval = value;
536 if (*string != '\0'
537 && info->function_end != (bfd_vma) -1
538 && info->function_end < endval)
539 endval = info->function_end;
540 if (! stab_emit_pending_vars (dhandle, info)
541 || ! debug_end_function (dhandle, endval))
542 return false;
543 info->within_function = false;
544 info->function_end = (bfd_vma) -1;
547 /* An empty string is emitted by gcc at the end of a compilation
548 unit. */
549 if (*string == '\0')
550 return true;
552 /* Just accumulate strings until we see a non N_SO symbol. If
553 the string starts with a directory separator or some other
554 form of absolute path specification, we discard the previously
555 accumulated strings. */
556 if (info->so_string == NULL)
557 info->so_string = xstrdup (string);
558 else
560 char *f;
562 f = info->so_string;
564 if (IS_ABSOLUTE_PATH (string))
565 info->so_string = xstrdup (string);
566 else
567 info->so_string = concat (info->so_string, string,
568 (const char *) NULL);
569 free (f);
572 info->so_value = value;
574 break;
576 case N_SOL:
577 /* Start an include file. */
578 if (! debug_start_source (dhandle, string))
579 return false;
580 break;
582 case N_BINCL:
583 /* Start an include file which may be replaced. */
584 push_bincl (info, string, value);
585 if (! debug_start_source (dhandle, string))
586 return false;
587 break;
589 case N_EINCL:
590 /* End an N_BINCL include. */
591 if (! debug_start_source (dhandle, pop_bincl (info)))
592 return false;
593 break;
595 case N_EXCL:
596 /* This is a duplicate of a header file named by N_BINCL which
597 was eliminated by the linker. */
598 if (! find_excl (info, string, value))
599 return false;
600 break;
602 case N_SLINE:
603 if (! debug_record_line (dhandle, desc,
604 value + info->function_start_offset))
605 return false;
606 break;
608 case N_BCOMM:
609 if (! debug_start_common_block (dhandle, string))
610 return false;
611 break;
613 case N_ECOMM:
614 if (! debug_end_common_block (dhandle, string))
615 return false;
616 break;
618 case N_FUN:
619 if (*string == '\0')
621 if (info->within_function)
623 /* This always marks the end of a function; we don't
624 need to worry about info->function_end. */
625 if (info->sections)
626 value += info->function_start_offset;
627 if (! stab_emit_pending_vars (dhandle, info)
628 || ! debug_end_function (dhandle, value))
629 return false;
630 info->within_function = false;
631 info->function_end = (bfd_vma) -1;
633 break;
636 /* A const static symbol in the .text section will have an N_FUN
637 entry. We need to use these to mark the end of the function,
638 in case we are looking at gcc output before it was changed to
639 always emit an empty N_FUN. We can't call debug_end_function
640 here, because it might be a local static symbol. */
641 if (info->within_function
642 && (info->function_end == (bfd_vma) -1
643 || value < info->function_end))
644 info->function_end = value;
646 /* Fall through. */
647 /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
648 symbols, and if it does not start with :S, gdb relocates the
649 value to the start of the section. gcc always seems to use
650 :S, so we don't worry about this. */
651 /* Fall through. */
652 default:
654 const char *colon;
656 colon = strchr (string, ':');
657 if (colon != NULL
658 && (colon[1] == 'f' || colon[1] == 'F'))
660 if (info->within_function)
662 bfd_vma endval;
664 endval = value;
665 if (info->function_end != (bfd_vma) -1
666 && info->function_end < endval)
667 endval = info->function_end;
668 if (! stab_emit_pending_vars (dhandle, info)
669 || ! debug_end_function (dhandle, endval))
670 return false;
671 info->function_end = (bfd_vma) -1;
673 /* For stabs in sections, line numbers and block addresses
674 are offsets from the start of the function. */
675 if (info->sections)
676 info->function_start_offset = value;
677 info->within_function = true;
680 if (! parse_stab_string (dhandle, info, type, desc, value, string))
681 return false;
683 break;
685 case N_OPT:
686 if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
687 info->gcc_compiled = 2;
688 else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
689 info->gcc_compiled = 1;
690 else
691 info->n_opt_found = true;
692 break;
694 case N_OBJ:
695 case N_ENDM:
696 case N_MAIN:
697 case N_WARNING:
698 break;
701 return true;
704 /* Parse the stabs string. */
706 static boolean
707 parse_stab_string (dhandle, info, stabtype, desc, value, string)
708 PTR dhandle;
709 struct stab_handle *info;
710 int stabtype;
711 int desc;
712 bfd_vma value;
713 const char *string;
715 const char *p;
716 char *name;
717 int type;
718 debug_type dtype;
719 boolean synonym;
720 boolean self_crossref;
721 unsigned int lineno;
722 debug_type *slot;
724 p = strchr (string, ':');
725 if (p == NULL)
726 return true;
728 while (p[1] == ':')
730 p += 2;
731 p = strchr (p, ':');
732 if (p == NULL)
734 bad_stab (string);
735 return false;
739 /* GCC 2.x puts the line number in desc. SunOS apparently puts in
740 the number of bytes occupied by a type or object, which we
741 ignore. */
742 if (info->gcc_compiled >= 2)
743 lineno = desc;
744 else
745 lineno = 0;
747 /* FIXME: Sometimes the special C++ names start with '.'. */
748 name = NULL;
749 if (string[0] == '$')
751 switch (string[1])
753 case 't':
754 name = "this";
755 break;
756 case 'v':
757 /* Was: name = "vptr"; */
758 break;
759 case 'e':
760 name = "eh_throw";
761 break;
762 case '_':
763 /* This was an anonymous type that was never fixed up. */
764 break;
765 case 'X':
766 /* SunPRO (3.0 at least) static variable encoding. */
767 break;
768 default:
769 warn_stab (string, _("unknown C++ encoded name"));
770 break;
774 if (name == NULL)
776 if (p == string || (string[0] == ' ' && p == string + 1))
777 name = NULL;
778 else
779 name = savestring (string, p - string);
782 ++p;
783 if (ISDIGIT (*p) || *p == '(' || *p == '-')
784 type = 'l';
785 else
786 type = *p++;
788 switch (type)
790 case 'c':
791 /* c is a special case, not followed by a type-number.
792 SYMBOL:c=iVALUE for an integer constant symbol.
793 SYMBOL:c=rVALUE for a floating constant symbol.
794 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
795 e.g. "b:c=e6,0" for "const b = blob1"
796 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
797 if (*p != '=')
799 bad_stab (string);
800 return false;
802 ++p;
803 switch (*p++)
805 case 'r':
806 /* Floating point constant. */
807 if (! debug_record_float_const (dhandle, name, atof (p)))
808 return false;
809 break;
810 case 'i':
811 /* Integer constant. */
812 /* Defining integer constants this way is kind of silly,
813 since 'e' constants allows the compiler to give not only
814 the value, but the type as well. C has at least int,
815 long, unsigned int, and long long as constant types;
816 other languages probably should have at least unsigned as
817 well as signed constants. */
818 if (! debug_record_int_const (dhandle, name, atoi (p)))
819 return false;
820 break;
821 case 'e':
822 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
823 can be represented as integral.
824 e.g. "b:c=e6,0" for "const b = blob1"
825 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
826 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
827 &p, (debug_type **) NULL);
828 if (dtype == DEBUG_TYPE_NULL)
829 return false;
830 if (*p != ',')
832 bad_stab (string);
833 return false;
835 if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
836 return false;
837 break;
838 default:
839 bad_stab (string);
840 return false;
843 break;
845 case 'C':
846 /* The name of a caught exception. */
847 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
848 &p, (debug_type **) NULL);
849 if (dtype == DEBUG_TYPE_NULL)
850 return false;
851 if (! debug_record_label (dhandle, name, dtype, value))
852 return false;
853 break;
855 case 'f':
856 case 'F':
857 /* A function definition. */
858 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
859 (debug_type **) NULL);
860 if (dtype == DEBUG_TYPE_NULL)
861 return false;
862 if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
863 return false;
865 /* Sun acc puts declared types of arguments here. We don't care
866 about their actual types (FIXME -- we should remember the whole
867 function prototype), but the list may define some new types
868 that we have to remember, so we must scan it now. */
869 while (*p == ';')
871 ++p;
872 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
873 (debug_type **) NULL)
874 == DEBUG_TYPE_NULL)
875 return false;
878 break;
880 case 'G':
882 char leading;
883 long c;
884 asymbol **ps;
886 /* A global symbol. The value must be extracted from the
887 symbol table. */
888 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
889 (debug_type **) NULL);
890 if (dtype == DEBUG_TYPE_NULL)
891 return false;
892 leading = bfd_get_symbol_leading_char (info->abfd);
893 for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
895 const char *n;
897 n = bfd_asymbol_name (*ps);
898 if (leading != '\0' && *n == leading)
899 ++n;
900 if (*n == *name && strcmp (n, name) == 0)
901 break;
903 if (c > 0)
904 value = bfd_asymbol_value (*ps);
905 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
906 value))
907 return false;
909 break;
911 /* This case is faked by a conditional above, when there is no
912 code letter in the dbx data. Dbx data never actually
913 contains 'l'. */
914 case 'l':
915 case 's':
916 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
917 (debug_type **) NULL);
918 if (dtype == DEBUG_TYPE_NULL)
919 return false;
920 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
921 value))
922 return false;
923 break;
925 case 'p':
926 /* A function parameter. */
927 if (*p != 'F')
928 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
929 (debug_type **) NULL);
930 else
932 /* pF is a two-letter code that means a function parameter in
933 Fortran. The type-number specifies the type of the return
934 value. Translate it into a pointer-to-function type. */
935 ++p;
936 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
937 (debug_type **) NULL);
938 if (dtype != DEBUG_TYPE_NULL)
940 debug_type ftype;
942 ftype = debug_make_function_type (dhandle, dtype,
943 (debug_type *) NULL, false);
944 dtype = debug_make_pointer_type (dhandle, ftype);
947 if (dtype == DEBUG_TYPE_NULL)
948 return false;
949 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
950 value))
951 return false;
953 /* FIXME: At this point gdb considers rearranging the parameter
954 address on a big endian machine if it is smaller than an int.
955 We have no way to do that, since we don't really know much
956 about the target. */
958 break;
960 case 'P':
961 if (stabtype == N_FUN)
963 /* Prototype of a function referenced by this file. */
964 while (*p == ';')
966 ++p;
967 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
968 (debug_type **) NULL)
969 == DEBUG_TYPE_NULL)
970 return false;
972 break;
974 /* Fall through. */
975 case 'R':
976 /* Parameter which is in a register. */
977 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
978 (debug_type **) NULL);
979 if (dtype == DEBUG_TYPE_NULL)
980 return false;
981 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
982 value))
983 return false;
984 break;
986 case 'r':
987 /* Register variable (either global or local). */
988 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
989 (debug_type **) NULL);
990 if (dtype == DEBUG_TYPE_NULL)
991 return false;
992 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
993 value))
994 return false;
996 /* FIXME: At this point gdb checks to combine pairs of 'p' and
997 'r' stabs into a single 'P' stab. */
999 break;
1001 case 'S':
1002 /* Static symbol at top level of file */
1003 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1004 (debug_type **) NULL);
1005 if (dtype == DEBUG_TYPE_NULL)
1006 return false;
1007 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
1008 value))
1009 return false;
1010 break;
1012 case 't':
1013 /* A typedef. */
1014 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1015 if (dtype == DEBUG_TYPE_NULL)
1016 return false;
1017 if (name == NULL)
1019 /* A nameless type. Nothing to do. */
1020 return true;
1023 dtype = debug_name_type (dhandle, name, dtype);
1024 if (dtype == DEBUG_TYPE_NULL)
1025 return false;
1027 if (slot != NULL)
1028 *slot = dtype;
1030 break;
1032 case 'T':
1033 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1034 by 't' which means we are typedef'ing it as well. */
1035 if (*p != 't')
1037 synonym = false;
1038 /* FIXME: gdb sets synonym to true if the current language
1039 is C++. */
1041 else
1043 synonym = true;
1044 ++p;
1047 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1048 if (dtype == DEBUG_TYPE_NULL)
1049 return false;
1050 if (name == NULL)
1051 return true;
1053 /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
1054 a cross reference to itself. These are generated by some
1055 versions of g++. */
1056 self_crossref = info->self_crossref;
1058 dtype = debug_tag_type (dhandle, name, dtype);
1059 if (dtype == DEBUG_TYPE_NULL)
1060 return false;
1061 if (slot != NULL)
1062 *slot = dtype;
1064 /* See if we have a cross reference to this tag which we can now
1065 fill in. Avoid filling in a cross reference to ourselves,
1066 because that would lead to circular debugging information. */
1067 if (! self_crossref)
1069 register struct stab_tag **pst;
1071 for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
1073 if ((*pst)->name[0] == name[0]
1074 && strcmp ((*pst)->name, name) == 0)
1076 (*pst)->slot = dtype;
1077 *pst = (*pst)->next;
1078 break;
1083 if (synonym)
1085 dtype = debug_name_type (dhandle, name, dtype);
1086 if (dtype == DEBUG_TYPE_NULL)
1087 return false;
1089 if (slot != NULL)
1090 *slot = dtype;
1093 break;
1095 case 'V':
1096 /* Static symbol of local scope */
1097 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1098 (debug_type **) NULL);
1099 if (dtype == DEBUG_TYPE_NULL)
1100 return false;
1101 /* FIXME: gdb checks os9k_stabs here. */
1102 if (! stab_record_variable (dhandle, info, name, dtype,
1103 DEBUG_LOCAL_STATIC, value))
1104 return false;
1105 break;
1107 case 'v':
1108 /* Reference parameter. */
1109 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1110 (debug_type **) NULL);
1111 if (dtype == DEBUG_TYPE_NULL)
1112 return false;
1113 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
1114 value))
1115 return false;
1116 break;
1118 case 'a':
1119 /* Reference parameter which is in a register. */
1120 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1121 (debug_type **) NULL);
1122 if (dtype == DEBUG_TYPE_NULL)
1123 return false;
1124 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1125 value))
1126 return false;
1127 break;
1129 case 'X':
1130 /* This is used by Sun FORTRAN for "function result value".
1131 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1132 that Pascal uses it too, but when I tried it Pascal used
1133 "x:3" (local symbol) instead. */
1134 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1135 (debug_type **) NULL);
1136 if (dtype == DEBUG_TYPE_NULL)
1137 return false;
1138 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1139 value))
1140 return false;
1141 break;
1143 default:
1144 bad_stab (string);
1145 return false;
1148 /* FIXME: gdb converts structure values to structure pointers in a
1149 couple of cases, depending upon the target. */
1151 return true;
1154 /* Parse a stabs type. The typename argument is non-NULL if this is a
1155 typedef or a tag definition. The pp argument points to the stab
1156 string, and is updated. The slotp argument points to a place to
1157 store the slot used if the type is being defined. */
1159 static debug_type
1160 parse_stab_type (dhandle, info, typename, pp, slotp)
1161 PTR dhandle;
1162 struct stab_handle *info;
1163 const char *typename;
1164 const char **pp;
1165 debug_type **slotp;
1167 const char *orig;
1168 int typenums[2];
1169 int size;
1170 boolean stringp;
1171 int descriptor;
1172 debug_type dtype;
1174 if (slotp != NULL)
1175 *slotp = NULL;
1177 orig = *pp;
1179 size = -1;
1180 stringp = false;
1182 info->self_crossref = false;
1184 /* Read type number if present. The type number may be omitted.
1185 for instance in a two-dimensional array declared with type
1186 "ar1;1;10;ar1;1;10;4". */
1187 if (! ISDIGIT (**pp) && **pp != '(' && **pp != '-')
1189 /* 'typenums=' not present, type is anonymous. Read and return
1190 the definition, but don't put it in the type vector. */
1191 typenums[0] = typenums[1] = -1;
1193 else
1195 if (! parse_stab_type_number (pp, typenums))
1196 return DEBUG_TYPE_NULL;
1198 if (**pp != '=')
1200 /* Type is not being defined here. Either it already
1201 exists, or this is a forward reference to it. */
1202 return stab_find_type (dhandle, info, typenums);
1205 /* Only set the slot if the type is being defined. This means
1206 that the mapping from type numbers to types will only record
1207 the name of the typedef which defines a type. If we don't do
1208 this, then something like
1209 typedef int foo;
1210 int i;
1211 will record that i is of type foo. Unfortunately, stabs
1212 information is ambiguous about variable types. For this code,
1213 typedef int foo;
1214 int i;
1215 foo j;
1216 the stabs information records both i and j as having the same
1217 type. This could be fixed by patching the compiler. */
1218 if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1219 *slotp = stab_find_slot (info, typenums);
1221 /* Type is being defined here. */
1222 /* Skip the '='. */
1223 ++*pp;
1225 while (**pp == '@')
1227 const char *p = *pp + 1;
1228 const char *attr;
1230 if (ISDIGIT (*p) || *p == '(' || *p == '-')
1232 /* Member type. */
1233 break;
1236 /* Type attributes. */
1237 attr = p;
1239 for (; *p != ';'; ++p)
1241 if (*p == '\0')
1243 bad_stab (orig);
1244 return DEBUG_TYPE_NULL;
1247 *pp = p + 1;
1249 switch (*attr)
1251 case 's':
1252 size = atoi (attr + 1);
1253 size /= 8; /* Size is in bits. We store it in bytes. */
1254 if (size <= 0)
1255 size = -1;
1256 break;
1258 case 'S':
1259 stringp = true;
1260 break;
1262 default:
1263 /* Ignore unrecognized type attributes, so future
1264 compilers can invent new ones. */
1265 break;
1270 descriptor = **pp;
1271 ++*pp;
1273 switch (descriptor)
1275 case 'x':
1277 enum debug_type_kind code;
1278 const char *q1, *q2, *p;
1280 /* A cross reference to another type. */
1282 switch (**pp)
1284 case 's':
1285 code = DEBUG_KIND_STRUCT;
1286 break;
1287 case 'u':
1288 code = DEBUG_KIND_UNION;
1289 break;
1290 case 'e':
1291 code = DEBUG_KIND_ENUM;
1292 break;
1293 default:
1294 /* Complain and keep going, so compilers can invent new
1295 cross-reference types. */
1296 warn_stab (orig, _("unrecognized cross reference type"));
1297 code = DEBUG_KIND_STRUCT;
1298 break;
1300 ++*pp;
1302 q1 = strchr (*pp, '<');
1303 p = strchr (*pp, ':');
1304 if (p == NULL)
1306 bad_stab (orig);
1307 return DEBUG_TYPE_NULL;
1309 if (q1 != NULL && p > q1 && p[1] == ':')
1311 int nest = 0;
1313 for (q2 = q1; *q2 != '\0'; ++q2)
1315 if (*q2 == '<')
1316 ++nest;
1317 else if (*q2 == '>')
1318 --nest;
1319 else if (*q2 == ':' && nest == 0)
1320 break;
1322 p = q2;
1323 if (*p != ':')
1325 bad_stab (orig);
1326 return DEBUG_TYPE_NULL;
1330 /* Some versions of g++ can emit stabs like
1331 fleep:T20=xsfleep:
1332 which define structures in terms of themselves. We need to
1333 tell the caller to avoid building a circular structure. */
1334 if (typename != NULL
1335 && strncmp (typename, *pp, p - *pp) == 0
1336 && typename[p - *pp] == '\0')
1337 info->self_crossref = true;
1339 dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1341 *pp = p + 1;
1343 break;
1345 case '-':
1346 case '0':
1347 case '1':
1348 case '2':
1349 case '3':
1350 case '4':
1351 case '5':
1352 case '6':
1353 case '7':
1354 case '8':
1355 case '9':
1356 case '(':
1358 const char *hold;
1359 int xtypenums[2];
1361 /* This type is defined as another type. */
1363 (*pp)--;
1364 hold = *pp;
1366 /* Peek ahead at the number to detect void. */
1367 if (! parse_stab_type_number (pp, xtypenums))
1368 return DEBUG_TYPE_NULL;
1370 if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1372 /* This type is being defined as itself, which means that
1373 it is void. */
1374 dtype = debug_make_void_type (dhandle);
1376 else
1378 *pp = hold;
1380 /* Go back to the number and have parse_stab_type get it.
1381 This means that we can deal with something like
1382 t(1,2)=(3,4)=... which the Lucid compiler uses. */
1383 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1384 pp, (debug_type **) NULL);
1385 if (dtype == DEBUG_TYPE_NULL)
1386 return DEBUG_TYPE_NULL;
1389 if (typenums[0] != -1)
1391 if (! stab_record_type (dhandle, info, typenums, dtype))
1392 return DEBUG_TYPE_NULL;
1395 break;
1398 case '*':
1399 dtype = debug_make_pointer_type (dhandle,
1400 parse_stab_type (dhandle, info,
1401 (const char *) NULL,
1403 (debug_type **) NULL));
1404 break;
1406 case '&':
1407 /* Reference to another type. */
1408 dtype = (debug_make_reference_type
1409 (dhandle,
1410 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1411 (debug_type **) NULL)));
1412 break;
1414 case 'f':
1415 /* Function returning another type. */
1416 /* FIXME: gdb checks os9k_stabs here. */
1417 dtype = (debug_make_function_type
1418 (dhandle,
1419 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1420 (debug_type **) NULL),
1421 (debug_type *) NULL, false));
1422 break;
1424 case 'k':
1425 /* Const qualifier on some type (Sun). */
1426 /* FIXME: gdb accepts 'c' here if os9k_stabs. */
1427 dtype = debug_make_const_type (dhandle,
1428 parse_stab_type (dhandle, info,
1429 (const char *) NULL,
1431 (debug_type **) NULL));
1432 break;
1434 case 'B':
1435 /* Volatile qual on some type (Sun). */
1436 /* FIXME: gdb accepts 'i' here if os9k_stabs. */
1437 dtype = (debug_make_volatile_type
1438 (dhandle,
1439 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1440 (debug_type **) NULL)));
1441 break;
1443 case '@':
1444 /* Offset (class & variable) type. This is used for a pointer
1445 relative to an object. */
1447 debug_type domain;
1448 debug_type memtype;
1450 /* Member type. */
1452 domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1453 (debug_type **) NULL);
1454 if (domain == DEBUG_TYPE_NULL)
1455 return DEBUG_TYPE_NULL;
1457 if (**pp != ',')
1459 bad_stab (orig);
1460 return DEBUG_TYPE_NULL;
1462 ++*pp;
1464 memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1465 (debug_type **) NULL);
1466 if (memtype == DEBUG_TYPE_NULL)
1467 return DEBUG_TYPE_NULL;
1469 dtype = debug_make_offset_type (dhandle, domain, memtype);
1471 break;
1473 case '#':
1474 /* Method (class & fn) type. */
1475 if (**pp == '#')
1477 debug_type return_type;
1479 ++*pp;
1480 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1481 pp, (debug_type **) NULL);
1482 if (return_type == DEBUG_TYPE_NULL)
1483 return DEBUG_TYPE_NULL;
1484 if (**pp != ';')
1486 bad_stab (orig);
1487 return DEBUG_TYPE_NULL;
1489 ++*pp;
1490 dtype = debug_make_method_type (dhandle, return_type,
1491 DEBUG_TYPE_NULL,
1492 (debug_type *) NULL, false);
1494 else
1496 debug_type domain;
1497 debug_type return_type;
1498 debug_type *args;
1499 unsigned int n;
1500 unsigned int alloc;
1501 boolean varargs;
1503 domain = parse_stab_type (dhandle, info, (const char *) NULL,
1504 pp, (debug_type **) NULL);
1505 if (domain == DEBUG_TYPE_NULL)
1506 return DEBUG_TYPE_NULL;
1508 if (**pp != ',')
1510 bad_stab (orig);
1511 return DEBUG_TYPE_NULL;
1513 ++*pp;
1515 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1516 pp, (debug_type **) NULL);
1517 if (return_type == DEBUG_TYPE_NULL)
1518 return DEBUG_TYPE_NULL;
1520 alloc = 10;
1521 args = (debug_type *) xmalloc (alloc * sizeof *args);
1522 n = 0;
1523 while (**pp != ';')
1525 if (**pp != ',')
1527 bad_stab (orig);
1528 return DEBUG_TYPE_NULL;
1530 ++*pp;
1532 if (n + 1 >= alloc)
1534 alloc += 10;
1535 args = ((debug_type *)
1536 xrealloc ((PTR) args, alloc * sizeof *args));
1539 args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1540 pp, (debug_type **) NULL);
1541 if (args[n] == DEBUG_TYPE_NULL)
1542 return DEBUG_TYPE_NULL;
1543 ++n;
1545 ++*pp;
1547 /* If the last type is not void, then this function takes a
1548 variable number of arguments. Otherwise, we must strip
1549 the void type. */
1550 if (n == 0
1551 || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1552 varargs = true;
1553 else
1555 --n;
1556 varargs = false;
1559 args[n] = DEBUG_TYPE_NULL;
1561 dtype = debug_make_method_type (dhandle, return_type, domain, args,
1562 varargs);
1564 break;
1566 case 'r':
1567 /* Range type. */
1568 dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
1569 break;
1571 case 'b':
1572 /* FIXME: gdb checks os9k_stabs here. */
1573 /* Sun ACC builtin int type. */
1574 dtype = parse_stab_sun_builtin_type (dhandle, pp);
1575 break;
1577 case 'R':
1578 /* Sun ACC builtin float type. */
1579 dtype = parse_stab_sun_floating_type (dhandle, pp);
1580 break;
1582 case 'e':
1583 /* Enumeration type. */
1584 dtype = parse_stab_enum_type (dhandle, pp);
1585 break;
1587 case 's':
1588 case 'u':
1589 /* Struct or union type. */
1590 dtype = parse_stab_struct_type (dhandle, info, typename, pp,
1591 descriptor == 's', typenums);
1592 break;
1594 case 'a':
1595 /* Array type. */
1596 if (**pp != 'r')
1598 bad_stab (orig);
1599 return DEBUG_TYPE_NULL;
1601 ++*pp;
1603 dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1604 break;
1606 case 'S':
1607 dtype = debug_make_set_type (dhandle,
1608 parse_stab_type (dhandle, info,
1609 (const char *) NULL,
1611 (debug_type **) NULL),
1612 stringp);
1613 break;
1615 default:
1616 bad_stab (orig);
1617 return DEBUG_TYPE_NULL;
1620 if (dtype == DEBUG_TYPE_NULL)
1621 return DEBUG_TYPE_NULL;
1623 if (typenums[0] != -1)
1625 if (! stab_record_type (dhandle, info, typenums, dtype))
1626 return DEBUG_TYPE_NULL;
1629 if (size != -1)
1631 if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1632 return DEBUG_TYPE_NULL;
1635 return dtype;
1638 /* Read a number by which a type is referred to in dbx data, or
1639 perhaps read a pair (FILENUM, TYPENUM) in parentheses. Just a
1640 single number N is equivalent to (0,N). Return the two numbers by
1641 storing them in the vector TYPENUMS. */
1643 static boolean
1644 parse_stab_type_number (pp, typenums)
1645 const char **pp;
1646 int *typenums;
1648 const char *orig;
1650 orig = *pp;
1652 if (**pp != '(')
1654 typenums[0] = 0;
1655 typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1657 else
1659 ++*pp;
1660 typenums[0] = (int) parse_number (pp, (boolean *) NULL);
1661 if (**pp != ',')
1663 bad_stab (orig);
1664 return false;
1666 ++*pp;
1667 typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1668 if (**pp != ')')
1670 bad_stab (orig);
1671 return false;
1673 ++*pp;
1676 return true;
1679 /* Parse a range type. */
1681 static debug_type
1682 parse_stab_range_type (dhandle, info, typename, pp, typenums)
1683 PTR dhandle;
1684 struct stab_handle *info;
1685 const char *typename;
1686 const char **pp;
1687 const int *typenums;
1689 const char *orig;
1690 int rangenums[2];
1691 boolean self_subrange;
1692 debug_type index_type;
1693 const char *s2, *s3;
1694 bfd_signed_vma n2, n3;
1695 boolean ov2, ov3;
1697 orig = *pp;
1699 index_type = DEBUG_TYPE_NULL;
1701 /* First comes a type we are a subrange of.
1702 In C it is usually 0, 1 or the type being defined. */
1703 if (! parse_stab_type_number (pp, rangenums))
1704 return DEBUG_TYPE_NULL;
1706 self_subrange = (rangenums[0] == typenums[0]
1707 && rangenums[1] == typenums[1]);
1709 if (**pp == '=')
1711 *pp = orig;
1712 index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1713 pp, (debug_type **) NULL);
1714 if (index_type == DEBUG_TYPE_NULL)
1715 return DEBUG_TYPE_NULL;
1718 if (**pp == ';')
1719 ++*pp;
1721 /* The remaining two operands are usually lower and upper bounds of
1722 the range. But in some special cases they mean something else. */
1723 s2 = *pp;
1724 n2 = parse_number (pp, &ov2);
1725 if (**pp != ';')
1727 bad_stab (orig);
1728 return DEBUG_TYPE_NULL;
1730 ++*pp;
1732 s3 = *pp;
1733 n3 = parse_number (pp, &ov3);
1734 if (**pp != ';')
1736 bad_stab (orig);
1737 return DEBUG_TYPE_NULL;
1739 ++*pp;
1741 if (ov2 || ov3)
1743 /* gcc will emit range stabs for long long types. Handle this
1744 as a special case. FIXME: This needs to be more general. */
1745 #define LLLOW "01000000000000000000000;"
1746 #define LLHIGH "0777777777777777777777;"
1747 #define ULLHIGH "01777777777777777777777;"
1748 if (index_type == DEBUG_TYPE_NULL)
1750 if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1751 && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1752 return debug_make_int_type (dhandle, 8, false);
1753 if (! ov2
1754 && n2 == 0
1755 && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1756 return debug_make_int_type (dhandle, 8, true);
1759 warn_stab (orig, _("numeric overflow"));
1762 if (index_type == DEBUG_TYPE_NULL)
1764 /* A type defined as a subrange of itself, with both bounds 0,
1765 is void. */
1766 if (self_subrange && n2 == 0 && n3 == 0)
1767 return debug_make_void_type (dhandle);
1769 /* A type defined as a subrange of itself, with n2 positive and
1770 n3 zero, is a complex type, and n2 is the number of bytes. */
1771 if (self_subrange && n3 == 0 && n2 > 0)
1772 return debug_make_complex_type (dhandle, n2);
1774 /* If n3 is zero and n2 is positive, this is a floating point
1775 type, and n2 is the number of bytes. */
1776 if (n3 == 0 && n2 > 0)
1777 return debug_make_float_type (dhandle, n2);
1779 /* If the upper bound is -1, this is an unsigned int. */
1780 if (n2 == 0 && n3 == -1)
1782 /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1783 long long int:t6=r1;0;-1;
1784 long long unsigned int:t7=r1;0;-1;
1785 We hack here to handle this reasonably. */
1786 if (typename != NULL)
1788 if (strcmp (typename, "long long int") == 0)
1789 return debug_make_int_type (dhandle, 8, false);
1790 else if (strcmp (typename, "long long unsigned int") == 0)
1791 return debug_make_int_type (dhandle, 8, true);
1793 /* FIXME: The size here really depends upon the target. */
1794 return debug_make_int_type (dhandle, 4, true);
1797 /* A range of 0 to 127 is char. */
1798 if (self_subrange && n2 == 0 && n3 == 127)
1799 return debug_make_int_type (dhandle, 1, false);
1801 /* FIXME: gdb checks for the language CHILL here. */
1803 if (n2 == 0)
1805 if (n3 < 0)
1806 return debug_make_int_type (dhandle, - n3, true);
1807 else if (n3 == 0xff)
1808 return debug_make_int_type (dhandle, 1, true);
1809 else if (n3 == 0xffff)
1810 return debug_make_int_type (dhandle, 2, true);
1811 else if (n3 == (bfd_signed_vma) 0xffffffff)
1812 return debug_make_int_type (dhandle, 4, true);
1813 #ifdef BFD64
1814 else if (n3 == ((((bfd_signed_vma) 0xffffffff) << 32) | 0xffffffff))
1815 return debug_make_int_type (dhandle, 8, true);
1816 #endif
1818 else if (n3 == 0
1819 && n2 < 0
1820 && (self_subrange || n2 == -8))
1821 return debug_make_int_type (dhandle, - n2, true);
1822 else if (n2 == - n3 - 1 || n2 == n3 + 1)
1824 if (n3 == 0x7f)
1825 return debug_make_int_type (dhandle, 1, false);
1826 else if (n3 == 0x7fff)
1827 return debug_make_int_type (dhandle, 2, false);
1828 else if (n3 == 0x7fffffff)
1829 return debug_make_int_type (dhandle, 4, false);
1830 #ifdef BFD64
1831 else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
1832 return debug_make_int_type (dhandle, 8, false);
1833 #endif
1837 /* At this point I don't have the faintest idea how to deal with a
1838 self_subrange type; I'm going to assume that this is used as an
1839 idiom, and that all of them are special cases. So . . . */
1840 if (self_subrange)
1842 bad_stab (orig);
1843 return DEBUG_TYPE_NULL;
1846 index_type = stab_find_type (dhandle, info, rangenums);
1847 if (index_type == DEBUG_TYPE_NULL)
1849 /* Does this actually ever happen? Is that why we are worrying
1850 about dealing with it rather than just calling error_type? */
1851 warn_stab (orig, _("missing index type"));
1852 index_type = debug_make_int_type (dhandle, 4, false);
1855 return debug_make_range_type (dhandle, index_type, n2, n3);
1858 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1859 typedefs in every file (for int, long, etc):
1861 type = b <signed> <width>; <offset>; <nbits>
1862 signed = u or s. Possible c in addition to u or s (for char?).
1863 offset = offset from high order bit to start bit of type.
1864 width is # bytes in object of this type, nbits is # bits in type.
1866 The width/offset stuff appears to be for small objects stored in
1867 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
1868 FIXME. */
1870 static debug_type
1871 parse_stab_sun_builtin_type (dhandle, pp)
1872 PTR dhandle;
1873 const char **pp;
1875 const char *orig;
1876 boolean unsignedp;
1877 bfd_vma bits;
1879 orig = *pp;
1881 switch (**pp)
1883 case 's':
1884 unsignedp = false;
1885 break;
1886 case 'u':
1887 unsignedp = true;
1888 break;
1889 default:
1890 bad_stab (orig);
1891 return DEBUG_TYPE_NULL;
1893 ++*pp;
1895 /* For some odd reason, all forms of char put a c here. This is strange
1896 because no other type has this honor. We can safely ignore this because
1897 we actually determine 'char'acterness by the number of bits specified in
1898 the descriptor. */
1899 if (**pp == 'c')
1900 ++*pp;
1902 /* The first number appears to be the number of bytes occupied
1903 by this type, except that unsigned short is 4 instead of 2.
1904 Since this information is redundant with the third number,
1905 we will ignore it. */
1906 (void) parse_number (pp, (boolean *) NULL);
1907 if (**pp != ';')
1909 bad_stab (orig);
1910 return DEBUG_TYPE_NULL;
1912 ++*pp;
1914 /* The second number is always 0, so ignore it too. */
1915 (void) parse_number (pp, (boolean *) NULL);
1916 if (**pp != ';')
1918 bad_stab (orig);
1919 return DEBUG_TYPE_NULL;
1921 ++*pp;
1923 /* The third number is the number of bits for this type. */
1924 bits = parse_number (pp, (boolean *) NULL);
1926 /* The type *should* end with a semicolon. If it are embedded
1927 in a larger type the semicolon may be the only way to know where
1928 the type ends. If this type is at the end of the stabstring we
1929 can deal with the omitted semicolon (but we don't have to like
1930 it). Don't bother to complain(), Sun's compiler omits the semicolon
1931 for "void". */
1932 if (**pp == ';')
1933 ++*pp;
1935 if (bits == 0)
1936 return debug_make_void_type (dhandle);
1938 return debug_make_int_type (dhandle, bits / 8, unsignedp);
1941 /* Parse a builtin floating type generated by the Sun compiler. */
1943 static debug_type
1944 parse_stab_sun_floating_type (dhandle, pp)
1945 PTR dhandle;
1946 const char **pp;
1948 const char *orig;
1949 bfd_vma details;
1950 bfd_vma bytes;
1952 orig = *pp;
1954 /* The first number has more details about the type, for example
1955 FN_COMPLEX. */
1956 details = parse_number (pp, (boolean *) NULL);
1957 if (**pp != ';')
1959 bad_stab (orig);
1960 return DEBUG_TYPE_NULL;
1963 /* The second number is the number of bytes occupied by this type */
1964 bytes = parse_number (pp, (boolean *) NULL);
1965 if (**pp != ';')
1967 bad_stab (orig);
1968 return DEBUG_TYPE_NULL;
1971 if (details == NF_COMPLEX
1972 || details == NF_COMPLEX16
1973 || details == NF_COMPLEX32)
1974 return debug_make_complex_type (dhandle, bytes);
1976 return debug_make_float_type (dhandle, bytes);
1979 /* Handle an enum type. */
1981 static debug_type
1982 parse_stab_enum_type (dhandle, pp)
1983 PTR dhandle;
1984 const char **pp;
1986 const char *orig;
1987 const char **names;
1988 bfd_signed_vma *values;
1989 unsigned int n;
1990 unsigned int alloc;
1992 orig = *pp;
1994 /* FIXME: gdb checks os9k_stabs here. */
1996 /* The aix4 compiler emits an extra field before the enum members;
1997 my guess is it's a type of some sort. Just ignore it. */
1998 if (**pp == '-')
2000 while (**pp != ':')
2001 ++*pp;
2002 ++*pp;
2005 /* Read the value-names and their values.
2006 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2007 A semicolon or comma instead of a NAME means the end. */
2008 alloc = 10;
2009 names = (const char **) xmalloc (alloc * sizeof *names);
2010 values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
2011 n = 0;
2012 while (**pp != '\0' && **pp != ';' && **pp != ',')
2014 const char *p;
2015 char *name;
2016 bfd_signed_vma val;
2018 p = *pp;
2019 while (*p != ':')
2020 ++p;
2022 name = savestring (*pp, p - *pp);
2024 *pp = p + 1;
2025 val = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
2026 if (**pp != ',')
2028 bad_stab (orig);
2029 return DEBUG_TYPE_NULL;
2031 ++*pp;
2033 if (n + 1 >= alloc)
2035 alloc += 10;
2036 names = ((const char **)
2037 xrealloc ((PTR) names, alloc * sizeof *names));
2038 values = ((bfd_signed_vma *)
2039 xrealloc ((PTR) values, alloc * sizeof *values));
2042 names[n] = name;
2043 values[n] = val;
2044 ++n;
2047 names[n] = NULL;
2048 values[n] = 0;
2050 if (**pp == ';')
2051 ++*pp;
2053 return debug_make_enum_type (dhandle, names, values);
2056 /* Read the description of a structure (or union type) and return an object
2057 describing the type.
2059 PP points to a character pointer that points to the next unconsumed token
2060 in the the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
2061 *PP will point to "4a:1,0,32;;". */
2063 static debug_type
2064 parse_stab_struct_type (dhandle, info, tagname, pp, structp, typenums)
2065 PTR dhandle;
2066 struct stab_handle *info;
2067 const char *tagname;
2068 const char **pp;
2069 boolean structp;
2070 const int *typenums;
2072 const char *orig;
2073 bfd_vma size;
2074 debug_baseclass *baseclasses;
2075 debug_field *fields;
2076 boolean statics;
2077 debug_method *methods;
2078 debug_type vptrbase;
2079 boolean ownvptr;
2081 orig = *pp;
2083 /* Get the size. */
2084 size = parse_number (pp, (boolean *) NULL);
2086 /* Get the other information. */
2087 if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
2088 || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
2089 || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
2090 || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
2091 &ownvptr))
2092 return DEBUG_TYPE_NULL;
2094 if (! statics
2095 && baseclasses == NULL
2096 && methods == NULL
2097 && vptrbase == DEBUG_TYPE_NULL
2098 && ! ownvptr)
2099 return debug_make_struct_type (dhandle, structp, size, fields);
2101 return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
2102 methods, vptrbase, ownvptr);
2105 /* The stabs for C++ derived classes contain baseclass information which
2106 is marked by a '!' character after the total size. This function is
2107 called when we encounter the baseclass marker, and slurps up all the
2108 baseclass information.
2110 Immediately following the '!' marker is the number of base classes that
2111 the class is derived from, followed by information for each base class.
2112 For each base class, there are two visibility specifiers, a bit offset
2113 to the base class information within the derived class, a reference to
2114 the type for the base class, and a terminating semicolon.
2116 A typical example, with two base classes, would be "!2,020,19;0264,21;".
2117 ^^ ^ ^ ^ ^ ^ ^
2118 Baseclass information marker __________________|| | | | | | |
2119 Number of baseclasses __________________________| | | | | | |
2120 Visibility specifiers (2) ________________________| | | | | |
2121 Offset in bits from start of class _________________| | | | |
2122 Type number for base class ___________________________| | | |
2123 Visibility specifiers (2) _______________________________| | |
2124 Offset in bits from start of class ________________________| |
2125 Type number of base class ____________________________________|
2127 Return true for success, false for failure. */
2129 static boolean
2130 parse_stab_baseclasses (dhandle, info, pp, retp)
2131 PTR dhandle;
2132 struct stab_handle *info;
2133 const char **pp;
2134 debug_baseclass **retp;
2136 const char *orig;
2137 unsigned int c, i;
2138 debug_baseclass *classes;
2140 *retp = NULL;
2142 orig = *pp;
2144 if (**pp != '!')
2146 /* No base classes. */
2147 return true;
2149 ++*pp;
2151 c = (unsigned int) parse_number (pp, (boolean *) NULL);
2153 if (**pp != ',')
2155 bad_stab (orig);
2156 return false;
2158 ++*pp;
2160 classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2162 for (i = 0; i < c; i++)
2164 boolean virtual;
2165 enum debug_visibility visibility;
2166 bfd_vma bitpos;
2167 debug_type type;
2169 switch (**pp)
2171 case '0':
2172 virtual = false;
2173 break;
2174 case '1':
2175 virtual = true;
2176 break;
2177 default:
2178 warn_stab (orig, _("unknown virtual character for baseclass"));
2179 virtual = false;
2180 break;
2182 ++*pp;
2184 switch (**pp)
2186 case '0':
2187 visibility = DEBUG_VISIBILITY_PRIVATE;
2188 break;
2189 case '1':
2190 visibility = DEBUG_VISIBILITY_PROTECTED;
2191 break;
2192 case '2':
2193 visibility = DEBUG_VISIBILITY_PUBLIC;
2194 break;
2195 default:
2196 warn_stab (orig, _("unknown visibility character for baseclass"));
2197 visibility = DEBUG_VISIBILITY_PUBLIC;
2198 break;
2200 ++*pp;
2202 /* The remaining value is the bit offset of the portion of the
2203 object corresponding to this baseclass. Always zero in the
2204 absence of multiple inheritance. */
2205 bitpos = parse_number (pp, (boolean *) NULL);
2206 if (**pp != ',')
2208 bad_stab (orig);
2209 return false;
2211 ++*pp;
2213 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2214 (debug_type **) NULL);
2215 if (type == DEBUG_TYPE_NULL)
2216 return false;
2218 classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2219 visibility);
2220 if (classes[i] == DEBUG_BASECLASS_NULL)
2221 return false;
2223 if (**pp != ';')
2224 return false;
2225 ++*pp;
2228 classes[i] = DEBUG_BASECLASS_NULL;
2230 *retp = classes;
2232 return true;
2235 /* Read struct or class data fields. They have the form:
2237 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2239 At the end, we see a semicolon instead of a field.
2241 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2242 a static field.
2244 The optional VISIBILITY is one of:
2246 '/0' (VISIBILITY_PRIVATE)
2247 '/1' (VISIBILITY_PROTECTED)
2248 '/2' (VISIBILITY_PUBLIC)
2249 '/9' (VISIBILITY_IGNORE)
2251 or nothing, for C style fields with public visibility.
2253 Returns 1 for success, 0 for failure. */
2255 static boolean
2256 parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2257 PTR dhandle;
2258 struct stab_handle *info;
2259 const char **pp;
2260 debug_field **retp;
2261 boolean *staticsp;
2263 const char *orig;
2264 const char *p;
2265 debug_field *fields;
2266 unsigned int c;
2267 unsigned int alloc;
2269 *retp = NULL;
2270 *staticsp = false;
2272 orig = *pp;
2274 c = 0;
2275 alloc = 10;
2276 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2277 while (**pp != ';')
2279 /* FIXME: gdb checks os9k_stabs here. */
2281 p = *pp;
2283 /* Add 1 to c to leave room for NULL pointer at end. */
2284 if (c + 1 >= alloc)
2286 alloc += 10;
2287 fields = ((debug_field *)
2288 xrealloc ((PTR) fields, alloc * sizeof *fields));
2291 /* If it starts with CPLUS_MARKER it is a special abbreviation,
2292 unless the CPLUS_MARKER is followed by an underscore, in
2293 which case it is just the name of an anonymous type, which we
2294 should handle like any other type name. We accept either '$'
2295 or '.', because a field name can never contain one of these
2296 characters except as a CPLUS_MARKER. */
2298 if ((*p == '$' || *p == '.') && p[1] != '_')
2300 ++*pp;
2301 if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2302 return false;
2303 ++c;
2304 continue;
2307 /* Look for the ':' that separates the field name from the field
2308 values. Data members are delimited by a single ':', while member
2309 functions are delimited by a pair of ':'s. When we hit the member
2310 functions (if any), terminate scan loop and return. */
2312 p = strchr (p, ':');
2313 if (p == NULL)
2315 bad_stab (orig);
2316 return false;
2319 if (p[1] == ':')
2320 break;
2322 if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2323 staticsp))
2324 return false;
2326 ++c;
2329 fields[c] = DEBUG_FIELD_NULL;
2331 *retp = fields;
2333 return true;
2336 /* Special GNU C++ name. */
2338 static boolean
2339 parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2340 PTR dhandle;
2341 struct stab_handle *info;
2342 const char **pp;
2343 debug_field *retp;
2345 const char *orig;
2346 int cpp_abbrev;
2347 debug_type context;
2348 const char *name;
2349 const char *typename;
2350 debug_type type;
2351 bfd_vma bitpos;
2353 *retp = DEBUG_FIELD_NULL;
2355 orig = *pp;
2357 if (**pp != 'v')
2359 bad_stab (*pp);
2360 return false;
2362 ++*pp;
2364 cpp_abbrev = **pp;
2365 ++*pp;
2367 /* At this point, *pp points to something like "22:23=*22...", where
2368 the type number before the ':' is the "context" and everything
2369 after is a regular type definition. Lookup the type, find it's
2370 name, and construct the field name. */
2372 context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2373 (debug_type **) NULL);
2374 if (context == DEBUG_TYPE_NULL)
2375 return false;
2377 switch (cpp_abbrev)
2379 case 'f':
2380 /* $vf -- a virtual function table pointer. */
2381 name = "_vptr$";
2382 break;
2383 case 'b':
2384 /* $vb -- a virtual bsomethingorother */
2385 typename = debug_get_type_name (dhandle, context);
2386 if (typename == NULL)
2388 warn_stab (orig, _("unnamed $vb type"));
2389 typename = "FOO";
2391 name = concat ("_vb$", typename, (const char *) NULL);
2392 break;
2393 default:
2394 warn_stab (orig, _("unrecognized C++ abbreviation"));
2395 name = "INVALID_CPLUSPLUS_ABBREV";
2396 break;
2399 if (**pp != ':')
2401 bad_stab (orig);
2402 return false;
2404 ++*pp;
2406 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2407 (debug_type **) NULL);
2408 if (**pp != ',')
2410 bad_stab (orig);
2411 return false;
2413 ++*pp;
2415 bitpos = parse_number (pp, (boolean *) NULL);
2416 if (**pp != ';')
2418 bad_stab (orig);
2419 return false;
2421 ++*pp;
2423 *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2424 DEBUG_VISIBILITY_PRIVATE);
2425 if (*retp == DEBUG_FIELD_NULL)
2426 return false;
2428 return true;
2431 /* Parse a single field in a struct or union. */
2433 static boolean
2434 parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2435 PTR dhandle;
2436 struct stab_handle *info;
2437 const char **pp;
2438 const char *p;
2439 debug_field *retp;
2440 boolean *staticsp;
2442 const char *orig;
2443 char *name;
2444 enum debug_visibility visibility;
2445 debug_type type;
2446 bfd_vma bitpos;
2447 bfd_vma bitsize;
2449 orig = *pp;
2451 /* FIXME: gdb checks ARM_DEMANGLING here. */
2453 name = savestring (*pp, p - *pp);
2455 *pp = p + 1;
2457 if (**pp != '/')
2458 visibility = DEBUG_VISIBILITY_PUBLIC;
2459 else
2461 ++*pp;
2462 switch (**pp)
2464 case '0':
2465 visibility = DEBUG_VISIBILITY_PRIVATE;
2466 break;
2467 case '1':
2468 visibility = DEBUG_VISIBILITY_PROTECTED;
2469 break;
2470 case '2':
2471 visibility = DEBUG_VISIBILITY_PUBLIC;
2472 break;
2473 default:
2474 warn_stab (orig, _("unknown visibility character for field"));
2475 visibility = DEBUG_VISIBILITY_PUBLIC;
2476 break;
2478 ++*pp;
2481 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2482 (debug_type **) NULL);
2483 if (type == DEBUG_TYPE_NULL)
2484 return false;
2486 if (**pp == ':')
2488 char *varname;
2490 /* This is a static class member. */
2491 ++*pp;
2492 p = strchr (*pp, ';');
2493 if (p == NULL)
2495 bad_stab (orig);
2496 return false;
2499 varname = savestring (*pp, p - *pp);
2501 *pp = p + 1;
2503 *retp = debug_make_static_member (dhandle, name, type, varname,
2504 visibility);
2505 *staticsp = true;
2507 return true;
2510 if (**pp != ',')
2512 bad_stab (orig);
2513 return false;
2515 ++*pp;
2517 bitpos = parse_number (pp, (boolean *) NULL);
2518 if (**pp != ',')
2520 bad_stab (orig);
2521 return false;
2523 ++*pp;
2525 bitsize = parse_number (pp, (boolean *) NULL);
2526 if (**pp != ';')
2528 bad_stab (orig);
2529 return false;
2531 ++*pp;
2533 if (bitpos == 0 && bitsize == 0)
2535 /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2536 so, it is a field which has been optimized out. The correct
2537 stab for this case is to use VISIBILITY_IGNORE, but that is a
2538 recent invention. (2) It is a 0-size array. For example
2539 union { int num; char str[0]; } foo. Printing "<no value>"
2540 for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2541 will continue to work, and a 0-size array as a whole doesn't
2542 have any contents to print.
2544 I suspect this probably could also happen with gcc -gstabs
2545 (not -gstabs+) for static fields, and perhaps other C++
2546 extensions. Hopefully few people use -gstabs with gdb, since
2547 it is intended for dbx compatibility. */
2548 visibility = DEBUG_VISIBILITY_IGNORE;
2551 /* FIXME: gdb does some stuff here to mark fields as unpacked. */
2553 *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2555 return true;
2558 /* Read member function stabs info for C++ classes. The form of each member
2559 function data is:
2561 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2563 An example with two member functions is:
2565 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2567 For the case of overloaded operators, the format is op$::*.funcs, where
2568 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2569 name (such as `+=') and `.' marks the end of the operator name. */
2571 static boolean
2572 parse_stab_members (dhandle, info, tagname, pp, typenums, retp)
2573 PTR dhandle;
2574 struct stab_handle *info;
2575 const char *tagname;
2576 const char **pp;
2577 const int *typenums;
2578 debug_method **retp;
2580 const char *orig;
2581 debug_method *methods;
2582 unsigned int c;
2583 unsigned int alloc;
2585 *retp = NULL;
2587 orig = *pp;
2589 alloc = 0;
2590 methods = NULL;
2591 c = 0;
2593 while (**pp != ';')
2595 const char *p;
2596 char *name;
2597 debug_method_variant *variants;
2598 unsigned int cvars;
2599 unsigned int allocvars;
2600 debug_type look_ahead_type;
2602 p = strchr (*pp, ':');
2603 if (p == NULL || p[1] != ':')
2604 break;
2606 /* FIXME: Some systems use something other than '$' here. */
2607 if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2609 name = savestring (*pp, p - *pp);
2610 *pp = p + 2;
2612 else
2614 /* This is a completely wierd case. In order to stuff in the
2615 names that might contain colons (the usual name delimiter),
2616 Mike Tiemann defined a different name format which is
2617 signalled if the identifier is "op$". In that case, the
2618 format is "op$::XXXX." where XXXX is the name. This is
2619 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2620 *pp = p + 2;
2621 for (p = *pp; *p != '.' && *p != '\0'; p++)
2623 if (*p != '.')
2625 bad_stab (orig);
2626 return false;
2628 name = savestring (*pp, p - *pp);
2629 *pp = p + 1;
2632 allocvars = 10;
2633 variants = ((debug_method_variant *)
2634 xmalloc (allocvars * sizeof *variants));
2635 cvars = 0;
2637 look_ahead_type = DEBUG_TYPE_NULL;
2641 debug_type type;
2642 boolean stub;
2643 char *argtypes;
2644 enum debug_visibility visibility;
2645 boolean constp, volatilep, staticp;
2646 bfd_vma voffset;
2647 debug_type context;
2648 const char *physname;
2649 boolean varargs;
2651 if (look_ahead_type != DEBUG_TYPE_NULL)
2653 /* g++ version 1 kludge */
2654 type = look_ahead_type;
2655 look_ahead_type = DEBUG_TYPE_NULL;
2657 else
2659 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2660 (debug_type **) NULL);
2661 if (type == DEBUG_TYPE_NULL)
2662 return false;
2663 if (**pp != ':')
2665 bad_stab (orig);
2666 return false;
2670 ++*pp;
2671 p = strchr (*pp, ';');
2672 if (p == NULL)
2674 bad_stab (orig);
2675 return false;
2678 stub = false;
2679 if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
2680 && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
2681 stub = true;
2683 argtypes = savestring (*pp, p - *pp);
2684 *pp = p + 1;
2686 switch (**pp)
2688 case '0':
2689 visibility = DEBUG_VISIBILITY_PRIVATE;
2690 break;
2691 case '1':
2692 visibility = DEBUG_VISIBILITY_PROTECTED;
2693 break;
2694 default:
2695 visibility = DEBUG_VISIBILITY_PUBLIC;
2696 break;
2698 ++*pp;
2700 constp = false;
2701 volatilep = false;
2702 switch (**pp)
2704 case 'A':
2705 /* Normal function. */
2706 ++*pp;
2707 break;
2708 case 'B':
2709 /* const member function. */
2710 constp = true;
2711 ++*pp;
2712 break;
2713 case 'C':
2714 /* volatile member function. */
2715 volatilep = true;
2716 ++*pp;
2717 break;
2718 case 'D':
2719 /* const volatile member function. */
2720 constp = true;
2721 volatilep = true;
2722 ++*pp;
2723 break;
2724 case '*':
2725 case '?':
2726 case '.':
2727 /* File compiled with g++ version 1; no information. */
2728 break;
2729 default:
2730 warn_stab (orig, _("const/volatile indicator missing"));
2731 break;
2734 staticp = false;
2735 switch (**pp)
2737 case '*':
2738 /* virtual member function, followed by index. The sign
2739 bit is supposedly set to distinguish
2740 pointers-to-methods from virtual function indicies. */
2741 ++*pp;
2742 voffset = parse_number (pp, (boolean *) NULL);
2743 if (**pp != ';')
2745 bad_stab (orig);
2746 return false;
2748 ++*pp;
2749 voffset &= 0x7fffffff;
2751 if (**pp == ';' || *pp == '\0')
2753 /* Must be g++ version 1. */
2754 context = DEBUG_TYPE_NULL;
2756 else
2758 /* Figure out from whence this virtual function
2759 came. It may belong to virtual function table of
2760 one of its baseclasses. */
2761 look_ahead_type = parse_stab_type (dhandle, info,
2762 (const char *) NULL,
2764 (debug_type **) NULL);
2765 if (**pp == ':')
2767 /* g++ version 1 overloaded methods. */
2768 context = DEBUG_TYPE_NULL;
2770 else
2772 context = look_ahead_type;
2773 look_ahead_type = DEBUG_TYPE_NULL;
2774 if (**pp != ';')
2776 bad_stab (orig);
2777 return false;
2779 ++*pp;
2782 break;
2784 case '?':
2785 /* static member function. */
2786 ++*pp;
2787 staticp = true;
2788 voffset = 0;
2789 context = DEBUG_TYPE_NULL;
2790 if (strncmp (argtypes, name, strlen (name)) != 0)
2791 stub = true;
2792 break;
2794 default:
2795 warn_stab (orig, "member function type missing");
2796 voffset = 0;
2797 context = DEBUG_TYPE_NULL;
2798 break;
2800 case '.':
2801 ++*pp;
2802 voffset = 0;
2803 context = DEBUG_TYPE_NULL;
2804 break;
2807 /* If the type is not a stub, then the argtypes string is
2808 the physical name of the function. Otherwise the
2809 argtypes string is the mangled form of the argument
2810 types, and the full type and the physical name must be
2811 extracted from them. */
2812 if (! stub)
2813 physname = argtypes;
2814 else
2816 debug_type class_type, return_type;
2818 class_type = stab_find_type (dhandle, info, typenums);
2819 if (class_type == DEBUG_TYPE_NULL)
2820 return false;
2821 return_type = debug_get_return_type (dhandle, type);
2822 if (return_type == DEBUG_TYPE_NULL)
2824 bad_stab (orig);
2825 return false;
2827 type = parse_stab_argtypes (dhandle, info, class_type, name,
2828 tagname, return_type, argtypes,
2829 constp, volatilep, &physname);
2830 if (type == DEBUG_TYPE_NULL)
2831 return false;
2834 if (cvars + 1 >= allocvars)
2836 allocvars += 10;
2837 variants = ((debug_method_variant *)
2838 xrealloc ((PTR) variants,
2839 allocvars * sizeof *variants));
2842 if (! staticp)
2843 variants[cvars] = debug_make_method_variant (dhandle, physname,
2844 type, visibility,
2845 constp, volatilep,
2846 voffset, context);
2847 else
2848 variants[cvars] = debug_make_static_method_variant (dhandle,
2849 physname,
2850 type,
2851 visibility,
2852 constp,
2853 volatilep);
2854 if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2855 return false;
2857 ++cvars;
2859 while (**pp != ';' && **pp != '\0');
2861 variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2863 if (**pp != '\0')
2864 ++*pp;
2866 if (c + 1 >= alloc)
2868 alloc += 10;
2869 methods = ((debug_method *)
2870 xrealloc ((PTR) methods, alloc * sizeof *methods));
2873 methods[c] = debug_make_method (dhandle, name, variants);
2875 ++c;
2878 if (methods != NULL)
2879 methods[c] = DEBUG_METHOD_NULL;
2881 *retp = methods;
2883 return true;
2886 /* Parse a string representing argument types for a method. Stabs
2887 tries to save space by packing argument types into a mangled
2888 string. This string should give us enough information to extract
2889 both argument types and the physical name of the function, given
2890 the tag name. */
2892 static debug_type
2893 parse_stab_argtypes (dhandle, info, class_type, fieldname, tagname,
2894 return_type, argtypes, constp, volatilep, pphysname)
2895 PTR dhandle;
2896 struct stab_handle *info;
2897 debug_type class_type;
2898 const char *fieldname;
2899 const char *tagname;
2900 debug_type return_type;
2901 const char *argtypes;
2902 boolean constp;
2903 boolean volatilep;
2904 const char **pphysname;
2906 boolean is_full_physname_constructor;
2907 boolean is_constructor;
2908 boolean is_destructor;
2909 debug_type *args;
2910 boolean varargs;
2912 /* Constructors are sometimes handled specially. */
2913 is_full_physname_constructor = ((argtypes[0] == '_'
2914 && argtypes[1] == '_'
2915 && (ISDIGIT (argtypes[2])
2916 || argtypes[2] == 'Q'
2917 || argtypes[2] == 't'))
2918 || strncmp (argtypes, "__ct", 4) == 0);
2920 is_constructor = (is_full_physname_constructor
2921 || (tagname != NULL
2922 && strcmp (fieldname, tagname) == 0));
2923 is_destructor = ((argtypes[0] == '_'
2924 && (argtypes[1] == '$' || argtypes[1] == '.')
2925 && argtypes[2] == '_')
2926 || strncmp (argtypes, "__dt", 4) == 0);
2928 if (is_destructor || is_full_physname_constructor)
2929 *pphysname = argtypes;
2930 else
2932 unsigned int len;
2933 const char *const_prefix;
2934 const char *volatile_prefix;
2935 char buf[20];
2936 unsigned int mangled_name_len;
2937 char *physname;
2939 len = tagname == NULL ? 0 : strlen (tagname);
2940 const_prefix = constp ? "C" : "";
2941 volatile_prefix = volatilep ? "V" : "";
2943 if (len == 0)
2944 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2945 else if (tagname != NULL && strchr (tagname, '<') != NULL)
2947 /* Template methods are fully mangled. */
2948 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2949 tagname = NULL;
2950 len = 0;
2952 else
2953 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2955 mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2956 + strlen (buf)
2957 + len
2958 + strlen (argtypes)
2959 + 1);
2961 if (fieldname[0] == 'o'
2962 && fieldname[1] == 'p'
2963 && (fieldname[2] == '$' || fieldname[2] == '.'))
2965 const char *opname;
2967 opname = cplus_mangle_opname (fieldname + 3, 0);
2968 if (opname == NULL)
2970 fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
2971 return DEBUG_TYPE_NULL;
2973 mangled_name_len += strlen (opname);
2974 physname = (char *) xmalloc (mangled_name_len);
2975 strncpy (physname, fieldname, 3);
2976 strcpy (physname + 3, opname);
2978 else
2980 physname = (char *) xmalloc (mangled_name_len);
2981 if (is_constructor)
2982 physname[0] = '\0';
2983 else
2984 strcpy (physname, fieldname);
2987 strcat (physname, buf);
2988 if (tagname != NULL)
2989 strcat (physname, tagname);
2990 strcat (physname, argtypes);
2992 *pphysname = physname;
2995 if (*argtypes == '\0' || is_destructor)
2997 args = (debug_type *) xmalloc (sizeof *args);
2998 *args = NULL;
2999 return debug_make_method_type (dhandle, return_type, class_type, args,
3000 false);
3003 args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs);
3004 if (args == NULL)
3005 return DEBUG_TYPE_NULL;
3007 return debug_make_method_type (dhandle, return_type, class_type, args,
3008 varargs);
3011 /* The tail end of stabs for C++ classes that contain a virtual function
3012 pointer contains a tilde, a %, and a type number.
3013 The type number refers to the base class (possibly this class itself) which
3014 contains the vtable pointer for the current class.
3016 This function is called when we have parsed all the method declarations,
3017 so we can look for the vptr base class info. */
3019 static boolean
3020 parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
3021 PTR dhandle;
3022 struct stab_handle *info;
3023 const char **pp;
3024 const int *typenums;
3025 debug_type *retvptrbase;
3026 boolean *retownvptr;
3028 const char *orig;
3029 const char *hold;
3030 int vtypenums[2];
3032 *retvptrbase = DEBUG_TYPE_NULL;
3033 *retownvptr = false;
3035 orig = *pp;
3037 /* If we are positioned at a ';', then skip it. */
3038 if (**pp == ';')
3039 ++*pp;
3041 if (**pp != '~')
3042 return true;
3044 ++*pp;
3046 if (**pp == '=' || **pp == '+' || **pp == '-')
3048 /* Obsolete flags that used to indicate the presence of
3049 constructors and/or destructors. */
3050 ++*pp;
3053 if (**pp != '%')
3054 return true;
3056 ++*pp;
3058 hold = *pp;
3060 /* The next number is the type number of the base class (possibly
3061 our own class) which supplies the vtable for this class. */
3062 if (! parse_stab_type_number (pp, vtypenums))
3063 return false;
3065 if (vtypenums[0] == typenums[0]
3066 && vtypenums[1] == typenums[1])
3067 *retownvptr = true;
3068 else
3070 debug_type vtype;
3071 const char *p;
3073 *pp = hold;
3075 vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3076 (debug_type **) NULL);
3077 for (p = *pp; *p != ';' && *p != '\0'; p++)
3079 if (*p != ';')
3081 bad_stab (orig);
3082 return false;
3085 *retvptrbase = vtype;
3087 *pp = p + 1;
3090 return true;
3093 /* Read a definition of an array type. */
3095 static debug_type
3096 parse_stab_array_type (dhandle, info, pp, stringp)
3097 PTR dhandle;
3098 struct stab_handle *info;
3099 const char **pp;
3100 boolean stringp;
3102 const char *orig;
3103 const char *p;
3104 int typenums[2];
3105 debug_type index_type;
3106 boolean adjustable;
3107 bfd_signed_vma lower, upper;
3108 debug_type element_type;
3110 /* Format of an array type:
3111 "ar<index type>;lower;upper;<array_contents_type>".
3112 OS9000: "arlower,upper;<array_contents_type>".
3114 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3115 for these, produce a type like float[][]. */
3117 orig = *pp;
3119 /* FIXME: gdb checks os9k_stabs here. */
3121 /* If the index type is type 0, we take it as int. */
3122 p = *pp;
3123 if (! parse_stab_type_number (&p, typenums))
3124 return DEBUG_TYPE_NULL;
3125 if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
3127 index_type = debug_find_named_type (dhandle, "int");
3128 if (index_type == DEBUG_TYPE_NULL)
3130 index_type = debug_make_int_type (dhandle, 4, false);
3131 if (index_type == DEBUG_TYPE_NULL)
3132 return DEBUG_TYPE_NULL;
3134 *pp = p;
3136 else
3138 index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3139 (debug_type **) NULL);
3142 if (**pp != ';')
3144 bad_stab (orig);
3145 return DEBUG_TYPE_NULL;
3147 ++*pp;
3149 adjustable = false;
3151 if (! ISDIGIT (**pp) && **pp != '-')
3153 ++*pp;
3154 adjustable = true;
3157 lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3158 if (**pp != ';')
3160 bad_stab (orig);
3161 return DEBUG_TYPE_NULL;
3163 ++*pp;
3165 if (! ISDIGIT (**pp) && **pp != '-')
3167 ++*pp;
3168 adjustable = true;
3171 upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3172 if (**pp != ';')
3174 bad_stab (orig);
3175 return DEBUG_TYPE_NULL;
3177 ++*pp;
3179 element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3180 (debug_type **) NULL);
3181 if (element_type == DEBUG_TYPE_NULL)
3182 return DEBUG_TYPE_NULL;
3184 if (adjustable)
3186 lower = 0;
3187 upper = -1;
3190 return debug_make_array_type (dhandle, element_type, index_type, lower,
3191 upper, stringp);
3194 /* This struct holds information about files we have seen using
3195 N_BINCL. */
3197 struct bincl_file
3199 /* The next N_BINCL file. */
3200 struct bincl_file *next;
3201 /* The next N_BINCL on the stack. */
3202 struct bincl_file *next_stack;
3203 /* The file name. */
3204 const char *name;
3205 /* The hash value. */
3206 bfd_vma hash;
3207 /* The file index. */
3208 unsigned int file;
3209 /* The list of types defined in this file. */
3210 struct stab_types *file_types;
3213 /* Start a new N_BINCL file, pushing it onto the stack. */
3215 static void
3216 push_bincl (info, name, hash)
3217 struct stab_handle *info;
3218 const char *name;
3219 bfd_vma hash;
3221 struct bincl_file *n;
3223 n = (struct bincl_file *) xmalloc (sizeof *n);
3224 n->next = info->bincl_list;
3225 n->next_stack = info->bincl_stack;
3226 n->name = name;
3227 n->hash = hash;
3228 n->file = info->files;
3229 n->file_types = NULL;
3230 info->bincl_list = n;
3231 info->bincl_stack = n;
3233 ++info->files;
3234 info->file_types = ((struct stab_types **)
3235 xrealloc ((PTR) info->file_types,
3236 (info->files
3237 * sizeof *info->file_types)));
3238 info->file_types[n->file] = NULL;
3241 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3242 stack. */
3244 static const char *
3245 pop_bincl (info)
3246 struct stab_handle *info;
3248 struct bincl_file *o;
3250 o = info->bincl_stack;
3251 if (o == NULL)
3252 return info->main_filename;
3253 info->bincl_stack = o->next_stack;
3255 o->file_types = info->file_types[o->file];
3257 if (info->bincl_stack == NULL)
3258 return info->main_filename;
3259 return info->bincl_stack->name;
3262 /* Handle an N_EXCL: get the types from the corresponding N_BINCL. */
3264 static boolean
3265 find_excl (info, name, hash)
3266 struct stab_handle *info;
3267 const char *name;
3268 bfd_vma hash;
3270 struct bincl_file *l;
3272 ++info->files;
3273 info->file_types = ((struct stab_types **)
3274 xrealloc ((PTR) info->file_types,
3275 (info->files
3276 * sizeof *info->file_types)));
3278 for (l = info->bincl_list; l != NULL; l = l->next)
3279 if (l->hash == hash && strcmp (l->name, name) == 0)
3280 break;
3281 if (l == NULL)
3283 warn_stab (name, _("Undefined N_EXCL"));
3284 info->file_types[info->files - 1] = NULL;
3285 return true;
3288 info->file_types[info->files - 1] = l->file_types;
3290 return true;
3293 /* Handle a variable definition. gcc emits variable definitions for a
3294 block before the N_LBRAC, so we must hold onto them until we see
3295 it. The SunPRO compiler emits variable definitions after the
3296 N_LBRAC, so we can call debug_record_variable immediately. */
3298 static boolean
3299 stab_record_variable (dhandle, info, name, type, kind, val)
3300 PTR dhandle;
3301 struct stab_handle *info;
3302 const char *name;
3303 debug_type type;
3304 enum debug_var_kind kind;
3305 bfd_vma val;
3307 struct stab_pending_var *v;
3309 if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3310 || ! info->within_function
3311 || (info->gcc_compiled == 0 && info->n_opt_found))
3312 return debug_record_variable (dhandle, name, type, kind, val);
3314 v = (struct stab_pending_var *) xmalloc (sizeof *v);
3315 memset (v, 0, sizeof *v);
3317 v->next = info->pending;
3318 v->name = name;
3319 v->type = type;
3320 v->kind = kind;
3321 v->val = val;
3322 info->pending = v;
3324 return true;
3327 /* Emit pending variable definitions. This is called after we see the
3328 N_LBRAC that starts the block. */
3330 static boolean
3331 stab_emit_pending_vars (dhandle, info)
3332 PTR dhandle;
3333 struct stab_handle *info;
3335 struct stab_pending_var *v;
3337 v = info->pending;
3338 while (v != NULL)
3340 struct stab_pending_var *next;
3342 if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3343 return false;
3345 next = v->next;
3346 free (v);
3347 v = next;
3350 info->pending = NULL;
3352 return true;
3355 /* Find the slot for a type in the database. */
3357 static debug_type *
3358 stab_find_slot (info, typenums)
3359 struct stab_handle *info;
3360 const int *typenums;
3362 int filenum;
3363 int index;
3364 struct stab_types **ps;
3366 filenum = typenums[0];
3367 index = typenums[1];
3369 if (filenum < 0 || (unsigned int) filenum >= info->files)
3371 fprintf (stderr, _("Type file number %d out of range\n"), filenum);
3372 return NULL;
3374 if (index < 0)
3376 fprintf (stderr, _("Type index number %d out of range\n"), index);
3377 return NULL;
3380 ps = info->file_types + filenum;
3382 while (index >= STAB_TYPES_SLOTS)
3384 if (*ps == NULL)
3386 *ps = (struct stab_types *) xmalloc (sizeof **ps);
3387 memset (*ps, 0, sizeof **ps);
3389 ps = &(*ps)->next;
3390 index -= STAB_TYPES_SLOTS;
3392 if (*ps == NULL)
3394 *ps = (struct stab_types *) xmalloc (sizeof **ps);
3395 memset (*ps, 0, sizeof **ps);
3398 return (*ps)->types + index;
3401 /* Find a type given a type number. If the type has not been
3402 allocated yet, create an indirect type. */
3404 static debug_type
3405 stab_find_type (dhandle, info, typenums)
3406 PTR dhandle;
3407 struct stab_handle *info;
3408 const int *typenums;
3410 debug_type *slot;
3412 if (typenums[0] == 0 && typenums[1] < 0)
3414 /* A negative type number indicates an XCOFF builtin type. */
3415 return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3418 slot = stab_find_slot (info, typenums);
3419 if (slot == NULL)
3420 return DEBUG_TYPE_NULL;
3422 if (*slot == DEBUG_TYPE_NULL)
3423 return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3425 return *slot;
3428 /* Record that a given type number refers to a given type. */
3430 static boolean
3431 stab_record_type (dhandle, info, typenums, type)
3432 PTR dhandle ATTRIBUTE_UNUSED;
3433 struct stab_handle *info;
3434 const int *typenums;
3435 debug_type type;
3437 debug_type *slot;
3439 slot = stab_find_slot (info, typenums);
3440 if (slot == NULL)
3441 return false;
3443 /* gdb appears to ignore type redefinitions, so we do as well. */
3445 *slot = type;
3447 return true;
3450 /* Return an XCOFF builtin type. */
3452 static debug_type
3453 stab_xcoff_builtin_type (dhandle, info, typenum)
3454 PTR dhandle;
3455 struct stab_handle *info;
3456 int typenum;
3458 debug_type rettype;
3459 const char *name;
3461 if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3463 fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
3464 return DEBUG_TYPE_NULL;
3466 if (info->xcoff_types[-typenum] != NULL)
3467 return info->xcoff_types[-typenum];
3469 switch (-typenum)
3471 case 1:
3472 /* The size of this and all the other types are fixed, defined
3473 by the debugging format. */
3474 name = "int";
3475 rettype = debug_make_int_type (dhandle, 4, false);
3476 break;
3477 case 2:
3478 name = "char";
3479 rettype = debug_make_int_type (dhandle, 1, false);
3480 break;
3481 case 3:
3482 name = "short";
3483 rettype = debug_make_int_type (dhandle, 2, false);
3484 break;
3485 case 4:
3486 name = "long";
3487 rettype = debug_make_int_type (dhandle, 4, false);
3488 break;
3489 case 5:
3490 name = "unsigned char";
3491 rettype = debug_make_int_type (dhandle, 1, true);
3492 break;
3493 case 6:
3494 name = "signed char";
3495 rettype = debug_make_int_type (dhandle, 1, false);
3496 break;
3497 case 7:
3498 name = "unsigned short";
3499 rettype = debug_make_int_type (dhandle, 2, true);
3500 break;
3501 case 8:
3502 name = "unsigned int";
3503 rettype = debug_make_int_type (dhandle, 4, true);
3504 break;
3505 case 9:
3506 name = "unsigned";
3507 rettype = debug_make_int_type (dhandle, 4, true);
3508 case 10:
3509 name = "unsigned long";
3510 rettype = debug_make_int_type (dhandle, 4, true);
3511 break;
3512 case 11:
3513 name = "void";
3514 rettype = debug_make_void_type (dhandle);
3515 break;
3516 case 12:
3517 /* IEEE single precision (32 bit). */
3518 name = "float";
3519 rettype = debug_make_float_type (dhandle, 4);
3520 break;
3521 case 13:
3522 /* IEEE double precision (64 bit). */
3523 name = "double";
3524 rettype = debug_make_float_type (dhandle, 8);
3525 break;
3526 case 14:
3527 /* This is an IEEE double on the RS/6000, and different machines
3528 with different sizes for "long double" should use different
3529 negative type numbers. See stabs.texinfo. */
3530 name = "long double";
3531 rettype = debug_make_float_type (dhandle, 8);
3532 break;
3533 case 15:
3534 name = "integer";
3535 rettype = debug_make_int_type (dhandle, 4, false);
3536 break;
3537 case 16:
3538 name = "boolean";
3539 rettype = debug_make_bool_type (dhandle, 4);
3540 break;
3541 case 17:
3542 name = "short real";
3543 rettype = debug_make_float_type (dhandle, 4);
3544 break;
3545 case 18:
3546 name = "real";
3547 rettype = debug_make_float_type (dhandle, 8);
3548 break;
3549 case 19:
3550 /* FIXME */
3551 name = "stringptr";
3552 rettype = NULL;
3553 break;
3554 case 20:
3555 /* FIXME */
3556 name = "character";
3557 rettype = debug_make_int_type (dhandle, 1, true);
3558 break;
3559 case 21:
3560 name = "logical*1";
3561 rettype = debug_make_bool_type (dhandle, 1);
3562 break;
3563 case 22:
3564 name = "logical*2";
3565 rettype = debug_make_bool_type (dhandle, 2);
3566 break;
3567 case 23:
3568 name = "logical*4";
3569 rettype = debug_make_bool_type (dhandle, 4);
3570 break;
3571 case 24:
3572 name = "logical";
3573 rettype = debug_make_bool_type (dhandle, 4);
3574 break;
3575 case 25:
3576 /* Complex type consisting of two IEEE single precision values. */
3577 name = "complex";
3578 rettype = debug_make_complex_type (dhandle, 8);
3579 break;
3580 case 26:
3581 /* Complex type consisting of two IEEE double precision values. */
3582 name = "double complex";
3583 rettype = debug_make_complex_type (dhandle, 16);
3584 break;
3585 case 27:
3586 name = "integer*1";
3587 rettype = debug_make_int_type (dhandle, 1, false);
3588 break;
3589 case 28:
3590 name = "integer*2";
3591 rettype = debug_make_int_type (dhandle, 2, false);
3592 break;
3593 case 29:
3594 name = "integer*4";
3595 rettype = debug_make_int_type (dhandle, 4, false);
3596 break;
3597 case 30:
3598 /* FIXME */
3599 name = "wchar";
3600 rettype = debug_make_int_type (dhandle, 2, false);
3601 break;
3602 case 31:
3603 name = "long long";
3604 rettype = debug_make_int_type (dhandle, 8, false);
3605 break;
3606 case 32:
3607 name = "unsigned long long";
3608 rettype = debug_make_int_type (dhandle, 8, true);
3609 break;
3610 case 33:
3611 name = "logical*8";
3612 rettype = debug_make_bool_type (dhandle, 8);
3613 break;
3614 case 34:
3615 name = "integer*8";
3616 rettype = debug_make_int_type (dhandle, 8, false);
3617 break;
3618 default:
3619 abort ();
3622 rettype = debug_name_type (dhandle, name, rettype);
3624 info->xcoff_types[-typenum] = rettype;
3626 return rettype;
3629 /* Find or create a tagged type. */
3631 static debug_type
3632 stab_find_tagged_type (dhandle, info, p, len, kind)
3633 PTR dhandle;
3634 struct stab_handle *info;
3635 const char *p;
3636 int len;
3637 enum debug_type_kind kind;
3639 char *name;
3640 debug_type dtype;
3641 struct stab_tag *st;
3643 name = savestring (p, len);
3645 /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3646 namespace. This is right for C, and I don't know how to handle
3647 other languages. FIXME. */
3648 dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3649 if (dtype != DEBUG_TYPE_NULL)
3651 free (name);
3652 return dtype;
3655 /* We need to allocate an entry on the undefined tag list. */
3656 for (st = info->tags; st != NULL; st = st->next)
3658 if (st->name[0] == name[0]
3659 && strcmp (st->name, name) == 0)
3661 if (st->kind == DEBUG_KIND_ILLEGAL)
3662 st->kind = kind;
3663 free (name);
3664 break;
3667 if (st == NULL)
3669 st = (struct stab_tag *) xmalloc (sizeof *st);
3670 memset (st, 0, sizeof *st);
3672 st->next = info->tags;
3673 st->name = name;
3674 st->kind = kind;
3675 st->slot = DEBUG_TYPE_NULL;
3676 st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3677 info->tags = st;
3680 return st->type;
3683 /* In order to get the correct argument types for a stubbed method, we
3684 need to extract the argument types from a C++ mangled string.
3685 Since the argument types can refer back to the return type, this
3686 means that we must demangle the entire physical name. In gdb this
3687 is done by calling cplus_demangle and running the results back
3688 through the C++ expression parser. Since we have no expression
3689 parser, we must duplicate much of the work of cplus_demangle here.
3691 We assume that GNU style demangling is used, since this is only
3692 done for method stubs, and only g++ should output that form of
3693 debugging information. */
3695 /* This structure is used to hold a pointer to type information which
3696 demangling a string. */
3698 struct stab_demangle_typestring
3700 /* The start of the type. This is not null terminated. */
3701 const char *typestring;
3702 /* The length of the type. */
3703 unsigned int len;
3706 /* This structure is used to hold information while demangling a
3707 string. */
3709 struct stab_demangle_info
3711 /* The debugging information handle. */
3712 PTR dhandle;
3713 /* The stab information handle. */
3714 struct stab_handle *info;
3715 /* The array of arguments we are building. */
3716 debug_type *args;
3717 /* Whether the method takes a variable number of arguments. */
3718 boolean varargs;
3719 /* The array of types we have remembered. */
3720 struct stab_demangle_typestring *typestrings;
3721 /* The number of typestrings. */
3722 unsigned int typestring_count;
3723 /* The number of typestring slots we have allocated. */
3724 unsigned int typestring_alloc;
3727 static void stab_bad_demangle PARAMS ((const char *));
3728 static unsigned int stab_demangle_count PARAMS ((const char **));
3729 static boolean stab_demangle_get_count
3730 PARAMS ((const char **, unsigned int *));
3731 static boolean stab_demangle_prefix
3732 PARAMS ((struct stab_demangle_info *, const char **));
3733 static boolean stab_demangle_function_name
3734 PARAMS ((struct stab_demangle_info *, const char **, const char *));
3735 static boolean stab_demangle_signature
3736 PARAMS ((struct stab_demangle_info *, const char **));
3737 static boolean stab_demangle_qualified
3738 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3739 static boolean stab_demangle_template
3740 PARAMS ((struct stab_demangle_info *, const char **, char **));
3741 static boolean stab_demangle_class
3742 PARAMS ((struct stab_demangle_info *, const char **, const char **));
3743 static boolean stab_demangle_args
3744 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3745 boolean *));
3746 static boolean stab_demangle_arg
3747 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3748 unsigned int *, unsigned int *));
3749 static boolean stab_demangle_type
3750 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3751 static boolean stab_demangle_fund_type
3752 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3753 static boolean stab_demangle_remember_type
3754 PARAMS ((struct stab_demangle_info *, const char *, int));
3756 /* Warn about a bad demangling. */
3758 static void
3759 stab_bad_demangle (s)
3760 const char *s;
3762 fprintf (stderr, _("bad mangled name `%s'\n"), s);
3765 /* Get a count from a stab string. */
3767 static unsigned int
3768 stab_demangle_count (pp)
3769 const char **pp;
3771 unsigned int count;
3773 count = 0;
3774 while (ISDIGIT (**pp))
3776 count *= 10;
3777 count += **pp - '0';
3778 ++*pp;
3780 return count;
3783 /* Require a count in a string. The count may be multiple digits, in
3784 which case it must end in an underscore. */
3786 static boolean
3787 stab_demangle_get_count (pp, pi)
3788 const char **pp;
3789 unsigned int *pi;
3791 if (! ISDIGIT (**pp))
3792 return false;
3794 *pi = **pp - '0';
3795 ++*pp;
3796 if (ISDIGIT (**pp))
3798 unsigned int count;
3799 const char *p;
3801 count = *pi;
3802 p = *pp;
3805 count *= 10;
3806 count += *p - '0';
3807 ++p;
3809 while (ISDIGIT (*p));
3810 if (*p == '_')
3812 *pp = p + 1;
3813 *pi = count;
3817 return true;
3820 /* This function demangles a physical name, returning a NULL
3821 terminated array of argument types. */
3823 static debug_type *
3824 stab_demangle_argtypes (dhandle, info, physname, pvarargs)
3825 PTR dhandle;
3826 struct stab_handle *info;
3827 const char *physname;
3828 boolean *pvarargs;
3830 struct stab_demangle_info minfo;
3832 minfo.dhandle = dhandle;
3833 minfo.info = info;
3834 minfo.args = NULL;
3835 minfo.varargs = false;
3836 minfo.typestring_alloc = 10;
3837 minfo.typestrings = ((struct stab_demangle_typestring *)
3838 xmalloc (minfo.typestring_alloc
3839 * sizeof *minfo.typestrings));
3840 minfo.typestring_count = 0;
3842 /* cplus_demangle checks for special GNU mangled forms, but we can't
3843 see any of them in mangled method argument types. */
3845 if (! stab_demangle_prefix (&minfo, &physname))
3846 goto error_return;
3848 if (*physname != '\0')
3850 if (! stab_demangle_signature (&minfo, &physname))
3851 goto error_return;
3854 free (minfo.typestrings);
3855 minfo.typestrings = NULL;
3857 if (minfo.args == NULL)
3858 fprintf (stderr, _("no argument types in mangled string\n"));
3860 *pvarargs = minfo.varargs;
3861 return minfo.args;
3863 error_return:
3864 if (minfo.typestrings != NULL)
3865 free (minfo.typestrings);
3866 return NULL;
3869 /* Demangle the prefix of the mangled name. */
3871 static boolean
3872 stab_demangle_prefix (minfo, pp)
3873 struct stab_demangle_info *minfo;
3874 const char **pp;
3876 const char *scan;
3877 unsigned int i;
3879 /* cplus_demangle checks for global constructors and destructors,
3880 but we can't see them in mangled argument types. */
3882 /* Look for `__'. */
3883 scan = *pp;
3886 scan = strchr (scan, '_');
3888 while (scan != NULL && *++scan != '_');
3890 if (scan == NULL)
3892 stab_bad_demangle (*pp);
3893 return false;
3896 --scan;
3898 /* We found `__'; move ahead to the last contiguous `__' pair. */
3899 i = strspn (scan, "_");
3900 if (i > 2)
3901 scan += i - 2;
3903 if (scan == *pp
3904 && (ISDIGIT (scan[2])
3905 || scan[2] == 'Q'
3906 || scan[2] == 't'))
3908 /* This is a GNU style constructor name. */
3909 *pp = scan + 2;
3910 return true;
3912 else if (scan == *pp
3913 && ! ISDIGIT (scan[2])
3914 && scan[2] != 't')
3916 /* Look for the `__' that separates the prefix from the
3917 signature. */
3918 while (*scan == '_')
3919 ++scan;
3920 scan = strstr (scan, "__");
3921 if (scan == NULL || scan[2] == '\0')
3923 stab_bad_demangle (*pp);
3924 return false;
3927 return stab_demangle_function_name (minfo, pp, scan);
3929 else if (scan[2] != '\0')
3931 /* The name doesn't start with `__', but it does contain `__'. */
3932 return stab_demangle_function_name (minfo, pp, scan);
3934 else
3936 stab_bad_demangle (*pp);
3937 return false;
3939 /*NOTREACHED*/
3942 /* Demangle a function name prefix. The scan argument points to the
3943 double underscore which separates the function name from the
3944 signature. */
3946 static boolean
3947 stab_demangle_function_name (minfo, pp, scan)
3948 struct stab_demangle_info *minfo;
3949 const char **pp;
3950 const char *scan;
3952 const char *name;
3954 /* The string from *pp to scan is the name of the function. We
3955 don't care about the name, since we just looking for argument
3956 types. However, for conversion operators, the name may include a
3957 type which we must remember in order to handle backreferences. */
3959 name = *pp;
3960 *pp = scan + 2;
3962 if (*pp - name >= 5
3963 && strncmp (name, "type", 4) == 0
3964 && (name[4] == '$' || name[4] == '.'))
3966 const char *tem;
3968 /* This is a type conversion operator. */
3969 tem = name + 5;
3970 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3971 return false;
3973 else if (name[0] == '_'
3974 && name[1] == '_'
3975 && name[2] == 'o'
3976 && name[3] == 'p')
3978 const char *tem;
3980 /* This is a type conversion operator. */
3981 tem = name + 4;
3982 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3983 return false;
3986 return true;
3989 /* Demangle the signature. This is where the argument types are
3990 found. */
3992 static boolean
3993 stab_demangle_signature (minfo, pp)
3994 struct stab_demangle_info *minfo;
3995 const char **pp;
3997 const char *orig;
3998 boolean expect_func, func_done;
3999 const char *hold;
4001 orig = *pp;
4003 expect_func = false;
4004 func_done = false;
4005 hold = NULL;
4007 while (**pp != '\0')
4009 switch (**pp)
4011 case 'Q':
4012 hold = *pp;
4013 if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
4014 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4015 return false;
4016 expect_func = true;
4017 hold = NULL;
4018 break;
4020 case 'S':
4021 /* Static member function. FIXME: Can this happen? */
4022 if (hold == NULL)
4023 hold = *pp;
4024 ++*pp;
4025 break;
4027 case 'C':
4028 /* Const member function. */
4029 if (hold == NULL)
4030 hold = *pp;
4031 ++*pp;
4032 break;
4034 case '0': case '1': case '2': case '3': case '4':
4035 case '5': case '6': case '7': case '8': case '9':
4036 if (hold == NULL)
4037 hold = *pp;
4038 if (! stab_demangle_class (minfo, pp, (const char **) NULL)
4039 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4040 return false;
4041 expect_func = true;
4042 hold = NULL;
4043 break;
4045 case 'F':
4046 /* Function. I don't know if this actually happens with g++
4047 output. */
4048 hold = NULL;
4049 func_done = true;
4050 ++*pp;
4051 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4052 return false;
4053 break;
4055 case 't':
4056 /* Template. */
4057 if (hold == NULL)
4058 hold = *pp;
4059 if (! stab_demangle_template (minfo, pp, (char **) NULL)
4060 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4061 return false;
4062 hold = NULL;
4063 expect_func = true;
4064 break;
4066 case '_':
4067 /* At the outermost level, we cannot have a return type
4068 specified, so if we run into another '_' at this point we
4069 are dealing with a mangled name that is either bogus, or
4070 has been mangled by some algorithm we don't know how to
4071 deal with. So just reject the entire demangling. */
4072 stab_bad_demangle (orig);
4073 return false;
4075 default:
4076 /* Assume we have stumbled onto the first outermost function
4077 argument token, and start processing args. */
4078 func_done = true;
4079 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4080 return false;
4081 break;
4084 if (expect_func)
4086 func_done = true;
4087 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4088 return false;
4092 if (! func_done)
4094 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4095 bar__3fooi is 'foo::bar(int)'. We get here when we find the
4096 first case, and need to ensure that the '(void)' gets added
4097 to the current declp. */
4098 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4099 return false;
4102 return true;
4105 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4106 mangled form of "Outer::Inner". */
4108 static boolean
4109 stab_demangle_qualified (minfo, pp, ptype)
4110 struct stab_demangle_info *minfo;
4111 const char **pp;
4112 debug_type *ptype;
4114 const char *orig;
4115 const char *p;
4116 unsigned int qualifiers;
4117 debug_type context;
4119 orig = *pp;
4121 switch ((*pp)[1])
4123 case '_':
4124 /* GNU mangled name with more than 9 classes. The count is
4125 preceded by an underscore (to distinguish it from the <= 9
4126 case) and followed by an underscore. */
4127 p = *pp + 2;
4128 if (! ISDIGIT (*p) || *p == '0')
4130 stab_bad_demangle (orig);
4131 return false;
4133 qualifiers = atoi (p);
4134 while (ISDIGIT (*p))
4135 ++p;
4136 if (*p != '_')
4138 stab_bad_demangle (orig);
4139 return false;
4141 *pp = p + 1;
4142 break;
4144 case '1': case '2': case '3': case '4': case '5':
4145 case '6': case '7': case '8': case '9':
4146 qualifiers = (*pp)[1] - '0';
4147 /* Skip an optional underscore after the count. */
4148 if ((*pp)[2] == '_')
4149 ++*pp;
4150 *pp += 2;
4151 break;
4153 case '0':
4154 default:
4155 stab_bad_demangle (orig);
4156 return false;
4159 context = DEBUG_TYPE_NULL;
4161 /* Pick off the names. */
4162 while (qualifiers-- > 0)
4164 if (**pp == '_')
4165 ++*pp;
4166 if (**pp == 't')
4168 char *name;
4170 if (! stab_demangle_template (minfo, pp,
4171 ptype != NULL ? &name : NULL))
4172 return false;
4174 if (ptype != NULL)
4176 context = stab_find_tagged_type (minfo->dhandle, minfo->info,
4177 name, strlen (name),
4178 DEBUG_KIND_CLASS);
4179 free (name);
4180 if (context == DEBUG_TYPE_NULL)
4181 return false;
4184 else
4186 unsigned int len;
4188 len = stab_demangle_count (pp);
4189 if (strlen (*pp) < len)
4191 stab_bad_demangle (orig);
4192 return false;
4195 if (ptype != NULL)
4197 const debug_field *fields;
4199 fields = NULL;
4200 if (context != DEBUG_TYPE_NULL)
4201 fields = debug_get_fields (minfo->dhandle, context);
4203 context = DEBUG_TYPE_NULL;
4205 if (fields != NULL)
4207 char *name;
4209 /* Try to find the type by looking through the
4210 fields of context until we find a field with the
4211 same type. This ought to work for a class
4212 defined within a class, but it won't work for,
4213 e.g., an enum defined within a class. stabs does
4214 not give us enough information to figure out the
4215 latter case. */
4217 name = savestring (*pp, len);
4219 for (; *fields != DEBUG_FIELD_NULL; fields++)
4221 debug_type ft;
4222 const char *dn;
4224 ft = debug_get_field_type (minfo->dhandle, *fields);
4225 if (ft == NULL)
4226 return false;
4227 dn = debug_get_type_name (minfo->dhandle, ft);
4228 if (dn != NULL && strcmp (dn, name) == 0)
4230 context = ft;
4231 break;
4235 free (name);
4238 if (context == DEBUG_TYPE_NULL)
4240 /* We have to fall back on finding the type by name.
4241 If there are more types to come, then this must
4242 be a class. Otherwise, it could be anything. */
4244 if (qualifiers == 0)
4246 char *name;
4248 name = savestring (*pp, len);
4249 context = debug_find_named_type (minfo->dhandle,
4250 name);
4251 free (name);
4254 if (context == DEBUG_TYPE_NULL)
4256 context = stab_find_tagged_type (minfo->dhandle,
4257 minfo->info,
4258 *pp, len,
4259 (qualifiers == 0
4260 ? DEBUG_KIND_ILLEGAL
4261 : DEBUG_KIND_CLASS));
4262 if (context == DEBUG_TYPE_NULL)
4263 return false;
4268 *pp += len;
4272 if (ptype != NULL)
4273 *ptype = context;
4275 return true;
4278 /* Demangle a template. If PNAME is not NULL, this sets *PNAME to a
4279 string representation of the template. */
4281 static boolean
4282 stab_demangle_template (minfo, pp, pname)
4283 struct stab_demangle_info *minfo;
4284 const char **pp;
4285 char **pname;
4287 const char *orig;
4288 unsigned int r, i;
4290 orig = *pp;
4292 ++*pp;
4294 /* Skip the template name. */
4295 r = stab_demangle_count (pp);
4296 if (r == 0 || strlen (*pp) < r)
4298 stab_bad_demangle (orig);
4299 return false;
4301 *pp += r;
4303 /* Get the size of the parameter list. */
4304 if (stab_demangle_get_count (pp, &r) == 0)
4306 stab_bad_demangle (orig);
4307 return false;
4310 for (i = 0; i < r; i++)
4312 if (**pp == 'Z')
4314 /* This is a type parameter. */
4315 ++*pp;
4316 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4317 return false;
4319 else
4321 const char *old_p;
4322 boolean pointerp, realp, integralp, charp, boolp;
4323 boolean done;
4325 old_p = *pp;
4326 pointerp = false;
4327 realp = false;
4328 integralp = false;
4329 charp = false;
4330 boolp = false;
4331 done = false;
4333 /* This is a value parameter. */
4335 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4336 return false;
4338 while (*old_p != '\0' && ! done)
4340 switch (*old_p)
4342 case 'P':
4343 case 'p':
4344 case 'R':
4345 pointerp = true;
4346 done = true;
4347 break;
4348 case 'C': /* Const. */
4349 case 'S': /* Signed. */
4350 case 'U': /* Unsigned. */
4351 case 'V': /* Volatile. */
4352 case 'F': /* Function. */
4353 case 'M': /* Member function. */
4354 case 'O': /* ??? */
4355 ++old_p;
4356 break;
4357 case 'Q': /* Qualified name. */
4358 integralp = true;
4359 done = true;
4360 break;
4361 case 'T': /* Remembered type. */
4362 abort ();
4363 case 'v': /* Void. */
4364 abort ();
4365 case 'x': /* Long long. */
4366 case 'l': /* Long. */
4367 case 'i': /* Int. */
4368 case 's': /* Short. */
4369 case 'w': /* Wchar_t. */
4370 integralp = true;
4371 done = true;
4372 break;
4373 case 'b': /* Bool. */
4374 boolp = true;
4375 done = true;
4376 break;
4377 case 'c': /* Char. */
4378 charp = true;
4379 done = true;
4380 break;
4381 case 'r': /* Long double. */
4382 case 'd': /* Double. */
4383 case 'f': /* Float. */
4384 realp = true;
4385 done = true;
4386 break;
4387 default:
4388 /* Assume it's a user defined integral type. */
4389 integralp = true;
4390 done = true;
4391 break;
4395 if (integralp)
4397 if (**pp == 'm')
4398 ++*pp;
4399 while (ISDIGIT (**pp))
4400 ++*pp;
4402 else if (charp)
4404 unsigned int val;
4406 if (**pp == 'm')
4407 ++*pp;
4408 val = stab_demangle_count (pp);
4409 if (val == 0)
4411 stab_bad_demangle (orig);
4412 return false;
4415 else if (boolp)
4417 unsigned int val;
4419 val = stab_demangle_count (pp);
4420 if (val != 0 && val != 1)
4422 stab_bad_demangle (orig);
4423 return false;
4426 else if (realp)
4428 if (**pp == 'm')
4429 ++*pp;
4430 while (ISDIGIT (**pp))
4431 ++*pp;
4432 if (**pp == '.')
4434 ++*pp;
4435 while (ISDIGIT (**pp))
4436 ++*pp;
4438 if (**pp == 'e')
4440 ++*pp;
4441 while (ISDIGIT (**pp))
4442 ++*pp;
4445 else if (pointerp)
4447 unsigned int len;
4449 if (! stab_demangle_get_count (pp, &len))
4451 stab_bad_demangle (orig);
4452 return false;
4454 *pp += len;
4459 /* We can translate this to a string fairly easily by invoking the
4460 regular demangling routine. */
4461 if (pname != NULL)
4463 char *s1, *s2, *s3, *s4 = NULL;
4464 char *from, *to;
4466 s1 = savestring (orig, *pp - orig);
4468 s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
4470 free (s1);
4472 s3 = cplus_demangle (s2, DMGL_ANSI);
4474 free (s2);
4476 if (s3 != NULL)
4477 s4 = strstr (s3, "::NoSuchStrinG");
4478 if (s3 == NULL || s4 == NULL)
4480 stab_bad_demangle (orig);
4481 if (s3 != NULL)
4482 free (s3);
4483 return false;
4486 /* Eliminating all spaces, except those between > characters,
4487 makes it more likely that the demangled name will match the
4488 name which g++ used as the structure name. */
4489 for (from = to = s3; from != s4; ++from)
4490 if (*from != ' '
4491 || (from[1] == '>' && from > s3 && from[-1] == '>'))
4492 *to++ = *from;
4494 *pname = savestring (s3, to - s3);
4496 free (s3);
4499 return true;
4502 /* Demangle a class name. */
4504 static boolean
4505 stab_demangle_class (minfo, pp, pstart)
4506 struct stab_demangle_info *minfo ATTRIBUTE_UNUSED;
4507 const char **pp;
4508 const char **pstart;
4510 const char *orig;
4511 unsigned int n;
4513 orig = *pp;
4515 n = stab_demangle_count (pp);
4516 if (strlen (*pp) < n)
4518 stab_bad_demangle (orig);
4519 return false;
4522 if (pstart != NULL)
4523 *pstart = *pp;
4525 *pp += n;
4527 return true;
4530 /* Demangle function arguments. If the pargs argument is not NULL, it
4531 is set to a NULL terminated array holding the arguments. */
4533 static boolean
4534 stab_demangle_args (minfo, pp, pargs, pvarargs)
4535 struct stab_demangle_info *minfo;
4536 const char **pp;
4537 debug_type **pargs;
4538 boolean *pvarargs;
4540 const char *orig;
4541 unsigned int alloc, count;
4543 orig = *pp;
4545 alloc = 10;
4546 if (pargs != NULL)
4548 *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4549 *pvarargs = false;
4551 count = 0;
4553 while (**pp != '_' && **pp != '\0' && **pp != 'e')
4555 if (**pp == 'N' || **pp == 'T')
4557 char temptype;
4558 unsigned int r, t;
4560 temptype = **pp;
4561 ++*pp;
4563 if (temptype == 'T')
4564 r = 1;
4565 else
4567 if (! stab_demangle_get_count (pp, &r))
4569 stab_bad_demangle (orig);
4570 return false;
4574 if (! stab_demangle_get_count (pp, &t))
4576 stab_bad_demangle (orig);
4577 return false;
4580 if (t >= minfo->typestring_count)
4582 stab_bad_demangle (orig);
4583 return false;
4585 while (r-- > 0)
4587 const char *tem;
4589 tem = minfo->typestrings[t].typestring;
4590 if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4591 return false;
4594 else
4596 if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4597 return false;
4601 if (pargs != NULL)
4602 (*pargs)[count] = DEBUG_TYPE_NULL;
4604 if (**pp == 'e')
4606 if (pargs != NULL)
4607 *pvarargs = true;
4608 ++*pp;
4611 return true;
4614 /* Demangle a single argument. */
4616 static boolean
4617 stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4618 struct stab_demangle_info *minfo;
4619 const char **pp;
4620 debug_type **pargs;
4621 unsigned int *pcount;
4622 unsigned int *palloc;
4624 const char *start;
4625 debug_type type;
4627 start = *pp;
4628 if (! stab_demangle_type (minfo, pp,
4629 pargs == NULL ? (debug_type *) NULL : &type)
4630 || ! stab_demangle_remember_type (minfo, start, *pp - start))
4631 return false;
4633 if (pargs != NULL)
4635 if (type == DEBUG_TYPE_NULL)
4636 return false;
4638 if (*pcount + 1 >= *palloc)
4640 *palloc += 10;
4641 *pargs = ((debug_type *)
4642 xrealloc (*pargs, *palloc * sizeof **pargs));
4644 (*pargs)[*pcount] = type;
4645 ++*pcount;
4648 return true;
4651 /* Demangle a type. If the ptype argument is not NULL, *ptype is set
4652 to the newly allocated type. */
4654 static boolean
4655 stab_demangle_type (minfo, pp, ptype)
4656 struct stab_demangle_info *minfo;
4657 const char **pp;
4658 debug_type *ptype;
4660 const char *orig;
4662 orig = *pp;
4664 switch (**pp)
4666 case 'P':
4667 case 'p':
4668 /* A pointer type. */
4669 ++*pp;
4670 if (! stab_demangle_type (minfo, pp, ptype))
4671 return false;
4672 if (ptype != NULL)
4673 *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4674 break;
4676 case 'R':
4677 /* A reference type. */
4678 ++*pp;
4679 if (! stab_demangle_type (minfo, pp, ptype))
4680 return false;
4681 if (ptype != NULL)
4682 *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4683 break;
4685 case 'A':
4686 /* An array. */
4688 unsigned long high;
4690 ++*pp;
4691 high = 0;
4692 while (**pp != '\0' && **pp != '_')
4694 if (! ISDIGIT (**pp))
4696 stab_bad_demangle (orig);
4697 return false;
4699 high *= 10;
4700 high += **pp - '0';
4701 ++*pp;
4703 if (**pp != '_')
4705 stab_bad_demangle (orig);
4706 return false;
4708 ++*pp;
4710 if (! stab_demangle_type (minfo, pp, ptype))
4711 return false;
4712 if (ptype != NULL)
4714 debug_type int_type;
4716 int_type = debug_find_named_type (minfo->dhandle, "int");
4717 if (int_type == NULL)
4718 int_type = debug_make_int_type (minfo->dhandle, 4, false);
4719 *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4720 0, high, false);
4723 break;
4725 case 'T':
4726 /* A back reference to a remembered type. */
4728 unsigned int i;
4729 const char *p;
4731 ++*pp;
4732 if (! stab_demangle_get_count (pp, &i))
4734 stab_bad_demangle (orig);
4735 return false;
4737 if (i >= minfo->typestring_count)
4739 stab_bad_demangle (orig);
4740 return false;
4742 p = minfo->typestrings[i].typestring;
4743 if (! stab_demangle_type (minfo, &p, ptype))
4744 return false;
4746 break;
4748 case 'F':
4749 /* A function. */
4751 debug_type *args;
4752 boolean varargs;
4754 ++*pp;
4755 if (! stab_demangle_args (minfo, pp,
4756 (ptype == NULL
4757 ? (debug_type **) NULL
4758 : &args),
4759 (ptype == NULL
4760 ? (boolean *) NULL
4761 : &varargs)))
4762 return false;
4763 if (**pp != '_')
4765 /* cplus_demangle will accept a function without a return
4766 type, but I don't know when that will happen, or what
4767 to do if it does. */
4768 stab_bad_demangle (orig);
4769 return false;
4771 ++*pp;
4772 if (! stab_demangle_type (minfo, pp, ptype))
4773 return false;
4774 if (ptype != NULL)
4775 *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4776 varargs);
4779 break;
4781 case 'M':
4782 case 'O':
4784 boolean memberp, constp, volatilep;
4785 debug_type class_type = DEBUG_TYPE_NULL;
4786 debug_type *args;
4787 boolean varargs;
4788 unsigned int n;
4789 const char *name;
4791 memberp = **pp == 'M';
4792 constp = false;
4793 volatilep = false;
4794 args = NULL;
4795 varargs = false;
4797 ++*pp;
4798 if (ISDIGIT (**pp))
4800 n = stab_demangle_count (pp);
4801 if (strlen (*pp) < n)
4803 stab_bad_demangle (orig);
4804 return false;
4806 name = *pp;
4807 *pp += n;
4809 if (ptype != NULL)
4811 class_type = stab_find_tagged_type (minfo->dhandle,
4812 minfo->info,
4813 name, (int) n,
4814 DEBUG_KIND_CLASS);
4815 if (class_type == DEBUG_TYPE_NULL)
4816 return false;
4819 else if (**pp == 'Q')
4821 if (! stab_demangle_qualified (minfo, pp,
4822 (ptype == NULL
4823 ? (debug_type *) NULL
4824 : &class_type)))
4825 return false;
4827 else
4829 stab_bad_demangle (orig);
4830 return false;
4833 if (memberp)
4835 if (**pp == 'C')
4837 constp = true;
4838 ++*pp;
4840 else if (**pp == 'V')
4842 volatilep = true;
4843 ++*pp;
4845 if (**pp != 'F')
4847 stab_bad_demangle (orig);
4848 return false;
4850 ++*pp;
4851 if (! stab_demangle_args (minfo, pp,
4852 (ptype == NULL
4853 ? (debug_type **) NULL
4854 : &args),
4855 (ptype == NULL
4856 ? (boolean *) NULL
4857 : &varargs)))
4858 return false;
4861 if (**pp != '_')
4863 stab_bad_demangle (orig);
4864 return false;
4866 ++*pp;
4868 if (! stab_demangle_type (minfo, pp, ptype))
4869 return false;
4871 if (ptype != NULL)
4873 if (! memberp)
4874 *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4875 *ptype);
4876 else
4878 /* FIXME: We have no way to record constp or
4879 volatilep. */
4880 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4881 class_type, args, varargs);
4885 break;
4887 case 'G':
4888 ++*pp;
4889 if (! stab_demangle_type (minfo, pp, ptype))
4890 return false;
4891 break;
4893 case 'C':
4894 ++*pp;
4895 if (! stab_demangle_type (minfo, pp, ptype))
4896 return false;
4897 if (ptype != NULL)
4898 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4899 break;
4901 case 'Q':
4903 const char *hold;
4905 hold = *pp;
4906 if (! stab_demangle_qualified (minfo, pp, ptype))
4907 return false;
4909 break;
4911 default:
4912 if (! stab_demangle_fund_type (minfo, pp, ptype))
4913 return false;
4914 break;
4917 return true;
4920 /* Demangle a fundamental type. If the ptype argument is not NULL,
4921 *ptype is set to the newly allocated type. */
4923 static boolean
4924 stab_demangle_fund_type (minfo, pp, ptype)
4925 struct stab_demangle_info *minfo;
4926 const char **pp;
4927 debug_type *ptype;
4929 const char *orig;
4930 boolean constp, volatilep, unsignedp, signedp;
4931 boolean done;
4933 orig = *pp;
4935 constp = false;
4936 volatilep = false;
4937 unsignedp = false;
4938 signedp = false;
4940 done = false;
4941 while (! done)
4943 switch (**pp)
4945 case 'C':
4946 constp = true;
4947 ++*pp;
4948 break;
4950 case 'U':
4951 unsignedp = true;
4952 ++*pp;
4953 break;
4955 case 'S':
4956 signedp = true;
4957 ++*pp;
4958 break;
4960 case 'V':
4961 volatilep = true;
4962 ++*pp;
4963 break;
4965 default:
4966 done = true;
4967 break;
4971 switch (**pp)
4973 case '\0':
4974 case '_':
4975 /* cplus_demangle permits this, but I don't know what it means. */
4976 stab_bad_demangle (orig);
4977 break;
4979 case 'v': /* void */
4980 if (ptype != NULL)
4982 *ptype = debug_find_named_type (minfo->dhandle, "void");
4983 if (*ptype == DEBUG_TYPE_NULL)
4984 *ptype = debug_make_void_type (minfo->dhandle);
4986 ++*pp;
4987 break;
4989 case 'x': /* long long */
4990 if (ptype != NULL)
4992 *ptype = debug_find_named_type (minfo->dhandle,
4993 (unsignedp
4994 ? "long long unsigned int"
4995 : "long long int"));
4996 if (*ptype == DEBUG_TYPE_NULL)
4997 *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
4999 ++*pp;
5000 break;
5002 case 'l': /* long */
5003 if (ptype != NULL)
5005 *ptype = debug_find_named_type (minfo->dhandle,
5006 (unsignedp
5007 ? "long unsigned int"
5008 : "long int"));
5009 if (*ptype == DEBUG_TYPE_NULL)
5010 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5012 ++*pp;
5013 break;
5015 case 'i': /* int */
5016 if (ptype != NULL)
5018 *ptype = debug_find_named_type (minfo->dhandle,
5019 (unsignedp
5020 ? "unsigned int"
5021 : "int"));
5022 if (*ptype == DEBUG_TYPE_NULL)
5023 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5025 ++*pp;
5026 break;
5028 case 's': /* short */
5029 if (ptype != NULL)
5031 *ptype = debug_find_named_type (minfo->dhandle,
5032 (unsignedp
5033 ? "short unsigned int"
5034 : "short int"));
5035 if (*ptype == DEBUG_TYPE_NULL)
5036 *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
5038 ++*pp;
5039 break;
5041 case 'b': /* bool */
5042 if (ptype != NULL)
5044 *ptype = debug_find_named_type (minfo->dhandle, "bool");
5045 if (*ptype == DEBUG_TYPE_NULL)
5046 *ptype = debug_make_bool_type (minfo->dhandle, 4);
5048 ++*pp;
5049 break;
5051 case 'c': /* char */
5052 if (ptype != NULL)
5054 *ptype = debug_find_named_type (minfo->dhandle,
5055 (unsignedp
5056 ? "unsigned char"
5057 : (signedp
5058 ? "signed char"
5059 : "char")));
5060 if (*ptype == DEBUG_TYPE_NULL)
5061 *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
5063 ++*pp;
5064 break;
5066 case 'w': /* wchar_t */
5067 if (ptype != NULL)
5069 *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
5070 if (*ptype == DEBUG_TYPE_NULL)
5071 *ptype = debug_make_int_type (minfo->dhandle, 2, true);
5073 ++*pp;
5074 break;
5076 case 'r': /* long double */
5077 if (ptype != NULL)
5079 *ptype = debug_find_named_type (minfo->dhandle, "long double");
5080 if (*ptype == DEBUG_TYPE_NULL)
5081 *ptype = debug_make_float_type (minfo->dhandle, 8);
5083 ++*pp;
5084 break;
5086 case 'd': /* double */
5087 if (ptype != NULL)
5089 *ptype = debug_find_named_type (minfo->dhandle, "double");
5090 if (*ptype == DEBUG_TYPE_NULL)
5091 *ptype = debug_make_float_type (minfo->dhandle, 8);
5093 ++*pp;
5094 break;
5096 case 'f': /* float */
5097 if (ptype != NULL)
5099 *ptype = debug_find_named_type (minfo->dhandle, "float");
5100 if (*ptype == DEBUG_TYPE_NULL)
5101 *ptype = debug_make_float_type (minfo->dhandle, 4);
5103 ++*pp;
5104 break;
5106 case 'G':
5107 ++*pp;
5108 if (! ISDIGIT (**pp))
5110 stab_bad_demangle (orig);
5111 return false;
5113 /* Fall through. */
5114 case '0': case '1': case '2': case '3': case '4':
5115 case '5': case '6': case '7': case '8': case '9':
5117 const char *hold;
5119 if (! stab_demangle_class (minfo, pp, &hold))
5120 return false;
5121 if (ptype != NULL)
5123 char *name;
5125 name = savestring (hold, *pp - hold);
5126 *ptype = debug_find_named_type (minfo->dhandle, name);
5127 free (name);
5128 if (*ptype == DEBUG_TYPE_NULL)
5130 /* FIXME: It is probably incorrect to assume that
5131 undefined types are tagged types. */
5132 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5133 hold, *pp - hold,
5134 DEBUG_KIND_ILLEGAL);
5135 if (*ptype == DEBUG_TYPE_NULL)
5136 return false;
5140 break;
5142 case 't':
5144 char *name;
5146 if (! stab_demangle_template (minfo, pp,
5147 ptype != NULL ? &name : NULL))
5148 return false;
5149 if (ptype != NULL)
5151 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5152 name, strlen (name),
5153 DEBUG_KIND_CLASS);
5154 free (name);
5155 if (*ptype == DEBUG_TYPE_NULL)
5156 return false;
5159 break;
5161 default:
5162 stab_bad_demangle (orig);
5163 return false;
5166 if (ptype != NULL)
5168 if (constp)
5169 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
5170 if (volatilep)
5171 *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5174 return true;
5177 /* Remember a type string in a demangled string. */
5179 static boolean
5180 stab_demangle_remember_type (minfo, p, len)
5181 struct stab_demangle_info *minfo;
5182 const char *p;
5183 int len;
5185 if (minfo->typestring_count >= minfo->typestring_alloc)
5187 minfo->typestring_alloc += 10;
5188 minfo->typestrings = ((struct stab_demangle_typestring *)
5189 xrealloc (minfo->typestrings,
5190 (minfo->typestring_alloc
5191 * sizeof *minfo->typestrings)));
5194 minfo->typestrings[minfo->typestring_count].typestring = p;
5195 minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5196 ++minfo->typestring_count;
5198 return true;