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