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