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