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