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