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