Merged in latest RS6000 diffs from Metin G. Ozisik.
[deliverable/binutils-gdb.git] / gdb / symfile.c
1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "gdbcore.h"
25 #include "frame.h"
26 #include "target.h"
27 #include "value.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "gdbcmd.h"
31 #include "breakpoint.h"
32
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 /* Global variables owned by this file */
42
43 int readnow_symbol_files; /* Read full symbols immediately */
44
45 /* External variables and functions referenced. */
46
47 extern int info_verbose;
48
49 /* Functions this file defines */
50
51 static void
52 load_command PARAMS ((char *, int));
53
54 static void
55 add_symbol_file_command PARAMS ((char *, int));
56
57 static void
58 cashier_psymtab PARAMS ((struct partial_symtab *));
59
60 static int
61 compare_psymbols PARAMS ((const void *, const void *));
62
63 static int
64 compare_symbols PARAMS ((const void *, const void *));
65
66 static bfd *
67 symfile_bfd_open PARAMS ((char *));
68
69 static void
70 find_sym_fns PARAMS ((struct objfile *));
71
72 static void
73 clear_symtab_users_once PARAMS ((void));
74
75 /* List of all available sym_fns. On gdb startup, each object file reader
76 calls add_symtab_fns() to register information on each format it is
77 prepared to read. */
78
79 static struct sym_fns *symtab_fns = NULL;
80
81 /* Structures with which to manage partial symbol allocation. */
82
83 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
84
85 /* Flag for whether user will be reloading symbols multiple times.
86 Defaults to ON for VxWorks, otherwise OFF. */
87
88 #ifdef SYMBOL_RELOADING_DEFAULT
89 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
90 #else
91 int symbol_reloading = 0;
92 #endif
93
94 /* Structure to manage complaints about symbol file contents. */
95
96 struct complaint complaint_root[1] = {
97 {(char *) 0, 0, complaint_root},
98 };
99
100 /* Some actual complaints. */
101
102 struct complaint oldsyms_complaint = {
103 "Replacing old symbols for `%s'", 0, 0 };
104
105 struct complaint empty_symtab_complaint = {
106 "Empty symbol table found for `%s'", 0, 0 };
107
108 \f
109 /* In the following sort, we always make sure that
110 register debug symbol declarations always come before regular
111 debug symbol declarations (as might happen when parameters are
112 then put into registers by the compiler).
113
114 Since this function is called from within qsort, in an ANSI environment
115 it must conform to the prototype for qsort, which specifies that the
116 comparison function takes two "void *" pointers. */
117
118 static int
119 compare_symbols (s1p, s2p)
120 const PTR s1p;
121 const PTR s2p;
122 {
123 register struct symbol **s1, **s2;
124 register int namediff;
125
126 s1 = (struct symbol **) s1p;
127 s2 = (struct symbol **) s2p;
128
129 /* Compare the initial characters. */
130 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
131 if (namediff != 0) return namediff;
132
133 /* If they match, compare the rest of the names. */
134 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
135 if (namediff != 0) return namediff;
136
137 /* For symbols of the same name, registers should come first. */
138 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
139 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
140 }
141
142 /*
143
144 LOCAL FUNCTION
145
146 compare_psymbols -- compare two partial symbols by name
147
148 DESCRIPTION
149
150 Given pointer to two partial symbol table entries, compare
151 them by name and return -N, 0, or +N (ala strcmp). Typically
152 used by sorting routines like qsort().
153
154 NOTES
155
156 Does direct compare of first two characters before punting
157 and passing to strcmp for longer compares. Note that the
158 original version had a bug whereby two null strings or two
159 identically named one character strings would return the
160 comparison of memory following the null byte.
161
162 */
163
164 static int
165 compare_psymbols (s1p, s2p)
166 const PTR s1p;
167 const PTR s2p;
168 {
169 register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
170 register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
171
172 if ((st1[0] - st2[0]) || !st1[0])
173 {
174 return (st1[0] - st2[0]);
175 }
176 else if ((st1[1] - st2[1]) || !st1[1])
177 {
178 return (st1[1] - st2[1]);
179 }
180 else
181 {
182 return (strcmp (st1 + 2, st2 + 2));
183 }
184 }
185
186 void
187 sort_pst_symbols (pst)
188 struct partial_symtab *pst;
189 {
190 /* Sort the global list; don't sort the static list */
191
192 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
193 pst -> n_global_syms, sizeof (struct partial_symbol),
194 compare_psymbols);
195 }
196
197 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
198
199 void
200 sort_block_syms (b)
201 register struct block *b;
202 {
203 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
204 sizeof (struct symbol *), compare_symbols);
205 }
206
207 /* Call sort_symtab_syms to sort alphabetically
208 the symbols of each block of one symtab. */
209
210 void
211 sort_symtab_syms (s)
212 register struct symtab *s;
213 {
214 register struct blockvector *bv;
215 int nbl;
216 int i;
217 register struct block *b;
218
219 if (s == 0)
220 return;
221 bv = BLOCKVECTOR (s);
222 nbl = BLOCKVECTOR_NBLOCKS (bv);
223 for (i = 0; i < nbl; i++)
224 {
225 b = BLOCKVECTOR_BLOCK (bv, i);
226 if (BLOCK_SHOULD_SORT (b))
227 sort_block_syms (b);
228 }
229 }
230
231 void
232 sort_all_symtab_syms ()
233 {
234 register struct symtab *s;
235 register struct objfile *objfile;
236
237 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
238 {
239 for (s = objfile -> symtabs; s != NULL; s = s -> next)
240 {
241 sort_symtab_syms (s);
242 }
243 }
244 }
245
246 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
247 (and add a null character at the end in the copy).
248 Returns the address of the copy. */
249
250 char *
251 obsavestring (ptr, size, obstackp)
252 char *ptr;
253 int size;
254 struct obstack *obstackp;
255 {
256 register char *p = (char *) obstack_alloc (obstackp, size + 1);
257 /* Open-coded bcopy--saves function call time.
258 These strings are usually short. */
259 {
260 register char *p1 = ptr;
261 register char *p2 = p;
262 char *end = ptr + size;
263 while (p1 != end)
264 *p2++ = *p1++;
265 }
266 p[size] = 0;
267 return p;
268 }
269
270 /* Concatenate strings S1, S2 and S3; return the new string.
271 Space is found in the symbol_obstack. */
272
273 char *
274 obconcat (obstackp, s1, s2, s3)
275 struct obstack *obstackp;
276 const char *s1, *s2, *s3;
277 {
278 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
279 register char *val = (char *) obstack_alloc (obstackp, len);
280 strcpy (val, s1);
281 strcat (val, s2);
282 strcat (val, s3);
283 return val;
284 }
285
286 /* Get the symbol table that corresponds to a partial_symtab.
287 This is fast after the first time you do it. In fact, there
288 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
289 case inline. */
290
291 struct symtab *
292 psymtab_to_symtab (pst)
293 register struct partial_symtab *pst;
294 {
295 /* If it's been looked up before, return it. */
296 if (pst->symtab)
297 return pst->symtab;
298
299 /* If it has not yet been read in, read it. */
300 if (!pst->readin)
301 {
302 (*pst->read_symtab) (pst);
303 }
304
305 return pst->symtab;
306 }
307
308 /* Initialize entry point information for this objfile. */
309
310 void
311 init_entry_point_info (objfile)
312 struct objfile *objfile;
313 {
314 /* Save startup file's range of PC addresses to help blockframe.c
315 decide where the bottom of the stack is. */
316
317 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
318 {
319 /* Executable file -- record its entry point so we'll recognize
320 the startup file because it contains the entry point. */
321 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
322 }
323 else
324 {
325 /* Examination of non-executable.o files. Short-circuit this stuff. */
326 /* ~0 will not be in any file, we hope. */
327 objfile -> ei.entry_point = ~0;
328 /* set the startup file to be an empty range. */
329 objfile -> ei.entry_file_lowpc = 0;
330 objfile -> ei.entry_file_highpc = 0;
331 }
332 }
333
334 /* Process a symbol file, as either the main file or as a dynamically
335 loaded file.
336
337 NAME is the file name (which will be tilde-expanded and made
338 absolute herein) (but we don't free or modify NAME itself).
339 FROM_TTY says how verbose to be. MAINLINE specifies whether this
340 is the main symbol file, or whether it's an extra symbol file such
341 as dynamically loaded code. If !mainline, ADDR is the address
342 where the text segment was loaded. If VERBO, the caller has printed
343 a verbose message about the symbol reading (and complaints can be
344 more terse about it). */
345
346 void
347 syms_from_objfile (objfile, addr, mainline, verbo)
348 struct objfile *objfile;
349 CORE_ADDR addr;
350 int mainline;
351 int verbo;
352 {
353 asection *text_sect;
354
355 /* There is a distinction between having no symbol table
356 (we refuse to read the file, leaving the old set of symbols around)
357 and having no debugging symbols in your symbol table (we read
358 the file and end up with a mostly empty symbol table).
359
360 FIXME: This strategy works correctly when the debugging symbols are
361 intermixed with "normal" symbols. However, when the debugging symbols
362 are separate, such as with ELF/DWARF, it is perfectly plausible for
363 the symbol table to be missing but still have all the DWARF info
364 intact. Thus in general it is wrong to assume that having no symbol
365 table implies no debugging information. */
366
367 if (!(bfd_get_file_flags (objfile -> obfd) & HAS_SYMS))
368 return;
369
370 init_entry_point_info (objfile);
371 find_sym_fns (objfile);
372
373 if (mainline)
374 {
375 /* Since no error yet, throw away the old symbol table. */
376
377 if (symfile_objfile != NULL)
378 {
379 free_objfile (symfile_objfile);
380 symfile_objfile = NULL;
381 }
382
383 (*objfile -> sf -> sym_new_init) (objfile);
384
385 /* For mainline, caller didn't know the specified address of the
386 text section. We fix that here. */
387
388 text_sect = bfd_get_section_by_name (objfile -> obfd, ".text");
389 addr = bfd_section_vma (objfile -> obfd, text_sect);
390 }
391
392 /* Initialize symbol reading routines for this objfile, allow complaints to
393 appear for this new file, and record how verbose to be, then do the
394 initial symbol reading for this file. */
395
396 (*objfile -> sf -> sym_init) (objfile);
397 clear_complaints (1, verbo);
398 (*objfile -> sf -> sym_read) (objfile, addr, mainline);
399
400 /* Don't allow char * to have a typename (else would get caddr_t.) */
401 /* Ditto void *. FIXME should do this for all the builtin types. */
402
403 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
404 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
405
406 if (mainline)
407 {
408 /* OK, make it the "real" symbol file. */
409 symfile_objfile = objfile;
410 }
411
412 /* If we have wiped out any old symbol tables, clean up. */
413 clear_symtab_users_once ();
414
415 /* We're done reading the symbol file; finish off complaints. */
416 clear_complaints (0, verbo);
417
418 /* Fixup all the breakpoints that may have been redefined by this
419 symbol file. */
420
421 breakpoint_re_set ();
422 }
423
424 /* Process a symbol file, as either the main file or as a dynamically
425 loaded file.
426
427 NAME is the file name (which will be tilde-expanded and made
428 absolute herein) (but we don't free or modify NAME itself).
429 FROM_TTY says how verbose to be. MAINLINE specifies whether this
430 is the main symbol file, or whether it's an extra symbol file such
431 as dynamically loaded code. If !mainline, ADDR is the address
432 where the text segment was loaded.
433
434 Upon success, returns a pointer to the objfile that was added.
435 Upon failure, jumps back to command level (never returns). */
436
437 struct objfile *
438 symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
439 char *name;
440 int from_tty;
441 CORE_ADDR addr;
442 int mainline;
443 int mapped;
444 int readnow;
445 {
446 struct objfile *objfile;
447 struct partial_symtab *psymtab;
448 bfd *abfd;
449 int mapped_it;
450
451 /* Open a bfd for the file and then check to see if the file has a
452 symbol table. There is a distinction between having no symbol table
453 (we refuse to read the file, leaving the old set of symbols around)
454 and having no debugging symbols in the symbol table (we read the file
455 and end up with a mostly empty symbol table, but with lots of stuff in
456 the minimal symbol table). We need to make the decision about whether
457 to continue with the file before allocating and building a objfile.
458
459 FIXME: This strategy works correctly when the debugging symbols are
460 intermixed with "normal" symbols. However, when the debugging symbols
461 are separate, such as with ELF/DWARF, it is perfectly plausible for
462 the symbol table to be missing but still have all the DWARF info
463 intact. Thus in general it is wrong to assume that having no symbol
464 table implies no debugging information. */
465
466 abfd = symfile_bfd_open (name);
467 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
468 {
469 error ("%s has no symbol-table", name);
470 }
471
472 if ((have_full_symbols () || have_partial_symbols ())
473 && mainline
474 && from_tty
475 && !query ("Load new symbol table from \"%s\"? ", name))
476 error ("Not confirmed.");
477
478 objfile = allocate_objfile (abfd, mapped);
479
480 /* If the objfile uses a mapped symbol file, and we have a psymtab for
481 it, then skip reading any symbols at this time. */
482
483 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
484 {
485 /* We mapped in an existing symbol table file that already has had
486 initial symbol reading performed, so we can skip that part. Notify
487 the user that instead of reading the symbols, they have been mapped.
488 */
489 if (from_tty || info_verbose)
490 {
491 printf_filtered ("Mapped symbols for %s...", name);
492 wrap_here ("");
493 fflush (stdout);
494 }
495 }
496 else
497 {
498 /* We either created a new mapped symbol table, mapped an existing
499 symbol table file which has not had initial symbol reading
500 performed, or need to read an unmapped symbol table. */
501 if (from_tty || info_verbose)
502 {
503 printf_filtered ("Reading symbols from %s...", name);
504 wrap_here ("");
505 fflush (stdout);
506 }
507 syms_from_objfile (objfile, addr, mainline, from_tty);
508 objfile -> flags |= OBJF_SYMS;
509 }
510
511 /* We now have at least a partial symbol table. Check to see if the
512 user requested that all symbols be read on initial access via either
513 the gdb startup command line or on a per symbol file basis. Expand
514 all partial symbol tables for this objfile if so. */
515
516 if (readnow || readnow_symbol_files)
517 {
518 if (from_tty || info_verbose)
519 {
520 printf_filtered ("expanding to full symbols...");
521 wrap_here ("");
522 fflush (stdout);
523 }
524
525 for (psymtab = objfile -> psymtabs;
526 psymtab != NULL;
527 psymtab = psymtab -> next)
528 {
529 (void) psymtab_to_symtab (psymtab);
530 }
531 }
532
533 if (from_tty || info_verbose)
534 {
535 printf_filtered ("done.\n");
536 fflush (stdout);
537 }
538
539 return (objfile);
540 }
541
542 /* This is the symbol-file command. Read the file, analyze its symbols,
543 and add a struct symtab to a symtab list. */
544
545 void
546 symbol_file_command (args, from_tty)
547 char *args;
548 int from_tty;
549 {
550 char **argv;
551 char *name = NULL;
552 struct cleanup *cleanups;
553 struct objfile *objfile;
554 int mapped = 0;
555 int readnow = 0;
556
557 dont_repeat ();
558
559 if (args == NULL)
560 {
561 if ((have_full_symbols () || have_partial_symbols ())
562 && from_tty
563 && !query ("Discard symbol table from `%s'? ",
564 symfile_objfile -> name))
565 error ("Not confirmed.");
566 free_all_objfiles ();
567 symfile_objfile = NULL;
568 }
569 else
570 {
571 if ((argv = buildargv (args)) == NULL)
572 {
573 nomem (0);
574 }
575 cleanups = make_cleanup (freeargv, (char *) argv);
576 while (*argv != NULL)
577 {
578 if (strcmp (*argv, "-mapped") == 0)
579 {
580 mapped = 1;
581 }
582 else if (strcmp (*argv, "-readnow") == 0)
583 {
584 readnow = 1;
585 }
586 else if (**argv == '-')
587 {
588 error ("unknown option `%s'", *argv);
589 }
590 else
591 {
592 name = *argv;
593 }
594 argv++;
595 }
596
597 if (name == NULL)
598 {
599 error ("no symbol file name was specified");
600 }
601 else
602 {
603 /* Getting new symbols may change our opinion about what is
604 frameless. */
605 reinit_frame_cache ();
606 objfile = symbol_file_add (name, from_tty, (CORE_ADDR)0, 1,
607 mapped, readnow);
608 }
609 do_cleanups (cleanups);
610 }
611 }
612
613 /* Open file specified by NAME and hand it off to BFD for preliminary
614 analysis. Result is a newly initialized bfd *, which includes a newly
615 malloc'd` copy of NAME (tilde-expanded and made absolute).
616 In case of trouble, error() is called. */
617
618 static bfd *
619 symfile_bfd_open (name)
620 char *name;
621 {
622 bfd *sym_bfd;
623 int desc;
624 char *absolute_name;
625
626 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
627
628 /* Look down path for it, allocate 2nd new malloc'd copy. */
629 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
630 if (desc < 0)
631 {
632 make_cleanup (free, name);
633 perror_with_name (name);
634 }
635 free (name); /* Free 1st new malloc'd copy */
636 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
637
638 sym_bfd = bfd_fdopenr (name, NULL, desc);
639 if (!sym_bfd)
640 {
641 close (desc);
642 make_cleanup (free, name);
643 error ("\"%s\": can't open to read symbols: %s.", name,
644 bfd_errmsg (bfd_error));
645 }
646
647 if (!bfd_check_format (sym_bfd, bfd_object))
648 {
649 bfd_close (sym_bfd); /* This also closes desc */
650 make_cleanup (free, name);
651 error ("\"%s\": can't read symbols: %s.", name,
652 bfd_errmsg (bfd_error));
653 }
654
655 return (sym_bfd);
656 }
657
658 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
659 startup by the _initialize routine in each object file format reader,
660 to register information about each format the the reader is prepared
661 to handle. */
662
663 void
664 add_symtab_fns (sf)
665 struct sym_fns *sf;
666 {
667 sf->next = symtab_fns;
668 symtab_fns = sf;
669 }
670
671
672 /* Initialize to read symbols from the symbol file sym_bfd. It either
673 returns or calls error(). The result is an initialized struct sym_fns
674 in the objfile structure, that contains cached information about the
675 symbol file. */
676
677 static void
678 find_sym_fns (objfile)
679 struct objfile *objfile;
680 {
681 struct sym_fns *sf, *sf2;
682
683 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
684 {
685 if (strncmp (bfd_get_target (objfile -> obfd),
686 sf -> sym_name, sf -> sym_namelen) == 0)
687 {
688 objfile -> sf = sf;
689 return;
690 }
691 }
692 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
693 bfd_get_target (objfile -> obfd));
694 }
695 \f
696 /* This function runs the load command of our current target. */
697
698 static void
699 load_command (arg, from_tty)
700 char *arg;
701 int from_tty;
702 {
703 target_load (arg, from_tty);
704 }
705
706 /* This function allows the addition of incrementally linked object files.
707 It does not modify any state in the target, only in the debugger. */
708
709 /* ARGSUSED */
710 static void
711 add_symbol_file_command (args, from_tty)
712 char *args;
713 int from_tty;
714 {
715 char *name = NULL;
716 CORE_ADDR text_addr;
717 char *arg;
718 int readnow;
719 int mapped;
720
721 dont_repeat ();
722
723 if (args == NULL)
724 {
725 error ("add-symbol-file takes a file name and an address");
726 }
727
728 /* Make a copy of the string that we can safely write into. */
729
730 args = strdup (args);
731 make_cleanup (free, args);
732
733 /* Pick off any -option args and the file name. */
734
735 while ((*args != '\000') && (name == NULL))
736 {
737 while (isspace (*args)) {args++;}
738 arg = args;
739 while ((*args != '\000') && !isspace (*args)) {args++;}
740 if (*args != '\000')
741 {
742 *args++ = '\000';
743 }
744 if (*arg != '-')
745 {
746 name = arg;
747 }
748 else if (strcmp (arg, "-mapped") == 0)
749 {
750 mapped = 1;
751 }
752 else if (strcmp (arg, "-readnow") == 0)
753 {
754 readnow = 1;
755 }
756 else
757 {
758 error ("unknown option `%s'", arg);
759 }
760 }
761
762 /* After picking off any options and the file name, args should be
763 left pointing at the remainder of the command line, which should
764 be the address expression to evaluate. */
765
766 if ((name == NULL) || (*args == '\000') )
767 {
768 error ("add-symbol-file takes a file name and an address");
769 }
770 name = tilde_expand (name);
771 make_cleanup (free, name);
772
773 text_addr = parse_and_eval_address (args);
774
775 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
776 name, local_hex_string (text_addr)))
777 error ("Not confirmed.");
778
779 /* Getting new symbols may change our opinion about what is
780 frameless. */
781
782 reinit_frame_cache ();
783
784 (void) symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
785 }
786 \f
787 /* Re-read symbols if a symbol-file has changed. */
788 void
789 reread_symbols ()
790 {
791 struct objfile *objfile;
792 long new_modtime;
793 int reread_one = 0;
794 struct stat new_statbuf;
795 int res;
796
797 /* With the addition of shared libraries, this should be modified,
798 the load time should be saved in the partial symbol tables, since
799 different tables may come from different source files. FIXME.
800 This routine should then walk down each partial symbol table
801 and see if the symbol table that it originates from has been changed */
802
803 the_big_top:
804 for (objfile = object_files; objfile; objfile = objfile->next) {
805 if (objfile->obfd) {
806 #ifdef IBM6000_TARGET
807 /* If this object is from a shared library, then you should
808 stat on the library name, not member name. */
809
810 if (objfile->obfd->my_archive)
811 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
812 else
813 #endif
814 res = stat (objfile->name, &new_statbuf);
815 if (res != 0) {
816 /* FIXME, should use print_sys_errmsg but it's not filtered. */
817 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
818 objfile->name);
819 continue;
820 }
821 new_modtime = new_statbuf.st_mtime;
822 if (new_modtime != objfile->mtime) {
823 printf_filtered ("`%s' has changed; re-reading symbols.\n",
824 objfile->name);
825 /* FIXME, this should use a different command...that would only
826 affect this objfile's symbols, and would reset objfile->mtime.
827 (objfile->mtime = new_modtime;)
828 HOWEVER, that command isn't written yet -- so call symbol_file_
829 command, and restart the scan from the top, because it munges
830 the object_files list. */
831 symbol_file_command (objfile->name, 0);
832 reread_one = 1;
833 goto the_big_top; /* Start over. */
834 }
835 }
836 }
837
838 if (reread_one)
839 breakpoint_re_set ();
840 }
841 \f
842 /* Functions to handle complaints during symbol reading. */
843
844 /* How many complaints about a particular thing should be printed before
845 we stop whining about it? Default is no whining at all, since so many
846 systems have ill-constructed symbol files. */
847
848 static unsigned stop_whining = 0;
849
850 /* Should each complaint be self explanatory, or should we assume that
851 a series of complaints is being produced?
852 case 0: self explanatory message.
853 case 1: First message of a series that must start off with explanation.
854 case 2: Subsequent message, when user already knows we are reading
855 symbols and we can just state our piece. */
856
857 static int complaint_series = 0;
858
859 /* Print a complaint about the input symbols, and link the complaint block
860 into a chain for later handling. */
861
862 void
863 complain (complaint, val)
864 struct complaint *complaint;
865 char *val;
866 {
867 complaint->counter++;
868 if (complaint->next == 0) {
869 complaint->next = complaint_root->next;
870 complaint_root->next = complaint;
871 }
872 if (complaint->counter > stop_whining)
873 return;
874 wrap_here ("");
875
876 switch (complaint_series + (info_verbose << 1)) {
877
878 /* Isolated messages, must be self-explanatory. */
879 case 0:
880 puts_filtered ("During symbol reading, ");
881 wrap_here("");
882 printf_filtered (complaint->message, val);
883 puts_filtered (".\n");
884 break;
885
886 /* First of a series, without `set verbose'. */
887 case 1:
888 puts_filtered ("During symbol reading...");
889 printf_filtered (complaint->message, val);
890 puts_filtered ("...");
891 wrap_here("");
892 complaint_series++;
893 break;
894
895 /* Subsequent messages of a series, or messages under `set verbose'.
896 (We'll already have produced a "Reading in symbols for XXX..." message
897 and will clean up at the end with a newline.) */
898 default:
899 printf_filtered (complaint->message, val);
900 puts_filtered ("...");
901 wrap_here("");
902 }
903 }
904
905 /* Clear out all complaint counters that have ever been incremented.
906 If sym_reading is 1, be less verbose about successive complaints,
907 since the messages are appearing all together during a command that
908 reads symbols (rather than scattered around as psymtabs get fleshed
909 out into symtabs at random times). If noisy is 1, we are in a
910 noisy symbol reading command, and our caller will print enough
911 context for the user to figure it out. */
912
913 void
914 clear_complaints (sym_reading, noisy)
915 int sym_reading;
916 int noisy;
917 {
918 struct complaint *p;
919
920 for (p = complaint_root->next; p != complaint_root; p = p->next)
921 p->counter = 0;
922
923 if (!sym_reading && !noisy && complaint_series > 1) {
924 /* Terminate previous series, since caller won't. */
925 puts_filtered ("\n");
926 }
927
928 complaint_series = sym_reading? 1 + noisy: 0;
929 }
930 \f
931 enum language
932 deduce_language_from_filename (filename)
933 char *filename;
934 {
935 char *c = strrchr (filename, '.');
936
937 if (!c) ; /* Get default. */
938 else if(!strcmp(c,".mod"))
939 return language_m2;
940 else if(!strcmp(c,".c"))
941 return language_c;
942 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
943 return language_cplus;
944
945 return language_unknown; /* default */
946 }
947 \f
948 /* allocate_symtab:
949
950 Allocate and partly initialize a new symbol table. Return a pointer
951 to it. error() if no space.
952
953 Caller must set these fields:
954 LINETABLE(symtab)
955 symtab->blockvector
956 symtab->dirname
957 symtab->free_code
958 symtab->free_ptr
959 initialize any EXTRA_SYMTAB_INFO
960 possibly free_named_symtabs (symtab->filename);
961 */
962
963 struct symtab *
964 allocate_symtab (filename, objfile)
965 char *filename;
966 struct objfile *objfile;
967 {
968 register struct symtab *symtab;
969
970 symtab = (struct symtab *)
971 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
972 (void) memset (symtab, 0, sizeof (*symtab));
973 symtab -> filename = obsavestring (filename, strlen (filename),
974 &objfile -> symbol_obstack);
975 symtab -> fullname = NULL;
976 symtab -> language = deduce_language_from_filename (filename);
977
978 /* Hook it to the objfile it comes from */
979
980 symtab -> objfile = objfile;
981 symtab -> next = objfile -> symtabs;
982 objfile -> symtabs = symtab;
983
984 #ifdef INIT_EXTRA_SYMTAB_INFO
985 INIT_EXTRA_SYMTAB_INFO (symtab);
986 #endif
987
988 return (symtab);
989 }
990
991 struct partial_symtab *
992 allocate_psymtab (filename, objfile)
993 char *filename;
994 struct objfile *objfile;
995 {
996 struct partial_symtab *psymtab;
997
998 if (objfile -> free_psymtabs)
999 {
1000 psymtab = objfile -> free_psymtabs;
1001 objfile -> free_psymtabs = psymtab -> next;
1002 }
1003 else
1004 psymtab = (struct partial_symtab *)
1005 obstack_alloc (&objfile -> psymbol_obstack,
1006 sizeof (struct partial_symtab));
1007
1008 (void) memset (psymtab, 0, sizeof (struct partial_symtab));
1009 psymtab -> filename = obsavestring (filename, strlen (filename),
1010 &objfile -> psymbol_obstack);
1011 psymtab -> symtab = NULL;
1012
1013 /* Hook it to the objfile it comes from */
1014
1015 psymtab -> objfile = objfile;
1016 psymtab -> next = objfile -> psymtabs;
1017 objfile -> psymtabs = psymtab;
1018
1019 return (psymtab);
1020 }
1021
1022 \f
1023 /* clear_symtab_users_once:
1024
1025 This function is run after symbol reading, or from a cleanup.
1026 If an old symbol table was obsoleted, the old symbol table
1027 has been blown away, but the other GDB data structures that may
1028 reference it have not yet been cleared or re-directed. (The old
1029 symtab was zapped, and the cleanup queued, in free_named_symtab()
1030 below.)
1031
1032 This function can be queued N times as a cleanup, or called
1033 directly; it will do all the work the first time, and then will be a
1034 no-op until the next time it is queued. This works by bumping a
1035 counter at queueing time. Much later when the cleanup is run, or at
1036 the end of symbol processing (in case the cleanup is discarded), if
1037 the queued count is greater than the "done-count", we do the work
1038 and set the done-count to the queued count. If the queued count is
1039 less than or equal to the done-count, we just ignore the call. This
1040 is needed because reading a single .o file will often replace many
1041 symtabs (one per .h file, for example), and we don't want to reset
1042 the breakpoints N times in the user's face.
1043
1044 The reason we both queue a cleanup, and call it directly after symbol
1045 reading, is because the cleanup protects us in case of errors, but is
1046 discarded if symbol reading is successful. */
1047
1048 static int clear_symtab_users_queued;
1049 static int clear_symtab_users_done;
1050
1051 static void
1052 clear_symtab_users_once ()
1053 {
1054 /* Enforce once-per-`do_cleanups'-semantics */
1055 if (clear_symtab_users_queued <= clear_symtab_users_done)
1056 return;
1057 clear_symtab_users_done = clear_symtab_users_queued;
1058
1059 printf ("Resetting debugger state after updating old symbol tables\n");
1060
1061 /* Someday, we should do better than this, by only blowing away
1062 the things that really need to be blown. */
1063 clear_value_history ();
1064 clear_displays ();
1065 clear_internalvars ();
1066 breakpoint_re_set ();
1067 set_default_breakpoint (0, 0, 0, 0);
1068 current_source_symtab = 0;
1069 }
1070
1071 /* Delete the specified psymtab, and any others that reference it. */
1072
1073 static void
1074 cashier_psymtab (pst)
1075 struct partial_symtab *pst;
1076 {
1077 struct partial_symtab *ps, *pprev;
1078 int i;
1079
1080 /* Find its previous psymtab in the chain */
1081 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1082 if (ps == pst)
1083 break;
1084 pprev = ps;
1085 }
1086
1087 if (ps) {
1088 /* Unhook it from the chain. */
1089 if (ps == pst->objfile->psymtabs)
1090 pst->objfile->psymtabs = ps->next;
1091 else
1092 pprev->next = ps->next;
1093
1094 /* FIXME, we can't conveniently deallocate the entries in the
1095 partial_symbol lists (global_psymbols/static_psymbols) that
1096 this psymtab points to. These just take up space until all
1097 the psymtabs are reclaimed. Ditto the dependencies list and
1098 filename, which are all in the psymbol_obstack. */
1099
1100 /* We need to cashier any psymtab that has this one as a dependency... */
1101 again:
1102 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1103 for (i = 0; i < ps->number_of_dependencies; i++) {
1104 if (ps->dependencies[i] == pst) {
1105 cashier_psymtab (ps);
1106 goto again; /* Must restart, chain has been munged. */
1107 }
1108 }
1109 }
1110 }
1111 }
1112
1113 /* If a symtab or psymtab for filename NAME is found, free it along
1114 with any dependent breakpoints, displays, etc.
1115 Used when loading new versions of object modules with the "add-file"
1116 command. This is only called on the top-level symtab or psymtab's name;
1117 it is not called for subsidiary files such as .h files.
1118
1119 Return value is 1 if we blew away the environment, 0 if not.
1120 FIXME. The return valu appears to never be used.
1121
1122 FIXME. I think this is not the best way to do this. We should
1123 work on being gentler to the environment while still cleaning up
1124 all stray pointers into the freed symtab. */
1125
1126 int
1127 free_named_symtabs (name)
1128 char *name;
1129 {
1130 register struct symtab *s;
1131 register struct symtab *prev;
1132 register struct partial_symtab *ps;
1133 struct blockvector *bv;
1134 int blewit = 0;
1135
1136 #if 0
1137 /* FIXME: With the new method of each objfile having it's own
1138 psymtab list, this function needs serious rethinking. In particular,
1139 why was it ever necessary to toss psymtabs with specific compilation
1140 unit filenames, as opposed to all psymtabs from a particular symbol
1141 file. */
1142
1143 /* We only wack things if the symbol-reload switch is set. */
1144 if (!symbol_reloading)
1145 return 0;
1146
1147 /* Some symbol formats have trouble providing file names... */
1148 if (name == 0 || *name == '\0')
1149 return 0;
1150
1151 /* Look for a psymtab with the specified name. */
1152
1153 again2:
1154 for (ps = partial_symtab_list; ps; ps = ps->next) {
1155 if (!strcmp (name, ps->filename)) {
1156 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1157 goto again2; /* Must restart, chain has been munged */
1158 }
1159 }
1160
1161 /* Look for a symtab with the specified name. */
1162
1163 for (s = symtab_list; s; s = s->next)
1164 {
1165 if (!strcmp (name, s->filename))
1166 break;
1167 prev = s;
1168 }
1169
1170 if (s)
1171 {
1172 if (s == symtab_list)
1173 symtab_list = s->next;
1174 else
1175 prev->next = s->next;
1176
1177 /* For now, queue a delete for all breakpoints, displays, etc., whether
1178 or not they depend on the symtab being freed. This should be
1179 changed so that only those data structures affected are deleted. */
1180
1181 /* But don't delete anything if the symtab is empty.
1182 This test is necessary due to a bug in "dbxread.c" that
1183 causes empty symtabs to be created for N_SO symbols that
1184 contain the pathname of the object file. (This problem
1185 has been fixed in GDB 3.9x). */
1186
1187 bv = BLOCKVECTOR (s);
1188 if (BLOCKVECTOR_NBLOCKS (bv) > 2
1189 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1190 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1191 {
1192 complain (&oldsyms_complaint, name);
1193
1194 clear_symtab_users_queued++;
1195 make_cleanup (clear_symtab_users_once, 0);
1196 blewit = 1;
1197 } else {
1198 complain (&empty_symtab_complaint, name);
1199 }
1200
1201 free_symtab (s);
1202 }
1203 else
1204 {
1205 /* It is still possible that some breakpoints will be affected
1206 even though no symtab was found, since the file might have
1207 been compiled without debugging, and hence not be associated
1208 with a symtab. In order to handle this correctly, we would need
1209 to keep a list of text address ranges for undebuggable files.
1210 For now, we do nothing, since this is a fairly obscure case. */
1211 ;
1212 }
1213
1214 /* FIXME, what about the minimal symbol table? */
1215 return blewit;
1216 #else
1217 return (0);
1218 #endif
1219 }
1220 \f
1221 /* Allocate and partially fill a partial symtab. It will be
1222 completely filled at the end of the symbol list.
1223
1224 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1225 is the address relative to which its symbols are (incremental) or 0
1226 (normal). */
1227
1228
1229 struct partial_symtab *
1230 start_psymtab_common (objfile, addr,
1231 filename, textlow, global_syms, static_syms)
1232 struct objfile *objfile;
1233 CORE_ADDR addr;
1234 char *filename;
1235 CORE_ADDR textlow;
1236 struct partial_symbol *global_syms;
1237 struct partial_symbol *static_syms;
1238 {
1239 struct partial_symtab *psymtab;
1240
1241 psymtab = allocate_psymtab (filename, objfile);
1242 psymtab -> addr = addr;
1243 psymtab -> textlow = textlow;
1244 psymtab -> texthigh = psymtab -> textlow; /* default */
1245 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1246 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1247 return (psymtab);
1248 }
1249
1250 \f
1251 void
1252 _initialize_symfile ()
1253 {
1254
1255 add_com ("symbol-file", class_files, symbol_file_command,
1256 "Load symbol table from executable file FILE.\n\
1257 The `file' command can also load symbol tables, as well as setting the file\n\
1258 to execute.");
1259
1260 add_com ("add-symbol-file", class_files, add_symbol_file_command,
1261 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1262 The second argument provides the starting address of the file's text.");
1263
1264 add_com ("load", class_files, load_command,
1265 "Dynamically load FILE into the running program, and record its symbols\n\
1266 for access from GDB.");
1267
1268 add_show_from_set
1269 (add_set_cmd ("complaints", class_support, var_zinteger,
1270 (char *)&stop_whining,
1271 "Set max number of complaints about incorrect symbols.",
1272 &setlist),
1273 &showlist);
1274
1275 add_show_from_set
1276 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1277 (char *)&symbol_reloading,
1278 "Set dynamic symbol table reloading multiple times in one run.",
1279 &setlist),
1280 &showlist);
1281
1282 }
This page took 0.072298 seconds and 5 git commands to generate.