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