* MAINTAINERS: Change e-mail address back to airs.com.
[binutils.git] / binutils / stabs.c
blob1ff4f6f60d220903a26be3495a4bcf53b62459bf
1 /* stabs.c -- Parse stabs debugging information
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This file contains code which parses stabs debugging information.
24 The organization of this code is based on the gdb stabs reading
25 code. The job it does is somewhat different, because it is not
26 trying to identify the correct address for anything. */
28 #include <stdio.h>
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "libiberty.h"
33 #include "safe-ctype.h"
34 #include "demangle.h"
35 #include "debug.h"
36 #include "budbg.h"
37 #include "filenames.h"
39 /* Meaningless definition needs by aout64.h. FIXME. */
40 #define BYTES_IN_WORD 4
42 #include "aout/aout64.h"
43 #include "aout/stab_gnu.h"
45 /* The number of predefined XCOFF types. */
47 #define XCOFF_TYPE_COUNT 34
49 /* This structure is used as a handle so that the stab parsing doesn't
50 need to use any static variables. */
52 struct stab_handle
54 /* The BFD. */
55 bfd *abfd;
56 /* TRUE if this is stabs in sections. */
57 bfd_boolean sections;
58 /* The symbol table. */
59 asymbol **syms;
60 /* The number of symbols. */
61 long symcount;
62 /* The accumulated file name string. */
63 char *so_string;
64 /* The value of the last N_SO symbol. */
65 bfd_vma so_value;
66 /* The value of the start of the file, so that we can handle file
67 relative N_LBRAC and N_RBRAC symbols. */
68 bfd_vma file_start_offset;
69 /* The offset of the start of the function, so that we can handle
70 function relative N_LBRAC and N_RBRAC symbols. */
71 bfd_vma function_start_offset;
72 /* The version number of gcc which compiled the current compilation
73 unit, 0 if not compiled by gcc. */
74 int gcc_compiled;
75 /* Whether an N_OPT symbol was seen that was not generated by gcc,
76 so that we can detect the SunPRO compiler. */
77 bfd_boolean n_opt_found;
78 /* The main file name. */
79 char *main_filename;
80 /* A stack of unfinished N_BINCL files. */
81 struct bincl_file *bincl_stack;
82 /* A list of finished N_BINCL files. */
83 struct bincl_file *bincl_list;
84 /* Whether we are inside a function or not. */
85 bfd_boolean within_function;
86 /* The address of the end of the function, used if we have seen an
87 N_FUN symbol while in a function. This is -1 if we have not seen
88 an N_FUN (the normal case). */
89 bfd_vma function_end;
90 /* The depth of block nesting. */
91 int block_depth;
92 /* List of pending variable definitions. */
93 struct stab_pending_var *pending;
94 /* Number of files for which we have types. */
95 unsigned int files;
96 /* Lists of types per file. */
97 struct stab_types **file_types;
98 /* Predefined XCOFF types. */
99 debug_type xcoff_types[XCOFF_TYPE_COUNT];
100 /* Undefined tags. */
101 struct stab_tag *tags;
102 /* Set by parse_stab_type if it sees a structure defined as a cross
103 reference to itself. Reset by parse_stab_type otherwise. */
104 bfd_boolean self_crossref;
107 /* A list of these structures is used to hold pending variable
108 definitions seen before the N_LBRAC of a block. */
110 struct stab_pending_var
112 /* Next pending variable definition. */
113 struct stab_pending_var *next;
114 /* Name. */
115 const char *name;
116 /* Type. */
117 debug_type type;
118 /* Kind. */
119 enum debug_var_kind kind;
120 /* Value. */
121 bfd_vma val;
124 /* A list of these structures is used to hold the types for a single
125 file. */
127 struct stab_types
129 /* Next set of slots for this file. */
130 struct stab_types *next;
131 /* Types indexed by type number. */
132 #define STAB_TYPES_SLOTS (16)
133 debug_type types[STAB_TYPES_SLOTS];
136 /* We keep a list of undefined tags that we encounter, so that we can
137 fill them in if the tag is later defined. */
139 struct stab_tag
141 /* Next undefined tag. */
142 struct stab_tag *next;
143 /* Tag name. */
144 const char *name;
145 /* Type kind. */
146 enum debug_type_kind kind;
147 /* Slot to hold real type when we discover it. If we don't, we fill
148 in an undefined tag type. */
149 debug_type slot;
150 /* Indirect type we have created to point at slot. */
151 debug_type type;
154 static char *savestring
155 PARAMS ((const char *, int));
156 static bfd_vma parse_number
157 PARAMS ((const char **, bfd_boolean *));
158 static void bad_stab
159 PARAMS ((const char *));
160 static void warn_stab
161 PARAMS ((const char *, const char *));
162 static bfd_boolean parse_stab_string
163 PARAMS ((PTR, struct stab_handle *, int, int, bfd_vma, const char *));
164 static debug_type parse_stab_type
165 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
166 debug_type **));
167 static bfd_boolean parse_stab_type_number
168 PARAMS ((const char **, int *));
169 static debug_type parse_stab_range_type
170 PARAMS ((PTR, struct stab_handle *, const char *, const char **,
171 const int *));
172 static debug_type parse_stab_sun_builtin_type
173 PARAMS ((PTR, const char **));
174 static debug_type parse_stab_sun_floating_type
175 PARAMS ((PTR, const char **));
176 static debug_type parse_stab_enum_type
177 PARAMS ((PTR, const char **));
178 static debug_type parse_stab_struct_type
179 PARAMS ((PTR, struct stab_handle *, const char *, const char **, bfd_boolean,
180 const int *));
181 static bfd_boolean parse_stab_baseclasses
182 PARAMS ((PTR, struct stab_handle *, const char **, debug_baseclass **));
183 static bfd_boolean parse_stab_struct_fields
184 PARAMS ((PTR, struct stab_handle *, const char **, debug_field **,
185 bfd_boolean *));
186 static bfd_boolean parse_stab_cpp_abbrev
187 PARAMS ((PTR, struct stab_handle *, const char **, debug_field *));
188 static bfd_boolean parse_stab_one_struct_field
189 PARAMS ((PTR, struct stab_handle *, const char **, const char *,
190 debug_field *, bfd_boolean *));
191 static bfd_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 *, bfd_boolean, bfd_boolean, const char **));
197 static bfd_boolean parse_stab_tilde_field
198 PARAMS ((PTR, struct stab_handle *, const char **, const int *,
199 debug_type *, bfd_boolean *));
200 static debug_type parse_stab_array_type
201 PARAMS ((PTR, struct stab_handle *, const char **, bfd_boolean));
202 static void push_bincl
203 PARAMS ((struct stab_handle *, const char *, bfd_vma));
204 static const char *pop_bincl
205 PARAMS ((struct stab_handle *));
206 static bfd_boolean find_excl
207 PARAMS ((struct stab_handle *, const char *, bfd_vma));
208 static bfd_boolean stab_record_variable
209 PARAMS ((PTR, struct stab_handle *, const char *, debug_type,
210 enum debug_var_kind, bfd_vma));
211 static bfd_boolean stab_emit_pending_vars
212 PARAMS ((PTR, struct stab_handle *));
213 static debug_type *stab_find_slot
214 PARAMS ((struct stab_handle *, const int *));
215 static debug_type stab_find_type
216 PARAMS ((PTR, struct stab_handle *, const int *));
217 static bfd_boolean stab_record_type
218 PARAMS ((PTR, struct stab_handle *, const int *, debug_type));
219 static debug_type stab_xcoff_builtin_type
220 PARAMS ((PTR, struct stab_handle *, int));
221 static debug_type stab_find_tagged_type
222 PARAMS ((PTR, struct stab_handle *, const char *, int,
223 enum debug_type_kind));
224 static debug_type *stab_demangle_argtypes
225 PARAMS ((PTR, struct stab_handle *, const char *, bfd_boolean *, unsigned int));
227 /* Save a string in memory. */
229 static char *
230 savestring (start, len)
231 const char *start;
232 int len;
234 char *ret;
236 ret = (char *) xmalloc (len + 1);
237 memcpy (ret, start, len);
238 ret[len] = '\0';
239 return ret;
242 /* Read a number from a string. */
244 static bfd_vma
245 parse_number (pp, poverflow)
246 const char **pp;
247 bfd_boolean *poverflow;
249 unsigned long ul;
250 const char *orig;
252 if (poverflow != NULL)
253 *poverflow = FALSE;
255 orig = *pp;
257 errno = 0;
258 ul = strtoul (*pp, (char **) pp, 0);
259 if (ul + 1 != 0 || errno == 0)
261 /* If bfd_vma is larger than unsigned long, and the number is
262 meant to be negative, we have to make sure that we sign
263 extend properly. */
264 if (*orig == '-')
265 return (bfd_vma) (bfd_signed_vma) (long) ul;
266 return (bfd_vma) ul;
269 /* Note that even though strtoul overflowed, it should have set *pp
270 to the end of the number, which is where we want it. */
272 if (sizeof (bfd_vma) > sizeof (unsigned long))
274 const char *p;
275 bfd_boolean neg;
276 int base;
277 bfd_vma over, lastdig;
278 bfd_boolean overflow;
279 bfd_vma v;
281 /* Our own version of strtoul, for a bfd_vma. */
283 p = orig;
285 neg = FALSE;
286 if (*p == '+')
287 ++p;
288 else if (*p == '-')
290 neg = TRUE;
291 ++p;
294 base = 10;
295 if (*p == '0')
297 if (p[1] == 'x' || p[1] == 'X')
299 base = 16;
300 p += 2;
302 else
304 base = 8;
305 ++p;
309 over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
310 lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
312 overflow = FALSE;
313 v = 0;
314 while (1)
316 int d;
318 d = *p++;
319 if (ISDIGIT (d))
320 d -= '0';
321 else if (ISUPPER (d))
322 d -= 'A';
323 else if (ISLOWER (d))
324 d -= 'a';
325 else
326 break;
328 if (d >= base)
329 break;
331 if (v > over || (v == over && (bfd_vma) d > lastdig))
333 overflow = TRUE;
334 break;
338 if (! overflow)
340 if (neg)
341 v = - v;
342 return v;
346 /* If we get here, the number is too large to represent in a
347 bfd_vma. */
349 if (poverflow != NULL)
350 *poverflow = TRUE;
351 else
352 warn_stab (orig, _("numeric overflow"));
354 return 0;
357 /* Give an error for a bad stab string. */
359 static void
360 bad_stab (p)
361 const char *p;
363 fprintf (stderr, _("Bad stab: %s\n"), p);
366 /* Warn about something in a stab string. */
368 static void
369 warn_stab (p, err)
370 const char *p;
371 const char *err;
373 fprintf (stderr, _("Warning: %s: %s\n"), err, p);
376 /* Create a handle to parse stabs symbols with. */
379 start_stab (dhandle, abfd, sections, syms, symcount)
380 PTR dhandle ATTRIBUTE_UNUSED;
381 bfd *abfd;
382 bfd_boolean sections;
383 asymbol **syms;
384 long symcount;
386 struct stab_handle *ret;
388 ret = (struct stab_handle *) xmalloc (sizeof *ret);
389 memset (ret, 0, sizeof *ret);
390 ret->abfd = abfd;
391 ret->sections = sections;
392 ret->syms = syms;
393 ret->symcount = symcount;
394 ret->files = 1;
395 ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
396 ret->file_types[0] = NULL;
397 ret->function_end = (bfd_vma) -1;
398 return (PTR) ret;
401 /* When we have processed all the stabs information, we need to go
402 through and fill in all the undefined tags. */
404 bfd_boolean
405 finish_stab (dhandle, handle)
406 PTR dhandle;
407 PTR handle;
409 struct stab_handle *info = (struct stab_handle *) handle;
410 struct stab_tag *st;
412 if (info->within_function)
414 if (! stab_emit_pending_vars (dhandle, info)
415 || ! debug_end_function (dhandle, info->function_end))
416 return FALSE;
417 info->within_function = FALSE;
418 info->function_end = (bfd_vma) -1;
421 for (st = info->tags; st != NULL; st = st->next)
423 enum debug_type_kind kind;
425 kind = st->kind;
426 if (kind == DEBUG_KIND_ILLEGAL)
427 kind = DEBUG_KIND_STRUCT;
428 st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
429 if (st->slot == DEBUG_TYPE_NULL)
430 return FALSE;
433 return TRUE;
436 /* Handle a single stabs symbol. */
438 bfd_boolean
439 parse_stab (dhandle, handle, type, desc, value, string)
440 PTR dhandle;
441 PTR handle;
442 int type;
443 int desc;
444 bfd_vma value;
445 const char *string;
447 struct stab_handle *info = (struct stab_handle *) handle;
449 /* gcc will emit two N_SO strings per compilation unit, one for the
450 directory name and one for the file name. We just collect N_SO
451 strings as we see them, and start the new compilation unit when
452 we see a non N_SO symbol. */
453 if (info->so_string != NULL
454 && (type != N_SO || *string == '\0' || value != info->so_value))
456 if (! debug_set_filename (dhandle, info->so_string))
457 return FALSE;
458 info->main_filename = info->so_string;
460 info->gcc_compiled = 0;
461 info->n_opt_found = FALSE;
463 /* Generally, for stabs in the symbol table, the N_LBRAC and
464 N_RBRAC symbols are relative to the N_SO symbol value. */
465 if (! info->sections)
466 info->file_start_offset = info->so_value;
468 /* We need to reset the mapping from type numbers to types. We
469 can't free the old mapping, because of the use of
470 debug_make_indirect_type. */
471 info->files = 1;
472 info->file_types = ((struct stab_types **)
473 xmalloc (sizeof *info->file_types));
474 info->file_types[0] = NULL;
476 info->so_string = NULL;
478 /* Now process whatever type we just got. */
481 switch (type)
483 case N_FN:
484 case N_FN_SEQ:
485 break;
487 case N_LBRAC:
488 /* Ignore extra outermost context from SunPRO cc and acc. */
489 if (info->n_opt_found && desc == 1)
490 break;
492 if (! info->within_function)
494 fprintf (stderr, _("N_LBRAC not within function\n"));
495 return FALSE;
498 /* Start an inner lexical block. */
499 if (! debug_start_block (dhandle,
500 (value
501 + info->file_start_offset
502 + info->function_start_offset)))
503 return FALSE;
505 /* Emit any pending variable definitions. */
506 if (! stab_emit_pending_vars (dhandle, info))
507 return FALSE;
509 ++info->block_depth;
510 break;
512 case N_RBRAC:
513 /* Ignore extra outermost context from SunPRO cc and acc. */
514 if (info->n_opt_found && desc == 1)
515 break;
517 /* We shouldn't have any pending variable definitions here, but,
518 if we do, we probably need to emit them before closing the
519 block. */
520 if (! stab_emit_pending_vars (dhandle, info))
521 return FALSE;
523 /* End an inner lexical block. */
524 if (! debug_end_block (dhandle,
525 (value
526 + info->file_start_offset
527 + info->function_start_offset)))
528 return FALSE;
530 --info->block_depth;
531 if (info->block_depth < 0)
533 fprintf (stderr, _("Too many N_RBRACs\n"));
534 return FALSE;
536 break;
538 case N_SO:
539 /* This always ends a function. */
540 if (info->within_function)
542 bfd_vma endval;
544 endval = value;
545 if (*string != '\0'
546 && info->function_end != (bfd_vma) -1
547 && info->function_end < endval)
548 endval = info->function_end;
549 if (! stab_emit_pending_vars (dhandle, info)
550 || ! debug_end_function (dhandle, endval))
551 return FALSE;
552 info->within_function = FALSE;
553 info->function_end = (bfd_vma) -1;
556 /* An empty string is emitted by gcc at the end of a compilation
557 unit. */
558 if (*string == '\0')
559 return TRUE;
561 /* Just accumulate strings until we see a non N_SO symbol. If
562 the string starts with a directory separator or some other
563 form of absolute path specification, we discard the previously
564 accumulated strings. */
565 if (info->so_string == NULL)
566 info->so_string = xstrdup (string);
567 else
569 char *f;
571 f = info->so_string;
573 if (IS_ABSOLUTE_PATH (string))
574 info->so_string = xstrdup (string);
575 else
576 info->so_string = concat (info->so_string, string,
577 (const char *) NULL);
578 free (f);
581 info->so_value = value;
583 break;
585 case N_SOL:
586 /* Start an include file. */
587 if (! debug_start_source (dhandle, string))
588 return FALSE;
589 break;
591 case N_BINCL:
592 /* Start an include file which may be replaced. */
593 push_bincl (info, string, value);
594 if (! debug_start_source (dhandle, string))
595 return FALSE;
596 break;
598 case N_EINCL:
599 /* End an N_BINCL include. */
600 if (! debug_start_source (dhandle, pop_bincl (info)))
601 return FALSE;
602 break;
604 case N_EXCL:
605 /* This is a duplicate of a header file named by N_BINCL which
606 was eliminated by the linker. */
607 if (! find_excl (info, string, value))
608 return FALSE;
609 break;
611 case N_SLINE:
612 if (! debug_record_line (dhandle, desc,
613 value + info->function_start_offset))
614 return FALSE;
615 break;
617 case N_BCOMM:
618 if (! debug_start_common_block (dhandle, string))
619 return FALSE;
620 break;
622 case N_ECOMM:
623 if (! debug_end_common_block (dhandle, string))
624 return FALSE;
625 break;
627 case N_FUN:
628 if (*string == '\0')
630 if (info->within_function)
632 /* This always marks the end of a function; we don't
633 need to worry about info->function_end. */
634 if (info->sections)
635 value += info->function_start_offset;
636 if (! stab_emit_pending_vars (dhandle, info)
637 || ! debug_end_function (dhandle, value))
638 return FALSE;
639 info->within_function = FALSE;
640 info->function_end = (bfd_vma) -1;
642 break;
645 /* A const static symbol in the .text section will have an N_FUN
646 entry. We need to use these to mark the end of the function,
647 in case we are looking at gcc output before it was changed to
648 always emit an empty N_FUN. We can't call debug_end_function
649 here, because it might be a local static symbol. */
650 if (info->within_function
651 && (info->function_end == (bfd_vma) -1
652 || value < info->function_end))
653 info->function_end = value;
655 /* Fall through. */
656 /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
657 symbols, and if it does not start with :S, gdb relocates the
658 value to the start of the section. gcc always seems to use
659 :S, so we don't worry about this. */
660 /* Fall through. */
661 default:
663 const char *colon;
665 colon = strchr (string, ':');
666 if (colon != NULL
667 && (colon[1] == 'f' || colon[1] == 'F'))
669 if (info->within_function)
671 bfd_vma endval;
673 endval = value;
674 if (info->function_end != (bfd_vma) -1
675 && info->function_end < endval)
676 endval = info->function_end;
677 if (! stab_emit_pending_vars (dhandle, info)
678 || ! debug_end_function (dhandle, endval))
679 return FALSE;
680 info->function_end = (bfd_vma) -1;
682 /* For stabs in sections, line numbers and block addresses
683 are offsets from the start of the function. */
684 if (info->sections)
685 info->function_start_offset = value;
686 info->within_function = TRUE;
689 if (! parse_stab_string (dhandle, info, type, desc, value, string))
690 return FALSE;
692 break;
694 case N_OPT:
695 if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
696 info->gcc_compiled = 2;
697 else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
698 info->gcc_compiled = 1;
699 else
700 info->n_opt_found = TRUE;
701 break;
703 case N_OBJ:
704 case N_ENDM:
705 case N_MAIN:
706 case N_WARNING:
707 break;
710 return TRUE;
713 /* Parse the stabs string. */
715 static bfd_boolean
716 parse_stab_string (dhandle, info, stabtype, desc, value, string)
717 PTR dhandle;
718 struct stab_handle *info;
719 int stabtype;
720 int desc;
721 bfd_vma value;
722 const char *string;
724 const char *p;
725 char *name;
726 int type;
727 debug_type dtype;
728 bfd_boolean synonym;
729 bfd_boolean self_crossref;
730 unsigned int lineno;
731 debug_type *slot;
733 p = strchr (string, ':');
734 if (p == NULL)
735 return TRUE;
737 while (p[1] == ':')
739 p += 2;
740 p = strchr (p, ':');
741 if (p == NULL)
743 bad_stab (string);
744 return FALSE;
748 /* GCC 2.x puts the line number in desc. SunOS apparently puts in
749 the number of bytes occupied by a type or object, which we
750 ignore. */
751 if (info->gcc_compiled >= 2)
752 lineno = desc;
753 else
754 lineno = 0;
756 /* FIXME: Sometimes the special C++ names start with '.'. */
757 name = NULL;
758 if (string[0] == '$')
760 switch (string[1])
762 case 't':
763 name = "this";
764 break;
765 case 'v':
766 /* Was: name = "vptr"; */
767 break;
768 case 'e':
769 name = "eh_throw";
770 break;
771 case '_':
772 /* This was an anonymous type that was never fixed up. */
773 break;
774 case 'X':
775 /* SunPRO (3.0 at least) static variable encoding. */
776 break;
777 default:
778 warn_stab (string, _("unknown C++ encoded name"));
779 break;
783 if (name == NULL)
785 if (p == string || (string[0] == ' ' && p == string + 1))
786 name = NULL;
787 else
788 name = savestring (string, p - string);
791 ++p;
792 if (ISDIGIT (*p) || *p == '(' || *p == '-')
793 type = 'l';
794 else
795 type = *p++;
797 switch (type)
799 case 'c':
800 /* c is a special case, not followed by a type-number.
801 SYMBOL:c=iVALUE for an integer constant symbol.
802 SYMBOL:c=rVALUE for a floating constant symbol.
803 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
804 e.g. "b:c=e6,0" for "const b = blob1"
805 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
806 if (*p != '=')
808 bad_stab (string);
809 return FALSE;
811 ++p;
812 switch (*p++)
814 case 'r':
815 /* Floating point constant. */
816 if (! debug_record_float_const (dhandle, name, atof (p)))
817 return FALSE;
818 break;
819 case 'i':
820 /* Integer constant. */
821 /* Defining integer constants this way is kind of silly,
822 since 'e' constants allows the compiler to give not only
823 the value, but the type as well. C has at least int,
824 long, unsigned int, and long long as constant types;
825 other languages probably should have at least unsigned as
826 well as signed constants. */
827 if (! debug_record_int_const (dhandle, name, atoi (p)))
828 return FALSE;
829 break;
830 case 'e':
831 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
832 can be represented as integral.
833 e.g. "b:c=e6,0" for "const b = blob1"
834 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
835 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
836 &p, (debug_type **) NULL);
837 if (dtype == DEBUG_TYPE_NULL)
838 return FALSE;
839 if (*p != ',')
841 bad_stab (string);
842 return FALSE;
844 if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
845 return FALSE;
846 break;
847 default:
848 bad_stab (string);
849 return FALSE;
852 break;
854 case 'C':
855 /* The name of a caught exception. */
856 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
857 &p, (debug_type **) NULL);
858 if (dtype == DEBUG_TYPE_NULL)
859 return FALSE;
860 if (! debug_record_label (dhandle, name, dtype, value))
861 return FALSE;
862 break;
864 case 'f':
865 case 'F':
866 /* A function definition. */
867 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
868 (debug_type **) NULL);
869 if (dtype == DEBUG_TYPE_NULL)
870 return FALSE;
871 if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
872 return FALSE;
874 /* Sun acc puts declared types of arguments here. We don't care
875 about their actual types (FIXME -- we should remember the whole
876 function prototype), but the list may define some new types
877 that we have to remember, so we must scan it now. */
878 while (*p == ';')
880 ++p;
881 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
882 (debug_type **) NULL)
883 == DEBUG_TYPE_NULL)
884 return FALSE;
887 break;
889 case 'G':
891 char leading;
892 long c;
893 asymbol **ps;
895 /* A global symbol. The value must be extracted from the
896 symbol table. */
897 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
898 (debug_type **) NULL);
899 if (dtype == DEBUG_TYPE_NULL)
900 return FALSE;
901 leading = bfd_get_symbol_leading_char (info->abfd);
902 for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
904 const char *n;
906 n = bfd_asymbol_name (*ps);
907 if (leading != '\0' && *n == leading)
908 ++n;
909 if (*n == *name && strcmp (n, name) == 0)
910 break;
912 if (c > 0)
913 value = bfd_asymbol_value (*ps);
914 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
915 value))
916 return FALSE;
918 break;
920 /* This case is faked by a conditional above, when there is no
921 code letter in the dbx data. Dbx data never actually
922 contains 'l'. */
923 case 'l':
924 case 's':
925 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
926 (debug_type **) NULL);
927 if (dtype == DEBUG_TYPE_NULL)
928 return FALSE;
929 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
930 value))
931 return FALSE;
932 break;
934 case 'p':
935 /* A function parameter. */
936 if (*p != 'F')
937 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
938 (debug_type **) NULL);
939 else
941 /* pF is a two-letter code that means a function parameter in
942 Fortran. The type-number specifies the type of the return
943 value. Translate it into a pointer-to-function type. */
944 ++p;
945 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
946 (debug_type **) NULL);
947 if (dtype != DEBUG_TYPE_NULL)
949 debug_type ftype;
951 ftype = debug_make_function_type (dhandle, dtype,
952 (debug_type *) NULL, FALSE);
953 dtype = debug_make_pointer_type (dhandle, ftype);
956 if (dtype == DEBUG_TYPE_NULL)
957 return FALSE;
958 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
959 value))
960 return FALSE;
962 /* FIXME: At this point gdb considers rearranging the parameter
963 address on a big endian machine if it is smaller than an int.
964 We have no way to do that, since we don't really know much
965 about the target. */
967 break;
969 case 'P':
970 if (stabtype == N_FUN)
972 /* Prototype of a function referenced by this file. */
973 while (*p == ';')
975 ++p;
976 if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
977 (debug_type **) NULL)
978 == DEBUG_TYPE_NULL)
979 return FALSE;
981 break;
983 /* Fall through. */
984 case 'R':
985 /* Parameter which is in a register. */
986 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
987 (debug_type **) NULL);
988 if (dtype == DEBUG_TYPE_NULL)
989 return FALSE;
990 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
991 value))
992 return FALSE;
993 break;
995 case 'r':
996 /* Register variable (either global or local). */
997 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
998 (debug_type **) NULL);
999 if (dtype == DEBUG_TYPE_NULL)
1000 return FALSE;
1001 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
1002 value))
1003 return FALSE;
1005 /* FIXME: At this point gdb checks to combine pairs of 'p' and
1006 'r' stabs into a single 'P' stab. */
1008 break;
1010 case 'S':
1011 /* Static symbol at top level of file */
1012 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1013 (debug_type **) NULL);
1014 if (dtype == DEBUG_TYPE_NULL)
1015 return FALSE;
1016 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
1017 value))
1018 return FALSE;
1019 break;
1021 case 't':
1022 /* A typedef. */
1023 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1024 if (dtype == DEBUG_TYPE_NULL)
1025 return FALSE;
1026 if (name == NULL)
1028 /* A nameless type. Nothing to do. */
1029 return TRUE;
1032 dtype = debug_name_type (dhandle, name, dtype);
1033 if (dtype == DEBUG_TYPE_NULL)
1034 return FALSE;
1036 if (slot != NULL)
1037 *slot = dtype;
1039 break;
1041 case 'T':
1042 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1043 by 't' which means we are typedef'ing it as well. */
1044 if (*p != 't')
1046 synonym = FALSE;
1047 /* FIXME: gdb sets synonym to TRUE if the current language
1048 is C++. */
1050 else
1052 synonym = TRUE;
1053 ++p;
1056 dtype = parse_stab_type (dhandle, info, name, &p, &slot);
1057 if (dtype == DEBUG_TYPE_NULL)
1058 return FALSE;
1059 if (name == NULL)
1060 return TRUE;
1062 /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
1063 a cross reference to itself. These are generated by some
1064 versions of g++. */
1065 self_crossref = info->self_crossref;
1067 dtype = debug_tag_type (dhandle, name, dtype);
1068 if (dtype == DEBUG_TYPE_NULL)
1069 return FALSE;
1070 if (slot != NULL)
1071 *slot = dtype;
1073 /* See if we have a cross reference to this tag which we can now
1074 fill in. Avoid filling in a cross reference to ourselves,
1075 because that would lead to circular debugging information. */
1076 if (! self_crossref)
1078 register struct stab_tag **pst;
1080 for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
1082 if ((*pst)->name[0] == name[0]
1083 && strcmp ((*pst)->name, name) == 0)
1085 (*pst)->slot = dtype;
1086 *pst = (*pst)->next;
1087 break;
1092 if (synonym)
1094 dtype = debug_name_type (dhandle, name, dtype);
1095 if (dtype == DEBUG_TYPE_NULL)
1096 return FALSE;
1098 if (slot != NULL)
1099 *slot = dtype;
1102 break;
1104 case 'V':
1105 /* Static symbol of local scope */
1106 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1107 (debug_type **) NULL);
1108 if (dtype == DEBUG_TYPE_NULL)
1109 return FALSE;
1110 /* FIXME: gdb checks os9k_stabs here. */
1111 if (! stab_record_variable (dhandle, info, name, dtype,
1112 DEBUG_LOCAL_STATIC, value))
1113 return FALSE;
1114 break;
1116 case 'v':
1117 /* Reference parameter. */
1118 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1119 (debug_type **) NULL);
1120 if (dtype == DEBUG_TYPE_NULL)
1121 return FALSE;
1122 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
1123 value))
1124 return FALSE;
1125 break;
1127 case 'a':
1128 /* Reference parameter which is in a register. */
1129 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1130 (debug_type **) NULL);
1131 if (dtype == DEBUG_TYPE_NULL)
1132 return FALSE;
1133 if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1134 value))
1135 return FALSE;
1136 break;
1138 case 'X':
1139 /* This is used by Sun FORTRAN for "function result value".
1140 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1141 that Pascal uses it too, but when I tried it Pascal used
1142 "x:3" (local symbol) instead. */
1143 dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1144 (debug_type **) NULL);
1145 if (dtype == DEBUG_TYPE_NULL)
1146 return FALSE;
1147 if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1148 value))
1149 return FALSE;
1150 break;
1152 default:
1153 bad_stab (string);
1154 return FALSE;
1157 /* FIXME: gdb converts structure values to structure pointers in a
1158 couple of cases, depending upon the target. */
1160 return TRUE;
1163 /* Parse a stabs type. The typename argument is non-NULL if this is a
1164 typedef or a tag definition. The pp argument points to the stab
1165 string, and is updated. The slotp argument points to a place to
1166 store the slot used if the type is being defined. */
1168 static debug_type
1169 parse_stab_type (dhandle, info, typename, pp, slotp)
1170 PTR dhandle;
1171 struct stab_handle *info;
1172 const char *typename;
1173 const char **pp;
1174 debug_type **slotp;
1176 const char *orig;
1177 int typenums[2];
1178 int size;
1179 bfd_boolean stringp;
1180 int descriptor;
1181 debug_type dtype;
1183 if (slotp != NULL)
1184 *slotp = NULL;
1186 orig = *pp;
1188 size = -1;
1189 stringp = FALSE;
1191 info->self_crossref = FALSE;
1193 /* Read type number if present. The type number may be omitted.
1194 for instance in a two-dimensional array declared with type
1195 "ar1;1;10;ar1;1;10;4". */
1196 if (! ISDIGIT (**pp) && **pp != '(' && **pp != '-')
1198 /* 'typenums=' not present, type is anonymous. Read and return
1199 the definition, but don't put it in the type vector. */
1200 typenums[0] = typenums[1] = -1;
1202 else
1204 if (! parse_stab_type_number (pp, typenums))
1205 return DEBUG_TYPE_NULL;
1207 if (**pp != '=')
1209 /* Type is not being defined here. Either it already
1210 exists, or this is a forward reference to it. */
1211 return stab_find_type (dhandle, info, typenums);
1214 /* Only set the slot if the type is being defined. This means
1215 that the mapping from type numbers to types will only record
1216 the name of the typedef which defines a type. If we don't do
1217 this, then something like
1218 typedef int foo;
1219 int i;
1220 will record that i is of type foo. Unfortunately, stabs
1221 information is ambiguous about variable types. For this code,
1222 typedef int foo;
1223 int i;
1224 foo j;
1225 the stabs information records both i and j as having the same
1226 type. This could be fixed by patching the compiler. */
1227 if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1228 *slotp = stab_find_slot (info, typenums);
1230 /* Type is being defined here. */
1231 /* Skip the '='. */
1232 ++*pp;
1234 while (**pp == '@')
1236 const char *p = *pp + 1;
1237 const char *attr;
1239 if (ISDIGIT (*p) || *p == '(' || *p == '-')
1241 /* Member type. */
1242 break;
1245 /* Type attributes. */
1246 attr = p;
1248 for (; *p != ';'; ++p)
1250 if (*p == '\0')
1252 bad_stab (orig);
1253 return DEBUG_TYPE_NULL;
1256 *pp = p + 1;
1258 switch (*attr)
1260 case 's':
1261 size = atoi (attr + 1);
1262 size /= 8; /* Size is in bits. We store it in bytes. */
1263 if (size <= 0)
1264 size = -1;
1265 break;
1267 case 'S':
1268 stringp = TRUE;
1269 break;
1271 default:
1272 /* Ignore unrecognized type attributes, so future
1273 compilers can invent new ones. */
1274 break;
1279 descriptor = **pp;
1280 ++*pp;
1282 switch (descriptor)
1284 case 'x':
1286 enum debug_type_kind code;
1287 const char *q1, *q2, *p;
1289 /* A cross reference to another type. */
1291 switch (**pp)
1293 case 's':
1294 code = DEBUG_KIND_STRUCT;
1295 break;
1296 case 'u':
1297 code = DEBUG_KIND_UNION;
1298 break;
1299 case 'e':
1300 code = DEBUG_KIND_ENUM;
1301 break;
1302 default:
1303 /* Complain and keep going, so compilers can invent new
1304 cross-reference types. */
1305 warn_stab (orig, _("unrecognized cross reference type"));
1306 code = DEBUG_KIND_STRUCT;
1307 break;
1309 ++*pp;
1311 q1 = strchr (*pp, '<');
1312 p = strchr (*pp, ':');
1313 if (p == NULL)
1315 bad_stab (orig);
1316 return DEBUG_TYPE_NULL;
1318 if (q1 != NULL && p > q1 && p[1] == ':')
1320 int nest = 0;
1322 for (q2 = q1; *q2 != '\0'; ++q2)
1324 if (*q2 == '<')
1325 ++nest;
1326 else if (*q2 == '>')
1327 --nest;
1328 else if (*q2 == ':' && nest == 0)
1329 break;
1331 p = q2;
1332 if (*p != ':')
1334 bad_stab (orig);
1335 return DEBUG_TYPE_NULL;
1339 /* Some versions of g++ can emit stabs like
1340 fleep:T20=xsfleep:
1341 which define structures in terms of themselves. We need to
1342 tell the caller to avoid building a circular structure. */
1343 if (typename != NULL
1344 && strncmp (typename, *pp, p - *pp) == 0
1345 && typename[p - *pp] == '\0')
1346 info->self_crossref = TRUE;
1348 dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1350 *pp = p + 1;
1352 break;
1354 case '-':
1355 case '0':
1356 case '1':
1357 case '2':
1358 case '3':
1359 case '4':
1360 case '5':
1361 case '6':
1362 case '7':
1363 case '8':
1364 case '9':
1365 case '(':
1367 const char *hold;
1368 int xtypenums[2];
1370 /* This type is defined as another type. */
1372 (*pp)--;
1373 hold = *pp;
1375 /* Peek ahead at the number to detect void. */
1376 if (! parse_stab_type_number (pp, xtypenums))
1377 return DEBUG_TYPE_NULL;
1379 if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1381 /* This type is being defined as itself, which means that
1382 it is void. */
1383 dtype = debug_make_void_type (dhandle);
1385 else
1387 *pp = hold;
1389 /* Go back to the number and have parse_stab_type get it.
1390 This means that we can deal with something like
1391 t(1,2)=(3,4)=... which the Lucid compiler uses. */
1392 dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1393 pp, (debug_type **) NULL);
1394 if (dtype == DEBUG_TYPE_NULL)
1395 return DEBUG_TYPE_NULL;
1398 if (typenums[0] != -1)
1400 if (! stab_record_type (dhandle, info, typenums, dtype))
1401 return DEBUG_TYPE_NULL;
1404 break;
1407 case '*':
1408 dtype = debug_make_pointer_type (dhandle,
1409 parse_stab_type (dhandle, info,
1410 (const char *) NULL,
1412 (debug_type **) NULL));
1413 break;
1415 case '&':
1416 /* Reference to another type. */
1417 dtype = (debug_make_reference_type
1418 (dhandle,
1419 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1420 (debug_type **) NULL)));
1421 break;
1423 case 'f':
1424 /* Function returning another type. */
1425 /* FIXME: gdb checks os9k_stabs here. */
1426 dtype = (debug_make_function_type
1427 (dhandle,
1428 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1429 (debug_type **) NULL),
1430 (debug_type *) NULL, FALSE));
1431 break;
1433 case 'k':
1434 /* Const qualifier on some type (Sun). */
1435 /* FIXME: gdb accepts 'c' here if os9k_stabs. */
1436 dtype = debug_make_const_type (dhandle,
1437 parse_stab_type (dhandle, info,
1438 (const char *) NULL,
1440 (debug_type **) NULL));
1441 break;
1443 case 'B':
1444 /* Volatile qual on some type (Sun). */
1445 /* FIXME: gdb accepts 'i' here if os9k_stabs. */
1446 dtype = (debug_make_volatile_type
1447 (dhandle,
1448 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1449 (debug_type **) NULL)));
1450 break;
1452 case '@':
1453 /* Offset (class & variable) type. This is used for a pointer
1454 relative to an object. */
1456 debug_type domain;
1457 debug_type memtype;
1459 /* Member type. */
1461 domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1462 (debug_type **) NULL);
1463 if (domain == DEBUG_TYPE_NULL)
1464 return DEBUG_TYPE_NULL;
1466 if (**pp != ',')
1468 bad_stab (orig);
1469 return DEBUG_TYPE_NULL;
1471 ++*pp;
1473 memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1474 (debug_type **) NULL);
1475 if (memtype == DEBUG_TYPE_NULL)
1476 return DEBUG_TYPE_NULL;
1478 dtype = debug_make_offset_type (dhandle, domain, memtype);
1480 break;
1482 case '#':
1483 /* Method (class & fn) type. */
1484 if (**pp == '#')
1486 debug_type return_type;
1488 ++*pp;
1489 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1490 pp, (debug_type **) NULL);
1491 if (return_type == DEBUG_TYPE_NULL)
1492 return DEBUG_TYPE_NULL;
1493 if (**pp != ';')
1495 bad_stab (orig);
1496 return DEBUG_TYPE_NULL;
1498 ++*pp;
1499 dtype = debug_make_method_type (dhandle, return_type,
1500 DEBUG_TYPE_NULL,
1501 (debug_type *) NULL, FALSE);
1503 else
1505 debug_type domain;
1506 debug_type return_type;
1507 debug_type *args;
1508 unsigned int n;
1509 unsigned int alloc;
1510 bfd_boolean varargs;
1512 domain = parse_stab_type (dhandle, info, (const char *) NULL,
1513 pp, (debug_type **) NULL);
1514 if (domain == DEBUG_TYPE_NULL)
1515 return DEBUG_TYPE_NULL;
1517 if (**pp != ',')
1519 bad_stab (orig);
1520 return DEBUG_TYPE_NULL;
1522 ++*pp;
1524 return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1525 pp, (debug_type **) NULL);
1526 if (return_type == DEBUG_TYPE_NULL)
1527 return DEBUG_TYPE_NULL;
1529 alloc = 10;
1530 args = (debug_type *) xmalloc (alloc * sizeof *args);
1531 n = 0;
1532 while (**pp != ';')
1534 if (**pp != ',')
1536 bad_stab (orig);
1537 return DEBUG_TYPE_NULL;
1539 ++*pp;
1541 if (n + 1 >= alloc)
1543 alloc += 10;
1544 args = ((debug_type *)
1545 xrealloc ((PTR) args, alloc * sizeof *args));
1548 args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1549 pp, (debug_type **) NULL);
1550 if (args[n] == DEBUG_TYPE_NULL)
1551 return DEBUG_TYPE_NULL;
1552 ++n;
1554 ++*pp;
1556 /* If the last type is not void, then this function takes a
1557 variable number of arguments. Otherwise, we must strip
1558 the void type. */
1559 if (n == 0
1560 || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1561 varargs = TRUE;
1562 else
1564 --n;
1565 varargs = FALSE;
1568 args[n] = DEBUG_TYPE_NULL;
1570 dtype = debug_make_method_type (dhandle, return_type, domain, args,
1571 varargs);
1573 break;
1575 case 'r':
1576 /* Range type. */
1577 dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
1578 break;
1580 case 'b':
1581 /* FIXME: gdb checks os9k_stabs here. */
1582 /* Sun ACC builtin int type. */
1583 dtype = parse_stab_sun_builtin_type (dhandle, pp);
1584 break;
1586 case 'R':
1587 /* Sun ACC builtin float type. */
1588 dtype = parse_stab_sun_floating_type (dhandle, pp);
1589 break;
1591 case 'e':
1592 /* Enumeration type. */
1593 dtype = parse_stab_enum_type (dhandle, pp);
1594 break;
1596 case 's':
1597 case 'u':
1598 /* Struct or union type. */
1599 dtype = parse_stab_struct_type (dhandle, info, typename, pp,
1600 descriptor == 's', typenums);
1601 break;
1603 case 'a':
1604 /* Array type. */
1605 if (**pp != 'r')
1607 bad_stab (orig);
1608 return DEBUG_TYPE_NULL;
1610 ++*pp;
1612 dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1613 break;
1615 case 'S':
1616 dtype = debug_make_set_type (dhandle,
1617 parse_stab_type (dhandle, info,
1618 (const char *) NULL,
1620 (debug_type **) NULL),
1621 stringp);
1622 break;
1624 default:
1625 bad_stab (orig);
1626 return DEBUG_TYPE_NULL;
1629 if (dtype == DEBUG_TYPE_NULL)
1630 return DEBUG_TYPE_NULL;
1632 if (typenums[0] != -1)
1634 if (! stab_record_type (dhandle, info, typenums, dtype))
1635 return DEBUG_TYPE_NULL;
1638 if (size != -1)
1640 if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1641 return DEBUG_TYPE_NULL;
1644 return dtype;
1647 /* Read a number by which a type is referred to in dbx data, or
1648 perhaps read a pair (FILENUM, TYPENUM) in parentheses. Just a
1649 single number N is equivalent to (0,N). Return the two numbers by
1650 storing them in the vector TYPENUMS. */
1652 static bfd_boolean
1653 parse_stab_type_number (pp, typenums)
1654 const char **pp;
1655 int *typenums;
1657 const char *orig;
1659 orig = *pp;
1661 if (**pp != '(')
1663 typenums[0] = 0;
1664 typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
1666 else
1668 ++*pp;
1669 typenums[0] = (int) parse_number (pp, (bfd_boolean *) NULL);
1670 if (**pp != ',')
1672 bad_stab (orig);
1673 return FALSE;
1675 ++*pp;
1676 typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
1677 if (**pp != ')')
1679 bad_stab (orig);
1680 return FALSE;
1682 ++*pp;
1685 return TRUE;
1688 /* Parse a range type. */
1690 static debug_type
1691 parse_stab_range_type (dhandle, info, typename, pp, typenums)
1692 PTR dhandle;
1693 struct stab_handle *info;
1694 const char *typename;
1695 const char **pp;
1696 const int *typenums;
1698 const char *orig;
1699 int rangenums[2];
1700 bfd_boolean self_subrange;
1701 debug_type index_type;
1702 const char *s2, *s3;
1703 bfd_signed_vma n2, n3;
1704 bfd_boolean ov2, ov3;
1706 orig = *pp;
1708 index_type = DEBUG_TYPE_NULL;
1710 /* First comes a type we are a subrange of.
1711 In C it is usually 0, 1 or the type being defined. */
1712 if (! parse_stab_type_number (pp, rangenums))
1713 return DEBUG_TYPE_NULL;
1715 self_subrange = (rangenums[0] == typenums[0]
1716 && rangenums[1] == typenums[1]);
1718 if (**pp == '=')
1720 *pp = orig;
1721 index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1722 pp, (debug_type **) NULL);
1723 if (index_type == DEBUG_TYPE_NULL)
1724 return DEBUG_TYPE_NULL;
1727 if (**pp == ';')
1728 ++*pp;
1730 /* The remaining two operands are usually lower and upper bounds of
1731 the range. But in some special cases they mean something else. */
1732 s2 = *pp;
1733 n2 = parse_number (pp, &ov2);
1734 if (**pp != ';')
1736 bad_stab (orig);
1737 return DEBUG_TYPE_NULL;
1739 ++*pp;
1741 s3 = *pp;
1742 n3 = parse_number (pp, &ov3);
1743 if (**pp != ';')
1745 bad_stab (orig);
1746 return DEBUG_TYPE_NULL;
1748 ++*pp;
1750 if (ov2 || ov3)
1752 /* gcc will emit range stabs for long long types. Handle this
1753 as a special case. FIXME: This needs to be more general. */
1754 #define LLLOW "01000000000000000000000;"
1755 #define LLHIGH "0777777777777777777777;"
1756 #define ULLHIGH "01777777777777777777777;"
1757 if (index_type == DEBUG_TYPE_NULL)
1759 if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1760 && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1761 return debug_make_int_type (dhandle, 8, FALSE);
1762 if (! ov2
1763 && n2 == 0
1764 && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1765 return debug_make_int_type (dhandle, 8, TRUE);
1768 warn_stab (orig, _("numeric overflow"));
1771 if (index_type == DEBUG_TYPE_NULL)
1773 /* A type defined as a subrange of itself, with both bounds 0,
1774 is void. */
1775 if (self_subrange && n2 == 0 && n3 == 0)
1776 return debug_make_void_type (dhandle);
1778 /* A type defined as a subrange of itself, with n2 positive and
1779 n3 zero, is a complex type, and n2 is the number of bytes. */
1780 if (self_subrange && n3 == 0 && n2 > 0)
1781 return debug_make_complex_type (dhandle, n2);
1783 /* If n3 is zero and n2 is positive, this is a floating point
1784 type, and n2 is the number of bytes. */
1785 if (n3 == 0 && n2 > 0)
1786 return debug_make_float_type (dhandle, n2);
1788 /* If the upper bound is -1, this is an unsigned int. */
1789 if (n2 == 0 && n3 == -1)
1791 /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1792 long long int:t6=r1;0;-1;
1793 long long unsigned int:t7=r1;0;-1;
1794 We hack here to handle this reasonably. */
1795 if (typename != NULL)
1797 if (strcmp (typename, "long long int") == 0)
1798 return debug_make_int_type (dhandle, 8, FALSE);
1799 else if (strcmp (typename, "long long unsigned int") == 0)
1800 return debug_make_int_type (dhandle, 8, TRUE);
1802 /* FIXME: The size here really depends upon the target. */
1803 return debug_make_int_type (dhandle, 4, TRUE);
1806 /* A range of 0 to 127 is char. */
1807 if (self_subrange && n2 == 0 && n3 == 127)
1808 return debug_make_int_type (dhandle, 1, FALSE);
1810 /* FIXME: gdb checks for the language CHILL here. */
1812 if (n2 == 0)
1814 if (n3 < 0)
1815 return debug_make_int_type (dhandle, - n3, TRUE);
1816 else if (n3 == 0xff)
1817 return debug_make_int_type (dhandle, 1, TRUE);
1818 else if (n3 == 0xffff)
1819 return debug_make_int_type (dhandle, 2, TRUE);
1820 else if (n3 == (bfd_signed_vma) 0xffffffff)
1821 return debug_make_int_type (dhandle, 4, TRUE);
1822 #ifdef BFD64
1823 else if (n3 == ((((bfd_signed_vma) 0xffffffff) << 32) | 0xffffffff))
1824 return debug_make_int_type (dhandle, 8, TRUE);
1825 #endif
1827 else if (n3 == 0
1828 && n2 < 0
1829 && (self_subrange || n2 == -8))
1830 return debug_make_int_type (dhandle, - n2, TRUE);
1831 else if (n2 == - n3 - 1 || n2 == n3 + 1)
1833 if (n3 == 0x7f)
1834 return debug_make_int_type (dhandle, 1, FALSE);
1835 else if (n3 == 0x7fff)
1836 return debug_make_int_type (dhandle, 2, FALSE);
1837 else if (n3 == 0x7fffffff)
1838 return debug_make_int_type (dhandle, 4, FALSE);
1839 #ifdef BFD64
1840 else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
1841 return debug_make_int_type (dhandle, 8, FALSE);
1842 #endif
1846 /* At this point I don't have the faintest idea how to deal with a
1847 self_subrange type; I'm going to assume that this is used as an
1848 idiom, and that all of them are special cases. So . . . */
1849 if (self_subrange)
1851 bad_stab (orig);
1852 return DEBUG_TYPE_NULL;
1855 index_type = stab_find_type (dhandle, info, rangenums);
1856 if (index_type == DEBUG_TYPE_NULL)
1858 /* Does this actually ever happen? Is that why we are worrying
1859 about dealing with it rather than just calling error_type? */
1860 warn_stab (orig, _("missing index type"));
1861 index_type = debug_make_int_type (dhandle, 4, FALSE);
1864 return debug_make_range_type (dhandle, index_type, n2, n3);
1867 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1868 typedefs in every file (for int, long, etc):
1870 type = b <signed> <width>; <offset>; <nbits>
1871 signed = u or s. Possible c in addition to u or s (for char?).
1872 offset = offset from high order bit to start bit of type.
1873 width is # bytes in object of this type, nbits is # bits in type.
1875 The width/offset stuff appears to be for small objects stored in
1876 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
1877 FIXME. */
1879 static debug_type
1880 parse_stab_sun_builtin_type (dhandle, pp)
1881 PTR dhandle;
1882 const char **pp;
1884 const char *orig;
1885 bfd_boolean unsignedp;
1886 bfd_vma bits;
1888 orig = *pp;
1890 switch (**pp)
1892 case 's':
1893 unsignedp = FALSE;
1894 break;
1895 case 'u':
1896 unsignedp = TRUE;
1897 break;
1898 default:
1899 bad_stab (orig);
1900 return DEBUG_TYPE_NULL;
1902 ++*pp;
1904 /* For some odd reason, all forms of char put a c here. This is strange
1905 because no other type has this honor. We can safely ignore this because
1906 we actually determine 'char'acterness by the number of bits specified in
1907 the descriptor. */
1908 if (**pp == 'c')
1909 ++*pp;
1911 /* The first number appears to be the number of bytes occupied
1912 by this type, except that unsigned short is 4 instead of 2.
1913 Since this information is redundant with the third number,
1914 we will ignore it. */
1915 (void) parse_number (pp, (bfd_boolean *) NULL);
1916 if (**pp != ';')
1918 bad_stab (orig);
1919 return DEBUG_TYPE_NULL;
1921 ++*pp;
1923 /* The second number is always 0, so ignore it too. */
1924 (void) parse_number (pp, (bfd_boolean *) NULL);
1925 if (**pp != ';')
1927 bad_stab (orig);
1928 return DEBUG_TYPE_NULL;
1930 ++*pp;
1932 /* The third number is the number of bits for this type. */
1933 bits = parse_number (pp, (bfd_boolean *) NULL);
1935 /* The type *should* end with a semicolon. If it are embedded
1936 in a larger type the semicolon may be the only way to know where
1937 the type ends. If this type is at the end of the stabstring we
1938 can deal with the omitted semicolon (but we don't have to like
1939 it). Don't bother to complain(), Sun's compiler omits the semicolon
1940 for "void". */
1941 if (**pp == ';')
1942 ++*pp;
1944 if (bits == 0)
1945 return debug_make_void_type (dhandle);
1947 return debug_make_int_type (dhandle, bits / 8, unsignedp);
1950 /* Parse a builtin floating type generated by the Sun compiler. */
1952 static debug_type
1953 parse_stab_sun_floating_type (dhandle, pp)
1954 PTR dhandle;
1955 const char **pp;
1957 const char *orig;
1958 bfd_vma details;
1959 bfd_vma bytes;
1961 orig = *pp;
1963 /* The first number has more details about the type, for example
1964 FN_COMPLEX. */
1965 details = parse_number (pp, (bfd_boolean *) NULL);
1966 if (**pp != ';')
1968 bad_stab (orig);
1969 return DEBUG_TYPE_NULL;
1972 /* The second number is the number of bytes occupied by this type */
1973 bytes = parse_number (pp, (bfd_boolean *) NULL);
1974 if (**pp != ';')
1976 bad_stab (orig);
1977 return DEBUG_TYPE_NULL;
1980 if (details == NF_COMPLEX
1981 || details == NF_COMPLEX16
1982 || details == NF_COMPLEX32)
1983 return debug_make_complex_type (dhandle, bytes);
1985 return debug_make_float_type (dhandle, bytes);
1988 /* Handle an enum type. */
1990 static debug_type
1991 parse_stab_enum_type (dhandle, pp)
1992 PTR dhandle;
1993 const char **pp;
1995 const char *orig;
1996 const char **names;
1997 bfd_signed_vma *values;
1998 unsigned int n;
1999 unsigned int alloc;
2001 orig = *pp;
2003 /* FIXME: gdb checks os9k_stabs here. */
2005 /* The aix4 compiler emits an extra field before the enum members;
2006 my guess is it's a type of some sort. Just ignore it. */
2007 if (**pp == '-')
2009 while (**pp != ':')
2010 ++*pp;
2011 ++*pp;
2014 /* Read the value-names and their values.
2015 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2016 A semicolon or comma instead of a NAME means the end. */
2017 alloc = 10;
2018 names = (const char **) xmalloc (alloc * sizeof *names);
2019 values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
2020 n = 0;
2021 while (**pp != '\0' && **pp != ';' && **pp != ',')
2023 const char *p;
2024 char *name;
2025 bfd_signed_vma val;
2027 p = *pp;
2028 while (*p != ':')
2029 ++p;
2031 name = savestring (*pp, p - *pp);
2033 *pp = p + 1;
2034 val = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
2035 if (**pp != ',')
2037 bad_stab (orig);
2038 return DEBUG_TYPE_NULL;
2040 ++*pp;
2042 if (n + 1 >= alloc)
2044 alloc += 10;
2045 names = ((const char **)
2046 xrealloc ((PTR) names, alloc * sizeof *names));
2047 values = ((bfd_signed_vma *)
2048 xrealloc ((PTR) values, alloc * sizeof *values));
2051 names[n] = name;
2052 values[n] = val;
2053 ++n;
2056 names[n] = NULL;
2057 values[n] = 0;
2059 if (**pp == ';')
2060 ++*pp;
2062 return debug_make_enum_type (dhandle, names, values);
2065 /* Read the description of a structure (or union type) and return an object
2066 describing the type.
2068 PP points to a character pointer that points to the next unconsumed token
2069 in the the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
2070 *PP will point to "4a:1,0,32;;". */
2072 static debug_type
2073 parse_stab_struct_type (dhandle, info, tagname, pp, structp, typenums)
2074 PTR dhandle;
2075 struct stab_handle *info;
2076 const char *tagname;
2077 const char **pp;
2078 bfd_boolean structp;
2079 const int *typenums;
2081 const char *orig;
2082 bfd_vma size;
2083 debug_baseclass *baseclasses;
2084 debug_field *fields;
2085 bfd_boolean statics;
2086 debug_method *methods;
2087 debug_type vptrbase;
2088 bfd_boolean ownvptr;
2090 orig = *pp;
2092 /* Get the size. */
2093 size = parse_number (pp, (bfd_boolean *) NULL);
2095 /* Get the other information. */
2096 if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
2097 || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
2098 || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
2099 || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
2100 &ownvptr))
2101 return DEBUG_TYPE_NULL;
2103 if (! statics
2104 && baseclasses == NULL
2105 && methods == NULL
2106 && vptrbase == DEBUG_TYPE_NULL
2107 && ! ownvptr)
2108 return debug_make_struct_type (dhandle, structp, size, fields);
2110 return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
2111 methods, vptrbase, ownvptr);
2114 /* The stabs for C++ derived classes contain baseclass information which
2115 is marked by a '!' character after the total size. This function is
2116 called when we encounter the baseclass marker, and slurps up all the
2117 baseclass information.
2119 Immediately following the '!' marker is the number of base classes that
2120 the class is derived from, followed by information for each base class.
2121 For each base class, there are two visibility specifiers, a bit offset
2122 to the base class information within the derived class, a reference to
2123 the type for the base class, and a terminating semicolon.
2125 A typical example, with two base classes, would be "!2,020,19;0264,21;".
2126 ^^ ^ ^ ^ ^ ^ ^
2127 Baseclass information marker __________________|| | | | | | |
2128 Number of baseclasses __________________________| | | | | | |
2129 Visibility specifiers (2) ________________________| | | | | |
2130 Offset in bits from start of class _________________| | | | |
2131 Type number for base class ___________________________| | | |
2132 Visibility specifiers (2) _______________________________| | |
2133 Offset in bits from start of class ________________________| |
2134 Type number of base class ____________________________________|
2136 Return TRUE for success, FALSE for failure. */
2138 static bfd_boolean
2139 parse_stab_baseclasses (dhandle, info, pp, retp)
2140 PTR dhandle;
2141 struct stab_handle *info;
2142 const char **pp;
2143 debug_baseclass **retp;
2145 const char *orig;
2146 unsigned int c, i;
2147 debug_baseclass *classes;
2149 *retp = NULL;
2151 orig = *pp;
2153 if (**pp != '!')
2155 /* No base classes. */
2156 return TRUE;
2158 ++*pp;
2160 c = (unsigned int) parse_number (pp, (bfd_boolean *) NULL);
2162 if (**pp != ',')
2164 bad_stab (orig);
2165 return FALSE;
2167 ++*pp;
2169 classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2171 for (i = 0; i < c; i++)
2173 bfd_boolean virtual;
2174 enum debug_visibility visibility;
2175 bfd_vma bitpos;
2176 debug_type type;
2178 switch (**pp)
2180 case '0':
2181 virtual = FALSE;
2182 break;
2183 case '1':
2184 virtual = TRUE;
2185 break;
2186 default:
2187 warn_stab (orig, _("unknown virtual character for baseclass"));
2188 virtual = FALSE;
2189 break;
2191 ++*pp;
2193 switch (**pp)
2195 case '0':
2196 visibility = DEBUG_VISIBILITY_PRIVATE;
2197 break;
2198 case '1':
2199 visibility = DEBUG_VISIBILITY_PROTECTED;
2200 break;
2201 case '2':
2202 visibility = DEBUG_VISIBILITY_PUBLIC;
2203 break;
2204 default:
2205 warn_stab (orig, _("unknown visibility character for baseclass"));
2206 visibility = DEBUG_VISIBILITY_PUBLIC;
2207 break;
2209 ++*pp;
2211 /* The remaining value is the bit offset of the portion of the
2212 object corresponding to this baseclass. Always zero in the
2213 absence of multiple inheritance. */
2214 bitpos = parse_number (pp, (bfd_boolean *) NULL);
2215 if (**pp != ',')
2217 bad_stab (orig);
2218 return FALSE;
2220 ++*pp;
2222 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2223 (debug_type **) NULL);
2224 if (type == DEBUG_TYPE_NULL)
2225 return FALSE;
2227 classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2228 visibility);
2229 if (classes[i] == DEBUG_BASECLASS_NULL)
2230 return FALSE;
2232 if (**pp != ';')
2233 return FALSE;
2234 ++*pp;
2237 classes[i] = DEBUG_BASECLASS_NULL;
2239 *retp = classes;
2241 return TRUE;
2244 /* Read struct or class data fields. They have the form:
2246 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2248 At the end, we see a semicolon instead of a field.
2250 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2251 a static field.
2253 The optional VISIBILITY is one of:
2255 '/0' (VISIBILITY_PRIVATE)
2256 '/1' (VISIBILITY_PROTECTED)
2257 '/2' (VISIBILITY_PUBLIC)
2258 '/9' (VISIBILITY_IGNORE)
2260 or nothing, for C style fields with public visibility.
2262 Returns 1 for success, 0 for failure. */
2264 static bfd_boolean
2265 parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2266 PTR dhandle;
2267 struct stab_handle *info;
2268 const char **pp;
2269 debug_field **retp;
2270 bfd_boolean *staticsp;
2272 const char *orig;
2273 const char *p;
2274 debug_field *fields;
2275 unsigned int c;
2276 unsigned int alloc;
2278 *retp = NULL;
2279 *staticsp = FALSE;
2281 orig = *pp;
2283 c = 0;
2284 alloc = 10;
2285 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2286 while (**pp != ';')
2288 /* FIXME: gdb checks os9k_stabs here. */
2290 p = *pp;
2292 /* Add 1 to c to leave room for NULL pointer at end. */
2293 if (c + 1 >= alloc)
2295 alloc += 10;
2296 fields = ((debug_field *)
2297 xrealloc ((PTR) fields, alloc * sizeof *fields));
2300 /* If it starts with CPLUS_MARKER it is a special abbreviation,
2301 unless the CPLUS_MARKER is followed by an underscore, in
2302 which case it is just the name of an anonymous type, which we
2303 should handle like any other type name. We accept either '$'
2304 or '.', because a field name can never contain one of these
2305 characters except as a CPLUS_MARKER. */
2307 if ((*p == '$' || *p == '.') && p[1] != '_')
2309 ++*pp;
2310 if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2311 return FALSE;
2312 ++c;
2313 continue;
2316 /* Look for the ':' that separates the field name from the field
2317 values. Data members are delimited by a single ':', while member
2318 functions are delimited by a pair of ':'s. When we hit the member
2319 functions (if any), terminate scan loop and return. */
2321 p = strchr (p, ':');
2322 if (p == NULL)
2324 bad_stab (orig);
2325 return FALSE;
2328 if (p[1] == ':')
2329 break;
2331 if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2332 staticsp))
2333 return FALSE;
2335 ++c;
2338 fields[c] = DEBUG_FIELD_NULL;
2340 *retp = fields;
2342 return TRUE;
2345 /* Special GNU C++ name. */
2347 static bfd_boolean
2348 parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2349 PTR dhandle;
2350 struct stab_handle *info;
2351 const char **pp;
2352 debug_field *retp;
2354 const char *orig;
2355 int cpp_abbrev;
2356 debug_type context;
2357 const char *name;
2358 const char *typename;
2359 debug_type type;
2360 bfd_vma bitpos;
2362 *retp = DEBUG_FIELD_NULL;
2364 orig = *pp;
2366 if (**pp != 'v')
2368 bad_stab (*pp);
2369 return FALSE;
2371 ++*pp;
2373 cpp_abbrev = **pp;
2374 ++*pp;
2376 /* At this point, *pp points to something like "22:23=*22...", where
2377 the type number before the ':' is the "context" and everything
2378 after is a regular type definition. Lookup the type, find it's
2379 name, and construct the field name. */
2381 context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2382 (debug_type **) NULL);
2383 if (context == DEBUG_TYPE_NULL)
2384 return FALSE;
2386 switch (cpp_abbrev)
2388 case 'f':
2389 /* $vf -- a virtual function table pointer. */
2390 name = "_vptr$";
2391 break;
2392 case 'b':
2393 /* $vb -- a virtual bsomethingorother */
2394 typename = debug_get_type_name (dhandle, context);
2395 if (typename == NULL)
2397 warn_stab (orig, _("unnamed $vb type"));
2398 typename = "FOO";
2400 name = concat ("_vb$", typename, (const char *) NULL);
2401 break;
2402 default:
2403 warn_stab (orig, _("unrecognized C++ abbreviation"));
2404 name = "INVALID_CPLUSPLUS_ABBREV";
2405 break;
2408 if (**pp != ':')
2410 bad_stab (orig);
2411 return FALSE;
2413 ++*pp;
2415 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2416 (debug_type **) NULL);
2417 if (**pp != ',')
2419 bad_stab (orig);
2420 return FALSE;
2422 ++*pp;
2424 bitpos = parse_number (pp, (bfd_boolean *) NULL);
2425 if (**pp != ';')
2427 bad_stab (orig);
2428 return FALSE;
2430 ++*pp;
2432 *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2433 DEBUG_VISIBILITY_PRIVATE);
2434 if (*retp == DEBUG_FIELD_NULL)
2435 return FALSE;
2437 return TRUE;
2440 /* Parse a single field in a struct or union. */
2442 static bfd_boolean
2443 parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2444 PTR dhandle;
2445 struct stab_handle *info;
2446 const char **pp;
2447 const char *p;
2448 debug_field *retp;
2449 bfd_boolean *staticsp;
2451 const char *orig;
2452 char *name;
2453 enum debug_visibility visibility;
2454 debug_type type;
2455 bfd_vma bitpos;
2456 bfd_vma bitsize;
2458 orig = *pp;
2460 /* FIXME: gdb checks ARM_DEMANGLING here. */
2462 name = savestring (*pp, p - *pp);
2464 *pp = p + 1;
2466 if (**pp != '/')
2467 visibility = DEBUG_VISIBILITY_PUBLIC;
2468 else
2470 ++*pp;
2471 switch (**pp)
2473 case '0':
2474 visibility = DEBUG_VISIBILITY_PRIVATE;
2475 break;
2476 case '1':
2477 visibility = DEBUG_VISIBILITY_PROTECTED;
2478 break;
2479 case '2':
2480 visibility = DEBUG_VISIBILITY_PUBLIC;
2481 break;
2482 default:
2483 warn_stab (orig, _("unknown visibility character for field"));
2484 visibility = DEBUG_VISIBILITY_PUBLIC;
2485 break;
2487 ++*pp;
2490 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2491 (debug_type **) NULL);
2492 if (type == DEBUG_TYPE_NULL)
2493 return FALSE;
2495 if (**pp == ':')
2497 char *varname;
2499 /* This is a static class member. */
2500 ++*pp;
2501 p = strchr (*pp, ';');
2502 if (p == NULL)
2504 bad_stab (orig);
2505 return FALSE;
2508 varname = savestring (*pp, p - *pp);
2510 *pp = p + 1;
2512 *retp = debug_make_static_member (dhandle, name, type, varname,
2513 visibility);
2514 *staticsp = TRUE;
2516 return TRUE;
2519 if (**pp != ',')
2521 bad_stab (orig);
2522 return FALSE;
2524 ++*pp;
2526 bitpos = parse_number (pp, (bfd_boolean *) NULL);
2527 if (**pp != ',')
2529 bad_stab (orig);
2530 return FALSE;
2532 ++*pp;
2534 bitsize = parse_number (pp, (bfd_boolean *) NULL);
2535 if (**pp != ';')
2537 bad_stab (orig);
2538 return FALSE;
2540 ++*pp;
2542 if (bitpos == 0 && bitsize == 0)
2544 /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2545 so, it is a field which has been optimized out. The correct
2546 stab for this case is to use VISIBILITY_IGNORE, but that is a
2547 recent invention. (2) It is a 0-size array. For example
2548 union { int num; char str[0]; } foo. Printing "<no value>"
2549 for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2550 will continue to work, and a 0-size array as a whole doesn't
2551 have any contents to print.
2553 I suspect this probably could also happen with gcc -gstabs
2554 (not -gstabs+) for static fields, and perhaps other C++
2555 extensions. Hopefully few people use -gstabs with gdb, since
2556 it is intended for dbx compatibility. */
2557 visibility = DEBUG_VISIBILITY_IGNORE;
2560 /* FIXME: gdb does some stuff here to mark fields as unpacked. */
2562 *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2564 return TRUE;
2567 /* Read member function stabs info for C++ classes. The form of each member
2568 function data is:
2570 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2572 An example with two member functions is:
2574 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2576 For the case of overloaded operators, the format is op$::*.funcs, where
2577 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2578 name (such as `+=') and `.' marks the end of the operator name. */
2580 static bfd_boolean
2581 parse_stab_members (dhandle, info, tagname, pp, typenums, retp)
2582 PTR dhandle;
2583 struct stab_handle *info;
2584 const char *tagname;
2585 const char **pp;
2586 const int *typenums;
2587 debug_method **retp;
2589 const char *orig;
2590 debug_method *methods;
2591 unsigned int c;
2592 unsigned int alloc;
2594 *retp = NULL;
2596 orig = *pp;
2598 alloc = 0;
2599 methods = NULL;
2600 c = 0;
2602 while (**pp != ';')
2604 const char *p;
2605 char *name;
2606 debug_method_variant *variants;
2607 unsigned int cvars;
2608 unsigned int allocvars;
2609 debug_type look_ahead_type;
2611 p = strchr (*pp, ':');
2612 if (p == NULL || p[1] != ':')
2613 break;
2615 /* FIXME: Some systems use something other than '$' here. */
2616 if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2618 name = savestring (*pp, p - *pp);
2619 *pp = p + 2;
2621 else
2623 /* This is a completely wierd case. In order to stuff in the
2624 names that might contain colons (the usual name delimiter),
2625 Mike Tiemann defined a different name format which is
2626 signalled if the identifier is "op$". In that case, the
2627 format is "op$::XXXX." where XXXX is the name. This is
2628 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2629 *pp = p + 2;
2630 for (p = *pp; *p != '.' && *p != '\0'; p++)
2632 if (*p != '.')
2634 bad_stab (orig);
2635 return FALSE;
2637 name = savestring (*pp, p - *pp);
2638 *pp = p + 1;
2641 allocvars = 10;
2642 variants = ((debug_method_variant *)
2643 xmalloc (allocvars * sizeof *variants));
2644 cvars = 0;
2646 look_ahead_type = DEBUG_TYPE_NULL;
2650 debug_type type;
2651 bfd_boolean stub;
2652 char *argtypes;
2653 enum debug_visibility visibility;
2654 bfd_boolean constp, volatilep, staticp;
2655 bfd_vma voffset;
2656 debug_type context;
2657 const char *physname;
2658 bfd_boolean varargs;
2660 if (look_ahead_type != DEBUG_TYPE_NULL)
2662 /* g++ version 1 kludge */
2663 type = look_ahead_type;
2664 look_ahead_type = DEBUG_TYPE_NULL;
2666 else
2668 type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2669 (debug_type **) NULL);
2670 if (type == DEBUG_TYPE_NULL)
2671 return FALSE;
2672 if (**pp != ':')
2674 bad_stab (orig);
2675 return FALSE;
2679 ++*pp;
2680 p = strchr (*pp, ';');
2681 if (p == NULL)
2683 bad_stab (orig);
2684 return FALSE;
2687 stub = FALSE;
2688 if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
2689 && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
2690 stub = TRUE;
2692 argtypes = savestring (*pp, p - *pp);
2693 *pp = p + 1;
2695 switch (**pp)
2697 case '0':
2698 visibility = DEBUG_VISIBILITY_PRIVATE;
2699 break;
2700 case '1':
2701 visibility = DEBUG_VISIBILITY_PROTECTED;
2702 break;
2703 default:
2704 visibility = DEBUG_VISIBILITY_PUBLIC;
2705 break;
2707 ++*pp;
2709 constp = FALSE;
2710 volatilep = FALSE;
2711 switch (**pp)
2713 case 'A':
2714 /* Normal function. */
2715 ++*pp;
2716 break;
2717 case 'B':
2718 /* const member function. */
2719 constp = TRUE;
2720 ++*pp;
2721 break;
2722 case 'C':
2723 /* volatile member function. */
2724 volatilep = TRUE;
2725 ++*pp;
2726 break;
2727 case 'D':
2728 /* const volatile member function. */
2729 constp = TRUE;
2730 volatilep = TRUE;
2731 ++*pp;
2732 break;
2733 case '*':
2734 case '?':
2735 case '.':
2736 /* File compiled with g++ version 1; no information. */
2737 break;
2738 default:
2739 warn_stab (orig, _("const/volatile indicator missing"));
2740 break;
2743 staticp = FALSE;
2744 switch (**pp)
2746 case '*':
2747 /* virtual member function, followed by index. The sign
2748 bit is supposedly set to distinguish
2749 pointers-to-methods from virtual function indicies. */
2750 ++*pp;
2751 voffset = parse_number (pp, (bfd_boolean *) NULL);
2752 if (**pp != ';')
2754 bad_stab (orig);
2755 return FALSE;
2757 ++*pp;
2758 voffset &= 0x7fffffff;
2760 if (**pp == ';' || *pp == '\0')
2762 /* Must be g++ version 1. */
2763 context = DEBUG_TYPE_NULL;
2765 else
2767 /* Figure out from whence this virtual function
2768 came. It may belong to virtual function table of
2769 one of its baseclasses. */
2770 look_ahead_type = parse_stab_type (dhandle, info,
2771 (const char *) NULL,
2773 (debug_type **) NULL);
2774 if (**pp == ':')
2776 /* g++ version 1 overloaded methods. */
2777 context = DEBUG_TYPE_NULL;
2779 else
2781 context = look_ahead_type;
2782 look_ahead_type = DEBUG_TYPE_NULL;
2783 if (**pp != ';')
2785 bad_stab (orig);
2786 return FALSE;
2788 ++*pp;
2791 break;
2793 case '?':
2794 /* static member function. */
2795 ++*pp;
2796 staticp = TRUE;
2797 voffset = 0;
2798 context = DEBUG_TYPE_NULL;
2799 if (strncmp (argtypes, name, strlen (name)) != 0)
2800 stub = TRUE;
2801 break;
2803 default:
2804 warn_stab (orig, "member function type missing");
2805 voffset = 0;
2806 context = DEBUG_TYPE_NULL;
2807 break;
2809 case '.':
2810 ++*pp;
2811 voffset = 0;
2812 context = DEBUG_TYPE_NULL;
2813 break;
2816 /* If the type is not a stub, then the argtypes string is
2817 the physical name of the function. Otherwise the
2818 argtypes string is the mangled form of the argument
2819 types, and the full type and the physical name must be
2820 extracted from them. */
2821 if (! stub)
2822 physname = argtypes;
2823 else
2825 debug_type class_type, return_type;
2827 class_type = stab_find_type (dhandle, info, typenums);
2828 if (class_type == DEBUG_TYPE_NULL)
2829 return FALSE;
2830 return_type = debug_get_return_type (dhandle, type);
2831 if (return_type == DEBUG_TYPE_NULL)
2833 bad_stab (orig);
2834 return FALSE;
2836 type = parse_stab_argtypes (dhandle, info, class_type, name,
2837 tagname, return_type, argtypes,
2838 constp, volatilep, &physname);
2839 if (type == DEBUG_TYPE_NULL)
2840 return FALSE;
2843 if (cvars + 1 >= allocvars)
2845 allocvars += 10;
2846 variants = ((debug_method_variant *)
2847 xrealloc ((PTR) variants,
2848 allocvars * sizeof *variants));
2851 if (! staticp)
2852 variants[cvars] = debug_make_method_variant (dhandle, physname,
2853 type, visibility,
2854 constp, volatilep,
2855 voffset, context);
2856 else
2857 variants[cvars] = debug_make_static_method_variant (dhandle,
2858 physname,
2859 type,
2860 visibility,
2861 constp,
2862 volatilep);
2863 if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2864 return FALSE;
2866 ++cvars;
2868 while (**pp != ';' && **pp != '\0');
2870 variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2872 if (**pp != '\0')
2873 ++*pp;
2875 if (c + 1 >= alloc)
2877 alloc += 10;
2878 methods = ((debug_method *)
2879 xrealloc ((PTR) methods, alloc * sizeof *methods));
2882 methods[c] = debug_make_method (dhandle, name, variants);
2884 ++c;
2887 if (methods != NULL)
2888 methods[c] = DEBUG_METHOD_NULL;
2890 *retp = methods;
2892 return TRUE;
2895 /* Parse a string representing argument types for a method. Stabs
2896 tries to save space by packing argument types into a mangled
2897 string. This string should give us enough information to extract
2898 both argument types and the physical name of the function, given
2899 the tag name. */
2901 static debug_type
2902 parse_stab_argtypes (dhandle, info, class_type, fieldname, tagname,
2903 return_type, argtypes, constp, volatilep, pphysname)
2904 PTR dhandle;
2905 struct stab_handle *info;
2906 debug_type class_type;
2907 const char *fieldname;
2908 const char *tagname;
2909 debug_type return_type;
2910 const char *argtypes;
2911 bfd_boolean constp;
2912 bfd_boolean volatilep;
2913 const char **pphysname;
2915 bfd_boolean is_full_physname_constructor;
2916 bfd_boolean is_constructor;
2917 bfd_boolean is_destructor;
2918 debug_type *args;
2919 bfd_boolean varargs;
2920 unsigned int physname_len = 0;
2922 /* Constructors are sometimes handled specially. */
2923 is_full_physname_constructor = ((argtypes[0] == '_'
2924 && argtypes[1] == '_'
2925 && (ISDIGIT (argtypes[2])
2926 || argtypes[2] == 'Q'
2927 || argtypes[2] == 't'))
2928 || strncmp (argtypes, "__ct", 4) == 0);
2930 is_constructor = (is_full_physname_constructor
2931 || (tagname != NULL
2932 && strcmp (fieldname, tagname) == 0));
2933 is_destructor = ((argtypes[0] == '_'
2934 && (argtypes[1] == '$' || argtypes[1] == '.')
2935 && argtypes[2] == '_')
2936 || strncmp (argtypes, "__dt", 4) == 0);
2938 if (is_destructor || is_full_physname_constructor)
2939 *pphysname = argtypes;
2940 else
2942 unsigned int len;
2943 const char *const_prefix;
2944 const char *volatile_prefix;
2945 char buf[20];
2946 unsigned int mangled_name_len;
2947 char *physname;
2949 len = tagname == NULL ? 0 : strlen (tagname);
2950 const_prefix = constp ? "C" : "";
2951 volatile_prefix = volatilep ? "V" : "";
2953 if (len == 0)
2954 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2955 else if (tagname != NULL && strchr (tagname, '<') != NULL)
2957 /* Template methods are fully mangled. */
2958 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2959 tagname = NULL;
2960 len = 0;
2962 else
2963 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2965 mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2966 + strlen (buf)
2967 + len
2968 + strlen (argtypes)
2969 + 1);
2971 if (fieldname[0] == 'o'
2972 && fieldname[1] == 'p'
2973 && (fieldname[2] == '$' || fieldname[2] == '.'))
2975 const char *opname;
2977 opname = cplus_mangle_opname (fieldname + 3, 0);
2978 if (opname == NULL)
2980 fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
2981 return DEBUG_TYPE_NULL;
2983 mangled_name_len += strlen (opname);
2984 physname = (char *) xmalloc (mangled_name_len);
2985 strncpy (physname, fieldname, 3);
2986 strcpy (physname + 3, opname);
2988 else
2990 physname = (char *) xmalloc (mangled_name_len);
2991 if (is_constructor)
2992 physname[0] = '\0';
2993 else
2994 strcpy (physname, fieldname);
2997 physname_len = strlen (physname);
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, physname_len);
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 bfd_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 bfd_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 bfd_boolean stringp;
3113 const char *orig;
3114 const char *p;
3115 int typenums[2];
3116 debug_type index_type;
3117 bfd_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 (**pp) && **pp != '-')
3164 ++*pp;
3165 adjustable = TRUE;
3168 lower = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
3169 if (**pp != ';')
3171 bad_stab (orig);
3172 return DEBUG_TYPE_NULL;
3174 ++*pp;
3176 if (! ISDIGIT (**pp) && **pp != '-')
3178 ++*pp;
3179 adjustable = TRUE;
3182 upper = (bfd_signed_vma) parse_number (pp, (bfd_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 bfd_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 bfd_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 bfd_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 bfd_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 bfd_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
3739 PARAMS ((const char *));
3740 static unsigned int stab_demangle_count
3741 PARAMS ((const char **));
3742 static bfd_boolean stab_demangle_get_count
3743 PARAMS ((const char **, unsigned int *));
3744 static bfd_boolean stab_demangle_prefix
3745 PARAMS ((struct stab_demangle_info *, const char **, unsigned int));
3746 static bfd_boolean stab_demangle_function_name
3747 PARAMS ((struct stab_demangle_info *, const char **, const char *));
3748 static bfd_boolean stab_demangle_signature
3749 PARAMS ((struct stab_demangle_info *, const char **));
3750 static bfd_boolean stab_demangle_qualified
3751 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3752 static bfd_boolean stab_demangle_template
3753 PARAMS ((struct stab_demangle_info *, const char **, char **));
3754 static bfd_boolean stab_demangle_class
3755 PARAMS ((struct stab_demangle_info *, const char **, const char **));
3756 static bfd_boolean stab_demangle_args
3757 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3758 bfd_boolean *));
3759 static bfd_boolean stab_demangle_arg
3760 PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3761 unsigned int *, unsigned int *));
3762 static bfd_boolean stab_demangle_type
3763 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3764 static bfd_boolean stab_demangle_fund_type
3765 PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3766 static bfd_boolean stab_demangle_remember_type
3767 PARAMS ((struct stab_demangle_info *, const char *, int));
3769 /* Warn about a bad demangling. */
3771 static void
3772 stab_bad_demangle (s)
3773 const char *s;
3775 fprintf (stderr, _("bad mangled name `%s'\n"), s);
3778 /* Get a count from a stab string. */
3780 static unsigned int
3781 stab_demangle_count (pp)
3782 const char **pp;
3784 unsigned int count;
3786 count = 0;
3787 while (ISDIGIT (**pp))
3789 count *= 10;
3790 count += **pp - '0';
3791 ++*pp;
3793 return count;
3796 /* Require a count in a string. The count may be multiple digits, in
3797 which case it must end in an underscore. */
3799 static bfd_boolean
3800 stab_demangle_get_count (pp, pi)
3801 const char **pp;
3802 unsigned int *pi;
3804 if (! ISDIGIT (**pp))
3805 return FALSE;
3807 *pi = **pp - '0';
3808 ++*pp;
3809 if (ISDIGIT (**pp))
3811 unsigned int count;
3812 const char *p;
3814 count = *pi;
3815 p = *pp;
3818 count *= 10;
3819 count += *p - '0';
3820 ++p;
3822 while (ISDIGIT (*p));
3823 if (*p == '_')
3825 *pp = p + 1;
3826 *pi = count;
3830 return TRUE;
3833 /* This function demangles a physical name, returning a NULL
3834 terminated array of argument types. */
3836 static debug_type *
3837 stab_demangle_argtypes (dhandle, info, physname, pvarargs, physname_len)
3838 PTR dhandle;
3839 struct stab_handle *info;
3840 const char *physname;
3841 bfd_boolean *pvarargs;
3842 unsigned int physname_len;
3844 struct stab_demangle_info minfo;
3846 minfo.dhandle = dhandle;
3847 minfo.info = info;
3848 minfo.args = NULL;
3849 minfo.varargs = FALSE;
3850 minfo.typestring_alloc = 10;
3851 minfo.typestrings = ((struct stab_demangle_typestring *)
3852 xmalloc (minfo.typestring_alloc
3853 * sizeof *minfo.typestrings));
3854 minfo.typestring_count = 0;
3856 /* cplus_demangle checks for special GNU mangled forms, but we can't
3857 see any of them in mangled method argument types. */
3859 if (! stab_demangle_prefix (&minfo, &physname, physname_len))
3860 goto error_return;
3862 if (*physname != '\0')
3864 if (! stab_demangle_signature (&minfo, &physname))
3865 goto error_return;
3868 free (minfo.typestrings);
3869 minfo.typestrings = NULL;
3871 if (minfo.args == NULL)
3872 fprintf (stderr, _("no argument types in mangled string\n"));
3874 *pvarargs = minfo.varargs;
3875 return minfo.args;
3877 error_return:
3878 if (minfo.typestrings != NULL)
3879 free (minfo.typestrings);
3880 return NULL;
3883 /* Demangle the prefix of the mangled name. */
3885 static bfd_boolean
3886 stab_demangle_prefix (minfo, pp, physname_len)
3887 struct stab_demangle_info *minfo;
3888 const char **pp;
3889 unsigned int physname_len;
3891 const char *scan;
3892 unsigned int i;
3894 /* cplus_demangle checks for global constructors and destructors,
3895 but we can't see them in mangled argument types. */
3897 if (physname_len)
3898 scan = *pp + physname_len;
3899 else
3901 /* Look for `__'. */
3902 scan = *pp;
3904 scan = strchr (scan, '_');
3905 while (scan != NULL && *++scan != '_');
3907 if (scan == NULL)
3909 stab_bad_demangle (*pp);
3910 return FALSE;
3913 --scan;
3915 /* We found `__'; move ahead to the last contiguous `__' pair. */
3916 i = strspn (scan, "_");
3917 if (i > 2)
3918 scan += i - 2;
3921 if (scan == *pp
3922 && (ISDIGIT (scan[2])
3923 || scan[2] == 'Q'
3924 || scan[2] == 't'))
3926 /* This is a GNU style constructor name. */
3927 *pp = scan + 2;
3928 return TRUE;
3930 else if (scan == *pp
3931 && ! ISDIGIT (scan[2])
3932 && scan[2] != 't')
3934 /* Look for the `__' that separates the prefix from the
3935 signature. */
3936 while (*scan == '_')
3937 ++scan;
3938 scan = strstr (scan, "__");
3939 if (scan == NULL || scan[2] == '\0')
3941 stab_bad_demangle (*pp);
3942 return FALSE;
3945 return stab_demangle_function_name (minfo, pp, scan);
3947 else if (scan[2] != '\0')
3949 /* The name doesn't start with `__', but it does contain `__'. */
3950 return stab_demangle_function_name (minfo, pp, scan);
3952 else
3954 stab_bad_demangle (*pp);
3955 return FALSE;
3957 /*NOTREACHED*/
3960 /* Demangle a function name prefix. The scan argument points to the
3961 double underscore which separates the function name from the
3962 signature. */
3964 static bfd_boolean
3965 stab_demangle_function_name (minfo, pp, scan)
3966 struct stab_demangle_info *minfo;
3967 const char **pp;
3968 const char *scan;
3970 const char *name;
3972 /* The string from *pp to scan is the name of the function. We
3973 don't care about the name, since we just looking for argument
3974 types. However, for conversion operators, the name may include a
3975 type which we must remember in order to handle backreferences. */
3977 name = *pp;
3978 *pp = scan + 2;
3980 if (*pp - name >= 5
3981 && strncmp (name, "type", 4) == 0
3982 && (name[4] == '$' || name[4] == '.'))
3984 const char *tem;
3986 /* This is a type conversion operator. */
3987 tem = name + 5;
3988 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3989 return FALSE;
3991 else if (name[0] == '_'
3992 && name[1] == '_'
3993 && name[2] == 'o'
3994 && name[3] == 'p')
3996 const char *tem;
3998 /* This is a type conversion operator. */
3999 tem = name + 4;
4000 if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
4001 return FALSE;
4004 return TRUE;
4007 /* Demangle the signature. This is where the argument types are
4008 found. */
4010 static bfd_boolean
4011 stab_demangle_signature (minfo, pp)
4012 struct stab_demangle_info *minfo;
4013 const char **pp;
4015 const char *orig;
4016 bfd_boolean expect_func, func_done;
4017 const char *hold;
4019 orig = *pp;
4021 expect_func = FALSE;
4022 func_done = FALSE;
4023 hold = NULL;
4025 while (**pp != '\0')
4027 switch (**pp)
4029 case 'Q':
4030 hold = *pp;
4031 if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
4032 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4033 return FALSE;
4034 expect_func = TRUE;
4035 hold = NULL;
4036 break;
4038 case 'S':
4039 /* Static member function. FIXME: Can this happen? */
4040 if (hold == NULL)
4041 hold = *pp;
4042 ++*pp;
4043 break;
4045 case 'C':
4046 /* Const member function. */
4047 if (hold == NULL)
4048 hold = *pp;
4049 ++*pp;
4050 break;
4052 case '0': case '1': case '2': case '3': case '4':
4053 case '5': case '6': case '7': case '8': case '9':
4054 if (hold == NULL)
4055 hold = *pp;
4056 if (! stab_demangle_class (minfo, pp, (const char **) NULL)
4057 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4058 return FALSE;
4059 expect_func = TRUE;
4060 hold = NULL;
4061 break;
4063 case 'F':
4064 /* Function. I don't know if this actually happens with g++
4065 output. */
4066 hold = NULL;
4067 func_done = TRUE;
4068 ++*pp;
4069 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4070 return FALSE;
4071 break;
4073 case 't':
4074 /* Template. */
4075 if (hold == NULL)
4076 hold = *pp;
4077 if (! stab_demangle_template (minfo, pp, (char **) NULL)
4078 || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4079 return FALSE;
4080 hold = NULL;
4081 expect_func = TRUE;
4082 break;
4084 case '_':
4085 /* At the outermost level, we cannot have a return type
4086 specified, so if we run into another '_' at this point we
4087 are dealing with a mangled name that is either bogus, or
4088 has been mangled by some algorithm we don't know how to
4089 deal with. So just reject the entire demangling. */
4090 stab_bad_demangle (orig);
4091 return FALSE;
4093 default:
4094 /* Assume we have stumbled onto the first outermost function
4095 argument token, and start processing args. */
4096 func_done = TRUE;
4097 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4098 return FALSE;
4099 break;
4102 if (expect_func)
4104 func_done = TRUE;
4105 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4106 return FALSE;
4110 if (! func_done)
4112 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4113 bar__3fooi is 'foo::bar(int)'. We get here when we find the
4114 first case, and need to ensure that the '(void)' gets added
4115 to the current declp. */
4116 if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4117 return FALSE;
4120 return TRUE;
4123 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4124 mangled form of "Outer::Inner". */
4126 static bfd_boolean
4127 stab_demangle_qualified (minfo, pp, ptype)
4128 struct stab_demangle_info *minfo;
4129 const char **pp;
4130 debug_type *ptype;
4132 const char *orig;
4133 const char *p;
4134 unsigned int qualifiers;
4135 debug_type context;
4137 orig = *pp;
4139 switch ((*pp)[1])
4141 case '_':
4142 /* GNU mangled name with more than 9 classes. The count is
4143 preceded by an underscore (to distinguish it from the <= 9
4144 case) and followed by an underscore. */
4145 p = *pp + 2;
4146 if (! ISDIGIT (*p) || *p == '0')
4148 stab_bad_demangle (orig);
4149 return FALSE;
4151 qualifiers = atoi (p);
4152 while (ISDIGIT (*p))
4153 ++p;
4154 if (*p != '_')
4156 stab_bad_demangle (orig);
4157 return FALSE;
4159 *pp = p + 1;
4160 break;
4162 case '1': case '2': case '3': case '4': case '5':
4163 case '6': case '7': case '8': case '9':
4164 qualifiers = (*pp)[1] - '0';
4165 /* Skip an optional underscore after the count. */
4166 if ((*pp)[2] == '_')
4167 ++*pp;
4168 *pp += 2;
4169 break;
4171 case '0':
4172 default:
4173 stab_bad_demangle (orig);
4174 return FALSE;
4177 context = DEBUG_TYPE_NULL;
4179 /* Pick off the names. */
4180 while (qualifiers-- > 0)
4182 if (**pp == '_')
4183 ++*pp;
4184 if (**pp == 't')
4186 char *name;
4188 if (! stab_demangle_template (minfo, pp,
4189 ptype != NULL ? &name : NULL))
4190 return FALSE;
4192 if (ptype != NULL)
4194 context = stab_find_tagged_type (minfo->dhandle, minfo->info,
4195 name, strlen (name),
4196 DEBUG_KIND_CLASS);
4197 free (name);
4198 if (context == DEBUG_TYPE_NULL)
4199 return FALSE;
4202 else
4204 unsigned int len;
4206 len = stab_demangle_count (pp);
4207 if (strlen (*pp) < len)
4209 stab_bad_demangle (orig);
4210 return FALSE;
4213 if (ptype != NULL)
4215 const debug_field *fields;
4217 fields = NULL;
4218 if (context != DEBUG_TYPE_NULL)
4219 fields = debug_get_fields (minfo->dhandle, context);
4221 context = DEBUG_TYPE_NULL;
4223 if (fields != NULL)
4225 char *name;
4227 /* Try to find the type by looking through the
4228 fields of context until we find a field with the
4229 same type. This ought to work for a class
4230 defined within a class, but it won't work for,
4231 e.g., an enum defined within a class. stabs does
4232 not give us enough information to figure out the
4233 latter case. */
4235 name = savestring (*pp, len);
4237 for (; *fields != DEBUG_FIELD_NULL; fields++)
4239 debug_type ft;
4240 const char *dn;
4242 ft = debug_get_field_type (minfo->dhandle, *fields);
4243 if (ft == NULL)
4244 return FALSE;
4245 dn = debug_get_type_name (minfo->dhandle, ft);
4246 if (dn != NULL && strcmp (dn, name) == 0)
4248 context = ft;
4249 break;
4253 free (name);
4256 if (context == DEBUG_TYPE_NULL)
4258 /* We have to fall back on finding the type by name.
4259 If there are more types to come, then this must
4260 be a class. Otherwise, it could be anything. */
4262 if (qualifiers == 0)
4264 char *name;
4266 name = savestring (*pp, len);
4267 context = debug_find_named_type (minfo->dhandle,
4268 name);
4269 free (name);
4272 if (context == DEBUG_TYPE_NULL)
4274 context = stab_find_tagged_type (minfo->dhandle,
4275 minfo->info,
4276 *pp, len,
4277 (qualifiers == 0
4278 ? DEBUG_KIND_ILLEGAL
4279 : DEBUG_KIND_CLASS));
4280 if (context == DEBUG_TYPE_NULL)
4281 return FALSE;
4286 *pp += len;
4290 if (ptype != NULL)
4291 *ptype = context;
4293 return TRUE;
4296 /* Demangle a template. If PNAME is not NULL, this sets *PNAME to a
4297 string representation of the template. */
4299 static bfd_boolean
4300 stab_demangle_template (minfo, pp, pname)
4301 struct stab_demangle_info *minfo;
4302 const char **pp;
4303 char **pname;
4305 const char *orig;
4306 unsigned int r, i;
4308 orig = *pp;
4310 ++*pp;
4312 /* Skip the template name. */
4313 r = stab_demangle_count (pp);
4314 if (r == 0 || strlen (*pp) < r)
4316 stab_bad_demangle (orig);
4317 return FALSE;
4319 *pp += r;
4321 /* Get the size of the parameter list. */
4322 if (stab_demangle_get_count (pp, &r) == 0)
4324 stab_bad_demangle (orig);
4325 return FALSE;
4328 for (i = 0; i < r; i++)
4330 if (**pp == 'Z')
4332 /* This is a type parameter. */
4333 ++*pp;
4334 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4335 return FALSE;
4337 else
4339 const char *old_p;
4340 bfd_boolean pointerp, realp, integralp, charp, boolp;
4341 bfd_boolean done;
4343 old_p = *pp;
4344 pointerp = FALSE;
4345 realp = FALSE;
4346 integralp = FALSE;
4347 charp = FALSE;
4348 boolp = FALSE;
4349 done = FALSE;
4351 /* This is a value parameter. */
4353 if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4354 return FALSE;
4356 while (*old_p != '\0' && ! done)
4358 switch (*old_p)
4360 case 'P':
4361 case 'p':
4362 case 'R':
4363 pointerp = TRUE;
4364 done = TRUE;
4365 break;
4366 case 'C': /* Const. */
4367 case 'S': /* Signed. */
4368 case 'U': /* Unsigned. */
4369 case 'V': /* Volatile. */
4370 case 'F': /* Function. */
4371 case 'M': /* Member function. */
4372 case 'O': /* ??? */
4373 ++old_p;
4374 break;
4375 case 'Q': /* Qualified name. */
4376 integralp = TRUE;
4377 done = TRUE;
4378 break;
4379 case 'T': /* Remembered type. */
4380 abort ();
4381 case 'v': /* Void. */
4382 abort ();
4383 case 'x': /* Long long. */
4384 case 'l': /* Long. */
4385 case 'i': /* Int. */
4386 case 's': /* Short. */
4387 case 'w': /* Wchar_t. */
4388 integralp = TRUE;
4389 done = TRUE;
4390 break;
4391 case 'b': /* Bool. */
4392 boolp = TRUE;
4393 done = TRUE;
4394 break;
4395 case 'c': /* Char. */
4396 charp = TRUE;
4397 done = TRUE;
4398 break;
4399 case 'r': /* Long double. */
4400 case 'd': /* Double. */
4401 case 'f': /* Float. */
4402 realp = TRUE;
4403 done = TRUE;
4404 break;
4405 default:
4406 /* Assume it's a user defined integral type. */
4407 integralp = TRUE;
4408 done = TRUE;
4409 break;
4413 if (integralp)
4415 if (**pp == 'm')
4416 ++*pp;
4417 while (ISDIGIT (**pp))
4418 ++*pp;
4420 else if (charp)
4422 unsigned int val;
4424 if (**pp == 'm')
4425 ++*pp;
4426 val = stab_demangle_count (pp);
4427 if (val == 0)
4429 stab_bad_demangle (orig);
4430 return FALSE;
4433 else if (boolp)
4435 unsigned int val;
4437 val = stab_demangle_count (pp);
4438 if (val != 0 && val != 1)
4440 stab_bad_demangle (orig);
4441 return FALSE;
4444 else if (realp)
4446 if (**pp == 'm')
4447 ++*pp;
4448 while (ISDIGIT (**pp))
4449 ++*pp;
4450 if (**pp == '.')
4452 ++*pp;
4453 while (ISDIGIT (**pp))
4454 ++*pp;
4456 if (**pp == 'e')
4458 ++*pp;
4459 while (ISDIGIT (**pp))
4460 ++*pp;
4463 else if (pointerp)
4465 unsigned int len;
4467 if (! stab_demangle_get_count (pp, &len))
4469 stab_bad_demangle (orig);
4470 return FALSE;
4472 *pp += len;
4477 /* We can translate this to a string fairly easily by invoking the
4478 regular demangling routine. */
4479 if (pname != NULL)
4481 char *s1, *s2, *s3, *s4 = NULL;
4482 char *from, *to;
4484 s1 = savestring (orig, *pp - orig);
4486 s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
4488 free (s1);
4490 s3 = cplus_demangle (s2, DMGL_ANSI);
4492 free (s2);
4494 if (s3 != NULL)
4495 s4 = strstr (s3, "::NoSuchStrinG");
4496 if (s3 == NULL || s4 == NULL)
4498 stab_bad_demangle (orig);
4499 if (s3 != NULL)
4500 free (s3);
4501 return FALSE;
4504 /* Eliminating all spaces, except those between > characters,
4505 makes it more likely that the demangled name will match the
4506 name which g++ used as the structure name. */
4507 for (from = to = s3; from != s4; ++from)
4508 if (*from != ' '
4509 || (from[1] == '>' && from > s3 && from[-1] == '>'))
4510 *to++ = *from;
4512 *pname = savestring (s3, to - s3);
4514 free (s3);
4517 return TRUE;
4520 /* Demangle a class name. */
4522 static bfd_boolean
4523 stab_demangle_class (minfo, pp, pstart)
4524 struct stab_demangle_info *minfo ATTRIBUTE_UNUSED;
4525 const char **pp;
4526 const char **pstart;
4528 const char *orig;
4529 unsigned int n;
4531 orig = *pp;
4533 n = stab_demangle_count (pp);
4534 if (strlen (*pp) < n)
4536 stab_bad_demangle (orig);
4537 return FALSE;
4540 if (pstart != NULL)
4541 *pstart = *pp;
4543 *pp += n;
4545 return TRUE;
4548 /* Demangle function arguments. If the pargs argument is not NULL, it
4549 is set to a NULL terminated array holding the arguments. */
4551 static bfd_boolean
4552 stab_demangle_args (minfo, pp, pargs, pvarargs)
4553 struct stab_demangle_info *minfo;
4554 const char **pp;
4555 debug_type **pargs;
4556 bfd_boolean *pvarargs;
4558 const char *orig;
4559 unsigned int alloc, count;
4561 orig = *pp;
4563 alloc = 10;
4564 if (pargs != NULL)
4566 *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4567 *pvarargs = FALSE;
4569 count = 0;
4571 while (**pp != '_' && **pp != '\0' && **pp != 'e')
4573 if (**pp == 'N' || **pp == 'T')
4575 char temptype;
4576 unsigned int r, t;
4578 temptype = **pp;
4579 ++*pp;
4581 if (temptype == 'T')
4582 r = 1;
4583 else
4585 if (! stab_demangle_get_count (pp, &r))
4587 stab_bad_demangle (orig);
4588 return FALSE;
4592 if (! stab_demangle_get_count (pp, &t))
4594 stab_bad_demangle (orig);
4595 return FALSE;
4598 if (t >= minfo->typestring_count)
4600 stab_bad_demangle (orig);
4601 return FALSE;
4603 while (r-- > 0)
4605 const char *tem;
4607 tem = minfo->typestrings[t].typestring;
4608 if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4609 return FALSE;
4612 else
4614 if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4615 return FALSE;
4619 if (pargs != NULL)
4620 (*pargs)[count] = DEBUG_TYPE_NULL;
4622 if (**pp == 'e')
4624 if (pargs != NULL)
4625 *pvarargs = TRUE;
4626 ++*pp;
4629 return TRUE;
4632 /* Demangle a single argument. */
4634 static bfd_boolean
4635 stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4636 struct stab_demangle_info *minfo;
4637 const char **pp;
4638 debug_type **pargs;
4639 unsigned int *pcount;
4640 unsigned int *palloc;
4642 const char *start;
4643 debug_type type;
4645 start = *pp;
4646 if (! stab_demangle_type (minfo, pp,
4647 pargs == NULL ? (debug_type *) NULL : &type)
4648 || ! stab_demangle_remember_type (minfo, start, *pp - start))
4649 return FALSE;
4651 if (pargs != NULL)
4653 if (type == DEBUG_TYPE_NULL)
4654 return FALSE;
4656 if (*pcount + 1 >= *palloc)
4658 *palloc += 10;
4659 *pargs = ((debug_type *)
4660 xrealloc (*pargs, *palloc * sizeof **pargs));
4662 (*pargs)[*pcount] = type;
4663 ++*pcount;
4666 return TRUE;
4669 /* Demangle a type. If the ptype argument is not NULL, *ptype is set
4670 to the newly allocated type. */
4672 static bfd_boolean
4673 stab_demangle_type (minfo, pp, ptype)
4674 struct stab_demangle_info *minfo;
4675 const char **pp;
4676 debug_type *ptype;
4678 const char *orig;
4680 orig = *pp;
4682 switch (**pp)
4684 case 'P':
4685 case 'p':
4686 /* A pointer type. */
4687 ++*pp;
4688 if (! stab_demangle_type (minfo, pp, ptype))
4689 return FALSE;
4690 if (ptype != NULL)
4691 *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4692 break;
4694 case 'R':
4695 /* A reference type. */
4696 ++*pp;
4697 if (! stab_demangle_type (minfo, pp, ptype))
4698 return FALSE;
4699 if (ptype != NULL)
4700 *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4701 break;
4703 case 'A':
4704 /* An array. */
4706 unsigned long high;
4708 ++*pp;
4709 high = 0;
4710 while (**pp != '\0' && **pp != '_')
4712 if (! ISDIGIT (**pp))
4714 stab_bad_demangle (orig);
4715 return FALSE;
4717 high *= 10;
4718 high += **pp - '0';
4719 ++*pp;
4721 if (**pp != '_')
4723 stab_bad_demangle (orig);
4724 return FALSE;
4726 ++*pp;
4728 if (! stab_demangle_type (minfo, pp, ptype))
4729 return FALSE;
4730 if (ptype != NULL)
4732 debug_type int_type;
4734 int_type = debug_find_named_type (minfo->dhandle, "int");
4735 if (int_type == NULL)
4736 int_type = debug_make_int_type (minfo->dhandle, 4, FALSE);
4737 *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4738 0, high, FALSE);
4741 break;
4743 case 'T':
4744 /* A back reference to a remembered type. */
4746 unsigned int i;
4747 const char *p;
4749 ++*pp;
4750 if (! stab_demangle_get_count (pp, &i))
4752 stab_bad_demangle (orig);
4753 return FALSE;
4755 if (i >= minfo->typestring_count)
4757 stab_bad_demangle (orig);
4758 return FALSE;
4760 p = minfo->typestrings[i].typestring;
4761 if (! stab_demangle_type (minfo, &p, ptype))
4762 return FALSE;
4764 break;
4766 case 'F':
4767 /* A function. */
4769 debug_type *args;
4770 bfd_boolean varargs;
4772 ++*pp;
4773 if (! stab_demangle_args (minfo, pp,
4774 (ptype == NULL
4775 ? (debug_type **) NULL
4776 : &args),
4777 (ptype == NULL
4778 ? (bfd_boolean *) NULL
4779 : &varargs)))
4780 return FALSE;
4781 if (**pp != '_')
4783 /* cplus_demangle will accept a function without a return
4784 type, but I don't know when that will happen, or what
4785 to do if it does. */
4786 stab_bad_demangle (orig);
4787 return FALSE;
4789 ++*pp;
4790 if (! stab_demangle_type (minfo, pp, ptype))
4791 return FALSE;
4792 if (ptype != NULL)
4793 *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4794 varargs);
4797 break;
4799 case 'M':
4800 case 'O':
4802 bfd_boolean memberp, constp, volatilep;
4803 debug_type class_type = DEBUG_TYPE_NULL;
4804 debug_type *args;
4805 bfd_boolean varargs;
4806 unsigned int n;
4807 const char *name;
4809 memberp = **pp == 'M';
4810 constp = FALSE;
4811 volatilep = FALSE;
4812 args = NULL;
4813 varargs = FALSE;
4815 ++*pp;
4816 if (ISDIGIT (**pp))
4818 n = stab_demangle_count (pp);
4819 if (strlen (*pp) < n)
4821 stab_bad_demangle (orig);
4822 return FALSE;
4824 name = *pp;
4825 *pp += n;
4827 if (ptype != NULL)
4829 class_type = stab_find_tagged_type (minfo->dhandle,
4830 minfo->info,
4831 name, (int) n,
4832 DEBUG_KIND_CLASS);
4833 if (class_type == DEBUG_TYPE_NULL)
4834 return FALSE;
4837 else if (**pp == 'Q')
4839 if (! stab_demangle_qualified (minfo, pp,
4840 (ptype == NULL
4841 ? (debug_type *) NULL
4842 : &class_type)))
4843 return FALSE;
4845 else
4847 stab_bad_demangle (orig);
4848 return FALSE;
4851 if (memberp)
4853 if (**pp == 'C')
4855 constp = TRUE;
4856 ++*pp;
4858 else if (**pp == 'V')
4860 volatilep = TRUE;
4861 ++*pp;
4863 if (**pp != 'F')
4865 stab_bad_demangle (orig);
4866 return FALSE;
4868 ++*pp;
4869 if (! stab_demangle_args (minfo, pp,
4870 (ptype == NULL
4871 ? (debug_type **) NULL
4872 : &args),
4873 (ptype == NULL
4874 ? (bfd_boolean *) NULL
4875 : &varargs)))
4876 return FALSE;
4879 if (**pp != '_')
4881 stab_bad_demangle (orig);
4882 return FALSE;
4884 ++*pp;
4886 if (! stab_demangle_type (minfo, pp, ptype))
4887 return FALSE;
4889 if (ptype != NULL)
4891 if (! memberp)
4892 *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4893 *ptype);
4894 else
4896 /* FIXME: We have no way to record constp or
4897 volatilep. */
4898 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4899 class_type, args, varargs);
4903 break;
4905 case 'G':
4906 ++*pp;
4907 if (! stab_demangle_type (minfo, pp, ptype))
4908 return FALSE;
4909 break;
4911 case 'C':
4912 ++*pp;
4913 if (! stab_demangle_type (minfo, pp, ptype))
4914 return FALSE;
4915 if (ptype != NULL)
4916 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4917 break;
4919 case 'Q':
4921 const char *hold;
4923 hold = *pp;
4924 if (! stab_demangle_qualified (minfo, pp, ptype))
4925 return FALSE;
4927 break;
4929 default:
4930 if (! stab_demangle_fund_type (minfo, pp, ptype))
4931 return FALSE;
4932 break;
4935 return TRUE;
4938 /* Demangle a fundamental type. If the ptype argument is not NULL,
4939 *ptype is set to the newly allocated type. */
4941 static bfd_boolean
4942 stab_demangle_fund_type (minfo, pp, ptype)
4943 struct stab_demangle_info *minfo;
4944 const char **pp;
4945 debug_type *ptype;
4947 const char *orig;
4948 bfd_boolean constp, volatilep, unsignedp, signedp;
4949 bfd_boolean done;
4951 orig = *pp;
4953 constp = FALSE;
4954 volatilep = FALSE;
4955 unsignedp = FALSE;
4956 signedp = FALSE;
4958 done = FALSE;
4959 while (! done)
4961 switch (**pp)
4963 case 'C':
4964 constp = TRUE;
4965 ++*pp;
4966 break;
4968 case 'U':
4969 unsignedp = TRUE;
4970 ++*pp;
4971 break;
4973 case 'S':
4974 signedp = TRUE;
4975 ++*pp;
4976 break;
4978 case 'V':
4979 volatilep = TRUE;
4980 ++*pp;
4981 break;
4983 default:
4984 done = TRUE;
4985 break;
4989 switch (**pp)
4991 case '\0':
4992 case '_':
4993 /* cplus_demangle permits this, but I don't know what it means. */
4994 stab_bad_demangle (orig);
4995 break;
4997 case 'v': /* void */
4998 if (ptype != NULL)
5000 *ptype = debug_find_named_type (minfo->dhandle, "void");
5001 if (*ptype == DEBUG_TYPE_NULL)
5002 *ptype = debug_make_void_type (minfo->dhandle);
5004 ++*pp;
5005 break;
5007 case 'x': /* long long */
5008 if (ptype != NULL)
5010 *ptype = debug_find_named_type (minfo->dhandle,
5011 (unsignedp
5012 ? "long long unsigned int"
5013 : "long long int"));
5014 if (*ptype == DEBUG_TYPE_NULL)
5015 *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
5017 ++*pp;
5018 break;
5020 case 'l': /* long */
5021 if (ptype != NULL)
5023 *ptype = debug_find_named_type (minfo->dhandle,
5024 (unsignedp
5025 ? "long unsigned int"
5026 : "long int"));
5027 if (*ptype == DEBUG_TYPE_NULL)
5028 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5030 ++*pp;
5031 break;
5033 case 'i': /* int */
5034 if (ptype != NULL)
5036 *ptype = debug_find_named_type (minfo->dhandle,
5037 (unsignedp
5038 ? "unsigned int"
5039 : "int"));
5040 if (*ptype == DEBUG_TYPE_NULL)
5041 *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5043 ++*pp;
5044 break;
5046 case 's': /* short */
5047 if (ptype != NULL)
5049 *ptype = debug_find_named_type (minfo->dhandle,
5050 (unsignedp
5051 ? "short unsigned int"
5052 : "short int"));
5053 if (*ptype == DEBUG_TYPE_NULL)
5054 *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
5056 ++*pp;
5057 break;
5059 case 'b': /* bool */
5060 if (ptype != NULL)
5062 *ptype = debug_find_named_type (minfo->dhandle, "bool");
5063 if (*ptype == DEBUG_TYPE_NULL)
5064 *ptype = debug_make_bool_type (minfo->dhandle, 4);
5066 ++*pp;
5067 break;
5069 case 'c': /* char */
5070 if (ptype != NULL)
5072 *ptype = debug_find_named_type (minfo->dhandle,
5073 (unsignedp
5074 ? "unsigned char"
5075 : (signedp
5076 ? "signed char"
5077 : "char")));
5078 if (*ptype == DEBUG_TYPE_NULL)
5079 *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
5081 ++*pp;
5082 break;
5084 case 'w': /* wchar_t */
5085 if (ptype != NULL)
5087 *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
5088 if (*ptype == DEBUG_TYPE_NULL)
5089 *ptype = debug_make_int_type (minfo->dhandle, 2, TRUE);
5091 ++*pp;
5092 break;
5094 case 'r': /* long double */
5095 if (ptype != NULL)
5097 *ptype = debug_find_named_type (minfo->dhandle, "long double");
5098 if (*ptype == DEBUG_TYPE_NULL)
5099 *ptype = debug_make_float_type (minfo->dhandle, 8);
5101 ++*pp;
5102 break;
5104 case 'd': /* double */
5105 if (ptype != NULL)
5107 *ptype = debug_find_named_type (minfo->dhandle, "double");
5108 if (*ptype == DEBUG_TYPE_NULL)
5109 *ptype = debug_make_float_type (minfo->dhandle, 8);
5111 ++*pp;
5112 break;
5114 case 'f': /* float */
5115 if (ptype != NULL)
5117 *ptype = debug_find_named_type (minfo->dhandle, "float");
5118 if (*ptype == DEBUG_TYPE_NULL)
5119 *ptype = debug_make_float_type (minfo->dhandle, 4);
5121 ++*pp;
5122 break;
5124 case 'G':
5125 ++*pp;
5126 if (! ISDIGIT (**pp))
5128 stab_bad_demangle (orig);
5129 return FALSE;
5131 /* Fall through. */
5132 case '0': case '1': case '2': case '3': case '4':
5133 case '5': case '6': case '7': case '8': case '9':
5135 const char *hold;
5137 if (! stab_demangle_class (minfo, pp, &hold))
5138 return FALSE;
5139 if (ptype != NULL)
5141 char *name;
5143 name = savestring (hold, *pp - hold);
5144 *ptype = debug_find_named_type (minfo->dhandle, name);
5145 free (name);
5146 if (*ptype == DEBUG_TYPE_NULL)
5148 /* FIXME: It is probably incorrect to assume that
5149 undefined types are tagged types. */
5150 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5151 hold, *pp - hold,
5152 DEBUG_KIND_ILLEGAL);
5153 if (*ptype == DEBUG_TYPE_NULL)
5154 return FALSE;
5158 break;
5160 case 't':
5162 char *name;
5164 if (! stab_demangle_template (minfo, pp,
5165 ptype != NULL ? &name : NULL))
5166 return FALSE;
5167 if (ptype != NULL)
5169 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5170 name, strlen (name),
5171 DEBUG_KIND_CLASS);
5172 free (name);
5173 if (*ptype == DEBUG_TYPE_NULL)
5174 return FALSE;
5177 break;
5179 default:
5180 stab_bad_demangle (orig);
5181 return FALSE;
5184 if (ptype != NULL)
5186 if (constp)
5187 *ptype = debug_make_const_type (minfo->dhandle, *ptype);
5188 if (volatilep)
5189 *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5192 return TRUE;
5195 /* Remember a type string in a demangled string. */
5197 static bfd_boolean
5198 stab_demangle_remember_type (minfo, p, len)
5199 struct stab_demangle_info *minfo;
5200 const char *p;
5201 int len;
5203 if (minfo->typestring_count >= minfo->typestring_alloc)
5205 minfo->typestring_alloc += 10;
5206 minfo->typestrings = ((struct stab_demangle_typestring *)
5207 xrealloc (minfo->typestrings,
5208 (minfo->typestring_alloc
5209 * sizeof *minfo->typestrings)));
5212 minfo->typestrings[minfo->typestring_count].typestring = p;
5213 minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5214 ++minfo->typestring_count;
5216 return TRUE;