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