gdb/buildsym.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Support routines for building symbol tables in GDB's internal format.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* This module provides subroutines used for creating and adding to
  15.    the symbol table.  These routines are called from various symbol-
  16.    file-reading routines.

  17.    Routines to support specific debugging information formats (stabs,
  18.    DWARF, etc) belong somewhere else.

  19.    The basic way this module is used is as follows:

  20.    buildsym_init ();
  21.    cleanups = make_cleanup (really_free_pendings, NULL);
  22.    cust = start_symtab (...);
  23.    ... read debug info ...
  24.    cust = end_symtab (...);
  25.    do_cleanups (cleanups);

  26.    The compunit symtab pointer ("cust") is returned from both start_symtab
  27.    and end_symtab to simplify the debug info readers.

  28.    There are minor variations on this, e.g., dwarf2read.c splits end_symtab
  29.    into two calls: end_symtab_get_static_block, end_symtab_from_static_block,
  30.    but all debug info readers follow this basic flow.

  31.    Reading DWARF Type Units is another variation:

  32.    buildsym_init ();
  33.    cleanups = make_cleanup (really_free_pendings, NULL);
  34.    cust = start_symtab (...);
  35.    ... read debug info ...
  36.    cust = end_expandable_symtab (...);
  37.    do_cleanups (cleanups);

  38.    And then reading subsequent Type Units within the containing "Comp Unit"
  39.    will use a second flow:

  40.    buildsym_init ();
  41.    cleanups = make_cleanup (really_free_pendings, NULL);
  42.    cust = restart_symtab (...);
  43.    ... read debug info ...
  44.    cust = augment_type_symtab (...);
  45.    do_cleanups (cleanups);

  46.    dbxread.c and xcoffread.c use another variation:

  47.    buildsym_init ();
  48.    cleanups = make_cleanup (really_free_pendings, NULL);
  49.    cust = start_symtab (...);
  50.    ... read debug info ...
  51.    cust = end_symtab (...);
  52.    ... start_symtab + read + end_symtab repeated ...
  53.    do_cleanups (cleanups);
  54. */

  55. #include "defs.h"
  56. #include "bfd.h"
  57. #include "gdb_obstack.h"
  58. #include "symtab.h"
  59. #include "symfile.h"
  60. #include "objfiles.h"
  61. #include "gdbtypes.h"
  62. #include "complaints.h"
  63. #include "expression.h"                /* For "enum exp_opcode" used by...  */
  64. #include "bcache.h"
  65. #include "filenames.h"                /* For DOSish file names.  */
  66. #include "macrotab.h"
  67. #include "demangle.h"                /* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
  68. #include "block.h"
  69. #include "cp-support.h"
  70. #include "dictionary.h"
  71. #include "addrmap.h"

  72. /* Ask buildsym.h to define the vars it normally declares `extern'.  */
  73. #define        EXTERN
  74. /**/
  75. #include "buildsym.h"                /* Our own declarations.  */
  76. #undef        EXTERN

  77. /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
  78.    questionable--see comment where we call them).  */

  79. #include "stabsread.h"

  80. /* Buildsym's counterpart to struct compunit_symtab.
  81.    TODO(dje): Move all related global state into here.  */

  82. struct buildsym_compunit
  83. {
  84.   /* The objfile we're reading debug info from.  */
  85.   struct objfile *objfile;

  86.   /* List of subfiles (source files).
  87.      Files are added to the front of the list.
  88.      This is important mostly for the language determination hacks we use,
  89.      which iterate over previously added files.  */
  90.   struct subfile *subfiles;

  91.   /* The subfile of the main source file.  */
  92.   struct subfile *main_subfile;

  93.   /* E.g., DW_AT_comp_dir if DWARF.  Space for this is malloc'd.  */
  94.   char *comp_dir;

  95.   /* Space for this is not malloc'd, and is assumed to have at least
  96.      the same lifetime as objfile.  */
  97.   const char *producer;

  98.   /* Space for this is not malloc'd, and is assumed to have at least
  99.      the same lifetime as objfile.  */
  100.   const char *debugformat;

  101.   /* The compunit we are building.  */
  102.   struct compunit_symtab *compunit_symtab;
  103. };

  104. /* The work-in-progress of the compunit we are building.
  105.    This is created first, before any subfiles by start_symtab.  */

  106. static struct buildsym_compunit *buildsym_compunit;

  107. /* List of free `struct pending' structures for reuse.  */

  108. static struct pending *free_pendings;

  109. /* Non-zero if symtab has line number info.  This prevents an
  110.    otherwise empty symtab from being tossed.  */

  111. static int have_line_numbers;

  112. /* The mutable address map for the compilation unit whose symbols
  113.    we're currently reading.  The symtabs' shared blockvector will
  114.    point to a fixed copy of this.  */
  115. static struct addrmap *pending_addrmap;

  116. /* The obstack on which we allocate pending_addrmap.
  117.    If pending_addrmap is NULL, this is uninitialized; otherwise, it is
  118.    initialized (and holds pending_addrmap).  */
  119. static struct obstack pending_addrmap_obstack;

  120. /* Non-zero if we recorded any ranges in the addrmap that are
  121.    different from those in the blockvector already.  We set this to
  122.    zero when we start processing a symfile, and if it's still zero at
  123.    the end, then we just toss the addrmap.  */
  124. static int pending_addrmap_interesting;

  125. /* An obstack used for allocating pending blocks.  */

  126. static struct obstack pending_block_obstack;

  127. /* List of blocks already made (lexical contexts already closed).
  128.    This is used at the end to make the blockvector.  */

  129. struct pending_block
  130.   {
  131.     struct pending_block *next;
  132.     struct block *block;
  133.   };

  134. /* Pointer to the head of a linked list of symbol blocks which have
  135.    already been finalized (lexical contexts already closed) and which
  136.    are just waiting to be built into a blockvector when finalizing the
  137.    associated symtab.  */

  138. static struct pending_block *pending_blocks;

  139. struct subfile_stack
  140.   {
  141.     struct subfile_stack *next;
  142.     char *name;
  143.   };

  144. static struct subfile_stack *subfile_stack;

  145. /* The macro table for the compilation unit whose symbols we're
  146.    currently reading.  */
  147. static struct macro_table *pending_macros;

  148. static void free_buildsym_compunit (void);

  149. static int compare_line_numbers (const void *ln1p, const void *ln2p);

  150. static void record_pending_block (struct objfile *objfile,
  151.                                   struct block *block,
  152.                                   struct pending_block *opblock);

  153. /* Initial sizes of data structures.  These are realloc'd larger if
  154.    needed, and realloc'd down to the size actually used, when
  155.    completed.  */

  156. #define        INITIAL_CONTEXT_STACK_SIZE        10
  157. #define        INITIAL_LINE_VECTOR_LENGTH        1000


  158. /* Maintain the lists of symbols and blocks.  */

  159. /* Add a symbol to one of the lists of symbols.  */

  160. void
  161. add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
  162. {
  163.   struct pending *link;

  164.   /* If this is an alias for another symbol, don't add it.  */
  165.   if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
  166.     return;

  167.   /* We keep PENDINGSIZE symbols in each link of the list.  If we
  168.      don't have a link with room in it, add a new link.  */
  169.   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
  170.     {
  171.       if (free_pendings)
  172.         {
  173.           link = free_pendings;
  174.           free_pendings = link->next;
  175.         }
  176.       else
  177.         {
  178.           link = (struct pending *) xmalloc (sizeof (struct pending));
  179.         }

  180.       link->next = *listhead;
  181.       *listhead = link;
  182.       link->nsyms = 0;
  183.     }

  184.   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
  185. }

  186. /* Find a symbol named NAME on a LIST.  NAME need not be
  187.    '\0'-terminated; LENGTH is the length of the name.  */

  188. struct symbol *
  189. find_symbol_in_list (struct pending *list, char *name, int length)
  190. {
  191.   int j;
  192.   const char *pp;

  193.   while (list != NULL)
  194.     {
  195.       for (j = list->nsyms; --j >= 0;)
  196.         {
  197.           pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
  198.           if (*pp == *name && strncmp (pp, name, length) == 0
  199.               && pp[length] == '\0')
  200.             {
  201.               return (list->symbol[j]);
  202.             }
  203.         }
  204.       list = list->next;
  205.     }
  206.   return (NULL);
  207. }

  208. /* At end of reading syms, or in case of quit, ensure everything associated
  209.    with building symtabs is freed.  This is intended to be registered as a
  210.    cleanup before doing psymtab->symtab expansion.

  211.    N.B. This is *not* intended to be used when building psymtabs.  Some debug
  212.    info readers call this anyway, which is harmless if confusing.  */

  213. void
  214. really_free_pendings (void *dummy)
  215. {
  216.   struct pending *next, *next1;

  217.   for (next = free_pendings; next; next = next1)
  218.     {
  219.       next1 = next->next;
  220.       xfree ((void *) next);
  221.     }
  222.   free_pendings = NULL;

  223.   free_pending_blocks ();

  224.   for (next = file_symbols; next != NULL; next = next1)
  225.     {
  226.       next1 = next->next;
  227.       xfree ((void *) next);
  228.     }
  229.   file_symbols = NULL;

  230.   for (next = global_symbols; next != NULL; next = next1)
  231.     {
  232.       next1 = next->next;
  233.       xfree ((void *) next);
  234.     }
  235.   global_symbols = NULL;

  236.   if (pending_macros)
  237.     free_macro_table (pending_macros);
  238.   pending_macros = NULL;

  239.   if (pending_addrmap)
  240.     obstack_free (&pending_addrmap_obstack, NULL);
  241.   pending_addrmap = NULL;

  242.   free_buildsym_compunit ();
  243. }

  244. /* This function is called to discard any pending blocks.  */

  245. void
  246. free_pending_blocks (void)
  247. {
  248.   if (pending_blocks != NULL)
  249.     {
  250.       obstack_free (&pending_block_obstack, NULL);
  251.       pending_blocks = NULL;
  252.     }
  253. }

  254. /* Take one of the lists of symbols and make a block from it.  Keep
  255.    the order the symbols have in the list (reversed from the input
  256.    file).  Put the block on the list of pending blocks.  */

  257. static struct block *
  258. finish_block_internal (struct symbol *symbol, struct pending **listhead,
  259.                        struct pending_block *old_blocks,
  260.                        CORE_ADDR start, CORE_ADDR end,
  261.                        int is_global, int expandable)
  262. {
  263.   struct objfile *objfile = buildsym_compunit->objfile;
  264.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  265.   struct pending *next, *next1;
  266.   struct block *block;
  267.   struct pending_block *pblock;
  268.   struct pending_block *opblock;

  269.   block = (is_global
  270.            ? allocate_global_block (&objfile->objfile_obstack)
  271.            : allocate_block (&objfile->objfile_obstack));

  272.   if (symbol)
  273.     {
  274.       BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
  275.                                                *listhead);
  276.     }
  277.   else
  278.     {
  279.       if (expandable)
  280.         {
  281.           BLOCK_DICT (block) = dict_create_hashed_expandable ();
  282.           dict_add_pending (BLOCK_DICT (block), *listhead);
  283.         }
  284.       else
  285.         {
  286.           BLOCK_DICT (block) =
  287.             dict_create_hashed (&objfile->objfile_obstack, *listhead);
  288.         }
  289.     }

  290.   BLOCK_START (block) = start;
  291.   BLOCK_END (block) = end;

  292.   /* Put the block in as the value of the symbol that names it.  */

  293.   if (symbol)
  294.     {
  295.       struct type *ftype = SYMBOL_TYPE (symbol);
  296.       struct dict_iterator iter;
  297.       SYMBOL_BLOCK_VALUE (symbol) = block;
  298.       BLOCK_FUNCTION (block) = symbol;

  299.       if (TYPE_NFIELDS (ftype) <= 0)
  300.         {
  301.           /* No parameter type information is recorded with the
  302.              function's type.  Set that from the type of the
  303.              parameter symbols.  */
  304.           int nparams = 0, iparams;
  305.           struct symbol *sym;

  306.           /* Here we want to directly access the dictionary, because
  307.              we haven't fully initialized the block yet.  */
  308.           ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
  309.             {
  310.               if (SYMBOL_IS_ARGUMENT (sym))
  311.                 nparams++;
  312.             }
  313.           if (nparams > 0)
  314.             {
  315.               TYPE_NFIELDS (ftype) = nparams;
  316.               TYPE_FIELDS (ftype) = (struct field *)
  317.                 TYPE_ALLOC (ftype, nparams * sizeof (struct field));

  318.               iparams = 0;
  319.               /* Here we want to directly access the dictionary, because
  320.                  we haven't fully initialized the block yet.  */
  321.               ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
  322.                 {
  323.                   if (iparams == nparams)
  324.                     break;

  325.                   if (SYMBOL_IS_ARGUMENT (sym))
  326.                     {
  327.                       TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
  328.                       TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
  329.                       iparams++;
  330.                     }
  331.                 }
  332.             }
  333.         }
  334.     }
  335.   else
  336.     {
  337.       BLOCK_FUNCTION (block) = NULL;
  338.     }

  339.   /* Now "free" the links of the list, and empty the list.  */

  340.   for (next = *listhead; next; next = next1)
  341.     {
  342.       next1 = next->next;
  343.       next->next = free_pendings;
  344.       free_pendings = next;
  345.     }
  346.   *listhead = NULL;

  347.   /* Check to be sure that the blocks have an end address that is
  348.      greater than starting address.  */

  349.   if (BLOCK_END (block) < BLOCK_START (block))
  350.     {
  351.       if (symbol)
  352.         {
  353.           complaint (&symfile_complaints,
  354.                      _("block end address less than block "
  355.                        "start address in %s (patched it)"),
  356.                      SYMBOL_PRINT_NAME (symbol));
  357.         }
  358.       else
  359.         {
  360.           complaint (&symfile_complaints,
  361.                      _("block end address %s less than block "
  362.                        "start address %s (patched it)"),
  363.                      paddress (gdbarch, BLOCK_END (block)),
  364.                      paddress (gdbarch, BLOCK_START (block)));
  365.         }
  366.       /* Better than nothing.  */
  367.       BLOCK_END (block) = BLOCK_START (block);
  368.     }

  369.   /* Install this block as the superblock of all blocks made since the
  370.      start of this scope that don't have superblocks yet.  */

  371.   opblock = NULL;
  372.   for (pblock = pending_blocks;
  373.        pblock && pblock != old_blocks;
  374.        pblock = pblock->next)
  375.     {
  376.       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
  377.         {
  378.           /* Check to be sure the blocks are nested as we receive
  379.              them.  If the compiler/assembler/linker work, this just
  380.              burns a small amount of time.

  381.              Skip blocks which correspond to a function; they're not
  382.              physically nested inside this other blocks, only
  383.              lexically nested.  */
  384.           if (BLOCK_FUNCTION (pblock->block) == NULL
  385.               && (BLOCK_START (pblock->block) < BLOCK_START (block)
  386.                   || BLOCK_END (pblock->block) > BLOCK_END (block)))
  387.             {
  388.               if (symbol)
  389.                 {
  390.                   complaint (&symfile_complaints,
  391.                              _("inner block not inside outer block in %s"),
  392.                              SYMBOL_PRINT_NAME (symbol));
  393.                 }
  394.               else
  395.                 {
  396.                   complaint (&symfile_complaints,
  397.                              _("inner block (%s-%s) not "
  398.                                "inside outer block (%s-%s)"),
  399.                              paddress (gdbarch, BLOCK_START (pblock->block)),
  400.                              paddress (gdbarch, BLOCK_END (pblock->block)),
  401.                              paddress (gdbarch, BLOCK_START (block)),
  402.                              paddress (gdbarch, BLOCK_END (block)));
  403.                 }
  404.               if (BLOCK_START (pblock->block) < BLOCK_START (block))
  405.                 BLOCK_START (pblock->block) = BLOCK_START (block);
  406.               if (BLOCK_END (pblock->block) > BLOCK_END (block))
  407.                 BLOCK_END (pblock->block) = BLOCK_END (block);
  408.             }
  409.           BLOCK_SUPERBLOCK (pblock->block) = block;
  410.         }
  411.       opblock = pblock;
  412.     }

  413.   block_set_using (block, using_directives, &objfile->objfile_obstack);
  414.   using_directives = NULL;

  415.   record_pending_block (objfile, block, opblock);

  416.   return block;
  417. }

  418. struct block *
  419. finish_block (struct symbol *symbol, struct pending **listhead,
  420.               struct pending_block *old_blocks,
  421.               CORE_ADDR start, CORE_ADDR end)
  422. {
  423.   return finish_block_internal (symbol, listhead, old_blocks,
  424.                                 start, end, 0, 0);
  425. }

  426. /* Record BLOCK on the list of all blocks in the file.  Put it after
  427.    OPBLOCK, or at the beginning if opblock is NULL.  This puts the
  428.    block in the list after all its subblocks.

  429.    Allocate the pending block struct in the objfile_obstack to save
  430.    time.  This wastes a little space.  FIXME: Is it worth it?  */

  431. static void
  432. record_pending_block (struct objfile *objfile, struct block *block,
  433.                       struct pending_block *opblock)
  434. {
  435.   struct pending_block *pblock;

  436.   if (pending_blocks == NULL)
  437.     obstack_init (&pending_block_obstack);

  438.   pblock = (struct pending_block *)
  439.     obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
  440.   pblock->block = block;
  441.   if (opblock)
  442.     {
  443.       pblock->next = opblock->next;
  444.       opblock->next = pblock;
  445.     }
  446.   else
  447.     {
  448.       pblock->next = pending_blocks;
  449.       pending_blocks = pblock;
  450.     }
  451. }


  452. /* Record that the range of addresses from START to END_INCLUSIVE
  453.    (inclusive, like it says) belongs to BLOCK.  BLOCK's start and end
  454.    addresses must be set already.  You must apply this function to all
  455.    BLOCK's children before applying it to BLOCK.

  456.    If a call to this function complicates the picture beyond that
  457.    already provided by BLOCK_START and BLOCK_END, then we create an
  458.    address map for the block.  */
  459. void
  460. record_block_range (struct block *block,
  461.                     CORE_ADDR start, CORE_ADDR end_inclusive)
  462. {
  463.   /* If this is any different from the range recorded in the block's
  464.      own BLOCK_START and BLOCK_END, then note that the address map has
  465.      become interesting.  Note that even if this block doesn't have
  466.      any "interesting" ranges, some later block might, so we still
  467.      need to record this block in the addrmap.  */
  468.   if (start != BLOCK_START (block)
  469.       || end_inclusive + 1 != BLOCK_END (block))
  470.     pending_addrmap_interesting = 1;

  471.   if (! pending_addrmap)
  472.     {
  473.       obstack_init (&pending_addrmap_obstack);
  474.       pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
  475.     }

  476.   addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
  477. }

  478. static struct blockvector *
  479. make_blockvector (void)
  480. {
  481.   struct objfile *objfile = buildsym_compunit->objfile;
  482.   struct pending_block *next;
  483.   struct blockvector *blockvector;
  484.   int i;

  485.   /* Count the length of the list of blocks.  */

  486.   for (next = pending_blocks, i = 0; next; next = next->next, i++)
  487.     {;
  488.     }

  489.   blockvector = (struct blockvector *)
  490.     obstack_alloc (&objfile->objfile_obstack,
  491.                    (sizeof (struct blockvector)
  492.                     + (i - 1) * sizeof (struct block *)));

  493.   /* Copy the blocks into the blockvector.  This is done in reverse
  494.      order, which happens to put the blocks into the proper order
  495.      (ascending starting address).  finish_block has hair to insert
  496.      each block into the list after its subblocks in order to make
  497.      sure this is true.  */

  498.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  499.   for (next = pending_blocks; next; next = next->next)
  500.     {
  501.       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  502.     }

  503.   free_pending_blocks ();

  504.   /* If we needed an address map for this symtab, record it in the
  505.      blockvector.  */
  506.   if (pending_addrmap && pending_addrmap_interesting)
  507.     BLOCKVECTOR_MAP (blockvector)
  508.       = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
  509.   else
  510.     BLOCKVECTOR_MAP (blockvector) = 0;

  511.   /* Some compilers output blocks in the wrong order, but we depend on
  512.      their being in the right order so we can binary search.  Check the
  513.      order and moan about it.
  514.      Note: Remember that the first two blocks are the global and static
  515.      blocks.  We could special case that fact and begin checking at block 2.
  516.      To avoid making that assumption we do not.  */
  517.   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
  518.     {
  519.       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
  520.         {
  521.           if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
  522.               > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
  523.             {
  524.               CORE_ADDR start
  525.                 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));

  526.               complaint (&symfile_complaints, _("block at %s out of order"),
  527.                          hex_string ((LONGEST) start));
  528.             }
  529.         }
  530.     }

  531.   return (blockvector);
  532. }

  533. /* Start recording information about source code that came from an
  534.    included (or otherwise merged-in) source file with a different
  535.    nameNAME is the name of the file (cannot be NULL).  */

  536. void
  537. start_subfile (const char *name)
  538. {
  539.   const char *subfile_dirname;
  540.   struct subfile *subfile;

  541.   gdb_assert (buildsym_compunit != NULL);

  542.   subfile_dirname = buildsym_compunit->comp_dir;

  543.   /* See if this subfile is already registered.  */

  544.   for (subfile = buildsym_compunit->subfiles; subfile; subfile = subfile->next)
  545.     {
  546.       char *subfile_name;

  547.       /* If NAME is an absolute path, and this subfile is not, then
  548.          attempt to create an absolute path to compare.  */
  549.       if (IS_ABSOLUTE_PATH (name)
  550.           && !IS_ABSOLUTE_PATH (subfile->name)
  551.           && subfile_dirname != NULL)
  552.         subfile_name = concat (subfile_dirname, SLASH_STRING,
  553.                                subfile->name, (char *) NULL);
  554.       else
  555.         subfile_name = subfile->name;

  556.       if (FILENAME_CMP (subfile_name, name) == 0)
  557.         {
  558.           current_subfile = subfile;
  559.           if (subfile_name != subfile->name)
  560.             xfree (subfile_name);
  561.           return;
  562.         }
  563.       if (subfile_name != subfile->name)
  564.         xfree (subfile_name);
  565.     }

  566.   /* This subfile is not known.  Add an entry for it.  */

  567.   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
  568.   memset (subfile, 0, sizeof (struct subfile));
  569.   subfile->buildsym_compunit = buildsym_compunit;

  570.   subfile->next = buildsym_compunit->subfiles;
  571.   buildsym_compunit->subfiles = subfile;

  572.   current_subfile = subfile;

  573.   subfile->name = xstrdup (name);

  574.   /* Initialize line-number recording for this subfile.  */
  575.   subfile->line_vector = NULL;

  576.   /* Default the source language to whatever can be deduced from the
  577.      filename.  If nothing can be deduced (such as for a C/C++ include
  578.      file with a ".h" extension), then inherit whatever language the
  579.      previous subfile had.  This kludgery is necessary because there
  580.      is no standard way in some object formats to record the source
  581.      language.  Also, when symtabs are allocated we try to deduce a
  582.      language then as well, but it is too late for us to use that
  583.      information while reading symbols, since symtabs aren't allocated
  584.      until after all the symbols have been processed for a given
  585.      source file.  */

  586.   subfile->language = deduce_language_from_filename (subfile->name);
  587.   if (subfile->language == language_unknown
  588.       && subfile->next != NULL)
  589.     {
  590.       subfile->language = subfile->next->language;
  591.     }

  592.   /* If the filename of this subfile ends in .C, then change the
  593.      language of any pending subfiles from C to C++.  We also accept
  594.      any other C++ suffixes accepted by deduce_language_from_filename.  */
  595.   /* Likewise for f2c.  */

  596.   if (subfile->name)
  597.     {
  598.       struct subfile *s;
  599.       enum language sublang = deduce_language_from_filename (subfile->name);

  600.       if (sublang == language_cplus || sublang == language_fortran)
  601.         for (s = buildsym_compunit->subfiles; s != NULL; s = s->next)
  602.           if (s->language == language_c)
  603.             s->language = sublang;
  604.     }

  605.   /* And patch up this file if necessary.  */
  606.   if (subfile->language == language_c
  607.       && subfile->next != NULL
  608.       && (subfile->next->language == language_cplus
  609.           || subfile->next->language == language_fortran))
  610.     {
  611.       subfile->language = subfile->next->language;
  612.     }
  613. }

  614. /* Start recording information about a primary source file (IOW, not an
  615.    included source file).
  616.    COMP_DIR is the directory in which the compilation unit was compiled
  617.    (or NULL if not known).  */

  618. static struct buildsym_compunit *
  619. start_buildsym_compunit (struct objfile *objfile, const char *comp_dir)
  620. {
  621.   struct buildsym_compunit *bscu;

  622.   bscu = (struct buildsym_compunit *)
  623.     xmalloc (sizeof (struct buildsym_compunit));
  624.   memset (bscu, 0, sizeof (struct buildsym_compunit));

  625.   bscu->objfile = objfile;
  626.   bscu->comp_dir = (comp_dir == NULL) ? NULL : xstrdup (comp_dir);

  627.   /* Initialize the debug format string to NULL.  We may supply it
  628.      later via a call to record_debugformat.  */
  629.   bscu->debugformat = NULL;

  630.   /* Similarly for the producer.  */
  631.   bscu->producer = NULL;

  632.   return bscu;
  633. }

  634. /* Delete the buildsym compunit.  */

  635. static void
  636. free_buildsym_compunit (void)
  637. {
  638.   struct subfile *subfile, *nextsub;

  639.   if (buildsym_compunit == NULL)
  640.     return;
  641.   for (subfile = buildsym_compunit->subfiles;
  642.        subfile != NULL;
  643.        subfile = nextsub)
  644.     {
  645.       nextsub = subfile->next;
  646.       xfree (subfile->name);
  647.       xfree (subfile->line_vector);
  648.       xfree (subfile);
  649.     }
  650.   xfree (buildsym_compunit->comp_dir);
  651.   xfree (buildsym_compunit);
  652.   buildsym_compunit = NULL;
  653.   current_subfile = NULL;
  654. }

  655. /* For stabs readers, the first N_SO symbol is assumed to be the
  656.    source file name, and the subfile struct is initialized using that
  657.    assumption.  If another N_SO symbol is later seen, immediately
  658.    following the first one, then the first one is assumed to be the
  659.    directory name and the second one is really the source file name.

  660.    So we have to patch up the subfile struct by moving the old name
  661.    value to dirname and remembering the new name.  Some sanity
  662.    checking is performed to ensure that the state of the subfile
  663.    struct is reasonable and that the old name we are assuming to be a
  664.    directory name actually is (by checking for a trailing '/').  */

  665. void
  666. patch_subfile_names (struct subfile *subfile, char *name)
  667. {
  668.   if (subfile != NULL
  669.       && buildsym_compunit->comp_dir == NULL
  670.       && subfile->name != NULL
  671.       && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
  672.     {
  673.       buildsym_compunit->comp_dir = subfile->name;
  674.       subfile->name = xstrdup (name);
  675.       set_last_source_file (name);

  676.       /* Default the source language to whatever can be deduced from
  677.          the filename.  If nothing can be deduced (such as for a C/C++
  678.          include file with a ".h" extension), then inherit whatever
  679.          language the previous subfile had.  This kludgery is
  680.          necessary because there is no standard way in some object
  681.          formats to record the source language.  Also, when symtabs
  682.          are allocated we try to deduce a language then as well, but
  683.          it is too late for us to use that information while reading
  684.          symbols, since symtabs aren't allocated until after all the
  685.          symbols have been processed for a given source file.  */

  686.       subfile->language = deduce_language_from_filename (subfile->name);
  687.       if (subfile->language == language_unknown
  688.           && subfile->next != NULL)
  689.         {
  690.           subfile->language = subfile->next->language;
  691.         }
  692.     }
  693. }

  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. void
  699. push_subfile (void)
  700. {
  701.   struct subfile_stack *tem
  702.     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));

  703.   tem->next = subfile_stack;
  704.   subfile_stack = tem;
  705.   if (current_subfile == NULL || current_subfile->name == NULL)
  706.     {
  707.       internal_error (__FILE__, __LINE__,
  708.                       _("failed internal consistency check"));
  709.     }
  710.   tem->name = current_subfile->name;
  711. }

  712. char *
  713. pop_subfile (void)
  714. {
  715.   char *name;
  716.   struct subfile_stack *link = subfile_stack;

  717.   if (link == NULL)
  718.     {
  719.       internal_error (__FILE__, __LINE__,
  720.                       _("failed internal consistency check"));
  721.     }
  722.   name = link->name;
  723.   subfile_stack = link->next;
  724.   xfree ((void *) link);
  725.   return (name);
  726. }

  727. /* Add a linetable entry for line number LINE and address PC to the
  728.    line vector for SUBFILE.  */

  729. void
  730. record_line (struct subfile *subfile, int line, CORE_ADDR pc)
  731. {
  732.   struct linetable_entry *e;

  733.   /* Ignore the dummy line number in libg.o */
  734.   if (line == 0xffff)
  735.     {
  736.       return;
  737.     }

  738.   /* Make sure line vector exists and is big enough.  */
  739.   if (!subfile->line_vector)
  740.     {
  741.       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
  742.       subfile->line_vector = (struct linetable *)
  743.         xmalloc (sizeof (struct linetable)
  744.            + subfile->line_vector_length * sizeof (struct linetable_entry));
  745.       subfile->line_vector->nitems = 0;
  746.       have_line_numbers = 1;
  747.     }

  748.   if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
  749.     {
  750.       subfile->line_vector_length *= 2;
  751.       subfile->line_vector = (struct linetable *)
  752.         xrealloc ((char *) subfile->line_vector,
  753.                   (sizeof (struct linetable)
  754.                    + (subfile->line_vector_length
  755.                       * sizeof (struct linetable_entry))));
  756.     }

  757.   /* Normally, we treat lines as unsorted.  But the end of sequence
  758.      marker is special.  We sort line markers at the same PC by line
  759.      number, so end of sequence markers (which have line == 0) appear
  760.      first.  This is right if the marker ends the previous function,
  761.      and there is no padding before the next function.  But it is
  762.      wrong if the previous line was empty and we are now marking a
  763.      switch to a different subfile.  We must leave the end of sequence
  764.      marker at the end of this group of lines, not sort the empty line
  765.      to after the marker.  The easiest way to accomplish this is to
  766.      delete any empty lines from our table, if they are followed by
  767.      end of sequence markers.  All we lose is the ability to set
  768.      breakpoints at some lines which contain no instructions
  769.      anyway.  */
  770.   if (line == 0 && subfile->line_vector->nitems > 0)
  771.     {
  772.       e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
  773.       while (subfile->line_vector->nitems > 0 && e->pc == pc)
  774.         {
  775.           e--;
  776.           subfile->line_vector->nitems--;
  777.         }
  778.     }

  779.   e = subfile->line_vector->item + subfile->line_vector->nitems++;
  780.   e->line = line;
  781.   e->pc = pc;
  782. }

  783. /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */

  784. static int
  785. compare_line_numbers (const void *ln1p, const void *ln2p)
  786. {
  787.   struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
  788.   struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;

  789.   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
  790.      Please keep it that way.  */
  791.   if (ln1->pc < ln2->pc)
  792.     return -1;

  793.   if (ln1->pc > ln2->pc)
  794.     return 1;

  795.   /* If pc equal, sort by line.  I'm not sure whether this is optimum
  796.      behavior (see comment at struct linetable in symtab.h).  */
  797.   return ln1->line - ln2->line;
  798. }

  799. /* See buildsym.h.  */

  800. struct compunit_symtab *
  801. buildsym_compunit_symtab (void)
  802. {
  803.   gdb_assert (buildsym_compunit != NULL);

  804.   return buildsym_compunit->compunit_symtab;
  805. }

  806. /* See buildsym.h.  */

  807. struct macro_table *
  808. get_macro_table (void)
  809. {
  810.   struct objfile *objfile;

  811.   gdb_assert (buildsym_compunit != NULL);

  812.   objfile = buildsym_compunit->objfile;

  813.   if (! pending_macros)
  814.     {
  815.       pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
  816.                                         objfile->per_bfd->macro_cache,
  817.                                         buildsym_compunit->compunit_symtab);
  818.     }

  819.   return pending_macros;
  820. }

  821. /* Init state to prepare for building a symtab.
  822.    Note: This can't be done in buildsym_init because dbxread.c and xcoffread.c
  823.    can call start_symtab+end_symtab multiple times after one call to
  824.    buildsym_init.  */

  825. static void
  826. prepare_for_building (const char *name, CORE_ADDR start_addr)
  827. {
  828.   set_last_source_file (name);
  829.   last_source_start_addr = start_addr;

  830.   local_symbols = NULL;
  831.   within_function = 0;
  832.   have_line_numbers = 0;

  833.   context_stack_depth = 0;

  834.   /* These should have been reset either by successful completion of building
  835.      a symtab, or by the really_free_pendings cleanup.  */
  836.   gdb_assert (file_symbols == NULL);
  837.   gdb_assert (global_symbols == NULL);
  838.   gdb_assert (pending_macros == NULL);
  839.   gdb_assert (pending_addrmap == NULL);
  840.   gdb_assert (current_subfile == NULL);
  841. }

  842. /* Start a new symtab for a new source file in OBJFILE.  Called, for example,
  843.    when a stabs symbol of type N_SO is seen, or when a DWARF
  844.    TAG_compile_unit DIE is seen.  It indicates the start of data for
  845.    one original source file.

  846.    NAME is the name of the file (cannot be NULL).  COMP_DIR is the directory in
  847.    which the file was compiled (or NULL if not known).  START_ADDR is the
  848.    lowest address of objects in the file (or 0 if not known).  */

  849. struct compunit_symtab *
  850. start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
  851.               CORE_ADDR start_addr)
  852. {
  853.   prepare_for_building (name, start_addr);

  854.   buildsym_compunit = start_buildsym_compunit (objfile, comp_dir);

  855.   /* Allocate the compunit symtab now.  The caller needs it to allocate
  856.      non-primary symtabs.  It is also needed by get_macro_table.  */
  857.   buildsym_compunit->compunit_symtab = allocate_compunit_symtab (objfile,
  858.                                                                  name);

  859.   /* Build the subfile for NAME (the main source file) so that we can record
  860.      a pointer to it for later.
  861.      IMPORTANT: Do not allocate a struct symtab for NAME here.
  862.      It can happen that the debug info provides a different path to NAME than
  863.      DIRNAME,NAME.  We cope with this in watch_main_source_file_lossage but
  864.      that only works if the main_subfile doesn't have a symtab yet.  */
  865.   start_subfile (name);
  866.   /* Save this so that we don't have to go looking for it at the end
  867.      of the subfiles list.  */
  868.   buildsym_compunit->main_subfile = current_subfile;

  869.   return buildsym_compunit->compunit_symtab;
  870. }

  871. /* Restart compilation for a symtab.
  872.    CUST is the result of end_expandable_symtab.
  873.    NAME, START_ADDR are the source file we are resuming with.

  874.    This is used when a symtab is built from multiple sources.
  875.    The symtab is first built with start_symtab/end_expandable_symtab
  876.    and then for each additional piece call restart_symtab/augment_*_symtab.
  877.    Note: At the moment there is only augment_type_symtab.  */

  878. void
  879. restart_symtab (struct compunit_symtab *cust,
  880.                 const char *name, CORE_ADDR start_addr)
  881. {
  882.   prepare_for_building (name, start_addr);

  883.   buildsym_compunit = start_buildsym_compunit (COMPUNIT_OBJFILE (cust),
  884.                                                COMPUNIT_DIRNAME (cust));
  885.   buildsym_compunit->compunit_symtab = cust;
  886. }

  887. /* Subroutine of end_symtab to simplify it.  Look for a subfile that
  888.    matches the main source file's basename.  If there is only one, and
  889.    if the main source file doesn't have any symbol or line number
  890.    information, then copy this file's symtab and line_vector to the
  891.    main source file's subfile and discard the other subfile.  This can
  892.    happen because of a compiler bug or from the user playing games
  893.    with #line or from things like a distributed build system that
  894.    manipulates the debug info.  This can also happen from an innocent
  895.    symlink in the paths, we don't canonicalize paths here.  */

  896. static void
  897. watch_main_source_file_lossage (void)
  898. {
  899.   struct subfile *mainsub, *subfile;

  900.   /* We have to watch for buildsym_compunit == NULL here.  It's a quirk of
  901.      end_symtab, it can return NULL so there may not be a main subfile.  */
  902.   if (buildsym_compunit == NULL)
  903.     return;

  904.   /* Get the main source file.  */
  905.   mainsub = buildsym_compunit->main_subfile;

  906.   /* If the main source file doesn't have any line number or symbol
  907.      info, look for an alias in another subfile.  */

  908.   if (mainsub->line_vector == NULL
  909.       && mainsub->symtab == NULL)
  910.     {
  911.       const char *mainbase = lbasename (mainsub->name);
  912.       int nr_matches = 0;
  913.       struct subfile *prevsub;
  914.       struct subfile *mainsub_alias = NULL;
  915.       struct subfile *prev_mainsub_alias = NULL;

  916.       prevsub = NULL;
  917.       for (subfile = buildsym_compunit->subfiles;
  918.            subfile != NULL;
  919.            subfile = subfile->next)
  920.         {
  921.           if (subfile == mainsub)
  922.             continue;
  923.           if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
  924.             {
  925.               ++nr_matches;
  926.               mainsub_alias = subfile;
  927.               prev_mainsub_alias = prevsub;
  928.             }
  929.           prevsub = subfile;
  930.         }

  931.       if (nr_matches == 1)
  932.         {
  933.           gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);

  934.           /* Found a match for the main source file.
  935.              Copy its line_vector and symtab to the main subfile
  936.              and then discard it.  */

  937.           mainsub->line_vector = mainsub_alias->line_vector;
  938.           mainsub->line_vector_length = mainsub_alias->line_vector_length;
  939.           mainsub->symtab = mainsub_alias->symtab;

  940.           if (prev_mainsub_alias == NULL)
  941.             buildsym_compunit->subfiles = mainsub_alias->next;
  942.           else
  943.             prev_mainsub_alias->next = mainsub_alias->next;
  944.           xfree (mainsub_alias->name);
  945.           xfree (mainsub_alias);
  946.         }
  947.     }
  948. }

  949. /* Helper function for qsort.  Parameters are `struct block *' pointers,
  950.    function sorts them in descending order by their BLOCK_START.  */

  951. static int
  952. block_compar (const void *ap, const void *bp)
  953. {
  954.   const struct block *a = *(const struct block **) ap;
  955.   const struct block *b = *(const struct block **) bp;

  956.   return ((BLOCK_START (b) > BLOCK_START (a))
  957.           - (BLOCK_START (b) < BLOCK_START (a)));
  958. }

  959. /* Reset state after a successful building of a symtab.
  960.    This exists because dbxread.c and xcoffread.c can call
  961.    start_symtab+end_symtab multiple times after one call to buildsym_init,
  962.    and before the really_free_pendings cleanup is called.
  963.    We keep the free_pendings list around for dbx/xcoff sake.  */

  964. static void
  965. reset_symtab_globals (void)
  966. {
  967.   set_last_source_file (NULL);

  968.   local_symbols = NULL;
  969.   file_symbols = NULL;
  970.   global_symbols = NULL;

  971.   /* We don't free pending_macros here because if the symtab was successfully
  972.      built then ownership was transferred to the symtab.  */
  973.   pending_macros = NULL;

  974.   if (pending_addrmap)
  975.     obstack_free (&pending_addrmap_obstack, NULL);
  976.   pending_addrmap = NULL;

  977.   free_buildsym_compunit ();
  978. }

  979. /* Implementation of the first part of end_symtab.  It allows modifying
  980.    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
  981.    If the returned value is NULL there is no blockvector created for
  982.    this symtab (you still must call end_symtab_from_static_block).

  983.    END_ADDR is the same as for end_symtab: the address of the end of the
  984.    file's text.

  985.    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
  986.    expandable.

  987.    If REQUIRED is non-zero, then a symtab is created even if it does
  988.    not contain any symbols.  */

  989. struct block *
  990. end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
  991. {
  992.   struct objfile *objfile = buildsym_compunit->objfile;

  993.   /* Finish the lexical context of the last function in the file; pop
  994.      the context stack.  */

  995.   if (context_stack_depth > 0)
  996.     {
  997.       struct context_stack *cstk = pop_context ();

  998.       /* Make a block for the local symbols within.  */
  999.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  1000.                     cstk->start_addr, end_addr);

  1001.       if (context_stack_depth > 0)
  1002.         {
  1003.           /* This is said to happen with SCO.  The old coffread.c
  1004.              code simply emptied the context stack, so we do the
  1005.              same.  FIXME: Find out why it is happening.  This is not
  1006.              believed to happen in most cases (even for coffread.c);
  1007.              it used to be an abort().  */
  1008.           complaint (&symfile_complaints,
  1009.                      _("Context stack not empty in end_symtab"));
  1010.           context_stack_depth = 0;
  1011.         }
  1012.     }

  1013.   /* Reordered executables may have out of order pending blocks; if
  1014.      OBJF_REORDERED is true, then sort the pending blocks.  */

  1015.   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
  1016.     {
  1017.       unsigned count = 0;
  1018.       struct pending_block *pb;
  1019.       struct block **barray, **bp;
  1020.       struct cleanup *back_to;

  1021.       for (pb = pending_blocks; pb != NULL; pb = pb->next)
  1022.         count++;

  1023.       barray = xmalloc (sizeof (*barray) * count);
  1024.       back_to = make_cleanup (xfree, barray);

  1025.       bp = barray;
  1026.       for (pb = pending_blocks; pb != NULL; pb = pb->next)
  1027.         *bp++ = pb->block;

  1028.       qsort (barray, count, sizeof (*barray), block_compar);

  1029.       bp = barray;
  1030.       for (pb = pending_blocks; pb != NULL; pb = pb->next)
  1031.         pb->block = *bp++;

  1032.       do_cleanups (back_to);
  1033.     }

  1034.   /* Cleanup any undefined types that have been left hanging around
  1035.      (this needs to be done before the finish_blocks so that
  1036.      file_symbols is still good).

  1037.      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
  1038.      specific, but harmless for other symbol readers, since on gdb
  1039.      startup or when finished reading stabs, the state is set so these
  1040.      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
  1041.      we make this cleaner?  */

  1042.   cleanup_undefined_stabs_types (objfile);
  1043.   finish_global_stabs (objfile);

  1044.   if (!required
  1045.       && pending_blocks == NULL
  1046.       && file_symbols == NULL
  1047.       && global_symbols == NULL
  1048.       && have_line_numbers == 0
  1049.       && pending_macros == NULL)
  1050.     {
  1051.       /* Ignore symtabs that have no functions with real debugging info.  */
  1052.       return NULL;
  1053.     }
  1054.   else
  1055.     {
  1056.       /* Define the STATIC_BLOCK.  */
  1057.       return finish_block_internal (NULL, &file_symbols, NULL,
  1058.                                     last_source_start_addr, end_addr,
  1059.                                     0, expandable);
  1060.     }
  1061. }

  1062. /* Subroutine of end_symtab_from_static_block to simplify it.
  1063.    Handle the "have blockvector" case.
  1064.    See end_symtab_from_static_block for a description of the arguments.  */

  1065. static struct compunit_symtab *
  1066. end_symtab_with_blockvector (struct block *static_block,
  1067.                              int section, int expandable)
  1068. {
  1069.   struct objfile *objfile = buildsym_compunit->objfile;
  1070.   struct compunit_symtab *cu = buildsym_compunit->compunit_symtab;
  1071.   struct symtab *symtab;
  1072.   struct blockvector *blockvector;
  1073.   struct subfile *subfile;
  1074.   CORE_ADDR end_addr;

  1075.   gdb_assert (static_block != NULL);
  1076.   gdb_assert (buildsym_compunit != NULL);
  1077.   gdb_assert (buildsym_compunit->subfiles != NULL);

  1078.   end_addr = BLOCK_END (static_block);

  1079.   /* Create the GLOBAL_BLOCK and build the blockvector.  */
  1080.   finish_block_internal (NULL, &global_symbols, NULL,
  1081.                          last_source_start_addr, end_addr,
  1082.                          1, expandable);
  1083.   blockvector = make_blockvector ();

  1084.   /* Read the line table if it has to be read separately.
  1085.      This is only used by xcoffread.c.  */
  1086.   if (objfile->sf->sym_read_linetable != NULL)
  1087.     objfile->sf->sym_read_linetable (objfile);

  1088.   /* Handle the case where the debug info specifies a different path
  1089.      for the main source file.  It can cause us to lose track of its
  1090.      line number information.  */
  1091.   watch_main_source_file_lossage ();

  1092.   /* Now create the symtab objects proper, if not already done,
  1093.      one for each subfile.  */

  1094.   for (subfile = buildsym_compunit->subfiles;
  1095.        subfile != NULL;
  1096.        subfile = subfile->next)
  1097.     {
  1098.       int linetablesize = 0;

  1099.       if (subfile->line_vector)
  1100.         {
  1101.           linetablesize = sizeof (struct linetable) +
  1102.             subfile->line_vector->nitems * sizeof (struct linetable_entry);

  1103.           /* Like the pending blocks, the line table may be
  1104.              scrambled in reordered executables.  Sort it if
  1105.              OBJF_REORDERED is true.  */
  1106.           if (objfile->flags & OBJF_REORDERED)
  1107.             qsort (subfile->line_vector->item,
  1108.                    subfile->line_vector->nitems,
  1109.                    sizeof (struct linetable_entry), compare_line_numbers);
  1110.         }

  1111.       /* Allocate a symbol table if necessary.  */
  1112.       if (subfile->symtab == NULL)
  1113.         subfile->symtab = allocate_symtab (cu, subfile->name);
  1114.       symtab = subfile->symtab;

  1115.       /* Fill in its components.  */

  1116.       if (subfile->line_vector)
  1117.         {
  1118.           /* Reallocate the line table on the symbol obstack.  */
  1119.           SYMTAB_LINETABLE (symtab) = (struct linetable *)
  1120.             obstack_alloc (&objfile->objfile_obstack, linetablesize);
  1121.           memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
  1122.                   linetablesize);
  1123.         }
  1124.       else
  1125.         {
  1126.           SYMTAB_LINETABLE (symtab) = NULL;
  1127.         }

  1128.       /* Use whatever language we have been using for this
  1129.          subfile, not the one that was deduced in allocate_symtab
  1130.          from the filename.  We already did our own deducing when
  1131.          we created the subfile, and we may have altered our
  1132.          opinion of what language it is from things we found in
  1133.          the symbols.  */
  1134.       symtab->language = subfile->language;
  1135.     }

  1136.   /* Make sure the symtab of main_subfile is the first in its list.  */
  1137.   {
  1138.     struct symtab *main_symtab, *prev_symtab;

  1139.     main_symtab = buildsym_compunit->main_subfile->symtab;
  1140.     prev_symtab = NULL;
  1141.     ALL_COMPUNIT_FILETABS (cu, symtab)
  1142.       {
  1143.         if (symtab == main_symtab)
  1144.           {
  1145.             if (prev_symtab != NULL)
  1146.               {
  1147.                 prev_symtab->next = main_symtab->next;
  1148.                 main_symtab->next = COMPUNIT_FILETABS (cu);
  1149.                 COMPUNIT_FILETABS (cu) = main_symtab;
  1150.               }
  1151.             break;
  1152.           }
  1153.         prev_symtab = symtab;
  1154.       }
  1155.     gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
  1156.   }

  1157.   /* Fill out the compunit symtab.  */

  1158.   if (buildsym_compunit->comp_dir != NULL)
  1159.     {
  1160.       /* Reallocate the dirname on the symbol obstack.  */
  1161.       COMPUNIT_DIRNAME (cu)
  1162.         = obstack_copy0 (&objfile->objfile_obstack,
  1163.                          buildsym_compunit->comp_dir,
  1164.                          strlen (buildsym_compunit->comp_dir));
  1165.     }

  1166.   /* Save the debug format string (if any) in the symtab.  */
  1167.   COMPUNIT_DEBUGFORMAT (cu) = buildsym_compunit->debugformat;

  1168.   /* Similarly for the producer.  */
  1169.   COMPUNIT_PRODUCER (cu) = buildsym_compunit->producer;

  1170.   COMPUNIT_BLOCKVECTOR (cu) = blockvector;
  1171.   {
  1172.     struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);

  1173.     set_block_compunit_symtab (b, cu);
  1174.   }

  1175.   COMPUNIT_BLOCK_LINE_SECTION (cu) = section;

  1176.   COMPUNIT_MACRO_TABLE (cu) = pending_macros;

  1177.   /* Default any symbols without a specified symtab to the primary symtab.  */
  1178.   {
  1179.     int block_i;

  1180.     /* The main source file's symtab.  */
  1181.     symtab = COMPUNIT_FILETABS (cu);

  1182.     for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
  1183.       {
  1184.         struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
  1185.         struct symbol *sym;
  1186.         struct dict_iterator iter;

  1187.         /* Inlined functions may have symbols not in the global or
  1188.            static symbol lists.  */
  1189.         if (BLOCK_FUNCTION (block) != NULL)
  1190.           if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
  1191.             symbol_set_symtab (BLOCK_FUNCTION (block), symtab);

  1192.         /* Note that we only want to fix up symbols from the local
  1193.            blocks, not blocks coming from included symtabs.  That is why
  1194.            we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
  1195.         ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
  1196.           if (symbol_symtab (sym) == NULL)
  1197.             symbol_set_symtab (sym, symtab);
  1198.       }
  1199.   }

  1200.   add_compunit_symtab_to_objfile (cu);

  1201.   return cu;
  1202. }

  1203. /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
  1204.    as value returned by end_symtab_get_static_block.

  1205.    SECTION is the same as for end_symtab: the section number
  1206.    (in objfile->section_offsets) of the blockvector and linetable.

  1207.    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
  1208.    expandable.  */

  1209. struct compunit_symtab *
  1210. end_symtab_from_static_block (struct block *static_block,
  1211.                               int section, int expandable)
  1212. {
  1213.   struct compunit_symtab *cu;

  1214.   if (static_block == NULL)
  1215.     {
  1216.       /* Handle the "no blockvector" case.
  1217.          When this happens there is nothing to record, so there's nothing
  1218.          to do: memory will be freed up later.

  1219.          Note: We won't be adding a compunit to the objfile's list of
  1220.          compunits, so there's nothing to unchain.  However, since each symtab
  1221.          is added to the objfile's obstack we can't free that space.
  1222.          We could do better, but this is believed to be a sufficiently rare
  1223.          event.  */
  1224.       cu = NULL;
  1225.     }
  1226.   else
  1227.     cu = end_symtab_with_blockvector (static_block, section, expandable);

  1228.   reset_symtab_globals ();

  1229.   return cu;
  1230. }

  1231. /* Finish the symbol definitions for one main source file, close off
  1232.    all the lexical contexts for that file (creating struct block's for
  1233.    them), then make the struct symtab for that file and put it in the
  1234.    list of all such.

  1235.    END_ADDR is the address of the end of the file's text.  SECTION is
  1236.    the section number (in objfile->section_offsets) of the blockvector
  1237.    and linetable.

  1238.    Note that it is possible for end_symtab() to return NULL.  In
  1239.    particular, for the DWARF case at least, it will return NULL when
  1240.    it finds a compilation unit that has exactly one DIE, a
  1241.    TAG_compile_unit DIE.  This can happen when we link in an object
  1242.    file that was compiled from an empty source file.  Returning NULL
  1243.    is probably not the correct thing to do, because then gdb will
  1244.    never know about this empty file (FIXME).

  1245.    If you need to modify STATIC_BLOCK before it is finalized you should
  1246.    call end_symtab_get_static_block and end_symtab_from_static_block
  1247.    yourself.  */

  1248. struct compunit_symtab *
  1249. end_symtab (CORE_ADDR end_addr, int section)
  1250. {
  1251.   struct block *static_block;

  1252.   static_block = end_symtab_get_static_block (end_addr, 0, 0);
  1253.   return end_symtab_from_static_block (static_block, section, 0);
  1254. }

  1255. /* Same as end_symtab except create a symtab that can be later added to.  */

  1256. struct compunit_symtab *
  1257. end_expandable_symtab (CORE_ADDR end_addr, int section)
  1258. {
  1259.   struct block *static_block;

  1260.   static_block = end_symtab_get_static_block (end_addr, 1, 0);
  1261.   return end_symtab_from_static_block (static_block, section, 1);
  1262. }

  1263. /* Subroutine of augment_type_symtab to simplify it.
  1264.    Attach the main source file's symtab to all symbols in PENDING_LIST that
  1265.    don't have one.  */

  1266. static void
  1267. set_missing_symtab (struct pending *pending_list,
  1268.                     struct compunit_symtab *cu)
  1269. {
  1270.   struct pending *pending;
  1271.   int i;

  1272.   for (pending = pending_list; pending != NULL; pending = pending->next)
  1273.     {
  1274.       for (i = 0; i < pending->nsyms; ++i)
  1275.         {
  1276.           if (symbol_symtab (pending->symbol[i]) == NULL)
  1277.             symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
  1278.         }
  1279.     }
  1280. }

  1281. /* Same as end_symtab, but for the case where we're adding more symbols
  1282.    to an existing symtab that is known to contain only type information.
  1283.    This is the case for DWARF4 Type Units.  */

  1284. void
  1285. augment_type_symtab (void)
  1286. {
  1287.   struct compunit_symtab *cust = buildsym_compunit->compunit_symtab;
  1288.   const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);

  1289.   if (context_stack_depth > 0)
  1290.     {
  1291.       complaint (&symfile_complaints,
  1292.                  _("Context stack not empty in augment_type_symtab"));
  1293.       context_stack_depth = 0;
  1294.     }
  1295.   if (pending_blocks != NULL)
  1296.     complaint (&symfile_complaints, _("Blocks in a type symtab"));
  1297.   if (pending_macros != NULL)
  1298.     complaint (&symfile_complaints, _("Macro in a type symtab"));
  1299.   if (have_line_numbers)
  1300.     complaint (&symfile_complaints,
  1301.                _("Line numbers recorded in a type symtab"));

  1302.   if (file_symbols != NULL)
  1303.     {
  1304.       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);

  1305.       /* First mark any symbols without a specified symtab as belonging
  1306.          to the primary symtab.  */
  1307.       set_missing_symtab (file_symbols, cust);

  1308.       dict_add_pending (BLOCK_DICT (block), file_symbols);
  1309.     }

  1310.   if (global_symbols != NULL)
  1311.     {
  1312.       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);

  1313.       /* First mark any symbols without a specified symtab as belonging
  1314.          to the primary symtab.  */
  1315.       set_missing_symtab (global_symbols, cust);

  1316.       dict_add_pending (BLOCK_DICT (block), global_symbols);
  1317.     }

  1318.   reset_symtab_globals ();
  1319. }

  1320. /* Push a context block.  Args are an identifying nesting level
  1321.    (checkable when you pop it), and the starting PC address of this
  1322.    context.  */

  1323. struct context_stack *
  1324. push_context (int desc, CORE_ADDR valu)
  1325. {
  1326.   struct context_stack *new;

  1327.   if (context_stack_depth == context_stack_size)
  1328.     {
  1329.       context_stack_size *= 2;
  1330.       context_stack = (struct context_stack *)
  1331.         xrealloc ((char *) context_stack,
  1332.                   (context_stack_size * sizeof (struct context_stack)));
  1333.     }

  1334.   new = &context_stack[context_stack_depth++];
  1335.   new->depth = desc;
  1336.   new->locals = local_symbols;
  1337.   new->old_blocks = pending_blocks;
  1338.   new->start_addr = valu;
  1339.   new->using_directives = using_directives;
  1340.   new->name = NULL;

  1341.   local_symbols = NULL;
  1342.   using_directives = NULL;

  1343.   return new;
  1344. }

  1345. /* Pop a context block.  Returns the address of the context block just
  1346.    popped.  */

  1347. struct context_stack *
  1348. pop_context (void)
  1349. {
  1350.   gdb_assert (context_stack_depth > 0);
  1351.   return (&context_stack[--context_stack_depth]);
  1352. }



  1353. /* Compute a small integer hash code for the given name.  */

  1354. int
  1355. hashname (const char *name)
  1356. {
  1357.     return (hash(name,strlen(name)) % HASHSIZE);
  1358. }


  1359. void
  1360. record_debugformat (const char *format)
  1361. {
  1362.   buildsym_compunit->debugformat = format;
  1363. }

  1364. void
  1365. record_producer (const char *producer)
  1366. {
  1367.   buildsym_compunit->producer = producer;
  1368. }

  1369. /* Merge the first symbol list SRCLIST into the second symbol list
  1370.    TARGETLIST by repeated calls to add_symbol_to_list().  This
  1371.    procedure "frees" each link of SRCLIST by adding it to the
  1372.    free_pendings list.  Caller must set SRCLIST to a null list after
  1373.    calling this function.

  1374.    Void return.  */

  1375. void
  1376. merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
  1377. {
  1378.   int i;

  1379.   if (!srclist || !*srclist)
  1380.     return;

  1381.   /* Merge in elements from current link.  */
  1382.   for (i = 0; i < (*srclist)->nsyms; i++)
  1383.     add_symbol_to_list ((*srclist)->symbol[i], targetlist);

  1384.   /* Recurse on next.  */
  1385.   merge_symbol_lists (&(*srclist)->next, targetlist);

  1386.   /* "Free" the current link.  */
  1387.   (*srclist)->next = free_pendings;
  1388.   free_pendings = (*srclist);
  1389. }


  1390. /* Name of source file whose symbol data we are now processing.  This
  1391.    comes from a symbol of type N_SO for stabs.  For Dwarf it comes
  1392.    from the DW_AT_name attribute of a DW_TAG_compile_unit DIE.  */

  1393. static char *last_source_file;

  1394. /* See buildsym.h.  */

  1395. void
  1396. set_last_source_file (const char *name)
  1397. {
  1398.   xfree (last_source_file);
  1399.   last_source_file = name == NULL ? NULL : xstrdup (name);
  1400. }

  1401. /* See buildsym.h.  */

  1402. const char *
  1403. get_last_source_file (void)
  1404. {
  1405.   return last_source_file;
  1406. }



  1407. /* Initialize anything that needs initializing when starting to read a
  1408.    fresh piece of a symbol file, e.g. reading in the stuff
  1409.    corresponding to a psymtab.  */

  1410. void
  1411. buildsym_init (void)
  1412. {
  1413.   using_directives = NULL;
  1414.   subfile_stack = NULL;

  1415.   pending_addrmap_interesting = 0;

  1416.   /* Context stack is initially empty.  Allocate first one with room
  1417.      for a few levels; reuse it forever afterward.  */
  1418.   if (context_stack == NULL)
  1419.     {
  1420.       context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
  1421.       context_stack = (struct context_stack *)
  1422.         xmalloc (context_stack_size * sizeof (struct context_stack));
  1423.     }

  1424.   /* Ensure the really_free_pendings cleanup was called after
  1425.      the last time.  */
  1426.   gdb_assert (free_pendings == NULL);
  1427.   gdb_assert (pending_blocks == NULL);
  1428.   gdb_assert (file_symbols == NULL);
  1429.   gdb_assert (global_symbols == NULL);
  1430.   gdb_assert (pending_macros == NULL);
  1431.   gdb_assert (pending_addrmap == NULL);
  1432.   gdb_assert (buildsym_compunit == NULL);
  1433. }

  1434. /* Initialize anything that needs initializing when a completely new
  1435.    symbol file is specified (not just adding some symbols from another
  1436.    file, e.g. a shared library).  */

  1437. void
  1438. buildsym_new_init (void)
  1439. {
  1440.   buildsym_init ();
  1441. }