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