home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / buildsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-26  |  27.1 KB  |  965 lines

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