Make it compile for cross-debugging.
[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,
350 overwrite it's type with the type from the one we are compacting out.
351*/
352
353
354static void
355compact_misc_function_vector ()
356{
357 struct misc_function *copyfrom;
358 struct misc_function *copyto;
359
360 copyfrom = copyto = misc_function_vector;
361 while (copyfrom < misc_function_vector + misc_function_count - 1)
362 {
363 if (copyfrom -> address == (copyfrom + 1) -> address
364 && (strcmp (copyfrom -> name, (copyfrom + 1) -> name) == 0))
365 {
366 if ((copyfrom + 1) -> type == mf_unknown)
367 {
368 (copyfrom + 1) -> type = copyfrom -> type;
369 }
370 copyfrom++;
371 }
372 else
373 {
374 *copyto++ = *copyfrom++;
375 }
376 }
377 *copyto++ = *copyfrom++;
378 misc_function_count = copyto - misc_function_vector;
379 misc_function_vector = (struct misc_function *)
380 xrealloc (misc_function_vector,
381 misc_function_count * sizeof (struct misc_function));
382
383}
384
bd5635a1
RP
385/* INCLINK nonzero means bunches are from an incrementally-linked file.
386 Add them to the existing bunches.
387 Otherwise INCLINK is zero, and we start from scratch. */
388void
389condense_misc_bunches (inclink)
390 int inclink;
391{
392 register int i, j;
393 register struct misc_bunch *bunch;
394
395 if (inclink)
396 {
397 misc_function_vector
398 = (struct misc_function *)
399 xrealloc (misc_function_vector, (misc_count + misc_function_count)
400 * sizeof (struct misc_function));
401 j = misc_function_count;
402 }
403 else
404 {
405 misc_function_vector
406 = (struct misc_function *)
407 xmalloc (misc_count * sizeof (struct misc_function));
408 j = 0;
409 }
410
411 bunch = misc_bunch;
412 while (bunch)
413 {
414 for (i = 0; i < misc_bunch_index; i++, j++)
415 {
416 misc_function_vector[j] = bunch->contents[i];
417#ifdef NAMES_HAVE_UNDERSCORE
418 if (misc_function_vector[j].name[0] == '_')
419 misc_function_vector[j].name++;
420#endif
d47d5315
JG
421#ifdef SOME_NAMES_HAVE_DOT
422 if (misc_function_vector[j].name[0] == '.')
423 misc_function_vector[j].name++;
424#endif
425
bd5635a1
RP
426 }
427 bunch = bunch->next;
428 misc_bunch_index = MISC_BUNCH_SIZE;
429 }
430
431 if (misc_function_count + misc_count != j) /* DEBUG */
432 printf_filtered ("Function counts are off! %d + %d != %d\n",
433 misc_function_count, misc_count, j);
434
435 misc_function_count = j;
436
437 /* Sort the misc functions by address. */
438
7d9884b9
JG
439 sort_misc_function_vector ();
440
441 /* Compact out any duplicates. */
442
443 compact_misc_function_vector ();
bd5635a1
RP
444}
445
446
447/* Get the symbol table that corresponds to a partial_symtab.
448 This is fast after the first time you do it. In fact, there
449 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
450 case inline. */
451
452struct symtab *
453psymtab_to_symtab (pst)
454 register struct partial_symtab *pst;
455{
bd5635a1
RP
456 /* If it's been looked up before, return it. */
457 if (pst->symtab)
458 return pst->symtab;
459
460 /* If it has not yet been read in, read it. */
461 if (!pst->readin)
462 {
463 (*pst->read_symtab) (pst);
464 }
465
61a7292f 466 return pst->symtab;
bd5635a1
RP
467}
468
469/* Process a symbol file, as either the main file or as a dynamically
470 loaded file.
471
b3fdaf3d
JK
472 NAME is the file name (which will be tilde-expanded and made
473 absolute herein) (but we don't free or modify NAME itself).
474 FROM_TTY says how verbose to be. MAINLINE specifies whether this
475 is the main symbol file, or whether it's an extra symbol file such
476 as dynamically loaded code. If !mainline, ADDR is the address
bd5635a1
RP
477 where the text segment was loaded. */
478
479void
7d9884b9
JG
480syms_from_objfile (objfile, addr, mainline)
481 struct objfile *objfile;
bd5635a1
RP
482 CORE_ADDR addr;
483 int mainline;
484{
bd5635a1
RP
485 asection *text_sect;
486 struct sym_fns *sf;
7d9884b9 487 bfd *sym_bfd = objfile->obfd;
bd5635a1 488
bd5635a1
RP
489 /* There is a distinction between having no symbol table
490 (we refuse to read the file, leaving the old set of symbols around)
491 and having no debugging symbols in your symbol table (we read
492 the file and end up with a mostly empty symbol table). */
493
494 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
d47d5315
JG
495 return;
496
497 /* Save startup file's range of PC addresses to help blockframe.c
498 decide where the bottom of the stack is. */
499 if (bfd_get_file_flags (sym_bfd) & EXEC_P)
bd5635a1 500 {
d47d5315
JG
501 /* Executable file -- record its entry point so we'll recognize
502 the startup file because it contains the entry point. */
503 entry_point = bfd_get_start_address (sym_bfd);
bd5635a1 504 }
d47d5315 505 else
bd5635a1 506 {
d47d5315
JG
507 /* Examination of non-executable.o files. Short-circuit this stuff. */
508 /* ~0 will not be in any file, we hope. */
509 entry_point = ~0;
510 /* set the startup file to be an empty range. */
511 startup_file_start = 0;
512 startup_file_end = 0;
bd5635a1
RP
513 }
514
7d9884b9 515 sf = symfile_init (objfile);
bd5635a1
RP
516
517 if (mainline)
518 {
519 /* Since no error yet, throw away the old symbol table. */
520
7d9884b9
JG
521 if (symfile_objfile)
522 free_objfile (symfile_objfile);
523 symfile_objfile = 0;
bd5635a1
RP
524
525 (*sf->sym_new_init) ();
526
527 /* For mainline, caller didn't know the specified address of the
528 text section. We fix that here. */
529 text_sect = bfd_get_section_by_name (sym_bfd, ".text");
530 addr = bfd_section_vma (sym_bfd, text_sect);
531 }
532
533 clear_complaints(); /* Allow complaints to appear for this new file. */
534
535 (*sf->sym_read) (sf, addr, mainline);
536
537 /* Don't allow char * to have a typename (else would get caddr_t.) */
538 /* Ditto void *. FIXME should do this for all the builtin types. */
539
540 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
541 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
542
543 if (mainline)
544 {
545 /* OK, make it the "real" symbol file. */
7d9884b9 546 symfile_objfile = objfile;
bd5635a1
RP
547 symfile_fns = sf;
548 }
549
0ef6f019
JG
550 /* If we have wiped out any old symbol tables, clean up. */
551 clear_symtab_users_once ();
d47d5315
JG
552}
553
554
555/* Process a symbol file, as either the main file or as a dynamically
556 loaded file.
557
558 NAME is the file name (which will be tilde-expanded and made
559 absolute herein) (but we don't free or modify NAME itself).
560 FROM_TTY says how verbose to be. MAINLINE specifies whether this
561 is the main symbol file, or whether it's an extra symbol file such
562 as dynamically loaded code. If !mainline, ADDR is the address
563 where the text segment was loaded. */
564
565void
566symbol_file_add (name, from_tty, addr, mainline)
567 char *name;
568 int from_tty;
569 CORE_ADDR addr;
570 int mainline;
571{
7d9884b9 572 struct objfile *objfile;
d47d5315
JG
573 bfd *sym_bfd;
574
7d9884b9
JG
575 objfile = symfile_open (name);
576 sym_bfd = objfile->obfd;
d47d5315
JG
577
578 /* There is a distinction between having no symbol table
579 (we refuse to read the file, leaving the old set of symbols around)
580 and having no debugging symbols in your symbol table (we read
7d9884b9
JG
581 the file and end up with a mostly empty symbol table, but with lots
582 of stuff in the misc function vector). */
d47d5315
JG
583
584 if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS))
585 {
586 error ("%s has no symbol-table", name);
587 }
588
589 if ((symtab_list || partial_symtab_list)
590 && mainline
591 && from_tty
592 && !query ("Load new symbol table from \"%s\"? ", name))
593 error ("Not confirmed.");
594
595 if (from_tty)
596 {
597 printf_filtered ("Reading symbols from %s...", name);
598 wrap_here ("");
599 fflush (stdout);
600 }
601
7d9884b9 602 syms_from_objfile (objfile, addr, mainline);
0ef6f019 603
bd5635a1
RP
604 if (from_tty)
605 {
0ef6f019 606 printf_filtered ("done.\n");
bd5635a1
RP
607 fflush (stdout);
608 }
609}
610
611/* This is the symbol-file command. Read the file, analyze its symbols,
612 and add a struct symtab to symtab_list. */
613
614void
615symbol_file_command (name, from_tty)
616 char *name;
617 int from_tty;
618{
619
620 dont_repeat ();
621
622 if (name == 0)
623 {
7d9884b9
JG
624 if (symfile_objfile) {
625 if ((symtab_list || partial_symtab_list)
626 && from_tty
627 && !query ("Discard symbol table from `%s'? ",
628 symfile_objfile->name))
629 error ("Not confirmed.");
630 free_objfile (symfile_objfile);
631 }
632 symfile_objfile = 0;
bd5635a1
RP
633 /* FIXME, this does not account for the main file and subsequent
634 files (shared libs, dynloads, etc) having different formats.
635 It only calls the cleanup routine for the main file's format. */
0ef6f019
JG
636 if (symfile_fns) {
637 (*symfile_fns->sym_new_init) ();
638 free (symfile_fns);
639 symfile_fns = 0;
640 }
bd5635a1
RP
641 return;
642 }
643
2403f49b
JK
644 /* Getting new symbols may change our opinion about what is
645 frameless. */
646 reinit_frame_cache ();
647
bd5635a1
RP
648 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1);
649}
650
651/* Open NAME and hand it off to BFD for preliminary analysis. Result
7d9884b9
JG
652 is newly malloc'd struct objfile *, which includes a newly malloc'd`
653 copy of NAME (tilde-expanded and made absolute).
654 In case of trouble, error() is called. */
bd5635a1 655
7d9884b9 656static struct objfile *
bd5635a1
RP
657symfile_open (name)
658 char *name;
659{
660 bfd *sym_bfd;
661 int desc;
662 char *absolute_name;
7d9884b9 663 struct objfile *objfile;
bd5635a1 664
7d9884b9 665 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 666
7d9884b9 667 /* Look down path for it, allocate 2nd new malloc'd copy. */
bd5635a1 668 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
7d9884b9
JG
669 if (desc < 0) {
670 make_cleanup (free, name);
bd5635a1 671 perror_with_name (name);
7d9884b9
JG
672 }
673 free (name); /* Free 1st new malloc'd copy */
674 name = absolute_name; /* Keep 2nd malloc'd copy in objfile and bfd */
bd5635a1
RP
675
676 sym_bfd = bfd_fdopenr (name, NULL, desc);
677 if (!sym_bfd)
678 {
679 close (desc);
7d9884b9 680 make_cleanup (free, name);
bd5635a1
RP
681 error ("Could not open `%s' to read symbols: %s",
682 name, bfd_errmsg (bfd_error));
683 }
bd5635a1 684
7d9884b9
JG
685 if (!bfd_check_format (sym_bfd, bfd_object)) {
686 bfd_close (sym_bfd); /* This also closes desc */
687 make_cleanup (free, name);
bd5635a1
RP
688 error ("\"%s\": can't read symbols: %s.",
689 name, bfd_errmsg (bfd_error));
7d9884b9
JG
690 }
691
692 objfile = allocate_objfile (sym_bfd, name);
693 return objfile;
694}
695
696
697/* Allocate a new objfile struct, fill it in as best we can, and return it.
698 FIXME-soon! Eventually, the objfile will contain the obstack in which
699 the symtabs and psymtabs are contained, so they can all be blown away
700 cheaply and easily. */
701
702struct objfile *
703allocate_objfile (abfd, filename)
704 bfd *abfd;
705 char *filename;
706{
707 struct objfile *objfile;
708
709 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
710 bzero (objfile, sizeof (*objfile));
711
712 objfile->obfd = abfd;
713 objfile->name = filename;
bd5635a1 714
7d9884b9
JG
715 objfile->symtabs = 0; /* Don't have any yet */
716 objfile->psymtabs = 0; /* Don't have any yet */
717
718 objfile->mtime = bfd_get_mtime (abfd);
719
720 /* Chain it to the list. */
721 objfile->next = object_files;
722 object_files = objfile;
723
724 return objfile;
725}
726
727
728/* Destroy an objfile and all the symtabs and psymtabs under it. */
729
730void
731free_objfile (objfile)
732 struct objfile *objfile;
733{
734 struct objfile *ofp;
735
736 if (objfile->name)
737 free (objfile->name);
738 if (objfile->obfd)
739 bfd_close (objfile->obfd);
740
741 /* Remove it from the chain of all objfiles. */
742 if (object_files == objfile)
743 object_files = objfile->next;
744 else for (ofp = object_files; ofp; ofp = ofp->next) {
745 if (ofp->next == objfile)
746 ofp->next = objfile->next;
747 }
748
749 /* FIXME! This should only free those associated with the objfile
750 being passed to us. THIS IS A KLUDGE TO BOOTSTRAP US. */
751 free_all_psymtabs ();
752 free_all_symtabs ();
753
754 free (objfile);
bd5635a1
RP
755}
756
7d9884b9 757
bd5635a1
RP
758/* Link a new symtab_fns into the global symtab_fns list.
759 Called by various _initialize routines. */
760
761void
762add_symtab_fns (sf)
763 struct sym_fns *sf;
764{
765 sf->next = symtab_fns;
766 symtab_fns = sf;
767}
768
769
770/* Initialize to read symbols from the symbol file sym_bfd. It either
771 returns or calls error(). The result is a malloc'd struct sym_fns
772 that contains cached information about the symbol file. */
773
774static struct sym_fns *
7d9884b9
JG
775symfile_init (objfile)
776 struct objfile *objfile;
bd5635a1
RP
777{
778 struct sym_fns *sf, *sf2;
7d9884b9 779 bfd *sym_bfd = objfile->obfd;
bd5635a1
RP
780
781 for (sf = symtab_fns; sf != NULL; sf = sf->next)
782 {
783 if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen))
784 {
785 sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2));
786 /* FIXME, who frees this? */
787 *sf2 = *sf;
7d9884b9 788 sf2->objfile = objfile;
bd5635a1
RP
789 sf2->sym_bfd = sym_bfd;
790 sf2->sym_private = 0; /* Not alloc'd yet */
791 (*sf2->sym_init) (sf2);
792 return sf2;
793 }
794 }
c9bd6710
JG
795 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
796 bfd_get_target (sym_bfd));
e1ce8aa5 797 return 0; /* Appease lint. */
bd5635a1
RP
798}
799\f
800/* This function runs the load command of our current target. */
801
802void
803load_command (arg, from_tty)
804 char *arg;
805 int from_tty;
806{
807 target_load (arg, from_tty);
808}
809
61a7292f
SG
810/* This function allows the addition of incrementally linked object files.
811 It does not modify any state in the target, only in the debugger. */
bd5635a1 812
e1ce8aa5 813/* ARGSUSED */
bd5635a1 814void
61a7292f
SG
815add_symbol_file_command (arg_string, from_tty)
816 char *arg_string;
bd5635a1
RP
817 int from_tty;
818{
819 char *name;
820 CORE_ADDR text_addr;
821
61a7292f
SG
822 /* Getting new symbols may change our opinion about what is
823 frameless. */
824 reinit_frame_cache ();
825
bd5635a1 826 if (arg_string == 0)
e74d7b43 827 error ("add-symbol-file takes a file name and an address");
bd5635a1
RP
828
829 arg_string = tilde_expand (arg_string);
830 make_cleanup (free, arg_string);
831
832 for( ; *arg_string == ' '; arg_string++ );
833 name = arg_string;
834 for( ; *arg_string && *arg_string != ' ' ; arg_string++ );
835 *arg_string++ = (char) 0;
836
837 if (name[0] == 0)
e74d7b43 838 error ("add-symbol-file takes a file name and an address");
bd5635a1
RP
839
840 text_addr = parse_and_eval_address (arg_string);
841
842 dont_repeat ();
843
d8ce1326
JG
844 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
845 name, local_hex_string (text_addr)))
bd5635a1
RP
846 error ("Not confirmed.");
847
848 symbol_file_add (name, 0, text_addr, 0);
849}
850\f
7d9884b9 851/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
852void
853reread_symbols ()
854{
7d9884b9
JG
855 struct objfile *objfile;
856 long new_modtime;
857 int reread_one = 0;
bd5635a1
RP
858
859 /* With the addition of shared libraries, this should be modified,
860 the load time should be saved in the partial symbol tables, since
861 different tables may come from different source files. FIXME.
862 This routine should then walk down each partial symbol table
863 and see if the symbol table that it originates from has been changed
864 */
865
7d9884b9
JG
866 for (objfile = object_files; objfile; objfile = objfile->next) {
867 if (objfile->obfd) {
868 objfile->obfd->mtime_set = false; /* Force it to reread. */
869 new_modtime = bfd_get_mtime (objfile->obfd);
870 if (new_modtime != objfile->mtime) {
871 printf_filtered ("`%s' has changed; re-reading symbols.\n",
872 objfile->name);
873 /* FIXME, this should use a different command...that would only
874 affect this objfile's symbols. */
875 symbol_file_command (objfile->name, 0);
876 objfile->mtime = new_modtime;
877 reread_one = 1;
878 }
bd5635a1 879 }
7d9884b9
JG
880 }
881
882 if (reread_one)
883 breakpoint_re_set ();
bd5635a1
RP
884}
885
bd5635a1
RP
886/* This function is really horrible, but to avoid it, there would need
887 to be more filling in of forward references. */
7cc43879 888void
bd5635a1
RP
889fill_in_vptr_fieldno (type)
890 struct type *type;
891{
bd5635a1 892 if (TYPE_VPTR_FIELDNO (type) < 0)
7cc43879
JK
893 {
894 int i;
895 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
896 {
897 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
898 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
899 {
900 TYPE_VPTR_FIELDNO (type)
901 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
902 TYPE_VPTR_BASETYPE (type)
903 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
904 break;
905 }
906 }
907 }
bd5635a1
RP
908}
909\f
910/* Functions to handle complaints during symbol reading. */
911
912/* How many complaints about a particular thing should be printed before
61a7292f
SG
913 we stop whining about it? Default is no whining at all, since so many
914 systems have ill-constructed symbol files. */
bd5635a1 915
61a7292f 916static unsigned stop_whining = 0;
bd5635a1
RP
917
918/* Print a complaint about the input symbols, and link the complaint block
7d9884b9 919 into a chain for later handling. */
bd5635a1 920
7d9884b9 921void
bd5635a1
RP
922complain (complaint, val)
923 struct complaint *complaint;
924 char *val;
925{
926 complaint->counter++;
927 if (complaint->next == 0) {
928 complaint->next = complaint_root->next;
929 complaint_root->next = complaint;
930 }
931 if (complaint->counter > stop_whining)
7d9884b9 932 return;
bd5635a1
RP
933 wrap_here ("");
934 if (!info_verbose) {
935 puts_filtered ("During symbol reading...");
936 }
937 printf_filtered (complaint->message, val);
938 puts_filtered ("...");
939 wrap_here("");
940 if (!info_verbose)
941 puts_filtered ("\n");
bd5635a1
RP
942}
943
944/* Clear out all complaint counters that have ever been incremented. */
945
946void
947clear_complaints ()
948{
949 struct complaint *p;
950
951 for (p = complaint_root->next; p != complaint_root; p = p->next)
952 p->counter = 0;
953}
954\f
7d9884b9
JG
955enum language
956deduce_language_from_filename (filename)
957 char *filename;
958{
959 char *c = rindex (filename, '.');
960
961 if (!c) ; /* Get default. */
962 else if(!strcmp(c,".mod"))
963 return language_m2;
964 else if(!strcmp(c,".c"))
965 return language_c;
966 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
967 return language_cplus;
968
969 return language_unknown; /* default */
970}
971\f
d8ce1326
JG
972/* allocate_symtab:
973
974 Allocate and partly initialize a new symbol table. Return a pointer
975 to it. error() if no space.
976
977 Caller must set these fields:
978 LINETABLE(symtab)
979 symtab->blockvector
d8ce1326
JG
980 symtab->dirname
981 symtab->free_code
982 symtab->free_ptr
983 initialize any EXTRA_SYMTAB_INFO
984 possibly free_named_symtabs (symtab->filename);
985 symtab->next = symtab_list;
986 symtab_list = symtab;
987 */
988
989struct symtab *
7d9884b9 990allocate_symtab(name, objfile)
d8ce1326 991 char *name;
7d9884b9 992 struct objfile *objfile;
d8ce1326
JG
993{
994 register struct symtab *symtab;
d8ce1326
JG
995
996 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
997 bzero (symtab, sizeof (*symtab));
998 symtab->filename = name;
999 symtab->fullname = NULL;
1000 symtab->nlines = 0;
1001 symtab->line_charpos = 0;
1002 symtab->version = 0;
7d9884b9 1003 symtab->language = deduce_language_from_filename (name);
d8ce1326 1004
7d9884b9
JG
1005 /* Hook it to the objfile it comes from */
1006 symtab->objfile = objfile;
1007 symtab->objfile_chain = objfile->symtabs;
1008 objfile->symtabs = symtab;
1009
1010#ifdef INIT_EXTRA_SYMTAB_INFO
1011 INIT_EXTRA_SYMTAB_INFO(symtab);
1012#endif
d8ce1326
JG
1013
1014 return symtab;
1015}
1016\f
9d199712
JG
1017/* clear_symtab_users_once:
1018
1019 This function is run after symbol reading, or from a cleanup.
1020 If an old symbol table was obsoleted, the old symbol table
1021 has been blown away, but the other GDB data structures that may
1022 reference it have not yet been cleared or re-directed. (The old
1023 symtab was zapped, and the cleanup queued, in free_named_symtab()
1024 below.)
1025
1026 This function can be queued N times as a cleanup, or called
1027 directly; it will do all the work the first time, and then will be a
1028 no-op until the next time it is queued. This works by bumping a
1029 counter at queueing time. Much later when the cleanup is run, or at
1030 the end of symbol processing (in case the cleanup is discarded), if
1031 the queued count is greater than the "done-count", we do the work
1032 and set the done-count to the queued count. If the queued count is
1033 less than or equal to the done-count, we just ignore the call. This
1034 is needed because reading a single .o file will often replace many
1035 symtabs (one per .h file, for example), and we don't want to reset
1036 the breakpoints N times in the user's face.
1037
1038 The reason we both queue a cleanup, and call it directly after symbol
1039 reading, is because the cleanup protects us in case of errors, but is
1040 discarded if symbol reading is successful. */
1041
1042static int clear_symtab_users_queued;
1043static int clear_symtab_users_done;
1044
1045static void
1046clear_symtab_users_once ()
1047{
1048 /* Enforce once-per-`do_cleanups'-semantics */
1049 if (clear_symtab_users_queued <= clear_symtab_users_done)
1050 return;
1051 clear_symtab_users_done = clear_symtab_users_queued;
1052
1053 printf ("Resetting debugger state after updating old symbol tables\n");
1054
1055 /* Someday, we should do better than this, by only blowing away
1056 the things that really need to be blown. */
1057 clear_value_history ();
1058 clear_displays ();
1059 clear_internalvars ();
1060 breakpoint_re_set ();
1061 set_default_breakpoint (0, 0, 0, 0);
1062 current_source_symtab = 0;
1063}
1064
1065/* Delete the specified psymtab, and any others that reference it. */
1066
e1ce8aa5 1067static void
9d199712
JG
1068cashier_psymtab (pst)
1069 struct partial_symtab *pst;
1070{
1071 struct partial_symtab *ps, *pprev;
1072 int i;
1073
1074 /* Find its previous psymtab in the chain */
1075 for (ps = partial_symtab_list; ps; ps = ps->next) {
1076 if (ps == pst)
1077 break;
1078 pprev = ps;
1079 }
1080
1081 if (ps) {
1082 /* Unhook it from the chain. */
1083 if (ps == partial_symtab_list)
1084 partial_symtab_list = ps->next;
1085 else
1086 pprev->next = ps->next;
1087
1088 /* FIXME, we can't conveniently deallocate the entries in the
1089 partial_symbol lists (global_psymbols/static_psymbols) that
1090 this psymtab points to. These just take up space until all
1091 the psymtabs are reclaimed. Ditto the dependencies list and
1092 filename, which are all in the psymbol_obstack. */
1093
1094 /* We need to cashier any psymtab that has this one as a dependency... */
1095again:
1096 for (ps = partial_symtab_list; ps; ps = ps->next) {
1097 for (i = 0; i < ps->number_of_dependencies; i++) {
1098 if (ps->dependencies[i] == pst) {
1099 cashier_psymtab (ps);
1100 goto again; /* Must restart, chain has been munged. */
1101 }
1102 }
1103 }
1104 }
1105}
1106
1107/* If a symtab or psymtab for filename NAME is found, free it along
1108 with any dependent breakpoints, displays, etc.
1109 Used when loading new versions of object modules with the "add-file"
1110 command. This is only called on the top-level symtab or psymtab's name;
1111 it is not called for subsidiary files such as .h files.
1112
1113 Return value is 1 if we blew away the environment, 0 if not.
1114
1115 FIXME. I think this is not the best way to do this. We should
1116 work on being gentler to the environment while still cleaning up
1117 all stray pointers into the freed symtab. */
1118
1119int
1120free_named_symtabs (name)
1121 char *name;
1122{
1123 register struct symtab *s;
1124 register struct symtab *prev;
1125 register struct partial_symtab *ps;
9d199712
JG
1126 struct blockvector *bv;
1127 int blewit = 0;
1128
61a7292f
SG
1129 /* We only wack things if the symbol-reload switch is set. */
1130 if (!symbol_reloading)
1131 return 0;
1132
d11c44f1
JG
1133 /* Some symbol formats have trouble providing file names... */
1134 if (name == 0 || *name == '\0')
1135 return 0;
1136
9d199712
JG
1137 /* Look for a psymtab with the specified name. */
1138
1139again2:
1140 for (ps = partial_symtab_list; ps; ps = ps->next) {
1141 if (!strcmp (name, ps->filename)) {
1142 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1143 goto again2; /* Must restart, chain has been munged */
1144 }
1145 }
1146
1147 /* Look for a symtab with the specified name. */
1148
1149 for (s = symtab_list; s; s = s->next)
1150 {
1151 if (!strcmp (name, s->filename))
1152 break;
1153 prev = s;
1154 }
1155
1156 if (s)
1157 {
1158 if (s == symtab_list)
1159 symtab_list = s->next;
1160 else
1161 prev->next = s->next;
1162
1163 /* For now, queue a delete for all breakpoints, displays, etc., whether
1164 or not they depend on the symtab being freed. This should be
1165 changed so that only those data structures affected are deleted. */
1166
1167 /* But don't delete anything if the symtab is empty.
1168 This test is necessary due to a bug in "dbxread.c" that
1169 causes empty symtabs to be created for N_SO symbols that
1170 contain the pathname of the object file. (This problem
1171 has been fixed in GDB 3.9x). */
1172
c9bd6710
JG
1173 bv = BLOCKVECTOR (s);
1174 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1175 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1176 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1177 {
1178 complain (&oldsyms_complaint, name);
1179
1180 clear_symtab_users_queued++;
1181 make_cleanup (clear_symtab_users_once, 0);
1182 blewit = 1;
1183 } else {
1184 complain (&empty_symtab_complaint, name);
1185 }
1186
1187 free_symtab (s);
1188 }
1189 else
d8ce1326
JG
1190 {
1191 /* It is still possible that some breakpoints will be affected
1192 even though no symtab was found, since the file might have
1193 been compiled without debugging, and hence not be associated
1194 with a symtab. In order to handle this correctly, we would need
1195 to keep a list of text address ranges for undebuggable files.
1196 For now, we do nothing, since this is a fairly obscure case. */
1197 ;
1198 }
9d199712
JG
1199
1200 /* FIXME, what about the misc function vector? */
1201 return blewit;
1202}
1203\f
7d9884b9
JG
1204/*
1205 * Free all partial_symtab storage.
1206 */
1207static void
1208free_all_psymtabs()
1209{
1210 obstack_free (psymbol_obstack, 0);
1211 obstack_init (psymbol_obstack);
1212 partial_symtab_list = (struct partial_symtab *) 0;
1213}
1214
1215/* Free all the symtabs that are currently installed,
1216 and all storage associated with them.
1217 Leaves us in a consistent state with no symtabs installed. */
1218
1219static void
1220free_all_symtabs ()
1221{
1222 register struct symtab *s, *snext;
1223
1224 /* All values will be invalid because their types will be! */
1225
1226 clear_value_history ();
1227 clear_displays ();
1228 clear_internalvars ();
1229#if defined (CLEAR_SOLIB)
1230 CLEAR_SOLIB ();
1231#endif
1232 set_default_breakpoint (0, 0, 0, 0);
1233
1234 current_source_symtab = 0;
1235
1236 for (s = symtab_list; s; s = snext)
1237 {
1238 snext = s->next;
1239 free_symtab (s);
1240 }
1241 symtab_list = 0;
1242 obstack_free (symbol_obstack, 0);
1243 obstack_init (symbol_obstack);
1244
1245 if (misc_function_vector)
1246 free (misc_function_vector);
1247 misc_function_count = 0;
1248 misc_function_vector = 0;
1249 clear_pc_function_cache();
1250}
1251\f
bd5635a1
RP
1252void
1253_initialize_symfile ()
1254{
1255
1256 add_com ("symbol-file", class_files, symbol_file_command,
1257 "Load symbol table from executable file FILE.\n\
1258The `file' command can also load symbol tables, as well as setting the file\n\
1259to execute.");
1260
e74d7b43 1261 add_com ("add-symbol-file", class_files, add_symbol_file_command,
bd5635a1
RP
1262 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
1263The second argument provides the starting address of the file's text.");
1264
1265 add_com ("load", class_files, load_command,
1266 "Dynamically load FILE into the running program, and record its symbols\n\
1267for access from GDB.");
1268
1269 add_show_from_set
1270 (add_set_cmd ("complaints", class_support, var_uinteger,
1271 (char *)&stop_whining,
1272 "Set max number of complaints about incorrect symbols.",
1273 &setlist),
1274 &showlist);
1275
61a7292f
SG
1276 add_show_from_set
1277 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1278 (char *)&symbol_reloading,
1279 "Set dynamic symbol table reloading multiple times in one run.",
1280 &setlist),
1281 &showlist);
1282
bd5635a1
RP
1283 obstack_init (symbol_obstack);
1284 obstack_init (psymbol_obstack);
1285}
This page took 0.100196 seconds and 4 git commands to generate.