GDB (API)
/home/stan/gdb/src/gdb/buildsym.c
Go to the documentation of this file.
00001 /* Support routines for building symbol tables in GDB's internal format.
00002    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 /* This module provides subroutines used for creating and adding to
00020    the symbol table.  These routines are called from various symbol-
00021    file-reading routines.
00022 
00023    Routines to support specific debugging information formats (stabs,
00024    DWARF, etc) belong somewhere else.  */
00025 
00026 #include "defs.h"
00027 #include "bfd.h"
00028 #include "gdb_obstack.h"
00029 #include "symtab.h"
00030 #include "symfile.h"
00031 #include "objfiles.h"
00032 #include "gdbtypes.h"
00033 #include "gdb_assert.h"
00034 #include "complaints.h"
00035 #include "gdb_string.h"
00036 #include "expression.h"         /* For "enum exp_opcode" used by...  */
00037 #include "bcache.h"
00038 #include "filenames.h"          /* For DOSish file names.  */
00039 #include "macrotab.h"
00040 #include "demangle.h"           /* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
00041 #include "block.h"
00042 #include "cp-support.h"
00043 #include "dictionary.h"
00044 #include "addrmap.h"
00045 
00046 /* Ask buildsym.h to define the vars it normally declares `extern'.  */
00047 #define EXTERN
00048 
00049 #include "buildsym.h"           /* Our own declarations.  */
00050 #undef  EXTERN
00051 
00052 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
00053    questionable--see comment where we call them).  */
00054 
00055 #include "stabsread.h"
00056 
00057 /* List of subfiles.  */
00058 
00059 static struct subfile *subfiles;
00060 
00061 /* List of free `struct pending' structures for reuse.  */
00062 
00063 static struct pending *free_pendings;
00064 
00065 /* Non-zero if symtab has line number info.  This prevents an
00066    otherwise empty symtab from being tossed.  */
00067 
00068 static int have_line_numbers;
00069 
00070 /* The mutable address map for the compilation unit whose symbols
00071    we're currently reading.  The symtabs' shared blockvector will
00072    point to a fixed copy of this.  */
00073 static struct addrmap *pending_addrmap;
00074 
00075 /* The obstack on which we allocate pending_addrmap.
00076    If pending_addrmap is NULL, this is uninitialized; otherwise, it is
00077    initialized (and holds pending_addrmap).  */
00078 static struct obstack pending_addrmap_obstack;
00079 
00080 /* Non-zero if we recorded any ranges in the addrmap that are
00081    different from those in the blockvector already.  We set this to
00082    zero when we start processing a symfile, and if it's still zero at
00083    the end, then we just toss the addrmap.  */
00084 static int pending_addrmap_interesting;
00085 
00086 /* An obstack used for allocating pending blocks.  */
00087 
00088 static struct obstack pending_block_obstack;
00089 
00090 /* List of blocks already made (lexical contexts already closed).
00091    This is used at the end to make the blockvector.  */
00092 
00093 struct pending_block
00094   {
00095     struct pending_block *next;
00096     struct block *block;
00097   };
00098 
00099 /* Pointer to the head of a linked list of symbol blocks which have
00100    already been finalized (lexical contexts already closed) and which
00101    are just waiting to be built into a blockvector when finalizing the
00102    associated symtab.  */
00103 
00104 static struct pending_block *pending_blocks;
00105 
00106 struct subfile_stack
00107   {
00108     struct subfile_stack *next;
00109     char *name;
00110   };
00111 
00112 static struct subfile_stack *subfile_stack;
00113 
00114 /* The macro table for the compilation unit whose symbols we're
00115    currently reading.  All the symtabs for the CU will point to this.  */
00116 static struct macro_table *pending_macros;
00117 
00118 static int compare_line_numbers (const void *ln1p, const void *ln2p);
00119 
00120 static void record_pending_block (struct objfile *objfile,
00121                                   struct block *block,
00122                                   struct pending_block *opblock);
00123 
00124 /* Initial sizes of data structures.  These are realloc'd larger if
00125    needed, and realloc'd down to the size actually used, when
00126    completed.  */
00127 
00128 #define INITIAL_CONTEXT_STACK_SIZE      10
00129 #define INITIAL_LINE_VECTOR_LENGTH      1000
00130 
00131 
00132 /* Maintain the lists of symbols and blocks.  */
00133 
00134 /* Add a symbol to one of the lists of symbols.  */
00135 
00136 void
00137 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
00138 {
00139   struct pending *link;
00140 
00141   /* If this is an alias for another symbol, don't add it.  */
00142   if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
00143     return;
00144 
00145   /* We keep PENDINGSIZE symbols in each link of the list.  If we
00146      don't have a link with room in it, add a new link.  */
00147   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
00148     {
00149       if (free_pendings)
00150         {
00151           link = free_pendings;
00152           free_pendings = link->next;
00153         }
00154       else
00155         {
00156           link = (struct pending *) xmalloc (sizeof (struct pending));
00157         }
00158 
00159       link->next = *listhead;
00160       *listhead = link;
00161       link->nsyms = 0;
00162     }
00163 
00164   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
00165 }
00166 
00167 /* Find a symbol named NAME on a LIST.  NAME need not be
00168    '\0'-terminated; LENGTH is the length of the name.  */
00169 
00170 struct symbol *
00171 find_symbol_in_list (struct pending *list, char *name, int length)
00172 {
00173   int j;
00174   const char *pp;
00175 
00176   while (list != NULL)
00177     {
00178       for (j = list->nsyms; --j >= 0;)
00179         {
00180           pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
00181           if (*pp == *name && strncmp (pp, name, length) == 0
00182               && pp[length] == '\0')
00183             {
00184               return (list->symbol[j]);
00185             }
00186         }
00187       list = list->next;
00188     }
00189   return (NULL);
00190 }
00191 
00192 /* At end of reading syms, or in case of quit, really free as many
00193    `struct pending's as we can easily find.  */
00194 
00195 void
00196 really_free_pendings (void *dummy)
00197 {
00198   struct pending *next, *next1;
00199 
00200   for (next = free_pendings; next; next = next1)
00201     {
00202       next1 = next->next;
00203       xfree ((void *) next);
00204     }
00205   free_pendings = NULL;
00206 
00207   free_pending_blocks ();
00208 
00209   for (next = file_symbols; next != NULL; next = next1)
00210     {
00211       next1 = next->next;
00212       xfree ((void *) next);
00213     }
00214   file_symbols = NULL;
00215 
00216   for (next = global_symbols; next != NULL; next = next1)
00217     {
00218       next1 = next->next;
00219       xfree ((void *) next);
00220     }
00221   global_symbols = NULL;
00222 
00223   if (pending_macros)
00224     free_macro_table (pending_macros);
00225 
00226   if (pending_addrmap)
00227     {
00228       obstack_free (&pending_addrmap_obstack, NULL);
00229       pending_addrmap = NULL;
00230     }
00231 }
00232 
00233 /* This function is called to discard any pending blocks.  */
00234 
00235 void
00236 free_pending_blocks (void)
00237 {
00238   if (pending_blocks != NULL)
00239     {
00240       obstack_free (&pending_block_obstack, NULL);
00241       pending_blocks = NULL;
00242     }
00243 }
00244 
00245 /* Take one of the lists of symbols and make a block from it.  Keep
00246    the order the symbols have in the list (reversed from the input
00247    file).  Put the block on the list of pending blocks.  */
00248 
00249 static struct block *
00250 finish_block_internal (struct symbol *symbol, struct pending **listhead,
00251                        struct pending_block *old_blocks,
00252                        CORE_ADDR start, CORE_ADDR end,
00253                        struct objfile *objfile,
00254                        int is_global, int expandable)
00255 {
00256   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00257   struct pending *next, *next1;
00258   struct block *block;
00259   struct pending_block *pblock;
00260   struct pending_block *opblock;
00261 
00262   block = (is_global
00263            ? allocate_global_block (&objfile->objfile_obstack)
00264            : allocate_block (&objfile->objfile_obstack));
00265 
00266   if (symbol)
00267     {
00268       BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
00269                                                *listhead);
00270     }
00271   else
00272     {
00273       if (expandable)
00274         {
00275           BLOCK_DICT (block) = dict_create_hashed_expandable ();
00276           dict_add_pending (BLOCK_DICT (block), *listhead);
00277         }
00278       else
00279         {
00280           BLOCK_DICT (block) =
00281             dict_create_hashed (&objfile->objfile_obstack, *listhead);
00282         }
00283     }
00284 
00285   BLOCK_START (block) = start;
00286   BLOCK_END (block) = end;
00287 
00288   /* Put the block in as the value of the symbol that names it.  */
00289 
00290   if (symbol)
00291     {
00292       struct type *ftype = SYMBOL_TYPE (symbol);
00293       struct dict_iterator iter;
00294       SYMBOL_BLOCK_VALUE (symbol) = block;
00295       BLOCK_FUNCTION (block) = symbol;
00296 
00297       if (TYPE_NFIELDS (ftype) <= 0)
00298         {
00299           /* No parameter type information is recorded with the
00300              function's type.  Set that from the type of the
00301              parameter symbols.  */
00302           int nparams = 0, iparams;
00303           struct symbol *sym;
00304 
00305           /* Here we want to directly access the dictionary, because
00306              we haven't fully initialized the block yet.  */
00307           ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
00308             {
00309               if (SYMBOL_IS_ARGUMENT (sym))
00310                 nparams++;
00311             }
00312           if (nparams > 0)
00313             {
00314               TYPE_NFIELDS (ftype) = nparams;
00315               TYPE_FIELDS (ftype) = (struct field *)
00316                 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
00317 
00318               iparams = 0;
00319               /* Here we want to directly access the dictionary, because
00320                  we haven't fully initialized the block yet.  */
00321               ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
00322                 {
00323                   if (iparams == nparams)
00324                     break;
00325 
00326                   if (SYMBOL_IS_ARGUMENT (sym))
00327                     {
00328                       TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
00329                       TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
00330                       iparams++;
00331                     }
00332                 }
00333             }
00334         }
00335     }
00336   else
00337     {
00338       BLOCK_FUNCTION (block) = NULL;
00339     }
00340 
00341   /* Now "free" the links of the list, and empty the list.  */
00342 
00343   for (next = *listhead; next; next = next1)
00344     {
00345       next1 = next->next;
00346       next->next = free_pendings;
00347       free_pendings = next;
00348     }
00349   *listhead = NULL;
00350 
00351   /* Check to be sure that the blocks have an end address that is
00352      greater than starting address.  */
00353 
00354   if (BLOCK_END (block) < BLOCK_START (block))
00355     {
00356       if (symbol)
00357         {
00358           complaint (&symfile_complaints,
00359                      _("block end address less than block "
00360                        "start address in %s (patched it)"),
00361                      SYMBOL_PRINT_NAME (symbol));
00362         }
00363       else
00364         {
00365           complaint (&symfile_complaints,
00366                      _("block end address %s less than block "
00367                        "start address %s (patched it)"),
00368                      paddress (gdbarch, BLOCK_END (block)),
00369                      paddress (gdbarch, BLOCK_START (block)));
00370         }
00371       /* Better than nothing.  */
00372       BLOCK_END (block) = BLOCK_START (block);
00373     }
00374 
00375   /* Install this block as the superblock of all blocks made since the
00376      start of this scope that don't have superblocks yet.  */
00377 
00378   opblock = NULL;
00379   for (pblock = pending_blocks; 
00380        pblock && pblock != old_blocks; 
00381        pblock = pblock->next)
00382     {
00383       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
00384         {
00385           /* Check to be sure the blocks are nested as we receive
00386              them.  If the compiler/assembler/linker work, this just
00387              burns a small amount of time.
00388 
00389              Skip blocks which correspond to a function; they're not
00390              physically nested inside this other blocks, only
00391              lexically nested.  */
00392           if (BLOCK_FUNCTION (pblock->block) == NULL
00393               && (BLOCK_START (pblock->block) < BLOCK_START (block)
00394                   || BLOCK_END (pblock->block) > BLOCK_END (block)))
00395             {
00396               if (symbol)
00397                 {
00398                   complaint (&symfile_complaints,
00399                              _("inner block not inside outer block in %s"),
00400                              SYMBOL_PRINT_NAME (symbol));
00401                 }
00402               else
00403                 {
00404                   complaint (&symfile_complaints,
00405                              _("inner block (%s-%s) not "
00406                                "inside outer block (%s-%s)"),
00407                              paddress (gdbarch, BLOCK_START (pblock->block)),
00408                              paddress (gdbarch, BLOCK_END (pblock->block)),
00409                              paddress (gdbarch, BLOCK_START (block)),
00410                              paddress (gdbarch, BLOCK_END (block)));
00411                 }
00412               if (BLOCK_START (pblock->block) < BLOCK_START (block))
00413                 BLOCK_START (pblock->block) = BLOCK_START (block);
00414               if (BLOCK_END (pblock->block) > BLOCK_END (block))
00415                 BLOCK_END (pblock->block) = BLOCK_END (block);
00416             }
00417           BLOCK_SUPERBLOCK (pblock->block) = block;
00418         }
00419       opblock = pblock;
00420     }
00421 
00422   block_set_using (block, using_directives, &objfile->objfile_obstack);
00423   using_directives = NULL;
00424 
00425   record_pending_block (objfile, block, opblock);
00426 
00427   return block;
00428 }
00429 
00430 struct block *
00431 finish_block (struct symbol *symbol, struct pending **listhead,
00432               struct pending_block *old_blocks,
00433               CORE_ADDR start, CORE_ADDR end,
00434               struct objfile *objfile)
00435 {
00436   return finish_block_internal (symbol, listhead, old_blocks,
00437                                 start, end, objfile, 0, 0);
00438 }
00439 
00440 /* Record BLOCK on the list of all blocks in the file.  Put it after
00441    OPBLOCK, or at the beginning if opblock is NULL.  This puts the
00442    block in the list after all its subblocks.
00443 
00444    Allocate the pending block struct in the objfile_obstack to save
00445    time.  This wastes a little space.  FIXME: Is it worth it?  */
00446 
00447 static void
00448 record_pending_block (struct objfile *objfile, struct block *block,
00449                       struct pending_block *opblock)
00450 {
00451   struct pending_block *pblock;
00452 
00453   if (pending_blocks == NULL)
00454     obstack_init (&pending_block_obstack);
00455 
00456   pblock = (struct pending_block *)
00457     obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
00458   pblock->block = block;
00459   if (opblock)
00460     {
00461       pblock->next = opblock->next;
00462       opblock->next = pblock;
00463     }
00464   else
00465     {
00466       pblock->next = pending_blocks;
00467       pending_blocks = pblock;
00468     }
00469 }
00470 
00471 
00472 /* Record that the range of addresses from START to END_INCLUSIVE
00473    (inclusive, like it says) belongs to BLOCK.  BLOCK's start and end
00474    addresses must be set already.  You must apply this function to all
00475    BLOCK's children before applying it to BLOCK.
00476 
00477    If a call to this function complicates the picture beyond that
00478    already provided by BLOCK_START and BLOCK_END, then we create an
00479    address map for the block.  */
00480 void
00481 record_block_range (struct block *block,
00482                     CORE_ADDR start, CORE_ADDR end_inclusive)
00483 {
00484   /* If this is any different from the range recorded in the block's
00485      own BLOCK_START and BLOCK_END, then note that the address map has
00486      become interesting.  Note that even if this block doesn't have
00487      any "interesting" ranges, some later block might, so we still
00488      need to record this block in the addrmap.  */
00489   if (start != BLOCK_START (block)
00490       || end_inclusive + 1 != BLOCK_END (block))
00491     pending_addrmap_interesting = 1;
00492 
00493   if (! pending_addrmap)
00494     {
00495       obstack_init (&pending_addrmap_obstack);
00496       pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
00497     }
00498 
00499   addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
00500 }
00501 
00502 
00503 static struct blockvector *
00504 make_blockvector (struct objfile *objfile)
00505 {
00506   struct pending_block *next;
00507   struct blockvector *blockvector;
00508   int i;
00509 
00510   /* Count the length of the list of blocks.  */
00511 
00512   for (next = pending_blocks, i = 0; next; next = next->next, i++)
00513     {;
00514     }
00515 
00516   blockvector = (struct blockvector *)
00517     obstack_alloc (&objfile->objfile_obstack,
00518                    (sizeof (struct blockvector)
00519                     + (i - 1) * sizeof (struct block *)));
00520 
00521   /* Copy the blocks into the blockvector.  This is done in reverse
00522      order, which happens to put the blocks into the proper order
00523      (ascending starting address).  finish_block has hair to insert
00524      each block into the list after its subblocks in order to make
00525      sure this is true.  */
00526 
00527   BLOCKVECTOR_NBLOCKS (blockvector) = i;
00528   for (next = pending_blocks; next; next = next->next)
00529     {
00530       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
00531     }
00532 
00533   free_pending_blocks ();
00534 
00535   /* If we needed an address map for this symtab, record it in the
00536      blockvector.  */
00537   if (pending_addrmap && pending_addrmap_interesting)
00538     BLOCKVECTOR_MAP (blockvector)
00539       = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
00540   else
00541     BLOCKVECTOR_MAP (blockvector) = 0;
00542 
00543   /* Some compilers output blocks in the wrong order, but we depend on
00544      their being in the right order so we can binary search.  Check the
00545      order and moan about it.
00546      Note: Remember that the first two blocks are the global and static
00547      blocks.  We could special case that fact and begin checking at block 2.
00548      To avoid making that assumption we do not.  */
00549   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
00550     {
00551       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
00552         {
00553           if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
00554               > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
00555             {
00556               CORE_ADDR start
00557                 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
00558 
00559               complaint (&symfile_complaints, _("block at %s out of order"),
00560                          hex_string ((LONGEST) start));
00561             }
00562         }
00563     }
00564 
00565   return (blockvector);
00566 }
00567 
00568 /* Start recording information about source code that came from an
00569    included (or otherwise merged-in) source file with a different
00570    name.  NAME is the name of the file (cannot be NULL), DIRNAME is
00571    the directory in which the file was compiled (or NULL if not
00572    known).  */
00573 
00574 void
00575 start_subfile (const char *name, const char *dirname)
00576 {
00577   struct subfile *subfile;
00578 
00579   /* See if this subfile is already known as a subfile of the current
00580      main source file.  */
00581 
00582   for (subfile = subfiles; subfile; subfile = subfile->next)
00583     {
00584       char *subfile_name;
00585 
00586       /* If NAME is an absolute path, and this subfile is not, then
00587          attempt to create an absolute path to compare.  */
00588       if (IS_ABSOLUTE_PATH (name)
00589           && !IS_ABSOLUTE_PATH (subfile->name)
00590           && subfile->dirname != NULL)
00591         subfile_name = concat (subfile->dirname, SLASH_STRING,
00592                                subfile->name, (char *) NULL);
00593       else
00594         subfile_name = subfile->name;
00595 
00596       if (FILENAME_CMP (subfile_name, name) == 0)
00597         {
00598           current_subfile = subfile;
00599           if (subfile_name != subfile->name)
00600             xfree (subfile_name);
00601           return;
00602         }
00603       if (subfile_name != subfile->name)
00604         xfree (subfile_name);
00605     }
00606 
00607   /* This subfile is not known.  Add an entry for it.  Make an entry
00608      for this subfile in the list of all subfiles of the current main
00609      source file.  */
00610 
00611   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
00612   memset ((char *) subfile, 0, sizeof (struct subfile));
00613   subfile->next = subfiles;
00614   subfiles = subfile;
00615   current_subfile = subfile;
00616 
00617   /* Save its name and compilation directory name.  */
00618   subfile->name = xstrdup (name);
00619   subfile->dirname = (dirname == NULL) ? NULL : xstrdup (dirname);
00620 
00621   /* Initialize line-number recording for this subfile.  */
00622   subfile->line_vector = NULL;
00623 
00624   /* Default the source language to whatever can be deduced from the
00625      filename.  If nothing can be deduced (such as for a C/C++ include
00626      file with a ".h" extension), then inherit whatever language the
00627      previous subfile had.  This kludgery is necessary because there
00628      is no standard way in some object formats to record the source
00629      language.  Also, when symtabs are allocated we try to deduce a
00630      language then as well, but it is too late for us to use that
00631      information while reading symbols, since symtabs aren't allocated
00632      until after all the symbols have been processed for a given
00633      source file.  */
00634 
00635   subfile->language = deduce_language_from_filename (subfile->name);
00636   if (subfile->language == language_unknown
00637       && subfile->next != NULL)
00638     {
00639       subfile->language = subfile->next->language;
00640     }
00641 
00642   /* Initialize the debug format string to NULL.  We may supply it
00643      later via a call to record_debugformat.  */
00644   subfile->debugformat = NULL;
00645 
00646   /* Similarly for the producer.  */
00647   subfile->producer = NULL;
00648 
00649   /* If the filename of this subfile ends in .C, then change the
00650      language of any pending subfiles from C to C++.  We also accept
00651      any other C++ suffixes accepted by deduce_language_from_filename.  */
00652   /* Likewise for f2c.  */
00653 
00654   if (subfile->name)
00655     {
00656       struct subfile *s;
00657       enum language sublang = deduce_language_from_filename (subfile->name);
00658 
00659       if (sublang == language_cplus || sublang == language_fortran)
00660         for (s = subfiles; s != NULL; s = s->next)
00661           if (s->language == language_c)
00662             s->language = sublang;
00663     }
00664 
00665   /* And patch up this file if necessary.  */
00666   if (subfile->language == language_c
00667       && subfile->next != NULL
00668       && (subfile->next->language == language_cplus
00669           || subfile->next->language == language_fortran))
00670     {
00671       subfile->language = subfile->next->language;
00672     }
00673 }
00674 
00675 /* For stabs readers, the first N_SO symbol is assumed to be the
00676    source file name, and the subfile struct is initialized using that
00677    assumption.  If another N_SO symbol is later seen, immediately
00678    following the first one, then the first one is assumed to be the
00679    directory name and the second one is really the source file name.
00680 
00681    So we have to patch up the subfile struct by moving the old name
00682    value to dirname and remembering the new name.  Some sanity
00683    checking is performed to ensure that the state of the subfile
00684    struct is reasonable and that the old name we are assuming to be a
00685    directory name actually is (by checking for a trailing '/').  */
00686 
00687 void
00688 patch_subfile_names (struct subfile *subfile, char *name)
00689 {
00690   if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
00691       && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
00692     {
00693       subfile->dirname = subfile->name;
00694       subfile->name = xstrdup (name);
00695       set_last_source_file (name);
00696 
00697       /* Default the source language to whatever can be deduced from
00698          the filename.  If nothing can be deduced (such as for a C/C++
00699          include file with a ".h" extension), then inherit whatever
00700          language the previous subfile had.  This kludgery is
00701          necessary because there is no standard way in some object
00702          formats to record the source language.  Also, when symtabs
00703          are allocated we try to deduce a language then as well, but
00704          it is too late for us to use that information while reading
00705          symbols, since symtabs aren't allocated until after all the
00706          symbols have been processed for a given source file.  */
00707 
00708       subfile->language = deduce_language_from_filename (subfile->name);
00709       if (subfile->language == language_unknown
00710           && subfile->next != NULL)
00711         {
00712           subfile->language = subfile->next->language;
00713         }
00714     }
00715 }
00716 
00717 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
00718    switching source files (different subfiles, as we call them) within
00719    one object file, but using a stack rather than in an arbitrary
00720    order.  */
00721 
00722 void
00723 push_subfile (void)
00724 {
00725   struct subfile_stack *tem
00726     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
00727 
00728   tem->next = subfile_stack;
00729   subfile_stack = tem;
00730   if (current_subfile == NULL || current_subfile->name == NULL)
00731     {
00732       internal_error (__FILE__, __LINE__, 
00733                       _("failed internal consistency check"));
00734     }
00735   tem->name = current_subfile->name;
00736 }
00737 
00738 char *
00739 pop_subfile (void)
00740 {
00741   char *name;
00742   struct subfile_stack *link = subfile_stack;
00743 
00744   if (link == NULL)
00745     {
00746       internal_error (__FILE__, __LINE__,
00747                       _("failed internal consistency check"));
00748     }
00749   name = link->name;
00750   subfile_stack = link->next;
00751   xfree ((void *) link);
00752   return (name);
00753 }
00754 
00755 /* Add a linetable entry for line number LINE and address PC to the
00756    line vector for SUBFILE.  */
00757 
00758 void
00759 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
00760 {
00761   struct linetable_entry *e;
00762 
00763   /* Ignore the dummy line number in libg.o */
00764   if (line == 0xffff)
00765     {
00766       return;
00767     }
00768 
00769   /* Make sure line vector exists and is big enough.  */
00770   if (!subfile->line_vector)
00771     {
00772       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
00773       subfile->line_vector = (struct linetable *)
00774         xmalloc (sizeof (struct linetable)
00775            + subfile->line_vector_length * sizeof (struct linetable_entry));
00776       subfile->line_vector->nitems = 0;
00777       have_line_numbers = 1;
00778     }
00779 
00780   if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
00781     {
00782       subfile->line_vector_length *= 2;
00783       subfile->line_vector = (struct linetable *)
00784         xrealloc ((char *) subfile->line_vector,
00785                   (sizeof (struct linetable)
00786                    + (subfile->line_vector_length
00787                       * sizeof (struct linetable_entry))));
00788     }
00789 
00790   /* Normally, we treat lines as unsorted.  But the end of sequence
00791      marker is special.  We sort line markers at the same PC by line
00792      number, so end of sequence markers (which have line == 0) appear
00793      first.  This is right if the marker ends the previous function,
00794      and there is no padding before the next function.  But it is
00795      wrong if the previous line was empty and we are now marking a
00796      switch to a different subfile.  We must leave the end of sequence
00797      marker at the end of this group of lines, not sort the empty line
00798      to after the marker.  The easiest way to accomplish this is to
00799      delete any empty lines from our table, if they are followed by
00800      end of sequence markers.  All we lose is the ability to set
00801      breakpoints at some lines which contain no instructions
00802      anyway.  */
00803   if (line == 0 && subfile->line_vector->nitems > 0)
00804     {
00805       e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
00806       while (subfile->line_vector->nitems > 0 && e->pc == pc)
00807         {
00808           e--;
00809           subfile->line_vector->nitems--;
00810         }
00811     }
00812 
00813   e = subfile->line_vector->item + subfile->line_vector->nitems++;
00814   e->line = line;
00815   e->pc = pc;
00816 }
00817 
00818 /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
00819 
00820 static int
00821 compare_line_numbers (const void *ln1p, const void *ln2p)
00822 {
00823   struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
00824   struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
00825 
00826   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
00827      Please keep it that way.  */
00828   if (ln1->pc < ln2->pc)
00829     return -1;
00830 
00831   if (ln1->pc > ln2->pc)
00832     return 1;
00833 
00834   /* If pc equal, sort by line.  I'm not sure whether this is optimum
00835      behavior (see comment at struct linetable in symtab.h).  */
00836   return ln1->line - ln2->line;
00837 }
00838 
00839 /* Return the macro table.
00840    Initialize it if this is the first use.  */
00841 
00842 struct macro_table *
00843 get_macro_table (struct objfile *objfile, const char *comp_dir)
00844 {
00845   if (! pending_macros)
00846     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
00847                                       objfile->per_bfd->macro_cache,
00848                                       comp_dir);
00849   return pending_macros;
00850 }
00851 
00852 /* Start a new symtab for a new source file.  Called, for example,
00853    when a stabs symbol of type N_SO is seen, or when a DWARF
00854    TAG_compile_unit DIE is seen.  It indicates the start of data for
00855    one original source file.
00856 
00857    NAME is the name of the file (cannot be NULL).  DIRNAME is the directory in
00858    which the file was compiled (or NULL if not known).  START_ADDR is the
00859    lowest address of objects in the file (or 0 if not known).  */
00860 
00861 void
00862 start_symtab (const char *name, const char *dirname, CORE_ADDR start_addr)
00863 {
00864   restart_symtab (start_addr);
00865   set_last_source_file (name);
00866   start_subfile (name, dirname);
00867 }
00868 
00869 /* Restart compilation for a symtab.
00870    This is used when a symtab is built from multiple sources.
00871    The symtab is first built with start_symtab and then for each additional
00872    piece call restart_symtab.  */
00873 
00874 void
00875 restart_symtab (CORE_ADDR start_addr)
00876 {
00877   set_last_source_file (NULL);
00878   last_source_start_addr = start_addr;
00879   file_symbols = NULL;
00880   global_symbols = NULL;
00881   within_function = 0;
00882   have_line_numbers = 0;
00883 
00884   /* Context stack is initially empty.  Allocate first one with room
00885      for 10 levels; reuse it forever afterward.  */
00886   if (context_stack == NULL)
00887     {
00888       context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
00889       context_stack = (struct context_stack *)
00890         xmalloc (context_stack_size * sizeof (struct context_stack));
00891     }
00892   context_stack_depth = 0;
00893 
00894   /* We shouldn't have any address map at this point.  */
00895   gdb_assert (! pending_addrmap);
00896 
00897   /* Initialize the list of sub source files with one entry for this
00898      file (the top-level source file).  */
00899   subfiles = NULL;
00900   current_subfile = NULL;
00901 }
00902 
00903 /* Subroutine of end_symtab to simplify it.  Look for a subfile that
00904    matches the main source file's basename.  If there is only one, and
00905    if the main source file doesn't have any symbol or line number
00906    information, then copy this file's symtab and line_vector to the
00907    main source file's subfile and discard the other subfile.  This can
00908    happen because of a compiler bug or from the user playing games
00909    with #line or from things like a distributed build system that
00910    manipulates the debug info.  */
00911 
00912 static void
00913 watch_main_source_file_lossage (void)
00914 {
00915   struct subfile *mainsub, *subfile;
00916 
00917   /* Find the main source file.
00918      This loop could be eliminated if start_symtab saved it for us.  */
00919   mainsub = NULL;
00920   for (subfile = subfiles; subfile; subfile = subfile->next)
00921     {
00922       /* The main subfile is guaranteed to be the last one.  */
00923       if (subfile->next == NULL)
00924         mainsub = subfile;
00925     }
00926 
00927   /* If the main source file doesn't have any line number or symbol
00928      info, look for an alias in another subfile.
00929 
00930      We have to watch for mainsub == NULL here.  It's a quirk of
00931      end_symtab, it can return NULL so there may not be a main
00932      subfile.  */
00933 
00934   if (mainsub
00935       && mainsub->line_vector == NULL
00936       && mainsub->symtab == NULL)
00937     {
00938       const char *mainbase = lbasename (mainsub->name);
00939       int nr_matches = 0;
00940       struct subfile *prevsub;
00941       struct subfile *mainsub_alias = NULL;
00942       struct subfile *prev_mainsub_alias = NULL;
00943 
00944       prevsub = NULL;
00945       for (subfile = subfiles;
00946            /* Stop before we get to the last one.  */
00947            subfile->next;
00948            subfile = subfile->next)
00949         {
00950           if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
00951             {
00952               ++nr_matches;
00953               mainsub_alias = subfile;
00954               prev_mainsub_alias = prevsub;
00955             }
00956           prevsub = subfile;
00957         }
00958 
00959       if (nr_matches == 1)
00960         {
00961           gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
00962 
00963           /* Found a match for the main source file.
00964              Copy its line_vector and symtab to the main subfile
00965              and then discard it.  */
00966 
00967           mainsub->line_vector = mainsub_alias->line_vector;
00968           mainsub->line_vector_length = mainsub_alias->line_vector_length;
00969           mainsub->symtab = mainsub_alias->symtab;
00970 
00971           if (prev_mainsub_alias == NULL)
00972             subfiles = mainsub_alias->next;
00973           else
00974             prev_mainsub_alias->next = mainsub_alias->next;
00975           xfree (mainsub_alias);
00976         }
00977     }
00978 }
00979 
00980 /* Helper function for qsort.  Parameters are `struct block *' pointers,
00981    function sorts them in descending order by their BLOCK_START.  */
00982 
00983 static int
00984 block_compar (const void *ap, const void *bp)
00985 {
00986   const struct block *a = *(const struct block **) ap;
00987   const struct block *b = *(const struct block **) bp;
00988 
00989   return ((BLOCK_START (b) > BLOCK_START (a))
00990           - (BLOCK_START (b) < BLOCK_START (a)));
00991 }
00992 
00993 /* Reset globals used to build symtabs.  */
00994 
00995 static void
00996 reset_symtab_globals (void)
00997 {
00998   set_last_source_file (NULL);
00999   current_subfile = NULL;
01000   pending_macros = NULL;
01001   if (pending_addrmap)
01002     {
01003       obstack_free (&pending_addrmap_obstack, NULL);
01004       pending_addrmap = NULL;
01005     }
01006 }
01007 
01008 /* Implementation of the first part of end_symtab.  It allows modifying
01009    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
01010    If the returned value is NULL there is no blockvector created for
01011    this symtab (you still must call end_symtab_from_static_block).
01012 
01013    END_ADDR is the same as for end_symtab: the address of the end of the
01014    file's text.
01015 
01016    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
01017    expandable.
01018 
01019    If REQUIRED is non-zero, then a symtab is created even if it does
01020    not contain any symbols.  */
01021 
01022 struct block *
01023 end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
01024                              int expandable, int required)
01025 {
01026   /* Finish the lexical context of the last function in the file; pop
01027      the context stack.  */
01028 
01029   if (context_stack_depth > 0)
01030     {
01031       struct context_stack *cstk = pop_context ();
01032 
01033       /* Make a block for the local symbols within.  */
01034       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
01035                     cstk->start_addr, end_addr, objfile);
01036 
01037       if (context_stack_depth > 0)
01038         {
01039           /* This is said to happen with SCO.  The old coffread.c
01040              code simply emptied the context stack, so we do the
01041              same.  FIXME: Find out why it is happening.  This is not
01042              believed to happen in most cases (even for coffread.c);
01043              it used to be an abort().  */
01044           complaint (&symfile_complaints,
01045                      _("Context stack not empty in end_symtab"));
01046           context_stack_depth = 0;
01047         }
01048     }
01049 
01050   /* Reordered executables may have out of order pending blocks; if
01051      OBJF_REORDERED is true, then sort the pending blocks.  */
01052 
01053   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
01054     {
01055       unsigned count = 0;
01056       struct pending_block *pb;
01057       struct block **barray, **bp;
01058       struct cleanup *back_to;
01059 
01060       for (pb = pending_blocks; pb != NULL; pb = pb->next)
01061         count++;
01062 
01063       barray = xmalloc (sizeof (*barray) * count);
01064       back_to = make_cleanup (xfree, barray);
01065 
01066       bp = barray;
01067       for (pb = pending_blocks; pb != NULL; pb = pb->next)
01068         *bp++ = pb->block;
01069 
01070       qsort (barray, count, sizeof (*barray), block_compar);
01071 
01072       bp = barray;
01073       for (pb = pending_blocks; pb != NULL; pb = pb->next)
01074         pb->block = *bp++;
01075 
01076       do_cleanups (back_to);
01077     }
01078 
01079   /* Cleanup any undefined types that have been left hanging around
01080      (this needs to be done before the finish_blocks so that
01081      file_symbols is still good).
01082 
01083      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
01084      specific, but harmless for other symbol readers, since on gdb
01085      startup or when finished reading stabs, the state is set so these
01086      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
01087      we make this cleaner?  */
01088 
01089   cleanup_undefined_stabs_types (objfile);
01090   finish_global_stabs (objfile);
01091 
01092   if (!required
01093       && pending_blocks == NULL
01094       && file_symbols == NULL
01095       && global_symbols == NULL
01096       && have_line_numbers == 0
01097       && pending_macros == NULL)
01098     {
01099       /* Ignore symtabs that have no functions with real debugging info.  */
01100       return NULL;
01101     }
01102   else
01103     {
01104       /* Define the STATIC_BLOCK.  */
01105       return finish_block_internal (NULL, &file_symbols, NULL,
01106                                     last_source_start_addr, end_addr, objfile,
01107                                     0, expandable);
01108     }
01109 }
01110 
01111 /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
01112    as value returned by end_symtab_get_static_block.
01113 
01114    SECTION is the same as for end_symtab: the section number
01115    (in objfile->section_offsets) of the blockvector and linetable.
01116 
01117    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
01118    expandable.  */
01119 
01120 struct symtab *
01121 end_symtab_from_static_block (struct block *static_block,
01122                               struct objfile *objfile, int section,
01123                               int expandable)
01124 {
01125   struct symtab *symtab = NULL;
01126   struct blockvector *blockvector;
01127   struct subfile *subfile;
01128   struct subfile *nextsub;
01129 
01130   if (static_block == NULL)
01131     {
01132       /* Ignore symtabs that have no functions with real debugging info.  */
01133       blockvector = NULL;
01134     }
01135   else
01136     {
01137       CORE_ADDR end_addr = BLOCK_END (static_block);
01138 
01139       /* Define after STATIC_BLOCK also GLOBAL_BLOCK, and build the
01140          blockvector.  */
01141       finish_block_internal (NULL, &global_symbols, NULL,
01142                              last_source_start_addr, end_addr, objfile,
01143                              1, expandable);
01144       blockvector = make_blockvector (objfile);
01145     }
01146 
01147   /* Read the line table if it has to be read separately.
01148      This is only used by xcoffread.c.  */
01149   if (objfile->sf->sym_read_linetable != NULL)
01150     objfile->sf->sym_read_linetable (objfile);
01151 
01152   /* Handle the case where the debug info specifies a different path
01153      for the main source file.  It can cause us to lose track of its
01154      line number information.  */
01155   watch_main_source_file_lossage ();
01156 
01157   /* Now create the symtab objects proper, one for each subfile.  */
01158   /* (The main file is the last one on the chain.)  */
01159 
01160   for (subfile = subfiles; subfile; subfile = nextsub)
01161     {
01162       int linetablesize = 0;
01163       symtab = NULL;
01164 
01165       /* If we have blocks of symbols, make a symtab.  Otherwise, just
01166          ignore this file and any line number info in it.  */
01167       if (blockvector)
01168         {
01169           if (subfile->line_vector)
01170             {
01171               linetablesize = sizeof (struct linetable) +
01172                 subfile->line_vector->nitems * sizeof (struct linetable_entry);
01173 
01174               /* Like the pending blocks, the line table may be
01175                  scrambled in reordered executables.  Sort it if
01176                  OBJF_REORDERED is true.  */
01177               if (objfile->flags & OBJF_REORDERED)
01178                 qsort (subfile->line_vector->item,
01179                        subfile->line_vector->nitems,
01180                      sizeof (struct linetable_entry), compare_line_numbers);
01181             }
01182 
01183           /* Now, allocate a symbol table.  */
01184           if (subfile->symtab == NULL)
01185             symtab = allocate_symtab (subfile->name, objfile);
01186           else
01187             symtab = subfile->symtab;
01188 
01189           /* Fill in its components.  */
01190           symtab->blockvector = blockvector;
01191           symtab->macro_table = pending_macros;
01192           if (subfile->line_vector)
01193             {
01194               /* Reallocate the line table on the symbol obstack.  */
01195               symtab->linetable = (struct linetable *)
01196                 obstack_alloc (&objfile->objfile_obstack, linetablesize);
01197               memcpy (symtab->linetable, subfile->line_vector, linetablesize);
01198             }
01199           else
01200             {
01201               symtab->linetable = NULL;
01202             }
01203           symtab->block_line_section = section;
01204           if (subfile->dirname)
01205             {
01206               /* Reallocate the dirname on the symbol obstack.  */
01207               symtab->dirname = (char *)
01208                 obstack_alloc (&objfile->objfile_obstack,
01209                                strlen (subfile->dirname) + 1);
01210               strcpy (symtab->dirname, subfile->dirname);
01211             }
01212           else
01213             {
01214               symtab->dirname = NULL;
01215             }
01216 
01217           /* Use whatever language we have been using for this
01218              subfile, not the one that was deduced in allocate_symtab
01219              from the filename.  We already did our own deducing when
01220              we created the subfile, and we may have altered our
01221              opinion of what language it is from things we found in
01222              the symbols.  */
01223           symtab->language = subfile->language;
01224 
01225           /* Save the debug format string (if any) in the symtab.  */
01226           symtab->debugformat = subfile->debugformat;
01227 
01228           /* Similarly for the producer.  */
01229           symtab->producer = subfile->producer;
01230 
01231           /* All symtabs for the main file and the subfiles share a
01232              blockvector, so we need to clear primary for everything
01233              but the main file.  */
01234 
01235           symtab->primary = 0;
01236         }
01237       else
01238         {
01239           if (subfile->symtab)
01240             {
01241               /* Since we are ignoring that subfile, we also need
01242                  to unlink the associated empty symtab that we created.
01243                  Otherwise, we can run into trouble because various parts
01244                  such as the block-vector are uninitialized whereas
01245                  the rest of the code assumes that they are.
01246                  
01247                  We can only unlink the symtab because it was allocated
01248                  on the objfile obstack.  */
01249               struct symtab *s;
01250 
01251               if (objfile->symtabs == subfile->symtab)
01252                 objfile->symtabs = objfile->symtabs->next;
01253               else
01254                 ALL_OBJFILE_SYMTABS (objfile, s)
01255                   if (s->next == subfile->symtab)
01256                     {
01257                       s->next = s->next->next;
01258                       break;
01259                     }
01260               subfile->symtab = NULL;
01261             }
01262         }
01263       if (subfile->name != NULL)
01264         {
01265           xfree ((void *) subfile->name);
01266         }
01267       if (subfile->dirname != NULL)
01268         {
01269           xfree ((void *) subfile->dirname);
01270         }
01271       if (subfile->line_vector != NULL)
01272         {
01273           xfree ((void *) subfile->line_vector);
01274         }
01275 
01276       nextsub = subfile->next;
01277       xfree ((void *) subfile);
01278     }
01279 
01280   /* Set this for the main source file.  */
01281   if (symtab)
01282     {
01283       symtab->primary = 1;
01284 
01285       if (symtab->blockvector)
01286         {
01287           struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector,
01288                                                GLOBAL_BLOCK);
01289 
01290           set_block_symtab (b, symtab);
01291         }
01292     }
01293 
01294   /* Default any symbols without a specified symtab to the primary
01295      symtab.  */
01296   if (blockvector)
01297     {
01298       int block_i;
01299 
01300       for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
01301         {
01302           struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
01303           struct symbol *sym;
01304           struct dict_iterator iter;
01305 
01306           /* Inlined functions may have symbols not in the global or
01307              static symbol lists.  */
01308           if (BLOCK_FUNCTION (block) != NULL)
01309             if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
01310               SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
01311 
01312           /* Note that we only want to fix up symbols from the local
01313              blocks, not blocks coming from included symtabs.  That is why
01314              we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
01315           ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
01316             if (SYMBOL_SYMTAB (sym) == NULL)
01317               SYMBOL_SYMTAB (sym) = symtab;
01318         }
01319     }
01320 
01321   reset_symtab_globals ();
01322 
01323   return symtab;
01324 }
01325 
01326 /* Finish the symbol definitions for one main source file, close off
01327    all the lexical contexts for that file (creating struct block's for
01328    them), then make the struct symtab for that file and put it in the
01329    list of all such.
01330 
01331    END_ADDR is the address of the end of the file's text.  SECTION is
01332    the section number (in objfile->section_offsets) of the blockvector
01333    and linetable.
01334 
01335    Note that it is possible for end_symtab() to return NULL.  In
01336    particular, for the DWARF case at least, it will return NULL when
01337    it finds a compilation unit that has exactly one DIE, a
01338    TAG_compile_unit DIE.  This can happen when we link in an object
01339    file that was compiled from an empty source file.  Returning NULL
01340    is probably not the correct thing to do, because then gdb will
01341    never know about this empty file (FIXME).
01342 
01343    If you need to modify STATIC_BLOCK before it is finalized you should
01344    call end_symtab_get_static_block and end_symtab_from_static_block
01345    yourself.  */
01346 
01347 struct symtab *
01348 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
01349 {
01350   struct block *static_block;
01351 
01352   static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
01353   return end_symtab_from_static_block (static_block, objfile, section, 0);
01354 }
01355 
01356 /* Same as end_symtab except create a symtab that can be later added to.  */
01357 
01358 struct symtab *
01359 end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
01360                        int section)
01361 {
01362   struct block *static_block;
01363 
01364   static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
01365   return end_symtab_from_static_block (static_block, objfile, section, 1);
01366 }
01367 
01368 /* Subroutine of augment_type_symtab to simplify it.
01369    Attach SYMTAB to all symbols in PENDING_LIST that don't have one.  */
01370 
01371 static void
01372 set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
01373 {
01374   struct pending *pending;
01375   int i;
01376 
01377   for (pending = pending_list; pending != NULL; pending = pending->next)
01378     {
01379       for (i = 0; i < pending->nsyms; ++i)
01380         {
01381           if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
01382             SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
01383         }
01384     }
01385 }
01386 
01387 /* Same as end_symtab, but for the case where we're adding more symbols
01388    to an existing symtab that is known to contain only type information.
01389    This is the case for DWARF4 Type Units.  */
01390 
01391 void
01392 augment_type_symtab (struct objfile *objfile, struct symtab *primary_symtab)
01393 {
01394   struct blockvector *blockvector = primary_symtab->blockvector;
01395 
01396   if (context_stack_depth > 0)
01397     {
01398       complaint (&symfile_complaints,
01399                  _("Context stack not empty in augment_type_symtab"));
01400       context_stack_depth = 0;
01401     }
01402   if (pending_blocks != NULL)
01403     complaint (&symfile_complaints, _("Blocks in a type symtab"));
01404   if (pending_macros != NULL)
01405     complaint (&symfile_complaints, _("Macro in a type symtab"));
01406   if (have_line_numbers)
01407     complaint (&symfile_complaints,
01408                _("Line numbers recorded in a type symtab"));
01409 
01410   if (file_symbols != NULL)
01411     {
01412       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
01413 
01414       /* First mark any symbols without a specified symtab as belonging
01415          to the primary symtab.  */
01416       set_missing_symtab (file_symbols, primary_symtab);
01417 
01418       dict_add_pending (BLOCK_DICT (block), file_symbols);
01419     }
01420 
01421   if (global_symbols != NULL)
01422     {
01423       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
01424 
01425       /* First mark any symbols without a specified symtab as belonging
01426          to the primary symtab.  */
01427       set_missing_symtab (global_symbols, primary_symtab);
01428 
01429       dict_add_pending (BLOCK_DICT (block), global_symbols);
01430     }
01431 
01432   reset_symtab_globals ();
01433 }
01434 
01435 /* Push a context block.  Args are an identifying nesting level
01436    (checkable when you pop it), and the starting PC address of this
01437    context.  */
01438 
01439 struct context_stack *
01440 push_context (int desc, CORE_ADDR valu)
01441 {
01442   struct context_stack *new;
01443 
01444   if (context_stack_depth == context_stack_size)
01445     {
01446       context_stack_size *= 2;
01447       context_stack = (struct context_stack *)
01448         xrealloc ((char *) context_stack,
01449                   (context_stack_size * sizeof (struct context_stack)));
01450     }
01451 
01452   new = &context_stack[context_stack_depth++];
01453   new->depth = desc;
01454   new->locals = local_symbols;
01455   new->old_blocks = pending_blocks;
01456   new->start_addr = valu;
01457   new->using_directives = using_directives;
01458   new->name = NULL;
01459 
01460   local_symbols = NULL;
01461   using_directives = NULL;
01462 
01463   return new;
01464 }
01465 
01466 /* Pop a context block.  Returns the address of the context block just
01467    popped.  */
01468 
01469 struct context_stack *
01470 pop_context (void)
01471 {
01472   gdb_assert (context_stack_depth > 0);
01473   return (&context_stack[--context_stack_depth]);
01474 }
01475 
01476 
01477 
01478 /* Compute a small integer hash code for the given name.  */
01479 
01480 int
01481 hashname (const char *name)
01482 {
01483     return (hash(name,strlen(name)) % HASHSIZE);
01484 }
01485 
01486 
01487 void
01488 record_debugformat (const char *format)
01489 {
01490   current_subfile->debugformat = format;
01491 }
01492 
01493 void
01494 record_producer (const char *producer)
01495 {
01496   current_subfile->producer = producer;
01497 }
01498 
01499 /* Merge the first symbol list SRCLIST into the second symbol list
01500    TARGETLIST by repeated calls to add_symbol_to_list().  This
01501    procedure "frees" each link of SRCLIST by adding it to the
01502    free_pendings list.  Caller must set SRCLIST to a null list after
01503    calling this function.
01504 
01505    Void return.  */
01506 
01507 void
01508 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
01509 {
01510   int i;
01511 
01512   if (!srclist || !*srclist)
01513     return;
01514 
01515   /* Merge in elements from current link.  */
01516   for (i = 0; i < (*srclist)->nsyms; i++)
01517     add_symbol_to_list ((*srclist)->symbol[i], targetlist);
01518 
01519   /* Recurse on next.  */
01520   merge_symbol_lists (&(*srclist)->next, targetlist);
01521 
01522   /* "Free" the current link.  */
01523   (*srclist)->next = free_pendings;
01524   free_pendings = (*srclist);
01525 }
01526 
01527 
01528 /* Name of source file whose symbol data we are now processing.  This
01529    comes from a symbol of type N_SO for stabs.  For Dwarf it comes
01530    from the DW_AT_name attribute of a DW_TAG_compile_unit DIE.  */
01531 
01532 static char *last_source_file;
01533 
01534 /* See buildsym.h.  */
01535 
01536 void
01537 set_last_source_file (const char *name)
01538 {
01539   xfree (last_source_file);
01540   last_source_file = name == NULL ? NULL : xstrdup (name);
01541 }
01542 
01543 /* See buildsym.h.  */
01544 
01545 const char *
01546 get_last_source_file (void)
01547 {
01548   return last_source_file;
01549 }
01550 
01551 
01552 
01553 /* Initialize anything that needs initializing when starting to read a
01554    fresh piece of a symbol file, e.g. reading in the stuff
01555    corresponding to a psymtab.  */
01556 
01557 void
01558 buildsym_init (void)
01559 {
01560   free_pendings = NULL;
01561   file_symbols = NULL;
01562   global_symbols = NULL;
01563   pending_blocks = NULL;
01564   pending_macros = NULL;
01565   using_directives = NULL;
01566   subfile_stack = NULL;
01567 
01568   /* We shouldn't have any address map at this point.  */
01569   gdb_assert (! pending_addrmap);
01570   pending_addrmap_interesting = 0;
01571 }
01572 
01573 /* Initialize anything that needs initializing when a completely new
01574    symbol file is specified (not just adding some symbols from another
01575    file, e.g. a shared library).  */
01576 
01577 void
01578 buildsym_new_init (void)
01579 {
01580   buildsym_init ();
01581 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines