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