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"
39 #include <sys/types.h>
49 /* 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
))
218 sort_all_symtab_syms ()
220 register struct symtab
*s
;
221 register struct objfile
*objfile
;
223 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
225 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
227 sort_symtab_syms (s
);
232 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
233 (and add a null character at the end in the copy).
234 Returns the address of the copy. */
237 obsavestring (ptr
, size
, obstackp
)
240 struct obstack
*obstackp
;
242 register char *p
= (char *) obstack_alloc (obstackp
, size
+ 1);
243 /* Open-coded memcpy--saves function call time.
244 These strings are usually short. */
246 register char *p1
= ptr
;
247 register char *p2
= p
;
248 char *end
= ptr
+ size
;
256 /* Concatenate strings S1, S2 and S3; return the new string.
257 Space is found in the symbol_obstack. */
260 obconcat (obstackp
, s1
, s2
, s3
)
261 struct obstack
*obstackp
;
262 const char *s1
, *s2
, *s3
;
264 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
265 register char *val
= (char *) obstack_alloc (obstackp
, len
);
272 /* Get the symbol table that corresponds to a partial_symtab.
273 This is fast after the first time you do it. In fact, there
274 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
278 psymtab_to_symtab (pst
)
279 register struct partial_symtab
*pst
;
281 /* If it's been looked up before, return it. */
285 /* If it has not yet been read in, read it. */
288 (*pst
->read_symtab
) (pst
);
294 /* Initialize entry point information for this objfile. */
297 init_entry_point_info (objfile
)
298 struct objfile
*objfile
;
300 /* Save startup file's range of PC addresses to help blockframe.c
301 decide where the bottom of the stack is. */
303 if (bfd_get_file_flags (objfile
-> obfd
) & EXEC_P
)
305 /* Executable file -- record its entry point so we'll recognize
306 the startup file because it contains the entry point. */
307 objfile
-> ei
.entry_point
= bfd_get_start_address (objfile
-> obfd
);
311 /* Examination of non-executable.o files. Short-circuit this stuff. */
312 /* ~0 will not be in any file, we hope. */
313 objfile
-> ei
.entry_point
= ~0;
314 /* set the startup file to be an empty range. */
315 objfile
-> ei
.entry_file_lowpc
= 0;
316 objfile
-> ei
.entry_file_highpc
= 0;
320 /* Remember the lowest-addressed loadable section we've seen.
321 This function is called via bfd_map_over_sections. */
323 #if 0 /* Not used yet */
325 find_lowest_section (abfd
, sect
, obj
)
330 asection
**lowest
= (asection
**)obj
;
332 if (0 == (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
))
335 *lowest
= sect
; /* First loadable section */
336 else if (bfd_section_vma (abfd
, *lowest
) >= bfd_section_vma (abfd
, sect
))
337 *lowest
= sect
; /* A lower loadable section */
341 /* Process a symbol file, as either the main file or as a dynamically
344 NAME is the file name (which will be tilde-expanded and made
345 absolute herein) (but we don't free or modify NAME itself).
346 FROM_TTY says how verbose to be. MAINLINE specifies whether this
347 is the main symbol file, or whether it's an extra symbol file such
348 as dynamically loaded code. If !mainline, ADDR is the address
349 where the text segment was loaded. If VERBO, the caller has printed
350 a verbose message about the symbol reading (and complaints can be
351 more terse about it). */
354 syms_from_objfile (objfile
, addr
, mainline
, verbo
)
355 struct objfile
*objfile
;
360 struct section_offsets
*section_offsets
;
361 asection
*lowest_sect
;
362 struct cleanup
*old_chain
;
364 init_entry_point_info (objfile
);
365 find_sym_fns (objfile
);
367 /* Make sure that partially constructed symbol tables will be cleaned up
368 if an error occurs during symbol reading. */
369 old_chain
= make_cleanup (free_objfile
, objfile
);
373 /* We will modify the main symbol table, make sure that all its users
374 will be cleaned up if an error occurs during symbol reading. */
375 make_cleanup (clear_symtab_users
, 0);
377 /* Since no error yet, throw away the old symbol table. */
379 if (symfile_objfile
!= NULL
)
381 free_objfile (symfile_objfile
);
382 symfile_objfile
= NULL
;
385 (*objfile
-> sf
-> sym_new_init
) (objfile
);
388 /* Convert addr into an offset rather than an absolute address.
389 We find the lowest address of a loaded segment in the objfile,
390 and assume that <addr> is where that got loaded. Due to historical
391 precedent, we warn if that doesn't happen to be the ".text"
396 addr
= 0; /* No offset from objfile addresses. */
400 lowest_sect
= bfd_get_section_by_name (objfile
->obfd
, ".text");
403 bfd_map_over_sections (objfile
->obfd
, find_lowest_section
,
407 if (lowest_sect
== 0)
408 warning ("no loadable sections found in added symbol-file %s",
410 else if (0 == bfd_get_section_name (objfile
->obfd
, lowest_sect
)
412 bfd_get_section_name (objfile
->obfd
, lowest_sect
)))
413 warning ("Lowest section in %s is %s at 0x%x",
415 bfd_section_name (objfile
->obfd
, lowest_sect
),
416 bfd_section_vma (objfile
->obfd
, lowest_sect
));
419 addr
-= bfd_section_vma (objfile
->obfd
, lowest_sect
);
422 /* Initialize symbol reading routines for this objfile, allow complaints to
423 appear for this new file, and record how verbose to be, then do the
424 initial symbol reading for this file. */
426 (*objfile
-> sf
-> sym_init
) (objfile
);
427 clear_complaints (1, verbo
);
429 /* If objfile->sf->sym_offsets doesn't set this, we don't care
431 objfile
->num_sections
= 0; /* krp-FIXME: why zero? */
432 section_offsets
= (*objfile
-> sf
-> sym_offsets
) (objfile
, addr
);
433 objfile
->section_offsets
= section_offsets
;
435 #ifndef IBM6000_TARGET
436 /* This is a SVR4/SunOS specific hack, I think. In any event, it
437 screws RS/6000. sym_offsets should be doing this sort of thing,
438 because it knows the mapping between bfd sections and
440 /* This is a hack. As far as I can tell, section offsets are not
441 target dependent. They are all set to addr with a couple of
442 exceptions. The exceptions are sysvr4 shared libraries, whose
443 offsets are kept in solib structures anyway and rs6000 xcoff
444 which handles shared libraries in a completely unique way.
446 Section offsets are built similarly, except that they are built
447 by adding addr in all cases because there is no clear mapping
448 from section_offsets into actual sections. Note that solib.c
449 has a different algorythm for finding section offsets.
451 These should probably all be collapsed into some target
452 independent form of shared library support. FIXME. */
456 struct obj_section
*s
;
458 for (s
= objfile
->sections
; s
< objfile
->sections_end
; ++s
)
460 s
->addr
-= s
->offset
;
462 s
->endaddr
-= s
->offset
;
467 #endif /* not IBM6000_TARGET */
469 (*objfile
-> sf
-> sym_read
) (objfile
, section_offsets
, mainline
);
471 /* Don't allow char * to have a typename (else would get caddr_t.) */
472 /* Ditto void *. FIXME should do this for all the builtin types. */
474 TYPE_NAME (lookup_pointer_type (builtin_type_char
)) = 0;
475 TYPE_NAME (lookup_pointer_type (builtin_type_void
)) = 0;
477 /* Mark the objfile has having had initial symbol read attempted. Note
478 that this does not mean we found any symbols... */
480 objfile
-> flags
|= OBJF_SYMS
;
482 /* Discard cleanups as symbol reading was successful. */
484 discard_cleanups (old_chain
);
487 /* Perform required actions after either reading in the initial
488 symbols for a new objfile, or mapping in the symbols from a reusable
492 new_symfile_objfile (objfile
, mainline
, verbo
)
493 struct objfile
*objfile
;
498 /* If this is the main symbol file we have to clean up all users of the
499 old main symbol file. Otherwise it is sufficient to fixup all the
500 breakpoints that may have been redefined by this symbol file. */
503 /* OK, make it the "real" symbol file. */
504 symfile_objfile
= objfile
;
506 clear_symtab_users ();
510 breakpoint_re_set ();
513 /* We're done reading the symbol file; finish off complaints. */
514 clear_complaints (0, verbo
);
517 /* Process a symbol file, as either the main file or as a dynamically
520 NAME is the file name (which will be tilde-expanded and made
521 absolute herein) (but we don't free or modify NAME itself).
522 FROM_TTY says how verbose to be. MAINLINE specifies whether this
523 is the main symbol file, or whether it's an extra symbol file such
524 as dynamically loaded code. If !mainline, ADDR is the address
525 where the text segment was loaded.
527 Upon success, returns a pointer to the objfile that was added.
528 Upon failure, jumps back to command level (never returns). */
531 symbol_file_add (name
, from_tty
, addr
, mainline
, mapped
, readnow
)
539 struct objfile
*objfile
;
540 struct partial_symtab
*psymtab
;
543 /* Open a bfd for the file, and give user a chance to burp if we'd be
544 interactively wiping out any existing symbols. */
546 abfd
= symfile_bfd_open (name
);
548 if ((have_full_symbols () || have_partial_symbols ())
551 && !query ("Load new symbol table from \"%s\"? ", name
))
552 error ("Not confirmed.");
554 objfile
= allocate_objfile (abfd
, mapped
);
556 /* If the objfile uses a mapped symbol file, and we have a psymtab for
557 it, then skip reading any symbols at this time. */
559 if ((objfile
-> flags
& OBJF_MAPPED
) && (objfile
-> flags
& OBJF_SYMS
))
561 /* We mapped in an existing symbol table file that already has had
562 initial symbol reading performed, so we can skip that part. Notify
563 the user that instead of reading the symbols, they have been mapped.
565 if (from_tty
|| info_verbose
)
567 printf_filtered ("Mapped symbols for %s...", name
);
571 init_entry_point_info (objfile
);
572 find_sym_fns (objfile
);
576 /* We either created a new mapped symbol table, mapped an existing
577 symbol table file which has not had initial symbol reading
578 performed, or need to read an unmapped symbol table. */
579 if (from_tty
|| info_verbose
)
581 printf_filtered ("Reading symbols from %s...", name
);
585 syms_from_objfile (objfile
, addr
, mainline
, from_tty
);
588 /* We now have at least a partial symbol table. Check to see if the
589 user requested that all symbols be read on initial access via either
590 the gdb startup command line or on a per symbol file basis. Expand
591 all partial symbol tables for this objfile if so. */
593 if (readnow
|| readnow_symbol_files
)
595 if (from_tty
|| info_verbose
)
597 printf_filtered ("expanding to full symbols...");
602 for (psymtab
= objfile
-> psymtabs
;
604 psymtab
= psymtab
-> next
)
606 psymtab_to_symtab (psymtab
);
610 if (from_tty
|| info_verbose
)
612 printf_filtered ("done.\n");
616 new_symfile_objfile (objfile
, mainline
, from_tty
);
618 /* Getting new symbols may change our opinion about what is
621 reinit_frame_cache ();
626 /* This is the symbol-file command. Read the file, analyze its symbols,
627 and add a struct symtab to a symtab list. */
630 symbol_file_command (args
, from_tty
)
636 struct cleanup
*cleanups
;
644 if ((have_full_symbols () || have_partial_symbols ())
646 && !query ("Discard symbol table from `%s'? ",
647 symfile_objfile
-> name
))
648 error ("Not confirmed.");
649 free_all_objfiles ();
650 symfile_objfile
= NULL
;
653 printf ("No symbol file now.\n");
658 if ((argv
= buildargv (args
)) == NULL
)
662 cleanups
= make_cleanup (freeargv
, (char *) argv
);
663 while (*argv
!= NULL
)
665 if (STREQ (*argv
, "-mapped"))
669 else if (STREQ (*argv
, "-readnow"))
673 else if (**argv
== '-')
675 error ("unknown option `%s'", *argv
);
686 error ("no symbol file name was specified");
690 symbol_file_add (name
, from_tty
, (CORE_ADDR
)0, 1, mapped
, readnow
);
691 set_initial_language ();
693 do_cleanups (cleanups
);
697 /* Set the initial language.
699 A better solution would be to record the language in the psymtab when reading
700 partial symbols, and then use it (if known) to set the language. This would
701 be a win for formats that encode the language in an easily discoverable place,
702 such as DWARF. For stabs, we can jump through hoops looking for specially
703 named symbols or try to intuit the language from the specific type of stabs
704 we find, but we can't do that until later when we read in full symbols.
708 set_initial_language ()
710 struct partial_symtab
*pst
;
711 enum language lang
= language_unknown
;
713 pst
= find_main_psymtab ();
716 if (pst
-> filename
!= NULL
)
718 lang
= deduce_language_from_filename (pst
-> filename
);
720 if (lang
== language_unknown
)
722 /* Make C the default language */
726 expected_language
= current_language
; /* Don't warn the user */
730 /* Open file specified by NAME and hand it off to BFD for preliminary
731 analysis. Result is a newly initialized bfd *, which includes a newly
732 malloc'd` copy of NAME (tilde-expanded and made absolute).
733 In case of trouble, error() is called. */
736 symfile_bfd_open (name
)
743 name
= tilde_expand (name
); /* Returns 1st new malloc'd copy */
745 /* Look down path for it, allocate 2nd new malloc'd copy. */
746 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
| O_BINARY
, 0, &absolute_name
);
749 make_cleanup (free
, name
);
750 perror_with_name (name
);
752 free (name
); /* Free 1st new malloc'd copy */
753 name
= absolute_name
; /* Keep 2nd malloc'd copy in bfd */
754 /* It'll be freed in free_objfile(). */
756 sym_bfd
= bfd_fdopenr (name
, gnutarget
, desc
);
760 make_cleanup (free
, name
);
761 error ("\"%s\": can't open to read symbols: %s.", name
,
762 bfd_errmsg (bfd_error
));
764 sym_bfd
->cacheable
= true;
766 if (!bfd_check_format (sym_bfd
, bfd_object
))
768 bfd_close (sym_bfd
); /* This also closes desc */
769 make_cleanup (free
, name
);
770 error ("\"%s\": can't read symbols: %s.", name
,
771 bfd_errmsg (bfd_error
));
777 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
778 startup by the _initialize routine in each object file format reader,
779 to register information about each format the the reader is prepared
786 sf
->next
= symtab_fns
;
791 /* Initialize to read symbols from the symbol file sym_bfd. It either
792 returns or calls error(). The result is an initialized struct sym_fns
793 in the objfile structure, that contains cached information about the
797 find_sym_fns (objfile
)
798 struct objfile
*objfile
;
802 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
-> next
)
804 if (strncmp (bfd_get_target (objfile
-> obfd
),
805 sf
-> sym_name
, sf
-> sym_namelen
) == 0)
811 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
812 bfd_get_target (objfile
-> obfd
));
815 /* This function runs the load command of our current target. */
818 load_command (arg
, from_tty
)
822 target_load (arg
, from_tty
);
825 /* This version of "load" should be usable for any target. Currently
826 it is just used for remote targets, not inftarg.c or core files,
827 on the theory that only in that case is it useful.
829 Avoiding xmodem and the like seems like a win (a) because we don't have
830 to worry about finding it, and (b) On VMS, fork() is very slow and so
831 we don't want to run a subprocess. On the other hand, I'm not sure how
832 performance compares. */
834 generic_load (filename
, from_tty
)
838 struct cleanup
*old_cleanups
;
840 bfd
*loadfile_bfd
= bfd_openr (filename
, gnutarget
);
841 if (loadfile_bfd
== NULL
)
843 perror_with_name (filename
);
846 old_cleanups
= make_cleanup (bfd_close
, loadfile_bfd
);
848 if (!bfd_check_format (loadfile_bfd
, bfd_object
))
850 error ("\"%s\" is not an object file: %s", filename
,
851 bfd_errmsg (bfd_error
));
854 for (s
= loadfile_bfd
->sections
; s
; s
= s
->next
)
856 if (s
->flags
& SEC_LOAD
)
860 size
= bfd_get_section_size_before_reloc (s
);
864 struct cleanup
*old_chain
;
867 buffer
= xmalloc (size
);
868 old_chain
= make_cleanup (free
, buffer
);
870 vma
= bfd_get_section_vma (loadfile_bfd
, s
);
872 /* Is this really necessary? I guess it gives the user something
873 to look at during a long download. */
874 printf_filtered ("Loading section %s, size 0x%x vma 0x%x\n",
875 bfd_get_section_name (loadfile_bfd
, s
),
878 bfd_get_section_contents (loadfile_bfd
, s
, buffer
, 0, size
);
880 target_write_memory (vma
, buffer
, size
);
882 do_cleanups (old_chain
);
887 /* We were doing this in remote-mips.c, I suspect it is right
888 for other targets too. */
889 write_pc (loadfile_bfd
->start_address
);
891 /* FIXME: are we supposed to call symbol_file_add or not? According to
892 a comment from remote-mips.c (where a call to symbol_file_add was
893 commented out), making the call confuses GDB if more than one file is
894 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
897 do_cleanups (old_cleanups
);
900 /* This function allows the addition of incrementally linked object files.
901 It does not modify any state in the target, only in the debugger. */
905 add_symbol_file_command (args
, from_tty
)
919 error ("add-symbol-file takes a file name and an address");
922 /* Make a copy of the string that we can safely write into. */
924 args
= strdup (args
);
925 make_cleanup (free
, args
);
927 /* Pick off any -option args and the file name. */
929 while ((*args
!= '\000') && (name
== NULL
))
931 while (isspace (*args
)) {args
++;}
933 while ((*args
!= '\000') && !isspace (*args
)) {args
++;}
942 else if (STREQ (arg
, "-mapped"))
946 else if (STREQ (arg
, "-readnow"))
952 error ("unknown option `%s'", arg
);
956 /* After picking off any options and the file name, args should be
957 left pointing at the remainder of the command line, which should
958 be the address expression to evaluate. */
960 if ((name
== NULL
) || (*args
== '\000') )
962 error ("add-symbol-file takes a file name and an address");
964 name
= tilde_expand (name
);
965 make_cleanup (free
, name
);
967 text_addr
= parse_and_eval_address (args
);
969 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
970 name
, local_hex_string (text_addr
)))
971 error ("Not confirmed.");
973 symbol_file_add (name
, 0, text_addr
, 0, mapped
, readnow
);
976 /* Re-read symbols if a symbol-file has changed. */
980 struct objfile
*objfile
;
983 struct stat new_statbuf
;
986 /* With the addition of shared libraries, this should be modified,
987 the load time should be saved in the partial symbol tables, since
988 different tables may come from different source files. FIXME.
989 This routine should then walk down each partial symbol table
990 and see if the symbol table that it originates from has been changed */
993 for (objfile
= object_files
; objfile
; objfile
= objfile
->next
) {
995 #ifdef IBM6000_TARGET
996 /* If this object is from a shared library, then you should
997 stat on the library name, not member name. */
999 if (objfile
->obfd
->my_archive
)
1000 res
= stat (objfile
->obfd
->my_archive
->filename
, &new_statbuf
);
1003 res
= stat (objfile
->name
, &new_statbuf
);
1005 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1006 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1010 new_modtime
= new_statbuf
.st_mtime
;
1011 if (new_modtime
!= objfile
->mtime
) {
1012 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1014 /* FIXME, this should use a different command...that would only
1015 affect this objfile's symbols, and would reset objfile->mtime.
1016 (objfile->mtime = new_modtime;)
1017 HOWEVER, that command isn't written yet -- so call symbol_file_
1018 command, and restart the scan from the top, because it munges
1019 the object_files list. */
1020 symbol_file_command (objfile
->name
, 0);
1022 goto the_big_top
; /* Start over. */
1028 breakpoint_re_set ();
1033 deduce_language_from_filename (filename
)
1040 else if (0 == (c
= strrchr (filename
, '.')))
1041 ; /* Get default. */
1042 else if(STREQ(c
,".mod"))
1044 else if(STREQ(c
,".c"))
1046 else if(STREQ(c
,".cc") || STREQ(c
,".C"))
1047 return language_cplus
;
1048 else if(STREQ(c
,".ch") || STREQ(c
,".c186") || STREQ(c
,".c286"))
1049 return language_chill
;
1051 return language_unknown
; /* default */
1056 Allocate and partly initialize a new symbol table. Return a pointer
1057 to it. error() if no space.
1059 Caller must set these fields:
1065 initialize any EXTRA_SYMTAB_INFO
1066 possibly free_named_symtabs (symtab->filename);
1070 allocate_symtab (filename
, objfile
)
1072 struct objfile
*objfile
;
1074 register struct symtab
*symtab
;
1076 symtab
= (struct symtab
*)
1077 obstack_alloc (&objfile
-> symbol_obstack
, sizeof (struct symtab
));
1078 memset (symtab
, 0, sizeof (*symtab
));
1079 symtab
-> filename
= obsavestring (filename
, strlen (filename
),
1080 &objfile
-> symbol_obstack
);
1081 symtab
-> fullname
= NULL
;
1082 symtab
-> language
= deduce_language_from_filename (filename
);
1084 /* Hook it to the objfile it comes from */
1086 symtab
-> objfile
= objfile
;
1087 symtab
-> next
= objfile
-> symtabs
;
1088 objfile
-> symtabs
= symtab
;
1090 #ifdef INIT_EXTRA_SYMTAB_INFO
1091 INIT_EXTRA_SYMTAB_INFO (symtab
);
1097 struct partial_symtab
*
1098 allocate_psymtab (filename
, objfile
)
1100 struct objfile
*objfile
;
1102 struct partial_symtab
*psymtab
;
1104 if (objfile
-> free_psymtabs
)
1106 psymtab
= objfile
-> free_psymtabs
;
1107 objfile
-> free_psymtabs
= psymtab
-> next
;
1110 psymtab
= (struct partial_symtab
*)
1111 obstack_alloc (&objfile
-> psymbol_obstack
,
1112 sizeof (struct partial_symtab
));
1114 memset (psymtab
, 0, sizeof (struct partial_symtab
));
1115 psymtab
-> filename
= obsavestring (filename
, strlen (filename
),
1116 &objfile
-> psymbol_obstack
);
1117 psymtab
-> symtab
= NULL
;
1119 /* Hook it to the objfile it comes from */
1121 psymtab
-> objfile
= objfile
;
1122 psymtab
-> next
= objfile
-> psymtabs
;
1123 objfile
-> psymtabs
= psymtab
;
1129 /* Reset all data structures in gdb which may contain references to symbol
1133 clear_symtab_users ()
1135 /* Someday, we should do better than this, by only blowing away
1136 the things that really need to be blown. */
1137 clear_value_history ();
1139 clear_internalvars ();
1140 breakpoint_re_set ();
1141 set_default_breakpoint (0, 0, 0, 0);
1142 current_source_symtab
= 0;
1143 current_source_line
= 0;
1146 /* clear_symtab_users_once:
1148 This function is run after symbol reading, or from a cleanup.
1149 If an old symbol table was obsoleted, the old symbol table
1150 has been blown away, but the other GDB data structures that may
1151 reference it have not yet been cleared or re-directed. (The old
1152 symtab was zapped, and the cleanup queued, in free_named_symtab()
1155 This function can be queued N times as a cleanup, or called
1156 directly; it will do all the work the first time, and then will be a
1157 no-op until the next time it is queued. This works by bumping a
1158 counter at queueing time. Much later when the cleanup is run, or at
1159 the end of symbol processing (in case the cleanup is discarded), if
1160 the queued count is greater than the "done-count", we do the work
1161 and set the done-count to the queued count. If the queued count is
1162 less than or equal to the done-count, we just ignore the call. This
1163 is needed because reading a single .o file will often replace many
1164 symtabs (one per .h file, for example), and we don't want to reset
1165 the breakpoints N times in the user's face.
1167 The reason we both queue a cleanup, and call it directly after symbol
1168 reading, is because the cleanup protects us in case of errors, but is
1169 discarded if symbol reading is successful. */
1172 /* FIXME: As free_named_symtabs is currently a big noop this function
1173 is no longer needed. */
1175 clear_symtab_users_once
PARAMS ((void));
1177 static int clear_symtab_users_queued
;
1178 static int clear_symtab_users_done
;
1181 clear_symtab_users_once ()
1183 /* Enforce once-per-`do_cleanups'-semantics */
1184 if (clear_symtab_users_queued
<= clear_symtab_users_done
)
1186 clear_symtab_users_done
= clear_symtab_users_queued
;
1188 clear_symtab_users ();
1192 /* Delete the specified psymtab, and any others that reference it. */
1195 cashier_psymtab (pst
)
1196 struct partial_symtab
*pst
;
1198 struct partial_symtab
*ps
, *pprev
= NULL
;
1201 /* Find its previous psymtab in the chain */
1202 for (ps
= pst
->objfile
->psymtabs
; ps
; ps
= ps
->next
) {
1209 /* Unhook it from the chain. */
1210 if (ps
== pst
->objfile
->psymtabs
)
1211 pst
->objfile
->psymtabs
= ps
->next
;
1213 pprev
->next
= ps
->next
;
1215 /* FIXME, we can't conveniently deallocate the entries in the
1216 partial_symbol lists (global_psymbols/static_psymbols) that
1217 this psymtab points to. These just take up space until all
1218 the psymtabs are reclaimed. Ditto the dependencies list and
1219 filename, which are all in the psymbol_obstack. */
1221 /* We need to cashier any psymtab that has this one as a dependency... */
1223 for (ps
= pst
->objfile
->psymtabs
; ps
; ps
= ps
->next
) {
1224 for (i
= 0; i
< ps
->number_of_dependencies
; i
++) {
1225 if (ps
->dependencies
[i
] == pst
) {
1226 cashier_psymtab (ps
);
1227 goto again
; /* Must restart, chain has been munged. */
1234 /* If a symtab or psymtab for filename NAME is found, free it along
1235 with any dependent breakpoints, displays, etc.
1236 Used when loading new versions of object modules with the "add-file"
1237 command. This is only called on the top-level symtab or psymtab's name;
1238 it is not called for subsidiary files such as .h files.
1240 Return value is 1 if we blew away the environment, 0 if not.
1241 FIXME. The return valu appears to never be used.
1243 FIXME. I think this is not the best way to do this. We should
1244 work on being gentler to the environment while still cleaning up
1245 all stray pointers into the freed symtab. */
1248 free_named_symtabs (name
)
1252 /* FIXME: With the new method of each objfile having it's own
1253 psymtab list, this function needs serious rethinking. In particular,
1254 why was it ever necessary to toss psymtabs with specific compilation
1255 unit filenames, as opposed to all psymtabs from a particular symbol
1257 Well, the answer is that some systems permit reloading of particular
1258 compilation units. We want to blow away any old info about these
1259 compilation units, regardless of which objfiles they arrived in. --gnu. */
1261 register struct symtab
*s
;
1262 register struct symtab
*prev
;
1263 register struct partial_symtab
*ps
;
1264 struct blockvector
*bv
;
1267 /* We only wack things if the symbol-reload switch is set. */
1268 if (!symbol_reloading
)
1271 /* Some symbol formats have trouble providing file names... */
1272 if (name
== 0 || *name
== '\0')
1275 /* Look for a psymtab with the specified name. */
1278 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
) {
1279 if (STREQ (name
, ps
->filename
)) {
1280 cashier_psymtab (ps
); /* Blow it away...and its little dog, too. */
1281 goto again2
; /* Must restart, chain has been munged */
1285 /* Look for a symtab with the specified name. */
1287 for (s
= symtab_list
; s
; s
= s
->next
)
1289 if (STREQ (name
, s
->filename
))
1296 if (s
== symtab_list
)
1297 symtab_list
= s
->next
;
1299 prev
->next
= s
->next
;
1301 /* For now, queue a delete for all breakpoints, displays, etc., whether
1302 or not they depend on the symtab being freed. This should be
1303 changed so that only those data structures affected are deleted. */
1305 /* But don't delete anything if the symtab is empty.
1306 This test is necessary due to a bug in "dbxread.c" that
1307 causes empty symtabs to be created for N_SO symbols that
1308 contain the pathname of the object file. (This problem
1309 has been fixed in GDB 3.9x). */
1311 bv
= BLOCKVECTOR (s
);
1312 if (BLOCKVECTOR_NBLOCKS (bv
) > 2
1313 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
))
1314 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
)))
1316 complain (&oldsyms_complaint
, name
);
1318 clear_symtab_users_queued
++;
1319 make_cleanup (clear_symtab_users_once
, 0);
1322 complain (&empty_symtab_complaint
, name
);
1329 /* It is still possible that some breakpoints will be affected
1330 even though no symtab was found, since the file might have
1331 been compiled without debugging, and hence not be associated
1332 with a symtab. In order to handle this correctly, we would need
1333 to keep a list of text address ranges for undebuggable files.
1334 For now, we do nothing, since this is a fairly obscure case. */
1338 /* FIXME, what about the minimal symbol table? */
1345 /* Allocate and partially fill a partial symtab. It will be
1346 completely filled at the end of the symbol list.
1348 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1349 is the address relative to which its symbols are (incremental) or 0
1353 struct partial_symtab
*
1354 start_psymtab_common (objfile
, section_offsets
,
1355 filename
, textlow
, global_syms
, static_syms
)
1356 struct objfile
*objfile
;
1357 struct section_offsets
*section_offsets
;
1360 struct partial_symbol
*global_syms
;
1361 struct partial_symbol
*static_syms
;
1363 struct partial_symtab
*psymtab
;
1365 psymtab
= allocate_psymtab (filename
, objfile
);
1366 psymtab
-> section_offsets
= section_offsets
;
1367 psymtab
-> textlow
= textlow
;
1368 psymtab
-> texthigh
= psymtab
-> textlow
; /* default */
1369 psymtab
-> globals_offset
= global_syms
- objfile
-> global_psymbols
.list
;
1370 psymtab
-> statics_offset
= static_syms
- objfile
-> static_psymbols
.list
;
1374 /* Debugging versions of functions that are usually inline macros
1377 #if !INLINE_ADD_PSYMBOL
1379 /* Add a symbol with a long value to a psymtab.
1380 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1383 add_psymbol_to_list (name
, namelength
, namespace, class, list
, val
, language
,
1387 enum namespace namespace;
1388 enum address_class
class;
1389 struct psymbol_allocation_list
*list
;
1391 enum language language
;
1392 struct objfile
*objfile
;
1394 register struct partial_symbol
*psym
;
1395 register char *demangled_name
;
1397 if (list
->next
>= list
->list
+ list
->size
)
1399 extend_psymbol_list (list
,objfile
);
1401 psym
= list
->next
++;
1403 SYMBOL_NAME (psym
) =
1404 (char *) obstack_alloc (&objfile
->psymbol_obstack
, namelength
+ 1);
1405 memcpy (SYMBOL_NAME (psym
), name
, namelength
);
1406 SYMBOL_NAME (psym
)[namelength
] = '\0';
1407 SYMBOL_VALUE (psym
) = val
;
1408 SYMBOL_LANGUAGE (psym
) = language
;
1409 PSYMBOL_NAMESPACE (psym
) = namespace;
1410 PSYMBOL_CLASS (psym
) = class;
1411 SYMBOL_INIT_DEMANGLED_NAME (psym
, &objfile
->psymbol_obstack
);
1414 /* Add a symbol with a CORE_ADDR value to a psymtab. */
1417 add_psymbol_addr_to_list (name
, namelength
, namespace, class, list
, val
,
1421 enum namespace namespace;
1422 enum address_class
class;
1423 struct psymbol_allocation_list
*list
;
1425 enum language language
;
1426 struct objfile
*objfile
;
1428 register struct partial_symbol
*psym
;
1429 register char *demangled_name
;
1431 if (list
->next
>= list
->list
+ list
->size
)
1433 extend_psymbol_list (list
,objfile
);
1435 psym
= list
->next
++;
1437 SYMBOL_NAME (psym
) =
1438 (char *) obstack_alloc (&objfile
->psymbol_obstack
, namelength
+ 1);
1439 memcpy (SYMBOL_NAME (psym
), name
, namelength
);
1440 SYMBOL_NAME (psym
)[namelength
] = '\0';
1441 SYMBOL_VALUE_ADDRESS (psym
) = val
;
1442 SYMBOL_LANGUAGE (psym
) = language
;
1443 PSYMBOL_NAMESPACE (psym
) = namespace;
1444 PSYMBOL_CLASS (psym
) = class;
1445 SYMBOL_INIT_DEMANGLED_NAME (psym
, &objfile
->psymbol_obstack
);
1448 #endif /* !INLINE_ADD_PSYMBOL */
1452 _initialize_symfile ()
1454 struct cmd_list_element
*c
;
1456 c
= add_cmd ("symbol-file", class_files
, symbol_file_command
,
1457 "Load symbol table from executable file FILE.\n\
1458 The `file' command can also load symbol tables, as well as setting the file\n\
1459 to execute.", &cmdlist
);
1460 c
->completer
= filename_completer
;
1462 c
= add_cmd ("add-symbol-file", class_files
, add_symbol_file_command
,
1463 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1464 The second argument provides the starting address of the file's text.",
1466 c
->completer
= filename_completer
;
1468 c
= add_cmd ("load", class_files
, load_command
,
1469 "Dynamically load FILE into the running program, and record its symbols\n\
1470 for access from GDB.", &cmdlist
);
1471 c
->completer
= filename_completer
;
1474 (add_set_cmd ("symbol-reloading", class_support
, var_boolean
,
1475 (char *)&symbol_reloading
,
1476 "Set dynamic symbol table reloading multiple times in one run.",