Preparation for gdb-4.1 release.
[binutils-gdb.git] / gdb / symfile.c
1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "param.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "gdbcmd.h"
31 #include "breakpoint.h"
32
33 #include <obstack.h>
34 #include <assert.h>
35
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40
41 extern int info_verbose;
42
43 extern void qsort ();
44 extern char *getenv ();
45
46 /* Functions this file defines */
47 static bfd *symfile_open();
48 static struct sym_fns *symfile_init();
49 static void clear_symtab_users_once();
50
51 /* List of all available sym_fns. */
52
53 struct sym_fns *symtab_fns = NULL;
54
55 /* Saves the sym_fns of the current symbol table, so we can call
56 the right XXX_new_init function when we free it. FIXME. This
57 should be extended to calling the new_init function for each
58 existing symtab or psymtab, since the main symbol file and
59 subsequent added symbol files can have different types. */
60
61 static struct sym_fns *symfile_fns;
62
63 /* Allocate an obstack to hold objects that should be freed
64 when we load a new symbol table.
65 This includes the symbols made by dbxread
66 and the types that are not permanent. */
67
68 struct obstack obstack1;
69
70 struct obstack *symbol_obstack = &obstack1;
71
72 /* This obstack will be used for partial_symbol objects. It can
73 probably actually be the same as the symbol_obstack above, but I'd
74 like to keep them seperate for now. If I want to later, I'll
75 replace one with the other. */
76
77 struct obstack obstack2;
78
79 struct obstack *psymbol_obstack = &obstack2;
80
81 /* File name symbols were loaded from. */
82
83 char *symfile = 0;
84
85 /* The modification date of the file when they were loaded. */
86
87 long /* really time_t */ symfile_mtime = 0;
88
89 /* Structures with which to manage partial symbol allocation. */
90
91 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
92
93 /* Flag for whether user will be reloading symbols multiple times.
94 Defaults to ON for VxWorks, otherwise OFF. */
95
96 #ifdef SYMBOL_RELOADING_DEFAULT
97 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
98 #else
99 int symbol_reloading = 0;
100 #endif
101
102 /* Structure to manage complaints about symbol file contents. */
103
104 struct complaint complaint_root[1] = {
105 {(char *)0, 0, complaint_root},
106 };
107
108 /* Some actual complaints. */
109
110 struct complaint oldsyms_complaint = {
111 "Replacing old symbols for `%s'", 0, 0 };
112
113 struct complaint empty_symtab_complaint = {
114 "Empty symbol table found for `%s'", 0, 0 };
115
116 \f
117 /* In the following sort, we always make sure that
118 register debug symbol declarations always come before regular
119 debug symbol declarations (as might happen when parameters are
120 then put into registers by the compiler). */
121
122 static int
123 compare_symbols (s1, s2)
124 struct symbol **s1, **s2;
125 {
126 register int namediff;
127
128 /* Compare the initial characters. */
129 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
130 if (namediff != 0) return namediff;
131
132 /* If they match, compare the rest of the names. */
133 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
134 if (namediff != 0) return namediff;
135
136 /* For symbols of the same name, registers should come first. */
137 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
138 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
139 }
140
141 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
142
143 void
144 sort_block_syms (b)
145 register struct block *b;
146 {
147 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
148 sizeof (struct symbol *), compare_symbols);
149 }
150
151 /* Call sort_symtab_syms to sort alphabetically
152 the symbols of each block of one symtab. */
153
154 void
155 sort_symtab_syms (s)
156 register struct symtab *s;
157 {
158 register struct blockvector *bv = BLOCKVECTOR (s);
159 int nbl = BLOCKVECTOR_NBLOCKS (bv);
160 int i;
161 register struct block *b;
162
163 for (i = 0; i < nbl; i++)
164 {
165 b = BLOCKVECTOR_BLOCK (bv, i);
166 if (BLOCK_SHOULD_SORT (b))
167 sort_block_syms (b);
168 }
169 }
170
171 void
172 sort_all_symtab_syms ()
173 {
174 register struct symtab *s;
175
176 for (s = symtab_list; s; s = s->next)
177 {
178 sort_symtab_syms (s);
179 }
180 }
181
182 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
183 (and add a null character at the end in the copy).
184 Returns the address of the copy. */
185
186 char *
187 obsavestring (ptr, size)
188 char *ptr;
189 int size;
190 {
191 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
192 /* Open-coded bcopy--saves function call time.
193 These strings are usually short. */
194 {
195 register char *p1 = ptr;
196 register char *p2 = p;
197 char *end = ptr + size;
198 while (p1 != end)
199 *p2++ = *p1++;
200 }
201 p[size] = 0;
202 return p;
203 }
204
205 /* Concatenate strings S1, S2 and S3; return the new string.
206 Space is found in the symbol_obstack. */
207
208 char *
209 obconcat (s1, s2, s3)
210 char *s1, *s2, *s3;
211 {
212 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
213 register char *val = (char *) obstack_alloc (symbol_obstack, len);
214 strcpy (val, s1);
215 strcat (val, s2);
216 strcat (val, s3);
217 return val;
218 }
219 \f
220 /* Accumulate the misc functions in bunches of 127.
221 At the end, copy them all into one newly allocated structure. */
222
223 #define MISC_BUNCH_SIZE 127
224
225 struct misc_bunch
226 {
227 struct misc_bunch *next;
228 struct misc_function contents[MISC_BUNCH_SIZE];
229 };
230
231 /* Bunch currently being filled up.
232 The next field points to chain of filled bunches. */
233
234 static struct misc_bunch *misc_bunch;
235
236 /* Number of slots filled in current bunch. */
237
238 static int misc_bunch_index;
239
240 /* Total number of misc functions recorded so far. */
241
242 static int misc_count;
243
244 void
245 init_misc_bunches ()
246 {
247 misc_count = 0;
248 misc_bunch = 0;
249 misc_bunch_index = MISC_BUNCH_SIZE;
250 }
251
252 void
253 prim_record_misc_function (name, address, misc_type)
254 char *name;
255 CORE_ADDR address;
256 enum misc_function_type misc_type;
257 {
258 register struct misc_bunch *new;
259
260 if (misc_bunch_index == MISC_BUNCH_SIZE)
261 {
262 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
263 misc_bunch_index = 0;
264 new->next = misc_bunch;
265 misc_bunch = new;
266 }
267 misc_bunch->contents[misc_bunch_index].name = name;
268 misc_bunch->contents[misc_bunch_index].address = address;
269 misc_bunch->contents[misc_bunch_index].type = misc_type;
270 misc_bunch->contents[misc_bunch_index].misc_info = 0;
271 misc_bunch_index++;
272 misc_count++;
273 }
274
275 static int
276 compare_misc_functions (fn1, fn2)
277 struct misc_function *fn1, *fn2;
278 {
279 /* Return a signed result based on unsigned comparisons
280 so that we sort into unsigned numeric order. */
281 if (fn1->address < fn2->address)
282 return -1;
283 if (fn1->address > fn2->address)
284 return 1;
285 return 0;
286 }
287
288 /* ARGSUSED */
289 void
290 discard_misc_bunches (foo)
291 int foo;
292 {
293 register struct misc_bunch *next;
294
295 while (misc_bunch)
296 {
297 next = misc_bunch->next;
298 free (misc_bunch);
299 misc_bunch = next;
300 }
301 }
302
303 /* INCLINK nonzero means bunches are from an incrementally-linked file.
304 Add them to the existing bunches.
305 Otherwise INCLINK is zero, and we start from scratch. */
306 void
307 condense_misc_bunches (inclink)
308 int inclink;
309 {
310 register int i, j;
311 register struct misc_bunch *bunch;
312
313 if (inclink)
314 {
315 misc_function_vector
316 = (struct misc_function *)
317 xrealloc (misc_function_vector, (misc_count + misc_function_count)
318 * sizeof (struct misc_function));
319 j = misc_function_count;
320 }
321 else
322 {
323 misc_function_vector
324 = (struct misc_function *)
325 xmalloc (misc_count * sizeof (struct misc_function));
326 j = 0;
327 }
328
329 bunch = misc_bunch;
330 while (bunch)
331 {
332 for (i = 0; i < misc_bunch_index; i++, j++)
333 {
334 misc_function_vector[j] = bunch->contents[i];
335 #ifdef NAMES_HAVE_UNDERSCORE
336 if (misc_function_vector[j].name[0] == '_')
337 misc_function_vector[j].name++;
338 #endif
339 }
340 bunch = bunch->next;
341 misc_bunch_index = MISC_BUNCH_SIZE;
342 }
343
344 if (misc_function_count + misc_count != j) /* DEBUG */
345 printf_filtered ("Function counts are off! %d + %d != %d\n",
346 misc_function_count, misc_count, j);
347
348 misc_function_count = j;
349
350 /* Sort the misc functions by address. */
351
352 qsort (misc_function_vector, misc_function_count,
353 sizeof (struct misc_function),
354 compare_misc_functions);
355 }
356
357
358 /* Get the symbol table that corresponds to a partial_symtab.
359 This is fast after the first time you do it. In fact, there
360 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
361 case inline. */
362
363 struct symtab *
364 psymtab_to_symtab (pst)
365 register struct partial_symtab *pst;
366 {
367 register struct symtab *result;
368
369 /* If it's been looked up before, return it. */
370 if (pst->symtab)
371 return pst->symtab;
372
373 /* If it has not yet been read in, read it. */
374 if (!pst->readin)
375 {
376 (*pst->read_symtab) (pst);
377 }
378
379 return pst->symtab;
380 }
381
382 /* Process a symbol file, as either the main file or as a dynamically
383 loaded file.
384
385 NAME is the file name (which will be tilde-expanded and made
386 absolute herein) (but we don't free or modify NAME itself).
387 FROM_TTY says how verbose to be. MAINLINE specifies whether this
388 is the main symbol file, or whether it's an extra symbol file such
389 as dynamically loaded code. If !mainline, ADDR is the address
390 where the text segment was loaded. */
391
392 void
393 symbol_file_add (name, from_tty, addr, mainline)
394 char *name;
395 int from_tty;
396 CORE_ADDR addr;
397 int mainline;
398 {
399 bfd *sym_bfd;
400 asection *text_sect;
401 struct sym_fns *sf;
402 char *realname;
403
404 sym_bfd = symfile_open (name);
405
406 entry_point = bfd_get_start_address (sym_bfd);
407
408 if (mainline)
409 symfile_mtime = bfd_get_mtime (sym_bfd);
410
411 /* There is a distinction between having no symbol table
412 (we refuse to read the file, leaving the old set of symbols around)
413 and having no debugging symbols in your symbol table (we read
414 the file and end up with a mostly empty symbol table). */
415
416 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
417 {
418 error ("%s has no symbol-table", name);
419 }
420
421 if ((symtab_list || partial_symtab_list)
422 && mainline
423 && from_tty
424 && !query ("Load new symbol table from \"%s\"? ", name))
425 error ("Not confirmed.");
426
427 if (from_tty)
428 {
429 printf_filtered ("Reading symbols from %s...", name);
430 wrap_here ("");
431 fflush (stdout);
432 }
433
434 sf = symfile_init (sym_bfd);
435 realname = bfd_get_filename (sym_bfd);
436 realname = savestring (realname, strlen (realname));
437 /* FIXME, this probably creates a storage leak... */
438
439 if (mainline)
440 {
441 /* Since no error yet, throw away the old symbol table. */
442
443 if (symfile)
444 free (symfile);
445 symfile = 0;
446 free_all_symtabs ();
447 free_all_psymtabs ();
448
449 (*sf->sym_new_init) ();
450
451 /* For mainline, caller didn't know the specified address of the
452 text section. We fix that here. */
453 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
454 addr = bfd_section_vma (sym_bfd, text_sect);
455 }
456
457 clear_complaints(); /* Allow complaints to appear for this new file. */
458
459 (*sf->sym_read) (sf, addr, mainline);
460
461 /* Don't allow char * to have a typename (else would get caddr_t.) */
462 /* Ditto void *. FIXME should do this for all the builtin types. */
463
464 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
465 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
466
467 if (mainline)
468 {
469 /* OK, make it the "real" symbol file. */
470 symfile = realname;
471 symfile_fns = sf;
472 }
473
474 /* If we have wiped out any old symbol tables, clean up. */
475 clear_symtab_users_once ();
476
477 if (from_tty)
478 {
479 printf_filtered ("done.\n");
480 fflush (stdout);
481 }
482 }
483
484 /* This is the symbol-file command. Read the file, analyze its symbols,
485 and add a struct symtab to symtab_list. */
486
487 void
488 symbol_file_command (name, from_tty)
489 char *name;
490 int from_tty;
491 {
492
493 dont_repeat ();
494
495 if (name == 0)
496 {
497 if ((symtab_list || partial_symtab_list)
498 && from_tty
499 && !query ("Discard symbol table from `%s'? ", symfile))
500 error ("Not confirmed.");
501 if (symfile)
502 free (symfile);
503 symfile = 0;
504 free_all_symtabs ();
505 free_all_psymtabs ();
506 /* FIXME, this does not account for the main file and subsequent
507 files (shared libs, dynloads, etc) having different formats.
508 It only calls the cleanup routine for the main file's format. */
509 if (symfile_fns) {
510 (*symfile_fns->sym_new_init) ();
511 free (symfile_fns);
512 symfile_fns = 0;
513 }
514 return;
515 }
516
517 /* Getting new symbols may change our opinion about what is
518 frameless. */
519 reinit_frame_cache ();
520
521 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
522 }
523
524 /* Open NAME and hand it off to BFD for preliminary analysis. Result
525 is a BFD *, which includes a new copy of NAME dynamically allocated
526 (which will be freed by the cleanup chain). In case of trouble,
527 error() is called. */
528
529 static bfd *
530 symfile_open (name)
531 char *name;
532 {
533 bfd *sym_bfd;
534 int desc;
535 char *absolute_name;
536
537 name = tilde_expand (name);
538 make_cleanup (free, name);
539
540 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
541 if (desc < 0)
542 perror_with_name (name);
543 else
544 {
545 make_cleanup (free, absolute_name);
546 name = absolute_name;
547 }
548
549 sym_bfd = bfd_fdopenr (name, NULL, desc);
550 if (!sym_bfd)
551 {
552 close (desc);
553 error ("Could not open `%s' to read symbols: %s",
554 name, bfd_errmsg (bfd_error));
555 }
556 make_cleanup (bfd_close, sym_bfd);
557
558 if (!bfd_check_format (sym_bfd, bfd_object))
559 error ("\"%s\": can't read symbols: %s.",
560 name, bfd_errmsg (bfd_error));
561
562 return sym_bfd;
563 }
564
565 /* Link a new symtab_fns into the global symtab_fns list.
566 Called by various _initialize routines. */
567
568 void
569 add_symtab_fns (sf)
570 struct sym_fns *sf;
571 {
572 sf->next = symtab_fns;
573 symtab_fns = sf;
574 }
575
576
577 /* Initialize to read symbols from the symbol file sym_bfd. It either
578 returns or calls error(). The result is a malloc'd struct sym_fns
579 that contains cached information about the symbol file. */
580
581 static struct sym_fns *
582 symfile_init (sym_bfd)
583 bfd *sym_bfd;
584 {
585 struct sym_fns *sf, *sf2;
586
587 for (sf = symtab_fns; sf != NULL; sf = sf->next)
588 {
589 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
590 {
591 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
592 /* FIXME, who frees this? */
593 *sf2 = *sf;
594 sf2->sym_bfd = sym_bfd;
595 sf2->sym_private = 0; /* Not alloc'd yet */
596 (*sf2->sym_init) (sf2);
597 return sf2;
598 }
599 }
600 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
601 return 0; /* Appease lint. */
602 }
603 \f
604 /* This function runs the load command of our current target. */
605
606 void
607 load_command (arg, from_tty)
608 char *arg;
609 int from_tty;
610 {
611 target_load (arg, from_tty);
612 }
613
614 /* This function allows the addition of incrementally linked object files.
615 It does not modify any state in the target, only in the debugger. */
616
617 /* ARGSUSED */
618 void
619 add_symbol_file_command (arg_string, from_tty)
620 char *arg_string;
621 int from_tty;
622 {
623 char *name;
624 CORE_ADDR text_addr;
625
626 /* Getting new symbols may change our opinion about what is
627 frameless. */
628 reinit_frame_cache ();
629
630 if (arg_string == 0)
631 error ("add-symbol-file takes a file name and an address");
632
633 arg_string = tilde_expand (arg_string);
634 make_cleanup (free, arg_string);
635
636 for( ; *arg_string == ' '; arg_string++ );
637 name = arg_string;
638 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
639 *arg_string++ = (char) 0;
640
641 if (name[0] == 0)
642 error ("add-symbol-file takes a file name and an address");
643
644 text_addr = parse_and_eval_address (arg_string);
645
646 dont_repeat ();
647
648 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
649 name, text_addr))
650 error ("Not confirmed.");
651
652 symbol_file_add (name, 0, text_addr, 0);
653 }
654 \f
655 /* Re-read symbols if the symbol-file has changed. */
656 void
657 reread_symbols ()
658 {
659 struct stat symstat;
660
661 /* With the addition of shared libraries, this should be modified,
662 the load time should be saved in the partial symbol tables, since
663 different tables may come from different source files. FIXME.
664 This routine should then walk down each partial symbol table
665 and see if the symbol table that it originates from has been changed
666 */
667
668 if (stat (symfile, &symstat) < 0)
669 /* Can't read symbol-file. Assume it is up to date. */
670 return;
671
672 if (symstat.st_mtime > symfile_mtime)
673 {
674 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
675 symbol_file_command (symfile, 0);
676 breakpoint_re_set ();
677 }
678 }
679
680 /* This function is really horrible, but to avoid it, there would need
681 to be more filling in of forward references. */
682 void
683 fill_in_vptr_fieldno (type)
684 struct type *type;
685 {
686 if (TYPE_VPTR_FIELDNO (type) < 0)
687 {
688 int i;
689 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
690 {
691 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
692 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
693 {
694 TYPE_VPTR_FIELDNO (type)
695 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
696 TYPE_VPTR_BASETYPE (type)
697 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
698 break;
699 }
700 }
701 }
702 }
703 \f
704 /* Functions to handle complaints during symbol reading. */
705
706 /* How many complaints about a particular thing should be printed before
707 we stop whining about it? Default is no whining at all, since so many
708 systems have ill-constructed symbol files. */
709
710 static unsigned stop_whining = 0;
711
712 /* Print a complaint about the input symbols, and link the complaint block
713 into a chain for later handling. Result is 1 if the complaint was
714 printed, 0 if it was suppressed. */
715
716 int
717 complain (complaint, val)
718 struct complaint *complaint;
719 char *val;
720 {
721 complaint->counter++;
722 if (complaint->next == 0) {
723 complaint->next = complaint_root->next;
724 complaint_root->next = complaint;
725 }
726 if (complaint->counter > stop_whining)
727 return 0;
728 wrap_here ("");
729 if (!info_verbose) {
730 puts_filtered ("During symbol reading...");
731 }
732 printf_filtered (complaint->message, val);
733 puts_filtered ("...");
734 wrap_here("");
735 if (!info_verbose)
736 puts_filtered ("\n");
737 return 1;
738 }
739
740 /* Clear out all complaint counters that have ever been incremented. */
741
742 void
743 clear_complaints ()
744 {
745 struct complaint *p;
746
747 for (p = complaint_root->next; p != complaint_root; p = p->next)
748 p->counter = 0;
749 }
750 \f
751 /* clear_symtab_users_once:
752
753 This function is run after symbol reading, or from a cleanup.
754 If an old symbol table was obsoleted, the old symbol table
755 has been blown away, but the other GDB data structures that may
756 reference it have not yet been cleared or re-directed. (The old
757 symtab was zapped, and the cleanup queued, in free_named_symtab()
758 below.)
759
760 This function can be queued N times as a cleanup, or called
761 directly; it will do all the work the first time, and then will be a
762 no-op until the next time it is queued. This works by bumping a
763 counter at queueing time. Much later when the cleanup is run, or at
764 the end of symbol processing (in case the cleanup is discarded), if
765 the queued count is greater than the "done-count", we do the work
766 and set the done-count to the queued count. If the queued count is
767 less than or equal to the done-count, we just ignore the call. This
768 is needed because reading a single .o file will often replace many
769 symtabs (one per .h file, for example), and we don't want to reset
770 the breakpoints N times in the user's face.
771
772 The reason we both queue a cleanup, and call it directly after symbol
773 reading, is because the cleanup protects us in case of errors, but is
774 discarded if symbol reading is successful. */
775
776 static int clear_symtab_users_queued;
777 static int clear_symtab_users_done;
778
779 static void
780 clear_symtab_users_once ()
781 {
782 /* Enforce once-per-`do_cleanups'-semantics */
783 if (clear_symtab_users_queued <= clear_symtab_users_done)
784 return;
785 clear_symtab_users_done = clear_symtab_users_queued;
786
787 printf ("Resetting debugger state after updating old symbol tables\n");
788
789 /* Someday, we should do better than this, by only blowing away
790 the things that really need to be blown. */
791 clear_value_history ();
792 clear_displays ();
793 clear_internalvars ();
794 breakpoint_re_set ();
795 set_default_breakpoint (0, 0, 0, 0);
796 current_source_symtab = 0;
797 }
798
799 /* Delete the specified psymtab, and any others that reference it. */
800
801 static void
802 cashier_psymtab (pst)
803 struct partial_symtab *pst;
804 {
805 struct partial_symtab *ps, *pprev;
806 int i;
807
808 /* Find its previous psymtab in the chain */
809 for (ps = partial_symtab_list; ps; ps = ps->next) {
810 if (ps == pst)
811 break;
812 pprev = ps;
813 }
814
815 if (ps) {
816 /* Unhook it from the chain. */
817 if (ps == partial_symtab_list)
818 partial_symtab_list = ps->next;
819 else
820 pprev->next = ps->next;
821
822 /* FIXME, we can't conveniently deallocate the entries in the
823 partial_symbol lists (global_psymbols/static_psymbols) that
824 this psymtab points to. These just take up space until all
825 the psymtabs are reclaimed. Ditto the dependencies list and
826 filename, which are all in the psymbol_obstack. */
827
828 /* We need to cashier any psymtab that has this one as a dependency... */
829 again:
830 for (ps = partial_symtab_list; ps; ps = ps->next) {
831 for (i = 0; i < ps->number_of_dependencies; i++) {
832 if (ps->dependencies[i] == pst) {
833 cashier_psymtab (ps);
834 goto again; /* Must restart, chain has been munged. */
835 }
836 }
837 }
838 }
839 }
840
841 /* If a symtab or psymtab for filename NAME is found, free it along
842 with any dependent breakpoints, displays, etc.
843 Used when loading new versions of object modules with the "add-file"
844 command. This is only called on the top-level symtab or psymtab's name;
845 it is not called for subsidiary files such as .h files.
846
847 Return value is 1 if we blew away the environment, 0 if not.
848
849 FIXME. I think this is not the best way to do this. We should
850 work on being gentler to the environment while still cleaning up
851 all stray pointers into the freed symtab. */
852
853 int
854 free_named_symtabs (name)
855 char *name;
856 {
857 register struct symtab *s;
858 register struct symtab *prev;
859 register struct partial_symtab *ps;
860 struct blockvector *bv;
861 int blewit = 0;
862
863 /* We only wack things if the symbol-reload switch is set. */
864 if (!symbol_reloading)
865 return 0;
866
867 /* Some symbol formats have trouble providing file names... */
868 if (name == 0 || *name == '\0')
869 return 0;
870
871 /* Look for a psymtab with the specified name. */
872
873 again2:
874 for (ps = partial_symtab_list; ps; ps = ps->next) {
875 if (!strcmp (name, ps->filename)) {
876 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
877 goto again2; /* Must restart, chain has been munged */
878 }
879 }
880
881 /* Look for a symtab with the specified name. */
882
883 for (s = symtab_list; s; s = s->next)
884 {
885 if (!strcmp (name, s->filename))
886 break;
887 prev = s;
888 }
889
890 if (s)
891 {
892 if (s == symtab_list)
893 symtab_list = s->next;
894 else
895 prev->next = s->next;
896
897 /* For now, queue a delete for all breakpoints, displays, etc., whether
898 or not they depend on the symtab being freed. This should be
899 changed so that only those data structures affected are deleted. */
900
901 /* But don't delete anything if the symtab is empty.
902 This test is necessary due to a bug in "dbxread.c" that
903 causes empty symtabs to be created for N_SO symbols that
904 contain the pathname of the object file. (This problem
905 has been fixed in GDB 3.9x). */
906
907 bv = BLOCKLIST (s);
908 if (BLOCKLIST_NBLOCKS (bv) > 2
909 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
910 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
911 {
912 complain (&oldsyms_complaint, name);
913
914 clear_symtab_users_queued++;
915 make_cleanup (clear_symtab_users_once, 0);
916 blewit = 1;
917 } else {
918 complain (&empty_symtab_complaint, name);
919 }
920
921 free_symtab (s);
922 }
923 else
924 /* It is still possible that some breakpoints will be affected
925 even though no symtab was found, since the file might have
926 been compiled without debugging, and hence not be associated
927 with a symtab. In order to handle this correctly, we would need
928 to keep a list of text address ranges for undebuggable files.
929 For now, we do nothing, since this is a fairly obscure case. */
930 ;
931
932 /* FIXME, what about the misc function vector? */
933 return blewit;
934 }
935 \f
936 void
937 _initialize_symfile ()
938 {
939
940 add_com ("symbol-file", class_files, symbol_file_command,
941 "Load symbol table from executable file FILE.\n\
942 The `file' command can also load symbol tables, as well as setting the file\n\
943 to execute.");
944
945 add_com ("add-symbol-file", class_files, add_symbol_file_command,
946 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
947 The second argument provides the starting address of the file's text.");
948
949 add_com ("load", class_files, load_command,
950 "Dynamically load FILE into the running program, and record its symbols\n\
951 for access from GDB.");
952
953 add_show_from_set
954 (add_set_cmd ("complaints", class_support, var_uinteger,
955 (char *)&stop_whining,
956 "Set max number of complaints about incorrect symbols.",
957 &setlist),
958 &showlist);
959
960 add_show_from_set
961 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
962 (char *)&symbol_reloading,
963 "Set dynamic symbol table reloading multiple times in one run.",
964 &setlist),
965 &showlist);
966
967 obstack_init (symbol_obstack);
968 obstack_init (psymbol_obstack);
969 }