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