* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / buildsym.c
1 /* Support routines for building symbol tables in GDB's internal format.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1995, 1996
3              Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* This module provides subroutines used for creating and adding to
22    the symbol table.  These routines are called from various symbol-
23    file-reading routines.
24
25    Routines to support specific debugging information formats (stabs,
26    DWARF, etc) belong somewhere else. */
27
28 #include "defs.h"
29 #include "bfd.h"
30 #include "obstack.h"
31 #include "symtab.h"
32 #include "symfile.h"            /* Needed for "struct complaint" */
33 #include "objfiles.h"
34 #include "gdbtypes.h"
35 #include "complaints.h"
36 #include "gdb_string.h"
37
38 /* Ask buildsym.h to define the vars it normally declares `extern'.  */
39 #define EXTERN  /**/
40 #include "buildsym.h"           /* Our own declarations */
41 #undef  EXTERN
42
43 /* For cleanup_undefined_types and finish_global_stabs (somewhat
44    questionable--see comment where we call them).  */
45 #include "stabsread.h"
46
47 /* List of free `struct pending' structures for reuse.  */
48
49 static struct pending *free_pendings;
50
51 /* Non-zero if symtab has line number info.  This prevents an otherwise empty
52    symtab from being tossed.  */
53
54 static int have_line_numbers;
55 \f
56 static int
57 compare_line_numbers PARAMS ((const void *, const void *));
58
59 \f
60 /* Initial sizes of data structures.  These are realloc'd larger if needed,
61    and realloc'd down to the size actually used, when completed.  */
62
63 #define INITIAL_CONTEXT_STACK_SIZE      10
64 #define INITIAL_LINE_VECTOR_LENGTH      1000
65
66 \f
67 /* Complaints about the symbols we have encountered.  */
68
69 struct complaint block_end_complaint =
70   {"block end address less than block start address in %s (patched it)", 0, 0};
71
72 struct complaint anon_block_end_complaint =
73   {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0};
74
75 struct complaint innerblock_complaint =
76   {"inner block not inside outer block in %s", 0, 0};
77
78 struct complaint innerblock_anon_complaint =
79   {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0};
80
81 struct complaint blockvector_complaint = 
82   {"block at 0x%lx out of order", 0, 0};
83
84 \f
85 /* maintain the lists of symbols and blocks */
86
87 /* Add a symbol to one of the lists of symbols.  */
88
89 void
90 add_symbol_to_list (symbol, listhead)
91      struct symbol *symbol;
92      struct pending **listhead;
93 {
94   register struct pending *link;
95
96   /* If this is an alias for another symbol, don't add it.  */
97   if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
98     return;
99       
100   /* We keep PENDINGSIZE symbols in each link of the list.
101      If we don't have a link with room in it, add a new link.  */
102   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
103     {
104       if (free_pendings)
105         {
106           link = free_pendings;
107           free_pendings = link->next;
108         }
109       else
110         {
111           link = (struct pending *) xmalloc (sizeof (struct pending));
112         }
113
114       link->next = *listhead;
115       *listhead = link;
116       link->nsyms = 0;
117     }
118
119   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
120 }
121
122 /* Find a symbol named NAME on a LIST.  NAME need not be '\0'-terminated;
123    LENGTH is the length of the name.  */
124
125 struct symbol *
126 find_symbol_in_list (list, name, length)
127      struct pending *list;
128      char *name;
129      int length;
130 {
131   int j;
132   char *pp;
133
134   while (list != NULL)
135     {
136       for (j = list->nsyms; --j >= 0; )
137         {
138           pp = SYMBOL_NAME (list->symbol[j]);
139           if (*pp == *name && strncmp (pp, name, length) == 0 &&
140               pp[length] == '\0')
141             {
142               return (list->symbol[j]);
143             }
144         }
145       list = list->next;
146     }
147   return (NULL);
148 }
149
150 /* At end of reading syms, or in case of quit,
151    really free as many `struct pending's as we can easily find. */
152
153 /* ARGSUSED */
154 void
155 really_free_pendings (foo)
156      int foo;
157 {
158   struct pending *next, *next1;
159
160   for (next = free_pendings; next; next = next1)
161     {
162       next1 = next->next;
163       free ((PTR)next);
164     }
165   free_pendings = NULL;
166
167   free_pending_blocks ();
168
169   for (next = file_symbols; next != NULL; next = next1)
170     {
171       next1 = next->next;
172       free ((PTR)next);
173     }
174   file_symbols = NULL;
175
176   for (next = global_symbols; next != NULL; next = next1)
177     {
178       next1 = next->next;
179       free ((PTR)next);
180     }
181   global_symbols = NULL;
182 }
183
184 /* This function is called to discard any pending blocks. */
185
186 void
187 free_pending_blocks ()
188 {
189 #if 0 /* Now we make the links in the symbol_obstack, so don't free them.  */
190   struct pending_block *bnext, *bnext1;
191
192   for (bnext = pending_blocks; bnext; bnext = bnext1)
193     {
194       bnext1 = bnext->next;
195       free ((PTR)bnext);
196     }
197 #endif
198   pending_blocks = NULL;
199 }
200
201 /* Take one of the lists of symbols and make a block from it.
202    Keep the order the symbols have in the list (reversed from the input file).
203    Put the block on the list of pending blocks.  */
204
205 void
206 finish_block (symbol, listhead, old_blocks, start, end, objfile)
207      struct symbol *symbol;
208      struct pending **listhead;
209      struct pending_block *old_blocks;
210      CORE_ADDR start, end;
211      struct objfile *objfile;
212 {
213   register struct pending *next, *next1;
214   register struct block *block;
215   register struct pending_block *pblock;
216   struct pending_block *opblock;
217   register int i;
218   register int j;
219
220   /* Count the length of the list of symbols.  */
221
222   for (next = *listhead, i = 0;
223        next;
224        i += next->nsyms, next = next->next)
225     {
226       /*EMPTY*/;
227     }
228
229   block = (struct block *) obstack_alloc (&objfile -> symbol_obstack,
230           (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
231
232   /* Copy the symbols into the block.  */
233
234   BLOCK_NSYMS (block) = i;
235   for (next = *listhead; next; next = next->next)
236     {
237       for (j = next->nsyms - 1; j >= 0; j--)
238         {
239           BLOCK_SYM (block, --i) = next->symbol[j];
240         }
241     }
242
243   BLOCK_START (block) = start;
244   BLOCK_END (block) = end;
245  /* Superblock filled in when containing block is made */
246   BLOCK_SUPERBLOCK (block) = NULL;
247   BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
248
249   /* Put the block in as the value of the symbol that names it.  */
250
251   if (symbol)
252     {
253       struct type *ftype = SYMBOL_TYPE (symbol);
254       SYMBOL_BLOCK_VALUE (symbol) = block;
255       BLOCK_FUNCTION (block) = symbol;
256
257       if (TYPE_NFIELDS (ftype) <= 0)
258         {
259           /* No parameter type information is recorded with the function's
260              type.  Set that from the type of the parameter symbols. */
261           int nparams = 0, iparams;
262           struct symbol *sym;
263           for (i = 0; i < BLOCK_NSYMS (block); i++)
264             {
265               sym = BLOCK_SYM (block, i);
266               switch (SYMBOL_CLASS (sym))
267                 {
268                 case LOC_ARG:
269                 case LOC_REF_ARG:
270                 case LOC_REGPARM:
271                 case LOC_REGPARM_ADDR:
272                 case LOC_BASEREG_ARG:
273                 case LOC_LOCAL_ARG:
274                   nparams++;
275                   break;
276                 case LOC_UNDEF:
277                 case LOC_CONST:
278                 case LOC_STATIC:
279                 case LOC_REGISTER:
280                 case LOC_LOCAL:
281                 case LOC_TYPEDEF:
282                 case LOC_LABEL:
283                 case LOC_BLOCK:
284                 case LOC_CONST_BYTES:
285                 case LOC_BASEREG:
286                 case LOC_UNRESOLVED:
287                 case LOC_OPTIMIZED_OUT:
288                 default:
289                   break;
290                 }
291             }
292           if (nparams > 0)
293             {
294               TYPE_NFIELDS (ftype) = nparams;
295               TYPE_FIELDS (ftype) = (struct field *)
296                 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
297                                                 
298               for (i = iparams = 0; iparams < nparams; i++)
299                 {
300                   sym = BLOCK_SYM (block, i);
301                   switch (SYMBOL_CLASS (sym))
302                     {
303                     case LOC_ARG:
304                     case LOC_REF_ARG:
305                     case LOC_REGPARM:
306                     case LOC_REGPARM_ADDR:
307                     case LOC_BASEREG_ARG:
308                     case LOC_LOCAL_ARG:
309                       TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
310                       iparams++;
311                       break;
312                     case LOC_UNDEF:
313                     case LOC_CONST:
314                     case LOC_STATIC:
315                     case LOC_REGISTER:
316                     case LOC_LOCAL:
317                     case LOC_TYPEDEF:
318                     case LOC_LABEL:
319                     case LOC_BLOCK:
320                     case LOC_CONST_BYTES:
321                     case LOC_BASEREG:
322                     case LOC_UNRESOLVED:
323                     case LOC_OPTIMIZED_OUT:
324                     default:
325                       break;
326                     }
327                 }
328             }
329         }
330     }
331   else
332     {
333       BLOCK_FUNCTION (block) = NULL;
334     }
335
336   /* Now "free" the links of the list, and empty the list.  */
337
338   for (next = *listhead; next; next = next1)
339     {
340       next1 = next->next;
341       next->next = free_pendings;
342       free_pendings = next;
343     }
344   *listhead = NULL;
345
346 #if 1
347   /* Check to be sure that the blocks have an end address that is
348      greater than starting address */
349
350   if (BLOCK_END (block) < BLOCK_START (block))
351     {
352       if (symbol)
353         {
354           complain (&block_end_complaint, SYMBOL_SOURCE_NAME (symbol));
355         }
356       else
357         {
358           complain (&anon_block_end_complaint, BLOCK_END (block), BLOCK_START (block));
359         }
360       /* Better than nothing */
361       BLOCK_END (block) = BLOCK_START (block);
362     }
363 #endif
364
365   /* Install this block as the superblock
366      of all blocks made since the start of this scope
367      that don't have superblocks yet.  */
368
369   opblock = NULL;
370   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
371     {
372       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
373         {
374 #if 1
375           /* Check to be sure the blocks are nested as we receive them. 
376              If the compiler/assembler/linker work, this just burns a small
377              amount of time.  */
378           if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
379               BLOCK_END   (pblock->block) > BLOCK_END   (block))
380             {
381               if (symbol)
382                 {
383                   complain (&innerblock_complaint,
384                             SYMBOL_SOURCE_NAME (symbol));
385                 }
386               else
387                 {
388                   complain (&innerblock_anon_complaint, BLOCK_START (pblock->block),
389                             BLOCK_END (pblock->block), BLOCK_START (block),
390                             BLOCK_END (block));
391                 }
392               if (BLOCK_START (pblock->block) < BLOCK_START (block))
393                 BLOCK_START (pblock->block) = BLOCK_START (block);
394               if (BLOCK_END (pblock->block) > BLOCK_END (block))
395                 BLOCK_END (pblock->block) = BLOCK_END (block);
396             }
397 #endif
398           BLOCK_SUPERBLOCK (pblock->block) = block;
399         }
400       opblock = pblock;
401     }
402
403   record_pending_block (objfile, block, opblock);
404 }
405
406 /* Record BLOCK on the list of all blocks in the file.  Put it after
407    OPBLOCK, or at the beginning if opblock is NULL.  This puts the block
408    in the list after all its subblocks.
409
410    Allocate the pending block struct in the symbol_obstack to save
411    time.  This wastes a little space.  FIXME: Is it worth it?  */
412
413 void
414 record_pending_block (objfile, block, opblock)
415      struct objfile* objfile;
416      struct block *block;
417      struct pending_block *opblock;
418 {
419   register struct pending_block *pblock;
420
421   pblock = (struct pending_block *)
422     obstack_alloc (&objfile -> symbol_obstack, sizeof (struct pending_block));
423   pblock -> block = block;
424   if (opblock)
425     {
426       pblock -> next = opblock -> next;
427       opblock -> next = pblock;
428     }
429   else
430     {
431       pblock -> next = pending_blocks;
432       pending_blocks = pblock;
433     }
434 }
435
436 /* Note that this is only used in this file and in dstread.c, which should be
437    fixed to not need direct access to this function.  When that is done, it can
438    be made static again. */
439
440 struct blockvector *
441 make_blockvector (objfile)
442      struct objfile *objfile;
443 {
444   register struct pending_block *next;
445   register struct blockvector *blockvector;
446   register int i;
447
448   /* Count the length of the list of blocks.  */
449
450   for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
451
452   blockvector = (struct blockvector *)
453     obstack_alloc (&objfile -> symbol_obstack,
454                    (sizeof (struct blockvector)
455                     + (i - 1) * sizeof (struct block *)));
456
457   /* Copy the blocks into the blockvector.
458      This is done in reverse order, which happens to put
459      the blocks into the proper order (ascending starting address).
460      finish_block has hair to insert each block into the list
461      after its subblocks in order to make sure this is true.  */
462
463   BLOCKVECTOR_NBLOCKS (blockvector) = i;
464   for (next = pending_blocks; next; next = next->next)
465     {
466       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
467     }
468
469 #if 0 /* Now we make the links in the obstack, so don't free them.  */
470   /* Now free the links of the list, and empty the list.  */
471
472   for (next = pending_blocks; next; next = next1)
473     {
474       next1 = next->next;
475       free (next);
476     }
477 #endif
478   pending_blocks = NULL;
479
480 #if 1  /* FIXME, shut this off after a while to speed up symbol reading.  */
481   /* Some compilers output blocks in the wrong order, but we depend
482      on their being in the right order so we can binary search. 
483      Check the order and moan about it.  FIXME.  */
484   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
485     {
486       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
487         {
488           if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
489               > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
490             {
491
492               /* FIXME-32x64: loses if CORE_ADDR doesn't fit in a
493                  long.  Possible solutions include a version of
494                  complain which takes a callback, a
495                  sprintf_address_numeric to match
496                  print_address_numeric, or a way to set up a GDB_FILE
497                  * which causes sprintf rather than fprintf to be
498                  called.  */
499
500               complain (&blockvector_complaint, 
501                         (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
502             }
503         }
504     }
505 #endif
506
507   return (blockvector);
508 }
509
510 \f
511 /* Start recording information about source code that came from an included
512    (or otherwise merged-in) source file with a different name.  NAME is
513    the name of the file (cannot be NULL), DIRNAME is the directory in which
514    it resides (or NULL if not known).  */
515
516 void
517 start_subfile (name, dirname)
518      char *name;
519      char *dirname;
520 {
521   register struct subfile *subfile;
522
523   /* See if this subfile is already known as a subfile of the
524      current main source file.  */
525
526   for (subfile = subfiles; subfile; subfile = subfile->next)
527     {
528       if (STREQ (subfile->name, name))
529         {
530           current_subfile = subfile;
531           return;
532         }
533     }
534
535   /* This subfile is not known.  Add an entry for it.
536      Make an entry for this subfile in the list of all subfiles
537      of the current main source file.  */
538
539   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
540   subfile->next = subfiles;
541   subfiles = subfile;
542   current_subfile = subfile;
543
544   /* Save its name and compilation directory name */
545   subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
546   subfile->dirname =
547     (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
548   
549   /* Initialize line-number recording for this subfile.  */
550   subfile->line_vector = NULL;
551
552   /* Default the source language to whatever can be deduced from
553      the filename.  If nothing can be deduced (such as for a C/C++
554      include file with a ".h" extension), then inherit whatever
555      language the previous subfile had.  This kludgery is necessary
556      because there is no standard way in some object formats to
557      record the source language.  Also, when symtabs are allocated
558      we try to deduce a language then as well, but it is too late
559      for us to use that information while reading symbols, since
560      symtabs aren't allocated until after all the symbols have
561      been processed for a given source file. */
562
563   subfile->language = deduce_language_from_filename (subfile->name);
564   if (subfile->language == language_unknown &&
565       subfile->next != NULL)
566     {
567       subfile->language = subfile->next->language;
568     }
569
570   /* Initialize the debug format string to NULL.  We may supply it
571      later via a call to record_debugformat. */
572   subfile->debugformat = NULL;
573
574   /* cfront output is a C program, so in most ways it looks like a C
575      program.  But to demangle we need to set the language to C++.  We
576      can distinguish cfront code by the fact that it has #line
577      directives which specify a file name ending in .C.
578
579      So if the filename of this subfile ends in .C, then change the language
580      of any pending subfiles from C to C++.  We also accept any other C++
581      suffixes accepted by deduce_language_from_filename (in particular,
582      some people use .cxx with cfront).  */
583   /* Likewise for f2c.  */
584
585   if (subfile->name)
586     {
587       struct subfile *s;
588       enum language sublang = deduce_language_from_filename (subfile->name);
589
590       if (sublang == language_cplus || sublang == language_fortran)
591         for (s = subfiles; s != NULL; s = s->next)
592           if (s->language == language_c)
593             s->language = sublang;
594     }
595
596   /* And patch up this file if necessary.  */
597   if (subfile->language == language_c
598       && subfile->next != NULL
599       && (subfile->next->language == language_cplus
600           || subfile->next->language == language_fortran))
601     {
602       subfile->language = subfile->next->language;
603     }
604 }
605
606 /* For stabs readers, the first N_SO symbol is assumed to be the source
607    file name, and the subfile struct is initialized using that assumption.
608    If another N_SO symbol is later seen, immediately following the first
609    one, then the first one is assumed to be the directory name and the
610    second one is really the source file name.
611
612    So we have to patch up the subfile struct by moving the old name value to
613    dirname and remembering the new name.  Some sanity checking is performed
614    to ensure that the state of the subfile struct is reasonable and that the
615    old name we are assuming to be a directory name actually is (by checking
616    for a trailing '/'). */
617
618 void
619 patch_subfile_names (subfile, name)
620      struct subfile *subfile;
621      char *name;
622 {
623   if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
624       && subfile->name[strlen(subfile->name)-1] == '/')
625     {
626       subfile->dirname = subfile->name;
627       subfile->name = savestring (name, strlen (name));
628       last_source_file = name;
629
630       /* Default the source language to whatever can be deduced from
631          the filename.  If nothing can be deduced (such as for a C/C++
632          include file with a ".h" extension), then inherit whatever
633          language the previous subfile had.  This kludgery is necessary
634          because there is no standard way in some object formats to
635          record the source language.  Also, when symtabs are allocated
636          we try to deduce a language then as well, but it is too late
637          for us to use that information while reading symbols, since
638          symtabs aren't allocated until after all the symbols have
639          been processed for a given source file. */
640
641       subfile->language = deduce_language_from_filename (subfile->name);
642       if (subfile->language == language_unknown &&
643           subfile->next != NULL)
644         {
645           subfile->language = subfile->next->language;
646         }
647     }
648 }
649
650 \f
651 /* Handle the N_BINCL and N_EINCL symbol types
652    that act like N_SOL for switching source files
653    (different subfiles, as we call them) within one object file,
654    but using a stack rather than in an arbitrary order.  */
655
656 void
657 push_subfile ()
658 {
659   register struct subfile_stack *tem
660     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
661
662   tem->next = subfile_stack;
663   subfile_stack = tem;
664   if (current_subfile == NULL || current_subfile->name == NULL)
665     {
666       abort ();
667     }
668   tem->name = current_subfile->name;
669 }
670
671 char *
672 pop_subfile ()
673 {
674   register char *name;
675   register struct subfile_stack *link = subfile_stack;
676
677   if (link == NULL)
678     {
679       abort ();
680     }
681   name = link->name;
682   subfile_stack = link->next;
683   free ((PTR)link);
684   return (name);
685 }
686
687 \f
688 /* Add a linetable entry for line number LINE and address PC to the line
689    vector for SUBFILE.  */
690
691 void
692 record_line (subfile, line, pc)
693      register struct subfile *subfile;
694      int line;
695      CORE_ADDR pc;
696 {
697   struct linetable_entry *e;
698   /* Ignore the dummy line number in libg.o */
699
700   if (line == 0xffff)
701     {
702       return;
703     }
704
705   /* Make sure line vector exists and is big enough.  */
706   if (!subfile->line_vector)
707     {
708       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
709       subfile->line_vector = (struct linetable *)
710         xmalloc (sizeof (struct linetable)
711           + subfile->line_vector_length * sizeof (struct linetable_entry));
712       subfile->line_vector->nitems = 0;
713       have_line_numbers = 1;
714     }
715
716   if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
717     {
718       subfile->line_vector_length *= 2;
719       subfile->line_vector = (struct linetable *)
720         xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
721           + subfile->line_vector_length * sizeof (struct linetable_entry)));
722     }
723
724   e = subfile->line_vector->item + subfile->line_vector->nitems++;
725   e->line = line; e->pc = pc;
726 }
727
728
729 /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
730
731 static int
732 compare_line_numbers (ln1p, ln2p)
733      const void *ln1p;
734      const void *ln2p;
735 {
736   struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
737   struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
738
739   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
740      Please keep it that way.  */
741   if (ln1->pc < ln2->pc)
742     return -1;
743
744   if (ln1->pc > ln2->pc)
745     return 1;
746
747   /* If pc equal, sort by line.  I'm not sure whether this is optimum
748      behavior (see comment at struct linetable in symtab.h).  */
749   return ln1->line - ln2->line;
750 }
751
752 \f
753 /* Start a new symtab for a new source file.
754    Called, for example, when a stabs symbol of type N_SO is seen, or when
755    a DWARF TAG_compile_unit DIE is seen.
756    It indicates the start of data for one original source file.  */
757
758 void
759 start_symtab (name, dirname, start_addr)
760      char *name;
761      char *dirname;
762      CORE_ADDR start_addr;
763 {
764
765   last_source_file = name;
766   last_source_start_addr = start_addr;
767   file_symbols = NULL;
768   global_symbols = NULL;
769   within_function = 0;
770   have_line_numbers = 0;
771
772   /* Context stack is initially empty.  Allocate first one with room for
773      10 levels; reuse it forever afterward.  */
774   if (context_stack == NULL)
775     {
776       context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
777       context_stack = (struct context_stack *)
778         xmalloc (context_stack_size * sizeof (struct context_stack));
779     }
780   context_stack_depth = 0;
781
782   /* Initialize the list of sub source files with one entry
783      for this file (the top-level source file).  */
784
785   subfiles = NULL;
786   current_subfile = NULL;
787   start_subfile (name, dirname);
788 }
789
790 /* Finish the symbol definitions for one main source file,
791    close off all the lexical contexts for that file
792    (creating struct block's for them), then make the struct symtab
793    for that file and put it in the list of all such.
794
795    END_ADDR is the address of the end of the file's text.
796    SECTION is the section number (in objfile->section_offsets) of
797    the blockvector and linetable.
798
799    Note that it is possible for end_symtab() to return NULL.  In particular,
800    for the DWARF case at least, it will return NULL when it finds a
801    compilation unit that has exactly one DIE, a TAG_compile_unit DIE.  This
802    can happen when we link in an object file that was compiled from an empty
803    source file.  Returning NULL is probably not the correct thing to do,
804    because then gdb will never know about this empty file (FIXME). */
805
806 struct symtab *
807 end_symtab (end_addr, objfile, section)
808      CORE_ADDR end_addr;
809      struct objfile *objfile;
810      int section;
811 {
812   register struct symtab *symtab = NULL;
813   register struct blockvector *blockvector;
814   register struct subfile *subfile;
815   register struct context_stack *cstk;
816   struct subfile *nextsub;
817
818   /* Finish the lexical context of the last function in the file;
819      pop the context stack.  */
820
821   if (context_stack_depth > 0)
822     {
823       cstk = pop_context();
824       /* Make a block for the local symbols within.  */
825       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
826                     cstk->start_addr, end_addr, objfile);
827
828       if (context_stack_depth > 0)
829         {
830           /* This is said to happen with SCO.  The old coffread.c code
831              simply emptied the context stack, so we do the same.  FIXME:
832              Find out why it is happening.  This is not believed to happen
833              in most cases (even for coffread.c); it used to be an abort().  */
834           static struct complaint msg =
835             {"Context stack not empty in end_symtab", 0, 0};
836           complain (&msg);
837           context_stack_depth = 0;
838         }
839     }
840
841   /* Reordered executables may have out of order pending blocks; if
842      OBJF_REORDERED is true, then sort the pending blocks.  */
843   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
844     {
845       /* FIXME!  Remove this horrid bubble sort and use merge sort!!! */
846       int swapped;
847       do
848         {
849           struct pending_block *pb, *pbnext;
850           
851           pb = pending_blocks;
852           pbnext = pb->next;
853           swapped = 0;
854
855           while (pbnext)
856             {
857               /* swap blocks if unordered! */
858           
859               if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block)) 
860                 {
861                   struct block *tmp = pb->block;
862                   pb->block = pbnext->block;
863                   pbnext->block = tmp;
864                   swapped = 1;
865                 }
866               pb = pbnext;
867               pbnext = pbnext->next;
868             }
869         } while (swapped);
870     }
871
872   /* Cleanup any undefined types that have been left hanging around
873      (this needs to be done before the finish_blocks so that
874      file_symbols is still good).
875
876      Both cleanup_undefined_types and finish_global_stabs are stabs
877      specific, but harmless for other symbol readers, since on gdb
878      startup or when finished reading stabs, the state is set so these
879      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
880      we make this cleaner?  */
881
882   cleanup_undefined_types ();
883   finish_global_stabs (objfile);
884
885   if (pending_blocks == NULL
886       && file_symbols == NULL
887       && global_symbols == NULL
888       && have_line_numbers == 0)
889     {
890       /* Ignore symtabs that have no functions with real debugging info */
891       blockvector = NULL;
892     }
893   else
894     {
895       /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
896       finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
897                     objfile);
898       finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
899                     objfile);
900       blockvector = make_blockvector (objfile);
901     }
902
903 #ifdef PROCESS_LINENUMBER_HOOK
904   PROCESS_LINENUMBER_HOOK ();                   /* Needed for xcoff. */
905 #endif
906
907   /* Now create the symtab objects proper, one for each subfile.  */
908   /* (The main file is the last one on the chain.)  */
909
910   for (subfile = subfiles; subfile; subfile = nextsub)
911     {
912       int linetablesize = 0;
913       /* If we have blocks of symbols, make a symtab.
914          Otherwise, just ignore this file and any line number info in it.  */
915       symtab = NULL;
916       if (blockvector)
917         {
918           if (subfile->line_vector)
919             {
920               linetablesize = sizeof (struct linetable) +
921                 subfile->line_vector->nitems * sizeof (struct linetable_entry);
922 #if 0
923               /* I think this is artifact from before it went on the obstack.
924                  I doubt we'll need the memory between now and when we
925                  free it later in this function.  */
926               /* First, shrink the linetable to make more memory.  */
927               subfile->line_vector = (struct linetable *)
928                 xrealloc ((char *) subfile->line_vector, linetablesize);
929 #endif
930
931               /* Like the pending blocks, the line table may be scrambled
932                  in reordered executables.  Sort it if OBJF_REORDERED is
933                  true.  */
934               if (objfile->flags & OBJF_REORDERED)
935                 qsort (subfile->line_vector->item,
936                        subfile->line_vector->nitems,
937                        sizeof (struct linetable_entry), compare_line_numbers);
938             }
939
940           /* Now, allocate a symbol table.  */
941           symtab = allocate_symtab (subfile->name, objfile);
942
943           /* Fill in its components.  */
944           symtab->blockvector = blockvector;
945           if (subfile->line_vector)
946             {
947               /* Reallocate the line table on the symbol obstack */
948               symtab->linetable = (struct linetable *) 
949                 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
950               memcpy (symtab->linetable, subfile->line_vector, linetablesize);
951             }
952           else
953             {
954               symtab->linetable = NULL;
955             }
956           symtab->block_line_section = section;
957           if (subfile->dirname)
958             {
959               /* Reallocate the dirname on the symbol obstack */
960               symtab->dirname = (char *)
961                 obstack_alloc (&objfile -> symbol_obstack,
962                                strlen (subfile -> dirname) + 1);
963               strcpy (symtab->dirname, subfile->dirname);
964             }
965           else
966             {
967               symtab->dirname = NULL;
968             }
969           symtab->free_code = free_linetable;
970           symtab->free_ptr = NULL;
971
972           /* Use whatever language we have been using for this subfile,
973              not the one that was deduced in allocate_symtab from the
974              filename.  We already did our own deducing when we created
975              the subfile, and we may have altered our opinion of what
976              language it is from things we found in the symbols. */
977           symtab->language = subfile->language;
978
979           /* Save the debug format string (if any) in the symtab */
980           if (subfile -> debugformat != NULL)
981             {
982               symtab->debugformat = obsavestring (subfile->debugformat,
983                                                   strlen (subfile->debugformat),
984                                                   &objfile -> symbol_obstack);
985             }
986
987           /* All symtabs for the main file and the subfiles share a
988              blockvector, so we need to clear primary for everything but
989              the main file.  */
990
991           symtab->primary = 0;
992         }
993       if (subfile->name != NULL)
994         {
995           free ((PTR) subfile->name);
996         }
997       if (subfile->dirname != NULL)
998         {
999           free ((PTR) subfile->dirname);
1000         }
1001       if (subfile->line_vector != NULL)
1002         {
1003           free ((PTR) subfile->line_vector);
1004         }
1005       if (subfile->debugformat != NULL)
1006         {
1007           free ((PTR) subfile->debugformat);
1008         }
1009
1010       nextsub = subfile->next;
1011       free ((PTR)subfile);
1012     }
1013
1014   /* Set this for the main source file.  */
1015   if (symtab)
1016     {
1017       symtab->primary = 1;
1018     }
1019
1020   last_source_file = NULL;
1021   current_subfile = NULL;
1022
1023   return (symtab);
1024 }
1025
1026
1027 /* Push a context block.  Args are an identifying nesting level (checkable
1028    when you pop it), and the starting PC address of this context.  */
1029
1030 struct context_stack *
1031 push_context (desc, valu)
1032      int desc;
1033      CORE_ADDR valu;
1034 {
1035   register struct context_stack *new;
1036
1037   if (context_stack_depth == context_stack_size)
1038     {
1039       context_stack_size *= 2;
1040       context_stack = (struct context_stack *)
1041         xrealloc ((char *) context_stack,
1042                   (context_stack_size * sizeof (struct context_stack)));
1043     }
1044
1045   new = &context_stack[context_stack_depth++];
1046   new->depth = desc;
1047   new->locals = local_symbols;
1048   new->old_blocks = pending_blocks;
1049   new->start_addr = valu;
1050   new->name = NULL;
1051
1052   local_symbols = NULL;
1053
1054   return (new);
1055 }
1056
1057 \f
1058 /* Compute a small integer hash code for the given name. */
1059
1060 int
1061 hashname (name)
1062      char *name;
1063 {
1064   register char *p = name;
1065   register int total = p[0];
1066   register int c;
1067
1068   c = p[1];
1069   total += c << 2;
1070   if (c)
1071     {
1072       c = p[2];
1073       total += c << 4;
1074       if (c)
1075         {
1076           total += p[3] << 6;
1077         }
1078     }
1079
1080   /* Ensure result is positive.  */
1081   if (total < 0)
1082     {
1083       total += (1000 << 6);
1084     }
1085   return (total % HASHSIZE);
1086 }
1087
1088 \f
1089 void
1090 record_debugformat (format)
1091      char *format;
1092 {
1093   current_subfile -> debugformat = savestring (format, strlen (format));
1094 }
1095
1096 \f
1097 /* Initialize anything that needs initializing when starting to read
1098    a fresh piece of a symbol file, e.g. reading in the stuff corresponding
1099    to a psymtab.  */
1100
1101 void
1102 buildsym_init ()
1103 {
1104   free_pendings = NULL;
1105   file_symbols = NULL;
1106   global_symbols = NULL;
1107   pending_blocks = NULL;
1108 }
1109
1110 /* Initialize anything that needs initializing when a completely new
1111    symbol file is specified (not just adding some symbols from another
1112    file, e.g. a shared library).  */
1113
1114 void
1115 buildsym_new_init ()
1116 {
1117   buildsym_init ();
1118 }
1119
1120 /* Initializer for this module */
1121
1122 void
1123 _initialize_buildsym ()
1124 {
1125 }