* configure,configure.in: Add target sparclet.
[binutils-gdb.git] / gdb / symfile.c
1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support, using pieces from other GDB modules.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbcmd.h"
32 #include "breakpoint.h"
33 #include "language.h"
34 #include "complaints.h"
35 #include "demangle.h"
36 #include "inferior.h" /* for write_pc */
37
38 #include "obstack.h"
39 #include <assert.h>
40
41 #include <sys/types.h>
42 #include <fcntl.h>
43 #include "gdb_string.h"
44 #include "gdb_stat.h"
45 #include <ctype.h>
46 #include <time.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #ifndef O_BINARY
52 #define O_BINARY 0
53 #endif
54
55 /* Global variables owned by this file */
56 int readnow_symbol_files; /* Read full symbols immediately */
57
58 struct complaint oldsyms_complaint = {
59 "Replacing old symbols for `%s'", 0, 0
60 };
61
62 struct complaint empty_symtab_complaint = {
63 "Empty symbol table found for `%s'", 0, 0
64 };
65
66 /* External variables and functions referenced. */
67
68 extern int info_verbose;
69
70 extern void report_transfer_performance PARAMS ((unsigned long,
71 time_t, time_t));
72
73 /* Functions this file defines */
74
75 static void set_initial_language PARAMS ((void));
76
77 static void load_command PARAMS ((char *, int));
78
79 static void add_symbol_file_command PARAMS ((char *, int));
80
81 static void add_shared_symbol_files_command PARAMS ((char *, int));
82
83 static void cashier_psymtab PARAMS ((struct partial_symtab *));
84
85 static int compare_psymbols PARAMS ((const void *, const void *));
86
87 static int compare_symbols PARAMS ((const void *, const void *));
88
89 static bfd *symfile_bfd_open PARAMS ((char *));
90
91 static void find_sym_fns PARAMS ((struct objfile *));
92
93 /* List of all available sym_fns. On gdb startup, each object file reader
94 calls add_symtab_fns() to register information on each format it is
95 prepared to read. */
96
97 static struct sym_fns *symtab_fns = NULL;
98
99 /* Flag for whether user will be reloading symbols multiple times.
100 Defaults to ON for VxWorks, otherwise OFF. */
101
102 #ifdef SYMBOL_RELOADING_DEFAULT
103 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
104 #else
105 int symbol_reloading = 0;
106 #endif
107
108 /* If true, then shared library symbols will be added automatically
109 when the inferior is created, new libraries are loaded, or when
110 attaching to the inferior. This is almost always what users
111 will want to have happen; but for very large programs, the startup
112 time will be excessive, and so if this is a problem, the user can
113 clear this flag and then add the shared library symbols as needed.
114 Note that there is a potential for confusion, since if the shared
115 library symbols are not loaded, commands like "info fun" will *not*
116 report all the functions that are actually present. */
117
118 int auto_solib_add = 1;
119
120 \f
121 /* Since this function is called from within qsort, in an ANSI environment
122 it must conform to the prototype for qsort, which specifies that the
123 comparison function takes two "void *" pointers. */
124
125 static int
126 compare_symbols (s1p, s2p)
127 const PTR s1p;
128 const PTR s2p;
129 {
130 register struct symbol **s1, **s2;
131
132 s1 = (struct symbol **) s1p;
133 s2 = (struct symbol **) s2p;
134
135 return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
136 }
137
138 /*
139
140 LOCAL FUNCTION
141
142 compare_psymbols -- compare two partial symbols by name
143
144 DESCRIPTION
145
146 Given pointers to pointers to two partial symbol table entries,
147 compare them by name and return -N, 0, or +N (ala strcmp).
148 Typically used by sorting routines like qsort().
149
150 NOTES
151
152 Does direct compare of first two characters before punting
153 and passing to strcmp for longer compares. Note that the
154 original version had a bug whereby two null strings or two
155 identically named one character strings would return the
156 comparison of memory following the null byte.
157
158 */
159
160 static int
161 compare_psymbols (s1p, s2p)
162 const PTR s1p;
163 const PTR s2p;
164 {
165 register char *st1 = SYMBOL_NAME (*(struct partial_symbol **) s1p);
166 register char *st2 = SYMBOL_NAME (*(struct partial_symbol **) s2p);
167
168 if ((st1[0] - st2[0]) || !st1[0])
169 {
170 return (st1[0] - st2[0]);
171 }
172 else if ((st1[1] - st2[1]) || !st1[1])
173 {
174 return (st1[1] - st2[1]);
175 }
176 else
177 {
178 return (STRCMP (st1 + 2, st2 + 2));
179 }
180 }
181
182 void
183 sort_pst_symbols (pst)
184 struct partial_symtab *pst;
185 {
186 /* Sort the global list; don't sort the static list */
187
188 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
189 pst -> n_global_syms, sizeof (struct partial_symbol *),
190 compare_psymbols);
191 }
192
193 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
194
195 void
196 sort_block_syms (b)
197 register struct block *b;
198 {
199 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
200 sizeof (struct symbol *), compare_symbols);
201 }
202
203 /* Call sort_symtab_syms to sort alphabetically
204 the symbols of each block of one symtab. */
205
206 void
207 sort_symtab_syms (s)
208 register struct symtab *s;
209 {
210 register struct blockvector *bv;
211 int nbl;
212 int i;
213 register struct block *b;
214
215 if (s == 0)
216 return;
217 bv = BLOCKVECTOR (s);
218 nbl = BLOCKVECTOR_NBLOCKS (bv);
219 for (i = 0; i < nbl; i++)
220 {
221 b = BLOCKVECTOR_BLOCK (bv, i);
222 if (BLOCK_SHOULD_SORT (b))
223 sort_block_syms (b);
224 }
225 }
226
227 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
228 (and add a null character at the end in the copy).
229 Returns the address of the copy. */
230
231 char *
232 obsavestring (ptr, size, obstackp)
233 char *ptr;
234 int size;
235 struct obstack *obstackp;
236 {
237 register char *p = (char *) obstack_alloc (obstackp, size + 1);
238 /* Open-coded memcpy--saves function call time.
239 These strings are usually short. */
240 {
241 register char *p1 = ptr;
242 register char *p2 = p;
243 char *end = ptr + size;
244 while (p1 != end)
245 *p2++ = *p1++;
246 }
247 p[size] = 0;
248 return p;
249 }
250
251 /* Concatenate strings S1, S2 and S3; return the new string.
252 Space is found in the symbol_obstack. */
253
254 char *
255 obconcat (obstackp, s1, s2, s3)
256 struct obstack *obstackp;
257 const char *s1, *s2, *s3;
258 {
259 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
260 register char *val = (char *) obstack_alloc (obstackp, len);
261 strcpy (val, s1);
262 strcat (val, s2);
263 strcat (val, s3);
264 return val;
265 }
266
267 /* True if we are nested inside psymtab_to_symtab. */
268
269 int currently_reading_symtab = 0;
270
271 static void
272 decrement_reading_symtab (dummy)
273 void *dummy;
274 {
275 currently_reading_symtab--;
276 }
277
278 /* Get the symbol table that corresponds to a partial_symtab.
279 This is fast after the first time you do it. In fact, there
280 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
281 case inline. */
282
283 struct symtab *
284 psymtab_to_symtab (pst)
285 register struct partial_symtab *pst;
286 {
287 /* If it's been looked up before, return it. */
288 if (pst->symtab)
289 return pst->symtab;
290
291 /* If it has not yet been read in, read it. */
292 if (!pst->readin)
293 {
294 struct cleanup *back_to = make_cleanup (decrement_reading_symtab, NULL);
295 currently_reading_symtab++;
296 (*pst->read_symtab) (pst);
297 do_cleanups (back_to);
298 }
299
300 return pst->symtab;
301 }
302
303 /* Initialize entry point information for this objfile. */
304
305 void
306 init_entry_point_info (objfile)
307 struct objfile *objfile;
308 {
309 /* Save startup file's range of PC addresses to help blockframe.c
310 decide where the bottom of the stack is. */
311
312 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
313 {
314 /* Executable file -- record its entry point so we'll recognize
315 the startup file because it contains the entry point. */
316 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
317 }
318 else
319 {
320 /* Examination of non-executable.o files. Short-circuit this stuff. */
321 objfile -> ei.entry_point = INVALID_ENTRY_POINT;
322 }
323 objfile -> ei.entry_file_lowpc = INVALID_ENTRY_LOWPC;
324 objfile -> ei.entry_file_highpc = INVALID_ENTRY_HIGHPC;
325 objfile -> ei.entry_func_lowpc = INVALID_ENTRY_LOWPC;
326 objfile -> ei.entry_func_highpc = INVALID_ENTRY_HIGHPC;
327 objfile -> ei.main_func_lowpc = INVALID_ENTRY_LOWPC;
328 objfile -> ei.main_func_highpc = INVALID_ENTRY_HIGHPC;
329 }
330
331 /* Get current entry point address. */
332
333 CORE_ADDR
334 entry_point_address()
335 {
336 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
337 }
338
339 /* Remember the lowest-addressed loadable section we've seen.
340 This function is called via bfd_map_over_sections.
341
342 In case of equal vmas, the section with the largest size becomes the
343 lowest-addressed loadable section.
344
345 If the vmas and sizes are equal, the last section is considered the
346 lowest-addressed loadable section. */
347
348 void
349 find_lowest_section (abfd, sect, obj)
350 bfd *abfd;
351 asection *sect;
352 PTR obj;
353 {
354 asection **lowest = (asection **)obj;
355
356 if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
357 return;
358 if (!*lowest)
359 *lowest = sect; /* First loadable section */
360 else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
361 *lowest = sect; /* A lower loadable section */
362 else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
363 && (bfd_section_size (abfd, (*lowest))
364 <= bfd_section_size (abfd, sect)))
365 *lowest = sect;
366 }
367
368 /* Process a symbol file, as either the main file or as a dynamically
369 loaded file.
370
371 NAME is the file name (which will be tilde-expanded and made
372 absolute herein) (but we don't free or modify NAME itself).
373 FROM_TTY says how verbose to be. MAINLINE specifies whether this
374 is the main symbol file, or whether it's an extra symbol file such
375 as dynamically loaded code. If !mainline, ADDR is the address
376 where the text segment was loaded. If VERBO, the caller has printed
377 a verbose message about the symbol reading (and complaints can be
378 more terse about it). */
379
380 void
381 syms_from_objfile (objfile, addr, mainline, verbo)
382 struct objfile *objfile;
383 CORE_ADDR addr;
384 int mainline;
385 int verbo;
386 {
387 struct section_offsets *section_offsets;
388 asection *lowest_sect;
389 struct cleanup *old_chain;
390
391 init_entry_point_info (objfile);
392 find_sym_fns (objfile);
393
394 /* Make sure that partially constructed symbol tables will be cleaned up
395 if an error occurs during symbol reading. */
396 old_chain = make_cleanup (free_objfile, objfile);
397
398 if (mainline)
399 {
400 /* We will modify the main symbol table, make sure that all its users
401 will be cleaned up if an error occurs during symbol reading. */
402 make_cleanup (clear_symtab_users, 0);
403
404 /* Since no error yet, throw away the old symbol table. */
405
406 if (symfile_objfile != NULL)
407 {
408 free_objfile (symfile_objfile);
409 symfile_objfile = NULL;
410 }
411
412 /* Currently we keep symbols from the add-symbol-file command.
413 If the user wants to get rid of them, they should do "symbol-file"
414 without arguments first. Not sure this is the best behavior
415 (PR 2207). */
416
417 (*objfile -> sf -> sym_new_init) (objfile);
418 }
419
420 /* Convert addr into an offset rather than an absolute address.
421 We find the lowest address of a loaded segment in the objfile,
422 and assume that <addr> is where that got loaded. Due to historical
423 precedent, we warn if that doesn't happen to be a text segment. */
424
425 if (mainline)
426 {
427 addr = 0; /* No offset from objfile addresses. */
428 }
429 else
430 {
431 lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
432 if (lowest_sect == NULL)
433 bfd_map_over_sections (objfile->obfd, find_lowest_section,
434 (PTR) &lowest_sect);
435
436 if (lowest_sect == NULL)
437 warning ("no loadable sections found in added symbol-file %s",
438 objfile->name);
439 else if ((bfd_get_section_flags (objfile->obfd, lowest_sect) & SEC_CODE)
440 == 0)
441 /* FIXME-32x64--assumes bfd_vma fits in long. */
442 warning ("Lowest section in %s is %s at 0x%lx",
443 objfile->name,
444 bfd_section_name (objfile->obfd, lowest_sect),
445 (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
446
447 if (lowest_sect)
448 addr -= bfd_section_vma (objfile->obfd, lowest_sect);
449 }
450
451 /* Initialize symbol reading routines for this objfile, allow complaints to
452 appear for this new file, and record how verbose to be, then do the
453 initial symbol reading for this file. */
454
455 (*objfile -> sf -> sym_init) (objfile);
456 clear_complaints (1, verbo);
457
458 section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
459 objfile->section_offsets = section_offsets;
460
461 #ifndef IBM6000_TARGET
462 /* This is a SVR4/SunOS specific hack, I think. In any event, it
463 screws RS/6000. sym_offsets should be doing this sort of thing,
464 because it knows the mapping between bfd sections and
465 section_offsets. */
466 /* This is a hack. As far as I can tell, section offsets are not
467 target dependent. They are all set to addr with a couple of
468 exceptions. The exceptions are sysvr4 shared libraries, whose
469 offsets are kept in solib structures anyway and rs6000 xcoff
470 which handles shared libraries in a completely unique way.
471
472 Section offsets are built similarly, except that they are built
473 by adding addr in all cases because there is no clear mapping
474 from section_offsets into actual sections. Note that solib.c
475 has a different algorythm for finding section offsets.
476
477 These should probably all be collapsed into some target
478 independent form of shared library support. FIXME. */
479
480 if (addr)
481 {
482 struct obj_section *s;
483
484 for (s = objfile->sections; s < objfile->sections_end; ++s)
485 {
486 s->addr -= s->offset;
487 s->addr += addr;
488 s->endaddr -= s->offset;
489 s->endaddr += addr;
490 s->offset += addr;
491 }
492 }
493 #endif /* not IBM6000_TARGET */
494
495 (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
496
497 if (!have_partial_symbols () && !have_full_symbols ())
498 {
499 wrap_here ("");
500 printf_filtered ("(no debugging symbols found)...");
501 wrap_here ("");
502 }
503
504 /* Don't allow char * to have a typename (else would get caddr_t).
505 Ditto void *. FIXME: Check whether this is now done by all the
506 symbol readers themselves (many of them now do), and if so remove
507 it from here. */
508
509 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
510 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
511
512 /* Mark the objfile has having had initial symbol read attempted. Note
513 that this does not mean we found any symbols... */
514
515 objfile -> flags |= OBJF_SYMS;
516
517 /* Discard cleanups as symbol reading was successful. */
518
519 discard_cleanups (old_chain);
520
521 /* Call this after reading in a new symbol table to give target dependant code
522 a crack at the new symbols. For instance, this could be used to update the
523 values of target-specific symbols GDB needs to keep track of (such as
524 _sigtramp, or whatever). */
525
526 TARGET_SYMFILE_POSTREAD (objfile);
527 }
528
529 /* Perform required actions after either reading in the initial
530 symbols for a new objfile, or mapping in the symbols from a reusable
531 objfile. */
532
533 void
534 new_symfile_objfile (objfile, mainline, verbo)
535 struct objfile *objfile;
536 int mainline;
537 int verbo;
538 {
539
540 /* If this is the main symbol file we have to clean up all users of the
541 old main symbol file. Otherwise it is sufficient to fixup all the
542 breakpoints that may have been redefined by this symbol file. */
543 if (mainline)
544 {
545 /* OK, make it the "real" symbol file. */
546 symfile_objfile = objfile;
547
548 clear_symtab_users ();
549 }
550 else
551 {
552 breakpoint_re_set ();
553 }
554
555 /* We're done reading the symbol file; finish off complaints. */
556 clear_complaints (0, verbo);
557 }
558
559 /* Process a symbol file, as either the main file or as a dynamically
560 loaded file.
561
562 NAME is the file name (which will be tilde-expanded and made
563 absolute herein) (but we don't free or modify NAME itself).
564 FROM_TTY says how verbose to be. MAINLINE specifies whether this
565 is the main symbol file, or whether it's an extra symbol file such
566 as dynamically loaded code. If !mainline, ADDR is the address
567 where the text segment was loaded.
568
569 Upon success, returns a pointer to the objfile that was added.
570 Upon failure, jumps back to command level (never returns). */
571
572 struct objfile *
573 symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
574 char *name;
575 int from_tty;
576 CORE_ADDR addr;
577 int mainline;
578 int mapped;
579 int readnow;
580 {
581 struct objfile *objfile;
582 struct partial_symtab *psymtab;
583 bfd *abfd;
584
585 /* Open a bfd for the file, and give user a chance to burp if we'd be
586 interactively wiping out any existing symbols. */
587
588 abfd = symfile_bfd_open (name);
589
590 if ((have_full_symbols () || have_partial_symbols ())
591 && mainline
592 && from_tty
593 && !query ("Load new symbol table from \"%s\"? ", name))
594 error ("Not confirmed.");
595
596 objfile = allocate_objfile (abfd, mapped);
597
598 /* If the objfile uses a mapped symbol file, and we have a psymtab for
599 it, then skip reading any symbols at this time. */
600
601 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
602 {
603 /* We mapped in an existing symbol table file that already has had
604 initial symbol reading performed, so we can skip that part. Notify
605 the user that instead of reading the symbols, they have been mapped.
606 */
607 if (from_tty || info_verbose)
608 {
609 printf_filtered ("Mapped symbols for %s...", name);
610 wrap_here ("");
611 gdb_flush (gdb_stdout);
612 }
613 init_entry_point_info (objfile);
614 find_sym_fns (objfile);
615 }
616 else
617 {
618 /* We either created a new mapped symbol table, mapped an existing
619 symbol table file which has not had initial symbol reading
620 performed, or need to read an unmapped symbol table. */
621 if (from_tty || info_verbose)
622 {
623 printf_filtered ("Reading symbols from %s...", name);
624 wrap_here ("");
625 gdb_flush (gdb_stdout);
626 }
627 syms_from_objfile (objfile, addr, mainline, from_tty);
628 }
629
630 /* We now have at least a partial symbol table. Check to see if the
631 user requested that all symbols be read on initial access via either
632 the gdb startup command line or on a per symbol file basis. Expand
633 all partial symbol tables for this objfile if so. */
634
635 if (readnow || readnow_symbol_files)
636 {
637 if (from_tty || info_verbose)
638 {
639 printf_filtered ("expanding to full symbols...");
640 wrap_here ("");
641 gdb_flush (gdb_stdout);
642 }
643
644 for (psymtab = objfile -> psymtabs;
645 psymtab != NULL;
646 psymtab = psymtab -> next)
647 {
648 psymtab_to_symtab (psymtab);
649 }
650 }
651
652 if (from_tty || info_verbose)
653 {
654 printf_filtered ("done.\n");
655 gdb_flush (gdb_stdout);
656 }
657
658 new_symfile_objfile (objfile, mainline, from_tty);
659
660 target_new_objfile (objfile);
661
662 return (objfile);
663 }
664
665 /* This is the symbol-file command. Read the file, analyze its
666 symbols, and add a struct symtab to a symtab list. The syntax of
667 the command is rather bizarre--(1) buildargv implements various
668 quoting conventions which are undocumented and have little or
669 nothing in common with the way things are quoted (or not quoted)
670 elsewhere in GDB, (2) options are used, which are not generally
671 used in GDB (perhaps "set mapped on", "set readnow on" would be
672 better), (3) the order of options matters, which is contrary to GNU
673 conventions (because it is confusing and inconvenient). */
674
675 void
676 symbol_file_command (args, from_tty)
677 char *args;
678 int from_tty;
679 {
680 char **argv;
681 char *name = NULL;
682 CORE_ADDR text_relocation = 0; /* text_relocation */
683 struct cleanup *cleanups;
684 int mapped = 0;
685 int readnow = 0;
686
687 dont_repeat ();
688
689 if (args == NULL)
690 {
691 if ((have_full_symbols () || have_partial_symbols ())
692 && from_tty
693 && !query ("Discard symbol table from `%s'? ",
694 symfile_objfile -> name))
695 error ("Not confirmed.");
696 free_all_objfiles ();
697 symfile_objfile = NULL;
698 if (from_tty)
699 {
700 printf_unfiltered ("No symbol file now.\n");
701 }
702 }
703 else
704 {
705 if ((argv = buildargv (args)) == NULL)
706 {
707 nomem (0);
708 }
709 cleanups = make_cleanup (freeargv, (char *) argv);
710 while (*argv != NULL)
711 {
712 if (STREQ (*argv, "-mapped"))
713 {
714 mapped = 1;
715 }
716 else if (STREQ (*argv, "-readnow"))
717 {
718 readnow = 1;
719 }
720 else if (**argv == '-')
721 {
722 error ("unknown option `%s'", *argv);
723 }
724 else
725 {
726 char *p;
727
728 name = *argv;
729
730 /* this is for rombug remote only, to get the text relocation by
731 using link command */
732 p = strrchr(name, '/');
733 if (p != NULL) p++;
734 else p = name;
735
736 target_link(p, &text_relocation);
737
738 if (text_relocation == (CORE_ADDR)0)
739 return;
740 else if (text_relocation == (CORE_ADDR)-1)
741 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
742 readnow);
743 else
744 symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
745 0, mapped, readnow);
746
747 /* Getting new symbols may change our opinion about what is
748 frameless. */
749 reinit_frame_cache ();
750
751 set_initial_language ();
752 }
753 argv++;
754 }
755
756 if (name == NULL)
757 {
758 error ("no symbol file name was specified");
759 }
760 do_cleanups (cleanups);
761 }
762 }
763
764 /* Set the initial language.
765
766 A better solution would be to record the language in the psymtab when reading
767 partial symbols, and then use it (if known) to set the language. This would
768 be a win for formats that encode the language in an easily discoverable place,
769 such as DWARF. For stabs, we can jump through hoops looking for specially
770 named symbols or try to intuit the language from the specific type of stabs
771 we find, but we can't do that until later when we read in full symbols.
772 FIXME. */
773
774 static void
775 set_initial_language ()
776 {
777 struct partial_symtab *pst;
778 enum language lang = language_unknown;
779
780 pst = find_main_psymtab ();
781 if (pst != NULL)
782 {
783 if (pst -> filename != NULL)
784 {
785 lang = deduce_language_from_filename (pst -> filename);
786 }
787 if (lang == language_unknown)
788 {
789 /* Make C the default language */
790 lang = language_c;
791 }
792 set_language (lang);
793 expected_language = current_language; /* Don't warn the user */
794 }
795 }
796
797 /* Open file specified by NAME and hand it off to BFD for preliminary
798 analysis. Result is a newly initialized bfd *, which includes a newly
799 malloc'd` copy of NAME (tilde-expanded and made absolute).
800 In case of trouble, error() is called. */
801
802 static bfd *
803 symfile_bfd_open (name)
804 char *name;
805 {
806 bfd *sym_bfd;
807 int desc;
808 char *absolute_name;
809
810 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
811
812 /* Look down path for it, allocate 2nd new malloc'd copy. */
813 desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
814 if (desc < 0)
815 {
816 make_cleanup (free, name);
817 perror_with_name (name);
818 }
819 free (name); /* Free 1st new malloc'd copy */
820 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
821 /* It'll be freed in free_objfile(). */
822
823 sym_bfd = bfd_fdopenr (name, gnutarget, desc);
824 if (!sym_bfd)
825 {
826 close (desc);
827 make_cleanup (free, name);
828 error ("\"%s\": can't open to read symbols: %s.", name,
829 bfd_errmsg (bfd_get_error ()));
830 }
831 sym_bfd->cacheable = true;
832
833 if (!bfd_check_format (sym_bfd, bfd_object))
834 {
835 /* FIXME: should be checking for errors from bfd_close (for one thing,
836 on error it does not free all the storage associated with the
837 bfd). */
838 bfd_close (sym_bfd); /* This also closes desc */
839 make_cleanup (free, name);
840 error ("\"%s\": can't read symbols: %s.", name,
841 bfd_errmsg (bfd_get_error ()));
842 }
843
844 return (sym_bfd);
845 }
846
847 /* Link a new symtab_fns into the global symtab_fns list. Called on gdb
848 startup by the _initialize routine in each object file format reader,
849 to register information about each format the the reader is prepared
850 to handle. */
851
852 void
853 add_symtab_fns (sf)
854 struct sym_fns *sf;
855 {
856 sf->next = symtab_fns;
857 symtab_fns = sf;
858 }
859
860
861 /* Initialize to read symbols from the symbol file sym_bfd. It either
862 returns or calls error(). The result is an initialized struct sym_fns
863 in the objfile structure, that contains cached information about the
864 symbol file. */
865
866 static void
867 find_sym_fns (objfile)
868 struct objfile *objfile;
869 {
870 struct sym_fns *sf;
871 enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
872 char *our_target = bfd_get_target (objfile -> obfd);
873
874 /* Special kludge for RS/6000 and PowerMac. See xcoffread.c. */
875 if (STREQ (our_target, "aixcoff-rs6000") ||
876 STREQ (our_target, "xcoff-powermac"))
877 our_flavour = (enum bfd_flavour)-1;
878
879 /* Special kludge for apollo. See dstread.c. */
880 if (STREQN (our_target, "apollo", 6))
881 our_flavour = (enum bfd_flavour)-2;
882
883 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
884 {
885 if (our_flavour == sf -> sym_flavour)
886 {
887 objfile -> sf = sf;
888 return;
889 }
890 }
891 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
892 bfd_get_target (objfile -> obfd));
893 }
894 \f
895 /* This function runs the load command of our current target. */
896
897 static void
898 load_command (arg, from_tty)
899 char *arg;
900 int from_tty;
901 {
902 if (arg == NULL)
903 arg = get_exec_file (1);
904 target_load (arg, from_tty);
905 }
906
907 /* This version of "load" should be usable for any target. Currently
908 it is just used for remote targets, not inftarg.c or core files,
909 on the theory that only in that case is it useful.
910
911 Avoiding xmodem and the like seems like a win (a) because we don't have
912 to worry about finding it, and (b) On VMS, fork() is very slow and so
913 we don't want to run a subprocess. On the other hand, I'm not sure how
914 performance compares. */
915 void
916 generic_load (filename, from_tty)
917 char *filename;
918 int from_tty;
919 {
920 struct cleanup *old_cleanups;
921 asection *s;
922 bfd *loadfile_bfd;
923 time_t start_time, end_time; /* Start and end times of download */
924 unsigned long data_count = 0; /* Number of bytes transferred to memory */
925 int n;
926 unsigned long load_offset = 0; /* offset to add to vma for each section */
927 char buf[128];
928
929 /* enable user to specify address for downloading as 2nd arg to load */
930 n = sscanf(filename, "%s 0x%x", buf, &load_offset);
931 if (n > 1 )
932 filename = buf;
933 else
934 load_offset = 0;
935
936 loadfile_bfd = bfd_openr (filename, gnutarget);
937 if (loadfile_bfd == NULL)
938 {
939 perror_with_name (filename);
940 return;
941 }
942 /* FIXME: should be checking for errors from bfd_close (for one thing,
943 on error it does not free all the storage associated with the
944 bfd). */
945 old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
946
947 if (!bfd_check_format (loadfile_bfd, bfd_object))
948 {
949 error ("\"%s\" is not an object file: %s", filename,
950 bfd_errmsg (bfd_get_error ()));
951 }
952
953 start_time = time (NULL);
954
955 for (s = loadfile_bfd->sections; s; s = s->next)
956 {
957 if (s->flags & SEC_LOAD)
958 {
959 bfd_size_type size;
960
961 size = bfd_get_section_size_before_reloc (s);
962 if (size > 0)
963 {
964 char *buffer;
965 struct cleanup *old_chain;
966 bfd_vma vma;
967
968 data_count += size;
969
970 buffer = xmalloc (size);
971 old_chain = make_cleanup (free, buffer);
972
973 vma = bfd_get_section_vma (loadfile_bfd, s);
974 vma += load_offset;
975
976 /* Is this really necessary? I guess it gives the user something
977 to look at during a long download. */
978 printf_filtered ("Loading section %s, size 0x%lx vma ",
979 bfd_get_section_name (loadfile_bfd, s),
980 (unsigned long) size);
981 print_address_numeric (vma, 1, gdb_stdout);
982 printf_filtered ("\n");
983
984 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
985
986 target_write_memory (vma, buffer, size);
987
988 do_cleanups (old_chain);
989 }
990 }
991 }
992
993 end_time = time (NULL);
994
995 printf_filtered ("Start address 0x%lx\n", loadfile_bfd->start_address);
996
997 /* We were doing this in remote-mips.c, I suspect it is right
998 for other targets too. */
999 write_pc (loadfile_bfd->start_address);
1000
1001 /* FIXME: are we supposed to call symbol_file_add or not? According to
1002 a comment from remote-mips.c (where a call to symbol_file_add was
1003 commented out), making the call confuses GDB if more than one file is
1004 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
1005 does. */
1006
1007 report_transfer_performance (data_count, start_time, end_time);
1008
1009 do_cleanups (old_cleanups);
1010 }
1011
1012 /* Report how fast the transfer went. */
1013
1014 void
1015 report_transfer_performance (data_count, start_time, end_time)
1016 unsigned long data_count;
1017 time_t start_time, end_time;
1018 {
1019 printf_filtered ("Transfer rate: ");
1020 if (end_time != start_time)
1021 printf_filtered ("%d bits/sec",
1022 (data_count * 8) / (end_time - start_time));
1023 else
1024 printf_filtered ("%d bits in <1 sec", (data_count * 8));
1025 printf_filtered (".\n");
1026 }
1027
1028 /* This function allows the addition of incrementally linked object files.
1029 It does not modify any state in the target, only in the debugger. */
1030
1031 /* ARGSUSED */
1032 static void
1033 add_symbol_file_command (args, from_tty)
1034 char *args;
1035 int from_tty;
1036 {
1037 char *name = NULL;
1038 CORE_ADDR text_addr;
1039 char *arg;
1040 int readnow = 0;
1041 int mapped = 0;
1042
1043 dont_repeat ();
1044
1045 if (args == NULL)
1046 {
1047 error ("add-symbol-file takes a file name and an address");
1048 }
1049
1050 /* Make a copy of the string that we can safely write into. */
1051
1052 args = strdup (args);
1053 make_cleanup (free, args);
1054
1055 /* Pick off any -option args and the file name. */
1056
1057 while ((*args != '\000') && (name == NULL))
1058 {
1059 while (isspace (*args)) {args++;}
1060 arg = args;
1061 while ((*args != '\000') && !isspace (*args)) {args++;}
1062 if (*args != '\000')
1063 {
1064 *args++ = '\000';
1065 }
1066 if (*arg != '-')
1067 {
1068 name = arg;
1069 }
1070 else if (STREQ (arg, "-mapped"))
1071 {
1072 mapped = 1;
1073 }
1074 else if (STREQ (arg, "-readnow"))
1075 {
1076 readnow = 1;
1077 }
1078 else
1079 {
1080 error ("unknown option `%s'", arg);
1081 }
1082 }
1083
1084 /* After picking off any options and the file name, args should be
1085 left pointing at the remainder of the command line, which should
1086 be the address expression to evaluate. */
1087
1088 if (name == NULL)
1089 {
1090 error ("add-symbol-file takes a file name");
1091 }
1092 name = tilde_expand (name);
1093 make_cleanup (free, name);
1094
1095 if (*args != '\000')
1096 {
1097 text_addr = parse_and_eval_address (args);
1098 }
1099 else
1100 {
1101 target_link(name, &text_addr);
1102 if (text_addr == (CORE_ADDR)-1)
1103 error("Don't know how to get text start location for this file");
1104 }
1105
1106 /* FIXME-32x64: Assumes text_addr fits in a long. */
1107 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
1108 name, local_hex_string ((unsigned long)text_addr)))
1109 error ("Not confirmed.");
1110
1111 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
1112
1113 /* Getting new symbols may change our opinion about what is
1114 frameless. */
1115 reinit_frame_cache ();
1116 }
1117 \f
1118 static void
1119 add_shared_symbol_files_command (args, from_tty)
1120 char *args;
1121 int from_tty;
1122 {
1123 #ifdef ADD_SHARED_SYMBOL_FILES
1124 ADD_SHARED_SYMBOL_FILES (args, from_tty);
1125 #else
1126 error ("This command is not available in this configuration of GDB.");
1127 #endif
1128 }
1129 \f
1130 /* Re-read symbols if a symbol-file has changed. */
1131 void
1132 reread_symbols ()
1133 {
1134 struct objfile *objfile;
1135 long new_modtime;
1136 int reread_one = 0;
1137 struct stat new_statbuf;
1138 int res;
1139
1140 /* With the addition of shared libraries, this should be modified,
1141 the load time should be saved in the partial symbol tables, since
1142 different tables may come from different source files. FIXME.
1143 This routine should then walk down each partial symbol table
1144 and see if the symbol table that it originates from has been changed */
1145
1146 for (objfile = object_files; objfile; objfile = objfile->next) {
1147 if (objfile->obfd) {
1148 #ifdef IBM6000_TARGET
1149 /* If this object is from a shared library, then you should
1150 stat on the library name, not member name. */
1151
1152 if (objfile->obfd->my_archive)
1153 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1154 else
1155 #endif
1156 res = stat (objfile->name, &new_statbuf);
1157 if (res != 0) {
1158 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1159 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1160 objfile->name);
1161 continue;
1162 }
1163 new_modtime = new_statbuf.st_mtime;
1164 if (new_modtime != objfile->mtime)
1165 {
1166 struct cleanup *old_cleanups;
1167 struct section_offsets *offsets;
1168 int num_offsets;
1169 int section_offsets_size;
1170 char *obfd_filename;
1171
1172 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1173 objfile->name);
1174
1175 /* There are various functions like symbol_file_add,
1176 symfile_bfd_open, syms_from_objfile, etc., which might
1177 appear to do what we want. But they have various other
1178 effects which we *don't* want. So we just do stuff
1179 ourselves. We don't worry about mapped files (for one thing,
1180 any mapped file will be out of date). */
1181
1182 /* If we get an error, blow away this objfile (not sure if
1183 that is the correct response for things like shared
1184 libraries). */
1185 old_cleanups = make_cleanup (free_objfile, objfile);
1186 /* We need to do this whenever any symbols go away. */
1187 make_cleanup (clear_symtab_users, 0);
1188
1189 /* Clean up any state BFD has sitting around. We don't need
1190 to close the descriptor but BFD lacks a way of closing the
1191 BFD without closing the descriptor. */
1192 obfd_filename = bfd_get_filename (objfile->obfd);
1193 if (!bfd_close (objfile->obfd))
1194 error ("Can't close BFD for %s: %s", objfile->name,
1195 bfd_errmsg (bfd_get_error ()));
1196 objfile->obfd = bfd_openr (obfd_filename, gnutarget);
1197 if (objfile->obfd == NULL)
1198 error ("Can't open %s to read symbols.", objfile->name);
1199 /* bfd_openr sets cacheable to true, which is what we want. */
1200 if (!bfd_check_format (objfile->obfd, bfd_object))
1201 error ("Can't read symbols from %s: %s.", objfile->name,
1202 bfd_errmsg (bfd_get_error ()));
1203
1204 /* Save the offsets, we will nuke them with the rest of the
1205 psymbol_obstack. */
1206 num_offsets = objfile->num_sections;
1207 section_offsets_size =
1208 sizeof (struct section_offsets)
1209 + sizeof (objfile->section_offsets->offsets) * num_offsets;
1210 offsets = (struct section_offsets *) alloca (section_offsets_size);
1211 memcpy (offsets, objfile->section_offsets, section_offsets_size);
1212
1213 /* Nuke all the state that we will re-read. Much of the following
1214 code which sets things to NULL really is necessary to tell
1215 other parts of GDB that there is nothing currently there. */
1216
1217 /* FIXME: Do we have to free a whole linked list, or is this
1218 enough? */
1219 if (objfile->global_psymbols.list)
1220 mfree (objfile->md, objfile->global_psymbols.list);
1221 memset (&objfile -> global_psymbols, 0,
1222 sizeof (objfile -> global_psymbols));
1223 if (objfile->static_psymbols.list)
1224 mfree (objfile->md, objfile->static_psymbols.list);
1225 memset (&objfile -> static_psymbols, 0,
1226 sizeof (objfile -> static_psymbols));
1227
1228 /* Free the obstacks for non-reusable objfiles */
1229 obstack_free (&objfile -> psymbol_cache.cache, 0);
1230 memset (&objfile -> psymbol_cache, 0,
1231 sizeof (objfile -> psymbol_cache));
1232 obstack_free (&objfile -> psymbol_obstack, 0);
1233 obstack_free (&objfile -> symbol_obstack, 0);
1234 obstack_free (&objfile -> type_obstack, 0);
1235 objfile->sections = NULL;
1236 objfile->symtabs = NULL;
1237 objfile->psymtabs = NULL;
1238 objfile->free_psymtabs = NULL;
1239 objfile->msymbols = NULL;
1240 objfile->minimal_symbol_count= 0;
1241 objfile->fundamental_types = NULL;
1242 if (objfile -> sf != NULL)
1243 {
1244 (*objfile -> sf -> sym_finish) (objfile);
1245 }
1246
1247 /* We never make this a mapped file. */
1248 objfile -> md = NULL;
1249 /* obstack_specify_allocation also initializes the obstack so
1250 it is empty. */
1251 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
1252 xmalloc, free);
1253 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1254 xmalloc, free);
1255 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1256 xmalloc, free);
1257 obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1258 xmalloc, free);
1259 if (build_objfile_section_table (objfile))
1260 {
1261 error ("Can't find the file sections in `%s': %s",
1262 objfile -> name, bfd_errmsg (bfd_get_error ()));
1263 }
1264
1265 /* We use the same section offsets as from last time. I'm not
1266 sure whether that is always correct for shared libraries. */
1267 objfile->section_offsets = (struct section_offsets *)
1268 obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1269 memcpy (objfile->section_offsets, offsets, section_offsets_size);
1270 objfile->num_sections = num_offsets;
1271
1272 /* What the hell is sym_new_init for, anyway? The concept of
1273 distinguishing between the main file and additional files
1274 in this way seems rather dubious. */
1275 if (objfile == symfile_objfile)
1276 (*objfile->sf->sym_new_init) (objfile);
1277
1278 (*objfile->sf->sym_init) (objfile);
1279 clear_complaints (1, 1);
1280 /* The "mainline" parameter is a hideous hack; I think leaving it
1281 zero is OK since dbxread.c also does what it needs to do if
1282 objfile->global_psymbols.size is 0. */
1283 (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
1284 if (!have_partial_symbols () && !have_full_symbols ())
1285 {
1286 wrap_here ("");
1287 printf_filtered ("(no debugging symbols found)\n");
1288 wrap_here ("");
1289 }
1290 objfile -> flags |= OBJF_SYMS;
1291
1292 /* We're done reading the symbol file; finish off complaints. */
1293 clear_complaints (0, 1);
1294
1295 /* Getting new symbols may change our opinion about what is
1296 frameless. */
1297
1298 reinit_frame_cache ();
1299
1300 /* Discard cleanups as symbol reading was successful. */
1301 discard_cleanups (old_cleanups);
1302
1303 /* If the mtime has changed between the time we set new_modtime
1304 and now, we *want* this to be out of date, so don't call stat
1305 again now. */
1306 objfile->mtime = new_modtime;
1307 reread_one = 1;
1308
1309 /* Call this after reading in a new symbol table to give target
1310 dependant code a crack at the new symbols. For instance, this
1311 could be used to update the values of target-specific symbols GDB
1312 needs to keep track of (such as _sigtramp, or whatever). */
1313
1314 TARGET_SYMFILE_POSTREAD (objfile);
1315 }
1316 }
1317 }
1318
1319 if (reread_one)
1320 clear_symtab_users ();
1321 }
1322
1323 \f
1324 enum language
1325 deduce_language_from_filename (filename)
1326 char *filename;
1327 {
1328 char *c;
1329
1330 if (0 == filename)
1331 ; /* Get default */
1332 else if (0 == (c = strrchr (filename, '.')))
1333 ; /* Get default. */
1334 else if (STREQ (c, ".c"))
1335 return language_c;
1336 else if (STREQ (c, ".cc") || STREQ (c, ".C") || STREQ (c, ".cxx")
1337 || STREQ (c, ".cpp") || STREQ (c, ".cp") || STREQ (c, ".c++"))
1338 return language_cplus;
1339 else if (STREQ (c, ".ch") || STREQ (c, ".c186") || STREQ (c, ".c286"))
1340 return language_chill;
1341 else if (STREQ (c, ".f") || STREQ (c, ".F"))
1342 return language_fortran;
1343 else if (STREQ (c, ".mod"))
1344 return language_m2;
1345 else if (STREQ (c, ".s") || STREQ (c, ".S"))
1346 return language_asm;
1347
1348 return language_unknown; /* default */
1349 }
1350 \f
1351 /* allocate_symtab:
1352
1353 Allocate and partly initialize a new symbol table. Return a pointer
1354 to it. error() if no space.
1355
1356 Caller must set these fields:
1357 LINETABLE(symtab)
1358 symtab->blockvector
1359 symtab->dirname
1360 symtab->free_code
1361 symtab->free_ptr
1362 initialize any EXTRA_SYMTAB_INFO
1363 possibly free_named_symtabs (symtab->filename);
1364 */
1365
1366 struct symtab *
1367 allocate_symtab (filename, objfile)
1368 char *filename;
1369 struct objfile *objfile;
1370 {
1371 register struct symtab *symtab;
1372
1373 symtab = (struct symtab *)
1374 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
1375 memset (symtab, 0, sizeof (*symtab));
1376 symtab -> filename = obsavestring (filename, strlen (filename),
1377 &objfile -> symbol_obstack);
1378 symtab -> fullname = NULL;
1379 symtab -> language = deduce_language_from_filename (filename);
1380
1381 /* Hook it to the objfile it comes from */
1382
1383 symtab -> objfile = objfile;
1384 symtab -> next = objfile -> symtabs;
1385 objfile -> symtabs = symtab;
1386
1387 #ifdef INIT_EXTRA_SYMTAB_INFO
1388 INIT_EXTRA_SYMTAB_INFO (symtab);
1389 #endif
1390
1391 return (symtab);
1392 }
1393
1394 struct partial_symtab *
1395 allocate_psymtab (filename, objfile)
1396 char *filename;
1397 struct objfile *objfile;
1398 {
1399 struct partial_symtab *psymtab;
1400
1401 if (objfile -> free_psymtabs)
1402 {
1403 psymtab = objfile -> free_psymtabs;
1404 objfile -> free_psymtabs = psymtab -> next;
1405 }
1406 else
1407 psymtab = (struct partial_symtab *)
1408 obstack_alloc (&objfile -> psymbol_obstack,
1409 sizeof (struct partial_symtab));
1410
1411 memset (psymtab, 0, sizeof (struct partial_symtab));
1412 psymtab -> filename = obsavestring (filename, strlen (filename),
1413 &objfile -> psymbol_obstack);
1414 psymtab -> symtab = NULL;
1415
1416 /* Hook it to the objfile it comes from */
1417
1418 psymtab -> objfile = objfile;
1419 psymtab -> next = objfile -> psymtabs;
1420 objfile -> psymtabs = psymtab;
1421
1422 return (psymtab);
1423 }
1424
1425 \f
1426 /* Reset all data structures in gdb which may contain references to symbol
1427 table date. */
1428
1429 void
1430 clear_symtab_users ()
1431 {
1432 /* Someday, we should do better than this, by only blowing away
1433 the things that really need to be blown. */
1434 clear_value_history ();
1435 clear_displays ();
1436 clear_internalvars ();
1437 breakpoint_re_set ();
1438 set_default_breakpoint (0, 0, 0, 0);
1439 current_source_symtab = 0;
1440 current_source_line = 0;
1441 clear_pc_function_cache ();
1442 target_new_objfile (NULL);
1443 }
1444
1445 /* clear_symtab_users_once:
1446
1447 This function is run after symbol reading, or from a cleanup.
1448 If an old symbol table was obsoleted, the old symbol table
1449 has been blown away, but the other GDB data structures that may
1450 reference it have not yet been cleared or re-directed. (The old
1451 symtab was zapped, and the cleanup queued, in free_named_symtab()
1452 below.)
1453
1454 This function can be queued N times as a cleanup, or called
1455 directly; it will do all the work the first time, and then will be a
1456 no-op until the next time it is queued. This works by bumping a
1457 counter at queueing time. Much later when the cleanup is run, or at
1458 the end of symbol processing (in case the cleanup is discarded), if
1459 the queued count is greater than the "done-count", we do the work
1460 and set the done-count to the queued count. If the queued count is
1461 less than or equal to the done-count, we just ignore the call. This
1462 is needed because reading a single .o file will often replace many
1463 symtabs (one per .h file, for example), and we don't want to reset
1464 the breakpoints N times in the user's face.
1465
1466 The reason we both queue a cleanup, and call it directly after symbol
1467 reading, is because the cleanup protects us in case of errors, but is
1468 discarded if symbol reading is successful. */
1469
1470 #if 0
1471 /* FIXME: As free_named_symtabs is currently a big noop this function
1472 is no longer needed. */
1473 static void
1474 clear_symtab_users_once PARAMS ((void));
1475
1476 static int clear_symtab_users_queued;
1477 static int clear_symtab_users_done;
1478
1479 static void
1480 clear_symtab_users_once ()
1481 {
1482 /* Enforce once-per-`do_cleanups'-semantics */
1483 if (clear_symtab_users_queued <= clear_symtab_users_done)
1484 return;
1485 clear_symtab_users_done = clear_symtab_users_queued;
1486
1487 clear_symtab_users ();
1488 }
1489 #endif
1490
1491 /* Delete the specified psymtab, and any others that reference it. */
1492
1493 static void
1494 cashier_psymtab (pst)
1495 struct partial_symtab *pst;
1496 {
1497 struct partial_symtab *ps, *pprev = NULL;
1498 int i;
1499
1500 /* Find its previous psymtab in the chain */
1501 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1502 if (ps == pst)
1503 break;
1504 pprev = ps;
1505 }
1506
1507 if (ps) {
1508 /* Unhook it from the chain. */
1509 if (ps == pst->objfile->psymtabs)
1510 pst->objfile->psymtabs = ps->next;
1511 else
1512 pprev->next = ps->next;
1513
1514 /* FIXME, we can't conveniently deallocate the entries in the
1515 partial_symbol lists (global_psymbols/static_psymbols) that
1516 this psymtab points to. These just take up space until all
1517 the psymtabs are reclaimed. Ditto the dependencies list and
1518 filename, which are all in the psymbol_obstack. */
1519
1520 /* We need to cashier any psymtab that has this one as a dependency... */
1521 again:
1522 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
1523 for (i = 0; i < ps->number_of_dependencies; i++) {
1524 if (ps->dependencies[i] == pst) {
1525 cashier_psymtab (ps);
1526 goto again; /* Must restart, chain has been munged. */
1527 }
1528 }
1529 }
1530 }
1531 }
1532
1533 /* If a symtab or psymtab for filename NAME is found, free it along
1534 with any dependent breakpoints, displays, etc.
1535 Used when loading new versions of object modules with the "add-file"
1536 command. This is only called on the top-level symtab or psymtab's name;
1537 it is not called for subsidiary files such as .h files.
1538
1539 Return value is 1 if we blew away the environment, 0 if not.
1540 FIXME. The return valu appears to never be used.
1541
1542 FIXME. I think this is not the best way to do this. We should
1543 work on being gentler to the environment while still cleaning up
1544 all stray pointers into the freed symtab. */
1545
1546 int
1547 free_named_symtabs (name)
1548 char *name;
1549 {
1550 #if 0
1551 /* FIXME: With the new method of each objfile having it's own
1552 psymtab list, this function needs serious rethinking. In particular,
1553 why was it ever necessary to toss psymtabs with specific compilation
1554 unit filenames, as opposed to all psymtabs from a particular symbol
1555 file? -- fnf
1556 Well, the answer is that some systems permit reloading of particular
1557 compilation units. We want to blow away any old info about these
1558 compilation units, regardless of which objfiles they arrived in. --gnu. */
1559
1560 register struct symtab *s;
1561 register struct symtab *prev;
1562 register struct partial_symtab *ps;
1563 struct blockvector *bv;
1564 int blewit = 0;
1565
1566 /* We only wack things if the symbol-reload switch is set. */
1567 if (!symbol_reloading)
1568 return 0;
1569
1570 /* Some symbol formats have trouble providing file names... */
1571 if (name == 0 || *name == '\0')
1572 return 0;
1573
1574 /* Look for a psymtab with the specified name. */
1575
1576 again2:
1577 for (ps = partial_symtab_list; ps; ps = ps->next) {
1578 if (STREQ (name, ps->filename)) {
1579 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1580 goto again2; /* Must restart, chain has been munged */
1581 }
1582 }
1583
1584 /* Look for a symtab with the specified name. */
1585
1586 for (s = symtab_list; s; s = s->next)
1587 {
1588 if (STREQ (name, s->filename))
1589 break;
1590 prev = s;
1591 }
1592
1593 if (s)
1594 {
1595 if (s == symtab_list)
1596 symtab_list = s->next;
1597 else
1598 prev->next = s->next;
1599
1600 /* For now, queue a delete for all breakpoints, displays, etc., whether
1601 or not they depend on the symtab being freed. This should be
1602 changed so that only those data structures affected are deleted. */
1603
1604 /* But don't delete anything if the symtab is empty.
1605 This test is necessary due to a bug in "dbxread.c" that
1606 causes empty symtabs to be created for N_SO symbols that
1607 contain the pathname of the object file. (This problem
1608 has been fixed in GDB 3.9x). */
1609
1610 bv = BLOCKVECTOR (s);
1611 if (BLOCKVECTOR_NBLOCKS (bv) > 2
1612 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1613 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1614 {
1615 complain (&oldsyms_complaint, name);
1616
1617 clear_symtab_users_queued++;
1618 make_cleanup (clear_symtab_users_once, 0);
1619 blewit = 1;
1620 } else {
1621 complain (&empty_symtab_complaint, name);
1622 }
1623
1624 free_symtab (s);
1625 }
1626 else
1627 {
1628 /* It is still possible that some breakpoints will be affected
1629 even though no symtab was found, since the file might have
1630 been compiled without debugging, and hence not be associated
1631 with a symtab. In order to handle this correctly, we would need
1632 to keep a list of text address ranges for undebuggable files.
1633 For now, we do nothing, since this is a fairly obscure case. */
1634 ;
1635 }
1636
1637 /* FIXME, what about the minimal symbol table? */
1638 return blewit;
1639 #else
1640 return (0);
1641 #endif
1642 }
1643 \f
1644 /* Allocate and partially fill a partial symtab. It will be
1645 completely filled at the end of the symbol list.
1646
1647 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1648 is the address relative to which its symbols are (incremental) or 0
1649 (normal). */
1650
1651
1652 struct partial_symtab *
1653 start_psymtab_common (objfile, section_offsets,
1654 filename, textlow, global_syms, static_syms)
1655 struct objfile *objfile;
1656 struct section_offsets *section_offsets;
1657 char *filename;
1658 CORE_ADDR textlow;
1659 struct partial_symbol **global_syms;
1660 struct partial_symbol **static_syms;
1661 {
1662 struct partial_symtab *psymtab;
1663
1664 psymtab = allocate_psymtab (filename, objfile);
1665 psymtab -> section_offsets = section_offsets;
1666 psymtab -> textlow = textlow;
1667 psymtab -> texthigh = psymtab -> textlow; /* default */
1668 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1669 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1670 return (psymtab);
1671 }
1672 \f
1673 /* Add a symbol with a long value to a psymtab.
1674 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1675
1676 void
1677 add_psymbol_to_list (name, namelength, namespace, class, list, val, coreaddr,
1678 language, objfile)
1679 char *name;
1680 int namelength;
1681 namespace_enum namespace;
1682 enum address_class class;
1683 struct psymbol_allocation_list *list;
1684 long val; /* Value as a long */
1685 CORE_ADDR coreaddr; /* Value as a CORE_ADDR */
1686 enum language language;
1687 struct objfile *objfile;
1688 {
1689 register struct partial_symbol *psym;
1690 char *buf = alloca (namelength + 1);
1691 /* psymbol is static so that there will be no uninitialized gaps in the
1692 structure which might contain random data, causing cache misses in
1693 bcache. */
1694 static struct partial_symbol psymbol;
1695
1696 /* Create local copy of the partial symbol */
1697 memcpy (buf, name, namelength);
1698 buf[namelength] = '\0';
1699 SYMBOL_NAME (&psymbol) = bcache (buf, namelength + 1, &objfile->psymbol_cache);
1700 /* val and coreaddr are mutually exclusive, one of them *will* be zero */
1701 if (val != 0)
1702 {
1703 SYMBOL_VALUE (&psymbol) = val;
1704 }
1705 else
1706 {
1707 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1708 }
1709 SYMBOL_SECTION (&psymbol) = 0;
1710 SYMBOL_LANGUAGE (&psymbol) = language;
1711 PSYMBOL_NAMESPACE (&psymbol) = namespace;
1712 PSYMBOL_CLASS (&psymbol) = class;
1713 SYMBOL_INIT_LANGUAGE_SPECIFIC (&psymbol, language);
1714
1715 /* Stash the partial symbol away in the cache */
1716 psym = bcache (&psymbol, sizeof (struct partial_symbol), &objfile->psymbol_cache);
1717
1718 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1719 if (list->next >= list->list + list->size)
1720 {
1721 extend_psymbol_list (list, objfile);
1722 }
1723 *list->next++ = psym;
1724 OBJSTAT (objfile, n_psyms++);
1725 }
1726
1727 /* Initialize storage for partial symbols. */
1728
1729 void
1730 init_psymbol_list (objfile, total_symbols)
1731 struct objfile *objfile;
1732 int total_symbols;
1733 {
1734 /* Free any previously allocated psymbol lists. */
1735
1736 if (objfile -> global_psymbols.list)
1737 {
1738 mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
1739 }
1740 if (objfile -> static_psymbols.list)
1741 {
1742 mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
1743 }
1744
1745 /* Current best guess is that approximately a twentieth
1746 of the total symbols (in a debugging file) are global or static
1747 oriented symbols */
1748
1749 objfile -> global_psymbols.size = total_symbols / 10;
1750 objfile -> static_psymbols.size = total_symbols / 10;
1751 objfile -> global_psymbols.next =
1752 objfile -> global_psymbols.list = (struct partial_symbol **)
1753 xmmalloc (objfile -> md, objfile -> global_psymbols.size
1754 * sizeof (struct partial_symbol *));
1755 objfile -> static_psymbols.next =
1756 objfile -> static_psymbols.list = (struct partial_symbol **)
1757 xmmalloc (objfile -> md, objfile -> static_psymbols.size
1758 * sizeof (struct partial_symbol *));
1759 }
1760 \f
1761 void
1762 _initialize_symfile ()
1763 {
1764 struct cmd_list_element *c;
1765
1766 c = add_cmd ("symbol-file", class_files, symbol_file_command,
1767 "Load symbol table from executable file FILE.\n\
1768 The `file' command can also load symbol tables, as well as setting the file\n\
1769 to execute.", &cmdlist);
1770 c->completer = filename_completer;
1771
1772 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
1773 "Usage: add-symbol-file FILE ADDR\n\
1774 Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1775 ADDR is the starting address of the file's text.",
1776 &cmdlist);
1777 c->completer = filename_completer;
1778
1779 c = add_cmd ("add-shared-symbol-files", class_files,
1780 add_shared_symbol_files_command,
1781 "Load the symbols from shared objects in the dynamic linker's link map.",
1782 &cmdlist);
1783 c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
1784 &cmdlist);
1785
1786 c = add_cmd ("load", class_files, load_command,
1787 "Dynamically load FILE into the running program, and record its symbols\n\
1788 for access from GDB.", &cmdlist);
1789 c->completer = filename_completer;
1790
1791 add_show_from_set
1792 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1793 (char *)&symbol_reloading,
1794 "Set dynamic symbol table reloading multiple times in one run.",
1795 &setlist),
1796 &showlist);
1797
1798 }