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