stabsread.c revision 1.9 1 /* Support routines for decoding "stabs" debugging information format.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Support routines for reading and decoding debugging information in
21 the "stabs" format. This format is used by some systems that use
22 COFF or ELF where the stabs data is placed in a special section (as
23 well as with many old systems that used the a.out object file
24 format). Avoid placing any object file format specific code in
25 this file. */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "gdb_obstack.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "expression.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "aout/stab_gnu.h" /* We always use GNU stabs, not native. */
36 #include "libaout.h"
37 #include "aout/aout64.h"
38 #include "gdb-stabs.h"
39 #include "buildsym-legacy.h"
40 #include "complaints.h"
41 #include "demangle.h"
42 #include "gdb-demangle.h"
43 #include "language.h"
44 #include "target-float.h"
45 #include "c-lang.h"
46 #include "cp-abi.h"
47 #include "cp-support.h"
48 #include <ctype.h>
49
50 #include "stabsread.h"
51
52 /* See stabsread.h for these globals. */
53 unsigned int symnum;
54 const char *(*next_symbol_text_func) (struct objfile *);
55 unsigned char processing_gcc_compilation;
56 int within_function;
57 struct symbol *global_sym_chain[HASHSIZE];
58 struct pending_stabs *global_stabs;
59 int previous_stab_code;
60 int *this_object_header_files;
61 int n_this_object_header_files;
62 int n_allocated_this_object_header_files;
63
64 struct nextfield
65 {
66 struct nextfield *next;
67
68 /* This is the raw visibility from the stab. It is not checked
69 for being one of the visibilities we recognize, so code which
70 examines this field better be able to deal. */
71 int visibility;
72
73 struct field field;
74 };
75
76 struct next_fnfieldlist
77 {
78 struct next_fnfieldlist *next;
79 struct fn_fieldlist fn_fieldlist;
80 };
81
82 /* The routines that read and process a complete stabs for a C struct or
83 C++ class pass lists of data member fields and lists of member function
84 fields in an instance of a field_info structure, as defined below.
85 This is part of some reorganization of low level C++ support and is
86 expected to eventually go away... (FIXME) */
87
88 struct stab_field_info
89 {
90 struct nextfield *list = nullptr;
91 struct next_fnfieldlist *fnlist = nullptr;
92
93 auto_obstack obstack;
94 };
95
96 static void
97 read_one_struct_field (struct stab_field_info *, const char **, const char *,
98 struct type *, struct objfile *);
99
100 static struct type *dbx_alloc_type (int[2], struct objfile *);
101
102 static long read_huge_number (const char **, int, int *, int);
103
104 static struct type *error_type (const char **, struct objfile *);
105
106 static void
107 patch_block_stabs (struct pending *, struct pending_stabs *,
108 struct objfile *);
109
110 static void fix_common_block (struct symbol *, CORE_ADDR);
111
112 static int read_type_number (const char **, int *);
113
114 static struct type *read_type (const char **, struct objfile *);
115
116 static struct type *read_range_type (const char **, int[2],
117 int, struct objfile *);
118
119 static struct type *read_sun_builtin_type (const char **,
120 int[2], struct objfile *);
121
122 static struct type *read_sun_floating_type (const char **, int[2],
123 struct objfile *);
124
125 static struct type *read_enum_type (const char **, struct type *, struct objfile *);
126
127 static struct type *rs6000_builtin_type (int, struct objfile *);
128
129 static int
130 read_member_functions (struct stab_field_info *, const char **, struct type *,
131 struct objfile *);
132
133 static int
134 read_struct_fields (struct stab_field_info *, const char **, struct type *,
135 struct objfile *);
136
137 static int
138 read_baseclasses (struct stab_field_info *, const char **, struct type *,
139 struct objfile *);
140
141 static int
142 read_tilde_fields (struct stab_field_info *, const char **, struct type *,
143 struct objfile *);
144
145 static int attach_fn_fields_to_type (struct stab_field_info *, struct type *);
146
147 static int attach_fields_to_type (struct stab_field_info *, struct type *,
148 struct objfile *);
149
150 static struct type *read_struct_type (const char **, struct type *,
151 enum type_code,
152 struct objfile *);
153
154 static struct type *read_array_type (const char **, struct type *,
155 struct objfile *);
156
157 static struct field *read_args (const char **, int, struct objfile *,
158 int *, int *);
159
160 static void add_undefined_type (struct type *, int[2]);
161
162 static int
163 read_cpp_abbrev (struct stab_field_info *, const char **, struct type *,
164 struct objfile *);
165
166 static const char *find_name_end (const char *name);
167
168 static int process_reference (const char **string);
169
170 void stabsread_clear_cache (void);
171
172 static const char vptr_name[] = "_vptr$";
173 static const char vb_name[] = "_vb$";
174
175 static void
176 invalid_cpp_abbrev_complaint (const char *arg1)
177 {
178 complaint (_("invalid C++ abbreviation `%s'"), arg1);
179 }
180
181 static void
182 reg_value_complaint (int regnum, int num_regs, const char *sym)
183 {
184 complaint (_("bad register number %d (max %d) in symbol %s"),
185 regnum, num_regs - 1, sym);
186 }
187
188 static void
189 stabs_general_complaint (const char *arg1)
190 {
191 complaint ("%s", arg1);
192 }
193
194 /* Make a list of forward references which haven't been defined. */
195
196 static struct type **undef_types;
197 static int undef_types_allocated;
198 static int undef_types_length;
199 static struct symbol *current_symbol = NULL;
200
201 /* Make a list of nameless types that are undefined.
202 This happens when another type is referenced by its number
203 before this type is actually defined. For instance "t(0,1)=k(0,2)"
204 and type (0,2) is defined only later. */
205
206 struct nat
207 {
208 int typenums[2];
209 struct type *type;
210 };
211 static struct nat *noname_undefs;
212 static int noname_undefs_allocated;
213 static int noname_undefs_length;
214
215 /* Check for and handle cretinous stabs symbol name continuation! */
216 #define STABS_CONTINUE(pp,objfile) \
217 do { \
218 if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
219 *(pp) = next_symbol_text (objfile); \
220 } while (0)
221
222 /* Vector of types defined so far, indexed by their type numbers.
223 (In newer sun systems, dbx uses a pair of numbers in parens,
224 as in "(SUBFILENUM,NUMWITHINSUBFILE)".
225 Then these numbers must be translated through the type_translations
226 hash table to get the index into the type vector.) */
227
228 static struct type **type_vector;
229
230 /* Number of elements allocated for type_vector currently. */
231
232 static int type_vector_length;
233
234 /* Initial size of type vector. Is realloc'd larger if needed, and
235 realloc'd down to the size actually used, when completed. */
236
237 #define INITIAL_TYPE_VECTOR_LENGTH 160
238
239
241 /* Look up a dbx type-number pair. Return the address of the slot
242 where the type for that number-pair is stored.
243 The number-pair is in TYPENUMS.
244
245 This can be used for finding the type associated with that pair
246 or for associating a new type with the pair. */
247
248 static struct type **
249 dbx_lookup_type (int typenums[2], struct objfile *objfile)
250 {
251 int filenum = typenums[0];
252 int index = typenums[1];
253 unsigned old_len;
254 int real_filenum;
255 struct header_file *f;
256 int f_orig_length;
257
258 if (filenum == -1) /* -1,-1 is for temporary types. */
259 return 0;
260
261 if (filenum < 0 || filenum >= n_this_object_header_files)
262 {
263 complaint (_("Invalid symbol data: type number "
264 "(%d,%d) out of range at symtab pos %d."),
265 filenum, index, symnum);
266 goto error_return;
267 }
268
269 if (filenum == 0)
270 {
271 if (index < 0)
272 {
273 /* Caller wants address of address of type. We think
274 that negative (rs6k builtin) types will never appear as
275 "lvalues", (nor should they), so we stuff the real type
276 pointer into a temp, and return its address. If referenced,
277 this will do the right thing. */
278 static struct type *temp_type;
279
280 temp_type = rs6000_builtin_type (index, objfile);
281 return &temp_type;
282 }
283
284 /* Type is defined outside of header files.
285 Find it in this object file's type vector. */
286 if (index >= type_vector_length)
287 {
288 old_len = type_vector_length;
289 if (old_len == 0)
290 {
291 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
292 type_vector = XNEWVEC (struct type *, type_vector_length);
293 }
294 while (index >= type_vector_length)
295 {
296 type_vector_length *= 2;
297 }
298 type_vector = (struct type **)
299 xrealloc ((char *) type_vector,
300 (type_vector_length * sizeof (struct type *)));
301 memset (&type_vector[old_len], 0,
302 (type_vector_length - old_len) * sizeof (struct type *));
303 }
304 return (&type_vector[index]);
305 }
306 else
307 {
308 real_filenum = this_object_header_files[filenum];
309
310 if (real_filenum >= N_HEADER_FILES (objfile))
311 {
312 static struct type *temp_type;
313
314 warning (_("GDB internal error: bad real_filenum"));
315
316 error_return:
317 temp_type = objfile_type (objfile)->builtin_error;
318 return &temp_type;
319 }
320
321 f = HEADER_FILES (objfile) + real_filenum;
322
323 f_orig_length = f->length;
324 if (index >= f_orig_length)
325 {
326 while (index >= f->length)
327 {
328 f->length *= 2;
329 }
330 f->vector = (struct type **)
331 xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
332 memset (&f->vector[f_orig_length], 0,
333 (f->length - f_orig_length) * sizeof (struct type *));
334 }
335 return (&f->vector[index]);
336 }
337 }
338
339 /* Make sure there is a type allocated for type numbers TYPENUMS
340 and return the type object.
341 This can create an empty (zeroed) type object.
342 TYPENUMS may be (-1, -1) to return a new type object that is not
343 put into the type vector, and so may not be referred to by number. */
344
345 static struct type *
346 dbx_alloc_type (int typenums[2], struct objfile *objfile)
347 {
348 struct type **type_addr;
349
350 if (typenums[0] == -1)
351 {
352 return (alloc_type (objfile));
353 }
354
355 type_addr = dbx_lookup_type (typenums, objfile);
356
357 /* If we are referring to a type not known at all yet,
358 allocate an empty type for it.
359 We will fill it in later if we find out how. */
360 if (*type_addr == 0)
361 {
362 *type_addr = alloc_type (objfile);
363 }
364
365 return (*type_addr);
366 }
367
368 /* Allocate a floating-point type of size BITS. */
369
370 static struct type *
371 dbx_init_float_type (struct objfile *objfile, int bits)
372 {
373 struct gdbarch *gdbarch = objfile->arch ();
374 const struct floatformat **format;
375 struct type *type;
376
377 format = gdbarch_floatformat_for_type (gdbarch, NULL, bits);
378 if (format)
379 type = init_float_type (objfile, bits, NULL, format);
380 else
381 type = init_type (objfile, TYPE_CODE_ERROR, bits, NULL);
382
383 return type;
384 }
385
386 /* for all the stabs in a given stab vector, build appropriate types
387 and fix their symbols in given symbol vector. */
388
389 static void
390 patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
391 struct objfile *objfile)
392 {
393 int ii;
394 char *name;
395 const char *pp;
396 struct symbol *sym;
397
398 if (stabs)
399 {
400 /* for all the stab entries, find their corresponding symbols and
401 patch their types! */
402
403 for (ii = 0; ii < stabs->count; ++ii)
404 {
405 name = stabs->stab[ii];
406 pp = (char *) strchr (name, ':');
407 gdb_assert (pp); /* Must find a ':' or game's over. */
408 while (pp[1] == ':')
409 {
410 pp += 2;
411 pp = (char *) strchr (pp, ':');
412 }
413 sym = find_symbol_in_list (symbols, name, pp - name);
414 if (!sym)
415 {
416 /* FIXME-maybe: it would be nice if we noticed whether
417 the variable was defined *anywhere*, not just whether
418 it is defined in this compilation unit. But neither
419 xlc or GCC seem to need such a definition, and until
420 we do psymtabs (so that the minimal symbols from all
421 compilation units are available now), I'm not sure
422 how to get the information. */
423
424 /* On xcoff, if a global is defined and never referenced,
425 ld will remove it from the executable. There is then
426 a N_GSYM stab for it, but no regular (C_EXT) symbol. */
427 sym = new (&objfile->objfile_obstack) symbol;
428 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
429 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
430 sym->set_linkage_name
431 (obstack_strndup (&objfile->objfile_obstack, name, pp - name));
432 pp += 2;
433 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
434 {
435 /* I don't think the linker does this with functions,
436 so as far as I know this is never executed.
437 But it doesn't hurt to check. */
438 SYMBOL_TYPE (sym) =
439 lookup_function_type (read_type (&pp, objfile));
440 }
441 else
442 {
443 SYMBOL_TYPE (sym) = read_type (&pp, objfile);
444 }
445 add_symbol_to_list (sym, get_global_symbols ());
446 }
447 else
448 {
449 pp += 2;
450 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
451 {
452 SYMBOL_TYPE (sym) =
453 lookup_function_type (read_type (&pp, objfile));
454 }
455 else
456 {
457 SYMBOL_TYPE (sym) = read_type (&pp, objfile);
458 }
459 }
460 }
461 }
462 }
463
464
466 /* Read a number by which a type is referred to in dbx data,
467 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
468 Just a single number N is equivalent to (0,N).
469 Return the two numbers by storing them in the vector TYPENUMS.
470 TYPENUMS will then be used as an argument to dbx_lookup_type.
471
472 Returns 0 for success, -1 for error. */
473
474 static int
475 read_type_number (const char **pp, int *typenums)
476 {
477 int nbits;
478
479 if (**pp == '(')
480 {
481 (*pp)++;
482 typenums[0] = read_huge_number (pp, ',', &nbits, 0);
483 if (nbits != 0)
484 return -1;
485 typenums[1] = read_huge_number (pp, ')', &nbits, 0);
486 if (nbits != 0)
487 return -1;
488 }
489 else
490 {
491 typenums[0] = 0;
492 typenums[1] = read_huge_number (pp, 0, &nbits, 0);
493 if (nbits != 0)
494 return -1;
495 }
496 return 0;
497 }
498
499
501 #define VISIBILITY_PRIVATE '0' /* Stabs character for private field */
502 #define VISIBILITY_PROTECTED '1' /* Stabs character for protected fld */
503 #define VISIBILITY_PUBLIC '2' /* Stabs character for public field */
504 #define VISIBILITY_IGNORE '9' /* Optimized out or zero length */
505
506 /* Structure for storing pointers to reference definitions for fast lookup
507 during "process_later". */
508
509 struct ref_map
510 {
511 const char *stabs;
512 CORE_ADDR value;
513 struct symbol *sym;
514 };
515
516 #define MAX_CHUNK_REFS 100
517 #define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
518 #define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
519
520 static struct ref_map *ref_map;
521
522 /* Ptr to free cell in chunk's linked list. */
523 static int ref_count = 0;
524
525 /* Number of chunks malloced. */
526 static int ref_chunk = 0;
527
528 /* This file maintains a cache of stabs aliases found in the symbol
529 table. If the symbol table changes, this cache must be cleared
530 or we are left holding onto data in invalid obstacks. */
531 void
532 stabsread_clear_cache (void)
533 {
534 ref_count = 0;
535 ref_chunk = 0;
536 }
537
538 /* Create array of pointers mapping refids to symbols and stab strings.
539 Add pointers to reference definition symbols and/or their values as we
540 find them, using their reference numbers as our index.
541 These will be used later when we resolve references. */
542 void
543 ref_add (int refnum, struct symbol *sym, const char *stabs, CORE_ADDR value)
544 {
545 if (ref_count == 0)
546 ref_chunk = 0;
547 if (refnum >= ref_count)
548 ref_count = refnum + 1;
549 if (ref_count > ref_chunk * MAX_CHUNK_REFS)
550 {
551 int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
552 int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
553
554 ref_map = (struct ref_map *)
555 xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
556 memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0,
557 new_chunks * REF_CHUNK_SIZE);
558 ref_chunk += new_chunks;
559 }
560 ref_map[refnum].stabs = stabs;
561 ref_map[refnum].sym = sym;
562 ref_map[refnum].value = value;
563 }
564
565 /* Return defined sym for the reference REFNUM. */
566 struct symbol *
567 ref_search (int refnum)
568 {
569 if (refnum < 0 || refnum > ref_count)
570 return 0;
571 return ref_map[refnum].sym;
572 }
573
574 /* Parse a reference id in STRING and return the resulting
575 reference number. Move STRING beyond the reference id. */
576
577 static int
578 process_reference (const char **string)
579 {
580 const char *p;
581 int refnum = 0;
582
583 if (**string != '#')
584 return 0;
585
586 /* Advance beyond the initial '#'. */
587 p = *string + 1;
588
589 /* Read number as reference id. */
590 while (*p && isdigit (*p))
591 {
592 refnum = refnum * 10 + *p - '0';
593 p++;
594 }
595 *string = p;
596 return refnum;
597 }
598
599 /* If STRING defines a reference, store away a pointer to the reference
600 definition for later use. Return the reference number. */
601
602 int
603 symbol_reference_defined (const char **string)
604 {
605 const char *p = *string;
606 int refnum = 0;
607
608 refnum = process_reference (&p);
609
610 /* Defining symbols end in '='. */
611 if (*p == '=')
612 {
613 /* Symbol is being defined here. */
614 *string = p + 1;
615 return refnum;
616 }
617 else
618 {
619 /* Must be a reference. Either the symbol has already been defined,
620 or this is a forward reference to it. */
621 *string = p;
622 return -1;
623 }
624 }
625
626 static int
627 stab_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
628 {
629 int regno = gdbarch_stab_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
630
631 if (regno < 0 || regno >= gdbarch_num_cooked_regs (gdbarch))
632 {
633 reg_value_complaint (regno, gdbarch_num_cooked_regs (gdbarch),
634 sym->print_name ());
635
636 regno = gdbarch_sp_regnum (gdbarch); /* Known safe, though useless. */
637 }
638
639 return regno;
640 }
641
642 static const struct symbol_register_ops stab_register_funcs = {
643 stab_reg_to_regnum
644 };
645
646 /* The "aclass" indices for computed symbols. */
647
648 static int stab_register_index;
649 static int stab_regparm_index;
650
651 struct symbol *
652 define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
653 struct objfile *objfile)
654 {
655 struct gdbarch *gdbarch = objfile->arch ();
656 struct symbol *sym;
657 const char *p = find_name_end (string);
658 int deftype;
659 int synonym = 0;
660 int i;
661
662 /* We would like to eliminate nameless symbols, but keep their types.
663 E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
664 to type 2, but, should not create a symbol to address that type. Since
665 the symbol will be nameless, there is no way any user can refer to it. */
666
667 int nameless;
668
669 /* Ignore syms with empty names. */
670 if (string[0] == 0)
671 return 0;
672
673 /* Ignore old-style symbols from cc -go. */
674 if (p == 0)
675 return 0;
676
677 while (p[1] == ':')
678 {
679 p += 2;
680 p = strchr (p, ':');
681 if (p == NULL)
682 {
683 complaint (
684 _("Bad stabs string '%s'"), string);
685 return NULL;
686 }
687 }
688
689 /* If a nameless stab entry, all we need is the type, not the symbol.
690 e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
691 nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
692
693 current_symbol = sym = new (&objfile->objfile_obstack) symbol;
694
695 if (processing_gcc_compilation)
696 {
697 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
698 number of bytes occupied by a type or object, which we ignore. */
699 SYMBOL_LINE (sym) = desc;
700 }
701 else
702 {
703 SYMBOL_LINE (sym) = 0; /* unknown */
704 }
705
706 sym->set_language (get_current_subfile ()->language,
707 &objfile->objfile_obstack);
708
709 if (is_cplus_marker (string[0]))
710 {
711 /* Special GNU C++ names. */
712 switch (string[1])
713 {
714 case 't':
715 sym->set_linkage_name ("this");
716 break;
717
718 case 'v': /* $vtbl_ptr_type */
719 goto normal;
720
721 case 'e':
722 sym->set_linkage_name ("eh_throw");
723 break;
724
725 case '_':
726 /* This was an anonymous type that was never fixed up. */
727 goto normal;
728
729 default:
730 complaint (_("Unknown C++ symbol name `%s'"),
731 string);
732 goto normal; /* Do *something* with it. */
733 }
734 }
735 else
736 {
737 normal:
738 gdb::unique_xmalloc_ptr<char> new_name;
739
740 if (sym->language () == language_cplus)
741 {
742 char *name = (char *) alloca (p - string + 1);
743
744 memcpy (name, string, p - string);
745 name[p - string] = '\0';
746 new_name = cp_canonicalize_string (name);
747 }
748 if (new_name != nullptr)
749 sym->compute_and_set_names (new_name.get (), true, objfile->per_bfd);
750 else
751 sym->compute_and_set_names (gdb::string_view (string, p - string), true,
752 objfile->per_bfd);
753
754 if (sym->language () == language_cplus)
755 cp_scan_for_anonymous_namespaces (get_buildsym_compunit (), sym,
756 objfile);
757
758 }
759 p++;
760
761 /* Determine the type of name being defined. */
762 #if 0
763 /* Getting GDB to correctly skip the symbol on an undefined symbol
764 descriptor and not ever dump core is a very dodgy proposition if
765 we do things this way. I say the acorn RISC machine can just
766 fix their compiler. */
767 /* The Acorn RISC machine's compiler can put out locals that don't
768 start with "234=" or "(3,4)=", so assume anything other than the
769 deftypes we know how to handle is a local. */
770 if (!strchr ("cfFGpPrStTvVXCR", *p))
771 #else
772 if (isdigit (*p) || *p == '(' || *p == '-')
773 #endif
774 deftype = 'l';
775 else
776 deftype = *p++;
777
778 switch (deftype)
779 {
780 case 'c':
781 /* c is a special case, not followed by a type-number.
782 SYMBOL:c=iVALUE for an integer constant symbol.
783 SYMBOL:c=rVALUE for a floating constant symbol.
784 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
785 e.g. "b:c=e6,0" for "const b = blob1"
786 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
787 if (*p != '=')
788 {
789 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
790 SYMBOL_TYPE (sym) = error_type (&p, objfile);
791 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
792 add_symbol_to_list (sym, get_file_symbols ());
793 return sym;
794 }
795 ++p;
796 switch (*p++)
797 {
798 case 'r':
799 {
800 gdb_byte *dbl_valu;
801 struct type *dbl_type;
802
803 dbl_type = objfile_type (objfile)->builtin_double;
804 dbl_valu
805 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
806 TYPE_LENGTH (dbl_type));
807
808 target_float_from_string (dbl_valu, dbl_type, std::string (p));
809
810 SYMBOL_TYPE (sym) = dbl_type;
811 SYMBOL_VALUE_BYTES (sym) = dbl_valu;
812 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
813 }
814 break;
815 case 'i':
816 {
817 /* Defining integer constants this way is kind of silly,
818 since 'e' constants allows the compiler to give not
819 only the value, but the type as well. C has at least
820 int, long, unsigned int, and long long as constant
821 types; other languages probably should have at least
822 unsigned as well as signed constants. */
823
824 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_long;
825 SYMBOL_VALUE (sym) = atoi (p);
826 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
827 }
828 break;
829
830 case 'c':
831 {
832 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_char;
833 SYMBOL_VALUE (sym) = atoi (p);
834 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
835 }
836 break;
837
838 case 's':
839 {
840 struct type *range_type;
841 int ind = 0;
842 char quote = *p++;
843 gdb_byte *string_local = (gdb_byte *) alloca (strlen (p));
844 gdb_byte *string_value;
845
846 if (quote != '\'' && quote != '"')
847 {
848 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
849 SYMBOL_TYPE (sym) = error_type (&p, objfile);
850 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
851 add_symbol_to_list (sym, get_file_symbols ());
852 return sym;
853 }
854
855 /* Find matching quote, rejecting escaped quotes. */
856 while (*p && *p != quote)
857 {
858 if (*p == '\\' && p[1] == quote)
859 {
860 string_local[ind] = (gdb_byte) quote;
861 ind++;
862 p += 2;
863 }
864 else if (*p)
865 {
866 string_local[ind] = (gdb_byte) (*p);
867 ind++;
868 p++;
869 }
870 }
871 if (*p != quote)
872 {
873 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
874 SYMBOL_TYPE (sym) = error_type (&p, objfile);
875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
876 add_symbol_to_list (sym, get_file_symbols ());
877 return sym;
878 }
879
880 /* NULL terminate the string. */
881 string_local[ind] = 0;
882 range_type
883 = create_static_range_type (NULL,
884 objfile_type (objfile)->builtin_int,
885 0, ind);
886 SYMBOL_TYPE (sym) = create_array_type (NULL,
887 objfile_type (objfile)->builtin_char,
888 range_type);
889 string_value
890 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, ind + 1);
891 memcpy (string_value, string_local, ind + 1);
892 p++;
893
894 SYMBOL_VALUE_BYTES (sym) = string_value;
895 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
896 }
897 break;
898
899 case 'e':
900 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
901 can be represented as integral.
902 e.g. "b:c=e6,0" for "const b = blob1"
903 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
904 {
905 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
906 SYMBOL_TYPE (sym) = read_type (&p, objfile);
907
908 if (*p != ',')
909 {
910 SYMBOL_TYPE (sym) = error_type (&p, objfile);
911 break;
912 }
913 ++p;
914
915 /* If the value is too big to fit in an int (perhaps because
916 it is unsigned), or something like that, we silently get
917 a bogus value. The type and everything else about it is
918 correct. Ideally, we should be using whatever we have
919 available for parsing unsigned and long long values,
920 however. */
921 SYMBOL_VALUE (sym) = atoi (p);
922 }
923 break;
924 default:
925 {
926 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
927 SYMBOL_TYPE (sym) = error_type (&p, objfile);
928 }
929 }
930 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
931 add_symbol_to_list (sym, get_file_symbols ());
932 return sym;
933
934 case 'C':
935 /* The name of a caught exception. */
936 SYMBOL_TYPE (sym) = read_type (&p, objfile);
937 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
938 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
939 SET_SYMBOL_VALUE_ADDRESS (sym, valu);
940 add_symbol_to_list (sym, get_local_symbols ());
941 break;
942
943 case 'f':
944 /* A static function definition. */
945 SYMBOL_TYPE (sym) = read_type (&p, objfile);
946 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
947 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
948 add_symbol_to_list (sym, get_file_symbols ());
949 /* fall into process_function_types. */
950
951 process_function_types:
952 /* Function result types are described as the result type in stabs.
953 We need to convert this to the function-returning-type-X type
954 in GDB. E.g. "int" is converted to "function returning int". */
955 if (SYMBOL_TYPE (sym)->code () != TYPE_CODE_FUNC)
956 SYMBOL_TYPE (sym) = lookup_function_type (SYMBOL_TYPE (sym));
957
958 /* All functions in C++ have prototypes. Stabs does not offer an
959 explicit way to identify prototyped or unprototyped functions,
960 but both GCC and Sun CC emit stabs for the "call-as" type rather
961 than the "declared-as" type for unprototyped functions, so
962 we treat all functions as if they were prototyped. This is used
963 primarily for promotion when calling the function from GDB. */
964 TYPE_PROTOTYPED (SYMBOL_TYPE (sym)) = 1;
965
966 /* fall into process_prototype_types. */
967
968 process_prototype_types:
969 /* Sun acc puts declared types of arguments here. */
970 if (*p == ';')
971 {
972 struct type *ftype = SYMBOL_TYPE (sym);
973 int nsemi = 0;
974 int nparams = 0;
975 const char *p1 = p;
976
977 /* Obtain a worst case guess for the number of arguments
978 by counting the semicolons. */
979 while (*p1)
980 {
981 if (*p1++ == ';')
982 nsemi++;
983 }
984
985 /* Allocate parameter information fields and fill them in. */
986 ftype->set_fields
987 ((struct field *)
988 TYPE_ALLOC (ftype, nsemi * sizeof (struct field)));
989 while (*p++ == ';')
990 {
991 struct type *ptype;
992
993 /* A type number of zero indicates the start of varargs.
994 FIXME: GDB currently ignores vararg functions. */
995 if (p[0] == '0' && p[1] == '\0')
996 break;
997 ptype = read_type (&p, objfile);
998
999 /* The Sun compilers mark integer arguments, which should
1000 be promoted to the width of the calling conventions, with
1001 a type which references itself. This type is turned into
1002 a TYPE_CODE_VOID type by read_type, and we have to turn
1003 it back into builtin_int here.
1004 FIXME: Do we need a new builtin_promoted_int_arg ? */
1005 if (ptype->code () == TYPE_CODE_VOID)
1006 ptype = objfile_type (objfile)->builtin_int;
1007 ftype->field (nparams).set_type (ptype);
1008 TYPE_FIELD_ARTIFICIAL (ftype, nparams++) = 0;
1009 }
1010 ftype->set_num_fields (nparams);
1011 TYPE_PROTOTYPED (ftype) = 1;
1012 }
1013 break;
1014
1015 case 'F':
1016 /* A global function definition. */
1017 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1018 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
1019 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1020 add_symbol_to_list (sym, get_global_symbols ());
1021 goto process_function_types;
1022
1023 case 'G':
1024 /* For a class G (global) symbol, it appears that the
1025 value is not correct. It is necessary to search for the
1026 corresponding linker definition to find the value.
1027 These definitions appear at the end of the namelist. */
1028 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1029 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
1030 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1031 /* Don't add symbol references to global_sym_chain.
1032 Symbol references don't have valid names and wont't match up with
1033 minimal symbols when the global_sym_chain is relocated.
1034 We'll fixup symbol references when we fixup the defining symbol. */
1035 if (sym->linkage_name () && sym->linkage_name ()[0] != '#')
1036 {
1037 i = hashname (sym->linkage_name ());
1038 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1039 global_sym_chain[i] = sym;
1040 }
1041 add_symbol_to_list (sym, get_global_symbols ());
1042 break;
1043
1044 /* This case is faked by a conditional above,
1045 when there is no code letter in the dbx data.
1046 Dbx data never actually contains 'l'. */
1047 case 's':
1048 case 'l':
1049 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1050 SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
1051 SYMBOL_VALUE (sym) = valu;
1052 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1053 add_symbol_to_list (sym, get_local_symbols ());
1054 break;
1055
1056 case 'p':
1057 if (*p == 'F')
1058 /* pF is a two-letter code that means a function parameter in Fortran.
1059 The type-number specifies the type of the return value.
1060 Translate it into a pointer-to-function type. */
1061 {
1062 p++;
1063 SYMBOL_TYPE (sym)
1064 = lookup_pointer_type
1065 (lookup_function_type (read_type (&p, objfile)));
1066 }
1067 else
1068 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1069
1070 SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
1071 SYMBOL_VALUE (sym) = valu;
1072 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1073 SYMBOL_IS_ARGUMENT (sym) = 1;
1074 add_symbol_to_list (sym, get_local_symbols ());
1075
1076 if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
1077 {
1078 /* On little-endian machines, this crud is never necessary,
1079 and, if the extra bytes contain garbage, is harmful. */
1080 break;
1081 }
1082
1083 /* If it's gcc-compiled, if it says `short', believe it. */
1084 if (processing_gcc_compilation
1085 || gdbarch_believe_pcc_promotion (gdbarch))
1086 break;
1087
1088 if (!gdbarch_believe_pcc_promotion (gdbarch))
1089 {
1090 /* If PCC says a parameter is a short or a char, it is
1091 really an int. */
1092 if (TYPE_LENGTH (SYMBOL_TYPE (sym))
1093 < gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT
1094 && SYMBOL_TYPE (sym)->code () == TYPE_CODE_INT)
1095 {
1096 SYMBOL_TYPE (sym) =
1097 TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1098 ? objfile_type (objfile)->builtin_unsigned_int
1099 : objfile_type (objfile)->builtin_int;
1100 }
1101 break;
1102 }
1103 /* Fall through. */
1104
1105 case 'P':
1106 /* acc seems to use P to declare the prototypes of functions that
1107 are referenced by this file. gdb is not prepared to deal
1108 with this extra information. FIXME, it ought to. */
1109 if (type == N_FUN)
1110 {
1111 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1112 goto process_prototype_types;
1113 }
1114 /*FALLTHROUGH */
1115
1116 case 'R':
1117 /* Parameter which is in a register. */
1118 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1119 SYMBOL_ACLASS_INDEX (sym) = stab_register_index;
1120 SYMBOL_IS_ARGUMENT (sym) = 1;
1121 SYMBOL_VALUE (sym) = valu;
1122 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1123 add_symbol_to_list (sym, get_local_symbols ());
1124 break;
1125
1126 case 'r':
1127 /* Register variable (either global or local). */
1128 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1129 SYMBOL_ACLASS_INDEX (sym) = stab_register_index;
1130 SYMBOL_VALUE (sym) = valu;
1131 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1132 if (within_function)
1133 {
1134 /* Sun cc uses a pair of symbols, one 'p' and one 'r', with
1135 the same name to represent an argument passed in a
1136 register. GCC uses 'P' for the same case. So if we find
1137 such a symbol pair we combine it into one 'P' symbol.
1138 For Sun cc we need to do this regardless of stabs_argument_has_addr, because the compiler puts out
1139 the 'p' symbol even if it never saves the argument onto
1140 the stack.
1141
1142 On most machines, we want to preserve both symbols, so
1143 that we can still get information about what is going on
1144 with the stack (VAX for computing args_printed, using
1145 stack slots instead of saved registers in backtraces,
1146 etc.).
1147
1148 Note that this code illegally combines
1149 main(argc) struct foo argc; { register struct foo argc; }
1150 but this case is considered pathological and causes a warning
1151 from a decent compiler. */
1152
1153 struct pending *local_symbols = *get_local_symbols ();
1154 if (local_symbols
1155 && local_symbols->nsyms > 0
1156 && gdbarch_stabs_argument_has_addr (gdbarch, SYMBOL_TYPE (sym)))
1157 {
1158 struct symbol *prev_sym;
1159
1160 prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
1161 if ((SYMBOL_CLASS (prev_sym) == LOC_REF_ARG
1162 || SYMBOL_CLASS (prev_sym) == LOC_ARG)
1163 && strcmp (prev_sym->linkage_name (),
1164 sym->linkage_name ()) == 0)
1165 {
1166 SYMBOL_ACLASS_INDEX (prev_sym) = stab_register_index;
1167 /* Use the type from the LOC_REGISTER; that is the type
1168 that is actually in that register. */
1169 SYMBOL_TYPE (prev_sym) = SYMBOL_TYPE (sym);
1170 SYMBOL_VALUE (prev_sym) = SYMBOL_VALUE (sym);
1171 sym = prev_sym;
1172 break;
1173 }
1174 }
1175 add_symbol_to_list (sym, get_local_symbols ());
1176 }
1177 else
1178 add_symbol_to_list (sym, get_file_symbols ());
1179 break;
1180
1181 case 'S':
1182 /* Static symbol at top level of file. */
1183 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1184 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
1185 SET_SYMBOL_VALUE_ADDRESS (sym, valu);
1186 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1187 add_symbol_to_list (sym, get_file_symbols ());
1188 break;
1189
1190 case 't':
1191 /* In Ada, there is no distinction between typedef and non-typedef;
1192 any type declaration implicitly has the equivalent of a typedef,
1193 and thus 't' is in fact equivalent to 'Tt'.
1194
1195 Therefore, for Ada units, we check the character immediately
1196 before the 't', and if we do not find a 'T', then make sure to
1197 create the associated symbol in the STRUCT_DOMAIN ('t' definitions
1198 will be stored in the VAR_DOMAIN). If the symbol was indeed
1199 defined as 'Tt' then the STRUCT_DOMAIN symbol will be created
1200 elsewhere, so we don't need to take care of that.
1201
1202 This is important to do, because of forward references:
1203 The cleanup of undefined types stored in undef_types only uses
1204 STRUCT_DOMAIN symbols to perform the replacement. */
1205 synonym = (sym->language () == language_ada && p[-2] != 'T');
1206
1207 /* Typedef */
1208 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1209
1210 /* For a nameless type, we don't want a create a symbol, thus we
1211 did not use `sym'. Return without further processing. */
1212 if (nameless)
1213 return NULL;
1214
1215 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
1216 SYMBOL_VALUE (sym) = valu;
1217 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1218 /* C++ vagaries: we may have a type which is derived from
1219 a base type which did not have its name defined when the
1220 derived class was output. We fill in the derived class's
1221 base part member's name here in that case. */
1222 if (SYMBOL_TYPE (sym)->name () != NULL)
1223 if ((SYMBOL_TYPE (sym)->code () == TYPE_CODE_STRUCT
1224 || SYMBOL_TYPE (sym)->code () == TYPE_CODE_UNION)
1225 && TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)))
1226 {
1227 int j;
1228
1229 for (j = TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)) - 1; j >= 0; j--)
1230 if (TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) == 0)
1231 TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) =
1232 TYPE_BASECLASS (SYMBOL_TYPE (sym), j)->name ();
1233 }
1234
1235 if (SYMBOL_TYPE (sym)->name () == NULL)
1236 {
1237 if ((SYMBOL_TYPE (sym)->code () == TYPE_CODE_PTR
1238 && strcmp (sym->linkage_name (), vtbl_ptr_name))
1239 || SYMBOL_TYPE (sym)->code () == TYPE_CODE_FUNC)
1240 {
1241 /* If we are giving a name to a type such as "pointer to
1242 foo" or "function returning foo", we better not set
1243 the TYPE_NAME. If the program contains "typedef char
1244 *caddr_t;", we don't want all variables of type char
1245 * to print as caddr_t. This is not just a
1246 consequence of GDB's type management; PCC and GCC (at
1247 least through version 2.4) both output variables of
1248 either type char * or caddr_t with the type number
1249 defined in the 't' symbol for caddr_t. If a future
1250 compiler cleans this up it GDB is not ready for it
1251 yet, but if it becomes ready we somehow need to
1252 disable this check (without breaking the PCC/GCC2.4
1253 case).
1254
1255 Sigh.
1256
1257 Fortunately, this check seems not to be necessary
1258 for anything except pointers or functions. */
1259 /* ezannoni: 2000-10-26. This seems to apply for
1260 versions of gcc older than 2.8. This was the original
1261 problem: with the following code gdb would tell that
1262 the type for name1 is caddr_t, and func is char().
1263
1264 typedef char *caddr_t;
1265 char *name2;
1266 struct x
1267 {
1268 char *name1;
1269 } xx;
1270 char *func()
1271 {
1272 }
1273 main () {}
1274 */
1275
1276 /* Pascal accepts names for pointer types. */
1277 if (get_current_subfile ()->language == language_pascal)
1278 SYMBOL_TYPE (sym)->set_name (sym->linkage_name ());
1279 }
1280 else
1281 SYMBOL_TYPE (sym)->set_name (sym->linkage_name ());
1282 }
1283
1284 add_symbol_to_list (sym, get_file_symbols ());
1285
1286 if (synonym)
1287 {
1288 /* Create the STRUCT_DOMAIN clone. */
1289 struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;
1290
1291 *struct_sym = *sym;
1292 SYMBOL_ACLASS_INDEX (struct_sym) = LOC_TYPEDEF;
1293 SYMBOL_VALUE (struct_sym) = valu;
1294 SYMBOL_DOMAIN (struct_sym) = STRUCT_DOMAIN;
1295 if (SYMBOL_TYPE (sym)->name () == 0)
1296 SYMBOL_TYPE (sym)->set_name
1297 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1298 (char *) NULL));
1299 add_symbol_to_list (struct_sym, get_file_symbols ());
1300 }
1301
1302 break;
1303
1304 case 'T':
1305 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1306 by 't' which means we are typedef'ing it as well. */
1307 synonym = *p == 't';
1308
1309 if (synonym)
1310 p++;
1311
1312 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1313
1314 /* For a nameless type, we don't want a create a symbol, thus we
1315 did not use `sym'. Return without further processing. */
1316 if (nameless)
1317 return NULL;
1318
1319 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
1320 SYMBOL_VALUE (sym) = valu;
1321 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
1322 if (SYMBOL_TYPE (sym)->name () == 0)
1323 SYMBOL_TYPE (sym)->set_name
1324 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1325 (char *) NULL));
1326 add_symbol_to_list (sym, get_file_symbols ());
1327
1328 if (synonym)
1329 {
1330 /* Clone the sym and then modify it. */
1331 struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;
1332
1333 *typedef_sym = *sym;
1334 SYMBOL_ACLASS_INDEX (typedef_sym) = LOC_TYPEDEF;
1335 SYMBOL_VALUE (typedef_sym) = valu;
1336 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
1337 if (SYMBOL_TYPE (sym)->name () == 0)
1338 SYMBOL_TYPE (sym)->set_name
1339 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1340 (char *) NULL));
1341 add_symbol_to_list (typedef_sym, get_file_symbols ());
1342 }
1343 break;
1344
1345 case 'V':
1346 /* Static symbol of local scope. */
1347 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1348 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
1349 SET_SYMBOL_VALUE_ADDRESS (sym, valu);
1350 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1351 add_symbol_to_list (sym, get_local_symbols ());
1352 break;
1353
1354 case 'v':
1355 /* Reference parameter */
1356 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1357 SYMBOL_ACLASS_INDEX (sym) = LOC_REF_ARG;
1358 SYMBOL_IS_ARGUMENT (sym) = 1;
1359 SYMBOL_VALUE (sym) = valu;
1360 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1361 add_symbol_to_list (sym, get_local_symbols ());
1362 break;
1363
1364 case 'a':
1365 /* Reference parameter which is in a register. */
1366 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1367 SYMBOL_ACLASS_INDEX (sym) = stab_regparm_index;
1368 SYMBOL_IS_ARGUMENT (sym) = 1;
1369 SYMBOL_VALUE (sym) = valu;
1370 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1371 add_symbol_to_list (sym, get_local_symbols ());
1372 break;
1373
1374 case 'X':
1375 /* This is used by Sun FORTRAN for "function result value".
1376 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1377 that Pascal uses it too, but when I tried it Pascal used
1378 "x:3" (local symbol) instead. */
1379 SYMBOL_TYPE (sym) = read_type (&p, objfile);
1380 SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
1381 SYMBOL_VALUE (sym) = valu;
1382 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1383 add_symbol_to_list (sym, get_local_symbols ());
1384 break;
1385
1386 default:
1387 SYMBOL_TYPE (sym) = error_type (&p, objfile);
1388 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
1389 SYMBOL_VALUE (sym) = 0;
1390 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1391 add_symbol_to_list (sym, get_file_symbols ());
1392 break;
1393 }
1394
1395 /* Some systems pass variables of certain types by reference instead
1396 of by value, i.e. they will pass the address of a structure (in a
1397 register or on the stack) instead of the structure itself. */
1398
1399 if (gdbarch_stabs_argument_has_addr (gdbarch, SYMBOL_TYPE (sym))
1400 && SYMBOL_IS_ARGUMENT (sym))
1401 {
1402 /* We have to convert LOC_REGISTER to LOC_REGPARM_ADDR (for
1403 variables passed in a register). */
1404 if (SYMBOL_CLASS (sym) == LOC_REGISTER)
1405 SYMBOL_ACLASS_INDEX (sym) = LOC_REGPARM_ADDR;
1406 /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
1407 and subsequent arguments on SPARC, for example). */
1408 else if (SYMBOL_CLASS (sym) == LOC_ARG)
1409 SYMBOL_ACLASS_INDEX (sym) = LOC_REF_ARG;
1410 }
1411
1412 return sym;
1413 }
1414
1415 /* Skip rest of this symbol and return an error type.
1416
1417 General notes on error recovery: error_type always skips to the
1418 end of the symbol (modulo cretinous dbx symbol name continuation).
1419 Thus code like this:
1420
1421 if (*(*pp)++ != ';')
1422 return error_type (pp, objfile);
1423
1424 is wrong because if *pp starts out pointing at '\0' (typically as the
1425 result of an earlier error), it will be incremented to point to the
1426 start of the next symbol, which might produce strange results, at least
1427 if you run off the end of the string table. Instead use
1428
1429 if (**pp != ';')
1430 return error_type (pp, objfile);
1431 ++*pp;
1432
1433 or
1434
1435 if (**pp != ';')
1436 foo = error_type (pp, objfile);
1437 else
1438 ++*pp;
1439
1440 And in case it isn't obvious, the point of all this hair is so the compiler
1441 can define new types and new syntaxes, and old versions of the
1442 debugger will be able to read the new symbol tables. */
1443
1444 static struct type *
1445 error_type (const char **pp, struct objfile *objfile)
1446 {
1447 complaint (_("couldn't parse type; debugger out of date?"));
1448 while (1)
1449 {
1450 /* Skip to end of symbol. */
1451 while (**pp != '\0')
1452 {
1453 (*pp)++;
1454 }
1455
1456 /* Check for and handle cretinous dbx symbol name continuation! */
1457 if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
1458 {
1459 *pp = next_symbol_text (objfile);
1460 }
1461 else
1462 {
1463 break;
1464 }
1465 }
1466 return objfile_type (objfile)->builtin_error;
1467 }
1468
1469
1471 /* Read type information or a type definition; return the type. Even
1472 though this routine accepts either type information or a type
1473 definition, the distinction is relevant--some parts of stabsread.c
1474 assume that type information starts with a digit, '-', or '(' in
1475 deciding whether to call read_type. */
1476
1477 static struct type *
1478 read_type (const char **pp, struct objfile *objfile)
1479 {
1480 struct type *type = 0;
1481 struct type *type1;
1482 int typenums[2];
1483 char type_descriptor;
1484
1485 /* Size in bits of type if specified by a type attribute, or -1 if
1486 there is no size attribute. */
1487 int type_size = -1;
1488
1489 /* Used to distinguish string and bitstring from char-array and set. */
1490 int is_string = 0;
1491
1492 /* Used to distinguish vector from array. */
1493 int is_vector = 0;
1494
1495 /* Read type number if present. The type number may be omitted.
1496 for instance in a two-dimensional array declared with type
1497 "ar1;1;10;ar1;1;10;4". */
1498 if ((**pp >= '0' && **pp <= '9')
1499 || **pp == '('
1500 || **pp == '-')
1501 {
1502 if (read_type_number (pp, typenums) != 0)
1503 return error_type (pp, objfile);
1504
1505 if (**pp != '=')
1506 {
1507 /* Type is not being defined here. Either it already
1508 exists, or this is a forward reference to it.
1509 dbx_alloc_type handles both cases. */
1510 type = dbx_alloc_type (typenums, objfile);
1511
1512 /* If this is a forward reference, arrange to complain if it
1513 doesn't get patched up by the time we're done
1514 reading. */
1515 if (type->code () == TYPE_CODE_UNDEF)
1516 add_undefined_type (type, typenums);
1517
1518 return type;
1519 }
1520
1521 /* Type is being defined here. */
1522 /* Skip the '='.
1523 Also skip the type descriptor - we get it below with (*pp)[-1]. */
1524 (*pp) += 2;
1525 }
1526 else
1527 {
1528 /* 'typenums=' not present, type is anonymous. Read and return
1529 the definition, but don't put it in the type vector. */
1530 typenums[0] = typenums[1] = -1;
1531 (*pp)++;
1532 }
1533
1534 again:
1535 type_descriptor = (*pp)[-1];
1536 switch (type_descriptor)
1537 {
1538 case 'x':
1539 {
1540 enum type_code code;
1541
1542 /* Used to index through file_symbols. */
1543 struct pending *ppt;
1544 int i;
1545
1546 /* Name including "struct", etc. */
1547 char *type_name;
1548
1549 {
1550 const char *from, *p, *q1, *q2;
1551
1552 /* Set the type code according to the following letter. */
1553 switch ((*pp)[0])
1554 {
1555 case 's':
1556 code = TYPE_CODE_STRUCT;
1557 break;
1558 case 'u':
1559 code = TYPE_CODE_UNION;
1560 break;
1561 case 'e':
1562 code = TYPE_CODE_ENUM;
1563 break;
1564 default:
1565 {
1566 /* Complain and keep going, so compilers can invent new
1567 cross-reference types. */
1568 complaint (_("Unrecognized cross-reference type `%c'"),
1569 (*pp)[0]);
1570 code = TYPE_CODE_STRUCT;
1571 break;
1572 }
1573 }
1574
1575 q1 = strchr (*pp, '<');
1576 p = strchr (*pp, ':');
1577 if (p == NULL)
1578 return error_type (pp, objfile);
1579 if (q1 && p > q1 && p[1] == ':')
1580 {
1581 int nesting_level = 0;
1582
1583 for (q2 = q1; *q2; q2++)
1584 {
1585 if (*q2 == '<')
1586 nesting_level++;
1587 else if (*q2 == '>')
1588 nesting_level--;
1589 else if (*q2 == ':' && nesting_level == 0)
1590 break;
1591 }
1592 p = q2;
1593 if (*p != ':')
1594 return error_type (pp, objfile);
1595 }
1596 type_name = NULL;
1597 if (get_current_subfile ()->language == language_cplus)
1598 {
1599 char *name = (char *) alloca (p - *pp + 1);
1600
1601 memcpy (name, *pp, p - *pp);
1602 name[p - *pp] = '\0';
1603
1604 gdb::unique_xmalloc_ptr<char> new_name = cp_canonicalize_string (name);
1605 if (new_name != nullptr)
1606 type_name = obstack_strdup (&objfile->objfile_obstack,
1607 new_name.get ());
1608 }
1609 if (type_name == NULL)
1610 {
1611 char *to = type_name = (char *)
1612 obstack_alloc (&objfile->objfile_obstack, p - *pp + 1);
1613
1614 /* Copy the name. */
1615 from = *pp + 1;
1616 while (from < p)
1617 *to++ = *from++;
1618 *to = '\0';
1619 }
1620
1621 /* Set the pointer ahead of the name which we just read, and
1622 the colon. */
1623 *pp = p + 1;
1624 }
1625
1626 /* If this type has already been declared, then reuse the same
1627 type, rather than allocating a new one. This saves some
1628 memory. */
1629
1630 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
1631 for (i = 0; i < ppt->nsyms; i++)
1632 {
1633 struct symbol *sym = ppt->symbol[i];
1634
1635 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
1636 && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
1637 && (SYMBOL_TYPE (sym)->code () == code)
1638 && strcmp (sym->linkage_name (), type_name) == 0)
1639 {
1640 obstack_free (&objfile->objfile_obstack, type_name);
1641 type = SYMBOL_TYPE (sym);
1642 if (typenums[0] != -1)
1643 *dbx_lookup_type (typenums, objfile) = type;
1644 return type;
1645 }
1646 }
1647
1648 /* Didn't find the type to which this refers, so we must
1649 be dealing with a forward reference. Allocate a type
1650 structure for it, and keep track of it so we can
1651 fill in the rest of the fields when we get the full
1652 type. */
1653 type = dbx_alloc_type (typenums, objfile);
1654 type->set_code (code);
1655 type->set_name (type_name);
1656 INIT_CPLUS_SPECIFIC (type);
1657 TYPE_STUB (type) = 1;
1658
1659 add_undefined_type (type, typenums);
1660 return type;
1661 }
1662
1663 case '-': /* RS/6000 built-in type */
1664 case '0':
1665 case '1':
1666 case '2':
1667 case '3':
1668 case '4':
1669 case '5':
1670 case '6':
1671 case '7':
1672 case '8':
1673 case '9':
1674 case '(':
1675 (*pp)--;
1676
1677 /* We deal with something like t(1,2)=(3,4)=... which
1678 the Lucid compiler and recent gcc versions (post 2.7.3) use. */
1679
1680 /* Allocate and enter the typedef type first.
1681 This handles recursive types. */
1682 type = dbx_alloc_type (typenums, objfile);
1683 type->set_code (TYPE_CODE_TYPEDEF);
1684 {
1685 struct type *xtype = read_type (pp, objfile);
1686
1687 if (type == xtype)
1688 {
1689 /* It's being defined as itself. That means it is "void". */
1690 type->set_code (TYPE_CODE_VOID);
1691 TYPE_LENGTH (type) = 1;
1692 }
1693 else if (type_size >= 0 || is_string)
1694 {
1695 /* This is the absolute wrong way to construct types. Every
1696 other debug format has found a way around this problem and
1697 the related problems with unnecessarily stubbed types;
1698 someone motivated should attempt to clean up the issue
1699 here as well. Once a type pointed to has been created it
1700 should not be modified.
1701
1702 Well, it's not *absolutely* wrong. Constructing recursive
1703 types (trees, linked lists) necessarily entails modifying
1704 types after creating them. Constructing any loop structure
1705 entails side effects. The Dwarf 2 reader does handle this
1706 more gracefully (it never constructs more than once
1707 instance of a type object, so it doesn't have to copy type
1708 objects wholesale), but it still mutates type objects after
1709 other folks have references to them.
1710
1711 Keep in mind that this circularity/mutation issue shows up
1712 at the source language level, too: C's "incomplete types",
1713 for example. So the proper cleanup, I think, would be to
1714 limit GDB's type smashing to match exactly those required
1715 by the source language. So GDB could have a
1716 "complete_this_type" function, but never create unnecessary
1717 copies of a type otherwise. */
1718 replace_type (type, xtype);
1719 type->set_name (NULL);
1720 }
1721 else
1722 {
1723 TYPE_TARGET_STUB (type) = 1;
1724 TYPE_TARGET_TYPE (type) = xtype;
1725 }
1726 }
1727 break;
1728
1729 /* In the following types, we must be sure to overwrite any existing
1730 type that the typenums refer to, rather than allocating a new one
1731 and making the typenums point to the new one. This is because there
1732 may already be pointers to the existing type (if it had been
1733 forward-referenced), and we must change it to a pointer, function,
1734 reference, or whatever, *in-place*. */
1735
1736 case '*': /* Pointer to another type */
1737 type1 = read_type (pp, objfile);
1738 type = make_pointer_type (type1, dbx_lookup_type (typenums, objfile));
1739 break;
1740
1741 case '&': /* Reference to another type */
1742 type1 = read_type (pp, objfile);
1743 type = make_reference_type (type1, dbx_lookup_type (typenums, objfile),
1744 TYPE_CODE_REF);
1745 break;
1746
1747 case 'f': /* Function returning another type */
1748 type1 = read_type (pp, objfile);
1749 type = make_function_type (type1, dbx_lookup_type (typenums, objfile));
1750 break;
1751
1752 case 'g': /* Prototyped function. (Sun) */
1753 {
1754 /* Unresolved questions:
1755
1756 - According to Sun's ``STABS Interface Manual'', for 'f'
1757 and 'F' symbol descriptors, a `0' in the argument type list
1758 indicates a varargs function. But it doesn't say how 'g'
1759 type descriptors represent that info. Someone with access
1760 to Sun's toolchain should try it out.
1761
1762 - According to the comment in define_symbol (search for
1763 `process_prototype_types:'), Sun emits integer arguments as
1764 types which ref themselves --- like `void' types. Do we
1765 have to deal with that here, too? Again, someone with
1766 access to Sun's toolchain should try it out and let us
1767 know. */
1768
1769 const char *type_start = (*pp) - 1;
1770 struct type *return_type = read_type (pp, objfile);
1771 struct type *func_type
1772 = make_function_type (return_type,
1773 dbx_lookup_type (typenums, objfile));
1774 struct type_list {
1775 struct type *type;
1776 struct type_list *next;
1777 } *arg_types = 0;
1778 int num_args = 0;
1779
1780 while (**pp && **pp != '#')
1781 {
1782 struct type *arg_type = read_type (pp, objfile);
1783 struct type_list *newobj = XALLOCA (struct type_list);
1784 newobj->type = arg_type;
1785 newobj->next = arg_types;
1786 arg_types = newobj;
1787 num_args++;
1788 }
1789 if (**pp == '#')
1790 ++*pp;
1791 else
1792 {
1793 complaint (_("Prototyped function type didn't "
1794 "end arguments with `#':\n%s"),
1795 type_start);
1796 }
1797
1798 /* If there is just one argument whose type is `void', then
1799 that's just an empty argument list. */
1800 if (arg_types
1801 && ! arg_types->next
1802 && arg_types->type->code () == TYPE_CODE_VOID)
1803 num_args = 0;
1804
1805 func_type->set_fields
1806 ((struct field *) TYPE_ALLOC (func_type,
1807 num_args * sizeof (struct field)));
1808 memset (func_type->fields (), 0, num_args * sizeof (struct field));
1809 {
1810 int i;
1811 struct type_list *t;
1812
1813 /* We stuck each argument type onto the front of the list
1814 when we read it, so the list is reversed. Build the
1815 fields array right-to-left. */
1816 for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
1817 func_type->field (i).set_type (t->type);
1818 }
1819 func_type->set_num_fields (num_args);
1820 TYPE_PROTOTYPED (func_type) = 1;
1821
1822 type = func_type;
1823 break;
1824 }
1825
1826 case 'k': /* Const qualifier on some type (Sun) */
1827 type = read_type (pp, objfile);
1828 type = make_cv_type (1, TYPE_VOLATILE (type), type,
1829 dbx_lookup_type (typenums, objfile));
1830 break;
1831
1832 case 'B': /* Volatile qual on some type (Sun) */
1833 type = read_type (pp, objfile);
1834 type = make_cv_type (TYPE_CONST (type), 1, type,
1835 dbx_lookup_type (typenums, objfile));
1836 break;
1837
1838 case '@':
1839 if (isdigit (**pp) || **pp == '(' || **pp == '-')
1840 { /* Member (class & variable) type */
1841 /* FIXME -- we should be doing smash_to_XXX types here. */
1842
1843 struct type *domain = read_type (pp, objfile);
1844 struct type *memtype;
1845
1846 if (**pp != ',')
1847 /* Invalid member type data format. */
1848 return error_type (pp, objfile);
1849 ++*pp;
1850
1851 memtype = read_type (pp, objfile);
1852 type = dbx_alloc_type (typenums, objfile);
1853 smash_to_memberptr_type (type, domain, memtype);
1854 }
1855 else
1856 /* type attribute */
1857 {
1858 const char *attr = *pp;
1859
1860 /* Skip to the semicolon. */
1861 while (**pp != ';' && **pp != '\0')
1862 ++(*pp);
1863 if (**pp == '\0')
1864 return error_type (pp, objfile);
1865 else
1866 ++ * pp; /* Skip the semicolon. */
1867
1868 switch (*attr)
1869 {
1870 case 's': /* Size attribute */
1871 type_size = atoi (attr + 1);
1872 if (type_size <= 0)
1873 type_size = -1;
1874 break;
1875
1876 case 'S': /* String attribute */
1877 /* FIXME: check to see if following type is array? */
1878 is_string = 1;
1879 break;
1880
1881 case 'V': /* Vector attribute */
1882 /* FIXME: check to see if following type is array? */
1883 is_vector = 1;
1884 break;
1885
1886 default:
1887 /* Ignore unrecognized type attributes, so future compilers
1888 can invent new ones. */
1889 break;
1890 }
1891 ++*pp;
1892 goto again;
1893 }
1894 break;
1895
1896 case '#': /* Method (class & fn) type */
1897 if ((*pp)[0] == '#')
1898 {
1899 /* We'll get the parameter types from the name. */
1900 struct type *return_type;
1901
1902 (*pp)++;
1903 return_type = read_type (pp, objfile);
1904 if (*(*pp)++ != ';')
1905 complaint (_("invalid (minimal) member type "
1906 "data format at symtab pos %d."),
1907 symnum);
1908 type = allocate_stub_method (return_type);
1909 if (typenums[0] != -1)
1910 *dbx_lookup_type (typenums, objfile) = type;
1911 }
1912 else
1913 {
1914 struct type *domain = read_type (pp, objfile);
1915 struct type *return_type;
1916 struct field *args;
1917 int nargs, varargs;
1918
1919 if (**pp != ',')
1920 /* Invalid member type data format. */
1921 return error_type (pp, objfile);
1922 else
1923 ++(*pp);
1924
1925 return_type = read_type (pp, objfile);
1926 args = read_args (pp, ';', objfile, &nargs, &varargs);
1927 if (args == NULL)
1928 return error_type (pp, objfile);
1929 type = dbx_alloc_type (typenums, objfile);
1930 smash_to_method_type (type, domain, return_type, args,
1931 nargs, varargs);
1932 }
1933 break;
1934
1935 case 'r': /* Range type */
1936 type = read_range_type (pp, typenums, type_size, objfile);
1937 if (typenums[0] != -1)
1938 *dbx_lookup_type (typenums, objfile) = type;
1939 break;
1940
1941 case 'b':
1942 {
1943 /* Sun ACC builtin int type */
1944 type = read_sun_builtin_type (pp, typenums, objfile);
1945 if (typenums[0] != -1)
1946 *dbx_lookup_type (typenums, objfile) = type;
1947 }
1948 break;
1949
1950 case 'R': /* Sun ACC builtin float type */
1951 type = read_sun_floating_type (pp, typenums, objfile);
1952 if (typenums[0] != -1)
1953 *dbx_lookup_type (typenums, objfile) = type;
1954 break;
1955
1956 case 'e': /* Enumeration type */
1957 type = dbx_alloc_type (typenums, objfile);
1958 type = read_enum_type (pp, type, objfile);
1959 if (typenums[0] != -1)
1960 *dbx_lookup_type (typenums, objfile) = type;
1961 break;
1962
1963 case 's': /* Struct type */
1964 case 'u': /* Union type */
1965 {
1966 enum type_code type_code = TYPE_CODE_UNDEF;
1967 type = dbx_alloc_type (typenums, objfile);
1968 switch (type_descriptor)
1969 {
1970 case 's':
1971 type_code = TYPE_CODE_STRUCT;
1972 break;
1973 case 'u':
1974 type_code = TYPE_CODE_UNION;
1975 break;
1976 }
1977 type = read_struct_type (pp, type, type_code, objfile);
1978 break;
1979 }
1980
1981 case 'a': /* Array type */
1982 if (**pp != 'r')
1983 return error_type (pp, objfile);
1984 ++*pp;
1985
1986 type = dbx_alloc_type (typenums, objfile);
1987 type = read_array_type (pp, type, objfile);
1988 if (is_string)
1989 type->set_code (TYPE_CODE_STRING);
1990 if (is_vector)
1991 make_vector_type (type);
1992 break;
1993
1994 case 'S': /* Set type */
1995 type1 = read_type (pp, objfile);
1996 type = create_set_type (NULL, type1);
1997 if (typenums[0] != -1)
1998 *dbx_lookup_type (typenums, objfile) = type;
1999 break;
2000
2001 default:
2002 --*pp; /* Go back to the symbol in error. */
2003 /* Particularly important if it was \0! */
2004 return error_type (pp, objfile);
2005 }
2006
2007 if (type == 0)
2008 {
2009 warning (_("GDB internal error, type is NULL in stabsread.c."));
2010 return error_type (pp, objfile);
2011 }
2012
2013 /* Size specified in a type attribute overrides any other size. */
2014 if (type_size != -1)
2015 TYPE_LENGTH (type) = (type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
2016
2017 return type;
2018 }
2019
2020 /* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
2022 Return the proper type node for a given builtin type number. */
2023
2024 static const struct objfile_key<struct type *,
2025 gdb::noop_deleter<struct type *>>
2026 rs6000_builtin_type_data;
2027
2028 static struct type *
2029 rs6000_builtin_type (int typenum, struct objfile *objfile)
2030 {
2031 struct type **negative_types = rs6000_builtin_type_data.get (objfile);
2032
2033 /* We recognize types numbered from -NUMBER_RECOGNIZED to -1. */
2034 #define NUMBER_RECOGNIZED 34
2035 struct type *rettype = NULL;
2036
2037 if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
2038 {
2039 complaint (_("Unknown builtin type %d"), typenum);
2040 return objfile_type (objfile)->builtin_error;
2041 }
2042
2043 if (!negative_types)
2044 {
2045 /* This includes an empty slot for type number -0. */
2046 negative_types = OBSTACK_CALLOC (&objfile->objfile_obstack,
2047 NUMBER_RECOGNIZED + 1, struct type *);
2048 rs6000_builtin_type_data.set (objfile, negative_types);
2049 }
2050
2051 if (negative_types[-typenum] != NULL)
2052 return negative_types[-typenum];
2053
2054 #if TARGET_CHAR_BIT != 8
2055 #error This code wrong for TARGET_CHAR_BIT not 8
2056 /* These definitions all assume that TARGET_CHAR_BIT is 8. I think
2057 that if that ever becomes not true, the correct fix will be to
2058 make the size in the struct type to be in bits, not in units of
2059 TARGET_CHAR_BIT. */
2060 #endif
2061
2062 switch (-typenum)
2063 {
2064 case 1:
2065 /* The size of this and all the other types are fixed, defined
2066 by the debugging format. If there is a type called "int" which
2067 is other than 32 bits, then it should use a new negative type
2068 number (or avoid negative type numbers for that case).
2069 See stabs.texinfo. */
2070 rettype = init_integer_type (objfile, 32, 0, "int");
2071 break;
2072 case 2:
2073 rettype = init_integer_type (objfile, 8, 0, "char");
2074 TYPE_NOSIGN (rettype) = 1;
2075 break;
2076 case 3:
2077 rettype = init_integer_type (objfile, 16, 0, "short");
2078 break;
2079 case 4:
2080 rettype = init_integer_type (objfile, 32, 0, "long");
2081 break;
2082 case 5:
2083 rettype = init_integer_type (objfile, 8, 1, "unsigned char");
2084 break;
2085 case 6:
2086 rettype = init_integer_type (objfile, 8, 0, "signed char");
2087 break;
2088 case 7:
2089 rettype = init_integer_type (objfile, 16, 1, "unsigned short");
2090 break;
2091 case 8:
2092 rettype = init_integer_type (objfile, 32, 1, "unsigned int");
2093 break;
2094 case 9:
2095 rettype = init_integer_type (objfile, 32, 1, "unsigned");
2096 break;
2097 case 10:
2098 rettype = init_integer_type (objfile, 32, 1, "unsigned long");
2099 break;
2100 case 11:
2101 rettype = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
2102 break;
2103 case 12:
2104 /* IEEE single precision (32 bit). */
2105 rettype = init_float_type (objfile, 32, "float",
2106 floatformats_ieee_single);
2107 break;
2108 case 13:
2109 /* IEEE double precision (64 bit). */
2110 rettype = init_float_type (objfile, 64, "double",
2111 floatformats_ieee_double);
2112 break;
2113 case 14:
2114 /* This is an IEEE double on the RS/6000, and different machines with
2115 different sizes for "long double" should use different negative
2116 type numbers. See stabs.texinfo. */
2117 rettype = init_float_type (objfile, 64, "long double",
2118 floatformats_ieee_double);
2119 break;
2120 case 15:
2121 rettype = init_integer_type (objfile, 32, 0, "integer");
2122 break;
2123 case 16:
2124 rettype = init_boolean_type (objfile, 32, 1, "boolean");
2125 break;
2126 case 17:
2127 rettype = init_float_type (objfile, 32, "short real",
2128 floatformats_ieee_single);
2129 break;
2130 case 18:
2131 rettype = init_float_type (objfile, 64, "real",
2132 floatformats_ieee_double);
2133 break;
2134 case 19:
2135 rettype = init_type (objfile, TYPE_CODE_ERROR, 0, "stringptr");
2136 break;
2137 case 20:
2138 rettype = init_character_type (objfile, 8, 1, "character");
2139 break;
2140 case 21:
2141 rettype = init_boolean_type (objfile, 8, 1, "logical*1");
2142 break;
2143 case 22:
2144 rettype = init_boolean_type (objfile, 16, 1, "logical*2");
2145 break;
2146 case 23:
2147 rettype = init_boolean_type (objfile, 32, 1, "logical*4");
2148 break;
2149 case 24:
2150 rettype = init_boolean_type (objfile, 32, 1, "logical");
2151 break;
2152 case 25:
2153 /* Complex type consisting of two IEEE single precision values. */
2154 rettype = init_complex_type ("complex",
2155 rs6000_builtin_type (12, objfile));
2156 break;
2157 case 26:
2158 /* Complex type consisting of two IEEE double precision values. */
2159 rettype = init_complex_type ("double complex",
2160 rs6000_builtin_type (13, objfile));
2161 break;
2162 case 27:
2163 rettype = init_integer_type (objfile, 8, 0, "integer*1");
2164 break;
2165 case 28:
2166 rettype = init_integer_type (objfile, 16, 0, "integer*2");
2167 break;
2168 case 29:
2169 rettype = init_integer_type (objfile, 32, 0, "integer*4");
2170 break;
2171 case 30:
2172 rettype = init_character_type (objfile, 16, 0, "wchar");
2173 break;
2174 case 31:
2175 rettype = init_integer_type (objfile, 64, 0, "long long");
2176 break;
2177 case 32:
2178 rettype = init_integer_type (objfile, 64, 1, "unsigned long long");
2179 break;
2180 case 33:
2181 rettype = init_integer_type (objfile, 64, 1, "logical*8");
2182 break;
2183 case 34:
2184 rettype = init_integer_type (objfile, 64, 0, "integer*8");
2185 break;
2186 }
2187 negative_types[-typenum] = rettype;
2188 return rettype;
2189 }
2190
2191 /* This page contains subroutines of read_type. */
2193
2194 /* Wrapper around method_name_from_physname to flag a complaint
2195 if there is an error. */
2196
2197 static char *
2198 stabs_method_name_from_physname (const char *physname)
2199 {
2200 char *method_name;
2201
2202 method_name = method_name_from_physname (physname);
2203
2204 if (method_name == NULL)
2205 {
2206 complaint (_("Method has bad physname %s\n"), physname);
2207 return NULL;
2208 }
2209
2210 return method_name;
2211 }
2212
2213 /* Read member function stabs info for C++ classes. The form of each member
2214 function data is:
2215
2216 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2217
2218 An example with two member functions is:
2219
2220 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2221
2222 For the case of overloaded operators, the format is op$::*.funcs, where
2223 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2224 name (such as `+=') and `.' marks the end of the operator name.
2225
2226 Returns 1 for success, 0 for failure. */
2227
2228 static int
2229 read_member_functions (struct stab_field_info *fip, const char **pp,
2230 struct type *type, struct objfile *objfile)
2231 {
2232 int nfn_fields = 0;
2233 int length = 0;
2234 int i;
2235 struct next_fnfield
2236 {
2237 struct next_fnfield *next;
2238 struct fn_field fn_field;
2239 }
2240 *sublist;
2241 struct type *look_ahead_type;
2242 struct next_fnfieldlist *new_fnlist;
2243 struct next_fnfield *new_sublist;
2244 char *main_fn_name;
2245 const char *p;
2246
2247 /* Process each list until we find something that is not a member function
2248 or find the end of the functions. */
2249
2250 while (**pp != ';')
2251 {
2252 /* We should be positioned at the start of the function name.
2253 Scan forward to find the first ':' and if it is not the
2254 first of a "::" delimiter, then this is not a member function. */
2255 p = *pp;
2256 while (*p != ':')
2257 {
2258 p++;
2259 }
2260 if (p[1] != ':')
2261 {
2262 break;
2263 }
2264
2265 sublist = NULL;
2266 look_ahead_type = NULL;
2267 length = 0;
2268
2269 new_fnlist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfieldlist);
2270
2271 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
2272 {
2273 /* This is a completely wierd case. In order to stuff in the
2274 names that might contain colons (the usual name delimiter),
2275 Mike Tiemann defined a different name format which is
2276 signalled if the identifier is "op$". In that case, the
2277 format is "op$::XXXX." where XXXX is the name. This is
2278 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2279 /* This lets the user type "break operator+".
2280 We could just put in "+" as the name, but that wouldn't
2281 work for "*". */
2282 static char opname[32] = "op$";
2283 char *o = opname + 3;
2284
2285 /* Skip past '::'. */
2286 *pp = p + 2;
2287
2288 STABS_CONTINUE (pp, objfile);
2289 p = *pp;
2290 while (*p != '.')
2291 {
2292 *o++ = *p++;
2293 }
2294 main_fn_name = savestring (opname, o - opname);
2295 /* Skip past '.' */
2296 *pp = p + 1;
2297 }
2298 else
2299 {
2300 main_fn_name = savestring (*pp, p - *pp);
2301 /* Skip past '::'. */
2302 *pp = p + 2;
2303 }
2304 new_fnlist->fn_fieldlist.name = main_fn_name;
2305
2306 do
2307 {
2308 new_sublist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfield);
2309
2310 /* Check for and handle cretinous dbx symbol name continuation! */
2311 if (look_ahead_type == NULL)
2312 {
2313 /* Normal case. */
2314 STABS_CONTINUE (pp, objfile);
2315
2316 new_sublist->fn_field.type = read_type (pp, objfile);
2317 if (**pp != ':')
2318 {
2319 /* Invalid symtab info for member function. */
2320 return 0;
2321 }
2322 }
2323 else
2324 {
2325 /* g++ version 1 kludge */
2326 new_sublist->fn_field.type = look_ahead_type;
2327 look_ahead_type = NULL;
2328 }
2329
2330 (*pp)++;
2331 p = *pp;
2332 while (*p != ';')
2333 {
2334 p++;
2335 }
2336
2337 /* These are methods, not functions. */
2338 if (new_sublist->fn_field.type->code () == TYPE_CODE_FUNC)
2339 new_sublist->fn_field.type->set_code (TYPE_CODE_METHOD);
2340 else
2341 gdb_assert (new_sublist->fn_field.type->code ()
2342 == TYPE_CODE_METHOD);
2343
2344 /* If this is just a stub, then we don't have the real name here. */
2345 if (TYPE_STUB (new_sublist->fn_field.type))
2346 {
2347 if (!TYPE_SELF_TYPE (new_sublist->fn_field.type))
2348 set_type_self_type (new_sublist->fn_field.type, type);
2349 new_sublist->fn_field.is_stub = 1;
2350 }
2351
2352 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2353 *pp = p + 1;
2354
2355 /* Set this member function's visibility fields. */
2356 switch (*(*pp)++)
2357 {
2358 case VISIBILITY_PRIVATE:
2359 new_sublist->fn_field.is_private = 1;
2360 break;
2361 case VISIBILITY_PROTECTED:
2362 new_sublist->fn_field.is_protected = 1;
2363 break;
2364 }
2365
2366 STABS_CONTINUE (pp, objfile);
2367 switch (**pp)
2368 {
2369 case 'A': /* Normal functions. */
2370 new_sublist->fn_field.is_const = 0;
2371 new_sublist->fn_field.is_volatile = 0;
2372 (*pp)++;
2373 break;
2374 case 'B': /* `const' member functions. */
2375 new_sublist->fn_field.is_const = 1;
2376 new_sublist->fn_field.is_volatile = 0;
2377 (*pp)++;
2378 break;
2379 case 'C': /* `volatile' member function. */
2380 new_sublist->fn_field.is_const = 0;
2381 new_sublist->fn_field.is_volatile = 1;
2382 (*pp)++;
2383 break;
2384 case 'D': /* `const volatile' member function. */
2385 new_sublist->fn_field.is_const = 1;
2386 new_sublist->fn_field.is_volatile = 1;
2387 (*pp)++;
2388 break;
2389 case '*': /* File compiled with g++ version 1 --
2390 no info. */
2391 case '?':
2392 case '.':
2393 break;
2394 default:
2395 complaint (_("const/volatile indicator missing, got '%c'"),
2396 **pp);
2397 break;
2398 }
2399
2400 switch (*(*pp)++)
2401 {
2402 case '*':
2403 {
2404 int nbits;
2405 /* virtual member function, followed by index.
2406 The sign bit is set to distinguish pointers-to-methods
2407 from virtual function indicies. Since the array is
2408 in words, the quantity must be shifted left by 1
2409 on 16 bit machine, and by 2 on 32 bit machine, forcing
2410 the sign bit out, and usable as a valid index into
2411 the array. Remove the sign bit here. */
2412 new_sublist->fn_field.voffset =
2413 (0x7fffffff & read_huge_number (pp, ';', &nbits, 0)) + 2;
2414 if (nbits != 0)
2415 return 0;
2416
2417 STABS_CONTINUE (pp, objfile);
2418 if (**pp == ';' || **pp == '\0')
2419 {
2420 /* Must be g++ version 1. */
2421 new_sublist->fn_field.fcontext = 0;
2422 }
2423 else
2424 {
2425 /* Figure out from whence this virtual function came.
2426 It may belong to virtual function table of
2427 one of its baseclasses. */
2428 look_ahead_type = read_type (pp, objfile);
2429 if (**pp == ':')
2430 {
2431 /* g++ version 1 overloaded methods. */
2432 }
2433 else
2434 {
2435 new_sublist->fn_field.fcontext = look_ahead_type;
2436 if (**pp != ';')
2437 {
2438 return 0;
2439 }
2440 else
2441 {
2442 ++*pp;
2443 }
2444 look_ahead_type = NULL;
2445 }
2446 }
2447 break;
2448 }
2449 case '?':
2450 /* static member function. */
2451 {
2452 int slen = strlen (main_fn_name);
2453
2454 new_sublist->fn_field.voffset = VOFFSET_STATIC;
2455
2456 /* For static member functions, we can't tell if they
2457 are stubbed, as they are put out as functions, and not as
2458 methods.
2459 GCC v2 emits the fully mangled name if
2460 dbxout.c:flag_minimal_debug is not set, so we have to
2461 detect a fully mangled physname here and set is_stub
2462 accordingly. Fully mangled physnames in v2 start with
2463 the member function name, followed by two underscores.
2464 GCC v3 currently always emits stubbed member functions,
2465 but with fully mangled physnames, which start with _Z. */
2466 if (!(strncmp (new_sublist->fn_field.physname,
2467 main_fn_name, slen) == 0
2468 && new_sublist->fn_field.physname[slen] == '_'
2469 && new_sublist->fn_field.physname[slen + 1] == '_'))
2470 {
2471 new_sublist->fn_field.is_stub = 1;
2472 }
2473 break;
2474 }
2475
2476 default:
2477 /* error */
2478 complaint (_("member function type missing, got '%c'"),
2479 (*pp)[-1]);
2480 /* Normal member function. */
2481 /* Fall through. */
2482
2483 case '.':
2484 /* normal member function. */
2485 new_sublist->fn_field.voffset = 0;
2486 new_sublist->fn_field.fcontext = 0;
2487 break;
2488 }
2489
2490 new_sublist->next = sublist;
2491 sublist = new_sublist;
2492 length++;
2493 STABS_CONTINUE (pp, objfile);
2494 }
2495 while (**pp != ';' && **pp != '\0');
2496
2497 (*pp)++;
2498 STABS_CONTINUE (pp, objfile);
2499
2500 /* Skip GCC 3.X member functions which are duplicates of the callable
2501 constructor/destructor. */
2502 if (strcmp_iw (main_fn_name, "__base_ctor ") == 0
2503 || strcmp_iw (main_fn_name, "__base_dtor ") == 0
2504 || strcmp (main_fn_name, "__deleting_dtor") == 0)
2505 {
2506 xfree (main_fn_name);
2507 }
2508 else
2509 {
2510 int has_destructor = 0, has_other = 0;
2511 int is_v3 = 0;
2512 struct next_fnfield *tmp_sublist;
2513
2514 /* Various versions of GCC emit various mostly-useless
2515 strings in the name field for special member functions.
2516
2517 For stub methods, we need to defer correcting the name
2518 until we are ready to unstub the method, because the current
2519 name string is used by gdb_mangle_name. The only stub methods
2520 of concern here are GNU v2 operators; other methods have their
2521 names correct (see caveat below).
2522
2523 For non-stub methods, in GNU v3, we have a complete physname.
2524 Therefore we can safely correct the name now. This primarily
2525 affects constructors and destructors, whose name will be
2526 __comp_ctor or __comp_dtor instead of Foo or ~Foo. Cast
2527 operators will also have incorrect names; for instance,
2528 "operator int" will be named "operator i" (i.e. the type is
2529 mangled).
2530
2531 For non-stub methods in GNU v2, we have no easy way to
2532 know if we have a complete physname or not. For most
2533 methods the result depends on the platform (if CPLUS_MARKER
2534 can be `$' or `.', it will use minimal debug information, or
2535 otherwise the full physname will be included).
2536
2537 Rather than dealing with this, we take a different approach.
2538 For v3 mangled names, we can use the full physname; for v2,
2539 we use cplus_demangle_opname (which is actually v2 specific),
2540 because the only interesting names are all operators - once again
2541 barring the caveat below. Skip this process if any method in the
2542 group is a stub, to prevent our fouling up the workings of
2543 gdb_mangle_name.
2544
2545 The caveat: GCC 2.95.x (and earlier?) put constructors and
2546 destructors in the same method group. We need to split this
2547 into two groups, because they should have different names.
2548 So for each method group we check whether it contains both
2549 routines whose physname appears to be a destructor (the physnames
2550 for and destructors are always provided, due to quirks in v2
2551 mangling) and routines whose physname does not appear to be a
2552 destructor. If so then we break up the list into two halves.
2553 Even if the constructors and destructors aren't in the same group
2554 the destructor will still lack the leading tilde, so that also
2555 needs to be fixed.
2556
2557 So, to summarize what we expect and handle here:
2558
2559 Given Given Real Real Action
2560 method name physname physname method name
2561
2562 __opi [none] __opi__3Foo operator int opname
2563 [now or later]
2564 Foo _._3Foo _._3Foo ~Foo separate and
2565 rename
2566 operator i _ZN3FoocviEv _ZN3FoocviEv operator int demangle
2567 __comp_ctor _ZN3FooC1ERKS_ _ZN3FooC1ERKS_ Foo demangle
2568 */
2569
2570 tmp_sublist = sublist;
2571 while (tmp_sublist != NULL)
2572 {
2573 if (tmp_sublist->fn_field.physname[0] == '_'
2574 && tmp_sublist->fn_field.physname[1] == 'Z')
2575 is_v3 = 1;
2576
2577 if (is_destructor_name (tmp_sublist->fn_field.physname))
2578 has_destructor++;
2579 else
2580 has_other++;
2581
2582 tmp_sublist = tmp_sublist->next;
2583 }
2584
2585 if (has_destructor && has_other)
2586 {
2587 struct next_fnfieldlist *destr_fnlist;
2588 struct next_fnfield *last_sublist;
2589
2590 /* Create a new fn_fieldlist for the destructors. */
2591
2592 destr_fnlist = OBSTACK_ZALLOC (&fip->obstack,
2593 struct next_fnfieldlist);
2594
2595 destr_fnlist->fn_fieldlist.name
2596 = obconcat (&objfile->objfile_obstack, "~",
2597 new_fnlist->fn_fieldlist.name, (char *) NULL);
2598
2599 destr_fnlist->fn_fieldlist.fn_fields =
2600 XOBNEWVEC (&objfile->objfile_obstack,
2601 struct fn_field, has_destructor);
2602 memset (destr_fnlist->fn_fieldlist.fn_fields, 0,
2603 sizeof (struct fn_field) * has_destructor);
2604 tmp_sublist = sublist;
2605 last_sublist = NULL;
2606 i = 0;
2607 while (tmp_sublist != NULL)
2608 {
2609 if (!is_destructor_name (tmp_sublist->fn_field.physname))
2610 {
2611 tmp_sublist = tmp_sublist->next;
2612 continue;
2613 }
2614
2615 destr_fnlist->fn_fieldlist.fn_fields[i++]
2616 = tmp_sublist->fn_field;
2617 if (last_sublist)
2618 last_sublist->next = tmp_sublist->next;
2619 else
2620 sublist = tmp_sublist->next;
2621 last_sublist = tmp_sublist;
2622 tmp_sublist = tmp_sublist->next;
2623 }
2624
2625 destr_fnlist->fn_fieldlist.length = has_destructor;
2626 destr_fnlist->next = fip->fnlist;
2627 fip->fnlist = destr_fnlist;
2628 nfn_fields++;
2629 length -= has_destructor;
2630 }
2631 else if (is_v3)
2632 {
2633 /* v3 mangling prevents the use of abbreviated physnames,
2634 so we can do this here. There are stubbed methods in v3
2635 only:
2636 - in -gstabs instead of -gstabs+
2637 - or for static methods, which are output as a function type
2638 instead of a method type. */
2639 char *new_method_name =
2640 stabs_method_name_from_physname (sublist->fn_field.physname);
2641
2642 if (new_method_name != NULL
2643 && strcmp (new_method_name,
2644 new_fnlist->fn_fieldlist.name) != 0)
2645 {
2646 new_fnlist->fn_fieldlist.name = new_method_name;
2647 xfree (main_fn_name);
2648 }
2649 else
2650 xfree (new_method_name);
2651 }
2652 else if (has_destructor && new_fnlist->fn_fieldlist.name[0] != '~')
2653 {
2654 new_fnlist->fn_fieldlist.name =
2655 obconcat (&objfile->objfile_obstack,
2656 "~", main_fn_name, (char *)NULL);
2657 xfree (main_fn_name);
2658 }
2659
2660 new_fnlist->fn_fieldlist.fn_fields
2661 = OBSTACK_CALLOC (&objfile->objfile_obstack, length, fn_field);
2662 for (i = length; (i--, sublist); sublist = sublist->next)
2663 {
2664 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
2665 }
2666
2667 new_fnlist->fn_fieldlist.length = length;
2668 new_fnlist->next = fip->fnlist;
2669 fip->fnlist = new_fnlist;
2670 nfn_fields++;
2671 }
2672 }
2673
2674 if (nfn_fields)
2675 {
2676 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2677 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2678 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
2679 memset (TYPE_FN_FIELDLISTS (type), 0,
2680 sizeof (struct fn_fieldlist) * nfn_fields);
2681 TYPE_NFN_FIELDS (type) = nfn_fields;
2682 }
2683
2684 return 1;
2685 }
2686
2687 /* Special GNU C++ name.
2688
2689 Returns 1 for success, 0 for failure. "failure" means that we can't
2690 keep parsing and it's time for error_type(). */
2691
2692 static int
2693 read_cpp_abbrev (struct stab_field_info *fip, const char **pp,
2694 struct type *type, struct objfile *objfile)
2695 {
2696 const char *p;
2697 const char *name;
2698 char cpp_abbrev;
2699 struct type *context;
2700
2701 p = *pp;
2702 if (*++p == 'v')
2703 {
2704 name = NULL;
2705 cpp_abbrev = *++p;
2706
2707 *pp = p + 1;
2708
2709 /* At this point, *pp points to something like "22:23=*22...",
2710 where the type number before the ':' is the "context" and
2711 everything after is a regular type definition. Lookup the
2712 type, find it's name, and construct the field name. */
2713
2714 context = read_type (pp, objfile);
2715
2716 switch (cpp_abbrev)
2717 {
2718 case 'f': /* $vf -- a virtual function table pointer */
2719 name = context->name ();
2720 if (name == NULL)
2721 {
2722 name = "";
2723 }
2724 fip->list->field.name = obconcat (&objfile->objfile_obstack,
2725 vptr_name, name, (char *) NULL);
2726 break;
2727
2728 case 'b': /* $vb -- a virtual bsomethingorother */
2729 name = context->name ();
2730 if (name == NULL)
2731 {
2732 complaint (_("C++ abbreviated type name "
2733 "unknown at symtab pos %d"),
2734 symnum);
2735 name = "FOO";
2736 }
2737 fip->list->field.name = obconcat (&objfile->objfile_obstack, vb_name,
2738 name, (char *) NULL);
2739 break;
2740
2741 default:
2742 invalid_cpp_abbrev_complaint (*pp);
2743 fip->list->field.name = obconcat (&objfile->objfile_obstack,
2744 "INVALID_CPLUSPLUS_ABBREV",
2745 (char *) NULL);
2746 break;
2747 }
2748
2749 /* At this point, *pp points to the ':'. Skip it and read the
2750 field type. */
2751
2752 p = ++(*pp);
2753 if (p[-1] != ':')
2754 {
2755 invalid_cpp_abbrev_complaint (*pp);
2756 return 0;
2757 }
2758 fip->list->field.set_type (read_type (pp, objfile));
2759 if (**pp == ',')
2760 (*pp)++; /* Skip the comma. */
2761 else
2762 return 0;
2763
2764 {
2765 int nbits;
2766
2767 SET_FIELD_BITPOS (fip->list->field,
2768 read_huge_number (pp, ';', &nbits, 0));
2769 if (nbits != 0)
2770 return 0;
2771 }
2772 /* This field is unpacked. */
2773 FIELD_BITSIZE (fip->list->field) = 0;
2774 fip->list->visibility = VISIBILITY_PRIVATE;
2775 }
2776 else
2777 {
2778 invalid_cpp_abbrev_complaint (*pp);
2779 /* We have no idea what syntax an unrecognized abbrev would have, so
2780 better return 0. If we returned 1, we would need to at least advance
2781 *pp to avoid an infinite loop. */
2782 return 0;
2783 }
2784 return 1;
2785 }
2786
2787 static void
2788 read_one_struct_field (struct stab_field_info *fip, const char **pp,
2789 const char *p, struct type *type,
2790 struct objfile *objfile)
2791 {
2792 struct gdbarch *gdbarch = objfile->arch ();
2793
2794 fip->list->field.name
2795 = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
2796 *pp = p + 1;
2797
2798 /* This means we have a visibility for a field coming. */
2799 if (**pp == '/')
2800 {
2801 (*pp)++;
2802 fip->list->visibility = *(*pp)++;
2803 }
2804 else
2805 {
2806 /* normal dbx-style format, no explicit visibility */
2807 fip->list->visibility = VISIBILITY_PUBLIC;
2808 }
2809
2810 fip->list->field.set_type (read_type (pp, objfile));
2811 if (**pp == ':')
2812 {
2813 p = ++(*pp);
2814 #if 0
2815 /* Possible future hook for nested types. */
2816 if (**pp == '!')
2817 {
2818 fip->list->field.bitpos = (long) -2; /* nested type */
2819 p = ++(*pp);
2820 }
2821 else
2822 ...;
2823 #endif
2824 while (*p != ';')
2825 {
2826 p++;
2827 }
2828 /* Static class member. */
2829 SET_FIELD_PHYSNAME (fip->list->field, savestring (*pp, p - *pp));
2830 *pp = p + 1;
2831 return;
2832 }
2833 else if (**pp != ',')
2834 {
2835 /* Bad structure-type format. */
2836 stabs_general_complaint ("bad structure-type format");
2837 return;
2838 }
2839
2840 (*pp)++; /* Skip the comma. */
2841
2842 {
2843 int nbits;
2844
2845 SET_FIELD_BITPOS (fip->list->field,
2846 read_huge_number (pp, ',', &nbits, 0));
2847 if (nbits != 0)
2848 {
2849 stabs_general_complaint ("bad structure-type format");
2850 return;
2851 }
2852 FIELD_BITSIZE (fip->list->field) = read_huge_number (pp, ';', &nbits, 0);
2853 if (nbits != 0)
2854 {
2855 stabs_general_complaint ("bad structure-type format");
2856 return;
2857 }
2858 }
2859
2860 if (FIELD_BITPOS (fip->list->field) == 0
2861 && FIELD_BITSIZE (fip->list->field) == 0)
2862 {
2863 /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
2864 it is a field which has been optimized out. The correct stab for
2865 this case is to use VISIBILITY_IGNORE, but that is a recent
2866 invention. (2) It is a 0-size array. For example
2867 union { int num; char str[0]; } foo. Printing _("<no value>" for
2868 str in "p foo" is OK, since foo.str (and thus foo.str[3])
2869 will continue to work, and a 0-size array as a whole doesn't
2870 have any contents to print.
2871
2872 I suspect this probably could also happen with gcc -gstabs (not
2873 -gstabs+) for static fields, and perhaps other C++ extensions.
2874 Hopefully few people use -gstabs with gdb, since it is intended
2875 for dbx compatibility. */
2876
2877 /* Ignore this field. */
2878 fip->list->visibility = VISIBILITY_IGNORE;
2879 }
2880 else
2881 {
2882 /* Detect an unpacked field and mark it as such.
2883 dbx gives a bit size for all fields.
2884 Note that forward refs cannot be packed,
2885 and treat enums as if they had the width of ints. */
2886
2887 struct type *field_type = check_typedef (fip->list->field.type ());
2888
2889 if (field_type->code () != TYPE_CODE_INT
2890 && field_type->code () != TYPE_CODE_RANGE
2891 && field_type->code () != TYPE_CODE_BOOL
2892 && field_type->code () != TYPE_CODE_ENUM)
2893 {
2894 FIELD_BITSIZE (fip->list->field) = 0;
2895 }
2896 if ((FIELD_BITSIZE (fip->list->field)
2897 == TARGET_CHAR_BIT * TYPE_LENGTH (field_type)
2898 || (field_type->code () == TYPE_CODE_ENUM
2899 && FIELD_BITSIZE (fip->list->field)
2900 == gdbarch_int_bit (gdbarch))
2901 )
2902 &&
2903 FIELD_BITPOS (fip->list->field) % 8 == 0)
2904 {
2905 FIELD_BITSIZE (fip->list->field) = 0;
2906 }
2907 }
2908 }
2909
2910
2911 /* Read struct or class data fields. They have the form:
2912
2913 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2914
2915 At the end, we see a semicolon instead of a field.
2916
2917 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2918 a static field.
2919
2920 The optional VISIBILITY is one of:
2921
2922 '/0' (VISIBILITY_PRIVATE)
2923 '/1' (VISIBILITY_PROTECTED)
2924 '/2' (VISIBILITY_PUBLIC)
2925 '/9' (VISIBILITY_IGNORE)
2926
2927 or nothing, for C style fields with public visibility.
2928
2929 Returns 1 for success, 0 for failure. */
2930
2931 static int
2932 read_struct_fields (struct stab_field_info *fip, const char **pp,
2933 struct type *type, struct objfile *objfile)
2934 {
2935 const char *p;
2936 struct nextfield *newobj;
2937
2938 /* We better set p right now, in case there are no fields at all... */
2939
2940 p = *pp;
2941
2942 /* Read each data member type until we find the terminating ';' at the end of
2943 the data member list, or break for some other reason such as finding the
2944 start of the member function list. */
2945 /* Stab string for structure/union does not end with two ';' in
2946 SUN C compiler 5.3 i.e. F6U2, hence check for end of string. */
2947
2948 while (**pp != ';' && **pp != '\0')
2949 {
2950 STABS_CONTINUE (pp, objfile);
2951 /* Get space to record the next field's data. */
2952 newobj = OBSTACK_ZALLOC (&fip->obstack, struct nextfield);
2953
2954 newobj->next = fip->list;
2955 fip->list = newobj;
2956
2957 /* Get the field name. */
2958 p = *pp;
2959
2960 /* If is starts with CPLUS_MARKER it is a special abbreviation,
2961 unless the CPLUS_MARKER is followed by an underscore, in
2962 which case it is just the name of an anonymous type, which we
2963 should handle like any other type name. */
2964
2965 if (is_cplus_marker (p[0]) && p[1] != '_')
2966 {
2967 if (!read_cpp_abbrev (fip, pp, type, objfile))
2968 return 0;
2969 continue;
2970 }
2971
2972 /* Look for the ':' that separates the field name from the field
2973 values. Data members are delimited by a single ':', while member
2974 functions are delimited by a pair of ':'s. When we hit the member
2975 functions (if any), terminate scan loop and return. */
2976
2977 while (*p != ':' && *p != '\0')
2978 {
2979 p++;
2980 }
2981 if (*p == '\0')
2982 return 0;
2983
2984 /* Check to see if we have hit the member functions yet. */
2985 if (p[1] == ':')
2986 {
2987 break;
2988 }
2989 read_one_struct_field (fip, pp, p, type, objfile);
2990 }
2991 if (p[0] == ':' && p[1] == ':')
2992 {
2993 /* (the deleted) chill the list of fields: the last entry (at
2994 the head) is a partially constructed entry which we now
2995 scrub. */
2996 fip->list = fip->list->next;
2997 }
2998 return 1;
2999 }
3000 /* *INDENT-OFF* */
3001 /* The stabs for C++ derived classes contain baseclass information which
3002 is marked by a '!' character after the total size. This function is
3003 called when we encounter the baseclass marker, and slurps up all the
3004 baseclass information.
3005
3006 Immediately following the '!' marker is the number of base classes that
3007 the class is derived from, followed by information for each base class.
3008 For each base class, there are two visibility specifiers, a bit offset
3009 to the base class information within the derived class, a reference to
3010 the type for the base class, and a terminating semicolon.
3011
3012 A typical example, with two base classes, would be "!2,020,19;0264,21;".
3013 ^^ ^ ^ ^ ^ ^ ^
3014 Baseclass information marker __________________|| | | | | | |
3015 Number of baseclasses __________________________| | | | | | |
3016 Visibility specifiers (2) ________________________| | | | | |
3017 Offset in bits from start of class _________________| | | | |
3018 Type number for base class ___________________________| | | |
3019 Visibility specifiers (2) _______________________________| | |
3020 Offset in bits from start of class ________________________| |
3021 Type number of base class ____________________________________|
3022
3023 Return 1 for success, 0 for (error-type-inducing) failure. */
3024 /* *INDENT-ON* */
3025
3026
3027
3028 static int
3029 read_baseclasses (struct stab_field_info *fip, const char **pp,
3030 struct type *type, struct objfile *objfile)
3031 {
3032 int i;
3033 struct nextfield *newobj;
3034
3035 if (**pp != '!')
3036 {
3037 return 1;
3038 }
3039 else
3040 {
3041 /* Skip the '!' baseclass information marker. */
3042 (*pp)++;
3043 }
3044
3045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3046 {
3047 int nbits;
3048
3049 TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits, 0);
3050 if (nbits != 0)
3051 return 0;
3052 }
3053
3054 #if 0
3055 /* Some stupid compilers have trouble with the following, so break
3056 it up into simpler expressions. */
3057 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
3058 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
3059 #else
3060 {
3061 int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
3062 char *pointer;
3063
3064 pointer = (char *) TYPE_ALLOC (type, num_bytes);
3065 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3066 }
3067 #endif /* 0 */
3068
3069 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
3070
3071 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
3072 {
3073 newobj = OBSTACK_ZALLOC (&fip->obstack, struct nextfield);
3074
3075 newobj->next = fip->list;
3076 fip->list = newobj;
3077 FIELD_BITSIZE (newobj->field) = 0; /* This should be an unpacked
3078 field! */
3079
3080 STABS_CONTINUE (pp, objfile);
3081 switch (**pp)
3082 {
3083 case '0':
3084 /* Nothing to do. */
3085 break;
3086 case '1':
3087 SET_TYPE_FIELD_VIRTUAL (type, i);
3088 break;
3089 default:
3090 /* Unknown character. Complain and treat it as non-virtual. */
3091 {
3092 complaint (_("Unknown virtual character `%c' for baseclass"),
3093 **pp);
3094 }
3095 }
3096 ++(*pp);
3097
3098 newobj->visibility = *(*pp)++;
3099 switch (newobj->visibility)
3100 {
3101 case VISIBILITY_PRIVATE:
3102 case VISIBILITY_PROTECTED:
3103 case VISIBILITY_PUBLIC:
3104 break;
3105 default:
3106 /* Bad visibility format. Complain and treat it as
3107 public. */
3108 {
3109 complaint (_("Unknown visibility `%c' for baseclass"),
3110 newobj->visibility);
3111 newobj->visibility = VISIBILITY_PUBLIC;
3112 }
3113 }
3114
3115 {
3116 int nbits;
3117
3118 /* The remaining value is the bit offset of the portion of the object
3119 corresponding to this baseclass. Always zero in the absence of
3120 multiple inheritance. */
3121
3122 SET_FIELD_BITPOS (newobj->field, read_huge_number (pp, ',', &nbits, 0));
3123 if (nbits != 0)
3124 return 0;
3125 }
3126
3127 /* The last piece of baseclass information is the type of the
3128 base class. Read it, and remember it's type name as this
3129 field's name. */
3130
3131 newobj->field.set_type (read_type (pp, objfile));
3132 newobj->field.name = newobj->field.type ()->name ();
3133
3134 /* Skip trailing ';' and bump count of number of fields seen. */
3135 if (**pp == ';')
3136 (*pp)++;
3137 else
3138 return 0;
3139 }
3140 return 1;
3141 }
3142
3143 /* The tail end of stabs for C++ classes that contain a virtual function
3144 pointer contains a tilde, a %, and a type number.
3145 The type number refers to the base class (possibly this class itself) which
3146 contains the vtable pointer for the current class.
3147
3148 This function is called when we have parsed all the method declarations,
3149 so we can look for the vptr base class info. */
3150
3151 static int
3152 read_tilde_fields (struct stab_field_info *fip, const char **pp,
3153 struct type *type, struct objfile *objfile)
3154 {
3155 const char *p;
3156
3157 STABS_CONTINUE (pp, objfile);
3158
3159 /* If we are positioned at a ';', then skip it. */
3160 if (**pp == ';')
3161 {
3162 (*pp)++;
3163 }
3164
3165 if (**pp == '~')
3166 {
3167 (*pp)++;
3168
3169 if (**pp == '=' || **pp == '+' || **pp == '-')
3170 {
3171 /* Obsolete flags that used to indicate the presence
3172 of constructors and/or destructors. */
3173 (*pp)++;
3174 }
3175
3176 /* Read either a '%' or the final ';'. */
3177 if (*(*pp)++ == '%')
3178 {
3179 /* The next number is the type number of the base class
3180 (possibly our own class) which supplies the vtable for
3181 this class. Parse it out, and search that class to find
3182 its vtable pointer, and install those into TYPE_VPTR_BASETYPE
3183 and TYPE_VPTR_FIELDNO. */
3184
3185 struct type *t;
3186 int i;
3187
3188 t = read_type (pp, objfile);
3189 p = (*pp)++;
3190 while (*p != '\0' && *p != ';')
3191 {
3192 p++;
3193 }
3194 if (*p == '\0')
3195 {
3196 /* Premature end of symbol. */
3197 return 0;
3198 }
3199
3200 set_type_vptr_basetype (type, t);
3201 if (type == t) /* Our own class provides vtbl ptr. */
3202 {
3203 for (i = t->num_fields () - 1;
3204 i >= TYPE_N_BASECLASSES (t);
3205 --i)
3206 {
3207 const char *name = TYPE_FIELD_NAME (t, i);
3208
3209 if (!strncmp (name, vptr_name, sizeof (vptr_name) - 2)
3210 && is_cplus_marker (name[sizeof (vptr_name) - 2]))
3211 {
3212 set_type_vptr_fieldno (type, i);
3213 goto gotit;
3214 }
3215 }
3216 /* Virtual function table field not found. */
3217 complaint (_("virtual function table pointer "
3218 "not found when defining class `%s'"),
3219 type->name ());
3220 return 0;
3221 }
3222 else
3223 {
3224 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
3225 }
3226
3227 gotit:
3228 *pp = p + 1;
3229 }
3230 }
3231 return 1;
3232 }
3233
3234 static int
3235 attach_fn_fields_to_type (struct stab_field_info *fip, struct type *type)
3236 {
3237 int n;
3238
3239 for (n = TYPE_NFN_FIELDS (type);
3240 fip->fnlist != NULL;
3241 fip->fnlist = fip->fnlist->next)
3242 {
3243 --n; /* Circumvent Sun3 compiler bug. */
3244 TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
3245 }
3246 return 1;
3247 }
3248
3249 /* Create the vector of fields, and record how big it is.
3250 We need this info to record proper virtual function table information
3251 for this class's virtual functions. */
3252
3253 static int
3254 attach_fields_to_type (struct stab_field_info *fip, struct type *type,
3255 struct objfile *objfile)
3256 {
3257 int nfields = 0;
3258 int non_public_fields = 0;
3259 struct nextfield *scan;
3260
3261 /* Count up the number of fields that we have, as well as taking note of
3262 whether or not there are any non-public fields, which requires us to
3263 allocate and build the private_field_bits and protected_field_bits
3264 bitfields. */
3265
3266 for (scan = fip->list; scan != NULL; scan = scan->next)
3267 {
3268 nfields++;
3269 if (scan->visibility != VISIBILITY_PUBLIC)
3270 {
3271 non_public_fields++;
3272 }
3273 }
3274
3275 /* Now we know how many fields there are, and whether or not there are any
3276 non-public fields. Record the field count, allocate space for the
3277 array of fields, and create blank visibility bitfields if necessary. */
3278
3279 type->set_num_fields (nfields);
3280 type->set_fields
3281 ((struct field *)
3282 TYPE_ALLOC (type, sizeof (struct field) * nfields));
3283 memset (type->fields (), 0, sizeof (struct field) * nfields);
3284
3285 if (non_public_fields)
3286 {
3287 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3288
3289 TYPE_FIELD_PRIVATE_BITS (type) =
3290 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3291 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3292
3293 TYPE_FIELD_PROTECTED_BITS (type) =
3294 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3295 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3296
3297 TYPE_FIELD_IGNORE_BITS (type) =
3298 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3299 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3300 }
3301
3302 /* Copy the saved-up fields into the field vector. Start from the
3303 head of the list, adding to the tail of the field array, so that
3304 they end up in the same order in the array in which they were
3305 added to the list. */
3306
3307 while (nfields-- > 0)
3308 {
3309 type->field (nfields) = fip->list->field;
3310 switch (fip->list->visibility)
3311 {
3312 case VISIBILITY_PRIVATE:
3313 SET_TYPE_FIELD_PRIVATE (type, nfields);
3314 break;
3315
3316 case VISIBILITY_PROTECTED:
3317 SET_TYPE_FIELD_PROTECTED (type, nfields);
3318 break;
3319
3320 case VISIBILITY_IGNORE:
3321 SET_TYPE_FIELD_IGNORE (type, nfields);
3322 break;
3323
3324 case VISIBILITY_PUBLIC:
3325 break;
3326
3327 default:
3328 /* Unknown visibility. Complain and treat it as public. */
3329 {
3330 complaint (_("Unknown visibility `%c' for field"),
3331 fip->list->visibility);
3332 }
3333 break;
3334 }
3335 fip->list = fip->list->next;
3336 }
3337 return 1;
3338 }
3339
3340
3341 /* Complain that the compiler has emitted more than one definition for the
3342 structure type TYPE. */
3343 static void
3344 complain_about_struct_wipeout (struct type *type)
3345 {
3346 const char *name = "";
3347 const char *kind = "";
3348
3349 if (type->name ())
3350 {
3351 name = type->name ();
3352 switch (type->code ())
3353 {
3354 case TYPE_CODE_STRUCT: kind = "struct "; break;
3355 case TYPE_CODE_UNION: kind = "union "; break;
3356 case TYPE_CODE_ENUM: kind = "enum "; break;
3357 default: kind = "";
3358 }
3359 }
3360 else
3361 {
3362 name = "<unknown>";
3363 kind = "";
3364 }
3365
3366 complaint (_("struct/union type gets multiply defined: %s%s"), kind, name);
3367 }
3368
3369 /* Set the length for all variants of a same main_type, which are
3370 connected in the closed chain.
3371
3372 This is something that needs to be done when a type is defined *after*
3373 some cross references to this type have already been read. Consider
3374 for instance the following scenario where we have the following two
3375 stabs entries:
3376
3377 .stabs "t:p(0,21)=*(0,22)=k(0,23)=xsdummy:",160,0,28,-24
3378 .stabs "dummy:T(0,23)=s16x:(0,1),0,3[...]"
3379
3380 A stubbed version of type dummy is created while processing the first
3381 stabs entry. The length of that type is initially set to zero, since
3382 it is unknown at this point. Also, a "constant" variation of type
3383 "dummy" is created as well (this is the "(0,22)=k(0,23)" section of
3384 the stabs line).
3385
3386 The second stabs entry allows us to replace the stubbed definition
3387 with the real definition. However, we still need to adjust the length
3388 of the "constant" variation of that type, as its length was left
3389 untouched during the main type replacement... */
3390
3391 static void
3392 set_length_in_type_chain (struct type *type)
3393 {
3394 struct type *ntype = TYPE_CHAIN (type);
3395
3396 while (ntype != type)
3397 {
3398 if (TYPE_LENGTH(ntype) == 0)
3399 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
3400 else
3401 complain_about_struct_wipeout (ntype);
3402 ntype = TYPE_CHAIN (ntype);
3403 }
3404 }
3405
3406 /* Read the description of a structure (or union type) and return an object
3407 describing the type.
3408
3409 PP points to a character pointer that points to the next unconsumed token
3410 in the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
3411 *PP will point to "4a:1,0,32;;".
3412
3413 TYPE points to an incomplete type that needs to be filled in.
3414
3415 OBJFILE points to the current objfile from which the stabs information is
3416 being read. (Note that it is redundant in that TYPE also contains a pointer
3417 to this same objfile, so it might be a good idea to eliminate it. FIXME).
3418 */
3419
3420 static struct type *
3421 read_struct_type (const char **pp, struct type *type, enum type_code type_code,
3422 struct objfile *objfile)
3423 {
3424 struct stab_field_info fi;
3425
3426 /* When describing struct/union/class types in stabs, G++ always drops
3427 all qualifications from the name. So if you've got:
3428 struct A { ... struct B { ... }; ... };
3429 then G++ will emit stabs for `struct A::B' that call it simply
3430 `struct B'. Obviously, if you've got a real top-level definition for
3431 `struct B', or other nested definitions, this is going to cause
3432 problems.
3433
3434 Obviously, GDB can't fix this by itself, but it can at least avoid
3435 scribbling on existing structure type objects when new definitions
3436 appear. */
3437 if (! (type->code () == TYPE_CODE_UNDEF
3438 || TYPE_STUB (type)))
3439 {
3440 complain_about_struct_wipeout (type);
3441
3442 /* It's probably best to return the type unchanged. */
3443 return type;
3444 }
3445
3446 INIT_CPLUS_SPECIFIC (type);
3447 type->set_code (type_code);
3448 TYPE_STUB (type) = 0;
3449
3450 /* First comes the total size in bytes. */
3451
3452 {
3453 int nbits;
3454
3455 TYPE_LENGTH (type) = read_huge_number (pp, 0, &nbits, 0);
3456 if (nbits != 0)
3457 return error_type (pp, objfile);
3458 set_length_in_type_chain (type);
3459 }
3460
3461 /* Now read the baseclasses, if any, read the regular C struct or C++
3462 class member fields, attach the fields to the type, read the C++
3463 member functions, attach them to the type, and then read any tilde
3464 field (baseclass specifier for the class holding the main vtable). */
3465
3466 if (!read_baseclasses (&fi, pp, type, objfile)
3467 || !read_struct_fields (&fi, pp, type, objfile)
3468 || !attach_fields_to_type (&fi, type, objfile)
3469 || !read_member_functions (&fi, pp, type, objfile)
3470 || !attach_fn_fields_to_type (&fi, type)
3471 || !read_tilde_fields (&fi, pp, type, objfile))
3472 {
3473 type = error_type (pp, objfile);
3474 }
3475
3476 return (type);
3477 }
3478
3479 /* Read a definition of an array type,
3480 and create and return a suitable type object.
3481 Also creates a range type which represents the bounds of that
3482 array. */
3483
3484 static struct type *
3485 read_array_type (const char **pp, struct type *type,
3486 struct objfile *objfile)
3487 {
3488 struct type *index_type, *element_type, *range_type;
3489 int lower, upper;
3490 int adjustable = 0;
3491 int nbits;
3492
3493 /* Format of an array type:
3494 "ar<index type>;lower;upper;<array_contents_type>".
3495 OS9000: "arlower,upper;<array_contents_type>".
3496
3497 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3498 for these, produce a type like float[][]. */
3499
3500 {
3501 index_type = read_type (pp, objfile);
3502 if (**pp != ';')
3503 /* Improper format of array type decl. */
3504 return error_type (pp, objfile);
3505 ++*pp;
3506 }
3507
3508 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3509 {
3510 (*pp)++;
3511 adjustable = 1;
3512 }
3513 lower = read_huge_number (pp, ';', &nbits, 0);
3514
3515 if (nbits != 0)
3516 return error_type (pp, objfile);
3517
3518 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3519 {
3520 (*pp)++;
3521 adjustable = 1;
3522 }
3523 upper = read_huge_number (pp, ';', &nbits, 0);
3524 if (nbits != 0)
3525 return error_type (pp, objfile);
3526
3527 element_type = read_type (pp, objfile);
3528
3529 if (adjustable)
3530 {
3531 lower = 0;
3532 upper = -1;
3533 }
3534
3535 range_type =
3536 create_static_range_type (NULL, index_type, lower, upper);
3537 type = create_array_type (type, element_type, range_type);
3538
3539 return type;
3540 }
3541
3542
3543 /* Read a definition of an enumeration type,
3544 and create and return a suitable type object.
3545 Also defines the symbols that represent the values of the type. */
3546
3547 static struct type *
3548 read_enum_type (const char **pp, struct type *type,
3549 struct objfile *objfile)
3550 {
3551 struct gdbarch *gdbarch = objfile->arch ();
3552 const char *p;
3553 char *name;
3554 long n;
3555 struct symbol *sym;
3556 int nsyms = 0;
3557 struct pending **symlist;
3558 struct pending *osyms, *syms;
3559 int o_nsyms;
3560 int nbits;
3561 int unsigned_enum = 1;
3562
3563 #if 0
3564 /* FIXME! The stabs produced by Sun CC merrily define things that ought
3565 to be file-scope, between N_FN entries, using N_LSYM. What's a mother
3566 to do? For now, force all enum values to file scope. */
3567 if (within_function)
3568 symlist = get_local_symbols ();
3569 else
3570 #endif
3571 symlist = get_file_symbols ();
3572 osyms = *symlist;
3573 o_nsyms = osyms ? osyms->nsyms : 0;
3574
3575 /* The aix4 compiler emits an extra field before the enum members;
3576 my guess is it's a type of some sort. Just ignore it. */
3577 if (**pp == '-')
3578 {
3579 /* Skip over the type. */
3580 while (**pp != ':')
3581 (*pp)++;
3582
3583 /* Skip over the colon. */
3584 (*pp)++;
3585 }
3586
3587 /* Read the value-names and their values.
3588 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
3589 A semicolon or comma instead of a NAME means the end. */
3590 while (**pp && **pp != ';' && **pp != ',')
3591 {
3592 STABS_CONTINUE (pp, objfile);
3593 p = *pp;
3594 while (*p != ':')
3595 p++;
3596 name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
3597 *pp = p + 1;
3598 n = read_huge_number (pp, ',', &nbits, 0);
3599 if (nbits != 0)
3600 return error_type (pp, objfile);
3601
3602 sym = new (&objfile->objfile_obstack) symbol;
3603 sym->set_linkage_name (name);
3604 sym->set_language (get_current_subfile ()->language,
3605 &objfile->objfile_obstack);
3606 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
3607 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
3608 SYMBOL_VALUE (sym) = n;
3609 if (n < 0)
3610 unsigned_enum = 0;
3611 add_symbol_to_list (sym, symlist);
3612 nsyms++;
3613 }
3614
3615 if (**pp == ';')
3616 (*pp)++; /* Skip the semicolon. */
3617
3618 /* Now fill in the fields of the type-structure. */
3619
3620 TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
3621 set_length_in_type_chain (type);
3622 type->set_code (TYPE_CODE_ENUM);
3623 TYPE_STUB (type) = 0;
3624 if (unsigned_enum)
3625 TYPE_UNSIGNED (type) = 1;
3626 type->set_num_fields (nsyms);
3627 type->set_fields
3628 ((struct field *)
3629 TYPE_ALLOC (type, sizeof (struct field) * nsyms));
3630 memset (type->fields (), 0, sizeof (struct field) * nsyms);
3631
3632 /* Find the symbols for the values and put them into the type.
3633 The symbols can be found in the symlist that we put them on
3634 to cause them to be defined. osyms contains the old value
3635 of that symlist; everything up to there was defined by us. */
3636 /* Note that we preserve the order of the enum constants, so
3637 that in something like "enum {FOO, LAST_THING=FOO}" we print
3638 FOO, not LAST_THING. */
3639
3640 for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
3641 {
3642 int last = syms == osyms ? o_nsyms : 0;
3643 int j = syms->nsyms;
3644
3645 for (; --j >= last; --n)
3646 {
3647 struct symbol *xsym = syms->symbol[j];
3648
3649 SYMBOL_TYPE (xsym) = type;
3650 TYPE_FIELD_NAME (type, n) = xsym->linkage_name ();
3651 SET_FIELD_ENUMVAL (type->field (n), SYMBOL_VALUE (xsym));
3652 TYPE_FIELD_BITSIZE (type, n) = 0;
3653 }
3654 if (syms == osyms)
3655 break;
3656 }
3657
3658 return type;
3659 }
3660
3661 /* Sun's ACC uses a somewhat saner method for specifying the builtin
3662 typedefs in every file (for int, long, etc):
3663
3664 type = b <signed> <width> <format type>; <offset>; <nbits>
3665 signed = u or s.
3666 optional format type = c or b for char or boolean.
3667 offset = offset from high order bit to start bit of type.
3668 width is # bytes in object of this type, nbits is # bits in type.
3669
3670 The width/offset stuff appears to be for small objects stored in
3671 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
3672 FIXME. */
3673
3674 static struct type *
3675 read_sun_builtin_type (const char **pp, int typenums[2], struct objfile *objfile)
3676 {
3677 int type_bits;
3678 int nbits;
3679 int unsigned_type;
3680 int boolean_type = 0;
3681
3682 switch (**pp)
3683 {
3684 case 's':
3685 unsigned_type = 0;
3686 break;
3687 case 'u':
3688 unsigned_type = 1;
3689 break;
3690 default:
3691 return error_type (pp, objfile);
3692 }
3693 (*pp)++;
3694
3695 /* For some odd reason, all forms of char put a c here. This is strange
3696 because no other type has this honor. We can safely ignore this because
3697 we actually determine 'char'acterness by the number of bits specified in
3698 the descriptor.
3699 Boolean forms, e.g Fortran logical*X, put a b here. */
3700
3701 if (**pp == 'c')
3702 (*pp)++;
3703 else if (**pp == 'b')
3704 {
3705 boolean_type = 1;
3706 (*pp)++;
3707 }
3708
3709 /* The first number appears to be the number of bytes occupied
3710 by this type, except that unsigned short is 4 instead of 2.
3711 Since this information is redundant with the third number,
3712 we will ignore it. */
3713 read_huge_number (pp, ';', &nbits, 0);
3714 if (nbits != 0)
3715 return error_type (pp, objfile);
3716
3717 /* The second number is always 0, so ignore it too. */
3718 read_huge_number (pp, ';', &nbits, 0);
3719 if (nbits != 0)
3720 return error_type (pp, objfile);
3721
3722 /* The third number is the number of bits for this type. */
3723 type_bits = read_huge_number (pp, 0, &nbits, 0);
3724 if (nbits != 0)
3725 return error_type (pp, objfile);
3726 /* The type *should* end with a semicolon. If it are embedded
3727 in a larger type the semicolon may be the only way to know where
3728 the type ends. If this type is at the end of the stabstring we
3729 can deal with the omitted semicolon (but we don't have to like
3730 it). Don't bother to complain(), Sun's compiler omits the semicolon
3731 for "void". */
3732 if (**pp == ';')
3733 ++(*pp);
3734
3735 if (type_bits == 0)
3736 {
3737 struct type *type = init_type (objfile, TYPE_CODE_VOID,
3738 TARGET_CHAR_BIT, NULL);
3739 if (unsigned_type)
3740 TYPE_UNSIGNED (type) = 1;
3741 return type;
3742 }
3743
3744 if (boolean_type)
3745 return init_boolean_type (objfile, type_bits, unsigned_type, NULL);
3746 else
3747 return init_integer_type (objfile, type_bits, unsigned_type, NULL);
3748 }
3749
3750 static struct type *
3751 read_sun_floating_type (const char **pp, int typenums[2],
3752 struct objfile *objfile)
3753 {
3754 int nbits;
3755 int details;
3756 int nbytes;
3757 struct type *rettype;
3758
3759 /* The first number has more details about the type, for example
3760 FN_COMPLEX. */
3761 details = read_huge_number (pp, ';', &nbits, 0);
3762 if (nbits != 0)
3763 return error_type (pp, objfile);
3764
3765 /* The second number is the number of bytes occupied by this type. */
3766 nbytes = read_huge_number (pp, ';', &nbits, 0);
3767 if (nbits != 0)
3768 return error_type (pp, objfile);
3769
3770 nbits = nbytes * TARGET_CHAR_BIT;
3771
3772 if (details == NF_COMPLEX || details == NF_COMPLEX16
3773 || details == NF_COMPLEX32)
3774 {
3775 rettype = dbx_init_float_type (objfile, nbits / 2);
3776 return init_complex_type (NULL, rettype);
3777 }
3778
3779 return dbx_init_float_type (objfile, nbits);
3780 }
3781
3782 /* Read a number from the string pointed to by *PP.
3783 The value of *PP is advanced over the number.
3784 If END is nonzero, the character that ends the
3785 number must match END, or an error happens;
3786 and that character is skipped if it does match.
3787 If END is zero, *PP is left pointing to that character.
3788
3789 If TWOS_COMPLEMENT_BITS is set to a strictly positive value and if
3790 the number is represented in an octal representation, assume that
3791 it is represented in a 2's complement representation with a size of
3792 TWOS_COMPLEMENT_BITS.
3793
3794 If the number fits in a long, set *BITS to 0 and return the value.
3795 If not, set *BITS to be the number of bits in the number and return 0.
3796
3797 If encounter garbage, set *BITS to -1 and return 0. */
3798
3799 static long
3800 read_huge_number (const char **pp, int end, int *bits,
3801 int twos_complement_bits)
3802 {
3803 const char *p = *pp;
3804 int sign = 1;
3805 int sign_bit = 0;
3806 long n = 0;
3807 int radix = 10;
3808 char overflow = 0;
3809 int nbits = 0;
3810 int c;
3811 long upper_limit;
3812 int twos_complement_representation = 0;
3813
3814 if (*p == '-')
3815 {
3816 sign = -1;
3817 p++;
3818 }
3819
3820 /* Leading zero means octal. GCC uses this to output values larger
3821 than an int (because that would be hard in decimal). */
3822 if (*p == '0')
3823 {
3824 radix = 8;
3825 p++;
3826 }
3827
3828 /* Skip extra zeros. */
3829 while (*p == '0')
3830 p++;
3831
3832 if (sign > 0 && radix == 8 && twos_complement_bits > 0)
3833 {
3834 /* Octal, possibly signed. Check if we have enough chars for a
3835 negative number. */
3836
3837 size_t len;
3838 const char *p1 = p;
3839
3840 while ((c = *p1) >= '0' && c < '8')
3841 p1++;
3842
3843 len = p1 - p;
3844 if (len > twos_complement_bits / 3
3845 || (twos_complement_bits % 3 == 0
3846 && len == twos_complement_bits / 3))
3847 {
3848 /* Ok, we have enough characters for a signed value, check
3849 for signedness by testing if the sign bit is set. */
3850 sign_bit = (twos_complement_bits % 3 + 2) % 3;
3851 c = *p - '0';
3852 if (c & (1 << sign_bit))
3853 {
3854 /* Definitely signed. */
3855 twos_complement_representation = 1;
3856 sign = -1;
3857 }
3858 }
3859 }
3860
3861 upper_limit = LONG_MAX / radix;
3862
3863 while ((c = *p++) >= '0' && c < ('0' + radix))
3864 {
3865 if (n <= upper_limit)
3866 {
3867 if (twos_complement_representation)
3868 {
3869 /* Octal, signed, twos complement representation. In
3870 this case, n is the corresponding absolute value. */
3871 if (n == 0)
3872 {
3873 long sn = c - '0' - ((2 * (c - '0')) | (2 << sign_bit));
3874
3875 n = -sn;
3876 }
3877 else
3878 {
3879 n *= radix;
3880 n -= c - '0';
3881 }
3882 }
3883 else
3884 {
3885 /* unsigned representation */
3886 n *= radix;
3887 n += c - '0'; /* FIXME this overflows anyway. */
3888 }
3889 }
3890 else
3891 overflow = 1;
3892
3893 /* This depends on large values being output in octal, which is
3894 what GCC does. */
3895 if (radix == 8)
3896 {
3897 if (nbits == 0)
3898 {
3899 if (c == '0')
3900 /* Ignore leading zeroes. */
3901 ;
3902 else if (c == '1')
3903 nbits = 1;
3904 else if (c == '2' || c == '3')
3905 nbits = 2;
3906 else
3907 nbits = 3;
3908 }
3909 else
3910 nbits += 3;
3911 }
3912 }
3913 if (end)
3914 {
3915 if (c && c != end)
3916 {
3917 if (bits != NULL)
3918 *bits = -1;
3919 return 0;
3920 }
3921 }
3922 else
3923 --p;
3924
3925 if (radix == 8 && twos_complement_bits > 0 && nbits > twos_complement_bits)
3926 {
3927 /* We were supposed to parse a number with maximum
3928 TWOS_COMPLEMENT_BITS bits, but something went wrong. */
3929 if (bits != NULL)
3930 *bits = -1;
3931 return 0;
3932 }
3933
3934 *pp = p;
3935 if (overflow)
3936 {
3937 if (nbits == 0)
3938 {
3939 /* Large decimal constants are an error (because it is hard to
3940 count how many bits are in them). */
3941 if (bits != NULL)
3942 *bits = -1;
3943 return 0;
3944 }
3945
3946 /* -0x7f is the same as 0x80. So deal with it by adding one to
3947 the number of bits. Two's complement represention octals
3948 can't have a '-' in front. */
3949 if (sign == -1 && !twos_complement_representation)
3950 ++nbits;
3951 if (bits)
3952 *bits = nbits;
3953 }
3954 else
3955 {
3956 if (bits)
3957 *bits = 0;
3958 return n * sign;
3959 }
3960 /* It's *BITS which has the interesting information. */
3961 return 0;
3962 }
3963
3964 static struct type *
3965 read_range_type (const char **pp, int typenums[2], int type_size,
3966 struct objfile *objfile)
3967 {
3968 struct gdbarch *gdbarch = objfile->arch ();
3969 const char *orig_pp = *pp;
3970 int rangenums[2];
3971 long n2, n3;
3972 int n2bits, n3bits;
3973 int self_subrange;
3974 struct type *result_type;
3975 struct type *index_type = NULL;
3976
3977 /* First comes a type we are a subrange of.
3978 In C it is usually 0, 1 or the type being defined. */
3979 if (read_type_number (pp, rangenums) != 0)
3980 return error_type (pp, objfile);
3981 self_subrange = (rangenums[0] == typenums[0] &&
3982 rangenums[1] == typenums[1]);
3983
3984 if (**pp == '=')
3985 {
3986 *pp = orig_pp;
3987 index_type = read_type (pp, objfile);
3988 }
3989
3990 /* A semicolon should now follow; skip it. */
3991 if (**pp == ';')
3992 (*pp)++;
3993
3994 /* The remaining two operands are usually lower and upper bounds
3995 of the range. But in some special cases they mean something else. */
3996 n2 = read_huge_number (pp, ';', &n2bits, type_size);
3997 n3 = read_huge_number (pp, ';', &n3bits, type_size);
3998
3999 if (n2bits == -1 || n3bits == -1)
4000 return error_type (pp, objfile);
4001
4002 if (index_type)
4003 goto handle_true_range;
4004
4005 /* If limits are huge, must be large integral type. */
4006 if (n2bits != 0 || n3bits != 0)
4007 {
4008 char got_signed = 0;
4009 char got_unsigned = 0;
4010 /* Number of bits in the type. */
4011 int nbits = 0;
4012
4013 /* If a type size attribute has been specified, the bounds of
4014 the range should fit in this size. If the lower bounds needs
4015 more bits than the upper bound, then the type is signed. */
4016 if (n2bits <= type_size && n3bits <= type_size)
4017 {
4018 if (n2bits == type_size && n2bits > n3bits)
4019 got_signed = 1;
4020 else
4021 got_unsigned = 1;
4022 nbits = type_size;
4023 }
4024 /* Range from 0 to <large number> is an unsigned large integral type. */
4025 else if ((n2bits == 0 && n2 == 0) && n3bits != 0)
4026 {
4027 got_unsigned = 1;
4028 nbits = n3bits;
4029 }
4030 /* Range from <large number> to <large number>-1 is a large signed
4031 integral type. Take care of the case where <large number> doesn't
4032 fit in a long but <large number>-1 does. */
4033 else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
4034 || (n2bits != 0 && n3bits == 0
4035 && (n2bits == sizeof (long) * HOST_CHAR_BIT)
4036 && n3 == LONG_MAX))
4037 {
4038 got_signed = 1;
4039 nbits = n2bits;
4040 }
4041
4042 if (got_signed || got_unsigned)
4043 return init_integer_type (objfile, nbits, got_unsigned, NULL);
4044 else
4045 return error_type (pp, objfile);
4046 }
4047
4048 /* A type defined as a subrange of itself, with bounds both 0, is void. */
4049 if (self_subrange && n2 == 0 && n3 == 0)
4050 return init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
4051
4052 /* If n3 is zero and n2 is positive, we want a floating type, and n2
4053 is the width in bytes.
4054
4055 Fortran programs appear to use this for complex types also. To
4056 distinguish between floats and complex, g77 (and others?) seem
4057 to use self-subranges for the complexes, and subranges of int for
4058 the floats.
4059
4060 Also note that for complexes, g77 sets n2 to the size of one of
4061 the member floats, not the whole complex beast. My guess is that
4062 this was to work well with pre-COMPLEX versions of gdb. */
4063
4064 if (n3 == 0 && n2 > 0)
4065 {
4066 struct type *float_type
4067 = dbx_init_float_type (objfile, n2 * TARGET_CHAR_BIT);
4068
4069 if (self_subrange)
4070 return init_complex_type (NULL, float_type);
4071 else
4072 return float_type;
4073 }
4074
4075 /* If the upper bound is -1, it must really be an unsigned integral. */
4076
4077 else if (n2 == 0 && n3 == -1)
4078 {
4079 int bits = type_size;
4080
4081 if (bits <= 0)
4082 {
4083 /* We don't know its size. It is unsigned int or unsigned
4084 long. GCC 2.3.3 uses this for long long too, but that is
4085 just a GDB 3.5 compatibility hack. */
4086 bits = gdbarch_int_bit (gdbarch);
4087 }
4088
4089 return init_integer_type (objfile, bits, 1, NULL);
4090 }
4091
4092 /* Special case: char is defined (Who knows why) as a subrange of
4093 itself with range 0-127. */
4094 else if (self_subrange && n2 == 0 && n3 == 127)
4095 {
4096 struct type *type = init_integer_type (objfile, TARGET_CHAR_BIT,
4097 0, NULL);
4098 TYPE_NOSIGN (type) = 1;
4099 return type;
4100 }
4101 /* We used to do this only for subrange of self or subrange of int. */
4102 else if (n2 == 0)
4103 {
4104 /* -1 is used for the upper bound of (4 byte) "unsigned int" and
4105 "unsigned long", and we already checked for that,
4106 so don't need to test for it here. */
4107
4108 if (n3 < 0)
4109 /* n3 actually gives the size. */
4110 return init_integer_type (objfile, -n3 * TARGET_CHAR_BIT, 1, NULL);
4111
4112 /* Is n3 == 2**(8n)-1 for some integer n? Then it's an
4113 unsigned n-byte integer. But do require n to be a power of
4114 two; we don't want 3- and 5-byte integers flying around. */
4115 {
4116 int bytes;
4117 unsigned long bits;
4118
4119 bits = n3;
4120 for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
4121 bits >>= 8;
4122 if (bits == 0
4123 && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
4124 return init_integer_type (objfile, bytes * TARGET_CHAR_BIT, 1, NULL);
4125 }
4126 }
4127 /* I think this is for Convex "long long". Since I don't know whether
4128 Convex sets self_subrange, I also accept that particular size regardless
4129 of self_subrange. */
4130 else if (n3 == 0 && n2 < 0
4131 && (self_subrange
4132 || n2 == -gdbarch_long_long_bit
4133 (gdbarch) / TARGET_CHAR_BIT))
4134 return init_integer_type (objfile, -n2 * TARGET_CHAR_BIT, 0, NULL);
4135 else if (n2 == -n3 - 1)
4136 {
4137 if (n3 == 0x7f)
4138 return init_integer_type (objfile, 8, 0, NULL);
4139 if (n3 == 0x7fff)
4140 return init_integer_type (objfile, 16, 0, NULL);
4141 if (n3 == 0x7fffffff)
4142 return init_integer_type (objfile, 32, 0, NULL);
4143 }
4144
4145 /* We have a real range type on our hands. Allocate space and
4146 return a real pointer. */
4147 handle_true_range:
4148
4149 if (self_subrange)
4150 index_type = objfile_type (objfile)->builtin_int;
4151 else
4152 index_type = *dbx_lookup_type (rangenums, objfile);
4153 if (index_type == NULL)
4154 {
4155 /* Does this actually ever happen? Is that why we are worrying
4156 about dealing with it rather than just calling error_type? */
4157
4158 complaint (_("base type %d of range type is not defined"), rangenums[1]);
4159
4160 index_type = objfile_type (objfile)->builtin_int;
4161 }
4162
4163 result_type
4164 = create_static_range_type (NULL, index_type, n2, n3);
4165 return (result_type);
4166 }
4167
4168 /* Read in an argument list. This is a list of types, separated by commas
4169 and terminated with END. Return the list of types read in, or NULL
4170 if there is an error. */
4171
4172 static struct field *
4173 read_args (const char **pp, int end, struct objfile *objfile, int *nargsp,
4174 int *varargsp)
4175 {
4176 /* FIXME! Remove this arbitrary limit! */
4177 struct type *types[1024]; /* Allow for fns of 1023 parameters. */
4178 int n = 0, i;
4179 struct field *rval;
4180
4181 while (**pp != end)
4182 {
4183 if (**pp != ',')
4184 /* Invalid argument list: no ','. */
4185 return NULL;
4186 (*pp)++;
4187 STABS_CONTINUE (pp, objfile);
4188 types[n++] = read_type (pp, objfile);
4189 }
4190 (*pp)++; /* get past `end' (the ':' character). */
4191
4192 if (n == 0)
4193 {
4194 /* We should read at least the THIS parameter here. Some broken stabs
4195 output contained `(0,41),(0,42)=@s8;-16;,(0,43),(0,1);' where should
4196 have been present ";-16,(0,43)" reference instead. This way the
4197 excessive ";" marker prematurely stops the parameters parsing. */
4198
4199 complaint (_("Invalid (empty) method arguments"));
4200 *varargsp = 0;
4201 }
4202 else if (types[n - 1]->code () != TYPE_CODE_VOID)
4203 *varargsp = 1;
4204 else
4205 {
4206 n--;
4207 *varargsp = 0;
4208 }
4209
4210 rval = XCNEWVEC (struct field, n);
4211 for (i = 0; i < n; i++)
4212 rval[i].set_type (types[i]);
4213 *nargsp = n;
4214 return rval;
4215 }
4216
4217 /* Common block handling. */
4219
4220 /* List of symbols declared since the last BCOMM. This list is a tail
4221 of local_symbols. When ECOMM is seen, the symbols on the list
4222 are noted so their proper addresses can be filled in later,
4223 using the common block base address gotten from the assembler
4224 stabs. */
4225
4226 static struct pending *common_block;
4227 static int common_block_i;
4228
4229 /* Name of the current common block. We get it from the BCOMM instead of the
4230 ECOMM to match IBM documentation (even though IBM puts the name both places
4231 like everyone else). */
4232 static char *common_block_name;
4233
4234 /* Process a N_BCOMM symbol. The storage for NAME is not guaranteed
4235 to remain after this function returns. */
4236
4237 void
4238 common_block_start (const char *name, struct objfile *objfile)
4239 {
4240 if (common_block_name != NULL)
4241 {
4242 complaint (_("Invalid symbol data: common block within common block"));
4243 }
4244 common_block = *get_local_symbols ();
4245 common_block_i = common_block ? common_block->nsyms : 0;
4246 common_block_name = obstack_strdup (&objfile->objfile_obstack, name);
4247 }
4248
4249 /* Process a N_ECOMM symbol. */
4250
4251 void
4252 common_block_end (struct objfile *objfile)
4253 {
4254 /* Symbols declared since the BCOMM are to have the common block
4255 start address added in when we know it. common_block and
4256 common_block_i point to the first symbol after the BCOMM in
4257 the local_symbols list; copy the list and hang it off the
4258 symbol for the common block name for later fixup. */
4259 int i;
4260 struct symbol *sym;
4261 struct pending *newobj = 0;
4262 struct pending *next;
4263 int j;
4264
4265 if (common_block_name == NULL)
4266 {
4267 complaint (_("ECOMM symbol unmatched by BCOMM"));
4268 return;
4269 }
4270
4271 sym = new (&objfile->objfile_obstack) symbol;
4272 /* Note: common_block_name already saved on objfile_obstack. */
4273 sym->set_linkage_name (common_block_name);
4274 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
4275
4276 /* Now we copy all the symbols which have been defined since the BCOMM. */
4277
4278 /* Copy all the struct pendings before common_block. */
4279 for (next = *get_local_symbols ();
4280 next != NULL && next != common_block;
4281 next = next->next)
4282 {
4283 for (j = 0; j < next->nsyms; j++)
4284 add_symbol_to_list (next->symbol[j], &newobj);
4285 }
4286
4287 /* Copy however much of COMMON_BLOCK we need. If COMMON_BLOCK is
4288 NULL, it means copy all the local symbols (which we already did
4289 above). */
4290
4291 if (common_block != NULL)
4292 for (j = common_block_i; j < common_block->nsyms; j++)
4293 add_symbol_to_list (common_block->symbol[j], &newobj);
4294
4295 SYMBOL_TYPE (sym) = (struct type *) newobj;
4296
4297 /* Should we be putting local_symbols back to what it was?
4298 Does it matter? */
4299
4300 i = hashname (sym->linkage_name ());
4301 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
4302 global_sym_chain[i] = sym;
4303 common_block_name = NULL;
4304 }
4305
4306 /* Add a common block's start address to the offset of each symbol
4307 declared to be in it (by being between a BCOMM/ECOMM pair that uses
4308 the common block name). */
4309
4310 static void
4311 fix_common_block (struct symbol *sym, CORE_ADDR valu)
4312 {
4313 struct pending *next = (struct pending *) SYMBOL_TYPE (sym);
4314
4315 for (; next; next = next->next)
4316 {
4317 int j;
4318
4319 for (j = next->nsyms - 1; j >= 0; j--)
4320 SET_SYMBOL_VALUE_ADDRESS (next->symbol[j],
4321 SYMBOL_VALUE_ADDRESS (next->symbol[j])
4322 + valu);
4323 }
4324 }
4325
4326
4328
4329 /* Add {TYPE, TYPENUMS} to the NONAME_UNDEFS vector.
4330 See add_undefined_type for more details. */
4331
4332 static void
4333 add_undefined_type_noname (struct type *type, int typenums[2])
4334 {
4335 struct nat nat;
4336
4337 nat.typenums[0] = typenums [0];
4338 nat.typenums[1] = typenums [1];
4339 nat.type = type;
4340
4341 if (noname_undefs_length == noname_undefs_allocated)
4342 {
4343 noname_undefs_allocated *= 2;
4344 noname_undefs = (struct nat *)
4345 xrealloc ((char *) noname_undefs,
4346 noname_undefs_allocated * sizeof (struct nat));
4347 }
4348 noname_undefs[noname_undefs_length++] = nat;
4349 }
4350
4351 /* Add TYPE to the UNDEF_TYPES vector.
4352 See add_undefined_type for more details. */
4353
4354 static void
4355 add_undefined_type_1 (struct type *type)
4356 {
4357 if (undef_types_length == undef_types_allocated)
4358 {
4359 undef_types_allocated *= 2;
4360 undef_types = (struct type **)
4361 xrealloc ((char *) undef_types,
4362 undef_types_allocated * sizeof (struct type *));
4363 }
4364 undef_types[undef_types_length++] = type;
4365 }
4366
4367 /* What about types defined as forward references inside of a small lexical
4368 scope? */
4369 /* Add a type to the list of undefined types to be checked through
4370 once this file has been read in.
4371
4372 In practice, we actually maintain two such lists: The first list
4373 (UNDEF_TYPES) is used for types whose name has been provided, and
4374 concerns forward references (eg 'xs' or 'xu' forward references);
4375 the second list (NONAME_UNDEFS) is used for types whose name is
4376 unknown at creation time, because they were referenced through
4377 their type number before the actual type was declared.
4378 This function actually adds the given type to the proper list. */
4379
4380 static void
4381 add_undefined_type (struct type *type, int typenums[2])
4382 {
4383 if (type->name () == NULL)
4384 add_undefined_type_noname (type, typenums);
4385 else
4386 add_undefined_type_1 (type);
4387 }
4388
4389 /* Try to fix all undefined types pushed on the UNDEF_TYPES vector. */
4390
4391 static void
4392 cleanup_undefined_types_noname (struct objfile *objfile)
4393 {
4394 int i;
4395
4396 for (i = 0; i < noname_undefs_length; i++)
4397 {
4398 struct nat nat = noname_undefs[i];
4399 struct type **type;
4400
4401 type = dbx_lookup_type (nat.typenums, objfile);
4402 if (nat.type != *type && (*type)->code () != TYPE_CODE_UNDEF)
4403 {
4404 /* The instance flags of the undefined type are still unset,
4405 and needs to be copied over from the reference type.
4406 Since replace_type expects them to be identical, we need
4407 to set these flags manually before hand. */
4408 TYPE_INSTANCE_FLAGS (nat.type) = TYPE_INSTANCE_FLAGS (*type);
4409 replace_type (nat.type, *type);
4410 }
4411 }
4412
4413 noname_undefs_length = 0;
4414 }
4415
4416 /* Go through each undefined type, see if it's still undefined, and fix it
4417 up if possible. We have two kinds of undefined types:
4418
4419 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
4420 Fix: update array length using the element bounds
4421 and the target type's length.
4422 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
4423 yet defined at the time a pointer to it was made.
4424 Fix: Do a full lookup on the struct/union tag. */
4425
4426 static void
4427 cleanup_undefined_types_1 (void)
4428 {
4429 struct type **type;
4430
4431 /* Iterate over every undefined type, and look for a symbol whose type
4432 matches our undefined type. The symbol matches if:
4433 1. It is a typedef in the STRUCT domain;
4434 2. It has the same name, and same type code;
4435 3. The instance flags are identical.
4436
4437 It is important to check the instance flags, because we have seen
4438 examples where the debug info contained definitions such as:
4439
4440 "foo_t:t30=B31=xefoo_t:"
4441
4442 In this case, we have created an undefined type named "foo_t" whose
4443 instance flags is null (when processing "xefoo_t"), and then created
4444 another type with the same name, but with different instance flags
4445 ('B' means volatile). I think that the definition above is wrong,
4446 since the same type cannot be volatile and non-volatile at the same
4447 time, but we need to be able to cope with it when it happens. The
4448 approach taken here is to treat these two types as different. */
4449
4450 for (type = undef_types; type < undef_types + undef_types_length; type++)
4451 {
4452 switch ((*type)->code ())
4453 {
4454
4455 case TYPE_CODE_STRUCT:
4456 case TYPE_CODE_UNION:
4457 case TYPE_CODE_ENUM:
4458 {
4459 /* Check if it has been defined since. Need to do this here
4460 as well as in check_typedef to deal with the (legitimate in
4461 C though not C++) case of several types with the same name
4462 in different source files. */
4463 if (TYPE_STUB (*type))
4464 {
4465 struct pending *ppt;
4466 int i;
4467 /* Name of the type, without "struct" or "union". */
4468 const char *type_name = (*type)->name ();
4469
4470 if (type_name == NULL)
4471 {
4472 complaint (_("need a type name"));
4473 break;
4474 }
4475 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
4476 {
4477 for (i = 0; i < ppt->nsyms; i++)
4478 {
4479 struct symbol *sym = ppt->symbol[i];
4480
4481 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
4482 && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
4483 && (SYMBOL_TYPE (sym)->code () ==
4484 (*type)->code ())
4485 && (TYPE_INSTANCE_FLAGS (*type) ==
4486 TYPE_INSTANCE_FLAGS (SYMBOL_TYPE (sym)))
4487 && strcmp (sym->linkage_name (), type_name) == 0)
4488 replace_type (*type, SYMBOL_TYPE (sym));
4489 }
4490 }
4491 }
4492 }
4493 break;
4494
4495 default:
4496 {
4497 complaint (_("forward-referenced types left unresolved, "
4498 "type code %d."),
4499 (*type)->code ());
4500 }
4501 break;
4502 }
4503 }
4504
4505 undef_types_length = 0;
4506 }
4507
4508 /* Try to fix all the undefined types we encountered while processing
4509 this unit. */
4510
4511 void
4512 cleanup_undefined_stabs_types (struct objfile *objfile)
4513 {
4514 cleanup_undefined_types_1 ();
4515 cleanup_undefined_types_noname (objfile);
4516 }
4517
4518 /* See stabsread.h. */
4519
4520 void
4521 scan_file_globals (struct objfile *objfile)
4522 {
4523 int hash;
4524 struct symbol *sym, *prev;
4525 struct objfile *resolve_objfile;
4526
4527 /* SVR4 based linkers copy referenced global symbols from shared
4528 libraries to the main executable.
4529 If we are scanning the symbols for a shared library, try to resolve
4530 them from the minimal symbols of the main executable first. */
4531
4532 if (symfile_objfile && objfile != symfile_objfile)
4533 resolve_objfile = symfile_objfile;
4534 else
4535 resolve_objfile = objfile;
4536
4537 while (1)
4538 {
4539 /* Avoid expensive loop through all minimal symbols if there are
4540 no unresolved symbols. */
4541 for (hash = 0; hash < HASHSIZE; hash++)
4542 {
4543 if (global_sym_chain[hash])
4544 break;
4545 }
4546 if (hash >= HASHSIZE)
4547 return;
4548
4549 for (minimal_symbol *msymbol : resolve_objfile->msymbols ())
4550 {
4551 QUIT;
4552
4553 /* Skip static symbols. */
4554 switch (MSYMBOL_TYPE (msymbol))
4555 {
4556 case mst_file_text:
4557 case mst_file_data:
4558 case mst_file_bss:
4559 continue;
4560 default:
4561 break;
4562 }
4563
4564 prev = NULL;
4565
4566 /* Get the hash index and check all the symbols
4567 under that hash index. */
4568
4569 hash = hashname (msymbol->linkage_name ());
4570
4571 for (sym = global_sym_chain[hash]; sym;)
4572 {
4573 if (strcmp (msymbol->linkage_name (), sym->linkage_name ()) == 0)
4574 {
4575 /* Splice this symbol out of the hash chain and
4576 assign the value we have to it. */
4577 if (prev)
4578 {
4579 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
4580 }
4581 else
4582 {
4583 global_sym_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
4584 }
4585
4586 /* Check to see whether we need to fix up a common block. */
4587 /* Note: this code might be executed several times for
4588 the same symbol if there are multiple references. */
4589 if (sym)
4590 {
4591 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
4592 {
4593 fix_common_block (sym,
4594 MSYMBOL_VALUE_ADDRESS (resolve_objfile,
4595 msymbol));
4596 }
4597 else
4598 {
4599 SET_SYMBOL_VALUE_ADDRESS
4600 (sym, MSYMBOL_VALUE_ADDRESS (resolve_objfile,
4601 msymbol));
4602 }
4603 SYMBOL_SECTION (sym) = MSYMBOL_SECTION (msymbol);
4604 }
4605
4606 if (prev)
4607 {
4608 sym = SYMBOL_VALUE_CHAIN (prev);
4609 }
4610 else
4611 {
4612 sym = global_sym_chain[hash];
4613 }
4614 }
4615 else
4616 {
4617 prev = sym;
4618 sym = SYMBOL_VALUE_CHAIN (sym);
4619 }
4620 }
4621 }
4622 if (resolve_objfile == objfile)
4623 break;
4624 resolve_objfile = objfile;
4625 }
4626
4627 /* Change the storage class of any remaining unresolved globals to
4628 LOC_UNRESOLVED and remove them from the chain. */
4629 for (hash = 0; hash < HASHSIZE; hash++)
4630 {
4631 sym = global_sym_chain[hash];
4632 while (sym)
4633 {
4634 prev = sym;
4635 sym = SYMBOL_VALUE_CHAIN (sym);
4636
4637 /* Change the symbol address from the misleading chain value
4638 to address zero. */
4639 SET_SYMBOL_VALUE_ADDRESS (prev, 0);
4640
4641 /* Complain about unresolved common block symbols. */
4642 if (SYMBOL_CLASS (prev) == LOC_STATIC)
4643 SYMBOL_ACLASS_INDEX (prev) = LOC_UNRESOLVED;
4644 else
4645 complaint (_("%s: common block `%s' from "
4646 "global_sym_chain unresolved"),
4647 objfile_name (objfile), prev->print_name ());
4648 }
4649 }
4650 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4651 }
4652
4653 /* Initialize anything that needs initializing when starting to read
4654 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
4655 to a psymtab. */
4656
4657 void
4658 stabsread_init (void)
4659 {
4660 }
4661
4662 /* Initialize anything that needs initializing when a completely new
4663 symbol file is specified (not just adding some symbols from another
4664 file, e.g. a shared library). */
4665
4666 void
4667 stabsread_new_init (void)
4668 {
4669 /* Empty the hash table of global syms looking for values. */
4670 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4671 }
4672
4673 /* Initialize anything that needs initializing at the same time as
4674 start_symtab() is called. */
4675
4676 void
4677 start_stabs (void)
4678 {
4679 global_stabs = NULL; /* AIX COFF */
4680 /* Leave FILENUM of 0 free for builtin types and this file's types. */
4681 n_this_object_header_files = 1;
4682 type_vector_length = 0;
4683 type_vector = (struct type **) 0;
4684 within_function = 0;
4685
4686 /* FIXME: If common_block_name is not already NULL, we should complain(). */
4687 common_block_name = NULL;
4688 }
4689
4690 /* Call after end_symtab(). */
4691
4692 void
4693 end_stabs (void)
4694 {
4695 if (type_vector)
4696 {
4697 xfree (type_vector);
4698 }
4699 type_vector = 0;
4700 type_vector_length = 0;
4701 previous_stab_code = 0;
4702 }
4703
4704 void
4705 finish_global_stabs (struct objfile *objfile)
4706 {
4707 if (global_stabs)
4708 {
4709 patch_block_stabs (*get_global_symbols (), global_stabs, objfile);
4710 xfree (global_stabs);
4711 global_stabs = NULL;
4712 }
4713 }
4714
4715 /* Find the end of the name, delimited by a ':', but don't match
4716 ObjC symbols which look like -[Foo bar::]:bla. */
4717 static const char *
4718 find_name_end (const char *name)
4719 {
4720 const char *s = name;
4721
4722 if (s[0] == '-' || *s == '+')
4723 {
4724 /* Must be an ObjC method symbol. */
4725 if (s[1] != '[')
4726 {
4727 error (_("invalid symbol name \"%s\""), name);
4728 }
4729 s = strchr (s, ']');
4730 if (s == NULL)
4731 {
4732 error (_("invalid symbol name \"%s\""), name);
4733 }
4734 return strchr (s, ':');
4735 }
4736 else
4737 {
4738 return strchr (s, ':');
4739 }
4740 }
4741
4742 /* See stabsread.h. */
4743
4744 int
4745 hashname (const char *name)
4746 {
4747 return fast_hash (name, strlen (name)) % HASHSIZE;
4748 }
4749
4750 /* Initializer for this module. */
4751
4752 void _initialize_stabsread ();
4753 void
4754 _initialize_stabsread ()
4755 {
4756 undef_types_allocated = 20;
4757 undef_types_length = 0;
4758 undef_types = XNEWVEC (struct type *, undef_types_allocated);
4759
4760 noname_undefs_allocated = 20;
4761 noname_undefs_length = 0;
4762 noname_undefs = XNEWVEC (struct nat, noname_undefs_allocated);
4763
4764 stab_register_index = register_symbol_register_impl (LOC_REGISTER,
4765 &stab_register_funcs);
4766 stab_regparm_index = register_symbol_register_impl (LOC_REGPARM_ADDR,
4767 &stab_register_funcs);
4768 }
4769