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