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 */
52 int readnow_symbol_files
; /* Read full symbols immediately */
54 struct complaint oldsyms_complaint
= {
55 "Replacing old symbols for `%s'", 0, 0
58 struct complaint empty_symtab_complaint
= {
59 "Empty symbol table found for `%s'", 0, 0
62 /* External variables and functions referenced. */
64 extern int info_verbose
;
66 /* Functions this file defines */
69 set_initial_language
PARAMS ((void));
72 load_command
PARAMS ((char *, int));
75 add_symbol_file_command
PARAMS ((char *, int));
78 cashier_psymtab
PARAMS ((struct partial_symtab
*));
81 compare_psymbols
PARAMS ((const void *, const void *));
84 compare_symbols
PARAMS ((const void *, const void *));
87 symfile_bfd_open
PARAMS ((char *));
90 find_sym_fns
PARAMS ((struct objfile
*));
92 /* List of all available sym_fns. On gdb startup, each object file reader
93 calls add_symtab_fns() to register information on each format it is
96 static struct sym_fns
*symtab_fns
= NULL
;
98 /* Structures with which to manage partial symbol allocation. */
100 struct psymbol_allocation_list global_psymbols
= {0}, static_psymbols
= {0};
102 /* Flag for whether user will be reloading symbols multiple times.
103 Defaults to ON for VxWorks, otherwise OFF. */
105 #ifdef SYMBOL_RELOADING_DEFAULT
106 int symbol_reloading
= SYMBOL_RELOADING_DEFAULT
;
108 int symbol_reloading
= 0;
112 /* Since this function is called from within qsort, in an ANSI environment
113 it must conform to the prototype for qsort, which specifies that the
114 comparison function takes two "void *" pointers. */
117 compare_symbols (s1p
, s2p
)
121 register struct symbol
**s1
, **s2
;
123 s1
= (struct symbol
**) s1p
;
124 s2
= (struct symbol
**) s2p
;
126 return (STRCMP (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
)));
133 compare_psymbols -- compare two partial symbols by name
137 Given pointer to two partial symbol table entries, compare
138 them by name and return -N, 0, or +N (ala strcmp). Typically
139 used by sorting routines like qsort().
143 Does direct compare of first two characters before punting
144 and passing to strcmp for longer compares. Note that the
145 original version had a bug whereby two null strings or two
146 identically named one character strings would return the
147 comparison of memory following the null byte.
152 compare_psymbols (s1p
, s2p
)
156 register char *st1
= SYMBOL_NAME ((struct partial_symbol
*) s1p
);
157 register char *st2
= SYMBOL_NAME ((struct partial_symbol
*) s2p
);
159 if ((st1
[0] - st2
[0]) || !st1
[0])
161 return (st1
[0] - st2
[0]);
163 else if ((st1
[1] - st2
[1]) || !st1
[1])
165 return (st1
[1] - st2
[1]);
169 return (STRCMP (st1
+ 2, st2
+ 2));
174 sort_pst_symbols (pst
)
175 struct partial_symtab
*pst
;
177 /* Sort the global list; don't sort the static list */
179 qsort (pst
-> objfile
-> global_psymbols
.list
+ pst
-> globals_offset
,
180 pst
-> n_global_syms
, sizeof (struct partial_symbol
),
184 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
188 register struct block
*b
;
190 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
191 sizeof (struct symbol
*), compare_symbols
);
194 /* Call sort_symtab_syms to sort alphabetically
195 the symbols of each block of one symtab. */
199 register struct symtab
*s
;
201 register struct blockvector
*bv
;
204 register struct block
*b
;
208 bv
= BLOCKVECTOR (s
);
209 nbl
= BLOCKVECTOR_NBLOCKS (bv
);
210 for (i
= 0; i
< nbl
; i
++)
212 b
= BLOCKVECTOR_BLOCK (bv
, i
);
213 if (BLOCK_SHOULD_SORT (b
))
218 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
219 (and add a null character at the end in the copy).
220 Returns the address of the copy. */
223 obsavestring (ptr
, size
, obstackp
)
226 struct obstack
*obstackp
;
228 register char *p
= (char *) obstack_alloc (obstackp
, size
+ 1);
229 /* Open-coded memcpy--saves function call time.
230 These strings are usually short. */
232 register char *p1
= ptr
;
233 register char *p2
= p
;
234 char *end
= ptr
+ size
;
242 /* Concatenate strings S1, S2 and S3; return the new string.
243 Space is found in the symbol_obstack. */
246 obconcat (obstackp
, s1
, s2
, s3
)
247 struct obstack
*obstackp
;
248 const char *s1
, *s2
, *s3
;
250 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
251 register char *val
= (char *) obstack_alloc (obstackp
, len
);
258 /* Get the symbol table that corresponds to a partial_symtab.
259 This is fast after the first time you do it. In fact, there
260 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
264 psymtab_to_symtab (pst
)
265 register struct partial_symtab
*pst
;
267 /* If it's been looked up before, return it. */
271 /* If it has not yet been read in, read it. */
274 (*pst
->read_symtab
) (pst
);
280 /* Initialize entry point information for this objfile. */
283 init_entry_point_info (objfile
)
284 struct objfile
*objfile
;
286 /* Save startup file's range of PC addresses to help blockframe.c
287 decide where the bottom of the stack is. */
289 if (bfd_get_file_flags (objfile
-> obfd
) & EXEC_P
)
291 /* Executable file -- record its entry point so we'll recognize
292 the startup file because it contains the entry point. */
293 objfile
-> ei
.entry_point
= bfd_get_start_address (objfile
-> obfd
);
297 /* Examination of non-executable.o files. Short-circuit this stuff. */
298 /* ~0 will not be in any file, we hope. */
299 objfile
-> ei
.entry_point
= ~0;
300 /* set the startup file to be an empty range. */
301 objfile
-> ei
.entry_file_lowpc
= 0;
302 objfile
-> ei
.entry_file_highpc
= 0;
306 /* Get current entry point address. */
309 entry_point_address()
311 return symfile_objfile
? symfile_objfile
->ei
.entry_point
: 0;
314 /* Remember the lowest-addressed loadable section we've seen.
315 This function is called via bfd_map_over_sections. */
317 #if 0 /* Not used yet */
319 find_lowest_section (abfd
, sect
, obj
)
324 asection
**lowest
= (asection
**)obj
;
326 if (0 == (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
))
329 *lowest
= sect
; /* First loadable section */
330 else if (bfd_section_vma (abfd
, *lowest
) >= bfd_section_vma (abfd
, sect
))
331 *lowest
= sect
; /* A lower loadable section */
335 /* Process a symbol file, as either the main file or as a dynamically
338 NAME is the file name (which will be tilde-expanded and made
339 absolute herein) (but we don't free or modify NAME itself).
340 FROM_TTY says how verbose to be. MAINLINE specifies whether this
341 is the main symbol file, or whether it's an extra symbol file such
342 as dynamically loaded code. If !mainline, ADDR is the address
343 where the text segment was loaded. If VERBO, the caller has printed
344 a verbose message about the symbol reading (and complaints can be
345 more terse about it). */
348 syms_from_objfile (objfile
, addr
, mainline
, verbo
)
349 struct objfile
*objfile
;
354 struct section_offsets
*section_offsets
;
355 asection
*lowest_sect
;
356 struct cleanup
*old_chain
;
358 init_entry_point_info (objfile
);
359 find_sym_fns (objfile
);
361 /* Make sure that partially constructed symbol tables will be cleaned up
362 if an error occurs during symbol reading. */
363 old_chain
= make_cleanup (free_objfile
, objfile
);
367 /* We will modify the main symbol table, make sure that all its users
368 will be cleaned up if an error occurs during symbol reading. */
369 make_cleanup (clear_symtab_users
, 0);
371 /* Since no error yet, throw away the old symbol table. */
373 if (symfile_objfile
!= NULL
)
375 free_objfile (symfile_objfile
);
376 symfile_objfile
= NULL
;
379 (*objfile
-> sf
-> sym_new_init
) (objfile
);
382 /* Convert addr into an offset rather than an absolute address.
383 We find the lowest address of a loaded segment in the objfile,
384 and assume that <addr> is where that got loaded. Due to historical
385 precedent, we warn if that doesn't happen to be the ".text"
390 addr
= 0; /* No offset from objfile addresses. */
394 lowest_sect
= bfd_get_section_by_name (objfile
->obfd
, ".text");
397 bfd_map_over_sections (objfile
->obfd
, find_lowest_section
,
401 if (lowest_sect
== 0)
402 warning ("no loadable sections found in added symbol-file %s",
404 else if (0 == bfd_get_section_name (objfile
->obfd
, lowest_sect
)
406 bfd_get_section_name (objfile
->obfd
, lowest_sect
)))
407 warning ("Lowest section in %s is %s at 0x%lx",
409 bfd_section_name (objfile
->obfd
, lowest_sect
),
410 (unsigned long) bfd_section_vma (objfile
->obfd
, lowest_sect
));
413 addr
-= bfd_section_vma (objfile
->obfd
, lowest_sect
);
416 /* Initialize symbol reading routines for this objfile, allow complaints to
417 appear for this new file, and record how verbose to be, then do the
418 initial symbol reading for this file. */
420 (*objfile
-> sf
-> sym_init
) (objfile
);
421 clear_complaints (1, verbo
);
423 section_offsets
= (*objfile
-> sf
-> sym_offsets
) (objfile
, addr
);
424 objfile
->section_offsets
= section_offsets
;
426 #ifndef IBM6000_TARGET
427 /* This is a SVR4/SunOS specific hack, I think. In any event, it
428 screws RS/6000. sym_offsets should be doing this sort of thing,
429 because it knows the mapping between bfd sections and
431 /* This is a hack. As far as I can tell, section offsets are not
432 target dependent. They are all set to addr with a couple of
433 exceptions. The exceptions are sysvr4 shared libraries, whose
434 offsets are kept in solib structures anyway and rs6000 xcoff
435 which handles shared libraries in a completely unique way.
437 Section offsets are built similarly, except that they are built
438 by adding addr in all cases because there is no clear mapping
439 from section_offsets into actual sections. Note that solib.c
440 has a different algorythm for finding section offsets.
442 These should probably all be collapsed into some target
443 independent form of shared library support. FIXME. */
447 struct obj_section
*s
;
449 for (s
= objfile
->sections
; s
< objfile
->sections_end
; ++s
)
451 s
->addr
-= s
->offset
;
453 s
->endaddr
-= s
->offset
;
458 #endif /* not IBM6000_TARGET */
460 (*objfile
-> sf
-> sym_read
) (objfile
, section_offsets
, mainline
);
462 /* Don't allow char * to have a typename (else would get caddr_t).
463 Ditto void *. FIXME: Check whether this is now done by all the
464 symbol readers themselves (many of them now do), and if so remove
467 TYPE_NAME (lookup_pointer_type (builtin_type_char
)) = 0;
468 TYPE_NAME (lookup_pointer_type (builtin_type_void
)) = 0;
470 /* Mark the objfile has having had initial symbol read attempted. Note
471 that this does not mean we found any symbols... */
473 objfile
-> flags
|= OBJF_SYMS
;
475 /* Discard cleanups as symbol reading was successful. */
477 discard_cleanups (old_chain
);
480 /* Perform required actions after either reading in the initial
481 symbols for a new objfile, or mapping in the symbols from a reusable
485 new_symfile_objfile (objfile
, mainline
, verbo
)
486 struct objfile
*objfile
;
491 /* If this is the main symbol file we have to clean up all users of the
492 old main symbol file. Otherwise it is sufficient to fixup all the
493 breakpoints that may have been redefined by this symbol file. */
496 /* OK, make it the "real" symbol file. */
497 symfile_objfile
= objfile
;
499 clear_symtab_users ();
503 breakpoint_re_set ();
506 /* We're done reading the symbol file; finish off complaints. */
507 clear_complaints (0, verbo
);
510 /* Process a symbol file, as either the main file or as a dynamically
513 NAME is the file name (which will be tilde-expanded and made
514 absolute herein) (but we don't free or modify NAME itself).
515 FROM_TTY says how verbose to be. MAINLINE specifies whether this
516 is the main symbol file, or whether it's an extra symbol file such
517 as dynamically loaded code. If !mainline, ADDR is the address
518 where the text segment was loaded.
520 Upon success, returns a pointer to the objfile that was added.
521 Upon failure, jumps back to command level (never returns). */
524 symbol_file_add (name
, from_tty
, addr
, mainline
, mapped
, readnow
)
532 struct objfile
*objfile
;
533 struct partial_symtab
*psymtab
;
536 /* Open a bfd for the file, and give user a chance to burp if we'd be
537 interactively wiping out any existing symbols. */
539 abfd
= symfile_bfd_open (name
);
541 if ((have_full_symbols () || have_partial_symbols ())
544 && !query ("Load new symbol table from \"%s\"? ", name
))
545 error ("Not confirmed.");
547 objfile
= allocate_objfile (abfd
, mapped
);
549 /* If the objfile uses a mapped symbol file, and we have a psymtab for
550 it, then skip reading any symbols at this time. */
552 if ((objfile
-> flags
& OBJF_MAPPED
) && (objfile
-> flags
& OBJF_SYMS
))
554 /* We mapped in an existing symbol table file that already has had
555 initial symbol reading performed, so we can skip that part. Notify
556 the user that instead of reading the symbols, they have been mapped.
558 if (from_tty
|| info_verbose
)
560 printf_filtered ("Mapped symbols for %s...", name
);
564 init_entry_point_info (objfile
);
565 find_sym_fns (objfile
);
569 /* We either created a new mapped symbol table, mapped an existing
570 symbol table file which has not had initial symbol reading
571 performed, or need to read an unmapped symbol table. */
572 if (from_tty
|| info_verbose
)
574 printf_filtered ("Reading symbols from %s...", name
);
578 syms_from_objfile (objfile
, addr
, mainline
, from_tty
);
581 /* We now have at least a partial symbol table. Check to see if the
582 user requested that all symbols be read on initial access via either
583 the gdb startup command line or on a per symbol file basis. Expand
584 all partial symbol tables for this objfile if so. */
586 if (readnow
|| readnow_symbol_files
)
588 if (from_tty
|| info_verbose
)
590 printf_filtered ("expanding to full symbols...");
595 for (psymtab
= objfile
-> psymtabs
;
597 psymtab
= psymtab
-> next
)
599 psymtab_to_symtab (psymtab
);
603 if (from_tty
|| info_verbose
)
605 printf_filtered ("done.\n");
609 new_symfile_objfile (objfile
, mainline
, from_tty
);
611 /* Getting new symbols may change our opinion about what is
614 reinit_frame_cache ();
619 /* This is the symbol-file command. Read the file, analyze its symbols,
620 and add a struct symtab to a symtab list. */
623 symbol_file_command (args
, from_tty
)
629 struct cleanup
*cleanups
;
637 if ((have_full_symbols () || have_partial_symbols ())
639 && !query ("Discard symbol table from `%s'? ",
640 symfile_objfile
-> name
))
641 error ("Not confirmed.");
642 free_all_objfiles ();
643 symfile_objfile
= NULL
;
646 printf ("No symbol file now.\n");
651 if ((argv
= buildargv (args
)) == NULL
)
655 cleanups
= make_cleanup (freeargv
, (char *) argv
);
656 while (*argv
!= NULL
)
658 if (STREQ (*argv
, "-mapped"))
662 else if (STREQ (*argv
, "-readnow"))
666 else if (**argv
== '-')
668 error ("unknown option `%s'", *argv
);
679 error ("no symbol file name was specified");
683 symbol_file_add (name
, from_tty
, (CORE_ADDR
)0, 1, mapped
, readnow
);
684 set_initial_language ();
686 do_cleanups (cleanups
);
690 /* Set the initial language.
692 A better solution would be to record the language in the psymtab when reading
693 partial symbols, and then use it (if known) to set the language. This would
694 be a win for formats that encode the language in an easily discoverable place,
695 such as DWARF. For stabs, we can jump through hoops looking for specially
696 named symbols or try to intuit the language from the specific type of stabs
697 we find, but we can't do that until later when we read in full symbols.
701 set_initial_language ()
703 struct partial_symtab
*pst
;
704 enum language lang
= language_unknown
;
706 pst
= find_main_psymtab ();
709 if (pst
-> filename
!= NULL
)
711 lang
= deduce_language_from_filename (pst
-> filename
);
713 if (lang
== language_unknown
)
715 /* Make C the default language */
719 expected_language
= current_language
; /* Don't warn the user */
723 /* Open file specified by NAME and hand it off to BFD for preliminary
724 analysis. Result is a newly initialized bfd *, which includes a newly
725 malloc'd` copy of NAME (tilde-expanded and made absolute).
726 In case of trouble, error() is called. */
729 symfile_bfd_open (name
)
736 name
= tilde_expand (name
); /* Returns 1st new malloc'd copy */
738 /* Look down path for it, allocate 2nd new malloc'd copy. */
739 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
| O_BINARY
, 0, &absolute_name
);
742 make_cleanup (free
, name
);
743 perror_with_name (name
);
745 free (name
); /* Free 1st new malloc'd copy */
746 name
= absolute_name
; /* Keep 2nd malloc'd copy in bfd */
747 /* It'll be freed in free_objfile(). */
749 sym_bfd
= bfd_fdopenr (name
, gnutarget
, desc
);
753 make_cleanup (free
, name
);
754 error ("\"%s\": can't open to read symbols: %s.", name
,
755 bfd_errmsg (bfd_error
));
757 sym_bfd
->cacheable
= true;
759 if (!bfd_check_format (sym_bfd
, bfd_object
))
761 bfd_close (sym_bfd
); /* This also closes desc */
762 make_cleanup (free
, name
);
763 error ("\"%s\": can't read symbols: %s.", name
,
764 bfd_errmsg (bfd_error
));
770 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
771 startup by the _initialize routine in each object file format reader,
772 to register information about each format the the reader is prepared
779 sf
->next
= symtab_fns
;
784 /* Initialize to read symbols from the symbol file sym_bfd. It either
785 returns or calls error(). The result is an initialized struct sym_fns
786 in the objfile structure, that contains cached information about the
790 find_sym_fns (objfile
)
791 struct objfile
*objfile
;
795 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
-> next
)
797 if (strncmp (bfd_get_target (objfile
-> obfd
),
798 sf
-> sym_name
, sf
-> sym_namelen
) == 0)
804 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
805 bfd_get_target (objfile
-> obfd
));
808 /* This function runs the load command of our current target. */
811 load_command (arg
, from_tty
)
815 target_load (arg
, from_tty
);
818 /* This version of "load" should be usable for any target. Currently
819 it is just used for remote targets, not inftarg.c or core files,
820 on the theory that only in that case is it useful.
822 Avoiding xmodem and the like seems like a win (a) because we don't have
823 to worry about finding it, and (b) On VMS, fork() is very slow and so
824 we don't want to run a subprocess. On the other hand, I'm not sure how
825 performance compares. */
827 generic_load (filename
, from_tty
)
831 struct cleanup
*old_cleanups
;
833 bfd
*loadfile_bfd
= bfd_openr (filename
, gnutarget
);
834 if (loadfile_bfd
== NULL
)
836 perror_with_name (filename
);
839 old_cleanups
= make_cleanup (bfd_close
, loadfile_bfd
);
841 if (!bfd_check_format (loadfile_bfd
, bfd_object
))
843 error ("\"%s\" is not an object file: %s", filename
,
844 bfd_errmsg (bfd_error
));
847 for (s
= loadfile_bfd
->sections
; s
; s
= s
->next
)
849 if (s
->flags
& SEC_LOAD
)
853 size
= bfd_get_section_size_before_reloc (s
);
857 struct cleanup
*old_chain
;
860 buffer
= xmalloc (size
);
861 old_chain
= make_cleanup (free
, buffer
);
863 vma
= bfd_get_section_vma (loadfile_bfd
, s
);
865 /* Is this really necessary? I guess it gives the user something
866 to look at during a long download. */
867 printf_filtered ("Loading section %s, size 0x%lx vma 0x%lx\n",
868 bfd_get_section_name (loadfile_bfd
, s
),
869 (unsigned long) size
, (unsigned long) vma
);
871 bfd_get_section_contents (loadfile_bfd
, s
, buffer
, 0, size
);
873 target_write_memory (vma
, buffer
, size
);
875 do_cleanups (old_chain
);
880 /* We were doing this in remote-mips.c, I suspect it is right
881 for other targets too. */
882 write_pc (loadfile_bfd
->start_address
);
884 /* FIXME: are we supposed to call symbol_file_add or not? According to
885 a comment from remote-mips.c (where a call to symbol_file_add was
886 commented out), making the call confuses GDB if more than one file is
887 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
890 do_cleanups (old_cleanups
);
893 /* This function allows the addition of incrementally linked object files.
894 It does not modify any state in the target, only in the debugger. */
898 add_symbol_file_command (args
, from_tty
)
912 error ("add-symbol-file takes a file name and an address");
915 /* Make a copy of the string that we can safely write into. */
917 args
= strdup (args
);
918 make_cleanup (free
, args
);
920 /* Pick off any -option args and the file name. */
922 while ((*args
!= '\000') && (name
== NULL
))
924 while (isspace (*args
)) {args
++;}
926 while ((*args
!= '\000') && !isspace (*args
)) {args
++;}
935 else if (STREQ (arg
, "-mapped"))
939 else if (STREQ (arg
, "-readnow"))
945 error ("unknown option `%s'", arg
);
949 /* After picking off any options and the file name, args should be
950 left pointing at the remainder of the command line, which should
951 be the address expression to evaluate. */
953 if ((name
== NULL
) || (*args
== '\000') )
955 error ("add-symbol-file takes a file name and an address");
957 name
= tilde_expand (name
);
958 make_cleanup (free
, name
);
960 text_addr
= parse_and_eval_address (args
);
962 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
963 name
, local_hex_string ((unsigned long)text_addr
)))
964 error ("Not confirmed.");
966 symbol_file_add (name
, 0, text_addr
, 0, mapped
, readnow
);
969 /* Re-read symbols if a symbol-file has changed. */
973 struct objfile
*objfile
;
976 struct stat new_statbuf
;
979 /* With the addition of shared libraries, this should be modified,
980 the load time should be saved in the partial symbol tables, since
981 different tables may come from different source files. FIXME.
982 This routine should then walk down each partial symbol table
983 and see if the symbol table that it originates from has been changed */
985 for (objfile
= object_files
; objfile
; objfile
= objfile
->next
) {
987 #ifdef IBM6000_TARGET
988 /* If this object is from a shared library, then you should
989 stat on the library name, not member name. */
991 if (objfile
->obfd
->my_archive
)
992 res
= stat (objfile
->obfd
->my_archive
->filename
, &new_statbuf
);
995 res
= stat (objfile
->name
, &new_statbuf
);
997 /* FIXME, should use print_sys_errmsg but it's not filtered. */
998 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1002 new_modtime
= new_statbuf
.st_mtime
;
1003 if (new_modtime
!= objfile
->mtime
)
1005 struct cleanup
*old_cleanups
;
1006 struct section_offsets
*offsets
;
1008 int section_offsets_size
;
1010 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1013 /* There are various functions like symbol_file_add,
1014 symfile_bfd_open, syms_from_objfile, etc., which might
1015 appear to do what we want. But they have various other
1016 effects which we *don't* want. So we just do stuff
1017 ourselves. We don't worry about mapped files (for one thing,
1018 any mapped file will be out of date). */
1020 /* If we get an error, blow away this objfile (not sure if
1021 that is the correct response for things like shared
1023 old_cleanups
= make_cleanup (free_objfile
, objfile
);
1024 /* We need to do this whenever any symbols go away. */
1025 make_cleanup (clear_symtab_users
, 0);
1027 /* Clean up any state BFD has sitting around. We don't need
1028 to close the descriptor but BFD lacks a way of closing the
1029 BFD without closing the descriptor. */
1030 if (!bfd_close (objfile
->obfd
))
1031 error ("Can't close BFD for %s.", objfile
->name
);
1032 objfile
->obfd
= bfd_openr (objfile
->name
, gnutarget
);
1033 if (objfile
->obfd
== NULL
)
1034 error ("Can't open %s to read symbols.", objfile
->name
);
1035 /* bfd_openr sets cacheable to true, which is what we want. */
1036 if (!bfd_check_format (objfile
->obfd
, bfd_object
))
1037 error ("Can't read symbols from %s: %s.", objfile
->name
,
1038 bfd_errmsg (bfd_error
));
1040 /* Save the offsets, we will nuke them with the rest of the
1042 num_offsets
= objfile
->num_sections
;
1043 section_offsets_size
=
1044 sizeof (struct section_offsets
)
1045 + sizeof (objfile
->section_offsets
->offsets
) * num_offsets
;
1046 offsets
= (struct section_offsets
*) alloca (section_offsets_size
);
1047 memcpy (offsets
, objfile
->section_offsets
, section_offsets_size
);
1049 /* Nuke all the state that we will re-read. Much of the following
1050 code which sets things to NULL really is necessary to tell
1051 other parts of GDB that there is nothing currently there. */
1053 /* FIXME: Do we have to free a whole linked list, or is this
1055 if (objfile
->global_psymbols
.list
)
1056 mfree (objfile
->md
, objfile
->global_psymbols
.list
);
1057 objfile
->global_psymbols
.list
= NULL
;
1058 objfile
->global_psymbols
.size
= 0;
1059 if (objfile
->static_psymbols
.list
)
1060 mfree (objfile
->md
, objfile
->static_psymbols
.list
);
1061 objfile
->static_psymbols
.list
= NULL
;
1062 objfile
->static_psymbols
.size
= 0;
1064 /* Free the obstacks for non-reusable objfiles */
1065 obstack_free (&objfile
-> psymbol_obstack
, 0);
1066 obstack_free (&objfile
-> symbol_obstack
, 0);
1067 obstack_free (&objfile
-> type_obstack
, 0);
1068 objfile
->sections
= NULL
;
1069 objfile
->symtabs
= NULL
;
1070 objfile
->psymtabs
= NULL
;
1071 objfile
->free_psymtabs
= NULL
;
1072 objfile
->msymbols
= NULL
;
1073 objfile
->minimal_symbol_count
= 0;
1074 objfile
->fundamental_types
= NULL
;
1075 if (objfile
-> sf
!= NULL
)
1077 (*objfile
-> sf
-> sym_finish
) (objfile
);
1080 /* We never make this a mapped file. */
1081 objfile
-> md
= NULL
;
1082 /* obstack_specify_allocation also initializes the obstack so
1084 obstack_specify_allocation (&objfile
-> psymbol_obstack
, 0, 0,
1086 obstack_specify_allocation (&objfile
-> symbol_obstack
, 0, 0,
1088 obstack_specify_allocation (&objfile
-> type_obstack
, 0, 0,
1090 if (build_objfile_section_table (objfile
))
1092 error ("Can't find the file sections in `%s': %s",
1093 objfile
-> name
, bfd_errmsg (bfd_error
));
1096 /* We use the same section offsets as from last time. I'm not
1097 sure whether that is always correct for shared libraries. */
1098 objfile
->section_offsets
= (struct section_offsets
*)
1099 obstack_alloc (&objfile
-> psymbol_obstack
, section_offsets_size
);
1100 memcpy (objfile
->section_offsets
, offsets
, section_offsets_size
);
1101 objfile
->num_sections
= num_offsets
;
1103 /* What the hell is sym_new_init for, anyway? The concept of
1104 distinguishing between the main file and additional files
1105 in this way seems rather dubious. */
1106 if (objfile
== symfile_objfile
)
1107 (*objfile
->sf
->sym_new_init
) (objfile
);
1109 (*objfile
->sf
->sym_init
) (objfile
);
1110 clear_complaints (1, 1);
1111 /* The "mainline" parameter is a hideous hack; I think leaving it
1112 zero is OK since dbxread.c also does what it needs to do if
1113 objfile->global_psymbols.size is 0. */
1114 (*objfile
->sf
->sym_read
) (objfile
, objfile
->section_offsets
, 0);
1115 objfile
-> flags
|= OBJF_SYMS
;
1117 /* We're done reading the symbol file; finish off complaints. */
1118 clear_complaints (0, 1);
1120 /* Getting new symbols may change our opinion about what is
1123 reinit_frame_cache ();
1125 /* Discard cleanups as symbol reading was successful. */
1126 discard_cleanups (old_cleanups
);
1128 /* If the mtime has changed between the time we set new_modtime
1129 and now, we *want* this to be out of date, so don't call stat
1131 objfile
->mtime
= new_modtime
;
1138 clear_symtab_users ();
1143 deduce_language_from_filename (filename
)
1150 else if (0 == (c
= strrchr (filename
, '.')))
1151 ; /* Get default. */
1152 else if (STREQ(c
,".mod"))
1154 else if (STREQ(c
,".c"))
1156 else if (STREQ (c
,".cc") || STREQ (c
,".C") || STREQ (c
, ".cxx"))
1157 return language_cplus
;
1158 else if (STREQ (c
,".ch") || STREQ (c
,".c186") || STREQ (c
,".c286"))
1159 return language_chill
;
1161 return language_unknown
; /* default */
1166 Allocate and partly initialize a new symbol table. Return a pointer
1167 to it. error() if no space.
1169 Caller must set these fields:
1175 initialize any EXTRA_SYMTAB_INFO
1176 possibly free_named_symtabs (symtab->filename);
1180 allocate_symtab (filename
, objfile
)
1182 struct objfile
*objfile
;
1184 register struct symtab
*symtab
;
1186 symtab
= (struct symtab
*)
1187 obstack_alloc (&objfile
-> symbol_obstack
, sizeof (struct symtab
));
1188 memset (symtab
, 0, sizeof (*symtab
));
1189 symtab
-> filename
= obsavestring (filename
, strlen (filename
),
1190 &objfile
-> symbol_obstack
);
1191 symtab
-> fullname
= NULL
;
1192 symtab
-> language
= deduce_language_from_filename (filename
);
1194 /* Hook it to the objfile it comes from */
1196 symtab
-> objfile
= objfile
;
1197 symtab
-> next
= objfile
-> symtabs
;
1198 objfile
-> symtabs
= symtab
;
1200 #ifdef INIT_EXTRA_SYMTAB_INFO
1201 INIT_EXTRA_SYMTAB_INFO (symtab
);
1207 struct partial_symtab
*
1208 allocate_psymtab (filename
, objfile
)
1210 struct objfile
*objfile
;
1212 struct partial_symtab
*psymtab
;
1214 if (objfile
-> free_psymtabs
)
1216 psymtab
= objfile
-> free_psymtabs
;
1217 objfile
-> free_psymtabs
= psymtab
-> next
;
1220 psymtab
= (struct partial_symtab
*)
1221 obstack_alloc (&objfile
-> psymbol_obstack
,
1222 sizeof (struct partial_symtab
));
1224 memset (psymtab
, 0, sizeof (struct partial_symtab
));
1225 psymtab
-> filename
= obsavestring (filename
, strlen (filename
),
1226 &objfile
-> psymbol_obstack
);
1227 psymtab
-> symtab
= NULL
;
1229 /* Hook it to the objfile it comes from */
1231 psymtab
-> objfile
= objfile
;
1232 psymtab
-> next
= objfile
-> psymtabs
;
1233 objfile
-> psymtabs
= psymtab
;
1239 /* Reset all data structures in gdb which may contain references to symbol
1243 clear_symtab_users ()
1245 /* Someday, we should do better than this, by only blowing away
1246 the things that really need to be blown. */
1247 clear_value_history ();
1249 clear_internalvars ();
1250 breakpoint_re_set ();
1251 set_default_breakpoint (0, 0, 0, 0);
1252 current_source_symtab
= 0;
1253 current_source_line
= 0;
1254 clear_pc_function_cache ();
1257 /* clear_symtab_users_once:
1259 This function is run after symbol reading, or from a cleanup.
1260 If an old symbol table was obsoleted, the old symbol table
1261 has been blown away, but the other GDB data structures that may
1262 reference it have not yet been cleared or re-directed. (The old
1263 symtab was zapped, and the cleanup queued, in free_named_symtab()
1266 This function can be queued N times as a cleanup, or called
1267 directly; it will do all the work the first time, and then will be a
1268 no-op until the next time it is queued. This works by bumping a
1269 counter at queueing time. Much later when the cleanup is run, or at
1270 the end of symbol processing (in case the cleanup is discarded), if
1271 the queued count is greater than the "done-count", we do the work
1272 and set the done-count to the queued count. If the queued count is
1273 less than or equal to the done-count, we just ignore the call. This
1274 is needed because reading a single .o file will often replace many
1275 symtabs (one per .h file, for example), and we don't want to reset
1276 the breakpoints N times in the user's face.
1278 The reason we both queue a cleanup, and call it directly after symbol
1279 reading, is because the cleanup protects us in case of errors, but is
1280 discarded if symbol reading is successful. */
1283 /* FIXME: As free_named_symtabs is currently a big noop this function
1284 is no longer needed. */
1286 clear_symtab_users_once
PARAMS ((void));
1288 static int clear_symtab_users_queued
;
1289 static int clear_symtab_users_done
;
1292 clear_symtab_users_once ()
1294 /* Enforce once-per-`do_cleanups'-semantics */
1295 if (clear_symtab_users_queued
<= clear_symtab_users_done
)
1297 clear_symtab_users_done
= clear_symtab_users_queued
;
1299 clear_symtab_users ();
1303 /* Delete the specified psymtab, and any others that reference it. */
1306 cashier_psymtab (pst
)
1307 struct partial_symtab
*pst
;
1309 struct partial_symtab
*ps
, *pprev
= NULL
;
1312 /* Find its previous psymtab in the chain */
1313 for (ps
= pst
->objfile
->psymtabs
; ps
; ps
= ps
->next
) {
1320 /* Unhook it from the chain. */
1321 if (ps
== pst
->objfile
->psymtabs
)
1322 pst
->objfile
->psymtabs
= ps
->next
;
1324 pprev
->next
= ps
->next
;
1326 /* FIXME, we can't conveniently deallocate the entries in the
1327 partial_symbol lists (global_psymbols/static_psymbols) that
1328 this psymtab points to. These just take up space until all
1329 the psymtabs are reclaimed. Ditto the dependencies list and
1330 filename, which are all in the psymbol_obstack. */
1332 /* We need to cashier any psymtab that has this one as a dependency... */
1334 for (ps
= pst
->objfile
->psymtabs
; ps
; ps
= ps
->next
) {
1335 for (i
= 0; i
< ps
->number_of_dependencies
; i
++) {
1336 if (ps
->dependencies
[i
] == pst
) {
1337 cashier_psymtab (ps
);
1338 goto again
; /* Must restart, chain has been munged. */
1345 /* If a symtab or psymtab for filename NAME is found, free it along
1346 with any dependent breakpoints, displays, etc.
1347 Used when loading new versions of object modules with the "add-file"
1348 command. This is only called on the top-level symtab or psymtab's name;
1349 it is not called for subsidiary files such as .h files.
1351 Return value is 1 if we blew away the environment, 0 if not.
1352 FIXME. The return valu appears to never be used.
1354 FIXME. I think this is not the best way to do this. We should
1355 work on being gentler to the environment while still cleaning up
1356 all stray pointers into the freed symtab. */
1359 free_named_symtabs (name
)
1363 /* FIXME: With the new method of each objfile having it's own
1364 psymtab list, this function needs serious rethinking. In particular,
1365 why was it ever necessary to toss psymtabs with specific compilation
1366 unit filenames, as opposed to all psymtabs from a particular symbol
1368 Well, the answer is that some systems permit reloading of particular
1369 compilation units. We want to blow away any old info about these
1370 compilation units, regardless of which objfiles they arrived in. --gnu. */
1372 register struct symtab
*s
;
1373 register struct symtab
*prev
;
1374 register struct partial_symtab
*ps
;
1375 struct blockvector
*bv
;
1378 /* We only wack things if the symbol-reload switch is set. */
1379 if (!symbol_reloading
)
1382 /* Some symbol formats have trouble providing file names... */
1383 if (name
== 0 || *name
== '\0')
1386 /* Look for a psymtab with the specified name. */
1389 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
1390 if (STREQ (name
, ps
->filename
)) {
1391 cashier_psymtab (ps
); /* Blow it away...and its little dog, too. */
1392 goto again2
; /* Must restart, chain has been munged */
1396 /* Look for a symtab with the specified name. */
1398 for (s
= symtab_list
; s
; s
= s
->next
)
1400 if (STREQ (name
, s
->filename
))
1407 if (s
== symtab_list
)
1408 symtab_list
= s
->next
;
1410 prev
->next
= s
->next
;
1412 /* For now, queue a delete for all breakpoints, displays, etc., whether
1413 or not they depend on the symtab being freed. This should be
1414 changed so that only those data structures affected are deleted. */
1416 /* But don't delete anything if the symtab is empty.
1417 This test is necessary due to a bug in "dbxread.c" that
1418 causes empty symtabs to be created for N_SO symbols that
1419 contain the pathname of the object file. (This problem
1420 has been fixed in GDB 3.9x). */
1422 bv
= BLOCKVECTOR (s
);
1423 if (BLOCKVECTOR_NBLOCKS (bv
) > 2
1424 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
))
1425 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
)))
1427 complain (&oldsyms_complaint
, name
);
1429 clear_symtab_users_queued
++;
1430 make_cleanup (clear_symtab_users_once
, 0);
1433 complain (&empty_symtab_complaint
, name
);
1440 /* It is still possible that some breakpoints will be affected
1441 even though no symtab was found, since the file might have
1442 been compiled without debugging, and hence not be associated
1443 with a symtab. In order to handle this correctly, we would need
1444 to keep a list of text address ranges for undebuggable files.
1445 For now, we do nothing, since this is a fairly obscure case. */
1449 /* FIXME, what about the minimal symbol table? */
1456 /* Allocate and partially fill a partial symtab. It will be
1457 completely filled at the end of the symbol list.
1459 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1460 is the address relative to which its symbols are (incremental) or 0
1464 struct partial_symtab
*
1465 start_psymtab_common (objfile
, section_offsets
,
1466 filename
, textlow
, global_syms
, static_syms
)
1467 struct objfile
*objfile
;
1468 struct section_offsets
*section_offsets
;
1471 struct partial_symbol
*global_syms
;
1472 struct partial_symbol
*static_syms
;
1474 struct partial_symtab
*psymtab
;
1476 psymtab
= allocate_psymtab (filename
, objfile
);
1477 psymtab
-> section_offsets
= section_offsets
;
1478 psymtab
-> textlow
= textlow
;
1479 psymtab
-> texthigh
= psymtab
-> textlow
; /* default */
1480 psymtab
-> globals_offset
= global_syms
- objfile
-> global_psymbols
.list
;
1481 psymtab
-> statics_offset
= static_syms
- objfile
-> static_psymbols
.list
;
1485 /* Debugging versions of functions that are usually inline macros
1488 #if !INLINE_ADD_PSYMBOL
1490 /* Add a symbol with a long value to a psymtab.
1491 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1494 add_psymbol_to_list (name
, namelength
, namespace, class, list
, val
, language
,
1498 enum namespace namespace;
1499 enum address_class
class;
1500 struct psymbol_allocation_list
*list
;
1502 enum language language
;
1503 struct objfile
*objfile
;
1505 register struct partial_symbol
*psym
;
1506 register char *demangled_name
;
1508 if (list
->next
>= list
->list
+ list
->size
)
1510 extend_psymbol_list (list
,objfile
);
1512 psym
= list
->next
++;
1514 SYMBOL_NAME (psym
) =
1515 (char *) obstack_alloc (&objfile
->psymbol_obstack
, namelength
+ 1);
1516 memcpy (SYMBOL_NAME (psym
), name
, namelength
);
1517 SYMBOL_NAME (psym
)[namelength
] = '\0';
1518 SYMBOL_VALUE (psym
) = val
;
1519 SYMBOL_LANGUAGE (psym
) = language
;
1520 PSYMBOL_NAMESPACE (psym
) = namespace;
1521 PSYMBOL_CLASS (psym
) = class;
1522 SYMBOL_INIT_DEMANGLED_NAME (psym
, &objfile
->psymbol_obstack
);
1525 /* Add a symbol with a CORE_ADDR value to a psymtab. */
1528 add_psymbol_addr_to_list (name
, namelength
, namespace, class, list
, val
,
1532 enum namespace namespace;
1533 enum address_class
class;
1534 struct psymbol_allocation_list
*list
;
1536 enum language language
;
1537 struct objfile
*objfile
;
1539 register struct partial_symbol
*psym
;
1540 register char *demangled_name
;
1542 if (list
->next
>= list
->list
+ list
->size
)
1544 extend_psymbol_list (list
,objfile
);
1546 psym
= list
->next
++;
1548 SYMBOL_NAME (psym
) =
1549 (char *) obstack_alloc (&objfile
->psymbol_obstack
, namelength
+ 1);
1550 memcpy (SYMBOL_NAME (psym
), name
, namelength
);
1551 SYMBOL_NAME (psym
)[namelength
] = '\0';
1552 SYMBOL_VALUE_ADDRESS (psym
) = val
;
1553 SYMBOL_LANGUAGE (psym
) = language
;
1554 PSYMBOL_NAMESPACE (psym
) = namespace;
1555 PSYMBOL_CLASS (psym
) = class;
1556 SYMBOL_INIT_DEMANGLED_NAME (psym
, &objfile
->psymbol_obstack
);
1559 #endif /* !INLINE_ADD_PSYMBOL */
1563 _initialize_symfile ()
1565 struct cmd_list_element
*c
;
1567 c
= add_cmd ("symbol-file", class_files
, symbol_file_command
,
1568 "Load symbol table from executable file FILE.\n\
1569 The `file' command can also load symbol tables, as well as setting the file\n\
1570 to execute.", &cmdlist
);
1571 c
->completer
= filename_completer
;
1573 c
= add_cmd ("add-symbol-file", class_files
, add_symbol_file_command
,
1574 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1575 The second argument provides the starting address of the file's text.",
1577 c
->completer
= filename_completer
;
1579 c
= add_cmd ("load", class_files
, load_command
,
1580 "Dynamically load FILE into the running program, and record its symbols\n\
1581 for access from GDB.", &cmdlist
);
1582 c
->completer
= filename_completer
;
1585 (add_set_cmd ("symbol-reloading", class_support
, var_boolean
,
1586 (char *)&symbol_reloading
,
1587 "Set dynamic symbol table reloading multiple times in one run.",