* buildsym.c (patch_subfile_name): Update last_source_file
[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    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* This module provides three functions: dbx_symfile_init,
22    which initializes to read a symbol file; dbx_new_init, which 
23    discards existing cached information when all symbols are being
24    discarded; and dbx_symfile_read, which reads a symbol table
25    from a file.
26
27    dbx_symfile_read only does the minimum work necessary for letting the
28    user "name" things symbolically; it does not read the entire symtab.
29    Instead, it reads the external and static symbols and puts them in partial
30    symbol tables.  When more extensive information is requested of a
31    file, the corresponding partial symbol table is mutated into a full
32    fledged symbol table by going back and reading the symbols
33    for real.  dbx_psymtab_to_symtab() is the function that does this */
34
35 #include "defs.h"
36 #include <string.h>
37
38 #if defined(USG) || defined(__CYGNUSCLIB__)
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #endif
42
43 #include <obstack.h>
44 #include <sys/param.h>
45 #ifndef NO_SYS_FILE
46 #include <sys/file.h>
47 #endif
48 #include <sys/stat.h>
49 #include <ctype.h>
50 #include "symtab.h"
51 #include "breakpoint.h"
52 #include "command.h"
53 #include "target.h"
54 #include "gdbcore.h"            /* for bfd stuff */
55 #include "libaout.h"            /* FIXME Secret internal BFD stuff for a.out */
56 #include "symfile.h"
57 #include "objfiles.h"
58 #include "buildsym.h"
59 #include "stabsread.h"
60 #include "gdb-stabs.h"
61 #include "demangle.h"
62 #include "language.h"           /* Needed inside partial-stab.h */
63 #include "complaints.h"
64
65 #include "aout/aout64.h"
66 #include "aout/stab_gnu.h"      /* We always use GNU stabs, not native, now */
67
68 #if !defined (SEEK_SET)
69 #define SEEK_SET 0
70 #define SEEK_CUR 1
71 #endif
72
73 /* Each partial symbol table entry contains a pointer to private data for the
74    read_symtab() function to use when expanding a partial symbol table entry
75    to a full symbol table entry.
76
77    For dbxread this structure contains the offset within the file symbol table
78    of first local symbol for this file, and length (in bytes) of the section
79    of the symbol table devoted to this file's symbols (actually, the section
80    bracketed may contain more than just this file's symbols).  It also contains
81    further information needed to locate the symbols if they are in an ELF file.
82
83    If ldsymlen is 0, the only reason for this thing's existence is the
84    dependency list.  Nothing else will happen when it is read in.  */
85
86 #define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
87 #define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
88 #define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
89 #define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
90 #define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
91 #define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
92 #define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
93
94 struct symloc {
95   int ldsymoff;
96   int ldsymlen;
97   int symbol_size;
98   int symbol_offset;
99   int string_offset;
100   int file_string_offset;
101 };
102
103 /* Macro to determine which symbols to ignore when reading the first symbol
104    of a file.  Some machines override this definition. */
105 #ifndef IGNORE_SYMBOL
106 /* This code is used on Ultrix systems.  Ignore it */
107 #define IGNORE_SYMBOL(type)  (type == (int)N_NSYMS)
108 #endif
109
110 /* Remember what we deduced to be the source language of this psymtab. */
111
112 static enum language psymtab_language = language_unknown;
113
114 /* Nonzero means give verbose info on gdb action.  From main.c.  */
115 extern int info_verbose;
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 static unsigned symbol_table_offset;
129
130 /* This is the offset of the string table in the executable file */
131 static unsigned string_table_offset;
132
133 /* For elf+stab executables, the n_strx field is not a simple index
134    into the string table.  Instead, each .o file has a base offset
135    in the string table, and the associated symbols contain offsets
136    from this base.  The following two variables contain the base
137    offset for the current and next .o files. */
138 static unsigned int file_string_table_offset;
139 static unsigned int next_file_string_table_offset;
140
141 /* .o and NLM files contain unrelocated addresses which are based at 0.  When
142    non-zero, this flag disables some of the special cases for Solaris elf+stab
143    text addresses at location 0. */
144
145 static int symfile_relocatable = 0;
146
147   /* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are relative
148      to the function start address.  */
149
150 static int block_address_function_relative = 0;
151 \f
152 /* This is the lowest text address we have yet encountered.  */
153 static CORE_ADDR lowest_text_address;
154
155 /* Complaints about the symbols we have encountered.  */
156
157 struct complaint lbrac_complaint = 
158   {"bad block start address patched", 0, 0};
159
160 struct complaint string_table_offset_complaint =
161   {"bad string table offset in symbol %d", 0, 0};
162
163 struct complaint unknown_symtype_complaint =
164   {"unknown symbol type %s", 0, 0};
165
166 struct complaint unknown_symchar_complaint =
167   {"unknown symbol descriptor `%c'", 0, 0};
168
169 struct complaint lbrac_rbrac_complaint =
170   {"block start larger than block end", 0, 0};
171
172 struct complaint lbrac_unmatched_complaint =
173   {"unmatched N_LBRAC before symtab pos %d", 0, 0};
174
175 struct complaint lbrac_mismatch_complaint =
176   {"N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", 0, 0};
177
178 struct complaint repeated_header_complaint =
179   {"\"repeated\" header file %s not previously seen, at symtab pos %d", 0, 0};
180 \f
181 /* During initial symbol readin, we need to have a structure to keep
182    track of which psymtabs have which bincls in them.  This structure
183    is used during readin to setup the list of dependencies within each
184    partial symbol table. */
185
186 struct header_file_location
187 {
188   char *name;                   /* Name of header file */
189   int instance;                 /* See above */
190   struct partial_symtab *pst;   /* Partial symtab that has the
191                                    BINCL/EINCL defs for this file */
192 };
193
194 /* The actual list and controling variables */
195 static struct header_file_location *bincl_list, *next_bincl;
196 static int bincls_allocated;
197
198 /* Local function prototypes */
199
200 static void
201 free_header_files PARAMS ((void));
202
203 static void
204 init_header_files PARAMS ((void));
205
206 static void
207 read_ofile_symtab PARAMS ((struct partial_symtab *));
208
209 static void
210 dbx_psymtab_to_symtab PARAMS ((struct partial_symtab *));
211
212 static void
213 dbx_psymtab_to_symtab_1 PARAMS ((struct partial_symtab *));
214
215 static void
216 read_dbx_dynamic_symtab PARAMS ((struct section_offsets *,
217                                  struct objfile *objfile));
218
219 static void
220 read_dbx_symtab PARAMS ((struct section_offsets *, struct objfile *,
221                          CORE_ADDR, int));
222
223 static void
224 free_bincl_list PARAMS ((struct objfile *));
225
226 static struct partial_symtab *
227 find_corresponding_bincl_psymtab PARAMS ((char *, int));
228
229 static void
230 add_bincl_to_list PARAMS ((struct partial_symtab *, char *, int));
231
232 static void
233 init_bincl_list PARAMS ((int, struct objfile *));
234
235 static void
236 init_psymbol_list PARAMS ((struct objfile *));
237
238 static char *
239 dbx_next_symbol_text PARAMS ((void));
240
241 static void
242 fill_symbuf PARAMS ((bfd *));
243
244 static void
245 dbx_symfile_init PARAMS ((struct objfile *));
246
247 static void
248 dbx_new_init PARAMS ((struct objfile *));
249
250 static void
251 dbx_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
252
253 static void
254 dbx_symfile_finish PARAMS ((struct objfile *));
255
256 static void
257 record_minimal_symbol PARAMS ((char *, CORE_ADDR, int, struct objfile *));
258
259 static void
260 add_new_header_file PARAMS ((char *, int));
261
262 static void
263 add_old_header_file PARAMS ((char *, int));
264
265 static void
266 add_this_object_header_file PARAMS ((int));
267
268 /* Free up old header file tables */
269
270 static void
271 free_header_files ()
272 {
273   register int i;
274
275   if (header_files != NULL)
276     {
277       for (i = 0; i < n_header_files; i++)
278         {
279           free (header_files[i].name);
280         }
281       free ((PTR)header_files);
282       header_files = NULL;
283       n_header_files = 0;
284     }
285   if (this_object_header_files)
286     {
287       free ((PTR)this_object_header_files);
288       this_object_header_files = NULL;
289     }
290   n_allocated_header_files = 0;
291   n_allocated_this_object_header_files = 0;
292 }
293
294 /* Allocate new header file tables */
295
296 static void
297 init_header_files ()
298 {
299   n_header_files = 0;
300   n_allocated_header_files = 10;
301   header_files = (struct header_file *)
302     xmalloc (10 * sizeof (struct header_file));
303
304   n_allocated_this_object_header_files = 10;
305   this_object_header_files = (int *) xmalloc (10 * sizeof (int));
306 }
307
308 /* Add header file number I for this object file
309    at the next successive FILENUM.  */
310
311 static void
312 add_this_object_header_file (i)
313      int i;
314 {
315   if (n_this_object_header_files == n_allocated_this_object_header_files)
316     {
317       n_allocated_this_object_header_files *= 2;
318       this_object_header_files
319         = (int *) xrealloc ((char *) this_object_header_files,
320                             n_allocated_this_object_header_files * sizeof (int));
321     }
322
323   this_object_header_files[n_this_object_header_files++] = i;
324 }
325
326 /* Add to this file an "old" header file, one already seen in
327    a previous object file.  NAME is the header file's name.
328    INSTANCE is its instance code, to select among multiple
329    symbol tables for the same header file.  */
330
331 static void
332 add_old_header_file (name, instance)
333      char *name;
334      int instance;
335 {
336   register struct header_file *p = header_files;
337   register int i;
338
339   for (i = 0; i < n_header_files; i++)
340     if (STREQ (p[i].name, name) && instance == p[i].instance)
341       {
342         add_this_object_header_file (i);
343         return;
344       }
345   complain (&repeated_header_complaint, name, symnum);
346 }
347
348 /* Add to this file a "new" header file: definitions for its types follow.
349    NAME is the header file's name.
350    Most often this happens only once for each distinct header file,
351    but not necessarily.  If it happens more than once, INSTANCE has
352    a different value each time, and references to the header file
353    use INSTANCE values to select among them.
354
355    dbx output contains "begin" and "end" markers for each new header file,
356    but at this level we just need to know which files there have been;
357    so we record the file when its "begin" is seen and ignore the "end".  */
358
359 static void
360 add_new_header_file (name, instance)
361      char *name;
362      int instance;
363 {
364   register int i;
365
366   /* Make sure there is room for one more header file.  */
367
368   if (n_header_files == n_allocated_header_files)
369     {
370       n_allocated_header_files *= 2;
371       header_files = (struct header_file *)
372         xrealloc ((char *) header_files,
373                   (n_allocated_header_files * sizeof (struct header_file)));
374     }
375
376   /* Create an entry for this header file.  */
377
378   i = n_header_files++;
379   header_files[i].name = savestring (name, strlen(name));
380   header_files[i].instance = instance;
381   header_files[i].length = 10;
382   header_files[i].vector
383     = (struct type **) xmalloc (10 * sizeof (struct type *));
384   memset (header_files[i].vector, 0, 10 * sizeof (struct type *));
385
386   add_this_object_header_file (i);
387 }
388
389 #if 0
390 static struct type **
391 explicit_lookup_type (real_filenum, index)
392      int real_filenum, index;
393 {
394   register struct header_file *f = &header_files[real_filenum];
395
396   if (index >= f->length)
397     {
398       f->length *= 2;
399       f->vector = (struct type **)
400         xrealloc (f->vector, f->length * sizeof (struct type *));
401       memset (&f->vector[f->length / 2],
402              '\0', f->length * sizeof (struct type *) / 2);
403     }
404   return &f->vector[index];
405 }
406 #endif
407 \f
408 static void
409 record_minimal_symbol (name, address, type, objfile)
410      char *name;
411      CORE_ADDR address;
412      int type;
413      struct objfile *objfile;
414 {
415   enum minimal_symbol_type ms_type;
416   int section;
417
418   switch (type)
419     {
420     case N_TEXT | N_EXT:
421       ms_type = mst_text;
422       section = SECT_OFF_TEXT;
423       break;
424     case N_DATA | N_EXT:
425       ms_type = mst_data;
426       section = SECT_OFF_DATA;
427       break;
428     case N_BSS | N_EXT:
429       ms_type = mst_bss;
430       section = SECT_OFF_BSS;
431       break;
432     case N_ABS | N_EXT:
433       ms_type = mst_abs;
434       section = -1;
435       break;
436 #ifdef N_SETV
437     case N_SETV | N_EXT:
438       ms_type = mst_data;
439       section = SECT_OFF_DATA;
440       break;
441     case N_SETV:
442       /* I don't think this type actually exists; since a N_SETV is the result
443          of going over many .o files, it doesn't make sense to have one
444          file local.  */
445       ms_type = mst_file_data;
446       section = SECT_OFF_DATA;
447       break;
448 #endif
449     case N_TEXT:
450     case N_NBTEXT:
451     case N_FN:
452     case N_FN_SEQ:
453       ms_type = mst_file_text;
454       section = SECT_OFF_TEXT;
455       break;
456     case N_DATA:
457       ms_type = mst_file_data;
458
459       /* Check for __DYNAMIC, which is used by Sun shared libraries. 
460          Record it as global even if it's local, not global, so
461          lookup_minimal_symbol can find it.  We don't check symbol_leading_char
462          because for SunOS4 it always is '_'.  */
463       if (name[8] == 'C' && STREQ ("__DYNAMIC", name))
464         ms_type = mst_data;
465
466       /* Same with virtual function tables, both global and static.  */
467       {
468         char *tempstring = name;
469         if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
470           ++tempstring;
471         if (VTBL_PREFIX_P ((tempstring)))
472           ms_type = mst_data;
473       }
474       section = SECT_OFF_DATA;
475       break;
476     case N_BSS:
477       ms_type = mst_file_bss;
478       section = SECT_OFF_BSS;
479       break;
480     default:
481       ms_type = mst_unknown;
482       section = -1;
483       break;
484   }
485
486   if ((ms_type == mst_file_text || ms_type == mst_text)
487       && address < lowest_text_address)
488     lowest_text_address = address;
489
490   prim_record_minimal_symbol_and_info
491     (obsavestring (name, strlen (name), &objfile -> symbol_obstack),
492      address,
493      ms_type,
494      NULL,
495      section,
496      objfile);
497 }
498 \f
499 /* Scan and build partial symbols for a symbol file.
500    We have been initialized by a call to dbx_symfile_init, which 
501    put all the relevant info into a "struct dbx_symfile_info",
502    hung off the objfile structure.
503
504    SECTION_OFFSETS contains offsets relative to which the symbols in the
505    various sections are (depending where the sections were actually loaded).
506    MAINLINE is true if we are reading the main symbol
507    table (as opposed to a shared lib or dynamically loaded file).  */
508
509 static void
510 dbx_symfile_read (objfile, section_offsets, mainline)
511      struct objfile *objfile;
512      struct section_offsets *section_offsets;
513      int mainline;      /* FIXME comments above */
514 {
515   bfd *sym_bfd;
516   int val;
517   struct cleanup *back_to;
518
519   val = strlen (objfile->name);
520
521   /* .o and .nlm files are relocatables with text, data and bss segs based at
522      0.  This flag disables special (Solaris stabs-in-elf only) fixups for
523      symbols with a value of 0.  XXX - This is a Krock.  Solaris stabs-in-elf
524      should be fixed to determine pst->textlow without using this text seg of
525      0 fixup crap. */
526
527   if (strcmp (&objfile->name[val-2], ".o") == 0
528       || strcmp (&objfile->name[val-4], ".nlm") == 0)
529     symfile_relocatable = 1;
530
531   /* This is true for Solaris (and all other systems which put stabs
532      in sections, hopefully, since it would be silly to do things
533      differently from Solaris), and false for SunOS4 and other a.out
534      file formats.  */
535   block_address_function_relative =
536     ((0 == strncmp (bfd_get_target (objfile->obfd), "elf", 3))
537      || (0 == strncmp (bfd_get_target (objfile->obfd), "som", 3))
538      || (0 == strncmp (bfd_get_target (objfile->obfd), "coff", 4))
539      || (0 == strncmp (bfd_get_target (objfile->obfd), "nlm", 3)));
540
541   sym_bfd = objfile->obfd;
542   val = bfd_seek (objfile->obfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
543   if (val < 0)
544     perror_with_name (objfile->name);
545
546   /* If we are reinitializing, or if we have never loaded syms yet, init */
547   if (mainline || objfile->global_psymbols.size == 0 || objfile->static_psymbols.size == 0)
548     init_psymbol_list (objfile);
549
550   symbol_size = DBX_SYMBOL_SIZE (objfile);
551   symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
552
553   pending_blocks = 0;
554   back_to = make_cleanup (really_free_pendings, 0);
555
556   init_minimal_symbol_collection ();
557   make_cleanup (discard_minimal_symbols, 0);
558
559   /* Now that the symbol table data of the executable file are all in core,
560      process them and define symbols accordingly.  */
561
562   read_dbx_symtab (section_offsets, objfile,
563                    bfd_section_vma  (sym_bfd, DBX_TEXT_SECT (objfile)),
564                    bfd_section_size (sym_bfd, DBX_TEXT_SECT (objfile)));
565
566   /* Add the dynamic symbols.  */
567
568   read_dbx_dynamic_symtab (section_offsets, objfile);
569
570   /* Install any minimal symbols that have been collected as the current
571      minimal symbols for this objfile. */
572
573   install_minimal_symbols (objfile);
574
575   do_cleanups (back_to);
576 }
577
578 /* Initialize anything that needs initializing when a completely new
579    symbol file is specified (not just adding some symbols from another
580    file, e.g. a shared library).  */
581
582 static void
583 dbx_new_init (ignore)
584      struct objfile *ignore;
585 {
586   stabsread_new_init ();
587   buildsym_new_init ();
588   init_header_files ();
589 }
590
591
592 /* dbx_symfile_init ()
593    is the dbx-specific initialization routine for reading symbols.
594    It is passed a struct objfile which contains, among other things,
595    the BFD for the file whose symbols are being read, and a slot for a pointer
596    to "private data" which we fill with goodies.
597
598    We read the string table into malloc'd space and stash a pointer to it.
599
600    Since BFD doesn't know how to read debug symbols in a format-independent
601    way (and may never do so...), we have to do it ourselves.  We will never
602    be called unless this is an a.out (or very similar) file. 
603    FIXME, there should be a cleaner peephole into the BFD environment here.  */
604
605 #define DBX_STRINGTAB_SIZE_SIZE sizeof(long)   /* FIXME */
606
607 static void
608 dbx_symfile_init (objfile)
609      struct objfile *objfile;
610 {
611   int val;
612   bfd *sym_bfd = objfile->obfd;
613   char *name = bfd_get_filename (sym_bfd);
614   unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
615
616   /* Allocate struct to keep track of the symfile */
617   objfile->sym_stab_info = (PTR)
618     xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
619
620   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
621 #define STRING_TABLE_OFFSET     (sym_bfd->origin + obj_str_filepos (sym_bfd))
622 #define SYMBOL_TABLE_OFFSET     (sym_bfd->origin + obj_sym_filepos (sym_bfd))
623
624   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
625
626   DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
627   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
628   if (!DBX_TEXT_SECT (objfile))
629     error ("Can't find .text section in symbol file");
630
631   DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
632   DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
633   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
634
635   /* Read the string table and stash it away in the psymbol_obstack.  It is
636      only needed as long as we need to expand psymbols into full symbols,
637      so when we blow away the psymbol the string table goes away as well.
638      Note that gdb used to use the results of attempting to malloc the
639      string table, based on the size it read, as a form of sanity check
640      for botched byte swapping, on the theory that a byte swapped string
641      table size would be so totally bogus that the malloc would fail.  Now
642      that we put in on the psymbol_obstack, we can't do this since gdb gets
643      a fatal error (out of virtual memory) if the size is bogus.  We can
644      however at least check to see if the size is less than the size of
645      the size field itself, or larger than the size of the entire file.
646      Note that all valid string tables have a size greater than zero, since
647      the bytes used to hold the size are included in the count. */
648
649   if (STRING_TABLE_OFFSET == 0)
650     {
651       /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
652          will never be zero, even when there is no string table.  This
653          would appear to be a bug in bfd. */
654       DBX_STRINGTAB_SIZE (objfile) = 0;
655       DBX_STRINGTAB (objfile) = NULL;
656     }
657   else
658     {
659       val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
660       if (val < 0)
661         perror_with_name (name);
662       
663       memset ((PTR) size_temp, 0, sizeof (size_temp));
664       val = bfd_read ((PTR) size_temp, sizeof (size_temp), 1, sym_bfd);
665       if (val < 0)
666         {
667           perror_with_name (name);
668         }
669       else if (val == 0)
670         {
671           /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
672              EOF if there is no string table, and attempting to read the size
673              from EOF will read zero bytes. */
674           DBX_STRINGTAB_SIZE (objfile) = 0;
675           DBX_STRINGTAB (objfile) = NULL;
676         }
677       else
678         {
679           /* Read some data that would appear to be the string table size.
680              If there really is a string table, then it is probably the right
681              size.  Byteswap if necessary and validate the size.  Note that
682              the minimum is DBX_STRINGTAB_SIZE_SIZE.  If we just read some
683              random data that happened to be at STRING_TABLE_OFFSET, because
684              bfd can't tell us there is no string table, the sanity checks may
685              or may not catch this. */
686           DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
687           
688           if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
689               || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
690             error ("ridiculous string table size (%d bytes).",
691                    DBX_STRINGTAB_SIZE (objfile));
692           
693           DBX_STRINGTAB (objfile) =
694             (char *) obstack_alloc (&objfile -> psymbol_obstack,
695                                     DBX_STRINGTAB_SIZE (objfile));
696           
697           /* Now read in the string table in one big gulp.  */
698           
699           val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
700           if (val < 0)
701             perror_with_name (name);
702           val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
703                           sym_bfd);
704           if (val != DBX_STRINGTAB_SIZE (objfile))
705             perror_with_name (name);
706         }
707     }
708 }
709
710 /* Perform any local cleanups required when we are done with a particular
711    objfile.  I.E, we are in the process of discarding all symbol information
712    for an objfile, freeing up all memory held for it, and unlinking the
713    objfile struct from the global list of known objfiles. */
714
715 static void
716 dbx_symfile_finish (objfile)
717      struct objfile *objfile;
718 {
719   if (objfile->sym_stab_info != NULL)
720     {
721       mfree (objfile -> md, objfile->sym_stab_info);
722     }
723   free_header_files ();
724 }
725
726 \f
727 /* Buffer for reading the symbol table entries.  */
728 static struct internal_nlist symbuf[4096];
729 static int symbuf_idx;
730 static int symbuf_end;
731
732 /* Name of last function encountered.  Used in Solaris to approximate
733    object file boundaries.  */
734 static char *last_function_name;
735
736 /* The address in memory of the string table of the object file we are
737    reading (which might not be the "main" object file, but might be a
738    shared library or some other dynamically loaded thing).  This is set
739    by read_dbx_symtab when building psymtabs, and by read_ofile_symtab 
740    when building symtabs, and is used only by next_symbol_text.  */
741 static char *stringtab_global;
742
743 /* Refill the symbol table input buffer
744    and set the variables that control fetching entries from it.
745    Reports an error if no data available.
746    This function can read past the end of the symbol table
747    (into the string table) but this does no harm.  */
748
749 static void
750 fill_symbuf (sym_bfd)
751      bfd *sym_bfd;
752 {
753   int nbytes = bfd_read ((PTR)symbuf, sizeof (symbuf), 1, sym_bfd);
754   if (nbytes < 0)
755     perror_with_name (bfd_get_filename (sym_bfd));
756   else if (nbytes == 0)
757     error ("Premature end of file reading symbol table");
758   symbuf_end = nbytes / symbol_size;
759   symbuf_idx = 0;
760 }
761
762 #define SWAP_SYMBOL(symp, abfd) \
763   { \
764     (symp)->n_strx = bfd_h_get_32(abfd,                 \
765                                 (unsigned char *)&(symp)->n_strx);      \
766     (symp)->n_desc = bfd_h_get_16 (abfd,                        \
767                                 (unsigned char *)&(symp)->n_desc);      \
768     (symp)->n_value = bfd_h_get_32 (abfd,                       \
769                                 (unsigned char *)&(symp)->n_value);     \
770   }
771
772 /* Invariant: The symbol pointed to by symbuf_idx is the first one
773    that hasn't been swapped.  Swap the symbol at the same time
774    that symbuf_idx is incremented.  */
775
776 /* dbx allows the text of a symbol name to be continued into the
777    next symbol name!  When such a continuation is encountered
778    (a \ at the end of the text of a name)
779    call this function to get the continuation.  */
780
781 static char *
782 dbx_next_symbol_text ()
783 {
784   if (symbuf_idx == symbuf_end)
785     fill_symbuf (symfile_bfd);
786   symnum++;
787   SWAP_SYMBOL(&symbuf[symbuf_idx], symfile_bfd);
788   return symbuf[symbuf_idx++].n_strx + stringtab_global
789           + file_string_table_offset;
790 }
791 \f
792 /* Initializes storage for all of the partial symbols that will be
793    created by read_dbx_symtab and subsidiaries.  */
794
795 static void
796 init_psymbol_list (objfile)
797      struct objfile *objfile;
798 {
799   /* Free any previously allocated psymbol lists.  */
800   if (objfile -> global_psymbols.list)
801     mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
802   if (objfile -> static_psymbols.list)
803     mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
804
805   /* Current best guess is that there are approximately a twentieth
806      of the total symbols (in a debugging file) are global or static
807      oriented symbols */
808   objfile -> global_psymbols.size = DBX_SYMCOUNT (objfile) / 10;
809   objfile -> static_psymbols.size = DBX_SYMCOUNT (objfile) / 10;
810   objfile -> global_psymbols.next = objfile -> global_psymbols.list = (struct partial_symbol *)
811     xmmalloc (objfile -> md, objfile -> global_psymbols.size * sizeof (struct partial_symbol));
812   objfile -> static_psymbols.next = objfile -> static_psymbols.list = (struct partial_symbol *)
813     xmmalloc (objfile -> md, objfile -> static_psymbols.size * sizeof (struct partial_symbol));
814 }
815
816 /* Initialize the list of bincls to contain none and have some
817    allocated.  */
818
819 static void
820 init_bincl_list (number, objfile)
821      int number;
822      struct objfile *objfile;
823 {
824   bincls_allocated = number;
825   next_bincl = bincl_list = (struct header_file_location *)
826     xmmalloc (objfile -> md, bincls_allocated * sizeof(struct header_file_location));
827 }
828
829 /* Add a bincl to the list.  */
830
831 static void
832 add_bincl_to_list (pst, name, instance)
833      struct partial_symtab *pst;
834      char *name;
835      int instance;
836 {
837   if (next_bincl >= bincl_list + bincls_allocated)
838     {
839       int offset = next_bincl - bincl_list;
840       bincls_allocated *= 2;
841       bincl_list = (struct header_file_location *)
842         xmrealloc (pst->objfile->md, (char *)bincl_list,
843                   bincls_allocated * sizeof (struct header_file_location));
844       next_bincl = bincl_list + offset;
845     }
846   next_bincl->pst = pst;
847   next_bincl->instance = instance;
848   next_bincl++->name = name;
849 }
850
851 /* Given a name, value pair, find the corresponding
852    bincl in the list.  Return the partial symtab associated
853    with that header_file_location.  */
854
855 static struct partial_symtab *
856 find_corresponding_bincl_psymtab (name, instance)
857      char *name;
858      int instance;
859 {
860   struct header_file_location *bincl;
861
862   for (bincl = bincl_list; bincl < next_bincl; bincl++)
863     if (bincl->instance == instance
864         && STREQ (name, bincl->name))
865       return bincl->pst;
866
867   complain (&repeated_header_complaint, name, symnum);
868   return (struct partial_symtab *) 0;
869 }
870
871 /* Free the storage allocated for the bincl list.  */
872
873 static void
874 free_bincl_list (objfile)
875      struct objfile *objfile;
876 {
877   mfree (objfile -> md, (PTR)bincl_list);
878   bincls_allocated = 0;
879 }
880
881 /* Scan a SunOs dynamic symbol table for symbols of interest and
882    add them to the minimal symbol table.  */
883
884 static void
885 read_dbx_dynamic_symtab (section_offsets, objfile)
886      struct section_offsets *section_offsets;
887      struct objfile *objfile;
888 {
889   bfd *abfd = objfile->obfd;
890   struct cleanup *back_to;
891   int counter;
892   long dynsym_size;
893   long dynsym_count;
894   asymbol **dynsyms;
895   asymbol **symptr;
896   arelent **relptr;
897   long dynrel_size;
898   long dynrel_count;
899   arelent **dynrels;
900   CORE_ADDR sym_value;
901   char *name;
902
903   /* Check that the symbol file has dynamic symbols that we know about.
904      bfd_arch_unknown can happen if we are reading a sun3 symbol file
905      on a sun4 host (and vice versa) and bfd is not configured
906      --with-target=all.  This would trigger an assertion in bfd/sunos.c,
907      so we ignore the dynamic symbols in this case.  */
908   if (bfd_get_flavour (abfd) != bfd_target_aout_flavour
909       || (bfd_get_file_flags (abfd) & DYNAMIC) == 0
910       || bfd_get_arch (abfd) == bfd_arch_unknown)
911     return;
912
913   dynsym_size = bfd_get_dynamic_symtab_upper_bound (abfd);
914   if (dynsym_size < 0)
915     return;
916
917   dynsyms = (asymbol **) xmalloc (dynsym_size);
918   back_to = make_cleanup (free, dynsyms);
919
920   dynsym_count = bfd_canonicalize_dynamic_symtab (abfd, dynsyms);
921   if (dynsym_count < 0)
922     {
923       do_cleanups (back_to);
924       return;
925     }
926
927   /* Enter dynamic symbols into the minimal symbol table
928      if this is a stripped executable.  */
929   if (bfd_get_symcount (abfd) <= 0)
930     {
931       symptr = dynsyms;
932       for (counter = 0; counter < dynsym_count; counter++, symptr++)
933         {
934           asymbol *sym = *symptr;
935           asection *sec;
936           int type;
937
938           sec = bfd_get_section (sym);
939
940           /* BFD symbols are section relative.  */
941           sym_value = sym->value + sec->vma;
942
943           if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
944             {
945               sym_value += ANOFFSET (section_offsets, SECT_OFF_TEXT);
946               type = N_TEXT;
947             }
948           else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
949             {
950               sym_value += ANOFFSET (section_offsets, SECT_OFF_DATA);
951               type = N_DATA;
952             }
953           else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
954             {
955               sym_value += ANOFFSET (section_offsets, SECT_OFF_BSS);
956               type = N_BSS;
957             }
958           else
959             continue;
960
961           if (sym->flags & BSF_GLOBAL)
962             type |= N_EXT;
963
964           record_minimal_symbol ((char *) bfd_asymbol_name (sym), sym_value,
965                                  type, objfile);
966         }
967     }
968
969   /* Symbols from shared libraries have a dynamic relocation entry
970      that points to the associated slot in the procedure linkage table.
971      We make a mininal symbol table entry with type mst_solib_trampoline
972      at the address in the procedure linkage table.  */
973   dynrel_size = bfd_get_dynamic_reloc_upper_bound (abfd);
974   if (dynrel_size < 0)
975     {
976       do_cleanups (back_to);
977       return;
978     }
979   
980   dynrels = (arelent **) xmalloc (dynrel_size);
981   make_cleanup (free, dynrels);
982
983   dynrel_count = bfd_canonicalize_dynamic_reloc (abfd, dynrels, dynsyms);
984   if (dynrel_count < 0)
985     {
986       do_cleanups (back_to);
987       return;
988     }
989
990   for (counter = 0, relptr = dynrels;
991        counter < dynrel_count;
992        counter++, relptr++)
993     {
994       arelent *rel = *relptr;
995       CORE_ADDR address =
996         rel->address + ANOFFSET (section_offsets, SECT_OFF_DATA);
997
998       switch (bfd_get_arch (abfd))
999         {
1000         case bfd_arch_sparc:
1001           if (rel->howto->type != RELOC_JMP_SLOT)
1002             continue;
1003           break;
1004         case bfd_arch_m68k:
1005           /* `16' is the type BFD produces for a jump table relocation.  */
1006           if (rel->howto->type != 16)
1007             continue;
1008
1009           /* Adjust address in the jump table to point to
1010              the start of the bsr instruction.  */
1011           address -= 2;
1012           break;
1013         default:
1014           continue;
1015         }
1016
1017       name = (char *) bfd_asymbol_name (*rel->sym_ptr_ptr);
1018       prim_record_minimal_symbol
1019         (obsavestring (name, strlen (name), &objfile -> symbol_obstack),
1020          address,
1021          mst_solib_trampoline,
1022          objfile);
1023     }
1024
1025   do_cleanups (back_to);
1026 }
1027
1028 /* Given pointers to an a.out symbol table in core containing dbx
1029    style data, setup partial_symtab's describing each source file for
1030    which debugging information is available.
1031    SYMFILE_NAME is the name of the file we are reading from
1032    and SECTION_OFFSETS is the set of offsets for the various sections
1033    of the file (a set of zeros if the mainline program).  */
1034
1035 static void
1036 read_dbx_symtab (section_offsets, objfile, text_addr, text_size)
1037      struct section_offsets *section_offsets;
1038      struct objfile *objfile;
1039      CORE_ADDR text_addr;
1040      int text_size;
1041 {
1042   register struct internal_nlist *bufp = 0;     /* =0 avoids gcc -Wall glitch */
1043   register char *namestring;
1044   int nsl;
1045   int past_first_source_file = 0;
1046   CORE_ADDR last_o_file_start = 0;
1047   struct cleanup *back_to;
1048   bfd *abfd;
1049
1050   /* Current partial symtab */
1051   struct partial_symtab *pst;
1052
1053   /* List of current psymtab's include files */
1054   char **psymtab_include_list;
1055   int includes_allocated;
1056   int includes_used;
1057
1058   /* Index within current psymtab dependency list */
1059   struct partial_symtab **dependency_list;
1060   int dependencies_used, dependencies_allocated;
1061
1062   /* FIXME.  We probably want to change stringtab_global rather than add this
1063      while processing every symbol entry.  FIXME.  */
1064   file_string_table_offset = 0;
1065   next_file_string_table_offset = 0;
1066
1067   stringtab_global = DBX_STRINGTAB (objfile);
1068   
1069   pst = (struct partial_symtab *) 0;
1070
1071   includes_allocated = 30;
1072   includes_used = 0;
1073   psymtab_include_list = (char **) alloca (includes_allocated *
1074                                            sizeof (char *));
1075
1076   dependencies_allocated = 30;
1077   dependencies_used = 0;
1078   dependency_list =
1079     (struct partial_symtab **) alloca (dependencies_allocated *
1080                                        sizeof (struct partial_symtab *));
1081
1082   /* Init bincl list */
1083   init_bincl_list (20, objfile);
1084   back_to = make_cleanup (free_bincl_list, objfile);
1085
1086   last_source_file = NULL;
1087
1088   lowest_text_address = (CORE_ADDR)-1;
1089
1090   symfile_bfd = objfile->obfd;  /* For next_text_symbol */
1091   abfd = objfile->obfd;
1092   symbuf_end = symbuf_idx = 0;
1093   next_symbol_text_func = dbx_next_symbol_text;
1094
1095   for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
1096     {
1097       /* Get the symbol for this run and pull out some info */
1098       QUIT;     /* allow this to be interruptable */
1099       if (symbuf_idx == symbuf_end)
1100         fill_symbuf (abfd);
1101       bufp = &symbuf[symbuf_idx++];
1102
1103       /*
1104        * Special case to speed up readin.
1105        */
1106       if (bufp->n_type == (unsigned char)N_SLINE) continue;
1107
1108       SWAP_SYMBOL (bufp, abfd);
1109
1110       /* Ok.  There is a lot of code duplicated in the rest of this
1111          switch statement (for efficiency reasons).  Since I don't
1112          like duplicating code, I will do my penance here, and
1113          describe the code which is duplicated:
1114
1115          *) The assignment to namestring.
1116          *) The call to strchr.
1117          *) The addition of a partial symbol the the two partial
1118             symbol lists.  This last is a large section of code, so
1119             I've imbedded it in the following macro.
1120          */
1121       
1122 /* Set namestring based on bufp.  If the string table index is invalid, 
1123    give a fake name, and print a single error message per symbol file read,
1124    rather than abort the symbol reading or flood the user with messages.  */
1125
1126 /*FIXME: Too many adds and indirections in here for the inner loop.  */
1127 #define SET_NAMESTRING()\
1128   if (((unsigned)bufp->n_strx + file_string_table_offset) >=            \
1129       DBX_STRINGTAB_SIZE (objfile)) {                                   \
1130     complain (&string_table_offset_complaint, symnum);                  \
1131     namestring = "<bad string table offset>";                           \
1132   } else                                                                \
1133     namestring = bufp->n_strx + file_string_table_offset +              \
1134                  DBX_STRINGTAB (objfile)
1135
1136 #define CUR_SYMBOL_TYPE bufp->n_type
1137 #define CUR_SYMBOL_VALUE bufp->n_value
1138 #define DBXREAD_ONLY
1139 #define START_PSYMTAB(ofile,secoff,fname,low,symoff,global_syms,static_syms)\
1140   start_psymtab(ofile, secoff, fname, low, symoff, global_syms, static_syms)
1141 #define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)\
1142   end_psymtab(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)
1143
1144 #include "partial-stab.h"
1145     }
1146
1147   /* If there's stuff to be cleaned up, clean it up.  */
1148   if (DBX_SYMCOUNT (objfile) > 0                        /* We have some syms */
1149 /*FIXME, does this have a bug at start address 0? */
1150       && last_o_file_start
1151       && objfile -> ei.entry_point < bufp->n_value
1152       && objfile -> ei.entry_point >= last_o_file_start)
1153     {
1154       objfile -> ei.entry_file_lowpc = last_o_file_start;
1155       objfile -> ei.entry_file_highpc = bufp->n_value;
1156     }
1157
1158   if (pst)
1159     {
1160       end_psymtab (pst, psymtab_include_list, includes_used,
1161                    symnum * symbol_size,
1162                    (lowest_text_address == (CORE_ADDR)-1
1163                     ? (text_addr + section_offsets->offsets[SECT_OFF_TEXT])
1164                     : lowest_text_address)
1165                    + text_size,
1166                    dependency_list, dependencies_used);
1167     }
1168
1169   do_cleanups (back_to);
1170 }
1171
1172 /* Allocate and partially fill a partial symtab.  It will be
1173    completely filled at the end of the symbol list.
1174
1175    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1176    is the address relative to which its symbols are (incremental) or 0
1177    (normal). */
1178
1179
1180 struct partial_symtab *
1181 start_psymtab (objfile, section_offsets,
1182                filename, textlow, ldsymoff, global_syms, static_syms)
1183      struct objfile *objfile;
1184      struct section_offsets *section_offsets;
1185      char *filename;
1186      CORE_ADDR textlow;
1187      int ldsymoff;
1188      struct partial_symbol *global_syms;
1189      struct partial_symbol *static_syms;
1190 {
1191   struct partial_symtab *result =
1192       start_psymtab_common(objfile, section_offsets,
1193                            filename, textlow, global_syms, static_syms);
1194
1195   result->read_symtab_private = (char *)
1196     obstack_alloc (&objfile -> psymbol_obstack, sizeof (struct symloc));
1197   LDSYMOFF(result) = ldsymoff;
1198   result->read_symtab = dbx_psymtab_to_symtab;
1199   SYMBOL_SIZE(result) = symbol_size;
1200   SYMBOL_OFFSET(result) = symbol_table_offset;
1201   STRING_OFFSET(result) = string_table_offset;
1202   FILE_STRING_OFFSET(result) = file_string_table_offset;
1203
1204   /* If we're handling an ELF file, drag some section-relocation info
1205      for this source file out of the ELF symbol table, to compensate for
1206      Sun brain death.  This replaces the section_offsets in this psymtab,
1207      if successful.  */
1208   elfstab_offset_sections (objfile, result);
1209
1210   /* Deduce the source language from the filename for this psymtab. */
1211   psymtab_language = deduce_language_from_filename (filename);
1212
1213   return result;
1214 }
1215
1216 /* Close off the current usage of PST.  
1217    Returns PST or NULL if the partial symtab was empty and thrown away.
1218
1219    FIXME:  List variables and peculiarities of same.  */
1220
1221 struct partial_symtab *
1222 end_psymtab (pst, include_list, num_includes, capping_symbol_offset,
1223              capping_text, dependency_list, number_dependencies)
1224      struct partial_symtab *pst;
1225      char **include_list;
1226      int num_includes;
1227      int capping_symbol_offset;
1228      CORE_ADDR capping_text;
1229      struct partial_symtab **dependency_list;
1230      int number_dependencies;
1231 {
1232   int i;
1233   struct objfile *objfile = pst -> objfile;
1234
1235   if (capping_symbol_offset != -1)
1236       LDSYMLEN(pst) = capping_symbol_offset - LDSYMOFF(pst);
1237   pst->texthigh = capping_text;
1238
1239 #ifdef SOFUN_ADDRESS_MAYBE_MISSING
1240   /* Under Solaris, the N_SO symbols always have a value of 0,
1241      instead of the usual address of the .o file.  Therefore,
1242      we have to do some tricks to fill in texthigh and textlow.
1243      The first trick is in partial-stab.h: if we see a static
1244      or global function, and the textlow for the current pst
1245      is still 0, then we use that function's address for 
1246      the textlow of the pst.  */
1247
1248   /* Now, to fill in texthigh, we remember the last function seen
1249      in the .o file (also in partial-stab.h).  Also, there's a hack in
1250      bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
1251      to here via the misc_info field.  Therefore, we can fill in
1252      a reliable texthigh by taking the address plus size of the
1253      last function in the file.  */
1254
1255   if (pst->texthigh == 0 && last_function_name) {
1256     char *p;
1257     int n;
1258     struct minimal_symbol *minsym;
1259
1260     p = strchr (last_function_name, ':');
1261     if (p == NULL)
1262       p = last_function_name;
1263     n = p - last_function_name;
1264     p = alloca (n + 1);
1265     strncpy (p, last_function_name, n);
1266     p[n] = 0;
1267     
1268     minsym = lookup_minimal_symbol (p, pst->filename, objfile);
1269
1270     if (minsym)
1271       pst->texthigh = SYMBOL_VALUE_ADDRESS (minsym) +
1272         (long) MSYMBOL_INFO (minsym);
1273
1274     last_function_name = NULL;
1275   }
1276
1277   /* this test will be true if the last .o file is only data */
1278   if (pst->textlow == 0)
1279     /* This loses if the text section really starts at address zero
1280        (generally true when we are debugging a .o file, for example).
1281        That is why this whole thing is inside SOFUN_ADDRESS_MAYBE_MISSING.  */
1282     pst->textlow = pst->texthigh;
1283
1284   /* If we know our own starting text address, then walk through all other
1285      psymtabs for this objfile, and if any didn't know their ending text
1286      address, set it to our starting address.  Take care to not set our
1287      own ending address to our starting address, nor to set addresses on
1288      `dependency' files that have both textlow and texthigh zero.  */
1289   if (pst->textlow) {
1290     struct partial_symtab *p1;
1291
1292     ALL_OBJFILE_PSYMTABS (objfile, p1) {
1293       if (p1->texthigh == 0  && p1->textlow != 0 && p1 != pst) {
1294         p1->texthigh = pst->textlow;
1295         /* if this file has only data, then make textlow match texthigh */
1296         if (p1->textlow == 0)
1297           p1->textlow = p1->texthigh;
1298       }
1299     }
1300   }
1301
1302   /* End of kludge for patching Solaris textlow and texthigh.  */
1303 #endif /* SOFUN_ADDRESS_MAYBE_MISSING.  */
1304
1305   pst->n_global_syms =
1306     objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
1307   pst->n_static_syms =
1308     objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
1309
1310   pst->number_of_dependencies = number_dependencies;
1311   if (number_dependencies)
1312     {
1313       pst->dependencies = (struct partial_symtab **)
1314         obstack_alloc (&objfile->psymbol_obstack,
1315                        number_dependencies * sizeof (struct partial_symtab *));
1316       memcpy (pst->dependencies, dependency_list,
1317              number_dependencies * sizeof (struct partial_symtab *));
1318     }
1319   else
1320     pst->dependencies = 0;
1321
1322   for (i = 0; i < num_includes; i++)
1323     {
1324       struct partial_symtab *subpst =
1325         allocate_psymtab (include_list[i], objfile);
1326
1327       subpst->section_offsets = pst->section_offsets;
1328       subpst->read_symtab_private =
1329           (char *) obstack_alloc (&objfile->psymbol_obstack,
1330                                   sizeof (struct symloc));
1331       LDSYMOFF(subpst) =
1332         LDSYMLEN(subpst) =
1333           subpst->textlow =
1334             subpst->texthigh = 0;
1335
1336       /* We could save slight bits of space by only making one of these,
1337          shared by the entire set of include files.  FIXME-someday.  */
1338       subpst->dependencies = (struct partial_symtab **)
1339         obstack_alloc (&objfile->psymbol_obstack,
1340                        sizeof (struct partial_symtab *));
1341       subpst->dependencies[0] = pst;
1342       subpst->number_of_dependencies = 1;
1343
1344       subpst->globals_offset =
1345         subpst->n_global_syms =
1346           subpst->statics_offset =
1347             subpst->n_static_syms = 0;
1348
1349       subpst->readin = 0;
1350       subpst->symtab = 0;
1351       subpst->read_symtab = pst->read_symtab;
1352     }
1353
1354   sort_pst_symbols (pst);
1355
1356   /* If there is already a psymtab or symtab for a file of this name, remove it.
1357      (If there is a symtab, more drastic things also happen.)
1358      This happens in VxWorks.  */
1359   free_named_symtabs (pst->filename);
1360
1361   if (num_includes == 0
1362       && number_dependencies == 0
1363       && pst->n_global_syms == 0
1364       && pst->n_static_syms == 0)
1365     {
1366       /* Throw away this psymtab, it's empty.  We can't deallocate it, since
1367          it is on the obstack, but we can forget to chain it on the list.  */
1368       /* Empty psymtabs happen as a result of header files which don't have
1369          any symbols in them.  There can be a lot of them.  But this check
1370          is wrong, in that a psymtab with N_SLINE entries but nothing else
1371          is not empty, but we don't realize that.  Fixing that without slowing
1372          things down might be tricky.  */
1373       struct partial_symtab *prev_pst;
1374
1375       /* First, snip it out of the psymtab chain */
1376
1377       if (pst->objfile->psymtabs == pst)
1378         pst->objfile->psymtabs = pst->next;
1379       else
1380         for (prev_pst = pst->objfile->psymtabs; prev_pst; prev_pst = pst->next)
1381           if (prev_pst->next == pst)
1382             prev_pst->next = pst->next;
1383
1384       /* Next, put it on a free list for recycling */
1385
1386       pst->next = pst->objfile->free_psymtabs;
1387       pst->objfile->free_psymtabs = pst;
1388
1389       /* Indicate that psymtab was thrown away.  */
1390       pst = (struct partial_symtab *)NULL;
1391     }
1392   return pst;
1393 }
1394 \f
1395 static void
1396 dbx_psymtab_to_symtab_1 (pst)
1397      struct partial_symtab *pst;
1398 {
1399   struct cleanup *old_chain;
1400   int i;
1401   
1402   if (!pst)
1403     return;
1404
1405   if (pst->readin)
1406     {
1407       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1408                pst->filename);
1409       return;
1410     }
1411
1412   /* Read in all partial symtabs on which this one is dependent */
1413   for (i = 0; i < pst->number_of_dependencies; i++)
1414     if (!pst->dependencies[i]->readin)
1415       {
1416         /* Inform about additional files that need to be read in.  */
1417         if (info_verbose)
1418           {
1419             fputs_filtered (" ", gdb_stdout);
1420             wrap_here ("");
1421             fputs_filtered ("and ", gdb_stdout);
1422             wrap_here ("");
1423             printf_filtered ("%s...", pst->dependencies[i]->filename);
1424             wrap_here ("");             /* Flush output */
1425             gdb_flush (gdb_stdout);
1426           }
1427         dbx_psymtab_to_symtab_1 (pst->dependencies[i]);
1428       }
1429
1430   if (LDSYMLEN(pst))            /* Otherwise it's a dummy */
1431     {
1432       /* Init stuff necessary for reading in symbols */
1433       stabsread_init ();
1434       buildsym_init ();
1435       old_chain = make_cleanup (really_free_pendings, 0);
1436       file_string_table_offset = FILE_STRING_OFFSET (pst);
1437       symbol_size = SYMBOL_SIZE (pst);
1438
1439       /* Read in this file's symbols */
1440       bfd_seek (pst->objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
1441       read_ofile_symtab (pst);
1442       sort_symtab_syms (pst->symtab);
1443
1444       do_cleanups (old_chain);
1445     }
1446
1447   pst->readin = 1;
1448 }
1449
1450 /* Read in all of the symbols for a given psymtab for real.
1451    Be verbose about it if the user wants that.  */
1452
1453 static void
1454 dbx_psymtab_to_symtab (pst)
1455      struct partial_symtab *pst;
1456 {
1457   bfd *sym_bfd;
1458
1459   if (!pst)
1460     return;
1461
1462   if (pst->readin)
1463     {
1464       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1465                pst->filename);
1466       return;
1467     }
1468
1469   if (LDSYMLEN(pst) || pst->number_of_dependencies)
1470     {
1471       /* Print the message now, before reading the string table,
1472          to avoid disconcerting pauses.  */
1473       if (info_verbose)
1474         {
1475           printf_filtered ("Reading in symbols for %s...", pst->filename);
1476           gdb_flush (gdb_stdout);
1477         }
1478
1479       sym_bfd = pst->objfile->obfd;
1480
1481       next_symbol_text_func = dbx_next_symbol_text;
1482
1483       dbx_psymtab_to_symtab_1 (pst);
1484
1485       /* Match with global symbols.  This only needs to be done once,
1486          after all of the symtabs and dependencies have been read in.   */
1487       scan_file_globals (pst->objfile);
1488
1489       /* Finish up the debug error message.  */
1490       if (info_verbose)
1491         printf_filtered ("done.\n");
1492     }
1493 }
1494
1495 /* Read in a defined section of a specific object file's symbols. */
1496   
1497 static void
1498 read_ofile_symtab (pst)
1499      struct partial_symtab *pst;
1500 {
1501   register char *namestring;
1502   register struct internal_nlist *bufp;
1503   unsigned char type;
1504   unsigned max_symnum;
1505   register bfd *abfd;
1506   struct objfile *objfile;
1507   int sym_offset;               /* Offset to start of symbols to read */
1508   int sym_size;                 /* Size of symbols to read */
1509   CORE_ADDR text_offset;        /* Start of text segment for symbols */
1510   int text_size;                /* Size of text segment for symbols */
1511   struct section_offsets *section_offsets;
1512
1513   objfile = pst->objfile;
1514   sym_offset = LDSYMOFF(pst);
1515   sym_size = LDSYMLEN(pst);
1516   text_offset = pst->textlow;
1517   text_size = pst->texthigh - pst->textlow;
1518   section_offsets = pst->section_offsets;
1519
1520   current_objfile = objfile;
1521   subfile_stack = NULL;
1522
1523   stringtab_global = DBX_STRINGTAB (objfile);
1524   last_source_file = NULL;
1525
1526   abfd = objfile->obfd;
1527   symfile_bfd = objfile->obfd;  /* Implicit param to next_text_symbol */
1528   symbuf_end = symbuf_idx = 0;
1529
1530   /* It is necessary to actually read one symbol *before* the start
1531      of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
1532      occurs before the N_SO symbol.
1533
1534      Detecting this in read_dbx_symtab
1535      would slow down initial readin, so we look for it here instead.  */
1536   if (!processing_acc_compilation && sym_offset >= (int)symbol_size)
1537     {
1538       bfd_seek (symfile_bfd, sym_offset - symbol_size, SEEK_CUR);
1539       fill_symbuf (abfd);
1540       bufp = &symbuf[symbuf_idx++];
1541       SWAP_SYMBOL (bufp, abfd);
1542
1543       SET_NAMESTRING ();
1544
1545       processing_gcc_compilation = 0;
1546       if (bufp->n_type == N_TEXT)
1547         {
1548           const char *tempstring = namestring;
1549
1550           if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1551             processing_gcc_compilation = 1;
1552           else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1553             processing_gcc_compilation = 2;
1554           if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
1555             ++tempstring;
1556           if (STREQN (tempstring, "__gnu_compiled", 14))
1557             processing_gcc_compilation = 2;
1558         }
1559
1560       /* Try to select a C++ demangling based on the compilation unit
1561          producer. */
1562
1563       if (processing_gcc_compilation)
1564         {
1565           if (AUTO_DEMANGLING)
1566             {
1567               set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1568             }
1569         }
1570     }
1571   else
1572     {
1573       /* The N_SO starting this symtab is the first symbol, so we
1574          better not check the symbol before it.  I'm not this can
1575          happen, but it doesn't hurt to check for it.  */
1576       bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
1577       processing_gcc_compilation = 0;
1578     }
1579
1580   if (symbuf_idx == symbuf_end)
1581     fill_symbuf (abfd);
1582   bufp = &symbuf[symbuf_idx];
1583   if (bufp->n_type != (unsigned char)N_SO)
1584     error("First symbol in segment of executable not a source symbol");
1585
1586   max_symnum = sym_size / symbol_size;
1587
1588   for (symnum = 0;
1589        symnum < max_symnum;
1590        symnum++)
1591     {
1592       QUIT;                     /* Allow this to be interruptable */
1593       if (symbuf_idx == symbuf_end)
1594         fill_symbuf(abfd);
1595       bufp = &symbuf[symbuf_idx++];
1596       SWAP_SYMBOL (bufp, abfd);
1597
1598       type = bufp->n_type;
1599
1600       SET_NAMESTRING ();
1601
1602       if (type & N_STAB) {
1603           process_one_symbol (type, bufp->n_desc, bufp->n_value,
1604                               namestring, section_offsets, objfile);
1605       }
1606       /* We skip checking for a new .o or -l file; that should never
1607          happen in this routine. */
1608       else if (type == N_TEXT)
1609         {
1610           /* I don't think this code will ever be executed, because
1611              the GCC_COMPILED_FLAG_SYMBOL usually is right before
1612              the N_SO symbol which starts this source file.
1613              However, there is no reason not to accept
1614              the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
1615
1616           if (STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
1617             processing_gcc_compilation = 1;
1618           else if (STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
1619             processing_gcc_compilation = 2;
1620
1621           if (AUTO_DEMANGLING)
1622             {
1623               set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
1624             }
1625         }
1626       else if (type & N_EXT || type == (unsigned char)N_TEXT
1627                || type == (unsigned char)N_NBTEXT
1628                ) {
1629           /* Global symbol: see if we came across a dbx defintion for
1630              a corresponding symbol.  If so, store the value.  Remove
1631              syms from the chain when their values are stored, but
1632              search the whole chain, as there may be several syms from
1633              different files with the same name. */
1634           /* This is probably not true.  Since the files will be read
1635              in one at a time, each reference to a global symbol will
1636              be satisfied in each file as it appears. So we skip this
1637              section. */
1638           ;
1639         }
1640     }
1641
1642   current_objfile = NULL;
1643
1644   /* In a Solaris elf file, this variable, which comes from the
1645      value of the N_SO symbol, will still be 0.  Luckily, text_offset,
1646      which comes from pst->textlow is correct. */
1647   if (last_source_start_addr == 0)
1648     last_source_start_addr = text_offset;
1649
1650   pst->symtab = end_symtab (text_offset + text_size, 0, 0, objfile,
1651                             SECT_OFF_TEXT);
1652   end_stabs ();
1653 }
1654
1655 \f
1656 /* This handles a single symbol from the symbol-file, building symbols
1657    into a GDB symtab.  It takes these arguments and an implicit argument.
1658
1659    TYPE is the type field of the ".stab" symbol entry.
1660    DESC is the desc field of the ".stab" entry.
1661    VALU is the value field of the ".stab" entry.
1662    NAME is the symbol name, in our address space.
1663    SECTION_OFFSETS is a set of amounts by which the sections of this object
1664           file were relocated when it was loaded into memory.
1665           All symbols that refer
1666           to memory locations need to be offset by these amounts.
1667    OBJFILE is the object file from which we are reading symbols.
1668                It is used in end_symtab.  */
1669
1670 void
1671 process_one_symbol (type, desc, valu, name, section_offsets, objfile)
1672      int type, desc;
1673      CORE_ADDR valu;
1674      char *name;
1675      struct section_offsets *section_offsets;
1676      struct objfile *objfile;
1677 {
1678 #ifdef SUN_FIXED_LBRAC_BUG
1679   /* If SUN_FIXED_LBRAC_BUG is defined, then it tells us whether we need
1680      to correct the address of N_LBRAC's.  If it is not defined, then
1681      we never need to correct the addresses.  */
1682
1683   /* This records the last pc address we've seen.  We depend on there being
1684      an SLINE or FUN or SO before the first LBRAC, since the variable does
1685      not get reset in between reads of different symbol files.  */
1686   static CORE_ADDR last_pc_address;
1687 #endif
1688
1689   register struct context_stack *new;
1690   /* This remembers the address of the start of a function.  It is used
1691      because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries are
1692      relative to the current function's start address.  On systems
1693      other than Solaris 2, this just holds the SECT_OFF_TEXT value, and is
1694      used to relocate these symbol types rather than SECTION_OFFSETS.  */
1695   static CORE_ADDR function_start_offset;
1696
1697   /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this source
1698      file.  Used to detect the SunPRO solaris compiler.  */
1699   static int n_opt_found;
1700
1701   /* The stab type used for the definition of the last function.
1702      N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers.  */
1703   static int function_stab_type = 0;
1704
1705   if (!block_address_function_relative)
1706     /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
1707        function start address, so just use the text offset.  */
1708     function_start_offset = ANOFFSET (section_offsets, SECT_OFF_TEXT);
1709
1710   /* Something is wrong if we see real data before
1711      seeing a source file name.  */
1712
1713   if (last_source_file == NULL && type != (unsigned char)N_SO)
1714     {
1715       /* Ignore any symbols which appear before an N_SO symbol.  Currently
1716          no one puts symbols there, but we should deal gracefully with the
1717          case.  A complain()t might be in order (if !IGNORE_SYMBOL (type)),
1718          but this should not be an error ().  */
1719       return;
1720     }
1721
1722   switch (type)
1723     {
1724     case N_FUN:
1725     case N_FNAME:
1726       /* Relocate for dynamic loading */
1727       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1728       goto define_a_symbol;
1729
1730     case N_LBRAC:
1731       /* This "symbol" just indicates the start of an inner lexical
1732          context within a function.  */
1733
1734       /* Ignore extra outermost context from SunPRO cc and acc.  */
1735       if (n_opt_found && desc == 1)
1736         break;
1737
1738 #if defined(BLOCK_ADDRESS_ABSOLUTE)
1739       /* Relocate for dynamic loading (?).  */
1740       valu += function_start_offset;
1741 #else
1742       if (block_address_function_relative)
1743         /* Relocate for Sun ELF acc fn-relative syms.  */
1744         valu += function_start_offset;
1745       else
1746         /* On most machines, the block addresses are relative to the
1747            N_SO, the linker did not relocate them (sigh).  */
1748         valu += last_source_start_addr;
1749 #endif
1750
1751 #ifdef SUN_FIXED_LBRAC_BUG
1752       if (!SUN_FIXED_LBRAC_BUG && valu < last_pc_address) {
1753         /* Patch current LBRAC pc value to match last handy pc value */
1754         complain (&lbrac_complaint);
1755         valu = last_pc_address;
1756       }
1757 #endif
1758       new = push_context (desc, valu);
1759       break;
1760
1761     case N_RBRAC:
1762       /* This "symbol" just indicates the end of an inner lexical
1763          context that was started with N_LBRAC.  */
1764
1765       /* Ignore extra outermost context from SunPRO cc and acc.  */
1766       if (n_opt_found && desc == 1)
1767         break;
1768
1769 #if defined(BLOCK_ADDRESS_ABSOLUTE)
1770       /* Relocate for dynamic loading (?).  */
1771       valu += function_start_offset;
1772 #else
1773       if (block_address_function_relative)
1774         /* Relocate for Sun ELF acc fn-relative syms.  */
1775         valu += function_start_offset;
1776       else
1777         /* On most machines, the block addresses are relative to the
1778            N_SO, the linker did not relocate them (sigh).  */
1779         valu += last_source_start_addr;
1780 #endif
1781
1782       new = pop_context();
1783       if (desc != new->depth)
1784         complain (&lbrac_mismatch_complaint, symnum);
1785
1786       /* Some compilers put the variable decls inside of an
1787          LBRAC/RBRAC block.  This macro should be nonzero if this
1788          is true.  DESC is N_DESC from the N_RBRAC symbol.
1789          GCC_P is true if we've detected the GCC_COMPILED_SYMBOL
1790          or the GCC2_COMPILED_SYMBOL.  */
1791 #if !defined (VARIABLES_INSIDE_BLOCK)
1792 #define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
1793 #endif
1794
1795       /* Can only use new->locals as local symbols here if we're in
1796          gcc or on a machine that puts them before the lbrack.  */
1797       if (!VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1798         local_symbols = new->locals;
1799
1800       if (context_stack_depth
1801           > !VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1802         {
1803           /* This is not the outermost LBRAC...RBRAC pair in the function,
1804              its local symbols preceded it, and are the ones just recovered
1805              from the context stack.  Define the block for them (but don't
1806              bother if the block contains no symbols.  Should we complain
1807              on blocks without symbols?  I can't think of any useful purpose
1808              for them).  */
1809           if (local_symbols != NULL)
1810             {
1811               /* Muzzle a compiler bug that makes end < start.  (which
1812                  compilers?  Is this ever harmful?).  */
1813               if (new->start_addr > valu)
1814                 {
1815                   complain (&lbrac_rbrac_complaint);
1816                   new->start_addr = valu;
1817                 }
1818               /* Make a block for the local symbols within.  */
1819               finish_block (0, &local_symbols, new->old_blocks,
1820                             new->start_addr, valu, objfile);
1821             }
1822         }
1823       else
1824         {
1825           /* This is the outermost LBRAC...RBRAC pair.  There is no
1826              need to do anything; leave the symbols that preceded it
1827              to be attached to the function's own block.  We need to
1828              indicate that we just moved outside of the function.  */
1829           within_function = 0;
1830         }
1831
1832       if (VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1833         /* Now pop locals of block just finished.  */
1834         local_symbols = new->locals;
1835       break;
1836
1837     case N_FN:
1838     case N_FN_SEQ:
1839       /* This kind of symbol indicates the start of an object file.  */
1840       /* Relocate for dynamic loading */
1841       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1842       break;
1843
1844     case N_SO:
1845       /* This type of symbol indicates the start of data
1846          for one source file.
1847          Finish the symbol table of the previous source file
1848          (if any) and start accumulating a new symbol table.  */
1849       /* Relocate for dynamic loading */
1850       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1851
1852       n_opt_found = 0;
1853
1854 #ifdef SUN_FIXED_LBRAC_BUG
1855       last_pc_address = valu;   /* Save for SunOS bug circumcision */
1856 #endif
1857
1858 #ifdef PCC_SOL_BROKEN
1859       /* pcc bug, occasionally puts out SO for SOL.  */
1860       if (context_stack_depth > 0)
1861         {
1862           start_subfile (name, NULL);
1863           break;
1864         }
1865 #endif
1866       if (last_source_file)
1867         {
1868           /* Check if previous symbol was also an N_SO (with some
1869              sanity checks).  If so, that one was actually the directory
1870              name, and the current one is the real file name.
1871              Patch things up. */           
1872           if (previous_stab_code == (unsigned char) N_SO)
1873             {
1874               patch_subfile_names (current_subfile, name);
1875               break;            /* Ignore repeated SOs */
1876             }
1877           end_symtab (valu, 0, 0, objfile, SECT_OFF_TEXT);
1878           end_stabs ();
1879         }
1880
1881       /* Null name means this just marks the end of text for this .o file.
1882          Don't start a new symtab in this case.  */
1883       if (*name == '\000')
1884         break;
1885
1886       start_stabs ();
1887       start_symtab (name, NULL, valu);
1888       break;
1889
1890     case N_SOL:
1891       /* This type of symbol indicates the start of data for
1892          a sub-source-file, one whose contents were copied or
1893          included in the compilation of the main source file
1894          (whose name was given in the N_SO symbol.)  */
1895       /* Relocate for dynamic loading */
1896       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1897       start_subfile (name, current_subfile->dirname);
1898       break;
1899
1900     case N_BINCL:
1901       push_subfile ();
1902       add_new_header_file (name, valu);
1903       start_subfile (name, current_subfile->dirname);
1904       break;
1905
1906     case N_EINCL:
1907       start_subfile (pop_subfile (), current_subfile->dirname);
1908       break;
1909
1910     case N_EXCL:
1911       add_old_header_file (name, valu);
1912       break;
1913
1914     case N_SLINE:
1915       /* This type of "symbol" really just records
1916          one line-number -- core-address correspondence.
1917          Enter it in the line list for this symbol table.  */
1918       /* Relocate for dynamic loading and for ELF acc fn-relative syms.  */
1919       valu += function_start_offset;
1920 #ifdef SUN_FIXED_LBRAC_BUG
1921       last_pc_address = valu;   /* Save for SunOS bug circumcision */
1922 #endif
1923       record_line (current_subfile, desc, valu);
1924       break;
1925
1926     case N_BCOMM:
1927       common_block_start (name, objfile);
1928       break;
1929
1930     case N_ECOMM:
1931       common_block_end (objfile);
1932       break;
1933
1934     /* The following symbol types need to have the appropriate offset added
1935        to their value; then we process symbol definitions in the name.  */
1936
1937     case N_STSYM:               /* Static symbol in data seg */
1938     case N_LCSYM:               /* Static symbol in BSS seg */
1939     case N_ROSYM:               /* Static symbol in Read-only data seg */
1940      /* HORRID HACK DEPT.  However, it's Sun's furgin' fault.
1941         Solaris2's stabs-in-elf makes *most* symbols relative
1942         but leaves a few absolute (at least for Solaris 2.1 and version
1943         2.0.1 of the SunPRO compiler).  N_STSYM and friends sit on the fence.
1944         .stab "foo:S...",N_STSYM        is absolute (ld relocates it)
1945         .stab "foo:V...",N_STSYM        is relative (section base subtracted).
1946         This leaves us no choice but to search for the 'S' or 'V'...
1947         (or pass the whole section_offsets stuff down ONE MORE function
1948         call level, which we really don't want to do).  */
1949       {
1950         char *p;
1951
1952         /* .o files and NLMs have non-zero text seg offsets, but don't need
1953            their static syms offset in this fashion.  XXX - This is really a
1954            crock that should be fixed in the solib handling code so that I
1955            don't have to work around it here. */
1956
1957         if (!symfile_relocatable)
1958           {
1959             p = strchr (name, ':');
1960             if (p != 0 && p[1] == 'S')
1961               {
1962                 /* The linker relocated it.  We don't want to add an
1963                    elfstab_offset_sections-type offset, but we *do* want
1964                    to add whatever solib.c passed to symbol_file_add as
1965                    addr (this is known to affect SunOS4, and I suspect ELF
1966                    too).  Since elfstab_offset_sections currently does not
1967                    muck with the text offset (there is no Ttext.text
1968                    symbol), we can get addr from the text offset.  If
1969                    elfstab_offset_sections ever starts dealing with the
1970                    text offset, and we still need to do this, we need to
1971                    invent a SECT_OFF_ADDR_KLUDGE or something.  */
1972                 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1973                 goto define_a_symbol;
1974               }
1975           }
1976         /* Since it's not the kludge case, re-dispatch to the right handler. */
1977         switch (type) {
1978         case N_STSYM:   goto case_N_STSYM;
1979         case N_LCSYM:   goto case_N_LCSYM;
1980         case N_ROSYM:   goto case_N_ROSYM;
1981         default:        abort();
1982         }
1983       }
1984
1985     case_N_STSYM:               /* Static symbol in data seg */
1986     case N_DSLINE:              /* Source line number, data seg */
1987       valu += ANOFFSET (section_offsets, SECT_OFF_DATA);
1988       goto define_a_symbol;
1989
1990     case_N_LCSYM:               /* Static symbol in BSS seg */
1991     case N_BSLINE:              /* Source line number, bss seg */
1992     /*   N_BROWS:       overlaps with N_BSLINE */
1993       valu += ANOFFSET (section_offsets, SECT_OFF_BSS);
1994       goto define_a_symbol;
1995
1996     case_N_ROSYM:               /* Static symbol in Read-only data seg */
1997       valu += ANOFFSET (section_offsets, SECT_OFF_RODATA);
1998       goto define_a_symbol;
1999
2000     case N_ENTRY:               /* Alternate entry point */
2001       /* Relocate for dynamic loading */
2002       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
2003       goto define_a_symbol;
2004
2005     /* The following symbol types we don't know how to process.  Handle
2006        them in a "default" way, but complain to people who care.  */
2007     default:
2008     case N_CATCH:               /* Exception handler catcher */
2009     case N_EHDECL:              /* Exception handler name */
2010     case N_PC:                  /* Global symbol in Pascal */
2011     case N_M2C:                 /* Modula-2 compilation unit */
2012     /*   N_MOD2:        overlaps with N_EHDECL */
2013     case N_SCOPE:               /* Modula-2 scope information */
2014     case N_ECOML:               /* End common (local name) */
2015     case N_NBTEXT:              /* Gould Non-Base-Register symbols??? */
2016     case N_NBDATA:
2017     case N_NBBSS:
2018     case N_NBSTS:
2019     case N_NBLCS:
2020       complain (&unknown_symtype_complaint, local_hex_string (type));
2021       /* FALLTHROUGH */
2022
2023     /* The following symbol types don't need the address field relocated,
2024        since it is either unused, or is absolute.  */
2025     define_a_symbol:
2026     case N_GSYM:                /* Global variable */
2027     case N_NSYMS:               /* Number of symbols (ultrix) */
2028     case N_NOMAP:               /* No map?  (ultrix) */
2029     case N_RSYM:                /* Register variable */
2030     case N_DEFD:                /* Modula-2 GNU module dependency */
2031     case N_SSYM:                /* Struct or union element */
2032     case N_LSYM:                /* Local symbol in stack */
2033     case N_PSYM:                /* Parameter variable */
2034     case N_LENG:                /* Length of preceding symbol type */
2035       if (name)
2036         {
2037           int deftype;
2038           char *colon_pos = strchr (name, ':');
2039           if (colon_pos == NULL)
2040             deftype = '\0';
2041           else
2042             deftype = colon_pos[1];
2043
2044           switch (deftype)
2045             {
2046             case 'f':
2047             case 'F':
2048               function_stab_type = type;
2049
2050 #ifdef SOFUN_ADDRESS_MAYBE_MISSING
2051               /* Deal with the SunPRO 3.0 compiler which omits the address
2052                  from N_FUN symbols.  */
2053               if (type == N_FUN && valu == 0)
2054                 {
2055                   struct minimal_symbol *msym;
2056                   char *p;
2057                   int n;
2058
2059                   p = strchr (name, ':');
2060                   if (p == NULL)
2061                     p = name;
2062                   n = p - name;
2063                   p = alloca (n + 1);
2064                   strncpy (p, name, n);
2065                   p[n] = 0;
2066
2067                   msym = lookup_minimal_symbol (p, last_source_file,
2068                                                 objfile);
2069                   if (msym)
2070                     valu = SYMBOL_VALUE_ADDRESS (msym);
2071                 }
2072 #endif
2073
2074 #ifdef SUN_FIXED_LBRAC_BUG
2075               /* The Sun acc compiler, under SunOS4, puts out
2076                  functions with N_GSYM or N_STSYM.  The problem is
2077                  that the address of the symbol is no good (for N_GSYM
2078                  it doesn't even attept an address; for N_STSYM it
2079                  puts out an address but then it gets relocated
2080                  relative to the data segment, not the text segment).
2081                  Currently we can't fix this up later as we do for
2082                  some types of symbol in scan_file_globals.
2083                  Fortunately we do have a way of finding the address -
2084                  we know that the value in last_pc_address is either
2085                  the one we want (if we're dealing with the first
2086                  function in an object file), or somewhere in the
2087                  previous function. This means that we can use the
2088                  minimal symbol table to get the address.  */
2089
2090               /* Starting with release 3.0, the Sun acc compiler,
2091                  under SunOS4, puts out functions with N_FUN and a value
2092                  of zero. This gets relocated to the start of the text
2093                  segment of the module, which is no good either.
2094                  Under SunOS4 we can deal with this as N_SLINE and N_SO
2095                  entries contain valid absolute addresses.
2096                  Release 3.0 acc also puts out N_OPT entries, which makes
2097                  it possible to discern acc from cc or gcc.  */
2098
2099               if (type == N_GSYM || type == N_STSYM
2100                   || (type == N_FUN
2101                       && n_opt_found && !block_address_function_relative))
2102                 {
2103                   struct minimal_symbol *m;
2104                   int l = colon_pos - name;
2105
2106                   m = lookup_minimal_symbol_by_pc (last_pc_address);
2107                   if (m && STREQN (SYMBOL_NAME (m), name, l))
2108                     /* last_pc_address was in this function */
2109                     valu = SYMBOL_VALUE (m);
2110                   else if (m && STREQN (SYMBOL_NAME (m+1), name, l))
2111                     /* last_pc_address was in last function */
2112                     valu = SYMBOL_VALUE (m+1);
2113                   else
2114                     /* Not found - use last_pc_address (for finish_block) */
2115                     valu = last_pc_address;
2116                 }
2117
2118               last_pc_address = valu;   /* Save for SunOS bug circumcision */
2119 #endif
2120
2121               if (block_address_function_relative)
2122                 /* For Solaris 2.0 compilers, the block addresses and
2123                    N_SLINE's are relative to the start of the
2124                    function.  On normal systems, and when using gcc on
2125                    Solaris 2.0, these addresses are just absolute, or
2126                    relative to the N_SO, depending on
2127                    BLOCK_ADDRESS_ABSOLUTE.  */
2128                 function_start_offset = valu;   
2129
2130               within_function = 1;
2131               if (context_stack_depth > 0)
2132                 {
2133                   new = pop_context ();
2134                   /* Make a block for the local symbols within.  */
2135                   finish_block (new->name, &local_symbols, new->old_blocks,
2136                                 new->start_addr, valu, objfile);
2137                 }
2138               /* Stack must be empty now.  */
2139               if (context_stack_depth != 0)
2140                 complain (&lbrac_unmatched_complaint, symnum);
2141
2142               new = push_context (0, valu);
2143               new->name = define_symbol (valu, name, desc, type, objfile);
2144               break;
2145
2146             default:
2147               define_symbol (valu, name, desc, type, objfile);
2148               break;
2149             }
2150         }
2151       break;
2152
2153     /* We use N_OPT to carry the gcc2_compiled flag.  Sun uses it
2154        for a bunch of other flags, too.  Someday we may parse their
2155        flags; for now we ignore theirs and hope they'll ignore ours.  */
2156     case N_OPT:                 /* Solaris 2:  Compiler options */
2157       if (name)
2158         {
2159           if (STREQ (name, GCC2_COMPILED_FLAG_SYMBOL))
2160             {
2161               processing_gcc_compilation = 2;
2162 #if 1         /* Works, but is experimental.  -fnf */
2163               if (AUTO_DEMANGLING)
2164                 {
2165                   set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
2166                 }
2167 #endif
2168             }
2169           else
2170             n_opt_found = 1;
2171         }
2172       break;
2173
2174     /* The following symbol types can be ignored.  */
2175     case N_OBJ:                 /* Solaris 2:  Object file dir and name */
2176     /*   N_UNDF:                   Solaris 2:  file separator mark */
2177     /*   N_UNDF: -- we will never encounter it, since we only process one
2178                     file's symbols at once.  */
2179     case N_ENDM:                /* Solaris 2:  End of module */
2180     case N_MAIN:                /* Name of main routine.  */
2181       break;
2182     }
2183
2184   previous_stab_code = type;
2185 }
2186 \f
2187 /* FIXME: The only difference between this and elfstab_build_psymtabs is
2188    the call to install_minimal_symbols for elf.  If the differences are
2189    really that small, the code should be shared.  */
2190
2191 /* Scan and build partial symbols for an coff symbol file.
2192    The coff file has already been processed to get its minimal symbols.
2193
2194    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2195    rolled into one.
2196
2197    OBJFILE is the object file we are reading symbols from.
2198    ADDR is the address relative to which the symbols are (e.g.
2199    the base address of the text segment).
2200    MAINLINE is true if we are reading the main symbol
2201    table (as opposed to a shared lib or dynamically loaded file).
2202    STABOFFSET and STABSIZE define the location in OBJFILE where the .stab
2203    section exists.
2204    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2205    .stabstr section exists.
2206
2207    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2208    adjusted for coff details. */
2209
2210 void
2211 coffstab_build_psymtabs (objfile, section_offsets, mainline, 
2212                                staboffset, stabsize,
2213                                stabstroffset, stabstrsize)
2214       struct objfile *objfile;
2215       struct section_offsets *section_offsets;
2216       int mainline;
2217       file_ptr staboffset;
2218       unsigned int stabsize;
2219       file_ptr stabstroffset;
2220       unsigned int stabstrsize;
2221 {
2222   int val;
2223   bfd *sym_bfd = objfile->obfd;
2224   char *name = bfd_get_filename (sym_bfd);
2225   struct dbx_symfile_info *info;
2226
2227   /* There is already a dbx_symfile_info allocated by our caller.
2228      It might even contain some info from the coff symtab to help us.  */
2229   info = (struct dbx_symfile_info *) objfile->sym_stab_info;
2230
2231   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
2232   if (!DBX_TEXT_SECT (objfile))
2233     error ("Can't find .text section in symbol file");
2234
2235 #define COFF_STABS_SYMBOL_SIZE  12      /* XXX FIXME XXX */
2236   DBX_SYMBOL_SIZE    (objfile) = COFF_STABS_SYMBOL_SIZE;
2237   DBX_SYMCOUNT       (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
2238   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
2239   DBX_SYMTAB_OFFSET  (objfile) = staboffset;
2240   
2241   if (stabstrsize > bfd_get_size (sym_bfd))
2242     error ("ridiculous string table size: %d bytes", stabstrsize);
2243   DBX_STRINGTAB (objfile) = (char *)
2244     obstack_alloc (&objfile->psymbol_obstack, stabstrsize+1);
2245
2246   /* Now read in the string table in one big gulp.  */
2247
2248   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
2249   if (val < 0)
2250     perror_with_name (name);
2251   val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2252   if (val != stabstrsize)
2253     perror_with_name (name);
2254
2255   stabsread_new_init ();
2256   buildsym_new_init ();
2257   free_header_files ();
2258   init_header_files ();
2259
2260   processing_acc_compilation = 1;
2261
2262   /* In a coff file, we've already installed the minimal symbols that came
2263      from the coff (non-stab) symbol table, so always act like an
2264      incremental load here. */
2265   dbx_symfile_read (objfile, section_offsets, 0);
2266 }
2267 \f
2268 /* Scan and build partial symbols for an ELF symbol file.
2269    This ELF file has already been processed to get its minimal symbols,
2270    and any DWARF symbols that were in it.
2271
2272    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2273    rolled into one.
2274
2275    OBJFILE is the object file we are reading symbols from.
2276    ADDR is the address relative to which the symbols are (e.g.
2277    the base address of the text segment).
2278    MAINLINE is true if we are reading the main symbol
2279    table (as opposed to a shared lib or dynamically loaded file).
2280    STABOFFSET and STABSIZE define the location in OBJFILE where the .stab
2281    section exists.
2282    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
2283    .stabstr section exists.
2284
2285    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
2286    adjusted for elf details. */
2287
2288 void
2289 elfstab_build_psymtabs (objfile, section_offsets, mainline, 
2290                                staboffset, stabsize,
2291                                stabstroffset, stabstrsize)
2292       struct objfile *objfile;
2293       struct section_offsets *section_offsets;
2294       int mainline;
2295       file_ptr staboffset;
2296       unsigned int stabsize;
2297       file_ptr stabstroffset;
2298       unsigned int stabstrsize;
2299 {
2300   int val;
2301   bfd *sym_bfd = objfile->obfd;
2302   char *name = bfd_get_filename (sym_bfd);
2303   struct dbx_symfile_info *info;
2304
2305   /* There is already a dbx_symfile_info allocated by our caller.
2306      It might even contain some info from the ELF symtab to help us.  */
2307   info = (struct dbx_symfile_info *) objfile->sym_stab_info;
2308
2309   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
2310   if (!DBX_TEXT_SECT (objfile))
2311     error ("Can't find .text section in symbol file");
2312
2313 #define ELF_STABS_SYMBOL_SIZE   12      /* XXX FIXME XXX */
2314   DBX_SYMBOL_SIZE    (objfile) = ELF_STABS_SYMBOL_SIZE;
2315   DBX_SYMCOUNT       (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
2316   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
2317   DBX_SYMTAB_OFFSET  (objfile) = staboffset;
2318   
2319   if (stabstrsize > bfd_get_size (sym_bfd))
2320     error ("ridiculous string table size: %d bytes", stabstrsize);
2321   DBX_STRINGTAB (objfile) = (char *)
2322     obstack_alloc (&objfile->psymbol_obstack, stabstrsize+1);
2323
2324   /* Now read in the string table in one big gulp.  */
2325
2326   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
2327   if (val < 0)
2328     perror_with_name (name);
2329   val = bfd_read (DBX_STRINGTAB (objfile), stabstrsize, 1, sym_bfd);
2330   if (val != stabstrsize)
2331     perror_with_name (name);
2332
2333   stabsread_new_init ();
2334   buildsym_new_init ();
2335   free_header_files ();
2336   init_header_files ();
2337   install_minimal_symbols (objfile);
2338
2339   processing_acc_compilation = 1;
2340
2341   /* In an elf file, we've already installed the minimal symbols that came
2342      from the elf (non-stab) symbol table, so always act like an
2343      incremental load here. */
2344   dbx_symfile_read (objfile, section_offsets, 0);
2345 }
2346 \f
2347 /* Scan and build partial symbols for a file with special sections for stabs
2348    and stabstrings.  The file has already been processed to get its minimal
2349    symbols, and any other symbols that might be necessary to resolve GSYMs.
2350
2351    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
2352    rolled into one.
2353
2354    OBJFILE is the object file we are reading symbols from.
2355    ADDR is the address relative to which the symbols are (e.g. the base address
2356         of the text segment).
2357    MAINLINE is true if we are reading the main symbol table (as opposed to a
2358             shared lib or dynamically loaded file).
2359    STAB_NAME is the name of the section that contains the stabs.
2360    STABSTR_NAME is the name of the section that contains the stab strings.
2361
2362    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read. */
2363
2364 void
2365 stabsect_build_psymtabs (objfile, section_offsets, mainline, stab_name,
2366                          stabstr_name, text_name)
2367      struct objfile *objfile;
2368      struct section_offsets *section_offsets;
2369      int mainline;
2370      char *stab_name;
2371      char *stabstr_name;
2372      char *text_name;
2373 {
2374   int val;
2375   bfd *sym_bfd = objfile->obfd;
2376   char *name = bfd_get_filename (sym_bfd);
2377   asection *stabsect;
2378   asection *stabstrsect;
2379
2380   stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
2381   stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
2382
2383   if (!stabsect)
2384     return;
2385
2386   if (!stabstrsect)
2387     error ("stabsect_build_psymtabs:  Found stabs (%s), but not string section (%s)",
2388            stab_name, stabstr_name);
2389
2390   objfile->sym_stab_info = (PTR) xmalloc (sizeof (struct dbx_symfile_info));
2391   memset (DBX_SYMFILE_INFO (objfile), 0, sizeof (struct dbx_symfile_info));
2392
2393   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, text_name);
2394   if (!DBX_TEXT_SECT (objfile))
2395     error ("Can't find %s section in symbol file", text_name);
2396
2397   DBX_SYMBOL_SIZE    (objfile) = sizeof (struct external_nlist);
2398   DBX_SYMCOUNT       (objfile) = bfd_section_size (sym_bfd, stabsect)
2399     / DBX_SYMBOL_SIZE (objfile);
2400   DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
2401   DBX_SYMTAB_OFFSET  (objfile) = stabsect->filepos; /* XXX - FIXME: POKING INSIDE BFD DATA STRUCTURES */
2402   
2403   if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
2404     error ("ridiculous string table size: %d bytes", DBX_STRINGTAB_SIZE (objfile));
2405   DBX_STRINGTAB (objfile) = (char *)
2406     obstack_alloc (&objfile->psymbol_obstack, DBX_STRINGTAB_SIZE (objfile) + 1);
2407
2408   /* Now read in the string table in one big gulp.  */
2409
2410   val = bfd_get_section_contents (sym_bfd, /* bfd */
2411                                   stabstrsect, /* bfd section */
2412                                   DBX_STRINGTAB (objfile), /* input buffer */
2413                                   0, /* offset into section */
2414                                   DBX_STRINGTAB_SIZE (objfile)); /* amount to read */
2415
2416   if (!val)
2417     perror_with_name (name);
2418
2419   stabsread_new_init ();
2420   buildsym_new_init ();
2421   free_header_files ();
2422   init_header_files ();
2423   install_minimal_symbols (objfile);
2424
2425   /* Now, do an incremental load */
2426
2427   processing_acc_compilation = 1;
2428   dbx_symfile_read (objfile, section_offsets, 0);
2429 }
2430 \f
2431 /* Parse the user's idea of an offset for dynamic linking, into our idea
2432    of how to represent it for fast symbol reading.  */
2433
2434 static struct section_offsets *
2435 dbx_symfile_offsets (objfile, addr)
2436      struct objfile *objfile;
2437      CORE_ADDR addr;
2438 {
2439   struct section_offsets *section_offsets;
2440   int i;
2441
2442   objfile->num_sections = SECT_OFF_MAX;
2443   section_offsets = (struct section_offsets *)
2444     obstack_alloc (&objfile -> psymbol_obstack,
2445                    sizeof (struct section_offsets)
2446                    + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
2447
2448   for (i = 0; i < SECT_OFF_MAX; i++)
2449     ANOFFSET (section_offsets, i) = addr;
2450   
2451   return section_offsets;
2452 }
2453 \f
2454 static struct sym_fns aout_sym_fns =
2455 {
2456   bfd_target_aout_flavour,
2457   dbx_new_init,         /* sym_new_init: init anything gbl to entire symtab */
2458   dbx_symfile_init,     /* sym_init: read initial info, setup for sym_read() */
2459   dbx_symfile_read,     /* sym_read: read a symbol file into symtab */
2460   dbx_symfile_finish,   /* sym_finish: finished with file, cleanup */
2461   dbx_symfile_offsets,  /* sym_offsets: parse user's offsets to internal form */
2462   NULL                  /* next: pointer to next struct sym_fns */
2463 };
2464
2465 void
2466 _initialize_dbxread ()
2467 {
2468   add_symtab_fns(&aout_sym_fns);
2469 }