Add sparclite support.
[deliverable/binutils-gdb.git] / gdb / symfile.c
CommitLineData
bd5635a1 1/* Generic symbol file reading for the GNU debugger, GDB.
30875e1c 2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
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 20
bd5635a1
RP
21#include "defs.h"
22#include "symtab.h"
30875e1c 23#include "gdbtypes.h"
bd5635a1
RP
24#include "gdbcore.h"
25#include "frame.h"
26#include "target.h"
27#include "value.h"
28#include "symfile.h"
bf349b77 29#include "objfiles.h"
bd5635a1
RP
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
30875e1c
SG
41/* Global variables owned by this file */
42
80d68b1d 43int readnow_symbol_files; /* Read full symbols immediately */
d47d5315 44
30875e1c 45/* External variables and functions referenced. */
bd5635a1 46
30875e1c 47extern int info_verbose;
bd5635a1
RP
48
49/* Functions this file defines */
7d9884b9 50
30875e1c
SG
51static void
52load_command PARAMS ((char *, int));
53
54static void
55add_symbol_file_command PARAMS ((char *, int));
56
30875e1c
SG
57static void
58cashier_psymtab PARAMS ((struct partial_symtab *));
bd5635a1 59
30875e1c
SG
60static int
61compare_psymbols PARAMS ((const void *, const void *));
bd5635a1 62
30875e1c
SG
63static int
64compare_symbols PARAMS ((const void *, const void *));
65
b0246b3b
FF
66static bfd *
67symfile_bfd_open PARAMS ((char *));
30875e1c 68
80d68b1d
FF
69static void
70find_sym_fns PARAMS ((struct objfile *));
30875e1c
SG
71
72static void
73clear_symtab_users_once PARAMS ((void));
bd5635a1 74
80d68b1d
FF
75/* List of all available sym_fns. On gdb startup, each object file reader
76 calls add_symtab_fns() to register information on each format it is
77 prepared to read. */
bd5635a1 78
80d68b1d 79static struct sym_fns *symtab_fns = NULL;
bd5635a1 80
bd5635a1
RP
81/* Structures with which to manage partial symbol allocation. */
82
83struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
84
61a7292f
SG
85/* Flag for whether user will be reloading symbols multiple times.
86 Defaults to ON for VxWorks, otherwise OFF. */
87
88#ifdef SYMBOL_RELOADING_DEFAULT
89int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
90#else
91int symbol_reloading = 0;
92#endif
93
bd5635a1
RP
94/* Structure to manage complaints about symbol file contents. */
95
96struct complaint complaint_root[1] = {
30875e1c 97 {(char *) 0, 0, complaint_root},
bd5635a1
RP
98};
99
9d199712
JG
100/* Some actual complaints. */
101
102struct complaint oldsyms_complaint = {
103 "Replacing old symbols for `%s'", 0, 0 };
104
105struct complaint empty_symtab_complaint = {
106 "Empty symbol table found for `%s'", 0, 0 };
107
bd5635a1
RP
108\f
109/* In the following sort, we always make sure that
110 register debug symbol declarations always come before regular
111 debug symbol declarations (as might happen when parameters are
30875e1c
SG
112 then put into registers by the compiler).
113
114 Since this function is called from within qsort, in an ANSI environment
115 it must conform to the prototype for qsort, which specifies that the
116 comparison function takes two "void *" pointers. */
bd5635a1
RP
117
118static int
30875e1c
SG
119compare_symbols (s1p, s2p)
120 const PTR s1p;
121 const PTR s2p;
bd5635a1 122{
30875e1c 123 register struct symbol **s1, **s2;
bd5635a1
RP
124 register int namediff;
125
30875e1c
SG
126 s1 = (struct symbol **) s1p;
127 s2 = (struct symbol **) s2p;
128
bd5635a1
RP
129 /* Compare the initial characters. */
130 namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
131 if (namediff != 0) return namediff;
132
133 /* If they match, compare the rest of the names. */
134 namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
135 if (namediff != 0) return namediff;
136
137 /* For symbols of the same name, registers should come first. */
138 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
139 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
140}
141
30875e1c
SG
142/*
143
144LOCAL FUNCTION
145
146 compare_psymbols -- compare two partial symbols by name
147
148DESCRIPTION
149
150 Given pointer to two partial symbol table entries, compare
151 them by name and return -N, 0, or +N (ala strcmp). Typically
152 used by sorting routines like qsort().
153
154NOTES
155
156 Does direct compare of first two characters before punting
157 and passing to strcmp for longer compares. Note that the
158 original version had a bug whereby two null strings or two
159 identically named one character strings would return the
160 comparison of memory following the null byte.
161
162 */
163
164static int
165compare_psymbols (s1p, s2p)
166 const PTR s1p;
167 const PTR s2p;
168{
169 register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
170 register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
171
172 if ((st1[0] - st2[0]) || !st1[0])
173 {
174 return (st1[0] - st2[0]);
175 }
176 else if ((st1[1] - st2[1]) || !st1[1])
177 {
178 return (st1[1] - st2[1]);
179 }
180 else
181 {
182 return (strcmp (st1 + 2, st2 + 2));
183 }
184}
185
186void
187sort_pst_symbols (pst)
188 struct partial_symtab *pst;
189{
190 /* Sort the global list; don't sort the static list */
191
192 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
193 pst -> n_global_syms, sizeof (struct partial_symbol),
194 compare_psymbols);
195}
196
bd5635a1
RP
197/* Call sort_block_syms to sort alphabetically the symbols of one block. */
198
199void
200sort_block_syms (b)
201 register struct block *b;
202{
203 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
204 sizeof (struct symbol *), compare_symbols);
205}
206
207/* Call sort_symtab_syms to sort alphabetically
208 the symbols of each block of one symtab. */
209
210void
211sort_symtab_syms (s)
212 register struct symtab *s;
213{
c9bd6710
JG
214 register struct blockvector *bv;
215 int nbl;
bd5635a1
RP
216 int i;
217 register struct block *b;
218
c9bd6710
JG
219 if (s == 0)
220 return;
221 bv = BLOCKVECTOR (s);
222 nbl = BLOCKVECTOR_NBLOCKS (bv);
bd5635a1
RP
223 for (i = 0; i < nbl; i++)
224 {
225 b = BLOCKVECTOR_BLOCK (bv, i);
226 if (BLOCK_SHOULD_SORT (b))
227 sort_block_syms (b);
228 }
229}
230
231void
232sort_all_symtab_syms ()
233{
234 register struct symtab *s;
30875e1c 235 register struct objfile *objfile;
bd5635a1 236
30875e1c 237 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
bd5635a1 238 {
30875e1c
SG
239 for (s = objfile -> symtabs; s != NULL; s = s -> next)
240 {
241 sort_symtab_syms (s);
242 }
bd5635a1
RP
243 }
244}
245
246/* Make a copy of the string at PTR with SIZE characters in the symbol obstack
247 (and add a null character at the end in the copy).
248 Returns the address of the copy. */
249
250char *
30875e1c 251obsavestring (ptr, size, obstackp)
bd5635a1
RP
252 char *ptr;
253 int size;
30875e1c 254 struct obstack *obstackp;
bd5635a1 255{
30875e1c 256 register char *p = (char *) obstack_alloc (obstackp, size + 1);
bd5635a1
RP
257 /* Open-coded bcopy--saves function call time.
258 These strings are usually short. */
259 {
260 register char *p1 = ptr;
261 register char *p2 = p;
262 char *end = ptr + size;
263 while (p1 != end)
264 *p2++ = *p1++;
265 }
266 p[size] = 0;
267 return p;
268}
269
270/* Concatenate strings S1, S2 and S3; return the new string.
271 Space is found in the symbol_obstack. */
272
273char *
30875e1c
SG
274obconcat (obstackp, s1, s2, s3)
275 struct obstack *obstackp;
276 const char *s1, *s2, *s3;
bd5635a1
RP
277{
278 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
30875e1c 279 register char *val = (char *) obstack_alloc (obstackp, len);
bd5635a1
RP
280 strcpy (val, s1);
281 strcat (val, s2);
282 strcat (val, s3);
283 return val;
284}
bd5635a1
RP
285
286/* Get the symbol table that corresponds to a partial_symtab.
287 This is fast after the first time you do it. In fact, there
288 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
289 case inline. */
290
291struct symtab *
292psymtab_to_symtab (pst)
293 register struct partial_symtab *pst;
294{
bd5635a1
RP
295 /* If it's been looked up before, return it. */
296 if (pst->symtab)
297 return pst->symtab;
298
299 /* If it has not yet been read in, read it. */
300 if (!pst->readin)
301 {
302 (*pst->read_symtab) (pst);
303 }
304
61a7292f 305 return pst->symtab;
bd5635a1
RP
306}
307
bf349b77
FF
308/* Initialize entry point information for this objfile. */
309
310void
311init_entry_point_info (objfile)
312 struct objfile *objfile;
313{
314 /* Save startup file's range of PC addresses to help blockframe.c
315 decide where the bottom of the stack is. */
316
317 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
318 {
319 /* Executable file -- record its entry point so we'll recognize
320 the startup file because it contains the entry point. */
321 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
322 }
323 else
324 {
325 /* Examination of non-executable.o files. Short-circuit this stuff. */
326 /* ~0 will not be in any file, we hope. */
327 objfile -> ei.entry_point = ~0;
328 /* set the startup file to be an empty range. */
329 objfile -> ei.entry_file_lowpc = 0;
330 objfile -> ei.entry_file_highpc = 0;
331 }
332}
333
bd5635a1
RP
334/* Process a symbol file, as either the main file or as a dynamically
335 loaded file.
336
b3fdaf3d
JK
337 NAME is the file name (which will be tilde-expanded and made
338 absolute herein) (but we don't free or modify NAME itself).
339 FROM_TTY says how verbose to be. MAINLINE specifies whether this
340 is the main symbol file, or whether it's an extra symbol file such
341 as dynamically loaded code. If !mainline, ADDR is the address
4369a140
JG
342 where the text segment was loaded. If VERBO, the caller has printed
343 a verbose message about the symbol reading (and complaints can be
344 more terse about it). */
bd5635a1
RP
345
346void
4369a140 347syms_from_objfile (objfile, addr, mainline, verbo)
7d9884b9 348 struct objfile *objfile;
bd5635a1
RP
349 CORE_ADDR addr;
350 int mainline;
4369a140 351 int verbo;
bd5635a1 352{
bd5635a1 353 asection *text_sect;
bd5635a1 354
bd5635a1
RP
355 /* There is a distinction between having no symbol table
356 (we refuse to read the file, leaving the old set of symbols around)
357 and having no debugging symbols in your symbol table (we read
bf349b77
FF
358 the file and end up with a mostly empty symbol table).
359
360 FIXME: This strategy works correctly when the debugging symbols are
361 intermixed with "normal" symbols. However, when the debugging symbols
362 are separate, such as with ELF/DWARF, it is perfectly plausible for
363 the symbol table to be missing but still have all the DWARF info
364 intact. Thus in general it is wrong to assume that having no symbol
365 table implies no debugging information. */
bd5635a1 366
b0246b3b 367 if (!(bfd_get_file_flags (objfile -> obfd) & HAS_SYMS))
d47d5315
JG
368 return;
369
bf349b77 370 init_entry_point_info (objfile);
80d68b1d 371 find_sym_fns (objfile);
bd5635a1
RP
372
373 if (mainline)
374 {
375 /* Since no error yet, throw away the old symbol table. */
376
80d68b1d
FF
377 if (symfile_objfile != NULL)
378 {
379 free_objfile (symfile_objfile);
380 symfile_objfile = NULL;
381 }
bd5635a1 382
80d68b1d 383 (*objfile -> sf -> sym_new_init) (objfile);
bd5635a1
RP
384
385 /* For mainline, caller didn't know the specified address of the
386 text section. We fix that here. */
80d68b1d 387
b0246b3b
FF
388 text_sect = bfd_get_section_by_name (objfile -> obfd, ".text");
389 addr = bfd_section_vma (objfile -> obfd, text_sect);
bd5635a1
RP
390 }
391
80d68b1d
FF
392 /* Initialize symbol reading routines for this objfile, allow complaints to
393 appear for this new file, and record how verbose to be, then do the
394 initial symbol reading for this file. */
4369a140 395
80d68b1d
FF
396 (*objfile -> sf -> sym_init) (objfile);
397 clear_complaints (1, verbo);
398 (*objfile -> sf -> sym_read) (objfile, addr, mainline);
bd5635a1
RP
399
400 /* Don't allow char * to have a typename (else would get caddr_t.) */
401 /* Ditto void *. FIXME should do this for all the builtin types. */
402
403 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
404 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
405
406 if (mainline)
407 {
408 /* OK, make it the "real" symbol file. */
7d9884b9 409 symfile_objfile = objfile;
bd5635a1
RP
410 }
411
0ef6f019
JG
412 /* If we have wiped out any old symbol tables, clean up. */
413 clear_symtab_users_once ();
4369a140
JG
414
415 /* We're done reading the symbol file; finish off complaints. */
80d68b1d 416 clear_complaints (0, verbo);
30875e1c 417
318bf84f
FF
418 /* Fixup all the breakpoints that may have been redefined by this
419 symbol file. */
30875e1c 420
318bf84f 421 breakpoint_re_set ();
30875e1c 422}
d47d5315
JG
423
424/* Process a symbol file, as either the main file or as a dynamically
425 loaded file.
426
427 NAME is the file name (which will be tilde-expanded and made
428 absolute herein) (but we don't free or modify NAME itself).
429 FROM_TTY says how verbose to be. MAINLINE specifies whether this
430 is the main symbol file, or whether it's an extra symbol file such
431 as dynamically loaded code. If !mainline, ADDR is the address
30875e1c 432 where the text segment was loaded.
d47d5315 433
30875e1c
SG
434 Upon success, returns a pointer to the objfile that was added.
435 Upon failure, jumps back to command level (never returns). */
436
437struct objfile *
b0246b3b 438symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
d47d5315
JG
439 char *name;
440 int from_tty;
441 CORE_ADDR addr;
442 int mainline;
318bf84f 443 int mapped;
b0246b3b 444 int readnow;
d47d5315 445{
7d9884b9 446 struct objfile *objfile;
b0246b3b 447 struct partial_symtab *psymtab;
80d68b1d 448 bfd *abfd;
d47d5315 449
80d68b1d
FF
450 /* Open a bfd for the file and then check to see if the file has a
451 symbol table. There is a distinction between having no symbol table
d47d5315 452 (we refuse to read the file, leaving the old set of symbols around)
80d68b1d
FF
453 and having no debugging symbols in the symbol table (we read the file
454 and end up with a mostly empty symbol table, but with lots of stuff in
455 the minimal symbol table). We need to make the decision about whether
456 to continue with the file before allocating and building a objfile.
457
458 FIXME: This strategy works correctly when the debugging symbols are
459 intermixed with "normal" symbols. However, when the debugging symbols
460 are separate, such as with ELF/DWARF, it is perfectly plausible for
461 the symbol table to be missing but still have all the DWARF info
462 intact. Thus in general it is wrong to assume that having no symbol
463 table implies no debugging information. */
464
465 abfd = symfile_bfd_open (name);
466 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
d47d5315
JG
467 {
468 error ("%s has no symbol-table", name);
469 }
470
80d68b1d
FF
471 if ((have_full_symbols () || have_partial_symbols ())
472 && mainline
473 && from_tty
474 && !query ("Load new symbol table from \"%s\"? ", name))
475 error ("Not confirmed.");
476
477 objfile = allocate_objfile (abfd, mapped);
478
318bf84f
FF
479 /* If the objfile uses a mapped symbol file, and we have a psymtab for
480 it, then skip reading any symbols at this time. */
d47d5315 481
bf349b77 482 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
d47d5315 483 {
80d68b1d 484 /* We mapped in an existing symbol table file that already has had
bf349b77
FF
485 initial symbol reading performed, so we can skip that part. Notify
486 the user that instead of reading the symbols, they have been mapped.
487 */
318bf84f
FF
488 if (from_tty || info_verbose)
489 {
80d68b1d
FF
490 printf_filtered ("Mapped symbols for %s...", name);
491 wrap_here ("");
318bf84f
FF
492 fflush (stdout);
493 }
d47d5315 494 }
318bf84f 495 else
bd5635a1 496 {
80d68b1d 497 /* We either created a new mapped symbol table, mapped an existing
bf349b77
FF
498 symbol table file which has not had initial symbol reading
499 performed, or need to read an unmapped symbol table. */
318bf84f
FF
500 if (from_tty || info_verbose)
501 {
502 printf_filtered ("Reading symbols from %s...", name);
503 wrap_here ("");
504 fflush (stdout);
505 }
318bf84f 506 syms_from_objfile (objfile, addr, mainline, from_tty);
bf349b77 507 objfile -> flags |= OBJF_SYMS;
80d68b1d
FF
508 }
509
510 /* We now have at least a partial symbol table. Check to see if the
511 user requested that all symbols be read on initial access via either
512 the gdb startup command line or on a per symbol file basis. Expand
513 all partial symbol tables for this objfile if so. */
b0246b3b 514
bf349b77 515 if (readnow || readnow_symbol_files)
80d68b1d 516 {
318bf84f
FF
517 if (from_tty || info_verbose)
518 {
80d68b1d
FF
519 printf_filtered ("expanding to full symbols...");
520 wrap_here ("");
318bf84f
FF
521 fflush (stdout);
522 }
80d68b1d
FF
523
524 for (psymtab = objfile -> psymtabs;
525 psymtab != NULL;
526 psymtab = psymtab -> next)
527 {
528 (void) psymtab_to_symtab (psymtab);
529 }
530 }
531
532 if (from_tty || info_verbose)
533 {
534 printf_filtered ("done.\n");
535 fflush (stdout);
bd5635a1 536 }
80d68b1d 537
30875e1c 538 return (objfile);
bd5635a1
RP
539}
540
541/* This is the symbol-file command. Read the file, analyze its symbols,
30875e1c 542 and add a struct symtab to a symtab list. */
bd5635a1
RP
543
544void
30875e1c
SG
545symbol_file_command (args, from_tty)
546 char *args;
bd5635a1
RP
547 int from_tty;
548{
30875e1c 549 char **argv;
b0246b3b 550 char *name = NULL;
30875e1c 551 struct cleanup *cleanups;
318bf84f 552 int mapped = 0;
30875e1c 553 int readnow = 0;
bd5635a1
RP
554
555 dont_repeat ();
556
30875e1c 557 if (args == NULL)
bd5635a1 558 {
cba0d141
JG
559 if ((have_full_symbols () || have_partial_symbols ())
560 && from_tty
561 && !query ("Discard symbol table from `%s'? ",
562 symfile_objfile -> name))
563 error ("Not confirmed.");
564 free_all_objfiles ();
30875e1c 565 symfile_objfile = NULL;
bd5635a1 566 }
30875e1c
SG
567 else
568 {
569 if ((argv = buildargv (args)) == NULL)
570 {
318bf84f 571 nomem (0);
30875e1c
SG
572 }
573 cleanups = make_cleanup (freeargv, (char *) argv);
b0246b3b 574 while (*argv != NULL)
30875e1c 575 {
b0246b3b 576 if (strcmp (*argv, "-mapped") == 0)
30875e1c 577 {
318bf84f 578 mapped = 1;
30875e1c 579 }
b0246b3b 580 else if (strcmp (*argv, "-readnow") == 0)
30875e1c
SG
581 {
582 readnow = 1;
583 }
b0246b3b
FF
584 else if (**argv == '-')
585 {
586 error ("unknown option `%s'", *argv);
587 }
588 else
589 {
590 name = *argv;
591 }
592 argv++;
30875e1c 593 }
2403f49b 594
b0246b3b
FF
595 if (name == NULL)
596 {
597 error ("no symbol file name was specified");
598 }
599 else
30875e1c 600 {
318bf84f
FF
601 /* Getting new symbols may change our opinion about what is
602 frameless. */
603 reinit_frame_cache ();
ac88ca20 604 (void) symbol_file_add (name, from_tty, (CORE_ADDR)0, 1,
b0246b3b 605 mapped, readnow);
30875e1c
SG
606 }
607 do_cleanups (cleanups);
608 }
bd5635a1
RP
609}
610
b0246b3b
FF
611/* Open file specified by NAME and hand it off to BFD for preliminary
612 analysis. Result is a newly initialized bfd *, which includes a newly
613 malloc'd` copy of NAME (tilde-expanded and made absolute).
7d9884b9 614 In case of trouble, error() is called. */
bd5635a1 615
b0246b3b
FF
616static bfd *
617symfile_bfd_open (name)
bd5635a1
RP
618 char *name;
619{
620 bfd *sym_bfd;
621 int desc;
622 char *absolute_name;
623
7d9884b9 624 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 625
7d9884b9 626 /* Look down path for it, allocate 2nd new malloc'd copy. */
bd5635a1 627 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
b0246b3b
FF
628 if (desc < 0)
629 {
630 make_cleanup (free, name);
631 perror_with_name (name);
632 }
7d9884b9 633 free (name); /* Free 1st new malloc'd copy */
30875e1c 634 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
bd5635a1
RP
635
636 sym_bfd = bfd_fdopenr (name, NULL, desc);
637 if (!sym_bfd)
638 {
639 close (desc);
7d9884b9 640 make_cleanup (free, name);
b0246b3b
FF
641 error ("\"%s\": can't open to read symbols: %s.", name,
642 bfd_errmsg (bfd_error));
bd5635a1 643 }
bd5635a1 644
b0246b3b
FF
645 if (!bfd_check_format (sym_bfd, bfd_object))
646 {
647 bfd_close (sym_bfd); /* This also closes desc */
648 make_cleanup (free, name);
649 error ("\"%s\": can't read symbols: %s.", name,
650 bfd_errmsg (bfd_error));
651 }
7d9884b9 652
b0246b3b 653 return (sym_bfd);
7d9884b9
JG
654}
655
80d68b1d
FF
656/* Link a new symtab_fns into the global symtab_fns list. Called on gdb
657 startup by the _initialize routine in each object file format reader,
658 to register information about each format the the reader is prepared
659 to handle. */
bd5635a1
RP
660
661void
662add_symtab_fns (sf)
663 struct sym_fns *sf;
664{
665 sf->next = symtab_fns;
666 symtab_fns = sf;
667}
668
669
670/* Initialize to read symbols from the symbol file sym_bfd. It either
80d68b1d
FF
671 returns or calls error(). The result is an initialized struct sym_fns
672 in the objfile structure, that contains cached information about the
673 symbol file. */
bd5635a1 674
80d68b1d
FF
675static void
676find_sym_fns (objfile)
7d9884b9 677 struct objfile *objfile;
bd5635a1 678{
ac88ca20 679 struct sym_fns *sf;
bd5635a1 680
80d68b1d 681 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
bd5635a1 682 {
80d68b1d
FF
683 if (strncmp (bfd_get_target (objfile -> obfd),
684 sf -> sym_name, sf -> sym_namelen) == 0)
bd5635a1 685 {
80d68b1d
FF
686 objfile -> sf = sf;
687 return;
bd5635a1
RP
688 }
689 }
c9bd6710 690 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
b0246b3b 691 bfd_get_target (objfile -> obfd));
bd5635a1
RP
692}
693\f
694/* This function runs the load command of our current target. */
695
30875e1c 696static void
bd5635a1
RP
697load_command (arg, from_tty)
698 char *arg;
699 int from_tty;
700{
701 target_load (arg, from_tty);
702}
703
61a7292f
SG
704/* This function allows the addition of incrementally linked object files.
705 It does not modify any state in the target, only in the debugger. */
bd5635a1 706
e1ce8aa5 707/* ARGSUSED */
30875e1c 708static void
b0246b3b
FF
709add_symbol_file_command (args, from_tty)
710 char *args;
bd5635a1
RP
711 int from_tty;
712{
b0246b3b 713 char *name = NULL;
bd5635a1 714 CORE_ADDR text_addr;
b0246b3b 715 char *arg;
ac88ca20
JG
716 int readnow = 0;
717 int mapped = 0;
bd5635a1 718
b0246b3b 719 dont_repeat ();
61a7292f 720
b0246b3b
FF
721 if (args == NULL)
722 {
723 error ("add-symbol-file takes a file name and an address");
724 }
bd5635a1 725
b0246b3b 726 /* Make a copy of the string that we can safely write into. */
bd5635a1 727
b0246b3b
FF
728 args = strdup (args);
729 make_cleanup (free, args);
730
731 /* Pick off any -option args and the file name. */
732
733 while ((*args != '\000') && (name == NULL))
734 {
735 while (isspace (*args)) {args++;}
736 arg = args;
737 while ((*args != '\000') && !isspace (*args)) {args++;}
738 if (*args != '\000')
739 {
740 *args++ = '\000';
741 }
742 if (*arg != '-')
743 {
744 name = arg;
745 }
746 else if (strcmp (arg, "-mapped") == 0)
747 {
748 mapped = 1;
749 }
750 else if (strcmp (arg, "-readnow") == 0)
751 {
752 readnow = 1;
753 }
754 else
755 {
756 error ("unknown option `%s'", arg);
757 }
758 }
bd5635a1 759
b0246b3b
FF
760 /* After picking off any options and the file name, args should be
761 left pointing at the remainder of the command line, which should
762 be the address expression to evaluate. */
bd5635a1 763
b0246b3b
FF
764 if ((name == NULL) || (*args == '\000') )
765 {
766 error ("add-symbol-file takes a file name and an address");
767 }
768 name = tilde_expand (name);
769 make_cleanup (free, name);
bd5635a1 770
b0246b3b 771 text_addr = parse_and_eval_address (args);
bd5635a1 772
d8ce1326
JG
773 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
774 name, local_hex_string (text_addr)))
bd5635a1
RP
775 error ("Not confirmed.");
776
b0246b3b
FF
777 /* Getting new symbols may change our opinion about what is
778 frameless. */
779
780 reinit_frame_cache ();
781
782 (void) symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
bd5635a1
RP
783}
784\f
7d9884b9 785/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
786void
787reread_symbols ()
788{
7d9884b9
JG
789 struct objfile *objfile;
790 long new_modtime;
791 int reread_one = 0;
cba0d141
JG
792 struct stat new_statbuf;
793 int res;
bd5635a1
RP
794
795 /* With the addition of shared libraries, this should be modified,
796 the load time should be saved in the partial symbol tables, since
797 different tables may come from different source files. FIXME.
798 This routine should then walk down each partial symbol table
30875e1c 799 and see if the symbol table that it originates from has been changed */
bd5635a1 800
30875e1c 801the_big_top:
7d9884b9
JG
802 for (objfile = object_files; objfile; objfile = objfile->next) {
803 if (objfile->obfd) {
1eeba686 804#ifdef IBM6000_TARGET
318bf84f
FF
805 /* If this object is from a shared library, then you should
806 stat on the library name, not member name. */
807
808 if (objfile->obfd->my_archive)
809 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
810 else
811#endif
cba0d141
JG
812 res = stat (objfile->name, &new_statbuf);
813 if (res != 0) {
814 /* FIXME, should use print_sys_errmsg but it's not filtered. */
815 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
816 objfile->name);
817 continue;
818 }
819 new_modtime = new_statbuf.st_mtime;
7d9884b9
JG
820 if (new_modtime != objfile->mtime) {
821 printf_filtered ("`%s' has changed; re-reading symbols.\n",
822 objfile->name);
823 /* FIXME, this should use a different command...that would only
30875e1c
SG
824 affect this objfile's symbols, and would reset objfile->mtime.
825 (objfile->mtime = new_modtime;)
826 HOWEVER, that command isn't written yet -- so call symbol_file_
827 command, and restart the scan from the top, because it munges
828 the object_files list. */
7d9884b9 829 symbol_file_command (objfile->name, 0);
7d9884b9 830 reread_one = 1;
30875e1c 831 goto the_big_top; /* Start over. */
7d9884b9 832 }
bd5635a1 833 }
7d9884b9
JG
834 }
835
836 if (reread_one)
837 breakpoint_re_set ();
bd5635a1 838}
bd5635a1
RP
839\f
840/* Functions to handle complaints during symbol reading. */
841
842/* How many complaints about a particular thing should be printed before
61a7292f
SG
843 we stop whining about it? Default is no whining at all, since so many
844 systems have ill-constructed symbol files. */
bd5635a1 845
61a7292f 846static unsigned stop_whining = 0;
bd5635a1 847
4369a140
JG
848/* Should each complaint be self explanatory, or should we assume that
849 a series of complaints is being produced?
850 case 0: self explanatory message.
851 case 1: First message of a series that must start off with explanation.
852 case 2: Subsequent message, when user already knows we are reading
853 symbols and we can just state our piece. */
854
855static int complaint_series = 0;
856
bd5635a1 857/* Print a complaint about the input symbols, and link the complaint block
7d9884b9 858 into a chain for later handling. */
bd5635a1 859
7d9884b9 860void
bd5635a1
RP
861complain (complaint, val)
862 struct complaint *complaint;
863 char *val;
864{
865 complaint->counter++;
866 if (complaint->next == 0) {
867 complaint->next = complaint_root->next;
868 complaint_root->next = complaint;
869 }
870 if (complaint->counter > stop_whining)
7d9884b9 871 return;
bd5635a1 872 wrap_here ("");
4369a140
JG
873
874 switch (complaint_series + (info_verbose << 1)) {
875
876 /* Isolated messages, must be self-explanatory. */
877 case 0:
878 puts_filtered ("During symbol reading, ");
879 wrap_here("");
880 printf_filtered (complaint->message, val);
881 puts_filtered (".\n");
882 break;
883
884 /* First of a series, without `set verbose'. */
885 case 1:
bd5635a1 886 puts_filtered ("During symbol reading...");
4369a140
JG
887 printf_filtered (complaint->message, val);
888 puts_filtered ("...");
889 wrap_here("");
890 complaint_series++;
891 break;
892
893 /* Subsequent messages of a series, or messages under `set verbose'.
894 (We'll already have produced a "Reading in symbols for XXX..." message
895 and will clean up at the end with a newline.) */
896 default:
897 printf_filtered (complaint->message, val);
898 puts_filtered ("...");
899 wrap_here("");
bd5635a1 900 }
bd5635a1
RP
901}
902
4369a140
JG
903/* Clear out all complaint counters that have ever been incremented.
904 If sym_reading is 1, be less verbose about successive complaints,
905 since the messages are appearing all together during a command that
906 reads symbols (rather than scattered around as psymtabs get fleshed
907 out into symtabs at random times). If noisy is 1, we are in a
908 noisy symbol reading command, and our caller will print enough
909 context for the user to figure it out. */
bd5635a1
RP
910
911void
4369a140
JG
912clear_complaints (sym_reading, noisy)
913 int sym_reading;
914 int noisy;
bd5635a1
RP
915{
916 struct complaint *p;
917
918 for (p = complaint_root->next; p != complaint_root; p = p->next)
919 p->counter = 0;
4369a140
JG
920
921 if (!sym_reading && !noisy && complaint_series > 1) {
922 /* Terminate previous series, since caller won't. */
923 puts_filtered ("\n");
924 }
925
926 complaint_series = sym_reading? 1 + noisy: 0;
bd5635a1
RP
927}
928\f
7d9884b9
JG
929enum language
930deduce_language_from_filename (filename)
931 char *filename;
932{
30875e1c 933 char *c = strrchr (filename, '.');
7d9884b9
JG
934
935 if (!c) ; /* Get default. */
936 else if(!strcmp(c,".mod"))
937 return language_m2;
938 else if(!strcmp(c,".c"))
939 return language_c;
940 else if(!strcmp(c,".cc") || !strcmp(c,".C"))
941 return language_cplus;
942
943 return language_unknown; /* default */
944}
945\f
d8ce1326
JG
946/* allocate_symtab:
947
948 Allocate and partly initialize a new symbol table. Return a pointer
949 to it. error() if no space.
950
951 Caller must set these fields:
952 LINETABLE(symtab)
953 symtab->blockvector
d8ce1326
JG
954 symtab->dirname
955 symtab->free_code
956 symtab->free_ptr
957 initialize any EXTRA_SYMTAB_INFO
958 possibly free_named_symtabs (symtab->filename);
d8ce1326
JG
959 */
960
961struct symtab *
30875e1c
SG
962allocate_symtab (filename, objfile)
963 char *filename;
964 struct objfile *objfile;
d8ce1326
JG
965{
966 register struct symtab *symtab;
d8ce1326 967
30875e1c
SG
968 symtab = (struct symtab *)
969 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
970 (void) memset (symtab, 0, sizeof (*symtab));
971 symtab -> filename = obsavestring (filename, strlen (filename),
972 &objfile -> symbol_obstack);
973 symtab -> fullname = NULL;
974 symtab -> language = deduce_language_from_filename (filename);
d8ce1326 975
7d9884b9 976 /* Hook it to the objfile it comes from */
30875e1c
SG
977
978 symtab -> objfile = objfile;
979 symtab -> next = objfile -> symtabs;
980 objfile -> symtabs = symtab;
7d9884b9
JG
981
982#ifdef INIT_EXTRA_SYMTAB_INFO
30875e1c 983 INIT_EXTRA_SYMTAB_INFO (symtab);
7d9884b9 984#endif
d8ce1326 985
30875e1c 986 return (symtab);
d8ce1326 987}
30875e1c
SG
988
989struct partial_symtab *
990allocate_psymtab (filename, objfile)
991 char *filename;
992 struct objfile *objfile;
993{
994 struct partial_symtab *psymtab;
995
cba0d141
JG
996 if (objfile -> free_psymtabs)
997 {
998 psymtab = objfile -> free_psymtabs;
999 objfile -> free_psymtabs = psymtab -> next;
1000 }
1001 else
1002 psymtab = (struct partial_symtab *)
1003 obstack_alloc (&objfile -> psymbol_obstack,
1004 sizeof (struct partial_symtab));
1005
30875e1c
SG
1006 (void) memset (psymtab, 0, sizeof (struct partial_symtab));
1007 psymtab -> filename = obsavestring (filename, strlen (filename),
1008 &objfile -> psymbol_obstack);
1009 psymtab -> symtab = NULL;
1010
1011 /* Hook it to the objfile it comes from */
1012
1013 psymtab -> objfile = objfile;
1014 psymtab -> next = objfile -> psymtabs;
1015 objfile -> psymtabs = psymtab;
1016
1017 return (psymtab);
1018}
1019
d8ce1326 1020\f
9d199712
JG
1021/* clear_symtab_users_once:
1022
1023 This function is run after symbol reading, or from a cleanup.
1024 If an old symbol table was obsoleted, the old symbol table
1025 has been blown away, but the other GDB data structures that may
1026 reference it have not yet been cleared or re-directed. (The old
1027 symtab was zapped, and the cleanup queued, in free_named_symtab()
1028 below.)
1029
1030 This function can be queued N times as a cleanup, or called
1031 directly; it will do all the work the first time, and then will be a
1032 no-op until the next time it is queued. This works by bumping a
1033 counter at queueing time. Much later when the cleanup is run, or at
1034 the end of symbol processing (in case the cleanup is discarded), if
1035 the queued count is greater than the "done-count", we do the work
1036 and set the done-count to the queued count. If the queued count is
1037 less than or equal to the done-count, we just ignore the call. This
1038 is needed because reading a single .o file will often replace many
1039 symtabs (one per .h file, for example), and we don't want to reset
1040 the breakpoints N times in the user's face.
1041
1042 The reason we both queue a cleanup, and call it directly after symbol
1043 reading, is because the cleanup protects us in case of errors, but is
1044 discarded if symbol reading is successful. */
1045
1046static int clear_symtab_users_queued;
1047static int clear_symtab_users_done;
1048
1049static void
1050clear_symtab_users_once ()
1051{
1052 /* Enforce once-per-`do_cleanups'-semantics */
1053 if (clear_symtab_users_queued <= clear_symtab_users_done)
1054 return;
1055 clear_symtab_users_done = clear_symtab_users_queued;
1056
1057 printf ("Resetting debugger state after updating old symbol tables\n");
1058
1059 /* Someday, we should do better than this, by only blowing away
1060 the things that really need to be blown. */
1061 clear_value_history ();
1062 clear_displays ();
1063 clear_internalvars ();
1064 breakpoint_re_set ();
1065 set_default_breakpoint (0, 0, 0, 0);
1066 current_source_symtab = 0;
1067}
1068
1069/* Delete the specified psymtab, and any others that reference it. */
1070
e1ce8aa5 1071static void
9d199712
JG
1072cashier_psymtab (pst)
1073 struct partial_symtab *pst;
1074{
1075 struct partial_symtab *ps, *pprev;
1076 int i;
1077
1078 /* Find its previous psymtab in the chain */
30875e1c 1079 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1080 if (ps == pst)
1081 break;
1082 pprev = ps;
1083 }
1084
1085 if (ps) {
1086 /* Unhook it from the chain. */
30875e1c
SG
1087 if (ps == pst->objfile->psymtabs)
1088 pst->objfile->psymtabs = ps->next;
9d199712
JG
1089 else
1090 pprev->next = ps->next;
1091
1092 /* FIXME, we can't conveniently deallocate the entries in the
1093 partial_symbol lists (global_psymbols/static_psymbols) that
1094 this psymtab points to. These just take up space until all
1095 the psymtabs are reclaimed. Ditto the dependencies list and
1096 filename, which are all in the psymbol_obstack. */
1097
1098 /* We need to cashier any psymtab that has this one as a dependency... */
1099again:
30875e1c 1100 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1101 for (i = 0; i < ps->number_of_dependencies; i++) {
1102 if (ps->dependencies[i] == pst) {
1103 cashier_psymtab (ps);
1104 goto again; /* Must restart, chain has been munged. */
1105 }
1106 }
1107 }
1108 }
1109}
1110
1111/* If a symtab or psymtab for filename NAME is found, free it along
1112 with any dependent breakpoints, displays, etc.
1113 Used when loading new versions of object modules with the "add-file"
1114 command. This is only called on the top-level symtab or psymtab's name;
1115 it is not called for subsidiary files such as .h files.
1116
1117 Return value is 1 if we blew away the environment, 0 if not.
30875e1c 1118 FIXME. The return valu appears to never be used.
9d199712
JG
1119
1120 FIXME. I think this is not the best way to do this. We should
1121 work on being gentler to the environment while still cleaning up
1122 all stray pointers into the freed symtab. */
1123
1124int
1125free_named_symtabs (name)
1126 char *name;
1127{
30875e1c
SG
1128#if 0
1129 /* FIXME: With the new method of each objfile having it's own
1130 psymtab list, this function needs serious rethinking. In particular,
1131 why was it ever necessary to toss psymtabs with specific compilation
1132 unit filenames, as opposed to all psymtabs from a particular symbol
ac88ca20
JG
1133 file? -- fnf
1134 Well, the answer is that some systems permit reloading of particular
1135 compilation units. We want to blow away any old info about these
1136 compilation units, regardless of which objfiles they arrived in. --gnu. */
1137
1138 register struct symtab *s;
1139 register struct symtab *prev;
1140 register struct partial_symtab *ps;
1141 struct blockvector *bv;
1142 int blewit = 0;
30875e1c 1143
61a7292f
SG
1144 /* We only wack things if the symbol-reload switch is set. */
1145 if (!symbol_reloading)
1146 return 0;
1147
d11c44f1
JG
1148 /* Some symbol formats have trouble providing file names... */
1149 if (name == 0 || *name == '\0')
1150 return 0;
1151
9d199712
JG
1152 /* Look for a psymtab with the specified name. */
1153
1154again2:
1155 for (ps = partial_symtab_list; ps; ps = ps->next) {
1156 if (!strcmp (name, ps->filename)) {
1157 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1158 goto again2; /* Must restart, chain has been munged */
1159 }
1160 }
1161
1162 /* Look for a symtab with the specified name. */
1163
1164 for (s = symtab_list; s; s = s->next)
1165 {
1166 if (!strcmp (name, s->filename))
1167 break;
1168 prev = s;
1169 }
1170
1171 if (s)
1172 {
1173 if (s == symtab_list)
1174 symtab_list = s->next;
1175 else
1176 prev->next = s->next;
1177
1178 /* For now, queue a delete for all breakpoints, displays, etc., whether
1179 or not they depend on the symtab being freed. This should be
1180 changed so that only those data structures affected are deleted. */
1181
1182 /* But don't delete anything if the symtab is empty.
1183 This test is necessary due to a bug in "dbxread.c" that
1184 causes empty symtabs to be created for N_SO symbols that
1185 contain the pathname of the object file. (This problem
1186 has been fixed in GDB 3.9x). */
1187
c9bd6710
JG
1188 bv = BLOCKVECTOR (s);
1189 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1190 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1191 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1192 {
1193 complain (&oldsyms_complaint, name);
1194
1195 clear_symtab_users_queued++;
1196 make_cleanup (clear_symtab_users_once, 0);
1197 blewit = 1;
1198 } else {
1199 complain (&empty_symtab_complaint, name);
1200 }
1201
1202 free_symtab (s);
1203 }
1204 else
d8ce1326
JG
1205 {
1206 /* It is still possible that some breakpoints will be affected
1207 even though no symtab was found, since the file might have
1208 been compiled without debugging, and hence not be associated
1209 with a symtab. In order to handle this correctly, we would need
1210 to keep a list of text address ranges for undebuggable files.
1211 For now, we do nothing, since this is a fairly obscure case. */
1212 ;
1213 }
9d199712 1214
30875e1c 1215 /* FIXME, what about the minimal symbol table? */
9d199712 1216 return blewit;
30875e1c
SG
1217#else
1218 return (0);
1219#endif
9d199712
JG
1220}
1221\f
d4ea2aba
PB
1222/* Allocate and partially fill a partial symtab. It will be
1223 completely filled at the end of the symbol list.
1224
1225 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1226 is the address relative to which its symbols are (incremental) or 0
1227 (normal). */
1228
1229
1230struct partial_symtab *
1231start_psymtab_common (objfile, addr,
1232 filename, textlow, global_syms, static_syms)
1233 struct objfile *objfile;
1234 CORE_ADDR addr;
1235 char *filename;
1236 CORE_ADDR textlow;
1237 struct partial_symbol *global_syms;
1238 struct partial_symbol *static_syms;
1239{
30875e1c
SG
1240 struct partial_symtab *psymtab;
1241
1242 psymtab = allocate_psymtab (filename, objfile);
1243 psymtab -> addr = addr;
1244 psymtab -> textlow = textlow;
1245 psymtab -> texthigh = psymtab -> textlow; /* default */
1246 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1247 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1248 return (psymtab);
7d9884b9
JG
1249}
1250
7d9884b9 1251\f
bd5635a1
RP
1252void
1253_initialize_symfile ()
1254{
1255
1256 add_com ("symbol-file", class_files, symbol_file_command,
30875e1c 1257 "Load symbol table from executable file FILE.\n\
bd5635a1
RP
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
4369a140 1270 (add_set_cmd ("complaints", class_support, var_zinteger,
bd5635a1
RP
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 1283}
This page took 0.123856 seconds and 4 git commands to generate.