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