symfile.c revision 19371
177218Sphk/* Generic symbol file reading for the GNU debugger, GDB.
277218Sphk   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
377218Sphk   Free Software Foundation, Inc.
477218Sphk   Contributed by Cygnus Support, using pieces from other GDB modules.
577218Sphk
677218SphkThis file is part of GDB.
777218Sphk
877218SphkThis program is free software; you can redistribute it and/or modify
977218Sphkit under the terms of the GNU General Public License as published by
1077218Sphkthe Free Software Foundation; either version 2 of the License, or
1177218Sphk(at your option) any later version.
1291454Sbrooks
1391454SbrooksThis program is distributed in the hope that it will be useful,
1477218Sphkbut WITHOUT ANY WARRANTY; without even the implied warranty of
1577218SphkMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1677218SphkGNU General Public License for more details.
1777218Sphk
1877218SphkYou should have received a copy of the GNU General Public License
1977218Sphkalong with this program; if not, write to the Free Software
2077218SphkFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2177218Sphk
2277218Sphk#include "defs.h"
2377218Sphk#include "symtab.h"
2477218Sphk#include "gdbtypes.h"
2577218Sphk#include "gdbcore.h"
2677218Sphk#include "frame.h"
2777218Sphk#include "target.h"
2877218Sphk#include "value.h"
2977218Sphk#include "symfile.h"
3077218Sphk#include "objfiles.h"
3177218Sphk#include "gdbcmd.h"
3277218Sphk#include "breakpoint.h"
3377218Sphk#include "language.h"
3477218Sphk#include "complaints.h"
3577218Sphk#include "demangle.h"
3677218Sphk#include "inferior.h" /* for write_pc */
3777218Sphk
3877218Sphk#include "obstack.h"
3977218Sphk#include <assert.h>
4077218Sphk
4177218Sphk#include <sys/types.h>
4277218Sphk#include <fcntl.h>
4377218Sphk#include "gdb_string.h"
4477218Sphk#include "gdb_stat.h"
4577218Sphk#include <ctype.h>
4677218Sphk#include <time.h>
4777218Sphk#ifdef HAVE_UNISTD_H
4877218Sphk#include <unistd.h>
4977218Sphk#endif
5077218Sphk
5177218Sphk#ifndef O_BINARY
5277218Sphk#define O_BINARY 0
5377218Sphk#endif
5477218Sphk
5577218Sphk/* Global variables owned by this file */
5677218Sphkint readnow_symbol_files;		/* Read full symbols immediately */
5777218Sphk
5877218Sphkstruct complaint oldsyms_complaint = {
5977218Sphk  "Replacing old symbols for `%s'", 0, 0
6077218Sphk};
6177218Sphk
6277218Sphkstruct complaint empty_symtab_complaint = {
6377218Sphk  "Empty symbol table found for `%s'", 0, 0
6477218Sphk};
6577218Sphk
6677218Sphk/* External variables and functions referenced. */
6777218Sphk
6877218Sphkextern int info_verbose;
6977218Sphk
7077218Sphk/* Functions this file defines */
7177218Sphk
7277218Sphkstatic void
7377218Sphkset_initial_language PARAMS ((void));
7477218Sphk
7577218Sphkstatic void
7677218Sphkload_command PARAMS ((char *, int));
77138593Ssam
7877218Sphkstatic void
79138593Ssamadd_symbol_file_command PARAMS ((char *, int));
80116957Ssam
8177218Sphkstatic void
8277218Sphkadd_shared_symbol_files_command PARAMS ((char *, int));
8377218Sphk
8477218Sphkstatic void
8577218Sphkcashier_psymtab PARAMS ((struct partial_symtab *));
86146873Sjhb
8777218Sphkstatic int
8877218Sphkcompare_psymbols PARAMS ((const void *, const void *));
8977218Sphk
9077218Sphkstatic int
91155931Ssamcompare_symbols PARAMS ((const void *, const void *));
92173275Ssam
9377218Sphkstatic bfd *
9477218Sphksymfile_bfd_open PARAMS ((char *));
95178354Ssam
9677218Sphkstatic void
97178354Ssamfind_sym_fns PARAMS ((struct objfile *));
98178354Ssam
99178354Ssam/* List of all available sym_fns.  On gdb startup, each object file reader
100178354Ssam   calls add_symtab_fns() to register information on each format it is
101178354Ssam   prepared to read. */
102178354Ssam
103178354Ssamstatic struct sym_fns *symtab_fns = NULL;
104178354Ssam
105178354Ssam/* Flag for whether user will be reloading symbols multiple times.
106178354Ssam   Defaults to ON for VxWorks, otherwise OFF.  */
107178354Ssam
108178354Ssam#ifdef SYMBOL_RELOADING_DEFAULT
109178354Ssamint symbol_reloading = SYMBOL_RELOADING_DEFAULT;
110178354Ssam#else
111178354Ssamint symbol_reloading = 0;
112178354Ssam#endif
113178354Ssam
114178354Ssam/* If true, then shared library symbols will be added automatically
115183261Ssam   when the inferior is created, new libraries are loaded, or when
116183261Ssam   attaching to the inferior.  This is almost always what users
117183261Ssam   will want to have happen; but for very large programs, the startup
118183261Ssam   time will be excessive, and so if this is a problem, the user can
119183261Ssam   clear this flag and then add the shared library symbols as needed.
120178354Ssam   Note that there is a potential for confusion, since if the shared
121178354Ssam   library symbols are not loaded, commands like "info fun" will *not*
122173275Ssam   report all the functions that are actually present.  */
123173275Ssam
124173275Ssamint auto_solib_add = 1;
125173275Ssam
126173275Ssam
127173275Ssam/* Since this function is called from within qsort, in an ANSI environment
128173275Ssam   it must conform to the prototype for qsort, which specifies that the
129173275Ssam   comparison function takes two "void *" pointers. */
130178354Ssam
131178354Ssamstatic int
132178354Ssamcompare_symbols (s1p, s2p)
133173275Ssam     const PTR s1p;
134173275Ssam     const PTR s2p;
135178354Ssam{
136173275Ssam  register struct symbol **s1, **s2;
137173275Ssam
138173275Ssam  s1 = (struct symbol **) s1p;
13977218Sphk  s2 = (struct symbol **) s2p;
14077218Sphk
14177218Sphk  return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
142178354Ssam}
143178354Ssam
144178354Ssam/*
145178354Ssam
146178354SsamLOCAL FUNCTION
14777218Sphk
148170531Ssam	compare_psymbols -- compare two partial symbols by name
149178354Ssam
150178354SsamDESCRIPTION
151178354Ssam
152178354Ssam	Given pointers to pointers to two partial symbol table entries,
153178354Ssam	compare them by name and return -N, 0, or +N (ala strcmp).
154178354Ssam	Typically used by sorting routines like qsort().
155173275Ssam
156173275SsamNOTES
157178354Ssam
158173275Ssam	Does direct compare of first two characters before punting
159173275Ssam	and passing to strcmp for longer compares.  Note that the
160170531Ssam	original version had a bug whereby two null strings or two
161173275Ssam	identically named one character strings would return the
162173275Ssam	comparison of memory following the null byte.
163173275Ssam
164173275Ssam */
165173275Ssam
166173275Ssamstatic int
167173275Ssamcompare_psymbols (s1p, s2p)
168173275Ssam     const PTR s1p;
169173275Ssam     const PTR s2p;
170173275Ssam{
171170531Ssam  register char *st1 = SYMBOL_NAME (*(struct partial_symbol **) s1p);
172170531Ssam  register char *st2 = SYMBOL_NAME (*(struct partial_symbol **) s2p);
173170531Ssam
174170531Ssam  if ((st1[0] - st2[0]) || !st1[0])
175170531Ssam    {
176170531Ssam      return (st1[0] - st2[0]);
177170531Ssam    }
178170531Ssam  else if ((st1[1] - st2[1]) || !st1[1])
179170531Ssam    {
180173275Ssam      return (st1[1] - st2[1]);
181170531Ssam    }
182170531Ssam  else
183173275Ssam    {
184170531Ssam      return (STRCMP (st1 + 2, st2 + 2));
185170531Ssam    }
186178354Ssam}
187178354Ssam
188178354Ssamvoid
189178354Ssamsort_pst_symbols (pst)
190178354Ssam     struct partial_symtab *pst;
191178354Ssam{
192178354Ssam  /* Sort the global list; don't sort the static list */
193181198Ssam
194178354Ssam  qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
195178354Ssam	 pst -> n_global_syms, sizeof (struct partial_symbol *),
196178354Ssam	 compare_psymbols);
197178354Ssam}
198170531Ssam
199170531Ssam/* Call sort_block_syms to sort alphabetically the symbols of one block.  */
200170531Ssam
201170531Ssamvoid
202170531Ssamsort_block_syms (b)
203170531Ssam     register struct block *b;
204170531Ssam{
205138593Ssam  qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
206170531Ssam	 sizeof (struct symbol *), compare_symbols);
207170531Ssam}
208170531Ssam
209170531Ssam/* Call sort_symtab_syms to sort alphabetically
210170531Ssam   the symbols of each block of one symtab.  */
211170531Ssam
212170531Ssamvoid
213170531Ssamsort_symtab_syms (s)
214170531Ssam     register struct symtab *s;
215170531Ssam{
216170531Ssam  register struct blockvector *bv;
217170531Ssam  int nbl;
218170531Ssam  int i;
219170531Ssam  register struct block *b;
220170531Ssam
221170531Ssam  if (s == 0)
222170531Ssam    return;
223170531Ssam  bv = BLOCKVECTOR (s);
224170531Ssam  nbl = BLOCKVECTOR_NBLOCKS (bv);
225170531Ssam  for (i = 0; i < nbl; i++)
226170531Ssam    {
227170531Ssam      b = BLOCKVECTOR_BLOCK (bv, i);
228170531Ssam      if (BLOCK_SHOULD_SORT (b))
229170531Ssam	sort_block_syms (b);
230170531Ssam    }
231170531Ssam}
232170531Ssam
233170531Ssam/* Make a copy of the string at PTR with SIZE characters in the symbol obstack
234170531Ssam   (and add a null character at the end in the copy).
235170531Ssam   Returns the address of the copy.  */
236170531Ssam
237170531Ssamchar *
238170531Ssamobsavestring (ptr, size, obstackp)
239170531Ssam     char *ptr;
240170531Ssam     int size;
241170531Ssam     struct obstack *obstackp;
242170531Ssam{
243170531Ssam  register char *p = (char *) obstack_alloc (obstackp, size + 1);
244170531Ssam  /* Open-coded memcpy--saves function call time.
245170531Ssam     These strings are usually short.  */
246170531Ssam  {
247170531Ssam    register char *p1 = ptr;
248170531Ssam    register char *p2 = p;
249170531Ssam    char *end = ptr + size;
250170531Ssam    while (p1 != end)
251170531Ssam      *p2++ = *p1++;
252170531Ssam  }
253170531Ssam  p[size] = 0;
254170531Ssam  return p;
255170531Ssam}
256170531Ssam
257170531Ssam/* Concatenate strings S1, S2 and S3; return the new string.
258170531Ssam   Space is found in the symbol_obstack.  */
259170531Ssam
260170531Ssamchar *
261170531Ssamobconcat (obstackp, s1, s2, s3)
262173275Ssam     struct obstack *obstackp;
263170531Ssam     const char *s1, *s2, *s3;
264170531Ssam{
265173275Ssam  register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
266173275Ssam  register char *val = (char *) obstack_alloc (obstackp, len);
267173275Ssam  strcpy (val, s1);
268173275Ssam  strcat (val, s2);
269173275Ssam  strcat (val, s3);
270173275Ssam  return val;
271170531Ssam}
272173275Ssam
273170531Ssam/* True if we are nested inside psymtab_to_symtab. */
274170531Ssam
275173275Ssamint currently_reading_symtab = 0;
276173275Ssam
277173275Ssamstatic void
278173275Ssamdecrement_reading_symtab (dummy)
279173275Ssam     void *dummy;
280173275Ssam{
281170531Ssam  currently_reading_symtab--;
282170531Ssam}
283170531Ssam
284170531Ssam/* Get the symbol table that corresponds to a partial_symtab.
285170531Ssam   This is fast after the first time you do it.  In fact, there
286170531Ssam   is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
287170531Ssam   case inline.  */
288170531Ssam
289170531Ssamstruct symtab *
290170531Ssampsymtab_to_symtab (pst)
291170531Ssam     register struct partial_symtab *pst;
292170531Ssam{
293170531Ssam  /* If it's been looked up before, return it. */
294170531Ssam  if (pst->symtab)
295170531Ssam    return pst->symtab;
296170531Ssam
297170531Ssam  /* If it has not yet been read in, read it.  */
298170531Ssam  if (!pst->readin)
299170531Ssam    {
300170531Ssam      struct cleanup *back_to = make_cleanup (decrement_reading_symtab, NULL);
301170531Ssam      currently_reading_symtab++;
302170531Ssam      (*pst->read_symtab) (pst);
303170531Ssam      do_cleanups (back_to);
304170531Ssam    }
305170531Ssam
306170531Ssam  return pst->symtab;
307170531Ssam}
308170531Ssam
309170531Ssam/* Initialize entry point information for this objfile. */
310170531Ssam
311170531Ssamvoid
312170531Ssaminit_entry_point_info (objfile)
313170531Ssam     struct objfile *objfile;
314170531Ssam{
315170531Ssam  /* Save startup file's range of PC addresses to help blockframe.c
316170531Ssam     decide where the bottom of the stack is.  */
317170531Ssam
318170531Ssam  if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
319170531Ssam    {
320170531Ssam      /* Executable file -- record its entry point so we'll recognize
321170531Ssam	 the startup file because it contains the entry point.  */
322173275Ssam      objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
323170531Ssam    }
324170531Ssam  else
325173275Ssam    {
326173275Ssam      /* Examination of non-executable.o files.  Short-circuit this stuff.  */
327173275Ssam      objfile -> ei.entry_point = INVALID_ENTRY_POINT;
328173275Ssam    }
329173275Ssam  objfile -> ei.entry_file_lowpc = INVALID_ENTRY_LOWPC;
330173275Ssam  objfile -> ei.entry_file_highpc = INVALID_ENTRY_HIGHPC;
331173275Ssam  objfile -> ei.entry_func_lowpc = INVALID_ENTRY_LOWPC;
332173275Ssam  objfile -> ei.entry_func_highpc = INVALID_ENTRY_HIGHPC;
333173275Ssam  objfile -> ei.main_func_lowpc = INVALID_ENTRY_LOWPC;
334173275Ssam  objfile -> ei.main_func_highpc = INVALID_ENTRY_HIGHPC;
335173275Ssam}
336173275Ssam
337173275Ssam/* Get current entry point address.  */
338173275Ssam
339173275SsamCORE_ADDR
340173275Ssamentry_point_address()
341173275Ssam{
342178354Ssam  return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
343178354Ssam}
344178354Ssam
345178354Ssam/* Remember the lowest-addressed loadable section we've seen.
346178354Ssam   This function is called via bfd_map_over_sections.
347178354Ssam
348178354Ssam   In case of equal vmas, the section with the largest size becomes the
349178354Ssam   lowest-addressed loadable section.
350178354Ssam
351178354Ssam   If the vmas and sizes are equal, the last section is considered the
352178354Ssam   lowest-addressed loadable section.  */
353178354Ssam
354178354Ssamvoid
355178354Ssamfind_lowest_section (abfd, sect, obj)
356178354Ssam     bfd *abfd;
357178354Ssam     asection *sect;
358178354Ssam     PTR obj;
359178354Ssam{
360178354Ssam  asection **lowest = (asection **)obj;
361178354Ssam
362178354Ssam  if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
363178354Ssam    return;
364178354Ssam  if (!*lowest)
365178354Ssam    *lowest = sect;		/* First loadable section */
366178354Ssam  else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
367178354Ssam    *lowest = sect;		/* A lower loadable section */
368178354Ssam  else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
369178354Ssam	   && (bfd_section_size (abfd, (*lowest))
370178354Ssam	       <= bfd_section_size (abfd, sect)))
371178354Ssam    *lowest = sect;
372178354Ssam}
373178354Ssam
374178354Ssam/* Process a symbol file, as either the main file or as a dynamically
375178354Ssam   loaded file.
376178354Ssam
377178354Ssam   NAME is the file name (which will be tilde-expanded and made
378178354Ssam   absolute herein) (but we don't free or modify NAME itself).
379178354Ssam   FROM_TTY says how verbose to be.  MAINLINE specifies whether this
380178354Ssam   is the main symbol file, or whether it's an extra symbol file such
381178354Ssam   as dynamically loaded code.  If !mainline, ADDR is the address
382178354Ssam   where the text segment was loaded.  If VERBO, the caller has printed
383178354Ssam   a verbose message about the symbol reading (and complaints can be
384178354Ssam   more terse about it).  */
385178354Ssam
386178354Ssamvoid
387178354Ssamsyms_from_objfile (objfile, addr, mainline, verbo)
388178354Ssam     struct objfile *objfile;
389178354Ssam     CORE_ADDR addr;
390178354Ssam     int mainline;
391178354Ssam     int verbo;
392178354Ssam{
393178354Ssam  struct section_offsets *section_offsets;
394178354Ssam  asection *lowest_sect;
395178354Ssam  struct cleanup *old_chain;
396178354Ssam
397178354Ssam  init_entry_point_info (objfile);
398178354Ssam  find_sym_fns (objfile);
399178354Ssam
400178354Ssam  /* Make sure that partially constructed symbol tables will be cleaned up
401178354Ssam     if an error occurs during symbol reading.  */
402178354Ssam  old_chain = make_cleanup (free_objfile, objfile);
403178354Ssam
404178354Ssam  if (mainline)
405178354Ssam    {
406178354Ssam      /* We will modify the main symbol table, make sure that all its users
407178354Ssam	 will be cleaned up if an error occurs during symbol reading.  */
408178354Ssam      make_cleanup (clear_symtab_users, 0);
409178354Ssam
410178354Ssam      /* Since no error yet, throw away the old symbol table.  */
411178354Ssam
412178354Ssam      if (symfile_objfile != NULL)
413178354Ssam	{
414178354Ssam	  free_objfile (symfile_objfile);
415178354Ssam	  symfile_objfile = NULL;
416178354Ssam	}
417178354Ssam
418178354Ssam      /* Currently we keep symbols from the add-symbol-file command.
419178354Ssam	 If the user wants to get rid of them, they should do "symbol-file"
420178354Ssam	 without arguments first.  Not sure this is the best behavior
421178354Ssam	 (PR 2207).  */
422178354Ssam
423178354Ssam      (*objfile -> sf -> sym_new_init) (objfile);
424178354Ssam    }
425178354Ssam
426178354Ssam  /* Convert addr into an offset rather than an absolute address.
427178354Ssam     We find the lowest address of a loaded segment in the objfile,
428186102Ssam     and assume that <addr> is where that got loaded.  Due to historical
429178354Ssam     precedent, we warn if that doesn't happen to be a text segment.  */
430178354Ssam
431178354Ssam  if (mainline)
432178354Ssam    {
433178354Ssam      addr = 0;		/* No offset from objfile addresses.  */
434178354Ssam    }
435178354Ssam  else
436178354Ssam    {
437178354Ssam      lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
438178354Ssam      if (lowest_sect == NULL)
439178354Ssam	bfd_map_over_sections (objfile->obfd, find_lowest_section,
440178354Ssam			       (PTR) &lowest_sect);
441178354Ssam
442178354Ssam      if (lowest_sect == NULL)
443178354Ssam	warning ("no loadable sections found in added symbol-file %s",
444178354Ssam		 objfile->name);
445178354Ssam      else if ((bfd_get_section_flags (objfile->obfd, lowest_sect) & SEC_CODE)
446178354Ssam	       == 0)
447178354Ssam	/* FIXME-32x64--assumes bfd_vma fits in long.  */
448178354Ssam	warning ("Lowest section in %s is %s at 0x%lx",
449178354Ssam		 objfile->name,
450178354Ssam		 bfd_section_name (objfile->obfd, lowest_sect),
451178354Ssam		 (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
452178354Ssam
453178354Ssam      if (lowest_sect)
454178354Ssam	addr -= bfd_section_vma (objfile->obfd, lowest_sect);
455178354Ssam    }
456178354Ssam
457178354Ssam  /* Initialize symbol reading routines for this objfile, allow complaints to
458178354Ssam     appear for this new file, and record how verbose to be, then do the
459186102Ssam     initial symbol reading for this file. */
460178354Ssam
461178354Ssam  (*objfile -> sf -> sym_init) (objfile);
462178354Ssam  clear_complaints (1, verbo);
463178354Ssam
464178354Ssam  section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
465178354Ssam  objfile->section_offsets = section_offsets;
466178354Ssam
467178354Ssam#ifndef IBM6000_TARGET
468178354Ssam  /* This is a SVR4/SunOS specific hack, I think.  In any event, it
469178354Ssam     screws RS/6000.  sym_offsets should be doing this sort of thing,
470178354Ssam     because it knows the mapping between bfd sections and
471178354Ssam     section_offsets.  */
472178354Ssam  /* This is a hack.  As far as I can tell, section offsets are not
473178354Ssam     target dependent.  They are all set to addr with a couple of
474178354Ssam     exceptions.  The exceptions are sysvr4 shared libraries, whose
475178354Ssam     offsets are kept in solib structures anyway and rs6000 xcoff
476178354Ssam     which handles shared libraries in a completely unique way.
477178354Ssam
478178354Ssam     Section offsets are built similarly, except that they are built
479178354Ssam     by adding addr in all cases because there is no clear mapping
480178354Ssam     from section_offsets into actual sections.  Note that solib.c
481178354Ssam     has a different algorythm for finding section offsets.
482178354Ssam
483178354Ssam     These should probably all be collapsed into some target
484178354Ssam     independent form of shared library support.  FIXME.  */
485178354Ssam
486178354Ssam  if (addr)
487178354Ssam    {
488178354Ssam      struct obj_section *s;
489178354Ssam
490178354Ssam      for (s = objfile->sections; s < objfile->sections_end; ++s)
491178354Ssam	{
492178354Ssam	  s->addr -= s->offset;
493178354Ssam	  s->addr += addr;
494178354Ssam	  s->endaddr -= s->offset;
495170531Ssam	  s->endaddr += addr;
496170531Ssam	  s->offset += addr;
497170531Ssam	}
498170531Ssam    }
499170531Ssam#endif /* not IBM6000_TARGET */
500170531Ssam
501170531Ssam  (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
502170531Ssam
503170531Ssam  if (!have_partial_symbols () && !have_full_symbols ())
504138593Ssam    {
505138593Ssam      wrap_here ("");
506173275Ssam      printf_filtered ("(no debugging symbols found)...");
507173275Ssam      wrap_here ("");
508138593Ssam    }
509138593Ssam
510138593Ssam  /* Don't allow char * to have a typename (else would get caddr_t).
51177218Sphk     Ditto void *.  FIXME: Check whether this is now done by all the
51277218Sphk     symbol readers themselves (many of them now do), and if so remove
51377218Sphk     it from here.  */
51477218Sphk
515151883Sbrooks  TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
51677218Sphk  TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
51777218Sphk
518121827Sbrooks  /* Mark the objfile has having had initial symbol read attempted.  Note
519178354Ssam     that this does not mean we found any symbols... */
52088748Sambrisko
52188748Sambrisko  objfile -> flags |= OBJF_SYMS;
52288748Sambrisko
52377218Sphk  /* Discard cleanups as symbol reading was successful.  */
52477218Sphk
52577218Sphk  discard_cleanups (old_chain);
526151883Sbrooks
527151883Sbrooks/* Call this after reading in a new symbol table to give target dependant code
52877218Sphk   a crack at the new symbols.  For instance, this could be used to update the
52977218Sphk   values of target-specific symbols GDB needs to keep track of (such as
53077218Sphk   _sigtramp, or whatever).  */
53177218Sphk
532138593Ssam  TARGET_SYMFILE_POSTREAD (objfile);
53377218Sphk}
53477218Sphk
53577218Sphk/* Perform required actions after either reading in the initial
53677218Sphk   symbols for a new objfile, or mapping in the symbols from a reusable
53777218Sphk   objfile. */
53877218Sphk
53977218Sphkvoid
54077218Sphknew_symfile_objfile (objfile, mainline, verbo)
54177218Sphk     struct objfile *objfile;
54277218Sphk     int mainline;
54377218Sphk     int verbo;
54477218Sphk{
545138593Ssam
546170531Ssam  /* If this is the main symbol file we have to clean up all users of the
547170531Ssam     old main symbol file. Otherwise it is sufficient to fixup all the
548170531Ssam     breakpoints that may have been redefined by this symbol file.  */
549170531Ssam  if (mainline)
550170531Ssam    {
551170531Ssam      /* OK, make it the "real" symbol file.  */
552170531Ssam      symfile_objfile = objfile;
553170531Ssam
554170531Ssam      clear_symtab_users ();
555170531Ssam    }
556170531Ssam  else
557170531Ssam    {
558173275Ssam      breakpoint_re_set ();
559138593Ssam    }
560170531Ssam
561170531Ssam  /* We're done reading the symbol file; finish off complaints.  */
562170531Ssam  clear_complaints (0, verbo);
563138593Ssam}
564170531Ssam
565166015Ssam/* Process a symbol file, as either the main file or as a dynamically
566170531Ssam   loaded file.
567170531Ssam
568170531Ssam   NAME is the file name (which will be tilde-expanded and made
569170531Ssam   absolute herein) (but we don't free or modify NAME itself).
570170531Ssam   FROM_TTY says how verbose to be.  MAINLINE specifies whether this
571170531Ssam   is the main symbol file, or whether it's an extra symbol file such
572170531Ssam   as dynamically loaded code.  If !mainline, ADDR is the address
573170531Ssam   where the text segment was loaded.
574170531Ssam
575170531Ssam   Upon success, returns a pointer to the objfile that was added.
576170531Ssam   Upon failure, jumps back to command level (never returns). */
577170531Ssam
578170531Ssamstruct objfile *
579170531Ssamsymbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
580170531Ssam     char *name;
581170531Ssam     int from_tty;
582170531Ssam     CORE_ADDR addr;
583170531Ssam     int mainline;
584170531Ssam     int mapped;
585170531Ssam     int readnow;
586170531Ssam{
587170531Ssam  struct objfile *objfile;
588170531Ssam  struct partial_symtab *psymtab;
589170531Ssam  bfd *abfd;
590170531Ssam
591170531Ssam  /* Open a bfd for the file, and give user a chance to burp if we'd be
592170531Ssam     interactively wiping out any existing symbols.  */
593170531Ssam
594170531Ssam  abfd = symfile_bfd_open (name);
595170531Ssam
596170531Ssam  if ((have_full_symbols () || have_partial_symbols ())
597170531Ssam      && mainline
598170531Ssam      && from_tty
599173275Ssam      && !query ("Load new symbol table from \"%s\"? ", name))
600170531Ssam      error ("Not confirmed.");
601170531Ssam
602170531Ssam  objfile = allocate_objfile (abfd, mapped);
603170531Ssam
604170531Ssam  /* If the objfile uses a mapped symbol file, and we have a psymtab for
605170531Ssam     it, then skip reading any symbols at this time. */
606170531Ssam
607170531Ssam  if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
608166015Ssam    {
609170531Ssam      /* We mapped in an existing symbol table file that already has had
610170531Ssam	 initial symbol reading performed, so we can skip that part.  Notify
611170531Ssam	 the user that instead of reading the symbols, they have been mapped.
612170531Ssam	 */
613170531Ssam      if (from_tty || info_verbose)
614170531Ssam	{
615170531Ssam	  printf_filtered ("Mapped symbols for %s...", name);
616170531Ssam	  wrap_here ("");
617170531Ssam	  gdb_flush (gdb_stdout);
618170531Ssam	}
619170531Ssam      init_entry_point_info (objfile);
620170531Ssam      find_sym_fns (objfile);
621170531Ssam    }
622170531Ssam  else
623170531Ssam    {
624170531Ssam      /* We either created a new mapped symbol table, mapped an existing
625170531Ssam	 symbol table file which has not had initial symbol reading
626170531Ssam	 performed, or need to read an unmapped symbol table. */
627173275Ssam      if (from_tty || info_verbose)
628170531Ssam	{
629165570Ssam	  printf_filtered ("Reading symbols from %s...", name);
630170531Ssam	  wrap_here ("");
631170531Ssam	  gdb_flush (gdb_stdout);
632170531Ssam	}
633170531Ssam      syms_from_objfile (objfile, addr, mainline, from_tty);
634170531Ssam    }
635170531Ssam
636170531Ssam  /* We now have at least a partial symbol table.  Check to see if the
637170531Ssam     user requested that all symbols be read on initial access via either
638170531Ssam     the gdb startup command line or on a per symbol file basis.  Expand
639170531Ssam     all partial symbol tables for this objfile if so. */
640170531Ssam
641170531Ssam  if (readnow || readnow_symbol_files)
642170531Ssam    {
643170531Ssam      if (from_tty || info_verbose)
644170531Ssam	{
645170531Ssam	  printf_filtered ("expanding to full symbols...");
646170531Ssam	  wrap_here ("");
647170531Ssam	  gdb_flush (gdb_stdout);
648170531Ssam	}
649173275Ssam
650173275Ssam      for (psymtab = objfile -> psymtabs;
651173275Ssam	   psymtab != NULL;
652173275Ssam	   psymtab = psymtab -> next)
653173275Ssam	{
654173275Ssam	  psymtab_to_symtab (psymtab);
655173275Ssam	}
656173275Ssam    }
657173275Ssam
658173275Ssam  if (from_tty || info_verbose)
659173275Ssam    {
660173275Ssam      printf_filtered ("done.\n");
661170531Ssam      gdb_flush (gdb_stdout);
662170531Ssam    }
663170531Ssam
664138593Ssam  new_symfile_objfile (objfile, mainline, from_tty);
665138593Ssam
666138593Ssam  return (objfile);
66777218Sphk}
66877218Sphk
669170531Ssam/* This is the symbol-file command.  Read the file, analyze its
670170531Ssam   symbols, and add a struct symtab to a symtab list.  The syntax of
671170531Ssam   the command is rather bizarre--(1) buildargv implements various
672138593Ssam   quoting conventions which are undocumented and have little or
673173275Ssam   nothing in common with the way things are quoted (or not quoted)
674179958Sthompsa   elsewhere in GDB, (2) options are used, which are not generally
675170531Ssam   used in GDB (perhaps "set mapped on", "set readnow on" would be
676170531Ssam   better), (3) the order of options matters, which is contrary to GNU
677179958Sthompsa   conventions (because it is confusing and inconvenient).  */
678181722Sthompsa
679181722Sthompsavoid
680181722Sthompsasymbol_file_command (args, from_tty)
681181722Sthompsa     char *args;
682173275Ssam     int from_tty;
683170531Ssam{
684170531Ssam  char **argv;
685170531Ssam  char *name = NULL;
686170531Ssam  CORE_ADDR text_relocation = 0;		/* text_relocation */
687170531Ssam  struct cleanup *cleanups;
688170531Ssam  int mapped = 0;
689170531Ssam  int readnow = 0;
690170531Ssam
691170531Ssam  dont_repeat ();
69277218Sphk
69377218Sphk  if (args == NULL)
694138593Ssam    {
695178354Ssam      if ((have_full_symbols () || have_partial_symbols ())
696178354Ssam	  && from_tty
697178354Ssam	  && !query ("Discard symbol table from `%s'? ",
698178354Ssam		     symfile_objfile -> name))
699178354Ssam	error ("Not confirmed.");
700178354Ssam      free_all_objfiles ();
701178354Ssam      symfile_objfile = NULL;
702178354Ssam      if (from_tty)
703178354Ssam	{
704178354Ssam	  printf_unfiltered ("No symbol file now.\n");
705178354Ssam	}
706178354Ssam    }
707178354Ssam  else
708178354Ssam    {
709178354Ssam      if ((argv = buildargv (args)) == NULL)
710178354Ssam	{
711178354Ssam	  nomem (0);
712178354Ssam	}
713178354Ssam      cleanups = make_cleanup (freeargv, (char *) argv);
714178354Ssam      while (*argv != NULL)
71577218Sphk	{
71677218Sphk	  if (STREQ (*argv, "-mapped"))
71777218Sphk	    {
71877218Sphk	      mapped = 1;
71991454Sbrooks	    }
72077218Sphk	  else if (STREQ (*argv, "-readnow"))
72191454Sbrooks	    {
72277218Sphk	      readnow = 1;
72391454Sbrooks	    }
72477218Sphk	  else if (**argv == '-')
725138593Ssam	    {
726138593Ssam	      error ("unknown option `%s'", *argv);
727138593Ssam	    }
728138593Ssam	  else
72977218Sphk	    {
730150708Sru            char *p;
73177218Sphk
73277218Sphk              name = *argv;
73377218Sphk
73477218Sphk              /* this is for rombug remote only, to get the text relocation by
73577218Sphk              using link command */
736138593Ssam              p = strrchr(name, '/');
73777218Sphk              if (p != NULL) p++;
73877218Sphk              else p = name;
73977218Sphk
74077218Sphk              target_link(p, &text_relocation);
74191454Sbrooks
74277218Sphk              if (text_relocation == (CORE_ADDR)0)
74391454Sbrooks                return;
74477218Sphk              else if (text_relocation == (CORE_ADDR)-1)
74591454Sbrooks                symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
74677218Sphk				 readnow);
74791454Sbrooks              else
74877218Sphk                symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
74991454Sbrooks				 0, mapped, readnow);
75077218Sphk
75177218Sphk	      /* Getting new symbols may change our opinion about what is
752150708Sru		 frameless.  */
75377218Sphk	      reinit_frame_cache ();
75477218Sphk
75577218Sphk              set_initial_language ();
75677218Sphk	    }
75777218Sphk	  argv++;
758138593Ssam	}
75977218Sphk
76077218Sphk      if (name == NULL)
76177218Sphk	{
76277218Sphk	  error ("no symbol file name was specified");
76377218Sphk	}
76477218Sphk      do_cleanups (cleanups);
76577218Sphk    }
76677218Sphk}
76777218Sphk
76877218Sphk/* Set the initial language.
769138593Ssam
77077218Sphk   A better solution would be to record the language in the psymtab when reading
77177218Sphk   partial symbols, and then use it (if known) to set the language.  This would
77277218Sphk   be a win for formats that encode the language in an easily discoverable place,
77377218Sphk   such as DWARF.  For stabs, we can jump through hoops looking for specially
77477218Sphk   named symbols or try to intuit the language from the specific type of stabs
775138593Ssam   we find, but we can't do that until later when we read in full symbols.
77677218Sphk   FIXME.  */
77777218Sphk
77877218Sphkstatic void
77977218Sphkset_initial_language ()
78091454Sbrooks{
78177218Sphk  struct partial_symtab *pst;
78291454Sbrooks  enum language lang = language_unknown;
78377218Sphk
78491454Sbrooks  pst = find_main_psymtab ();
78577218Sphk  if (pst != NULL)
78677218Sphk    {
787150708Sru      if (pst -> filename != NULL)
78877218Sphk	{
78977218Sphk	  lang = deduce_language_from_filename (pst -> filename);
79077218Sphk        }
79177218Sphk      if (lang == language_unknown)
79277218Sphk	{
793138593Ssam	    /* Make C the default language */
79477218Sphk	    lang = language_c;
79577218Sphk	}
79677218Sphk      set_language (lang);
79777218Sphk      expected_language = current_language;	/* Don't warn the user */
79877218Sphk    }
799139493Ssam}
800139493Ssam
801139493Ssam/* Open file specified by NAME and hand it off to BFD for preliminary
802139493Ssam   analysis.  Result is a newly initialized bfd *, which includes a newly
803139493Ssam   malloc'd` copy of NAME (tilde-expanded and made absolute).
804139493Ssam   In case of trouble, error() is called.  */
805138593Ssam
80677218Sphkstatic bfd *
80777218Sphksymfile_bfd_open (name)
808139493Ssam     char *name;
809139493Ssam{
810139493Ssam  bfd *sym_bfd;
811139493Ssam  int desc;
81277218Sphk  char *absolute_name;
81377218Sphk
814138593Ssam  name = tilde_expand (name);	/* Returns 1st new malloc'd copy */
81577218Sphk
81677218Sphk  /* Look down path for it, allocate 2nd new malloc'd copy.  */
81777218Sphk  desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
81877218Sphk  if (desc < 0)
819120178Ssam    {
82077218Sphk      make_cleanup (free, name);
821178354Ssam      perror_with_name (name);
82277218Sphk    }
82377218Sphk  free (name);			/* Free 1st new malloc'd copy */
82477218Sphk  name = absolute_name;		/* Keep 2nd malloc'd copy in bfd */
82577218Sphk				/* It'll be freed in free_objfile(). */
82677218Sphk
82777218Sphk  sym_bfd = bfd_fdopenr (name, gnutarget, desc);
82877218Sphk  if (!sym_bfd)
82977218Sphk    {
83077218Sphk      close (desc);
83177218Sphk      make_cleanup (free, name);
83277218Sphk      error ("\"%s\": can't open to read symbols: %s.", name,
83377218Sphk	     bfd_errmsg (bfd_get_error ()));
834148686Sstefanf    }
835148686Sstefanf  sym_bfd->cacheable = true;
83677218Sphk
83777218Sphk  if (!bfd_check_format (sym_bfd, bfd_object))
838138593Ssam    {
83977218Sphk      /* FIXME: should be checking for errors from bfd_close (for one thing,
84077218Sphk	 on error it does not free all the storage associated with the
84177218Sphk	 bfd).  */
84277218Sphk      bfd_close (sym_bfd);	/* This also closes desc */
843120178Ssam      make_cleanup (free, name);
84477218Sphk      error ("\"%s\": can't read symbols: %s.", name,
84577218Sphk	     bfd_errmsg (bfd_get_error ()));
84677218Sphk    }
847178354Ssam
84877218Sphk  return (sym_bfd);
84977218Sphk}
85077218Sphk
85191454Sbrooks/* Link a new symtab_fns into the global symtab_fns list.  Called on gdb
85277218Sphk   startup by the _initialize routine in each object file format reader,
85377218Sphk   to register information about each format the the reader is prepared
85477218Sphk   to handle. */
855151827Sbrooks
856151827Sbrooksvoid
85777218Sphkadd_symtab_fns (sf)
85877218Sphk     struct sym_fns *sf;
85977218Sphk{
86077218Sphk  sf->next = symtab_fns;
86177218Sphk  symtab_fns = sf;
86277218Sphk}
86377218Sphk
86477218Sphk
86577218Sphk/* Initialize to read symbols from the symbol file sym_bfd.  It either
86677218Sphk   returns or calls error().  The result is an initialized struct sym_fns
86777218Sphk   in the objfile structure, that contains cached information about the
86877218Sphk   symbol file.  */
86991454Sbrooks
87077218Sphkstatic void
87177218Sphkfind_sym_fns (objfile)
87277218Sphk     struct objfile *objfile;
87377218Sphk{
87477218Sphk  struct sym_fns *sf;
87577218Sphk  enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
876138593Ssam  char *our_target = bfd_get_target (objfile -> obfd);
877127649Ssam
878127649Ssam  /* Special kludge for RS/6000 and PowerMac.  See xcoffread.c.  */
879148416Ssam  if (STREQ (our_target, "aixcoff-rs6000") ||
880148416Ssam      STREQ (our_target, "xcoff-powermac"))
881127649Ssam    our_flavour = (enum bfd_flavour)-1;
882127649Ssam
883138593Ssam  /* Special kludge for apollo.  See dstread.c.  */
884127649Ssam  if (STREQN (our_target, "apollo", 6))
885127649Ssam    our_flavour = (enum bfd_flavour)-2;
886127649Ssam
887127649Ssam  for (sf = symtab_fns; sf != NULL; sf = sf -> next)
888127649Ssam    {
889127649Ssam      if (our_flavour == sf -> sym_flavour)
890127649Ssam	{
891127649Ssam	  objfile -> sf = sf;
892173275Ssam	  return;
893127649Ssam	}
894127649Ssam    }
895150708Sru  error ("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown.",
896127649Ssam	 bfd_get_target (objfile -> obfd));
897127649Ssam}
898127649Ssam
899127649Ssam/* This function runs the load command of our current target.  */
900127649Ssam
901138593Ssamstatic void
902173275Ssamload_command (arg, from_tty)
903173275Ssam     char *arg;
904173275Ssam     int from_tty;
905173275Ssam{
906173275Ssam  if (arg == NULL)
907173275Ssam    arg = get_exec_file (1);
908173275Ssam  target_load (arg, from_tty);
909173275Ssam}
910173275Ssam
911173275Ssam/* This version of "load" should be usable for any target.  Currently
912173275Ssam   it is just used for remote targets, not inftarg.c or core files,
913173275Ssam   on the theory that only in that case is it useful.
914173275Ssam
915173275Ssam   Avoiding xmodem and the like seems like a win (a) because we don't have
916173275Ssam   to worry about finding it, and (b) On VMS, fork() is very slow and so
917173275Ssam   we don't want to run a subprocess.  On the other hand, I'm not sure how
918127649Ssam   performance compares.  */
919127649Ssamvoid
920173275Ssamgeneric_load (filename, from_tty)
921173275Ssam    char *filename;
922173275Ssam    int from_tty;
923173275Ssam{
924173275Ssam  struct cleanup *old_cleanups;
925173275Ssam  asection *s;
926173275Ssam  bfd *loadfile_bfd;
927127649Ssam  time_t start_time, end_time;	/* Start and end times of download */
928127649Ssam  unsigned long data_count;	/* Number of bytes transferred to memory */
929138593Ssam
930138593Ssam  loadfile_bfd = bfd_openr (filename, gnutarget);
931138593Ssam  if (loadfile_bfd == NULL)
932138593Ssam    {
933138593Ssam      perror_with_name (filename);
934138593Ssam      return;
93577218Sphk    }
936138593Ssam  /* FIXME: should be checking for errors from bfd_close (for one thing,
93777218Sphk     on error it does not free all the storage associated with the
938138593Ssam     bfd).  */
939138593Ssam  old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
940138593Ssam
941138593Ssam  if (!bfd_check_format (loadfile_bfd, bfd_object))
942138593Ssam    {
943138593Ssam      error ("\"%s\" is not an object file: %s", filename,
944138593Ssam	     bfd_errmsg (bfd_get_error ()));
945150708Sru    }
946138593Ssam
947138593Ssam  start_time = time (NULL);
948138593Ssam
949138593Ssam  for (s = loadfile_bfd->sections; s; s = s->next)
950138593Ssam    {
951138593Ssam      if (s->flags & SEC_LOAD)
952138593Ssam	{
953138593Ssam	  bfd_size_type size;
954138593Ssam
955138593Ssam	  size = bfd_get_section_size_before_reloc (s);
956138593Ssam	  if (size > 0)
957138593Ssam	    {
958138593Ssam	      char *buffer;
959138593Ssam	      struct cleanup *old_chain;
960138593Ssam	      bfd_vma vma;
961138593Ssam
962138593Ssam	      data_count += size;
963138593Ssam
964138593Ssam	      buffer = xmalloc (size);
965138593Ssam	      old_chain = make_cleanup (free, buffer);
966138593Ssam
967138593Ssam	      vma = bfd_get_section_vma (loadfile_bfd, s);
968138593Ssam
969170531Ssam	      /* Is this really necessary?  I guess it gives the user something
970170531Ssam		 to look at during a long download.  */
971170531Ssam	      printf_filtered ("Loading section %s, size 0x%lx vma ",
972170531Ssam			       bfd_get_section_name (loadfile_bfd, s),
973170531Ssam			       (unsigned long) size);
974170531Ssam	      print_address_numeric (vma, 1, gdb_stdout);
975170531Ssam	      printf_filtered ("\n");
976170531Ssam
977170531Ssam	      bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
978170531Ssam
979170531Ssam	      target_write_memory (vma, buffer, size);
980170531Ssam
981138593Ssam	      do_cleanups (old_chain);
982138593Ssam	    }
983138593Ssam	}
984138593Ssam    }
985138593Ssam
986138593Ssam  end_time = time (NULL);
987138593Ssam
988138593Ssam  /* We were doing this in remote-mips.c, I suspect it is right
989138593Ssam     for other targets too.  */
990138593Ssam  write_pc (loadfile_bfd->start_address);
991138593Ssam
992138593Ssam  /* FIXME: are we supposed to call symbol_file_add or not?  According to
993138593Ssam     a comment from remote-mips.c (where a call to symbol_file_add was
994173275Ssam     commented out), making the call confuses GDB if more than one file is
995138593Ssam     loaded in.  remote-nindy.c had no call to symbol_file_add, but remote-vx.c
996138593Ssam     does.  */
997138593Ssam
998138593Ssam  if (end_time != start_time)
999138593Ssam   printf_filtered ("Transfer rate: %d bits/sec.\n",
1000138593Ssam                    (data_count * 8)/(end_time - start_time));
1001138593Ssam
1002146873Sjhb  do_cleanups (old_cleanups);
1003138593Ssam}
1004138593Ssam
1005138593Ssam/* This function allows the addition of incrementally linked object files.
1006138593Ssam   It does not modify any state in the target, only in the debugger.  */
1007138593Ssam
1008146873Sjhb/* ARGSUSED */
1009138593Ssamstatic void
1010138593Ssamadd_symbol_file_command (args, from_tty)
1011146873Sjhb     char *args;
1012138593Ssam     int from_tty;
1013138593Ssam{
1014138593Ssam  char *name = NULL;
1015138593Ssam  CORE_ADDR text_addr;
1016138593Ssam  char *arg;
1017138593Ssam  int readnow = 0;
1018138593Ssam  int mapped = 0;
1019138593Ssam
1020138593Ssam  dont_repeat ();
1021138593Ssam
1022173275Ssam  if (args == NULL)
1023173275Ssam    {
1024138593Ssam      error ("add-symbol-file takes a file name and an address");
1025173275Ssam    }
1026138593Ssam
1027138593Ssam  /* Make a copy of the string that we can safely write into. */
1028138593Ssam
1029170531Ssam  args = strdup (args);
1030138593Ssam  make_cleanup (free, args);
1031138593Ssam
1032138593Ssam  /* Pick off any -option args and the file name. */
1033138593Ssam
1034138593Ssam  while ((*args != '\000') && (name == NULL))
1035138593Ssam    {
1036138593Ssam      while (isspace (*args)) {args++;}
1037138593Ssam      arg = args;
1038138593Ssam      while ((*args != '\000') && !isspace (*args)) {args++;}
1039138593Ssam      if (*args != '\000')
1040138593Ssam	{
1041155702Ssam	  *args++ = '\000';
1042138593Ssam	}
1043138593Ssam      if (*arg != '-')
1044138593Ssam	{
1045138593Ssam	  name = arg;
1046138593Ssam	}
1047138593Ssam      else if (STREQ (arg, "-mapped"))
1048138593Ssam	{
1049138593Ssam	  mapped = 1;
1050138593Ssam	}
1051138593Ssam      else if (STREQ (arg, "-readnow"))
1052138593Ssam	{
1053138593Ssam	  readnow = 1;
1054138593Ssam	}
1055138593Ssam      else
1056138593Ssam	{
1057138593Ssam	  error ("unknown option `%s'", arg);
1058138593Ssam	}
1059138593Ssam    }
1060138593Ssam
1061138593Ssam  /* After picking off any options and the file name, args should be
1062138593Ssam     left pointing at the remainder of the command line, which should
1063138593Ssam     be the address expression to evaluate. */
1064138593Ssam
1065138593Ssam  if (name == NULL)
1066138593Ssam    {
1067138593Ssam      error ("add-symbol-file takes a file name");
1068138593Ssam    }
1069138593Ssam  name = tilde_expand (name);
1070138593Ssam  make_cleanup (free, name);
1071138593Ssam
1072138593Ssam  if (*args != '\000')
1073138593Ssam    {
1074138593Ssam      text_addr = parse_and_eval_address (args);
1075138593Ssam    }
1076138593Ssam  else
1077138593Ssam    {
1078138593Ssam      target_link(name, &text_addr);
1079138593Ssam      if (text_addr == (CORE_ADDR)-1)
1080138593Ssam	error("Don't know how to get text start location for this file");
1081138593Ssam    }
1082138593Ssam
1083138593Ssam  /* FIXME-32x64: Assumes text_addr fits in a long.  */
1084138593Ssam  if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
1085138593Ssam	      name, local_hex_string ((unsigned long)text_addr)))
1086138593Ssam    error ("Not confirmed.");
1087138593Ssam
1088138593Ssam  symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
1089138593Ssam
1090138593Ssam  /* Getting new symbols may change our opinion about what is
1091138593Ssam     frameless.  */
1092138593Ssam  reinit_frame_cache ();
1093138593Ssam}
1094138593Ssam
1095138593Ssamstatic void
1096138593Ssamadd_shared_symbol_files_command  (args, from_tty)
1097138593Ssam     char *args;
1098138593Ssam     int from_tty;
1099138593Ssam{
1100148621Ssam#ifdef ADD_SHARED_SYMBOL_FILES
1101138593Ssam  ADD_SHARED_SYMBOL_FILES (args, from_tty);
1102148621Ssam#else
1103138593Ssam  error ("This command is not available in this configuration of GDB.");
1104148621Ssam#endif
1105148621Ssam}
1106148621Ssam
1107148621Ssam/* Re-read symbols if a symbol-file has changed.  */
1108148621Ssamvoid
1109138593Ssamreread_symbols ()
1110138593Ssam{
1111148621Ssam  struct objfile *objfile;
1112138593Ssam  long new_modtime;
1113148621Ssam  int reread_one = 0;
1114138593Ssam  struct stat new_statbuf;
1115148621Ssam  int res;
1116148621Ssam
1117148621Ssam  /* With the addition of shared libraries, this should be modified,
1118148621Ssam     the load time should be saved in the partial symbol tables, since
1119148621Ssam     different tables may come from different source files.  FIXME.
1120138593Ssam     This routine should then walk down each partial symbol table
1121138593Ssam     and see if the symbol table that it originates from has been changed */
1122138593Ssam
1123138593Ssam  for (objfile = object_files; objfile; objfile = objfile->next) {
1124138593Ssam    if (objfile->obfd) {
1125138593Ssam#ifdef IBM6000_TARGET
1126138593Ssam     /* If this object is from a shared library, then you should
1127138593Ssam        stat on the library name, not member name. */
1128138593Ssam
1129138593Ssam     if (objfile->obfd->my_archive)
1130138593Ssam       res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1131138593Ssam     else
1132138593Ssam#endif
1133138593Ssam      res = stat (objfile->name, &new_statbuf);
1134138593Ssam      if (res != 0) {
1135138593Ssam	/* FIXME, should use print_sys_errmsg but it's not filtered. */
1136138593Ssam	printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1137138593Ssam			 objfile->name);
1138138593Ssam	continue;
1139138593Ssam      }
1140138593Ssam      new_modtime = new_statbuf.st_mtime;
1141138593Ssam      if (new_modtime != objfile->mtime)
1142138593Ssam	{
1143138593Ssam	  struct cleanup *old_cleanups;
1144138593Ssam	  struct section_offsets *offsets;
1145138593Ssam	  int num_offsets;
1146138593Ssam	  int section_offsets_size;
1147138593Ssam	  char *obfd_filename;
1148138593Ssam
1149138593Ssam	  printf_filtered ("`%s' has changed; re-reading symbols.\n",
1150138593Ssam			   objfile->name);
1151138593Ssam
1152138593Ssam	  /* There are various functions like symbol_file_add,
1153138593Ssam	     symfile_bfd_open, syms_from_objfile, etc., which might
1154138593Ssam	     appear to do what we want.  But they have various other
1155138593Ssam	     effects which we *don't* want.  So we just do stuff
1156138593Ssam	     ourselves.  We don't worry about mapped files (for one thing,
1157138593Ssam	     any mapped file will be out of date).  */
1158138593Ssam
1159138593Ssam	  /* If we get an error, blow away this objfile (not sure if
1160138593Ssam	     that is the correct response for things like shared
1161138593Ssam	     libraries).  */
1162138593Ssam	  old_cleanups = make_cleanup (free_objfile, objfile);
1163138593Ssam	  /* We need to do this whenever any symbols go away.  */
1164138593Ssam	  make_cleanup (clear_symtab_users, 0);
1165138593Ssam
1166138593Ssam	  /* Clean up any state BFD has sitting around.  We don't need
1167155702Ssam	     to close the descriptor but BFD lacks a way of closing the
1168138593Ssam	     BFD without closing the descriptor.  */
1169138593Ssam	  obfd_filename = bfd_get_filename (objfile->obfd);
1170138593Ssam	  if (!bfd_close (objfile->obfd))
1171138593Ssam	    error ("Can't close BFD for %s: %s", objfile->name,
1172138593Ssam		   bfd_errmsg (bfd_get_error ()));
1173138593Ssam	  objfile->obfd = bfd_openr (obfd_filename, gnutarget);
1174138593Ssam	  if (objfile->obfd == NULL)
1175138593Ssam	    error ("Can't open %s to read symbols.", objfile->name);
1176138593Ssam	  /* bfd_openr sets cacheable to true, which is what we want.  */
1177138593Ssam	  if (!bfd_check_format (objfile->obfd, bfd_object))
1178138593Ssam	    error ("Can't read symbols from %s: %s.", objfile->name,
1179138593Ssam		   bfd_errmsg (bfd_get_error ()));
1180138593Ssam
1181138593Ssam	  /* Save the offsets, we will nuke them with the rest of the
1182138593Ssam	     psymbol_obstack.  */
1183138593Ssam	  num_offsets = objfile->num_sections;
1184138593Ssam	  section_offsets_size =
1185138593Ssam	    sizeof (struct section_offsets)
1186138593Ssam	      + sizeof (objfile->section_offsets->offsets) * num_offsets;
1187138593Ssam	  offsets = (struct section_offsets *) alloca (section_offsets_size);
1188138593Ssam	  memcpy (offsets, objfile->section_offsets, section_offsets_size);
1189138593Ssam
1190138593Ssam	  /* Nuke all the state that we will re-read.  Much of the following
1191138593Ssam	     code which sets things to NULL really is necessary to tell
1192138593Ssam	     other parts of GDB that there is nothing currently there.  */
1193149029Ssam
1194149029Ssam	  /* FIXME: Do we have to free a whole linked list, or is this
1195149029Ssam	     enough?  */
1196149029Ssam	  if (objfile->global_psymbols.list)
1197149029Ssam	    mfree (objfile->md, objfile->global_psymbols.list);
1198149029Ssam	  memset (&objfile -> global_psymbols, 0,
1199155702Ssam		  sizeof (objfile -> global_psymbols));
1200149029Ssam	  if (objfile->static_psymbols.list)
1201149029Ssam	    mfree (objfile->md, objfile->static_psymbols.list);
1202149029Ssam	  memset (&objfile -> static_psymbols, 0,
1203149029Ssam		  sizeof (objfile -> static_psymbols));
1204149029Ssam
1205149029Ssam	  /* Free the obstacks for non-reusable objfiles */
1206149029Ssam	  obstack_free (&objfile -> psymbol_cache.cache, 0);
1207149029Ssam	  memset (&objfile -> psymbol_cache, 0,
1208149029Ssam		  sizeof (objfile -> psymbol_cache));
1209149029Ssam	  obstack_free (&objfile -> psymbol_obstack, 0);
1210149029Ssam	  obstack_free (&objfile -> symbol_obstack, 0);
1211149029Ssam	  obstack_free (&objfile -> type_obstack, 0);
1212149029Ssam	  objfile->sections = NULL;
1213170531Ssam	  objfile->symtabs = NULL;
1214149029Ssam	  objfile->psymtabs = NULL;
1215149029Ssam	  objfile->free_psymtabs = NULL;
1216149029Ssam	  objfile->msymbols = NULL;
1217138593Ssam	  objfile->minimal_symbol_count= 0;
1218138593Ssam	  objfile->fundamental_types = NULL;
1219138593Ssam	  if (objfile -> sf != NULL)
1220138593Ssam	    {
1221138593Ssam	      (*objfile -> sf -> sym_finish) (objfile);
1222147795Ssam	    }
1223147795Ssam
1224147795Ssam	  /* We never make this a mapped file.  */
1225147795Ssam	  objfile -> md = NULL;
1226147795Ssam	  /* obstack_specify_allocation also initializes the obstack so
1227147795Ssam	     it is empty.  */
1228153422Ssam	  obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
1229170531Ssam				      xmalloc, free);
1230153422Ssam	  obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1231170531Ssam				      xmalloc, free);
1232153422Ssam	  obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1233153422Ssam				      xmalloc, free);
1234148416Ssam	  obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1235170531Ssam				      xmalloc, free);
1236170531Ssam	  if (build_objfile_section_table (objfile))
1237170531Ssam	    {
1238170531Ssam	      error ("Can't find the file sections in `%s': %s",
1239170531Ssam		     objfile -> name, bfd_errmsg (bfd_get_error ()));
1240170531Ssam	    }
1241170531Ssam
1242170531Ssam	  /* We use the same section offsets as from last time.  I'm not
1243170531Ssam	     sure whether that is always correct for shared libraries.  */
1244170531Ssam	  objfile->section_offsets = (struct section_offsets *)
1245170531Ssam	    obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1246170531Ssam	  memcpy (objfile->section_offsets, offsets, section_offsets_size);
1247170531Ssam	  objfile->num_sections = num_offsets;
1248170531Ssam
1249170531Ssam	  /* What the hell is sym_new_init for, anyway?  The concept of
1250170531Ssam	     distinguishing between the main file and additional files
1251170531Ssam	     in this way seems rather dubious.  */
1252178354Ssam	  if (objfile == symfile_objfile)
1253178354Ssam	    (*objfile->sf->sym_new_init) (objfile);
1254178354Ssam
1255178354Ssam	  (*objfile->sf->sym_init) (objfile);
1256178354Ssam	  clear_complaints (1, 1);
1257178354Ssam	  /* The "mainline" parameter is a hideous hack; I think leaving it
1258178354Ssam	     zero is OK since dbxread.c also does what it needs to do if
1259178354Ssam	     objfile->global_psymbols.size is 0.  */
1260178354Ssam	  (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
1261178354Ssam	  if (!have_partial_symbols () && !have_full_symbols ())
1262178354Ssam	    {
1263178354Ssam	      wrap_here ("");
1264170531Ssam	      printf_filtered ("(no debugging symbols found)\n");
1265178354Ssam	      wrap_here ("");
1266178354Ssam	    }
1267178354Ssam	  objfile -> flags |= OBJF_SYMS;
1268178354Ssam
1269178354Ssam	  /* We're done reading the symbol file; finish off complaints.  */
1270178354Ssam	  clear_complaints (0, 1);
1271178354Ssam
1272178354Ssam	  /* Getting new symbols may change our opinion about what is
1273178354Ssam	     frameless.  */
1274178354Ssam
1275178354Ssam	  reinit_frame_cache ();
1276178354Ssam
1277178354Ssam	  /* Discard cleanups as symbol reading was successful.  */
1278178354Ssam	  discard_cleanups (old_cleanups);
1279178354Ssam
1280178354Ssam	  /* If the mtime has changed between the time we set new_modtime
1281178354Ssam	     and now, we *want* this to be out of date, so don't call stat
1282178354Ssam	     again now.  */
1283178354Ssam	  objfile->mtime = new_modtime;
1284178354Ssam	  reread_one = 1;
1285178354Ssam
1286178354Ssam	  /* Call this after reading in a new symbol table to give target
1287178354Ssam	     dependant code a crack at the new symbols.  For instance, this
1288178354Ssam	     could be used to update the values of target-specific symbols GDB
1289178354Ssam	     needs to keep track of (such as _sigtramp, or whatever).  */
1290178354Ssam
1291178354Ssam	  TARGET_SYMFILE_POSTREAD (objfile);
1292178354Ssam	}
1293178354Ssam    }
1294178354Ssam  }
1295178354Ssam
1296178354Ssam  if (reread_one)
1297178354Ssam    clear_symtab_users ();
1298178354Ssam}
1299178354Ssam
1300178354Ssam
1301178354Ssamenum language
1302178354Ssamdeduce_language_from_filename (filename)
1303178354Ssam     char *filename;
1304178354Ssam{
1305178354Ssam  char *c;
1306178354Ssam
1307178354Ssam  if (0 == filename)
1308178354Ssam    ; /* Get default */
1309170531Ssam  else if (0 == (c = strrchr (filename, '.')))
1310170531Ssam    ; /* Get default. */
1311178354Ssam  else if (STREQ (c, ".c"))
1312178354Ssam    return language_c;
1313178354Ssam  else if (STREQ (c, ".cc") || STREQ (c, ".C") || STREQ (c, ".cxx")
1314178354Ssam	   || STREQ (c, ".cpp") || STREQ (c, ".cp") || STREQ (c, ".c++"))
1315178354Ssam    return language_cplus;
1316178354Ssam  else if (STREQ (c, ".ch") || STREQ (c, ".c186") || STREQ (c, ".c286"))
1317178354Ssam    return language_chill;
1318178354Ssam  else if (STREQ (c, ".f") || STREQ (c, ".F"))
1319178354Ssam    return language_fortran;
1320178354Ssam  else if (STREQ (c, ".mod"))
1321178354Ssam    return language_m2;
1322178354Ssam  else if (STREQ (c, ".s") || STREQ (c, ".S"))
1323178354Ssam    return language_asm;
1324178354Ssam
1325178354Ssam  return language_unknown;		/* default */
1326178354Ssam}
1327178354Ssam
1328178354Ssam/* allocate_symtab:
1329178354Ssam
1330178354Ssam   Allocate and partly initialize a new symbol table.  Return a pointer
1331178354Ssam   to it.  error() if no space.
1332178354Ssam
1333178354Ssam   Caller must set these fields:
1334178354Ssam	LINETABLE(symtab)
1335178354Ssam	symtab->blockvector
1336178354Ssam	symtab->dirname
1337178354Ssam	symtab->free_code
1338178354Ssam	symtab->free_ptr
1339178354Ssam	initialize any EXTRA_SYMTAB_INFO
1340178354Ssam	possibly free_named_symtabs (symtab->filename);
1341178354Ssam */
1342178354Ssam
1343178354Ssamstruct symtab *
1344178354Ssamallocate_symtab (filename, objfile)
1345178354Ssam     char *filename;
1346178354Ssam     struct objfile *objfile;
1347178354Ssam{
1348178354Ssam  register struct symtab *symtab;
1349178354Ssam
1350178354Ssam  symtab = (struct symtab *)
1351178354Ssam    obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
1352178354Ssam  memset (symtab, 0, sizeof (*symtab));
1353178354Ssam  symtab -> filename = obsavestring (filename, strlen (filename),
1354178354Ssam				     &objfile -> symbol_obstack);
1355178354Ssam  symtab -> fullname = NULL;
1356178354Ssam  symtab -> language = deduce_language_from_filename (filename);
1357178354Ssam
1358178354Ssam  /* Hook it to the objfile it comes from */
1359178354Ssam
1360178354Ssam  symtab -> objfile = objfile;
1361178354Ssam  symtab -> next = objfile -> symtabs;
1362178354Ssam  objfile -> symtabs = symtab;
1363178354Ssam
1364178354Ssam#ifdef INIT_EXTRA_SYMTAB_INFO
1365178354Ssam  INIT_EXTRA_SYMTAB_INFO (symtab);
1366178354Ssam#endif
1367178354Ssam
1368178354Ssam  return (symtab);
1369178354Ssam}
1370178354Ssam
1371178354Ssamstruct partial_symtab *
1372178354Ssamallocate_psymtab (filename, objfile)
1373178354Ssam     char *filename;
1374178354Ssam     struct objfile *objfile;
1375178354Ssam{
1376178354Ssam  struct partial_symtab *psymtab;
1377178354Ssam
1378178354Ssam  if (objfile -> free_psymtabs)
1379178354Ssam    {
1380178354Ssam      psymtab = objfile -> free_psymtabs;
1381178354Ssam      objfile -> free_psymtabs = psymtab -> next;
1382178354Ssam    }
1383178354Ssam  else
1384178354Ssam    psymtab = (struct partial_symtab *)
1385178354Ssam      obstack_alloc (&objfile -> psymbol_obstack,
1386178354Ssam		     sizeof (struct partial_symtab));
1387178354Ssam
1388178354Ssam  memset (psymtab, 0, sizeof (struct partial_symtab));
1389178354Ssam  psymtab -> filename = obsavestring (filename, strlen (filename),
1390178354Ssam				      &objfile -> psymbol_obstack);
1391178354Ssam  psymtab -> symtab = NULL;
1392178354Ssam
1393178354Ssam  /* Hook it to the objfile it comes from */
1394178354Ssam
1395178354Ssam  psymtab -> objfile = objfile;
1396178354Ssam  psymtab -> next = objfile -> psymtabs;
1397178354Ssam  objfile -> psymtabs = psymtab;
1398178354Ssam
1399178354Ssam  return (psymtab);
1400178354Ssam}
1401178354Ssam
1402178354Ssam
1403178354Ssam/* Reset all data structures in gdb which may contain references to symbol
1404178354Ssam   table date.  */
1405178354Ssam
1406178354Ssamvoid
1407178354Ssamclear_symtab_users ()
1408178354Ssam{
1409170531Ssam  /* Someday, we should do better than this, by only blowing away
1410178354Ssam     the things that really need to be blown.  */
1411170531Ssam  clear_value_history ();
1412178354Ssam  clear_displays ();
1413178354Ssam  clear_internalvars ();
1414178354Ssam  breakpoint_re_set ();
1415178354Ssam  set_default_breakpoint (0, 0, 0, 0);
1416178354Ssam  current_source_symtab = 0;
1417178354Ssam  current_source_line = 0;
1418178354Ssam  clear_pc_function_cache ();
1419178354Ssam}
1420178354Ssam
1421178354Ssam/* clear_symtab_users_once:
1422178354Ssam
1423178354Ssam   This function is run after symbol reading, or from a cleanup.
1424178354Ssam   If an old symbol table was obsoleted, the old symbol table
1425178354Ssam   has been blown away, but the other GDB data structures that may
1426170531Ssam   reference it have not yet been cleared or re-directed.  (The old
1427170531Ssam   symtab was zapped, and the cleanup queued, in free_named_symtab()
1428170531Ssam   below.)
1429178354Ssam
1430170531Ssam   This function can be queued N times as a cleanup, or called
1431178354Ssam   directly; it will do all the work the first time, and then will be a
1432178354Ssam   no-op until the next time it is queued.  This works by bumping a
1433178354Ssam   counter at queueing time.  Much later when the cleanup is run, or at
1434178354Ssam   the end of symbol processing (in case the cleanup is discarded), if
1435178354Ssam   the queued count is greater than the "done-count", we do the work
1436178354Ssam   and set the done-count to the queued count.  If the queued count is
1437178354Ssam   less than or equal to the done-count, we just ignore the call.  This
1438178354Ssam   is needed because reading a single .o file will often replace many
1439178354Ssam   symtabs (one per .h file, for example), and we don't want to reset
1440178354Ssam   the breakpoints N times in the user's face.
1441170531Ssam
1442170531Ssam   The reason we both queue a cleanup, and call it directly after symbol
1443170531Ssam   reading, is because the cleanup protects us in case of errors, but is
1444178354Ssam   discarded if symbol reading is successful.  */
1445170531Ssam
1446178354Ssam#if 0
1447178354Ssam/* FIXME:  As free_named_symtabs is currently a big noop this function
1448178354Ssam   is no longer needed.  */
1449178354Ssamstatic void
1450178354Ssamclear_symtab_users_once PARAMS ((void));
1451178354Ssam
1452178354Ssamstatic int clear_symtab_users_queued;
1453178354Ssamstatic int clear_symtab_users_done;
1454178354Ssam
1455178354Ssamstatic void
1456170531Ssamclear_symtab_users_once ()
1457170531Ssam{
1458170531Ssam  /* Enforce once-per-`do_cleanups'-semantics */
1459178354Ssam  if (clear_symtab_users_queued <= clear_symtab_users_done)
1460170531Ssam    return;
1461178354Ssam  clear_symtab_users_done = clear_symtab_users_queued;
1462178354Ssam
1463178354Ssam  clear_symtab_users ();
1464178354Ssam}
1465178354Ssam#endif
1466178354Ssam
1467178354Ssam/* Delete the specified psymtab, and any others that reference it.  */
1468178354Ssam
1469178354Ssamstatic void
1470178354Ssamcashier_psymtab (pst)
1471170531Ssam     struct partial_symtab *pst;
1472170531Ssam{
1473170531Ssam  struct partial_symtab *ps, *pprev = NULL;
1474178354Ssam  int i;
1475170531Ssam
1476178354Ssam  /* Find its previous psymtab in the chain */
1477178354Ssam  for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1478178354Ssam    if (ps == pst)
1479178354Ssam      break;
1480178354Ssam    pprev = ps;
1481178354Ssam  }
1482178354Ssam
1483178354Ssam  if (ps) {
1484178354Ssam    /* Unhook it from the chain.  */
1485178354Ssam    if (ps == pst->objfile->psymtabs)
1486178354Ssam      pst->objfile->psymtabs = ps->next;
1487178354Ssam    else
1488178354Ssam      pprev->next = ps->next;
1489178354Ssam
1490178354Ssam    /* FIXME, we can't conveniently deallocate the entries in the
1491178354Ssam       partial_symbol lists (global_psymbols/static_psymbols) that
1492178354Ssam       this psymtab points to.  These just take up space until all
1493178354Ssam       the psymtabs are reclaimed.  Ditto the dependencies list and
1494178354Ssam       filename, which are all in the psymbol_obstack.  */
1495178354Ssam
1496178354Ssam    /* We need to cashier any psymtab that has this one as a dependency... */
1497170531Ssamagain:
1498170531Ssam    for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1499170531Ssam      for (i = 0; i < ps->number_of_dependencies; i++) {
1500178354Ssam	if (ps->dependencies[i] == pst) {
1501153354Ssam	  cashier_psymtab (ps);
1502178354Ssam	  goto again;		/* Must restart, chain has been munged. */
1503178354Ssam	}
1504178354Ssam      }
1505178354Ssam    }
1506178354Ssam  }
1507178354Ssam}
1508178354Ssam
1509178354Ssam/* If a symtab or psymtab for filename NAME is found, free it along
1510178354Ssam   with any dependent breakpoints, displays, etc.
1511178354Ssam   Used when loading new versions of object modules with the "add-file"
1512153354Ssam   command.  This is only called on the top-level symtab or psymtab's name;
1513178354Ssam   it is not called for subsidiary files such as .h files.
1514178354Ssam
1515178354Ssam   Return value is 1 if we blew away the environment, 0 if not.
1516178354Ssam   FIXME.  The return valu appears to never be used.
1517153354Ssam
1518153354Ssam   FIXME.  I think this is not the best way to do this.  We should
1519148416Ssam   work on being gentler to the environment while still cleaning up
1520148416Ssam   all stray pointers into the freed symtab.  */
1521148416Ssam
1522148416Ssamint
1523148416Ssamfree_named_symtabs (name)
1524148416Ssam     char *name;
1525160687Ssam{
1526160687Ssam#if 0
1527160687Ssam  /* FIXME:  With the new method of each objfile having it's own
1528160687Ssam     psymtab list, this function needs serious rethinking.  In particular,
1529160687Ssam     why was it ever necessary to toss psymtabs with specific compilation
1530160687Ssam     unit filenames, as opposed to all psymtabs from a particular symbol
1531160687Ssam     file?  -- fnf
1532170531Ssam     Well, the answer is that some systems permit reloading of particular
1533170531Ssam     compilation units.  We want to blow away any old info about these
1534170531Ssam     compilation units, regardless of which objfiles they arrived in. --gnu.  */
1535170531Ssam
1536170531Ssam  register struct symtab *s;
1537170531Ssam  register struct symtab *prev;
1538170531Ssam  register struct partial_symtab *ps;
1539170531Ssam  struct blockvector *bv;
1540170531Ssam  int blewit = 0;
1541170531Ssam
1542170531Ssam  /* We only wack things if the symbol-reload switch is set.  */
1543170531Ssam  if (!symbol_reloading)
1544173275Ssam    return 0;
1545178354Ssam
1546178354Ssam  /* Some symbol formats have trouble providing file names... */
1547178354Ssam  if (name == 0 || *name == '\0')
1548178354Ssam    return 0;
1549178354Ssam
1550178354Ssam  /* Look for a psymtab with the specified name.  */
1551173275Ssam
1552173275Ssamagain2:
1553173275Ssam  for (ps = partial_symtab_list; ps; ps = ps->next) {
1554173275Ssam    if (STREQ (name, ps->filename)) {
1555173275Ssam      cashier_psymtab (ps);	/* Blow it away...and its little dog, too.  */
1556173275Ssam      goto again2;		/* Must restart, chain has been munged */
1557173275Ssam    }
1558173275Ssam  }
1559173275Ssam
1560173275Ssam  /* Look for a symtab with the specified name.  */
1561173275Ssam
1562173275Ssam  for (s = symtab_list; s; s = s->next)
1563173275Ssam    {
1564173275Ssam      if (STREQ (name, s->filename))
1565173275Ssam	break;
1566173275Ssam      prev = s;
1567173275Ssam    }
1568173275Ssam
1569173275Ssam  if (s)
1570173275Ssam    {
1571173275Ssam      if (s == symtab_list)
1572173275Ssam	symtab_list = s->next;
1573173275Ssam      else
1574173275Ssam	prev->next = s->next;
1575173275Ssam
1576173275Ssam      /* For now, queue a delete for all breakpoints, displays, etc., whether
1577173275Ssam	 or not they depend on the symtab being freed.  This should be
1578173275Ssam	 changed so that only those data structures affected are deleted.  */
1579173275Ssam
1580173275Ssam      /* But don't delete anything if the symtab is empty.
1581173275Ssam	 This test is necessary due to a bug in "dbxread.c" that
1582173275Ssam	 causes empty symtabs to be created for N_SO symbols that
1583173275Ssam	 contain the pathname of the object file.  (This problem
1584173275Ssam	 has been fixed in GDB 3.9x).  */
1585173275Ssam
1586173275Ssam      bv = BLOCKVECTOR (s);
1587173275Ssam      if (BLOCKVECTOR_NBLOCKS (bv) > 2
1588173275Ssam	  || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1589173275Ssam	  || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1590173275Ssam	{
1591173275Ssam	  complain (&oldsyms_complaint, name);
1592173275Ssam
1593173275Ssam	  clear_symtab_users_queued++;
1594173275Ssam	  make_cleanup (clear_symtab_users_once, 0);
1595173275Ssam	  blewit = 1;
1596173275Ssam	} else {
1597173275Ssam	  complain (&empty_symtab_complaint, name);
1598173275Ssam	}
1599173275Ssam
1600173275Ssam      free_symtab (s);
1601173275Ssam    }
1602173275Ssam  else
1603173275Ssam    {
1604173275Ssam      /* It is still possible that some breakpoints will be affected
1605173275Ssam	 even though no symtab was found, since the file might have
1606183260Ssam	 been compiled without debugging, and hence not be associated
1607173275Ssam	 with a symtab.  In order to handle this correctly, we would need
1608173275Ssam	 to keep a list of text address ranges for undebuggable files.
1609173275Ssam	 For now, we do nothing, since this is a fairly obscure case.  */
1610173275Ssam      ;
1611173275Ssam    }
1612173275Ssam
1613173275Ssam  /* FIXME, what about the minimal symbol table? */
1614173275Ssam  return blewit;
1615173275Ssam#else
1616173275Ssam  return (0);
1617173275Ssam#endif
1618173275Ssam}
1619173275Ssam
1620173275Ssam/* Allocate and partially fill a partial symtab.  It will be
1621173275Ssam   completely filled at the end of the symbol list.
1622173275Ssam
1623173275Ssam   SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1624173275Ssam   is the address relative to which its symbols are (incremental) or 0
1625173275Ssam   (normal). */
1626173275Ssam
1627173275Ssam
1628173275Ssamstruct partial_symtab *
1629173275Ssamstart_psymtab_common (objfile, section_offsets,
1630173275Ssam		      filename, textlow, global_syms, static_syms)
1631173275Ssam     struct objfile *objfile;
1632173275Ssam     struct section_offsets *section_offsets;
1633173275Ssam     char *filename;
1634173275Ssam     CORE_ADDR textlow;
1635173275Ssam     struct partial_symbol **global_syms;
1636173275Ssam     struct partial_symbol **static_syms;
1637173275Ssam{
1638173275Ssam  struct partial_symtab *psymtab;
1639173275Ssam
1640173275Ssam  psymtab = allocate_psymtab (filename, objfile);
1641173275Ssam  psymtab -> section_offsets = section_offsets;
1642173275Ssam  psymtab -> textlow = textlow;
1643173275Ssam  psymtab -> texthigh = psymtab -> textlow;  /* default */
1644173275Ssam  psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1645173275Ssam  psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1646173275Ssam  return (psymtab);
1647173275Ssam}
1648173275Ssam
1649173275Ssam/* Add a symbol with a long value to a psymtab.
1650173275Ssam   Since one arg is a struct, we pass in a ptr and deref it (sigh).  */
1651173275Ssam
1652173275Ssamvoid
1653173275Ssamadd_psymbol_to_list (name, namelength, namespace, class, list, val, coreaddr,
1654173275Ssam		     language, objfile)
1655173275Ssam     char *name;
1656173275Ssam     int namelength;
1657173275Ssam     namespace_enum namespace;
1658173275Ssam     enum address_class class;
1659173275Ssam     struct psymbol_allocation_list *list;
1660173275Ssam     long val;					/* Value as a long */
1661173275Ssam     CORE_ADDR coreaddr;			/* Value as a CORE_ADDR */
1662173275Ssam     enum language language;
1663173275Ssam     struct objfile *objfile;
1664173275Ssam{
1665173275Ssam  register struct partial_symbol *psym;
1666173275Ssam  char *buf = alloca (namelength + 1);
1667173275Ssam  /* psymbol is static so that there will be no uninitialized gaps in the
1668173275Ssam     structure which might contain random data, causing cache misses in
1669173275Ssam     bcache. */
1670173275Ssam  static struct partial_symbol psymbol;
1671173275Ssam
1672173275Ssam  /* Create local copy of the partial symbol */
1673173275Ssam  memcpy (buf, name, namelength);
1674173275Ssam  buf[namelength] = '\0';
1675173275Ssam  SYMBOL_NAME (&psymbol) = bcache (buf, namelength + 1, &objfile->psymbol_cache);
1676173275Ssam  /* val and coreaddr are mutually exclusive, one of them *will* be zero */
1677173275Ssam  if (val != 0)
1678173275Ssam    {
1679173275Ssam      SYMBOL_VALUE (&psymbol) = val;
1680178354Ssam    }
1681178354Ssam  else
1682178354Ssam    {
1683178354Ssam      SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1684178354Ssam    }
1685178354Ssam  SYMBOL_SECTION (&psymbol) = 0;
1686173275Ssam  SYMBOL_LANGUAGE (&psymbol) = language;
1687173275Ssam  PSYMBOL_NAMESPACE (&psymbol) = namespace;
1688173275Ssam  PSYMBOL_CLASS (&psymbol) = class;
1689173275Ssam  SYMBOL_INIT_LANGUAGE_SPECIFIC (&psymbol, language);
1690173275Ssam
1691173275Ssam  /* Stash the partial symbol away in the cache */
1692178354Ssam  psym = bcache (&psymbol, sizeof (struct partial_symbol), &objfile->psymbol_cache);
1693178354Ssam
1694178354Ssam  /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1695178354Ssam  if (list->next >= list->list + list->size)
1696178354Ssam    {
1697178354Ssam      extend_psymbol_list (list, objfile);
1698178354Ssam    }
1699178354Ssam  *list->next++ = psym;
1700178354Ssam  OBJSTAT (objfile, n_psyms++);
1701178354Ssam}
1702178354Ssam
1703183261Ssam/* Initialize storage for partial symbols.  */
1704183261Ssam
1705183261Ssamvoid
1706183261Ssaminit_psymbol_list (objfile, total_symbols)
1707183261Ssam     struct objfile *objfile;
1708183261Ssam     int total_symbols;
1709183261Ssam{
1710183261Ssam  /* Free any previously allocated psymbol lists.  */
1711183261Ssam
1712183261Ssam  if (objfile -> global_psymbols.list)
1713183261Ssam    {
1714183261Ssam      mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
1715178354Ssam    }
1716178354Ssam  if (objfile -> static_psymbols.list)
1717178354Ssam    {
1718178354Ssam      mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
1719178354Ssam    }
1720178354Ssam
1721178354Ssam  /* Current best guess is that approximately a twentieth
1722178354Ssam     of the total symbols (in a debugging file) are global or static
1723178354Ssam     oriented symbols */
1724178354Ssam
1725178354Ssam  objfile -> global_psymbols.size = total_symbols / 10;
1726178354Ssam  objfile -> static_psymbols.size = total_symbols / 10;
1727178354Ssam  objfile -> global_psymbols.next =
1728178354Ssam    objfile -> global_psymbols.list = (struct partial_symbol **)
1729178354Ssam      xmmalloc (objfile -> md, objfile -> global_psymbols.size
1730178354Ssam			     * sizeof (struct partial_symbol *));
1731178354Ssam  objfile -> static_psymbols.next =
1732178354Ssam    objfile -> static_psymbols.list = (struct partial_symbol **)
1733178354Ssam      xmmalloc (objfile -> md, objfile -> static_psymbols.size
1734178354Ssam			     * sizeof (struct partial_symbol *));
1735178354Ssam}
1736178354Ssam
1737178354Ssamvoid
1738178354Ssam_initialize_symfile ()
1739178354Ssam{
1740178354Ssam  struct cmd_list_element *c;
1741178354Ssam
1742178354Ssam  c = add_cmd ("symbol-file", class_files, symbol_file_command,
1743178354Ssam   "Load symbol table from executable file FILE.\n\
1744178354SsamThe `file' command can also load symbol tables, as well as setting the file\n\
1745178354Ssamto execute.", &cmdlist);
1746178354Ssam  c->completer = filename_completer;
1747178354Ssam
1748178354Ssam  c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
1749178354Ssam   "Usage: add-symbol-file FILE ADDR\n\
1750178354SsamLoad the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1751178354SsamADDR is the starting address of the file's text.",
1752178354Ssam	       &cmdlist);
1753178354Ssam  c->completer = filename_completer;
1754178354Ssam
1755178354Ssam  c = add_cmd ("add-shared-symbol-files", class_files,
1756178354Ssam	       add_shared_symbol_files_command,
1757178354Ssam   "Load the symbols from shared objects in the dynamic linker's link map.",
1758178354Ssam   	       &cmdlist);
1759178354Ssam  c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
1760178354Ssam		     &cmdlist);
1761178354Ssam
1762178354Ssam  c = add_cmd ("load", class_files, load_command,
1763178354Ssam   "Dynamically load FILE into the running program, and record its symbols\n\
1764178354Ssamfor access from GDB.", &cmdlist);
1765178354Ssam  c->completer = filename_completer;
1766178354Ssam
1767178354Ssam  add_show_from_set
1768178354Ssam    (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1769178354Ssam		  (char *)&symbol_reloading,
1770178354Ssam	  "Set dynamic symbol table reloading multiple times in one run.",
1771178354Ssam		  &setlist),
1772178354Ssam     &showlist);
1773186101Ssam
1774186101Ssam}
1775186101Ssam