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