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