2001-01-01 Philip Blundell <philb@gnu.org>
[binutils.git] / binutils / stabs.c
blob90f7666690eed48ec432c8a419c1039dec99cd78
1 /* stabs.c -- Parse stabs debugging information
2 Copyright (C) 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains code which parses stabs debugging information.
23 The organization of this code is based on the gdb stabs reading
24 code. The job it does is somewhat different, because it is not
25 trying to identify the correct address for anything. */
27 #include <stdio.h>
28 #include <ctype.h>
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "libiberty.h"
33 #include "demangle.h"
34 #include "debug.h"
35 #include "budbg.h"
37 /* Meaningless definition needs by aout64.h. FIXME. */
38 #define BYTES_IN_WORD 4
40 #include "aout/aout64.h"
41 #include "aout/stab_gnu.h"
43 #ifndef DIR_SEPARATOR
44 #ifdef _WIN32
45 #define DIR_SEPARATOR '\\'
46 #else
47 #define DIR_SEPARATOR '/'
48 #endif
49 #endif
51 /* The number of predefined XCOFF types. */
53 #define XCOFF_TYPE_COUNT 34
55 /* This structure is used as a handle so that the stab parsing doesn't
56 need to use any static variables. */
58 struct stab_handle
60 /* The BFD. */
61 bfd *abfd;
62 /* True if this is stabs in sections. */
63 boolean sections;
64 /* The symbol table. */
65 asymbol **syms;
66 /* The number of symbols. */
67 long symcount;
68 /* The accumulated file name string. */
69 char *so_string;
70 /* The value of the last N_SO symbol. */
71 bfd_vma so_value;
72 /* The value of the start of the file, so that we can handle file
73 relative N_LBRAC and N_RBRAC symbols. */
74 bfd_vma file_start_offset;
75 /* The offset of the start of the function, so that we can handle
76 function relative N_LBRAC and N_RBRAC symbols. */
77 bfd_vma function_start_offset;
78 /* The version number of gcc which compiled the current compilation
79 unit, 0 if not compiled by gcc. */
80 int gcc_compiled;
81 /* Whether an N_OPT symbol was seen that was not generated by gcc,
82 so that we can detect the SunPRO compiler. */
83 boolean n_opt_found;
84 /* The main file name. */
85 char *main_filename;
86 /* A stack of unfinished N_BINCL files. */
87 struct bincl_file *bincl_stack;
88 /* A list of finished N_BINCL files. */
89 struct bincl_file *bincl_list;
90 /* Whether we are inside a function or not. */
91 boolean within_function;
92 /* The address of the end of the function, used if we have seen an
93 N_FUN symbol while in a function. This is -1 if we have not seen
94 an N_FUN (the normal case). */
95 bfd_vma function_end;
96 /* The depth of block nesting. */
97 int block_depth;
98 /* List of pending variable definitions. */
99 struct stab_pending_var *pending;
100 /* Number of files for which we have types. */
101 unsigned int files;
102 /* Lists of types per file. */
103 struct stab_types **file_types;
104 /* Predefined XCOFF types. */
105 debug_type xcoff_types[XCOFF_TYPE_COUNT];
106 /* Undefined tags. */
107 struct stab_tag *tags;
108 /* Set by parse_stab_type if it sees a structure defined as a cross
109 reference to itself. Reset by parse_stab_type otherwise. */
110 boolean self_crossref;
113 /* A list of these structures is used to hold pending variable
114 definitions seen before the N_LBRAC of a block. */
116 struct stab_pending_var
118 /* Next pending variable definition. */
119 struct stab_pending_var *next;
120 /* Name. */
121 const char *name;
122 /* Type. */
123 debug_type type;
124 /* Kind. */
125 enum debug_var_kind kind;
126 /* Value. */
127 bfd_vma val;
130 /* A list of these structures is used to hold the types for a single
131 file. */
133 struct stab_types
135 /* Next set of slots for this file. */
136 struct stab_types *next;
137 /* Types indexed by type number. */
138 #define STAB_TYPES_SLOTS (16)
139 debug_type types[STAB_TYPES_SLOTS];
142 /* We keep a list of undefined tags that we encounter, so that we can
143 fill them in if the tag is later defined. */
145 struct stab_tag
147 /* Next undefined tag. */
148 struct stab_tag *next;
149 /* Tag name. */
150 const char *name;
151 /* Type kind. */
152 enum debug_type_kind kind;
153 /* Slot to hold real type when we discover it. If we don't, we fill
154 in an undefined tag type. */
155 debug_type slot;
156 /* Indirect type we have created to point at slot. */
157 debug_type type;
160 static char *savestring PARAMS ((const char *, int));
161 static bfd_vma parse_number PARAMS ((const char **, boolean *));
162 static void bad_stab PARAMS ((const char *));
163 static void warn_stab PARAMS ((const char *, const char *));
164 static boolean parse_stab_string
165 PARAMS ((PTR, struct stab_handle *, int, int, bfd_vma, const char *));
166 static debug_type parse_stab_type
167 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
168 debug_type **));
169 static boolean parse_stab_type_number
170 PARAMS ((const char **, int *));
171 static debug_type parse_stab_range_type
172 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
173 const int *));
174 static debug_type parse_stab_sun_builtin_type PARAMS ((PTR, const char **));
175 static debug_type parse_stab_sun_floating_type
176 PARAMS ((PTR, const char **));
177 static debug_type parse_stab_enum_type PARAMS ((PTR, const char **));
178 static debug_type parse_stab_struct_type
179 PARAMS ((PTR, struct stab_handle *, const char *, const char **, boolean,
180 const int *));
181 static boolean parse_stab_baseclasses
182 PARAMS ((PTR, struct stab_handle *, const char **, debug_baseclass **));
183 static boolean parse_stab_struct_fields
184 PARAMS ((PTR, struct stab_handle *, const char **, debug_field **,
185 boolean *));
186 static boolean parse_stab_cpp_abbrev
187 PARAMS ((PTR, struct stab_handle *, const char **, debug_field *));
188 static boolean parse_stab_one_struct_field
189 PARAMS ((PTR, struct stab_handle *, const char **, const char *,
190 debug_field *, boolean *));
191 static boolean parse_stab_members
192 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
193 const int *, debug_method **));
194 static debug_type parse_stab_argtypes
195 PARAMS ((PTR, struct stab_handle *, debug_type, const char *, const char *,
196 debug_type, const char *, boolean, boolean, const char **));
197 static boolean parse_stab_tilde_field
198 PARAMS ((PTR, struct stab_handle *, const char **, const int *,
199 debug_type *, boolean *));
200 static debug_type parse_stab_array_type
201 PARAMS ((PTR, struct stab_handle *, const char **, boolean));
202 static void push_bincl PARAMS ((struct stab_handle *, const char *, bfd_vma));
203 static const char *pop_bincl PARAMS ((struct stab_handle *));
204 static boolean find_excl
205 PARAMS ((struct stab_handle *, const char *, bfd_vma));
206 static boolean stab_record_variable
207 PARAMS ((PTR, struct stab_handle *, const char *, debug_type,
208 enum debug_var_kind, bfd_vma));
209 static boolean stab_emit_pending_vars PARAMS ((PTR, struct stab_handle *));
210 static debug_type *stab_find_slot
211 PARAMS ((struct stab_handle *, const int *));
212 static debug_type stab_find_type
213 PARAMS ((PTR, struct stab_handle *, const int *));
214 static boolean stab_record_type
215 PARAMS ((PTR, struct stab_handle *, const int *, debug_type));
216 static debug_type stab_xcoff_builtin_type
217 PARAMS ((PTR, struct stab_handle *, int));
218 static debug_type stab_find_tagged_type
219 PARAMS ((PTR, struct stab_handle *, const char *, int,
220 enum debug_type_kind));
221 static debug_type *stab_demangle_argtypes
222 PARAMS ((PTR, struct stab_handle *, const char *, boolean *));
224 /* Save a string in memory. */
226 static char *
227 savestring (start, len)
228 const char *start;
229 int len;
231 char *ret;
233 ret = (char *) xmalloc (len + 1);
234 memcpy (ret, start, len);
235 ret[len] = '\0';
236 return ret;
239 /* Read a number from a string. */
241 static bfd_vma
242 parse_number (pp, poverflow)
243 const char **pp;
244 boolean *poverflow;
246 unsigned long ul;
247 const char *orig;
249 if (poverflow != NULL)
250 *poverflow = false;
252 orig = *pp;
254 errno = 0;
255 ul = strtoul (*pp, (char **) pp, 0);
256 if (ul + 1 != 0 || errno == 0)
258 /* If bfd_vma is larger than unsigned long, and the number is
259 meant to be negative, we have to make sure that we sign
260 extend properly. */
261 if (*orig == '-')
262 return (bfd_vma) (bfd_signed_vma) (long) ul;
263 return (bfd_vma) ul;
266 /* Note that even though strtoul overflowed, it should have set *pp
267 to the end of the number, which is where we want it. */
269 if (sizeof (bfd_vma) > sizeof (unsigned long))
271 const char *p;
272 boolean neg;
273 int base;
274 bfd_vma over, lastdig;
275 boolean overflow;
276 bfd_vma v;
278 /* Our own version of strtoul, for a bfd_vma. */
280 p = orig;
282 neg = false;
283 if (*p == '+')
284 ++p;
285 else if (*p == '-')
287 neg = true;
288 ++p;
291 base = 10;
292 if (*p == '0')
294 if (p[1] == 'x' || p[1] == 'X')
296 base = 16;
297 p += 2;
299 else
301 base = 8;
302 ++p;
306 over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
307 lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
309 overflow = false;
310 v = 0;
311 while (1)
313 int d;
315 d = *p++;
316 if (isdigit ((unsigned char) d))
317 d -= '0';
318 else if (isupper ((unsigned char) d))
319 d -= 'A';
320 else if (islower ((unsigned char) d))
321 d -= 'a';
322 else
323 break;
325 if (d >= base)
326 break;
328 if (v > over || (v == over && (bfd_vma) d > lastdig))
330 overflow = true;
331 break;
335 if (! overflow)
337 if (neg)
338 v = - v;
339 return v;
343 /* If we get here, the number is too large to represent in a
344 bfd_vma. */
346 if (poverflow != NULL)
347 *poverflow = true;
348 else
349 warn_stab (orig, _("numeric overflow"));
351 return 0;
354 /* Give an error for a bad stab string. */
356 static void
357 bad_stab (p)
358 const char *p;
360 fprintf (stderr, _("Bad stab: %s\n"), p);
363 /* Warn about something in a stab string. */
365 static void
366 warn_stab (p, err)
367 const char *p;
368 const char *err;
370 fprintf (stderr, _("Warning: %s: %s\n"), err, p);
373 /* Create a handle to parse stabs symbols with. */
375 /*ARGSUSED*/
377 start_stab (dhandle, abfd, sections, syms, symcount)
378 PTR dhandle ATTRIBUTE_UNUSED;
379 bfd *abfd;
380 boolean sections;
381 asymbol **syms;
382 long symcount;
384 struct stab_handle *ret;
386 ret = (struct stab_handle *) xmalloc (sizeof *ret);
387 memset (ret, 0, sizeof *ret);
388 ret->abfd = abfd;
389 ret->sections = sections;
390 ret->syms = syms;
391 ret->symcount = symcount;
392 ret->files = 1;
393 ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
394 ret->file_types[0] = NULL;
395 ret->function_end = (bfd_vma) -1;
396 return (PTR) ret;
399 /* When we have processed all the stabs information, we need to go
400 through and fill in all the undefined tags. */
402 boolean
403 finish_stab (dhandle, handle)
404 PTR dhandle;
405 PTR handle;
407 struct stab_handle *info = (struct stab_handle *) handle;
408 struct stab_tag *st;
410 if (info->within_function)
412 if (! stab_emit_pending_vars (dhandle, info)
413 || ! debug_end_function (dhandle, info->function_end))
414 return false;
415 info->within_function = false;
416 info->function_end = (bfd_vma) -1;
419 for (st = info->tags; st != NULL; st = st->next)
421 enum debug_type_kind kind;
423 kind = st->kind;
424 if (kind == DEBUG_KIND_ILLEGAL)
425 kind = DEBUG_KIND_STRUCT;
426 st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
427 if (st->slot == DEBUG_TYPE_NULL)
428 return false;
431 return true;
434 /* Handle a single stabs symbol. */
436 boolean
437 parse_stab (dhandle, handle, type, desc, value, string)
438 PTR dhandle;
439 PTR handle;
440 int type;
441 int desc;
442 bfd_vma value;
443 const char *string;
445 struct stab_handle *info = (struct stab_handle *) handle;
447 /* gcc will emit two N_SO strings per compilation unit, one for the
448 directory name and one for the file name. We just collect N_SO
449 strings as we see them, and start the new compilation unit when
450 we see a non N_SO symbol. */
451 if (info->so_string != NULL
452 && (type != N_SO || *string == '\0' || value != info->so_value))
454 if (! debug_set_filename (dhandle, info->so_string))
455 return false;
456 info->main_filename = info->so_string;
458 info->gcc_compiled = 0;
459 info->n_opt_found = false;
461 /* Generally, for stabs in the symbol table, the N_LBRAC and
462 N_RBRAC symbols are relative to the N_SO symbol value. */
463 if (! info->sections)
464 info->file_start_offset = info->so_value;
466 /* We need to reset the mapping from type numbers to types. We
467 can't free the old mapping, because of the use of
468 debug_make_indirect_type. */
469 info->files = 1;
470 info->file_types = ((struct stab_types **)
471 xmalloc (sizeof *info->file_types));
472 info->file_types[0] = NULL;
474 info->so_string = NULL;
476 /* Now process whatever type we just got. */
479 switch (type)
481 case N_FN:
482 case N_FN_SEQ:
483 break;
485 case N_LBRAC:
486 /* Ignore extra outermost context from SunPRO cc and acc. */
487 if (info->n_opt_found && desc == 1)
488 break;
490 if (! info->within_function)
492 fprintf (stderr, _("N_LBRAC not within function\n"));
493 return false;
496 /* Start an inner lexical block. */
497 if (! debug_start_block (dhandle,
498 (value
499 + info->file_start_offset
500 + info->function_start_offset)))
501 return false;
503 /* Emit any pending variable definitions. */
504 if (! stab_emit_pending_vars (dhandle, info))
505 return false;
507 ++info->block_depth;
508 break;
510 case N_RBRAC:
511 /* Ignore extra outermost context from SunPRO cc and acc. */
512 if (info->n_opt_found && desc == 1)
513 break;
515 /* We shouldn't have any pending variable definitions here, but,
516 if we do, we probably need to emit them before closing the
517 block. */
518 if (! stab_emit_pending_vars (dhandle, info))
519 return false;
521 /* End an inner lexical block. */
522 if (! debug_end_block (dhandle,
523 (value
524 + info->file_start_offset
525 + info->function_start_offset)))
526 return false;
528 --info->block_depth;
529 if (info->block_depth < 0)
531 fprintf (stderr, _("Too many N_RBRACs\n"));
532 return false;
534 break;
536 case N_SO:
537 /* This always ends a function. */
538 if (info->within_function)
540 bfd_vma endval;
542 endval = value;
543 if (*string != '\0'
544 && info->function_end != (bfd_vma) -1
545 && info->function_end < endval)
546 endval = info->function_end;
547 if (! stab_emit_pending_vars (dhandle, info)
548 || ! debug_end_function (dhandle, endval))
549 return false;
550 info->within_function = false;
551 info->function_end = (bfd_vma) -1;
554 /* An empty string is emitted by gcc at the end of a compilation
555 unit. */
556 if (*string == '\0')
557 return true;
559 /* Just accumulate strings until we see a non N_SO symbol. If
560 the string starts with a directory separator or some other
561 form of absolute path specification, we discard the previously
562 accumulated strings. */
563 if (info->so_string == NULL)
564 info->so_string = xstrdup (string);
565 else
567 char *f;
569 f = info->so_string;
571 if ( (string[0] == '/')
572 || (string[0] == DIR_SEPARATOR)
573 || ( (DIR_SEPARATOR == '\\')
574 && (string[1] == ':')
575 && ( (string[2] == DIR_SEPARATOR)
576 || (string[2] == '/'))))
577 info->so_string = xstrdup (string);
578 else
579 info->so_string = concat (info->so_string, string,
580 (const char *) NULL);
581 free (f);
584 info->so_value = value;
586 break;
588 case N_SOL:
589 /* Start an include file. */
590 if (! debug_start_source (dhandle, string))
591 return false;
592 break;
594 case N_BINCL:
595 /* Start an include file which may be replaced. */
596 push_bincl (info, string, value);
597 if (! debug_start_source (dhandle, string))
598 return false;
599 break;
601 case N_EINCL:
602 /* End an N_BINCL include. */
603 if (! debug_start_source (dhandle, pop_bincl (info)))
604 return false;
605 break;
607 case N_EXCL:
608 /* This is a duplicate of a header file named by N_BINCL which
609 was eliminated by the linker. */
610 if (! find_excl (info, string, value))
611 return false;
612 break;
614 case N_SLINE:
615 if (! debug_record_line (dhandle, desc,
616 value + info->function_start_offset))
617 return false;
618 break;
620 case N_BCOMM:
621 if (! debug_start_common_block (dhandle, string))
622 return false;
623 break;
625 case N_ECOMM:
626 if (! debug_end_common_block (dhandle, string))
627 return false;
628 break;
630 case N_FUN:
631 if (*string == '\0')
633 if (info->within_function)
635 /* This always marks the end of a function; we don't
636 need to worry about info->function_end. */
637 if (info->sections)
638 value += info->function_start_offset;
639 if (! stab_emit_pending_vars (dhandle, info)
640 || ! debug_end_function (dhandle, value))
641 return false;
642 info->within_function = false;
643 info->function_end = (bfd_vma) -1;
645 break;
648 /* A const static symbol in the .text section will have an N_FUN
649 entry. We need to use these to mark the end of the function,
650 in case we are looking at gcc output before it was changed to
651 always emit an empty N_FUN. We can't call debug_end_function
652 here, because it might be a local static symbol. */
653 if (info->within_function
654 && (info->function_end == (bfd_vma) -1
655 || value < info->function_end))
656 info->function_end = value;
658 /* Fall through. */
659 /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
660 symbols, and if it does not start with :S, gdb relocates the
661 value to the start of the section. gcc always seems to use
662 :S, so we don't worry about this. */
663 /* Fall through. */
664 default:
666 const char *colon;
668 colon = strchr (string, ':');
669 if (colon != NULL
670 && (colon[1] == 'f' || colon[1] == 'F'))
672 if (info->within_function)
674 bfd_vma endval;
676 endval = value;
677 if (info->function_end != (bfd_vma) -1
678 && info->function_end < endval)
679 endval = info->function_end;
680 if (! stab_emit_pending_vars (dhandle, info)
681 || ! debug_end_function (dhandle, endval))
682 return false;
683 info->function_end = (bfd_vma) -1;
685 /* For stabs in sections, line numbers and block addresses
686 are offsets from the start of the function. */
687 if (info->sections)
688 info->function_start_offset = value;
689 info->within_function = true;
692 if (! parse_stab_string (dhandle, info, type, desc, value, string))
693 return false;
695 break;
697 case N_OPT:
698 if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
699 info->gcc_compiled = 2;
700 else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
701 info->gcc_compiled = 1;
702 else
703 info->n_opt_found = true;
704 break;
706 case N_OBJ:
707 case N_ENDM:
708 case N_MAIN:
709 break;
712 return true;
715 /* Parse the stabs string. */
717 static boolean
718 parse_stab_string (dhandle, info, stabtype, desc, value, string)
719 PTR dhandle;
720 struct stab_handle *info;
721 int stabtype;
722 int desc;
723 bfd_vma value;
724 const char *string;
726 const char *p;
727 char *name;
728 int type;
729 debug_type dtype;
730 boolean synonym;
731 boolean self_crossref;
732 unsigned int lineno;
733 debug_type *slot;
735 p = strchr (string, ':');
736 if (p == NULL)
737 return true;
739 while (p[1] == ':')
741 p += 2;
742 p = strchr (p, ':');
743 if (p == NULL)
745 bad_stab (string);
746 return false;
750 /* GCC 2.x puts the line number in desc. SunOS apparently puts in
751 the number of bytes occupied by a type or object, which we
752 ignore. */
753 if (info->gcc_compiled >= 2)
754 lineno = desc;
755 else
756 lineno = 0;
758 /* FIXME: Sometimes the special C++ names start with '.'. */
759 name = NULL;
760 if (string[0] == '$')
762 switch (string[1])
764 case 't':
765 name = "this";
766 break;
767 case 'v':
768 /* Was: name = "vptr"; */
769 break;
770 case 'e':
771 name = "eh_throw";
772 break;
773 case '_':
774 /* This was an anonymous type that was never fixed up. */
775 break;
776 case 'X':
777 /* SunPRO (3.0 at least) static variable encoding. */
778 break;
779 default:
780 warn_stab (string, _("unknown C++ encoded name"));
781 break;
785 if (name == NULL)
787 if (p == string || (string[0] == ' ' && p == string + 1))
788 name = NULL;
789 else
790 name = savestring (string, p - string);
793 ++p;
794 if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
795 type = 'l';
796 else
797 type = *p++;
799 switch (type)
801 case 'c':
802 /* c is a special case, not followed by a type-number.
803 SYMBOL:c=iVALUE for an integer constant symbol.
804 SYMBOL:c=rVALUE for a floating constant symbol.
805 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
806 e.g. "b:c=e6,0" for "const b = blob1"
807 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
808 if (*p != '=')
810 bad_stab (string);
811 return false;
813 ++p;
814 switch (*p++)
816 case 'r':
817 /* Floating point constant. */
818 if (! debug_record_float_const (dhandle, name, atof (p)))
819 return false;
820 break;
821 case 'i':
822 /* Integer constant. */
823 /* Defining integer constants this way is kind of silly,
824 since 'e' constants allows the compiler to give not only
825 the value, but the type as well. C has at least int,
826 long, unsigned int, and long long as constant types;
827 other languages probably should have at least unsigned as
828 well as signed constants. */
829 if (! debug_record_int_const (dhandle, name, atoi (p)))
830 return false;
831 break;
832 case 'e':
833 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
834 can be represented as integral.
835 e.g. "b:c=e6,0" for "const b = blob1"
836 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
837 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
838 &p, (debug_type **) NULL);
839 if (dtype == DEBUG_TYPE_NULL)
840 return false;
841 if (*p != ',')
843 bad_stab (string);
844 return false;
846 if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
847 return false;
848 break;
849 default:
850 bad_stab (string);
851 return false;
854 break;
856 case 'C':
857 /* The name of a caught exception. */
858 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
859 &p, (debug_type **) NULL);
860 if (dtype == DEBUG_TYPE_NULL)
861 return false;
862 if (! debug_record_label (dhandle, name, dtype, value))
863 return false;
864 break;
866 case 'f':
867 case 'F':
868 /* A function definition. */
869 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
870 (debug_type **) NULL);
871 if (dtype == DEBUG_TYPE_NULL)
872 return false;
873 if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
874 return false;
876 /* Sun acc puts declared types of arguments here. We don't care
877 about their actual types (FIXME -- we should remember the whole
878 function prototype), but the list may define some new types
879 that we have to remember, so we must scan it now. */
880 while (*p == ';')
882 ++p;
883 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
884 (debug_type **) NULL)
885 == DEBUG_TYPE_NULL)
886 return false;
889 break;
891 case 'G':
893 char leading;
894 long c;
895 asymbol **ps;
897 /* A global symbol. The value must be extracted from the
898 symbol table. */
899 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
900 (debug_type **) NULL);
901 if (dtype == DEBUG_TYPE_NULL)
902 return false;
903 leading = bfd_get_symbol_leading_char (info->abfd);
904 for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
906 const char *n;
908 n = bfd_asymbol_name (*ps);
909 if (leading != '\0' && *n == leading)
910 ++n;
911 if (*n == *name && strcmp (n, name) == 0)
912 break;
914 if (c > 0)
915 value = bfd_asymbol_value (*ps);
916 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
917 value))
918 return false;
920 break;
922 /* This case is faked by a conditional above, when there is no
923 code letter in the dbx data. Dbx data never actually
924 contains 'l'. */
925 case 'l':
926 case 's':
927 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
928 (debug_type **) NULL);
929 if (dtype == DEBUG_TYPE_NULL)
930 return false;
931 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
932 value))
933 return false;
934 break;
936 case 'p':
937 /* A function parameter. */
938 if (*p != 'F')
939 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
940 (debug_type **) NULL);
941 else
943 /* pF is a two-letter code that means a function parameter in
944 Fortran. The type-number specifies the type of the return
945 value. Translate it into a pointer-to-function type. */
946 ++p;
947 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
948 (debug_type **) NULL);
949 if (dtype != DEBUG_TYPE_NULL)
951 debug_type ftype;
953 ftype = debug_make_function_type (dhandle, dtype,
954 (debug_type *) NULL, false);
955 dtype = debug_make_pointer_type (dhandle, ftype);
958 if (dtype == DEBUG_TYPE_NULL)
959 return false;
960 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
961 value))
962 return false;
964 /* FIXME: At this point gdb considers rearranging the parameter
965 address on a big endian machine if it is smaller than an int.
966 We have no way to do that, since we don't really know much
967 about the target. */
969 break;
971 case 'P':
972 if (stabtype == N_FUN)
974 /* Prototype of a function referenced by this file. */
975 while (*p == ';')
977 ++p;
978 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
979 (debug_type **) NULL)
980 == DEBUG_TYPE_NULL)
981 return false;
983 break;
985 /* Fall through. */
986 case 'R':
987 /* Parameter which is in a register. */
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 (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
993 value))
994 return false;
995 break;
997 case 'r':
998 /* Register variable (either global or local). */
999 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1000 (debug_type **) NULL);
1001 if (dtype == DEBUG_TYPE_NULL)
1002 return false;
1003 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
1004 value))
1005 return false;
1007 /* FIXME: At this point gdb checks to combine pairs of 'p' and
1008 'r' stabs into a single 'P' stab. */
1010 break;
1012 case 'S':
1013 /* Static symbol at top level of file */
1014 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1015 (debug_type **) NULL);
1016 if (dtype == DEBUG_TYPE_NULL)
1017 return false;
1018 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
1019 value))
1020 return false;
1021 break;
1023 case 't':
1024 /* A typedef. */
1025 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1026 if (dtype == DEBUG_TYPE_NULL)
1027 return false;
1028 if (name == NULL)
1030 /* A nameless type. Nothing to do. */
1031 return true;
1034 dtype = debug_name_type (dhandle, name, dtype);
1035 if (dtype == DEBUG_TYPE_NULL)
1036 return false;
1038 if (slot != NULL)
1039 *slot = dtype;
1041 break;
1043 case 'T':
1044 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1045 by 't' which means we are typedef'ing it as well. */
1046 if (*p != 't')
1048 synonym = false;
1049 /* FIXME: gdb sets synonym to true if the current language
1050 is C++. */
1052 else
1054 synonym = true;
1055 ++p;
1058 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1059 if (dtype == DEBUG_TYPE_NULL)
1060 return false;
1061 if (name == NULL)
1062 return true;
1064 /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
1065 a cross reference to itself. These are generated by some
1066 versions of g++. */
1067 self_crossref = info->self_crossref;
1069 dtype = debug_tag_type (dhandle, name, dtype);
1070 if (dtype == DEBUG_TYPE_NULL)
1071 return false;
1072 if (slot != NULL)
1073 *slot = dtype;
1075 /* See if we have a cross reference to this tag which we can now
1076 fill in. Avoid filling in a cross reference to ourselves,
1077 because that would lead to circular debugging information. */
1078 if (! self_crossref)
1080 register struct stab_tag **pst;
1082 for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
1084 if ((*pst)->name[0] == name[0]
1085 && strcmp ((*pst)->name, name) == 0)
1087 (*pst)->slot = dtype;
1088 *pst = (*pst)->next;
1089 break;
1094 if (synonym)
1096 dtype = debug_name_type (dhandle, name, dtype);
1097 if (dtype == DEBUG_TYPE_NULL)
1098 return false;
1100 if (slot != NULL)
1101 *slot = dtype;
1104 break;
1106 case 'V':
1107 /* Static symbol of local scope */
1108 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1109 (debug_type **) NULL);
1110 if (dtype == DEBUG_TYPE_NULL)
1111 return false;
1112 /* FIXME: gdb checks os9k_stabs here. */
1113 if (! stab_record_variable (dhandle, info, name, dtype,
1114 DEBUG_LOCAL_STATIC, value))
1115 return false;
1116 break;
1118 case 'v':
1119 /* Reference parameter. */
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_REFERENCE,
1125 value))
1126 return false;
1127 break;
1129 case 'a':
1130 /* Reference parameter which is in a register. */
1131 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1132 (debug_type **) NULL);
1133 if (dtype == DEBUG_TYPE_NULL)
1134 return false;
1135 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1136 value))
1137 return false;
1138 break;
1140 case 'X':
1141 /* This is used by Sun FORTRAN for "function result value".
1142 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1143 that Pascal uses it too, but when I tried it Pascal used
1144 "x:3" (local symbol) instead. */
1145 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1146 (debug_type **) NULL);
1147 if (dtype == DEBUG_TYPE_NULL)
1148 return false;
1149 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1150 value))
1151 return false;
1152 break;
1154 default:
1155 bad_stab (string);
1156 return false;
1159 /* FIXME: gdb converts structure values to structure pointers in a
1160 couple of cases, depending upon the target. */
1162 return true;
1165 /* Parse a stabs type. The typename argument is non-NULL if this is a
1166 typedef or a tag definition. The pp argument points to the stab
1167 string, and is updated. The slotp argument points to a place to
1168 store the slot used if the type is being defined. */
1170 static debug_type
1171 parse_stab_type (dhandle, info, typename, pp, slotp)
1172 PTR dhandle;
1173 struct stab_handle *info;
1174 const char *typename;
1175 const char **pp;
1176 debug_type **slotp;
1178 const char *orig;
1179 int typenums[2];
1180 int size;
1181 boolean stringp;
1182 int descriptor;
1183 debug_type dtype;
1185 if (slotp != NULL)
1186 *slotp = NULL;
1188 orig = *pp;
1190 size = -1;
1191 stringp = false;
1193 info->self_crossref = false;
1195 /* Read type number if present. The type number may be omitted.
1196 for instance in a two-dimensional array declared with type
1197 "ar1;1;10;ar1;1;10;4". */
1198 if (! isdigit ((unsigned char) **pp) && **pp != '(' && **pp != '-')
1200 /* 'typenums=' not present, type is anonymous. Read and return
1201 the definition, but don't put it in the type vector. */
1202 typenums[0] = typenums[1] = -1;
1204 else
1206 if (! parse_stab_type_number (pp, typenums))
1207 return DEBUG_TYPE_NULL;
1209 if (**pp != '=')
1211 /* Type is not being defined here. Either it already
1212 exists, or this is a forward reference to it. */
1213 return stab_find_type (dhandle, info, typenums);
1216 /* Only set the slot if the type is being defined. This means
1217 that the mapping from type numbers to types will only record
1218 the name of the typedef which defines a type. If we don't do
1219 this, then something like
1220 typedef int foo;
1221 int i;
1222 will record that i is of type foo. Unfortunately, stabs
1223 information is ambiguous about variable types. For this code,
1224 typedef int foo;
1225 int i;
1226 foo j;
1227 the stabs information records both i and j as having the same
1228 type. This could be fixed by patching the compiler. */
1229 if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1230 *slotp = stab_find_slot (info, typenums);
1232 /* Type is being defined here. */
1233 /* Skip the '='. */
1234 ++*pp;
1236 while (**pp == '@')
1238 const char *p = *pp + 1;
1239 const char *attr;
1241 if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
1243 /* Member type. */
1244 break;
1247 /* Type attributes. */
1248 attr = p;
1250 for (; *p != ';'; ++p)
1252 if (*p == '\0')
1254 bad_stab (orig);
1255 return DEBUG_TYPE_NULL;
1258 *pp = p + 1;
1260 switch (*attr)
1262 case 's':
1263 size = atoi (attr + 1);
1264 size /= 8; /* Size is in bits. We store it in bytes. */
1265 if (size <= 0)
1266 size = -1;
1267 break;
1269 case 'S':
1270 stringp = true;
1271 break;
1273 default:
1274 /* Ignore unrecognized type attributes, so future
1275 compilers can invent new ones. */
1276 break;
1281 descriptor = **pp;
1282 ++*pp;
1284 switch (descriptor)
1286 case 'x':
1288 enum debug_type_kind code;
1289 const char *q1, *q2, *p;
1291 /* A cross reference to another type. */
1293 switch (**pp)
1295 case 's':
1296 code = DEBUG_KIND_STRUCT;
1297 break;
1298 case 'u':
1299 code = DEBUG_KIND_UNION;
1300 break;
1301 case 'e':
1302 code = DEBUG_KIND_ENUM;
1303 break;
1304 default:
1305 /* Complain and keep going, so compilers can invent new
1306 cross-reference types. */
1307 warn_stab (orig, _("unrecognized cross reference type"));
1308 code = DEBUG_KIND_STRUCT;
1309 break;
1311 ++*pp;
1313 q1 = strchr (*pp, '<');
1314 p = strchr (*pp, ':');
1315 if (p == NULL)
1317 bad_stab (orig);
1318 return DEBUG_TYPE_NULL;
1320 if (q1 != NULL && p > q1 && p[1] == ':')
1322 int nest = 0;
1324 for (q2 = q1; *q2 != '\0'; ++q2)
1326 if (*q2 == '<')
1327 ++nest;
1328 else if (*q2 == '>')
1329 --nest;
1330 else if (*q2 == ':' && nest == 0)
1331 break;
1333 p = q2;
1334 if (*p != ':')
1336 bad_stab (orig);
1337 return DEBUG_TYPE_NULL;
1341 /* Some versions of g++ can emit stabs like
1342 fleep:T20=xsfleep:
1343 which define structures in terms of themselves. We need to
1344 tell the caller to avoid building a circular structure. */
1345 if (typename != NULL
1346 && strncmp (typename, *pp, p - *pp) == 0
1347 && typename[p - *pp] == '\0')
1348 info->self_crossref = true;
1350 dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1352 *pp = p + 1;
1354 break;
1356 case '-':
1357 case '0':
1358 case '1':
1359 case '2':
1360 case '3':
1361 case '4':
1362 case '5':
1363 case '6':
1364 case '7':
1365 case '8':
1366 case '9':
1367 case '(':
1369 const char *hold;
1370 int xtypenums[2];
1372 /* This type is defined as another type. */
1374 (*pp)--;
1375 hold = *pp;
1377 /* Peek ahead at the number to detect void. */
1378 if (! parse_stab_type_number (pp, xtypenums))
1379 return DEBUG_TYPE_NULL;
1381 if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1383 /* This type is being defined as itself, which means that
1384 it is void. */
1385 dtype = debug_make_void_type (dhandle);
1387 else
1389 *pp = hold;
1391 /* Go back to the number and have parse_stab_type get it.
1392 This means that we can deal with something like
1393 t(1,2)=(3,4)=... which the Lucid compiler uses. */
1394 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1395 pp, (debug_type **) NULL);
1396 if (dtype == DEBUG_TYPE_NULL)
1397 return DEBUG_TYPE_NULL;
1400 if (typenums[0] != -1)
1402 if (! stab_record_type (dhandle, info, typenums, dtype))
1403 return DEBUG_TYPE_NULL;
1406 break;
1409 case '*':
1410 dtype = debug_make_pointer_type (dhandle,
1411 parse_stab_type (dhandle, info,
1412 (const char *) NULL,
1414 (debug_type **) NULL));
1415 break;
1417 case '&':
1418 /* Reference to another type. */
1419 dtype = (debug_make_reference_type
1420 (dhandle,
1421 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1422 (debug_type **) NULL)));
1423 break;
1425 case 'f':
1426 /* Function returning another type. */
1427 /* FIXME: gdb checks os9k_stabs here. */
1428 dtype = (debug_make_function_type
1429 (dhandle,
1430 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1431 (debug_type **) NULL),
1432 (debug_type *) NULL, false));
1433 break;
1435 case 'k':
1436 /* Const qualifier on some type (Sun). */
1437 /* FIXME: gdb accepts 'c' here if os9k_stabs. */
1438 dtype = debug_make_const_type (dhandle,
1439 parse_stab_type (dhandle, info,
1440 (const char *) NULL,
1442 (debug_type **) NULL));
1443 break;
1445 case 'B':
1446 /* Volatile qual on some type (Sun). */
1447 /* FIXME: gdb accepts 'i' here if os9k_stabs. */
1448 dtype = (debug_make_volatile_type
1449 (dhandle,
1450 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1451 (debug_type **) NULL)));
1452 break;
1454 case '@':
1455 /* Offset (class & variable) type. This is used for a pointer
1456 relative to an object. */
1458 debug_type domain;
1459 debug_type memtype;
1461 /* Member type. */
1463 domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1464 (debug_type **) NULL);
1465 if (domain == DEBUG_TYPE_NULL)
1466 return DEBUG_TYPE_NULL;
1468 if (**pp != ',')
1470 bad_stab (orig);
1471 return DEBUG_TYPE_NULL;
1473 ++*pp;
1475 memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1476 (debug_type **) NULL);
1477 if (memtype == DEBUG_TYPE_NULL)
1478 return DEBUG_TYPE_NULL;
1480 dtype = debug_make_offset_type (dhandle, domain, memtype);
1482 break;
1484 case '#':
1485 /* Method (class & fn) type. */
1486 if (**pp == '#')
1488 debug_type return_type;
1490 ++*pp;
1491 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1492 pp, (debug_type **) NULL);
1493 if (return_type == DEBUG_TYPE_NULL)
1494 return DEBUG_TYPE_NULL;
1495 if (**pp != ';')
1497 bad_stab (orig);
1498 return DEBUG_TYPE_NULL;
1500 ++*pp;
1501 dtype = debug_make_method_type (dhandle, return_type,
1502 DEBUG_TYPE_NULL,
1503 (debug_type *) NULL, false);
1505 else
1507 debug_type domain;
1508 debug_type return_type;
1509 debug_type *args;
1510 unsigned int n;
1511 unsigned int alloc;
1512 boolean varargs;
1514 domain = parse_stab_type (dhandle, info, (const char *) NULL,
1515 pp, (debug_type **) NULL);
1516 if (domain == DEBUG_TYPE_NULL)
1517 return DEBUG_TYPE_NULL;
1519 if (**pp != ',')
1521 bad_stab (orig);
1522 return DEBUG_TYPE_NULL;
1524 ++*pp;
1526 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1527 pp, (debug_type **) NULL);
1528 if (return_type == DEBUG_TYPE_NULL)
1529 return DEBUG_TYPE_NULL;
1531 alloc = 10;
1532 args = (debug_type *) xmalloc (alloc * sizeof *args);
1533 n = 0;
1534 while (**pp != ';')
1536 if (**pp != ',')
1538 bad_stab (orig);
1539 return DEBUG_TYPE_NULL;
1541 ++*pp;
1543 if (n + 1 >= alloc)
1545 alloc += 10;
1546 args = ((debug_type *)
1547 xrealloc ((PTR) args, alloc * sizeof *args));
1550 args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1551 pp, (debug_type **) NULL);
1552 if (args[n] == DEBUG_TYPE_NULL)
1553 return DEBUG_TYPE_NULL;
1554 ++n;
1556 ++*pp;
1558 /* If the last type is not void, then this function takes a
1559 variable number of arguments. Otherwise, we must strip
1560 the void type. */
1561 if (n == 0
1562 || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1563 varargs = true;
1564 else
1566 --n;
1567 varargs = false;
1570 args[n] = DEBUG_TYPE_NULL;
1572 dtype = debug_make_method_type (dhandle, return_type, domain, args,
1573 varargs);
1575 break;
1577 case 'r':
1578 /* Range type. */
1579 dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
1580 break;
1582 case 'b':
1583 /* FIXME: gdb checks os9k_stabs here. */
1584 /* Sun ACC builtin int type. */
1585 dtype = parse_stab_sun_builtin_type (dhandle, pp);
1586 break;
1588 case 'R':
1589 /* Sun ACC builtin float type. */
1590 dtype = parse_stab_sun_floating_type (dhandle, pp);
1591 break;
1593 case 'e':
1594 /* Enumeration type. */
1595 dtype = parse_stab_enum_type (dhandle, pp);
1596 break;
1598 case 's':
1599 case 'u':
1600 /* Struct or union type. */
1601 dtype = parse_stab_struct_type (dhandle, info, typename, pp,
1602 descriptor == 's', typenums);
1603 break;
1605 case 'a':
1606 /* Array type. */
1607 if (**pp != 'r')
1609 bad_stab (orig);
1610 return DEBUG_TYPE_NULL;
1612 ++*pp;
1614 dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1615 break;
1617 case 'S':
1618 dtype = debug_make_set_type (dhandle,
1619 parse_stab_type (dhandle, info,
1620 (const char *) NULL,
1622 (debug_type **) NULL),
1623 stringp);
1624 break;
1626 default:
1627 bad_stab (orig);
1628 return DEBUG_TYPE_NULL;
1631 if (dtype == DEBUG_TYPE_NULL)
1632 return DEBUG_TYPE_NULL;
1634 if (typenums[0] != -1)
1636 if (! stab_record_type (dhandle, info, typenums, dtype))
1637 return DEBUG_TYPE_NULL;
1640 if (size != -1)
1642 if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1643 return DEBUG_TYPE_NULL;
1646 return dtype;
1649 /* Read a number by which a type is referred to in dbx data, or
1650 perhaps read a pair (FILENUM, TYPENUM) in parentheses. Just a
1651 single number N is equivalent to (0,N). Return the two numbers by
1652 storing them in the vector TYPENUMS. */
1654 static boolean
1655 parse_stab_type_number (pp, typenums)
1656 const char **pp;
1657 int *typenums;
1659 const char *orig;
1661 orig = *pp;
1663 if (**pp != '(')
1665 typenums[0] = 0;
1666 typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1668 else
1670 ++*pp;
1671 typenums[0] = (int) parse_number (pp, (boolean *) NULL);
1672 if (**pp != ',')
1674 bad_stab (orig);
1675 return false;
1677 ++*pp;
1678 typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1679 if (**pp != ')')
1681 bad_stab (orig);
1682 return false;
1684 ++*pp;
1687 return true;
1690 /* Parse a range type. */
1692 static debug_type
1693 parse_stab_range_type (dhandle, info, typename, pp, typenums)
1694 PTR dhandle;
1695 struct stab_handle *info;
1696 const char *typename;
1697 const char **pp;
1698 const int *typenums;
1700 const char *orig;
1701 int rangenums[2];
1702 boolean self_subrange;
1703 debug_type index_type;
1704 const char *s2, *s3;
1705 bfd_signed_vma n2, n3;
1706 boolean ov2, ov3;
1708 orig = *pp;
1710 index_type = DEBUG_TYPE_NULL;
1712 /* First comes a type we are a subrange of.
1713 In C it is usually 0, 1 or the type being defined. */
1714 if (! parse_stab_type_number (pp, rangenums))
1715 return DEBUG_TYPE_NULL;
1717 self_subrange = (rangenums[0] == typenums[0]
1718 && rangenums[1] == typenums[1]);
1720 if (**pp == '=')
1722 *pp = orig;
1723 index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1724 pp, (debug_type **) NULL);
1725 if (index_type == DEBUG_TYPE_NULL)
1726 return DEBUG_TYPE_NULL;
1729 if (**pp == ';')
1730 ++*pp;
1732 /* The remaining two operands are usually lower and upper bounds of
1733 the range. But in some special cases they mean something else. */
1734 s2 = *pp;
1735 n2 = parse_number (pp, &ov2);
1736 if (**pp != ';')
1738 bad_stab (orig);
1739 return DEBUG_TYPE_NULL;
1741 ++*pp;
1743 s3 = *pp;
1744 n3 = parse_number (pp, &ov3);
1745 if (**pp != ';')
1747 bad_stab (orig);
1748 return DEBUG_TYPE_NULL;
1750 ++*pp;
1752 if (ov2 || ov3)
1754 /* gcc will emit range stabs for long long types. Handle this
1755 as a special case. FIXME: This needs to be more general. */
1756 #define LLLOW "01000000000000000000000;"
1757 #define LLHIGH "0777777777777777777777;"
1758 #define ULLHIGH "01777777777777777777777;"
1759 if (index_type == DEBUG_TYPE_NULL)
1761 if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1762 && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1763 return debug_make_int_type (dhandle, 8, false);
1764 if (! ov2
1765 && n2 == 0
1766 && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1767 return debug_make_int_type (dhandle, 8, true);
1770 warn_stab (orig, _("numeric overflow"));
1773 if (index_type == DEBUG_TYPE_NULL)
1775 /* A type defined as a subrange of itself, with both bounds 0,
1776 is void. */
1777 if (self_subrange && n2 == 0 && n3 == 0)
1778 return debug_make_void_type (dhandle);
1780 /* A type defined as a subrange of itself, with n2 positive and
1781 n3 zero, is a complex type, and n2 is the number of bytes. */
1782 if (self_subrange && n3 == 0 && n2 > 0)
1783 return debug_make_complex_type (dhandle, n2);
1785 /* If n3 is zero and n2 is positive, this is a floating point
1786 type, and n2 is the number of bytes. */
1787 if (n3 == 0 && n2 > 0)
1788 return debug_make_float_type (dhandle, n2);
1790 /* If the upper bound is -1, this is an unsigned int. */
1791 if (n2 == 0 && n3 == -1)
1793 /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1794 long long int:t6=r1;0;-1;
1795 long long unsigned int:t7=r1;0;-1;
1796 We hack here to handle this reasonably. */
1797 if (typename != NULL)
1799 if (strcmp (typename, "long long int") == 0)
1800 return debug_make_int_type (dhandle, 8, false);
1801 else if (strcmp (typename, "long long unsigned int") == 0)
1802 return debug_make_int_type (dhandle, 8, true);
1804 /* FIXME: The size here really depends upon the target. */
1805 return debug_make_int_type (dhandle, 4, true);
1808 /* A range of 0 to 127 is char. */
1809 if (self_subrange && n2 == 0 && n3 == 127)
1810 return debug_make_int_type (dhandle, 1, false);
1812 /* FIXME: gdb checks for the language CHILL here. */
1814 if (n2 == 0)
1816 if (n3 < 0)
1817 return debug_make_int_type (dhandle, - n3, true);
1818 else if (n3 == 0xff)
1819 return debug_make_int_type (dhandle, 1, true);
1820 else if (n3 == 0xffff)
1821 return debug_make_int_type (dhandle, 2, true);
1822 else if (n3 == (bfd_signed_vma) 0xffffffff)
1823 return debug_make_int_type (dhandle, 4, true);
1824 #ifdef BFD64
1825 else if (n3 == ((((bfd_signed_vma) 0xffffffff) << 32) | 0xffffffff))
1826 return debug_make_int_type (dhandle, 8, true);
1827 #endif
1829 else if (n3 == 0
1830 && n2 < 0
1831 && (self_subrange || n2 == -8))
1832 return debug_make_int_type (dhandle, - n2, true);
1833 else if (n2 == - n3 - 1 || n2 == n3 + 1)
1835 if (n3 == 0x7f)
1836 return debug_make_int_type (dhandle, 1, false);
1837 else if (n3 == 0x7fff)
1838 return debug_make_int_type (dhandle, 2, false);
1839 else if (n3 == 0x7fffffff)
1840 return debug_make_int_type (dhandle, 4, false);
1841 #ifdef BFD64
1842 else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
1843 return debug_make_int_type (dhandle, 8, false);
1844 #endif
1848 /* At this point I don't have the faintest idea how to deal with a
1849 self_subrange type; I'm going to assume that this is used as an
1850 idiom, and that all of them are special cases. So . . . */
1851 if (self_subrange)
1853 bad_stab (orig);
1854 return DEBUG_TYPE_NULL;
1857 index_type = stab_find_type (dhandle, info, rangenums);
1858 if (index_type == DEBUG_TYPE_NULL)
1860 /* Does this actually ever happen? Is that why we are worrying
1861 about dealing with it rather than just calling error_type? */
1862 warn_stab (orig, _("missing index type"));
1863 index_type = debug_make_int_type (dhandle, 4, false);
1866 return debug_make_range_type (dhandle, index_type, n2, n3);
1869 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1870 typedefs in every file (for int, long, etc):
1872 type = b <signed> <width>; <offset>; <nbits>
1873 signed = u or s. Possible c in addition to u or s (for char?).
1874 offset = offset from high order bit to start bit of type.
1875 width is # bytes in object of this type, nbits is # bits in type.
1877 The width/offset stuff appears to be for small objects stored in
1878 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
1879 FIXME. */
1881 static debug_type
1882 parse_stab_sun_builtin_type (dhandle, pp)
1883 PTR dhandle;
1884 const char **pp;
1886 const char *orig;
1887 boolean unsignedp;
1888 bfd_vma bits;
1890 orig = *pp;
1892 switch (**pp)
1894 case 's':
1895 unsignedp = false;
1896 break;
1897 case 'u':
1898 unsignedp = true;
1899 break;
1900 default:
1901 bad_stab (orig);
1902 return DEBUG_TYPE_NULL;
1904 ++*pp;
1906 /* For some odd reason, all forms of char put a c here. This is strange
1907 because no other type has this honor. We can safely ignore this because
1908 we actually determine 'char'acterness by the number of bits specified in
1909 the descriptor. */
1910 if (**pp == 'c')
1911 ++*pp;
1913 /* The first number appears to be the number of bytes occupied
1914 by this type, except that unsigned short is 4 instead of 2.
1915 Since this information is redundant with the third number,
1916 we will ignore it. */
1917 (void) parse_number (pp, (boolean *) NULL);
1918 if (**pp != ';')
1920 bad_stab (orig);
1921 return DEBUG_TYPE_NULL;
1923 ++*pp;
1925 /* The second number is always 0, so ignore it too. */
1926 (void) parse_number (pp, (boolean *) NULL);
1927 if (**pp != ';')
1929 bad_stab (orig);
1930 return DEBUG_TYPE_NULL;
1932 ++*pp;
1934 /* The third number is the number of bits for this type. */
1935 bits = parse_number (pp, (boolean *) NULL);
1937 /* The type *should* end with a semicolon. If it are embedded
1938 in a larger type the semicolon may be the only way to know where
1939 the type ends. If this type is at the end of the stabstring we
1940 can deal with the omitted semicolon (but we don't have to like
1941 it). Don't bother to complain(), Sun's compiler omits the semicolon
1942 for "void". */
1943 if (**pp == ';')
1944 ++*pp;
1946 if (bits == 0)
1947 return debug_make_void_type (dhandle);
1949 return debug_make_int_type (dhandle, bits / 8, unsignedp);
1952 /* Parse a builtin floating type generated by the Sun compiler. */
1954 static debug_type
1955 parse_stab_sun_floating_type (dhandle, pp)
1956 PTR dhandle;
1957 const char **pp;
1959 const char *orig;
1960 bfd_vma details;
1961 bfd_vma bytes;
1963 orig = *pp;
1965 /* The first number has more details about the type, for example
1966 FN_COMPLEX. */
1967 details = parse_number (pp, (boolean *) NULL);
1968 if (**pp != ';')
1970 bad_stab (orig);
1971 return DEBUG_TYPE_NULL;
1974 /* The second number is the number of bytes occupied by this type */
1975 bytes = parse_number (pp, (boolean *) NULL);
1976 if (**pp != ';')
1978 bad_stab (orig);
1979 return DEBUG_TYPE_NULL;
1982 if (details == NF_COMPLEX
1983 || details == NF_COMPLEX16
1984 || details == NF_COMPLEX32)
1985 return debug_make_complex_type (dhandle, bytes);
1987 return debug_make_float_type (dhandle, bytes);
1990 /* Handle an enum type. */
1992 static debug_type
1993 parse_stab_enum_type (dhandle, pp)
1994 PTR dhandle;
1995 const char **pp;
1997 const char *orig;
1998 const char **names;
1999 bfd_signed_vma *values;
2000 unsigned int n;
2001 unsigned int alloc;
2003 orig = *pp;
2005 /* FIXME: gdb checks os9k_stabs here. */
2007 /* The aix4 compiler emits an extra field before the enum members;
2008 my guess is it's a type of some sort. Just ignore it. */
2009 if (**pp == '-')
2011 while (**pp != ':')
2012 ++*pp;
2013 ++*pp;
2016 /* Read the value-names and their values.
2017 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2018 A semicolon or comma instead of a NAME means the end. */
2019 alloc = 10;
2020 names = (const char **) xmalloc (alloc * sizeof *names);
2021 values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
2022 n = 0;
2023 while (**pp != '\0' && **pp != ';' && **pp != ',')
2025 const char *p;
2026 char *name;
2027 bfd_signed_vma val;
2029 p = *pp;
2030 while (*p != ':')
2031 ++p;
2033 name = savestring (*pp, p - *pp);
2035 *pp = p + 1;
2036 val = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
2037 if (**pp != ',')
2039 bad_stab (orig);
2040 return DEBUG_TYPE_NULL;
2042 ++*pp;
2044 if (n + 1 >= alloc)
2046 alloc += 10;
2047 names = ((const char **)
2048 xrealloc ((PTR) names, alloc * sizeof *names));
2049 values = ((bfd_signed_vma *)
2050 xrealloc ((PTR) values, alloc * sizeof *values));
2053 names[n] = name;
2054 values[n] = val;
2055 ++n;
2058 names[n] = NULL;
2059 values[n] = 0;
2061 if (**pp == ';')
2062 ++*pp;
2064 return debug_make_enum_type (dhandle, names, values);
2067 /* Read the description of a structure (or union type) and return an object
2068 describing the type.
2070 PP points to a character pointer that points to the next unconsumed token
2071 in the the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
2072 *PP will point to "4a:1,0,32;;". */
2074 static debug_type
2075 parse_stab_struct_type (dhandle, info, tagname, pp, structp, typenums)
2076 PTR dhandle;
2077 struct stab_handle *info;
2078 const char *tagname;
2079 const char **pp;
2080 boolean structp;
2081 const int *typenums;
2083 const char *orig;
2084 bfd_vma size;
2085 debug_baseclass *baseclasses;
2086 debug_field *fields;
2087 boolean statics;
2088 debug_method *methods;
2089 debug_type vptrbase;
2090 boolean ownvptr;
2092 orig = *pp;
2094 /* Get the size. */
2095 size = parse_number (pp, (boolean *) NULL);
2097 /* Get the other information. */
2098 if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
2099 || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
2100 || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
2101 || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
2102 &ownvptr))
2103 return DEBUG_TYPE_NULL;
2105 if (! statics
2106 && baseclasses == NULL
2107 && methods == NULL
2108 && vptrbase == DEBUG_TYPE_NULL
2109 && ! ownvptr)
2110 return debug_make_struct_type (dhandle, structp, size, fields);
2112 return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
2113 methods, vptrbase, ownvptr);
2116 /* The stabs for C++ derived classes contain baseclass information which
2117 is marked by a '!' character after the total size. This function is
2118 called when we encounter the baseclass marker, and slurps up all the
2119 baseclass information.
2121 Immediately following the '!' marker is the number of base classes that
2122 the class is derived from, followed by information for each base class.
2123 For each base class, there are two visibility specifiers, a bit offset
2124 to the base class information within the derived class, a reference to
2125 the type for the base class, and a terminating semicolon.
2127 A typical example, with two base classes, would be "!2,020,19;0264,21;".
2128 ^^ ^ ^ ^ ^ ^ ^
2129 Baseclass information marker __________________|| | | | | | |
2130 Number of baseclasses __________________________| | | | | | |
2131 Visibility specifiers (2) ________________________| | | | | |
2132 Offset in bits from start of class _________________| | | | |
2133 Type number for base class ___________________________| | | |
2134 Visibility specifiers (2) _______________________________| | |
2135 Offset in bits from start of class ________________________| |
2136 Type number of base class ____________________________________|
2138 Return true for success, false for failure. */
2140 static boolean
2141 parse_stab_baseclasses (dhandle, info, pp, retp)
2142 PTR dhandle;
2143 struct stab_handle *info;
2144 const char **pp;
2145 debug_baseclass **retp;
2147 const char *orig;
2148 unsigned int c, i;
2149 debug_baseclass *classes;
2151 *retp = NULL;
2153 orig = *pp;
2155 if (**pp != '!')
2157 /* No base classes. */
2158 return true;
2160 ++*pp;
2162 c = (unsigned int) parse_number (pp, (boolean *) NULL);
2164 if (**pp != ',')
2166 bad_stab (orig);
2167 return false;
2169 ++*pp;
2171 classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2173 for (i = 0; i < c; i++)
2175 boolean virtual;
2176 enum debug_visibility visibility;
2177 bfd_vma bitpos;
2178 debug_type type;
2180 switch (**pp)
2182 case '0':
2183 virtual = false;
2184 break;
2185 case '1':
2186 virtual = true;
2187 break;
2188 default:
2189 warn_stab (orig, _("unknown virtual character for baseclass"));
2190 virtual = false;
2191 break;
2193 ++*pp;
2195 switch (**pp)
2197 case '0':
2198 visibility = DEBUG_VISIBILITY_PRIVATE;
2199 break;
2200 case '1':
2201 visibility = DEBUG_VISIBILITY_PROTECTED;
2202 break;
2203 case '2':
2204 visibility = DEBUG_VISIBILITY_PUBLIC;
2205 break;
2206 default:
2207 warn_stab (orig, _("unknown visibility character for baseclass"));
2208 visibility = DEBUG_VISIBILITY_PUBLIC;
2209 break;
2211 ++*pp;
2213 /* The remaining value is the bit offset of the portion of the
2214 object corresponding to this baseclass. Always zero in the
2215 absence of multiple inheritance. */
2216 bitpos = parse_number (pp, (boolean *) NULL);
2217 if (**pp != ',')
2219 bad_stab (orig);
2220 return false;
2222 ++*pp;
2224 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2225 (debug_type **) NULL);
2226 if (type == DEBUG_TYPE_NULL)
2227 return false;
2229 classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2230 visibility);
2231 if (classes[i] == DEBUG_BASECLASS_NULL)
2232 return false;
2234 if (**pp != ';')
2235 return false;
2236 ++*pp;
2239 classes[i] = DEBUG_BASECLASS_NULL;
2241 *retp = classes;
2243 return true;
2246 /* Read struct or class data fields. They have the form:
2248 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2250 At the end, we see a semicolon instead of a field.
2252 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2253 a static field.
2255 The optional VISIBILITY is one of:
2257 '/0' (VISIBILITY_PRIVATE)
2258 '/1' (VISIBILITY_PROTECTED)
2259 '/2' (VISIBILITY_PUBLIC)
2260 '/9' (VISIBILITY_IGNORE)
2262 or nothing, for C style fields with public visibility.
2264 Returns 1 for success, 0 for failure. */
2266 static boolean
2267 parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2268 PTR dhandle;
2269 struct stab_handle *info;
2270 const char **pp;
2271 debug_field **retp;
2272 boolean *staticsp;
2274 const char *orig;
2275 const char *p;
2276 debug_field *fields;
2277 unsigned int c;
2278 unsigned int alloc;
2280 *retp = NULL;
2281 *staticsp = false;
2283 orig = *pp;
2285 c = 0;
2286 alloc = 10;
2287 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2288 while (**pp != ';')
2290 /* FIXME: gdb checks os9k_stabs here. */
2292 p = *pp;
2294 /* Add 1 to c to leave room for NULL pointer at end. */
2295 if (c + 1 >= alloc)
2297 alloc += 10;
2298 fields = ((debug_field *)
2299 xrealloc ((PTR) fields, alloc * sizeof *fields));
2302 /* If it starts with CPLUS_MARKER it is a special abbreviation,
2303 unless the CPLUS_MARKER is followed by an underscore, in
2304 which case it is just the name of an anonymous type, which we
2305 should handle like any other type name. We accept either '$'
2306 or '.', because a field name can never contain one of these
2307 characters except as a CPLUS_MARKER. */
2309 if ((*p == '$' || *p == '.') && p[1] != '_')
2311 ++*pp;
2312 if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2313 return false;
2314 ++c;
2315 continue;
2318 /* Look for the ':' that separates the field name from the field
2319 values. Data members are delimited by a single ':', while member
2320 functions are delimited by a pair of ':'s. When we hit the member
2321 functions (if any), terminate scan loop and return. */
2323 p = strchr (p, ':');
2324 if (p == NULL)
2326 bad_stab (orig);
2327 return false;
2330 if (p[1] == ':')
2331 break;
2333 if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2334 staticsp))
2335 return false;
2337 ++c;
2340 fields[c] = DEBUG_FIELD_NULL;
2342 *retp = fields;
2344 return true;
2347 /* Special GNU C++ name. */
2349 static boolean
2350 parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2351 PTR dhandle;
2352 struct stab_handle *info;
2353 const char **pp;
2354 debug_field *retp;
2356 const char *orig;
2357 int cpp_abbrev;
2358 debug_type context;
2359 const char *name;
2360 const char *typename;
2361 debug_type type;
2362 bfd_vma bitpos;
2364 *retp = DEBUG_FIELD_NULL;
2366 orig = *pp;
2368 if (**pp != 'v')
2370 bad_stab (*pp);
2371 return false;
2373 ++*pp;
2375 cpp_abbrev = **pp;
2376 ++*pp;
2378 /* At this point, *pp points to something like "22:23=*22...", where
2379 the type number before the ':' is the "context" and everything
2380 after is a regular type definition. Lookup the type, find it's
2381 name, and construct the field name. */
2383 context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2384 (debug_type **) NULL);
2385 if (context == DEBUG_TYPE_NULL)
2386 return false;
2388 switch (cpp_abbrev)
2390 case 'f':
2391 /* $vf -- a virtual function table pointer. */
2392 name = "_vptr$";
2393 break;
2394 case 'b':
2395 /* $vb -- a virtual bsomethingorother */
2396 typename = debug_get_type_name (dhandle, context);
2397 if (typename == NULL)
2399 warn_stab (orig, _("unnamed $vb type"));
2400 typename = "FOO";
2402 name = concat ("_vb$", typename, (const char *) NULL);
2403 break;
2404 default:
2405 warn_stab (orig, _("unrecognized C++ abbreviation"));
2406 name = "INVALID_CPLUSPLUS_ABBREV";
2407 break;
2410 if (**pp != ':')
2412 bad_stab (orig);
2413 return false;
2415 ++*pp;
2417 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2418 (debug_type **) NULL);
2419 if (**pp != ',')
2421 bad_stab (orig);
2422 return false;
2424 ++*pp;
2426 bitpos = parse_number (pp, (boolean *) NULL);
2427 if (**pp != ';')
2429 bad_stab (orig);
2430 return false;
2432 ++*pp;
2434 *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2435 DEBUG_VISIBILITY_PRIVATE);
2436 if (*retp == DEBUG_FIELD_NULL)
2437 return false;
2439 return true;
2442 /* Parse a single field in a struct or union. */
2444 static boolean
2445 parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2446 PTR dhandle;
2447 struct stab_handle *info;
2448 const char **pp;
2449 const char *p;
2450 debug_field *retp;
2451 boolean *staticsp;
2453 const char *orig;
2454 char *name;
2455 enum debug_visibility visibility;
2456 debug_type type;
2457 bfd_vma bitpos;
2458 bfd_vma bitsize;
2460 orig = *pp;
2462 /* FIXME: gdb checks ARM_DEMANGLING here. */
2464 name = savestring (*pp, p - *pp);
2466 *pp = p + 1;
2468 if (**pp != '/')
2469 visibility = DEBUG_VISIBILITY_PUBLIC;
2470 else
2472 ++*pp;
2473 switch (**pp)
2475 case '0':
2476 visibility = DEBUG_VISIBILITY_PRIVATE;
2477 break;
2478 case '1':
2479 visibility = DEBUG_VISIBILITY_PROTECTED;
2480 break;
2481 case '2':
2482 visibility = DEBUG_VISIBILITY_PUBLIC;
2483 break;
2484 default:
2485 warn_stab (orig, _("unknown visibility character for field"));
2486 visibility = DEBUG_VISIBILITY_PUBLIC;
2487 break;
2489 ++*pp;
2492 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2493 (debug_type **) NULL);
2494 if (type == DEBUG_TYPE_NULL)
2495 return false;
2497 if (**pp == ':')
2499 char *varname;
2501 /* This is a static class member. */
2502 ++*pp;
2503 p = strchr (*pp, ';');
2504 if (p == NULL)
2506 bad_stab (orig);
2507 return false;
2510 varname = savestring (*pp, p - *pp);
2512 *pp = p + 1;
2514 *retp = debug_make_static_member (dhandle, name, type, varname,
2515 visibility);
2516 *staticsp = true;
2518 return true;
2521 if (**pp != ',')
2523 bad_stab (orig);
2524 return false;
2526 ++*pp;
2528 bitpos = parse_number (pp, (boolean *) NULL);
2529 if (**pp != ',')
2531 bad_stab (orig);
2532 return false;
2534 ++*pp;
2536 bitsize = parse_number (pp, (boolean *) NULL);
2537 if (**pp != ';')
2539 bad_stab (orig);
2540 return false;
2542 ++*pp;
2544 if (bitpos == 0 && bitsize == 0)
2546 /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2547 so, it is a field which has been optimized out. The correct
2548 stab for this case is to use VISIBILITY_IGNORE, but that is a
2549 recent invention. (2) It is a 0-size array. For example
2550 union { int num; char str[0]; } foo. Printing "<no value>"
2551 for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2552 will continue to work, and a 0-size array as a whole doesn't
2553 have any contents to print.
2555 I suspect this probably could also happen with gcc -gstabs
2556 (not -gstabs+) for static fields, and perhaps other C++
2557 extensions. Hopefully few people use -gstabs with gdb, since
2558 it is intended for dbx compatibility. */
2559 visibility = DEBUG_VISIBILITY_IGNORE;
2562 /* FIXME: gdb does some stuff here to mark fields as unpacked. */
2564 *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2566 return true;
2569 /* Read member function stabs info for C++ classes. The form of each member
2570 function data is:
2572 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2574 An example with two member functions is:
2576 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2578 For the case of overloaded operators, the format is op$::*.funcs, where
2579 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2580 name (such as `+=') and `.' marks the end of the operator name. */
2582 static boolean
2583 parse_stab_members (dhandle, info, tagname, pp, typenums, retp)
2584 PTR dhandle;
2585 struct stab_handle *info;
2586 const char *tagname;
2587 const char **pp;
2588 const int *typenums;
2589 debug_method **retp;
2591 const char *orig;
2592 debug_method *methods;
2593 unsigned int c;
2594 unsigned int alloc;
2596 *retp = NULL;
2598 orig = *pp;
2600 alloc = 0;
2601 methods = NULL;
2602 c = 0;
2604 while (**pp != ';')
2606 const char *p;
2607 char *name;
2608 debug_method_variant *variants;
2609 unsigned int cvars;
2610 unsigned int allocvars;
2611 debug_type look_ahead_type;
2613 p = strchr (*pp, ':');
2614 if (p == NULL || p[1] != ':')
2615 break;
2617 /* FIXME: Some systems use something other than '$' here. */
2618 if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2620 name = savestring (*pp, p - *pp);
2621 *pp = p + 2;
2623 else
2625 /* This is a completely wierd case. In order to stuff in the
2626 names that might contain colons (the usual name delimiter),
2627 Mike Tiemann defined a different name format which is
2628 signalled if the identifier is "op$". In that case, the
2629 format is "op$::XXXX." where XXXX is the name. This is
2630 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2631 *pp = p + 2;
2632 for (p = *pp; *p != '.' && *p != '\0'; p++)
2634 if (*p != '.')
2636 bad_stab (orig);
2637 return false;
2639 name = savestring (*pp, p - *pp);
2640 *pp = p + 1;
2643 allocvars = 10;
2644 variants = ((debug_method_variant *)
2645 xmalloc (allocvars * sizeof *variants));
2646 cvars = 0;
2648 look_ahead_type = DEBUG_TYPE_NULL;
2652 debug_type type;
2653 boolean stub;
2654 char *argtypes;
2655 enum debug_visibility visibility;
2656 boolean constp, volatilep, staticp;
2657 bfd_vma voffset;
2658 debug_type context;
2659 const char *physname;
2660 boolean varargs;
2662 if (look_ahead_type != DEBUG_TYPE_NULL)
2664 /* g++ version 1 kludge */
2665 type = look_ahead_type;
2666 look_ahead_type = DEBUG_TYPE_NULL;
2668 else
2670 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2671 (debug_type **) NULL);
2672 if (type == DEBUG_TYPE_NULL)
2673 return false;
2674 if (**pp != ':')
2676 bad_stab (orig);
2677 return false;
2681 ++*pp;
2682 p = strchr (*pp, ';');
2683 if (p == NULL)
2685 bad_stab (orig);
2686 return false;
2689 stub = false;
2690 if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
2691 && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
2692 stub = true;
2694 argtypes = savestring (*pp, p - *pp);
2695 *pp = p + 1;
2697 switch (**pp)
2699 case '0':
2700 visibility = DEBUG_VISIBILITY_PRIVATE;
2701 break;
2702 case '1':
2703 visibility = DEBUG_VISIBILITY_PROTECTED;
2704 break;
2705 default:
2706 visibility = DEBUG_VISIBILITY_PUBLIC;
2707 break;
2709 ++*pp;
2711 constp = false;
2712 volatilep = false;
2713 switch (**pp)
2715 case 'A':
2716 /* Normal function. */
2717 ++*pp;
2718 break;
2719 case 'B':
2720 /* const member function. */
2721 constp = true;
2722 ++*pp;
2723 break;
2724 case 'C':
2725 /* volatile member function. */
2726 volatilep = true;
2727 ++*pp;
2728 break;
2729 case 'D':
2730 /* const volatile member function. */
2731 constp = true;
2732 volatilep = true;
2733 ++*pp;
2734 break;
2735 case '*':
2736 case '?':
2737 case '.':
2738 /* File compiled with g++ version 1; no information. */
2739 break;
2740 default:
2741 warn_stab (orig, _("const/volatile indicator missing"));
2742 break;
2745 staticp = false;
2746 switch (**pp)
2748 case '*':
2749 /* virtual member function, followed by index. The sign
2750 bit is supposedly set to distinguish
2751 pointers-to-methods from virtual function indicies. */
2752 ++*pp;
2753 voffset = parse_number (pp, (boolean *) NULL);
2754 if (**pp != ';')
2756 bad_stab (orig);
2757 return false;
2759 ++*pp;
2760 voffset &= 0x7fffffff;
2762 if (**pp == ';' || *pp == '\0')
2764 /* Must be g++ version 1. */
2765 context = DEBUG_TYPE_NULL;
2767 else
2769 /* Figure out from whence this virtual function
2770 came. It may belong to virtual function table of
2771 one of its baseclasses. */
2772 look_ahead_type = parse_stab_type (dhandle, info,
2773 (const char *) NULL,
2775 (debug_type **) NULL);
2776 if (**pp == ':')
2778 /* g++ version 1 overloaded methods. */
2779 context = DEBUG_TYPE_NULL;
2781 else
2783 context = look_ahead_type;
2784 look_ahead_type = DEBUG_TYPE_NULL;
2785 if (**pp != ';')
2787 bad_stab (orig);
2788 return false;
2790 ++*pp;
2793 break;
2795 case '?':
2796 /* static member function. */
2797 ++*pp;
2798 staticp = true;
2799 voffset = 0;
2800 context = DEBUG_TYPE_NULL;
2801 if (strncmp (argtypes, name, strlen (name)) != 0)
2802 stub = true;
2803 break;
2805 default:
2806 warn_stab (orig, "member function type missing");
2807 voffset = 0;
2808 context = DEBUG_TYPE_NULL;
2809 break;
2811 case '.':
2812 ++*pp;
2813 voffset = 0;
2814 context = DEBUG_TYPE_NULL;
2815 break;
2818 /* If the type is not a stub, then the argtypes string is
2819 the physical name of the function. Otherwise the
2820 argtypes string is the mangled form of the argument
2821 types, and the full type and the physical name must be
2822 extracted from them. */
2823 if (! stub)
2824 physname = argtypes;
2825 else
2827 debug_type class_type, return_type;
2829 class_type = stab_find_type (dhandle, info, typenums);
2830 if (class_type == DEBUG_TYPE_NULL)
2831 return false;
2832 return_type = debug_get_return_type (dhandle, type);
2833 if (return_type == DEBUG_TYPE_NULL)
2835 bad_stab (orig);
2836 return false;
2838 type = parse_stab_argtypes (dhandle, info, class_type, name,
2839 tagname, return_type, argtypes,
2840 constp, volatilep, &physname);
2841 if (type == DEBUG_TYPE_NULL)
2842 return false;
2845 if (cvars + 1 >= allocvars)
2847 allocvars += 10;
2848 variants = ((debug_method_variant *)
2849 xrealloc ((PTR) variants,
2850 allocvars * sizeof *variants));
2853 if (! staticp)
2854 variants[cvars] = debug_make_method_variant (dhandle, physname,
2855 type, visibility,
2856 constp, volatilep,
2857 voffset, context);
2858 else
2859 variants[cvars] = debug_make_static_method_variant (dhandle,
2860 physname,
2861 type,
2862 visibility,
2863 constp,
2864 volatilep);
2865 if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2866 return false;
2868 ++cvars;
2870 while (**pp != ';' && **pp != '\0');
2872 variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2874 if (**pp != '\0')
2875 ++*pp;
2877 if (c + 1 >= alloc)
2879 alloc += 10;
2880 methods = ((debug_method *)
2881 xrealloc ((PTR) methods, alloc * sizeof *methods));
2884 methods[c] = debug_make_method (dhandle, name, variants);
2886 ++c;
2889 if (methods != NULL)
2890 methods[c] = DEBUG_METHOD_NULL;
2892 *retp = methods;
2894 return true;
2897 /* Parse a string representing argument types for a method. Stabs
2898 tries to save space by packing argument types into a mangled
2899 string. This string should give us enough information to extract
2900 both argument types and the physical name of the function, given
2901 the tag name. */
2903 static debug_type
2904 parse_stab_argtypes (dhandle, info, class_type, fieldname, tagname,
2905 return_type, argtypes, constp, volatilep, pphysname)
2906 PTR dhandle;
2907 struct stab_handle *info;
2908 debug_type class_type;
2909 const char *fieldname;
2910 const char *tagname;
2911 debug_type return_type;
2912 const char *argtypes;
2913 boolean constp;
2914 boolean volatilep;
2915 const char **pphysname;
2917 boolean is_full_physname_constructor;
2918 boolean is_constructor;
2919 boolean is_destructor;
2920 debug_type *args;
2921 boolean varargs;
2923 /* Constructors are sometimes handled specially. */
2924 is_full_physname_constructor = ((argtypes[0] == '_'
2925 && argtypes[1] == '_'
2926 && (isdigit ((unsigned char) argtypes[2])
2927 || argtypes[2] == 'Q'
2928 || argtypes[2] == 't'))
2929 || strncmp (argtypes, "__ct", 4) == 0);
2931 is_constructor = (is_full_physname_constructor
2932 || (tagname != NULL
2933 && strcmp (fieldname, tagname) == 0));
2934 is_destructor = ((argtypes[0] == '_'
2935 && (argtypes[1] == '$' || argtypes[1] == '.')
2936 && argtypes[2] == '_')
2937 || strncmp (argtypes, "__dt", 4) == 0);
2939 if (is_destructor || is_full_physname_constructor)
2940 *pphysname = argtypes;
2941 else
2943 unsigned int len;
2944 const char *const_prefix;
2945 const char *volatile_prefix;
2946 char buf[20];
2947 unsigned int mangled_name_len;
2948 char *physname;
2950 len = tagname == NULL ? 0 : strlen (tagname);
2951 const_prefix = constp ? "C" : "";
2952 volatile_prefix = volatilep ? "V" : "";
2954 if (len == 0)
2955 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2956 else if (tagname != NULL && strchr (tagname, '<') != NULL)
2958 /* Template methods are fully mangled. */
2959 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2960 tagname = NULL;
2961 len = 0;
2963 else
2964 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2966 mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2967 + strlen (buf)
2968 + len
2969 + strlen (argtypes)
2970 + 1);
2972 if (fieldname[0] == 'o'
2973 && fieldname[1] == 'p'
2974 && (fieldname[2] == '$' || fieldname[2] == '.'))
2976 const char *opname;
2978 opname = cplus_mangle_opname (fieldname + 3, 0);
2979 if (opname == NULL)
2981 fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
2982 return DEBUG_TYPE_NULL;
2984 mangled_name_len += strlen (opname);
2985 physname = (char *) xmalloc (mangled_name_len);
2986 strncpy (physname, fieldname, 3);
2987 strcpy (physname + 3, opname);
2989 else
2991 physname = (char *) xmalloc (mangled_name_len);
2992 if (is_constructor)
2993 physname[0] = '\0';
2994 else
2995 strcpy (physname, fieldname);
2998 strcat (physname, buf);
2999 if (tagname != NULL)
3000 strcat (physname, tagname);
3001 strcat (physname, argtypes);
3003 *pphysname = physname;
3006 if (*argtypes == '\0' || is_destructor)
3008 args = (debug_type *) xmalloc (sizeof *args);
3009 *args = NULL;
3010 return debug_make_method_type (dhandle, return_type, class_type, args,
3011 false);
3014 args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs);
3015 if (args == NULL)
3016 return DEBUG_TYPE_NULL;
3018 return debug_make_method_type (dhandle, return_type, class_type, args,
3019 varargs);
3022 /* The tail end of stabs for C++ classes that contain a virtual function
3023 pointer contains a tilde, a %, and a type number.
3024 The type number refers to the base class (possibly this class itself) which
3025 contains the vtable pointer for the current class.
3027 This function is called when we have parsed all the method declarations,
3028 so we can look for the vptr base class info. */
3030 static boolean
3031 parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
3032 PTR dhandle;
3033 struct stab_handle *info;
3034 const char **pp;
3035 const int *typenums;
3036 debug_type *retvptrbase;
3037 boolean *retownvptr;
3039 const char *orig;
3040 const char *hold;
3041 int vtypenums[2];
3043 *retvptrbase = DEBUG_TYPE_NULL;
3044 *retownvptr = false;
3046 orig = *pp;
3048 /* If we are positioned at a ';', then skip it. */
3049 if (**pp == ';')
3050 ++*pp;
3052 if (**pp != '~')
3053 return true;
3055 ++*pp;
3057 if (**pp == '=' || **pp == '+' || **pp == '-')
3059 /* Obsolete flags that used to indicate the presence of
3060 constructors and/or destructors. */
3061 ++*pp;
3064 if (**pp != '%')
3065 return true;
3067 ++*pp;
3069 hold = *pp;
3071 /* The next number is the type number of the base class (possibly
3072 our own class) which supplies the vtable for this class. */
3073 if (! parse_stab_type_number (pp, vtypenums))
3074 return false;
3076 if (vtypenums[0] == typenums[0]
3077 && vtypenums[1] == typenums[1])
3078 *retownvptr = true;
3079 else
3081 debug_type vtype;
3082 const char *p;
3084 *pp = hold;
3086 vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3087 (debug_type **) NULL);
3088 for (p = *pp; *p != ';' && *p != '\0'; p++)
3090 if (*p != ';')
3092 bad_stab (orig);
3093 return false;
3096 *retvptrbase = vtype;
3098 *pp = p + 1;
3101 return true;
3104 /* Read a definition of an array type. */
3106 static debug_type
3107 parse_stab_array_type (dhandle, info, pp, stringp)
3108 PTR dhandle;
3109 struct stab_handle *info;
3110 const char **pp;
3111 boolean stringp;
3113 const char *orig;
3114 const char *p;
3115 int typenums[2];
3116 debug_type index_type;
3117 boolean adjustable;
3118 bfd_signed_vma lower, upper;
3119 debug_type element_type;
3121 /* Format of an array type:
3122 "ar<index type>;lower;upper;<array_contents_type>".
3123 OS9000: "arlower,upper;<array_contents_type>".
3125 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3126 for these, produce a type like float[][]. */
3128 orig = *pp;
3130 /* FIXME: gdb checks os9k_stabs here. */
3132 /* If the index type is type 0, we take it as int. */
3133 p = *pp;
3134 if (! parse_stab_type_number (&p, typenums))
3135 return DEBUG_TYPE_NULL;
3136 if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
3138 index_type = debug_find_named_type (dhandle, "int");
3139 if (index_type == DEBUG_TYPE_NULL)
3141 index_type = debug_make_int_type (dhandle, 4, false);
3142 if (index_type == DEBUG_TYPE_NULL)
3143 return DEBUG_TYPE_NULL;
3145 *pp = p;
3147 else
3149 index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3150 (debug_type **) NULL);
3153 if (**pp != ';')
3155 bad_stab (orig);
3156 return DEBUG_TYPE_NULL;
3158 ++*pp;
3160 adjustable = false;
3162 if (! isdigit ((unsigned char) **pp) && **pp != '-')
3164 ++*pp;
3165 adjustable = true;
3168 lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3169 if (**pp != ';')
3171 bad_stab (orig);
3172 return DEBUG_TYPE_NULL;
3174 ++*pp;
3176 if (! isdigit ((unsigned char) **pp) && **pp != '-')
3178 ++*pp;
3179 adjustable = true;
3182 upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3183 if (**pp != ';')
3185 bad_stab (orig);
3186 return DEBUG_TYPE_NULL;
3188 ++*pp;
3190 element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3191 (debug_type **) NULL);
3192 if (element_type == DEBUG_TYPE_NULL)
3193 return DEBUG_TYPE_NULL;
3195 if (adjustable)
3197 lower = 0;
3198 upper = -1;
3201 return debug_make_array_type (dhandle, element_type, index_type, lower,
3202 upper, stringp);
3205 /* This struct holds information about files we have seen using
3206 N_BINCL. */
3208 struct bincl_file
3210 /* The next N_BINCL file. */
3211 struct bincl_file *next;
3212 /* The next N_BINCL on the stack. */
3213 struct bincl_file *next_stack;
3214 /* The file name. */
3215 const char *name;
3216 /* The hash value. */
3217 bfd_vma hash;
3218 /* The file index. */
3219 unsigned int file;
3220 /* The list of types defined in this file. */
3221 struct stab_types *file_types;
3224 /* Start a new N_BINCL file, pushing it onto the stack. */
3226 static void
3227 push_bincl (info, name, hash)
3228 struct stab_handle *info;
3229 const char *name;
3230 bfd_vma hash;
3232 struct bincl_file *n;
3234 n = (struct bincl_file *) xmalloc (sizeof *n);
3235 n->next = info->bincl_list;
3236 n->next_stack = info->bincl_stack;
3237 n->name = name;
3238 n->hash = hash;
3239 n->file = info->files;
3240 n->file_types = NULL;
3241 info->bincl_list = n;
3242 info->bincl_stack = n;
3244 ++info->files;
3245 info->file_types = ((struct stab_types **)
3246 xrealloc ((PTR) info->file_types,
3247 (info->files
3248 * sizeof *info->file_types)));
3249 info->file_types[n->file] = NULL;
3252 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3253 stack. */
3255 static const char *
3256 pop_bincl (info)
3257 struct stab_handle *info;
3259 struct bincl_file *o;
3261 o = info->bincl_stack;
3262 if (o == NULL)
3263 return info->main_filename;
3264 info->bincl_stack = o->next_stack;
3266 o->file_types = info->file_types[o->file];
3268 if (info->bincl_stack == NULL)
3269 return info->main_filename;
3270 return info->bincl_stack->name;
3273 /* Handle an N_EXCL: get the types from the corresponding N_BINCL. */
3275 static boolean
3276 find_excl (info, name, hash)
3277 struct stab_handle *info;
3278 const char *name;
3279 bfd_vma hash;
3281 struct bincl_file *l;
3283 ++info->files;
3284 info->file_types = ((struct stab_types **)
3285 xrealloc ((PTR) info->file_types,
3286 (info->files
3287 * sizeof *info->file_types)));
3289 for (l = info->bincl_list; l != NULL; l = l->next)
3290 if (l->hash == hash && strcmp (l->name, name) == 0)
3291 break;
3292 if (l == NULL)
3294 warn_stab (name, _("Undefined N_EXCL"));
3295 info->file_types[info->files - 1] = NULL;
3296 return true;
3299 info->file_types[info->files - 1] = l->file_types;
3301 return true;
3304 /* Handle a variable definition. gcc emits variable definitions for a
3305 block before the N_LBRAC, so we must hold onto them until we see
3306 it. The SunPRO compiler emits variable definitions after the
3307 N_LBRAC, so we can call debug_record_variable immediately. */
3309 static boolean
3310 stab_record_variable (dhandle, info, name, type, kind, val)
3311 PTR dhandle;
3312 struct stab_handle *info;
3313 const char *name;
3314 debug_type type;
3315 enum debug_var_kind kind;
3316 bfd_vma val;
3318 struct stab_pending_var *v;
3320 if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3321 || ! info->within_function
3322 || (info->gcc_compiled == 0 && info->n_opt_found))
3323 return debug_record_variable (dhandle, name, type, kind, val);
3325 v = (struct stab_pending_var *) xmalloc (sizeof *v);
3326 memset (v, 0, sizeof *v);
3328 v->next = info->pending;
3329 v->name = name;
3330 v->type = type;
3331 v->kind = kind;
3332 v->val = val;
3333 info->pending = v;
3335 return true;
3338 /* Emit pending variable definitions. This is called after we see the
3339 N_LBRAC that starts the block. */
3341 static boolean
3342 stab_emit_pending_vars (dhandle, info)
3343 PTR dhandle;
3344 struct stab_handle *info;
3346 struct stab_pending_var *v;
3348 v = info->pending;
3349 while (v != NULL)
3351 struct stab_pending_var *next;
3353 if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3354 return false;
3356 next = v->next;
3357 free (v);
3358 v = next;
3361 info->pending = NULL;
3363 return true;
3366 /* Find the slot for a type in the database. */
3368 static debug_type *
3369 stab_find_slot (info, typenums)
3370 struct stab_handle *info;
3371 const int *typenums;
3373 int filenum;
3374 int index;
3375 struct stab_types **ps;
3377 filenum = typenums[0];
3378 index = typenums[1];
3380 if (filenum < 0 || (unsigned int) filenum >= info->files)
3382 fprintf (stderr, _("Type file number %d out of range\n"), filenum);
3383 return NULL;
3385 if (index < 0)
3387 fprintf (stderr, _("Type index number %d out of range\n"), index);
3388 return NULL;
3391 ps = info->file_types + filenum;
3393 while (index >= STAB_TYPES_SLOTS)
3395 if (*ps == NULL)
3397 *ps = (struct stab_types *) xmalloc (sizeof **ps);
3398 memset (*ps, 0, sizeof **ps);
3400 ps = &(*ps)->next;
3401 index -= STAB_TYPES_SLOTS;
3403 if (*ps == NULL)
3405 *ps = (struct stab_types *) xmalloc (sizeof **ps);
3406 memset (*ps, 0, sizeof **ps);
3409 return (*ps)->types + index;
3412 /* Find a type given a type number. If the type has not been
3413 allocated yet, create an indirect type. */
3415 static debug_type
3416 stab_find_type (dhandle, info, typenums)
3417 PTR dhandle;
3418 struct stab_handle *info;
3419 const int *typenums;
3421 debug_type *slot;
3423 if (typenums[0] == 0 && typenums[1] < 0)
3425 /* A negative type number indicates an XCOFF builtin type. */
3426 return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3429 slot = stab_find_slot (info, typenums);
3430 if (slot == NULL)
3431 return DEBUG_TYPE_NULL;
3433 if (*slot == DEBUG_TYPE_NULL)
3434 return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3436 return *slot;
3439 /* Record that a given type number refers to a given type. */
3441 static boolean
3442 stab_record_type (dhandle, info, typenums, type)
3443 PTR dhandle ATTRIBUTE_UNUSED;
3444 struct stab_handle *info;
3445 const int *typenums;
3446 debug_type type;
3448 debug_type *slot;
3450 slot = stab_find_slot (info, typenums);
3451 if (slot == NULL)
3452 return false;
3454 /* gdb appears to ignore type redefinitions, so we do as well. */
3456 *slot = type;
3458 return true;
3461 /* Return an XCOFF builtin type. */
3463 static debug_type
3464 stab_xcoff_builtin_type (dhandle, info, typenum)
3465 PTR dhandle;
3466 struct stab_handle *info;
3467 int typenum;
3469 debug_type rettype;
3470 const char *name;
3472 if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3474 fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
3475 return DEBUG_TYPE_NULL;
3477 if (info->xcoff_types[-typenum] != NULL)
3478 return info->xcoff_types[-typenum];
3480 switch (-typenum)
3482 case 1:
3483 /* The size of this and all the other types are fixed, defined
3484 by the debugging format. */
3485 name = "int";
3486 rettype = debug_make_int_type (dhandle, 4, false);
3487 break;
3488 case 2:
3489 name = "char";
3490 rettype = debug_make_int_type (dhandle, 1, false);
3491 break;
3492 case 3:
3493 name = "short";
3494 rettype = debug_make_int_type (dhandle, 2, false);
3495 break;
3496 case 4:
3497 name = "long";
3498 rettype = debug_make_int_type (dhandle, 4, false);
3499 break;
3500 case 5:
3501 name = "unsigned char";
3502 rettype = debug_make_int_type (dhandle, 1, true);
3503 break;
3504 case 6:
3505 name = "signed char";
3506 rettype = debug_make_int_type (dhandle, 1, false);
3507 break;
3508 case 7:
3509 name = "unsigned short";
3510 rettype = debug_make_int_type (dhandle, 2, true);
3511 break;
3512 case 8:
3513 name = "unsigned int";
3514 rettype = debug_make_int_type (dhandle, 4, true);
3515 break;
3516 case 9:
3517 name = "unsigned";
3518 rettype = debug_make_int_type (dhandle, 4, true);
3519 case 10:
3520 name = "unsigned long";
3521 rettype = debug_make_int_type (dhandle, 4, true);
3522 break;
3523 case 11:
3524 name = "void";
3525 rettype = debug_make_void_type (dhandle);
3526 break;
3527 case 12:
3528 /* IEEE single precision (32 bit). */
3529 name = "float";
3530 rettype = debug_make_float_type (dhandle, 4);
3531 break;
3532 case 13:
3533 /* IEEE double precision (64 bit). */
3534 name = "double";
3535 rettype = debug_make_float_type (dhandle, 8);
3536 break;
3537 case 14:
3538 /* This is an IEEE double on the RS/6000, and different machines
3539 with different sizes for "long double" should use different
3540 negative type numbers. See stabs.texinfo. */
3541 name = "long double";
3542 rettype = debug_make_float_type (dhandle, 8);
3543 break;
3544 case 15:
3545 name = "integer";
3546 rettype = debug_make_int_type (dhandle, 4, false);
3547 break;
3548 case 16:
3549 name = "boolean";
3550 rettype = debug_make_bool_type (dhandle, 4);
3551 break;
3552 case 17:
3553 name = "short real";
3554 rettype = debug_make_float_type (dhandle, 4);
3555 break;
3556 case 18:
3557 name = "real";
3558 rettype = debug_make_float_type (dhandle, 8);
3559 break;
3560 case 19:
3561 /* FIXME */
3562 name = "stringptr";
3563 rettype = NULL;
3564 break;
3565 case 20:
3566 /* FIXME */
3567 name = "character";
3568 rettype = debug_make_int_type (dhandle, 1, true);
3569 break;
3570 case 21:
3571 name = "logical*1";
3572 rettype = debug_make_bool_type (dhandle, 1);
3573 break;
3574 case 22:
3575 name = "logical*2";
3576 rettype = debug_make_bool_type (dhandle, 2);
3577 break;
3578 case 23:
3579 name = "logical*4";
3580 rettype = debug_make_bool_type (dhandle, 4);
3581 break;
3582 case 24:
3583 name = "logical";
3584 rettype = debug_make_bool_type (dhandle, 4);
3585 break;
3586 case 25:
3587 /* Complex type consisting of two IEEE single precision values. */
3588 name = "complex";
3589 rettype = debug_make_complex_type (dhandle, 8);
3590 break;
3591 case 26:
3592 /* Complex type consisting of two IEEE double precision values. */
3593 name = "double complex";
3594 rettype = debug_make_complex_type (dhandle, 16);
3595 break;
3596 case 27:
3597 name = "integer*1";
3598 rettype = debug_make_int_type (dhandle, 1, false);
3599 break;
3600 case 28:
3601 name = "integer*2";
3602 rettype = debug_make_int_type (dhandle, 2, false);
3603 break;
3604 case 29:
3605 name = "integer*4";
3606 rettype = debug_make_int_type (dhandle, 4, false);
3607 break;
3608 case 30:
3609 /* FIXME */
3610 name = "wchar";
3611 rettype = debug_make_int_type (dhandle, 2, false);
3612 break;
3613 case 31:
3614 name = "long long";
3615 rettype = debug_make_int_type (dhandle, 8, false);
3616 break;
3617 case 32:
3618 name = "unsigned long long";
3619 rettype = debug_make_int_type (dhandle, 8, true);
3620 break;
3621 case 33:
3622 name = "logical*8";
3623 rettype = debug_make_bool_type (dhandle, 8);
3624 break;
3625 case 34:
3626 name = "integer*8";
3627 rettype = debug_make_int_type (dhandle, 8, false);
3628 break;
3629 default:
3630 abort ();
3633 rettype = debug_name_type (dhandle, name, rettype);
3635 info->xcoff_types[-typenum] = rettype;
3637 return rettype;
3640 /* Find or create a tagged type. */
3642 static debug_type
3643 stab_find_tagged_type (dhandle, info, p, len, kind)
3644 PTR dhandle;
3645 struct stab_handle *info;
3646 const char *p;
3647 int len;
3648 enum debug_type_kind kind;
3650 char *name;
3651 debug_type dtype;
3652 struct stab_tag *st;
3654 name = savestring (p, len);
3656 /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3657 namespace. This is right for C, and I don't know how to handle
3658 other languages. FIXME. */
3659 dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3660 if (dtype != DEBUG_TYPE_NULL)
3662 free (name);
3663 return dtype;
3666 /* We need to allocate an entry on the undefined tag list. */
3667 for (st = info->tags; st != NULL; st = st->next)
3669 if (st->name[0] == name[0]
3670 && strcmp (st->name, name) == 0)
3672 if (st->kind == DEBUG_KIND_ILLEGAL)
3673 st->kind = kind;
3674 free (name);
3675 break;
3678 if (st == NULL)
3680 st = (struct stab_tag *) xmalloc (sizeof *st);
3681 memset (st, 0, sizeof *st);
3683 st->next = info->tags;
3684 st->name = name;
3685 st->kind = kind;
3686 st->slot = DEBUG_TYPE_NULL;
3687 st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3688 info->tags = st;
3691 return st->type;
3694 /* In order to get the correct argument types for a stubbed method, we
3695 need to extract the argument types from a C++ mangled string.
3696 Since the argument types can refer back to the return type, this
3697 means that we must demangle the entire physical name. In gdb this
3698 is done by calling cplus_demangle and running the results back
3699 through the C++ expression parser. Since we have no expression
3700 parser, we must duplicate much of the work of cplus_demangle here.
3702 We assume that GNU style demangling is used, since this is only
3703 done for method stubs, and only g++ should output that form of
3704 debugging information. */
3706 /* This structure is used to hold a pointer to type information which
3707 demangling a string. */
3709 struct stab_demangle_typestring
3711 /* The start of the type. This is not null terminated. */
3712 const char *typestring;
3713 /* The length of the type. */
3714 unsigned int len;
3717 /* This structure is used to hold information while demangling a
3718 string. */
3720 struct stab_demangle_info
3722 /* The debugging information handle. */
3723 PTR dhandle;
3724 /* The stab information handle. */
3725 struct stab_handle *info;
3726 /* The array of arguments we are building. */
3727 debug_type *args;
3728 /* Whether the method takes a variable number of arguments. */
3729 boolean varargs;
3730 /* The array of types we have remembered. */
3731 struct stab_demangle_typestring *typestrings;
3732 /* The number of typestrings. */
3733 unsigned int typestring_count;
3734 /* The number of typestring slots we have allocated. */
3735 unsigned int typestring_alloc;
3738 static void stab_bad_demangle PARAMS ((const char *));
3739 static unsigned int stab_demangle_count PARAMS ((const char **));
3740 static boolean stab_demangle_get_count
3741 PARAMS ((const char **, unsigned int *));
3742 static boolean stab_demangle_prefix
3743 PARAMS ((struct stab_demangle_info *, const char **));
3744 static boolean stab_demangle_function_name
3745 PARAMS ((struct stab_demangle_info *, const char **, const char *));
3746 static boolean stab_demangle_signature
3747 PARAMS ((struct stab_demangle_info *, const char **));
3748 static boolean stab_demangle_qualified
3749 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3750 static boolean stab_demangle_template
3751 PARAMS ((struct stab_demangle_info *, const char **, char **));
3752 static boolean stab_demangle_class
3753 PARAMS ((struct stab_demangle_info *, const char **, const char **));
3754 static boolean stab_demangle_args
3755 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3756 boolean *));
3757 static boolean stab_demangle_arg
3758 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3759 unsigned int *, unsigned int *));
3760 static boolean stab_demangle_type
3761 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3762 static boolean stab_demangle_fund_type
3763 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3764 static boolean stab_demangle_remember_type
3765 PARAMS ((struct stab_demangle_info *, const char *, int));
3767 /* Warn about a bad demangling. */
3769 static void
3770 stab_bad_demangle (s)
3771 const char *s;
3773 fprintf (stderr, _("bad mangled name `%s'\n"), s);
3776 /* Get a count from a stab string. */
3778 static unsigned int
3779 stab_demangle_count (pp)
3780 const char **pp;
3782 unsigned int count;
3784 count = 0;
3785 while (isdigit ((unsigned char) **pp))
3787 count *= 10;
3788 count += **pp - '0';
3789 ++*pp;
3791 return count;
3794 /* Require a count in a string. The count may be multiple digits, in
3795 which case it must end in an underscore. */
3797 static boolean
3798 stab_demangle_get_count (pp, pi)
3799 const char **pp;
3800 unsigned int *pi;
3802 if (! isdigit ((unsigned char) **pp))
3803 return false;
3805 *pi = **pp - '0';
3806 ++*pp;
3807 if (isdigit ((unsigned char) **pp))
3809 unsigned int count;
3810 const char *p;
3812 count = *pi;
3813 p = *pp;
3816 count *= 10;
3817 count += *p - '0';
3818 ++p;
3820 while (isdigit ((unsigned char) *p));
3821 if (*p == '_')
3823 *pp = p + 1;
3824 *pi = count;
3828 return true;
3831 /* This function demangles a physical name, returning a NULL
3832 terminated array of argument types. */
3834 static debug_type *
3835 stab_demangle_argtypes (dhandle, info, physname, pvarargs)
3836 PTR dhandle;
3837 struct stab_handle *info;
3838 const char *physname;
3839 boolean *pvarargs;
3841 struct stab_demangle_info minfo;
3843 minfo.dhandle = dhandle;
3844 minfo.info = info;
3845 minfo.args = NULL;
3846 minfo.varargs = false;
3847 minfo.typestring_alloc = 10;
3848 minfo.typestrings = ((struct stab_demangle_typestring *)
3849 xmalloc (minfo.typestring_alloc
3850 * sizeof *minfo.typestrings));
3851 minfo.typestring_count = 0;
3853 /* cplus_demangle checks for special GNU mangled forms, but we can't
3854 see any of them in mangled method argument types. */
3856 if (! stab_demangle_prefix (&minfo, &physname))
3857 goto error_return;
3859 if (*physname != '\0')
3861 if (! stab_demangle_signature (&minfo, &physname))
3862 goto error_return;
3865 free (minfo.typestrings);
3866 minfo.typestrings = NULL;
3868 if (minfo.args == NULL)
3869 fprintf (stderr, _("no argument types in mangled string\n"));
3871 *pvarargs = minfo.varargs;
3872 return minfo.args;
3874 error_return:
3875 if (minfo.typestrings != NULL)
3876 free (minfo.typestrings);
3877 return NULL;
3880 /* Demangle the prefix of the mangled name. */
3882 static boolean
3883 stab_demangle_prefix (minfo, pp)
3884 struct stab_demangle_info *minfo;
3885 const char **pp;
3887 const char *scan;
3888 unsigned int i;
3890 /* cplus_demangle checks for global constructors and destructors,
3891 but we can't see them in mangled argument types. */
3893 /* Look for `__'. */
3894 scan = *pp;
3897 scan = strchr (scan, '_');
3899 while (scan != NULL && *++scan != '_');
3901 if (scan == NULL)
3903 stab_bad_demangle (*pp);
3904 return false;
3907 --scan;
3909 /* We found `__'; move ahead to the last contiguous `__' pair. */
3910 i = strspn (scan, "_");
3911 if (i > 2)
3912 scan += i - 2;
3914 if (scan == *pp
3915 && (isdigit ((unsigned char) scan[2])
3916 || scan[2] == 'Q'
3917 || scan[2] == 't'))
3919 /* This is a GNU style constructor name. */
3920 *pp = scan + 2;
3921 return true;
3923 else if (scan == *pp
3924 && ! isdigit ((unsigned char) scan[2])
3925 && scan[2] != 't')
3927 /* Look for the `__' that separates the prefix from the
3928 signature. */
3929 while (*scan == '_')
3930 ++scan;
3931 scan = strstr (scan, "__");
3932 if (scan == NULL || scan[2] == '\0')
3934 stab_bad_demangle (*pp);
3935 return false;
3938 return stab_demangle_function_name (minfo, pp, scan);
3940 else if (scan[2] != '\0')
3942 /* The name doesn't start with `__', but it does contain `__'. */
3943 return stab_demangle_function_name (minfo, pp, scan);
3945 else
3947 stab_bad_demangle (*pp);
3948 return false;
3950 /*NOTREACHED*/
3953 /* Demangle a function name prefix. The scan argument points to the
3954 double underscore which separates the function name from the
3955 signature. */
3957 static boolean
3958 stab_demangle_function_name (minfo, pp, scan)
3959 struct stab_demangle_info *minfo;
3960 const char **pp;
3961 const char *scan;
3963 const char *name;
3965 /* The string from *pp to scan is the name of the function. We
3966 don't care about the name, since we just looking for argument
3967 types. However, for conversion operators, the name may include a
3968 type which we must remember in order to handle backreferences. */
3970 name = *pp;
3971 *pp = scan + 2;
3973 if (*pp - name >= 5
3974 && strncmp (name, "type", 4) == 0
3975 && (name[4] == '$' || name[4] == '.'))
3977 const char *tem;
3979 /* This is a type conversion operator. */
3980 tem = name + 5;
3981 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3982 return false;
3984 else if (name[0] == '_'
3985 && name[1] == '_'
3986 && name[2] == 'o'
3987 && name[3] == 'p')
3989 const char *tem;
3991 /* This is a type conversion operator. */
3992 tem = name + 4;
3993 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3994 return false;
3997 return true;
4000 /* Demangle the signature. This is where the argument types are
4001 found. */
4003 static boolean
4004 stab_demangle_signature (minfo, pp)
4005 struct stab_demangle_info *minfo;
4006 const char **pp;
4008 const char *orig;
4009 boolean expect_func, func_done;
4010 const char *hold;
4012 orig = *pp;
4014 expect_func = false;
4015 func_done = false;
4016 hold = NULL;
4018 while (**pp != '\0')
4020 switch (**pp)
4022 case 'Q':
4023 hold = *pp;
4024 if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
4025 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4026 return false;
4027 expect_func = true;
4028 hold = NULL;
4029 break;
4031 case 'S':
4032 /* Static member function. FIXME: Can this happen? */
4033 if (hold == NULL)
4034 hold = *pp;
4035 ++*pp;
4036 break;
4038 case 'C':
4039 /* Const member function. */
4040 if (hold == NULL)
4041 hold = *pp;
4042 ++*pp;
4043 break;
4045 case '0': case '1': case '2': case '3': case '4':
4046 case '5': case '6': case '7': case '8': case '9':
4047 if (hold == NULL)
4048 hold = *pp;
4049 if (! stab_demangle_class (minfo, pp, (const char **) NULL)
4050 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4051 return false;
4052 expect_func = true;
4053 hold = NULL;
4054 break;
4056 case 'F':
4057 /* Function. I don't know if this actually happens with g++
4058 output. */
4059 hold = NULL;
4060 func_done = true;
4061 ++*pp;
4062 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4063 return false;
4064 break;
4066 case 't':
4067 /* Template. */
4068 if (hold == NULL)
4069 hold = *pp;
4070 if (! stab_demangle_template (minfo, pp, (char **) NULL)
4071 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4072 return false;
4073 hold = NULL;
4074 expect_func = true;
4075 break;
4077 case '_':
4078 /* At the outermost level, we cannot have a return type
4079 specified, so if we run into another '_' at this point we
4080 are dealing with a mangled name that is either bogus, or
4081 has been mangled by some algorithm we don't know how to
4082 deal with. So just reject the entire demangling. */
4083 stab_bad_demangle (orig);
4084 return false;
4086 default:
4087 /* Assume we have stumbled onto the first outermost function
4088 argument token, and start processing args. */
4089 func_done = true;
4090 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4091 return false;
4092 break;
4095 if (expect_func)
4097 func_done = true;
4098 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4099 return false;
4103 if (! func_done)
4105 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4106 bar__3fooi is 'foo::bar(int)'. We get here when we find the
4107 first case, and need to ensure that the '(void)' gets added
4108 to the current declp. */
4109 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4110 return false;
4113 return true;
4116 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4117 mangled form of "Outer::Inner". */
4119 static boolean
4120 stab_demangle_qualified (minfo, pp, ptype)
4121 struct stab_demangle_info *minfo;
4122 const char **pp;
4123 debug_type *ptype;
4125 const char *orig;
4126 const char *p;
4127 unsigned int qualifiers;
4128 debug_type context;
4130 orig = *pp;
4132 switch ((*pp)[1])
4134 case '_':
4135 /* GNU mangled name with more than 9 classes. The count is
4136 preceded by an underscore (to distinguish it from the <= 9
4137 case) and followed by an underscore. */
4138 p = *pp + 2;
4139 if (! isdigit ((unsigned char) *p) || *p == '0')
4141 stab_bad_demangle (orig);
4142 return false;
4144 qualifiers = atoi (p);
4145 while (isdigit ((unsigned char) *p))
4146 ++p;
4147 if (*p != '_')
4149 stab_bad_demangle (orig);
4150 return false;
4152 *pp = p + 1;
4153 break;
4155 case '1': case '2': case '3': case '4': case '5':
4156 case '6': case '7': case '8': case '9':
4157 qualifiers = (*pp)[1] - '0';
4158 /* Skip an optional underscore after the count. */
4159 if ((*pp)[2] == '_')
4160 ++*pp;
4161 *pp += 2;
4162 break;
4164 case '0':
4165 default:
4166 stab_bad_demangle (orig);
4167 return false;
4170 context = DEBUG_TYPE_NULL;
4172 /* Pick off the names. */
4173 while (qualifiers-- > 0)
4175 if (**pp == '_')
4176 ++*pp;
4177 if (**pp == 't')
4179 char *name;
4181 if (! stab_demangle_template (minfo, pp,
4182 ptype != NULL ? &name : NULL))
4183 return false;
4185 if (ptype != NULL)
4187 context = stab_find_tagged_type (minfo->dhandle, minfo->info,
4188 name, strlen (name),
4189 DEBUG_KIND_CLASS);
4190 free (name);
4191 if (context == DEBUG_TYPE_NULL)
4192 return false;
4195 else
4197 unsigned int len;
4199 len = stab_demangle_count (pp);
4200 if (strlen (*pp) < len)
4202 stab_bad_demangle (orig);
4203 return false;
4206 if (ptype != NULL)
4208 const debug_field *fields;
4210 fields = NULL;
4211 if (context != DEBUG_TYPE_NULL)
4212 fields = debug_get_fields (minfo->dhandle, context);
4214 context = DEBUG_TYPE_NULL;
4216 if (fields != NULL)
4218 char *name;
4220 /* Try to find the type by looking through the
4221 fields of context until we find a field with the
4222 same type. This ought to work for a class
4223 defined within a class, but it won't work for,
4224 e.g., an enum defined within a class. stabs does
4225 not give us enough information to figure out the
4226 latter case. */
4228 name = savestring (*pp, len);
4230 for (; *fields != DEBUG_FIELD_NULL; fields++)
4232 debug_type ft;
4233 const char *dn;
4235 ft = debug_get_field_type (minfo->dhandle, *fields);
4236 if (ft == NULL)
4237 return false;
4238 dn = debug_get_type_name (minfo->dhandle, ft);
4239 if (dn != NULL && strcmp (dn, name) == 0)
4241 context = ft;
4242 break;
4246 free (name);
4249 if (context == DEBUG_TYPE_NULL)
4251 /* We have to fall back on finding the type by name.
4252 If there are more types to come, then this must
4253 be a class. Otherwise, it could be anything. */
4255 if (qualifiers == 0)
4257 char *name;
4259 name = savestring (*pp, len);
4260 context = debug_find_named_type (minfo->dhandle,
4261 name);
4262 free (name);
4265 if (context == DEBUG_TYPE_NULL)
4267 context = stab_find_tagged_type (minfo->dhandle,
4268 minfo->info,
4269 *pp, len,
4270 (qualifiers == 0
4271 ? DEBUG_KIND_ILLEGAL
4272 : DEBUG_KIND_CLASS));
4273 if (context == DEBUG_TYPE_NULL)
4274 return false;
4279 *pp += len;
4283 if (ptype != NULL)
4284 *ptype = context;
4286 return true;
4289 /* Demangle a template. If PNAME is not NULL, this sets *PNAME to a
4290 string representation of the template. */
4292 static boolean
4293 stab_demangle_template (minfo, pp, pname)
4294 struct stab_demangle_info *minfo;
4295 const char **pp;
4296 char **pname;
4298 const char *orig;
4299 unsigned int r, i;
4301 orig = *pp;
4303 ++*pp;
4305 /* Skip the template name. */
4306 r = stab_demangle_count (pp);
4307 if (r == 0 || strlen (*pp) < r)
4309 stab_bad_demangle (orig);
4310 return false;
4312 *pp += r;
4314 /* Get the size of the parameter list. */
4315 if (stab_demangle_get_count (pp, &r) == 0)
4317 stab_bad_demangle (orig);
4318 return false;
4321 for (i = 0; i < r; i++)
4323 if (**pp == 'Z')
4325 /* This is a type parameter. */
4326 ++*pp;
4327 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4328 return false;
4330 else
4332 const char *old_p;
4333 boolean pointerp, realp, integralp, charp, boolp;
4334 boolean done;
4336 old_p = *pp;
4337 pointerp = false;
4338 realp = false;
4339 integralp = false;
4340 charp = false;
4341 boolp = false;
4342 done = false;
4344 /* This is a value parameter. */
4346 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4347 return false;
4349 while (*old_p != '\0' && ! done)
4351 switch (*old_p)
4353 case 'P':
4354 case 'p':
4355 case 'R':
4356 pointerp = true;
4357 done = true;
4358 break;
4359 case 'C': /* Const. */
4360 case 'S': /* Signed. */
4361 case 'U': /* Unsigned. */
4362 case 'V': /* Volatile. */
4363 case 'F': /* Function. */
4364 case 'M': /* Member function. */
4365 case 'O': /* ??? */
4366 ++old_p;
4367 break;
4368 case 'Q': /* Qualified name. */
4369 integralp = true;
4370 done = true;
4371 break;
4372 case 'T': /* Remembered type. */
4373 abort ();
4374 case 'v': /* Void. */
4375 abort ();
4376 case 'x': /* Long long. */
4377 case 'l': /* Long. */
4378 case 'i': /* Int. */
4379 case 's': /* Short. */
4380 case 'w': /* Wchar_t. */
4381 integralp = true;
4382 done = true;
4383 break;
4384 case 'b': /* Bool. */
4385 boolp = true;
4386 done = true;
4387 break;
4388 case 'c': /* Char. */
4389 charp = true;
4390 done = true;
4391 break;
4392 case 'r': /* Long double. */
4393 case 'd': /* Double. */
4394 case 'f': /* Float. */
4395 realp = true;
4396 done = true;
4397 break;
4398 default:
4399 /* Assume it's a user defined integral type. */
4400 integralp = true;
4401 done = true;
4402 break;
4406 if (integralp)
4408 if (**pp == 'm')
4409 ++*pp;
4410 while (isdigit ((unsigned char) **pp))
4411 ++*pp;
4413 else if (charp)
4415 unsigned int val;
4417 if (**pp == 'm')
4418 ++*pp;
4419 val = stab_demangle_count (pp);
4420 if (val == 0)
4422 stab_bad_demangle (orig);
4423 return false;
4426 else if (boolp)
4428 unsigned int val;
4430 val = stab_demangle_count (pp);
4431 if (val != 0 && val != 1)
4433 stab_bad_demangle (orig);
4434 return false;
4437 else if (realp)
4439 if (**pp == 'm')
4440 ++*pp;
4441 while (isdigit ((unsigned char) **pp))
4442 ++*pp;
4443 if (**pp == '.')
4445 ++*pp;
4446 while (isdigit ((unsigned char) **pp))
4447 ++*pp;
4449 if (**pp == 'e')
4451 ++*pp;
4452 while (isdigit ((unsigned char) **pp))
4453 ++*pp;
4456 else if (pointerp)
4458 unsigned int len;
4460 if (! stab_demangle_get_count (pp, &len))
4462 stab_bad_demangle (orig);
4463 return false;
4465 *pp += len;
4470 /* We can translate this to a string fairly easily by invoking the
4471 regular demangling routine. */
4472 if (pname != NULL)
4474 char *s1, *s2, *s3, *s4;
4475 char *from, *to;
4477 s1 = savestring (orig, *pp - orig);
4479 s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
4481 free (s1);
4483 s3 = cplus_demangle (s2, DMGL_ANSI);
4485 free (s2);
4487 if (s3 != NULL)
4488 s4 = strstr (s3, "::NoSuchStrinG");
4489 if (s3 == NULL || s4 == NULL)
4491 stab_bad_demangle (orig);
4492 if (s3 != NULL)
4493 free (s3);
4494 return false;
4497 /* Eliminating all spaces, except those between > characters,
4498 makes it more likely that the demangled name will match the
4499 name which g++ used as the structure name. */
4500 for (from = to = s3; from != s4; ++from)
4501 if (*from != ' '
4502 || (from[1] == '>' && from > s3 && from[-1] == '>'))
4503 *to++ = *from;
4505 *pname = savestring (s3, to - s3);
4507 free (s3);
4510 return true;
4513 /* Demangle a class name. */
4515 static boolean
4516 stab_demangle_class (minfo, pp, pstart)
4517 struct stab_demangle_info *minfo ATTRIBUTE_UNUSED;
4518 const char **pp;
4519 const char **pstart;
4521 const char *orig;
4522 unsigned int n;
4524 orig = *pp;
4526 n = stab_demangle_count (pp);
4527 if (strlen (*pp) < n)
4529 stab_bad_demangle (orig);
4530 return false;
4533 if (pstart != NULL)
4534 *pstart = *pp;
4536 *pp += n;
4538 return true;
4541 /* Demangle function arguments. If the pargs argument is not NULL, it
4542 is set to a NULL terminated array holding the arguments. */
4544 static boolean
4545 stab_demangle_args (minfo, pp, pargs, pvarargs)
4546 struct stab_demangle_info *minfo;
4547 const char **pp;
4548 debug_type **pargs;
4549 boolean *pvarargs;
4551 const char *orig;
4552 unsigned int alloc, count;
4554 orig = *pp;
4556 alloc = 10;
4557 if (pargs != NULL)
4559 *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4560 *pvarargs = false;
4562 count = 0;
4564 while (**pp != '_' && **pp != '\0' && **pp != 'e')
4566 if (**pp == 'N' || **pp == 'T')
4568 char temptype;
4569 unsigned int r, t;
4571 temptype = **pp;
4572 ++*pp;
4574 if (temptype == 'T')
4575 r = 1;
4576 else
4578 if (! stab_demangle_get_count (pp, &r))
4580 stab_bad_demangle (orig);
4581 return false;
4585 if (! stab_demangle_get_count (pp, &t))
4587 stab_bad_demangle (orig);
4588 return false;
4591 if (t >= minfo->typestring_count)
4593 stab_bad_demangle (orig);
4594 return false;
4596 while (r-- > 0)
4598 const char *tem;
4600 tem = minfo->typestrings[t].typestring;
4601 if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4602 return false;
4605 else
4607 if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4608 return false;
4612 if (pargs != NULL)
4613 (*pargs)[count] = DEBUG_TYPE_NULL;
4615 if (**pp == 'e')
4617 if (pargs != NULL)
4618 *pvarargs = true;
4619 ++*pp;
4622 return true;
4625 /* Demangle a single argument. */
4627 static boolean
4628 stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4629 struct stab_demangle_info *minfo;
4630 const char **pp;
4631 debug_type **pargs;
4632 unsigned int *pcount;
4633 unsigned int *palloc;
4635 const char *start;
4636 debug_type type;
4638 start = *pp;
4639 if (! stab_demangle_type (minfo, pp,
4640 pargs == NULL ? (debug_type *) NULL : &type)
4641 || ! stab_demangle_remember_type (minfo, start, *pp - start))
4642 return false;
4644 if (pargs != NULL)
4646 if (type == DEBUG_TYPE_NULL)
4647 return false;
4649 if (*pcount + 1 >= *palloc)
4651 *palloc += 10;
4652 *pargs = ((debug_type *)
4653 xrealloc (*pargs, *palloc * sizeof **pargs));
4655 (*pargs)[*pcount] = type;
4656 ++*pcount;
4659 return true;
4662 /* Demangle a type. If the ptype argument is not NULL, *ptype is set
4663 to the newly allocated type. */
4665 static boolean
4666 stab_demangle_type (minfo, pp, ptype)
4667 struct stab_demangle_info *minfo;
4668 const char **pp;
4669 debug_type *ptype;
4671 const char *orig;
4673 orig = *pp;
4675 switch (**pp)
4677 case 'P':
4678 case 'p':
4679 /* A pointer type. */
4680 ++*pp;
4681 if (! stab_demangle_type (minfo, pp, ptype))
4682 return false;
4683 if (ptype != NULL)
4684 *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4685 break;
4687 case 'R':
4688 /* A reference type. */
4689 ++*pp;
4690 if (! stab_demangle_type (minfo, pp, ptype))
4691 return false;
4692 if (ptype != NULL)
4693 *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4694 break;
4696 case 'A':
4697 /* An array. */
4699 unsigned long high;
4701 ++*pp;
4702 high = 0;
4703 while (**pp != '\0' && **pp != '_')
4705 if (! isdigit ((unsigned char) **pp))
4707 stab_bad_demangle (orig);
4708 return false;
4710 high *= 10;
4711 high += **pp - '0';
4712 ++*pp;
4714 if (**pp != '_')
4716 stab_bad_demangle (orig);
4717 return false;
4719 ++*pp;
4721 if (! stab_demangle_type (minfo, pp, ptype))
4722 return false;
4723 if (ptype != NULL)
4725 debug_type int_type;
4727 int_type = debug_find_named_type (minfo->dhandle, "int");
4728 if (int_type == NULL)
4729 int_type = debug_make_int_type (minfo->dhandle, 4, false);
4730 *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4731 0, high, false);
4734 break;
4736 case 'T':
4737 /* A back reference to a remembered type. */
4739 unsigned int i;
4740 const char *p;
4742 ++*pp;
4743 if (! stab_demangle_get_count (pp, &i))
4745 stab_bad_demangle (orig);
4746 return false;
4748 if (i >= minfo->typestring_count)
4750 stab_bad_demangle (orig);
4751 return false;
4753 p = minfo->typestrings[i].typestring;
4754 if (! stab_demangle_type (minfo, &p, ptype))
4755 return false;
4757 break;
4759 case 'F':
4760 /* A function. */
4762 debug_type *args;
4763 boolean varargs;
4765 ++*pp;
4766 if (! stab_demangle_args (minfo, pp,
4767 (ptype == NULL
4768 ? (debug_type **) NULL
4769 : &args),
4770 (ptype == NULL
4771 ? (boolean *) NULL
4772 : &varargs)))
4773 return false;
4774 if (**pp != '_')
4776 /* cplus_demangle will accept a function without a return
4777 type, but I don't know when that will happen, or what
4778 to do if it does. */
4779 stab_bad_demangle (orig);
4780 return false;
4782 ++*pp;
4783 if (! stab_demangle_type (minfo, pp, ptype))
4784 return false;
4785 if (ptype != NULL)
4786 *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4787 varargs);
4790 break;
4792 case 'M':
4793 case 'O':
4795 boolean memberp, constp, volatilep;
4796 debug_type class_type = DEBUG_TYPE_NULL;
4797 debug_type *args;
4798 boolean varargs;
4799 unsigned int n;
4800 const char *name;
4802 memberp = **pp == 'M';
4803 constp = false;
4804 volatilep = false;
4805 args = NULL;
4806 varargs = false;
4808 ++*pp;
4809 if (isdigit ((unsigned char) **pp))
4811 n = stab_demangle_count (pp);
4812 if (strlen (*pp) < n)
4814 stab_bad_demangle (orig);
4815 return false;
4817 name = *pp;
4818 *pp += n;
4820 if (ptype != NULL)
4822 class_type = stab_find_tagged_type (minfo->dhandle,
4823 minfo->info,
4824 name, (int) n,
4825 DEBUG_KIND_CLASS);
4826 if (class_type == DEBUG_TYPE_NULL)
4827 return false;
4830 else if (**pp == 'Q')
4832 if (! stab_demangle_qualified (minfo, pp,
4833 (ptype == NULL
4834 ? (debug_type *) NULL
4835 : &class_type)))
4836 return false;
4838 else
4840 stab_bad_demangle (orig);
4841 return false;
4844 if (memberp)
4846 if (**pp == 'C')
4848 constp = true;
4849 ++*pp;
4851 else if (**pp == 'V')
4853 volatilep = true;
4854 ++*pp;
4856 if (**pp != 'F')
4858 stab_bad_demangle (orig);
4859 return false;
4861 ++*pp;
4862 if (! stab_demangle_args (minfo, pp,
4863 (ptype == NULL
4864 ? (debug_type **) NULL
4865 : &args),
4866 (ptype == NULL
4867 ? (boolean *) NULL
4868 : &varargs)))
4869 return false;
4872 if (**pp != '_')
4874 stab_bad_demangle (orig);
4875 return false;
4877 ++*pp;
4879 if (! stab_demangle_type (minfo, pp, ptype))
4880 return false;
4882 if (ptype != NULL)
4884 if (! memberp)
4885 *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4886 *ptype);
4887 else
4889 /* FIXME: We have no way to record constp or
4890 volatilep. */
4891 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4892 class_type, args, varargs);
4896 break;
4898 case 'G':
4899 ++*pp;
4900 if (! stab_demangle_type (minfo, pp, ptype))
4901 return false;
4902 break;
4904 case 'C':
4905 ++*pp;
4906 if (! stab_demangle_type (minfo, pp, ptype))
4907 return false;
4908 if (ptype != NULL)
4909 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4910 break;
4912 case 'Q':
4914 const char *hold;
4916 hold = *pp;
4917 if (! stab_demangle_qualified (minfo, pp, ptype))
4918 return false;
4920 break;
4922 default:
4923 if (! stab_demangle_fund_type (minfo, pp, ptype))
4924 return false;
4925 break;
4928 return true;
4931 /* Demangle a fundamental type. If the ptype argument is not NULL,
4932 *ptype is set to the newly allocated type. */
4934 static boolean
4935 stab_demangle_fund_type (minfo, pp, ptype)
4936 struct stab_demangle_info *minfo;
4937 const char **pp;
4938 debug_type *ptype;
4940 const char *orig;
4941 boolean constp, volatilep, unsignedp, signedp;
4942 boolean done;
4944 orig = *pp;
4946 constp = false;
4947 volatilep = false;
4948 unsignedp = false;
4949 signedp = false;
4951 done = false;
4952 while (! done)
4954 switch (**pp)
4956 case 'C':
4957 constp = true;
4958 ++*pp;
4959 break;
4961 case 'U':
4962 unsignedp = true;
4963 ++*pp;
4964 break;
4966 case 'S':
4967 signedp = true;
4968 ++*pp;
4969 break;
4971 case 'V':
4972 volatilep = true;
4973 ++*pp;
4974 break;
4976 default:
4977 done = true;
4978 break;
4982 switch (**pp)
4984 case '\0':
4985 case '_':
4986 /* cplus_demangle permits this, but I don't know what it means. */
4987 stab_bad_demangle (orig);
4988 break;
4990 case 'v': /* void */
4991 if (ptype != NULL)
4993 *ptype = debug_find_named_type (minfo->dhandle, "void");
4994 if (*ptype == DEBUG_TYPE_NULL)
4995 *ptype = debug_make_void_type (minfo->dhandle);
4997 ++*pp;
4998 break;
5000 case 'x': /* long long */
5001 if (ptype != NULL)
5003 *ptype = debug_find_named_type (minfo->dhandle,
5004 (unsignedp
5005 ? "long long unsigned int"
5006 : "long long int"));
5007 if (*ptype == DEBUG_TYPE_NULL)
5008 *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
5010 ++*pp;
5011 break;
5013 case 'l': /* long */
5014 if (ptype != NULL)
5016 *ptype = debug_find_named_type (minfo->dhandle,
5017 (unsignedp
5018 ? "long unsigned int"
5019 : "long int"));
5020 if (*ptype == DEBUG_TYPE_NULL)
5021 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5023 ++*pp;
5024 break;
5026 case 'i': /* int */
5027 if (ptype != NULL)
5029 *ptype = debug_find_named_type (minfo->dhandle,
5030 (unsignedp
5031 ? "unsigned int"
5032 : "int"));
5033 if (*ptype == DEBUG_TYPE_NULL)
5034 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5036 ++*pp;
5037 break;
5039 case 's': /* short */
5040 if (ptype != NULL)
5042 *ptype = debug_find_named_type (minfo->dhandle,
5043 (unsignedp
5044 ? "short unsigned int"
5045 : "short int"));
5046 if (*ptype == DEBUG_TYPE_NULL)
5047 *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
5049 ++*pp;
5050 break;
5052 case 'b': /* bool */
5053 if (ptype != NULL)
5055 *ptype = debug_find_named_type (minfo->dhandle, "bool");
5056 if (*ptype == DEBUG_TYPE_NULL)
5057 *ptype = debug_make_bool_type (minfo->dhandle, 4);
5059 ++*pp;
5060 break;
5062 case 'c': /* char */
5063 if (ptype != NULL)
5065 *ptype = debug_find_named_type (minfo->dhandle,
5066 (unsignedp
5067 ? "unsigned char"
5068 : (signedp
5069 ? "signed char"
5070 : "char")));
5071 if (*ptype == DEBUG_TYPE_NULL)
5072 *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
5074 ++*pp;
5075 break;
5077 case 'w': /* wchar_t */
5078 if (ptype != NULL)
5080 *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
5081 if (*ptype == DEBUG_TYPE_NULL)
5082 *ptype = debug_make_int_type (minfo->dhandle, 2, true);
5084 ++*pp;
5085 break;
5087 case 'r': /* long double */
5088 if (ptype != NULL)
5090 *ptype = debug_find_named_type (minfo->dhandle, "long double");
5091 if (*ptype == DEBUG_TYPE_NULL)
5092 *ptype = debug_make_float_type (minfo->dhandle, 8);
5094 ++*pp;
5095 break;
5097 case 'd': /* double */
5098 if (ptype != NULL)
5100 *ptype = debug_find_named_type (minfo->dhandle, "double");
5101 if (*ptype == DEBUG_TYPE_NULL)
5102 *ptype = debug_make_float_type (minfo->dhandle, 8);
5104 ++*pp;
5105 break;
5107 case 'f': /* float */
5108 if (ptype != NULL)
5110 *ptype = debug_find_named_type (minfo->dhandle, "float");
5111 if (*ptype == DEBUG_TYPE_NULL)
5112 *ptype = debug_make_float_type (minfo->dhandle, 4);
5114 ++*pp;
5115 break;
5117 case 'G':
5118 ++*pp;
5119 if (! isdigit ((unsigned char) **pp))
5121 stab_bad_demangle (orig);
5122 return false;
5124 /* Fall through. */
5125 case '0': case '1': case '2': case '3': case '4':
5126 case '5': case '6': case '7': case '8': case '9':
5128 const char *hold;
5130 if (! stab_demangle_class (minfo, pp, &hold))
5131 return false;
5132 if (ptype != NULL)
5134 char *name;
5136 name = savestring (hold, *pp - hold);
5137 *ptype = debug_find_named_type (minfo->dhandle, name);
5138 free (name);
5139 if (*ptype == DEBUG_TYPE_NULL)
5141 /* FIXME: It is probably incorrect to assume that
5142 undefined types are tagged types. */
5143 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5144 hold, *pp - hold,
5145 DEBUG_KIND_ILLEGAL);
5146 if (*ptype == DEBUG_TYPE_NULL)
5147 return false;
5151 break;
5153 case 't':
5155 char *name;
5157 if (! stab_demangle_template (minfo, pp,
5158 ptype != NULL ? &name : NULL))
5159 return false;
5160 if (ptype != NULL)
5162 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5163 name, strlen (name),
5164 DEBUG_KIND_CLASS);
5165 free (name);
5166 if (*ptype == DEBUG_TYPE_NULL)
5167 return false;
5170 break;
5172 default:
5173 stab_bad_demangle (orig);
5174 return false;
5177 if (ptype != NULL)
5179 if (constp)
5180 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
5181 if (volatilep)
5182 *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5185 return true;
5188 /* Remember a type string in a demangled string. */
5190 static boolean
5191 stab_demangle_remember_type (minfo, p, len)
5192 struct stab_demangle_info *minfo;
5193 const char *p;
5194 int len;
5196 if (minfo->typestring_count >= minfo->typestring_alloc)
5198 minfo->typestring_alloc += 10;
5199 minfo->typestrings = ((struct stab_demangle_typestring *)
5200 xrealloc (minfo->typestrings,
5201 (minfo->typestring_alloc
5202 * sizeof *minfo->typestrings)));
5205 minfo->typestrings[minfo->typestring_count].typestring = p;
5206 minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5207 ++minfo->typestring_count;
5209 return true;