testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / stabsread.c
blob25a7c87b7c8b833375567818072e3d658e38860c
1 /* Support routines for decoding "stabs" debugging information format.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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. */
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 "psymtab.h"
37 #include "libaout.h"
38 #include "aout/aout64.h"
39 #include "gdb-stabs.h"
40 #include "buildsym-legacy.h"
41 #include "complaints.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "language.h"
45 #include "target-float.h"
46 #include "c-lang.h"
47 #include "cp-abi.h"
48 #include "cp-support.h"
49 #include <ctype.h>
50 #include "block.h"
51 #include "filenames.h"
53 #include "stabsread.h"
55 /* See stabsread.h for these globals. */
56 unsigned int symnum;
57 const char *(*next_symbol_text_func) (struct objfile *);
58 unsigned char processing_gcc_compilation;
59 int within_function;
60 struct symbol *global_sym_chain[HASHSIZE];
61 struct pending_stabs *global_stabs;
62 int previous_stab_code;
63 int *this_object_header_files;
64 int n_this_object_header_files;
65 int n_allocated_this_object_header_files;
67 /* See stabsread.h. */
69 const registry<objfile>::key<dbx_symfile_info> dbx_objfile_data_key;
71 dbx_symfile_info::~dbx_symfile_info ()
73 if (header_files != NULL)
75 int i = n_header_files;
76 struct header_file *hfiles = header_files;
78 while (--i >= 0)
80 xfree (hfiles[i].name);
81 xfree (hfiles[i].vector);
83 xfree (hfiles);
87 struct stabs_nextfield
89 struct stabs_nextfield *next;
91 struct field field;
94 struct next_fnfieldlist
96 struct next_fnfieldlist *next;
97 struct fn_fieldlist fn_fieldlist;
100 /* The routines that read and process a complete stabs for a C struct or
101 C++ class pass lists of data member fields and lists of member function
102 fields in an instance of a field_info structure, as defined below.
103 This is part of some reorganization of low level C++ support and is
104 expected to eventually go away... (FIXME) */
106 struct stab_field_info
108 struct stabs_nextfield *list = nullptr;
109 struct next_fnfieldlist *fnlist = nullptr;
111 auto_obstack obstack;
114 static void
115 read_one_struct_field (struct stab_field_info *, const char **, const char *,
116 struct type *, struct objfile *);
118 static struct type *dbx_alloc_type (int[2], struct objfile *);
120 static long read_huge_number (const char **, int, int *, int);
122 static struct type *error_type (const char **, struct objfile *);
124 static void
125 patch_block_stabs (struct pending *, struct pending_stabs *,
126 struct objfile *);
128 static int read_type_number (const char **, int *);
130 static struct type *read_type (const char **, struct objfile *);
132 static struct type *read_range_type (const char **, int[2],
133 int, struct objfile *);
135 static struct type *read_sun_builtin_type (const char **,
136 int[2], struct objfile *);
138 static struct type *read_sun_floating_type (const char **, int[2],
139 struct objfile *);
141 static struct type *read_enum_type (const char **, struct type *, struct objfile *);
143 static struct type *rs6000_builtin_type (int, struct objfile *);
145 static int
146 read_member_functions (struct stab_field_info *, const char **, struct type *,
147 struct objfile *);
149 static int
150 read_struct_fields (struct stab_field_info *, const char **, struct type *,
151 struct objfile *);
153 static int
154 read_baseclasses (struct stab_field_info *, const char **, struct type *,
155 struct objfile *);
157 static int
158 read_tilde_fields (struct stab_field_info *, const char **, struct type *,
159 struct objfile *);
161 static int attach_fn_fields_to_type (struct stab_field_info *, struct type *);
163 static int attach_fields_to_type (struct stab_field_info *, struct type *,
164 struct objfile *);
166 static struct type *read_struct_type (const char **, struct type *,
167 enum type_code,
168 struct objfile *);
170 static struct type *read_array_type (const char **, struct type *,
171 struct objfile *);
173 static struct field *read_args (const char **, int, struct objfile *,
174 int *, int *);
176 static void add_undefined_type (struct type *, int[2]);
178 static int
179 read_cpp_abbrev (struct stab_field_info *, const char **, struct type *,
180 struct objfile *);
182 static const char *find_name_end (const char *name);
184 static int process_reference (const char **string);
186 void stabsread_clear_cache (void);
188 static const char vptr_name[] = "_vptr$";
189 static const char vb_name[] = "_vb$";
191 void
192 unknown_symtype_complaint (const char *arg1)
194 complaint (_("unknown symbol type %s"), arg1);
197 void
198 lbrac_mismatch_complaint (int arg1)
200 complaint (_("N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d"), arg1);
203 void
204 repeated_header_complaint (const char *arg1, int arg2)
206 complaint (_("\"repeated\" header file %s not "
207 "previously seen, at symtab pos %d"),
208 arg1, arg2);
211 static void
212 invalid_cpp_abbrev_complaint (const char *arg1)
214 complaint (_("invalid C++ abbreviation `%s'"), arg1);
217 static void
218 reg_value_complaint (int regnum, int num_regs, const char *sym)
220 complaint (_("bad register number %d (max %d) in symbol %s"),
221 regnum, num_regs - 1, sym);
224 static void
225 stabs_general_complaint (const char *arg1)
227 complaint ("%s", arg1);
230 static void
231 function_outside_compilation_unit_complaint (const char *arg1)
233 complaint (_("function `%s' appears to be defined "
234 "outside of all compilation units"),
235 arg1);
238 /* Make a list of forward references which haven't been defined. */
240 static struct type **undef_types;
241 static int undef_types_allocated;
242 static int undef_types_length;
243 static struct symbol *current_symbol = NULL;
245 /* Make a list of nameless types that are undefined.
246 This happens when another type is referenced by its number
247 before this type is actually defined. For instance "t(0,1)=k(0,2)"
248 and type (0,2) is defined only later. */
250 struct nat
252 int typenums[2];
253 struct type *type;
255 static struct nat *noname_undefs;
256 static int noname_undefs_allocated;
257 static int noname_undefs_length;
259 /* Check for and handle cretinous stabs symbol name continuation! */
260 #define STABS_CONTINUE(pp,objfile) \
261 do { \
262 if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
263 *(pp) = next_symbol_text (objfile); \
264 } while (0)
266 /* Vector of types defined so far, indexed by their type numbers.
267 (In newer sun systems, dbx uses a pair of numbers in parens,
268 as in "(SUBFILENUM,NUMWITHINSUBFILE)".
269 Then these numbers must be translated through the type_translations
270 hash table to get the index into the type vector.) */
272 static struct type **type_vector;
274 /* Number of elements allocated for type_vector currently. */
276 static int type_vector_length;
278 /* Initial size of type vector. Is realloc'd larger if needed, and
279 realloc'd down to the size actually used, when completed. */
281 #define INITIAL_TYPE_VECTOR_LENGTH 160
284 /* Look up a dbx type-number pair. Return the address of the slot
285 where the type for that number-pair is stored.
286 The number-pair is in TYPENUMS.
288 This can be used for finding the type associated with that pair
289 or for associating a new type with the pair. */
291 static struct type **
292 dbx_lookup_type (int typenums[2], struct objfile *objfile)
294 int filenum = typenums[0];
295 int index = typenums[1];
296 unsigned old_len;
297 int real_filenum;
298 struct header_file *f;
299 int f_orig_length;
301 if (filenum == -1) /* -1,-1 is for temporary types. */
302 return 0;
304 if (filenum < 0 || filenum >= n_this_object_header_files)
306 complaint (_("Invalid symbol data: type number "
307 "(%d,%d) out of range at symtab pos %d."),
308 filenum, index, symnum);
309 goto error_return;
312 if (filenum == 0)
314 if (index < 0)
316 /* Caller wants address of address of type. We think
317 that negative (rs6k builtin) types will never appear as
318 "lvalues", (nor should they), so we stuff the real type
319 pointer into a temp, and return its address. If referenced,
320 this will do the right thing. */
321 static struct type *temp_type;
323 temp_type = rs6000_builtin_type (index, objfile);
324 return &temp_type;
327 /* Type is defined outside of header files.
328 Find it in this object file's type vector. */
329 if (index >= type_vector_length)
331 old_len = type_vector_length;
332 if (old_len == 0)
334 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
335 type_vector = XNEWVEC (struct type *, type_vector_length);
337 while (index >= type_vector_length)
339 type_vector_length *= 2;
341 type_vector = (struct type **)
342 xrealloc ((char *) type_vector,
343 (type_vector_length * sizeof (struct type *)));
344 memset (&type_vector[old_len], 0,
345 (type_vector_length - old_len) * sizeof (struct type *));
347 return (&type_vector[index]);
349 else
351 real_filenum = this_object_header_files[filenum];
353 if (real_filenum >= N_HEADER_FILES (objfile))
355 static struct type *temp_type;
357 warning (_("GDB internal error: bad real_filenum"));
359 error_return:
360 temp_type = builtin_type (objfile)->builtin_error;
361 return &temp_type;
364 f = HEADER_FILES (objfile) + real_filenum;
366 f_orig_length = f->length;
367 if (index >= f_orig_length)
369 while (index >= f->length)
371 f->length *= 2;
373 f->vector = (struct type **)
374 xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
375 memset (&f->vector[f_orig_length], 0,
376 (f->length - f_orig_length) * sizeof (struct type *));
378 return (&f->vector[index]);
382 /* Make sure there is a type allocated for type numbers TYPENUMS
383 and return the type object.
384 This can create an empty (zeroed) type object.
385 TYPENUMS may be (-1, -1) to return a new type object that is not
386 put into the type vector, and so may not be referred to by number. */
388 static struct type *
389 dbx_alloc_type (int typenums[2], struct objfile *objfile)
391 struct type **type_addr;
393 if (typenums[0] == -1)
395 return type_allocator (objfile,
396 get_current_subfile ()->language).new_type ();
399 type_addr = dbx_lookup_type (typenums, objfile);
401 /* If we are referring to a type not known at all yet,
402 allocate an empty type for it.
403 We will fill it in later if we find out how. */
404 if (*type_addr == 0)
406 *type_addr = type_allocator (objfile,
407 get_current_subfile ()->language).new_type ();
410 return (*type_addr);
413 /* Allocate a floating-point type of size BITS. */
415 static struct type *
416 dbx_init_float_type (struct objfile *objfile, int bits)
418 struct gdbarch *gdbarch = objfile->arch ();
419 const struct floatformat **format;
420 struct type *type;
422 format = gdbarch_floatformat_for_type (gdbarch, NULL, bits);
423 type_allocator alloc (objfile, get_current_subfile ()->language);
424 if (format)
425 type = init_float_type (alloc, bits, NULL, format);
426 else
427 type = alloc.new_type (TYPE_CODE_ERROR, bits, NULL);
429 return type;
432 /* for all the stabs in a given stab vector, build appropriate types
433 and fix their symbols in given symbol vector. */
435 static void
436 patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
437 struct objfile *objfile)
439 int ii;
440 char *name;
441 const char *pp;
442 struct symbol *sym;
444 if (stabs)
446 /* for all the stab entries, find their corresponding symbols and
447 patch their types! */
449 for (ii = 0; ii < stabs->count; ++ii)
451 name = stabs->stab[ii];
452 pp = (char *) strchr (name, ':');
453 gdb_assert (pp); /* Must find a ':' or game's over. */
454 while (pp[1] == ':')
456 pp += 2;
457 pp = (char *) strchr (pp, ':');
459 sym = find_symbol_in_list (symbols, name, pp - name);
460 if (!sym)
462 /* FIXME-maybe: it would be nice if we noticed whether
463 the variable was defined *anywhere*, not just whether
464 it is defined in this compilation unit. But neither
465 xlc or GCC seem to need such a definition, and until
466 we do psymtabs (so that the minimal symbols from all
467 compilation units are available now), I'm not sure
468 how to get the information. */
470 /* On xcoff, if a global is defined and never referenced,
471 ld will remove it from the executable. There is then
472 a N_GSYM stab for it, but no regular (C_EXT) symbol. */
473 sym = new (&objfile->objfile_obstack) symbol;
474 sym->set_domain (VAR_DOMAIN);
475 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
476 sym->set_linkage_name
477 (obstack_strndup (&objfile->objfile_obstack, name, pp - name));
478 pp += 2;
479 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
481 /* I don't think the linker does this with functions,
482 so as far as I know this is never executed.
483 But it doesn't hurt to check. */
484 sym->set_type
485 (lookup_function_type (read_type (&pp, objfile)));
487 else
489 sym->set_type (read_type (&pp, objfile));
491 add_symbol_to_list (sym, get_global_symbols ());
493 else
495 pp += 2;
496 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
498 sym->set_type
499 (lookup_function_type (read_type (&pp, objfile)));
501 else
503 sym->set_type (read_type (&pp, objfile));
511 /* Read a number by which a type is referred to in dbx data,
512 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
513 Just a single number N is equivalent to (0,N).
514 Return the two numbers by storing them in the vector TYPENUMS.
515 TYPENUMS will then be used as an argument to dbx_lookup_type.
517 Returns 0 for success, -1 for error. */
519 static int
520 read_type_number (const char **pp, int *typenums)
522 int nbits;
524 if (**pp == '(')
526 (*pp)++;
527 typenums[0] = read_huge_number (pp, ',', &nbits, 0);
528 if (nbits != 0)
529 return -1;
530 typenums[1] = read_huge_number (pp, ')', &nbits, 0);
531 if (nbits != 0)
532 return -1;
534 else
536 typenums[0] = 0;
537 typenums[1] = read_huge_number (pp, 0, &nbits, 0);
538 if (nbits != 0)
539 return -1;
541 return 0;
545 /* Free up old header file tables. */
547 void
548 free_header_files (void)
550 if (this_object_header_files)
552 xfree (this_object_header_files);
553 this_object_header_files = NULL;
555 n_allocated_this_object_header_files = 0;
558 /* Allocate new header file tables. */
560 void
561 init_header_files (void)
563 n_allocated_this_object_header_files = 10;
564 this_object_header_files = XNEWVEC (int, 10);
567 /* Close off the current usage of PST.
568 Returns PST or NULL if the partial symtab was empty and thrown away.
570 FIXME: List variables and peculiarities of same. */
572 legacy_psymtab *
573 stabs_end_psymtab (struct objfile *objfile, psymtab_storage *partial_symtabs,
574 legacy_psymtab *pst,
575 const char **include_list, int num_includes,
576 int capping_symbol_offset, unrelocated_addr capping_text,
577 legacy_psymtab **dependency_list,
578 int number_dependencies,
579 int textlow_not_set)
581 int i;
582 struct gdbarch *gdbarch = objfile->arch ();
583 dbx_symfile_info *key = dbx_objfile_data_key. get (objfile);
585 if (capping_symbol_offset != -1)
586 LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
587 pst->set_text_high (capping_text);
589 /* Under Solaris, the N_SO symbols always have a value of 0,
590 instead of the usual address of the .o file. Therefore,
591 we have to do some tricks to fill in texthigh and textlow.
592 The first trick is: if we see a static
593 or global function, and the textlow for the current pst
594 is not set (ie: textlow_not_set), then we use that function's
595 address for the textlow of the pst. */
597 /* Now, to fill in texthigh, we remember the last function seen
598 in the .o file. Also, there's a hack in
599 bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
600 to here via the misc_info field. Therefore, we can fill in
601 a reliable texthigh by taking the address plus size of the
602 last function in the file. */
604 if (!pst->text_high_valid && key->ctx.last_function_name
605 && gdbarch_sofun_address_maybe_missing (gdbarch))
607 int n;
609 const char *colon = strchr (key->ctx.last_function_name, ':');
610 if (colon == NULL)
611 n = 0;
612 else
613 n = colon - key->ctx.last_function_name;
614 char *p = (char *) alloca (n + 2);
615 strncpy (p, key->ctx.last_function_name, n);
616 p[n] = 0;
618 bound_minimal_symbol minsym
619 = lookup_minimal_symbol (current_program_space, p, objfile,
620 pst->filename);
621 if (minsym.minsym == NULL)
623 /* Sun Fortran appends an underscore to the minimal symbol name,
624 try again with an appended underscore if the minimal symbol
625 was not found. */
626 p[n] = '_';
627 p[n + 1] = 0;
628 minsym = lookup_minimal_symbol (current_program_space, p, objfile,
629 pst->filename);
632 if (minsym.minsym)
633 pst->set_text_high
634 (unrelocated_addr (CORE_ADDR (minsym.minsym->unrelocated_address ())
635 + minsym.minsym->size ()));
637 key->ctx.last_function_name = NULL;
640 if (!gdbarch_sofun_address_maybe_missing (gdbarch))
642 /* This test will be true if the last .o file is only data. */
643 else if (textlow_not_set)
644 pst->set_text_low (pst->unrelocated_text_high ());
645 else
647 /* If we know our own starting text address, then walk through all other
648 psymtabs for this objfile, and if any didn't know their ending text
649 address, set it to our starting address. Take care to not set our
650 own ending address to our starting address. */
652 for (partial_symtab *p1 : partial_symtabs->range ())
653 if (!p1->text_high_valid && p1->text_low_valid && p1 != pst)
654 p1->set_text_high (pst->unrelocated_text_low ());
657 /* End of kludge for patching Solaris textlow and texthigh. */
659 pst->end ();
661 pst->number_of_dependencies = number_dependencies;
662 if (number_dependencies)
664 pst->dependencies
665 = partial_symtabs->allocate_dependencies (number_dependencies);
666 memcpy (pst->dependencies, dependency_list,
667 number_dependencies * sizeof (legacy_psymtab *));
669 else
670 pst->dependencies = 0;
672 for (i = 0; i < num_includes; i++)
674 legacy_psymtab *subpst =
675 new legacy_psymtab (include_list[i], partial_symtabs, objfile->per_bfd);
677 subpst->read_symtab_private =
678 XOBNEW (&objfile->objfile_obstack, struct symloc);
679 LDSYMOFF (subpst) =
680 LDSYMLEN (subpst) = 0;
682 /* We could save slight bits of space by only making one of these,
683 shared by the entire set of include files. FIXME-someday. */
684 subpst->dependencies =
685 partial_symtabs->allocate_dependencies (1);
686 subpst->dependencies[0] = pst;
687 subpst->number_of_dependencies = 1;
689 subpst->legacy_read_symtab = pst->legacy_read_symtab;
690 subpst->legacy_expand_psymtab = pst->legacy_expand_psymtab;
693 if (num_includes == 0
694 && number_dependencies == 0
695 && pst->empty ()
696 && key->ctx.has_line_numbers == 0)
698 /* Throw away this psymtab, it's empty. */
699 /* Empty psymtabs happen as a result of header files which don't have
700 any symbols in them. There can be a lot of them. But this check
701 is wrong, in that a psymtab with N_SLINE entries but nothing else
702 is not empty, but we don't realize that. Fixing that without slowing
703 things down might be tricky. */
705 partial_symtabs->discard_psymtab (pst);
707 /* Indicate that psymtab was thrown away. */
708 pst = NULL;
710 return pst;
713 /* Set namestring based on nlist. If the string table index is invalid,
714 give a fake name, and print a single error message per symbol file read,
715 rather than abort the symbol reading or flood the user with messages. */
717 static const char *
718 set_namestring (struct objfile *objfile, const struct internal_nlist *nlist)
720 const char *namestring;
721 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
723 if (nlist->n_strx + key->ctx.file_string_table_offset
724 >= DBX_STRINGTAB_SIZE (objfile)
725 || nlist->n_strx + key->ctx.file_string_table_offset < nlist->n_strx)
727 complaint (_("bad string table offset in symbol %d"),
728 symnum);
729 namestring = "<bad string table offset>";
731 else
732 namestring = (nlist->n_strx + key->ctx.file_string_table_offset
733 + DBX_STRINGTAB (objfile));
734 return namestring;
737 static void
738 stabs_seek (int sym_offset, struct objfile *objfile)
740 dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
741 if (key->ctx.stabs_data)
743 key->ctx.symbuf_read += sym_offset;
744 key->ctx.symbuf_left -= sym_offset;
746 else
747 if (bfd_seek (objfile->obfd.get (), sym_offset, SEEK_CUR) != 0)
748 perror_with_name (bfd_get_filename (objfile->obfd.get ()));
751 /* Buffer for reading the symbol table entries. */
752 static struct external_nlist symbuf[4096];
753 static int symbuf_idx;
754 static int symbuf_end;
756 /* Refill the symbol table input buffer
757 and set the variables that control fetching entries from it.
758 Reports an error if no data available.
759 This function can read past the end of the symbol table
760 (into the string table) but this does no harm. */
762 static void
763 fill_symbuf (bfd *sym_bfd, struct objfile *objfile)
765 unsigned int count;
766 int nbytes;
767 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
769 if (key->ctx.stabs_data)
771 nbytes = sizeof (symbuf);
772 if (nbytes > key->ctx.symbuf_left)
773 nbytes = key->ctx.symbuf_left;
774 memcpy (symbuf, key->ctx.stabs_data + key->ctx.symbuf_read, nbytes);
776 else if (key->ctx.symbuf_sections == NULL)
778 count = sizeof (symbuf);
779 nbytes = bfd_read (symbuf, count, sym_bfd);
781 else
783 if (key->ctx.symbuf_left <= 0)
785 file_ptr filepos = (*key->ctx.symbuf_sections)[key->ctx.sect_idx]->filepos;
787 if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
788 perror_with_name (bfd_get_filename (sym_bfd));
789 key->ctx.symbuf_left = bfd_section_size ((*key->ctx.symbuf_sections)[key->ctx.sect_idx]);
790 key->ctx.symbol_table_offset = filepos - key->ctx.symbuf_read;
791 ++key->ctx.sect_idx;
794 count = key->ctx.symbuf_left;
795 if (count > sizeof (symbuf))
796 count = sizeof (symbuf);
797 nbytes = bfd_read (symbuf, count, sym_bfd);
800 if (nbytes < 0)
801 perror_with_name (bfd_get_filename (sym_bfd));
802 else if (nbytes == 0)
803 error (_("Premature end of file reading symbol table"));
804 symbuf_end = nbytes / key->ctx.symbol_size;
805 symbuf_idx = 0;
806 key->ctx.symbuf_left -= nbytes;
807 key->ctx.symbuf_read += nbytes;
810 /* Read in a defined section of a specific object file's symbols. */
812 static void
813 read_ofile_symtab (struct objfile *objfile, legacy_psymtab *pst)
815 const char *namestring;
816 struct external_nlist *bufp;
817 struct internal_nlist nlist;
818 unsigned char type;
819 unsigned max_symnum;
820 bfd *abfd;
821 int sym_offset; /* Offset to start of symbols to read */
822 int sym_size; /* Size of symbols to read */
823 CORE_ADDR text_offset; /* Start of text segment for symbols */
824 int text_size; /* Size of text segment for symbols */
825 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
827 sym_offset = LDSYMOFF (pst);
828 sym_size = LDSYMLEN (pst);
829 text_offset = pst->text_low (objfile);
830 text_size = pst->text_high (objfile) - pst->text_low (objfile);
831 const section_offsets &section_offsets = objfile->section_offsets;
833 key->ctx.stringtab_global = DBX_STRINGTAB (objfile);
834 set_last_source_file (NULL);
836 abfd = objfile->obfd.get ();
837 symbuf_end = symbuf_idx = 0;
838 key->ctx.symbuf_read = 0;
839 key->ctx.symbuf_left = sym_offset + sym_size;
841 /* It is necessary to actually read one symbol *before* the start
842 of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
843 occurs before the N_SO symbol.
845 Detecting this in read_stabs_symtab
846 would slow down initial readin, so we look for it here instead. */
847 if (!key->ctx.processing_acc_compilation && sym_offset >= (int) key->ctx.symbol_size)
849 stabs_seek (sym_offset - key->ctx.symbol_size, objfile);
850 fill_symbuf (abfd, objfile);
851 bufp = &symbuf[symbuf_idx++];
852 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
853 OBJSTAT (objfile, n_stabs++);
855 namestring = set_namestring (objfile, &nlist);
857 processing_gcc_compilation = 0;
858 if (nlist.n_type == N_TEXT)
860 const char *tempstring = namestring;
862 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
863 processing_gcc_compilation = 1;
864 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
865 processing_gcc_compilation = 2;
866 if (*tempstring != '\0'
867 && *tempstring == bfd_get_symbol_leading_char (objfile->obfd.get ()))
868 ++tempstring;
869 if (startswith (tempstring, "__gnu_compiled"))
870 processing_gcc_compilation = 2;
873 else
875 /* The N_SO starting this symtab is the first symbol, so we
876 better not check the symbol before it. I'm not this can
877 happen, but it doesn't hurt to check for it. */
878 stabs_seek (sym_offset, objfile);
879 processing_gcc_compilation = 0;
882 if (symbuf_idx == symbuf_end)
883 fill_symbuf (abfd, objfile);
884 bufp = &symbuf[symbuf_idx];
885 if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
886 error (_("First symbol in segment of executable not a source symbol"));
888 max_symnum = sym_size / key->ctx.symbol_size;
890 for (symnum = 0;
891 symnum < max_symnum;
892 symnum++)
894 QUIT; /* Allow this to be interruptable. */
895 if (symbuf_idx == symbuf_end)
896 fill_symbuf (abfd, objfile);
897 bufp = &symbuf[symbuf_idx++];
898 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
899 OBJSTAT (objfile, n_stabs++);
901 type = bfd_h_get_8 (abfd, bufp->e_type);
903 namestring = set_namestring (objfile, &nlist);
905 if (type & N_STAB)
907 if (sizeof (nlist.n_value) > 4
908 /* We are a 64-bit debugger debugging a 32-bit program. */
909 && (type == N_LSYM || type == N_PSYM))
910 /* We have to be careful with the n_value in the case of N_LSYM
911 and N_PSYM entries, because they are signed offsets from frame
912 pointer, but we actually read them as unsigned 32-bit values.
913 This is not a problem for 32-bit debuggers, for which negative
914 values end up being interpreted correctly (as negative
915 offsets) due to integer overflow.
916 But we need to sign-extend the value for 64-bit debuggers,
917 or we'll end up interpreting negative values as very large
918 positive offsets. */
919 nlist.n_value = (nlist.n_value ^ 0x80000000) - 0x80000000;
920 process_one_symbol (type, nlist.n_desc, nlist.n_value,
921 namestring, section_offsets, objfile,
922 PST_LANGUAGE (pst));
924 /* We skip checking for a new .o or -l file; that should never
925 happen in this routine. */
926 else if (type == N_TEXT)
928 /* I don't think this code will ever be executed, because
929 the GCC_COMPILED_FLAG_SYMBOL usually is right before
930 the N_SO symbol which starts this source file.
931 However, there is no reason not to accept
932 the GCC_COMPILED_FLAG_SYMBOL anywhere. */
934 if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
935 processing_gcc_compilation = 1;
936 else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
937 processing_gcc_compilation = 2;
939 else if (type & N_EXT || type == (unsigned char) N_TEXT
940 || type == (unsigned char) N_NBTEXT)
942 /* Global symbol: see if we came across a dbx definition for
943 a corresponding symbol. If so, store the value. Remove
944 syms from the chain when their values are stored, but
945 search the whole chain, as there may be several syms from
946 different files with the same name. */
947 /* This is probably not true. Since the files will be read
948 in one at a time, each reference to a global symbol will
949 be satisfied in each file as it appears. So we skip this
950 section. */
955 /* In a Solaris elf file, this variable, which comes from the value
956 of the N_SO symbol, will still be 0. Luckily, text_offset, which
957 comes from low text address of PST, is correct. */
958 if (get_last_source_start_addr () == 0)
959 set_last_source_start_addr (text_offset);
961 /* In reordered executables last_source_start_addr may not be the
962 lower bound for this symtab, instead use text_offset which comes
963 from the low text address of PST, which is correct. */
964 if (get_last_source_start_addr () > text_offset)
965 set_last_source_start_addr (text_offset);
967 pst->compunit_symtab = end_compunit_symtab (text_offset + text_size);
969 end_stabs ();
973 static void
974 dbx_expand_psymtab (legacy_psymtab *pst, struct objfile *objfile)
976 gdb_assert (!pst->readin);
977 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
979 /* Read in all partial symtabs on which this one is dependent. */
980 pst->expand_dependencies (objfile);
982 if (LDSYMLEN (pst)) /* Otherwise it's a dummy. */
984 /* Init stuff necessary for reading in symbols */
985 stabsread_init ();
986 scoped_free_pendings free_pending;
987 key->ctx.file_string_table_offset = FILE_STRING_OFFSET (pst);
988 key->ctx.symbol_size = SYMBOL_SIZE (pst);
990 /* Read in this file's symbols. */
991 if (bfd_seek (objfile->obfd.get (), SYMBOL_OFFSET (pst), SEEK_SET) == 0)
992 read_ofile_symtab (objfile, pst);
995 pst->readin = true;
998 /* Invariant: The symbol pointed to by symbuf_idx is the first one
999 that hasn't been swapped. Swap the symbol at the same time
1000 that symbuf_idx is incremented. */
1002 /* dbx allows the text of a symbol name to be continued into the
1003 next symbol name! When such a continuation is encountered
1004 (a \ at the end of the text of a name)
1005 call this function to get the continuation. */
1007 static const char *
1008 dbx_next_symbol_text (struct objfile *objfile)
1010 struct internal_nlist nlist;
1011 dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
1013 if (symbuf_idx == symbuf_end)
1014 fill_symbuf (objfile->obfd.get (), objfile);
1016 symnum++;
1017 INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], objfile->obfd.get ());
1018 OBJSTAT (objfile, n_stabs++);
1020 symbuf_idx++;
1022 return nlist.n_strx + key->ctx.stringtab_global
1023 + key->ctx.file_string_table_offset;
1026 /* Read in all of the symbols for a given psymtab for real.
1027 Be verbose about it if the user wants that. SELF is not NULL. */
1029 static void
1030 stabs_read_symtab (legacy_psymtab *self, struct objfile *objfile)
1032 gdb_assert (!self->readin);
1034 if (LDSYMLEN (self) || self->number_of_dependencies)
1036 next_symbol_text_func = dbx_next_symbol_text;
1037 dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
1040 scoped_restore restore_stabs_data = make_scoped_restore (&key->ctx.stabs_data);
1041 gdb::unique_xmalloc_ptr<gdb_byte> data_holder;
1042 if (DBX_STAB_SECTION (objfile))
1044 key->ctx.stabs_data
1045 = symfile_relocate_debug_section (objfile,
1046 DBX_STAB_SECTION (objfile),
1047 NULL);
1048 data_holder.reset (key->ctx.stabs_data);
1051 self->expand_psymtab (objfile);
1054 /* Match with global symbols. This only needs to be done once,
1055 after all of the symtabs and dependencies have been read in. */
1056 scan_file_globals (objfile);
1060 static void
1061 record_minimal_symbol (minimal_symbol_reader &reader,
1062 const char *name, unrelocated_addr address, int type,
1063 struct objfile *objfile)
1065 enum minimal_symbol_type ms_type;
1066 int section;
1067 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
1069 switch (type)
1071 case N_TEXT | N_EXT:
1072 ms_type = mst_text;
1073 section = SECT_OFF_TEXT (objfile);
1074 break;
1075 case N_DATA | N_EXT:
1076 ms_type = mst_data;
1077 section = SECT_OFF_DATA (objfile);
1078 break;
1079 case N_BSS | N_EXT:
1080 ms_type = mst_bss;
1081 section = SECT_OFF_BSS (objfile);
1082 break;
1083 case N_ABS | N_EXT:
1084 ms_type = mst_abs;
1085 section = -1;
1086 break;
1087 #ifdef N_SETV
1088 case N_SETV | N_EXT:
1089 ms_type = mst_data;
1090 section = SECT_OFF_DATA (objfile);
1091 break;
1092 case N_SETV:
1093 /* I don't think this type actually exists; since a N_SETV is the result
1094 of going over many .o files, it doesn't make sense to have one
1095 file local. */
1096 ms_type = mst_file_data;
1097 section = SECT_OFF_DATA (objfile);
1098 break;
1099 #endif
1100 case N_TEXT:
1101 case N_NBTEXT:
1102 case N_FN:
1103 case N_FN_SEQ:
1104 ms_type = mst_file_text;
1105 section = SECT_OFF_TEXT (objfile);
1106 break;
1107 case N_DATA:
1108 ms_type = mst_file_data;
1110 /* Check for __DYNAMIC, which is used by Sun shared libraries.
1111 Record it as global even if it's local, not global, so
1112 lookup_minimal_symbol can find it. We don't check symbol_leading_char
1113 because for SunOS4 it always is '_'. */
1114 if (strcmp ("__DYNAMIC", name) == 0)
1115 ms_type = mst_data;
1117 /* Same with virtual function tables, both global and static. */
1119 const char *tempstring = name;
1121 if (*tempstring != '\0'
1122 && *tempstring == bfd_get_symbol_leading_char (objfile->obfd.get ()))
1123 ++tempstring;
1124 if (is_vtable_name (tempstring))
1125 ms_type = mst_data;
1127 section = SECT_OFF_DATA (objfile);
1128 break;
1129 case N_BSS:
1130 ms_type = mst_file_bss;
1131 section = SECT_OFF_BSS (objfile);
1132 break;
1133 default:
1134 ms_type = mst_unknown;
1135 section = -1;
1136 break;
1139 if ((ms_type == mst_file_text || ms_type == mst_text)
1140 && address < key->ctx.lowest_text_address)
1141 key->ctx.lowest_text_address = address;
1143 reader.record_with_info (name, address, ms_type, section);
1146 /* Given a name, value pair, find the corresponding
1147 bincl in the list. Return the partial symtab associated
1148 with that header_file_location. */
1150 static legacy_psymtab *
1151 find_corresponding_bincl_psymtab (const char *name, int instance,
1152 struct objfile* objfile)
1154 stabsread_context ctx = dbx_objfile_data_key.get (objfile) -> ctx;
1155 for (const header_file_location &bincl : ctx.bincl_list)
1156 if (bincl.instance == instance
1157 && strcmp (name, bincl.name) == 0)
1158 return bincl.pst;
1160 repeated_header_complaint (name, symnum);
1161 return (legacy_psymtab *) 0;
1164 /* Allocate and partially fill a partial symtab. It will be
1165 completely filled at the end of the symbol list.
1167 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1168 is the address relative to which its symbols are (incremental) or 0
1169 (normal). */
1171 static legacy_psymtab *
1172 start_psymtab (psymtab_storage *partial_symtabs, struct objfile *objfile,
1173 const char *filename, unrelocated_addr textlow, int ldsymoff)
1175 legacy_psymtab *result = new legacy_psymtab (filename, partial_symtabs,
1176 objfile->per_bfd, textlow);
1178 struct dbx_symfile_info *key = dbx_objfile_data_key.get(objfile);
1180 result->read_symtab_private =
1181 XOBNEW (&objfile->objfile_obstack, struct symloc);
1182 LDSYMOFF (result) = ldsymoff;
1183 result->legacy_read_symtab = stabs_read_symtab;
1184 result->legacy_expand_psymtab = dbx_expand_psymtab;
1185 SYMBOL_SIZE (result) = key->ctx.symbol_size;
1186 SYMBOL_OFFSET (result) = key->ctx.symbol_table_offset;
1187 STRING_OFFSET (result) = 0; /* This used to be an uninitialized global. */
1188 FILE_STRING_OFFSET (result) = key->ctx.file_string_table_offset;
1190 /* Deduce the source language from the filename for this psymtab. */
1191 key->ctx.psymtab_language = deduce_language_from_filename (filename);
1192 PST_LANGUAGE (result) = key->ctx.psymtab_language;
1194 return result;
1197 /* See stabsread.h. */
1199 static void
1200 read_stabs_symtab_1 (minimal_symbol_reader &reader,
1201 psymtab_storage *partial_symtabs,
1202 struct objfile *objfile)
1204 struct gdbarch *gdbarch = objfile->arch ();
1205 struct external_nlist *bufp = 0; /* =0 avoids gcc -Wall glitch. */
1206 struct internal_nlist nlist;
1207 CORE_ADDR text_addr;
1208 int text_size;
1209 const char *sym_name;
1210 int sym_len;
1211 unsigned int next_file_string_table_offset = 0;
1212 struct dbx_symfile_info *dbx = dbx_objfile_data_key.get(objfile);
1214 const char *namestring;
1215 int nsl;
1216 int past_first_source_file = 0;
1217 CORE_ADDR last_function_start = 0;
1218 bfd *abfd;
1219 int textlow_not_set;
1220 int data_sect_index;
1222 /* Current partial symtab. */
1223 legacy_psymtab *pst;
1225 /* List of current psymtab's include files. */
1226 const char **psymtab_include_list;
1227 int includes_allocated;
1228 int includes_used;
1230 /* Index within current psymtab dependency list. */
1231 legacy_psymtab **dependency_list;
1232 int dependencies_used, dependencies_allocated;
1234 text_addr = DBX_TEXT_ADDR (objfile);
1235 text_size = DBX_TEXT_SIZE (objfile);
1237 /* FIXME. We probably want to change stringtab_global rather than add this
1238 while processing every symbol entry. FIXME. */
1239 dbx->ctx.file_string_table_offset = 0;
1241 dbx->ctx.stringtab_global = DBX_STRINGTAB (objfile);
1243 pst = (legacy_psymtab *) 0;
1245 includes_allocated = 30;
1246 includes_used = 0;
1247 psymtab_include_list = (const char **) alloca (includes_allocated *
1248 sizeof (const char *));
1250 dependencies_allocated = 30;
1251 dependencies_used = 0;
1252 dependency_list =
1253 (legacy_psymtab **) alloca (dependencies_allocated *
1254 sizeof (legacy_psymtab *));
1256 /* Init bincl list */
1257 std::vector<struct header_file_location> bincl_storage;
1258 scoped_restore restore_bincl_global
1259 = make_scoped_restore (&(dbx->ctx.bincl_list), bincl_storage);
1261 set_last_source_file (NULL);
1263 dbx->ctx.lowest_text_address = (unrelocated_addr) -1;
1265 abfd = objfile->obfd.get ();
1266 symbuf_end = symbuf_idx = 0;
1267 next_symbol_text_func = dbx_next_symbol_text;
1268 textlow_not_set = 1;
1269 dbx->ctx.has_line_numbers = 0;
1271 /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
1272 to global and static variables. The stab for a global or static
1273 variable doesn't give us any indication of which section it's in,
1274 so we can't tell immediately which offset in
1275 objfile->section_offsets we should apply to the variable's
1276 address.
1278 We could certainly find out which section contains the variable
1279 by looking up the variable's unrelocated address with
1280 find_pc_section, but that would be expensive; this is the
1281 function that constructs the partial symbol tables by examining
1282 every symbol in the entire executable, and it's
1283 performance-critical. So that expense would not be welcome. I'm
1284 not sure what to do about this at the moment.
1286 What we have done for years is to simply assume that the .data
1287 section's offset is appropriate for all global and static
1288 variables. Recently, this was expanded to fall back to the .bss
1289 section's offset if there is no .data section, and then to the
1290 .rodata section's offset. */
1291 data_sect_index = objfile->sect_index_data;
1292 if (data_sect_index == -1)
1293 data_sect_index = SECT_OFF_BSS (objfile);
1294 if (data_sect_index == -1)
1295 data_sect_index = SECT_OFF_RODATA (objfile);
1297 /* If data_sect_index is still -1, that's okay. It's perfectly fine
1298 for the file to have no .data, no .bss, and no .text at all, if
1299 it also has no global or static variables. */
1301 for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1303 /* Get the symbol for this run and pull out some info. */
1304 QUIT; /* Allow this to be interruptable. */
1305 if (symbuf_idx == symbuf_end)
1306 fill_symbuf (abfd, objfile);
1307 bufp = &symbuf[symbuf_idx++];
1310 * Special case to speed up readin.
1312 if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
1314 dbx->ctx.has_line_numbers = 1;
1315 continue;
1318 INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1319 OBJSTAT (objfile, n_stabs++);
1321 /* Ok. There is a lot of code duplicated in the rest of this
1322 switch statement (for efficiency reasons). Since I don't
1323 like duplicating code, I will do my penance here, and
1324 describe the code which is duplicated:
1326 *) The assignment to namestring.
1327 *) The call to strchr.
1328 *) The addition of a partial symbol the two partial
1329 symbol lists. This last is a large section of code, so
1330 I've embedded it in the following macro. */
1332 switch (nlist.n_type)
1335 * Standard, external, non-debugger, symbols
1338 case N_TEXT | N_EXT:
1339 case N_NBTEXT | N_EXT:
1340 goto record_it;
1342 case N_DATA | N_EXT:
1343 case N_NBDATA | N_EXT:
1344 goto record_it;
1346 case N_BSS:
1347 case N_BSS | N_EXT:
1348 case N_NBBSS | N_EXT:
1349 case N_SETV | N_EXT: /* FIXME, is this in BSS? */
1350 goto record_it;
1352 case N_ABS | N_EXT:
1353 record_it:
1354 namestring = set_namestring (objfile, &nlist);
1356 record_minimal_symbol (reader, namestring,
1357 unrelocated_addr (nlist.n_value),
1358 nlist.n_type, objfile); /* Always */
1359 continue;
1361 /* Standard, local, non-debugger, symbols. */
1363 case N_NBTEXT:
1365 /* We need to be able to deal with both N_FN or N_TEXT,
1366 because we have no way of knowing whether the sys-supplied ld
1367 or GNU ld was used to make the executable. Sequents throw
1368 in another wrinkle -- they renumbered N_FN. */
1370 case N_FN:
1371 case N_FN_SEQ:
1372 case N_TEXT:
1373 namestring = set_namestring (objfile, &nlist);
1375 if ((namestring[0] == '-' && namestring[1] == 'l')
1376 || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
1377 && namestring[nsl - 2] == '.'))
1379 unrelocated_addr unrel_val = unrelocated_addr (nlist.n_value);
1381 if (past_first_source_file && pst
1382 /* The gould NP1 uses low values for .o and -l symbols
1383 which are not the address. */
1384 && unrel_val >= pst->unrelocated_text_low ())
1386 stabs_end_psymtab (objfile, partial_symtabs,
1387 pst, psymtab_include_list,
1388 includes_used, symnum * dbx->ctx.symbol_size,
1389 unrel_val > pst->unrelocated_text_high ()
1390 ? unrel_val : pst->unrelocated_text_high (),
1391 dependency_list, dependencies_used,
1392 textlow_not_set);
1393 pst = (legacy_psymtab *) 0;
1394 includes_used = 0;
1395 dependencies_used = 0;
1396 dbx->ctx.has_line_numbers = 0;
1398 else
1399 past_first_source_file = 1;
1401 else
1402 goto record_it;
1403 continue;
1405 case N_DATA:
1406 goto record_it;
1408 case N_UNDF | N_EXT:
1409 /* The case (nlist.n_value != 0) is a "Fortran COMMON" symbol.
1410 We used to rely on the target to tell us whether it knows
1411 where the symbol has been relocated to, but none of the
1412 target implementations actually provided that operation.
1413 So we just ignore the symbol, the same way we would do if
1414 we had a target-side symbol lookup which returned no match.
1416 All other symbols (with nlist.n_value == 0), are really
1417 undefined, and so we ignore them too. */
1418 continue;
1420 case N_UNDF:
1421 if (dbx->ctx.processing_acc_compilation && nlist.n_strx == 1)
1423 /* Deal with relative offsets in the string table
1424 used in ELF+STAB under Solaris. If we want to use the
1425 n_strx field, which contains the name of the file,
1426 we must adjust file_string_table_offset *before* calling
1427 set_namestring(). */
1428 past_first_source_file = 1;
1429 dbx->ctx.file_string_table_offset = next_file_string_table_offset;
1430 next_file_string_table_offset =
1431 dbx->ctx.file_string_table_offset + nlist.n_value;
1432 if (next_file_string_table_offset < dbx->ctx.file_string_table_offset)
1433 error (_("string table offset backs up at %d"), symnum);
1434 /* FIXME -- replace error() with complaint. */
1435 continue;
1437 continue;
1439 /* Lots of symbol types we can just ignore. */
1441 case N_ABS:
1442 case N_NBDATA:
1443 case N_NBBSS:
1444 continue;
1446 /* Keep going . . . */
1449 * Special symbol types for GNU
1451 case N_INDR:
1452 case N_INDR | N_EXT:
1453 case N_SETA:
1454 case N_SETA | N_EXT:
1455 case N_SETT:
1456 case N_SETT | N_EXT:
1457 case N_SETD:
1458 case N_SETD | N_EXT:
1459 case N_SETB:
1460 case N_SETB | N_EXT:
1461 case N_SETV:
1462 continue;
1465 * Debugger symbols
1468 case N_SO:
1470 CORE_ADDR valu;
1471 static int prev_so_symnum = -10;
1472 static int first_so_symnum;
1473 const char *p;
1474 static const char *dirname_nso;
1475 int prev_textlow_not_set;
1477 valu = nlist.n_value;
1479 prev_textlow_not_set = textlow_not_set;
1481 /* A zero value is probably an indication for the SunPRO 3.0
1482 compiler. stabs_end_psymtab explicitly tests for zero, so
1483 don't relocate it. */
1485 if (nlist.n_value == 0
1486 && gdbarch_sofun_address_maybe_missing (gdbarch))
1488 textlow_not_set = 1;
1489 valu = 0;
1491 else
1492 textlow_not_set = 0;
1494 past_first_source_file = 1;
1496 if (prev_so_symnum != symnum - 1)
1497 { /* Here if prev stab wasn't N_SO. */
1498 first_so_symnum = symnum;
1500 if (pst)
1502 unrelocated_addr unrel_value = unrelocated_addr (valu);
1503 stabs_end_psymtab (objfile, partial_symtabs,
1504 pst, psymtab_include_list,
1505 includes_used, symnum * dbx->ctx.symbol_size,
1506 unrel_value > pst->unrelocated_text_high ()
1507 ? unrel_value
1508 : pst->unrelocated_text_high (),
1509 dependency_list, dependencies_used,
1510 prev_textlow_not_set);
1511 pst = (legacy_psymtab *) 0;
1512 includes_used = 0;
1513 dependencies_used = 0;
1514 dbx->ctx.has_line_numbers = 0;
1518 prev_so_symnum = symnum;
1520 /* End the current partial symtab and start a new one. */
1522 namestring = set_namestring (objfile, &nlist);
1524 /* Null name means end of .o file. Don't start a new one. */
1525 if (*namestring == '\000')
1526 continue;
1528 /* Some compilers (including gcc) emit a pair of initial N_SOs.
1529 The first one is a directory name; the second the file name.
1530 If pst exists, is empty, and has a filename ending in '/',
1531 we assume the previous N_SO was a directory name. */
1533 p = lbasename (namestring);
1534 if (p != namestring && *p == '\000')
1536 /* Save the directory name SOs locally, then save it into
1537 the psymtab when it's created below. */
1538 dirname_nso = namestring;
1539 continue;
1542 /* Some other compilers (C++ ones in particular) emit useless
1543 SOs for non-existant .c files. We ignore all subsequent SOs
1544 that immediately follow the first. */
1546 if (!pst)
1548 pst = start_psymtab (partial_symtabs, objfile,
1549 namestring,
1550 unrelocated_addr (valu),
1551 first_so_symnum * dbx->ctx.symbol_size);
1552 pst->dirname = dirname_nso;
1553 dirname_nso = NULL;
1555 continue;
1558 case N_BINCL:
1560 enum language tmp_language;
1562 /* Add this bincl to the bincl_list for future EXCLs. No
1563 need to save the string; it'll be around until
1564 read_stabs_symtab function returns. */
1566 namestring = set_namestring (objfile, &nlist);
1567 tmp_language = deduce_language_from_filename (namestring);
1569 /* Only change the psymtab's language if we've learned
1570 something useful (eg. tmp_language is not language_unknown).
1571 In addition, to match what start_subfile does, never change
1572 from C++ to C. */
1573 if (tmp_language != language_unknown
1574 && (tmp_language != language_c
1575 || dbx->ctx.psymtab_language != language_cplus))
1576 dbx->ctx.psymtab_language = tmp_language;
1578 if (pst == NULL)
1580 /* FIXME: we should not get here without a PST to work on.
1581 Attempt to recover. */
1582 complaint (_("N_BINCL %s not in entries for "
1583 "any file, at symtab pos %d"),
1584 namestring, symnum);
1585 continue;
1587 dbx->ctx.bincl_list.emplace_back (namestring, nlist.n_value, pst);
1589 /* Mark down an include file in the current psymtab. */
1591 goto record_include_file;
1594 case N_SOL:
1596 enum language tmp_language;
1598 /* Mark down an include file in the current psymtab. */
1599 namestring = set_namestring (objfile, &nlist);
1600 tmp_language = deduce_language_from_filename (namestring);
1602 /* Only change the psymtab's language if we've learned
1603 something useful (eg. tmp_language is not language_unknown).
1604 In addition, to match what start_subfile does, never change
1605 from C++ to C. */
1606 if (tmp_language != language_unknown
1607 && (tmp_language != language_c
1608 || dbx->ctx.psymtab_language != language_cplus))
1609 dbx->ctx.psymtab_language = tmp_language;
1611 /* In C++, one may expect the same filename to come round many
1612 times, when code is coming alternately from the main file
1613 and from inline functions in other files. So I check to see
1614 if this is a file we've seen before -- either the main
1615 source file, or a previously included file.
1617 This seems to be a lot of time to be spending on N_SOL, but
1618 things like "break c-exp.y:435" need to work (I
1619 suppose the psymtab_include_list could be hashed or put
1620 in a binary tree, if profiling shows this is a major hog). */
1621 if (pst && filename_cmp (namestring, pst->filename) == 0)
1622 continue;
1624 int i;
1626 for (i = 0; i < includes_used; i++)
1627 if (filename_cmp (namestring, psymtab_include_list[i]) == 0)
1629 i = -1;
1630 break;
1632 if (i == -1)
1633 continue;
1636 record_include_file:
1638 psymtab_include_list[includes_used++] = namestring;
1639 if (includes_used >= includes_allocated)
1641 const char **orig = psymtab_include_list;
1643 psymtab_include_list = (const char **)
1644 alloca ((includes_allocated *= 2) * sizeof (const char *));
1645 memcpy (psymtab_include_list, orig,
1646 includes_used * sizeof (const char *));
1648 continue;
1650 case N_LSYM: /* Typedef or automatic variable. */
1651 case N_STSYM: /* Data seg var -- static. */
1652 case N_LCSYM: /* BSS " */
1653 case N_ROSYM: /* Read-only data seg var -- static. */
1654 case N_NBSTS: /* Gould nobase. */
1655 case N_NBLCS: /* symbols. */
1656 case N_FUN:
1657 case N_GSYM: /* Global (extern) variable; can be
1658 data or bss (sigh FIXME). */
1660 /* Following may probably be ignored; I'll leave them here
1661 for now (until I do Pascal and Modula 2 extensions). */
1663 case N_PC: /* I may or may not need this; I
1664 suspect not. */
1665 case N_M2C: /* I suspect that I can ignore this here. */
1666 case N_SCOPE: /* Same. */
1668 const char *p;
1670 namestring = set_namestring (objfile, &nlist);
1672 /* See if this is an end of function stab. */
1673 if (pst && nlist.n_type == N_FUN && *namestring == '\000')
1675 unrelocated_addr valu;
1677 /* It's value is the size (in bytes) of the function for
1678 function relative stabs, or the address of the function's
1679 end for old style stabs. */
1680 valu = unrelocated_addr (nlist.n_value + last_function_start);
1681 if (pst->unrelocated_text_high () == unrelocated_addr (0)
1682 || valu > pst->unrelocated_text_high ())
1683 pst->set_text_high (valu);
1684 break;
1687 p = (char *) strchr (namestring, ':');
1688 if (!p)
1689 continue; /* Not a debugging symbol. */
1691 sym_len = 0;
1692 sym_name = NULL; /* pacify "gcc -Werror" */
1693 if (dbx->ctx.psymtab_language == language_cplus)
1695 std::string name (namestring, p - namestring);
1696 gdb::unique_xmalloc_ptr<char> new_name
1697 = cp_canonicalize_string (name.c_str ());
1698 if (new_name != nullptr)
1700 sym_len = strlen (new_name.get ());
1701 sym_name = obstack_strdup (&objfile->objfile_obstack,
1702 new_name.get ());
1705 else if (dbx->ctx.psymtab_language == language_c)
1707 std::string name (namestring, p - namestring);
1708 gdb::unique_xmalloc_ptr<char> new_name
1709 = c_canonicalize_name (name.c_str ());
1710 if (new_name != nullptr)
1712 sym_len = strlen (new_name.get ());
1713 sym_name = obstack_strdup (&objfile->objfile_obstack,
1714 new_name.get ());
1718 if (sym_len == 0)
1720 sym_name = namestring;
1721 sym_len = p - namestring;
1724 /* Main processing section for debugging symbols which
1725 the initial read through the symbol tables needs to worry
1726 about. If we reach this point, the symbol which we are
1727 considering is definitely one we are interested in.
1728 p must also contain the (valid) index into the namestring
1729 which indicates the debugging type symbol. */
1731 switch (p[1])
1733 case 'S':
1734 if (pst != nullptr)
1735 pst->add_psymbol (std::string_view (sym_name, sym_len), true,
1736 VAR_DOMAIN, LOC_STATIC,
1737 data_sect_index,
1738 psymbol_placement::STATIC,
1739 unrelocated_addr (nlist.n_value),
1740 dbx->ctx.psymtab_language,
1741 partial_symtabs, objfile);
1742 else
1743 complaint (_("static `%*s' appears to be defined "
1744 "outside of all compilation units"),
1745 sym_len, sym_name);
1746 continue;
1748 case 'G':
1749 /* The addresses in these entries are reported to be
1750 wrong. See the code that reads 'G's for symtabs. */
1751 if (pst != nullptr)
1752 pst->add_psymbol (std::string_view (sym_name, sym_len), true,
1753 VAR_DOMAIN, LOC_STATIC,
1754 data_sect_index,
1755 psymbol_placement::GLOBAL,
1756 unrelocated_addr (nlist.n_value),
1757 dbx->ctx.psymtab_language,
1758 partial_symtabs, objfile);
1759 else
1760 complaint (_("global `%*s' appears to be defined "
1761 "outside of all compilation units"),
1762 sym_len, sym_name);
1763 continue;
1765 case 'T':
1766 /* When a 'T' entry is defining an anonymous enum, it
1767 may have a name which is the empty string, or a
1768 single space. Since they're not really defining a
1769 symbol, those shouldn't go in the partial symbol
1770 table. We do pick up the elements of such enums at
1771 'check_enum:', below. */
1772 if (p >= namestring + 2
1773 || (p == namestring + 1
1774 && namestring[0] != ' '))
1776 if (pst != nullptr)
1777 pst->add_psymbol (std::string_view (sym_name, sym_len),
1778 true, STRUCT_DOMAIN, LOC_TYPEDEF, -1,
1779 psymbol_placement::STATIC,
1780 unrelocated_addr (0),
1781 dbx->ctx.psymtab_language,
1782 partial_symtabs, objfile);
1783 else
1784 complaint (_("enum, struct, or union `%*s' appears "
1785 "to be defined outside of all "
1786 "compilation units"),
1787 sym_len, sym_name);
1788 if (p[2] == 't')
1790 /* Also a typedef with the same name. */
1791 if (pst != nullptr)
1792 pst->add_psymbol (std::string_view (sym_name, sym_len),
1793 true, VAR_DOMAIN, LOC_TYPEDEF, -1,
1794 psymbol_placement::STATIC,
1795 unrelocated_addr (0),
1796 dbx->ctx.psymtab_language,
1797 partial_symtabs, objfile);
1798 else
1799 complaint (_("typedef `%*s' appears to be defined "
1800 "outside of all compilation units"),
1801 sym_len, sym_name);
1802 p += 1;
1805 goto check_enum;
1807 case 't':
1808 if (p != namestring) /* a name is there, not just :T... */
1810 if (pst != nullptr)
1811 pst->add_psymbol (std::string_view (sym_name, sym_len),
1812 true, VAR_DOMAIN, LOC_TYPEDEF, -1,
1813 psymbol_placement::STATIC,
1814 unrelocated_addr (0),
1815 dbx->ctx.psymtab_language,
1816 partial_symtabs, objfile);
1817 else
1818 complaint (_("typename `%*s' appears to be defined "
1819 "outside of all compilation units"),
1820 sym_len, sym_name);
1822 check_enum:
1823 /* If this is an enumerated type, we need to
1824 add all the enum constants to the partial symbol
1825 table. This does not cover enums without names, e.g.
1826 "enum {a, b} c;" in C, but fortunately those are
1827 rare. There is no way for GDB to find those from the
1828 enum type without spending too much time on it. Thus
1829 to solve this problem, the compiler needs to put out the
1830 enum in a nameless type. GCC2 does this. */
1832 /* We are looking for something of the form
1833 <name> ":" ("t" | "T") [<number> "="] "e"
1834 {<constant> ":" <value> ","} ";". */
1836 /* Skip over the colon and the 't' or 'T'. */
1837 p += 2;
1838 /* This type may be given a number. Also, numbers can come
1839 in pairs like (0,26). Skip over it. */
1840 while ((*p >= '0' && *p <= '9')
1841 || *p == '(' || *p == ',' || *p == ')'
1842 || *p == '=')
1843 p++;
1845 if (*p++ == 'e')
1847 /* The aix4 compiler emits extra crud before the members. */
1848 if (*p == '-')
1850 /* Skip over the type (?). */
1851 while (*p != ':')
1852 p++;
1854 /* Skip over the colon. */
1855 p++;
1858 /* We have found an enumerated type. */
1859 /* According to comments in read_enum_type
1860 a comma could end it instead of a semicolon.
1861 I don't know where that happens.
1862 Accept either. */
1863 while (*p && *p != ';' && *p != ',')
1865 const char *q;
1867 /* Check for and handle cretinous dbx symbol name
1868 continuation! */
1869 if (*p == '\\' || (*p == '?' && p[1] == '\0'))
1870 p = next_symbol_text (objfile);
1872 /* Point to the character after the name
1873 of the enum constant. */
1874 for (q = p; *q && *q != ':'; q++)
1876 /* Note that the value doesn't matter for
1877 enum constants in psymtabs, just in symtabs. */
1878 if (pst != nullptr)
1879 pst->add_psymbol (std::string_view (p, q - p), true,
1880 VAR_DOMAIN, LOC_CONST, -1,
1881 psymbol_placement::STATIC,
1882 unrelocated_addr (0),
1883 dbx->ctx.psymtab_language,
1884 partial_symtabs, objfile);
1885 else
1886 complaint (_("enum constant `%*s' appears to be defined "
1887 "outside of all compilation units"),
1888 ((int) (q - p)), p);
1889 /* Point past the name. */
1890 p = q;
1891 /* Skip over the value. */
1892 while (*p && *p != ',')
1893 p++;
1894 /* Advance past the comma. */
1895 if (*p)
1896 p++;
1899 continue;
1901 case 'c':
1902 /* Constant, e.g. from "const" in Pascal. */
1903 if (pst != nullptr)
1904 pst->add_psymbol (std::string_view (sym_name, sym_len), true,
1905 VAR_DOMAIN, LOC_CONST, -1,
1906 psymbol_placement::STATIC,
1907 unrelocated_addr (0),
1908 dbx->ctx.psymtab_language,
1909 partial_symtabs, objfile);
1910 else
1911 complaint (_("constant `%*s' appears to be defined "
1912 "outside of all compilation units"),
1913 sym_len, sym_name);
1915 continue;
1917 case 'f':
1918 if (! pst)
1920 std::string name (namestring, (p - namestring));
1921 function_outside_compilation_unit_complaint (name.c_str ());
1923 /* Kludges for ELF/STABS with Sun ACC. */
1924 dbx->ctx.last_function_name = namestring;
1925 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1926 value for the bottom of the text seg in those cases. */
1927 if (nlist.n_value == 0
1928 && gdbarch_sofun_address_maybe_missing (gdbarch))
1930 bound_minimal_symbol minsym
1931 = find_stab_function (namestring,
1932 pst ? pst->filename : NULL, objfile);
1933 if (minsym.minsym != NULL)
1934 nlist.n_value
1935 = CORE_ADDR (minsym.minsym->unrelocated_address ());
1937 if (pst && textlow_not_set
1938 && gdbarch_sofun_address_maybe_missing (gdbarch))
1940 pst->set_text_low (unrelocated_addr (nlist.n_value));
1941 textlow_not_set = 0;
1943 /* End kludge. */
1945 /* Keep track of the start of the last function so we
1946 can handle end of function symbols. */
1947 last_function_start = nlist.n_value;
1949 /* In reordered executables this function may lie outside
1950 the bounds created by N_SO symbols. If that's the case
1951 use the address of this function as the low bound for
1952 the partial symbol table. */
1953 if (pst
1954 && (textlow_not_set
1955 || (unrelocated_addr (nlist.n_value)
1956 < pst->unrelocated_text_low ()
1957 && (nlist.n_value != 0))))
1959 pst->set_text_low (unrelocated_addr (nlist.n_value));
1960 textlow_not_set = 0;
1962 if (pst != nullptr)
1963 pst->add_psymbol (std::string_view (sym_name, sym_len), true,
1964 VAR_DOMAIN, LOC_BLOCK,
1965 SECT_OFF_TEXT (objfile),
1966 psymbol_placement::STATIC,
1967 unrelocated_addr (nlist.n_value),
1968 dbx->ctx.psymtab_language,
1969 partial_symtabs, objfile);
1970 continue;
1972 /* Global functions were ignored here, but now they
1973 are put into the global psymtab like one would expect.
1974 They're also in the minimal symbol table. */
1975 case 'F':
1976 if (! pst)
1978 std::string name (namestring, (p - namestring));
1979 function_outside_compilation_unit_complaint (name.c_str ());
1981 /* Kludges for ELF/STABS with Sun ACC. */
1982 dbx->ctx.last_function_name = namestring;
1983 /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1984 value for the bottom of the text seg in those cases. */
1985 if (nlist.n_value == 0
1986 && gdbarch_sofun_address_maybe_missing (gdbarch))
1988 bound_minimal_symbol minsym
1989 = find_stab_function (namestring,
1990 pst ? pst->filename : NULL, objfile);
1991 if (minsym.minsym != NULL)
1992 nlist.n_value
1993 = CORE_ADDR (minsym.minsym->unrelocated_address ());
1995 if (pst && textlow_not_set
1996 && gdbarch_sofun_address_maybe_missing (gdbarch))
1998 pst->set_text_low (unrelocated_addr (nlist.n_value));
1999 textlow_not_set = 0;
2001 /* End kludge. */
2003 /* Keep track of the start of the last function so we
2004 can handle end of function symbols. */
2005 last_function_start = nlist.n_value;
2007 /* In reordered executables this function may lie outside
2008 the bounds created by N_SO symbols. If that's the case
2009 use the address of this function as the low bound for
2010 the partial symbol table. */
2011 if (pst
2012 && (textlow_not_set
2013 || (unrelocated_addr (nlist.n_value)
2014 < pst->unrelocated_text_low ()
2015 && (nlist.n_value != 0))))
2017 pst->set_text_low (unrelocated_addr (nlist.n_value));
2018 textlow_not_set = 0;
2020 if (pst != nullptr)
2021 pst->add_psymbol (std::string_view (sym_name, sym_len), true,
2022 VAR_DOMAIN, LOC_BLOCK,
2023 SECT_OFF_TEXT (objfile),
2024 psymbol_placement::GLOBAL,
2025 unrelocated_addr (nlist.n_value),
2026 dbx->ctx.psymtab_language,
2027 partial_symtabs, objfile);
2028 continue;
2030 /* Two things show up here (hopefully); static symbols of
2031 local scope (static used inside braces) or extensions
2032 of structure symbols. We can ignore both. */
2033 case 'V':
2034 case '(':
2035 case '0':
2036 case '1':
2037 case '2':
2038 case '3':
2039 case '4':
2040 case '5':
2041 case '6':
2042 case '7':
2043 case '8':
2044 case '9':
2045 case '-':
2046 case '#': /* For symbol identification (used in live ranges). */
2047 continue;
2049 case ':':
2050 /* It is a C++ nested symbol. We don't need to record it
2051 (I don't think); if we try to look up foo::bar::baz,
2052 then symbols for the symtab containing foo should get
2053 read in, I think. */
2054 /* Someone says sun cc puts out symbols like
2055 /foo/baz/maclib::/usr/local/bin/maclib,
2056 which would get here with a symbol type of ':'. */
2057 continue;
2059 default:
2060 /* Unexpected symbol descriptor. The second and subsequent stabs
2061 of a continued stab can show up here. The question is
2062 whether they ever can mimic a normal stab--it would be
2063 nice if not, since we certainly don't want to spend the
2064 time searching to the end of every string looking for
2065 a backslash. */
2067 complaint (_("unknown symbol descriptor `%c'"),
2068 p[1]);
2070 /* Ignore it; perhaps it is an extension that we don't
2071 know about. */
2072 continue;
2076 case N_EXCL:
2078 namestring = set_namestring (objfile, &nlist);
2080 /* Find the corresponding bincl and mark that psymtab on the
2081 psymtab dependency list. */
2083 legacy_psymtab *needed_pst =
2084 find_corresponding_bincl_psymtab (namestring, nlist.n_value, objfile);
2086 /* If this include file was defined earlier in this file,
2087 leave it alone. */
2088 if (needed_pst == pst)
2089 continue;
2091 if (needed_pst)
2093 int i;
2094 int found = 0;
2096 for (i = 0; i < dependencies_used; i++)
2097 if (dependency_list[i] == needed_pst)
2099 found = 1;
2100 break;
2103 /* If it's already in the list, skip the rest. */
2104 if (found)
2105 continue;
2107 dependency_list[dependencies_used++] = needed_pst;
2108 if (dependencies_used >= dependencies_allocated)
2110 legacy_psymtab **orig = dependency_list;
2112 dependency_list =
2113 (legacy_psymtab **)
2114 alloca ((dependencies_allocated *= 2)
2115 * sizeof (legacy_psymtab *));
2116 memcpy (dependency_list, orig,
2117 (dependencies_used
2118 * sizeof (legacy_psymtab *)));
2119 #ifdef DEBUG_INFO
2120 gdb_printf (gdb_stderr,
2121 "Had to reallocate "
2122 "dependency list.\n");
2123 gdb_printf (gdb_stderr,
2124 "New dependencies allocated: %d\n",
2125 dependencies_allocated);
2126 #endif
2130 continue;
2132 case N_ENDM:
2133 /* Solaris 2 end of module, finish current partial symbol
2134 table. stabs_end_psymtab will set the high text address of
2135 PST to the proper value, which is necessary if a module
2136 compiled without debugging info follows this module. */
2137 if (pst && gdbarch_sofun_address_maybe_missing (gdbarch))
2139 stabs_end_psymtab (objfile, partial_symtabs, pst,
2140 psymtab_include_list, includes_used,
2141 symnum * dbx->ctx.symbol_size,
2142 (unrelocated_addr) 0, dependency_list,
2143 dependencies_used, textlow_not_set);
2144 pst = (legacy_psymtab *) 0;
2145 includes_used = 0;
2146 dependencies_used = 0;
2147 dbx->ctx.has_line_numbers = 0;
2149 continue;
2151 case N_RBRAC:
2152 #ifdef HANDLE_RBRAC
2153 HANDLE_RBRAC (nlist.n_value);
2154 continue;
2155 #endif
2156 case N_EINCL:
2157 case N_DSLINE:
2158 case N_BSLINE:
2159 case N_SSYM: /* Claim: Structure or union element.
2160 Hopefully, I can ignore this. */
2161 case N_ENTRY: /* Alternate entry point; can ignore. */
2162 case N_MAIN: /* Can definitely ignore this. */
2163 case N_CATCH: /* These are GNU C++ extensions */
2164 case N_EHDECL: /* that can safely be ignored here. */
2165 case N_LENG:
2166 case N_BCOMM:
2167 case N_ECOMM:
2168 case N_ECOML:
2169 case N_FNAME:
2170 case N_SLINE:
2171 case N_RSYM:
2172 case N_PSYM:
2173 case N_BNSYM:
2174 case N_ENSYM:
2175 case N_LBRAC:
2176 case N_NSYMS: /* Ultrix 4.0: symbol count */
2177 case N_DEFD: /* GNU Modula-2 */
2178 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
2180 case N_OBJ: /* Useless types from Solaris. */
2181 case N_OPT:
2182 case N_PATCH:
2183 /* These symbols aren't interesting; don't worry about them. */
2184 continue;
2186 default:
2187 /* If we haven't found it yet, ignore it. It's probably some
2188 new type we don't know about yet. */
2189 unknown_symtype_complaint (hex_string (nlist.n_type));
2190 continue;
2194 /* If there's stuff to be cleaned up, clean it up. */
2195 if (pst)
2197 /* Don't set high text address of PST lower than it already
2198 is. */
2199 unrelocated_addr text_end
2200 = (unrelocated_addr
2201 ((dbx->ctx.lowest_text_address == (unrelocated_addr) -1
2202 ? text_addr
2203 : CORE_ADDR (dbx->ctx.lowest_text_address))
2204 + text_size));
2206 stabs_end_psymtab (objfile, partial_symtabs,
2207 pst, psymtab_include_list, includes_used,
2208 symnum * dbx->ctx.symbol_size,
2209 (text_end > pst->unrelocated_text_high ()
2210 ? text_end : pst->unrelocated_text_high ()),
2211 dependency_list, dependencies_used, textlow_not_set);
2215 /* Scan and build partial symbols for a symbol file.
2216 We have been initialized by a call to dbx_symfile_init, which
2217 put all the relevant info into a "struct dbx_symfile_info",
2218 hung off the objfile structure. */
2220 void
2221 read_stabs_symtab (struct objfile *objfile, symfile_add_flags symfile_flags)
2223 bfd *sym_bfd;
2224 int val;
2225 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
2227 sym_bfd = objfile->obfd.get ();
2229 /* .o and .nlm files are relocatables with text, data and bss segs based at
2230 0. This flag disables special (Solaris stabs-in-elf only) fixups for
2231 symbols with a value of 0. */
2233 key->ctx.symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
2235 val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
2236 if (val < 0)
2237 perror_with_name (objfile_name (objfile));
2239 key->ctx.symbol_size = DBX_SYMBOL_SIZE (objfile);
2240 key->ctx.symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
2242 scoped_free_pendings free_pending;
2244 minimal_symbol_reader reader (objfile);
2246 /* Read stabs data from executable file and define symbols. */
2248 psymbol_functions *psf = new psymbol_functions ();
2249 psymtab_storage *partial_symtabs = psf->get_partial_symtabs ().get ();
2250 objfile->qf.emplace_front (psf);
2251 read_stabs_symtab_1 (reader, partial_symtabs, objfile);
2253 /* Install any minimal symbols that have been collected as the current
2254 minimal symbols for this objfile. */
2256 reader.install ();
2259 /* Record the namespace that the function defined by SYMBOL was
2260 defined in, if necessary. BLOCK is the associated block; use
2261 OBSTACK for allocation. */
2263 static void
2264 cp_set_block_scope (const struct symbol *symbol,
2265 struct block *block,
2266 struct obstack *obstack)
2268 if (symbol->demangled_name () != NULL)
2270 /* Try to figure out the appropriate namespace from the
2271 demangled name. */
2273 /* FIXME: carlton/2003-04-15: If the function in question is
2274 a method of a class, the name will actually include the
2275 name of the class as well. This should be harmless, but
2276 is a little unfortunate. */
2278 const char *name = symbol->demangled_name ();
2279 unsigned int prefix_len = cp_entire_prefix_len (name);
2281 block->set_scope (obstack_strndup (obstack, name, prefix_len),
2282 obstack);
2286 bound_minimal_symbol
2287 find_stab_function (const char *namestring, const char *filename,
2288 struct objfile *objfile)
2290 int n;
2292 const char *colon = strchr (namestring, ':');
2293 if (colon == NULL)
2294 n = 0;
2295 else
2296 n = colon - namestring;
2298 char *p = (char *) alloca (n + 2);
2299 strncpy (p, namestring, n);
2300 p[n] = 0;
2302 bound_minimal_symbol msym
2303 = lookup_minimal_symbol (current_program_space, p, objfile, filename);
2304 if (msym.minsym == NULL)
2306 /* Sun Fortran appends an underscore to the minimal symbol name,
2307 try again with an appended underscore if the minimal symbol
2308 was not found. */
2309 p[n] = '_';
2310 p[n + 1] = 0;
2311 msym
2312 = lookup_minimal_symbol (current_program_space, p, objfile, filename);
2315 if (msym.minsym == NULL && filename != NULL)
2317 /* Try again without the filename. */
2318 p[n] = 0;
2319 msym = lookup_minimal_symbol (current_program_space, p, objfile);
2321 if (msym.minsym == NULL && filename != NULL)
2323 /* And try again for Sun Fortran, but without the filename. */
2324 p[n] = '_';
2325 p[n + 1] = 0;
2326 msym = lookup_minimal_symbol (current_program_space, p, objfile);
2329 return msym;
2332 /* Add header file number I for this object file
2333 at the next successive FILENUM. */
2335 static void
2336 add_this_object_header_file (int i)
2338 if (n_this_object_header_files == n_allocated_this_object_header_files)
2340 n_allocated_this_object_header_files *= 2;
2341 this_object_header_files
2342 = (int *) xrealloc ((char *) this_object_header_files,
2343 n_allocated_this_object_header_files * sizeof (int));
2346 this_object_header_files[n_this_object_header_files++] = i;
2349 /* Add to this file an "old" header file, one already seen in
2350 a previous object file. NAME is the header file's name.
2351 INSTANCE is its instance code, to select among multiple
2352 symbol tables for the same header file. */
2354 static void
2355 add_old_header_file (const char *name, int instance, struct objfile *objfile)
2357 struct header_file *p = HEADER_FILES (objfile);
2358 int i;
2360 for (i = 0; i < N_HEADER_FILES (objfile); i++)
2361 if (filename_cmp (p[i].name, name) == 0 && instance == p[i].instance)
2363 add_this_object_header_file (i);
2364 return;
2366 repeated_header_complaint (name, symnum);
2369 /* Add to this file a "new" header file: definitions for its types follow.
2370 NAME is the header file's name.
2371 Most often this happens only once for each distinct header file,
2372 but not necessarily. If it happens more than once, INSTANCE has
2373 a different value each time, and references to the header file
2374 use INSTANCE values to select among them.
2376 dbx output contains "begin" and "end" markers for each new header file,
2377 but at this level we just need to know which files there have been;
2378 so we record the file when its "begin" is seen and ignore the "end". */
2380 static void
2381 add_new_header_file (const char *name, int instance, struct objfile *objfile)
2383 int i;
2384 struct header_file *hfile;
2386 /* Make sure there is room for one more header file. */
2388 i = N_ALLOCATED_HEADER_FILES (objfile);
2390 if (N_HEADER_FILES (objfile) == i)
2392 if (i == 0)
2394 N_ALLOCATED_HEADER_FILES (objfile) = 10;
2395 HEADER_FILES (objfile) = (struct header_file *)
2396 xmalloc (10 * sizeof (struct header_file));
2398 else
2400 i *= 2;
2401 N_ALLOCATED_HEADER_FILES (objfile) = i;
2402 HEADER_FILES (objfile) = (struct header_file *)
2403 xrealloc ((char *) HEADER_FILES (objfile),
2404 (i * sizeof (struct header_file)));
2408 /* Create an entry for this header file. */
2410 i = N_HEADER_FILES (objfile)++;
2411 hfile = HEADER_FILES (objfile) + i;
2412 hfile->name = xstrdup (name);
2413 hfile->instance = instance;
2414 hfile->length = 10;
2415 hfile->vector = XCNEWVEC (struct type *, 10);
2417 add_this_object_header_file (i);
2420 /* See stabsread.h. */
2422 void
2423 process_one_symbol (int type, int desc, CORE_ADDR valu, const char *name,
2424 const section_offsets &section_offsets,
2425 struct objfile *objfile, enum language language)
2427 struct gdbarch *gdbarch = objfile->arch ();
2428 struct context_stack *newobj;
2429 struct context_stack cstk;
2430 /* This remembers the address of the start of a function. It is
2431 used because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries
2432 are relative to the current function's start address. On systems
2433 other than Solaris 2, this just holds the SECT_OFF_TEXT value,
2434 and is used to relocate these symbol types rather than
2435 SECTION_OFFSETS. */
2436 static CORE_ADDR function_start_offset;
2438 /* This holds the address of the start of a function, without the
2439 system peculiarities of function_start_offset. */
2440 static CORE_ADDR last_function_start;
2442 /* If this is nonzero, we've seen an N_SLINE since the start of the
2443 current function. We use this to tell us to move the first sline
2444 to the beginning of the function regardless of what its given
2445 value is. */
2446 static int sline_found_in_function = 1;
2448 /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this
2449 source file. Used to detect the SunPRO solaris compiler. */
2450 static int n_opt_found;
2452 /* The section index for this symbol. */
2453 int section_index = -1;
2455 struct dbx_symfile_info *key = dbx_objfile_data_key.get (objfile);
2457 /* Something is wrong if we see real data before seeing a source
2458 file name. */
2460 if (get_last_source_file () == NULL && type != (unsigned char) N_SO)
2462 /* Ignore any symbols which appear before an N_SO symbol.
2463 Currently no one puts symbols there, but we should deal
2464 gracefully with the case. A complain()t might be in order,
2465 but this should not be an error (). */
2466 return;
2469 switch (type)
2471 case N_FUN:
2472 case N_FNAME:
2474 if (*name == '\000')
2476 /* This N_FUN marks the end of a function. This closes off
2477 the current block. */
2478 struct block *block;
2480 if (outermost_context_p ())
2482 lbrac_mismatch_complaint (symnum);
2483 break;
2486 /* The following check is added before recording line 0 at
2487 end of function so as to handle hand-generated stabs
2488 which may have an N_FUN stabs at the end of the function,
2489 but no N_SLINE stabs. */
2490 if (sline_found_in_function)
2492 CORE_ADDR addr = last_function_start + valu;
2494 record_line
2495 (get_current_subfile (), 0,
2496 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch, addr)
2497 - objfile->text_section_offset ()));
2500 within_function = 0;
2501 cstk = pop_context ();
2503 /* Make a block for the local symbols within. */
2504 block = finish_block (cstk.name,
2505 cstk.old_blocks, NULL,
2506 cstk.start_addr, cstk.start_addr + valu);
2508 /* For C++, set the block's scope. */
2509 if (cstk.name->language () == language_cplus)
2510 cp_set_block_scope (cstk.name, block, &objfile->objfile_obstack);
2512 /* May be switching to an assembler file which may not be using
2513 block relative stabs, so reset the offset. */
2514 function_start_offset = 0;
2516 break;
2519 sline_found_in_function = 0;
2521 /* Relocate for dynamic loading. */
2522 section_index = SECT_OFF_TEXT (objfile);
2523 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2524 valu = gdbarch_addr_bits_remove (gdbarch, valu);
2525 last_function_start = valu;
2527 goto define_a_symbol;
2529 case N_LBRAC:
2530 /* This "symbol" just indicates the start of an inner lexical
2531 context within a function. */
2533 /* Ignore extra outermost context from SunPRO cc and acc. */
2534 if (n_opt_found && desc == 1)
2535 break;
2537 valu += function_start_offset;
2539 push_context (desc, valu);
2540 break;
2542 case N_RBRAC:
2543 /* This "symbol" just indicates the end of an inner lexical
2544 context that was started with N_LBRAC. */
2546 /* Ignore extra outermost context from SunPRO cc and acc. */
2547 if (n_opt_found && desc == 1)
2548 break;
2550 valu += function_start_offset;
2552 if (outermost_context_p ())
2554 lbrac_mismatch_complaint (symnum);
2555 break;
2558 cstk = pop_context ();
2559 if (desc != cstk.depth)
2560 lbrac_mismatch_complaint (symnum);
2562 if (*get_local_symbols () != NULL)
2564 /* GCC development snapshots from March to December of
2565 2000 would output N_LSYM entries after N_LBRAC
2566 entries. As a consequence, these symbols are simply
2567 discarded. Complain if this is the case. */
2568 complaint (_("misplaced N_LBRAC entry; discarding local "
2569 "symbols which have no enclosing block"));
2571 *get_local_symbols () = cstk.locals;
2573 if (get_context_stack_depth () > 1)
2575 /* This is not the outermost LBRAC...RBRAC pair in the
2576 function, its local symbols preceded it, and are the ones
2577 just recovered from the context stack. Define the block
2578 for them (but don't bother if the block contains no
2579 symbols. Should we complain on blocks without symbols?
2580 I can't think of any useful purpose for them). */
2581 if (*get_local_symbols () != NULL)
2583 /* Muzzle a compiler bug that makes end < start.
2585 ??? Which compilers? Is this ever harmful?. */
2586 if (cstk.start_addr > valu)
2588 complaint (_("block start larger than block end"));
2589 cstk.start_addr = valu;
2591 /* Make a block for the local symbols within. */
2592 finish_block (0, cstk.old_blocks, NULL,
2593 cstk.start_addr, valu);
2596 else
2598 /* This is the outermost LBRAC...RBRAC pair. There is no
2599 need to do anything; leave the symbols that preceded it
2600 to be attached to the function's own block. We need to
2601 indicate that we just moved outside of the function. */
2602 within_function = 0;
2605 break;
2607 case N_FN:
2608 case N_FN_SEQ:
2609 /* This kind of symbol indicates the start of an object file.
2610 Relocate for dynamic loading. */
2611 section_index = SECT_OFF_TEXT (objfile);
2612 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2613 break;
2615 case N_SO:
2616 /* This type of symbol indicates the start of data for one
2617 source file. Finish the symbol table of the previous source
2618 file (if any) and start accumulating a new symbol table.
2619 Relocate for dynamic loading. */
2620 section_index = SECT_OFF_TEXT (objfile);
2621 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2623 n_opt_found = 0;
2625 if (get_last_source_file ())
2627 /* Check if previous symbol was also an N_SO (with some
2628 sanity checks). If so, that one was actually the
2629 directory name, and the current one is the real file
2630 name. Patch things up. */
2631 if (previous_stab_code == (unsigned char) N_SO)
2633 patch_subfile_names (get_current_subfile (), name);
2634 break; /* Ignore repeated SOs. */
2636 end_compunit_symtab (valu);
2637 end_stabs ();
2640 /* Null name means this just marks the end of text for this .o
2641 file. Don't start a new symtab in this case. */
2642 if (*name == '\000')
2643 break;
2645 function_start_offset = 0;
2647 start_stabs ();
2648 start_compunit_symtab (objfile, name, NULL, valu, language);
2649 record_debugformat ("stabs");
2650 break;
2652 case N_SOL:
2653 /* This type of symbol indicates the start of data for a
2654 sub-source-file, one whose contents were copied or included
2655 in the compilation of the main source file (whose name was
2656 given in the N_SO symbol). Relocate for dynamic loading. */
2657 section_index = SECT_OFF_TEXT (objfile);
2658 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2659 start_subfile (name);
2660 break;
2662 case N_BINCL:
2663 push_subfile ();
2664 add_new_header_file (name, valu, objfile);
2665 start_subfile (name);
2666 break;
2668 case N_EINCL:
2669 start_subfile (pop_subfile ());
2670 break;
2672 case N_EXCL:
2673 add_old_header_file (name, valu, objfile);
2674 break;
2676 case N_SLINE:
2677 /* This type of "symbol" really just records one line-number --
2678 core-address correspondence. Enter it in the line list for
2679 this symbol table. */
2681 /* Relocate for dynamic loading and for ELF acc
2682 function-relative symbols. */
2683 valu += function_start_offset;
2685 /* GCC 2.95.3 emits the first N_SLINE stab somewhere in the
2686 middle of the prologue instead of right at the start of the
2687 function. To deal with this we record the address for the
2688 first N_SLINE stab to be the start of the function instead of
2689 the listed location. We really shouldn't to this. When
2690 compiling with optimization, this first N_SLINE stab might be
2691 optimized away. Other (non-GCC) compilers don't emit this
2692 stab at all. There is no real harm in having an extra
2693 numbered line, although it can be a bit annoying for the
2694 user. However, it totally screws up our testsuite.
2696 So for now, keep adjusting the address of the first N_SLINE
2697 stab, but only for code compiled with GCC. */
2699 if (within_function && sline_found_in_function == 0)
2701 CORE_ADDR addr = processing_gcc_compilation == 2 ?
2702 last_function_start : valu;
2704 record_line
2705 (get_current_subfile (), desc,
2706 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch, addr)
2707 - objfile->text_section_offset ()));
2708 sline_found_in_function = 1;
2710 else
2711 record_line
2712 (get_current_subfile (), desc,
2713 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch, valu)
2714 - objfile->text_section_offset ()));
2715 break;
2717 case N_BCOMM:
2718 common_block_start (name, objfile);
2719 break;
2721 case N_ECOMM:
2722 common_block_end (objfile);
2723 break;
2725 /* The following symbol types need to have the appropriate
2726 offset added to their value; then we process symbol
2727 definitions in the name. */
2729 case N_STSYM: /* Static symbol in data segment. */
2730 case N_LCSYM: /* Static symbol in BSS segment. */
2731 case N_ROSYM: /* Static symbol in read-only data segment. */
2732 /* HORRID HACK DEPT. However, it's Sun's furgin' fault.
2733 Solaris 2's stabs-in-elf makes *most* symbols relative but
2734 leaves a few absolute (at least for Solaris 2.1 and version
2735 2.0.1 of the SunPRO compiler). N_STSYM and friends sit on
2736 the fence. .stab "foo:S...",N_STSYM is absolute (ld
2737 relocates it) .stab "foo:V...",N_STSYM is relative (section
2738 base subtracted). This leaves us no choice but to search for
2739 the 'S' or 'V'... (or pass the whole section_offsets stuff
2740 down ONE MORE function call level, which we really don't want
2741 to do). */
2743 const char *p;
2745 /* Normal object file and NLMs have non-zero text seg offsets,
2746 but don't need their static syms offset in this fashion.
2747 XXX - This is really a crock that should be fixed in the
2748 solib handling code so that I don't have to work around it
2749 here. */
2751 if (!key->ctx.symfile_relocatable)
2753 p = strchr (name, ':');
2754 if (p != 0 && p[1] == 'S')
2756 /* The linker relocated it. We don't want to add a
2757 Sun-stabs Tfoo.foo-like offset, but we *do*
2758 want to add whatever solib.c passed to
2759 symbol_file_add as addr (this is known to affect
2760 SunOS 4, and I suspect ELF too). Since there is no
2761 Ttext.text symbol, we can get addr from the text offset. */
2762 section_index = SECT_OFF_TEXT (objfile);
2763 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2764 goto define_a_symbol;
2767 /* Since it's not the kludge case, re-dispatch to the right
2768 handler. */
2769 switch (type)
2771 case N_STSYM:
2772 goto case_N_STSYM;
2773 case N_LCSYM:
2774 goto case_N_LCSYM;
2775 case N_ROSYM:
2776 goto case_N_ROSYM;
2777 default:
2778 internal_error (_("failed internal consistency check"));
2782 case_N_STSYM: /* Static symbol in data segment. */
2783 case N_DSLINE: /* Source line number, data segment. */
2784 section_index = SECT_OFF_DATA (objfile);
2785 valu += section_offsets[SECT_OFF_DATA (objfile)];
2786 goto define_a_symbol;
2788 case_N_LCSYM: /* Static symbol in BSS segment. */
2789 case N_BSLINE: /* Source line number, BSS segment. */
2790 /* N_BROWS: overlaps with N_BSLINE. */
2791 section_index = SECT_OFF_BSS (objfile);
2792 valu += section_offsets[SECT_OFF_BSS (objfile)];
2793 goto define_a_symbol;
2795 case_N_ROSYM: /* Static symbol in read-only data segment. */
2796 section_index = SECT_OFF_RODATA (objfile);
2797 valu += section_offsets[SECT_OFF_RODATA (objfile)];
2798 goto define_a_symbol;
2800 case N_ENTRY: /* Alternate entry point. */
2801 /* Relocate for dynamic loading. */
2802 section_index = SECT_OFF_TEXT (objfile);
2803 valu += section_offsets[SECT_OFF_TEXT (objfile)];
2804 goto define_a_symbol;
2806 /* The following symbol types we don't know how to process.
2807 Handle them in a "default" way, but complain to people who
2808 care. */
2809 default:
2810 case N_CATCH: /* Exception handler catcher. */
2811 case N_EHDECL: /* Exception handler name. */
2812 case N_PC: /* Global symbol in Pascal. */
2813 case N_M2C: /* Modula-2 compilation unit. */
2814 /* N_MOD2: overlaps with N_EHDECL. */
2815 case N_SCOPE: /* Modula-2 scope information. */
2816 case N_ECOML: /* End common (local name). */
2817 case N_NBTEXT: /* Gould Non-Base-Register symbols??? */
2818 case N_NBDATA:
2819 case N_NBBSS:
2820 case N_NBSTS:
2821 case N_NBLCS:
2822 unknown_symtype_complaint (hex_string (type));
2824 define_a_symbol:
2825 [[fallthrough]];
2826 /* These symbol types don't need the address field relocated,
2827 since it is either unused, or is absolute. */
2828 case N_GSYM: /* Global variable. */
2829 case N_NSYMS: /* Number of symbols (Ultrix). */
2830 case N_NOMAP: /* No map? (Ultrix). */
2831 case N_RSYM: /* Register variable. */
2832 case N_DEFD: /* Modula-2 GNU module dependency. */
2833 case N_SSYM: /* Struct or union element. */
2834 case N_LSYM: /* Local symbol in stack. */
2835 case N_PSYM: /* Parameter variable. */
2836 case N_LENG: /* Length of preceding symbol type. */
2837 if (name)
2839 int deftype;
2840 const char *colon_pos = strchr (name, ':');
2842 if (colon_pos == NULL)
2843 deftype = '\0';
2844 else
2845 deftype = colon_pos[1];
2847 switch (deftype)
2849 case 'f':
2850 case 'F':
2851 /* Deal with the SunPRO 3.0 compiler which omits the
2852 address from N_FUN symbols. */
2853 if (type == N_FUN
2854 && valu == section_offsets[SECT_OFF_TEXT (objfile)]
2855 && gdbarch_sofun_address_maybe_missing (gdbarch))
2857 bound_minimal_symbol minsym
2858 = find_stab_function (name, get_last_source_file (),
2859 objfile);
2860 if (minsym.minsym != NULL)
2861 valu = minsym.value_address ();
2864 /* These addresses are absolute. */
2865 function_start_offset = valu;
2867 within_function = 1;
2869 if (get_context_stack_depth () > 1)
2871 complaint (_("unmatched N_LBRAC before symtab pos %d"),
2872 symnum);
2873 break;
2876 if (!outermost_context_p ())
2878 struct block *block;
2880 cstk = pop_context ();
2881 /* Make a block for the local symbols within. */
2882 block = finish_block (cstk.name,
2883 cstk.old_blocks, NULL,
2884 cstk.start_addr, valu);
2886 /* For C++, set the block's scope. */
2887 if (cstk.name->language () == language_cplus)
2888 cp_set_block_scope (cstk.name, block,
2889 &objfile->objfile_obstack);
2892 newobj = push_context (0, valu);
2893 newobj->name = define_symbol (valu, name, desc, type, objfile);
2894 if (newobj->name != nullptr)
2895 newobj->name->set_section_index (section_index);
2896 break;
2898 default:
2900 struct symbol *sym = define_symbol (valu, name, desc, type,
2901 objfile);
2902 if (sym != nullptr)
2903 sym->set_section_index (section_index);
2905 break;
2908 break;
2910 /* We use N_OPT to carry the gcc2_compiled flag. Sun uses it
2911 for a bunch of other flags, too. Someday we may parse their
2912 flags; for now we ignore theirs and hope they'll ignore ours. */
2913 case N_OPT: /* Solaris 2: Compiler options. */
2914 if (name)
2916 if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2918 processing_gcc_compilation = 2;
2920 else
2921 n_opt_found = 1;
2923 break;
2925 case N_MAIN: /* Name of main routine. */
2926 /* FIXME: If one has a symbol file with N_MAIN and then replaces
2927 it with a symbol file with "main" and without N_MAIN. I'm
2928 not sure exactly what rule to follow but probably something
2929 like: N_MAIN takes precedence over "main" no matter what
2930 objfile it is in; If there is more than one N_MAIN, choose
2931 the one in the symfile_objfile; If there is more than one
2932 N_MAIN within a given objfile, complain() and choose
2933 arbitrarily. (kingdon) */
2934 if (name != NULL)
2935 set_objfile_main_name (objfile, name, language_unknown);
2936 break;
2938 /* The following symbol types can be ignored. */
2939 case N_OBJ: /* Solaris 2: Object file dir and name. */
2940 case N_PATCH: /* Solaris 2: Patch Run Time Checker. */
2941 /* N_UNDF: Solaris 2: File separator mark. */
2942 /* N_UNDF: -- we will never encounter it, since we only process
2943 one file's symbols at once. */
2944 case N_ENDM: /* Solaris 2: End of module. */
2945 case N_ALIAS: /* SunPro F77: alias name, ignore for now. */
2946 break;
2949 /* '#' is a GNU C extension to allow one symbol to refer to another
2950 related symbol.
2952 Generally this is used so that an alias can refer to its main
2953 symbol. */
2954 gdb_assert (name);
2955 if (name[0] == '#')
2957 /* Initialize symbol reference names and determine if this is a
2958 definition. If a symbol reference is being defined, go ahead
2959 and add it. Otherwise, just return. */
2961 const char *s = name;
2962 int refnum;
2964 /* If this stab defines a new reference ID that is not on the
2965 reference list, then put it on the reference list.
2967 We go ahead and advance NAME past the reference, even though
2968 it is not strictly necessary at this time. */
2969 refnum = symbol_reference_defined (&s);
2970 if (refnum >= 0)
2971 if (!ref_search (refnum))
2972 ref_add (refnum, 0, name, valu);
2973 name = s;
2976 previous_stab_code = type;
2979 #define VISIBILITY_PRIVATE '0' /* Stabs character for private field */
2980 #define VISIBILITY_PROTECTED '1' /* Stabs character for protected fld */
2981 #define VISIBILITY_PUBLIC '2' /* Stabs character for public field */
2982 #define VISIBILITY_IGNORE '9' /* Optimized out or zero length */
2984 /* Structure for storing pointers to reference definitions for fast lookup
2985 during "process_later". */
2987 struct ref_map
2989 const char *stabs;
2990 CORE_ADDR value;
2991 struct symbol *sym;
2994 #define MAX_CHUNK_REFS 100
2995 #define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
2996 #define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
2998 static struct ref_map *ref_map;
3000 /* Ptr to free cell in chunk's linked list. */
3001 static int ref_count = 0;
3003 /* Number of chunks malloced. */
3004 static int ref_chunk = 0;
3006 /* This file maintains a cache of stabs aliases found in the symbol
3007 table. If the symbol table changes, this cache must be cleared
3008 or we are left holding onto data in invalid obstacks. */
3009 void
3010 stabsread_clear_cache (void)
3012 ref_count = 0;
3013 ref_chunk = 0;
3016 /* Create array of pointers mapping refids to symbols and stab strings.
3017 Add pointers to reference definition symbols and/or their values as we
3018 find them, using their reference numbers as our index.
3019 These will be used later when we resolve references. */
3020 void
3021 ref_add (int refnum, struct symbol *sym, const char *stabs, CORE_ADDR value)
3023 if (ref_count == 0)
3024 ref_chunk = 0;
3025 if (refnum >= ref_count)
3026 ref_count = refnum + 1;
3027 if (ref_count > ref_chunk * MAX_CHUNK_REFS)
3029 int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
3030 int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
3032 ref_map = (struct ref_map *)
3033 xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
3034 memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0,
3035 new_chunks * REF_CHUNK_SIZE);
3036 ref_chunk += new_chunks;
3038 ref_map[refnum].stabs = stabs;
3039 ref_map[refnum].sym = sym;
3040 ref_map[refnum].value = value;
3043 /* Return defined sym for the reference REFNUM. */
3044 struct symbol *
3045 ref_search (int refnum)
3047 if (refnum < 0 || refnum > ref_count)
3048 return 0;
3049 return ref_map[refnum].sym;
3052 /* Parse a reference id in STRING and return the resulting
3053 reference number. Move STRING beyond the reference id. */
3055 static int
3056 process_reference (const char **string)
3058 const char *p;
3059 int refnum = 0;
3061 if (**string != '#')
3062 return 0;
3064 /* Advance beyond the initial '#'. */
3065 p = *string + 1;
3067 /* Read number as reference id. */
3068 while (*p && isdigit (*p))
3070 refnum = refnum * 10 + *p - '0';
3071 p++;
3073 *string = p;
3074 return refnum;
3077 /* If STRING defines a reference, store away a pointer to the reference
3078 definition for later use. Return the reference number. */
3081 symbol_reference_defined (const char **string)
3083 const char *p = *string;
3084 int refnum = 0;
3086 refnum = process_reference (&p);
3088 /* Defining symbols end in '='. */
3089 if (*p == '=')
3091 /* Symbol is being defined here. */
3092 *string = p + 1;
3093 return refnum;
3095 else
3097 /* Must be a reference. Either the symbol has already been defined,
3098 or this is a forward reference to it. */
3099 *string = p;
3100 return -1;
3104 static int
3105 stab_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
3107 int regno = gdbarch_stab_reg_to_regnum (gdbarch, sym->value_longest ());
3109 if (regno < 0 || regno >= gdbarch_num_cooked_regs (gdbarch))
3111 reg_value_complaint (regno, gdbarch_num_cooked_regs (gdbarch),
3112 sym->print_name ());
3114 regno = gdbarch_sp_regnum (gdbarch); /* Known safe, though useless. */
3117 return regno;
3120 static const struct symbol_register_ops stab_register_funcs = {
3121 stab_reg_to_regnum
3124 /* The "aclass" indices for computed symbols. */
3126 static int stab_register_index;
3127 static int stab_regparm_index;
3129 struct symbol *
3130 define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
3131 struct objfile *objfile)
3133 struct gdbarch *gdbarch = objfile->arch ();
3134 struct symbol *sym;
3135 const char *p = find_name_end (string);
3136 int deftype;
3137 int synonym = 0;
3138 int i;
3140 /* We would like to eliminate nameless symbols, but keep their types.
3141 E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
3142 to type 2, but, should not create a symbol to address that type. Since
3143 the symbol will be nameless, there is no way any user can refer to it. */
3145 int nameless;
3147 /* Ignore syms with empty names. */
3148 if (string[0] == 0)
3149 return 0;
3151 /* Ignore old-style symbols from cc -go. */
3152 if (p == 0)
3153 return 0;
3155 while (p[1] == ':')
3157 p += 2;
3158 p = strchr (p, ':');
3159 if (p == NULL)
3161 complaint (
3162 _("Bad stabs string '%s'"), string);
3163 return NULL;
3167 /* If a nameless stab entry, all we need is the type, not the symbol.
3168 e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
3169 nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
3171 current_symbol = sym = new (&objfile->objfile_obstack) symbol;
3173 if (processing_gcc_compilation)
3175 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
3176 number of bytes occupied by a type or object, which we ignore. */
3177 sym->set_line (desc);
3179 else
3181 sym->set_line (0); /* unknown */
3184 sym->set_language (get_current_subfile ()->language,
3185 &objfile->objfile_obstack);
3187 if (is_cplus_marker (string[0]))
3189 /* Special GNU C++ names. */
3190 switch (string[1])
3192 case 't':
3193 sym->set_linkage_name ("this");
3194 break;
3196 case 'v': /* $vtbl_ptr_type */
3197 goto normal;
3199 case 'e':
3200 sym->set_linkage_name ("eh_throw");
3201 break;
3203 case '_':
3204 /* This was an anonymous type that was never fixed up. */
3205 goto normal;
3207 default:
3208 complaint (_("Unknown C++ symbol name `%s'"),
3209 string);
3210 goto normal; /* Do *something* with it. */
3213 else
3215 normal:
3216 gdb::unique_xmalloc_ptr<char> new_name;
3218 if (sym->language () == language_cplus)
3220 std::string name (string, p - string);
3221 new_name = cp_canonicalize_string (name.c_str ());
3223 else if (sym->language () == language_c)
3225 std::string name (string, p - string);
3226 new_name = c_canonicalize_name (name.c_str ());
3228 if (new_name != nullptr)
3229 sym->compute_and_set_names (new_name.get (), true, objfile->per_bfd);
3230 else
3231 sym->compute_and_set_names (std::string_view (string, p - string), true,
3232 objfile->per_bfd);
3234 if (sym->language () == language_cplus)
3235 cp_scan_for_anonymous_namespaces (get_buildsym_compunit (), sym,
3236 objfile);
3239 p++;
3241 /* Determine the type of name being defined. */
3242 #if 0
3243 /* Getting GDB to correctly skip the symbol on an undefined symbol
3244 descriptor and not ever dump core is a very dodgy proposition if
3245 we do things this way. I say the acorn RISC machine can just
3246 fix their compiler. */
3247 /* The Acorn RISC machine's compiler can put out locals that don't
3248 start with "234=" or "(3,4)=", so assume anything other than the
3249 deftypes we know how to handle is a local. */
3250 if (!strchr ("cfFGpPrStTvVXCR", *p))
3251 #else
3252 if (isdigit (*p) || *p == '(' || *p == '-')
3253 #endif
3254 deftype = 'l';
3255 else
3256 deftype = *p++;
3258 switch (deftype)
3260 case 'c':
3261 /* c is a special case, not followed by a type-number.
3262 SYMBOL:c=iVALUE for an integer constant symbol.
3263 SYMBOL:c=rVALUE for a floating constant symbol.
3264 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
3265 e.g. "b:c=e6,0" for "const b = blob1"
3266 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
3267 if (*p != '=')
3269 sym->set_aclass_index (LOC_CONST);
3270 sym->set_type (error_type (&p, objfile));
3271 sym->set_domain (VAR_DOMAIN);
3272 add_symbol_to_list (sym, get_file_symbols ());
3273 return sym;
3275 ++p;
3276 switch (*p++)
3278 case 'r':
3280 gdb_byte *dbl_valu;
3281 struct type *dbl_type;
3283 dbl_type = builtin_type (objfile)->builtin_double;
3284 dbl_valu
3285 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
3286 dbl_type->length ());
3288 target_float_from_string (dbl_valu, dbl_type, std::string (p));
3290 sym->set_type (dbl_type);
3291 sym->set_value_bytes (dbl_valu);
3292 sym->set_aclass_index (LOC_CONST_BYTES);
3294 break;
3295 case 'i':
3297 /* Defining integer constants this way is kind of silly,
3298 since 'e' constants allows the compiler to give not
3299 only the value, but the type as well. C has at least
3300 int, long, unsigned int, and long long as constant
3301 types; other languages probably should have at least
3302 unsigned as well as signed constants. */
3304 sym->set_type (builtin_type (objfile)->builtin_long);
3305 sym->set_value_longest (atoi (p));
3306 sym->set_aclass_index (LOC_CONST);
3308 break;
3310 case 'c':
3312 sym->set_type (builtin_type (objfile)->builtin_char);
3313 sym->set_value_longest (atoi (p));
3314 sym->set_aclass_index (LOC_CONST);
3316 break;
3318 case 's':
3320 struct type *range_type;
3321 int ind = 0;
3322 char quote = *p++;
3323 gdb_byte *string_local = (gdb_byte *) alloca (strlen (p));
3324 gdb_byte *string_value;
3326 if (quote != '\'' && quote != '"')
3328 sym->set_aclass_index (LOC_CONST);
3329 sym->set_type (error_type (&p, objfile));
3330 sym->set_domain (VAR_DOMAIN);
3331 add_symbol_to_list (sym, get_file_symbols ());
3332 return sym;
3335 /* Find matching quote, rejecting escaped quotes. */
3336 while (*p && *p != quote)
3338 if (*p == '\\' && p[1] == quote)
3340 string_local[ind] = (gdb_byte) quote;
3341 ind++;
3342 p += 2;
3344 else if (*p)
3346 string_local[ind] = (gdb_byte) (*p);
3347 ind++;
3348 p++;
3351 if (*p != quote)
3353 sym->set_aclass_index (LOC_CONST);
3354 sym->set_type (error_type (&p, objfile));
3355 sym->set_domain (VAR_DOMAIN);
3356 add_symbol_to_list (sym, get_file_symbols ());
3357 return sym;
3360 /* NULL terminate the string. */
3361 string_local[ind] = 0;
3362 type_allocator alloc (objfile, get_current_subfile ()->language);
3363 range_type
3364 = create_static_range_type (alloc,
3365 builtin_type (objfile)->builtin_int,
3366 0, ind);
3367 sym->set_type
3368 (create_array_type (alloc, builtin_type (objfile)->builtin_char,
3369 range_type));
3370 string_value
3371 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, ind + 1);
3372 memcpy (string_value, string_local, ind + 1);
3373 p++;
3375 sym->set_value_bytes (string_value);
3376 sym->set_aclass_index (LOC_CONST_BYTES);
3378 break;
3380 case 'e':
3381 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
3382 can be represented as integral.
3383 e.g. "b:c=e6,0" for "const b = blob1"
3384 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
3386 sym->set_aclass_index (LOC_CONST);
3387 sym->set_type (read_type (&p, objfile));
3389 if (*p != ',')
3391 sym->set_type (error_type (&p, objfile));
3392 break;
3394 ++p;
3396 /* If the value is too big to fit in an int (perhaps because
3397 it is unsigned), or something like that, we silently get
3398 a bogus value. The type and everything else about it is
3399 correct. Ideally, we should be using whatever we have
3400 available for parsing unsigned and long long values,
3401 however. */
3402 sym->set_value_longest (atoi (p));
3404 break;
3405 default:
3407 sym->set_aclass_index (LOC_CONST);
3408 sym->set_type (error_type (&p, objfile));
3411 sym->set_domain (VAR_DOMAIN);
3412 add_symbol_to_list (sym, get_file_symbols ());
3413 return sym;
3415 case 'C':
3416 /* The name of a caught exception. */
3417 sym->set_type (read_type (&p, objfile));
3418 sym->set_aclass_index (LOC_LABEL);
3419 sym->set_domain (VAR_DOMAIN);
3420 sym->set_value_address (valu);
3421 add_symbol_to_list (sym, get_local_symbols ());
3422 break;
3424 case 'f':
3425 /* A static function definition. */
3426 sym->set_type (read_type (&p, objfile));
3427 sym->set_aclass_index (LOC_BLOCK);
3428 sym->set_domain (FUNCTION_DOMAIN);
3429 add_symbol_to_list (sym, get_file_symbols ());
3430 /* fall into process_function_types. */
3432 process_function_types:
3433 /* Function result types are described as the result type in stabs.
3434 We need to convert this to the function-returning-type-X type
3435 in GDB. E.g. "int" is converted to "function returning int". */
3436 if (sym->type ()->code () != TYPE_CODE_FUNC)
3437 sym->set_type (lookup_function_type (sym->type ()));
3439 /* All functions in C++ have prototypes. Stabs does not offer an
3440 explicit way to identify prototyped or unprototyped functions,
3441 but both GCC and Sun CC emit stabs for the "call-as" type rather
3442 than the "declared-as" type for unprototyped functions, so
3443 we treat all functions as if they were prototyped. This is used
3444 primarily for promotion when calling the function from GDB. */
3445 sym->type ()->set_is_prototyped (true);
3447 /* fall into process_prototype_types. */
3449 process_prototype_types:
3450 /* Sun acc puts declared types of arguments here. */
3451 if (*p == ';')
3453 struct type *ftype = sym->type ();
3454 int nsemi = 0;
3455 int nparams = 0;
3456 const char *p1 = p;
3458 /* Obtain a worst case guess for the number of arguments
3459 by counting the semicolons. */
3460 while (*p1)
3462 if (*p1++ == ';')
3463 nsemi++;
3466 /* Allocate parameter information fields and fill them in. */
3467 ftype->alloc_fields (nsemi);
3468 while (*p++ == ';')
3470 struct type *ptype;
3472 /* A type number of zero indicates the start of varargs.
3473 FIXME: GDB currently ignores vararg functions. */
3474 if (p[0] == '0' && p[1] == '\0')
3475 break;
3476 ptype = read_type (&p, objfile);
3478 /* The Sun compilers mark integer arguments, which should
3479 be promoted to the width of the calling conventions, with
3480 a type which references itself. This type is turned into
3481 a TYPE_CODE_VOID type by read_type, and we have to turn
3482 it back into builtin_int here.
3483 FIXME: Do we need a new builtin_promoted_int_arg ? */
3484 if (ptype->code () == TYPE_CODE_VOID)
3485 ptype = builtin_type (objfile)->builtin_int;
3486 ftype->field (nparams).set_type (ptype);
3487 ftype->field (nparams).set_is_artificial (false);
3488 nparams++;
3490 ftype->set_num_fields (nparams);
3491 ftype->set_is_prototyped (true);
3493 break;
3495 case 'F':
3496 /* A global function definition. */
3497 sym->set_type (read_type (&p, objfile));
3498 sym->set_aclass_index (LOC_BLOCK);
3499 sym->set_domain (FUNCTION_DOMAIN);
3500 add_symbol_to_list (sym, get_global_symbols ());
3501 goto process_function_types;
3503 case 'G':
3504 /* For a class G (global) symbol, it appears that the
3505 value is not correct. It is necessary to search for the
3506 corresponding linker definition to find the value.
3507 These definitions appear at the end of the namelist. */
3508 sym->set_type (read_type (&p, objfile));
3509 sym->set_aclass_index (LOC_STATIC);
3510 sym->set_domain (VAR_DOMAIN);
3511 /* Don't add symbol references to global_sym_chain.
3512 Symbol references don't have valid names and wont't match up with
3513 minimal symbols when the global_sym_chain is relocated.
3514 We'll fixup symbol references when we fixup the defining symbol. */
3515 if (sym->linkage_name () && sym->linkage_name ()[0] != '#')
3517 i = hashname (sym->linkage_name ());
3518 sym->set_value_chain (global_sym_chain[i]);
3519 global_sym_chain[i] = sym;
3521 add_symbol_to_list (sym, get_global_symbols ());
3522 break;
3524 /* This case is faked by a conditional above,
3525 when there is no code letter in the dbx data.
3526 Dbx data never actually contains 'l'. */
3527 case 's':
3528 case 'l':
3529 sym->set_type (read_type (&p, objfile));
3530 sym->set_aclass_index (LOC_LOCAL);
3531 sym->set_value_longest (valu);
3532 sym->set_domain (VAR_DOMAIN);
3533 add_symbol_to_list (sym, get_local_symbols ());
3534 break;
3536 case 'p':
3537 if (*p == 'F')
3538 /* pF is a two-letter code that means a function parameter in Fortran.
3539 The type-number specifies the type of the return value.
3540 Translate it into a pointer-to-function type. */
3542 p++;
3543 sym->set_type
3544 (lookup_pointer_type
3545 (lookup_function_type (read_type (&p, objfile))));
3547 else
3548 sym->set_type (read_type (&p, objfile));
3550 sym->set_aclass_index (LOC_ARG);
3551 sym->set_value_longest (valu);
3552 sym->set_domain (VAR_DOMAIN);
3553 sym->set_is_argument (1);
3554 add_symbol_to_list (sym, get_local_symbols ());
3556 if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
3558 /* On little-endian machines, this crud is never necessary,
3559 and, if the extra bytes contain garbage, is harmful. */
3560 break;
3563 /* If it's gcc-compiled, if it says `short', believe it. */
3564 if (processing_gcc_compilation
3565 || gdbarch_believe_pcc_promotion (gdbarch))
3566 break;
3568 if (!gdbarch_believe_pcc_promotion (gdbarch))
3570 /* If PCC says a parameter is a short or a char, it is
3571 really an int. */
3572 if (sym->type ()->length ()
3573 < gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT
3574 && sym->type ()->code () == TYPE_CODE_INT)
3576 sym->set_type
3577 (sym->type ()->is_unsigned ()
3578 ? builtin_type (objfile)->builtin_unsigned_int
3579 : builtin_type (objfile)->builtin_int);
3581 break;
3583 [[fallthrough]];
3585 case 'P':
3586 /* acc seems to use P to declare the prototypes of functions that
3587 are referenced by this file. gdb is not prepared to deal
3588 with this extra information. FIXME, it ought to. */
3589 if (type == N_FUN)
3591 sym->set_type (read_type (&p, objfile));
3592 goto process_prototype_types;
3594 [[fallthrough]];
3596 case 'R':
3597 /* Parameter which is in a register. */
3598 sym->set_type (read_type (&p, objfile));
3599 sym->set_aclass_index (stab_register_index);
3600 sym->set_is_argument (1);
3601 sym->set_value_longest (valu);
3602 sym->set_domain (VAR_DOMAIN);
3603 add_symbol_to_list (sym, get_local_symbols ());
3604 break;
3606 case 'r':
3607 /* Register variable (either global or local). */
3608 sym->set_type (read_type (&p, objfile));
3609 sym->set_aclass_index (stab_register_index);
3610 sym->set_value_longest (valu);
3611 sym->set_domain (VAR_DOMAIN);
3612 if (within_function)
3614 /* Sun cc uses a pair of symbols, one 'p' and one 'r', with
3615 the same name to represent an argument passed in a
3616 register. GCC uses 'P' for the same case. So if we find
3617 such a symbol pair we combine it into one 'P' symbol.
3618 For Sun cc we need to do this regardless of stabs_argument_has_addr, because the compiler puts out
3619 the 'p' symbol even if it never saves the argument onto
3620 the stack.
3622 On most machines, we want to preserve both symbols, so
3623 that we can still get information about what is going on
3624 with the stack (VAX for computing args_printed, using
3625 stack slots instead of saved registers in backtraces,
3626 etc.).
3628 Note that this code illegally combines
3629 main(argc) struct foo argc; { register struct foo argc; }
3630 but this case is considered pathological and causes a warning
3631 from a decent compiler. */
3633 struct pending *local_symbols = *get_local_symbols ();
3634 if (local_symbols
3635 && local_symbols->nsyms > 0
3636 && gdbarch_stabs_argument_has_addr (gdbarch, sym->type ()))
3638 struct symbol *prev_sym;
3640 prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
3641 if ((prev_sym->aclass () == LOC_REF_ARG
3642 || prev_sym->aclass () == LOC_ARG)
3643 && strcmp (prev_sym->linkage_name (),
3644 sym->linkage_name ()) == 0)
3646 prev_sym->set_aclass_index (stab_register_index);
3647 /* Use the type from the LOC_REGISTER; that is the type
3648 that is actually in that register. */
3649 prev_sym->set_type (sym->type ());
3650 prev_sym->set_value_longest (sym->value_longest ());
3651 sym = prev_sym;
3652 break;
3655 add_symbol_to_list (sym, get_local_symbols ());
3657 else
3658 add_symbol_to_list (sym, get_file_symbols ());
3659 break;
3661 case 'S':
3662 /* Static symbol at top level of file. */
3663 sym->set_type (read_type (&p, objfile));
3664 sym->set_aclass_index (LOC_STATIC);
3665 sym->set_value_address (valu);
3666 sym->set_domain (VAR_DOMAIN);
3667 add_symbol_to_list (sym, get_file_symbols ());
3668 break;
3670 case 't':
3671 /* In Ada, there is no distinction between typedef and non-typedef;
3672 any type declaration implicitly has the equivalent of a typedef,
3673 and thus 't' is in fact equivalent to 'Tt'.
3675 Therefore, for Ada units, we check the character immediately
3676 before the 't', and if we do not find a 'T', then make sure to
3677 create the associated symbol in the STRUCT_DOMAIN ('t' definitions
3678 will be stored in the VAR_DOMAIN). If the symbol was indeed
3679 defined as 'Tt' then the STRUCT_DOMAIN symbol will be created
3680 elsewhere, so we don't need to take care of that.
3682 This is important to do, because of forward references:
3683 The cleanup of undefined types stored in undef_types only uses
3684 STRUCT_DOMAIN symbols to perform the replacement. */
3685 synonym = (sym->language () == language_ada && p[-2] != 'T');
3687 /* Typedef */
3688 sym->set_type (read_type (&p, objfile));
3690 /* For a nameless type, we don't want a create a symbol, thus we
3691 did not use `sym'. Return without further processing. */
3692 if (nameless)
3693 return NULL;
3695 sym->set_aclass_index (LOC_TYPEDEF);
3696 sym->set_value_longest (valu);
3697 sym->set_domain (TYPE_DOMAIN);
3698 /* C++ vagaries: we may have a type which is derived from
3699 a base type which did not have its name defined when the
3700 derived class was output. We fill in the derived class's
3701 base part member's name here in that case. */
3702 if (sym->type ()->name () != NULL)
3703 if ((sym->type ()->code () == TYPE_CODE_STRUCT
3704 || sym->type ()->code () == TYPE_CODE_UNION)
3705 && TYPE_N_BASECLASSES (sym->type ()))
3707 int j;
3709 for (j = TYPE_N_BASECLASSES (sym->type ()) - 1; j >= 0; j--)
3710 if (TYPE_BASECLASS_NAME (sym->type (), j) == 0)
3711 sym->type ()->field (j).set_name
3712 (TYPE_BASECLASS (sym->type (), j)->name ());
3715 if (sym->type ()->name () == NULL)
3717 if ((sym->type ()->code () == TYPE_CODE_PTR
3718 && strcmp (sym->linkage_name (), vtbl_ptr_name))
3719 || sym->type ()->code () == TYPE_CODE_FUNC)
3721 /* If we are giving a name to a type such as "pointer to
3722 foo" or "function returning foo", we better not set
3723 the TYPE_NAME. If the program contains "typedef char
3724 *caddr_t;", we don't want all variables of type char
3725 * to print as caddr_t. This is not just a
3726 consequence of GDB's type management; PCC and GCC (at
3727 least through version 2.4) both output variables of
3728 either type char * or caddr_t with the type number
3729 defined in the 't' symbol for caddr_t. If a future
3730 compiler cleans this up it GDB is not ready for it
3731 yet, but if it becomes ready we somehow need to
3732 disable this check (without breaking the PCC/GCC2.4
3733 case).
3735 Sigh.
3737 Fortunately, this check seems not to be necessary
3738 for anything except pointers or functions. */
3739 /* ezannoni: 2000-10-26. This seems to apply for
3740 versions of gcc older than 2.8. This was the original
3741 problem: with the following code gdb would tell that
3742 the type for name1 is caddr_t, and func is char().
3744 typedef char *caddr_t;
3745 char *name2;
3746 struct x
3748 char *name1;
3749 } xx;
3750 char *func()
3753 main () {}
3756 /* Pascal accepts names for pointer types. */
3757 if (get_current_subfile ()->language == language_pascal)
3758 sym->type ()->set_name (sym->linkage_name ());
3760 else
3761 sym->type ()->set_name (sym->linkage_name ());
3764 add_symbol_to_list (sym, get_file_symbols ());
3766 if (synonym)
3768 /* Create the STRUCT_DOMAIN clone. */
3769 struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;
3771 *struct_sym = *sym;
3772 struct_sym->set_aclass_index (LOC_TYPEDEF);
3773 struct_sym->set_value_longest (valu);
3774 struct_sym->set_domain (STRUCT_DOMAIN);
3775 if (sym->type ()->name () == 0)
3776 sym->type ()->set_name
3777 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
3778 (char *) NULL));
3779 add_symbol_to_list (struct_sym, get_file_symbols ());
3782 break;
3784 case 'T':
3785 /* Struct, union, or enum tag. For GNU C++, this can be be followed
3786 by 't' which means we are typedef'ing it as well. */
3787 synonym = *p == 't';
3789 if (synonym)
3790 p++;
3792 sym->set_type (read_type (&p, objfile));
3794 /* For a nameless type, we don't want a create a symbol, thus we
3795 did not use `sym'. Return without further processing. */
3796 if (nameless)
3797 return NULL;
3799 sym->set_aclass_index (LOC_TYPEDEF);
3800 sym->set_value_longest (valu);
3801 sym->set_domain (STRUCT_DOMAIN);
3802 if (sym->type ()->name () == 0)
3803 sym->type ()->set_name
3804 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
3805 (char *) NULL));
3806 add_symbol_to_list (sym, get_file_symbols ());
3808 if (synonym)
3810 /* Clone the sym and then modify it. */
3811 struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;
3813 *typedef_sym = *sym;
3814 typedef_sym->set_aclass_index (LOC_TYPEDEF);
3815 typedef_sym->set_value_longest (valu);
3816 typedef_sym->set_domain (TYPE_DOMAIN);
3817 if (sym->type ()->name () == 0)
3818 sym->type ()->set_name
3819 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
3820 (char *) NULL));
3821 add_symbol_to_list (typedef_sym, get_file_symbols ());
3823 break;
3825 case 'V':
3826 /* Static symbol of local scope. */
3827 sym->set_type (read_type (&p, objfile));
3828 sym->set_aclass_index (LOC_STATIC);
3829 sym->set_value_address (valu);
3830 sym->set_domain (VAR_DOMAIN);
3831 add_symbol_to_list (sym, get_local_symbols ());
3832 break;
3834 case 'v':
3835 /* Reference parameter */
3836 sym->set_type (read_type (&p, objfile));
3837 sym->set_aclass_index (LOC_REF_ARG);
3838 sym->set_is_argument (1);
3839 sym->set_value_longest (valu);
3840 sym->set_domain (VAR_DOMAIN);
3841 add_symbol_to_list (sym, get_local_symbols ());
3842 break;
3844 case 'a':
3845 /* Reference parameter which is in a register. */
3846 sym->set_type (read_type (&p, objfile));
3847 sym->set_aclass_index (stab_regparm_index);
3848 sym->set_is_argument (1);
3849 sym->set_value_longest (valu);
3850 sym->set_domain (VAR_DOMAIN);
3851 add_symbol_to_list (sym, get_local_symbols ());
3852 break;
3854 case 'X':
3855 /* This is used by Sun FORTRAN for "function result value".
3856 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
3857 that Pascal uses it too, but when I tried it Pascal used
3858 "x:3" (local symbol) instead. */
3859 sym->set_type (read_type (&p, objfile));
3860 sym->set_aclass_index (LOC_LOCAL);
3861 sym->set_value_longest (valu);
3862 sym->set_domain (VAR_DOMAIN);
3863 add_symbol_to_list (sym, get_local_symbols ());
3864 break;
3866 default:
3867 sym->set_type (error_type (&p, objfile));
3868 sym->set_aclass_index (LOC_CONST);
3869 sym->set_value_longest (0);
3870 sym->set_domain (VAR_DOMAIN);
3871 add_symbol_to_list (sym, get_file_symbols ());
3872 break;
3875 /* Some systems pass variables of certain types by reference instead
3876 of by value, i.e. they will pass the address of a structure (in a
3877 register or on the stack) instead of the structure itself. */
3879 if (gdbarch_stabs_argument_has_addr (gdbarch, sym->type ())
3880 && sym->is_argument ())
3882 /* We have to convert LOC_REGISTER to LOC_REGPARM_ADDR (for
3883 variables passed in a register). */
3884 if (sym->aclass () == LOC_REGISTER)
3885 sym->set_aclass_index (LOC_REGPARM_ADDR);
3886 /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
3887 and subsequent arguments on SPARC, for example). */
3888 else if (sym->aclass () == LOC_ARG)
3889 sym->set_aclass_index (LOC_REF_ARG);
3892 return sym;
3895 /* Skip rest of this symbol and return an error type.
3897 General notes on error recovery: error_type always skips to the
3898 end of the symbol (modulo cretinous dbx symbol name continuation).
3899 Thus code like this:
3901 if (*(*pp)++ != ';')
3902 return error_type (pp, objfile);
3904 is wrong because if *pp starts out pointing at '\0' (typically as the
3905 result of an earlier error), it will be incremented to point to the
3906 start of the next symbol, which might produce strange results, at least
3907 if you run off the end of the string table. Instead use
3909 if (**pp != ';')
3910 return error_type (pp, objfile);
3911 ++*pp;
3915 if (**pp != ';')
3916 foo = error_type (pp, objfile);
3917 else
3918 ++*pp;
3920 And in case it isn't obvious, the point of all this hair is so the compiler
3921 can define new types and new syntaxes, and old versions of the
3922 debugger will be able to read the new symbol tables. */
3924 static struct type *
3925 error_type (const char **pp, struct objfile *objfile)
3927 complaint (_("couldn't parse type; debugger out of date?"));
3928 while (1)
3930 /* Skip to end of symbol. */
3931 while (**pp != '\0')
3933 (*pp)++;
3936 /* Check for and handle cretinous dbx symbol name continuation! */
3937 if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
3939 *pp = next_symbol_text (objfile);
3941 else
3943 break;
3946 return builtin_type (objfile)->builtin_error;
3950 /* Allocate a stub method whose return type is TYPE. This apparently
3951 happens for speed of symbol reading, since parsing out the
3952 arguments to the method is cpu-intensive, the way we are doing it.
3953 So, we will fill in arguments later. This always returns a fresh
3954 type. */
3956 static struct type *
3957 allocate_stub_method (struct type *type)
3959 struct type *mtype;
3961 mtype = type_allocator (type).new_type ();
3962 mtype->set_code (TYPE_CODE_METHOD);
3963 mtype->set_length (1);
3964 mtype->set_is_stub (true);
3965 mtype->set_target_type (type);
3966 /* TYPE_SELF_TYPE (mtype) = unknown yet */
3967 return mtype;
3970 /* Read type information or a type definition; return the type. Even
3971 though this routine accepts either type information or a type
3972 definition, the distinction is relevant--some parts of stabsread.c
3973 assume that type information starts with a digit, '-', or '(' in
3974 deciding whether to call read_type. */
3976 static struct type *
3977 read_type (const char **pp, struct objfile *objfile)
3979 struct type *type = 0;
3980 struct type *type1;
3981 int typenums[2];
3982 char type_descriptor;
3984 /* Size in bits of type if specified by a type attribute, or -1 if
3985 there is no size attribute. */
3986 int type_size = -1;
3988 /* Used to distinguish string and bitstring from char-array and set. */
3989 int is_string = 0;
3991 /* Used to distinguish vector from array. */
3992 int is_vector = 0;
3994 /* Read type number if present. The type number may be omitted.
3995 for instance in a two-dimensional array declared with type
3996 "ar1;1;10;ar1;1;10;4". */
3997 if ((**pp >= '0' && **pp <= '9')
3998 || **pp == '('
3999 || **pp == '-')
4001 if (read_type_number (pp, typenums) != 0)
4002 return error_type (pp, objfile);
4004 if (**pp != '=')
4006 /* Type is not being defined here. Either it already
4007 exists, or this is a forward reference to it.
4008 dbx_alloc_type handles both cases. */
4009 type = dbx_alloc_type (typenums, objfile);
4011 /* If this is a forward reference, arrange to complain if it
4012 doesn't get patched up by the time we're done
4013 reading. */
4014 if (type->code () == TYPE_CODE_UNDEF)
4015 add_undefined_type (type, typenums);
4017 return type;
4020 /* Type is being defined here. */
4021 /* Skip the '='.
4022 Also skip the type descriptor - we get it below with (*pp)[-1]. */
4023 (*pp) += 2;
4025 else
4027 /* 'typenums=' not present, type is anonymous. Read and return
4028 the definition, but don't put it in the type vector. */
4029 typenums[0] = typenums[1] = -1;
4030 (*pp)++;
4033 again:
4034 type_descriptor = (*pp)[-1];
4035 switch (type_descriptor)
4037 case 'x':
4039 enum type_code code;
4041 /* Used to index through file_symbols. */
4042 struct pending *ppt;
4043 int i;
4045 /* Name including "struct", etc. */
4046 char *type_name;
4049 const char *from, *p, *q1, *q2;
4051 /* Set the type code according to the following letter. */
4052 switch ((*pp)[0])
4054 case 's':
4055 code = TYPE_CODE_STRUCT;
4056 break;
4057 case 'u':
4058 code = TYPE_CODE_UNION;
4059 break;
4060 case 'e':
4061 code = TYPE_CODE_ENUM;
4062 break;
4063 default:
4065 /* Complain and keep going, so compilers can invent new
4066 cross-reference types. */
4067 complaint (_("Unrecognized cross-reference type `%c'"),
4068 (*pp)[0]);
4069 code = TYPE_CODE_STRUCT;
4070 break;
4074 q1 = strchr (*pp, '<');
4075 p = strchr (*pp, ':');
4076 if (p == NULL)
4077 return error_type (pp, objfile);
4078 if (q1 && p > q1 && p[1] == ':')
4080 int nesting_level = 0;
4082 for (q2 = q1; *q2; q2++)
4084 if (*q2 == '<')
4085 nesting_level++;
4086 else if (*q2 == '>')
4087 nesting_level--;
4088 else if (*q2 == ':' && nesting_level == 0)
4089 break;
4091 p = q2;
4092 if (*p != ':')
4093 return error_type (pp, objfile);
4095 type_name = NULL;
4096 if (get_current_subfile ()->language == language_cplus)
4098 std::string name (*pp, p - *pp);
4099 gdb::unique_xmalloc_ptr<char> new_name
4100 = cp_canonicalize_string (name.c_str ());
4101 if (new_name != nullptr)
4102 type_name = obstack_strdup (&objfile->objfile_obstack,
4103 new_name.get ());
4105 else if (get_current_subfile ()->language == language_c)
4107 std::string name (*pp, p - *pp);
4108 gdb::unique_xmalloc_ptr<char> new_name
4109 = c_canonicalize_name (name.c_str ());
4110 if (new_name != nullptr)
4111 type_name = obstack_strdup (&objfile->objfile_obstack,
4112 new_name.get ());
4114 if (type_name == NULL)
4116 char *to = type_name = (char *)
4117 obstack_alloc (&objfile->objfile_obstack, p - *pp + 1);
4119 /* Copy the name. */
4120 from = *pp + 1;
4121 while (from < p)
4122 *to++ = *from++;
4123 *to = '\0';
4126 /* Set the pointer ahead of the name which we just read, and
4127 the colon. */
4128 *pp = p + 1;
4131 /* If this type has already been declared, then reuse the same
4132 type, rather than allocating a new one. This saves some
4133 memory. */
4135 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
4136 for (i = 0; i < ppt->nsyms; i++)
4138 struct symbol *sym = ppt->symbol[i];
4140 if (sym->aclass () == LOC_TYPEDEF
4141 && sym->domain () == STRUCT_DOMAIN
4142 && (sym->type ()->code () == code)
4143 && strcmp (sym->linkage_name (), type_name) == 0)
4145 obstack_free (&objfile->objfile_obstack, type_name);
4146 type = sym->type ();
4147 if (typenums[0] != -1)
4148 *dbx_lookup_type (typenums, objfile) = type;
4149 return type;
4153 /* Didn't find the type to which this refers, so we must
4154 be dealing with a forward reference. Allocate a type
4155 structure for it, and keep track of it so we can
4156 fill in the rest of the fields when we get the full
4157 type. */
4158 type = dbx_alloc_type (typenums, objfile);
4159 type->set_code (code);
4160 type->set_name (type_name);
4161 INIT_CPLUS_SPECIFIC (type);
4162 type->set_is_stub (true);
4164 add_undefined_type (type, typenums);
4165 return type;
4168 case '-': /* RS/6000 built-in type */
4169 case '0':
4170 case '1':
4171 case '2':
4172 case '3':
4173 case '4':
4174 case '5':
4175 case '6':
4176 case '7':
4177 case '8':
4178 case '9':
4179 case '(':
4180 (*pp)--;
4182 /* We deal with something like t(1,2)=(3,4)=... which
4183 the Lucid compiler and recent gcc versions (post 2.7.3) use. */
4185 /* Allocate and enter the typedef type first.
4186 This handles recursive types. */
4187 type = dbx_alloc_type (typenums, objfile);
4188 type->set_code (TYPE_CODE_TYPEDEF);
4190 struct type *xtype = read_type (pp, objfile);
4192 if (type == xtype)
4194 /* It's being defined as itself. That means it is "void". */
4195 type->set_code (TYPE_CODE_VOID);
4196 type->set_length (1);
4198 else if (type_size >= 0 || is_string)
4200 /* This is the absolute wrong way to construct types. Every
4201 other debug format has found a way around this problem and
4202 the related problems with unnecessarily stubbed types;
4203 someone motivated should attempt to clean up the issue
4204 here as well. Once a type pointed to has been created it
4205 should not be modified.
4207 Well, it's not *absolutely* wrong. Constructing recursive
4208 types (trees, linked lists) necessarily entails modifying
4209 types after creating them. Constructing any loop structure
4210 entails side effects. The Dwarf 2 reader does handle this
4211 more gracefully (it never constructs more than once
4212 instance of a type object, so it doesn't have to copy type
4213 objects wholesale), but it still mutates type objects after
4214 other folks have references to them.
4216 Keep in mind that this circularity/mutation issue shows up
4217 at the source language level, too: C's "incomplete types",
4218 for example. So the proper cleanup, I think, would be to
4219 limit GDB's type smashing to match exactly those required
4220 by the source language. So GDB could have a
4221 "complete_this_type" function, but never create unnecessary
4222 copies of a type otherwise. */
4223 replace_type (type, xtype);
4224 type->set_name (NULL);
4226 else
4228 type->set_target_is_stub (true);
4229 type->set_target_type (xtype);
4232 break;
4234 /* In the following types, we must be sure to overwrite any existing
4235 type that the typenums refer to, rather than allocating a new one
4236 and making the typenums point to the new one. This is because there
4237 may already be pointers to the existing type (if it had been
4238 forward-referenced), and we must change it to a pointer, function,
4239 reference, or whatever, *in-place*. */
4241 case '*': /* Pointer to another type */
4242 type1 = read_type (pp, objfile);
4243 type = make_pointer_type (type1, dbx_lookup_type (typenums, objfile));
4244 break;
4246 case '&': /* Reference to another type */
4247 type1 = read_type (pp, objfile);
4248 type = make_reference_type (type1, dbx_lookup_type (typenums, objfile),
4249 TYPE_CODE_REF);
4250 break;
4252 case 'f': /* Function returning another type */
4253 type1 = read_type (pp, objfile);
4254 type = make_function_type (type1, dbx_lookup_type (typenums, objfile));
4255 break;
4257 case 'g': /* Prototyped function. (Sun) */
4259 /* Unresolved questions:
4261 - According to Sun's ``STABS Interface Manual'', for 'f'
4262 and 'F' symbol descriptors, a `0' in the argument type list
4263 indicates a varargs function. But it doesn't say how 'g'
4264 type descriptors represent that info. Someone with access
4265 to Sun's toolchain should try it out.
4267 - According to the comment in define_symbol (search for
4268 `process_prototype_types:'), Sun emits integer arguments as
4269 types which ref themselves --- like `void' types. Do we
4270 have to deal with that here, too? Again, someone with
4271 access to Sun's toolchain should try it out and let us
4272 know. */
4274 const char *type_start = (*pp) - 1;
4275 struct type *return_type = read_type (pp, objfile);
4276 struct type *func_type
4277 = make_function_type (return_type,
4278 dbx_lookup_type (typenums, objfile));
4279 struct type_list {
4280 struct type *type;
4281 struct type_list *next;
4282 } *arg_types = 0;
4283 int num_args = 0;
4285 while (**pp && **pp != '#')
4287 struct type *arg_type = read_type (pp, objfile);
4288 struct type_list *newobj = XALLOCA (struct type_list);
4289 newobj->type = arg_type;
4290 newobj->next = arg_types;
4291 arg_types = newobj;
4292 num_args++;
4294 if (**pp == '#')
4295 ++*pp;
4296 else
4298 complaint (_("Prototyped function type didn't "
4299 "end arguments with `#':\n%s"),
4300 type_start);
4303 /* If there is just one argument whose type is `void', then
4304 that's just an empty argument list. */
4305 if (arg_types
4306 && ! arg_types->next
4307 && arg_types->type->code () == TYPE_CODE_VOID)
4308 num_args = 0;
4310 func_type->alloc_fields (num_args);
4312 int i;
4313 struct type_list *t;
4315 /* We stuck each argument type onto the front of the list
4316 when we read it, so the list is reversed. Build the
4317 fields array right-to-left. */
4318 for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
4319 func_type->field (i).set_type (t->type);
4321 func_type->set_num_fields (num_args);
4322 func_type->set_is_prototyped (true);
4324 type = func_type;
4325 break;
4328 case 'k': /* Const qualifier on some type (Sun) */
4329 type = read_type (pp, objfile);
4330 type = make_cv_type (1, TYPE_VOLATILE (type), type,
4331 dbx_lookup_type (typenums, objfile));
4332 break;
4334 case 'B': /* Volatile qual on some type (Sun) */
4335 type = read_type (pp, objfile);
4336 type = make_cv_type (TYPE_CONST (type), 1, type,
4337 dbx_lookup_type (typenums, objfile));
4338 break;
4340 case '@':
4341 if (isdigit (**pp) || **pp == '(' || **pp == '-')
4342 { /* Member (class & variable) type */
4343 /* FIXME -- we should be doing smash_to_XXX types here. */
4345 struct type *domain = read_type (pp, objfile);
4346 struct type *memtype;
4348 if (**pp != ',')
4349 /* Invalid member type data format. */
4350 return error_type (pp, objfile);
4351 ++*pp;
4353 memtype = read_type (pp, objfile);
4354 type = dbx_alloc_type (typenums, objfile);
4355 smash_to_memberptr_type (type, domain, memtype);
4357 else
4358 /* type attribute */
4360 const char *attr = *pp;
4362 /* Skip to the semicolon. */
4363 while (**pp != ';' && **pp != '\0')
4364 ++(*pp);
4365 if (**pp == '\0')
4366 return error_type (pp, objfile);
4367 else
4368 ++ * pp; /* Skip the semicolon. */
4370 switch (*attr)
4372 case 's': /* Size attribute */
4373 type_size = atoi (attr + 1);
4374 if (type_size <= 0)
4375 type_size = -1;
4376 break;
4378 case 'S': /* String attribute */
4379 /* FIXME: check to see if following type is array? */
4380 is_string = 1;
4381 break;
4383 case 'V': /* Vector attribute */
4384 /* FIXME: check to see if following type is array? */
4385 is_vector = 1;
4386 break;
4388 default:
4389 /* Ignore unrecognized type attributes, so future compilers
4390 can invent new ones. */
4391 break;
4393 ++*pp;
4394 goto again;
4396 break;
4398 case '#': /* Method (class & fn) type */
4399 if ((*pp)[0] == '#')
4401 /* We'll get the parameter types from the name. */
4402 struct type *return_type;
4404 (*pp)++;
4405 return_type = read_type (pp, objfile);
4406 if (*(*pp)++ != ';')
4407 complaint (_("invalid (minimal) member type "
4408 "data format at symtab pos %d."),
4409 symnum);
4410 type = allocate_stub_method (return_type);
4411 if (typenums[0] != -1)
4412 *dbx_lookup_type (typenums, objfile) = type;
4414 else
4416 struct type *domain = read_type (pp, objfile);
4417 struct type *return_type;
4418 struct field *args;
4419 int nargs, varargs;
4421 if (**pp != ',')
4422 /* Invalid member type data format. */
4423 return error_type (pp, objfile);
4424 else
4425 ++(*pp);
4427 return_type = read_type (pp, objfile);
4428 args = read_args (pp, ';', objfile, &nargs, &varargs);
4429 if (args == NULL)
4430 return error_type (pp, objfile);
4431 type = dbx_alloc_type (typenums, objfile);
4432 smash_to_method_type (type, domain, return_type, args,
4433 nargs, varargs);
4435 break;
4437 case 'r': /* Range type */
4438 type = read_range_type (pp, typenums, type_size, objfile);
4439 if (typenums[0] != -1)
4440 *dbx_lookup_type (typenums, objfile) = type;
4441 break;
4443 case 'b':
4445 /* Sun ACC builtin int type */
4446 type = read_sun_builtin_type (pp, typenums, objfile);
4447 if (typenums[0] != -1)
4448 *dbx_lookup_type (typenums, objfile) = type;
4450 break;
4452 case 'R': /* Sun ACC builtin float type */
4453 type = read_sun_floating_type (pp, typenums, objfile);
4454 if (typenums[0] != -1)
4455 *dbx_lookup_type (typenums, objfile) = type;
4456 break;
4458 case 'e': /* Enumeration type */
4459 type = dbx_alloc_type (typenums, objfile);
4460 type = read_enum_type (pp, type, objfile);
4461 if (typenums[0] != -1)
4462 *dbx_lookup_type (typenums, objfile) = type;
4463 break;
4465 case 's': /* Struct type */
4466 case 'u': /* Union type */
4468 enum type_code type_code = TYPE_CODE_UNDEF;
4469 type = dbx_alloc_type (typenums, objfile);
4470 switch (type_descriptor)
4472 case 's':
4473 type_code = TYPE_CODE_STRUCT;
4474 break;
4475 case 'u':
4476 type_code = TYPE_CODE_UNION;
4477 break;
4479 type = read_struct_type (pp, type, type_code, objfile);
4480 break;
4483 case 'a': /* Array type */
4484 if (**pp != 'r')
4485 return error_type (pp, objfile);
4486 ++*pp;
4488 type = dbx_alloc_type (typenums, objfile);
4489 type = read_array_type (pp, type, objfile);
4490 if (is_string)
4491 type->set_code (TYPE_CODE_STRING);
4492 if (is_vector)
4493 make_vector_type (type);
4494 break;
4496 case 'S': /* Set type */
4498 type1 = read_type (pp, objfile);
4499 type_allocator alloc (objfile, get_current_subfile ()->language);
4500 type = create_set_type (alloc, type1);
4501 if (typenums[0] != -1)
4502 *dbx_lookup_type (typenums, objfile) = type;
4504 break;
4506 default:
4507 --*pp; /* Go back to the symbol in error. */
4508 /* Particularly important if it was \0! */
4509 return error_type (pp, objfile);
4512 if (type == 0)
4514 warning (_("GDB internal error, type is NULL in stabsread.c."));
4515 return error_type (pp, objfile);
4518 /* Size specified in a type attribute overrides any other size. */
4519 if (type_size != -1)
4520 type->set_length ((type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT);
4522 return type;
4525 /* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
4526 Return the proper type node for a given builtin type number. */
4528 static const registry<objfile>::key<struct type *,
4529 gdb::noop_deleter<struct type *>>
4530 rs6000_builtin_type_data;
4532 static struct type *
4533 rs6000_builtin_type (int typenum, struct objfile *objfile)
4535 struct type **negative_types = rs6000_builtin_type_data.get (objfile);
4537 /* We recognize types numbered from -NUMBER_RECOGNIZED to -1. */
4538 #define NUMBER_RECOGNIZED 34
4539 struct type *rettype = NULL;
4541 if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
4543 complaint (_("Unknown builtin type %d"), typenum);
4544 return builtin_type (objfile)->builtin_error;
4547 if (!negative_types)
4549 /* This includes an empty slot for type number -0. */
4550 negative_types = OBSTACK_CALLOC (&objfile->objfile_obstack,
4551 NUMBER_RECOGNIZED + 1, struct type *);
4552 rs6000_builtin_type_data.set (objfile, negative_types);
4555 if (negative_types[-typenum] != NULL)
4556 return negative_types[-typenum];
4558 #if TARGET_CHAR_BIT != 8
4559 #error This code wrong for TARGET_CHAR_BIT not 8
4560 /* These definitions all assume that TARGET_CHAR_BIT is 8. I think
4561 that if that ever becomes not true, the correct fix will be to
4562 make the size in the struct type to be in bits, not in units of
4563 TARGET_CHAR_BIT. */
4564 #endif
4566 type_allocator alloc (objfile, get_current_subfile ()->language);
4567 switch (-typenum)
4569 case 1:
4570 /* The size of this and all the other types are fixed, defined
4571 by the debugging format. If there is a type called "int" which
4572 is other than 32 bits, then it should use a new negative type
4573 number (or avoid negative type numbers for that case).
4574 See stabs.texinfo. */
4575 rettype = init_integer_type (alloc, 32, 0, "int");
4576 break;
4577 case 2:
4578 rettype = init_integer_type (alloc, 8, 0, "char");
4579 rettype->set_has_no_signedness (true);
4580 break;
4581 case 3:
4582 rettype = init_integer_type (alloc, 16, 0, "short");
4583 break;
4584 case 4:
4585 rettype = init_integer_type (alloc, 32, 0, "long");
4586 break;
4587 case 5:
4588 rettype = init_integer_type (alloc, 8, 1, "unsigned char");
4589 break;
4590 case 6:
4591 rettype = init_integer_type (alloc, 8, 0, "signed char");
4592 break;
4593 case 7:
4594 rettype = init_integer_type (alloc, 16, 1, "unsigned short");
4595 break;
4596 case 8:
4597 rettype = init_integer_type (alloc, 32, 1, "unsigned int");
4598 break;
4599 case 9:
4600 rettype = init_integer_type (alloc, 32, 1, "unsigned");
4601 break;
4602 case 10:
4603 rettype = init_integer_type (alloc, 32, 1, "unsigned long");
4604 break;
4605 case 11:
4606 rettype = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
4607 break;
4608 case 12:
4609 /* IEEE single precision (32 bit). */
4610 rettype = init_float_type (alloc, 32, "float",
4611 floatformats_ieee_single);
4612 break;
4613 case 13:
4614 /* IEEE double precision (64 bit). */
4615 rettype = init_float_type (alloc, 64, "double",
4616 floatformats_ieee_double);
4617 break;
4618 case 14:
4619 /* This is an IEEE double on the RS/6000, and different machines with
4620 different sizes for "long double" should use different negative
4621 type numbers. See stabs.texinfo. */
4622 rettype = init_float_type (alloc, 64, "long double",
4623 floatformats_ieee_double);
4624 break;
4625 case 15:
4626 rettype = init_integer_type (alloc, 32, 0, "integer");
4627 break;
4628 case 16:
4629 rettype = init_boolean_type (alloc, 32, 1, "boolean");
4630 break;
4631 case 17:
4632 rettype = init_float_type (alloc, 32, "short real",
4633 floatformats_ieee_single);
4634 break;
4635 case 18:
4636 rettype = init_float_type (alloc, 64, "real",
4637 floatformats_ieee_double);
4638 break;
4639 case 19:
4640 rettype = alloc.new_type (TYPE_CODE_ERROR, 0, "stringptr");
4641 break;
4642 case 20:
4643 rettype = init_character_type (alloc, 8, 1, "character");
4644 break;
4645 case 21:
4646 rettype = init_boolean_type (alloc, 8, 1, "logical*1");
4647 break;
4648 case 22:
4649 rettype = init_boolean_type (alloc, 16, 1, "logical*2");
4650 break;
4651 case 23:
4652 rettype = init_boolean_type (alloc, 32, 1, "logical*4");
4653 break;
4654 case 24:
4655 rettype = init_boolean_type (alloc, 32, 1, "logical");
4656 break;
4657 case 25:
4658 /* Complex type consisting of two IEEE single precision values. */
4659 rettype = init_complex_type ("complex",
4660 rs6000_builtin_type (12, objfile));
4661 break;
4662 case 26:
4663 /* Complex type consisting of two IEEE double precision values. */
4664 rettype = init_complex_type ("double complex",
4665 rs6000_builtin_type (13, objfile));
4666 break;
4667 case 27:
4668 rettype = init_integer_type (alloc, 8, 0, "integer*1");
4669 break;
4670 case 28:
4671 rettype = init_integer_type (alloc, 16, 0, "integer*2");
4672 break;
4673 case 29:
4674 rettype = init_integer_type (alloc, 32, 0, "integer*4");
4675 break;
4676 case 30:
4677 rettype = init_character_type (alloc, 16, 0, "wchar");
4678 break;
4679 case 31:
4680 rettype = init_integer_type (alloc, 64, 0, "long long");
4681 break;
4682 case 32:
4683 rettype = init_integer_type (alloc, 64, 1, "unsigned long long");
4684 break;
4685 case 33:
4686 rettype = init_integer_type (alloc, 64, 1, "logical*8");
4687 break;
4688 case 34:
4689 rettype = init_integer_type (alloc, 64, 0, "integer*8");
4690 break;
4692 negative_types[-typenum] = rettype;
4693 return rettype;
4696 /* This page contains subroutines of read_type. */
4698 /* Wrapper around method_name_from_physname to flag a complaint
4699 if there is an error. */
4701 static char *
4702 stabs_method_name_from_physname (const char *physname)
4704 char *method_name;
4706 method_name = method_name_from_physname (physname);
4708 if (method_name == NULL)
4710 complaint (_("Method has bad physname %s\n"), physname);
4711 return NULL;
4714 return method_name;
4717 /* Read member function stabs info for C++ classes. The form of each member
4718 function data is:
4720 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
4722 An example with two member functions is:
4724 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
4726 For the case of overloaded operators, the format is op$::*.funcs, where
4727 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
4728 name (such as `+=') and `.' marks the end of the operator name.
4730 Returns 1 for success, 0 for failure. */
4732 static int
4733 read_member_functions (struct stab_field_info *fip, const char **pp,
4734 struct type *type, struct objfile *objfile)
4736 int nfn_fields = 0;
4737 int length = 0;
4738 int i;
4739 struct next_fnfield
4741 struct next_fnfield *next;
4742 struct fn_field fn_field;
4744 *sublist;
4745 struct type *look_ahead_type;
4746 struct next_fnfieldlist *new_fnlist;
4747 struct next_fnfield *new_sublist;
4748 char *main_fn_name;
4749 const char *p;
4751 /* Process each list until we find something that is not a member function
4752 or find the end of the functions. */
4754 while (**pp != ';')
4756 /* We should be positioned at the start of the function name.
4757 Scan forward to find the first ':' and if it is not the
4758 first of a "::" delimiter, then this is not a member function. */
4759 p = *pp;
4760 while (*p != ':')
4762 p++;
4764 if (p[1] != ':')
4766 break;
4769 sublist = NULL;
4770 look_ahead_type = NULL;
4771 length = 0;
4773 new_fnlist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfieldlist);
4775 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
4777 /* This is a completely wierd case. In order to stuff in the
4778 names that might contain colons (the usual name delimiter),
4779 Mike Tiemann defined a different name format which is
4780 signalled if the identifier is "op$". In that case, the
4781 format is "op$::XXXX." where XXXX is the name. This is
4782 used for names like "+" or "=". YUUUUUUUK! FIXME! */
4783 /* This lets the user type "break operator+".
4784 We could just put in "+" as the name, but that wouldn't
4785 work for "*". */
4786 static char opname[32] = "op$";
4787 char *o = opname + 3;
4789 /* Skip past '::'. */
4790 *pp = p + 2;
4792 STABS_CONTINUE (pp, objfile);
4793 p = *pp;
4794 while (*p != '.')
4796 *o++ = *p++;
4798 main_fn_name = savestring (opname, o - opname);
4799 /* Skip past '.' */
4800 *pp = p + 1;
4802 else
4804 main_fn_name = savestring (*pp, p - *pp);
4805 /* Skip past '::'. */
4806 *pp = p + 2;
4808 new_fnlist->fn_fieldlist.name = main_fn_name;
4812 new_sublist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfield);
4814 /* Check for and handle cretinous dbx symbol name continuation! */
4815 if (look_ahead_type == NULL)
4817 /* Normal case. */
4818 STABS_CONTINUE (pp, objfile);
4820 new_sublist->fn_field.type = read_type (pp, objfile);
4821 if (**pp != ':')
4823 /* Invalid symtab info for member function. */
4824 return 0;
4827 else
4829 /* g++ version 1 kludge */
4830 new_sublist->fn_field.type = look_ahead_type;
4831 look_ahead_type = NULL;
4834 (*pp)++;
4835 p = *pp;
4836 while (*p != ';')
4838 p++;
4841 /* These are methods, not functions. */
4842 if (new_sublist->fn_field.type->code () == TYPE_CODE_FUNC)
4843 new_sublist->fn_field.type->set_code (TYPE_CODE_METHOD);
4845 /* If this is just a stub, then we don't have the real name here. */
4846 if (new_sublist->fn_field.type->is_stub ())
4848 if (!TYPE_SELF_TYPE (new_sublist->fn_field.type))
4849 set_type_self_type (new_sublist->fn_field.type, type);
4850 new_sublist->fn_field.is_stub = 1;
4853 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
4854 *pp = p + 1;
4856 /* Set this member function's visibility fields. */
4857 switch (*(*pp)++)
4859 case VISIBILITY_PRIVATE:
4860 new_sublist->fn_field.accessibility = accessibility::PRIVATE;
4861 break;
4862 case VISIBILITY_PROTECTED:
4863 new_sublist->fn_field.accessibility = accessibility::PROTECTED;
4864 break;
4867 STABS_CONTINUE (pp, objfile);
4868 switch (**pp)
4870 case 'A': /* Normal functions. */
4871 new_sublist->fn_field.is_const = 0;
4872 new_sublist->fn_field.is_volatile = 0;
4873 (*pp)++;
4874 break;
4875 case 'B': /* `const' member functions. */
4876 new_sublist->fn_field.is_const = 1;
4877 new_sublist->fn_field.is_volatile = 0;
4878 (*pp)++;
4879 break;
4880 case 'C': /* `volatile' member function. */
4881 new_sublist->fn_field.is_const = 0;
4882 new_sublist->fn_field.is_volatile = 1;
4883 (*pp)++;
4884 break;
4885 case 'D': /* `const volatile' member function. */
4886 new_sublist->fn_field.is_const = 1;
4887 new_sublist->fn_field.is_volatile = 1;
4888 (*pp)++;
4889 break;
4890 case '*': /* File compiled with g++ version 1 --
4891 no info. */
4892 case '?':
4893 case '.':
4894 break;
4895 default:
4896 complaint (_("const/volatile indicator missing, got '%c'"),
4897 **pp);
4898 break;
4901 switch (*(*pp)++)
4903 case '*':
4905 int nbits;
4906 /* virtual member function, followed by index.
4907 The sign bit is set to distinguish pointers-to-methods
4908 from virtual function indicies. Since the array is
4909 in words, the quantity must be shifted left by 1
4910 on 16 bit machine, and by 2 on 32 bit machine, forcing
4911 the sign bit out, and usable as a valid index into
4912 the array. Remove the sign bit here. */
4913 new_sublist->fn_field.voffset =
4914 (0x7fffffff & read_huge_number (pp, ';', &nbits, 0)) + 2;
4915 if (nbits != 0)
4916 return 0;
4918 STABS_CONTINUE (pp, objfile);
4919 if (**pp == ';' || **pp == '\0')
4921 /* Must be g++ version 1. */
4922 new_sublist->fn_field.fcontext = 0;
4924 else
4926 /* Figure out from whence this virtual function came.
4927 It may belong to virtual function table of
4928 one of its baseclasses. */
4929 look_ahead_type = read_type (pp, objfile);
4930 if (**pp == ':')
4932 /* g++ version 1 overloaded methods. */
4934 else
4936 new_sublist->fn_field.fcontext = look_ahead_type;
4937 if (**pp != ';')
4939 return 0;
4941 else
4943 ++*pp;
4945 look_ahead_type = NULL;
4948 break;
4950 case '?':
4951 /* static member function. */
4953 int slen = strlen (main_fn_name);
4955 new_sublist->fn_field.voffset = VOFFSET_STATIC;
4957 /* For static member functions, we can't tell if they
4958 are stubbed, as they are put out as functions, and not as
4959 methods.
4960 GCC v2 emits the fully mangled name if
4961 dbxout.c:flag_minimal_debug is not set, so we have to
4962 detect a fully mangled physname here and set is_stub
4963 accordingly. Fully mangled physnames in v2 start with
4964 the member function name, followed by two underscores.
4965 GCC v3 currently always emits stubbed member functions,
4966 but with fully mangled physnames, which start with _Z. */
4967 if (!(strncmp (new_sublist->fn_field.physname,
4968 main_fn_name, slen) == 0
4969 && new_sublist->fn_field.physname[slen] == '_'
4970 && new_sublist->fn_field.physname[slen + 1] == '_'))
4972 new_sublist->fn_field.is_stub = 1;
4974 break;
4977 default:
4978 /* error */
4979 complaint (_("member function type missing, got '%c'"),
4980 (*pp)[-1]);
4981 /* Normal member function. */
4982 [[fallthrough]];
4984 case '.':
4985 /* normal member function. */
4986 new_sublist->fn_field.voffset = 0;
4987 new_sublist->fn_field.fcontext = 0;
4988 break;
4991 new_sublist->next = sublist;
4992 sublist = new_sublist;
4993 length++;
4994 STABS_CONTINUE (pp, objfile);
4996 while (**pp != ';' && **pp != '\0');
4998 (*pp)++;
4999 STABS_CONTINUE (pp, objfile);
5001 /* Skip GCC 3.X member functions which are duplicates of the callable
5002 constructor/destructor. */
5003 if (strcmp_iw (main_fn_name, "__base_ctor ") == 0
5004 || strcmp_iw (main_fn_name, "__base_dtor ") == 0
5005 || strcmp (main_fn_name, "__deleting_dtor") == 0)
5007 xfree (main_fn_name);
5009 else
5011 int has_destructor = 0, has_other = 0;
5012 int is_v3 = 0;
5013 struct next_fnfield *tmp_sublist;
5015 /* Various versions of GCC emit various mostly-useless
5016 strings in the name field for special member functions.
5018 For stub methods, we need to defer correcting the name
5019 until we are ready to unstub the method, because the current
5020 name string is used by gdb_mangle_name. The only stub methods
5021 of concern here are GNU v2 operators; other methods have their
5022 names correct (see caveat below).
5024 For non-stub methods, in GNU v3, we have a complete physname.
5025 Therefore we can safely correct the name now. This primarily
5026 affects constructors and destructors, whose name will be
5027 __comp_ctor or __comp_dtor instead of Foo or ~Foo. Cast
5028 operators will also have incorrect names; for instance,
5029 "operator int" will be named "operator i" (i.e. the type is
5030 mangled).
5032 For non-stub methods in GNU v2, we have no easy way to
5033 know if we have a complete physname or not. For most
5034 methods the result depends on the platform (if CPLUS_MARKER
5035 can be `$' or `.', it will use minimal debug information, or
5036 otherwise the full physname will be included).
5038 Rather than dealing with this, we take a different approach.
5039 For v3 mangled names, we can use the full physname; for v2,
5040 we use cplus_demangle_opname (which is actually v2 specific),
5041 because the only interesting names are all operators - once again
5042 barring the caveat below. Skip this process if any method in the
5043 group is a stub, to prevent our fouling up the workings of
5044 gdb_mangle_name.
5046 The caveat: GCC 2.95.x (and earlier?) put constructors and
5047 destructors in the same method group. We need to split this
5048 into two groups, because they should have different names.
5049 So for each method group we check whether it contains both
5050 routines whose physname appears to be a destructor (the physnames
5051 for and destructors are always provided, due to quirks in v2
5052 mangling) and routines whose physname does not appear to be a
5053 destructor. If so then we break up the list into two halves.
5054 Even if the constructors and destructors aren't in the same group
5055 the destructor will still lack the leading tilde, so that also
5056 needs to be fixed.
5058 So, to summarize what we expect and handle here:
5060 Given Given Real Real Action
5061 method name physname physname method name
5063 __opi [none] __opi__3Foo operator int opname
5064 [now or later]
5065 Foo _._3Foo _._3Foo ~Foo separate and
5066 rename
5067 operator i _ZN3FoocviEv _ZN3FoocviEv operator int demangle
5068 __comp_ctor _ZN3FooC1ERKS_ _ZN3FooC1ERKS_ Foo demangle
5071 tmp_sublist = sublist;
5072 while (tmp_sublist != NULL)
5074 if (tmp_sublist->fn_field.physname[0] == '_'
5075 && tmp_sublist->fn_field.physname[1] == 'Z')
5076 is_v3 = 1;
5078 if (is_destructor_name (tmp_sublist->fn_field.physname))
5079 has_destructor++;
5080 else
5081 has_other++;
5083 tmp_sublist = tmp_sublist->next;
5086 if (has_destructor && has_other)
5088 struct next_fnfieldlist *destr_fnlist;
5089 struct next_fnfield *last_sublist;
5091 /* Create a new fn_fieldlist for the destructors. */
5093 destr_fnlist = OBSTACK_ZALLOC (&fip->obstack,
5094 struct next_fnfieldlist);
5096 destr_fnlist->fn_fieldlist.name
5097 = obconcat (&objfile->objfile_obstack, "~",
5098 new_fnlist->fn_fieldlist.name, (char *) NULL);
5100 destr_fnlist->fn_fieldlist.fn_fields =
5101 XOBNEWVEC (&objfile->objfile_obstack,
5102 struct fn_field, has_destructor);
5103 memset (destr_fnlist->fn_fieldlist.fn_fields, 0,
5104 sizeof (struct fn_field) * has_destructor);
5105 tmp_sublist = sublist;
5106 last_sublist = NULL;
5107 i = 0;
5108 while (tmp_sublist != NULL)
5110 if (!is_destructor_name (tmp_sublist->fn_field.physname))
5112 tmp_sublist = tmp_sublist->next;
5113 continue;
5116 destr_fnlist->fn_fieldlist.fn_fields[i++]
5117 = tmp_sublist->fn_field;
5118 if (last_sublist)
5119 last_sublist->next = tmp_sublist->next;
5120 else
5121 sublist = tmp_sublist->next;
5122 last_sublist = tmp_sublist;
5123 tmp_sublist = tmp_sublist->next;
5126 destr_fnlist->fn_fieldlist.length = has_destructor;
5127 destr_fnlist->next = fip->fnlist;
5128 fip->fnlist = destr_fnlist;
5129 nfn_fields++;
5130 length -= has_destructor;
5132 else if (is_v3)
5134 /* v3 mangling prevents the use of abbreviated physnames,
5135 so we can do this here. There are stubbed methods in v3
5136 only:
5137 - in -gstabs instead of -gstabs+
5138 - or for static methods, which are output as a function type
5139 instead of a method type. */
5140 char *new_method_name =
5141 stabs_method_name_from_physname (sublist->fn_field.physname);
5143 if (new_method_name != NULL
5144 && strcmp (new_method_name,
5145 new_fnlist->fn_fieldlist.name) != 0)
5147 new_fnlist->fn_fieldlist.name = new_method_name;
5148 xfree (main_fn_name);
5150 else
5151 xfree (new_method_name);
5153 else if (has_destructor && new_fnlist->fn_fieldlist.name[0] != '~')
5155 new_fnlist->fn_fieldlist.name =
5156 obconcat (&objfile->objfile_obstack,
5157 "~", main_fn_name, (char *)NULL);
5158 xfree (main_fn_name);
5161 new_fnlist->fn_fieldlist.fn_fields
5162 = OBSTACK_CALLOC (&objfile->objfile_obstack, length, fn_field);
5163 for (i = length; (i--, sublist); sublist = sublist->next)
5165 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
5168 new_fnlist->fn_fieldlist.length = length;
5169 new_fnlist->next = fip->fnlist;
5170 fip->fnlist = new_fnlist;
5171 nfn_fields++;
5175 if (nfn_fields)
5177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5178 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
5179 TYPE_ZALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
5180 TYPE_NFN_FIELDS (type) = nfn_fields;
5183 return 1;
5186 /* Special GNU C++ name.
5188 Returns 1 for success, 0 for failure. "failure" means that we can't
5189 keep parsing and it's time for error_type(). */
5191 static int
5192 read_cpp_abbrev (struct stab_field_info *fip, const char **pp,
5193 struct type *type, struct objfile *objfile)
5195 const char *p;
5196 const char *name;
5197 char cpp_abbrev;
5198 struct type *context;
5200 p = *pp;
5201 if (*++p == 'v')
5203 name = NULL;
5204 cpp_abbrev = *++p;
5206 *pp = p + 1;
5208 /* At this point, *pp points to something like "22:23=*22...",
5209 where the type number before the ':' is the "context" and
5210 everything after is a regular type definition. Lookup the
5211 type, find it's name, and construct the field name. */
5213 context = read_type (pp, objfile);
5215 switch (cpp_abbrev)
5217 case 'f': /* $vf -- a virtual function table pointer */
5218 name = context->name ();
5219 if (name == NULL)
5221 name = "";
5223 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
5224 vptr_name, name, (char *) NULL));
5225 break;
5227 case 'b': /* $vb -- a virtual bsomethingorother */
5228 name = context->name ();
5229 if (name == NULL)
5231 complaint (_("C++ abbreviated type name "
5232 "unknown at symtab pos %d"),
5233 symnum);
5234 name = "FOO";
5236 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
5237 vb_name, name, (char *) NULL));
5238 break;
5240 default:
5241 invalid_cpp_abbrev_complaint (*pp);
5242 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
5243 "INVALID_CPLUSPLUS_ABBREV",
5244 (char *) NULL));
5245 break;
5248 /* At this point, *pp points to the ':'. Skip it and read the
5249 field type. */
5251 p = ++(*pp);
5252 if (p[-1] != ':')
5254 invalid_cpp_abbrev_complaint (*pp);
5255 return 0;
5257 fip->list->field.set_type (read_type (pp, objfile));
5258 if (**pp == ',')
5259 (*pp)++; /* Skip the comma. */
5260 else
5261 return 0;
5264 int nbits;
5266 fip->list->field.set_loc_bitpos (read_huge_number (pp, ';', &nbits, 0));
5267 if (nbits != 0)
5268 return 0;
5270 /* This field is unpacked. */
5271 fip->list->field.set_bitsize (0);
5272 fip->list->field.set_accessibility (accessibility::PRIVATE);
5274 else
5276 invalid_cpp_abbrev_complaint (*pp);
5277 /* We have no idea what syntax an unrecognized abbrev would have, so
5278 better return 0. If we returned 1, we would need to at least advance
5279 *pp to avoid an infinite loop. */
5280 return 0;
5282 return 1;
5285 static void
5286 read_one_struct_field (struct stab_field_info *fip, const char **pp,
5287 const char *p, struct type *type,
5288 struct objfile *objfile)
5290 struct gdbarch *gdbarch = objfile->arch ();
5292 fip->list->field.set_name
5293 (obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp));
5294 *pp = p + 1;
5296 /* This means we have a visibility for a field coming. */
5297 int visibility;
5298 if (**pp == '/')
5300 (*pp)++;
5301 visibility = *(*pp)++;
5303 else
5305 /* normal dbx-style format, no explicit visibility */
5306 visibility = VISIBILITY_PUBLIC;
5309 switch (visibility)
5311 case VISIBILITY_PRIVATE:
5312 fip->list->field.set_accessibility (accessibility::PRIVATE);
5313 break;
5315 case VISIBILITY_PROTECTED:
5316 fip->list->field.set_accessibility (accessibility::PROTECTED);
5317 break;
5319 case VISIBILITY_IGNORE:
5320 fip->list->field.set_ignored ();
5321 break;
5323 case VISIBILITY_PUBLIC:
5324 break;
5326 default:
5327 /* Unknown visibility. Complain and treat it as public. */
5329 complaint (_("Unknown visibility `%c' for field"),
5330 visibility);
5332 break;
5335 fip->list->field.set_type (read_type (pp, objfile));
5336 if (**pp == ':')
5338 p = ++(*pp);
5339 #if 0
5340 /* Possible future hook for nested types. */
5341 if (**pp == '!')
5343 fip->list->field.bitpos = (long) -2; /* nested type */
5344 p = ++(*pp);
5346 else
5347 ...;
5348 #endif
5349 while (*p != ';')
5351 p++;
5353 /* Static class member. */
5354 fip->list->field.set_loc_physname (savestring (*pp, p - *pp));
5355 *pp = p + 1;
5356 return;
5358 else if (**pp != ',')
5360 /* Bad structure-type format. */
5361 stabs_general_complaint ("bad structure-type format");
5362 return;
5365 (*pp)++; /* Skip the comma. */
5368 int nbits;
5370 fip->list->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
5371 if (nbits != 0)
5373 stabs_general_complaint ("bad structure-type format");
5374 return;
5376 fip->list->field.set_bitsize (read_huge_number (pp, ';', &nbits, 0));
5377 if (nbits != 0)
5379 stabs_general_complaint ("bad structure-type format");
5380 return;
5384 if (fip->list->field.loc_bitpos () == 0
5385 && fip->list->field.bitsize () == 0)
5387 /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
5388 it is a field which has been optimized out. The correct stab for
5389 this case is to use VISIBILITY_IGNORE, but that is a recent
5390 invention. (2) It is a 0-size array. For example
5391 union { int num; char str[0]; } foo. Printing _("<no value>" for
5392 str in "p foo" is OK, since foo.str (and thus foo.str[3])
5393 will continue to work, and a 0-size array as a whole doesn't
5394 have any contents to print.
5396 I suspect this probably could also happen with gcc -gstabs (not
5397 -gstabs+) for static fields, and perhaps other C++ extensions.
5398 Hopefully few people use -gstabs with gdb, since it is intended
5399 for dbx compatibility. */
5401 /* Ignore this field. */
5402 fip->list->field.set_ignored ();
5404 else
5406 /* Detect an unpacked field and mark it as such.
5407 dbx gives a bit size for all fields.
5408 Note that forward refs cannot be packed,
5409 and treat enums as if they had the width of ints. */
5411 struct type *field_type = check_typedef (fip->list->field.type ());
5413 if (field_type->code () != TYPE_CODE_INT
5414 && field_type->code () != TYPE_CODE_RANGE
5415 && field_type->code () != TYPE_CODE_BOOL
5416 && field_type->code () != TYPE_CODE_ENUM)
5418 fip->list->field.set_bitsize (0);
5420 if ((fip->list->field.bitsize ()
5421 == TARGET_CHAR_BIT * field_type->length ()
5422 || (field_type->code () == TYPE_CODE_ENUM
5423 && (fip->list->field.bitsize ()
5424 == gdbarch_int_bit (gdbarch)))
5427 fip->list->field.loc_bitpos () % 8 == 0)
5429 fip->list->field.set_bitsize (0);
5435 /* Read struct or class data fields. They have the form:
5437 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
5439 At the end, we see a semicolon instead of a field.
5441 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
5442 a static field.
5444 The optional VISIBILITY is one of:
5446 '/0' (VISIBILITY_PRIVATE)
5447 '/1' (VISIBILITY_PROTECTED)
5448 '/2' (VISIBILITY_PUBLIC)
5449 '/9' (VISIBILITY_IGNORE)
5451 or nothing, for C style fields with public visibility.
5453 Returns 1 for success, 0 for failure. */
5455 static int
5456 read_struct_fields (struct stab_field_info *fip, const char **pp,
5457 struct type *type, struct objfile *objfile)
5459 const char *p;
5460 struct stabs_nextfield *newobj;
5462 /* We better set p right now, in case there are no fields at all... */
5464 p = *pp;
5466 /* Read each data member type until we find the terminating ';' at the end of
5467 the data member list, or break for some other reason such as finding the
5468 start of the member function list. */
5469 /* Stab string for structure/union does not end with two ';' in
5470 SUN C compiler 5.3 i.e. F6U2, hence check for end of string. */
5472 while (**pp != ';' && **pp != '\0')
5474 STABS_CONTINUE (pp, objfile);
5475 /* Get space to record the next field's data. */
5476 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
5478 newobj->next = fip->list;
5479 fip->list = newobj;
5481 /* Get the field name. */
5482 p = *pp;
5484 /* If is starts with CPLUS_MARKER it is a special abbreviation,
5485 unless the CPLUS_MARKER is followed by an underscore, in
5486 which case it is just the name of an anonymous type, which we
5487 should handle like any other type name. */
5489 if (is_cplus_marker (p[0]) && p[1] != '_')
5491 if (!read_cpp_abbrev (fip, pp, type, objfile))
5492 return 0;
5493 continue;
5496 /* Look for the ':' that separates the field name from the field
5497 values. Data members are delimited by a single ':', while member
5498 functions are delimited by a pair of ':'s. When we hit the member
5499 functions (if any), terminate scan loop and return. */
5501 while (*p != ':' && *p != '\0')
5503 p++;
5505 if (*p == '\0')
5506 return 0;
5508 /* Check to see if we have hit the member functions yet. */
5509 if (p[1] == ':')
5511 break;
5513 read_one_struct_field (fip, pp, p, type, objfile);
5515 if (p[0] == ':' && p[1] == ':')
5517 /* (the deleted) chill the list of fields: the last entry (at
5518 the head) is a partially constructed entry which we now
5519 scrub. */
5520 fip->list = fip->list->next;
5522 return 1;
5524 /* The stabs for C++ derived classes contain baseclass information which
5525 is marked by a '!' character after the total size. This function is
5526 called when we encounter the baseclass marker, and slurps up all the
5527 baseclass information.
5529 Immediately following the '!' marker is the number of base classes that
5530 the class is derived from, followed by information for each base class.
5531 For each base class, there are two visibility specifiers, a bit offset
5532 to the base class information within the derived class, a reference to
5533 the type for the base class, and a terminating semicolon.
5535 A typical example, with two base classes, would be "!2,020,19;0264,21;".
5536 ^^ ^ ^ ^ ^ ^ ^
5537 Baseclass information marker __________________|| | | | | | |
5538 Number of baseclasses __________________________| | | | | | |
5539 Visibility specifiers (2) ________________________| | | | | |
5540 Offset in bits from start of class _________________| | | | |
5541 Type number for base class ___________________________| | | |
5542 Visibility specifiers (2) _______________________________| | |
5543 Offset in bits from start of class ________________________| |
5544 Type number of base class ____________________________________|
5546 Return 1 for success, 0 for (error-type-inducing) failure. */
5550 static int
5551 read_baseclasses (struct stab_field_info *fip, const char **pp,
5552 struct type *type, struct objfile *objfile)
5554 int i;
5555 struct stabs_nextfield *newobj;
5557 if (**pp != '!')
5559 return 1;
5561 else
5563 /* Skip the '!' baseclass information marker. */
5564 (*pp)++;
5567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5569 int nbits;
5571 TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits, 0);
5572 if (nbits != 0)
5573 return 0;
5576 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
5578 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
5580 newobj->next = fip->list;
5581 fip->list = newobj;
5582 newobj->field.set_bitsize (0); /* This should be an unpacked
5583 field! */
5585 STABS_CONTINUE (pp, objfile);
5586 switch (**pp)
5588 case '0':
5589 /* Nothing to do. */
5590 break;
5591 case '1':
5592 newobj->field.set_virtual ();
5593 break;
5594 default:
5595 /* Unknown character. Complain and treat it as non-virtual. */
5597 complaint (_("Unknown virtual character `%c' for baseclass"),
5598 **pp);
5601 ++(*pp);
5603 int visibility = *(*pp)++;
5604 switch (visibility)
5606 case VISIBILITY_PRIVATE:
5607 newobj->field.set_accessibility (accessibility::PRIVATE);
5608 break;
5609 case VISIBILITY_PROTECTED:
5610 newobj->field.set_accessibility (accessibility::PROTECTED);
5611 break;
5612 case VISIBILITY_PUBLIC:
5613 break;
5614 default:
5615 /* Bad visibility format. Complain and treat it as
5616 public. */
5618 complaint (_("Unknown visibility `%c' for baseclass"),
5619 visibility);
5624 int nbits;
5626 /* The remaining value is the bit offset of the portion of the object
5627 corresponding to this baseclass. Always zero in the absence of
5628 multiple inheritance. */
5630 newobj->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
5631 if (nbits != 0)
5632 return 0;
5635 /* The last piece of baseclass information is the type of the
5636 base class. Read it, and remember it's type name as this
5637 field's name. */
5639 newobj->field.set_type (read_type (pp, objfile));
5640 newobj->field.set_name (newobj->field.type ()->name ());
5642 /* Skip trailing ';' and bump count of number of fields seen. */
5643 if (**pp == ';')
5644 (*pp)++;
5645 else
5646 return 0;
5648 return 1;
5651 /* The tail end of stabs for C++ classes that contain a virtual function
5652 pointer contains a tilde, a %, and a type number.
5653 The type number refers to the base class (possibly this class itself) which
5654 contains the vtable pointer for the current class.
5656 This function is called when we have parsed all the method declarations,
5657 so we can look for the vptr base class info. */
5659 static int
5660 read_tilde_fields (struct stab_field_info *fip, const char **pp,
5661 struct type *type, struct objfile *objfile)
5663 const char *p;
5665 STABS_CONTINUE (pp, objfile);
5667 /* If we are positioned at a ';', then skip it. */
5668 if (**pp == ';')
5670 (*pp)++;
5673 if (**pp == '~')
5675 (*pp)++;
5677 if (**pp == '=' || **pp == '+' || **pp == '-')
5679 /* Obsolete flags that used to indicate the presence
5680 of constructors and/or destructors. */
5681 (*pp)++;
5684 /* Read either a '%' or the final ';'. */
5685 if (*(*pp)++ == '%')
5687 /* The next number is the type number of the base class
5688 (possibly our own class) which supplies the vtable for
5689 this class. Parse it out, and search that class to find
5690 its vtable pointer, and install those into TYPE_VPTR_BASETYPE
5691 and TYPE_VPTR_FIELDNO. */
5693 struct type *t;
5694 int i;
5696 t = read_type (pp, objfile);
5697 p = (*pp)++;
5698 while (*p != '\0' && *p != ';')
5700 p++;
5702 if (*p == '\0')
5704 /* Premature end of symbol. */
5705 return 0;
5708 set_type_vptr_basetype (type, t);
5709 if (type == t) /* Our own class provides vtbl ptr. */
5711 for (i = t->num_fields () - 1;
5712 i >= TYPE_N_BASECLASSES (t);
5713 --i)
5715 const char *name = t->field (i).name ();
5717 if (!strncmp (name, vptr_name, sizeof (vptr_name) - 2)
5718 && is_cplus_marker (name[sizeof (vptr_name) - 2]))
5720 set_type_vptr_fieldno (type, i);
5721 goto gotit;
5724 /* Virtual function table field not found. */
5725 complaint (_("virtual function table pointer "
5726 "not found when defining class `%s'"),
5727 type->name ());
5728 return 0;
5730 else
5732 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
5735 gotit:
5736 *pp = p + 1;
5739 return 1;
5742 static int
5743 attach_fn_fields_to_type (struct stab_field_info *fip, struct type *type)
5745 int n;
5747 for (n = TYPE_NFN_FIELDS (type);
5748 fip->fnlist != NULL;
5749 fip->fnlist = fip->fnlist->next)
5751 --n; /* Circumvent Sun3 compiler bug. */
5752 TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
5754 return 1;
5757 /* Create the vector of fields, and record how big it is.
5758 We need this info to record proper virtual function table information
5759 for this class's virtual functions. */
5761 static int
5762 attach_fields_to_type (struct stab_field_info *fip, struct type *type,
5763 struct objfile *objfile)
5765 int nfields = 0;
5766 struct stabs_nextfield *scan;
5768 /* Count up the number of fields that we have. */
5770 for (scan = fip->list; scan != NULL; scan = scan->next)
5771 nfields++;
5773 /* Now we know how many fields there are, and whether or not there are any
5774 non-public fields. Record the field count, allocate space for the
5775 array of fields. */
5777 type->alloc_fields (nfields);
5779 /* Copy the saved-up fields into the field vector. Start from the
5780 head of the list, adding to the tail of the field array, so that
5781 they end up in the same order in the array in which they were
5782 added to the list. */
5784 while (nfields-- > 0)
5786 type->field (nfields) = fip->list->field;
5787 fip->list = fip->list->next;
5789 return 1;
5793 /* Complain that the compiler has emitted more than one definition for the
5794 structure type TYPE. */
5795 static void
5796 complain_about_struct_wipeout (struct type *type)
5798 const char *name = "";
5799 const char *kind = "";
5801 if (type->name ())
5803 name = type->name ();
5804 switch (type->code ())
5806 case TYPE_CODE_STRUCT: kind = "struct "; break;
5807 case TYPE_CODE_UNION: kind = "union "; break;
5808 case TYPE_CODE_ENUM: kind = "enum "; break;
5809 default: kind = "";
5812 else
5814 name = "<unknown>";
5815 kind = "";
5818 complaint (_("struct/union type gets multiply defined: %s%s"), kind, name);
5821 /* Set the length for all variants of a same main_type, which are
5822 connected in the closed chain.
5824 This is something that needs to be done when a type is defined *after*
5825 some cross references to this type have already been read. Consider
5826 for instance the following scenario where we have the following two
5827 stabs entries:
5829 .stabs "t:p(0,21)=*(0,22)=k(0,23)=xsdummy:",160,0,28,-24
5830 .stabs "dummy:T(0,23)=s16x:(0,1),0,3[...]"
5832 A stubbed version of type dummy is created while processing the first
5833 stabs entry. The length of that type is initially set to zero, since
5834 it is unknown at this point. Also, a "constant" variation of type
5835 "dummy" is created as well (this is the "(0,22)=k(0,23)" section of
5836 the stabs line).
5838 The second stabs entry allows us to replace the stubbed definition
5839 with the real definition. However, we still need to adjust the length
5840 of the "constant" variation of that type, as its length was left
5841 untouched during the main type replacement... */
5843 static void
5844 set_length_in_type_chain (struct type *type)
5846 struct type *ntype = TYPE_CHAIN (type);
5848 while (ntype != type)
5850 if (ntype->length () == 0)
5851 ntype->set_length (type->length ());
5852 else
5853 complain_about_struct_wipeout (ntype);
5854 ntype = TYPE_CHAIN (ntype);
5858 /* Read the description of a structure (or union type) and return an object
5859 describing the type.
5861 PP points to a character pointer that points to the next unconsumed token
5862 in the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
5863 *PP will point to "4a:1,0,32;;".
5865 TYPE points to an incomplete type that needs to be filled in.
5867 OBJFILE points to the current objfile from which the stabs information is
5868 being read. (Note that it is redundant in that TYPE also contains a pointer
5869 to this same objfile, so it might be a good idea to eliminate it. FIXME).
5872 static struct type *
5873 read_struct_type (const char **pp, struct type *type, enum type_code type_code,
5874 struct objfile *objfile)
5876 struct stab_field_info fi;
5878 /* When describing struct/union/class types in stabs, G++ always drops
5879 all qualifications from the name. So if you've got:
5880 struct A { ... struct B { ... }; ... };
5881 then G++ will emit stabs for `struct A::B' that call it simply
5882 `struct B'. Obviously, if you've got a real top-level definition for
5883 `struct B', or other nested definitions, this is going to cause
5884 problems.
5886 Obviously, GDB can't fix this by itself, but it can at least avoid
5887 scribbling on existing structure type objects when new definitions
5888 appear. */
5889 if (! (type->code () == TYPE_CODE_UNDEF
5890 || type->is_stub ()))
5892 complain_about_struct_wipeout (type);
5894 /* It's probably best to return the type unchanged. */
5895 return type;
5898 INIT_CPLUS_SPECIFIC (type);
5899 type->set_code (type_code);
5900 type->set_is_stub (false);
5902 /* First comes the total size in bytes. */
5905 int nbits;
5907 type->set_length (read_huge_number (pp, 0, &nbits, 0));
5908 if (nbits != 0)
5909 return error_type (pp, objfile);
5910 set_length_in_type_chain (type);
5913 /* Now read the baseclasses, if any, read the regular C struct or C++
5914 class member fields, attach the fields to the type, read the C++
5915 member functions, attach them to the type, and then read any tilde
5916 field (baseclass specifier for the class holding the main vtable). */
5918 if (!read_baseclasses (&fi, pp, type, objfile)
5919 || !read_struct_fields (&fi, pp, type, objfile)
5920 || !attach_fields_to_type (&fi, type, objfile)
5921 || !read_member_functions (&fi, pp, type, objfile)
5922 || !attach_fn_fields_to_type (&fi, type)
5923 || !read_tilde_fields (&fi, pp, type, objfile))
5925 type = error_type (pp, objfile);
5928 return (type);
5931 /* Read a definition of an array type,
5932 and create and return a suitable type object.
5933 Also creates a range type which represents the bounds of that
5934 array. */
5936 static struct type *
5937 read_array_type (const char **pp, struct type *type,
5938 struct objfile *objfile)
5940 struct type *index_type, *element_type, *range_type;
5941 int lower, upper;
5942 int adjustable = 0;
5943 int nbits;
5945 /* Format of an array type:
5946 "ar<index type>;lower;upper;<array_contents_type>".
5947 OS9000: "arlower,upper;<array_contents_type>".
5949 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
5950 for these, produce a type like float[][]. */
5953 index_type = read_type (pp, objfile);
5954 if (**pp != ';')
5955 /* Improper format of array type decl. */
5956 return error_type (pp, objfile);
5957 ++*pp;
5960 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
5962 (*pp)++;
5963 adjustable = 1;
5965 lower = read_huge_number (pp, ';', &nbits, 0);
5967 if (nbits != 0)
5968 return error_type (pp, objfile);
5970 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
5972 (*pp)++;
5973 adjustable = 1;
5975 upper = read_huge_number (pp, ';', &nbits, 0);
5976 if (nbits != 0)
5977 return error_type (pp, objfile);
5979 element_type = read_type (pp, objfile);
5981 if (adjustable)
5983 lower = 0;
5984 upper = -1;
5987 type_allocator alloc (objfile, get_current_subfile ()->language);
5988 range_type =
5989 create_static_range_type (alloc, index_type, lower, upper);
5990 type_allocator smash_alloc (type, type_allocator::SMASH);
5991 type = create_array_type (smash_alloc, element_type, range_type);
5993 return type;
5997 /* Read a definition of an enumeration type,
5998 and create and return a suitable type object.
5999 Also defines the symbols that represent the values of the type. */
6001 static struct type *
6002 read_enum_type (const char **pp, struct type *type,
6003 struct objfile *objfile)
6005 struct gdbarch *gdbarch = objfile->arch ();
6006 const char *p;
6007 char *name;
6008 long n;
6009 struct symbol *sym;
6010 int nsyms = 0;
6011 struct pending **symlist;
6012 struct pending *osyms, *syms;
6013 int o_nsyms;
6014 int nbits;
6015 int unsigned_enum = 1;
6017 #if 0
6018 /* FIXME! The stabs produced by Sun CC merrily define things that ought
6019 to be file-scope, between N_FN entries, using N_LSYM. What's a mother
6020 to do? For now, force all enum values to file scope. */
6021 if (within_function)
6022 symlist = get_local_symbols ();
6023 else
6024 #endif
6025 symlist = get_file_symbols ();
6026 osyms = *symlist;
6027 o_nsyms = osyms ? osyms->nsyms : 0;
6029 /* The aix4 compiler emits an extra field before the enum members;
6030 my guess is it's a type of some sort. Just ignore it. */
6031 if (**pp == '-')
6033 /* Skip over the type. */
6034 while (**pp != ':')
6035 (*pp)++;
6037 /* Skip over the colon. */
6038 (*pp)++;
6041 /* Read the value-names and their values.
6042 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
6043 A semicolon or comma instead of a NAME means the end. */
6044 while (**pp && **pp != ';' && **pp != ',')
6046 STABS_CONTINUE (pp, objfile);
6047 p = *pp;
6048 while (*p != ':')
6049 p++;
6050 name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
6051 *pp = p + 1;
6052 n = read_huge_number (pp, ',', &nbits, 0);
6053 if (nbits != 0)
6054 return error_type (pp, objfile);
6056 sym = new (&objfile->objfile_obstack) symbol;
6057 sym->set_linkage_name (name);
6058 sym->set_language (get_current_subfile ()->language,
6059 &objfile->objfile_obstack);
6060 sym->set_aclass_index (LOC_CONST);
6061 sym->set_domain (VAR_DOMAIN);
6062 sym->set_value_longest (n);
6063 if (n < 0)
6064 unsigned_enum = 0;
6065 add_symbol_to_list (sym, symlist);
6066 nsyms++;
6069 if (**pp == ';')
6070 (*pp)++; /* Skip the semicolon. */
6072 /* Now fill in the fields of the type-structure. */
6074 type->set_length (gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT);
6075 set_length_in_type_chain (type);
6076 type->set_code (TYPE_CODE_ENUM);
6077 type->set_is_stub (false);
6078 if (unsigned_enum)
6079 type->set_is_unsigned (true);
6080 type->alloc_fields (nsyms);
6082 /* Find the symbols for the values and put them into the type.
6083 The symbols can be found in the symlist that we put them on
6084 to cause them to be defined. osyms contains the old value
6085 of that symlist; everything up to there was defined by us. */
6086 /* Note that we preserve the order of the enum constants, so
6087 that in something like "enum {FOO, LAST_THING=FOO}" we print
6088 FOO, not LAST_THING. */
6090 for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
6092 int last = syms == osyms ? o_nsyms : 0;
6093 int j = syms->nsyms;
6095 for (; --j >= last; --n)
6097 struct symbol *xsym = syms->symbol[j];
6099 xsym->set_type (type);
6100 type->field (n).set_name (xsym->linkage_name ());
6101 type->field (n).set_loc_enumval (xsym->value_longest ());
6102 type->field (n).set_bitsize (0);
6104 if (syms == osyms)
6105 break;
6108 return type;
6111 /* Sun's ACC uses a somewhat saner method for specifying the builtin
6112 typedefs in every file (for int, long, etc):
6114 type = b <signed> <width> <format type>; <offset>; <nbits>
6115 signed = u or s.
6116 optional format type = c or b for char or boolean.
6117 offset = offset from high order bit to start bit of type.
6118 width is # bytes in object of this type, nbits is # bits in type.
6120 The width/offset stuff appears to be for small objects stored in
6121 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
6122 FIXME. */
6124 static struct type *
6125 read_sun_builtin_type (const char **pp, int typenums[2], struct objfile *objfile)
6127 int type_bits;
6128 int nbits;
6129 int unsigned_type;
6130 int boolean_type = 0;
6132 switch (**pp)
6134 case 's':
6135 unsigned_type = 0;
6136 break;
6137 case 'u':
6138 unsigned_type = 1;
6139 break;
6140 default:
6141 return error_type (pp, objfile);
6143 (*pp)++;
6145 /* For some odd reason, all forms of char put a c here. This is strange
6146 because no other type has this honor. We can safely ignore this because
6147 we actually determine 'char'acterness by the number of bits specified in
6148 the descriptor.
6149 Boolean forms, e.g Fortran logical*X, put a b here. */
6151 if (**pp == 'c')
6152 (*pp)++;
6153 else if (**pp == 'b')
6155 boolean_type = 1;
6156 (*pp)++;
6159 /* The first number appears to be the number of bytes occupied
6160 by this type, except that unsigned short is 4 instead of 2.
6161 Since this information is redundant with the third number,
6162 we will ignore it. */
6163 read_huge_number (pp, ';', &nbits, 0);
6164 if (nbits != 0)
6165 return error_type (pp, objfile);
6167 /* The second number is always 0, so ignore it too. */
6168 read_huge_number (pp, ';', &nbits, 0);
6169 if (nbits != 0)
6170 return error_type (pp, objfile);
6172 /* The third number is the number of bits for this type. */
6173 type_bits = read_huge_number (pp, 0, &nbits, 0);
6174 if (nbits != 0)
6175 return error_type (pp, objfile);
6176 /* The type *should* end with a semicolon. If it are embedded
6177 in a larger type the semicolon may be the only way to know where
6178 the type ends. If this type is at the end of the stabstring we
6179 can deal with the omitted semicolon (but we don't have to like
6180 it). Don't bother to complain(), Sun's compiler omits the semicolon
6181 for "void". */
6182 if (**pp == ';')
6183 ++(*pp);
6185 type_allocator alloc (objfile, get_current_subfile ()->language);
6186 if (type_bits == 0)
6188 struct type *type = alloc.new_type (TYPE_CODE_VOID,
6189 TARGET_CHAR_BIT, nullptr);
6190 if (unsigned_type)
6191 type->set_is_unsigned (true);
6193 return type;
6196 if (boolean_type)
6197 return init_boolean_type (alloc, type_bits, unsigned_type, NULL);
6198 else
6199 return init_integer_type (alloc, type_bits, unsigned_type, NULL);
6202 static struct type *
6203 read_sun_floating_type (const char **pp, int typenums[2],
6204 struct objfile *objfile)
6206 int nbits;
6207 int details;
6208 int nbytes;
6209 struct type *rettype;
6211 /* The first number has more details about the type, for example
6212 FN_COMPLEX. */
6213 details = read_huge_number (pp, ';', &nbits, 0);
6214 if (nbits != 0)
6215 return error_type (pp, objfile);
6217 /* The second number is the number of bytes occupied by this type. */
6218 nbytes = read_huge_number (pp, ';', &nbits, 0);
6219 if (nbits != 0)
6220 return error_type (pp, objfile);
6222 nbits = nbytes * TARGET_CHAR_BIT;
6224 if (details == NF_COMPLEX || details == NF_COMPLEX16
6225 || details == NF_COMPLEX32)
6227 rettype = dbx_init_float_type (objfile, nbits / 2);
6228 return init_complex_type (NULL, rettype);
6231 return dbx_init_float_type (objfile, nbits);
6234 /* Read a number from the string pointed to by *PP.
6235 The value of *PP is advanced over the number.
6236 If END is nonzero, the character that ends the
6237 number must match END, or an error happens;
6238 and that character is skipped if it does match.
6239 If END is zero, *PP is left pointing to that character.
6241 If TWOS_COMPLEMENT_BITS is set to a strictly positive value and if
6242 the number is represented in an octal representation, assume that
6243 it is represented in a 2's complement representation with a size of
6244 TWOS_COMPLEMENT_BITS.
6246 If the number fits in a long, set *BITS to 0 and return the value.
6247 If not, set *BITS to be the number of bits in the number and return 0.
6249 If encounter garbage, set *BITS to -1 and return 0. */
6251 static long
6252 read_huge_number (const char **pp, int end, int *bits,
6253 int twos_complement_bits)
6255 const char *p = *pp;
6256 int sign = 1;
6257 int sign_bit = 0;
6258 long n = 0;
6259 int radix = 10;
6260 char overflow = 0;
6261 int nbits = 0;
6262 int c;
6263 long upper_limit;
6264 int twos_complement_representation = 0;
6266 if (*p == '-')
6268 sign = -1;
6269 p++;
6272 /* Leading zero means octal. GCC uses this to output values larger
6273 than an int (because that would be hard in decimal). */
6274 if (*p == '0')
6276 radix = 8;
6277 p++;
6280 /* Skip extra zeros. */
6281 while (*p == '0')
6282 p++;
6284 if (sign > 0 && radix == 8 && twos_complement_bits > 0)
6286 /* Octal, possibly signed. Check if we have enough chars for a
6287 negative number. */
6289 size_t len;
6290 const char *p1 = p;
6292 while ((c = *p1) >= '0' && c < '8')
6293 p1++;
6295 len = p1 - p;
6296 if (len > twos_complement_bits / 3
6297 || (twos_complement_bits % 3 == 0
6298 && len == twos_complement_bits / 3))
6300 /* Ok, we have enough characters for a signed value, check
6301 for signedness by testing if the sign bit is set. */
6302 sign_bit = (twos_complement_bits % 3 + 2) % 3;
6303 c = *p - '0';
6304 if (c & (1 << sign_bit))
6306 /* Definitely signed. */
6307 twos_complement_representation = 1;
6308 sign = -1;
6313 upper_limit = LONG_MAX / radix;
6315 while ((c = *p++) >= '0' && c < ('0' + radix))
6317 if (n <= upper_limit)
6319 if (twos_complement_representation)
6321 /* Octal, signed, twos complement representation. In
6322 this case, n is the corresponding absolute value. */
6323 if (n == 0)
6325 long sn = c - '0' - ((2 * (c - '0')) | (2 << sign_bit));
6327 n = -sn;
6329 else
6331 n *= radix;
6332 n -= c - '0';
6335 else
6337 /* unsigned representation */
6338 n *= radix;
6339 n += c - '0'; /* FIXME this overflows anyway. */
6342 else
6343 overflow = 1;
6345 /* This depends on large values being output in octal, which is
6346 what GCC does. */
6347 if (radix == 8)
6349 if (nbits == 0)
6351 if (c == '0')
6352 /* Ignore leading zeroes. */
6354 else if (c == '1')
6355 nbits = 1;
6356 else if (c == '2' || c == '3')
6357 nbits = 2;
6358 else
6359 nbits = 3;
6361 else
6362 nbits += 3;
6365 if (end)
6367 if (c && c != end)
6369 if (bits != NULL)
6370 *bits = -1;
6371 return 0;
6374 else
6375 --p;
6377 if (radix == 8 && twos_complement_bits > 0 && nbits > twos_complement_bits)
6379 /* We were supposed to parse a number with maximum
6380 TWOS_COMPLEMENT_BITS bits, but something went wrong. */
6381 if (bits != NULL)
6382 *bits = -1;
6383 return 0;
6386 *pp = p;
6387 if (overflow)
6389 if (nbits == 0)
6391 /* Large decimal constants are an error (because it is hard to
6392 count how many bits are in them). */
6393 if (bits != NULL)
6394 *bits = -1;
6395 return 0;
6398 /* -0x7f is the same as 0x80. So deal with it by adding one to
6399 the number of bits. Two's complement representation octals
6400 can't have a '-' in front. */
6401 if (sign == -1 && !twos_complement_representation)
6402 ++nbits;
6403 if (bits)
6404 *bits = nbits;
6406 else
6408 if (bits)
6409 *bits = 0;
6410 return n * sign;
6412 /* It's *BITS which has the interesting information. */
6413 return 0;
6416 static struct type *
6417 read_range_type (const char **pp, int typenums[2], int type_size,
6418 struct objfile *objfile)
6420 struct gdbarch *gdbarch = objfile->arch ();
6421 const char *orig_pp = *pp;
6422 int rangenums[2];
6423 long n2, n3;
6424 int n2bits, n3bits;
6425 int self_subrange;
6426 struct type *result_type;
6427 struct type *index_type = NULL;
6429 /* First comes a type we are a subrange of.
6430 In C it is usually 0, 1 or the type being defined. */
6431 if (read_type_number (pp, rangenums) != 0)
6432 return error_type (pp, objfile);
6433 self_subrange = (rangenums[0] == typenums[0] &&
6434 rangenums[1] == typenums[1]);
6436 if (**pp == '=')
6438 *pp = orig_pp;
6439 index_type = read_type (pp, objfile);
6442 /* A semicolon should now follow; skip it. */
6443 if (**pp == ';')
6444 (*pp)++;
6446 /* The remaining two operands are usually lower and upper bounds
6447 of the range. But in some special cases they mean something else. */
6448 n2 = read_huge_number (pp, ';', &n2bits, type_size);
6449 n3 = read_huge_number (pp, ';', &n3bits, type_size);
6451 if (n2bits == -1 || n3bits == -1)
6452 return error_type (pp, objfile);
6454 type_allocator alloc (objfile, get_current_subfile ()->language);
6456 if (index_type)
6457 goto handle_true_range;
6459 /* If limits are huge, must be large integral type. */
6460 if (n2bits != 0 || n3bits != 0)
6462 char got_signed = 0;
6463 char got_unsigned = 0;
6464 /* Number of bits in the type. */
6465 int nbits = 0;
6467 /* If a type size attribute has been specified, the bounds of
6468 the range should fit in this size. If the lower bounds needs
6469 more bits than the upper bound, then the type is signed. */
6470 if (n2bits <= type_size && n3bits <= type_size)
6472 if (n2bits == type_size && n2bits > n3bits)
6473 got_signed = 1;
6474 else
6475 got_unsigned = 1;
6476 nbits = type_size;
6478 /* Range from 0 to <large number> is an unsigned large integral type. */
6479 else if ((n2bits == 0 && n2 == 0) && n3bits != 0)
6481 got_unsigned = 1;
6482 nbits = n3bits;
6484 /* Range from <large number> to <large number>-1 is a large signed
6485 integral type. Take care of the case where <large number> doesn't
6486 fit in a long but <large number>-1 does. */
6487 else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
6488 || (n2bits != 0 && n3bits == 0
6489 && (n2bits == sizeof (long) * HOST_CHAR_BIT)
6490 && n3 == LONG_MAX))
6492 got_signed = 1;
6493 nbits = n2bits;
6496 if (got_signed || got_unsigned)
6497 return init_integer_type (alloc, nbits, got_unsigned, NULL);
6498 else
6499 return error_type (pp, objfile);
6502 /* A type defined as a subrange of itself, with bounds both 0, is void. */
6503 if (self_subrange && n2 == 0 && n3 == 0)
6504 return alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, nullptr);
6506 /* If n3 is zero and n2 is positive, we want a floating type, and n2
6507 is the width in bytes.
6509 Fortran programs appear to use this for complex types also. To
6510 distinguish between floats and complex, g77 (and others?) seem
6511 to use self-subranges for the complexes, and subranges of int for
6512 the floats.
6514 Also note that for complexes, g77 sets n2 to the size of one of
6515 the member floats, not the whole complex beast. My guess is that
6516 this was to work well with pre-COMPLEX versions of gdb. */
6518 if (n3 == 0 && n2 > 0)
6520 struct type *float_type
6521 = dbx_init_float_type (objfile, n2 * TARGET_CHAR_BIT);
6523 if (self_subrange)
6524 return init_complex_type (NULL, float_type);
6525 else
6526 return float_type;
6529 /* If the upper bound is -1, it must really be an unsigned integral. */
6531 else if (n2 == 0 && n3 == -1)
6533 int bits = type_size;
6535 if (bits <= 0)
6537 /* We don't know its size. It is unsigned int or unsigned
6538 long. GCC 2.3.3 uses this for long long too, but that is
6539 just a GDB 3.5 compatibility hack. */
6540 bits = gdbarch_int_bit (gdbarch);
6543 return init_integer_type (alloc, bits, 1, NULL);
6546 /* Special case: char is defined (Who knows why) as a subrange of
6547 itself with range 0-127. */
6548 else if (self_subrange && n2 == 0 && n3 == 127)
6550 struct type *type = init_integer_type (alloc, TARGET_CHAR_BIT,
6551 0, NULL);
6552 type->set_has_no_signedness (true);
6553 return type;
6555 /* We used to do this only for subrange of self or subrange of int. */
6556 else if (n2 == 0)
6558 /* -1 is used for the upper bound of (4 byte) "unsigned int" and
6559 "unsigned long", and we already checked for that,
6560 so don't need to test for it here. */
6562 if (n3 < 0)
6563 /* n3 actually gives the size. */
6564 return init_integer_type (alloc, -n3 * TARGET_CHAR_BIT, 1, NULL);
6566 /* Is n3 == 2**(8n)-1 for some integer n? Then it's an
6567 unsigned n-byte integer. But do require n to be a power of
6568 two; we don't want 3- and 5-byte integers flying around. */
6570 int bytes;
6571 unsigned long bits;
6573 bits = n3;
6574 for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
6575 bits >>= 8;
6576 if (bits == 0
6577 && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
6578 return init_integer_type (alloc, bytes * TARGET_CHAR_BIT, 1, NULL);
6581 /* I think this is for Convex "long long". Since I don't know whether
6582 Convex sets self_subrange, I also accept that particular size regardless
6583 of self_subrange. */
6584 else if (n3 == 0 && n2 < 0
6585 && (self_subrange
6586 || n2 == -gdbarch_long_long_bit
6587 (gdbarch) / TARGET_CHAR_BIT))
6588 return init_integer_type (alloc, -n2 * TARGET_CHAR_BIT, 0, NULL);
6589 else if (n2 == -n3 - 1)
6591 if (n3 == 0x7f)
6592 return init_integer_type (alloc, 8, 0, NULL);
6593 if (n3 == 0x7fff)
6594 return init_integer_type (alloc, 16, 0, NULL);
6595 if (n3 == 0x7fffffff)
6596 return init_integer_type (alloc, 32, 0, NULL);
6599 /* We have a real range type on our hands. Allocate space and
6600 return a real pointer. */
6601 handle_true_range:
6603 if (self_subrange)
6604 index_type = builtin_type (objfile)->builtin_int;
6605 else
6606 index_type = *dbx_lookup_type (rangenums, objfile);
6607 if (index_type == NULL)
6609 /* Does this actually ever happen? Is that why we are worrying
6610 about dealing with it rather than just calling error_type? */
6612 complaint (_("base type %d of range type is not defined"), rangenums[1]);
6614 index_type = builtin_type (objfile)->builtin_int;
6617 result_type
6618 = create_static_range_type (alloc, index_type, n2, n3);
6619 return (result_type);
6622 /* Read in an argument list. This is a list of types, separated by commas
6623 and terminated with END. Return the list of types read in, or NULL
6624 if there is an error. */
6626 static struct field *
6627 read_args (const char **pp, int end, struct objfile *objfile, int *nargsp,
6628 int *varargsp)
6630 /* FIXME! Remove this arbitrary limit! */
6631 struct type *types[1024]; /* Allow for fns of 1023 parameters. */
6632 int n = 0, i;
6633 struct field *rval;
6635 while (**pp != end)
6637 if (**pp != ',')
6638 /* Invalid argument list: no ','. */
6639 return NULL;
6640 (*pp)++;
6641 STABS_CONTINUE (pp, objfile);
6642 types[n++] = read_type (pp, objfile);
6644 (*pp)++; /* get past `end' (the ':' character). */
6646 if (n == 0)
6648 /* We should read at least the THIS parameter here. Some broken stabs
6649 output contained `(0,41),(0,42)=@s8;-16;,(0,43),(0,1);' where should
6650 have been present ";-16,(0,43)" reference instead. This way the
6651 excessive ";" marker prematurely stops the parameters parsing. */
6653 complaint (_("Invalid (empty) method arguments"));
6654 *varargsp = 0;
6656 else if (types[n - 1]->code () != TYPE_CODE_VOID)
6657 *varargsp = 1;
6658 else
6660 n--;
6661 *varargsp = 0;
6664 rval = XCNEWVEC (struct field, n);
6665 for (i = 0; i < n; i++)
6666 rval[i].set_type (types[i]);
6667 *nargsp = n;
6668 return rval;
6671 /* Common block handling. */
6673 /* List of symbols declared since the last BCOMM. This list is a tail
6674 of local_symbols. When ECOMM is seen, the symbols on the list
6675 are noted so their proper addresses can be filled in later,
6676 using the common block base address gotten from the assembler
6677 stabs. */
6679 static struct pending *common_block;
6680 static int common_block_i;
6682 /* Name of the current common block. We get it from the BCOMM instead of the
6683 ECOMM to match IBM documentation (even though IBM puts the name both places
6684 like everyone else). */
6685 static char *common_block_name;
6687 /* Process a N_BCOMM symbol. The storage for NAME is not guaranteed
6688 to remain after this function returns. */
6690 void
6691 common_block_start (const char *name, struct objfile *objfile)
6693 if (common_block_name != NULL)
6695 complaint (_("Invalid symbol data: common block within common block"));
6697 common_block = *get_local_symbols ();
6698 common_block_i = common_block ? common_block->nsyms : 0;
6699 common_block_name = obstack_strdup (&objfile->objfile_obstack, name);
6702 /* Process a N_ECOMM symbol. */
6704 void
6705 common_block_end (struct objfile *objfile)
6707 /* Symbols declared since the BCOMM are to have the common block
6708 start address added in when we know it. common_block and
6709 common_block_i point to the first symbol after the BCOMM in
6710 the local_symbols list; copy the list and hang it off the
6711 symbol for the common block name for later fixup. */
6712 int i;
6713 struct symbol *sym;
6714 struct pending *newobj = 0;
6715 struct pending *next;
6716 int j;
6718 if (common_block_name == NULL)
6720 complaint (_("ECOMM symbol unmatched by BCOMM"));
6721 return;
6724 sym = new (&objfile->objfile_obstack) symbol;
6725 /* Note: common_block_name already saved on objfile_obstack. */
6726 sym->set_linkage_name (common_block_name);
6727 sym->set_aclass_index (LOC_BLOCK);
6729 /* Now we copy all the symbols which have been defined since the BCOMM. */
6731 /* Copy all the struct pendings before common_block. */
6732 for (next = *get_local_symbols ();
6733 next != NULL && next != common_block;
6734 next = next->next)
6736 for (j = 0; j < next->nsyms; j++)
6737 add_symbol_to_list (next->symbol[j], &newobj);
6740 /* Copy however much of COMMON_BLOCK we need. If COMMON_BLOCK is
6741 NULL, it means copy all the local symbols (which we already did
6742 above). */
6744 if (common_block != NULL)
6745 for (j = common_block_i; j < common_block->nsyms; j++)
6746 add_symbol_to_list (common_block->symbol[j], &newobj);
6748 sym->set_type ((struct type *) newobj);
6750 /* Should we be putting local_symbols back to what it was?
6751 Does it matter? */
6753 i = hashname (sym->linkage_name ());
6754 sym->set_value_chain (global_sym_chain[i]);
6755 global_sym_chain[i] = sym;
6756 common_block_name = NULL;
6759 /* Add a common block's start address to the offset of each symbol
6760 declared to be in it (by being between a BCOMM/ECOMM pair that uses
6761 the common block name). */
6763 static void
6764 fix_common_block (struct symbol *sym, CORE_ADDR valu, int section_index)
6766 struct pending *next = (struct pending *) sym->type ();
6768 for (; next; next = next->next)
6770 int j;
6772 for (j = next->nsyms - 1; j >= 0; j--)
6774 next->symbol[j]->set_value_address
6775 (next->symbol[j]->value_address () + valu);
6776 next->symbol[j]->set_section_index (section_index);
6783 /* Add {TYPE, TYPENUMS} to the NONAME_UNDEFS vector.
6784 See add_undefined_type for more details. */
6786 static void
6787 add_undefined_type_noname (struct type *type, int typenums[2])
6789 struct nat nat;
6791 nat.typenums[0] = typenums [0];
6792 nat.typenums[1] = typenums [1];
6793 nat.type = type;
6795 if (noname_undefs_length == noname_undefs_allocated)
6797 noname_undefs_allocated *= 2;
6798 noname_undefs = (struct nat *)
6799 xrealloc ((char *) noname_undefs,
6800 noname_undefs_allocated * sizeof (struct nat));
6802 noname_undefs[noname_undefs_length++] = nat;
6805 /* Add TYPE to the UNDEF_TYPES vector.
6806 See add_undefined_type for more details. */
6808 static void
6809 add_undefined_type_1 (struct type *type)
6811 if (undef_types_length == undef_types_allocated)
6813 undef_types_allocated *= 2;
6814 undef_types = (struct type **)
6815 xrealloc ((char *) undef_types,
6816 undef_types_allocated * sizeof (struct type *));
6818 undef_types[undef_types_length++] = type;
6821 /* What about types defined as forward references inside of a small lexical
6822 scope? */
6823 /* Add a type to the list of undefined types to be checked through
6824 once this file has been read in.
6826 In practice, we actually maintain two such lists: The first list
6827 (UNDEF_TYPES) is used for types whose name has been provided, and
6828 concerns forward references (eg 'xs' or 'xu' forward references);
6829 the second list (NONAME_UNDEFS) is used for types whose name is
6830 unknown at creation time, because they were referenced through
6831 their type number before the actual type was declared.
6832 This function actually adds the given type to the proper list. */
6834 static void
6835 add_undefined_type (struct type *type, int typenums[2])
6837 if (type->name () == NULL)
6838 add_undefined_type_noname (type, typenums);
6839 else
6840 add_undefined_type_1 (type);
6843 /* Try to fix all undefined types pushed on the UNDEF_TYPES vector. */
6845 static void
6846 cleanup_undefined_types_noname (struct objfile *objfile)
6848 int i;
6850 for (i = 0; i < noname_undefs_length; i++)
6852 struct nat nat = noname_undefs[i];
6853 struct type **type;
6855 type = dbx_lookup_type (nat.typenums, objfile);
6856 if (nat.type != *type && (*type)->code () != TYPE_CODE_UNDEF)
6858 /* The instance flags of the undefined type are still unset,
6859 and needs to be copied over from the reference type.
6860 Since replace_type expects them to be identical, we need
6861 to set these flags manually before hand. */
6862 nat.type->set_instance_flags ((*type)->instance_flags ());
6863 replace_type (nat.type, *type);
6867 noname_undefs_length = 0;
6870 /* Go through each undefined type, see if it's still undefined, and fix it
6871 up if possible. We have two kinds of undefined types:
6873 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
6874 Fix: update array length using the element bounds
6875 and the target type's length.
6876 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
6877 yet defined at the time a pointer to it was made.
6878 Fix: Do a full lookup on the struct/union tag. */
6880 static void
6881 cleanup_undefined_types_1 (void)
6883 struct type **type;
6885 /* Iterate over every undefined type, and look for a symbol whose type
6886 matches our undefined type. The symbol matches if:
6887 1. It is a typedef in the STRUCT domain;
6888 2. It has the same name, and same type code;
6889 3. The instance flags are identical.
6891 It is important to check the instance flags, because we have seen
6892 examples where the debug info contained definitions such as:
6894 "foo_t:t30=B31=xefoo_t:"
6896 In this case, we have created an undefined type named "foo_t" whose
6897 instance flags is null (when processing "xefoo_t"), and then created
6898 another type with the same name, but with different instance flags
6899 ('B' means volatile). I think that the definition above is wrong,
6900 since the same type cannot be volatile and non-volatile at the same
6901 time, but we need to be able to cope with it when it happens. The
6902 approach taken here is to treat these two types as different. */
6904 for (type = undef_types; type < undef_types + undef_types_length; type++)
6906 switch ((*type)->code ())
6909 case TYPE_CODE_STRUCT:
6910 case TYPE_CODE_UNION:
6911 case TYPE_CODE_ENUM:
6913 /* Check if it has been defined since. Need to do this here
6914 as well as in check_typedef to deal with the (legitimate in
6915 C though not C++) case of several types with the same name
6916 in different source files. */
6917 if ((*type)->is_stub ())
6919 struct pending *ppt;
6920 int i;
6921 /* Name of the type, without "struct" or "union". */
6922 const char *type_name = (*type)->name ();
6924 if (type_name == NULL)
6926 complaint (_("need a type name"));
6927 break;
6929 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
6931 for (i = 0; i < ppt->nsyms; i++)
6933 struct symbol *sym = ppt->symbol[i];
6935 if (sym->aclass () == LOC_TYPEDEF
6936 && sym->domain () == STRUCT_DOMAIN
6937 && (sym->type ()->code () == (*type)->code ())
6938 && ((*type)->instance_flags ()
6939 == sym->type ()->instance_flags ())
6940 && strcmp (sym->linkage_name (), type_name) == 0)
6941 replace_type (*type, sym->type ());
6946 break;
6948 default:
6950 complaint (_("forward-referenced types left unresolved, "
6951 "type code %d."),
6952 (*type)->code ());
6954 break;
6958 undef_types_length = 0;
6961 /* Try to fix all the undefined types we encountered while processing
6962 this unit. */
6964 void
6965 cleanup_undefined_stabs_types (struct objfile *objfile)
6967 cleanup_undefined_types_1 ();
6968 cleanup_undefined_types_noname (objfile);
6971 /* See stabsread.h. */
6973 void
6974 scan_file_globals (struct objfile *objfile)
6976 int hash;
6977 struct symbol *sym, *prev;
6978 struct objfile *resolve_objfile;
6980 /* SVR4 based linkers copy referenced global symbols from shared
6981 libraries to the main executable.
6982 If we are scanning the symbols for a shared library, try to resolve
6983 them from the minimal symbols of the main executable first. */
6985 if (current_program_space->symfile_object_file
6986 && objfile != current_program_space->symfile_object_file)
6987 resolve_objfile = current_program_space->symfile_object_file;
6988 else
6989 resolve_objfile = objfile;
6991 while (1)
6993 /* Avoid expensive loop through all minimal symbols if there are
6994 no unresolved symbols. */
6995 for (hash = 0; hash < HASHSIZE; hash++)
6997 if (global_sym_chain[hash])
6998 break;
7000 if (hash >= HASHSIZE)
7001 return;
7003 for (minimal_symbol *msymbol : resolve_objfile->msymbols ())
7005 QUIT;
7007 /* Skip static symbols. */
7008 switch (msymbol->type ())
7010 case mst_file_text:
7011 case mst_file_data:
7012 case mst_file_bss:
7013 continue;
7014 default:
7015 break;
7018 prev = NULL;
7020 /* Get the hash index and check all the symbols
7021 under that hash index. */
7023 hash = hashname (msymbol->linkage_name ());
7025 for (sym = global_sym_chain[hash]; sym;)
7027 if (strcmp (msymbol->linkage_name (), sym->linkage_name ()) == 0)
7029 /* Splice this symbol out of the hash chain and
7030 assign the value we have to it. */
7031 if (prev)
7033 prev->set_value_chain (sym->value_chain ());
7035 else
7037 global_sym_chain[hash] = sym->value_chain ();
7040 /* Check to see whether we need to fix up a common block. */
7041 /* Note: this code might be executed several times for
7042 the same symbol if there are multiple references. */
7043 if (sym)
7045 if (sym->aclass () == LOC_BLOCK)
7046 fix_common_block
7047 (sym, msymbol->value_address (resolve_objfile),
7048 msymbol->section_index ());
7049 else
7050 sym->set_value_address
7051 (msymbol->value_address (resolve_objfile));
7052 sym->set_section_index (msymbol->section_index ());
7055 if (prev)
7057 sym = prev->value_chain ();
7059 else
7061 sym = global_sym_chain[hash];
7064 else
7066 prev = sym;
7067 sym = sym->value_chain ();
7071 if (resolve_objfile == objfile)
7072 break;
7073 resolve_objfile = objfile;
7076 /* Change the storage class of any remaining unresolved globals to
7077 LOC_UNRESOLVED and remove them from the chain. */
7078 for (hash = 0; hash < HASHSIZE; hash++)
7080 sym = global_sym_chain[hash];
7081 while (sym)
7083 prev = sym;
7084 sym = sym->value_chain ();
7086 /* Change the symbol address from the misleading chain value
7087 to address zero. */
7088 prev->set_value_address (0);
7090 /* Complain about unresolved common block symbols. */
7091 if (prev->aclass () == LOC_STATIC)
7092 prev->set_aclass_index (LOC_UNRESOLVED);
7093 else
7094 complaint (_("%s: common block `%s' from "
7095 "global_sym_chain unresolved"),
7096 objfile_name (objfile), prev->print_name ());
7099 memset (global_sym_chain, 0, sizeof (global_sym_chain));
7102 /* Initialize anything that needs initializing when starting to read
7103 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
7104 to a psymtab. */
7106 void
7107 stabsread_init (void)
7111 /* Initialize anything that needs initializing when a completely new
7112 symbol file is specified (not just adding some symbols from another
7113 file, e.g. a shared library). */
7115 void
7116 stabsread_new_init (void)
7118 /* Empty the hash table of global syms looking for values. */
7119 memset (global_sym_chain, 0, sizeof (global_sym_chain));
7122 /* Initialize anything that needs initializing at the same time as
7123 start_compunit_symtab() is called. */
7125 void
7126 start_stabs (void)
7128 global_stabs = NULL; /* AIX COFF */
7129 /* Leave FILENUM of 0 free for builtin types and this file's types. */
7130 n_this_object_header_files = 1;
7131 type_vector_length = 0;
7132 type_vector = (struct type **) 0;
7133 within_function = 0;
7135 /* FIXME: If common_block_name is not already NULL, we should complain(). */
7136 common_block_name = NULL;
7139 /* Call after end_compunit_symtab(). */
7141 void
7142 end_stabs (void)
7144 if (type_vector)
7146 xfree (type_vector);
7148 type_vector = 0;
7149 type_vector_length = 0;
7150 previous_stab_code = 0;
7153 void
7154 finish_global_stabs (struct objfile *objfile)
7156 if (global_stabs)
7158 patch_block_stabs (*get_global_symbols (), global_stabs, objfile);
7159 xfree (global_stabs);
7160 global_stabs = NULL;
7164 /* Find the end of the name, delimited by a ':', but don't match
7165 ObjC symbols which look like -[Foo bar::]:bla. */
7166 static const char *
7167 find_name_end (const char *name)
7169 const char *s = name;
7171 if (s[0] == '-' || *s == '+')
7173 /* Must be an ObjC method symbol. */
7174 if (s[1] != '[')
7176 error (_("invalid symbol name \"%s\""), name);
7178 s = strchr (s, ']');
7179 if (s == NULL)
7181 error (_("invalid symbol name \"%s\""), name);
7183 return strchr (s, ':');
7185 else
7187 return strchr (s, ':');
7191 /* See stabsread.h. */
7194 hashname (const char *name)
7196 return fast_hash (name, strlen (name)) % HASHSIZE;
7199 /* Initializer for this module. */
7201 void _initialize_stabsread ();
7202 void
7203 _initialize_stabsread ()
7205 undef_types_allocated = 20;
7206 undef_types_length = 0;
7207 undef_types = XNEWVEC (struct type *, undef_types_allocated);
7209 noname_undefs_allocated = 20;
7210 noname_undefs_length = 0;
7211 noname_undefs = XNEWVEC (struct nat, noname_undefs_allocated);
7213 stab_register_index = register_symbol_register_impl (LOC_REGISTER,
7214 &stab_register_funcs);
7215 stab_regparm_index = register_symbol_register_impl (LOC_REGPARM_ADDR,
7216 &stab_register_funcs);