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