8745796879d0497385858732ed5e686b7e1873b1
[deliverable/binutils-gdb.git] / gdb / symfile.c
1 /* Generic symbol file reading for the GNU debugger, GDB.
2 Copyright 1990, 1991 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 <stdio.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbcore.h"
25 #include "frame.h"
26 #include "target.h"
27 #include "value.h"
28 #include "symfile.h"
29 #include "gdbcmd.h"
30 #include "breakpoint.h"
31
32 #include <obstack.h>
33 #include <assert.h>
34
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <sys/stat.h>
39
40 CORE_ADDR entry_point; /* Where execution starts in symfile */
41
42 extern int info_verbose;
43
44 extern void qsort ();
45 extern char *getenv ();
46 extern char *rindex ();
47
48 extern CORE_ADDR startup_file_start; /* From blockframe.c */
49 extern CORE_ADDR startup_file_end; /* From blockframe.c */
50
51 /* Functions this file defines */
52 static struct objfile *symfile_open ();
53 static struct sym_fns *symfile_init ();
54 static void clear_symtab_users_once ();
55
56 static void free_all_psymtabs ();
57 static void free_all_symtabs ();
58
59 /* List of all available sym_fns. */
60
61 struct sym_fns *symtab_fns = NULL;
62
63 /* Saves the sym_fns of the current symbol table, so we can call
64 the right XXX_new_init function when we free it. FIXME. This
65 should be extended to calling the new_init function for each
66 existing symtab or psymtab, since the main symbol file and
67 subsequent added symbol files can have different types. */
68
69 static struct sym_fns *symfile_fns;
70
71 /* Allocate an obstack to hold objects that should be freed
72 when we load a new symbol table.
73 This includes the symbols made by dbxread
74 and the types that are not permanent. */
75
76 struct obstack obstack1;
77
78 struct obstack *symbol_obstack = &obstack1;
79
80 /* This obstack will be used for partial_symbol objects. It can
81 probably actually be the same as the symbol_obstack above, but I'd
82 like to keep them seperate for now. If I want to later, I'll
83 replace one with the other. */
84
85 struct obstack obstack2;
86
87 struct obstack *psymbol_obstack = &obstack2;
88
89 /* The object file that the main symbol table was loaded from (e.g. the
90 argument to the "symbol-file" or "file" command). */
91
92 struct objfile *symfile_objfile = 0;
93
94 /* Structures with which to manage partial symbol allocation. */
95
96 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
97
98 /* Flag for whether user will be reloading symbols multiple times.
99 Defaults to ON for VxWorks, otherwise OFF. */
100
101 #ifdef SYMBOL_RELOADING_DEFAULT
102 int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
103 #else
104 int symbol_reloading = 0;
105 #endif
106
107 /* Structure to manage complaints about symbol file contents. */
108
109 struct complaint complaint_root[1] = {
110 {(char *)0, 0, complaint_root},
111 };
112
113 /* Some actual complaints. */
114
115 struct complaint oldsyms_complaint = {
116 "Replacing old symbols for `%s'", 0, 0 };
117
118 struct complaint empty_symtab_complaint = {
119 "Empty symbol table found for `%s'", 0, 0 };
120
121 \f
122 /* In the following sort, we always make sure that
123 register debug symbol declarations always come before regular
124 debug symbol declarations (as might happen when parameters are
125 then put into registers by the compiler). */
126
127 static int
128 compare_symbols (s1, s2)
129 struct symbol **s1, **s2;
130 {
131 register int namediff;
132
133 /* Compare the initial characters. */
134 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
135 if (namediff != 0) return namediff;
136
137 /* If they match, compare the rest of the names. */
138 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
139 if (namediff != 0) return namediff;
140
141 /* For symbols of the same name, registers should come first. */
142 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
143 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
144 }
145
146 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
147
148 void
149 sort_block_syms (b)
150 register struct block *b;
151 {
152 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
153 sizeof (struct symbol *), compare_symbols);
154 }
155
156 /* Call sort_symtab_syms to sort alphabetically
157 the symbols of each block of one symtab. */
158
159 void
160 sort_symtab_syms (s)
161 register struct symtab *s;
162 {
163 register struct blockvector *bv;
164 int nbl;
165 int i;
166 register struct block *b;
167
168 if (s == 0)
169 return;
170 bv = BLOCKVECTOR (s);
171 nbl = BLOCKVECTOR_NBLOCKS (bv);
172 for (i = 0; i < nbl; i++)
173 {
174 b = BLOCKVECTOR_BLOCK (bv, i);
175 if (BLOCK_SHOULD_SORT (b))
176 sort_block_syms (b);
177 }
178 }
179
180 void
181 sort_all_symtab_syms ()
182 {
183 register struct symtab *s;
184
185 for (s = symtab_list; s; s = s->next)
186 {
187 sort_symtab_syms (s);
188 }
189 }
190
191 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
192 (and add a null character at the end in the copy).
193 Returns the address of the copy. */
194
195 char *
196 obsavestring (ptr, size)
197 char *ptr;
198 int size;
199 {
200 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
201 /* Open-coded bcopy--saves function call time.
202 These strings are usually short. */
203 {
204 register char *p1 = ptr;
205 register char *p2 = p;
206 char *end = ptr + size;
207 while (p1 != end)
208 *p2++ = *p1++;
209 }
210 p[size] = 0;
211 return p;
212 }
213
214 /* Concatenate strings S1, S2 and S3; return the new string.
215 Space is found in the symbol_obstack. */
216
217 char *
218 obconcat (s1, s2, s3)
219 char *s1, *s2, *s3;
220 {
221 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
222 register char *val = (char *) obstack_alloc (symbol_obstack, len);
223 strcpy (val, s1);
224 strcat (val, s2);
225 strcat (val, s3);
226 return val;
227 }
228 \f
229 /* Accumulate the misc functions in bunches of 127.
230 At the end, copy them all into one newly allocated structure. */
231
232 #define MISC_BUNCH_SIZE 127
233
234 struct misc_bunch
235 {
236 struct misc_bunch *next;
237 struct misc_function contents[MISC_BUNCH_SIZE];
238 };
239
240 /* Bunch currently being filled up.
241 The next field points to chain of filled bunches. */
242
243 static struct misc_bunch *misc_bunch;
244
245 /* Number of slots filled in current bunch. */
246
247 static int misc_bunch_index;
248
249 /* Total number of misc functions recorded so far. */
250
251 static int misc_count;
252
253 void
254 init_misc_bunches ()
255 {
256 misc_count = 0;
257 misc_bunch = 0;
258 misc_bunch_index = MISC_BUNCH_SIZE;
259 }
260
261 void
262 prim_record_misc_function (name, address, misc_type)
263 char *name;
264 CORE_ADDR address;
265 enum misc_function_type misc_type;
266 {
267 register struct misc_bunch *new;
268
269 if (misc_bunch_index == MISC_BUNCH_SIZE)
270 {
271 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
272 misc_bunch_index = 0;
273 new->next = misc_bunch;
274 misc_bunch = new;
275 }
276 misc_bunch->contents[misc_bunch_index].name = name;
277 misc_bunch->contents[misc_bunch_index].address = address;
278 misc_bunch->contents[misc_bunch_index].type = misc_type;
279 misc_bunch->contents[misc_bunch_index].misc_info = 0;
280 misc_bunch_index++;
281 misc_count++;
282 }
283
284 static int
285 compare_misc_functions (fn1, fn2)
286 struct misc_function *fn1, *fn2;
287 {
288 /* Return a signed result based on unsigned comparisons
289 so that we sort into unsigned numeric order. */
290 if (fn1->address < fn2->address)
291 return -1;
292 if (fn1->address > fn2->address)
293 return 1;
294 return 0;
295 }
296
297 /* ARGSUSED */
298 void
299 discard_misc_bunches (foo)
300 int foo;
301 {
302 register struct misc_bunch *next;
303
304 while (misc_bunch)
305 {
306 next = misc_bunch->next;
307 free (misc_bunch);
308 misc_bunch = next;
309 }
310 }
311
312 /* After adding things to the vector, sort or re-sort it into address order. */
313 void
314 sort_misc_function_vector ()
315 {
316 qsort (misc_function_vector, misc_function_count,
317 sizeof (struct misc_function),
318 compare_misc_functions);
319 }
320
321 /* Compact duplicate entries out of the misc function vector by walking
322 through the vector and compacting out entries with duplicate addresses
323 and matching names.
324
325 When files contain multiple sources of symbol information, it is
326 possible for the misc function vector to contain many duplicate entries.
327 As an example, SVR4 systems use ELF formatted object files, which
328 usually contain at least two different types of symbol tables (a
329 standard ELF one and a smaller dynamic linking table), as well as
330 DWARF debugging information for files compiled with -g.
331
332 Without compacting, the misc function vector for gdb itself contains
333 over a 1000 duplicates, about a third of the total table size. Aside
334 from the potential trap of not noticing that two successive entries
335 identify the same location, this duplication impacts the time required
336 to linearly scan the table, which is done in a number of places. So
337 just do one linear scan here and toss out the duplicates.
338
339 Note that the strings themselves are allocated on the symbol_obstack,
340 so we can't easily reclaim their memory. They will get automatically
341 freed when the symbol table is freed.
342
343 Also note we only go up to the next to last entry within the loop
344 and then copy the last entry explicitly after the loop terminates.
345
346 Since the different sources of information for each symbol may
347 have different levels of "completeness", we may have duplicates
348 that have one entry with type "mf_unknown" and the other with a
349 known type. So if the one we are leaving alone has type mf_unknown,
350 overwrite its type with the type from the one we are compacting out. */
351
352 static void
353 compact_misc_function_vector ()
354 {
355 struct misc_function *copyfrom;
356 struct misc_function *copyto;
357
358 if (misc_function_count == 0)
359 return;
360
361 copyfrom = copyto = misc_function_vector;
362 while (copyfrom < misc_function_vector + misc_function_count - 1)
363 {
364 if (copyfrom -> address == (copyfrom + 1) -> address
365 && (strcmp (copyfrom -> name, (copyfrom + 1) -> name) == 0))
366 {
367 if ((copyfrom + 1) -> type == mf_unknown)
368 {
369 (copyfrom + 1) -> type = copyfrom -> type;
370 }
371 copyfrom++;
372 }
373 else
374 {
375 *copyto++ = *copyfrom++;
376 }
377 }
378 *copyto++ = *copyfrom++;
379 misc_function_count = copyto - misc_function_vector;
380 misc_function_vector = (struct misc_function *)
381 xrealloc (misc_function_vector,
382 misc_function_count * sizeof (struct misc_function));
383
384 }
385
386 /* INCLINK nonzero means bunches are from an incrementally-linked file.
387 Add them to the existing bunches.
388 Otherwise INCLINK is zero, and we start from scratch. */
389 void
390 condense_misc_bunches (inclink)
391 int inclink;
392 {
393 register int i, j;
394 register struct misc_bunch *bunch;
395
396 if (inclink)
397 {
398 misc_function_vector
399 = (struct misc_function *)
400 xrealloc (misc_function_vector, (misc_count + misc_function_count)
401 * sizeof (struct misc_function));
402 j = misc_function_count;
403 }
404 else
405 {
406 misc_function_vector
407 = (struct misc_function *)
408 xmalloc (misc_count * sizeof (struct misc_function));
409 j = 0;
410 }
411
412 bunch = misc_bunch;
413 while (bunch)
414 {
415 for (i = 0; i < misc_bunch_index; i++, j++)
416 {
417 misc_function_vector[j] = bunch->contents[i];
418 #ifdef NAMES_HAVE_UNDERSCORE
419 if (misc_function_vector[j].name[0] == '_')
420 misc_function_vector[j].name++;
421 #endif
422 #ifdef SOME_NAMES_HAVE_DOT
423 if (misc_function_vector[j].name[0] == '.')
424 misc_function_vector[j].name++;
425 #endif
426
427 }
428 bunch = bunch->next;
429 misc_bunch_index = MISC_BUNCH_SIZE;
430 }
431
432 if (misc_function_count + misc_count != j) /* DEBUG */
433 printf_filtered ("Function counts are off! %d + %d != %d\n",
434 misc_function_count, misc_count, j);
435
436 misc_function_count = j;
437
438 /* Sort the misc functions by address. */
439
440 sort_misc_function_vector ();
441
442 /* Compact out any duplicates. */
443
444 compact_misc_function_vector ();
445 }
446
447
448 /* Get the symbol table that corresponds to a partial_symtab.
449 This is fast after the first time you do it. In fact, there
450 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
451 case inline. */
452
453 struct symtab *
454 psymtab_to_symtab (pst)
455 register struct partial_symtab *pst;
456 {
457 /* If it's been looked up before, return it. */
458 if (pst->symtab)
459 return pst->symtab;
460
461 /* If it has not yet been read in, read it. */
462 if (!pst->readin)
463 {
464 (*pst->read_symtab) (pst);
465 }
466
467 return pst->symtab;
468 }
469
470 /* Process a symbol file, as either the main file or as a dynamically
471 loaded file.
472
473 NAME is the file name (which will be tilde-expanded and made
474 absolute herein) (but we don't free or modify NAME itself).
475 FROM_TTY says how verbose to be. MAINLINE specifies whether this
476 is the main symbol file, or whether it's an extra symbol file such
477 as dynamically loaded code. If !mainline, ADDR is the address
478 where the text segment was loaded. If VERBO, the caller has printed
479 a verbose message about the symbol reading (and complaints can be
480 more terse about it). */
481
482 void
483 syms_from_objfile (objfile, addr, mainline, verbo)
484 struct objfile *objfile;
485 CORE_ADDR addr;
486 int mainline;
487 int verbo;
488 {
489 asection *text_sect;
490 struct sym_fns *sf;
491 bfd *sym_bfd = objfile->obfd;
492
493 /* There is a distinction between having no symbol table
494 (we refuse to read the file, leaving the old set of symbols around)
495 and having no debugging symbols in your symbol table (we read
496 the file and end up with a mostly empty symbol table). */
497
498 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
499 return;
500
501 /* Save startup file's range of PC addresses to help blockframe.c
502 decide where the bottom of the stack is. */
503 if (bfd_get_file_flags (sym_bfd) & EXEC_P)
504 {
505 /* Executable file -- record its entry point so we'll recognize
506 the startup file because it contains the entry point. */
507 entry_point = bfd_get_start_address (sym_bfd);
508 }
509 else
510 {
511 /* Examination of non-executable.o files. Short-circuit this stuff. */
512 /* ~0 will not be in any file, we hope. */
513 entry_point = ~0;
514 /* set the startup file to be an empty range. */
515 startup_file_start = 0;
516 startup_file_end = 0;
517 }
518
519 sf = symfile_init (objfile);
520
521 if (mainline)
522 {
523 /* Since no error yet, throw away the old symbol table. */
524
525 if (symfile_objfile)
526 free_objfile (symfile_objfile);
527 symfile_objfile = 0;
528
529 (*sf->sym_new_init) ();
530
531 /* For mainline, caller didn't know the specified address of the
532 text section. We fix that here. */
533 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
534 addr = bfd_section_vma (sym_bfd, text_sect);
535 }
536
537 /* Allow complaints to appear for this new file, and record how
538 verbose to be. */
539
540 clear_complaints(1, verbo);
541
542 (*sf->sym_read) (sf, addr, mainline);
543
544 /* Don't allow char * to have a typename (else would get caddr_t.) */
545 /* Ditto void *. FIXME should do this for all the builtin types. */
546
547 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
548 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
549
550 if (mainline)
551 {
552 /* OK, make it the "real" symbol file. */
553 symfile_objfile = objfile;
554 symfile_fns = sf;
555 }
556
557 /* If we have wiped out any old symbol tables, clean up. */
558 clear_symtab_users_once ();
559
560 /* We're done reading the symbol file; finish off complaints. */
561 clear_complaints(0, verbo);
562 }
563
564
565 /* Process a symbol file, as either the main file or as a dynamically
566 loaded file.
567
568 NAME is the file name (which will be tilde-expanded and made
569 absolute herein) (but we don't free or modify NAME itself).
570 FROM_TTY says how verbose to be. MAINLINE specifies whether this
571 is the main symbol file, or whether it's an extra symbol file such
572 as dynamically loaded code. If !mainline, ADDR is the address
573 where the text segment was loaded. */
574
575 void
576 symbol_file_add (name, from_tty, addr, mainline)
577 char *name;
578 int from_tty;
579 CORE_ADDR addr;
580 int mainline;
581 {
582 struct objfile *objfile;
583 bfd *sym_bfd;
584
585 objfile = symfile_open (name);
586 sym_bfd = objfile->obfd;
587
588 /* There is a distinction between having no symbol table
589 (we refuse to read the file, leaving the old set of symbols around)
590 and having no debugging symbols in your symbol table (we read
591 the file and end up with a mostly empty symbol table, but with lots
592 of stuff in the misc function vector). */
593
594 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
595 {
596 error ("%s has no symbol-table", name);
597 }
598
599 if ((symtab_list || partial_symtab_list)
600 && mainline
601 && from_tty
602 && !query ("Load new symbol table from \"%s\"? ", name))
603 error ("Not confirmed.");
604
605 if (from_tty || info_verbose)
606 {
607 printf_filtered ("Reading symbols from %s...", name);
608 wrap_here ("");
609 fflush (stdout);
610 }
611
612 syms_from_objfile (objfile, addr, mainline, from_tty);
613
614 if (from_tty || info_verbose)
615 {
616 printf_filtered ("done.\n");
617 fflush (stdout);
618 }
619 }
620
621 /* This is the symbol-file command. Read the file, analyze its symbols,
622 and add a struct symtab to symtab_list. */
623
624 void
625 symbol_file_command (name, from_tty)
626 char *name;
627 int from_tty;
628 {
629
630 dont_repeat ();
631
632 if (name == 0)
633 {
634 if (symfile_objfile) {
635 if ((symtab_list || partial_symtab_list)
636 && from_tty
637 && !query ("Discard symbol table from `%s'? ",
638 symfile_objfile->name))
639 error ("Not confirmed.");
640 free_objfile (symfile_objfile);
641 }
642 symfile_objfile = 0;
643 /* FIXME, this does not account for the main file and subsequent
644 files (shared libs, dynloads, etc) having different formats.
645 It only calls the cleanup routine for the main file's format. */
646 if (symfile_fns) {
647 (*symfile_fns->sym_new_init) ();
648 free (symfile_fns);
649 symfile_fns = 0;
650 }
651 return;
652 }
653
654 /* Getting new symbols may change our opinion about what is
655 frameless. */
656 reinit_frame_cache ();
657
658 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
659 }
660
661 /* Open NAME and hand it off to BFD for preliminary analysis. Result
662 is newly malloc'd struct objfile *, which includes a newly malloc'd`
663 copy of NAME (tilde-expanded and made absolute).
664 In case of trouble, error() is called. */
665
666 static struct objfile *
667 symfile_open (name)
668 char *name;
669 {
670 bfd *sym_bfd;
671 int desc;
672 char *absolute_name;
673 struct objfile *objfile;
674
675 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
676
677 /* Look down path for it, allocate 2nd new malloc'd copy. */
678 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
679 if (desc < 0) {
680 make_cleanup (free, name);
681 perror_with_name (name);
682 }
683 free (name); /* Free 1st new malloc'd copy */
684 name = absolute_name; /* Keep 2nd malloc'd copy in objfile and bfd */
685
686 sym_bfd = bfd_fdopenr (name, NULL, desc);
687 if (!sym_bfd)
688 {
689 close (desc);
690 make_cleanup (free, name);
691 error ("Could not open `%s' to read symbols: %s",
692 name, bfd_errmsg (bfd_error));
693 }
694
695 if (!bfd_check_format (sym_bfd, bfd_object)) {
696 bfd_close (sym_bfd); /* This also closes desc */
697 make_cleanup (free, name);
698 error ("\"%s\": can't read symbols: %s.",
699 name, bfd_errmsg (bfd_error));
700 }
701
702 objfile = allocate_objfile (sym_bfd, name);
703 return objfile;
704 }
705
706
707 /* Allocate a new objfile struct, fill it in as best we can, and return it.
708 FIXME-soon! Eventually, the objfile will contain the obstack in which
709 the symtabs and psymtabs are contained, so they can all be blown away
710 cheaply and easily. */
711
712 struct objfile *
713 allocate_objfile (abfd, filename)
714 bfd *abfd;
715 char *filename;
716 {
717 struct objfile *objfile;
718
719 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
720 bzero (objfile, sizeof (*objfile));
721
722 objfile->obfd = abfd;
723 objfile->name = filename;
724
725 objfile->symtabs = 0; /* Don't have any yet */
726 objfile->psymtabs = 0; /* Don't have any yet */
727
728 objfile->mtime = bfd_get_mtime (abfd);
729
730 /* Chain it to the list. */
731 objfile->next = object_files;
732 object_files = objfile;
733
734 return objfile;
735 }
736
737
738 /* Destroy an objfile and all the symtabs and psymtabs under it. */
739
740 void
741 free_objfile (objfile)
742 struct objfile *objfile;
743 {
744 struct objfile *ofp;
745
746 if (objfile->name)
747 free (objfile->name);
748 if (objfile->obfd)
749 bfd_close (objfile->obfd);
750
751 /* Remove it from the chain of all objfiles. */
752 if (object_files == objfile)
753 object_files = objfile->next;
754 else for (ofp = object_files; ofp; ofp = ofp->next) {
755 if (ofp->next == objfile)
756 ofp->next = objfile->next;
757 }
758
759 /* FIXME! This should only free those associated with the objfile
760 being passed to us. THIS IS A KLUDGE TO BOOTSTRAP US. */
761 free_all_psymtabs ();
762 free_all_symtabs ();
763
764 free (objfile);
765 }
766
767
768 /* Link a new symtab_fns into the global symtab_fns list.
769 Called by various _initialize routines. */
770
771 void
772 add_symtab_fns (sf)
773 struct sym_fns *sf;
774 {
775 sf->next = symtab_fns;
776 symtab_fns = sf;
777 }
778
779
780 /* Initialize to read symbols from the symbol file sym_bfd. It either
781 returns or calls error(). The result is a malloc'd struct sym_fns
782 that contains cached information about the symbol file. */
783
784 static struct sym_fns *
785 symfile_init (objfile)
786 struct objfile *objfile;
787 {
788 struct sym_fns *sf, *sf2;
789 bfd *sym_bfd = objfile->obfd;
790
791 for (sf = symtab_fns; sf != NULL; sf = sf->next)
792 {
793 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
794 {
795 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
796 /* FIXME, who frees this? */
797 *sf2 = *sf;
798 sf2->objfile = objfile;
799 sf2->sym_bfd = sym_bfd;
800 sf2->sym_private = 0; /* Not alloc'd yet */
801 (*sf2->sym_init) (sf2);
802 return sf2;
803 }
804 }
805 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
806 bfd_get_target (sym_bfd));
807 return 0; /* Appease lint. */
808 }
809 \f
810 /* This function runs the load command of our current target. */
811
812 void
813 load_command (arg, from_tty)
814 char *arg;
815 int from_tty;
816 {
817 target_load (arg, from_tty);
818 }
819
820 /* This function allows the addition of incrementally linked object files.
821 It does not modify any state in the target, only in the debugger. */
822
823 /* ARGSUSED */
824 void
825 add_symbol_file_command (arg_string, from_tty)
826 char *arg_string;
827 int from_tty;
828 {
829 char *name;
830 CORE_ADDR text_addr;
831
832 /* Getting new symbols may change our opinion about what is
833 frameless. */
834 reinit_frame_cache ();
835
836 if (arg_string == 0)
837 error ("add-symbol-file takes a file name and an address");
838
839 arg_string = tilde_expand (arg_string);
840 make_cleanup (free, arg_string);
841
842 for( ; *arg_string == ' '; arg_string++ );
843 name = arg_string;
844 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
845 *arg_string++ = (char) 0;
846
847 if (name[0] == 0)
848 error ("add-symbol-file takes a file name and an address");
849
850 text_addr = parse_and_eval_address (arg_string);
851
852 dont_repeat ();
853
854 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
855 name, local_hex_string (text_addr)))
856 error ("Not confirmed.");
857
858 symbol_file_add (name, 0, text_addr, 0);
859 }
860 \f
861 /* Re-read symbols if a symbol-file has changed. */
862 void
863 reread_symbols ()
864 {
865 struct objfile *objfile;
866 long new_modtime;
867 int reread_one = 0;
868
869 /* With the addition of shared libraries, this should be modified,
870 the load time should be saved in the partial symbol tables, since
871 different tables may come from different source files. FIXME.
872 This routine should then walk down each partial symbol table
873 and see if the symbol table that it originates from has been changed
874 */
875
876 for (objfile = object_files; objfile; objfile = objfile->next) {
877 if (objfile->obfd) {
878 new_modtime = bfd_get_mtime (objfile->obfd);
879 if (new_modtime != objfile->mtime) {
880 printf_filtered ("`%s' has changed; re-reading symbols.\n",
881 objfile->name);
882 /* FIXME, this should use a different command...that would only
883 affect this objfile's symbols. */
884 symbol_file_command (objfile->name, 0);
885 objfile->mtime = new_modtime;
886 reread_one = 1;
887 }
888 }
889 }
890
891 if (reread_one)
892 breakpoint_re_set ();
893 }
894
895 /* This function is really horrible, but to avoid it, there would need
896 to be more filling in of forward references. */
897 void
898 fill_in_vptr_fieldno (type)
899 struct type *type;
900 {
901 if (TYPE_VPTR_FIELDNO (type) < 0)
902 {
903 int i;
904 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
905 {
906 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
907 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
908 {
909 TYPE_VPTR_FIELDNO (type)
910 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
911 TYPE_VPTR_BASETYPE (type)
912 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
913 break;
914 }
915 }
916 }
917 }
918 \f
919 /* Functions to handle complaints during symbol reading. */
920
921 /* How many complaints about a particular thing should be printed before
922 we stop whining about it? Default is no whining at all, since so many
923 systems have ill-constructed symbol files. */
924
925 static unsigned stop_whining = 0;
926
927 /* Should each complaint be self explanatory, or should we assume that
928 a series of complaints is being produced?
929 case 0: self explanatory message.
930 case 1: First message of a series that must start off with explanation.
931 case 2: Subsequent message, when user already knows we are reading
932 symbols and we can just state our piece. */
933
934 static int complaint_series = 0;
935
936 /* Print a complaint about the input symbols, and link the complaint block
937 into a chain for later handling. */
938
939 void
940 complain (complaint, val)
941 struct complaint *complaint;
942 char *val;
943 {
944 complaint->counter++;
945 if (complaint->next == 0) {
946 complaint->next = complaint_root->next;
947 complaint_root->next = complaint;
948 }
949 if (complaint->counter > stop_whining)
950 return;
951 wrap_here ("");
952
953 switch (complaint_series + (info_verbose << 1)) {
954
955 /* Isolated messages, must be self-explanatory. */
956 case 0:
957 puts_filtered ("During symbol reading, ");
958 wrap_here("");
959 printf_filtered (complaint->message, val);
960 puts_filtered (".\n");
961 break;
962
963 /* First of a series, without `set verbose'. */
964 case 1:
965 puts_filtered ("During symbol reading...");
966 printf_filtered (complaint->message, val);
967 puts_filtered ("...");
968 wrap_here("");
969 complaint_series++;
970 break;
971
972 /* Subsequent messages of a series, or messages under `set verbose'.
973 (We'll already have produced a "Reading in symbols for XXX..." message
974 and will clean up at the end with a newline.) */
975 default:
976 printf_filtered (complaint->message, val);
977 puts_filtered ("...");
978 wrap_here("");
979 }
980 }
981
982 /* Clear out all complaint counters that have ever been incremented.
983 If sym_reading is 1, be less verbose about successive complaints,
984 since the messages are appearing all together during a command that
985 reads symbols (rather than scattered around as psymtabs get fleshed
986 out into symtabs at random times). If noisy is 1, we are in a
987 noisy symbol reading command, and our caller will print enough
988 context for the user to figure it out. */
989
990 void
991 clear_complaints (sym_reading, noisy)
992 int sym_reading;
993 int noisy;
994 {
995 struct complaint *p;
996
997 for (p = complaint_root->next; p != complaint_root; p = p->next)
998 p->counter = 0;
999
1000 if (!sym_reading && !noisy && complaint_series > 1) {
1001 /* Terminate previous series, since caller won't. */
1002 puts_filtered ("\n");
1003 }
1004
1005 complaint_series = sym_reading? 1 + noisy: 0;
1006 }
1007 \f
1008 enum language
1009 deduce_language_from_filename (filename)
1010 char *filename;
1011 {
1012 char *c = rindex (filename, '.');
1013
1014 if (!c) ; /* Get default. */
1015 else if(!strcmp(c,".mod"))
1016 return language_m2;
1017 else if(!strcmp(c,".c"))
1018 return language_c;
1019 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
1020 return language_cplus;
1021
1022 return language_unknown; /* default */
1023 }
1024 \f
1025 /* allocate_symtab:
1026
1027 Allocate and partly initialize a new symbol table. Return a pointer
1028 to it. error() if no space.
1029
1030 Caller must set these fields:
1031 LINETABLE(symtab)
1032 symtab->blockvector
1033 symtab->dirname
1034 symtab->free_code
1035 symtab->free_ptr
1036 initialize any EXTRA_SYMTAB_INFO
1037 possibly free_named_symtabs (symtab->filename);
1038 symtab->next = symtab_list;
1039 symtab_list = symtab;
1040 */
1041
1042 struct symtab *
1043 allocate_symtab(name, objfile)
1044 char *name;
1045 struct objfile *objfile;
1046 {
1047 register struct symtab *symtab;
1048
1049 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
1050 bzero (symtab, sizeof (*symtab));
1051 symtab->filename = name;
1052 symtab->fullname = NULL;
1053 symtab->nlines = 0;
1054 symtab->line_charpos = 0;
1055 symtab->version = 0;
1056 symtab->language = deduce_language_from_filename (name);
1057
1058 /* Hook it to the objfile it comes from */
1059 symtab->objfile = objfile;
1060 symtab->objfile_chain = objfile->symtabs;
1061 objfile->symtabs = symtab;
1062
1063 #ifdef INIT_EXTRA_SYMTAB_INFO
1064 INIT_EXTRA_SYMTAB_INFO(symtab);
1065 #endif
1066
1067 return symtab;
1068 }
1069 \f
1070 /* clear_symtab_users_once:
1071
1072 This function is run after symbol reading, or from a cleanup.
1073 If an old symbol table was obsoleted, the old symbol table
1074 has been blown away, but the other GDB data structures that may
1075 reference it have not yet been cleared or re-directed. (The old
1076 symtab was zapped, and the cleanup queued, in free_named_symtab()
1077 below.)
1078
1079 This function can be queued N times as a cleanup, or called
1080 directly; it will do all the work the first time, and then will be a
1081 no-op until the next time it is queued. This works by bumping a
1082 counter at queueing time. Much later when the cleanup is run, or at
1083 the end of symbol processing (in case the cleanup is discarded), if
1084 the queued count is greater than the "done-count", we do the work
1085 and set the done-count to the queued count. If the queued count is
1086 less than or equal to the done-count, we just ignore the call. This
1087 is needed because reading a single .o file will often replace many
1088 symtabs (one per .h file, for example), and we don't want to reset
1089 the breakpoints N times in the user's face.
1090
1091 The reason we both queue a cleanup, and call it directly after symbol
1092 reading, is because the cleanup protects us in case of errors, but is
1093 discarded if symbol reading is successful. */
1094
1095 static int clear_symtab_users_queued;
1096 static int clear_symtab_users_done;
1097
1098 static void
1099 clear_symtab_users_once ()
1100 {
1101 /* Enforce once-per-`do_cleanups'-semantics */
1102 if (clear_symtab_users_queued <= clear_symtab_users_done)
1103 return;
1104 clear_symtab_users_done = clear_symtab_users_queued;
1105
1106 printf ("Resetting debugger state after updating old symbol tables\n");
1107
1108 /* Someday, we should do better than this, by only blowing away
1109 the things that really need to be blown. */
1110 clear_value_history ();
1111 clear_displays ();
1112 clear_internalvars ();
1113 breakpoint_re_set ();
1114 set_default_breakpoint (0, 0, 0, 0);
1115 current_source_symtab = 0;
1116 }
1117
1118 /* Delete the specified psymtab, and any others that reference it. */
1119
1120 static void
1121 cashier_psymtab (pst)
1122 struct partial_symtab *pst;
1123 {
1124 struct partial_symtab *ps, *pprev;
1125 int i;
1126
1127 /* Find its previous psymtab in the chain */
1128 for (ps = partial_symtab_list; ps; ps = ps->next) {
1129 if (ps == pst)
1130 break;
1131 pprev = ps;
1132 }
1133
1134 if (ps) {
1135 /* Unhook it from the chain. */
1136 if (ps == partial_symtab_list)
1137 partial_symtab_list = ps->next;
1138 else
1139 pprev->next = ps->next;
1140
1141 /* FIXME, we can't conveniently deallocate the entries in the
1142 partial_symbol lists (global_psymbols/static_psymbols) that
1143 this psymtab points to. These just take up space until all
1144 the psymtabs are reclaimed. Ditto the dependencies list and
1145 filename, which are all in the psymbol_obstack. */
1146
1147 /* We need to cashier any psymtab that has this one as a dependency... */
1148 again:
1149 for (ps = partial_symtab_list; ps; ps = ps->next) {
1150 for (i = 0; i < ps->number_of_dependencies; i++) {
1151 if (ps->dependencies[i] == pst) {
1152 cashier_psymtab (ps);
1153 goto again; /* Must restart, chain has been munged. */
1154 }
1155 }
1156 }
1157 }
1158 }
1159
1160 /* If a symtab or psymtab for filename NAME is found, free it along
1161 with any dependent breakpoints, displays, etc.
1162 Used when loading new versions of object modules with the "add-file"
1163 command. This is only called on the top-level symtab or psymtab's name;
1164 it is not called for subsidiary files such as .h files.
1165
1166 Return value is 1 if we blew away the environment, 0 if not.
1167
1168 FIXME. I think this is not the best way to do this. We should
1169 work on being gentler to the environment while still cleaning up
1170 all stray pointers into the freed symtab. */
1171
1172 int
1173 free_named_symtabs (name)
1174 char *name;
1175 {
1176 register struct symtab *s;
1177 register struct symtab *prev;
1178 register struct partial_symtab *ps;
1179 struct blockvector *bv;
1180 int blewit = 0;
1181
1182 /* We only wack things if the symbol-reload switch is set. */
1183 if (!symbol_reloading)
1184 return 0;
1185
1186 /* Some symbol formats have trouble providing file names... */
1187 if (name == 0 || *name == '\0')
1188 return 0;
1189
1190 /* Look for a psymtab with the specified name. */
1191
1192 again2:
1193 for (ps = partial_symtab_list; ps; ps = ps->next) {
1194 if (!strcmp (name, ps->filename)) {
1195 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1196 goto again2; /* Must restart, chain has been munged */
1197 }
1198 }
1199
1200 /* Look for a symtab with the specified name. */
1201
1202 for (s = symtab_list; s; s = s->next)
1203 {
1204 if (!strcmp (name, s->filename))
1205 break;
1206 prev = s;
1207 }
1208
1209 if (s)
1210 {
1211 if (s == symtab_list)
1212 symtab_list = s->next;
1213 else
1214 prev->next = s->next;
1215
1216 /* For now, queue a delete for all breakpoints, displays, etc., whether
1217 or not they depend on the symtab being freed. This should be
1218 changed so that only those data structures affected are deleted. */
1219
1220 /* But don't delete anything if the symtab is empty.
1221 This test is necessary due to a bug in "dbxread.c" that
1222 causes empty symtabs to be created for N_SO symbols that
1223 contain the pathname of the object file. (This problem
1224 has been fixed in GDB 3.9x). */
1225
1226 bv = BLOCKVECTOR (s);
1227 if (BLOCKVECTOR_NBLOCKS (bv) > 2
1228 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1229 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1230 {
1231 complain (&oldsyms_complaint, name);
1232
1233 clear_symtab_users_queued++;
1234 make_cleanup (clear_symtab_users_once, 0);
1235 blewit = 1;
1236 } else {
1237 complain (&empty_symtab_complaint, name);
1238 }
1239
1240 free_symtab (s);
1241 }
1242 else
1243 {
1244 /* It is still possible that some breakpoints will be affected
1245 even though no symtab was found, since the file might have
1246 been compiled without debugging, and hence not be associated
1247 with a symtab. In order to handle this correctly, we would need
1248 to keep a list of text address ranges for undebuggable files.
1249 For now, we do nothing, since this is a fairly obscure case. */
1250 ;
1251 }
1252
1253 /* FIXME, what about the misc function vector? */
1254 return blewit;
1255 }
1256 \f
1257 /* Allocate and partially fill a partial symtab. It will be
1258 completely filled at the end of the symbol list.
1259
1260 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1261 is the address relative to which its symbols are (incremental) or 0
1262 (normal). */
1263
1264
1265 struct partial_symtab *
1266 start_psymtab_common (objfile, addr,
1267 filename, textlow, global_syms, static_syms)
1268 struct objfile *objfile;
1269 CORE_ADDR addr;
1270 char *filename;
1271 CORE_ADDR textlow;
1272 struct partial_symbol *global_syms;
1273 struct partial_symbol *static_syms;
1274 {
1275 int filename_length = strlen (filename) + 1;
1276 struct partial_symtab *result =
1277 (struct partial_symtab *) obstack_alloc (psymbol_obstack,
1278 sizeof (struct partial_symtab));
1279
1280 result->addr = addr;
1281
1282 result->filename = (char *) obstack_alloc (psymbol_obstack, filename_length);
1283 memcpy (result->filename, filename, filename_length);
1284
1285 result->textlow = textlow;
1286
1287 result->readin = 0;
1288 result->symtab = NULL;
1289
1290 result->globals_offset = global_syms - global_psymbols.list;
1291 result->statics_offset = static_syms - static_psymbols.list;
1292
1293 result->n_global_syms = 0;
1294 result->n_static_syms = 0;
1295
1296 /* Chain it to the list owned by the current object file. */
1297 result->objfile = objfile;
1298 result->objfile_chain = objfile->psymtabs;
1299 objfile->psymtabs = result;
1300
1301 return result;
1302 }
1303
1304 /*
1305 * Free all partial_symtab storage.
1306 */
1307 static void
1308 free_all_psymtabs()
1309 {
1310 obstack_free (psymbol_obstack, 0);
1311 obstack_init (psymbol_obstack);
1312 partial_symtab_list = (struct partial_symtab *) 0;
1313 }
1314
1315 /* Free all the symtabs that are currently installed,
1316 and all storage associated with them.
1317 Leaves us in a consistent state with no symtabs installed. */
1318
1319 static void
1320 free_all_symtabs ()
1321 {
1322 register struct symtab *s, *snext;
1323
1324 /* All values will be invalid because their types will be! */
1325
1326 clear_value_history ();
1327 clear_displays ();
1328 clear_internalvars ();
1329 #if defined (CLEAR_SOLIB)
1330 CLEAR_SOLIB ();
1331 #endif
1332 set_default_breakpoint (0, 0, 0, 0);
1333
1334 current_source_symtab = 0;
1335
1336 for (s = symtab_list; s; s = snext)
1337 {
1338 snext = s->next;
1339 free_symtab (s);
1340 }
1341 symtab_list = 0;
1342 obstack_free (symbol_obstack, 0);
1343 obstack_init (symbol_obstack);
1344
1345 if (misc_function_vector)
1346 free (misc_function_vector);
1347 misc_function_count = 0;
1348 misc_function_vector = 0;
1349 clear_pc_function_cache();
1350 }
1351 \f
1352 void
1353 _initialize_symfile ()
1354 {
1355
1356 add_com ("symbol-file", class_files, symbol_file_command,
1357 "Load symbol table from executable file FILE.\n\
1358 The `file' command can also load symbol tables, as well as setting the file\n\
1359 to execute.");
1360
1361 add_com ("add-symbol-file", class_files, add_symbol_file_command,
1362 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1363 The second argument provides the starting address of the file's text.");
1364
1365 add_com ("load", class_files, load_command,
1366 "Dynamically load FILE into the running program, and record its symbols\n\
1367 for access from GDB.");
1368
1369 add_show_from_set
1370 (add_set_cmd ("complaints", class_support, var_zinteger,
1371 (char *)&stop_whining,
1372 "Set max number of complaints about incorrect symbols.",
1373 &setlist),
1374 &showlist);
1375
1376 add_show_from_set
1377 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1378 (char *)&symbol_reloading,
1379 "Set dynamic symbol table reloading multiple times in one run.",
1380 &setlist),
1381 &showlist);
1382
1383 obstack_init (symbol_obstack);
1384 obstack_init (psymbol_obstack);
1385 }
This page took 0.055691 seconds and 3 git commands to generate.