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