1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
5 This file is part of GDB.
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.
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.
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. */
31 #include "breakpoint.h"
33 #include "complaints.h"
35 #include "inferior.h" /* for write_pc */
40 #include <sys/types.h>
50 /* Global variables owned by this file */
51 int readnow_symbol_files; /* Read full symbols immediately */
53 struct complaint oldsyms_complaint = {
54 "Replacing old symbols for `%s'", 0, 0
57 struct complaint empty_symtab_complaint = {
58 "Empty symbol table found for `%s'", 0, 0
61 /* External variables and functions referenced. */
63 extern int info_verbose;
65 /* Functions this file defines */
68 set_initial_language PARAMS ((void));
71 load_command PARAMS ((char *, int));
74 add_symbol_file_command PARAMS ((char *, int));
77 cashier_psymtab PARAMS ((struct partial_symtab *));
80 compare_psymbols PARAMS ((const void *, const void *));
83 compare_symbols PARAMS ((const void *, const void *));
86 symfile_bfd_open PARAMS ((char *));
89 find_sym_fns PARAMS ((struct objfile *));
91 /* List of all available sym_fns. On gdb startup, each object file reader
92 calls add_symtab_fns() to register information on each format it is
95 static struct sym_fns *symtab_fns = NULL;
97 /* Structures with which to manage partial symbol allocation. */
99 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
101 /* Flag for whether user will be reloading symbols multiple times.
102 Defaults to ON for VxWorks, otherwise OFF. */
104 #ifdef SYMBOL_RELOADING_DEFAULT
105 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
107 int symbol_reloading = 0;
111 /* Since this function is called from within qsort, in an ANSI environment
112 it must conform to the prototype for qsort, which specifies that the
113 comparison function takes two "void *" pointers. */
116 compare_symbols (s1p, s2p)
120 register struct symbol **s1, **s2;
122 s1 = (struct symbol **) s1p;
123 s2 = (struct symbol **) s2p;
125 return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
132 compare_psymbols -- compare two partial symbols by name
136 Given pointer to two partial symbol table entries, compare
137 them by name and return -N, 0, or +N (ala strcmp). Typically
138 used by sorting routines like qsort().
142 Does direct compare of first two characters before punting
143 and passing to strcmp for longer compares. Note that the
144 original version had a bug whereby two null strings or two
145 identically named one character strings would return the
146 comparison of memory following the null byte.
151 compare_psymbols (s1p, s2p)
155 register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
156 register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
158 if ((st1[0] - st2[0]) || !st1[0])
160 return (st1[0] - st2[0]);
162 else if ((st1[1] - st2[1]) || !st1[1])
164 return (st1[1] - st2[1]);
168 return (STRCMP (st1 + 2, st2 + 2));
173 sort_pst_symbols (pst)
174 struct partial_symtab *pst;
176 /* Sort the global list; don't sort the static list */
178 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
179 pst -> n_global_syms, sizeof (struct partial_symbol),
183 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
187 register struct block *b;
189 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
190 sizeof (struct symbol *), compare_symbols);
193 /* Call sort_symtab_syms to sort alphabetically
194 the symbols of each block of one symtab. */
198 register struct symtab *s;
200 register struct blockvector *bv;
203 register struct block *b;
207 bv = BLOCKVECTOR (s);
208 nbl = BLOCKVECTOR_NBLOCKS (bv);
209 for (i = 0; i < nbl; i++)
211 b = BLOCKVECTOR_BLOCK (bv, i);
212 if (BLOCK_SHOULD_SORT (b))
217 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
218 (and add a null character at the end in the copy).
219 Returns the address of the copy. */
222 obsavestring (ptr, size, obstackp)
225 struct obstack *obstackp;
227 register char *p = (char *) obstack_alloc (obstackp, size + 1);
228 /* Open-coded memcpy--saves function call time.
229 These strings are usually short. */
231 register char *p1 = ptr;
232 register char *p2 = p;
233 char *end = ptr + size;
241 /* Concatenate strings S1, S2 and S3; return the new string.
242 Space is found in the symbol_obstack. */
245 obconcat (obstackp, s1, s2, s3)
246 struct obstack *obstackp;
247 const char *s1, *s2, *s3;
249 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
250 register char *val = (char *) obstack_alloc (obstackp, len);
257 /* Get the symbol table that corresponds to a partial_symtab.
258 This is fast after the first time you do it. In fact, there
259 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
263 psymtab_to_symtab (pst)
264 register struct partial_symtab *pst;
266 /* If it's been looked up before, return it. */
270 /* If it has not yet been read in, read it. */
273 (*pst->read_symtab) (pst);
279 /* Initialize entry point information for this objfile. */
282 init_entry_point_info (objfile)
283 struct objfile *objfile;
285 /* Save startup file's range of PC addresses to help blockframe.c
286 decide where the bottom of the stack is. */
288 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
290 /* Executable file -- record its entry point so we'll recognize
291 the startup file because it contains the entry point. */
292 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
296 /* Examination of non-executable.o files. Short-circuit this stuff. */
297 /* ~0 will not be in any file, we hope. */
298 objfile -> ei.entry_point = ~0;
299 /* set the startup file to be an empty range. */
300 objfile -> ei.entry_file_lowpc = 0;
301 objfile -> ei.entry_file_highpc = 0;
305 /* Get current entry point address. */
308 entry_point_address()
310 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
313 /* Remember the lowest-addressed loadable section we've seen.
314 This function is called via bfd_map_over_sections. */
316 #if 0 /* Not used yet */
318 find_lowest_section (abfd, sect, obj)
323 asection **lowest = (asection **)obj;
325 if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
328 *lowest = sect; /* First loadable section */
329 else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect))
330 *lowest = sect; /* A lower loadable section */
334 /* Process a symbol file, as either the main file or as a dynamically
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). */
347 syms_from_objfile (objfile, addr, mainline, verbo)
348 struct objfile *objfile;
353 struct section_offsets *section_offsets;
354 asection *lowest_sect;
355 struct cleanup *old_chain;
357 init_entry_point_info (objfile);
358 find_sym_fns (objfile);
360 /* Make sure that partially constructed symbol tables will be cleaned up
361 if an error occurs during symbol reading. */
362 old_chain = make_cleanup (free_objfile, objfile);
366 /* We will modify the main symbol table, make sure that all its users
367 will be cleaned up if an error occurs during symbol reading. */
368 make_cleanup (clear_symtab_users, 0);
370 /* Since no error yet, throw away the old symbol table. */
372 if (symfile_objfile != NULL)
374 free_objfile (symfile_objfile);
375 symfile_objfile = NULL;
378 /* Currently we keep symbols from the add-symbol-file command.
379 If the user wants to get rid of them, they should do "symbol-file"
380 without arguments first. Not sure this is the best behavior
383 (*objfile -> sf -> sym_new_init) (objfile);
386 /* Convert addr into an offset rather than an absolute address.
387 We find the lowest address of a loaded segment in the objfile,
388 and assume that <addr> is where that got loaded. Due to historical
389 precedent, we warn if that doesn't happen to be the ".text"
394 addr = 0; /* No offset from objfile addresses. */
398 lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
401 bfd_map_over_sections (objfile->obfd, find_lowest_section,
405 if (lowest_sect == 0)
406 warning ("no loadable sections found in added symbol-file %s",
408 else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect)
410 bfd_get_section_name (objfile->obfd, lowest_sect)))
411 /* FIXME-32x64--assumes bfd_vma fits in long. */
412 warning ("Lowest section in %s is %s at 0x%lx",
414 bfd_section_name (objfile->obfd, lowest_sect),
415 (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
418 addr -= bfd_section_vma (objfile->obfd, lowest_sect);
421 /* Initialize symbol reading routines for this objfile, allow complaints to
422 appear for this new file, and record how verbose to be, then do the
423 initial symbol reading for this file. */
425 (*objfile -> sf -> sym_init) (objfile);
426 clear_complaints (1, verbo);
428 section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
429 objfile->section_offsets = section_offsets;
431 #ifndef IBM6000_TARGET
432 /* This is a SVR4/SunOS specific hack, I think. In any event, it
433 screws RS/6000. sym_offsets should be doing this sort of thing,
434 because it knows the mapping between bfd sections and
436 /* This is a hack. As far as I can tell, section offsets are not
437 target dependent. They are all set to addr with a couple of
438 exceptions. The exceptions are sysvr4 shared libraries, whose
439 offsets are kept in solib structures anyway and rs6000 xcoff
440 which handles shared libraries in a completely unique way.
442 Section offsets are built similarly, except that they are built
443 by adding addr in all cases because there is no clear mapping
444 from section_offsets into actual sections. Note that solib.c
445 has a different algorythm for finding section offsets.
447 These should probably all be collapsed into some target
448 independent form of shared library support. FIXME. */
452 struct obj_section *s;
454 for (s = objfile->sections; s < objfile->sections_end; ++s)
456 s->addr -= s->offset;
458 s->endaddr -= s->offset;
463 #endif /* not IBM6000_TARGET */
465 (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
467 /* Don't allow char * to have a typename (else would get caddr_t).
468 Ditto void *. FIXME: Check whether this is now done by all the
469 symbol readers themselves (many of them now do), and if so remove
472 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
473 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
475 /* Mark the objfile has having had initial symbol read attempted. Note
476 that this does not mean we found any symbols... */
478 objfile -> flags |= OBJF_SYMS;
480 /* Discard cleanups as symbol reading was successful. */
482 discard_cleanups (old_chain);
485 /* Perform required actions after either reading in the initial
486 symbols for a new objfile, or mapping in the symbols from a reusable
490 new_symfile_objfile (objfile, mainline, verbo)
491 struct objfile *objfile;
496 /* If this is the main symbol file we have to clean up all users of the
497 old main symbol file. Otherwise it is sufficient to fixup all the
498 breakpoints that may have been redefined by this symbol file. */
501 /* OK, make it the "real" symbol file. */
502 symfile_objfile = objfile;
504 clear_symtab_users ();
508 breakpoint_re_set ();
511 /* We're done reading the symbol file; finish off complaints. */
512 clear_complaints (0, verbo);
515 /* Process a symbol file, as either the main file or as a dynamically
518 NAME is the file name (which will be tilde-expanded and made
519 absolute herein) (but we don't free or modify NAME itself).
520 FROM_TTY says how verbose to be. MAINLINE specifies whether this
521 is the main symbol file, or whether it's an extra symbol file such
522 as dynamically loaded code. If !mainline, ADDR is the address
523 where the text segment was loaded.
525 Upon success, returns a pointer to the objfile that was added.
526 Upon failure, jumps back to command level (never returns). */
529 symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
537 struct objfile *objfile;
538 struct partial_symtab *psymtab;
541 /* Open a bfd for the file, and give user a chance to burp if we'd be
542 interactively wiping out any existing symbols. */
544 abfd = symfile_bfd_open (name);
546 if ((have_full_symbols () || have_partial_symbols ())
549 && !query ("Load new symbol table from \"%s\"? ", name))
550 error ("Not confirmed.");
552 objfile = allocate_objfile (abfd, mapped);
554 /* If the objfile uses a mapped symbol file, and we have a psymtab for
555 it, then skip reading any symbols at this time. */
557 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
559 /* We mapped in an existing symbol table file that already has had
560 initial symbol reading performed, so we can skip that part. Notify
561 the user that instead of reading the symbols, they have been mapped.
563 if (from_tty || info_verbose)
565 printf_filtered ("Mapped symbols for %s...", name);
567 gdb_flush (gdb_stdout);
569 init_entry_point_info (objfile);
570 find_sym_fns (objfile);
574 /* We either created a new mapped symbol table, mapped an existing
575 symbol table file which has not had initial symbol reading
576 performed, or need to read an unmapped symbol table. */
577 if (from_tty || info_verbose)
579 printf_filtered ("Reading symbols from %s...", name);
581 gdb_flush (gdb_stdout);
583 syms_from_objfile (objfile, addr, mainline, from_tty);
586 /* We now have at least a partial symbol table. Check to see if the
587 user requested that all symbols be read on initial access via either
588 the gdb startup command line or on a per symbol file basis. Expand
589 all partial symbol tables for this objfile if so. */
591 if (readnow || readnow_symbol_files)
593 if (from_tty || info_verbose)
595 printf_filtered ("expanding to full symbols...");
597 gdb_flush (gdb_stdout);
600 for (psymtab = objfile -> psymtabs;
602 psymtab = psymtab -> next)
604 psymtab_to_symtab (psymtab);
608 if (from_tty || info_verbose)
610 printf_filtered ("done.\n");
611 gdb_flush (gdb_stdout);
614 new_symfile_objfile (objfile, mainline, from_tty);
616 /* Getting new symbols may change our opinion about what is
619 reinit_frame_cache ();
624 /* This is the symbol-file command. Read the file, analyze its
625 symbols, and add a struct symtab to a symtab list. The syntax of
626 the command is rather bizarre--(1) buildargv implements various
627 quoting conventions which are undocumented and have little or
628 nothing in common with the way things are quoted (or not quoted)
629 elsewhere in GDB, (2) options are used, which are not generally
630 used in GDB (perhaps "set mapped on", "set readnow on" would be
631 better), (3) the order of options matters, which is contrary to GNU
632 conventions (because it is confusing and inconvenient). */
635 symbol_file_command (args, from_tty)
641 CORE_ADDR text_relocation = 0; /* text_relocation */
642 struct cleanup *cleanups;
650 if ((have_full_symbols () || have_partial_symbols ())
652 && !query ("Discard symbol table from `%s'? ",
653 symfile_objfile -> name))
654 error ("Not confirmed.");
655 free_all_objfiles ();
656 symfile_objfile = NULL;
659 printf_unfiltered ("No symbol file now.\n");
664 if ((argv = buildargv (args)) == NULL)
668 cleanups = make_cleanup (freeargv, (char *) argv);
669 while (*argv != NULL)
671 if (STREQ (*argv, "-mapped"))
675 else if (STREQ (*argv, "-readnow"))
679 else if (**argv == '-')
681 error ("unknown option `%s'", *argv);
689 /* this is for rombug remote only, to get the text relocation by
690 using link command */
691 p = strrchr(name, '/');
695 target_link(p, &text_relocation);
697 if (text_relocation == (CORE_ADDR)0)
699 else if (text_relocation == (CORE_ADDR)-1)
700 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
703 symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
705 set_initial_language ();
712 error ("no symbol file name was specified");
714 do_cleanups (cleanups);
718 /* Set the initial language.
720 A better solution would be to record the language in the psymtab when reading
721 partial symbols, and then use it (if known) to set the language. This would
722 be a win for formats that encode the language in an easily discoverable place,
723 such as DWARF. For stabs, we can jump through hoops looking for specially
724 named symbols or try to intuit the language from the specific type of stabs
725 we find, but we can't do that until later when we read in full symbols.
729 set_initial_language ()
731 struct partial_symtab *pst;
732 enum language lang = language_unknown;
734 pst = find_main_psymtab ();
737 if (pst -> filename != NULL)
739 lang = deduce_language_from_filename (pst -> filename);
741 if (lang == language_unknown)
743 /* Make C the default language */
747 expected_language = current_language; /* Don't warn the user */
751 /* Open file specified by NAME and hand it off to BFD for preliminary
752 analysis. Result is a newly initialized bfd *, which includes a newly
753 malloc'd` copy of NAME (tilde-expanded and made absolute).
754 In case of trouble, error() is called. */
757 symfile_bfd_open (name)
764 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
766 /* Look down path for it, allocate 2nd new malloc'd copy. */
767 desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
770 make_cleanup (free, name);
771 perror_with_name (name);
773 free (name); /* Free 1st new malloc'd copy */
774 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
775 /* It'll be freed in free_objfile(). */
777 sym_bfd = bfd_fdopenr (name, gnutarget, desc);
781 make_cleanup (free, name);
782 error ("\"%s\": can't open to read symbols: %s.", name,
783 bfd_errmsg (bfd_get_error ()));
785 sym_bfd->cacheable = true;
787 if (!bfd_check_format (sym_bfd, bfd_object))
789 bfd_close (sym_bfd); /* This also closes desc */
790 make_cleanup (free, name);
791 error ("\"%s\": can't read symbols: %s.", name,
792 bfd_errmsg (bfd_get_error ()));
798 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
799 startup by the _initialize routine in each object file format reader,
800 to register information about each format the the reader is prepared
807 sf->next = symtab_fns;
812 /* Initialize to read symbols from the symbol file sym_bfd. It either
813 returns or calls error(). The result is an initialized struct sym_fns
814 in the objfile structure, that contains cached information about the
818 find_sym_fns (objfile)
819 struct objfile *objfile;
822 enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
823 char *our_target = bfd_get_target (objfile -> obfd);
825 /* Special kludge for RS/6000. See xcoffread.c. */
826 if (STREQ (our_target, "aixcoff-rs6000"))
827 our_flavour = (enum bfd_flavour)-1;
829 /* Special kludge for apollo. See dstread.c. */
830 if (STREQN (our_target, "apollo", 6))
831 our_flavour = (enum bfd_flavour)-2;
833 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
835 if (our_flavour == sf -> sym_flavour)
841 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
842 bfd_get_target (objfile -> obfd));
845 /* This function runs the load command of our current target. */
848 load_command (arg, from_tty)
852 target_load (arg, from_tty);
855 /* This version of "load" should be usable for any target. Currently
856 it is just used for remote targets, not inftarg.c or core files,
857 on the theory that only in that case is it useful.
859 Avoiding xmodem and the like seems like a win (a) because we don't have
860 to worry about finding it, and (b) On VMS, fork() is very slow and so
861 we don't want to run a subprocess. On the other hand, I'm not sure how
862 performance compares. */
864 generic_load (filename, from_tty)
868 struct cleanup *old_cleanups;
872 if (filename == NULL)
873 filename = get_exec_file (1);
875 loadfile_bfd = bfd_openr (filename, gnutarget);
876 if (loadfile_bfd == NULL)
878 perror_with_name (filename);
881 old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
883 if (!bfd_check_format (loadfile_bfd, bfd_object))
885 error ("\"%s\" is not an object file: %s", filename,
886 bfd_errmsg (bfd_get_error ()));
889 for (s = loadfile_bfd->sections; s; s = s->next)
891 if (s->flags & SEC_LOAD)
895 size = bfd_get_section_size_before_reloc (s);
899 struct cleanup *old_chain;
902 buffer = xmalloc (size);
903 old_chain = make_cleanup (free, buffer);
905 vma = bfd_get_section_vma (loadfile_bfd, s);
907 /* Is this really necessary? I guess it gives the user something
908 to look at during a long download. */
909 printf_filtered ("Loading section %s, size 0x%lx vma ",
910 bfd_get_section_name (loadfile_bfd, s),
911 (unsigned long) size);
912 print_address_numeric (vma, 1, gdb_stdout);
913 printf_filtered ("\n");
915 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
917 target_write_memory (vma, buffer, size);
919 do_cleanups (old_chain);
924 /* We were doing this in remote-mips.c, I suspect it is right
925 for other targets too. */
926 write_pc (loadfile_bfd->start_address);
928 /* FIXME: are we supposed to call symbol_file_add or not? According to
929 a comment from remote-mips.c (where a call to symbol_file_add was
930 commented out), making the call confuses GDB if more than one file is
931 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
934 do_cleanups (old_cleanups);
937 /* This function allows the addition of incrementally linked object files.
938 It does not modify any state in the target, only in the debugger. */
942 add_symbol_file_command (args, from_tty)
956 error ("add-symbol-file takes a file name and an address");
959 /* Make a copy of the string that we can safely write into. */
961 args = strdup (args);
962 make_cleanup (free, args);
964 /* Pick off any -option args and the file name. */
966 while ((*args != '\000') && (name == NULL))
968 while (isspace (*args)) {args++;}
970 while ((*args != '\000') && !isspace (*args)) {args++;}
979 else if (STREQ (arg, "-mapped"))
983 else if (STREQ (arg, "-readnow"))
989 error ("unknown option `%s'", arg);
993 /* After picking off any options and the file name, args should be
994 left pointing at the remainder of the command line, which should
995 be the address expression to evaluate. */
999 error ("add-symbol-file takes a file name");
1001 name = tilde_expand (name);
1002 make_cleanup (free, name);
1004 if (*args != '\000')
1006 text_addr = parse_and_eval_address (args);
1010 target_link(name, &text_addr);
1011 if (text_addr == (CORE_ADDR)-1)
1012 error("Don't know how to get text start location for this file");
1015 /* FIXME-32x64: Assumes text_addr fits in a long. */
1016 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
1017 name, local_hex_string ((unsigned long)text_addr)))
1018 error ("Not confirmed.");
1020 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
1023 /* Re-read symbols if a symbol-file has changed. */
1027 struct objfile *objfile;
1030 struct stat new_statbuf;
1033 /* With the addition of shared libraries, this should be modified,
1034 the load time should be saved in the partial symbol tables, since
1035 different tables may come from different source files. FIXME.
1036 This routine should then walk down each partial symbol table
1037 and see if the symbol table that it originates from has been changed */
1039 for (objfile = object_files; objfile; objfile = objfile->next) {
1040 if (objfile->obfd) {
1041 #ifdef IBM6000_TARGET
1042 /* If this object is from a shared library, then you should
1043 stat on the library name, not member name. */
1045 if (objfile->obfd->my_archive)
1046 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1049 res = stat (objfile->name, &new_statbuf);
1051 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1052 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1056 new_modtime = new_statbuf.st_mtime;
1057 if (new_modtime != objfile->mtime)
1059 struct cleanup *old_cleanups;
1060 struct section_offsets *offsets;
1062 int section_offsets_size;
1064 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1067 /* There are various functions like symbol_file_add,
1068 symfile_bfd_open, syms_from_objfile, etc., which might
1069 appear to do what we want. But they have various other
1070 effects which we *don't* want. So we just do stuff
1071 ourselves. We don't worry about mapped files (for one thing,
1072 any mapped file will be out of date). */
1074 /* If we get an error, blow away this objfile (not sure if
1075 that is the correct response for things like shared
1077 old_cleanups = make_cleanup (free_objfile, objfile);
1078 /* We need to do this whenever any symbols go away. */
1079 make_cleanup (clear_symtab_users, 0);
1081 /* Clean up any state BFD has sitting around. We don't need
1082 to close the descriptor but BFD lacks a way of closing the
1083 BFD without closing the descriptor. */
1084 if (!bfd_close (objfile->obfd))
1085 error ("Can't close BFD for %s.", objfile->name);
1086 objfile->obfd = bfd_openr (objfile->name, gnutarget);
1087 if (objfile->obfd == NULL)
1088 error ("Can't open %s to read symbols.", objfile->name);
1089 /* bfd_openr sets cacheable to true, which is what we want. */
1090 if (!bfd_check_format (objfile->obfd, bfd_object))
1091 error ("Can't read symbols from %s: %s.", objfile->name,
1092 bfd_errmsg (bfd_get_error ()));
1094 /* Save the offsets, we will nuke them with the rest of the
1096 num_offsets = objfile->num_sections;
1097 section_offsets_size =
1098 sizeof (struct section_offsets)
1099 + sizeof (objfile->section_offsets->offsets) * num_offsets;
1100 offsets = (struct section_offsets *) alloca (section_offsets_size);
1101 memcpy (offsets, objfile->section_offsets, section_offsets_size);
1103 /* Nuke all the state that we will re-read. Much of the following
1104 code which sets things to NULL really is necessary to tell
1105 other parts of GDB that there is nothing currently there. */
1107 /* FIXME: Do we have to free a whole linked list, or is this
1109 if (objfile->global_psymbols.list)
1110 mfree (objfile->md, objfile->global_psymbols.list);
1111 objfile->global_psymbols.list = NULL;
1112 objfile->global_psymbols.next = NULL;
1113 objfile->global_psymbols.size = 0;
1114 if (objfile->static_psymbols.list)
1115 mfree (objfile->md, objfile->static_psymbols.list);
1116 objfile->static_psymbols.list = NULL;
1117 objfile->static_psymbols.next = NULL;
1118 objfile->static_psymbols.size = 0;
1120 /* Free the obstacks for non-reusable objfiles */
1121 obstack_free (&objfile -> psymbol_obstack, 0);
1122 obstack_free (&objfile -> symbol_obstack, 0);
1123 obstack_free (&objfile -> type_obstack, 0);
1124 objfile->sections = NULL;
1125 objfile->symtabs = NULL;
1126 objfile->psymtabs = NULL;
1127 objfile->free_psymtabs = NULL;
1128 objfile->msymbols = NULL;
1129 objfile->minimal_symbol_count= 0;
1130 objfile->fundamental_types = NULL;
1131 if (objfile -> sf != NULL)
1133 (*objfile -> sf -> sym_finish) (objfile);
1136 /* We never make this a mapped file. */
1137 objfile -> md = NULL;
1138 /* obstack_specify_allocation also initializes the obstack so
1140 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1142 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1144 obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1146 if (build_objfile_section_table (objfile))
1148 error ("Can't find the file sections in `%s': %s",
1149 objfile -> name, bfd_errmsg (bfd_get_error ()));
1152 /* We use the same section offsets as from last time. I'm not
1153 sure whether that is always correct for shared libraries. */
1154 objfile->section_offsets = (struct section_offsets *)
1155 obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1156 memcpy (objfile->section_offsets, offsets, section_offsets_size);
1157 objfile->num_sections = num_offsets;
1159 /* What the hell is sym_new_init for, anyway? The concept of
1160 distinguishing between the main file and additional files
1161 in this way seems rather dubious. */
1162 if (objfile == symfile_objfile)
1163 (*objfile->sf->sym_new_init) (objfile);
1165 (*objfile->sf->sym_init) (objfile);
1166 clear_complaints (1, 1);
1167 /* The "mainline" parameter is a hideous hack; I think leaving it
1168 zero is OK since dbxread.c also does what it needs to do if
1169 objfile->global_psymbols.size is 0. */
1170 (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
1171 objfile -> flags |= OBJF_SYMS;
1173 /* We're done reading the symbol file; finish off complaints. */
1174 clear_complaints (0, 1);
1176 /* Getting new symbols may change our opinion about what is
1179 reinit_frame_cache ();
1181 /* Discard cleanups as symbol reading was successful. */
1182 discard_cleanups (old_cleanups);
1184 /* If the mtime has changed between the time we set new_modtime
1185 and now, we *want* this to be out of date, so don't call stat
1187 objfile->mtime = new_modtime;
1194 clear_symtab_users ();
1199 deduce_language_from_filename (filename)
1206 else if (0 == (c = strrchr (filename, '.')))
1207 ; /* Get default. */
1208 else if (STREQ(c,".mod"))
1210 else if (STREQ(c,".c"))
1212 else if (STREQ(c,".s"))
1213 return language_asm;
1214 else if (STREQ (c,".cc") || STREQ (c,".C") || STREQ (c, ".cxx")
1215 || STREQ (c, ".cpp"))
1216 return language_cplus;
1217 else if (STREQ (c,".ch") || STREQ (c,".c186") || STREQ (c,".c286"))
1218 return language_chill;
1220 return language_unknown; /* default */
1225 Allocate and partly initialize a new symbol table. Return a pointer
1226 to it. error() if no space.
1228 Caller must set these fields:
1234 initialize any EXTRA_SYMTAB_INFO
1235 possibly free_named_symtabs (symtab->filename);
1239 allocate_symtab (filename, objfile)
1241 struct objfile *objfile;
1243 register struct symtab *symtab;
1245 symtab = (struct symtab *)
1246 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
1247 memset (symtab, 0, sizeof (*symtab));
1248 symtab -> filename = obsavestring (filename, strlen (filename),
1249 &objfile -> symbol_obstack);
1250 symtab -> fullname = NULL;
1251 symtab -> language = deduce_language_from_filename (filename);
1253 /* Hook it to the objfile it comes from */
1255 symtab -> objfile = objfile;
1256 symtab -> next = objfile -> symtabs;
1257 objfile -> symtabs = symtab;
1259 #ifdef INIT_EXTRA_SYMTAB_INFO
1260 INIT_EXTRA_SYMTAB_INFO (symtab);
1266 struct partial_symtab *
1267 allocate_psymtab (filename, objfile)
1269 struct objfile *objfile;
1271 struct partial_symtab *psymtab;
1273 if (objfile -> free_psymtabs)
1275 psymtab = objfile -> free_psymtabs;
1276 objfile -> free_psymtabs = psymtab -> next;
1279 psymtab = (struct partial_symtab *)
1280 obstack_alloc (&objfile -> psymbol_obstack,
1281 sizeof (struct partial_symtab));
1283 memset (psymtab, 0, sizeof (struct partial_symtab));
1284 psymtab -> filename = obsavestring (filename, strlen (filename),
1285 &objfile -> psymbol_obstack);
1286 psymtab -> symtab = NULL;
1288 /* Hook it to the objfile it comes from */
1290 psymtab -> objfile = objfile;
1291 psymtab -> next = objfile -> psymtabs;
1292 objfile -> psymtabs = psymtab;
1298 /* Reset all data structures in gdb which may contain references to symbol
1302 clear_symtab_users ()
1304 /* Someday, we should do better than this, by only blowing away
1305 the things that really need to be blown. */
1306 clear_value_history ();
1308 clear_internalvars ();
1309 breakpoint_re_set ();
1310 set_default_breakpoint (0, 0, 0, 0);
1311 current_source_symtab = 0;
1312 current_source_line = 0;
1313 clear_pc_function_cache ();
1316 /* clear_symtab_users_once:
1318 This function is run after symbol reading, or from a cleanup.
1319 If an old symbol table was obsoleted, the old symbol table
1320 has been blown away, but the other GDB data structures that may
1321 reference it have not yet been cleared or re-directed. (The old
1322 symtab was zapped, and the cleanup queued, in free_named_symtab()
1325 This function can be queued N times as a cleanup, or called
1326 directly; it will do all the work the first time, and then will be a
1327 no-op until the next time it is queued. This works by bumping a
1328 counter at queueing time. Much later when the cleanup is run, or at
1329 the end of symbol processing (in case the cleanup is discarded), if
1330 the queued count is greater than the "done-count", we do the work
1331 and set the done-count to the queued count. If the queued count is
1332 less than or equal to the done-count, we just ignore the call. This
1333 is needed because reading a single .o file will often replace many
1334 symtabs (one per .h file, for example), and we don't want to reset
1335 the breakpoints N times in the user's face.
1337 The reason we both queue a cleanup, and call it directly after symbol
1338 reading, is because the cleanup protects us in case of errors, but is
1339 discarded if symbol reading is successful. */
1342 /* FIXME: As free_named_symtabs is currently a big noop this function
1343 is no longer needed. */
1345 clear_symtab_users_once PARAMS ((void));
1347 static int clear_symtab_users_queued;
1348 static int clear_symtab_users_done;
1351 clear_symtab_users_once ()
1353 /* Enforce once-per-`do_cleanups'-semantics */
1354 if (clear_symtab_users_queued <= clear_symtab_users_done)
1356 clear_symtab_users_done = clear_symtab_users_queued;
1358 clear_symtab_users ();
1362 /* Delete the specified psymtab, and any others that reference it. */
1365 cashier_psymtab (pst)
1366 struct partial_symtab *pst;
1368 struct partial_symtab *ps, *pprev = NULL;
1371 /* Find its previous psymtab in the chain */
1372 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1379 /* Unhook it from the chain. */
1380 if (ps == pst->objfile->psymtabs)
1381 pst->objfile->psymtabs = ps->next;
1383 pprev->next = ps->next;
1385 /* FIXME, we can't conveniently deallocate the entries in the
1386 partial_symbol lists (global_psymbols/static_psymbols) that
1387 this psymtab points to. These just take up space until all
1388 the psymtabs are reclaimed. Ditto the dependencies list and
1389 filename, which are all in the psymbol_obstack. */
1391 /* We need to cashier any psymtab that has this one as a dependency... */
1393 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1394 for (i = 0; i < ps->number_of_dependencies; i++) {
1395 if (ps->dependencies[i] == pst) {
1396 cashier_psymtab (ps);
1397 goto again; /* Must restart, chain has been munged. */
1404 /* If a symtab or psymtab for filename NAME is found, free it along
1405 with any dependent breakpoints, displays, etc.
1406 Used when loading new versions of object modules with the "add-file"
1407 command. This is only called on the top-level symtab or psymtab's name;
1408 it is not called for subsidiary files such as .h files.
1410 Return value is 1 if we blew away the environment, 0 if not.
1411 FIXME. The return valu appears to never be used.
1413 FIXME. I think this is not the best way to do this. We should
1414 work on being gentler to the environment while still cleaning up
1415 all stray pointers into the freed symtab. */
1418 free_named_symtabs (name)
1422 /* FIXME: With the new method of each objfile having it's own
1423 psymtab list, this function needs serious rethinking. In particular,
1424 why was it ever necessary to toss psymtabs with specific compilation
1425 unit filenames, as opposed to all psymtabs from a particular symbol
1427 Well, the answer is that some systems permit reloading of particular
1428 compilation units. We want to blow away any old info about these
1429 compilation units, regardless of which objfiles they arrived in. --gnu. */
1431 register struct symtab *s;
1432 register struct symtab *prev;
1433 register struct partial_symtab *ps;
1434 struct blockvector *bv;
1437 /* We only wack things if the symbol-reload switch is set. */
1438 if (!symbol_reloading)
1441 /* Some symbol formats have trouble providing file names... */
1442 if (name == 0 || *name == '\0')
1445 /* Look for a psymtab with the specified name. */
1448 for (ps = partial_symtab_list; ps; ps = ps->next) {
1449 if (STREQ (name, ps->filename)) {
1450 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1451 goto again2; /* Must restart, chain has been munged */
1455 /* Look for a symtab with the specified name. */
1457 for (s = symtab_list; s; s = s->next)
1459 if (STREQ (name, s->filename))
1466 if (s == symtab_list)
1467 symtab_list = s->next;
1469 prev->next = s->next;
1471 /* For now, queue a delete for all breakpoints, displays, etc., whether
1472 or not they depend on the symtab being freed. This should be
1473 changed so that only those data structures affected are deleted. */
1475 /* But don't delete anything if the symtab is empty.
1476 This test is necessary due to a bug in "dbxread.c" that
1477 causes empty symtabs to be created for N_SO symbols that
1478 contain the pathname of the object file. (This problem
1479 has been fixed in GDB 3.9x). */
1481 bv = BLOCKVECTOR (s);
1482 if (BLOCKVECTOR_NBLOCKS (bv) > 2
1483 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1484 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1486 complain (&oldsyms_complaint, name);
1488 clear_symtab_users_queued++;
1489 make_cleanup (clear_symtab_users_once, 0);
1492 complain (&empty_symtab_complaint, name);
1499 /* It is still possible that some breakpoints will be affected
1500 even though no symtab was found, since the file might have
1501 been compiled without debugging, and hence not be associated
1502 with a symtab. In order to handle this correctly, we would need
1503 to keep a list of text address ranges for undebuggable files.
1504 For now, we do nothing, since this is a fairly obscure case. */
1508 /* FIXME, what about the minimal symbol table? */
1515 /* Allocate and partially fill a partial symtab. It will be
1516 completely filled at the end of the symbol list.
1518 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1519 is the address relative to which its symbols are (incremental) or 0
1523 struct partial_symtab *
1524 start_psymtab_common (objfile, section_offsets,
1525 filename, textlow, global_syms, static_syms)
1526 struct objfile *objfile;
1527 struct section_offsets *section_offsets;
1530 struct partial_symbol *global_syms;
1531 struct partial_symbol *static_syms;
1533 struct partial_symtab *psymtab;
1535 psymtab = allocate_psymtab (filename, objfile);
1536 psymtab -> section_offsets = section_offsets;
1537 psymtab -> textlow = textlow;
1538 psymtab -> texthigh = psymtab -> textlow; /* default */
1539 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1540 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1544 /* Debugging versions of functions that are usually inline macros
1547 #if !INLINE_ADD_PSYMBOL
1549 /* Add a symbol with a long value to a psymtab.
1550 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1553 add_psymbol_to_list (name, namelength, namespace, class, list, val, language,
1557 enum namespace namespace;
1558 enum address_class class;
1559 struct psymbol_allocation_list *list;
1561 enum language language;
1562 struct objfile *objfile;
1564 register struct partial_symbol *psym;
1565 register char *demangled_name;
1567 if (list->next >= list->list + list->size)
1569 extend_psymbol_list (list,objfile);
1571 psym = list->next++;
1573 SYMBOL_NAME (psym) =
1574 (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
1575 memcpy (SYMBOL_NAME (psym), name, namelength);
1576 SYMBOL_NAME (psym)[namelength] = '\0';
1577 SYMBOL_VALUE (psym) = val;
1578 SYMBOL_LANGUAGE (psym) = language;
1579 PSYMBOL_NAMESPACE (psym) = namespace;
1580 PSYMBOL_CLASS (psym) = class;
1581 SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack);
1584 /* Add a symbol with a CORE_ADDR value to a psymtab. */
1587 add_psymbol_addr_to_list (name, namelength, namespace, class, list, val,
1591 enum namespace namespace;
1592 enum address_class class;
1593 struct psymbol_allocation_list *list;
1595 enum language language;
1596 struct objfile *objfile;
1598 register struct partial_symbol *psym;
1599 register char *demangled_name;
1601 if (list->next >= list->list + list->size)
1603 extend_psymbol_list (list,objfile);
1605 psym = list->next++;
1607 SYMBOL_NAME (psym) =
1608 (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
1609 memcpy (SYMBOL_NAME (psym), name, namelength);
1610 SYMBOL_NAME (psym)[namelength] = '\0';
1611 SYMBOL_VALUE_ADDRESS (psym) = val;
1612 SYMBOL_LANGUAGE (psym) = language;
1613 PSYMBOL_NAMESPACE (psym) = namespace;
1614 PSYMBOL_CLASS (psym) = class;
1615 SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack);
1618 #endif /* !INLINE_ADD_PSYMBOL */
1622 _initialize_symfile ()
1624 struct cmd_list_element *c;
1626 c = add_cmd ("symbol-file", class_files, symbol_file_command,
1627 "Load symbol table from executable file FILE.\n\
1628 The `file' command can also load symbol tables, as well as setting the file\n\
1629 to execute.", &cmdlist);
1630 c->completer = filename_completer;
1632 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
1633 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1634 The second argument provides the starting address of the file's text.",
1636 c->completer = filename_completer;
1638 c = add_cmd ("load", class_files, load_command,
1639 "Dynamically load FILE into the running program, and record its symbols\n\
1640 for access from GDB.", &cmdlist);
1641 c->completer = filename_completer;
1644 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1645 (char *)&symbol_reloading,
1646 "Set dynamic symbol table reloading multiple times in one run.",