Various changes to clean up ADD_PSYMBOL_TO_LIST and ADD_PSYMBOL_ADDR_TO_LIST
[platform/upstream/binutils.git] / gdb / dbxread.c
1 /* Read dbx symbol tables and convert to internal format, for GDB.
2    Copyright (C) 1986-1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* This module provides three functions: dbx_symfile_init,
21    which initializes to read a symbol file; dbx_new_init, which 
22    discards existing cached information when all symbols are being
23    discarded; and dbx_symfile_read, which reads a symbol table
24    from a file.
25
26    dbx_symfile_read only does the minimum work necessary for letting the
27    user "name" things symbolically; it does not read the entire symtab.
28    Instead, it reads the external and static symbols and puts them in partial
29    symbol tables.  When more extensive information is requested of a
30    file, the corresponding partial symbol table is mutated into a full
31    fledged symbol table by going back and reading the symbols
32    for real.  dbx_psymtab_to_symtab() is the function that does this */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include "defs.h"
37
38 #ifdef USG
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #define L_SET 0
42 #define L_INCR 1
43 #endif
44
45 #include <obstack.h>
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <ctype.h>
50 #include "symtab.h"
51 #include "breakpoint.h"
52 #include "command.h"
53 #include "target.h"
54 #include "gdbcore.h"            /* for bfd stuff */
55 #include "libaout.h"            /* FIXME Secret internal BFD stuff for a.out */
56 #include "symfile.h"
57 #include "buildsym.h"
58
59 #include "aout/aout64.h"
60 #include "aout/stab_gnu.h"      /* We always use GNU stabs, not native, now */
61
62 /* Information is passed among various dbxread routines for accessing
63    symbol files.  A pointer to this structure is kept in the sym_private
64    field of the struct sym_fns passed in by symfile.h.  */
65  
66 struct dbx_symfile_info {
67   asection *text_sect;          /* Text section accessor */
68   int symcount;                 /* How many symbols are there in the file */
69   char *stringtab;              /* The actual string table */
70   int stringtab_size;           /* Its size */
71   off_t symtab_offset;          /* Offset in file to symbol table */
72 };
73
74
75 /* Each partial symbol table entry contains a pointer to private data for the
76    read_symtab() function to use when expanding a partial symbol table entry
77    to a full symbol table entry.
78
79    For dbxread this structure contains the offset within the file symbol table
80    of first local symbol for this file, and length (in bytes) of the section
81    of the symbol table devoted to this file's symbols (actually, the section
82    bracketed may contain more than just this file's symbols).  If ldsymlen is
83    0, the only reason for this thing's existence is the dependency list.
84    Nothing else will happen when it is read in. */
85
86 #define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
87 #define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
88
89 struct symloc {
90   int ldsymoff;
91   int ldsymlen;
92 };
93
94 extern void qsort ();
95 extern double atof ();
96
97 /* Forward declarations */
98
99 static void read_dbx_symtab ();
100 static void init_psymbol_list ();
101 extern void process_one_symbol ();
102 void start_subfile ();
103 int hashname ();
104 static struct pending *copy_pending ();
105 static struct symtab *read_ofile_symtab ();
106 static void dbx_psymtab_to_symtab ();
107
108 /* Macro to determine which symbols to ignore when reading the first symbol
109    of a file.  Some machines override this definition. */
110 #ifndef IGNORE_SYMBOL
111 /* This code is used on Ultrix systems.  Ignore it */
112 #define IGNORE_SYMBOL(type)  (type == (int)N_NSYMS)
113 #endif
114
115 /* Macro for name of symbol to indicate a file compiled with gcc. */
116 #ifndef GCC_COMPILED_FLAG_SYMBOL
117 #define GCC_COMPILED_FLAG_SYMBOL "gcc_compiled."
118 #endif
119
120 /* Define this as 1 if a pcc declaration of a char or short argument
121    gives the correct address.  Otherwise assume pcc gives the
122    address of the corresponding int, which is not the same on a
123    big-endian machine.  */
124
125 #ifndef BELIEVE_PCC_PROMOTION
126 #define BELIEVE_PCC_PROMOTION 0
127 #endif
128
129 /* Nonzero means give verbose info on gdb action.  From main.c.  */
130 extern int info_verbose;
131
132 /* The BFD for this file -- implicit parameter to next_symbol_text.  */
133
134 static bfd *symfile_bfd;
135
136 /* The objfile for this file -- only good in process_one_symbol().  */
137
138 static struct objfile *our_objfile;
139
140 /* String table for the main symbol file.  It is kept in memory
141    permanently, to speed up symbol reading.  Other files' symbol tables
142    are read in on demand.  FIXME, this should be cleaner.  */
143
144 static char *symfile_string_table;
145 static int symfile_string_table_size;
146
147 /* The size of each symbol in the symbol file (in external form).
148    This is set by dbx_symfile_read when building psymtabs, and by
149    dbx_psymtab_to_symtab when building symtabs.  */
150
151 static unsigned symbol_size;
152
153 /* Complaints about the symbols we have encountered.  */
154
155 struct complaint lbrac_complaint = 
156   {"bad block start address patched", 0, 0};
157
158 struct complaint string_table_offset_complaint =
159   {"bad string table offset in symbol %d", 0, 0};
160
161 struct complaint unknown_symtype_complaint =
162   {"unknown symbol type %s", 0, 0};
163
164 struct complaint lbrac_rbrac_complaint =
165   {"block start larger than block end", 0, 0};
166
167 struct complaint lbrac_unmatched_complaint =
168   {"unmatched N_LBRAC before symtab pos %d", 0, 0};
169
170 struct complaint lbrac_mismatch_complaint =
171   {"N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", 0, 0};
172 \f
173 /* During initial symbol readin, we need to have a structure to keep
174    track of which psymtabs have which bincls in them.  This structure
175    is used during readin to setup the list of dependencies within each
176    partial symbol table. */
177
178 struct header_file_location
179 {
180   char *name;                   /* Name of header file */
181   int instance;                 /* See above */
182   struct partial_symtab *pst;   /* Partial symtab that has the
183                                    BINCL/EINCL defs for this file */
184 };
185
186 /* The actual list and controling variables */
187 static struct header_file_location *bincl_list, *next_bincl;
188 static int bincls_allocated;
189
190 /* Free up old header file tables, and allocate new ones.
191    We're reading a new symbol file now.  */
192
193 void
194 free_and_init_header_files ()
195 {
196   register int i;
197   for (i = 0; i < n_header_files; i++)
198     free (header_files[i].name);
199   if (header_files)                     /* First time null */
200     free (header_files);
201   if (this_object_header_files)         /* First time null */
202     free (this_object_header_files);
203
204   n_allocated_header_files = 10;
205   header_files = (struct header_file *) xmalloc (10 * sizeof (struct header_file));
206   n_header_files = 0;
207
208   n_allocated_this_object_header_files = 10;
209   this_object_header_files = (int *) xmalloc (10 * sizeof (int));
210 }
211
212 /* Called at the start of each object file's symbols.
213    Clear out the mapping of header file numbers to header files.  */
214
215 void
216 new_object_header_files ()
217 {
218   /* Leave FILENUM of 0 free for builtin types and this file's types.  */
219   n_this_object_header_files = 1;
220   header_file_prev_index = -1;
221 }
222
223 /* Add header file number I for this object file
224    at the next successive FILENUM.  */
225
226 static void
227 add_this_object_header_file (i)
228      int i;
229 {
230   if (n_this_object_header_files == n_allocated_this_object_header_files)
231     {
232       n_allocated_this_object_header_files *= 2;
233       this_object_header_files
234         = (int *) xrealloc (this_object_header_files,
235                             n_allocated_this_object_header_files * sizeof (int));
236     }
237
238   this_object_header_files[n_this_object_header_files++] = i;
239 }
240
241 /* Add to this file an "old" header file, one already seen in
242    a previous object file.  NAME is the header file's name.
243    INSTANCE is its instance code, to select among multiple
244    symbol tables for the same header file.  */
245
246 static void
247 add_old_header_file (name, instance)
248      char *name;
249      int instance;
250 {
251   register struct header_file *p = header_files;
252   register int i;
253
254   for (i = 0; i < n_header_files; i++)
255     if (!strcmp (p[i].name, name) && instance == p[i].instance)
256       {
257         add_this_object_header_file (i);
258         return;
259       }
260   error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
261          symnum);
262 }
263
264 /* Add to this file a "new" header file: definitions for its types follow.
265    NAME is the header file's name.
266    Most often this happens only once for each distinct header file,
267    but not necessarily.  If it happens more than once, INSTANCE has
268    a different value each time, and references to the header file
269    use INSTANCE values to select among them.
270
271    dbx output contains "begin" and "end" markers for each new header file,
272    but at this level we just need to know which files there have been;
273    so we record the file when its "begin" is seen and ignore the "end".  */
274
275 static void
276 add_new_header_file (name, instance)
277      char *name;
278      int instance;
279 {
280   register int i;
281   header_file_prev_index = -1;
282
283   /* Make sure there is room for one more header file.  */
284
285   if (n_header_files == n_allocated_header_files)
286     {
287       n_allocated_header_files *= 2;
288       header_files = (struct header_file *)
289         xrealloc (header_files,
290                   (n_allocated_header_files
291                    * sizeof (struct header_file)));
292     }
293
294   /* Create an entry for this header file.  */
295
296   i = n_header_files++;
297   header_files[i].name = savestring (name, strlen(name));
298   header_files[i].instance = instance;
299   header_files[i].length = 10;
300   header_files[i].vector
301     = (struct type **) xmalloc (10 * sizeof (struct type *));
302   bzero (header_files[i].vector, 10 * sizeof (struct type *));
303
304   add_this_object_header_file (i);
305 }
306
307 #if 0
308 static struct type **
309 explicit_lookup_type (real_filenum, index)
310      int real_filenum, index;
311 {
312   register struct header_file *f = &header_files[real_filenum];
313
314   if (index >= f->length)
315     {
316       f->length *= 2;
317       f->vector = (struct type **)
318         xrealloc (f->vector, f->length * sizeof (struct type *));
319       bzero (&f->vector[f->length / 2],
320              f->length * sizeof (struct type *) / 2);
321     }
322   return &f->vector[index];
323 }
324 #endif
325 \f
326 static void
327 record_misc_function (name, address, type)
328      char *name;
329      CORE_ADDR address;
330      int type;
331 {
332   enum misc_function_type misc_type;
333
334   switch (type &~ N_EXT) {
335     case N_TEXT:  misc_type = mf_text; break;
336     case N_DATA:  misc_type = mf_data; break;
337     case N_BSS:   misc_type = mf_bss;  break;
338     case N_ABS:   misc_type = mf_abs;  break;
339 #ifdef N_SETV
340     case N_SETV:  misc_type = mf_data; break;
341 #endif
342     default:      misc_type = mf_unknown; break;
343   }
344
345   prim_record_misc_function (obsavestring (name, strlen (name)),
346                              address, misc_type);
347 }
348 \f
349 /* Scan and build partial symbols for a symbol file.
350    We have been initialized by a call to dbx_symfile_init, which 
351    put all the relevant info into a "struct dbx_symfile_info"
352    hung off the struct sym_fns SF.
353
354    ADDR is the address relative to which the symbols in it are (e.g.
355    the base address of the text segment).
356    MAINLINE is true if we are reading the main symbol
357    table (as opposed to a shared lib or dynamically loaded file).  */
358
359 static void
360 dbx_symfile_read (sf, addr, mainline)
361      struct sym_fns *sf;
362      CORE_ADDR addr;
363      int mainline;      /* FIXME comments above */
364 {
365   struct dbx_symfile_info *info = (struct dbx_symfile_info *) (sf->sym_private);
366   bfd *sym_bfd = sf->objfile->obfd;
367   int val;
368
369   val = bfd_seek (sf->objfile->obfd, info->symtab_offset, L_SET);
370   if (val < 0)
371     perror_with_name (sf->objfile->name);
372
373   /* If mainline, set global string table pointers, and reinitialize global
374      partial symbol list.  */
375   if (mainline) {
376     symfile_string_table = info->stringtab;
377     symfile_string_table_size = info->stringtab_size;
378   }
379
380   /* If we are reinitializing, or if we have never loaded syms yet, init */
381   if (mainline || global_psymbols.size == 0 || static_psymbols.size == 0)
382     init_psymbol_list (info->symcount);
383
384   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
385   symbol_size = obj_symbol_entry_size (sym_bfd);
386
387   pending_blocks = 0;
388   make_cleanup (really_free_pendings, 0);
389
390   init_misc_bunches ();
391   make_cleanup (discard_misc_bunches, 0);
392
393   /* Now that the symbol table data of the executable file are all in core,
394      process them and define symbols accordingly.  */
395
396   read_dbx_symtab (addr - bfd_section_vma (sym_bfd, info->text_sect), /*offset*/
397                    sf->objfile, info->stringtab, info->stringtab_size,
398                    info->symcount,
399                    bfd_section_vma  (sym_bfd, info->text_sect),
400                    bfd_section_size (sym_bfd, info->text_sect));
401
402   /* Go over the misc symbol bunches and install them in vector.  */
403
404   condense_misc_bunches (!mainline);
405
406   /* Free up any memory we allocated for ourselves.  */
407
408   if (!mainline) {
409     free (info->stringtab);     /* Stringtab is only saved for mainline */
410   }
411   free (info);
412   sf->sym_private = 0;          /* Zap pointer to our (now gone) info struct */
413
414   if (!partial_symtab_list) {
415     wrap_here ("");
416     printf_filtered ("(no debugging symbols found)...");
417     wrap_here ("");
418   }
419 }
420
421 /* Initialize anything that needs initializing when a completely new
422    symbol file is specified (not just adding some symbols from another
423    file, e.g. a shared library).  */
424
425 static void
426 dbx_new_init ()
427 {
428   buildsym_new_init ();
429
430   /* Don't put these on the cleanup chain; they need to stick around
431      until the next call to dbx_new_init.  *Then* we'll free them. */
432   if (symfile_string_table)
433     {
434       free (symfile_string_table);
435       symfile_string_table = 0;
436       symfile_string_table_size = 0;
437     }
438   free_and_init_header_files ();
439 }
440
441
442 /* dbx_symfile_init ()
443    is the dbx-specific initialization routine for reading symbols.
444    It is passed a struct sym_fns which contains, among other things,
445    the BFD for the file whose symbols are being read, and a slot for a pointer
446    to "private data" which we fill with goodies.
447
448    We read the string table into malloc'd space and stash a pointer to it.
449
450    Since BFD doesn't know how to read debug symbols in a format-independent
451    way (and may never do so...), we have to do it ourselves.  We will never
452    be called unless this is an a.out (or very similar) file. 
453    FIXME, there should be a cleaner peephole into the BFD environment here.  */
454
455 static void
456 dbx_symfile_init (sf)
457   struct sym_fns *sf;
458 {
459   int val;
460   bfd *sym_bfd = sf->objfile->obfd;
461   char *name = bfd_get_filename (sym_bfd);
462   struct dbx_symfile_info *info;
463   unsigned char size_temp[4];
464
465   /* Allocate struct to keep track of the symfile */
466   sf->sym_private = xmalloc (sizeof (*info));
467   info = (struct dbx_symfile_info *)sf->sym_private;
468
469   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
470 #define STRING_TABLE_OFFSET     (sym_bfd->origin + obj_str_filepos (sym_bfd))
471 #define SYMBOL_TABLE_OFFSET     (sym_bfd->origin + obj_sym_filepos (sym_bfd))
472   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
473
474   info->text_sect = bfd_get_section_by_name (sym_bfd, ".text");
475   if (!info->text_sect)
476     abort();
477   info->symcount = bfd_get_symcount (sym_bfd);
478
479   /* Read the string table size and check it for bogosity.  */
480   val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
481   if (val < 0)
482       perror_with_name (name);
483
484   val = bfd_read (size_temp, sizeof (long), 1, sym_bfd);
485   if (val < 0)
486       perror_with_name (name);
487   info->stringtab_size = bfd_h_get_32 (sym_bfd, size_temp);
488   
489   if (info->stringtab_size >= 0)
490     {
491       info->stringtab = (char *) xmalloc (info->stringtab_size);
492       /* Caller is responsible for freeing the string table.  No cleanup. */
493     }
494   else
495     info->stringtab = NULL;
496   if (info->stringtab == NULL && info->stringtab_size != 0)
497     error ("ridiculous string table size: %d bytes", info->stringtab_size);
498
499   /* Now read in the string table in one big gulp.  */
500
501   val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
502   if (val < 0)
503     perror_with_name (name);
504   val = bfd_read (info->stringtab, info->stringtab_size, 1, sym_bfd);
505   if (val != info->stringtab_size)
506     perror_with_name (name);
507
508   /* Record the position of the symbol table for later use.  */
509
510   info->symtab_offset = SYMBOL_TABLE_OFFSET;
511 }
512 \f
513 /* Buffer for reading the symbol table entries.  */
514 static struct internal_nlist symbuf[4096];
515 static int symbuf_idx;
516 static int symbuf_end;
517
518 /* The address in memory of the string table of the object file we are
519    reading (which might not be the "main" object file, but might be a
520    shared library or some other dynamically loaded thing).  This is set
521    by read_dbx_symtab when building psymtabs, and by read_ofile_symtab 
522    when building symtabs, and is used only by next_symbol_text.  */
523 static char *stringtab_global;
524
525 /* Refill the symbol table input buffer
526    and set the variables that control fetching entries from it.
527    Reports an error if no data available.
528    This function can read past the end of the symbol table
529    (into the string table) but this does no harm.  */
530
531 static void
532 fill_symbuf (sym_bfd)
533      bfd *sym_bfd;
534 {
535   int nbytes = bfd_read (symbuf, sizeof (symbuf), 1, sym_bfd);
536   if (nbytes < 0)
537     perror_with_name (bfd_get_filename (sym_bfd));
538   else if (nbytes == 0)
539     error ("Premature end of file reading symbol table");
540   symbuf_end = nbytes / symbol_size;
541   symbuf_idx = 0;
542 }
543
544 #define SWAP_SYMBOL(symp, abfd) \
545   { \
546     (symp)->n_strx = bfd_h_get_32(abfd,                 \
547                                 (unsigned char *)&(symp)->n_strx);      \
548     (symp)->n_desc = bfd_h_get_16 (abfd,                        \
549                                 (unsigned char *)&(symp)->n_desc);      \
550     (symp)->n_value = bfd_h_get_32 (abfd,                       \
551                                 (unsigned char *)&(symp)->n_value);     \
552   }
553
554 /* Invariant: The symbol pointed to by symbuf_idx is the first one
555    that hasn't been swapped.  Swap the symbol at the same time
556    that symbuf_idx is incremented.  */
557
558 /* dbx allows the text of a symbol name to be continued into the
559    next symbol name!  When such a continuation is encountered
560    (a \ at the end of the text of a name)
561    call this function to get the continuation.  */
562
563 #ifdef READ_MIPS_FORMAT
564 extern char *next_symbol_text ();
565 #else
566 char *
567 next_symbol_text ()
568 {
569   if (symbuf_idx == symbuf_end)
570     fill_symbuf (symfile_bfd);
571   symnum++;
572   SWAP_SYMBOL(&symbuf[symbuf_idx], symfile_bfd);
573   return symbuf[symbuf_idx++].n_strx + stringtab_global;
574 }
575 #endif
576 \f
577 /* Initializes storage for all of the partial symbols that will be
578    created by read_dbx_symtab and subsidiaries.  */
579
580 static void
581 init_psymbol_list (total_symbols)
582      int total_symbols;
583 {
584   /* Free any previously allocated psymbol lists.  */
585   if (global_psymbols.list)
586     free (global_psymbols.list);
587   if (static_psymbols.list)
588     free (static_psymbols.list);
589
590   /* Current best guess is that there are approximately a twentieth
591      of the total symbols (in a debugging file) are global or static
592      oriented symbols */
593   global_psymbols.size = total_symbols / 10;
594   static_psymbols.size = total_symbols / 10;
595   global_psymbols.next = global_psymbols.list = (struct partial_symbol *)
596     xmalloc (global_psymbols.size * sizeof (struct partial_symbol));
597   static_psymbols.next = static_psymbols.list = (struct partial_symbol *)
598     xmalloc (static_psymbols.size * sizeof (struct partial_symbol));
599 }
600
601 /* Initialize the list of bincls to contain none and have some
602    allocated.  */
603
604 static void
605 init_bincl_list (number)
606      int number;
607 {
608   bincls_allocated = number;
609   next_bincl = bincl_list = (struct header_file_location *)
610       xmalloc (bincls_allocated * sizeof(struct header_file_location));
611 }
612
613 /* Add a bincl to the list.  */
614
615 static void
616 add_bincl_to_list (pst, name, instance)
617      struct partial_symtab *pst;
618      char *name;
619      int instance;
620 {
621   if (next_bincl >= bincl_list + bincls_allocated)
622     {
623       int offset = next_bincl - bincl_list;
624       bincls_allocated *= 2;
625       bincl_list = (struct header_file_location *)
626         xrealloc ((char *)bincl_list,
627                   bincls_allocated * sizeof (struct header_file_location));
628       next_bincl = bincl_list + offset;
629     }
630   next_bincl->pst = pst;
631   next_bincl->instance = instance;
632   next_bincl++->name = name;
633 }
634
635 /* Given a name, value pair, find the corresponding
636    bincl in the list.  Return the partial symtab associated
637    with that header_file_location.  */
638
639 static struct partial_symtab *
640 find_corresponding_bincl_psymtab (name, instance)
641      char *name;
642      int instance;
643 {
644   struct header_file_location *bincl;
645
646   for (bincl = bincl_list; bincl < next_bincl; bincl++)
647     if (bincl->instance == instance
648         && !strcmp (name, bincl->name))
649       return bincl->pst;
650
651   return (struct partial_symtab *) 0;
652 }
653
654 /* Free the storage allocated for the bincl list.  */
655
656 static void
657 free_bincl_list ()
658 {
659   free (bincl_list);
660   bincls_allocated = 0;
661 }
662
663 /* Given pointers to an a.out symbol table in core containing dbx
664    style data, setup partial_symtab's describing each source file for
665    which debugging information is available.  NLISTLEN is the number
666    of symbols in the symbol table.  All symbol names are given as
667    offsets relative to STRINGTAB.  STRINGTAB_SIZE is the size of
668    STRINGTAB.  SYMFILE_NAME is the name of the file we are reading from
669    and ADDR is its relocated address (if incremental) or 0 (if not).  */
670
671 static void
672 read_dbx_symtab (addr, objfile, stringtab, stringtab_size, nlistlen,
673                  text_addr, text_size)
674      CORE_ADDR addr;
675      struct objfile *objfile;
676      register char *stringtab;
677      register long stringtab_size;
678      register int nlistlen;
679      CORE_ADDR text_addr;
680      int text_size;
681 {
682   register struct internal_nlist *bufp;
683   register char *namestring;
684   int nsl;
685   int past_first_source_file = 0;
686   CORE_ADDR last_o_file_start = 0;
687   struct cleanup *old_chain;
688   bfd *abfd;
689
690   /* End of the text segment of the executable file.  */
691   CORE_ADDR end_of_text_addr;
692
693   /* Current partial symtab */
694   struct partial_symtab *pst;
695
696   /* List of current psymtab's include files */
697   char **psymtab_include_list;
698   int includes_allocated;
699   int includes_used;
700
701   /* Index within current psymtab dependency list */
702   struct partial_symtab **dependency_list;
703   int dependencies_used, dependencies_allocated;
704
705   stringtab_global = stringtab;
706   
707   pst = (struct partial_symtab *) 0;
708
709   includes_allocated = 30;
710   includes_used = 0;
711   psymtab_include_list = (char **) alloca (includes_allocated *
712                                            sizeof (char *));
713
714   dependencies_allocated = 30;
715   dependencies_used = 0;
716   dependency_list =
717     (struct partial_symtab **) alloca (dependencies_allocated *
718                                        sizeof (struct partial_symtab *));
719
720   old_chain = make_cleanup (free_objfile, objfile);
721
722   /* Init bincl list */
723   init_bincl_list (20);
724   make_cleanup (free_bincl_list, 0);
725
726   last_source_file = 0;
727
728 #ifdef END_OF_TEXT_DEFAULT
729   end_of_text_addr = END_OF_TEXT_DEFAULT;
730 #else
731   end_of_text_addr = text_addr + addr + text_size;      /* Relocate */
732 #endif
733
734   symfile_bfd = objfile->obfd;  /* For next_text_symbol */
735   abfd = objfile->obfd;
736   symbuf_end = symbuf_idx = 0;
737
738   for (symnum = 0; symnum < nlistlen; symnum++)
739     {
740       /* Get the symbol for this run and pull out some info */
741       QUIT;     /* allow this to be interruptable */
742       if (symbuf_idx == symbuf_end)
743         fill_symbuf (abfd);
744       bufp = &symbuf[symbuf_idx++];
745
746       /*
747        * Special case to speed up readin.
748        */
749       if (bufp->n_type == (unsigned char)N_SLINE) continue;
750
751       SWAP_SYMBOL (bufp, abfd);
752
753       /* Ok.  There is a lot of code duplicated in the rest of this
754          switch statement (for efficiency reasons).  Since I don't
755          like duplicating code, I will do my penance here, and
756          describe the code which is duplicated:
757
758          *) The assignment to namestring.
759          *) The call to strchr.
760          *) The addition of a partial symbol the the two partial
761             symbol lists.  This last is a large section of code, so
762             I've imbedded it in the following macro.
763          */
764       
765 /* Set namestring based on bufp.  If the string table index is invalid, 
766    give a fake name, and print a single error message per symbol file read,
767    rather than abort the symbol reading or flood the user with messages.  */
768 #define SET_NAMESTRING()\
769   if (((unsigned)bufp->n_strx) >= stringtab_size) {     \
770     complain (&string_table_offset_complaint, symnum);                  \
771     namestring = "foo";                                                 \
772   } else                                                                \
773     namestring = bufp->n_strx + stringtab
774
775 #define CUR_SYMBOL_TYPE bufp->n_type
776 #define CUR_SYMBOL_VALUE bufp->n_value
777 #define DBXREAD_ONLY
778 #define CHECK_SECOND_N_SO() \
779           if (symbuf_idx == symbuf_end) \
780               fill_symbuf (abfd);\
781           bufp = &symbuf[symbuf_idx];\
782           /* n_type is only a char, so swapping swapping is irrelevant.  */\
783           if (CUR_SYMBOL_TYPE == (unsigned char)N_SO)\
784             {\
785               SWAP_SYMBOL (bufp, abfd);\
786               SET_NAMESTRING ();\
787               valu = CUR_SYMBOL_VALUE;\
788               symbuf_idx++;\
789               symnum++;\
790             }
791 #define START_PSYMTAB(ofile,addr,fname,low,symoff,global_syms,static_syms)\
792   start_psymtab(ofile, addr, fname, low, symoff, global_syms, static_syms)
793 #define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)\
794   end_psymtab(pst,ilist,ninc,c_off,c_text,dep_list,n_deps)
795 #include "partial-stab.h"
796     }
797
798   /* If there's stuff to be cleaned up, clean it up.  */
799   if (nlistlen > 0                              /* We have some syms */
800       && entry_point < bufp->n_value
801       && entry_point >= last_o_file_start)
802     {
803       startup_file_start = last_o_file_start;
804       startup_file_end = bufp->n_value;
805     }
806
807   if (pst)
808     {
809       end_psymtab (pst, psymtab_include_list, includes_used,
810                    symnum * symbol_size, end_of_text_addr,
811                    dependency_list, dependencies_used);
812     }
813
814   free_bincl_list ();
815   discard_cleanups (old_chain);
816 }
817
818 /* Allocate and partially fill a partial symtab.  It will be
819    completely filled at the end of the symbol list.
820
821    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
822    is the address relative to which its symbols are (incremental) or 0
823    (normal). */
824
825
826 struct partial_symtab *
827 start_psymtab (objfile, addr,
828                filename, textlow, ldsymoff, global_syms, static_syms)
829      struct objfile *objfile;
830      CORE_ADDR addr;
831      char *filename;
832      CORE_ADDR textlow;
833      int ldsymoff;
834      struct partial_symbol *global_syms;
835      struct partial_symbol *static_syms;
836 {
837   struct partial_symtab *result =
838     (struct partial_symtab *) obstack_alloc (psymbol_obstack,
839                                              sizeof (struct partial_symtab));
840
841   result->addr = addr;
842
843   result->filename =
844     (char *) obstack_alloc (psymbol_obstack,
845                             strlen (filename) + 1);
846   strcpy (result->filename, filename);
847
848   result->textlow = textlow;
849   result->read_symtab_private = (char *) obstack_alloc (psymbol_obstack,
850                                                sizeof (struct symloc));
851   if (ldsymoff != -1)
852     LDSYMOFF(result) = ldsymoff;
853
854   result->readin = 0;
855   result->symtab = 0;
856   result->read_symtab = dbx_psymtab_to_symtab;
857
858   result->globals_offset = global_syms - global_psymbols.list;
859   result->statics_offset = static_syms - static_psymbols.list;
860
861   result->n_global_syms = 0;
862   result->n_static_syms = 0;
863
864   /* Chain it to the list owned by the current object file.  */
865   result->objfile = objfile;
866   result->objfile_chain = objfile->psymtabs;
867   objfile->psymtabs = result;
868
869   return result;
870 }
871
872 static int
873 compare_psymbols (s1, s2)
874      register struct partial_symbol *s1, *s2;
875 {
876   register char
877     *st1 = SYMBOL_NAME (s1),
878     *st2 = SYMBOL_NAME (s2);
879
880   if (st1[0] - st2[0])
881     return st1[0] - st2[0];
882   if (st1[1] - st2[1])
883     return st1[1] - st2[1];
884   return strcmp (st1 + 2, st2 + 2);
885 }
886
887 /* Close off the current usage of a partial_symbol table entry.  This
888    involves setting the correct number of includes (with a realloc),
889    setting the high text mark, setting the symbol length in the
890    executable, and setting the length of the global and static lists
891    of psymbols.
892
893    The global symbols and static symbols are then seperately sorted.
894
895    Then the partial symtab is put on the global list.
896    *** List variables and peculiarities of same. ***
897    */
898 void
899 end_psymtab (pst, include_list, num_includes, capping_symbol_offset,
900              capping_text, dependency_list, number_dependencies)
901      struct partial_symtab *pst;
902      char **include_list;
903      int num_includes;
904      int capping_symbol_offset;
905      CORE_ADDR capping_text;
906      struct partial_symtab **dependency_list;
907      int number_dependencies;
908 /*     struct partial_symbol *capping_global, *capping_static;*/
909 {
910   int i;
911
912   if (capping_symbol_offset != -1)
913       LDSYMLEN(pst) = capping_symbol_offset - LDSYMOFF(pst);
914   pst->texthigh = capping_text;
915
916   pst->n_global_syms =
917     global_psymbols.next - (global_psymbols.list + pst->globals_offset);
918   pst->n_static_syms =
919     static_psymbols.next - (static_psymbols.list + pst->statics_offset);
920
921   pst->number_of_dependencies = number_dependencies;
922   if (number_dependencies)
923     {
924       pst->dependencies = (struct partial_symtab **)
925         obstack_alloc (psymbol_obstack,
926                        number_dependencies * sizeof (struct partial_symtab *));
927       memcpy (pst->dependencies, dependency_list,
928              number_dependencies * sizeof (struct partial_symtab *));
929     }
930   else
931     pst->dependencies = 0;
932
933   for (i = 0; i < num_includes; i++)
934     {
935       struct partial_symtab *subpst =
936         (struct partial_symtab *)
937           obstack_alloc (psymbol_obstack,
938                          sizeof (struct partial_symtab));
939
940       subpst->filename =
941         (char *) obstack_alloc (psymbol_obstack,
942                                 strlen (include_list[i]) + 1);
943       strcpy (subpst->filename, include_list[i]);
944
945       /* Chain it to the list that this object file owns.  */
946       subpst->objfile = pst->objfile;
947       subpst->objfile_chain = pst->objfile->psymtabs;
948       pst->objfile->psymtabs = subpst;
949
950       subpst->addr = pst->addr;
951       subpst->read_symtab_private = (char *) obstack_alloc (psymbol_obstack,
952                                                    sizeof (struct symloc));
953       LDSYMOFF(subpst) =
954         LDSYMLEN(subpst) =
955           subpst->textlow =
956             subpst->texthigh = 0;
957
958       /* We could save slight bits of space by only making one of these,
959          shared by the entire set of include files.  FIXME-someday.  */
960       subpst->dependencies = (struct partial_symtab **)
961         obstack_alloc (psymbol_obstack,
962                        sizeof (struct partial_symtab *));
963       subpst->dependencies[0] = pst;
964       subpst->number_of_dependencies = 1;
965
966       subpst->globals_offset =
967         subpst->n_global_syms =
968           subpst->statics_offset =
969             subpst->n_static_syms = 0;
970
971       subpst->readin = 0;
972       subpst->symtab = 0;
973       subpst->read_symtab = dbx_psymtab_to_symtab;
974
975       subpst->next = partial_symtab_list;
976       partial_symtab_list = subpst;
977     }
978
979   /* Sort the global list; don't sort the static list */
980   qsort (global_psymbols.list + pst->globals_offset, pst->n_global_syms,
981          sizeof (struct partial_symbol), compare_psymbols);
982
983   /* If there is already a psymtab or symtab for a file of this name, remove it.
984      (If there is a symtab, more drastic things also happen.)
985      This happens in VxWorks.  */
986   free_named_symtabs (pst->filename);
987
988   if (num_includes == 0
989    && number_dependencies == 0
990    && pst->n_global_syms == 0
991    && pst->n_static_syms == 0) {
992     /* Throw away this psymtab, it's empty.  We can't deallocate it, since
993        it is on the obstack, but we can forget to chain it on the list.  */
994     ;
995   } else {
996     /* Put the psymtab on the psymtab list */
997     pst->next = partial_symtab_list;
998     partial_symtab_list = pst;
999   }
1000 }
1001 \f
1002 static void
1003 psymtab_to_symtab_1 (pst, stringtab, stringtab_size, sym_offset)
1004      struct partial_symtab *pst;
1005      char *stringtab;
1006      int stringtab_size;
1007      int sym_offset;
1008 {
1009   struct cleanup *old_chain;
1010   int i;
1011   
1012   if (!pst)
1013     return;
1014
1015   if (pst->readin)
1016     {
1017       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1018                pst->filename);
1019       return;
1020     }
1021
1022   /* Read in all partial symtabs on which this one is dependent */
1023   for (i = 0; i < pst->number_of_dependencies; i++)
1024     if (!pst->dependencies[i]->readin)
1025       {
1026         /* Inform about additional files that need to be read in.  */
1027         if (info_verbose)
1028           {
1029             fputs_filtered (" ", stdout);
1030             wrap_here ("");
1031             fputs_filtered ("and ", stdout);
1032             wrap_here ("");
1033             printf_filtered ("%s...", pst->dependencies[i]->filename);
1034             wrap_here ("");             /* Flush output */
1035             fflush (stdout);
1036           }
1037         psymtab_to_symtab_1 (pst->dependencies[i],
1038                              stringtab, stringtab_size, sym_offset);
1039       }
1040
1041   if (LDSYMLEN(pst))            /* Otherwise it's a dummy */
1042     {
1043       /* Init stuff necessary for reading in symbols */
1044       buildsym_init ();
1045       old_chain = make_cleanup (really_free_pendings, 0);
1046
1047       /* Read in this files symbols */
1048       bfd_seek (pst->objfile->obfd, sym_offset, L_SET);
1049       pst->symtab =
1050         read_ofile_symtab (pst->objfile, stringtab, stringtab_size,
1051                            LDSYMOFF(pst),
1052                            LDSYMLEN(pst), pst->textlow,
1053                            pst->texthigh - pst->textlow, pst->addr);
1054       sort_symtab_syms (pst->symtab);
1055
1056       do_cleanups (old_chain);
1057     }
1058
1059   pst->readin = 1;
1060 }
1061
1062 /*
1063  * Read in all of the symbols for a given psymtab for real.
1064  * Be verbose about it if the user wants that.
1065  */
1066 static void
1067 dbx_psymtab_to_symtab (pst)
1068      struct partial_symtab *pst;
1069 {
1070   char *stringtab;
1071   int stsize, val;
1072   bfd *sym_bfd;
1073   long st_temp;
1074
1075   if (!pst)
1076     return;
1077
1078   if (pst->readin)
1079     {
1080       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1081                pst->filename);
1082       return;
1083     }
1084
1085   if (LDSYMLEN(pst) || pst->number_of_dependencies)
1086     {
1087       /* Print the message now, before reading the string table,
1088          to avoid disconcerting pauses.  */
1089       if (info_verbose)
1090         {
1091           printf_filtered ("Reading in symbols for %s...", pst->filename);
1092           fflush (stdout);
1093         }
1094
1095       sym_bfd = pst->objfile->obfd;
1096
1097       /* We keep the string table for the main symfile resident in memory, but
1098          not the string table for any other symbol files.  */
1099       if (symfile_objfile != pst->objfile)
1100         {
1101           /* Read in the string table */
1102
1103           /* FIXME, this uses internal BFD variables.  See above in
1104              dbx_symbol_file_open where the macro is defined!  */
1105           bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
1106
1107           val = bfd_read (&st_temp, sizeof st_temp, 1, sym_bfd);
1108           if (val < 0)
1109               perror_with_name (pst->objfile->name);
1110           stsize = bfd_h_get_32 (sym_bfd, (unsigned char *)&st_temp);
1111 #if 0
1112           /* BFD doesn't provide a way to know the total file size, sigh */
1113           struct stat statbuf;
1114           if (fstat (desc, &statbuf) < 0)
1115             perror_with_name (pst->objfile->name);
1116           
1117           if (stsize >= 0 && stsize < statbuf.st_size)
1118 #else
1119           if (stsize >= 0)
1120 #endif
1121             {
1122 #ifdef BROKEN_LARGE_ALLOCA
1123               stringtab = (char *) xmalloc (stsize);
1124               make_cleanup (free, stringtab);
1125 #else
1126               stringtab = (char *) alloca (stsize);
1127 #endif
1128             }
1129           else
1130             stringtab = NULL;
1131           if (stringtab == NULL && stsize != 0)
1132             error ("ridiculous string table size: %d bytes", stsize);
1133
1134           /* FIXME, this uses internal BFD variables.  See above in
1135              dbx_symbol_file_open where the macro is defined!  */
1136           val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
1137           if (val < 0)
1138             perror_with_name (pst->objfile->name);
1139           val = bfd_read (stringtab, stsize, 1, sym_bfd);
1140           if (val < 0)
1141             perror_with_name (pst->objfile->name);
1142         }
1143       else
1144         {
1145           stringtab = symfile_string_table;
1146           stsize = symfile_string_table_size;
1147         }
1148
1149       /* FIXME POKING INSIDE BFD DATA STRUCTURES */
1150       symbol_size = obj_symbol_entry_size (sym_bfd);
1151
1152       /* FIXME, this uses internal BFD variables.  See above in
1153          dbx_symbol_file_open where the macro is defined!  */
1154       psymtab_to_symtab_1 (pst, stringtab, stsize,
1155                            SYMBOL_TABLE_OFFSET);
1156
1157       /* Match with global symbols.  This only needs to be done once,
1158          after all of the symtabs and dependencies have been read in.   */
1159       scan_file_globals ();
1160
1161       /* Finish up the debug error message.  */
1162       if (info_verbose)
1163         printf_filtered ("done.\n");
1164     }
1165 }
1166
1167 /*
1168  * Read in a defined section of a specific object file's symbols.
1169  *
1170  * DESC is the file descriptor for the file, positioned at the
1171  * beginning of the symtab
1172  * STRINGTAB is a pointer to the files string
1173  * table, already read in
1174  * SYM_OFFSET is the offset within the file of
1175  * the beginning of the symbols we want to read, NUM_SUMBOLS is the
1176  * number of symbols to read
1177  * TEXT_OFFSET is the beginning of the text segment we are reading symbols for
1178  * TEXT_SIZE is the size of the text segment read in.
1179  * OFFSET is a relocation offset which gets added to each symbol
1180  */
1181
1182 static struct symtab *
1183 read_ofile_symtab (objfile, stringtab, stringtab_size, sym_offset,
1184                    sym_size, text_offset, text_size, offset)
1185      struct objfile *objfile;
1186      register char *stringtab;
1187      unsigned int stringtab_size;
1188      int sym_offset;
1189      int sym_size;
1190      CORE_ADDR text_offset;
1191      int text_size;
1192      int offset;
1193 {
1194   register char *namestring;
1195   register struct internal_nlist *bufp;
1196   unsigned char type;
1197   unsigned max_symnum;
1198   register bfd *abfd;
1199
1200   subfile_stack = 0;
1201
1202   stringtab_global = stringtab;
1203   last_source_file = 0;
1204
1205   abfd = objfile->obfd;
1206   symfile_bfd = objfile->obfd;  /* Implicit param to next_text_symbol */
1207   our_objfile = objfile;  /* For end_symtab calls in process_one_symbol */
1208   symbuf_end = symbuf_idx = 0;
1209
1210   /* It is necessary to actually read one symbol *before* the start
1211      of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
1212      occurs before the N_SO symbol.
1213
1214      Detecting this in read_dbx_symtab
1215      would slow down initial readin, so we look for it here instead.  */
1216   if (sym_offset >= (int)symbol_size)
1217     {
1218       bfd_seek (symfile_bfd, sym_offset - symbol_size, L_INCR);
1219       fill_symbuf (abfd);
1220       bufp = &symbuf[symbuf_idx++];
1221       SWAP_SYMBOL (bufp, abfd);
1222
1223       SET_NAMESTRING ();
1224
1225       processing_gcc_compilation =
1226         (bufp->n_type == N_TEXT
1227          && !strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL));
1228       /* FIXME!!!  Check for gcc2_compiled... */
1229     }
1230   else
1231     {
1232       /* The N_SO starting this symtab is the first symbol, so we
1233          better not check the symbol before it.  I'm not this can
1234          happen, but it doesn't hurt to check for it.  */
1235       bfd_seek (symfile_bfd, sym_offset, L_INCR);
1236       processing_gcc_compilation = 0;
1237     }
1238
1239   if (symbuf_idx == symbuf_end)
1240     fill_symbuf (abfd);
1241   bufp = &symbuf[symbuf_idx];
1242   if (bufp->n_type != (unsigned char)N_SO)
1243     error("First symbol in segment of executable not a source symbol");
1244
1245   max_symnum = sym_size / symbol_size;
1246
1247   for (symnum = 0;
1248        symnum < max_symnum;
1249        symnum++)
1250     {
1251       QUIT;                     /* Allow this to be interruptable */
1252       if (symbuf_idx == symbuf_end)
1253         fill_symbuf(abfd);
1254       bufp = &symbuf[symbuf_idx++];
1255       SWAP_SYMBOL (bufp, abfd);
1256
1257       type = bufp->n_type;
1258       if (type == (unsigned char)N_CATCH)
1259         {
1260           /* N_CATCH is not fixed up by the linker, and unfortunately,
1261              there's no other place to put it in the .stab map.  */
1262           bufp->n_value += text_offset + offset;
1263         }
1264       else {
1265         type &= ~N_EXT;         /* Ignore external-bit */
1266         if (type == N_TEXT || type == N_DATA || type == N_BSS)
1267           bufp->n_value += offset;
1268         type = bufp->n_type;
1269       }
1270
1271       SET_NAMESTRING ();
1272
1273       if (type & N_STAB) {
1274         process_one_symbol (type, bufp->n_desc, bufp->n_value, namestring);
1275         /* our_objfile is an implicit parameter.  */
1276
1277       }
1278       /* We skip checking for a new .o or -l file; that should never
1279          happen in this routine. */
1280       else if (type == N_TEXT
1281                && !strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL))
1282         /* I don't think this code will ever be executed, because
1283            the GCC_COMPILED_FLAG_SYMBOL usually is right before
1284            the N_SO symbol which starts this source file.
1285            However, there is no reason not to accept
1286            the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
1287         processing_gcc_compilation = 1;
1288       else if (type & N_EXT || type == (unsigned char)N_TEXT
1289                || type == (unsigned char)N_NBTEXT
1290                ) {
1291           /* Global symbol: see if we came across a dbx defintion for
1292              a corresponding symbol.  If so, store the value.  Remove
1293              syms from the chain when their values are stored, but
1294              search the whole chain, as there may be several syms from
1295              different files with the same name. */
1296           /* This is probably not true.  Since the files will be read
1297              in one at a time, each reference to a global symbol will
1298              be satisfied in each file as it appears. So we skip this
1299              section. */
1300           ;
1301         }
1302     }
1303
1304   return end_symtab (text_offset + text_size, 0, 0, objfile);
1305 }
1306 \f
1307 int
1308 hashname (name)
1309      char *name;
1310 {
1311   register char *p = name;
1312   register int total = p[0];
1313   register int c;
1314
1315   c = p[1];
1316   total += c << 2;
1317   if (c)
1318     {
1319       c = p[2];
1320       total += c << 4;
1321       if (c)
1322         total += p[3] << 6;
1323     }
1324
1325   /* Ensure result is positive.  */
1326   if (total < 0) total += (1000 << 6);
1327   return total % HASHSIZE;
1328 }
1329
1330 \f
1331 void
1332 process_one_symbol (type, desc, valu, name)
1333      int type, desc;
1334      CORE_ADDR valu;
1335      char *name;
1336 {
1337 #ifndef SUN_FIXED_LBRAC_BUG
1338   /* This records the last pc address we've seen.  We depend on their being
1339      an SLINE or FUN or SO before the first LBRAC, since the variable does
1340      not get reset in between reads of different symbol files.  */
1341   static CORE_ADDR last_pc_address;
1342 #endif
1343   register struct context_stack *new;
1344   char *colon_pos;
1345
1346   /* Something is wrong if we see real data before
1347      seeing a source file name.  */
1348
1349   if (last_source_file == 0 && type != (unsigned char)N_SO)
1350     {
1351       /* Currently this ignores N_ENTRY on Gould machines, N_NSYM on machines
1352          where that code is defined.  */
1353       if (IGNORE_SYMBOL (type))
1354         return;
1355
1356       /* FIXME, this should not be an error, since it precludes extending
1357          the symbol table information in this way...  */
1358       error ("Invalid symbol data: does not start by identifying a source file.");
1359     }
1360
1361   switch (type)
1362     {
1363     case N_FUN:
1364     case N_FNAME:
1365       /* Either of these types of symbols indicates the start of
1366          a new function.  We must process its "name" normally for dbx,
1367          but also record the start of a new lexical context, and possibly
1368          also the end of the lexical context for the previous function.  */
1369       /* This is not always true.  This type of symbol may indicate a
1370          text segment variable.  */
1371
1372 #ifndef SUN_FIXED_LBRAC_BUG
1373       last_pc_address = valu;   /* Save for SunOS bug circumcision */
1374 #endif
1375
1376       colon_pos = strchr (name, ':');
1377       if (!colon_pos++
1378           || (*colon_pos != 'f' && *colon_pos != 'F'))
1379         {
1380           define_symbol (valu, name, desc, type);
1381           break;
1382         }
1383
1384       within_function = 1;
1385       if (context_stack_depth > 0)
1386         {
1387           new = pop_context ();
1388           /* Make a block for the local symbols within.  */
1389           finish_block (new->name, &local_symbols, new->old_blocks,
1390                         new->start_addr, valu);
1391         }
1392       /* Stack must be empty now.  */
1393       if (context_stack_depth != 0)
1394         complain (lbrac_unmatched_complaint, symnum);
1395
1396       new = push_context (0, valu);
1397       new->name = define_symbol (valu, name, desc, type);
1398       break;
1399
1400     case N_CATCH:
1401       /* Record the address at which this catch takes place.  */
1402       define_symbol (valu, name, desc, type);
1403       break;
1404
1405     case N_EHDECL:
1406       /* Don't know what to do with these yet.  */
1407       error ("action uncertain for eh extensions");
1408       break;
1409
1410     case N_LBRAC:
1411       /* This "symbol" just indicates the start of an inner lexical
1412          context within a function.  */
1413
1414 #if !defined (BLOCK_ADDRESS_ABSOLUTE)
1415       /* On most machines, the block addresses are relative to the
1416          N_SO, the linker did not relocate them (sigh).  */
1417       valu += last_source_start_addr;
1418 #endif
1419
1420 #ifndef SUN_FIXED_LBRAC_BUG
1421       if (valu < last_pc_address) {
1422         /* Patch current LBRAC pc value to match last handy pc value */
1423         complain (&lbrac_complaint, 0);
1424         valu = last_pc_address;
1425       }
1426 #endif
1427       new = push_context (desc, valu);
1428       break;
1429
1430     case N_RBRAC:
1431       /* This "symbol" just indicates the end of an inner lexical
1432          context that was started with N_LBRAC.  */
1433
1434 #if !defined (BLOCK_ADDRESS_ABSOLUTE)
1435       /* On most machines, the block addresses are relative to the
1436          N_SO, the linker did not relocate them (sigh).  */
1437       valu += last_source_start_addr;
1438 #endif
1439
1440       new = pop_context();
1441       if (desc != new->depth)
1442         complain (lbrac_mismatch_complaint, symnum);
1443
1444       /* Some compilers put the variable decls inside of an
1445          LBRAC/RBRAC block.  This macro should be nonzero if this
1446          is true.  DESC is N_DESC from the N_RBRAC symbol.
1447          GCC_P is true if we've detected the GCC_COMPILED_SYMBOL.  */
1448 #if !defined (VARIABLES_INSIDE_BLOCK)
1449 #define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
1450 #endif
1451
1452       /* Can only use new->locals as local symbols here if we're in
1453          gcc or on a machine that puts them before the lbrack.  */
1454       if (!VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1455         local_symbols = new->locals;
1456
1457       /* If this is not the outermost LBRAC...RBRAC pair in the
1458          function, its local symbols preceded it, and are the ones
1459          just recovered from the context stack.  Defined the block for them.
1460
1461          If this is the outermost LBRAC...RBRAC pair, there is no
1462          need to do anything; leave the symbols that preceded it
1463          to be attached to the function's own block.  However, if
1464          it is so, we need to indicate that we just moved outside
1465          of the function.  */
1466       if (local_symbols
1467           && (context_stack_depth
1468               > !VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation)))
1469         {
1470           /* FIXME Muzzle a compiler bug that makes end < start.  */
1471           if (new->start_addr > valu)
1472             {
1473               complain(&lbrac_rbrac_complaint, 0);
1474               new->start_addr = valu;
1475             }
1476           /* Make a block for the local symbols within.  */
1477           finish_block (0, &local_symbols, new->old_blocks,
1478                         new->start_addr, valu);
1479         }
1480       else
1481         {
1482           within_function = 0;
1483         }
1484       if (VARIABLES_INSIDE_BLOCK(desc, processing_gcc_compilation))
1485         /* Now pop locals of block just finished.  */
1486         local_symbols = new->locals;
1487       break;
1488
1489     case N_FN:
1490     case N_FN_SEQ:
1491       /* This kind of symbol indicates the start of an object file.  */
1492       break;
1493
1494     case N_SO:
1495       /* This type of symbol indicates the start of data
1496          for one source file.
1497          Finish the symbol table of the previous source file
1498          (if any) and start accumulating a new symbol table.  */
1499 #ifndef SUN_FIXED_LBRAC_BUG
1500       last_pc_address = valu;   /* Save for SunOS bug circumcision */
1501 #endif
1502   
1503 #ifdef PCC_SOL_BROKEN
1504       /* pcc bug, occasionally puts out SO for SOL.  */
1505       if (context_stack_depth > 0)
1506         {
1507           start_subfile (name, NULL);
1508           break;
1509         }
1510 #endif
1511       if (last_source_file)
1512         {
1513           /* Check if previous symbol was also an N_SO (with some
1514              sanity checks).  If so, that one was actually the directory
1515              name, and the current one is the real file name.
1516              Patch things up. */           
1517           if (previous_stab_code == N_SO
1518               && current_subfile && current_subfile->dirname == NULL
1519               && current_subfile->name != NULL
1520               && current_subfile->name[strlen(current_subfile->name)-1] == '/')
1521             {
1522               current_subfile->dirname = current_subfile->name;
1523               current_subfile->name = obsavestring (name, strlen (name));
1524               break;
1525             }
1526           (void)end_symtab (valu, 0, 0);
1527         }
1528       start_symtab (name, NULL, valu);
1529       break;
1530
1531     case N_SOL:
1532       /* This type of symbol indicates the start of data for
1533          a sub-source-file, one whose contents were copied or
1534          included in the compilation of the main source file
1535          (whose name was given in the N_SO symbol.)  */
1536       start_subfile (name, NULL);
1537       break;
1538
1539     case N_BINCL:
1540       push_subfile ();
1541       add_new_header_file (name, valu);
1542       start_subfile (name, NULL);
1543       break;
1544
1545     case N_EINCL:
1546       start_subfile (pop_subfile (), NULL);
1547       break;
1548
1549     case N_EXCL:
1550       add_old_header_file (name, valu);
1551       break;
1552
1553     case N_SLINE:
1554       /* This type of "symbol" really just records
1555          one line-number -- core-address correspondence.
1556          Enter it in the line list for this symbol table.  */
1557 #ifndef SUN_FIXED_LBRAC_BUG
1558       last_pc_address = valu;   /* Save for SunOS bug circumcision */
1559 #endif
1560       record_line (current_subfile, desc, valu);
1561       break;
1562
1563     case N_BCOMM:
1564       if (common_block)
1565         error ("Invalid symbol data: common within common at symtab pos %d",
1566                symnum);
1567       common_block = local_symbols;
1568       common_block_i = local_symbols ? local_symbols->nsyms : 0;
1569       break;
1570
1571     case N_ECOMM:
1572       /* Symbols declared since the BCOMM are to have the common block
1573          start address added in when we know it.  common_block points to
1574          the first symbol after the BCOMM in the local_symbols list;
1575          copy the list and hang it off the symbol for the common block name
1576          for later fixup.  */
1577       {
1578         int i;
1579         struct symbol *sym =
1580           (struct symbol *) xmalloc (sizeof (struct symbol));
1581         bzero (sym, sizeof *sym);
1582         SYMBOL_NAME (sym) = savestring (name, strlen (name));
1583         SYMBOL_CLASS (sym) = LOC_BLOCK;
1584         SYMBOL_NAMESPACE (sym) = (enum namespace)((long)
1585           copy_pending (local_symbols, common_block_i, common_block));
1586         i = hashname (SYMBOL_NAME (sym));
1587         SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1588         global_sym_chain[i] = sym;
1589         common_block = 0;
1590         break;
1591       }
1592
1593     case N_ECOML:
1594     case N_LENG:
1595     case N_DEFD:                /* GNU Modula-2 symbol */
1596       break;
1597
1598     default:
1599       if (name)
1600         define_symbol (valu, name, desc, type);
1601     }
1602
1603   previous_stab_code = type;
1604 }
1605 \f
1606 /* Copy a pending list, used to record the contents of a common
1607    block for later fixup.  */
1608 static struct pending *
1609 copy_pending (beg, begi, end)
1610     struct pending *beg, *end;
1611     int begi;
1612 {
1613   struct pending *new = 0;
1614   struct pending *next;
1615
1616   for (next = beg; next != 0 && (next != end || begi < end->nsyms);
1617        next = next->next, begi = 0)
1618     {
1619       register int j;
1620       for (j = begi; j < next->nsyms; j++)
1621         add_symbol_to_list (next->symbol[j], &new);
1622     }
1623   return new;
1624 }
1625 \f
1626 /* Register our willingness to decode symbols for SunOS and a.out and
1627    b.out files handled by BFD... */
1628 static struct sym_fns sunos_sym_fns = {"sunOs", 6,
1629               dbx_new_init, dbx_symfile_init, dbx_symfile_read};
1630
1631 static struct sym_fns aout_sym_fns = {"a.out", 5,
1632               dbx_new_init, dbx_symfile_init, dbx_symfile_read};
1633
1634 static struct sym_fns bout_sym_fns = {"b.out", 5,
1635               dbx_new_init, dbx_symfile_init, dbx_symfile_read};
1636
1637 void
1638 _initialize_dbxread ()
1639 {
1640   add_symtab_fns(&sunos_sym_fns);
1641   add_symtab_fns(&aout_sym_fns);
1642   add_symtab_fns(&bout_sym_fns);
1643 }