* sparc-pinsn.c (compare_opcodes): Make it prefer v6, v7,
[deliverable/binutils-gdb.git] / gdb / symfile.c
CommitLineData
bd5635a1
RP
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
5This file is part of GDB.
6
61a7292f 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
61a7292f
SG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
61a7292f 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
61a7292f
SG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1
RP
20
21#include <stdio.h>
22#include "defs.h"
23#include "symtab.h"
bd5635a1
RP
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
d47d5315
JG
40CORE_ADDR entry_point; /* Where execution starts in symfile */
41
bd5635a1
RP
42extern int info_verbose;
43
bd5635a1
RP
44extern void qsort ();
45extern char *getenv ();
d8ce1326 46extern char *rindex ();
bd5635a1 47
d47d5315
JG
48extern CORE_ADDR startup_file_start; /* From blockframe.c */
49extern CORE_ADDR startup_file_end; /* From blockframe.c */
50
bd5635a1 51/* Functions this file defines */
7d9884b9
JG
52static struct objfile *symfile_open ();
53static struct sym_fns *symfile_init ();
54static void clear_symtab_users_once ();
55
56static void free_all_psymtabs ();
57static void free_all_symtabs ();
bd5635a1
RP
58
59/* List of all available sym_fns. */
60
61struct sym_fns *symtab_fns = NULL;
62
63/* Saves the sym_fns of the current symbol table, so we can call
61a7292f
SG
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. */
bd5635a1
RP
68
69static 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
76struct obstack obstack1;
77
78struct 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
85struct obstack obstack2;
86
87struct obstack *psymbol_obstack = &obstack2;
88
7d9884b9
JG
89/* The object file that the main symbol table was loaded from (e.g. the
90 argument to the "symbol-file" or "file" command). */
bd5635a1 91
7d9884b9 92struct objfile *symfile_objfile = 0;
bd5635a1
RP
93
94/* Structures with which to manage partial symbol allocation. */
95
96struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
97
61a7292f
SG
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
102int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
103#else
104int symbol_reloading = 0;
105#endif
106
bd5635a1
RP
107/* Structure to manage complaints about symbol file contents. */
108
109struct complaint complaint_root[1] = {
110 {(char *)0, 0, complaint_root},
111};
112
9d199712
JG
113/* Some actual complaints. */
114
115struct complaint oldsyms_complaint = {
116 "Replacing old symbols for `%s'", 0, 0 };
117
118struct complaint empty_symtab_complaint = {
119 "Empty symbol table found for `%s'", 0, 0 };
120
bd5635a1
RP
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
127static int
128compare_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
148void
149sort_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
159void
160sort_symtab_syms (s)
161 register struct symtab *s;
162{
c9bd6710
JG
163 register struct blockvector *bv;
164 int nbl;
bd5635a1
RP
165 int i;
166 register struct block *b;
167
c9bd6710
JG
168 if (s == 0)
169 return;
170 bv = BLOCKVECTOR (s);
171 nbl = BLOCKVECTOR_NBLOCKS (bv);
bd5635a1
RP
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
180void
181sort_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
195char *
196obsavestring (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
217char *
218obconcat (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
234struct 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
243static struct misc_bunch *misc_bunch;
244
245/* Number of slots filled in current bunch. */
246
247static int misc_bunch_index;
248
249/* Total number of misc functions recorded so far. */
250
251static int misc_count;
252
253void
254init_misc_bunches ()
255{
256 misc_count = 0;
257 misc_bunch = 0;
258 misc_bunch_index = MISC_BUNCH_SIZE;
259}
260
261void
262prim_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
284static int
285compare_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 */
298void
299discard_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
7d9884b9
JG
312/* After adding things to the vector, sort or re-sort it into address order. */
313void
314sort_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,
7a6093e8 350 overwrite its type with the type from the one we are compacting out. */
7d9884b9
JG
351
352static void
353compact_misc_function_vector ()
354{
355 struct misc_function *copyfrom;
356 struct misc_function *copyto;
357
7a6093e8
JG
358 if (misc_function_count == 0)
359 return;
360
7d9884b9
JG
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
bd5635a1
RP
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. */
389void
390condense_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
d47d5315
JG
422#ifdef SOME_NAMES_HAVE_DOT
423 if (misc_function_vector[j].name[0] == '.')
424 misc_function_vector[j].name++;
425#endif
426
bd5635a1
RP
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
7d9884b9
JG
440 sort_misc_function_vector ();
441
442 /* Compact out any duplicates. */
443
444 compact_misc_function_vector ();
bd5635a1
RP
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
453struct symtab *
454psymtab_to_symtab (pst)
455 register struct partial_symtab *pst;
456{
bd5635a1
RP
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
61a7292f 467 return pst->symtab;
bd5635a1
RP
468}
469
470/* Process a symbol file, as either the main file or as a dynamically
471 loaded file.
472
b3fdaf3d
JK
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
4369a140
JG
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). */
bd5635a1
RP
481
482void
4369a140 483syms_from_objfile (objfile, addr, mainline, verbo)
7d9884b9 484 struct objfile *objfile;
bd5635a1
RP
485 CORE_ADDR addr;
486 int mainline;
4369a140 487 int verbo;
bd5635a1 488{
bd5635a1
RP
489 asection *text_sect;
490 struct sym_fns *sf;
7d9884b9 491 bfd *sym_bfd = objfile->obfd;
bd5635a1 492
bd5635a1
RP
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))
d47d5315
JG
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)
bd5635a1 504 {
d47d5315
JG
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);
bd5635a1 508 }
d47d5315 509 else
bd5635a1 510 {
d47d5315
JG
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;
bd5635a1
RP
517 }
518
7d9884b9 519 sf = symfile_init (objfile);
bd5635a1
RP
520
521 if (mainline)
522 {
523 /* Since no error yet, throw away the old symbol table. */
524
7d9884b9
JG
525 if (symfile_objfile)
526 free_objfile (symfile_objfile);
527 symfile_objfile = 0;
bd5635a1
RP
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
4369a140
JG
537 /* Allow complaints to appear for this new file, and record how
538 verbose to be. */
539
540 clear_complaints(1, verbo);
bd5635a1
RP
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. */
7d9884b9 553 symfile_objfile = objfile;
bd5635a1
RP
554 symfile_fns = sf;
555 }
556
0ef6f019
JG
557 /* If we have wiped out any old symbol tables, clean up. */
558 clear_symtab_users_once ();
4369a140
JG
559
560 /* We're done reading the symbol file; finish off complaints. */
561 clear_complaints(0, verbo);
d47d5315
JG
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
575void
576symbol_file_add (name, from_tty, addr, mainline)
577 char *name;
578 int from_tty;
579 CORE_ADDR addr;
580 int mainline;
581{
7d9884b9 582 struct objfile *objfile;
d47d5315
JG
583 bfd *sym_bfd;
584
7d9884b9
JG
585 objfile = symfile_open (name);
586 sym_bfd = objfile->obfd;
d47d5315
JG
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
7d9884b9
JG
591 the file and end up with a mostly empty symbol table, but with lots
592 of stuff in the misc function vector). */
d47d5315
JG
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)
606 {
607 printf_filtered ("Reading symbols from %s...", name);
608 wrap_here ("");
609 fflush (stdout);
610 }
611
4369a140 612 syms_from_objfile (objfile, addr, mainline, from_tty);
0ef6f019 613
bd5635a1
RP
614 if (from_tty)
615 {
0ef6f019 616 printf_filtered ("done.\n");
bd5635a1
RP
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
624void
625symbol_file_command (name, from_tty)
626 char *name;
627 int from_tty;
628{
629
630 dont_repeat ();
631
632 if (name == 0)
633 {
7d9884b9
JG
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;
bd5635a1
RP
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. */
0ef6f019
JG
646 if (symfile_fns) {
647 (*symfile_fns->sym_new_init) ();
648 free (symfile_fns);
649 symfile_fns = 0;
650 }
bd5635a1
RP
651 return;
652 }
653
2403f49b
JK
654 /* Getting new symbols may change our opinion about what is
655 frameless. */
656 reinit_frame_cache ();
657
bd5635a1
RP
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
7d9884b9
JG
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. */
bd5635a1 665
7d9884b9 666static struct objfile *
bd5635a1
RP
667symfile_open (name)
668 char *name;
669{
670 bfd *sym_bfd;
671 int desc;
672 char *absolute_name;
7d9884b9 673 struct objfile *objfile;
bd5635a1 674
7d9884b9 675 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 676
7d9884b9 677 /* Look down path for it, allocate 2nd new malloc'd copy. */
bd5635a1 678 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
7d9884b9
JG
679 if (desc < 0) {
680 make_cleanup (free, name);
bd5635a1 681 perror_with_name (name);
7d9884b9
JG
682 }
683 free (name); /* Free 1st new malloc'd copy */
684 name = absolute_name; /* Keep 2nd malloc'd copy in objfile and bfd */
bd5635a1
RP
685
686 sym_bfd = bfd_fdopenr (name, NULL, desc);
687 if (!sym_bfd)
688 {
689 close (desc);
7d9884b9 690 make_cleanup (free, name);
bd5635a1
RP
691 error ("Could not open `%s' to read symbols: %s",
692 name, bfd_errmsg (bfd_error));
693 }
bd5635a1 694
7d9884b9
JG
695 if (!bfd_check_format (sym_bfd, bfd_object)) {
696 bfd_close (sym_bfd); /* This also closes desc */
697 make_cleanup (free, name);
bd5635a1
RP
698 error ("\"%s\": can't read symbols: %s.",
699 name, bfd_errmsg (bfd_error));
7d9884b9
JG
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
712struct objfile *
713allocate_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;
bd5635a1 724
7d9884b9
JG
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
740void
741free_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);
bd5635a1
RP
765}
766
7d9884b9 767
bd5635a1
RP
768/* Link a new symtab_fns into the global symtab_fns list.
769 Called by various _initialize routines. */
770
771void
772add_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
784static struct sym_fns *
7d9884b9
JG
785symfile_init (objfile)
786 struct objfile *objfile;
bd5635a1
RP
787{
788 struct sym_fns *sf, *sf2;
7d9884b9 789 bfd *sym_bfd = objfile->obfd;
bd5635a1
RP
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;
7d9884b9 798 sf2->objfile = objfile;
bd5635a1
RP
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 }
c9bd6710
JG
805 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
806 bfd_get_target (sym_bfd));
e1ce8aa5 807 return 0; /* Appease lint. */
bd5635a1
RP
808}
809\f
810/* This function runs the load command of our current target. */
811
812void
813load_command (arg, from_tty)
814 char *arg;
815 int from_tty;
816{
817 target_load (arg, from_tty);
818}
819
61a7292f
SG
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. */
bd5635a1 822
e1ce8aa5 823/* ARGSUSED */
bd5635a1 824void
61a7292f
SG
825add_symbol_file_command (arg_string, from_tty)
826 char *arg_string;
bd5635a1
RP
827 int from_tty;
828{
829 char *name;
830 CORE_ADDR text_addr;
831
61a7292f
SG
832 /* Getting new symbols may change our opinion about what is
833 frameless. */
834 reinit_frame_cache ();
835
bd5635a1 836 if (arg_string == 0)
e74d7b43 837 error ("add-symbol-file takes a file name and an address");
bd5635a1
RP
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)
e74d7b43 848 error ("add-symbol-file takes a file name and an address");
bd5635a1
RP
849
850 text_addr = parse_and_eval_address (arg_string);
851
852 dont_repeat ();
853
d8ce1326
JG
854 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
855 name, local_hex_string (text_addr)))
bd5635a1
RP
856 error ("Not confirmed.");
857
858 symbol_file_add (name, 0, text_addr, 0);
859}
860\f
7d9884b9 861/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
862void
863reread_symbols ()
864{
7d9884b9
JG
865 struct objfile *objfile;
866 long new_modtime;
867 int reread_one = 0;
bd5635a1
RP
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
7d9884b9
JG
876 for (objfile = object_files; objfile; objfile = objfile->next) {
877 if (objfile->obfd) {
878 objfile->obfd->mtime_set = false; /* Force it to reread. */
879 new_modtime = bfd_get_mtime (objfile->obfd);
880 if (new_modtime != objfile->mtime) {
881 printf_filtered ("`%s' has changed; re-reading symbols.\n",
882 objfile->name);
883 /* FIXME, this should use a different command...that would only
884 affect this objfile's symbols. */
885 symbol_file_command (objfile->name, 0);
886 objfile->mtime = new_modtime;
887 reread_one = 1;
888 }
bd5635a1 889 }
7d9884b9
JG
890 }
891
892 if (reread_one)
893 breakpoint_re_set ();
bd5635a1
RP
894}
895
bd5635a1
RP
896/* This function is really horrible, but to avoid it, there would need
897 to be more filling in of forward references. */
7cc43879 898void
bd5635a1
RP
899fill_in_vptr_fieldno (type)
900 struct type *type;
901{
bd5635a1 902 if (TYPE_VPTR_FIELDNO (type) < 0)
7cc43879
JK
903 {
904 int i;
905 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
906 {
907 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
908 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
909 {
910 TYPE_VPTR_FIELDNO (type)
911 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
912 TYPE_VPTR_BASETYPE (type)
913 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
914 break;
915 }
916 }
917 }
bd5635a1
RP
918}
919\f
920/* Functions to handle complaints during symbol reading. */
921
922/* How many complaints about a particular thing should be printed before
61a7292f
SG
923 we stop whining about it? Default is no whining at all, since so many
924 systems have ill-constructed symbol files. */
bd5635a1 925
61a7292f 926static unsigned stop_whining = 0;
bd5635a1 927
4369a140
JG
928/* Should each complaint be self explanatory, or should we assume that
929 a series of complaints is being produced?
930 case 0: self explanatory message.
931 case 1: First message of a series that must start off with explanation.
932 case 2: Subsequent message, when user already knows we are reading
933 symbols and we can just state our piece. */
934
935static int complaint_series = 0;
936
bd5635a1 937/* Print a complaint about the input symbols, and link the complaint block
7d9884b9 938 into a chain for later handling. */
bd5635a1 939
7d9884b9 940void
bd5635a1
RP
941complain (complaint, val)
942 struct complaint *complaint;
943 char *val;
944{
945 complaint->counter++;
946 if (complaint->next == 0) {
947 complaint->next = complaint_root->next;
948 complaint_root->next = complaint;
949 }
950 if (complaint->counter > stop_whining)
7d9884b9 951 return;
bd5635a1 952 wrap_here ("");
4369a140
JG
953
954 switch (complaint_series + (info_verbose << 1)) {
955
956 /* Isolated messages, must be self-explanatory. */
957 case 0:
958 puts_filtered ("During symbol reading, ");
959 wrap_here("");
960 printf_filtered (complaint->message, val);
961 puts_filtered (".\n");
962 break;
963
964 /* First of a series, without `set verbose'. */
965 case 1:
bd5635a1 966 puts_filtered ("During symbol reading...");
4369a140
JG
967 printf_filtered (complaint->message, val);
968 puts_filtered ("...");
969 wrap_here("");
970 complaint_series++;
971 break;
972
973 /* Subsequent messages of a series, or messages under `set verbose'.
974 (We'll already have produced a "Reading in symbols for XXX..." message
975 and will clean up at the end with a newline.) */
976 default:
977 printf_filtered (complaint->message, val);
978 puts_filtered ("...");
979 wrap_here("");
bd5635a1 980 }
bd5635a1
RP
981}
982
4369a140
JG
983/* Clear out all complaint counters that have ever been incremented.
984 If sym_reading is 1, be less verbose about successive complaints,
985 since the messages are appearing all together during a command that
986 reads symbols (rather than scattered around as psymtabs get fleshed
987 out into symtabs at random times). If noisy is 1, we are in a
988 noisy symbol reading command, and our caller will print enough
989 context for the user to figure it out. */
bd5635a1
RP
990
991void
4369a140
JG
992clear_complaints (sym_reading, noisy)
993 int sym_reading;
994 int noisy;
bd5635a1
RP
995{
996 struct complaint *p;
997
998 for (p = complaint_root->next; p != complaint_root; p = p->next)
999 p->counter = 0;
4369a140
JG
1000
1001 if (!sym_reading && !noisy && complaint_series > 1) {
1002 /* Terminate previous series, since caller won't. */
1003 puts_filtered ("\n");
1004 }
1005
1006 complaint_series = sym_reading? 1 + noisy: 0;
bd5635a1
RP
1007}
1008\f
7d9884b9
JG
1009enum language
1010deduce_language_from_filename (filename)
1011 char *filename;
1012{
1013 char *c = rindex (filename, '.');
1014
1015 if (!c) ; /* Get default. */
1016 else if(!strcmp(c,".mod"))
1017 return language_m2;
1018 else if(!strcmp(c,".c"))
1019 return language_c;
1020 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
1021 return language_cplus;
1022
1023 return language_unknown; /* default */
1024}
1025\f
d8ce1326
JG
1026/* allocate_symtab:
1027
1028 Allocate and partly initialize a new symbol table. Return a pointer
1029 to it. error() if no space.
1030
1031 Caller must set these fields:
1032 LINETABLE(symtab)
1033 symtab->blockvector
d8ce1326
JG
1034 symtab->dirname
1035 symtab->free_code
1036 symtab->free_ptr
1037 initialize any EXTRA_SYMTAB_INFO
1038 possibly free_named_symtabs (symtab->filename);
1039 symtab->next = symtab_list;
1040 symtab_list = symtab;
1041 */
1042
1043struct symtab *
7d9884b9 1044allocate_symtab(name, objfile)
d8ce1326 1045 char *name;
7d9884b9 1046 struct objfile *objfile;
d8ce1326
JG
1047{
1048 register struct symtab *symtab;
d8ce1326
JG
1049
1050 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
1051 bzero (symtab, sizeof (*symtab));
1052 symtab->filename = name;
1053 symtab->fullname = NULL;
1054 symtab->nlines = 0;
1055 symtab->line_charpos = 0;
1056 symtab->version = 0;
7d9884b9 1057 symtab->language = deduce_language_from_filename (name);
d8ce1326 1058
7d9884b9
JG
1059 /* Hook it to the objfile it comes from */
1060 symtab->objfile = objfile;
1061 symtab->objfile_chain = objfile->symtabs;
1062 objfile->symtabs = symtab;
1063
1064#ifdef INIT_EXTRA_SYMTAB_INFO
1065 INIT_EXTRA_SYMTAB_INFO(symtab);
1066#endif
d8ce1326
JG
1067
1068 return symtab;
1069}
1070\f
9d199712
JG
1071/* clear_symtab_users_once:
1072
1073 This function is run after symbol reading, or from a cleanup.
1074 If an old symbol table was obsoleted, the old symbol table
1075 has been blown away, but the other GDB data structures that may
1076 reference it have not yet been cleared or re-directed. (The old
1077 symtab was zapped, and the cleanup queued, in free_named_symtab()
1078 below.)
1079
1080 This function can be queued N times as a cleanup, or called
1081 directly; it will do all the work the first time, and then will be a
1082 no-op until the next time it is queued. This works by bumping a
1083 counter at queueing time. Much later when the cleanup is run, or at
1084 the end of symbol processing (in case the cleanup is discarded), if
1085 the queued count is greater than the "done-count", we do the work
1086 and set the done-count to the queued count. If the queued count is
1087 less than or equal to the done-count, we just ignore the call. This
1088 is needed because reading a single .o file will often replace many
1089 symtabs (one per .h file, for example), and we don't want to reset
1090 the breakpoints N times in the user's face.
1091
1092 The reason we both queue a cleanup, and call it directly after symbol
1093 reading, is because the cleanup protects us in case of errors, but is
1094 discarded if symbol reading is successful. */
1095
1096static int clear_symtab_users_queued;
1097static int clear_symtab_users_done;
1098
1099static void
1100clear_symtab_users_once ()
1101{
1102 /* Enforce once-per-`do_cleanups'-semantics */
1103 if (clear_symtab_users_queued <= clear_symtab_users_done)
1104 return;
1105 clear_symtab_users_done = clear_symtab_users_queued;
1106
1107 printf ("Resetting debugger state after updating old symbol tables\n");
1108
1109 /* Someday, we should do better than this, by only blowing away
1110 the things that really need to be blown. */
1111 clear_value_history ();
1112 clear_displays ();
1113 clear_internalvars ();
1114 breakpoint_re_set ();
1115 set_default_breakpoint (0, 0, 0, 0);
1116 current_source_symtab = 0;
1117}
1118
1119/* Delete the specified psymtab, and any others that reference it. */
1120
e1ce8aa5 1121static void
9d199712
JG
1122cashier_psymtab (pst)
1123 struct partial_symtab *pst;
1124{
1125 struct partial_symtab *ps, *pprev;
1126 int i;
1127
1128 /* Find its previous psymtab in the chain */
1129 for (ps = partial_symtab_list; ps; ps = ps->next) {
1130 if (ps == pst)
1131 break;
1132 pprev = ps;
1133 }
1134
1135 if (ps) {
1136 /* Unhook it from the chain. */
1137 if (ps == partial_symtab_list)
1138 partial_symtab_list = ps->next;
1139 else
1140 pprev->next = ps->next;
1141
1142 /* FIXME, we can't conveniently deallocate the entries in the
1143 partial_symbol lists (global_psymbols/static_psymbols) that
1144 this psymtab points to. These just take up space until all
1145 the psymtabs are reclaimed. Ditto the dependencies list and
1146 filename, which are all in the psymbol_obstack. */
1147
1148 /* We need to cashier any psymtab that has this one as a dependency... */
1149again:
1150 for (ps = partial_symtab_list; ps; ps = ps->next) {
1151 for (i = 0; i < ps->number_of_dependencies; i++) {
1152 if (ps->dependencies[i] == pst) {
1153 cashier_psymtab (ps);
1154 goto again; /* Must restart, chain has been munged. */
1155 }
1156 }
1157 }
1158 }
1159}
1160
1161/* If a symtab or psymtab for filename NAME is found, free it along
1162 with any dependent breakpoints, displays, etc.
1163 Used when loading new versions of object modules with the "add-file"
1164 command. This is only called on the top-level symtab or psymtab's name;
1165 it is not called for subsidiary files such as .h files.
1166
1167 Return value is 1 if we blew away the environment, 0 if not.
1168
1169 FIXME. I think this is not the best way to do this. We should
1170 work on being gentler to the environment while still cleaning up
1171 all stray pointers into the freed symtab. */
1172
1173int
1174free_named_symtabs (name)
1175 char *name;
1176{
1177 register struct symtab *s;
1178 register struct symtab *prev;
1179 register struct partial_symtab *ps;
9d199712
JG
1180 struct blockvector *bv;
1181 int blewit = 0;
1182
61a7292f
SG
1183 /* We only wack things if the symbol-reload switch is set. */
1184 if (!symbol_reloading)
1185 return 0;
1186
d11c44f1
JG
1187 /* Some symbol formats have trouble providing file names... */
1188 if (name == 0 || *name == '\0')
1189 return 0;
1190
9d199712
JG
1191 /* Look for a psymtab with the specified name. */
1192
1193again2:
1194 for (ps = partial_symtab_list; ps; ps = ps->next) {
1195 if (!strcmp (name, ps->filename)) {
1196 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1197 goto again2; /* Must restart, chain has been munged */
1198 }
1199 }
1200
1201 /* Look for a symtab with the specified name. */
1202
1203 for (s = symtab_list; s; s = s->next)
1204 {
1205 if (!strcmp (name, s->filename))
1206 break;
1207 prev = s;
1208 }
1209
1210 if (s)
1211 {
1212 if (s == symtab_list)
1213 symtab_list = s->next;
1214 else
1215 prev->next = s->next;
1216
1217 /* For now, queue a delete for all breakpoints, displays, etc., whether
1218 or not they depend on the symtab being freed. This should be
1219 changed so that only those data structures affected are deleted. */
1220
1221 /* But don't delete anything if the symtab is empty.
1222 This test is necessary due to a bug in "dbxread.c" that
1223 causes empty symtabs to be created for N_SO symbols that
1224 contain the pathname of the object file. (This problem
1225 has been fixed in GDB 3.9x). */
1226
c9bd6710
JG
1227 bv = BLOCKVECTOR (s);
1228 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1229 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1230 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1231 {
1232 complain (&oldsyms_complaint, name);
1233
1234 clear_symtab_users_queued++;
1235 make_cleanup (clear_symtab_users_once, 0);
1236 blewit = 1;
1237 } else {
1238 complain (&empty_symtab_complaint, name);
1239 }
1240
1241 free_symtab (s);
1242 }
1243 else
d8ce1326
JG
1244 {
1245 /* It is still possible that some breakpoints will be affected
1246 even though no symtab was found, since the file might have
1247 been compiled without debugging, and hence not be associated
1248 with a symtab. In order to handle this correctly, we would need
1249 to keep a list of text address ranges for undebuggable files.
1250 For now, we do nothing, since this is a fairly obscure case. */
1251 ;
1252 }
9d199712
JG
1253
1254 /* FIXME, what about the misc function vector? */
1255 return blewit;
1256}
1257\f
7d9884b9
JG
1258/*
1259 * Free all partial_symtab storage.
1260 */
1261static void
1262free_all_psymtabs()
1263{
1264 obstack_free (psymbol_obstack, 0);
1265 obstack_init (psymbol_obstack);
1266 partial_symtab_list = (struct partial_symtab *) 0;
1267}
1268
1269/* Free all the symtabs that are currently installed,
1270 and all storage associated with them.
1271 Leaves us in a consistent state with no symtabs installed. */
1272
1273static void
1274free_all_symtabs ()
1275{
1276 register struct symtab *s, *snext;
1277
1278 /* All values will be invalid because their types will be! */
1279
1280 clear_value_history ();
1281 clear_displays ();
1282 clear_internalvars ();
1283#if defined (CLEAR_SOLIB)
1284 CLEAR_SOLIB ();
1285#endif
1286 set_default_breakpoint (0, 0, 0, 0);
1287
1288 current_source_symtab = 0;
1289
1290 for (s = symtab_list; s; s = snext)
1291 {
1292 snext = s->next;
1293 free_symtab (s);
1294 }
1295 symtab_list = 0;
1296 obstack_free (symbol_obstack, 0);
1297 obstack_init (symbol_obstack);
1298
1299 if (misc_function_vector)
1300 free (misc_function_vector);
1301 misc_function_count = 0;
1302 misc_function_vector = 0;
1303 clear_pc_function_cache();
1304}
1305\f
bd5635a1
RP
1306void
1307_initialize_symfile ()
1308{
1309
1310 add_com ("symbol-file", class_files, symbol_file_command,
1311 "Load symbol table from executable file FILE.\n\
1312The `file' command can also load symbol tables, as well as setting the file\n\
1313to execute.");
1314
e74d7b43 1315 add_com ("add-symbol-file", class_files, add_symbol_file_command,
bd5635a1
RP
1316 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1317The second argument provides the starting address of the file's text.");
1318
1319 add_com ("load", class_files, load_command,
1320 "Dynamically load FILE into the running program, and record its symbols\n\
1321for access from GDB.");
1322
1323 add_show_from_set
4369a140 1324 (add_set_cmd ("complaints", class_support, var_zinteger,
bd5635a1
RP
1325 (char *)&stop_whining,
1326 "Set max number of complaints about incorrect symbols.",
1327 &setlist),
1328 &showlist);
1329
61a7292f
SG
1330 add_show_from_set
1331 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1332 (char *)&symbol_reloading,
1333 "Set dynamic symbol table reloading multiple times in one run.",
1334 &setlist),
1335 &showlist);
1336
bd5635a1
RP
1337 obstack_init (symbol_obstack);
1338 obstack_init (psymbol_obstack);
1339}
This page took 0.128357 seconds and 4 git commands to generate.