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