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