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