Warn if /proc is not accessible
[external/binutils.git] / gdb / dbxread.c
1 /* Read dbx symbol tables and convert to internal format, for GDB.
2    Copyright (C) 1986-2018 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* This module provides three functions: dbx_symfile_init,
20    which initializes to read a symbol file; dbx_new_init, which 
21    discards existing cached information when all symbols are being
22    discarded; and dbx_symfile_read, which reads a symbol table
23    from a file.
24
25    dbx_symfile_read only does the minimum work necessary for letting the
26    user "name" things symbolically; it does not read the entire symtab.
27    Instead, it reads the external and static symbols and puts them in partial
28    symbol tables.  When more extensive information is requested of a
29    file, the corresponding partial symbol table is mutated into a full
30    fledged symbol table by going back and reading the symbols
31    for real.  dbx_psymtab_to_symtab() is the function that does this */
32
33 #include "defs.h"
34 #if defined(__CYGNUSCLIB__)
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #endif
38
39 #include "gdb_obstack.h"
40 #include <sys/stat.h>
41 #include "symtab.h"
42 #include "breakpoint.h"
43 #include "target.h"
44 #include "gdbcore.h"            /* for bfd stuff */
45 #include "libaout.h"            /* FIXME Secret internal BFD stuff for a.out */
46 #include "filenames.h"
47 #include "objfiles.h"
48 #include "buildsym.h"
49 #include "stabsread.h"
50 #include "gdb-stabs.h"
51 #include "demangle.h"
52 #include "complaints.h"
53 #include "cp-abi.h"
54 #include "cp-support.h"
55 #include "psympriv.h"
56 #include "block.h"
57 #include "aout/aout64.h"
58 #include "aout/stab_gnu.h"      /* We always use GNU stabs, not
59                                    native, now.  */
60 \f
61
62 /* Key for dbx-associated data.  */
63
64 const struct objfile_data *dbx_objfile_data_key;
65
66 /* We put a pointer to this structure in the read_symtab_private field
67    of the psymtab.  */
68
69 struct symloc
70   {
71     /* Offset within the file symbol table of first local symbol for this
72        file.  */
73
74     int ldsymoff;
75
76     /* Length (in bytes) of the section of the symbol table devoted to
77        this file's symbols (actually, the section bracketed may contain
78        more than just this file's symbols).  If ldsymlen is 0, the only
79        reason for this thing's existence is the dependency list.  Nothing
80        else will happen when it is read in.  */
81
82     int ldsymlen;
83
84     /* The size of each symbol in the symbol file (in external form).  */
85
86     int symbol_size;
87
88     /* Further information needed to locate the symbols if they are in
89        an ELF file.  */
90
91     int symbol_offset;
92     int string_offset;
93     int file_string_offset;
94     enum language pst_language;
95   };
96
97 #define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
98 #define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
99 #define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
100 #define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
101 #define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
102 #define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
103 #define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
104 #define PST_LANGUAGE(p) (SYMLOC(p)->pst_language)
105 \f
106
107 /* The objfile we are currently reading.  */
108
109 static struct objfile *dbxread_objfile;
110
111 /* Remember what we deduced to be the source language of this psymtab.  */
112
113 static enum language psymtab_language = language_unknown;
114
115 /* The BFD for this file -- implicit parameter to next_symbol_text.  */
116
117 static bfd *symfile_bfd;
118
119 /* The size of each symbol in the symbol file (in external form).
120    This is set by dbx_symfile_read when building psymtabs, and by
121    dbx_psymtab_to_symtab when building symtabs.  */
122
123 static unsigned symbol_size;
124
125 /* This is the offset of the symbol table in the executable file.  */
126
127 static unsigned symbol_table_offset;
128
129 /* This is the offset of the string table in the executable file.  */
130
131 static unsigned string_table_offset;
132
133 /* For elf+stab executables, the n_strx field is not a simple index
134    into the string table.  Instead, each .o file has a base offset in
135    the string table, and the associated symbols contain offsets from
136    this base.  The following two variables contain the base offset for
137    the current and next .o files.  */
138
139 static unsigned int file_string_table_offset;
140 static unsigned int next_file_string_table_offset;
141
142 /* .o and NLM files contain unrelocated addresses which are based at
143    0.  When non-zero, this flag disables some of the special cases for
144    Solaris elf+stab text addresses at location 0.  */
145
146 static int symfile_relocatable = 0;
147
148 \f
149 /* The lowest text address we have yet encountered.  This is needed
150    because in an a.out file, there is no header field which tells us
151    what address the program is actually going to be loaded at, so we
152    need to make guesses based on the symbols (which *are* relocated to
153    reflect the address it will be loaded at).  */
154
155 static CORE_ADDR lowest_text_address;
156
157 /* Non-zero if there is any line number info in the objfile.  Prevents
158    dbx_end_psymtab from discarding an otherwise empty psymtab.  */
159
160 static int has_line_numbers;
161
162 /* Complaints about the symbols we have encountered.  */
163
164 static void
165 unknown_symtype_complaint (const char *arg1)
166 {
167   complaint (_("unknown symbol type %s"), arg1);
168 }
169
170 static void
171 lbrac_mismatch_complaint (int arg1)
172 {
173   complaint (_("N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d"), arg1);
174 }
175
176 static void
177 repeated_header_complaint (const char *arg1, int arg2)
178 {
179   complaint (_("\"repeated\" header file %s not "
180                "previously seen, at symtab pos %d"),
181              arg1, arg2);
182 }
183
184 /* find_text_range --- find start and end of loadable code sections
185
186    The find_text_range function finds the shortest address range that
187    encloses all sections containing executable code, and stores it in
188    objfile's text_addr and text_size members.
189
190    dbx_symfile_read will use this to finish off the partial symbol
191    table, in some cases.  */
192
193 static void
194 find_text_range (bfd * sym_bfd, struct objfile *objfile)
195 {
196   asection *sec;
197   int found_any = 0;
198   CORE_ADDR start = 0;
199   CORE_ADDR end = 0;
200
201   for (sec = sym_bfd->sections; sec; sec = sec->next)
202     if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
203       {
204         CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
205         CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
206
207         if (found_any)
208           {
209             if (sec_start < start)
210               start = sec_start;
211             if (sec_end > end)
212               end = sec_end;
213           }
214         else
215           {
216             start = sec_start;
217             end = sec_end;
218           }
219
220         found_any = 1;
221       }
222
223   if (!found_any)
224     error (_("Can't find any code sections in symbol file"));
225
226   DBX_TEXT_ADDR (objfile) = start;
227   DBX_TEXT_SIZE (objfile) = end - start;
228 }
229 \f
230
231
232 /* During initial symbol readin, we need to have a structure to keep
233    track of which psymtabs have which bincls in them.  This structure
234    is used during readin to setup the list of dependencies within each
235    partial symbol table.  */
236
237 struct header_file_location
238 {
239   header_file_location (const char *name_, int instance_,
240                         struct partial_symtab *pst_)
241     : name (name_),
242       instance (instance_),
243       pst (pst_)
244   {
245   }
246
247   const char *name;             /* Name of header file */
248   int instance;                 /* See above */
249   struct partial_symtab *pst;   /* Partial symtab that has the
250                                    BINCL/EINCL defs for this file.  */
251 };
252
253 /* The list of bincls.  */
254 static std::vector<struct header_file_location> *bincl_list;
255
256 /* Local function prototypes.  */
257
258 static void read_ofile_symtab (struct objfile *, struct partial_symtab *);
259
260 static void dbx_read_symtab (struct partial_symtab *self,
261                              struct objfile *objfile);
262
263 static void dbx_psymtab_to_symtab_1 (struct objfile *, struct partial_symtab *);
264
265 static void read_dbx_symtab (minimal_symbol_reader &, struct objfile *);
266
267 static struct partial_symtab *find_corresponding_bincl_psymtab (const char *,
268                                                                 int);
269
270 static const char *dbx_next_symbol_text (struct objfile *);
271
272 static void fill_symbuf (bfd *);
273
274 static void dbx_symfile_init (struct objfile *);
275
276 static void dbx_new_init (struct objfile *);
277
278 static void dbx_symfile_read (struct objfile *, symfile_add_flags);
279
280 static void dbx_symfile_finish (struct objfile *);
281
282 static void record_minimal_symbol (minimal_symbol_reader &,
283                                    const char *, CORE_ADDR, int,
284                                    struct objfile *);
285
286 static void add_new_header_file (const char *, int);
287
288 static void add_old_header_file (const char *, int);
289
290 static void add_this_object_header_file (int);
291
292 static struct partial_symtab *start_psymtab (struct objfile *, const char *,
293                                              CORE_ADDR, int,
294                                              std::vector<partial_symbol *> &,
295                                              std::vector<partial_symbol *> &);
296
297 /* Free up old header file tables.  */
298
299 void
300 free_header_files (void)
301 {
302   if (this_object_header_files)
303     {
304       xfree (this_object_header_files);
305       this_object_header_files = NULL;
306     }
307   n_allocated_this_object_header_files = 0;
308 }
309
310 /* Allocate new header file tables.  */
311
312 void
313 init_header_files (void)
314 {
315   n_allocated_this_object_header_files = 10;
316   this_object_header_files = XNEWVEC (int, 10);
317 }
318
319 /* Add header file number I for this object file
320    at the next successive FILENUM.  */
321
322 static void
323 add_this_object_header_file (int i)
324 {
325   if (n_this_object_header_files == n_allocated_this_object_header_files)
326     {
327       n_allocated_this_object_header_files *= 2;
328       this_object_header_files
329         = (int *) xrealloc ((char *) this_object_header_files,
330                        n_allocated_this_object_header_files * sizeof (int));
331     }
332
333   this_object_header_files[n_this_object_header_files++] = i;
334 }
335
336 /* Add to this file an "old" header file, one already seen in
337    a previous object file.  NAME is the header file's name.
338    INSTANCE is its instance code, to select among multiple
339    symbol tables for the same header file.  */
340
341 static void
342 add_old_header_file (const char *name, int instance)
343 {
344   struct header_file *p = HEADER_FILES (dbxread_objfile);
345   int i;
346
347   for (i = 0; i < N_HEADER_FILES (dbxread_objfile); i++)
348     if (filename_cmp (p[i].name, name) == 0 && instance == p[i].instance)
349       {
350         add_this_object_header_file (i);
351         return;
352       }
353   repeated_header_complaint (name, symnum);
354 }
355
356 /* Add to this file a "new" header file: definitions for its types follow.
357    NAME is the header file's name.
358    Most often this happens only once for each distinct header file,
359    but not necessarily.  If it happens more than once, INSTANCE has
360    a different value each time, and references to the header file
361    use INSTANCE values to select among them.
362
363    dbx output contains "begin" and "end" markers for each new header file,
364    but at this level we just need to know which files there have been;
365    so we record the file when its "begin" is seen and ignore the "end".  */
366
367 static void
368 add_new_header_file (const char *name, int instance)
369 {
370   int i;
371   struct header_file *hfile;
372
373   /* Make sure there is room for one more header file.  */
374
375   i = N_ALLOCATED_HEADER_FILES (dbxread_objfile);
376
377   if (N_HEADER_FILES (dbxread_objfile) == i)
378     {
379       if (i == 0)
380         {
381           N_ALLOCATED_HEADER_FILES (dbxread_objfile) = 10;
382           HEADER_FILES (dbxread_objfile) = (struct header_file *)
383             xmalloc (10 * sizeof (struct header_file));
384         }
385       else
386         {
387           i *= 2;
388           N_ALLOCATED_HEADER_FILES (dbxread_objfile) = i;
389           HEADER_FILES (dbxread_objfile) = (struct header_file *)
390             xrealloc ((char *) HEADER_FILES (dbxread_objfile),
391                       (i * sizeof (struct header_file)));
392         }
393     }
394
395   /* Create an entry for this header file.  */
396
397   i = N_HEADER_FILES (dbxread_objfile)++;
398   hfile = HEADER_FILES (dbxread_objfile) + i;
399   hfile->name = xstrdup (name);
400   hfile->instance = instance;
401   hfile->length = 10;
402   hfile->vector = XCNEWVEC (struct type *, 10);
403
404   add_this_object_header_file (i);
405 }
406
407 #if 0
408 static struct type **
409 explicit_lookup_type (int real_filenum, int index)
410 {
411   struct header_file *f = &HEADER_FILES (dbxread_objfile)[real_filenum];
412
413   if (index >= f->length)
414     {
415       f->length *= 2;
416       f->vector = (struct type **)
417         xrealloc (f->vector, f->length * sizeof (struct type *));
418       memset (&f->vector[f->length / 2],
419               '\0', f->length * sizeof (struct type *) / 2);
420     }
421   return &f->vector[index];
422 }
423 #endif
424 \f
425 static void
426 record_minimal_symbol (minimal_symbol_reader &reader,
427                        const char *name, CORE_ADDR address, int type,
428                        struct objfile *objfile)
429 {
430   enum minimal_symbol_type ms_type;
431   int section;
432
433   switch (type)
434     {
435     case N_TEXT | N_EXT:
436       ms_type = mst_text;
437       section = SECT_OFF_TEXT (objfile);
438       break;
439     case N_DATA | N_EXT:
440       ms_type = mst_data;
441       section = SECT_OFF_DATA (objfile);
442       break;
443     case N_BSS | N_EXT:
444       ms_type = mst_bss;
445       section = SECT_OFF_BSS (objfile);
446       break;
447     case N_ABS | N_EXT:
448       ms_type = mst_abs;
449       section = -1;
450       break;
451 #ifdef N_SETV
452     case N_SETV | N_EXT:
453       ms_type = mst_data;
454       section = SECT_OFF_DATA (objfile);
455       break;
456     case N_SETV:
457       /* I don't think this type actually exists; since a N_SETV is the result
458          of going over many .o files, it doesn't make sense to have one
459          file local.  */
460       ms_type = mst_file_data;
461       section = SECT_OFF_DATA (objfile);
462       break;
463 #endif
464     case N_TEXT:
465     case N_NBTEXT:
466     case N_FN:
467     case N_FN_SEQ:
468       ms_type = mst_file_text;
469       section = SECT_OFF_TEXT (objfile);
470       break;
471     case N_DATA:
472       ms_type = mst_file_data;
473
474       /* Check for __DYNAMIC, which is used by Sun shared libraries. 
475          Record it as global even if it's local, not global, so
476          lookup_minimal_symbol can find it.  We don't check symbol_leading_char
477          because for SunOS4 it always is '_'.  */
478       if (name[8] == 'C' && strcmp ("__DYNAMIC", name) == 0)
479         ms_type = mst_data;
480
481       /* Same with virtual function tables, both global and static.  */
482       {
483         const char *tempstring = name;
484
485         if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
486           ++tempstring;
487         if (is_vtable_name (tempstring))
488           ms_type = mst_data;
489       }
490       section = SECT_OFF_DATA (objfile);
491       break;
492     case N_BSS:
493       ms_type = mst_file_bss;
494       section = SECT_OFF_BSS (objfile);
495       break;
496     default:
497       ms_type = mst_unknown;
498       section = -1;
499       break;
500     }
501
502   if ((ms_type == mst_file_text || ms_type == mst_text)
503       && address < lowest_text_address)
504     lowest_text_address = address;
505
506   reader.record_with_info (name, address, ms_type, section);
507 }
508 \f
509 /* Scan and build partial symbols for a symbol file.
510    We have been initialized by a call to dbx_symfile_init, which 
511    put all the relevant info into a "struct dbx_symfile_info",
512    hung off the objfile structure.  */
513
514 static void
515 dbx_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
516 {
517   bfd *sym_bfd;
518   int val;
519
520   sym_bfd = objfile->obfd;
521
522   /* .o and .nlm files are relocatables with text, data and bss segs based at
523      0.  This flag disables special (Solaris stabs-in-elf only) fixups for
524      symbols with a value of 0.  */
525
526   symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
527
528   val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
529   if (val < 0)
530     perror_with_name (objfile_name (objfile));
531
532   /* Size the symbol table.  */
533   if (objfile->global_psymbols.capacity () == 0
534       && objfile->static_psymbols.capacity () == 0)
535     init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
536
537   symbol_size = DBX_SYMBOL_SIZE (objfile);
538   symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
539
540   free_pending_blocks ();
541   scoped_free_pendings free_pending;
542
543   minimal_symbol_reader reader (objfile);
544
545   /* Read stabs data from executable file and define symbols.  */
546
547   read_dbx_symtab (reader, objfile);
548
549   /* Install any minimal symbols that have been collected as the current
550      minimal symbols for this objfile.  */
551
552   reader.install ();
553 }
554
555 /* Initialize anything that needs initializing when a completely new
556    symbol file is specified (not just adding some symbols from another
557    file, e.g. a shared library).  */
558
559 static void
560 dbx_new_init (struct objfile *ignore)
561 {
562   stabsread_new_init ();
563   buildsym_new_init ();
564   init_header_files ();
565 }
566
567
568 /* dbx_symfile_init ()
569    is the dbx-specific initialization routine for reading symbols.
570    It is passed a struct objfile which contains, among other things,
571    the BFD for the file whose symbols are being read, and a slot for a pointer
572    to "private data" which we fill with goodies.
573
574    We read the string table into malloc'd space and stash a pointer to it.
575
576    Since BFD doesn't know how to read debug symbols in a format-independent
577    way (and may never do so...), we have to do it ourselves.  We will never
578    be called unless this is an a.out (or very similar) file.
579    FIXME, there should be a cleaner peephole into the BFD environment here.  */
580
581 #define DBX_STRINGTAB_SIZE_SIZE sizeof(long)    /* FIXME */
582
583 static void
584 dbx_symfile_init (struct objfile *objfile)
585 {
586   int val;
587   bfd *sym_bfd = objfile->obfd;
588   char *name = bfd_get_filename (sym_bfd);
589   asection *text_sect;
590   unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
591   struct dbx_symfile_info *dbx;
592
593   /* Allocate struct to keep track of the symfile.  */
594   dbx = XCNEW (struct dbx_symfile_info);
595   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
596
597   DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
598   DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
599   DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
600
601   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
602 #define STRING_TABLE_OFFSET     (sym_bfd->origin + obj_str_filepos (sym_bfd))
603 #define SYMBOL_TABLE_OFFSET     (sym_bfd->origin + obj_sym_filepos (sym_bfd))
604
605   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
606
607   text_sect = bfd_get_section_by_name (sym_bfd, ".text");
608   if (!text_sect)
609     error (_("Can't find .text section in symbol file"));
610   DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
611   DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
612
613   DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
614   DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
615   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
616
617   /* Read the string table and stash it away in the objfile_obstack.
618      When we blow away the objfile the string table goes away as well.
619      Note that gdb used to use the results of attempting to malloc the
620      string table, based on the size it read, as a form of sanity check
621      for botched byte swapping, on the theory that a byte swapped string
622      table size would be so totally bogus that the malloc would fail.  Now
623      that we put in on the objfile_obstack, we can't do this since gdb gets
624      a fatal error (out of virtual memory) if the size is bogus.  We can
625      however at least check to see if the size is less than the size of
626      the size field itself, or larger than the size of the entire file.
627      Note that all valid string tables have a size greater than zero, since
628      the bytes used to hold the size are included in the count.  */
629
630   if (STRING_TABLE_OFFSET == 0)
631     {
632       /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
633          will never be zero, even when there is no string table.  This
634          would appear to be a bug in bfd.  */
635       DBX_STRINGTAB_SIZE (objfile) = 0;
636       DBX_STRINGTAB (objfile) = NULL;
637     }
638   else
639     {
640       val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
641       if (val < 0)
642         perror_with_name (name);
643
644       memset (size_temp, 0, sizeof (size_temp));
645       val = bfd_bread (size_temp, sizeof (size_temp), sym_bfd);
646       if (val < 0)
647         {
648           perror_with_name (name);
649         }
650       else if (val == 0)
651         {
652           /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
653              EOF if there is no string table, and attempting to read the size
654              from EOF will read zero bytes.  */
655           DBX_STRINGTAB_SIZE (objfile) = 0;
656           DBX_STRINGTAB (objfile) = NULL;
657         }
658       else
659         {
660           /* Read some data that would appear to be the string table size.
661              If there really is a string table, then it is probably the right
662              size.  Byteswap if necessary and validate the size.  Note that
663              the minimum is DBX_STRINGTAB_SIZE_SIZE.  If we just read some
664              random data that happened to be at STRING_TABLE_OFFSET, because
665              bfd can't tell us there is no string table, the sanity checks may
666              or may not catch this.  */
667           DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
668
669           if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
670               || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
671             error (_("ridiculous string table size (%d bytes)."),
672                    DBX_STRINGTAB_SIZE (objfile));
673
674           DBX_STRINGTAB (objfile) =
675             (char *) obstack_alloc (&objfile->objfile_obstack,
676                                     DBX_STRINGTAB_SIZE (objfile));
677           OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
678
679           /* Now read in the string table in one big gulp.  */
680
681           val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
682           if (val < 0)
683             perror_with_name (name);
684           val = bfd_bread (DBX_STRINGTAB (objfile),
685                            DBX_STRINGTAB_SIZE (objfile),
686                            sym_bfd);
687           if (val != DBX_STRINGTAB_SIZE (objfile))
688             perror_with_name (name);
689         }
690     }
691 }
692
693 /* Perform any local cleanups required when we are done with a particular
694    objfile.  I.E, we are in the process of discarding all symbol information
695    for an objfile, freeing up all memory held for it, and unlinking the
696    objfile struct from the global list of known objfiles.  */
697
698 static void
699 dbx_symfile_finish (struct objfile *objfile)
700 {
701   free_header_files ();
702 }
703
704 static void
705 dbx_free_symfile_info (struct objfile *objfile, void *arg)
706 {
707   struct dbx_symfile_info *dbx = (struct dbx_symfile_info *) arg;
708
709   if (dbx->header_files != NULL)
710     {
711       int i = dbx->n_header_files;
712       struct header_file *hfiles = dbx->header_files;
713
714       while (--i >= 0)
715         {
716           xfree (hfiles[i].name);
717           xfree (hfiles[i].vector);
718         }
719       xfree (hfiles);
720     }
721
722   xfree (dbx);
723 }
724
725 \f
726
727 /* Buffer for reading the symbol table entries.  */
728 static struct external_nlist symbuf[4096];
729 static int symbuf_idx;
730 static int symbuf_end;
731
732 /* Name of last function encountered.  Used in Solaris to approximate
733    object file boundaries.  */
734 static const char *last_function_name;
735
736 /* The address in memory of the string table of the object file we are
737    reading (which might not be the "main" object file, but might be a
738    shared library or some other dynamically loaded thing).  This is
739    set by read_dbx_symtab when building psymtabs, and by
740    read_ofile_symtab when building symtabs, and is used only by
741    next_symbol_text.  FIXME: If that is true, we don't need it when
742    building psymtabs, right?  */
743 static char *stringtab_global;
744
745 /* These variables are used to control fill_symbuf when the stabs
746    symbols are not contiguous (as may be the case when a COFF file is
747    linked using --split-by-reloc).  */
748 static struct stab_section_list *symbuf_sections;
749 static unsigned int symbuf_left;
750 static unsigned int symbuf_read;
751
752 /* This variable stores a global stabs buffer, if we read stabs into
753    memory in one chunk in order to process relocations.  */
754 static bfd_byte *stabs_data;
755
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.  */
761
762 static void
763 fill_symbuf (bfd *sym_bfd)
764 {
765   unsigned int count;
766   int nbytes;
767
768   if (stabs_data)
769     {
770       nbytes = sizeof (symbuf);
771       if (nbytes > symbuf_left)
772         nbytes = symbuf_left;
773       memcpy (symbuf, stabs_data + symbuf_read, nbytes);
774     }
775   else if (symbuf_sections == NULL)
776     {
777       count = sizeof (symbuf);
778       nbytes = bfd_bread (symbuf, count, sym_bfd);
779     }
780   else
781     {
782       if (symbuf_left <= 0)
783         {
784           file_ptr filepos = symbuf_sections->section->filepos;
785
786           if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
787             perror_with_name (bfd_get_filename (sym_bfd));
788           symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
789           symbol_table_offset = filepos - symbuf_read;
790           symbuf_sections = symbuf_sections->next;
791         }
792
793       count = symbuf_left;
794       if (count > sizeof (symbuf))
795         count = sizeof (symbuf);
796       nbytes = bfd_bread (symbuf, count, sym_bfd);
797     }
798
799   if (nbytes < 0)
800     perror_with_name (bfd_get_filename (sym_bfd));
801   else if (nbytes == 0)
802     error (_("Premature end of file reading symbol table"));
803   symbuf_end = nbytes / symbol_size;
804   symbuf_idx = 0;
805   symbuf_left -= nbytes;
806   symbuf_read += nbytes;
807 }
808
809 static void
810 stabs_seek (int sym_offset)
811 {
812   if (stabs_data)
813     {
814       symbuf_read += sym_offset;
815       symbuf_left -= sym_offset;
816     }
817   else
818     bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
819 }
820
821 #define INTERNALIZE_SYMBOL(intern, extern, abfd)                        \
822   {                                                                     \
823     (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx);            \
824     (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type);             \
825     (intern).n_other = 0;                                               \
826     (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc);            \
827     if (bfd_get_sign_extend_vma (abfd))                                 \
828       (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value); \
829     else                                                                \
830       (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value);        \
831   }
832
833 /* Invariant: The symbol pointed to by symbuf_idx is the first one
834    that hasn't been swapped.  Swap the symbol at the same time
835    that symbuf_idx is incremented.  */
836
837 /* dbx allows the text of a symbol name to be continued into the
838    next symbol name!  When such a continuation is encountered
839    (a \ at the end of the text of a name)
840    call this function to get the continuation.  */
841
842 static const char *
843 dbx_next_symbol_text (struct objfile *objfile)
844 {
845   struct internal_nlist nlist;
846
847   if (symbuf_idx == symbuf_end)
848     fill_symbuf (symfile_bfd);
849
850   symnum++;
851   INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
852   OBJSTAT (objfile, n_stabs++);
853
854   symbuf_idx++;
855
856   return nlist.n_strx + stringtab_global + file_string_table_offset;
857 }
858 \f
859
860 /* Given a name, value pair, find the corresponding
861    bincl in the list.  Return the partial symtab associated
862    with that header_file_location.  */
863
864 static struct partial_symtab *
865 find_corresponding_bincl_psymtab (const char *name, int instance)
866 {
867   for (const header_file_location &bincl : *bincl_list)
868     if (bincl.instance == instance
869         && strcmp (name, bincl.name) == 0)
870       return bincl.pst;
871
872   repeated_header_complaint (name, symnum);
873   return (struct partial_symtab *) 0;
874 }
875
876 /* Set namestring based on nlist.  If the string table index is invalid, 
877    give a fake name, and print a single error message per symbol file read,
878    rather than abort the symbol reading or flood the user with messages.  */
879
880 static const char *
881 set_namestring (struct objfile *objfile, const struct internal_nlist *nlist)
882 {
883   const char *namestring;
884
885   if (nlist->n_strx + file_string_table_offset
886       >= DBX_STRINGTAB_SIZE (objfile)
887       || nlist->n_strx + file_string_table_offset < nlist->n_strx)
888     {
889       complaint (_("bad string table offset in symbol %d"),
890                  symnum);
891       namestring = "<bad string table offset>";
892     } 
893   else
894     namestring = (nlist->n_strx + file_string_table_offset
895                   + DBX_STRINGTAB (objfile));
896   return namestring;
897 }
898
899 static CORE_ADDR
900 find_stab_function_addr (const char *namestring, const char *filename,
901                          struct objfile *objfile)
902 {
903   struct bound_minimal_symbol msym;
904   int n;
905
906   const char *colon = strchr (namestring, ':');
907   if (colon == NULL)
908     n = 0;
909   else
910     n = colon - namestring;
911
912   char *p = (char *) alloca (n + 2);
913   strncpy (p, namestring, n);
914   p[n] = 0;
915
916   msym = lookup_minimal_symbol (p, filename, objfile);
917   if (msym.minsym == NULL)
918     {
919       /* Sun Fortran appends an underscore to the minimal symbol name,
920          try again with an appended underscore if the minimal symbol
921          was not found.  */
922       p[n] = '_';
923       p[n + 1] = 0;
924       msym = lookup_minimal_symbol (p, filename, objfile);
925     }
926
927   if (msym.minsym == NULL && filename != NULL)
928     {
929       /* Try again without the filename.  */
930       p[n] = 0;
931       msym = lookup_minimal_symbol (p, NULL, objfile);
932     }
933   if (msym.minsym == NULL && filename != NULL)
934     {
935       /* And try again for Sun Fortran, but without the filename.  */
936       p[n] = '_';
937       p[n + 1] = 0;
938       msym = lookup_minimal_symbol (p, NULL, objfile);
939     }
940
941   return msym.minsym == NULL ? 0 : BMSYMBOL_VALUE_ADDRESS (msym);
942 }
943
944 static void
945 function_outside_compilation_unit_complaint (const char *arg1)
946 {
947   complaint (_("function `%s' appears to be defined "
948                "outside of all compilation units"),
949              arg1);
950 }
951
952 /* Setup partial_symtab's describing each source file for which
953    debugging information is available.  */
954
955 static void
956 read_dbx_symtab (minimal_symbol_reader &reader, struct objfile *objfile)
957 {
958   struct gdbarch *gdbarch = get_objfile_arch (objfile);
959   struct external_nlist *bufp = 0;      /* =0 avoids gcc -Wall glitch.  */
960   struct internal_nlist nlist;
961   CORE_ADDR text_addr;
962   int text_size;
963   const char *sym_name;
964   int sym_len;
965
966   const char *namestring;
967   int nsl;
968   int past_first_source_file = 0;
969   CORE_ADDR last_function_start = 0;
970   bfd *abfd;
971   int textlow_not_set;
972   int data_sect_index;
973
974   /* Current partial symtab.  */
975   struct partial_symtab *pst;
976
977   /* List of current psymtab's include files.  */
978   const char **psymtab_include_list;
979   int includes_allocated;
980   int includes_used;
981
982   /* Index within current psymtab dependency list.  */
983   struct partial_symtab **dependency_list;
984   int dependencies_used, dependencies_allocated;
985
986   text_addr = DBX_TEXT_ADDR (objfile);
987   text_size = DBX_TEXT_SIZE (objfile);
988
989   /* FIXME.  We probably want to change stringtab_global rather than add this
990      while processing every symbol entry.  FIXME.  */
991   file_string_table_offset = 0;
992   next_file_string_table_offset = 0;
993
994   stringtab_global = DBX_STRINGTAB (objfile);
995
996   pst = (struct partial_symtab *) 0;
997
998   includes_allocated = 30;
999   includes_used = 0;
1000   psymtab_include_list = (const char **) alloca (includes_allocated *
1001                                                  sizeof (const char *));
1002
1003   dependencies_allocated = 30;
1004   dependencies_used = 0;
1005   dependency_list =
1006     (struct partial_symtab **) alloca (dependencies_allocated *
1007                                        sizeof (struct partial_symtab *));
1008
1009   /* Init bincl list */
1010   std::vector<struct header_file_location> bincl_storage;
1011   scoped_restore restore_bincl_global
1012     = make_scoped_restore (&bincl_list, &bincl_storage);
1013
1014   set_last_source_file (NULL);
1015
1016   lowest_text_address = (CORE_ADDR) -1;
1017
1018   symfile_bfd = objfile->obfd;  /* For next_text_symbol.  */
1019   abfd = objfile->obfd;
1020   symbuf_end = symbuf_idx = 0;
1021   next_symbol_text_func = dbx_next_symbol_text;
1022   textlow_not_set = 1;
1023   has_line_numbers = 0;
1024
1025   /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
1026      to global and static variables.  The stab for a global or static
1027      variable doesn't give us any indication of which section it's in,
1028      so we can't tell immediately which offset in
1029      objfile->section_offsets we should apply to the variable's
1030      address.
1031
1032      We could certainly find out which section contains the variable
1033      by looking up the variable's unrelocated address with
1034      find_pc_section, but that would be expensive; this is the
1035      function that constructs the partial symbol tables by examining
1036      every symbol in the entire executable, and it's
1037      performance-critical.  So that expense would not be welcome.  I'm
1038      not sure what to do about this at the moment.
1039
1040      What we have done for years is to simply assume that the .data
1041      section's offset is appropriate for all global and static
1042      variables.  Recently, this was expanded to fall back to the .bss
1043      section's offset if there is no .data section, and then to the
1044      .rodata section's offset.  */
1045   data_sect_index = objfile->sect_index_data;
1046   if (data_sect_index == -1)
1047     data_sect_index = SECT_OFF_BSS (objfile);
1048   if (data_sect_index == -1)
1049     data_sect_index = SECT_OFF_RODATA (objfile);
1050
1051   /* If data_sect_index is still -1, that's okay.  It's perfectly fine
1052      for the file to have no .data, no .bss, and no .text at all, if
1053      it also has no global or static variables.  If it does, we will
1054      get an internal error from an ANOFFSET macro below when we try to
1055      use data_sect_index.  */
1056
1057   for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1058     {
1059       /* Get the symbol for this run and pull out some info.  */
1060       QUIT;                     /* Allow this to be interruptable.  */
1061       if (symbuf_idx == symbuf_end)
1062         fill_symbuf (abfd);
1063       bufp = &symbuf[symbuf_idx++];
1064
1065       /*
1066        * Special case to speed up readin.
1067        */
1068       if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
1069         {
1070           has_line_numbers = 1;
1071           continue;
1072         }
1073
1074       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
1075       OBJSTAT (objfile, n_stabs++);
1076
1077       /* Ok.  There is a lot of code duplicated in the rest of this
1078          switch statement (for efficiency reasons).  Since I don't
1079          like duplicating code, I will do my penance here, and
1080          describe the code which is duplicated:
1081
1082          *) The assignment to namestring.
1083          *) The call to strchr.
1084          *) The addition of a partial symbol the two partial
1085          symbol lists.  This last is a large section of code, so
1086          I've imbedded it in the following macro.  */
1087
1088       switch (nlist.n_type)
1089         {
1090           /*
1091            * Standard, external, non-debugger, symbols
1092            */
1093
1094         case N_TEXT | N_EXT:
1095         case N_NBTEXT | N_EXT:
1096           goto record_it;
1097
1098         case N_DATA | N_EXT:
1099         case N_NBDATA | N_EXT:
1100           goto record_it;
1101
1102         case N_BSS:
1103         case N_BSS | N_EXT:
1104         case N_NBBSS | N_EXT:
1105         case N_SETV | N_EXT:            /* FIXME, is this in BSS? */
1106           goto record_it;
1107
1108         case N_ABS | N_EXT:
1109           record_it:
1110           namestring = set_namestring (objfile, &nlist);
1111
1112           record_minimal_symbol (reader, namestring, nlist.n_value,
1113                                  nlist.n_type, objfile);        /* Always */
1114           continue;
1115
1116           /* Standard, local, non-debugger, symbols.  */
1117
1118         case N_NBTEXT:
1119
1120           /* We need to be able to deal with both N_FN or N_TEXT,
1121              because we have no way of knowing whether the sys-supplied ld
1122              or GNU ld was used to make the executable.  Sequents throw
1123              in another wrinkle -- they renumbered N_FN.  */
1124
1125         case N_FN:
1126         case N_FN_SEQ:
1127         case N_TEXT:
1128           namestring = set_namestring (objfile, &nlist);
1129
1130           if ((namestring[0] == '-' && namestring[1] == 'l')
1131               || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
1132                   && namestring[nsl - 2] == '.'))
1133             {
1134               nlist.n_value += ANOFFSET (objfile->section_offsets,
1135                                          SECT_OFF_TEXT (objfile));
1136
1137               if (past_first_source_file && pst
1138                   /* The gould NP1 uses low values for .o and -l symbols
1139                      which are not the address.  */
1140                   && nlist.n_value >= pst->textlow)
1141                 {
1142                   dbx_end_psymtab (objfile, pst, psymtab_include_list,
1143                                    includes_used, symnum * symbol_size,
1144                                    nlist.n_value > pst->texthigh
1145                                    ? nlist.n_value : pst->texthigh,
1146                                    dependency_list, dependencies_used,
1147                                    textlow_not_set);
1148                   pst = (struct partial_symtab *) 0;
1149                   includes_used = 0;
1150                   dependencies_used = 0;
1151                   has_line_numbers = 0;
1152                 }
1153               else
1154                 past_first_source_file = 1;
1155             }
1156           else
1157             goto record_it;
1158           continue;
1159
1160         case N_DATA:
1161           goto record_it;
1162
1163         case N_UNDF | N_EXT:
1164           /* The case (nlist.n_value != 0) is a "Fortran COMMON" symbol.
1165              We used to rely on the target to tell us whether it knows
1166              where the symbol has been relocated to, but none of the
1167              target implementations actually provided that operation.
1168              So we just ignore the symbol, the same way we would do if
1169              we had a target-side symbol lookup which returned no match.
1170
1171              All other symbols (with nlist.n_value == 0), are really
1172              undefined, and so we ignore them too.  */
1173           continue;
1174
1175         case N_UNDF:
1176           if (processing_acc_compilation && nlist.n_strx == 1)
1177             {
1178               /* Deal with relative offsets in the string table
1179                  used in ELF+STAB under Solaris.  If we want to use the
1180                  n_strx field, which contains the name of the file,
1181                  we must adjust file_string_table_offset *before* calling
1182                  set_namestring().  */
1183               past_first_source_file = 1;
1184               file_string_table_offset = next_file_string_table_offset;
1185               next_file_string_table_offset =
1186                 file_string_table_offset + nlist.n_value;
1187               if (next_file_string_table_offset < file_string_table_offset)
1188                 error (_("string table offset backs up at %d"), symnum);
1189               /* FIXME -- replace error() with complaint.  */
1190               continue;
1191             }
1192           continue;
1193
1194           /* Lots of symbol types we can just ignore.  */
1195
1196         case N_ABS:
1197         case N_NBDATA:
1198         case N_NBBSS:
1199           continue;
1200
1201           /* Keep going . . .  */
1202
1203           /*
1204            * Special symbol types for GNU
1205            */
1206         case N_INDR:
1207         case N_INDR | N_EXT:
1208         case N_SETA:
1209         case N_SETA | N_EXT:
1210         case N_SETT:
1211         case N_SETT | N_EXT:
1212         case N_SETD:
1213         case N_SETD | N_EXT:
1214         case N_SETB:
1215         case N_SETB | N_EXT:
1216         case N_SETV:
1217           continue;
1218
1219           /*
1220            * Debugger symbols
1221            */
1222
1223         case N_SO:
1224           {
1225             CORE_ADDR valu;
1226             static int prev_so_symnum = -10;
1227             static int first_so_symnum;
1228             const char *p;
1229             static const char *dirname_nso;
1230             int prev_textlow_not_set;
1231
1232             valu = nlist.n_value + ANOFFSET (objfile->section_offsets,
1233                                              SECT_OFF_TEXT (objfile));
1234
1235             prev_textlow_not_set = textlow_not_set;
1236
1237             /* A zero value is probably an indication for the SunPRO 3.0
1238                compiler.  dbx_end_psymtab explicitly tests for zero, so
1239                don't relocate it.  */
1240
1241             if (nlist.n_value == 0
1242                 && gdbarch_sofun_address_maybe_missing (gdbarch))
1243               {
1244                 textlow_not_set = 1;
1245                 valu = 0;
1246               }
1247             else
1248               textlow_not_set = 0;
1249
1250             past_first_source_file = 1;
1251
1252             if (prev_so_symnum != symnum - 1)
1253               {                 /* Here if prev stab wasn't N_SO.  */
1254                 first_so_symnum = symnum;
1255
1256                 if (pst)
1257                   {
1258                     dbx_end_psymtab (objfile, pst, psymtab_include_list,
1259                                      includes_used, symnum * symbol_size,
1260                                      valu > pst->texthigh
1261                                      ? valu : pst->texthigh,
1262                                      dependency_list, dependencies_used,
1263                                      prev_textlow_not_set);
1264                     pst = (struct partial_symtab *) 0;
1265                     includes_used = 0;
1266                     dependencies_used = 0;
1267                     has_line_numbers = 0;
1268                   }
1269               }
1270
1271             prev_so_symnum = symnum;
1272
1273             /* End the current partial symtab and start a new one.  */
1274
1275             namestring = set_namestring (objfile, &nlist);
1276
1277             /* Null name means end of .o file.  Don't start a new one.  */
1278             if (*namestring == '\000')
1279               continue;
1280
1281             /* Some compilers (including gcc) emit a pair of initial N_SOs.
1282                The first one is a directory name; the second the file name.
1283                If pst exists, is empty, and has a filename ending in '/',
1284                we assume the previous N_SO was a directory name.  */
1285
1286             p = lbasename (namestring);
1287             if (p != namestring && *p == '\000')
1288               {
1289                 /* Save the directory name SOs locally, then save it into
1290                    the psymtab when it's created below.  */
1291                 dirname_nso = namestring;
1292                 continue;               
1293               }
1294
1295             /* Some other compilers (C++ ones in particular) emit useless
1296                SOs for non-existant .c files.  We ignore all subsequent SOs
1297                that immediately follow the first.  */
1298
1299             if (!pst)
1300               {
1301                 pst = start_psymtab (objfile,
1302                                      namestring, valu,
1303                                      first_so_symnum * symbol_size,
1304                                      objfile->global_psymbols,
1305                                      objfile->static_psymbols);
1306                 pst->dirname = dirname_nso;
1307                 dirname_nso = NULL;
1308               }
1309             continue;
1310           }
1311
1312         case N_BINCL:
1313           {
1314             enum language tmp_language;
1315
1316             /* Add this bincl to the bincl_list for future EXCLs.  No
1317                need to save the string; it'll be around until
1318                read_dbx_symtab function returns.  */
1319
1320             namestring = set_namestring (objfile, &nlist);
1321             tmp_language = deduce_language_from_filename (namestring);
1322
1323             /* Only change the psymtab's language if we've learned
1324                something useful (eg. tmp_language is not language_unknown).
1325                In addition, to match what start_subfile does, never change
1326                from C++ to C.  */
1327             if (tmp_language != language_unknown
1328                 && (tmp_language != language_c
1329                     || psymtab_language != language_cplus))
1330               psymtab_language = tmp_language;
1331
1332             if (pst == NULL)
1333               {
1334                 /* FIXME: we should not get here without a PST to work on.
1335                    Attempt to recover.  */
1336                 complaint (_("N_BINCL %s not in entries for "
1337                              "any file, at symtab pos %d"),
1338                            namestring, symnum);
1339                 continue;
1340               }
1341             bincl_list->emplace_back (namestring, nlist.n_value, pst);
1342
1343             /* Mark down an include file in the current psymtab.  */
1344
1345             goto record_include_file;
1346           }
1347
1348         case N_SOL:
1349           {
1350             enum language tmp_language;
1351
1352             /* Mark down an include file in the current psymtab.  */
1353             namestring = set_namestring (objfile, &nlist);
1354             tmp_language = deduce_language_from_filename (namestring);
1355
1356             /* Only change the psymtab's language if we've learned
1357                something useful (eg. tmp_language is not language_unknown).
1358                In addition, to match what start_subfile does, never change
1359                from C++ to C.  */
1360             if (tmp_language != language_unknown
1361                 && (tmp_language != language_c
1362                     || psymtab_language != language_cplus))
1363               psymtab_language = tmp_language;
1364
1365             /* In C++, one may expect the same filename to come round many
1366                times, when code is coming alternately from the main file
1367                and from inline functions in other files.  So I check to see
1368                if this is a file we've seen before -- either the main
1369                source file, or a previously included file.
1370
1371                This seems to be a lot of time to be spending on N_SOL, but
1372                things like "break c-exp.y:435" need to work (I
1373                suppose the psymtab_include_list could be hashed or put
1374                in a binary tree, if profiling shows this is a major hog).  */
1375             if (pst && filename_cmp (namestring, pst->filename) == 0)
1376               continue;
1377             {
1378               int i;
1379
1380               for (i = 0; i < includes_used; i++)
1381                 if (filename_cmp (namestring, psymtab_include_list[i]) == 0)
1382                   {
1383                     i = -1;
1384                     break;
1385                   }
1386               if (i == -1)
1387                 continue;
1388             }
1389
1390           record_include_file:
1391
1392             psymtab_include_list[includes_used++] = namestring;
1393             if (includes_used >= includes_allocated)
1394               {
1395                 const char **orig = psymtab_include_list;
1396
1397                 psymtab_include_list = (const char **)
1398                   alloca ((includes_allocated *= 2) * sizeof (const char *));
1399                 memcpy (psymtab_include_list, orig,
1400                         includes_used * sizeof (const char *));
1401               }
1402             continue;
1403           }
1404         case N_LSYM:            /* Typedef or automatic variable.  */
1405         case N_STSYM:           /* Data seg var -- static.  */
1406         case N_LCSYM:           /* BSS      "  */
1407         case N_ROSYM:           /* Read-only data seg var -- static.  */
1408         case N_NBSTS:           /* Gould nobase.  */
1409         case N_NBLCS:           /* symbols.  */
1410         case N_FUN:
1411         case N_GSYM:            /* Global (extern) variable; can be
1412                                    data or bss (sigh FIXME).  */
1413
1414           /* Following may probably be ignored; I'll leave them here
1415              for now (until I do Pascal and Modula 2 extensions).  */
1416
1417         case N_PC:              /* I may or may not need this; I
1418                                    suspect not.  */
1419         case N_M2C:             /* I suspect that I can ignore this here.  */
1420         case N_SCOPE:           /* Same.   */
1421         {
1422           const char *p;
1423
1424           namestring = set_namestring (objfile, &nlist);
1425
1426           /* See if this is an end of function stab.  */
1427           if (pst && nlist.n_type == N_FUN && *namestring == '\000')
1428             {
1429               CORE_ADDR valu;
1430
1431               /* It's value is the size (in bytes) of the function for
1432                  function relative stabs, or the address of the function's
1433                  end for old style stabs.  */
1434               valu = nlist.n_value + last_function_start;
1435               if (pst->texthigh == 0 || valu > pst->texthigh)
1436                 pst->texthigh = valu;
1437               break;
1438             }
1439
1440           p = (char *) strchr (namestring, ':');
1441           if (!p)
1442             continue;           /* Not a debugging symbol.   */
1443
1444           sym_len = 0;
1445           sym_name = NULL;      /* pacify "gcc -Werror" */
1446           if (psymtab_language == language_cplus)
1447             {
1448               std::string name (namestring, p - namestring);
1449               std::string new_name = cp_canonicalize_string (name.c_str ());
1450               if (!new_name.empty ())
1451                 {
1452                   sym_len = new_name.length ();
1453                   sym_name = (char *) obstack_copy0 (&objfile->objfile_obstack,
1454                                                      new_name.c_str (),
1455                                                      sym_len);
1456                 }
1457             }
1458
1459           if (sym_len == 0)
1460             {
1461               sym_name = namestring;
1462               sym_len = p - namestring;
1463             }
1464
1465           /* Main processing section for debugging symbols which
1466              the initial read through the symbol tables needs to worry
1467              about.  If we reach this point, the symbol which we are
1468              considering is definitely one we are interested in.
1469              p must also contain the (valid) index into the namestring
1470              which indicates the debugging type symbol.  */
1471
1472           switch (p[1])
1473             {
1474             case 'S':
1475               nlist.n_value += ANOFFSET (objfile->section_offsets,
1476                                          data_sect_index);
1477
1478               if (gdbarch_static_transform_name_p (gdbarch))
1479                 gdbarch_static_transform_name (gdbarch, namestring);
1480
1481               add_psymbol_to_list (sym_name, sym_len, 1,
1482                                    VAR_DOMAIN, LOC_STATIC,
1483                                    &objfile->static_psymbols,
1484                                    nlist.n_value, psymtab_language, objfile);
1485               continue;
1486
1487             case 'G':
1488               nlist.n_value += ANOFFSET (objfile->section_offsets,
1489                                          data_sect_index);
1490               /* The addresses in these entries are reported to be
1491                  wrong.  See the code that reads 'G's for symtabs.  */
1492               add_psymbol_to_list (sym_name, sym_len, 1,
1493                                    VAR_DOMAIN, LOC_STATIC,
1494                                    &objfile->global_psymbols,
1495                                    nlist.n_value, psymtab_language, objfile);
1496               continue;
1497
1498             case 'T':
1499               /* When a 'T' entry is defining an anonymous enum, it
1500                  may have a name which is the empty string, or a
1501                  single space.  Since they're not really defining a
1502                  symbol, those shouldn't go in the partial symbol
1503                  table.  We do pick up the elements of such enums at
1504                  'check_enum:', below.  */
1505               if (p >= namestring + 2
1506                   || (p == namestring + 1
1507                       && namestring[0] != ' '))
1508                 {
1509                   add_psymbol_to_list (sym_name, sym_len, 1,
1510                                        STRUCT_DOMAIN, LOC_TYPEDEF,
1511                                        &objfile->static_psymbols,
1512                                        0, psymtab_language, objfile);
1513                   if (p[2] == 't')
1514                     {
1515                       /* Also a typedef with the same name.  */
1516                       add_psymbol_to_list (sym_name, sym_len, 1,
1517                                            VAR_DOMAIN, LOC_TYPEDEF,
1518                                            &objfile->static_psymbols,
1519                                            0, psymtab_language, objfile);
1520                       p += 1;
1521                     }
1522                 }
1523               goto check_enum;
1524
1525             case 't':
1526               if (p != namestring)      /* a name is there, not just :T...  */
1527                 {
1528                   add_psymbol_to_list (sym_name, sym_len, 1,
1529                                        VAR_DOMAIN, LOC_TYPEDEF,
1530                                        &objfile->static_psymbols,
1531                                        0, psymtab_language, objfile);
1532                 }
1533             check_enum:
1534               /* If this is an enumerated type, we need to
1535                  add all the enum constants to the partial symbol
1536                  table.  This does not cover enums without names, e.g.
1537                  "enum {a, b} c;" in C, but fortunately those are
1538                  rare.  There is no way for GDB to find those from the
1539                  enum type without spending too much time on it.  Thus
1540                  to solve this problem, the compiler needs to put out the
1541                  enum in a nameless type.  GCC2 does this.  */
1542
1543               /* We are looking for something of the form
1544                  <name> ":" ("t" | "T") [<number> "="] "e"
1545                  {<constant> ":" <value> ","} ";".  */
1546
1547               /* Skip over the colon and the 't' or 'T'.  */
1548               p += 2;
1549               /* This type may be given a number.  Also, numbers can come
1550                  in pairs like (0,26).  Skip over it.  */
1551               while ((*p >= '0' && *p <= '9')
1552                      || *p == '(' || *p == ',' || *p == ')'
1553                      || *p == '=')
1554                 p++;
1555
1556               if (*p++ == 'e')
1557                 {
1558                   /* The aix4 compiler emits extra crud before the members.  */
1559                   if (*p == '-')
1560                     {
1561                       /* Skip over the type (?).  */
1562                       while (*p != ':')
1563                         p++;
1564
1565                       /* Skip over the colon.  */
1566                       p++;
1567                     }
1568
1569                   /* We have found an enumerated type.  */
1570                   /* According to comments in read_enum_type
1571                      a comma could end it instead of a semicolon.
1572                      I don't know where that happens.
1573                      Accept either.  */
1574                   while (*p && *p != ';' && *p != ',')
1575                     {
1576                       const char *q;
1577
1578                       /* Check for and handle cretinous dbx symbol name
1579                          continuation!  */
1580                       if (*p == '\\' || (*p == '?' && p[1] == '\0'))
1581                         p = next_symbol_text (objfile);
1582
1583                       /* Point to the character after the name
1584                          of the enum constant.  */
1585                       for (q = p; *q && *q != ':'; q++)
1586                         ;
1587                       /* Note that the value doesn't matter for
1588                          enum constants in psymtabs, just in symtabs.  */
1589                       add_psymbol_to_list (p, q - p, 1,
1590                                            VAR_DOMAIN, LOC_CONST,
1591                                            &objfile->static_psymbols, 0,
1592                                            psymtab_language, objfile);
1593                       /* Point past the name.  */
1594                       p = q;
1595                       /* Skip over the value.  */
1596                       while (*p && *p != ',')
1597                         p++;
1598                       /* Advance past the comma.  */
1599                       if (*p)
1600                         p++;
1601                     }
1602                 }
1603               continue;
1604
1605             case 'c':
1606               /* Constant, e.g. from "const" in Pascal.  */
1607               add_psymbol_to_list (sym_name, sym_len, 1,
1608                                    VAR_DOMAIN, LOC_CONST,
1609                                    &objfile->static_psymbols, 0,
1610                                    psymtab_language, objfile);
1611               continue;
1612
1613             case 'f':
1614               if (! pst)
1615                 {
1616                   int name_len = p - namestring;
1617                   char *name = (char *) xmalloc (name_len + 1);
1618
1619                   memcpy (name, namestring, name_len);
1620                   name[name_len] = '\0';
1621                   function_outside_compilation_unit_complaint (name);
1622                   xfree (name);
1623                 }
1624               nlist.n_value += ANOFFSET (objfile->section_offsets, 
1625                                          SECT_OFF_TEXT (objfile));
1626               /* Kludges for ELF/STABS with Sun ACC.  */
1627               last_function_name = namestring;
1628               /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1629                  value for the bottom of the text seg in those cases.  */
1630               if (nlist.n_value == ANOFFSET (objfile->section_offsets, 
1631                                              SECT_OFF_TEXT (objfile))
1632                   && gdbarch_sofun_address_maybe_missing (gdbarch))
1633                 {
1634                   CORE_ADDR minsym_valu = 
1635                     find_stab_function_addr (namestring, 
1636                                              pst ? pst->filename : NULL, 
1637                                              objfile);
1638
1639                   /* find_stab_function_addr will return 0 if the minimal
1640                      symbol wasn't found.  (Unfortunately, this might also
1641                      be a valid address.)  Anyway, if it *does* return 0,
1642                      it is likely that the value was set correctly to begin
1643                      with...  */
1644                   if (minsym_valu != 0)
1645                     nlist.n_value = minsym_valu;
1646                 }
1647               if (pst && textlow_not_set
1648                   && gdbarch_sofun_address_maybe_missing (gdbarch))
1649                 {
1650                   pst->textlow = nlist.n_value;
1651                   textlow_not_set = 0;
1652                 }
1653               /* End kludge.  */
1654
1655               /* Keep track of the start of the last function so we
1656                  can handle end of function symbols.  */
1657               last_function_start = nlist.n_value;
1658
1659               /* In reordered executables this function may lie outside
1660                  the bounds created by N_SO symbols.  If that's the case
1661                  use the address of this function as the low bound for
1662                  the partial symbol table.  */
1663               if (pst
1664                   && (textlow_not_set
1665                       || (nlist.n_value < pst->textlow
1666                           && (nlist.n_value
1667                               != ANOFFSET (objfile->section_offsets,
1668                                            SECT_OFF_TEXT (objfile))))))
1669                 {
1670                   pst->textlow = nlist.n_value;
1671                   textlow_not_set = 0;
1672                 }
1673               add_psymbol_to_list (sym_name, sym_len, 1,
1674                                    VAR_DOMAIN, LOC_BLOCK,
1675                                    &objfile->static_psymbols,
1676                                    nlist.n_value, psymtab_language, objfile);
1677               continue;
1678
1679               /* Global functions were ignored here, but now they
1680                  are put into the global psymtab like one would expect.
1681                  They're also in the minimal symbol table.  */
1682             case 'F':
1683               if (! pst)
1684                 {
1685                   int name_len = p - namestring;
1686                   char *name = (char *) xmalloc (name_len + 1);
1687
1688                   memcpy (name, namestring, name_len);
1689                   name[name_len] = '\0';
1690                   function_outside_compilation_unit_complaint (name);
1691                   xfree (name);
1692                 }
1693               nlist.n_value += ANOFFSET (objfile->section_offsets, 
1694                                          SECT_OFF_TEXT (objfile));
1695               /* Kludges for ELF/STABS with Sun ACC.  */
1696               last_function_name = namestring;
1697               /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
1698                  value for the bottom of the text seg in those cases.  */
1699               if (nlist.n_value == ANOFFSET (objfile->section_offsets, 
1700                                              SECT_OFF_TEXT (objfile))
1701                   && gdbarch_sofun_address_maybe_missing (gdbarch))
1702                 {
1703                   CORE_ADDR minsym_valu = 
1704                     find_stab_function_addr (namestring, 
1705                                              pst ? pst->filename : NULL, 
1706                                              objfile);
1707
1708                   /* find_stab_function_addr will return 0 if the minimal
1709                      symbol wasn't found.  (Unfortunately, this might also
1710                      be a valid address.)  Anyway, if it *does* return 0,
1711                      it is likely that the value was set correctly to begin
1712                      with...  */
1713                   if (minsym_valu != 0)
1714                     nlist.n_value = minsym_valu;
1715                 }
1716               if (pst && textlow_not_set
1717                   && gdbarch_sofun_address_maybe_missing (gdbarch))
1718                 {
1719                   pst->textlow = nlist.n_value;
1720                   textlow_not_set = 0;
1721                 }
1722               /* End kludge.  */
1723
1724               /* Keep track of the start of the last function so we
1725                  can handle end of function symbols.  */
1726               last_function_start = nlist.n_value;
1727
1728               /* In reordered executables this function may lie outside
1729                  the bounds created by N_SO symbols.  If that's the case
1730                  use the address of this function as the low bound for
1731                  the partial symbol table.  */
1732               if (pst
1733                   && (textlow_not_set
1734                       || (nlist.n_value < pst->textlow
1735                           && (nlist.n_value
1736                               != ANOFFSET (objfile->section_offsets,
1737                                            SECT_OFF_TEXT (objfile))))))
1738                 {
1739                   pst->textlow = nlist.n_value;
1740                   textlow_not_set = 0;
1741                 }
1742               add_psymbol_to_list (sym_name, sym_len, 1,
1743                                    VAR_DOMAIN, LOC_BLOCK,
1744                                    &objfile->global_psymbols,
1745                                    nlist.n_value, psymtab_language, objfile);
1746               continue;
1747
1748               /* Two things show up here (hopefully); static symbols of
1749                  local scope (static used inside braces) or extensions
1750                  of structure symbols.  We can ignore both.  */
1751             case 'V':
1752             case '(':
1753             case '0':
1754             case '1':
1755             case '2':
1756             case '3':
1757             case '4':
1758             case '5':
1759             case '6':
1760             case '7':
1761             case '8':
1762             case '9':
1763             case '-':
1764             case '#':   /* For symbol identification (used in live ranges).  */
1765               continue;
1766
1767             case ':':
1768               /* It is a C++ nested symbol.  We don't need to record it
1769                  (I don't think); if we try to look up foo::bar::baz,
1770                  then symbols for the symtab containing foo should get
1771                  read in, I think.  */
1772               /* Someone says sun cc puts out symbols like
1773                  /foo/baz/maclib::/usr/local/bin/maclib,
1774                  which would get here with a symbol type of ':'.  */
1775               continue;
1776
1777             default:
1778               /* Unexpected symbol descriptor.  The second and subsequent stabs
1779                  of a continued stab can show up here.  The question is
1780                  whether they ever can mimic a normal stab--it would be
1781                  nice if not, since we certainly don't want to spend the
1782                  time searching to the end of every string looking for
1783                  a backslash.  */
1784
1785               complaint (_("unknown symbol descriptor `%c'"),
1786                          p[1]);
1787
1788               /* Ignore it; perhaps it is an extension that we don't
1789                  know about.  */
1790               continue;
1791             }
1792         }
1793
1794         case N_EXCL:
1795
1796           namestring = set_namestring (objfile, &nlist);
1797
1798           /* Find the corresponding bincl and mark that psymtab on the
1799              psymtab dependency list.  */
1800           {
1801             struct partial_symtab *needed_pst =
1802               find_corresponding_bincl_psymtab (namestring, nlist.n_value);
1803
1804             /* If this include file was defined earlier in this file,
1805                leave it alone.  */
1806             if (needed_pst == pst)
1807               continue;
1808
1809             if (needed_pst)
1810               {
1811                 int i;
1812                 int found = 0;
1813
1814                 for (i = 0; i < dependencies_used; i++)
1815                   if (dependency_list[i] == needed_pst)
1816                     {
1817                       found = 1;
1818                       break;
1819                     }
1820
1821                 /* If it's already in the list, skip the rest.  */
1822                 if (found)
1823                   continue;
1824
1825                 dependency_list[dependencies_used++] = needed_pst;
1826                 if (dependencies_used >= dependencies_allocated)
1827                   {
1828                     struct partial_symtab **orig = dependency_list;
1829
1830                     dependency_list =
1831                       (struct partial_symtab **)
1832                       alloca ((dependencies_allocated *= 2)
1833                               * sizeof (struct partial_symtab *));
1834                     memcpy (dependency_list, orig,
1835                             (dependencies_used
1836                              * sizeof (struct partial_symtab *)));
1837 #ifdef DEBUG_INFO
1838                     fprintf_unfiltered (gdb_stderr,
1839                                         "Had to reallocate "
1840                                         "dependency list.\n");
1841                     fprintf_unfiltered (gdb_stderr,
1842                                         "New dependencies allocated: %d\n",
1843                                         dependencies_allocated);
1844 #endif
1845                   }
1846               }
1847           }
1848           continue;
1849
1850         case N_ENDM:
1851           /* Solaris 2 end of module, finish current partial symbol table.
1852              dbx_end_psymtab will set pst->texthigh to the proper value, which
1853              is necessary if a module compiled without debugging info
1854              follows this module.  */
1855           if (pst && gdbarch_sofun_address_maybe_missing (gdbarch))
1856             {
1857               dbx_end_psymtab (objfile, pst,
1858                                psymtab_include_list, includes_used,
1859                                symnum * symbol_size,
1860                                (CORE_ADDR) 0, dependency_list,
1861                                dependencies_used, textlow_not_set);
1862               pst = (struct partial_symtab *) 0;
1863               includes_used = 0;
1864               dependencies_used = 0;
1865               has_line_numbers = 0;
1866             }
1867           continue;
1868
1869         case N_RBRAC:
1870 #ifdef HANDLE_RBRAC
1871           HANDLE_RBRAC (nlist.n_value);
1872           continue;
1873 #endif
1874         case N_EINCL:
1875         case N_DSLINE:
1876         case N_BSLINE:
1877         case N_SSYM:            /* Claim: Structure or union element.
1878                                    Hopefully, I can ignore this.  */
1879         case N_ENTRY:           /* Alternate entry point; can ignore.  */
1880         case N_MAIN:            /* Can definitely ignore this.   */
1881         case N_CATCH:           /* These are GNU C++ extensions */
1882         case N_EHDECL:          /* that can safely be ignored here.  */
1883         case N_LENG:
1884         case N_BCOMM:
1885         case N_ECOMM:
1886         case N_ECOML:
1887         case N_FNAME:
1888         case N_SLINE:
1889         case N_RSYM:
1890         case N_PSYM:
1891         case N_BNSYM:
1892         case N_ENSYM:
1893         case N_LBRAC:
1894         case N_NSYMS:           /* Ultrix 4.0: symbol count */
1895         case N_DEFD:            /* GNU Modula-2 */
1896         case N_ALIAS:           /* SunPro F77: alias name, ignore for now.  */
1897
1898         case N_OBJ:             /* Useless types from Solaris.  */
1899         case N_OPT:
1900         case N_PATCH:
1901           /* These symbols aren't interesting; don't worry about them.  */
1902           continue;
1903
1904         default:
1905           /* If we haven't found it yet, ignore it.  It's probably some
1906              new type we don't know about yet.  */
1907           unknown_symtype_complaint (hex_string (nlist.n_type));
1908           continue;
1909         }
1910     }
1911
1912   /* If there's stuff to be cleaned up, clean it up.  */
1913   if (pst)
1914     {
1915       /* Don't set pst->texthigh lower than it already is.  */
1916       CORE_ADDR text_end =
1917         (lowest_text_address == (CORE_ADDR) -1
1918          ? (text_addr + ANOFFSET (objfile->section_offsets,
1919                                   SECT_OFF_TEXT (objfile)))
1920          : lowest_text_address)
1921         + text_size;
1922
1923       dbx_end_psymtab (objfile, pst, psymtab_include_list, includes_used,
1924                        symnum * symbol_size,
1925                        text_end > pst->texthigh ? text_end : pst->texthigh,
1926                        dependency_list, dependencies_used, textlow_not_set);
1927     }
1928 }
1929
1930 /* Allocate and partially fill a partial symtab.  It will be
1931    completely filled at the end of the symbol list.
1932
1933    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1934    is the address relative to which its symbols are (incremental) or 0
1935    (normal).  */
1936
1937 static struct partial_symtab *
1938 start_psymtab (struct objfile *objfile, const char *filename, CORE_ADDR textlow,
1939                int ldsymoff, std::vector<partial_symbol *> &global_psymbols,
1940                std::vector<partial_symbol *> &static_psymbols)
1941 {
1942   struct partial_symtab *result =
1943     start_psymtab_common (objfile, filename, textlow,
1944                           global_psymbols, static_psymbols);
1945
1946   result->read_symtab_private =
1947     XOBNEW (&objfile->objfile_obstack, struct symloc);
1948   LDSYMOFF (result) = ldsymoff;
1949   result->read_symtab = dbx_read_symtab;
1950   SYMBOL_SIZE (result) = symbol_size;
1951   SYMBOL_OFFSET (result) = symbol_table_offset;
1952   STRING_OFFSET (result) = string_table_offset;
1953   FILE_STRING_OFFSET (result) = file_string_table_offset;
1954
1955   /* Deduce the source language from the filename for this psymtab.  */
1956   psymtab_language = deduce_language_from_filename (filename);
1957   PST_LANGUAGE (result) = psymtab_language;
1958
1959   return result;
1960 }
1961
1962 /* Close off the current usage of PST.
1963    Returns PST or NULL if the partial symtab was empty and thrown away.
1964
1965    FIXME:  List variables and peculiarities of same.  */
1966
1967 struct partial_symtab *
1968 dbx_end_psymtab (struct objfile *objfile, struct partial_symtab *pst,
1969                  const char **include_list, int num_includes,
1970                  int capping_symbol_offset, CORE_ADDR capping_text,
1971                  struct partial_symtab **dependency_list,
1972                  int number_dependencies,
1973                  int textlow_not_set)
1974 {
1975   int i;
1976   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1977
1978   if (capping_symbol_offset != -1)
1979     LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
1980   pst->texthigh = capping_text;
1981
1982   /* Under Solaris, the N_SO symbols always have a value of 0,
1983      instead of the usual address of the .o file.  Therefore,
1984      we have to do some tricks to fill in texthigh and textlow.
1985      The first trick is: if we see a static
1986      or global function, and the textlow for the current pst
1987      is not set (ie: textlow_not_set), then we use that function's
1988      address for the textlow of the pst.  */
1989
1990   /* Now, to fill in texthigh, we remember the last function seen
1991      in the .o file.  Also, there's a hack in
1992      bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
1993      to here via the misc_info field.  Therefore, we can fill in
1994      a reliable texthigh by taking the address plus size of the
1995      last function in the file.  */
1996
1997   if (pst->texthigh == 0 && last_function_name
1998       && gdbarch_sofun_address_maybe_missing (gdbarch))
1999     {
2000       int n;
2001       struct bound_minimal_symbol minsym;
2002
2003       const char *colon = strchr (last_function_name, ':');
2004       if (colon == NULL)
2005         n = 0;
2006       else
2007         n = colon - last_function_name;
2008       char *p = (char *) alloca (n + 2);
2009       strncpy (p, last_function_name, n);
2010       p[n] = 0;
2011
2012       minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2013       if (minsym.minsym == NULL)
2014         {
2015           /* Sun Fortran appends an underscore to the minimal symbol name,
2016              try again with an appended underscore if the minimal symbol
2017              was not found.  */
2018           p[n] = '_';
2019           p[n + 1] = 0;
2020           minsym = lookup_minimal_symbol (p, pst->filename, objfile);
2021         }
2022
2023       if (minsym.minsym)
2024         pst->texthigh = (BMSYMBOL_VALUE_ADDRESS (minsym)
2025                          + MSYMBOL_SIZE (minsym.minsym));
2026
2027       last_function_name = NULL;
2028     }
2029
2030   if (!gdbarch_sofun_address_maybe_missing (gdbarch))
2031     ;
2032   /* This test will be true if the last .o file is only data.  */
2033   else if (textlow_not_set)
2034     pst->textlow = pst->texthigh;
2035   else
2036     {
2037       struct partial_symtab *p1;
2038
2039       /* If we know our own starting text address, then walk through all other
2040          psymtabs for this objfile, and if any didn't know their ending text
2041          address, set it to our starting address.  Take care to not set our
2042          own ending address to our starting address, nor to set addresses on
2043          `dependency' files that have both textlow and texthigh zero.  */
2044
2045       ALL_OBJFILE_PSYMTABS (objfile, p1)
2046       {
2047         if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
2048           {
2049             p1->texthigh = pst->textlow;
2050             /* If this file has only data, then make textlow match
2051                texthigh.  */
2052             if (p1->textlow == 0)
2053               p1->textlow = p1->texthigh;
2054           }
2055       }
2056     }
2057
2058   /* End of kludge for patching Solaris textlow and texthigh.  */
2059
2060   end_psymtab_common (objfile, pst);
2061
2062   pst->number_of_dependencies = number_dependencies;
2063   if (number_dependencies)
2064     {
2065       pst->dependencies = XOBNEWVEC (&objfile->objfile_obstack,
2066                                      struct partial_symtab *,
2067                                      number_dependencies);
2068       memcpy (pst->dependencies, dependency_list,
2069               number_dependencies * sizeof (struct partial_symtab *));
2070     }
2071   else
2072     pst->dependencies = 0;
2073
2074   for (i = 0; i < num_includes; i++)
2075     {
2076       struct partial_symtab *subpst =
2077         allocate_psymtab (include_list[i], objfile);
2078
2079       subpst->read_symtab_private =
2080         XOBNEW (&objfile->objfile_obstack, struct symloc);
2081       LDSYMOFF (subpst) =
2082         LDSYMLEN (subpst) =
2083         subpst->textlow =
2084         subpst->texthigh = 0;
2085
2086       /* We could save slight bits of space by only making one of these,
2087          shared by the entire set of include files.  FIXME-someday.  */
2088       subpst->dependencies =
2089         XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
2090       subpst->dependencies[0] = pst;
2091       subpst->number_of_dependencies = 1;
2092
2093       subpst->globals_offset =
2094         subpst->n_global_syms =
2095         subpst->statics_offset =
2096         subpst->n_static_syms = 0;
2097
2098       subpst->readin = 0;
2099       subpst->compunit_symtab = 0;
2100       subpst->read_symtab = pst->read_symtab;
2101     }
2102
2103   if (num_includes == 0
2104       && number_dependencies == 0
2105       && pst->n_global_syms == 0
2106       && pst->n_static_syms == 0
2107       && has_line_numbers == 0)
2108     {
2109       /* Throw away this psymtab, it's empty.  We can't deallocate it, since
2110          it is on the obstack, but we can forget to chain it on the list.  */
2111       /* Empty psymtabs happen as a result of header files which don't have
2112          any symbols in them.  There can be a lot of them.  But this check
2113          is wrong, in that a psymtab with N_SLINE entries but nothing else
2114          is not empty, but we don't realize that.  Fixing that without slowing
2115          things down might be tricky.  */
2116
2117       discard_psymtab (objfile, pst);
2118
2119       /* Indicate that psymtab was thrown away.  */
2120       pst = NULL;
2121     }
2122   return pst;
2123 }
2124 \f
2125 static void
2126 dbx_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
2127 {
2128   int i;
2129
2130   if (pst->readin)
2131     {
2132       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  "
2133                           "Shouldn't happen.\n",
2134                           pst->filename);
2135       return;
2136     }
2137
2138   /* Read in all partial symtabs on which this one is dependent.  */
2139   for (i = 0; i < pst->number_of_dependencies; i++)
2140     if (!pst->dependencies[i]->readin)
2141       {
2142         /* Inform about additional files that need to be read in.  */
2143         if (info_verbose)
2144           {
2145             fputs_filtered (" ", gdb_stdout);
2146             wrap_here ("");
2147             fputs_filtered ("and ", gdb_stdout);
2148             wrap_here ("");
2149             printf_filtered ("%s...", pst->dependencies[i]->filename);
2150             wrap_here ("");     /* Flush output.  */
2151             gdb_flush (gdb_stdout);
2152           }
2153         dbx_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
2154       }
2155
2156   if (LDSYMLEN (pst))           /* Otherwise it's a dummy.  */
2157     {
2158       /* Init stuff necessary for reading in symbols */
2159       stabsread_init ();
2160       buildsym_init ();
2161       scoped_free_pendings free_pending;
2162       file_string_table_offset = FILE_STRING_OFFSET (pst);
2163       symbol_size = SYMBOL_SIZE (pst);
2164
2165       /* Read in this file's symbols.  */
2166       bfd_seek (objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
2167       read_ofile_symtab (objfile, pst);
2168     }
2169
2170   pst->readin = 1;
2171 }
2172
2173 /* Read in all of the symbols for a given psymtab for real.
2174    Be verbose about it if the user wants that.  SELF is not NULL.  */
2175
2176 static void
2177 dbx_read_symtab (struct partial_symtab *self, struct objfile *objfile)
2178 {
2179   if (self->readin)
2180     {
2181       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  "
2182                           "Shouldn't happen.\n",
2183                           self->filename);
2184       return;
2185     }
2186
2187   if (LDSYMLEN (self) || self->number_of_dependencies)
2188     {
2189       /* Print the message now, before reading the string table,
2190          to avoid disconcerting pauses.  */
2191       if (info_verbose)
2192         {
2193           printf_filtered ("Reading in symbols for %s...", self->filename);
2194           gdb_flush (gdb_stdout);
2195         }
2196
2197       next_symbol_text_func = dbx_next_symbol_text;
2198
2199       {
2200         scoped_restore restore_stabs_data = make_scoped_restore (&stabs_data);
2201         gdb::unique_xmalloc_ptr<gdb_byte> data_holder;
2202         if (DBX_STAB_SECTION (objfile))
2203           {
2204             stabs_data
2205               = symfile_relocate_debug_section (objfile,
2206                                                 DBX_STAB_SECTION (objfile),
2207                                                 NULL);
2208             data_holder.reset (stabs_data);
2209           }
2210
2211         dbx_psymtab_to_symtab_1 (objfile, self);
2212       }
2213
2214       /* Match with global symbols.  This only needs to be done once,
2215          after all of the symtabs and dependencies have been read in.   */
2216       scan_file_globals (objfile);
2217
2218       /* Finish up the debug error message.  */
2219       if (info_verbose)
2220         printf_filtered ("done.\n");
2221     }
2222 }
2223
2224 /* Read in a defined section of a specific object file's symbols.  */
2225
2226 static void
2227 read_ofile_symtab (struct objfile *objfile, struct partial_symtab *pst)
2228 {
2229   const char *namestring;
2230   struct external_nlist *bufp;
2231   struct internal_nlist nlist;
2232   unsigned char type;
2233   unsigned max_symnum;
2234   bfd *abfd;
2235   int sym_offset;               /* Offset to start of symbols to read */
2236   int sym_size;                 /* Size of symbols to read */
2237   CORE_ADDR text_offset;        /* Start of text segment for symbols */
2238   int text_size;                /* Size of text segment for symbols */
2239   struct section_offsets *section_offsets;
2240
2241   sym_offset = LDSYMOFF (pst);
2242   sym_size = LDSYMLEN (pst);
2243   text_offset = pst->textlow;
2244   text_size = pst->texthigh - pst->textlow;
2245   section_offsets = objfile->section_offsets;
2246
2247   dbxread_objfile = objfile;
2248
2249   stringtab_global = DBX_STRINGTAB (objfile);
2250   set_last_source_file (NULL);
2251
2252   abfd = objfile->obfd;
2253   symfile_bfd = objfile->obfd;  /* Implicit param to next_text_symbol.  */
2254   symbuf_end = symbuf_idx = 0;
2255   symbuf_read = 0;
2256   symbuf_left = sym_offset + sym_size;
2257
2258   /* It is necessary to actually read one symbol *before* the start
2259      of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
2260      occurs before the N_SO symbol.
2261
2262      Detecting this in read_dbx_symtab
2263      would slow down initial readin, so we look for it here instead.  */
2264   if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
2265     {
2266       stabs_seek (sym_offset - symbol_size);
2267       fill_symbuf (abfd);
2268       bufp = &symbuf[symbuf_idx++];
2269       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2270       OBJSTAT (objfile, n_stabs++);
2271
2272       namestring = set_namestring (objfile, &nlist);
2273
2274       processing_gcc_compilation = 0;
2275       if (nlist.n_type == N_TEXT)
2276         {
2277           const char *tempstring = namestring;
2278
2279           if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
2280             processing_gcc_compilation = 1;
2281           else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2282             processing_gcc_compilation = 2;
2283           if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
2284             ++tempstring;
2285           if (startswith (tempstring, "__gnu_compiled"))
2286             processing_gcc_compilation = 2;
2287         }
2288     }
2289   else
2290     {
2291       /* The N_SO starting this symtab is the first symbol, so we
2292          better not check the symbol before it.  I'm not this can
2293          happen, but it doesn't hurt to check for it.  */
2294       stabs_seek (sym_offset);
2295       processing_gcc_compilation = 0;
2296     }
2297
2298   if (symbuf_idx == symbuf_end)
2299     fill_symbuf (abfd);
2300   bufp = &symbuf[symbuf_idx];
2301   if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
2302     error (_("First symbol in segment of executable not a source symbol"));
2303
2304   max_symnum = sym_size / symbol_size;
2305
2306   for (symnum = 0;
2307        symnum < max_symnum;
2308        symnum++)
2309     {
2310       QUIT;                     /* Allow this to be interruptable.  */
2311       if (symbuf_idx == symbuf_end)
2312         fill_symbuf (abfd);
2313       bufp = &symbuf[symbuf_idx++];
2314       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
2315       OBJSTAT (objfile, n_stabs++);
2316
2317       type = bfd_h_get_8 (abfd, bufp->e_type);
2318
2319       namestring = set_namestring (objfile, &nlist);
2320
2321       if (type & N_STAB)
2322         {
2323           if (sizeof (nlist.n_value) > 4
2324               /* We are a 64-bit debugger debugging a 32-bit program.  */
2325               && (type == N_LSYM || type == N_PSYM))
2326               /* We have to be careful with the n_value in the case of N_LSYM
2327                  and N_PSYM entries, because they are signed offsets from frame
2328                  pointer, but we actually read them as unsigned 32-bit values.
2329                  This is not a problem for 32-bit debuggers, for which negative
2330                  values end up being interpreted correctly (as negative
2331                  offsets) due to integer overflow.
2332                  But we need to sign-extend the value for 64-bit debuggers,
2333                  or we'll end up interpreting negative values as very large
2334                  positive offsets.  */
2335             nlist.n_value = (nlist.n_value ^ 0x80000000) - 0x80000000;
2336           process_one_symbol (type, nlist.n_desc, nlist.n_value,
2337                               namestring, section_offsets, objfile,
2338                               PST_LANGUAGE (pst));
2339         }
2340       /* We skip checking for a new .o or -l file; that should never
2341          happen in this routine.  */
2342       else if (type == N_TEXT)
2343         {
2344           /* I don't think this code will ever be executed, because
2345              the GCC_COMPILED_FLAG_SYMBOL usually is right before
2346              the N_SO symbol which starts this source file.
2347              However, there is no reason not to accept
2348              the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
2349
2350           if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
2351             processing_gcc_compilation = 1;
2352           else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2353             processing_gcc_compilation = 2;
2354         }
2355       else if (type & N_EXT || type == (unsigned char) N_TEXT
2356                || type == (unsigned char) N_NBTEXT)
2357         {
2358           /* Global symbol: see if we came across a dbx defintion for
2359              a corresponding symbol.  If so, store the value.  Remove
2360              syms from the chain when their values are stored, but
2361              search the whole chain, as there may be several syms from
2362              different files with the same name.  */
2363           /* This is probably not true.  Since the files will be read
2364              in one at a time, each reference to a global symbol will
2365              be satisfied in each file as it appears.  So we skip this
2366              section.  */
2367           ;
2368         }
2369     }
2370
2371   /* In a Solaris elf file, this variable, which comes from the
2372      value of the N_SO symbol, will still be 0.  Luckily, text_offset,
2373      which comes from pst->textlow is correct.  */
2374   if (last_source_start_addr == 0)
2375     last_source_start_addr = text_offset;
2376
2377   /* In reordered executables last_source_start_addr may not be the
2378      lower bound for this symtab, instead use text_offset which comes
2379      from pst->textlow which is correct.  */
2380   if (last_source_start_addr > text_offset)
2381     last_source_start_addr = text_offset;
2382
2383   pst->compunit_symtab = end_symtab (text_offset + text_size,
2384                                      SECT_OFF_TEXT (objfile));
2385
2386   end_stabs ();
2387
2388   dbxread_objfile = NULL;
2389 }
2390 \f
2391
2392 /* Record the namespace that the function defined by SYMBOL was
2393    defined in, if necessary.  BLOCK is the associated block; use
2394    OBSTACK for allocation.  */
2395
2396 static void
2397 cp_set_block_scope (const struct symbol *symbol,
2398                     struct block *block,
2399                     struct obstack *obstack)
2400 {
2401   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
2402     {
2403       /* Try to figure out the appropriate namespace from the
2404          demangled name.  */
2405
2406       /* FIXME: carlton/2003-04-15: If the function in question is
2407          a method of a class, the name will actually include the
2408          name of the class as well.  This should be harmless, but
2409          is a little unfortunate.  */
2410
2411       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
2412       unsigned int prefix_len = cp_entire_prefix_len (name);
2413
2414       block_set_scope (block,
2415                        (const char *) obstack_copy0 (obstack, name, prefix_len),
2416                        obstack);
2417     }
2418 }
2419
2420 /* This handles a single symbol from the symbol-file, building symbols
2421    into a GDB symtab.  It takes these arguments and an implicit argument.
2422
2423    TYPE is the type field of the ".stab" symbol entry.
2424    DESC is the desc field of the ".stab" entry.
2425    VALU is the value field of the ".stab" entry.
2426    NAME is the symbol name, in our address space.
2427    SECTION_OFFSETS is a set of amounts by which the sections of this
2428    object file were relocated when it was loaded into memory.  Note
2429    that these section_offsets are not the objfile->section_offsets but
2430    the pst->section_offsets.  All symbols that refer to memory
2431    locations need to be offset by these amounts.
2432    OBJFILE is the object file from which we are reading symbols.  It
2433    is used in end_symtab.
2434    LANGUAGE is the language of the symtab.
2435 */
2436
2437 void
2438 process_one_symbol (int type, int desc, CORE_ADDR valu, const char *name,
2439                     const struct section_offsets *section_offsets,
2440                     struct objfile *objfile, enum language language)
2441 {
2442   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2443   struct context_stack *newobj;
2444   /* This remembers the address of the start of a function.  It is
2445      used because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries
2446      are relative to the current function's start address.  On systems
2447      other than Solaris 2, this just holds the SECT_OFF_TEXT value,
2448      and is used to relocate these symbol types rather than
2449      SECTION_OFFSETS.  */
2450   static CORE_ADDR function_start_offset;
2451
2452   /* This holds the address of the start of a function, without the
2453      system peculiarities of function_start_offset.  */
2454   static CORE_ADDR last_function_start;
2455
2456   /* If this is nonzero, we've seen an N_SLINE since the start of the
2457      current function.  We use this to tell us to move the first sline
2458      to the beginning of the function regardless of what its given
2459      value is.  */
2460   static int sline_found_in_function = 1;
2461
2462   /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this
2463      source file.  Used to detect the SunPRO solaris compiler.  */
2464   static int n_opt_found;
2465
2466   /* Something is wrong if we see real data before seeing a source
2467      file name.  */
2468
2469   if (get_last_source_file () == NULL && type != (unsigned char) N_SO)
2470     {
2471       /* Ignore any symbols which appear before an N_SO symbol.
2472          Currently no one puts symbols there, but we should deal
2473          gracefully with the case.  A complain()t might be in order,
2474          but this should not be an error ().  */
2475       return;
2476     }
2477
2478   switch (type)
2479     {
2480     case N_FUN:
2481     case N_FNAME:
2482
2483       if (*name == '\000')
2484         {
2485           /* This N_FUN marks the end of a function.  This closes off
2486              the current block.  */
2487           struct block *block;
2488
2489           if (context_stack_depth <= 0)
2490             {
2491               lbrac_mismatch_complaint (symnum);
2492               break;
2493             }
2494
2495           /* The following check is added before recording line 0 at
2496              end of function so as to handle hand-generated stabs
2497              which may have an N_FUN stabs at the end of the function,
2498              but no N_SLINE stabs.  */
2499           if (sline_found_in_function)
2500             {
2501               CORE_ADDR addr = last_function_start + valu;
2502
2503               record_line (current_subfile, 0,
2504                            gdbarch_addr_bits_remove (gdbarch, addr));
2505             }
2506
2507           within_function = 0;
2508           newobj = pop_context ();
2509
2510           /* Make a block for the local symbols within.  */
2511           block = finish_block (newobj->name, &local_symbols,
2512                                 newobj->old_blocks, NULL,
2513                                 newobj->start_addr, newobj->start_addr + valu);
2514
2515           /* For C++, set the block's scope.  */
2516           if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
2517             cp_set_block_scope (newobj->name, block, &objfile->objfile_obstack);
2518
2519           /* May be switching to an assembler file which may not be using
2520              block relative stabs, so reset the offset.  */
2521           function_start_offset = 0;
2522
2523           break;
2524         }
2525
2526       sline_found_in_function = 0;
2527
2528       /* Relocate for dynamic loading.  */
2529       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2530       valu = gdbarch_addr_bits_remove (gdbarch, valu);
2531       last_function_start = valu;
2532
2533       goto define_a_symbol;
2534
2535     case N_LBRAC:
2536       /* This "symbol" just indicates the start of an inner lexical
2537          context within a function.  */
2538
2539       /* Ignore extra outermost context from SunPRO cc and acc.  */
2540       if (n_opt_found && desc == 1)
2541         break;
2542
2543       valu += function_start_offset;
2544
2545       push_context (desc, valu);
2546       break;
2547
2548     case N_RBRAC:
2549       /* This "symbol" just indicates the end of an inner lexical
2550          context that was started with N_LBRAC.  */
2551
2552       /* Ignore extra outermost context from SunPRO cc and acc.  */
2553       if (n_opt_found && desc == 1)
2554         break;
2555
2556       valu += function_start_offset;
2557
2558       if (context_stack_depth <= 0)
2559         {
2560           lbrac_mismatch_complaint (symnum);
2561           break;
2562         }
2563
2564       newobj = pop_context ();
2565       if (desc != newobj->depth)
2566         lbrac_mismatch_complaint (symnum);
2567
2568       if (local_symbols != NULL)
2569         {
2570           /* GCC development snapshots from March to December of
2571              2000 would output N_LSYM entries after N_LBRAC
2572              entries.  As a consequence, these symbols are simply
2573              discarded.  Complain if this is the case.  */
2574           complaint (_("misplaced N_LBRAC entry; discarding local "
2575                        "symbols which have no enclosing block"));
2576         }
2577       local_symbols = newobj->locals;
2578
2579       if (context_stack_depth > 1)
2580         {
2581           /* This is not the outermost LBRAC...RBRAC pair in the
2582              function, its local symbols preceded it, and are the ones
2583              just recovered from the context stack.  Define the block
2584              for them (but don't bother if the block contains no
2585              symbols.  Should we complain on blocks without symbols?
2586              I can't think of any useful purpose for them).  */
2587           if (local_symbols != NULL)
2588             {
2589               /* Muzzle a compiler bug that makes end < start.
2590
2591                  ??? Which compilers?  Is this ever harmful?.  */
2592               if (newobj->start_addr > valu)
2593                 {
2594                   complaint (_("block start larger than block end"));
2595                   newobj->start_addr = valu;
2596                 }
2597               /* Make a block for the local symbols within.  */
2598               finish_block (0, &local_symbols, newobj->old_blocks, NULL,
2599                             newobj->start_addr, valu);
2600             }
2601         }
2602       else
2603         {
2604           /* This is the outermost LBRAC...RBRAC pair.  There is no
2605              need to do anything; leave the symbols that preceded it
2606              to be attached to the function's own block.  We need to
2607              indicate that we just moved outside of the function.  */
2608           within_function = 0;
2609         }
2610
2611       break;
2612
2613     case N_FN:
2614     case N_FN_SEQ:
2615       /* This kind of symbol indicates the start of an object file.
2616          Relocate for dynamic loading.  */
2617       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2618       break;
2619
2620     case N_SO:
2621       /* This type of symbol indicates the start of data for one
2622          source file.  Finish the symbol table of the previous source
2623          file (if any) and start accumulating a new symbol table.
2624          Relocate for dynamic loading.  */
2625       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2626
2627       n_opt_found = 0;
2628
2629       if (get_last_source_file ())
2630         {
2631           /* Check if previous symbol was also an N_SO (with some
2632              sanity checks).  If so, that one was actually the
2633              directory name, and the current one is the real file
2634              name.  Patch things up.  */
2635           if (previous_stab_code == (unsigned char) N_SO)
2636             {
2637               patch_subfile_names (current_subfile, name);
2638               break;            /* Ignore repeated SOs.  */
2639             }
2640           end_symtab (valu, SECT_OFF_TEXT (objfile));
2641           end_stabs ();
2642         }
2643
2644       /* Null name means this just marks the end of text for this .o
2645          file.  Don't start a new symtab in this case.  */
2646       if (*name == '\000')
2647         break;
2648
2649       function_start_offset = 0;
2650
2651       start_stabs ();
2652       start_symtab (objfile, name, NULL, valu, language);
2653       record_debugformat ("stabs");
2654       break;
2655
2656     case N_SOL:
2657       /* This type of symbol indicates the start of data for a
2658          sub-source-file, one whose contents were copied or included
2659          in the compilation of the main source file (whose name was
2660          given in the N_SO symbol).  Relocate for dynamic loading.  */
2661       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2662       start_subfile (name);
2663       break;
2664
2665     case N_BINCL:
2666       push_subfile ();
2667       add_new_header_file (name, valu);
2668       start_subfile (name);
2669       break;
2670
2671     case N_EINCL:
2672       start_subfile (pop_subfile ());
2673       break;
2674
2675     case N_EXCL:
2676       add_old_header_file (name, valu);
2677       break;
2678
2679     case N_SLINE:
2680       /* This type of "symbol" really just records one line-number --
2681          core-address correspondence.  Enter it in the line list for
2682          this symbol table.  */
2683
2684       /* Relocate for dynamic loading and for ELF acc
2685          function-relative symbols.  */
2686       valu += function_start_offset;
2687
2688       /* GCC 2.95.3 emits the first N_SLINE stab somwehere in the
2689          middle of the prologue instead of right at the start of the
2690          function.  To deal with this we record the address for the
2691          first N_SLINE stab to be the start of the function instead of
2692          the listed location.  We really shouldn't to this.  When
2693          compiling with optimization, this first N_SLINE stab might be
2694          optimized away.  Other (non-GCC) compilers don't emit this
2695          stab at all.  There is no real harm in having an extra
2696          numbered line, although it can be a bit annoying for the
2697          user.  However, it totally screws up our testsuite.
2698
2699          So for now, keep adjusting the address of the first N_SLINE
2700          stab, but only for code compiled with GCC.  */
2701
2702       if (within_function && sline_found_in_function == 0)
2703         {
2704           CORE_ADDR addr = processing_gcc_compilation == 2 ?
2705                            last_function_start : valu;
2706
2707           record_line (current_subfile, desc,
2708                        gdbarch_addr_bits_remove (gdbarch, addr));
2709           sline_found_in_function = 1;
2710         }
2711       else
2712         record_line (current_subfile, desc,
2713                      gdbarch_addr_bits_remove (gdbarch, valu));
2714       break;
2715
2716     case N_BCOMM:
2717       common_block_start (name, objfile);
2718       break;
2719
2720     case N_ECOMM:
2721       common_block_end (objfile);
2722       break;
2723
2724       /* The following symbol types need to have the appropriate
2725          offset added to their value; then we process symbol
2726          definitions in the name.  */
2727
2728     case N_STSYM:               /* Static symbol in data segment.  */
2729     case N_LCSYM:               /* Static symbol in BSS segment.  */
2730     case N_ROSYM:               /* Static symbol in read-only data segment.  */
2731       /* HORRID HACK DEPT.  However, it's Sun's furgin' fault.
2732          Solaris 2's stabs-in-elf makes *most* symbols relative but
2733          leaves a few absolute (at least for Solaris 2.1 and version
2734          2.0.1 of the SunPRO compiler).  N_STSYM and friends sit on
2735          the fence.  .stab "foo:S...",N_STSYM is absolute (ld
2736          relocates it) .stab "foo:V...",N_STSYM is relative (section
2737          base subtracted).  This leaves us no choice but to search for
2738          the 'S' or 'V'...  (or pass the whole section_offsets stuff
2739          down ONE MORE function call level, which we really don't want
2740          to do).  */
2741       {
2742         const char *p;
2743
2744         /* Normal object file and NLMs have non-zero text seg offsets,
2745            but don't need their static syms offset in this fashion.
2746            XXX - This is really a crock that should be fixed in the
2747            solib handling code so that I don't have to work around it
2748            here.  */
2749
2750         if (!symfile_relocatable)
2751           {
2752             p = strchr (name, ':');
2753             if (p != 0 && p[1] == 'S')
2754               {
2755                 /* The linker relocated it.  We don't want to add a
2756                    Sun-stabs Tfoo.foo-like offset, but we *do*
2757                    want to add whatever solib.c passed to
2758                    symbol_file_add as addr (this is known to affect
2759                    SunOS 4, and I suspect ELF too).  Since there is no
2760                    Ttext.text symbol, we can get addr from the text offset.  */
2761                 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2762                 goto define_a_symbol;
2763               }
2764           }
2765         /* Since it's not the kludge case, re-dispatch to the right
2766            handler.  */
2767         switch (type)
2768           {
2769           case N_STSYM:
2770             goto case_N_STSYM;
2771           case N_LCSYM:
2772             goto case_N_LCSYM;
2773           case N_ROSYM:
2774             goto case_N_ROSYM;
2775           default:
2776             internal_error (__FILE__, __LINE__,
2777                             _("failed internal consistency check"));
2778           }
2779       }
2780
2781     case_N_STSYM:               /* Static symbol in data segment.  */
2782     case N_DSLINE:              /* Source line number, data segment.  */
2783       valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
2784       goto define_a_symbol;
2785
2786     case_N_LCSYM:               /* Static symbol in BSS segment.  */
2787     case N_BSLINE:              /* Source line number, BSS segment.  */
2788       /* N_BROWS: overlaps with N_BSLINE.  */
2789       valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
2790       goto define_a_symbol;
2791
2792     case_N_ROSYM:               /* Static symbol in read-only data segment.  */
2793       valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
2794       goto define_a_symbol;
2795
2796     case N_ENTRY:               /* Alternate entry point.  */
2797       /* Relocate for dynamic loading.  */
2798       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
2799       goto define_a_symbol;
2800
2801       /* The following symbol types we don't know how to process.
2802          Handle them in a "default" way, but complain to people who
2803          care.  */
2804     default:
2805     case N_CATCH:               /* Exception handler catcher.  */
2806     case N_EHDECL:              /* Exception handler name.  */
2807     case N_PC:                  /* Global symbol in Pascal.  */
2808     case N_M2C:                 /* Modula-2 compilation unit.  */
2809       /* N_MOD2: overlaps with N_EHDECL.  */
2810     case N_SCOPE:               /* Modula-2 scope information.  */
2811     case N_ECOML:               /* End common (local name).  */
2812     case N_NBTEXT:              /* Gould Non-Base-Register symbols???  */
2813     case N_NBDATA:
2814     case N_NBBSS:
2815     case N_NBSTS:
2816     case N_NBLCS:
2817       unknown_symtype_complaint (hex_string (type));
2818       /* FALLTHROUGH */
2819
2820     define_a_symbol:
2821       /* These symbol types don't need the address field relocated,
2822          since it is either unused, or is absolute.  */
2823     case N_GSYM:                /* Global variable.  */
2824     case N_NSYMS:               /* Number of symbols (Ultrix).  */
2825     case N_NOMAP:               /* No map?  (Ultrix).  */
2826     case N_RSYM:                /* Register variable.  */
2827     case N_DEFD:                /* Modula-2 GNU module dependency.  */
2828     case N_SSYM:                /* Struct or union element.  */
2829     case N_LSYM:                /* Local symbol in stack.  */
2830     case N_PSYM:                /* Parameter variable.  */
2831     case N_LENG:                /* Length of preceding symbol type.  */
2832       if (name)
2833         {
2834           int deftype;
2835           const char *colon_pos = strchr (name, ':');
2836
2837           if (colon_pos == NULL)
2838             deftype = '\0';
2839           else
2840             deftype = colon_pos[1];
2841
2842           switch (deftype)
2843             {
2844             case 'f':
2845             case 'F':
2846               /* Deal with the SunPRO 3.0 compiler which omits the
2847                  address from N_FUN symbols.  */
2848               if (type == N_FUN
2849                   && valu == ANOFFSET (section_offsets,
2850                                        SECT_OFF_TEXT (objfile))
2851                   && gdbarch_sofun_address_maybe_missing (gdbarch))
2852                 {
2853                   CORE_ADDR minsym_valu = 
2854                     find_stab_function_addr (name, get_last_source_file (),
2855                                              objfile);
2856
2857                   /* The function find_stab_function_addr will return
2858                      0 if the minimal symbol wasn't found.
2859                      (Unfortunately, this might also be a valid
2860                      address.)  Anyway, if it *does* return 0, it is
2861                      likely that the value was set correctly to begin
2862                      with...  */
2863                   if (minsym_valu != 0)
2864                     valu = minsym_valu;
2865                 }
2866
2867               /* These addresses are absolute.  */
2868               function_start_offset = valu;
2869
2870               within_function = 1;
2871
2872               if (context_stack_depth > 1)
2873                 {
2874                   complaint (_("unmatched N_LBRAC before symtab pos %d"),
2875                              symnum);
2876                   break;
2877                 }
2878
2879               if (context_stack_depth > 0)
2880                 {
2881                   struct block *block;
2882
2883                   newobj = pop_context ();
2884                   /* Make a block for the local symbols within.  */
2885                   block = finish_block (newobj->name, &local_symbols,
2886                                         newobj->old_blocks, NULL,
2887                                         newobj->start_addr, valu);
2888
2889                   /* For C++, set the block's scope.  */
2890                   if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
2891                     cp_set_block_scope (newobj->name, block,
2892                                         &objfile->objfile_obstack);
2893                 }
2894
2895               newobj = push_context (0, valu);
2896               newobj->name = define_symbol (valu, name, desc, type, objfile);
2897               break;
2898
2899             default:
2900               define_symbol (valu, name, desc, type, objfile);
2901               break;
2902             }
2903         }
2904       break;
2905
2906       /* We use N_OPT to carry the gcc2_compiled flag.  Sun uses it
2907          for a bunch of other flags, too.  Someday we may parse their
2908          flags; for now we ignore theirs and hope they'll ignore ours.  */
2909     case N_OPT:                 /* Solaris 2: Compiler options.  */
2910       if (name)
2911         {
2912           if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
2913             {
2914               processing_gcc_compilation = 2;
2915             }
2916           else
2917             n_opt_found = 1;
2918         }
2919       break;
2920
2921     case N_MAIN:                /* Name of main routine.  */
2922       /* FIXME: If one has a symbol file with N_MAIN and then replaces
2923          it with a symbol file with "main" and without N_MAIN.  I'm
2924          not sure exactly what rule to follow but probably something
2925          like: N_MAIN takes precedence over "main" no matter what
2926          objfile it is in; If there is more than one N_MAIN, choose
2927          the one in the symfile_objfile; If there is more than one
2928          N_MAIN within a given objfile, complain() and choose
2929          arbitrarily.  (kingdon) */
2930       if (name != NULL)
2931         set_objfile_main_name (objfile, name, language_unknown);
2932       break;
2933
2934       /* The following symbol types can be ignored.  */
2935     case N_OBJ:                 /* Solaris 2: Object file dir and name.  */
2936     case N_PATCH:               /* Solaris 2: Patch Run Time Checker.  */
2937       /* N_UNDF:                   Solaris 2: File separator mark.  */
2938       /* N_UNDF: -- we will never encounter it, since we only process
2939          one file's symbols at once.  */
2940     case N_ENDM:                /* Solaris 2: End of module.  */
2941     case N_ALIAS:               /* SunPro F77: alias name, ignore for now.  */
2942       break;
2943     }
2944
2945   /* '#' is a GNU C extension to allow one symbol to refer to another
2946      related symbol.
2947
2948      Generally this is used so that an alias can refer to its main
2949      symbol.  */
2950   gdb_assert (name);
2951   if (name[0] == '#')
2952     {
2953       /* Initialize symbol reference names and determine if this is a
2954          definition.  If a symbol reference is being defined, go ahead
2955          and add it.  Otherwise, just return.  */
2956
2957       const char *s = name;
2958       int refnum;
2959
2960       /* If this stab defines a new reference ID that is not on the
2961          reference list, then put it on the reference list.
2962
2963          We go ahead and advance NAME past the reference, even though
2964          it is not strictly necessary at this time.  */
2965       refnum = symbol_reference_defined (&s);
2966       if (refnum >= 0)
2967         if (!ref_search (refnum))
2968           ref_add (refnum, 0, name, valu);
2969       name = s;
2970     }
2971
2972   previous_stab_code = type;
2973 }
2974 \f
2975 /* FIXME: The only difference between this and elfstab_build_psymtabs
2976    is the call to install_minimal_symbols for elf, and the support for
2977    split sections.  If the differences are really that small, the code
2978    should be shared.  */
2979
2980 /* Scan and build partial symbols for an coff symbol file.
2981    The coff file has already been processed to get its minimal symbols.
2982
2983    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2984    rolled into one.
2985
2986    OBJFILE is the object file we are reading symbols from.
2987    ADDR is the address relative to which the symbols are (e.g.
2988    the base address of the text segment).
2989    TEXTADDR is the address of the text section.
2990    TEXTSIZE is the size of the text section.
2991    STABSECTS is the list of .stab sections in OBJFILE.
2992    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2993    .stabstr section exists.
2994
2995    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2996    adjusted for coff details.  */
2997
2998 void
2999 coffstab_build_psymtabs (struct objfile *objfile,
3000                          CORE_ADDR textaddr, unsigned int textsize,
3001                          struct stab_section_list *stabsects,
3002                          file_ptr stabstroffset, unsigned int stabstrsize)
3003 {
3004   int val;
3005   bfd *sym_bfd = objfile->obfd;
3006   char *name = bfd_get_filename (sym_bfd);
3007   unsigned int stabsize;
3008
3009   DBX_TEXT_ADDR (objfile) = textaddr;
3010   DBX_TEXT_SIZE (objfile) = textsize;
3011
3012 #define COFF_STABS_SYMBOL_SIZE  12      /* XXX FIXME XXX */
3013   DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
3014   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
3015
3016   if (stabstrsize > bfd_get_size (sym_bfd))
3017     error (_("ridiculous string table size: %d bytes"), stabstrsize);
3018   DBX_STRINGTAB (objfile) = (char *)
3019     obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
3020   OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
3021
3022   /* Now read in the string table in one big gulp.  */
3023
3024   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3025   if (val < 0)
3026     perror_with_name (name);
3027   val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
3028   if (val != stabstrsize)
3029     perror_with_name (name);
3030
3031   stabsread_new_init ();
3032   buildsym_new_init ();
3033   free_header_files ();
3034   init_header_files ();
3035
3036   processing_acc_compilation = 1;
3037
3038   /* In a coff file, we've already installed the minimal symbols that came
3039      from the coff (non-stab) symbol table, so always act like an
3040      incremental load here.  */
3041   if (stabsects->next == NULL)
3042     {
3043       stabsize = bfd_section_size (sym_bfd, stabsects->section);
3044       DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
3045       DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3046     }
3047   else
3048     {
3049       struct stab_section_list *stabsect;
3050
3051       DBX_SYMCOUNT (objfile) = 0;
3052       for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
3053         {
3054           stabsize = bfd_section_size (sym_bfd, stabsect->section);
3055           DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
3056         }
3057
3058       DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
3059
3060       symbuf_sections = stabsects->next;
3061       symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
3062       symbuf_read = 0;
3063     }
3064
3065   dbx_symfile_read (objfile, 0);
3066 }
3067 \f
3068 /* Scan and build partial symbols for an ELF symbol file.
3069    This ELF file has already been processed to get its minimal symbols.
3070
3071    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3072    rolled into one.
3073
3074    OBJFILE is the object file we are reading symbols from.
3075    ADDR is the address relative to which the symbols are (e.g.
3076    the base address of the text segment).
3077    STABSECT is the BFD section information for the .stab section.
3078    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
3079    .stabstr section exists.
3080
3081    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
3082    adjusted for elf details.  */
3083
3084 void
3085 elfstab_build_psymtabs (struct objfile *objfile, asection *stabsect,
3086                         file_ptr stabstroffset, unsigned int stabstrsize)
3087 {
3088   int val;
3089   bfd *sym_bfd = objfile->obfd;
3090   char *name = bfd_get_filename (sym_bfd);
3091
3092   /* Find the first and last text address.  dbx_symfile_read seems to
3093      want this.  */
3094   find_text_range (sym_bfd, objfile);
3095
3096 #define ELF_STABS_SYMBOL_SIZE   12      /* XXX FIXME XXX */
3097   DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
3098   DBX_SYMCOUNT (objfile)
3099     = bfd_section_size (objfile->obfd, stabsect) / DBX_SYMBOL_SIZE (objfile);
3100   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
3101   DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
3102   DBX_STAB_SECTION (objfile) = stabsect;
3103
3104   if (stabstrsize > bfd_get_size (sym_bfd))
3105     error (_("ridiculous string table size: %d bytes"), stabstrsize);
3106   DBX_STRINGTAB (objfile) = (char *)
3107     obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
3108   OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
3109
3110   /* Now read in the string table in one big gulp.  */
3111
3112   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
3113   if (val < 0)
3114     perror_with_name (name);
3115   val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
3116   if (val != stabstrsize)
3117     perror_with_name (name);
3118
3119   stabsread_new_init ();
3120   buildsym_new_init ();
3121   free_header_files ();
3122   init_header_files ();
3123
3124   processing_acc_compilation = 1;
3125
3126   symbuf_read = 0;
3127   symbuf_left = bfd_section_size (objfile->obfd, stabsect);
3128
3129   scoped_restore restore_stabs_data = make_scoped_restore (&stabs_data);
3130   gdb::unique_xmalloc_ptr<gdb_byte> data_holder;
3131
3132   stabs_data = symfile_relocate_debug_section (objfile, stabsect, NULL);
3133   if (stabs_data)
3134     data_holder.reset (stabs_data);
3135
3136   /* In an elf file, we've already installed the minimal symbols that came
3137      from the elf (non-stab) symbol table, so always act like an
3138      incremental load here.  dbx_symfile_read should not generate any new
3139      minimal symbols, since we will have already read the ELF dynamic symbol
3140      table and normal symbol entries won't be in the ".stab" section; but in
3141      case it does, it will install them itself.  */
3142   dbx_symfile_read (objfile, 0);
3143 }
3144 \f
3145 /* Scan and build partial symbols for a file with special sections for stabs
3146    and stabstrings.  The file has already been processed to get its minimal
3147    symbols, and any other symbols that might be necessary to resolve GSYMs.
3148
3149    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
3150    rolled into one.
3151
3152    OBJFILE is the object file we are reading symbols from.
3153    ADDR is the address relative to which the symbols are (e.g. the base address
3154    of the text segment).
3155    STAB_NAME is the name of the section that contains the stabs.
3156    STABSTR_NAME is the name of the section that contains the stab strings.
3157
3158    This routine is mostly copied from dbx_symfile_init and
3159    dbx_symfile_read.  */
3160
3161 void
3162 stabsect_build_psymtabs (struct objfile *objfile, char *stab_name,
3163                          char *stabstr_name, char *text_name)
3164 {
3165   int val;
3166   bfd *sym_bfd = objfile->obfd;
3167   char *name = bfd_get_filename (sym_bfd);
3168   asection *stabsect;
3169   asection *stabstrsect;
3170   asection *text_sect;
3171   struct dbx_symfile_info *dbx;
3172
3173   stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
3174   stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
3175
3176   if (!stabsect)
3177     return;
3178
3179   if (!stabstrsect)
3180     error (_("stabsect_build_psymtabs:  Found stabs (%s), "
3181              "but not string section (%s)"),
3182            stab_name, stabstr_name);
3183
3184   dbx = XCNEW (struct dbx_symfile_info);
3185   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
3186
3187   text_sect = bfd_get_section_by_name (sym_bfd, text_name);
3188   if (!text_sect)
3189     error (_("Can't find %s section in symbol file"), text_name);
3190   DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
3191   DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
3192
3193   DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
3194   DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
3195     / DBX_SYMBOL_SIZE (objfile);
3196   DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
3197   DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;      /* XXX - FIXME: POKING
3198                                                            INSIDE BFD DATA
3199                                                            STRUCTURES */
3200
3201   if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
3202     error (_("ridiculous string table size: %d bytes"),
3203            DBX_STRINGTAB_SIZE (objfile));
3204   DBX_STRINGTAB (objfile) = (char *)
3205     obstack_alloc (&objfile->objfile_obstack,
3206                    DBX_STRINGTAB_SIZE (objfile) + 1);
3207   OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
3208
3209   /* Now read in the string table in one big gulp.  */
3210
3211   val = bfd_get_section_contents (sym_bfd,      /* bfd */
3212                                   stabstrsect,  /* bfd section */
3213                                   DBX_STRINGTAB (objfile), /* input buffer */
3214                                   0,            /* offset into section */
3215                                   DBX_STRINGTAB_SIZE (objfile)); /* amount to
3216                                                                     read */
3217
3218   if (!val)
3219     perror_with_name (name);
3220
3221   stabsread_new_init ();
3222   buildsym_new_init ();
3223   free_header_files ();
3224   init_header_files ();
3225
3226   /* Now, do an incremental load.  */
3227
3228   processing_acc_compilation = 1;
3229   dbx_symfile_read (objfile, 0);
3230 }
3231 \f
3232 static const struct sym_fns aout_sym_fns =
3233 {
3234   dbx_new_init,                 /* init anything gbl to entire symtab */
3235   dbx_symfile_init,             /* read initial info, setup for sym_read() */
3236   dbx_symfile_read,             /* read a symbol file into symtab */
3237   NULL,                         /* sym_read_psymbols */
3238   dbx_symfile_finish,           /* finished with file, cleanup */
3239   default_symfile_offsets,      /* parse user's offsets to internal form */
3240   default_symfile_segments,     /* Get segment information from a file.  */
3241   NULL,
3242   default_symfile_relocate,     /* Relocate a debug section.  */
3243   NULL,                         /* sym_probe_fns */
3244   &psym_functions
3245 };
3246
3247 void
3248 _initialize_dbxread (void)
3249 {
3250   add_symtab_fns (bfd_target_aout_flavour, &aout_sym_fns);
3251
3252   dbx_objfile_data_key
3253     = register_objfile_data_with_cleanup (NULL, dbx_free_symfile_info);
3254 }