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