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