d2f82552e983985342f9e0a6000af656e450f9e2
[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 GDB 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 1, or (at your option)
10 any later version.
11
12 GDB 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 GDB; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "param.h"
25 #include "gdbcore.h"
26 #include "frame.h"
27 #include "target.h"
28 #include "value.h"
29 #include "symfile.h"
30 #include "gdbcmd.h"
31 #include "breakpoint.h"
32
33 #include <obstack.h>
34 #include <assert.h>
35
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <sys/stat.h>
40
41 extern int info_verbose;
42
43 extern int close ();
44 extern void qsort ();
45 extern char *getenv ();
46
47 /* Functions this file defines */
48 static bfd *symfile_open();
49 static struct sym_fns *symfile_init();
50
51 /* List of all available sym_fns. */
52
53 struct sym_fns *symtab_fns = NULL;
54
55 /* Saves the sym_fns of the current symbol table, so we can call
56 the right sym_discard function when we free it. */
57
58 static struct sym_fns *symfile_fns;
59
60 /* Allocate an obstack to hold objects that should be freed
61 when we load a new symbol table.
62 This includes the symbols made by dbxread
63 and the types that are not permanent. */
64
65 struct obstack obstack1;
66
67 struct obstack *symbol_obstack = &obstack1;
68
69 /* This obstack will be used for partial_symbol objects. It can
70 probably actually be the same as the symbol_obstack above, but I'd
71 like to keep them seperate for now. If I want to later, I'll
72 replace one with the other. */
73
74 struct obstack obstack2;
75
76 struct obstack *psymbol_obstack = &obstack2;
77
78 /* File name symbols were loaded from. */
79
80 char *symfile = 0;
81
82 /* The modification date of the file when they were loaded. */
83
84 int symfile_mtime = 0;
85
86 /* Structures with which to manage partial symbol allocation. */
87
88 struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
89
90 /* Structure to manage complaints about symbol file contents. */
91
92 struct complaint complaint_root[1] = {
93 {(char *)0, 0, complaint_root},
94 };
95
96 \f
97 /* In the following sort, we always make sure that
98 register debug symbol declarations always come before regular
99 debug symbol declarations (as might happen when parameters are
100 then put into registers by the compiler). */
101
102 static int
103 compare_symbols (s1, s2)
104 struct symbol **s1, **s2;
105 {
106 register int namediff;
107
108 /* Compare the initial characters. */
109 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
110 if (namediff != 0) return namediff;
111
112 /* If they match, compare the rest of the names. */
113 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
114 if (namediff != 0) return namediff;
115
116 /* For symbols of the same name, registers should come first. */
117 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
118 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
119 }
120
121 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
122
123 void
124 sort_block_syms (b)
125 register struct block *b;
126 {
127 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
128 sizeof (struct symbol *), compare_symbols);
129 }
130
131 /* Call sort_symtab_syms to sort alphabetically
132 the symbols of each block of one symtab. */
133
134 void
135 sort_symtab_syms (s)
136 register struct symtab *s;
137 {
138 register struct blockvector *bv = BLOCKVECTOR (s);
139 int nbl = BLOCKVECTOR_NBLOCKS (bv);
140 int i;
141 register struct block *b;
142
143 for (i = 0; i < nbl; i++)
144 {
145 b = BLOCKVECTOR_BLOCK (bv, i);
146 if (BLOCK_SHOULD_SORT (b))
147 sort_block_syms (b);
148 }
149 }
150
151 void
152 sort_all_symtab_syms ()
153 {
154 register struct symtab *s;
155
156 for (s = symtab_list; s; s = s->next)
157 {
158 sort_symtab_syms (s);
159 }
160 }
161
162 /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
163 (and add a null character at the end in the copy).
164 Returns the address of the copy. */
165
166 char *
167 obsavestring (ptr, size)
168 char *ptr;
169 int size;
170 {
171 register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
172 /* Open-coded bcopy--saves function call time.
173 These strings are usually short. */
174 {
175 register char *p1 = ptr;
176 register char *p2 = p;
177 char *end = ptr + size;
178 while (p1 != end)
179 *p2++ = *p1++;
180 }
181 p[size] = 0;
182 return p;
183 }
184
185 /* Concatenate strings S1, S2 and S3; return the new string.
186 Space is found in the symbol_obstack. */
187
188 char *
189 obconcat (s1, s2, s3)
190 char *s1, *s2, *s3;
191 {
192 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
193 register char *val = (char *) obstack_alloc (symbol_obstack, len);
194 strcpy (val, s1);
195 strcat (val, s2);
196 strcat (val, s3);
197 return val;
198 }
199 \f
200 /* Accumulate the misc functions in bunches of 127.
201 At the end, copy them all into one newly allocated structure. */
202
203 #define MISC_BUNCH_SIZE 127
204
205 struct misc_bunch
206 {
207 struct misc_bunch *next;
208 struct misc_function contents[MISC_BUNCH_SIZE];
209 };
210
211 /* Bunch currently being filled up.
212 The next field points to chain of filled bunches. */
213
214 static struct misc_bunch *misc_bunch;
215
216 /* Number of slots filled in current bunch. */
217
218 static int misc_bunch_index;
219
220 /* Total number of misc functions recorded so far. */
221
222 static int misc_count;
223
224 void
225 init_misc_bunches ()
226 {
227 misc_count = 0;
228 misc_bunch = 0;
229 misc_bunch_index = MISC_BUNCH_SIZE;
230 }
231
232 void
233 prim_record_misc_function (name, address, misc_type)
234 char *name;
235 CORE_ADDR address;
236 enum misc_function_type misc_type;
237 {
238 register struct misc_bunch *new;
239
240 if (misc_bunch_index == MISC_BUNCH_SIZE)
241 {
242 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
243 misc_bunch_index = 0;
244 new->next = misc_bunch;
245 misc_bunch = new;
246 }
247 misc_bunch->contents[misc_bunch_index].name = name;
248 misc_bunch->contents[misc_bunch_index].address = address;
249 misc_bunch->contents[misc_bunch_index].type = misc_type;
250 misc_bunch->contents[misc_bunch_index].misc_info = 0;
251 misc_bunch_index++;
252 misc_count++;
253 }
254
255 static int
256 compare_misc_functions (fn1, fn2)
257 struct misc_function *fn1, *fn2;
258 {
259 /* Return a signed result based on unsigned comparisons
260 so that we sort into unsigned numeric order. */
261 if (fn1->address < fn2->address)
262 return -1;
263 if (fn1->address > fn2->address)
264 return 1;
265 return 0;
266 }
267
268 /* ARGSUSED */
269 void
270 discard_misc_bunches (foo)
271 int foo;
272 {
273 register struct misc_bunch *next;
274
275 while (misc_bunch)
276 {
277 next = misc_bunch->next;
278 free (misc_bunch);
279 misc_bunch = next;
280 }
281 }
282
283 /* INCLINK nonzero means bunches are from an incrementally-linked file.
284 Add them to the existing bunches.
285 Otherwise INCLINK is zero, and we start from scratch. */
286 void
287 condense_misc_bunches (inclink)
288 int inclink;
289 {
290 register int i, j;
291 register struct misc_bunch *bunch;
292
293 if (inclink)
294 {
295 misc_function_vector
296 = (struct misc_function *)
297 xrealloc (misc_function_vector, (misc_count + misc_function_count)
298 * sizeof (struct misc_function));
299 j = misc_function_count;
300 }
301 else
302 {
303 misc_function_vector
304 = (struct misc_function *)
305 xmalloc (misc_count * sizeof (struct misc_function));
306 j = 0;
307 }
308
309 bunch = misc_bunch;
310 while (bunch)
311 {
312 for (i = 0; i < misc_bunch_index; i++, j++)
313 {
314 misc_function_vector[j] = bunch->contents[i];
315 #ifdef NAMES_HAVE_UNDERSCORE
316 if (misc_function_vector[j].name[0] == '_')
317 misc_function_vector[j].name++;
318 #endif
319 }
320 bunch = bunch->next;
321 misc_bunch_index = MISC_BUNCH_SIZE;
322 }
323
324 if (misc_function_count + misc_count != j) /* DEBUG */
325 printf_filtered ("Function counts are off! %d + %d != %d\n",
326 misc_function_count, misc_count, j);
327
328 misc_function_count = j;
329
330 /* Sort the misc functions by address. */
331
332 qsort (misc_function_vector, misc_function_count,
333 sizeof (struct misc_function),
334 compare_misc_functions);
335 }
336
337
338 /* Get the symbol table that corresponds to a partial_symtab.
339 This is fast after the first time you do it. In fact, there
340 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
341 case inline. */
342
343 struct symtab *
344 psymtab_to_symtab (pst)
345 register struct partial_symtab *pst;
346 {
347 register struct symtab *result;
348
349 /* If it's been looked up before, return it. */
350 if (pst->symtab)
351 return pst->symtab;
352
353 /* If it has not yet been read in, read it. */
354 if (!pst->readin)
355 {
356 (*pst->read_symtab) (pst);
357 }
358
359 /* Search through list for correct name. */
360 for (result = symtab_list; result; result = result->next)
361 if (!strcmp (result->filename, pst->filename))
362 {
363 pst->symtab = result; /* Remember where it was. */
364 return result;
365 }
366
367 return 0;
368 }
369
370 /* Process a symbol file, as either the main file or as a dynamically
371 loaded file.
372
373 NAME is the file name (which will be tilde-expanded and made absolute
374 herein). FROM_TTY says how verbose to be. MAINLINE specifies whether
375 this is the main symbol file, or whether it's an extra symbol file
376 such as dynamically loaded code. If !mainline, ADDR is the address
377 where the text segment was loaded. */
378
379 void
380 symbol_file_add (name, from_tty, addr, mainline)
381 char *name;
382 int from_tty;
383 CORE_ADDR addr;
384 int mainline;
385 {
386 bfd *sym_bfd;
387 asection *text_sect;
388 struct sym_fns *sf;
389 char *realname;
390
391 sym_bfd = symfile_open (name);
392
393 entry_point = bfd_get_start_address (sym_bfd);
394
395 if (mainline)
396 symfile_mtime = bfd_get_mtime (sym_bfd);
397
398 /* There is a distinction between having no symbol table
399 (we refuse to read the file, leaving the old set of symbols around)
400 and having no debugging symbols in your symbol table (we read
401 the file and end up with a mostly empty symbol table). */
402
403 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
404 {
405 error ("%s has no symbol-table", name);
406 }
407
408 if ((symtab_list || partial_symtab_list)
409 && mainline
410 && from_tty
411 && !query ("Load new symbol table from \"%s\"? ", name))
412 error ("Not confirmed.");
413
414 if (from_tty)
415 {
416 printf_filtered ("Reading symbol data from %s...", name);
417 wrap_here ("");
418 fflush (stdout);
419 }
420
421 sf = symfile_init (sym_bfd);
422 realname = bfd_get_filename (sym_bfd);
423 realname = savestring (realname, strlen (realname));
424 /* FIXME, this probably creates a storage leak... */
425
426 if (mainline)
427 {
428 /* Since no error yet, throw away the old symbol table. */
429
430 if (symfile)
431 free (symfile);
432 symfile = 0;
433 free_all_symtabs ();
434 free_all_psymtabs ();
435
436 (*sf->sym_new_init) ();
437
438 /* For mainline, caller didn't know the specified address of the
439 text section. We fix that here. */
440 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
441 addr = bfd_section_vma (sym_bfd, text_sect);
442 }
443
444 clear_complaints(); /* Allow complaints to appear for this new file. */
445
446 (*sf->sym_read) (sf, addr, mainline);
447
448 /* Don't allow char * to have a typename (else would get caddr_t.) */
449 /* Ditto void *. FIXME should do this for all the builtin types. */
450
451 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
452 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
453
454 if (mainline)
455 {
456 /* OK, make it the "real" symbol file. */
457 symfile = realname;
458 symfile_fns = sf;
459 }
460
461 /* If we have wiped out any old symbol tables, clean up. */
462 clear_symtab_users_once ();
463
464 if (from_tty)
465 {
466 printf_filtered ("done.\n");
467 fflush (stdout);
468 }
469 }
470
471 /* This is the symbol-file command. Read the file, analyze its symbols,
472 and add a struct symtab to symtab_list. */
473
474 void
475 symbol_file_command (name, from_tty)
476 char *name;
477 int from_tty;
478 {
479
480 dont_repeat ();
481
482 if (name == 0)
483 {
484 if ((symtab_list || partial_symtab_list)
485 && from_tty
486 && !query ("Discard symbol table from `%s'? ", symfile))
487 error ("Not confirmed.");
488 if (symfile)
489 free (symfile);
490 symfile = 0;
491 free_all_symtabs ();
492 free_all_psymtabs ();
493 /* FIXME, this does not account for the main file and subsequent
494 files (shared libs, dynloads, etc) having different formats.
495 It only calls the cleanup routine for the main file's format. */
496 if (symfile_fns) {
497 (*symfile_fns->sym_new_init) ();
498 free (symfile_fns);
499 symfile_fns = 0;
500 }
501 return;
502 }
503
504 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
505 }
506
507 /* Open NAME and hand it off to BFD for preliminary analysis. Result
508 is a BFD *, which includes a new copy of NAME dynamically allocated
509 (which will be freed by the cleanup chain). In case of trouble,
510 error() is called. */
511
512 static bfd *
513 symfile_open (name)
514 char *name;
515 {
516 bfd *sym_bfd;
517 int desc;
518 char *absolute_name;
519
520 name = tilde_expand (name);
521 make_cleanup (free, name);
522
523 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
524 if (desc < 0)
525 perror_with_name (name);
526 else
527 {
528 make_cleanup (free, absolute_name);
529 name = absolute_name;
530 }
531
532 sym_bfd = bfd_fdopenr (name, NULL, desc);
533 if (!sym_bfd)
534 {
535 close (desc);
536 error ("Could not open `%s' to read symbols: %s",
537 name, bfd_errmsg (bfd_error));
538 }
539 make_cleanup (bfd_close, sym_bfd);
540
541 if (!bfd_check_format (sym_bfd, bfd_object))
542 error ("\"%s\": can't read symbols: %s.",
543 name, bfd_errmsg (bfd_error));
544
545 return sym_bfd;
546 }
547
548 /* Link a new symtab_fns into the global symtab_fns list.
549 Called by various _initialize routines. */
550
551 void
552 add_symtab_fns (sf)
553 struct sym_fns *sf;
554 {
555 sf->next = symtab_fns;
556 symtab_fns = sf;
557 }
558
559
560 /* Initialize to read symbols from the symbol file sym_bfd. It either
561 returns or calls error(). The result is a malloc'd struct sym_fns
562 that contains cached information about the symbol file. */
563
564 static struct sym_fns *
565 symfile_init (sym_bfd)
566 bfd *sym_bfd;
567 {
568 struct sym_fns *sf, *sf2;
569
570 for (sf = symtab_fns; sf != NULL; sf = sf->next)
571 {
572 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
573 {
574 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
575 /* FIXME, who frees this? */
576 *sf2 = *sf;
577 sf2->sym_bfd = sym_bfd;
578 sf2->sym_private = 0; /* Not alloc'd yet */
579 (*sf2->sym_init) (sf2);
580 return sf2;
581 }
582 }
583 error ("I'm sorry, Dave, I can't do that. Symbol format unknown.");
584 }
585 \f
586 /* This function runs the load command of our current target. */
587
588 void
589 load_command (arg, from_tty)
590 char *arg;
591 int from_tty;
592 {
593 target_load (arg, from_tty);
594 }
595
596 /* This function runs the add_syms command of our current target. */
597
598 void
599 add_syms_command (args, from_tty)
600 char *args;
601 int from_tty;
602 {
603 target_add_syms (args, from_tty);
604 }
605
606 /* This function allows the addition of incrementally linked object files. */
607
608 void
609 add_syms_addr_command (arg_string, from_tty)
610 char* arg_string;
611 int from_tty;
612 {
613 char *name;
614 CORE_ADDR text_addr;
615
616 if (arg_string == 0)
617 error ("add-syms takes a file name and an address");
618
619 arg_string = tilde_expand (arg_string);
620 make_cleanup (free, arg_string);
621
622 for( ; *arg_string == ' '; arg_string++ );
623 name = arg_string;
624 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
625 *arg_string++ = (char) 0;
626
627 if (name[0] == 0)
628 error ("add-syms takes a file name and an address");
629
630 text_addr = parse_and_eval_address (arg_string);
631
632 dont_repeat ();
633
634 if (!query ("add symbol table from file \"%s\" at text_addr = 0x%x\n",
635 name, text_addr))
636 error ("Not confirmed.");
637
638 symbol_file_add (name, 0, text_addr, 0);
639 }
640 \f
641 /* Re-read symbols if the symbol-file has changed. */
642 void
643 reread_symbols ()
644 {
645 struct stat symstat;
646
647 /* With the addition of shared libraries, this should be modified,
648 the load time should be saved in the partial symbol tables, since
649 different tables may come from different source files. FIXME.
650 This routine should then walk down each partial symbol table
651 and see if the symbol table that it originates from has been changed
652 */
653
654 if (stat (symfile, &symstat) < 0)
655 /* Can't read symbol-file. Assume it is up to date. */
656 return;
657
658 if (symstat.st_mtime > symfile_mtime)
659 {
660 printf_filtered ("Symbol file has changed; re-reading symbols.\n");
661 symbol_file_command (symfile, 0);
662 breakpoint_re_set ();
663 }
664 }
665
666
667 /* This function is really horrible, but to avoid it, there would need
668 to be more filling in of forward references. */
669 int
670 fill_in_vptr_fieldno (type)
671 struct type *type;
672 {
673 check_stub_type (type);
674 if (TYPE_VPTR_FIELDNO (type) < 0)
675 TYPE_VPTR_FIELDNO (type) =
676 fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
677 return TYPE_VPTR_FIELDNO (type);
678 }
679 \f
680 /* Functions to handle complaints during symbol reading. */
681
682 /* How many complaints about a particular thing should be printed before
683 we stop whining about it? */
684
685 static unsigned stop_whining = 1;
686
687 /* Print a complaint about the input symbols, and link the complaint block
688 into a chain for later handling. Result is 1 if the complaint was
689 printed, 0 if it was suppressed. */
690
691 int
692 complain (complaint, val)
693 struct complaint *complaint;
694 char *val;
695 {
696 complaint->counter++;
697 if (complaint->next == 0) {
698 complaint->next = complaint_root->next;
699 complaint_root->next = complaint;
700 }
701 if (complaint->counter > stop_whining)
702 return 0;
703 wrap_here ("");
704 if (!info_verbose) {
705 puts_filtered ("During symbol reading...");
706 }
707 printf_filtered (complaint->message, val);
708 puts_filtered ("...");
709 wrap_here("");
710 if (!info_verbose)
711 puts_filtered ("\n");
712 return 1;
713 }
714
715 /* Clear out all complaint counters that have ever been incremented. */
716
717 void
718 clear_complaints ()
719 {
720 struct complaint *p;
721
722 for (p = complaint_root->next; p != complaint_root; p = p->next)
723 p->counter = 0;
724 }
725 \f
726 void
727 _initialize_symfile ()
728 {
729
730 add_com ("symbol-file", class_files, symbol_file_command,
731 "Load symbol table from executable file FILE.\n\
732 The `file' command can also load symbol tables, as well as setting the file\n\
733 to execute.");
734
735 add_com ("add-syms", class_files, add_syms_command,
736 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
737 The second argument provides the starting address of the file's text.");
738
739 add_com ("load", class_files, load_command,
740 "Dynamically load FILE into the running program, and record its symbols\n\
741 for access from GDB.");
742
743 add_show_from_set
744 (add_set_cmd ("complaints", class_support, var_uinteger,
745 (char *)&stop_whining,
746 "Set max number of complaints about incorrect symbols.",
747 &setlist),
748 &showlist);
749
750 obstack_init (symbol_obstack);
751 obstack_init (psymbol_obstack);
752 }
This page took 0.044187 seconds and 4 git commands to generate.