home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / symfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-19  |  49.2 KB  |  1,703 lines

  1. /* Generic symbol file reading for the GNU debugger, GDB.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support, using pieces from other GDB modules.
  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. #include "defs.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "gdbcore.h"
  25. #include "frame.h"
  26. #include "target.h"
  27. #include "value.h"
  28. #include "symfile.h"
  29. #include "objfiles.h"
  30. #include "gdbcmd.h"
  31. #include "breakpoint.h"
  32. #include "language.h"
  33. #include "complaints.h"
  34. #include "demangle.h"
  35. #include "inferior.h" /* for write_pc */
  36.  
  37. #include <obstack.h>
  38. #include <assert.h>
  39.  
  40. #include <sys/types.h>
  41. #include <fcntl.h>
  42. #include <string.h>
  43. #include <sys/stat.h>
  44. #include <ctype.h>
  45.  
  46. #ifndef O_BINARY
  47. #define O_BINARY 0
  48. #endif
  49.  
  50. /* Global variables owned by this file */
  51. int readnow_symbol_files;        /* Read full symbols immediately */
  52.  
  53. struct complaint oldsyms_complaint = {
  54.   "Replacing old symbols for `%s'", 0, 0
  55. };
  56.  
  57. struct complaint empty_symtab_complaint = {
  58.   "Empty symbol table found for `%s'", 0, 0
  59. };
  60.  
  61. /* External variables and functions referenced. */
  62.  
  63. extern int info_verbose;
  64.  
  65. /* Functions this file defines */
  66.  
  67. static void
  68. set_initial_language PARAMS ((void));
  69.  
  70. static void
  71. load_command PARAMS ((char *, int));
  72.  
  73. static void
  74. add_symbol_file_command PARAMS ((char *, int));
  75.  
  76. static void
  77. add_shared_symbol_files_command PARAMS ((char *, int));
  78.  
  79. static void
  80. cashier_psymtab PARAMS ((struct partial_symtab *));
  81.  
  82. static int
  83. compare_psymbols PARAMS ((const void *, const void *));
  84.  
  85. static int
  86. compare_symbols PARAMS ((const void *, const void *));
  87.  
  88. static bfd *
  89. symfile_bfd_open PARAMS ((char *));
  90.  
  91. static void
  92. find_sym_fns PARAMS ((struct objfile *));
  93.  
  94. /* List of all available sym_fns.  On gdb startup, each object file reader
  95.    calls add_symtab_fns() to register information on each format it is
  96.    prepared to read. */
  97.  
  98. static struct sym_fns *symtab_fns = NULL;
  99.  
  100. /* Structures with which to manage partial symbol allocation.  */
  101.  
  102. struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
  103.  
  104. /* Flag for whether user will be reloading symbols multiple times.
  105.    Defaults to ON for VxWorks, otherwise OFF.  */
  106.  
  107. #ifdef SYMBOL_RELOADING_DEFAULT
  108. int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
  109. #else
  110. int symbol_reloading = 0;
  111. #endif
  112.  
  113.  
  114. /* Since this function is called from within qsort, in an ANSI environment
  115.    it must conform to the prototype for qsort, which specifies that the
  116.    comparison function takes two "void *" pointers. */
  117.  
  118. static int
  119. compare_symbols (s1p, s2p)
  120.      const PTR s1p;
  121.      const PTR s2p;
  122. {
  123.   register struct symbol **s1, **s2;
  124.  
  125.   s1 = (struct symbol **) s1p;
  126.   s2 = (struct symbol **) s2p;
  127.  
  128.   return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
  129. }
  130.  
  131. /*
  132.  
  133. LOCAL FUNCTION
  134.  
  135.     compare_psymbols -- compare two partial symbols by name
  136.  
  137. DESCRIPTION
  138.  
  139.     Given pointer to two partial symbol table entries, compare
  140.     them by name and return -N, 0, or +N (ala strcmp).  Typically
  141.     used by sorting routines like qsort().
  142.  
  143. NOTES
  144.  
  145.     Does direct compare of first two characters before punting
  146.     and passing to strcmp for longer compares.  Note that the
  147.     original version had a bug whereby two null strings or two
  148.     identically named one character strings would return the
  149.     comparison of memory following the null byte.
  150.  
  151.  */
  152.  
  153. static int
  154. compare_psymbols (s1p, s2p)
  155.      const PTR s1p;
  156.      const PTR s2p;
  157. {
  158.   register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
  159.   register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
  160.  
  161.   if ((st1[0] - st2[0]) || !st1[0])
  162.     {
  163.       return (st1[0] - st2[0]);
  164.     }
  165.   else if ((st1[1] - st2[1]) || !st1[1])
  166.     {
  167.       return (st1[1] - st2[1]);
  168.     }
  169.   else
  170.     {
  171.       return (STRCMP (st1 + 2, st2 + 2));
  172.     }
  173. }
  174.  
  175. void
  176. sort_pst_symbols (pst)
  177.      struct partial_symtab *pst;
  178. {
  179.   /* Sort the global list; don't sort the static list */
  180.  
  181.   qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
  182.      pst -> n_global_syms, sizeof (struct partial_symbol),
  183.      compare_psymbols);
  184. }
  185.  
  186. /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
  187.  
  188. void
  189. sort_block_syms (b)
  190.      register struct block *b;
  191. {
  192.   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  193.      sizeof (struct symbol *), compare_symbols);
  194. }
  195.  
  196. /* Call sort_symtab_syms to sort alphabetically
  197.    the symbols of each block of one symtab.  */
  198.  
  199. void
  200. sort_symtab_syms (s)
  201.      register struct symtab *s;
  202. {
  203.   register struct blockvector *bv;
  204.   int nbl;
  205.   int i;
  206.   register struct block *b;
  207.  
  208.   if (s == 0)
  209.     return;
  210.   bv = BLOCKVECTOR (s);
  211.   nbl = BLOCKVECTOR_NBLOCKS (bv);
  212.   for (i = 0; i < nbl; i++)
  213.     {
  214.       b = BLOCKVECTOR_BLOCK (bv, i);
  215.       if (BLOCK_SHOULD_SORT (b))
  216.     sort_block_syms (b);
  217.     }
  218. }
  219.  
  220. /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
  221.    (and add a null character at the end in the copy).
  222.    Returns the address of the copy.  */
  223.  
  224. char *
  225. obsavestring (ptr, size, obstackp)
  226.      char *ptr;
  227.      int size;
  228.      struct obstack *obstackp;
  229. {
  230.   register char *p = (char *) obstack_alloc (obstackp, size + 1);
  231.   /* Open-coded memcpy--saves function call time.
  232.      These strings are usually short.  */
  233.   {
  234.     register char *p1 = ptr;
  235.     register char *p2 = p;
  236.     char *end = ptr + size;
  237.     while (p1 != end)
  238.       *p2++ = *p1++;
  239.   }
  240.   p[size] = 0;
  241.   return p;
  242. }
  243.  
  244. /* Concatenate strings S1, S2 and S3; return the new string.
  245.    Space is found in the symbol_obstack.  */
  246.  
  247. char *
  248. obconcat (obstackp, s1, s2, s3)
  249.      struct obstack *obstackp;
  250.      const char *s1, *s2, *s3;
  251. {
  252.   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  253.   register char *val = (char *) obstack_alloc (obstackp, len);
  254.   strcpy (val, s1);
  255.   strcat (val, s2);
  256.   strcat (val, s3);
  257.   return val;
  258. }
  259.  
  260. /* Get the symbol table that corresponds to a partial_symtab.
  261.    This is fast after the first time you do it.  In fact, there
  262.    is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
  263.    case inline.  */
  264.  
  265. struct symtab *
  266. psymtab_to_symtab (pst)
  267.      register struct partial_symtab *pst;
  268. {
  269.   /* If it's been looked up before, return it. */
  270.   if (pst->symtab)
  271.     return pst->symtab;
  272.  
  273.   /* If it has not yet been read in, read it.  */
  274.   if (!pst->readin)
  275.     { 
  276.       (*pst->read_symtab) (pst);
  277.     }
  278.  
  279.   return pst->symtab;
  280. }
  281.  
  282. /* Initialize entry point information for this objfile. */
  283.  
  284. void
  285. init_entry_point_info (objfile)
  286.      struct objfile *objfile;
  287. {
  288.   /* Save startup file's range of PC addresses to help blockframe.c
  289.      decide where the bottom of the stack is.  */
  290.  
  291.   if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
  292.     {
  293.       /* Executable file -- record its entry point so we'll recognize
  294.      the startup file because it contains the entry point.  */
  295.       objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
  296.     }
  297.   else
  298.     {
  299.       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
  300.       objfile -> ei.entry_point = INVALID_ENTRY_POINT;
  301.       objfile -> ei.entry_file_lowpc = INVALID_ENTRY_LOWPC;
  302.       objfile -> ei.entry_file_highpc = INVALID_ENTRY_HIGHPC;
  303.     }
  304. }
  305.  
  306. /* Get current entry point address.  */
  307.  
  308. CORE_ADDR
  309. entry_point_address()
  310. {
  311.   return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
  312. }
  313.  
  314. /* Remember the lowest-addressed loadable section we've seen.  
  315.    This function is called via bfd_map_over_sections. 
  316.  
  317.    In case of equal vmas, the section with the largest size becomes the
  318.    lowest-addressed loadable section.
  319.  
  320.    If the vmas and sizes are equal, the last section is considered the
  321.    lowest-addressed loadable section.  */
  322.  
  323. static void
  324. find_lowest_section (abfd, sect, obj)
  325.      bfd *abfd;
  326.      asection *sect;
  327.      PTR obj;
  328. {
  329.   asection **lowest = (asection **)obj;
  330.  
  331.   if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
  332.     return;
  333.   if (!*lowest)
  334.     *lowest = sect;        /* First loadable section */
  335.   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
  336.     *lowest = sect;        /* A lower loadable section */
  337.   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
  338.        && (bfd_section_size (abfd, (*lowest))
  339.            <= bfd_section_size (abfd, sect)))
  340.     *lowest = sect;
  341. }
  342.  
  343. /* Process a symbol file, as either the main file or as a dynamically
  344.    loaded file.
  345.  
  346.    NAME is the file name (which will be tilde-expanded and made
  347.    absolute herein) (but we don't free or modify NAME itself).
  348.    FROM_TTY says how verbose to be.  MAINLINE specifies whether this
  349.    is the main symbol file, or whether it's an extra symbol file such
  350.    as dynamically loaded code.  If !mainline, ADDR is the address
  351.    where the text segment was loaded.  If VERBO, the caller has printed
  352.    a verbose message about the symbol reading (and complaints can be
  353.    more terse about it).  */
  354.  
  355. void
  356. syms_from_objfile (objfile, addr, mainline, verbo)
  357.      struct objfile *objfile;
  358.      CORE_ADDR addr;
  359.      int mainline;
  360.      int verbo;
  361. {
  362.   struct section_offsets *section_offsets;
  363.   asection *lowest_sect;
  364.   struct cleanup *old_chain;
  365.  
  366.   init_entry_point_info (objfile);
  367.   find_sym_fns (objfile);
  368.  
  369.   /* Make sure that partially constructed symbol tables will be cleaned up
  370.      if an error occurs during symbol reading.  */
  371.   old_chain = make_cleanup (free_objfile, objfile);
  372.  
  373.   if (mainline) 
  374.     {
  375.       /* We will modify the main symbol table, make sure that all its users
  376.      will be cleaned up if an error occurs during symbol reading.  */
  377.       make_cleanup (clear_symtab_users, 0);
  378.  
  379.       /* Since no error yet, throw away the old symbol table.  */
  380.  
  381.       if (symfile_objfile != NULL)
  382.     {
  383.       free_objfile (symfile_objfile);
  384.       symfile_objfile = NULL;
  385.     }
  386.  
  387.       /* Currently we keep symbols from the add-symbol-file command.
  388.      If the user wants to get rid of them, they should do "symbol-file"
  389.      without arguments first.  Not sure this is the best behavior
  390.      (PR 2207).  */
  391.  
  392.       (*objfile -> sf -> sym_new_init) (objfile);
  393.     }
  394.  
  395.   /* Convert addr into an offset rather than an absolute address.
  396.      We find the lowest address of a loaded segment in the objfile,
  397.      and assume that <addr> is where that got loaded.  Due to historical
  398.      precedent, we warn if that doesn't happen to be a text segment.  */
  399.  
  400.   if (mainline)
  401.     {
  402.       addr = 0;        /* No offset from objfile addresses.  */
  403.     }
  404.   else
  405.     {
  406.       lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
  407.       if (lowest_sect == NULL)
  408.     bfd_map_over_sections (objfile->obfd, find_lowest_section,
  409.                    (PTR) &lowest_sect);
  410.  
  411.       if (lowest_sect == NULL)
  412.     warning ("no loadable sections found in added symbol-file %s",
  413.          objfile->name);
  414.       else if ((bfd_get_section_flags (objfile->obfd, lowest_sect) & SEC_CODE)
  415.            == 0)
  416.     /* FIXME-32x64--assumes bfd_vma fits in long.  */
  417.     warning ("Lowest section in %s is %s at 0x%lx",
  418.          objfile->name,
  419.          bfd_section_name (objfile->obfd, lowest_sect),
  420.          (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
  421.  
  422.       if (lowest_sect)
  423.     addr -= bfd_section_vma (objfile->obfd, lowest_sect);
  424.     }
  425.  
  426.   /* Initialize symbol reading routines for this objfile, allow complaints to
  427.      appear for this new file, and record how verbose to be, then do the
  428.      initial symbol reading for this file. */
  429.  
  430.   (*objfile -> sf -> sym_init) (objfile);
  431.   clear_complaints (1, verbo);
  432.  
  433.   section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
  434.   objfile->section_offsets = section_offsets;
  435.  
  436. #ifndef IBM6000_TARGET
  437.   /* This is a SVR4/SunOS specific hack, I think.  In any event, it
  438.      screws RS/6000.  sym_offsets should be doing this sort of thing,
  439.      because it knows the mapping between bfd sections and
  440.      section_offsets.  */
  441.   /* This is a hack.  As far as I can tell, section offsets are not
  442.      target dependent.  They are all set to addr with a couple of
  443.      exceptions.  The exceptions are sysvr4 shared libraries, whose
  444.      offsets are kept in solib structures anyway and rs6000 xcoff
  445.      which handles shared libraries in a completely unique way.
  446.  
  447.      Section offsets are built similarly, except that they are built
  448.      by adding addr in all cases because there is no clear mapping
  449.      from section_offsets into actual sections.  Note that solib.c
  450.      has a different algorythm for finding section offsets.
  451.  
  452.      These should probably all be collapsed into some target
  453.      independent form of shared library support.  FIXME.  */
  454.  
  455.   if (addr)
  456.     {
  457.       struct obj_section *s;
  458.  
  459.       for (s = objfile->sections; s < objfile->sections_end; ++s)
  460.     {
  461.       s->addr -= s->offset;
  462.       s->addr += addr;
  463.       s->endaddr -= s->offset;
  464.       s->endaddr += addr;
  465.       s->offset += addr;
  466.     }
  467.     }
  468. #endif /* not IBM6000_TARGET */
  469.  
  470.   (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
  471.  
  472.   if (!have_partial_symbols () && !have_full_symbols ())
  473.     {
  474.       wrap_here ("");
  475.       printf_filtered ("(no debugging symbols found)...");
  476.       wrap_here ("");
  477.     }
  478.  
  479.   /* Don't allow char * to have a typename (else would get caddr_t).
  480.      Ditto void *.  FIXME: Check whether this is now done by all the
  481.      symbol readers themselves (many of them now do), and if so remove
  482.      it from here.  */
  483.  
  484.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  485.   TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
  486.  
  487.   /* Mark the objfile has having had initial symbol read attempted.  Note
  488.      that this does not mean we found any symbols... */
  489.  
  490.   objfile -> flags |= OBJF_SYMS;
  491.  
  492.   /* Discard cleanups as symbol reading was successful.  */
  493.  
  494.   discard_cleanups (old_chain);
  495. }
  496.  
  497. /* Perform required actions after either reading in the initial
  498.    symbols for a new objfile, or mapping in the symbols from a reusable
  499.    objfile. */
  500.    
  501. void
  502. new_symfile_objfile (objfile, mainline, verbo)
  503.      struct objfile *objfile;
  504.      int mainline;
  505.      int verbo;
  506. {
  507.  
  508.   /* If this is the main symbol file we have to clean up all users of the
  509.      old main symbol file. Otherwise it is sufficient to fixup all the
  510.      breakpoints that may have been redefined by this symbol file.  */
  511.   if (mainline)
  512.     {
  513.       /* OK, make it the "real" symbol file.  */
  514.       symfile_objfile = objfile;
  515.  
  516.       clear_symtab_users ();
  517.     }
  518.   else
  519.     {
  520.       breakpoint_re_set ();
  521.     }
  522.  
  523.   /* We're done reading the symbol file; finish off complaints.  */
  524.   clear_complaints (0, verbo);
  525. }
  526.  
  527. /* Process a symbol file, as either the main file or as a dynamically
  528.    loaded file.
  529.  
  530.    NAME is the file name (which will be tilde-expanded and made
  531.    absolute herein) (but we don't free or modify NAME itself).
  532.    FROM_TTY says how verbose to be.  MAINLINE specifies whether this
  533.    is the main symbol file, or whether it's an extra symbol file such
  534.    as dynamically loaded code.  If !mainline, ADDR is the address
  535.    where the text segment was loaded.
  536.  
  537.    Upon success, returns a pointer to the objfile that was added.
  538.    Upon failure, jumps back to command level (never returns). */
  539.  
  540. struct objfile *
  541. symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
  542.      char *name;
  543.      int from_tty;
  544.      CORE_ADDR addr;
  545.      int mainline;
  546.      int mapped;
  547.      int readnow;
  548. {
  549.   struct objfile *objfile;
  550.   struct partial_symtab *psymtab;
  551.   bfd *abfd;
  552.  
  553.   /* Open a bfd for the file, and give user a chance to burp if we'd be
  554.      interactively wiping out any existing symbols.  */
  555.  
  556.   abfd = symfile_bfd_open (name);
  557.  
  558.   if ((have_full_symbols () || have_partial_symbols ())
  559.       && mainline
  560.       && from_tty
  561.       && !query ("Load new symbol table from \"%s\"? ", name))
  562.       error ("Not confirmed.");
  563.  
  564.   objfile = allocate_objfile (abfd, mapped);
  565.  
  566.   /* If the objfile uses a mapped symbol file, and we have a psymtab for
  567.      it, then skip reading any symbols at this time. */
  568.  
  569.   if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
  570.     {
  571.       /* We mapped in an existing symbol table file that already has had
  572.      initial symbol reading performed, so we can skip that part.  Notify
  573.      the user that instead of reading the symbols, they have been mapped.
  574.      */
  575.       if (from_tty || info_verbose)
  576.     {
  577.       printf_filtered ("Mapped symbols for %s...", name);
  578.       wrap_here ("");
  579.       gdb_flush (gdb_stdout);
  580.     }
  581.       init_entry_point_info (objfile);
  582.       find_sym_fns (objfile);
  583.     }
  584.   else
  585.     {
  586.       /* We either created a new mapped symbol table, mapped an existing
  587.      symbol table file which has not had initial symbol reading
  588.      performed, or need to read an unmapped symbol table. */
  589.       if (from_tty || info_verbose)
  590.     {
  591.       printf_filtered ("Reading symbols from %s...", name);
  592.       wrap_here ("");
  593.       gdb_flush (gdb_stdout);
  594.     }
  595.       syms_from_objfile (objfile, addr, mainline, from_tty);
  596.     }      
  597.  
  598.   /* We now have at least a partial symbol table.  Check to see if the
  599.      user requested that all symbols be read on initial access via either
  600.      the gdb startup command line or on a per symbol file basis.  Expand
  601.      all partial symbol tables for this objfile if so. */
  602.  
  603.   if (readnow || readnow_symbol_files)
  604.     {
  605.       if (from_tty || info_verbose)
  606.     {
  607.       printf_filtered ("expanding to full symbols...");
  608.       wrap_here ("");
  609.       gdb_flush (gdb_stdout);
  610.     }
  611.  
  612.       for (psymtab = objfile -> psymtabs;
  613.        psymtab != NULL;
  614.        psymtab = psymtab -> next)
  615.     {
  616.       psymtab_to_symtab (psymtab);
  617.     }
  618.     }
  619.  
  620.   if (from_tty || info_verbose)
  621.     {
  622.       printf_filtered ("done.\n");
  623.       gdb_flush (gdb_stdout);
  624.     }
  625.  
  626.   new_symfile_objfile (objfile, mainline, from_tty);
  627.  
  628.   return (objfile);
  629. }
  630.  
  631. /* This is the symbol-file command.  Read the file, analyze its
  632.    symbols, and add a struct symtab to a symtab list.  The syntax of
  633.    the command is rather bizarre--(1) buildargv implements various
  634.    quoting conventions which are undocumented and have little or
  635.    nothing in common with the way things are quoted (or not quoted)
  636.    elsewhere in GDB, (2) options are used, which are not generally
  637.    used in GDB (perhaps "set mapped on", "set readnow on" would be
  638.    better), (3) the order of options matters, which is contrary to GNU
  639.    conventions (because it is confusing and inconvenient).  */
  640.  
  641. void
  642. symbol_file_command (args, from_tty)
  643.      char *args;
  644.      int from_tty;
  645. {
  646.   char **argv;
  647.   char *name = NULL;
  648.   CORE_ADDR text_relocation = 0;        /* text_relocation */
  649.   struct cleanup *cleanups;
  650.   int mapped = 0;
  651.   int readnow = 0;
  652.  
  653.   dont_repeat ();
  654.  
  655.   if (args == NULL)
  656.     {
  657.       if ((have_full_symbols () || have_partial_symbols ())
  658.       && from_tty
  659.       && !query ("Discard symbol table from `%s'? ",
  660.              symfile_objfile -> name))
  661.     error ("Not confirmed.");
  662.       free_all_objfiles ();
  663.       symfile_objfile = NULL;
  664.       if (from_tty)
  665.     {
  666.       printf_unfiltered ("No symbol file now.\n");
  667.     }
  668.     }
  669.   else
  670.     {
  671.       if ((argv = buildargv (args)) == NULL)
  672.     {
  673.       nomem (0);
  674.     }
  675.       cleanups = make_cleanup (freeargv, (char *) argv);
  676.       while (*argv != NULL)
  677.     {
  678.       if (STREQ (*argv, "-mapped"))
  679.         {
  680.           mapped = 1;
  681.         }
  682.       else if (STREQ (*argv, "-readnow"))
  683.         {
  684.           readnow = 1;
  685.         }
  686.       else if (**argv == '-')
  687.         {
  688.           error ("unknown option `%s'", *argv);
  689.         }
  690.       else
  691.         {
  692.             char *p;
  693.  
  694.               name = *argv;
  695.  
  696.               /* this is for rombug remote only, to get the text relocation by
  697.               using link command */
  698.               p = strrchr(name, '/');
  699.               if (p != NULL) p++;
  700.               else p = name;
  701.  
  702.               target_link(p, &text_relocation);
  703.  
  704.               if (text_relocation == (CORE_ADDR)0)
  705.                 return;
  706.               else if (text_relocation == (CORE_ADDR)-1)
  707.                 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
  708.                  readnow);
  709.               else
  710.                 symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
  711.                  0, mapped, readnow);
  712.  
  713.           /* Getting new symbols may change our opinion about what is
  714.          frameless.  */
  715.           reinit_frame_cache ();
  716.  
  717.               set_initial_language ();
  718.         }
  719.       argv++;
  720.     }
  721.  
  722.       if (name == NULL)
  723.     {
  724.       error ("no symbol file name was specified");
  725.     }
  726.       do_cleanups (cleanups);
  727.     }
  728. }
  729.  
  730. /* Set the initial language.
  731.  
  732.    A better solution would be to record the language in the psymtab when reading
  733.    partial symbols, and then use it (if known) to set the language.  This would
  734.    be a win for formats that encode the language in an easily discoverable place,
  735.    such as DWARF.  For stabs, we can jump through hoops looking for specially
  736.    named symbols or try to intuit the language from the specific type of stabs
  737.    we find, but we can't do that until later when we read in full symbols.
  738.    FIXME.  */
  739.  
  740. static void
  741. set_initial_language ()
  742. {
  743.   struct partial_symtab *pst;
  744.   enum language lang = language_unknown;      
  745.  
  746.   pst = find_main_psymtab ();
  747.   if (pst != NULL)
  748.     {
  749.       if (pst -> filename != NULL)
  750.     {
  751.       lang = deduce_language_from_filename (pst -> filename);
  752.         }
  753.       if (lang == language_unknown)
  754.     {
  755.         /* Make C the default language */
  756.         lang = language_c;
  757.     }
  758.       set_language (lang);
  759.       expected_language = current_language;    /* Don't warn the user */
  760.     }
  761. }
  762.  
  763. /* Open file specified by NAME and hand it off to BFD for preliminary
  764.    analysis.  Result is a newly initialized bfd *, which includes a newly
  765.    malloc'd` copy of NAME (tilde-expanded and made absolute).
  766.    In case of trouble, error() is called.  */
  767.  
  768. static bfd *
  769. symfile_bfd_open (name)
  770.      char *name;
  771. {
  772.   bfd *sym_bfd;
  773.   int desc;
  774.   char *absolute_name;
  775.  
  776.   name = tilde_expand (name);    /* Returns 1st new malloc'd copy */
  777.  
  778.   /* Look down path for it, allocate 2nd new malloc'd copy.  */
  779.   desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
  780.   if (desc < 0)
  781.     {
  782.       make_cleanup (free, name);
  783.       perror_with_name (name);
  784.     }
  785.   free (name);            /* Free 1st new malloc'd copy */
  786.   name = absolute_name;        /* Keep 2nd malloc'd copy in bfd */
  787.                 /* It'll be freed in free_objfile(). */
  788.  
  789.   sym_bfd = bfd_fdopenr (name, gnutarget, desc);
  790.   if (!sym_bfd)
  791.     {
  792.       close (desc);
  793.       make_cleanup (free, name);
  794.       error ("\"%s\": can't open to read symbols: %s.", name,
  795.          bfd_errmsg (bfd_get_error ()));
  796.     }
  797.   sym_bfd->cacheable = true;
  798.  
  799.   if (!bfd_check_format (sym_bfd, bfd_object))
  800.     {
  801.       /* FIXME: should be checking for errors from bfd_close (for one thing,
  802.      on error it does not free all the storage associated with the
  803.      bfd).  */
  804.       bfd_close (sym_bfd);    /* This also closes desc */
  805.       make_cleanup (free, name);
  806.       error ("\"%s\": can't read symbols: %s.", name,
  807.          bfd_errmsg (bfd_get_error ()));
  808.     }
  809.  
  810.   return (sym_bfd);
  811. }
  812.  
  813. /* Link a new symtab_fns into the global symtab_fns list.  Called on gdb
  814.    startup by the _initialize routine in each object file format reader,
  815.    to register information about each format the the reader is prepared
  816.    to handle. */
  817.  
  818. void
  819. add_symtab_fns (sf)
  820.      struct sym_fns *sf;
  821. {
  822.   sf->next = symtab_fns;
  823.   symtab_fns = sf;
  824. }
  825.  
  826.  
  827. /* Initialize to read symbols from the symbol file sym_bfd.  It either
  828.    returns or calls error().  The result is an initialized struct sym_fns
  829.    in the objfile structure, that contains cached information about the
  830.    symbol file.  */
  831.  
  832. static void
  833. find_sym_fns (objfile)
  834.      struct objfile *objfile;
  835. {
  836.   struct sym_fns *sf;
  837.   enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
  838.   char *our_target = bfd_get_target (objfile -> obfd);
  839.  
  840.   /* Special kludge for RS/6000.  See xcoffread.c.  */
  841.   if (STREQ (our_target, "aixcoff-rs6000"))
  842.     our_flavour = (enum bfd_flavour)-1;
  843.  
  844.   /* Special kludge for apollo.  See dstread.c.  */
  845.   if (STREQN (our_target, "apollo", 6))
  846.     our_flavour = (enum bfd_flavour)-2;
  847.  
  848.   for (sf = symtab_fns; sf != NULL; sf = sf -> next)
  849.     {
  850.       if (our_flavour == sf -> sym_flavour)
  851.     {
  852.       objfile -> sf = sf;
  853.       return;
  854.     }
  855.     }
  856.   error ("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown.",
  857.      bfd_get_target (objfile -> obfd));
  858. }
  859.  
  860. /* This function runs the load command of our current target.  */
  861.  
  862. static void
  863. load_command (arg, from_tty)
  864.      char *arg;
  865.      int from_tty;
  866. {
  867.   if (arg == NULL)
  868.     arg = get_exec_file (1);
  869.   target_load (arg, from_tty);
  870. }
  871.  
  872. /* This version of "load" should be usable for any target.  Currently
  873.    it is just used for remote targets, not inftarg.c or core files,
  874.    on the theory that only in that case is it useful.
  875.  
  876.    Avoiding xmodem and the like seems like a win (a) because we don't have
  877.    to worry about finding it, and (b) On VMS, fork() is very slow and so
  878.    we don't want to run a subprocess.  On the other hand, I'm not sure how
  879.    performance compares.  */
  880. void
  881. generic_load (filename, from_tty)
  882.     char *filename;
  883.     int from_tty;
  884. {
  885.   struct cleanup *old_cleanups;
  886.   asection *s;
  887.   bfd *loadfile_bfd;
  888.  
  889.   loadfile_bfd = bfd_openr (filename, gnutarget);
  890.   if (loadfile_bfd == NULL)
  891.     {
  892.       perror_with_name (filename);
  893.       return;
  894.     }
  895.   /* FIXME: should be checking for errors from bfd_close (for one thing,
  896.      on error it does not free all the storage associated with the
  897.      bfd).  */
  898.   old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
  899.  
  900.   if (!bfd_check_format (loadfile_bfd, bfd_object)) 
  901.     {
  902.       error ("\"%s\" is not an object file: %s", filename,
  903.          bfd_errmsg (bfd_get_error ()));
  904.     }
  905.   
  906.   for (s = loadfile_bfd->sections; s; s = s->next) 
  907.     {
  908.       if (s->flags & SEC_LOAD) 
  909.     {
  910.       bfd_size_type size;
  911.  
  912.       size = bfd_get_section_size_before_reloc (s);
  913.       if (size > 0)
  914.         {
  915.           char *buffer;
  916.           struct cleanup *old_chain;
  917.           bfd_vma vma;
  918.  
  919.           buffer = xmalloc (size);
  920.           old_chain = make_cleanup (free, buffer);
  921.  
  922.           vma = bfd_get_section_vma (loadfile_bfd, s);
  923.  
  924.           /* Is this really necessary?  I guess it gives the user something
  925.          to look at during a long download.  */
  926.           printf_filtered ("Loading section %s, size 0x%lx vma ",
  927.                    bfd_get_section_name (loadfile_bfd, s),
  928.                    (unsigned long) size);
  929.           print_address_numeric (vma, 1, gdb_stdout);
  930.           printf_filtered ("\n");
  931.  
  932.           bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
  933.  
  934.           target_write_memory (vma, buffer, size);
  935.  
  936.           do_cleanups (old_chain);
  937.         }
  938.     }
  939.     }
  940.  
  941.   /* We were doing this in remote-mips.c, I suspect it is right
  942.      for other targets too.  */
  943.   write_pc (loadfile_bfd->start_address);
  944.  
  945.   /* FIXME: are we supposed to call symbol_file_add or not?  According to
  946.      a comment from remote-mips.c (where a call to symbol_file_add was
  947.      commented out), making the call confuses GDB if more than one file is
  948.      loaded in.  remote-nindy.c had no call to symbol_file_add, but remote-vx.c
  949.      does.  */
  950.  
  951.   do_cleanups (old_cleanups);
  952. }
  953.  
  954. /* This function allows the addition of incrementally linked object files.
  955.    It does not modify any state in the target, only in the debugger.  */
  956.  
  957. /* ARGSUSED */
  958. static void
  959. add_symbol_file_command (args, from_tty)
  960.      char *args;
  961.      int from_tty;
  962. {
  963.   char *name = NULL;
  964.   CORE_ADDR text_addr;
  965.   char *arg;
  966.   int readnow = 0;
  967.   int mapped = 0;
  968.   
  969.   dont_repeat ();
  970.  
  971.   if (args == NULL)
  972.     {
  973.       error ("add-symbol-file takes a file name and an address");
  974.     }
  975.  
  976.   /* Make a copy of the string that we can safely write into. */
  977.  
  978.   args = strdup (args);
  979.   make_cleanup (free, args);
  980.  
  981.   /* Pick off any -option args and the file name. */
  982.  
  983.   while ((*args != '\000') && (name == NULL))
  984.     {
  985.       while (isspace (*args)) {args++;}
  986.       arg = args;
  987.       while ((*args != '\000') && !isspace (*args)) {args++;}
  988.       if (*args != '\000')
  989.     {
  990.       *args++ = '\000';
  991.     }
  992.       if (*arg != '-')
  993.     {
  994.       name = arg;
  995.     }
  996.       else if (STREQ (arg, "-mapped"))
  997.     {
  998.       mapped = 1;
  999.     }
  1000.       else if (STREQ (arg, "-readnow"))
  1001.     {
  1002.       readnow = 1;
  1003.     }
  1004.       else
  1005.     {
  1006.       error ("unknown option `%s'", arg);
  1007.     }
  1008.     }
  1009.  
  1010.   /* After picking off any options and the file name, args should be
  1011.      left pointing at the remainder of the command line, which should
  1012.      be the address expression to evaluate. */
  1013.  
  1014.   if (name == NULL)
  1015.     {
  1016.       error ("add-symbol-file takes a file name");
  1017.     }
  1018.   name = tilde_expand (name);
  1019.   make_cleanup (free, name);
  1020.  
  1021.   if (*args != '\000')
  1022.     {
  1023.       text_addr = parse_and_eval_address (args);
  1024.     }
  1025.   else
  1026.     {
  1027.       target_link(name, &text_addr);
  1028.       if (text_addr == (CORE_ADDR)-1)
  1029.     error("Don't know how to get text start location for this file");
  1030.     }
  1031.  
  1032.   /* FIXME-32x64: Assumes text_addr fits in a long.  */
  1033.   if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
  1034.           name, local_hex_string ((unsigned long)text_addr)))
  1035.     error ("Not confirmed.");
  1036.  
  1037.   symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
  1038.  
  1039.   /* Getting new symbols may change our opinion about what is
  1040.      frameless.  */
  1041.   reinit_frame_cache ();
  1042. }
  1043.  
  1044. static void
  1045. add_shared_symbol_files_command  (args, from_tty)
  1046.      char *args;
  1047.      int from_tty;
  1048. {
  1049. #ifdef ADD_SHARED_SYMBOL_FILES
  1050.   ADD_SHARED_SYMBOL_FILES (args, from_tty);
  1051. #else
  1052.   error ("This command is not available in this configuration of GDB.");
  1053. #endif  
  1054. }
  1055.  
  1056. /* Re-read symbols if a symbol-file has changed.  */
  1057. void
  1058. reread_symbols ()
  1059. {
  1060.   struct objfile *objfile;
  1061.   long new_modtime;
  1062.   int reread_one = 0;
  1063.   struct stat new_statbuf;
  1064.   int res;
  1065.  
  1066.   /* With the addition of shared libraries, this should be modified,
  1067.      the load time should be saved in the partial symbol tables, since
  1068.      different tables may come from different source files.  FIXME.
  1069.      This routine should then walk down each partial symbol table
  1070.      and see if the symbol table that it originates from has been changed */
  1071.  
  1072.   for (objfile = object_files; objfile; objfile = objfile->next) {
  1073.     if (objfile->obfd) {
  1074. #ifdef IBM6000_TARGET
  1075.      /* If this object is from a shared library, then you should
  1076.         stat on the library name, not member name. */
  1077.  
  1078.      if (objfile->obfd->my_archive)
  1079.        res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
  1080.      else
  1081. #endif
  1082.       res = stat (objfile->name, &new_statbuf);
  1083.       if (res != 0) {
  1084.     /* FIXME, should use print_sys_errmsg but it's not filtered. */
  1085.     printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
  1086.              objfile->name);
  1087.     continue;
  1088.       }
  1089.       new_modtime = new_statbuf.st_mtime;
  1090.       if (new_modtime != objfile->mtime)
  1091.     {
  1092.       struct cleanup *old_cleanups;
  1093.       struct section_offsets *offsets;
  1094.       int num_offsets;
  1095.       int section_offsets_size;
  1096.       char *obfd_filename;
  1097.  
  1098.       printf_filtered ("`%s' has changed; re-reading symbols.\n",
  1099.                objfile->name);
  1100.  
  1101.       /* There are various functions like symbol_file_add,
  1102.          symfile_bfd_open, syms_from_objfile, etc., which might
  1103.          appear to do what we want.  But they have various other
  1104.          effects which we *don't* want.  So we just do stuff
  1105.          ourselves.  We don't worry about mapped files (for one thing,
  1106.          any mapped file will be out of date).  */
  1107.  
  1108.       /* If we get an error, blow away this objfile (not sure if
  1109.          that is the correct response for things like shared
  1110.          libraries).  */
  1111.       old_cleanups = make_cleanup (free_objfile, objfile);
  1112.       /* We need to do this whenever any symbols go away.  */
  1113.       make_cleanup (clear_symtab_users, 0);
  1114.  
  1115.       /* Clean up any state BFD has sitting around.  We don't need
  1116.          to close the descriptor but BFD lacks a way of closing the
  1117.          BFD without closing the descriptor.  */
  1118.       obfd_filename = bfd_get_filename (objfile->obfd);
  1119.       if (!bfd_close (objfile->obfd))
  1120.         error ("Can't close BFD for %s: %s", objfile->name,
  1121.            bfd_errmsg (bfd_get_error ()));
  1122.       objfile->obfd = bfd_openr (obfd_filename, gnutarget);
  1123.       if (objfile->obfd == NULL)
  1124.         error ("Can't open %s to read symbols.", objfile->name);
  1125.       /* bfd_openr sets cacheable to true, which is what we want.  */
  1126.       if (!bfd_check_format (objfile->obfd, bfd_object))
  1127.         error ("Can't read symbols from %s: %s.", objfile->name,
  1128.            bfd_errmsg (bfd_get_error ()));
  1129.  
  1130.       /* Save the offsets, we will nuke them with the rest of the
  1131.          psymbol_obstack.  */
  1132.       num_offsets = objfile->num_sections;
  1133.       section_offsets_size =
  1134.         sizeof (struct section_offsets)
  1135.           + sizeof (objfile->section_offsets->offsets) * num_offsets;
  1136.       offsets = (struct section_offsets *) alloca (section_offsets_size);
  1137.       memcpy (offsets, objfile->section_offsets, section_offsets_size);
  1138.  
  1139.       /* Nuke all the state that we will re-read.  Much of the following
  1140.          code which sets things to NULL really is necessary to tell
  1141.          other parts of GDB that there is nothing currently there.  */
  1142.  
  1143.       /* FIXME: Do we have to free a whole linked list, or is this
  1144.          enough?  */
  1145.       if (objfile->global_psymbols.list)
  1146.         mfree (objfile->md, objfile->global_psymbols.list);
  1147.       objfile->global_psymbols.list = NULL;
  1148.       objfile->global_psymbols.next = NULL;
  1149.       objfile->global_psymbols.size = 0;
  1150.       if (objfile->static_psymbols.list)
  1151.         mfree (objfile->md, objfile->static_psymbols.list);
  1152.       objfile->static_psymbols.list = NULL;
  1153.       objfile->static_psymbols.next = NULL;
  1154.       objfile->static_psymbols.size = 0;
  1155.  
  1156.       /* Free the obstacks for non-reusable objfiles */
  1157.       obstack_free (&objfile -> psymbol_obstack, 0);
  1158.       obstack_free (&objfile -> symbol_obstack, 0);
  1159.       obstack_free (&objfile -> type_obstack, 0);
  1160.       objfile->sections = NULL;
  1161.       objfile->symtabs = NULL;
  1162.       objfile->psymtabs = NULL;
  1163.       objfile->free_psymtabs = NULL;
  1164.       objfile->msymbols = NULL;
  1165.       objfile->minimal_symbol_count= 0;
  1166.       objfile->fundamental_types = NULL;
  1167.       if (objfile -> sf != NULL)
  1168.         {
  1169.           (*objfile -> sf -> sym_finish) (objfile);
  1170.         }
  1171.  
  1172.       /* We never make this a mapped file.  */
  1173.       objfile -> md = NULL;
  1174.       /* obstack_specify_allocation also initializes the obstack so
  1175.          it is empty.  */
  1176.       obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
  1177.                       xmalloc, free);
  1178.       obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
  1179.                       xmalloc, free);
  1180.       obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
  1181.                       xmalloc, free);
  1182.       if (build_objfile_section_table (objfile))
  1183.         {
  1184.           error ("Can't find the file sections in `%s': %s", 
  1185.              objfile -> name, bfd_errmsg (bfd_get_error ()));
  1186.         }
  1187.  
  1188.       /* We use the same section offsets as from last time.  I'm not
  1189.          sure whether that is always correct for shared libraries.  */
  1190.       objfile->section_offsets = (struct section_offsets *)
  1191.         obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
  1192.       memcpy (objfile->section_offsets, offsets, section_offsets_size);
  1193.       objfile->num_sections = num_offsets;
  1194.  
  1195.       /* What the hell is sym_new_init for, anyway?  The concept of
  1196.          distinguishing between the main file and additional files
  1197.          in this way seems rather dubious.  */
  1198.       if (objfile == symfile_objfile)
  1199.         (*objfile->sf->sym_new_init) (objfile);
  1200.  
  1201.       (*objfile->sf->sym_init) (objfile);
  1202.       clear_complaints (1, 1);
  1203.       /* The "mainline" parameter is a hideous hack; I think leaving it
  1204.          zero is OK since dbxread.c also does what it needs to do if
  1205.          objfile->global_psymbols.size is 0.  */
  1206.       (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
  1207.       if (!have_partial_symbols () && !have_full_symbols ())
  1208.         {
  1209.           wrap_here ("");
  1210.           printf_filtered ("(no debugging symbols found)\n");
  1211.           wrap_here ("");
  1212.         }
  1213.       objfile -> flags |= OBJF_SYMS;
  1214.  
  1215.       /* We're done reading the symbol file; finish off complaints.  */
  1216.       clear_complaints (0, 1);
  1217.  
  1218.       /* Getting new symbols may change our opinion about what is
  1219.          frameless.  */
  1220.  
  1221.       reinit_frame_cache ();
  1222.  
  1223.       /* Discard cleanups as symbol reading was successful.  */
  1224.       discard_cleanups (old_cleanups);
  1225.  
  1226.       /* If the mtime has changed between the time we set new_modtime
  1227.          and now, we *want* this to be out of date, so don't call stat
  1228.          again now.  */
  1229.       objfile->mtime = new_modtime;
  1230.       reread_one = 1;
  1231.     }
  1232.     }
  1233.   }
  1234.  
  1235.   if (reread_one)
  1236.     clear_symtab_users ();
  1237. }
  1238.  
  1239.  
  1240. enum language
  1241. deduce_language_from_filename (filename)
  1242.      char *filename;
  1243. {
  1244.   char *c;
  1245.   
  1246.   if (0 == filename) 
  1247.     ; /* Get default */
  1248.   else if (0 == (c = strrchr (filename, '.')))
  1249.     ; /* Get default. */
  1250.   else if (STREQ (c, ".c"))
  1251.     return language_c;
  1252.   else if (STREQ (c, ".cc") || STREQ (c, ".C") || STREQ (c, ".cxx")
  1253.        || STREQ (c, ".cpp") || STREQ (c, ".cp") || STREQ (c, ".c++"))
  1254.     return language_cplus;
  1255.   else if (STREQ (c, ".ch") || STREQ (c, ".c186") || STREQ (c, ".c286"))
  1256.     return language_chill;
  1257.   else if (STREQ (c, ".f") || STREQ (c, ".F"))
  1258.     return language_fortran;
  1259.   else if (STREQ (c, ".mod"))
  1260.     return language_m2;
  1261.   else if (STREQ (c, ".s") || STREQ (c, ".S"))
  1262.     return language_asm;
  1263.  
  1264.   return language_unknown;        /* default */
  1265. }
  1266.  
  1267. /* allocate_symtab:
  1268.  
  1269.    Allocate and partly initialize a new symbol table.  Return a pointer
  1270.    to it.  error() if no space.
  1271.  
  1272.    Caller must set these fields:
  1273.     LINETABLE(symtab)
  1274.     symtab->blockvector
  1275.     symtab->dirname
  1276.     symtab->free_code
  1277.     symtab->free_ptr
  1278.     initialize any EXTRA_SYMTAB_INFO
  1279.     possibly free_named_symtabs (symtab->filename);
  1280.  */
  1281.  
  1282. struct symtab *
  1283. allocate_symtab (filename, objfile)
  1284.      char *filename;
  1285.      struct objfile *objfile;
  1286. {
  1287.   register struct symtab *symtab;
  1288.  
  1289.   symtab = (struct symtab *)
  1290.     obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
  1291.   memset (symtab, 0, sizeof (*symtab));
  1292.   symtab -> filename = obsavestring (filename, strlen (filename),
  1293.                      &objfile -> symbol_obstack);
  1294.   symtab -> fullname = NULL;
  1295.   symtab -> language = deduce_language_from_filename (filename);
  1296.  
  1297.   /* Hook it to the objfile it comes from */
  1298.  
  1299.   symtab -> objfile = objfile;
  1300.   symtab -> next = objfile -> symtabs;
  1301.   objfile -> symtabs = symtab;
  1302.  
  1303. #ifdef INIT_EXTRA_SYMTAB_INFO
  1304.   INIT_EXTRA_SYMTAB_INFO (symtab);
  1305. #endif
  1306.  
  1307.   return (symtab);
  1308. }
  1309.  
  1310. struct partial_symtab *
  1311. allocate_psymtab (filename, objfile)
  1312.      char *filename;
  1313.      struct objfile *objfile;
  1314. {
  1315.   struct partial_symtab *psymtab;
  1316.  
  1317.   if (objfile -> free_psymtabs)
  1318.     {
  1319.       psymtab = objfile -> free_psymtabs;
  1320.       objfile -> free_psymtabs = psymtab -> next;
  1321.     }
  1322.   else
  1323.     psymtab = (struct partial_symtab *)
  1324.       obstack_alloc (&objfile -> psymbol_obstack,
  1325.              sizeof (struct partial_symtab));
  1326.  
  1327.   memset (psymtab, 0, sizeof (struct partial_symtab));
  1328.   psymtab -> filename = obsavestring (filename, strlen (filename),
  1329.                       &objfile -> psymbol_obstack);
  1330.   psymtab -> symtab = NULL;
  1331.  
  1332.   /* Hook it to the objfile it comes from */
  1333.  
  1334.   psymtab -> objfile = objfile;
  1335.   psymtab -> next = objfile -> psymtabs;
  1336.   objfile -> psymtabs = psymtab;
  1337.   
  1338.   return (psymtab);
  1339. }
  1340.  
  1341.  
  1342. /* Reset all data structures in gdb which may contain references to symbol
  1343.    table date.  */
  1344.  
  1345. void
  1346. clear_symtab_users ()
  1347. {
  1348.   /* Someday, we should do better than this, by only blowing away
  1349.      the things that really need to be blown.  */
  1350.   clear_value_history ();
  1351.   clear_displays ();
  1352.   clear_internalvars ();
  1353.   breakpoint_re_set ();
  1354.   set_default_breakpoint (0, 0, 0, 0);
  1355.   current_source_symtab = 0;
  1356.   current_source_line = 0;
  1357.   clear_pc_function_cache ();
  1358. }
  1359.  
  1360. /* clear_symtab_users_once:
  1361.  
  1362.    This function is run after symbol reading, or from a cleanup.
  1363.    If an old symbol table was obsoleted, the old symbol table
  1364.    has been blown away, but the other GDB data structures that may 
  1365.    reference it have not yet been cleared or re-directed.  (The old
  1366.    symtab was zapped, and the cleanup queued, in free_named_symtab()
  1367.    below.)
  1368.  
  1369.    This function can be queued N times as a cleanup, or called
  1370.    directly; it will do all the work the first time, and then will be a
  1371.    no-op until the next time it is queued.  This works by bumping a
  1372.    counter at queueing time.  Much later when the cleanup is run, or at
  1373.    the end of symbol processing (in case the cleanup is discarded), if
  1374.    the queued count is greater than the "done-count", we do the work
  1375.    and set the done-count to the queued count.  If the queued count is
  1376.    less than or equal to the done-count, we just ignore the call.  This
  1377.    is needed because reading a single .o file will often replace many
  1378.    symtabs (one per .h file, for example), and we don't want to reset
  1379.    the breakpoints N times in the user's face.
  1380.  
  1381.    The reason we both queue a cleanup, and call it directly after symbol
  1382.    reading, is because the cleanup protects us in case of errors, but is
  1383.    discarded if symbol reading is successful.  */
  1384.  
  1385. #if 0
  1386. /* FIXME:  As free_named_symtabs is currently a big noop this function
  1387.    is no longer needed.  */
  1388. static void
  1389. clear_symtab_users_once PARAMS ((void));
  1390.  
  1391. static int clear_symtab_users_queued;
  1392. static int clear_symtab_users_done;
  1393.  
  1394. static void
  1395. clear_symtab_users_once ()
  1396. {
  1397.   /* Enforce once-per-`do_cleanups'-semantics */
  1398.   if (clear_symtab_users_queued <= clear_symtab_users_done)
  1399.     return;
  1400.   clear_symtab_users_done = clear_symtab_users_queued;
  1401.  
  1402.   clear_symtab_users ();
  1403. }
  1404. #endif
  1405.  
  1406. /* Delete the specified psymtab, and any others that reference it.  */
  1407.  
  1408. static void
  1409. cashier_psymtab (pst)
  1410.      struct partial_symtab *pst;
  1411. {
  1412.   struct partial_symtab *ps, *pprev = NULL;
  1413.   int i;
  1414.  
  1415.   /* Find its previous psymtab in the chain */
  1416.   for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
  1417.     if (ps == pst)
  1418.       break;
  1419.     pprev = ps;
  1420.   }
  1421.  
  1422.   if (ps) {
  1423.     /* Unhook it from the chain.  */
  1424.     if (ps == pst->objfile->psymtabs)
  1425.       pst->objfile->psymtabs = ps->next;
  1426.     else
  1427.       pprev->next = ps->next;
  1428.  
  1429.     /* FIXME, we can't conveniently deallocate the entries in the
  1430.        partial_symbol lists (global_psymbols/static_psymbols) that
  1431.        this psymtab points to.  These just take up space until all
  1432.        the psymtabs are reclaimed.  Ditto the dependencies list and
  1433.        filename, which are all in the psymbol_obstack.  */
  1434.  
  1435.     /* We need to cashier any psymtab that has this one as a dependency... */
  1436. again:
  1437.     for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
  1438.       for (i = 0; i < ps->number_of_dependencies; i++) {
  1439.     if (ps->dependencies[i] == pst) {
  1440.       cashier_psymtab (ps);
  1441.       goto again;        /* Must restart, chain has been munged. */
  1442.     }
  1443.       }
  1444.     }
  1445.   }
  1446. }
  1447.  
  1448. /* If a symtab or psymtab for filename NAME is found, free it along
  1449.    with any dependent breakpoints, displays, etc.
  1450.    Used when loading new versions of object modules with the "add-file"
  1451.    command.  This is only called on the top-level symtab or psymtab's name;
  1452.    it is not called for subsidiary files such as .h files.
  1453.  
  1454.    Return value is 1 if we blew away the environment, 0 if not.
  1455.    FIXME.  The return valu appears to never be used.
  1456.  
  1457.    FIXME.  I think this is not the best way to do this.  We should
  1458.    work on being gentler to the environment while still cleaning up
  1459.    all stray pointers into the freed symtab.  */
  1460.  
  1461. int
  1462. free_named_symtabs (name)
  1463.      char *name;
  1464. {
  1465. #if 0
  1466.   /* FIXME:  With the new method of each objfile having it's own
  1467.      psymtab list, this function needs serious rethinking.  In particular,
  1468.      why was it ever necessary to toss psymtabs with specific compilation
  1469.      unit filenames, as opposed to all psymtabs from a particular symbol
  1470.      file?  -- fnf
  1471.      Well, the answer is that some systems permit reloading of particular
  1472.      compilation units.  We want to blow away any old info about these
  1473.      compilation units, regardless of which objfiles they arrived in. --gnu.  */
  1474.  
  1475.   register struct symtab *s;
  1476.   register struct symtab *prev;
  1477.   register struct partial_symtab *ps;
  1478.   struct blockvector *bv;
  1479.   int blewit = 0;
  1480.  
  1481.   /* We only wack things if the symbol-reload switch is set.  */
  1482.   if (!symbol_reloading)
  1483.     return 0;
  1484.  
  1485.   /* Some symbol formats have trouble providing file names... */
  1486.   if (name == 0 || *name == '\0')
  1487.     return 0;
  1488.  
  1489.   /* Look for a psymtab with the specified name.  */
  1490.  
  1491. again2:
  1492.   for (ps = partial_symtab_list; ps; ps = ps->next) {
  1493.     if (STREQ (name, ps->filename)) {
  1494.       cashier_psymtab (ps);    /* Blow it away...and its little dog, too.  */
  1495.       goto again2;        /* Must restart, chain has been munged */
  1496.     }
  1497.   }
  1498.  
  1499.   /* Look for a symtab with the specified name.  */
  1500.  
  1501.   for (s = symtab_list; s; s = s->next)
  1502.     {
  1503.       if (STREQ (name, s->filename))
  1504.     break;
  1505.       prev = s;
  1506.     }
  1507.  
  1508.   if (s)
  1509.     {
  1510.       if (s == symtab_list)
  1511.     symtab_list = s->next;
  1512.       else
  1513.     prev->next = s->next;
  1514.  
  1515.       /* For now, queue a delete for all breakpoints, displays, etc., whether
  1516.      or not they depend on the symtab being freed.  This should be
  1517.      changed so that only those data structures affected are deleted.  */
  1518.  
  1519.       /* But don't delete anything if the symtab is empty.
  1520.      This test is necessary due to a bug in "dbxread.c" that
  1521.      causes empty symtabs to be created for N_SO symbols that
  1522.      contain the pathname of the object file.  (This problem
  1523.      has been fixed in GDB 3.9x).  */
  1524.  
  1525.       bv = BLOCKVECTOR (s);
  1526.       if (BLOCKVECTOR_NBLOCKS (bv) > 2
  1527.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
  1528.       || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
  1529.     {
  1530.       complain (&oldsyms_complaint, name);
  1531.  
  1532.       clear_symtab_users_queued++;
  1533.       make_cleanup (clear_symtab_users_once, 0);
  1534.       blewit = 1;
  1535.     } else {
  1536.       complain (&empty_symtab_complaint, name);
  1537.     }
  1538.  
  1539.       free_symtab (s);
  1540.     }
  1541.   else
  1542.     {
  1543.       /* It is still possible that some breakpoints will be affected
  1544.      even though no symtab was found, since the file might have
  1545.      been compiled without debugging, and hence not be associated
  1546.      with a symtab.  In order to handle this correctly, we would need
  1547.      to keep a list of text address ranges for undebuggable files.
  1548.      For now, we do nothing, since this is a fairly obscure case.  */
  1549.       ;
  1550.     }
  1551.  
  1552.   /* FIXME, what about the minimal symbol table? */
  1553.   return blewit;
  1554. #else
  1555.   return (0);
  1556. #endif
  1557. }
  1558.  
  1559. /* Allocate and partially fill a partial symtab.  It will be
  1560.    completely filled at the end of the symbol list.
  1561.  
  1562.    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
  1563.    is the address relative to which its symbols are (incremental) or 0
  1564.    (normal). */
  1565.  
  1566.  
  1567. struct partial_symtab *
  1568. start_psymtab_common (objfile, section_offsets,
  1569.               filename, textlow, global_syms, static_syms)
  1570.      struct objfile *objfile;
  1571.      struct section_offsets *section_offsets;
  1572.      char *filename;
  1573.      CORE_ADDR textlow;
  1574.      struct partial_symbol *global_syms;
  1575.      struct partial_symbol *static_syms;
  1576. {
  1577.   struct partial_symtab *psymtab;
  1578.  
  1579.   psymtab = allocate_psymtab (filename, objfile);
  1580.   psymtab -> section_offsets = section_offsets;
  1581.   psymtab -> textlow = textlow;
  1582.   psymtab -> texthigh = psymtab -> textlow;  /* default */
  1583.   psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
  1584.   psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
  1585.   return (psymtab);
  1586. }
  1587.  
  1588. /* Debugging versions of functions that are usually inline macros
  1589.    (see symfile.h).  */
  1590.  
  1591. #if !INLINE_ADD_PSYMBOL
  1592.  
  1593. /* Add a symbol with a long value to a psymtab.
  1594.    Since one arg is a struct, we pass in a ptr and deref it (sigh).  */
  1595.  
  1596. void
  1597. add_psymbol_to_list (name, namelength, namespace, class, list, val, language,
  1598.              objfile)
  1599.      char *name;
  1600.      int namelength;
  1601.      enum namespace namespace;
  1602.      enum address_class class;
  1603.      struct psymbol_allocation_list *list;
  1604.      long val;
  1605.      enum language language;
  1606.      struct objfile *objfile;
  1607. {
  1608.   register struct partial_symbol *psym;
  1609.   register char *demangled_name;
  1610.  
  1611.   if (list->next >= list->list + list->size)
  1612.     {
  1613.       extend_psymbol_list (list,objfile);
  1614.     }
  1615.   psym = list->next++;
  1616.   
  1617.   SYMBOL_NAME (psym) =
  1618.     (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
  1619.   memcpy (SYMBOL_NAME (psym), name, namelength);
  1620.   SYMBOL_NAME (psym)[namelength] = '\0';
  1621.   SYMBOL_VALUE (psym) = val;
  1622.   SYMBOL_LANGUAGE (psym) = language;
  1623.   PSYMBOL_NAMESPACE (psym) = namespace;
  1624.   PSYMBOL_CLASS (psym) = class;
  1625.   SYMBOL_INIT_LANGUAGE_SPECIFIC (psym, language);
  1626. }
  1627.  
  1628. /* Add a symbol with a CORE_ADDR value to a psymtab. */
  1629.  
  1630. void
  1631. add_psymbol_addr_to_list (name, namelength, namespace, class, list, val,
  1632.               language, objfile)
  1633.      char *name;
  1634.      int namelength;
  1635.      enum namespace namespace;
  1636.      enum address_class class;
  1637.      struct psymbol_allocation_list *list;
  1638.      CORE_ADDR val;
  1639.      enum language language;
  1640.      struct objfile *objfile;
  1641. {
  1642.   register struct partial_symbol *psym;
  1643.   register char *demangled_name;
  1644.  
  1645.   if (list->next >= list->list + list->size)
  1646.     {
  1647.       extend_psymbol_list (list,objfile);
  1648.     }
  1649.   psym = list->next++;
  1650.   
  1651.   SYMBOL_NAME (psym) =
  1652.     (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
  1653.   memcpy (SYMBOL_NAME (psym), name, namelength);
  1654.   SYMBOL_NAME (psym)[namelength] = '\0';
  1655.   SYMBOL_VALUE_ADDRESS (psym) = val;
  1656.   SYMBOL_LANGUAGE (psym) = language;
  1657.   PSYMBOL_NAMESPACE (psym) = namespace;
  1658.   PSYMBOL_CLASS (psym) = class;
  1659.   SYMBOL_INIT_LANGUAGE_SPECIFIC (psym, language);
  1660. }
  1661.  
  1662. #endif /* !INLINE_ADD_PSYMBOL */
  1663.  
  1664.  
  1665. void
  1666. _initialize_symfile ()
  1667. {
  1668.   struct cmd_list_element *c;
  1669.   
  1670.   c = add_cmd ("symbol-file", class_files, symbol_file_command,
  1671.    "Load symbol table from executable file FILE.\n\
  1672. The `file' command can also load symbol tables, as well as setting the file\n\
  1673. to execute.", &cmdlist);
  1674.   c->completer = filename_completer;
  1675.  
  1676.   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
  1677.    "Usage: add-symbol-file FILE ADDR\n\
  1678. Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
  1679. ADDR is the starting address of the file's text.",
  1680.            &cmdlist);
  1681.   c->completer = filename_completer;
  1682.  
  1683.   c = add_cmd ("add-shared-symbol-files", class_files,
  1684.            add_shared_symbol_files_command,
  1685.    "Load the symbols from shared objects in the dynamic linker's link map.",
  1686.               &cmdlist);
  1687.   c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
  1688.              &cmdlist);
  1689.  
  1690.   c = add_cmd ("load", class_files, load_command,
  1691.    "Dynamically load FILE into the running program, and record its symbols\n\
  1692. for access from GDB.", &cmdlist);
  1693.   c->completer = filename_completer;
  1694.  
  1695.   add_show_from_set
  1696.     (add_set_cmd ("symbol-reloading", class_support, var_boolean,
  1697.           (char *)&symbol_reloading,
  1698.       "Set dynamic symbol table reloading multiple times in one run.",
  1699.           &setlist),
  1700.      &showlist);
  1701.  
  1702. }
  1703.