Modified Files:
[deliverable/binutils-gdb.git] / gdb / symfile.c
CommitLineData
bd5635a1 1/* Generic symbol file reading for the GNU debugger, GDB.
2093fe68 2 Copyright 1990, 1991, 1992, 1993 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"
e58de8a2 32#include "language.h"
51b80b00 33#include "complaints.h"
2e4964ad 34#include "demangle.h"
4d57c599 35#include "inferior.h" /* for write_pc */
bd5635a1
RP
36
37#include <obstack.h>
38#include <assert.h>
39
40#include <sys/types.h>
41#include <fcntl.h>
42#include <string.h>
43#include <sys/stat.h>
9342ecb9 44#include <ctype.h>
bd5635a1 45
2093fe68
RP
46#ifndef O_BINARY
47#define O_BINARY 0
48#endif
49
30875e1c
SG
50/* Global variables owned by this file */
51
80d68b1d 52int readnow_symbol_files; /* Read full symbols immediately */
d47d5315 53
51b80b00
FF
54struct complaint oldsyms_complaint = {
55 "Replacing old symbols for `%s'", 0, 0
56};
57
58struct complaint empty_symtab_complaint = {
59 "Empty symbol table found for `%s'", 0, 0
60};
61
30875e1c 62/* External variables and functions referenced. */
bd5635a1 63
30875e1c 64extern int info_verbose;
bd5635a1
RP
65
66/* Functions this file defines */
7d9884b9 67
e58de8a2
FF
68static void
69set_initial_language PARAMS ((void));
70
30875e1c
SG
71static void
72load_command PARAMS ((char *, int));
73
74static void
75add_symbol_file_command PARAMS ((char *, int));
76
30875e1c
SG
77static void
78cashier_psymtab PARAMS ((struct partial_symtab *));
bd5635a1 79
30875e1c
SG
80static int
81compare_psymbols PARAMS ((const void *, const void *));
bd5635a1 82
30875e1c
SG
83static int
84compare_symbols PARAMS ((const void *, const void *));
85
b0246b3b
FF
86static bfd *
87symfile_bfd_open PARAMS ((char *));
30875e1c 88
80d68b1d
FF
89static void
90find_sym_fns PARAMS ((struct objfile *));
30875e1c 91
80d68b1d
FF
92/* List of all available sym_fns. On gdb startup, each object file reader
93 calls add_symtab_fns() to register information on each format it is
94 prepared to read. */
bd5635a1 95
80d68b1d 96static struct sym_fns *symtab_fns = NULL;
bd5635a1 97
bd5635a1
RP
98/* Structures with which to manage partial symbol allocation. */
99
100struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0};
101
61a7292f
SG
102/* Flag for whether user will be reloading symbols multiple times.
103 Defaults to ON for VxWorks, otherwise OFF. */
104
105#ifdef SYMBOL_RELOADING_DEFAULT
106int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
107#else
108int symbol_reloading = 0;
109#endif
110
bd5635a1 111\f
ade40d31 112/* Since this function is called from within qsort, in an ANSI environment
30875e1c
SG
113 it must conform to the prototype for qsort, which specifies that the
114 comparison function takes two "void *" pointers. */
bd5635a1
RP
115
116static int
30875e1c
SG
117compare_symbols (s1p, s2p)
118 const PTR s1p;
119 const PTR s2p;
bd5635a1 120{
30875e1c 121 register struct symbol **s1, **s2;
bd5635a1 122
30875e1c
SG
123 s1 = (struct symbol **) s1p;
124 s2 = (struct symbol **) s2p;
125
ade40d31 126 return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
bd5635a1
RP
127}
128
30875e1c
SG
129/*
130
131LOCAL FUNCTION
132
133 compare_psymbols -- compare two partial symbols by name
134
135DESCRIPTION
136
137 Given pointer to two partial symbol table entries, compare
138 them by name and return -N, 0, or +N (ala strcmp). Typically
139 used by sorting routines like qsort().
140
141NOTES
142
143 Does direct compare of first two characters before punting
144 and passing to strcmp for longer compares. Note that the
145 original version had a bug whereby two null strings or two
146 identically named one character strings would return the
147 comparison of memory following the null byte.
148
149 */
150
151static int
152compare_psymbols (s1p, s2p)
153 const PTR s1p;
154 const PTR s2p;
155{
156 register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p);
157 register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p);
158
159 if ((st1[0] - st2[0]) || !st1[0])
160 {
161 return (st1[0] - st2[0]);
162 }
163 else if ((st1[1] - st2[1]) || !st1[1])
164 {
165 return (st1[1] - st2[1]);
166 }
167 else
168 {
2e4964ad 169 return (STRCMP (st1 + 2, st2 + 2));
30875e1c
SG
170 }
171}
172
173void
174sort_pst_symbols (pst)
175 struct partial_symtab *pst;
176{
177 /* Sort the global list; don't sort the static list */
178
179 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
180 pst -> n_global_syms, sizeof (struct partial_symbol),
181 compare_psymbols);
182}
183
bd5635a1
RP
184/* Call sort_block_syms to sort alphabetically the symbols of one block. */
185
186void
187sort_block_syms (b)
188 register struct block *b;
189{
190 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
191 sizeof (struct symbol *), compare_symbols);
192}
193
194/* Call sort_symtab_syms to sort alphabetically
195 the symbols of each block of one symtab. */
196
197void
198sort_symtab_syms (s)
199 register struct symtab *s;
200{
c9bd6710
JG
201 register struct blockvector *bv;
202 int nbl;
bd5635a1
RP
203 int i;
204 register struct block *b;
205
c9bd6710
JG
206 if (s == 0)
207 return;
208 bv = BLOCKVECTOR (s);
209 nbl = BLOCKVECTOR_NBLOCKS (bv);
bd5635a1
RP
210 for (i = 0; i < nbl; i++)
211 {
212 b = BLOCKVECTOR_BLOCK (bv, i);
213 if (BLOCK_SHOULD_SORT (b))
214 sort_block_syms (b);
215 }
216}
217
bd5635a1
RP
218/* Make a copy of the string at PTR with SIZE characters in the symbol obstack
219 (and add a null character at the end in the copy).
220 Returns the address of the copy. */
221
222char *
30875e1c 223obsavestring (ptr, size, obstackp)
bd5635a1
RP
224 char *ptr;
225 int size;
30875e1c 226 struct obstack *obstackp;
bd5635a1 227{
30875e1c 228 register char *p = (char *) obstack_alloc (obstackp, size + 1);
ade40d31 229 /* Open-coded memcpy--saves function call time.
bd5635a1
RP
230 These strings are usually short. */
231 {
232 register char *p1 = ptr;
233 register char *p2 = p;
234 char *end = ptr + size;
235 while (p1 != end)
236 *p2++ = *p1++;
237 }
238 p[size] = 0;
239 return p;
240}
241
242/* Concatenate strings S1, S2 and S3; return the new string.
243 Space is found in the symbol_obstack. */
244
245char *
30875e1c
SG
246obconcat (obstackp, s1, s2, s3)
247 struct obstack *obstackp;
248 const char *s1, *s2, *s3;
bd5635a1
RP
249{
250 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
30875e1c 251 register char *val = (char *) obstack_alloc (obstackp, len);
bd5635a1
RP
252 strcpy (val, s1);
253 strcat (val, s2);
254 strcat (val, s3);
255 return val;
256}
bd5635a1
RP
257
258/* Get the symbol table that corresponds to a partial_symtab.
259 This is fast after the first time you do it. In fact, there
260 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
261 case inline. */
262
263struct symtab *
264psymtab_to_symtab (pst)
265 register struct partial_symtab *pst;
266{
bd5635a1
RP
267 /* If it's been looked up before, return it. */
268 if (pst->symtab)
269 return pst->symtab;
270
271 /* If it has not yet been read in, read it. */
272 if (!pst->readin)
273 {
274 (*pst->read_symtab) (pst);
275 }
276
61a7292f 277 return pst->symtab;
bd5635a1
RP
278}
279
bf349b77
FF
280/* Initialize entry point information for this objfile. */
281
282void
283init_entry_point_info (objfile)
284 struct objfile *objfile;
285{
286 /* Save startup file's range of PC addresses to help blockframe.c
287 decide where the bottom of the stack is. */
288
289 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
290 {
291 /* Executable file -- record its entry point so we'll recognize
292 the startup file because it contains the entry point. */
293 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
294 }
295 else
296 {
297 /* Examination of non-executable.o files. Short-circuit this stuff. */
298 /* ~0 will not be in any file, we hope. */
299 objfile -> ei.entry_point = ~0;
300 /* set the startup file to be an empty range. */
301 objfile -> ei.entry_file_lowpc = 0;
302 objfile -> ei.entry_file_highpc = 0;
303 }
304}
305
4d57c599
JK
306/* Get current entry point address. */
307
308CORE_ADDR
309entry_point_address()
310{
311 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
312}
313
a8e033f2
SG
314/* Remember the lowest-addressed loadable section we've seen.
315 This function is called via bfd_map_over_sections. */
316
317#if 0 /* Not used yet */
318static void
319find_lowest_section (abfd, sect, obj)
320 bfd *abfd;
321 asection *sect;
322 PTR obj;
323{
324 asection **lowest = (asection **)obj;
325
326 if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
327 return;
328 if (!*lowest)
329 *lowest = sect; /* First loadable section */
330 else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect))
331 *lowest = sect; /* A lower loadable section */
332}
333#endif
334
bd5635a1
RP
335/* Process a symbol file, as either the main file or as a dynamically
336 loaded file.
337
b3fdaf3d
JK
338 NAME is the file name (which will be tilde-expanded and made
339 absolute herein) (but we don't free or modify NAME itself).
340 FROM_TTY says how verbose to be. MAINLINE specifies whether this
341 is the main symbol file, or whether it's an extra symbol file such
342 as dynamically loaded code. If !mainline, ADDR is the address
4369a140
JG
343 where the text segment was loaded. If VERBO, the caller has printed
344 a verbose message about the symbol reading (and complaints can be
345 more terse about it). */
bd5635a1
RP
346
347void
4369a140 348syms_from_objfile (objfile, addr, mainline, verbo)
7d9884b9 349 struct objfile *objfile;
bd5635a1
RP
350 CORE_ADDR addr;
351 int mainline;
4369a140 352 int verbo;
bd5635a1 353{
a8e033f2
SG
354 struct section_offsets *section_offsets;
355 asection *lowest_sect;
ade40d31 356 struct cleanup *old_chain;
bd5635a1 357
bf349b77 358 init_entry_point_info (objfile);
80d68b1d 359 find_sym_fns (objfile);
bd5635a1 360
ade40d31
RP
361 /* Make sure that partially constructed symbol tables will be cleaned up
362 if an error occurs during symbol reading. */
363 old_chain = make_cleanup (free_objfile, objfile);
364
bd5635a1
RP
365 if (mainline)
366 {
ade40d31
RP
367 /* We will modify the main symbol table, make sure that all its users
368 will be cleaned up if an error occurs during symbol reading. */
369 make_cleanup (clear_symtab_users, 0);
370
bd5635a1
RP
371 /* Since no error yet, throw away the old symbol table. */
372
80d68b1d
FF
373 if (symfile_objfile != NULL)
374 {
375 free_objfile (symfile_objfile);
376 symfile_objfile = NULL;
377 }
bd5635a1 378
f6c4bf1a
JK
379 /* Currently we keep symbols from the add-symbol-file command.
380 If the user wants to get rid of them, they should do "symbol-file"
381 without arguments first. Not sure this is the best behavior
382 (PR 2207). */
383
80d68b1d 384 (*objfile -> sf -> sym_new_init) (objfile);
a8e033f2 385 }
bd5635a1 386
a8e033f2
SG
387 /* Convert addr into an offset rather than an absolute address.
388 We find the lowest address of a loaded segment in the objfile,
389 and assume that <addr> is where that got loaded. Due to historical
390 precedent, we warn if that doesn't happen to be the ".text"
391 segment. */
80d68b1d 392
a8e033f2
SG
393 if (mainline)
394 {
395 addr = 0; /* No offset from objfile addresses. */
396 }
397 else
398 {
399 lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
400#if 0
401 lowest_sect = 0;
402 bfd_map_over_sections (objfile->obfd, find_lowest_section,
403 (PTR) &lowest_sect);
404#endif
405
406 if (lowest_sect == 0)
407 warning ("no loadable sections found in added symbol-file %s",
408 objfile->name);
409 else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect)
2e4964ad 410 || !STREQ (".text",
a8e033f2 411 bfd_get_section_name (objfile->obfd, lowest_sect)))
c4a081e1 412 /* FIXME-32x64--assumes bfd_vma fits in long. */
4d57c599 413 warning ("Lowest section in %s is %s at 0x%lx",
a8e033f2
SG
414 objfile->name,
415 bfd_section_name (objfile->obfd, lowest_sect),
4d57c599 416 (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
a8e033f2
SG
417
418 if (lowest_sect)
419 addr -= bfd_section_vma (objfile->obfd, lowest_sect);
bd5635a1
RP
420 }
421
80d68b1d
FF
422 /* Initialize symbol reading routines for this objfile, allow complaints to
423 appear for this new file, and record how verbose to be, then do the
424 initial symbol reading for this file. */
4369a140 425
80d68b1d
FF
426 (*objfile -> sf -> sym_init) (objfile);
427 clear_complaints (1, verbo);
2093fe68 428
a8e033f2 429 section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
2093fe68
RP
430 objfile->section_offsets = section_offsets;
431
4365c36c
JK
432#ifndef IBM6000_TARGET
433 /* This is a SVR4/SunOS specific hack, I think. In any event, it
434 screws RS/6000. sym_offsets should be doing this sort of thing,
435 because it knows the mapping between bfd sections and
436 section_offsets. */
5aefc1ca
FF
437 /* This is a hack. As far as I can tell, section offsets are not
438 target dependent. They are all set to addr with a couple of
439 exceptions. The exceptions are sysvr4 shared libraries, whose
440 offsets are kept in solib structures anyway and rs6000 xcoff
441 which handles shared libraries in a completely unique way.
442
443 Section offsets are built similarly, except that they are built
444 by adding addr in all cases because there is no clear mapping
445 from section_offsets into actual sections. Note that solib.c
446 has a different algorythm for finding section offsets.
447
448 These should probably all be collapsed into some target
449 independent form of shared library support. FIXME. */
450
451 if (addr)
452 {
453 struct obj_section *s;
454
455 for (s = objfile->sections; s < objfile->sections_end; ++s)
456 {
457 s->addr -= s->offset;
458 s->addr += addr;
459 s->endaddr -= s->offset;
460 s->endaddr += addr;
461 s->offset += addr;
462 }
463 }
4365c36c 464#endif /* not IBM6000_TARGET */
5aefc1ca 465
a8e033f2 466 (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
bd5635a1 467
4d57c599
JK
468 /* Don't allow char * to have a typename (else would get caddr_t).
469 Ditto void *. FIXME: Check whether this is now done by all the
470 symbol readers themselves (many of them now do), and if so remove
471 it from here. */
bd5635a1
RP
472
473 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
474 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
475
9342ecb9
JG
476 /* Mark the objfile has having had initial symbol read attempted. Note
477 that this does not mean we found any symbols... */
478
479 objfile -> flags |= OBJF_SYMS;
ade40d31
RP
480
481 /* Discard cleanups as symbol reading was successful. */
482
483 discard_cleanups (old_chain);
9342ecb9
JG
484}
485
ade40d31 486/* Perform required actions after either reading in the initial
9342ecb9
JG
487 symbols for a new objfile, or mapping in the symbols from a reusable
488 objfile. */
489
490void
491new_symfile_objfile (objfile, mainline, verbo)
492 struct objfile *objfile;
493 int mainline;
494 int verbo;
495{
ade40d31
RP
496
497 /* If this is the main symbol file we have to clean up all users of the
498 old main symbol file. Otherwise it is sufficient to fixup all the
499 breakpoints that may have been redefined by this symbol file. */
bd5635a1
RP
500 if (mainline)
501 {
502 /* OK, make it the "real" symbol file. */
7d9884b9 503 symfile_objfile = objfile;
bd5635a1 504
ade40d31
RP
505 clear_symtab_users ();
506 }
507 else
508 {
509 breakpoint_re_set ();
510 }
4369a140
JG
511
512 /* We're done reading the symbol file; finish off complaints. */
80d68b1d 513 clear_complaints (0, verbo);
30875e1c 514}
d47d5315
JG
515
516/* Process a symbol file, as either the main file or as a dynamically
517 loaded file.
518
519 NAME is the file name (which will be tilde-expanded and made
520 absolute herein) (but we don't free or modify NAME itself).
521 FROM_TTY says how verbose to be. MAINLINE specifies whether this
522 is the main symbol file, or whether it's an extra symbol file such
523 as dynamically loaded code. If !mainline, ADDR is the address
30875e1c 524 where the text segment was loaded.
d47d5315 525
30875e1c
SG
526 Upon success, returns a pointer to the objfile that was added.
527 Upon failure, jumps back to command level (never returns). */
528
529struct objfile *
b0246b3b 530symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
d47d5315
JG
531 char *name;
532 int from_tty;
533 CORE_ADDR addr;
534 int mainline;
318bf84f 535 int mapped;
b0246b3b 536 int readnow;
d47d5315 537{
7d9884b9 538 struct objfile *objfile;
b0246b3b 539 struct partial_symtab *psymtab;
80d68b1d 540 bfd *abfd;
d47d5315 541
2093fe68
RP
542 /* Open a bfd for the file, and give user a chance to burp if we'd be
543 interactively wiping out any existing symbols. */
80d68b1d
FF
544
545 abfd = symfile_bfd_open (name);
d47d5315 546
80d68b1d
FF
547 if ((have_full_symbols () || have_partial_symbols ())
548 && mainline
549 && from_tty
550 && !query ("Load new symbol table from \"%s\"? ", name))
551 error ("Not confirmed.");
a8e033f2 552
80d68b1d
FF
553 objfile = allocate_objfile (abfd, mapped);
554
318bf84f
FF
555 /* If the objfile uses a mapped symbol file, and we have a psymtab for
556 it, then skip reading any symbols at this time. */
d47d5315 557
bf349b77 558 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
d47d5315 559 {
80d68b1d 560 /* We mapped in an existing symbol table file that already has had
bf349b77
FF
561 initial symbol reading performed, so we can skip that part. Notify
562 the user that instead of reading the symbols, they have been mapped.
563 */
318bf84f
FF
564 if (from_tty || info_verbose)
565 {
80d68b1d
FF
566 printf_filtered ("Mapped symbols for %s...", name);
567 wrap_here ("");
199b2450 568 gdb_flush (gdb_stdout);
318bf84f 569 }
9342ecb9
JG
570 init_entry_point_info (objfile);
571 find_sym_fns (objfile);
d47d5315 572 }
318bf84f 573 else
bd5635a1 574 {
80d68b1d 575 /* We either created a new mapped symbol table, mapped an existing
bf349b77
FF
576 symbol table file which has not had initial symbol reading
577 performed, or need to read an unmapped symbol table. */
318bf84f
FF
578 if (from_tty || info_verbose)
579 {
580 printf_filtered ("Reading symbols from %s...", name);
581 wrap_here ("");
199b2450 582 gdb_flush (gdb_stdout);
318bf84f 583 }
318bf84f 584 syms_from_objfile (objfile, addr, mainline, from_tty);
80d68b1d
FF
585 }
586
587 /* We now have at least a partial symbol table. Check to see if the
588 user requested that all symbols be read on initial access via either
589 the gdb startup command line or on a per symbol file basis. Expand
590 all partial symbol tables for this objfile if so. */
b0246b3b 591
bf349b77 592 if (readnow || readnow_symbol_files)
80d68b1d 593 {
318bf84f
FF
594 if (from_tty || info_verbose)
595 {
80d68b1d
FF
596 printf_filtered ("expanding to full symbols...");
597 wrap_here ("");
199b2450 598 gdb_flush (gdb_stdout);
318bf84f 599 }
80d68b1d
FF
600
601 for (psymtab = objfile -> psymtabs;
602 psymtab != NULL;
603 psymtab = psymtab -> next)
604 {
4ed3a9ea 605 psymtab_to_symtab (psymtab);
80d68b1d
FF
606 }
607 }
608
609 if (from_tty || info_verbose)
610 {
611 printf_filtered ("done.\n");
199b2450 612 gdb_flush (gdb_stdout);
bd5635a1 613 }
80d68b1d 614
ade40d31
RP
615 new_symfile_objfile (objfile, mainline, from_tty);
616
617 /* Getting new symbols may change our opinion about what is
618 frameless. */
619
620 reinit_frame_cache ();
621
30875e1c 622 return (objfile);
bd5635a1
RP
623}
624
625/* This is the symbol-file command. Read the file, analyze its symbols,
30875e1c 626 and add a struct symtab to a symtab list. */
bd5635a1
RP
627
628void
30875e1c
SG
629symbol_file_command (args, from_tty)
630 char *args;
bd5635a1
RP
631 int from_tty;
632{
30875e1c 633 char **argv;
b0246b3b 634 char *name = NULL;
30875e1c 635 struct cleanup *cleanups;
318bf84f 636 int mapped = 0;
30875e1c 637 int readnow = 0;
bd5635a1
RP
638
639 dont_repeat ();
640
30875e1c 641 if (args == NULL)
bd5635a1 642 {
cba0d141
JG
643 if ((have_full_symbols () || have_partial_symbols ())
644 && from_tty
645 && !query ("Discard symbol table from `%s'? ",
646 symfile_objfile -> name))
647 error ("Not confirmed.");
648 free_all_objfiles ();
30875e1c 649 symfile_objfile = NULL;
9342ecb9
JG
650 if (from_tty)
651 {
199b2450 652 printf_unfiltered ("No symbol file now.\n");
9342ecb9 653 }
bd5635a1 654 }
30875e1c
SG
655 else
656 {
657 if ((argv = buildargv (args)) == NULL)
658 {
318bf84f 659 nomem (0);
30875e1c
SG
660 }
661 cleanups = make_cleanup (freeargv, (char *) argv);
b0246b3b 662 while (*argv != NULL)
30875e1c 663 {
2e4964ad 664 if (STREQ (*argv, "-mapped"))
30875e1c 665 {
318bf84f 666 mapped = 1;
30875e1c 667 }
2e4964ad 668 else if (STREQ (*argv, "-readnow"))
30875e1c
SG
669 {
670 readnow = 1;
671 }
b0246b3b
FF
672 else if (**argv == '-')
673 {
674 error ("unknown option `%s'", *argv);
675 }
676 else
677 {
678 name = *argv;
679 }
680 argv++;
30875e1c 681 }
2403f49b 682
b0246b3b
FF
683 if (name == NULL)
684 {
685 error ("no symbol file name was specified");
686 }
687 else
30875e1c 688 {
4ed3a9ea 689 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped, readnow);
e58de8a2 690 set_initial_language ();
30875e1c
SG
691 }
692 do_cleanups (cleanups);
693 }
bd5635a1
RP
694}
695
e58de8a2
FF
696/* Set the initial language.
697
698 A better solution would be to record the language in the psymtab when reading
699 partial symbols, and then use it (if known) to set the language. This would
700 be a win for formats that encode the language in an easily discoverable place,
701 such as DWARF. For stabs, we can jump through hoops looking for specially
702 named symbols or try to intuit the language from the specific type of stabs
703 we find, but we can't do that until later when we read in full symbols.
704 FIXME. */
705
706static void
707set_initial_language ()
708{
709 struct partial_symtab *pst;
710 enum language lang = language_unknown;
711
712 pst = find_main_psymtab ();
713 if (pst != NULL)
714 {
715 if (pst -> filename != NULL)
716 {
717 lang = deduce_language_from_filename (pst -> filename);
718 }
719 if (lang == language_unknown)
720 {
721 /* Make C the default language */
722 lang = language_c;
723 }
724 set_language (lang);
725 expected_language = current_language; /* Don't warn the user */
726 }
727}
728
b0246b3b
FF
729/* Open file specified by NAME and hand it off to BFD for preliminary
730 analysis. Result is a newly initialized bfd *, which includes a newly
731 malloc'd` copy of NAME (tilde-expanded and made absolute).
7d9884b9 732 In case of trouble, error() is called. */
bd5635a1 733
b0246b3b
FF
734static bfd *
735symfile_bfd_open (name)
bd5635a1
RP
736 char *name;
737{
738 bfd *sym_bfd;
739 int desc;
740 char *absolute_name;
741
7d9884b9 742 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 743
7d9884b9 744 /* Look down path for it, allocate 2nd new malloc'd copy. */
2093fe68 745 desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
b0246b3b
FF
746 if (desc < 0)
747 {
748 make_cleanup (free, name);
749 perror_with_name (name);
750 }
7d9884b9 751 free (name); /* Free 1st new malloc'd copy */
30875e1c 752 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
346168a2 753 /* It'll be freed in free_objfile(). */
bd5635a1 754
ade40d31 755 sym_bfd = bfd_fdopenr (name, gnutarget, desc);
bd5635a1
RP
756 if (!sym_bfd)
757 {
758 close (desc);
7d9884b9 759 make_cleanup (free, name);
b0246b3b 760 error ("\"%s\": can't open to read symbols: %s.", name,
c4a081e1 761 bfd_errmsg (bfd_get_error ()));
bd5635a1 762 }
e58de8a2 763 sym_bfd->cacheable = true;
bd5635a1 764
b0246b3b
FF
765 if (!bfd_check_format (sym_bfd, bfd_object))
766 {
767 bfd_close (sym_bfd); /* This also closes desc */
768 make_cleanup (free, name);
769 error ("\"%s\": can't read symbols: %s.", name,
c4a081e1 770 bfd_errmsg (bfd_get_error ()));
b0246b3b 771 }
7d9884b9 772
b0246b3b 773 return (sym_bfd);
7d9884b9
JG
774}
775
80d68b1d
FF
776/* Link a new symtab_fns into the global symtab_fns list. Called on gdb
777 startup by the _initialize routine in each object file format reader,
778 to register information about each format the the reader is prepared
779 to handle. */
bd5635a1
RP
780
781void
782add_symtab_fns (sf)
783 struct sym_fns *sf;
784{
785 sf->next = symtab_fns;
786 symtab_fns = sf;
787}
788
789
790/* Initialize to read symbols from the symbol file sym_bfd. It either
80d68b1d
FF
791 returns or calls error(). The result is an initialized struct sym_fns
792 in the objfile structure, that contains cached information about the
793 symbol file. */
bd5635a1 794
80d68b1d
FF
795static void
796find_sym_fns (objfile)
7d9884b9 797 struct objfile *objfile;
bd5635a1 798{
ac88ca20 799 struct sym_fns *sf;
0eed42de 800 enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
c4a081e1 801 char *our_target = bfd_get_target (objfile -> obfd);
0eed42de
JK
802
803 /* Special kludge for RS/6000. See xcoffread.c. */
c4a081e1 804 if (STREQ (our_target, "aixcoff-rs6000"))
0eed42de 805 our_flavour = (enum bfd_flavour)-1;
bd5635a1 806
c4a081e1
DM
807 /* Special kludge for apollo. See dstread.c. */
808 if (STREQN (our_target, "apollo", 6))
809 our_flavour = (enum bfd_flavour)-2;
810
80d68b1d 811 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
bd5635a1 812 {
0eed42de 813 if (our_flavour == sf -> sym_flavour)
bd5635a1 814 {
80d68b1d
FF
815 objfile -> sf = sf;
816 return;
bd5635a1
RP
817 }
818 }
c9bd6710 819 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
b0246b3b 820 bfd_get_target (objfile -> obfd));
bd5635a1
RP
821}
822\f
823/* This function runs the load command of our current target. */
824
30875e1c 825static void
bd5635a1
RP
826load_command (arg, from_tty)
827 char *arg;
828 int from_tty;
829{
830 target_load (arg, from_tty);
831}
832
ade40d31
RP
833/* This version of "load" should be usable for any target. Currently
834 it is just used for remote targets, not inftarg.c or core files,
835 on the theory that only in that case is it useful.
836
837 Avoiding xmodem and the like seems like a win (a) because we don't have
838 to worry about finding it, and (b) On VMS, fork() is very slow and so
839 we don't want to run a subprocess. On the other hand, I'm not sure how
840 performance compares. */
841void
842generic_load (filename, from_tty)
843 char *filename;
844 int from_tty;
845{
846 struct cleanup *old_cleanups;
847 asection *s;
c4a081e1
DM
848 bfd *loadfile_bfd;
849
850 if (filename == NULL)
851 filename = get_exec_file (1);
852
853 loadfile_bfd = bfd_openr (filename, gnutarget);
ade40d31
RP
854 if (loadfile_bfd == NULL)
855 {
856 perror_with_name (filename);
857 return;
858 }
859 old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
860
861 if (!bfd_check_format (loadfile_bfd, bfd_object))
862 {
863 error ("\"%s\" is not an object file: %s", filename,
c4a081e1 864 bfd_errmsg (bfd_get_error ()));
ade40d31
RP
865 }
866
867 for (s = loadfile_bfd->sections; s; s = s->next)
868 {
869 if (s->flags & SEC_LOAD)
870 {
871 bfd_size_type size;
872
873 size = bfd_get_section_size_before_reloc (s);
874 if (size > 0)
875 {
876 char *buffer;
877 struct cleanup *old_chain;
878 bfd_vma vma;
879
880 buffer = xmalloc (size);
881 old_chain = make_cleanup (free, buffer);
882
883 vma = bfd_get_section_vma (loadfile_bfd, s);
884
885 /* Is this really necessary? I guess it gives the user something
886 to look at during a long download. */
c4a081e1 887 printf_filtered ("Loading section %s, size 0x%lx vma ",
ade40d31 888 bfd_get_section_name (loadfile_bfd, s),
c4a081e1
DM
889 (unsigned long) size);
890 print_address_numeric (vma, gdb_stdout);
891 printf_filtered ("\n");
ade40d31
RP
892
893 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
894
895 target_write_memory (vma, buffer, size);
896
897 do_cleanups (old_chain);
898 }
899 }
900 }
901
902 /* We were doing this in remote-mips.c, I suspect it is right
903 for other targets too. */
904 write_pc (loadfile_bfd->start_address);
905
906 /* FIXME: are we supposed to call symbol_file_add or not? According to
907 a comment from remote-mips.c (where a call to symbol_file_add was
908 commented out), making the call confuses GDB if more than one file is
909 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
910 does. */
911
912 do_cleanups (old_cleanups);
913}
914
61a7292f
SG
915/* This function allows the addition of incrementally linked object files.
916 It does not modify any state in the target, only in the debugger. */
bd5635a1 917
e1ce8aa5 918/* ARGSUSED */
30875e1c 919static void
b0246b3b
FF
920add_symbol_file_command (args, from_tty)
921 char *args;
bd5635a1
RP
922 int from_tty;
923{
b0246b3b 924 char *name = NULL;
bd5635a1 925 CORE_ADDR text_addr;
b0246b3b 926 char *arg;
ac88ca20
JG
927 int readnow = 0;
928 int mapped = 0;
bd5635a1 929
b0246b3b 930 dont_repeat ();
61a7292f 931
b0246b3b
FF
932 if (args == NULL)
933 {
934 error ("add-symbol-file takes a file name and an address");
935 }
bd5635a1 936
b0246b3b 937 /* Make a copy of the string that we can safely write into. */
bd5635a1 938
b0246b3b
FF
939 args = strdup (args);
940 make_cleanup (free, args);
941
942 /* Pick off any -option args and the file name. */
943
944 while ((*args != '\000') && (name == NULL))
945 {
946 while (isspace (*args)) {args++;}
947 arg = args;
948 while ((*args != '\000') && !isspace (*args)) {args++;}
949 if (*args != '\000')
950 {
951 *args++ = '\000';
952 }
953 if (*arg != '-')
954 {
955 name = arg;
956 }
2e4964ad 957 else if (STREQ (arg, "-mapped"))
b0246b3b
FF
958 {
959 mapped = 1;
960 }
2e4964ad 961 else if (STREQ (arg, "-readnow"))
b0246b3b
FF
962 {
963 readnow = 1;
964 }
965 else
966 {
967 error ("unknown option `%s'", arg);
968 }
969 }
bd5635a1 970
b0246b3b
FF
971 /* After picking off any options and the file name, args should be
972 left pointing at the remainder of the command line, which should
973 be the address expression to evaluate. */
bd5635a1 974
b0246b3b
FF
975 if ((name == NULL) || (*args == '\000') )
976 {
977 error ("add-symbol-file takes a file name and an address");
978 }
979 name = tilde_expand (name);
980 make_cleanup (free, name);
bd5635a1 981
b0246b3b 982 text_addr = parse_and_eval_address (args);
bd5635a1 983
c4a081e1 984 /* FIXME-32x64: Assumes text_addr fits in a long. */
d8ce1326 985 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
4d57c599 986 name, local_hex_string ((unsigned long)text_addr)))
bd5635a1
RP
987 error ("Not confirmed.");
988
4ed3a9ea 989 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
bd5635a1
RP
990}
991\f
7d9884b9 992/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
993void
994reread_symbols ()
995{
7d9884b9
JG
996 struct objfile *objfile;
997 long new_modtime;
998 int reread_one = 0;
cba0d141
JG
999 struct stat new_statbuf;
1000 int res;
bd5635a1
RP
1001
1002 /* With the addition of shared libraries, this should be modified,
1003 the load time should be saved in the partial symbol tables, since
1004 different tables may come from different source files. FIXME.
1005 This routine should then walk down each partial symbol table
30875e1c 1006 and see if the symbol table that it originates from has been changed */
bd5635a1 1007
7d9884b9
JG
1008 for (objfile = object_files; objfile; objfile = objfile->next) {
1009 if (objfile->obfd) {
1eeba686 1010#ifdef IBM6000_TARGET
318bf84f
FF
1011 /* If this object is from a shared library, then you should
1012 stat on the library name, not member name. */
1013
1014 if (objfile->obfd->my_archive)
1015 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1016 else
1017#endif
cba0d141
JG
1018 res = stat (objfile->name, &new_statbuf);
1019 if (res != 0) {
1020 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1021 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1022 objfile->name);
1023 continue;
1024 }
1025 new_modtime = new_statbuf.st_mtime;
4d57c599
JK
1026 if (new_modtime != objfile->mtime)
1027 {
1028 struct cleanup *old_cleanups;
1029 struct section_offsets *offsets;
1030 int num_offsets;
1031 int section_offsets_size;
1032
1033 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1034 objfile->name);
1035
1036 /* There are various functions like symbol_file_add,
1037 symfile_bfd_open, syms_from_objfile, etc., which might
1038 appear to do what we want. But they have various other
1039 effects which we *don't* want. So we just do stuff
1040 ourselves. We don't worry about mapped files (for one thing,
1041 any mapped file will be out of date). */
1042
1043 /* If we get an error, blow away this objfile (not sure if
1044 that is the correct response for things like shared
1045 libraries). */
1046 old_cleanups = make_cleanup (free_objfile, objfile);
1047 /* We need to do this whenever any symbols go away. */
1048 make_cleanup (clear_symtab_users, 0);
1049
1050 /* Clean up any state BFD has sitting around. We don't need
1051 to close the descriptor but BFD lacks a way of closing the
1052 BFD without closing the descriptor. */
1053 if (!bfd_close (objfile->obfd))
1054 error ("Can't close BFD for %s.", objfile->name);
1055 objfile->obfd = bfd_openr (objfile->name, gnutarget);
1056 if (objfile->obfd == NULL)
1057 error ("Can't open %s to read symbols.", objfile->name);
1058 /* bfd_openr sets cacheable to true, which is what we want. */
1059 if (!bfd_check_format (objfile->obfd, bfd_object))
1060 error ("Can't read symbols from %s: %s.", objfile->name,
c4a081e1 1061 bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1062
1063 /* Save the offsets, we will nuke them with the rest of the
1064 psymbol_obstack. */
1065 num_offsets = objfile->num_sections;
1066 section_offsets_size =
1067 sizeof (struct section_offsets)
1068 + sizeof (objfile->section_offsets->offsets) * num_offsets;
1069 offsets = (struct section_offsets *) alloca (section_offsets_size);
1070 memcpy (offsets, objfile->section_offsets, section_offsets_size);
1071
1072 /* Nuke all the state that we will re-read. Much of the following
1073 code which sets things to NULL really is necessary to tell
1074 other parts of GDB that there is nothing currently there. */
1075
1076 /* FIXME: Do we have to free a whole linked list, or is this
1077 enough? */
1078 if (objfile->global_psymbols.list)
1079 mfree (objfile->md, objfile->global_psymbols.list);
1080 objfile->global_psymbols.list = NULL;
1081 objfile->global_psymbols.size = 0;
1082 if (objfile->static_psymbols.list)
1083 mfree (objfile->md, objfile->static_psymbols.list);
1084 objfile->static_psymbols.list = NULL;
1085 objfile->static_psymbols.size = 0;
1086
1087 /* Free the obstacks for non-reusable objfiles */
1088 obstack_free (&objfile -> psymbol_obstack, 0);
1089 obstack_free (&objfile -> symbol_obstack, 0);
1090 obstack_free (&objfile -> type_obstack, 0);
1091 objfile->sections = NULL;
1092 objfile->symtabs = NULL;
1093 objfile->psymtabs = NULL;
1094 objfile->free_psymtabs = NULL;
1095 objfile->msymbols = NULL;
1096 objfile->minimal_symbol_count= 0;
1097 objfile->fundamental_types = NULL;
1098 if (objfile -> sf != NULL)
1099 {
1100 (*objfile -> sf -> sym_finish) (objfile);
1101 }
1102
1103 /* We never make this a mapped file. */
1104 objfile -> md = NULL;
1105 /* obstack_specify_allocation also initializes the obstack so
1106 it is empty. */
1107 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1108 xmalloc, free);
1109 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1110 xmalloc, free);
1111 obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1112 xmalloc, free);
1113 if (build_objfile_section_table (objfile))
1114 {
1115 error ("Can't find the file sections in `%s': %s",
c4a081e1 1116 objfile -> name, bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1117 }
1118
1119 /* We use the same section offsets as from last time. I'm not
1120 sure whether that is always correct for shared libraries. */
1121 objfile->section_offsets = (struct section_offsets *)
1122 obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1123 memcpy (objfile->section_offsets, offsets, section_offsets_size);
1124 objfile->num_sections = num_offsets;
1125
1126 /* What the hell is sym_new_init for, anyway? The concept of
1127 distinguishing between the main file and additional files
1128 in this way seems rather dubious. */
1129 if (objfile == symfile_objfile)
1130 (*objfile->sf->sym_new_init) (objfile);
1131
1132 (*objfile->sf->sym_init) (objfile);
1133 clear_complaints (1, 1);
1134 /* The "mainline" parameter is a hideous hack; I think leaving it
1135 zero is OK since dbxread.c also does what it needs to do if
1136 objfile->global_psymbols.size is 0. */
1137 (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
1138 objfile -> flags |= OBJF_SYMS;
1139
1140 /* We're done reading the symbol file; finish off complaints. */
1141 clear_complaints (0, 1);
1142
1143 /* Getting new symbols may change our opinion about what is
1144 frameless. */
1145
1146 reinit_frame_cache ();
1147
1148 /* Discard cleanups as symbol reading was successful. */
1149 discard_cleanups (old_cleanups);
1150
1151 /* If the mtime has changed between the time we set new_modtime
1152 and now, we *want* this to be out of date, so don't call stat
1153 again now. */
1154 objfile->mtime = new_modtime;
1155 reread_one = 1;
1156 }
bd5635a1 1157 }
7d9884b9
JG
1158 }
1159
1160 if (reread_one)
4d57c599 1161 clear_symtab_users ();
bd5635a1 1162}
bd5635a1 1163
bd5635a1 1164\f
7d9884b9
JG
1165enum language
1166deduce_language_from_filename (filename)
1167 char *filename;
1168{
2093fe68 1169 char *c;
7d9884b9 1170
2093fe68
RP
1171 if (0 == filename)
1172 ; /* Get default */
1173 else if (0 == (c = strrchr (filename, '.')))
1174 ; /* Get default. */
4d57c599 1175 else if (STREQ(c,".mod"))
2093fe68 1176 return language_m2;
4d57c599 1177 else if (STREQ(c,".c"))
2093fe68 1178 return language_c;
4d57c599 1179 else if (STREQ (c,".cc") || STREQ (c,".C") || STREQ (c, ".cxx"))
2093fe68 1180 return language_cplus;
4d57c599 1181 else if (STREQ (c,".ch") || STREQ (c,".c186") || STREQ (c,".c286"))
2093fe68 1182 return language_chill;
7d9884b9
JG
1183
1184 return language_unknown; /* default */
1185}
1186\f
d8ce1326
JG
1187/* allocate_symtab:
1188
1189 Allocate and partly initialize a new symbol table. Return a pointer
1190 to it. error() if no space.
1191
1192 Caller must set these fields:
1193 LINETABLE(symtab)
1194 symtab->blockvector
d8ce1326
JG
1195 symtab->dirname
1196 symtab->free_code
1197 symtab->free_ptr
1198 initialize any EXTRA_SYMTAB_INFO
1199 possibly free_named_symtabs (symtab->filename);
d8ce1326
JG
1200 */
1201
1202struct symtab *
30875e1c
SG
1203allocate_symtab (filename, objfile)
1204 char *filename;
1205 struct objfile *objfile;
d8ce1326
JG
1206{
1207 register struct symtab *symtab;
d8ce1326 1208
30875e1c
SG
1209 symtab = (struct symtab *)
1210 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
4ed3a9ea 1211 memset (symtab, 0, sizeof (*symtab));
30875e1c
SG
1212 symtab -> filename = obsavestring (filename, strlen (filename),
1213 &objfile -> symbol_obstack);
1214 symtab -> fullname = NULL;
1215 symtab -> language = deduce_language_from_filename (filename);
d8ce1326 1216
7d9884b9 1217 /* Hook it to the objfile it comes from */
30875e1c
SG
1218
1219 symtab -> objfile = objfile;
1220 symtab -> next = objfile -> symtabs;
1221 objfile -> symtabs = symtab;
7d9884b9
JG
1222
1223#ifdef INIT_EXTRA_SYMTAB_INFO
30875e1c 1224 INIT_EXTRA_SYMTAB_INFO (symtab);
7d9884b9 1225#endif
d8ce1326 1226
30875e1c 1227 return (symtab);
d8ce1326 1228}
30875e1c
SG
1229
1230struct partial_symtab *
1231allocate_psymtab (filename, objfile)
1232 char *filename;
1233 struct objfile *objfile;
1234{
1235 struct partial_symtab *psymtab;
1236
cba0d141
JG
1237 if (objfile -> free_psymtabs)
1238 {
1239 psymtab = objfile -> free_psymtabs;
1240 objfile -> free_psymtabs = psymtab -> next;
1241 }
1242 else
1243 psymtab = (struct partial_symtab *)
1244 obstack_alloc (&objfile -> psymbol_obstack,
1245 sizeof (struct partial_symtab));
1246
4ed3a9ea 1247 memset (psymtab, 0, sizeof (struct partial_symtab));
30875e1c
SG
1248 psymtab -> filename = obsavestring (filename, strlen (filename),
1249 &objfile -> psymbol_obstack);
1250 psymtab -> symtab = NULL;
1251
1252 /* Hook it to the objfile it comes from */
1253
1254 psymtab -> objfile = objfile;
1255 psymtab -> next = objfile -> psymtabs;
1256 objfile -> psymtabs = psymtab;
1257
1258 return (psymtab);
1259}
1260
d8ce1326 1261\f
ade40d31
RP
1262/* Reset all data structures in gdb which may contain references to symbol
1263 table date. */
1264
1265void
1266clear_symtab_users ()
1267{
1268 /* Someday, we should do better than this, by only blowing away
1269 the things that really need to be blown. */
1270 clear_value_history ();
1271 clear_displays ();
1272 clear_internalvars ();
1273 breakpoint_re_set ();
1274 set_default_breakpoint (0, 0, 0, 0);
1275 current_source_symtab = 0;
1276 current_source_line = 0;
4d57c599 1277 clear_pc_function_cache ();
ade40d31
RP
1278}
1279
9d199712
JG
1280/* clear_symtab_users_once:
1281
1282 This function is run after symbol reading, or from a cleanup.
1283 If an old symbol table was obsoleted, the old symbol table
1284 has been blown away, but the other GDB data structures that may
1285 reference it have not yet been cleared or re-directed. (The old
1286 symtab was zapped, and the cleanup queued, in free_named_symtab()
1287 below.)
1288
1289 This function can be queued N times as a cleanup, or called
1290 directly; it will do all the work the first time, and then will be a
1291 no-op until the next time it is queued. This works by bumping a
1292 counter at queueing time. Much later when the cleanup is run, or at
1293 the end of symbol processing (in case the cleanup is discarded), if
1294 the queued count is greater than the "done-count", we do the work
1295 and set the done-count to the queued count. If the queued count is
1296 less than or equal to the done-count, we just ignore the call. This
1297 is needed because reading a single .o file will often replace many
1298 symtabs (one per .h file, for example), and we don't want to reset
1299 the breakpoints N times in the user's face.
1300
1301 The reason we both queue a cleanup, and call it directly after symbol
1302 reading, is because the cleanup protects us in case of errors, but is
1303 discarded if symbol reading is successful. */
1304
ade40d31 1305#if 0
996ccb30
JK
1306/* FIXME: As free_named_symtabs is currently a big noop this function
1307 is no longer needed. */
ade40d31
RP
1308static void
1309clear_symtab_users_once PARAMS ((void));
1310
9d199712
JG
1311static int clear_symtab_users_queued;
1312static int clear_symtab_users_done;
1313
ade40d31 1314static void
9d199712
JG
1315clear_symtab_users_once ()
1316{
1317 /* Enforce once-per-`do_cleanups'-semantics */
1318 if (clear_symtab_users_queued <= clear_symtab_users_done)
1319 return;
1320 clear_symtab_users_done = clear_symtab_users_queued;
1321
ade40d31 1322 clear_symtab_users ();
9d199712 1323}
ade40d31 1324#endif
9d199712
JG
1325
1326/* Delete the specified psymtab, and any others that reference it. */
1327
e1ce8aa5 1328static void
9d199712
JG
1329cashier_psymtab (pst)
1330 struct partial_symtab *pst;
1331{
46c28185 1332 struct partial_symtab *ps, *pprev = NULL;
9d199712
JG
1333 int i;
1334
1335 /* Find its previous psymtab in the chain */
30875e1c 1336 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1337 if (ps == pst)
1338 break;
1339 pprev = ps;
1340 }
1341
1342 if (ps) {
1343 /* Unhook it from the chain. */
30875e1c
SG
1344 if (ps == pst->objfile->psymtabs)
1345 pst->objfile->psymtabs = ps->next;
9d199712
JG
1346 else
1347 pprev->next = ps->next;
1348
1349 /* FIXME, we can't conveniently deallocate the entries in the
1350 partial_symbol lists (global_psymbols/static_psymbols) that
1351 this psymtab points to. These just take up space until all
1352 the psymtabs are reclaimed. Ditto the dependencies list and
1353 filename, which are all in the psymbol_obstack. */
1354
1355 /* We need to cashier any psymtab that has this one as a dependency... */
1356again:
30875e1c 1357 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1358 for (i = 0; i < ps->number_of_dependencies; i++) {
1359 if (ps->dependencies[i] == pst) {
1360 cashier_psymtab (ps);
1361 goto again; /* Must restart, chain has been munged. */
1362 }
1363 }
1364 }
1365 }
1366}
1367
1368/* If a symtab or psymtab for filename NAME is found, free it along
1369 with any dependent breakpoints, displays, etc.
1370 Used when loading new versions of object modules with the "add-file"
1371 command. This is only called on the top-level symtab or psymtab's name;
1372 it is not called for subsidiary files such as .h files.
1373
1374 Return value is 1 if we blew away the environment, 0 if not.
30875e1c 1375 FIXME. The return valu appears to never be used.
9d199712
JG
1376
1377 FIXME. I think this is not the best way to do this. We should
1378 work on being gentler to the environment while still cleaning up
1379 all stray pointers into the freed symtab. */
1380
1381int
1382free_named_symtabs (name)
1383 char *name;
1384{
30875e1c
SG
1385#if 0
1386 /* FIXME: With the new method of each objfile having it's own
1387 psymtab list, this function needs serious rethinking. In particular,
1388 why was it ever necessary to toss psymtabs with specific compilation
1389 unit filenames, as opposed to all psymtabs from a particular symbol
ac88ca20
JG
1390 file? -- fnf
1391 Well, the answer is that some systems permit reloading of particular
1392 compilation units. We want to blow away any old info about these
1393 compilation units, regardless of which objfiles they arrived in. --gnu. */
1394
1395 register struct symtab *s;
1396 register struct symtab *prev;
1397 register struct partial_symtab *ps;
1398 struct blockvector *bv;
1399 int blewit = 0;
30875e1c 1400
61a7292f
SG
1401 /* We only wack things if the symbol-reload switch is set. */
1402 if (!symbol_reloading)
1403 return 0;
1404
d11c44f1
JG
1405 /* Some symbol formats have trouble providing file names... */
1406 if (name == 0 || *name == '\0')
1407 return 0;
1408
9d199712
JG
1409 /* Look for a psymtab with the specified name. */
1410
1411again2:
1412 for (ps = partial_symtab_list; ps; ps = ps->next) {
2e4964ad 1413 if (STREQ (name, ps->filename)) {
9d199712
JG
1414 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1415 goto again2; /* Must restart, chain has been munged */
1416 }
1417 }
1418
1419 /* Look for a symtab with the specified name. */
1420
1421 for (s = symtab_list; s; s = s->next)
1422 {
2e4964ad 1423 if (STREQ (name, s->filename))
9d199712
JG
1424 break;
1425 prev = s;
1426 }
1427
1428 if (s)
1429 {
1430 if (s == symtab_list)
1431 symtab_list = s->next;
1432 else
1433 prev->next = s->next;
1434
1435 /* For now, queue a delete for all breakpoints, displays, etc., whether
1436 or not they depend on the symtab being freed. This should be
1437 changed so that only those data structures affected are deleted. */
1438
1439 /* But don't delete anything if the symtab is empty.
1440 This test is necessary due to a bug in "dbxread.c" that
1441 causes empty symtabs to be created for N_SO symbols that
1442 contain the pathname of the object file. (This problem
1443 has been fixed in GDB 3.9x). */
1444
c9bd6710
JG
1445 bv = BLOCKVECTOR (s);
1446 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1447 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1448 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1449 {
1450 complain (&oldsyms_complaint, name);
1451
1452 clear_symtab_users_queued++;
1453 make_cleanup (clear_symtab_users_once, 0);
1454 blewit = 1;
1455 } else {
1456 complain (&empty_symtab_complaint, name);
1457 }
1458
1459 free_symtab (s);
1460 }
1461 else
d8ce1326
JG
1462 {
1463 /* It is still possible that some breakpoints will be affected
1464 even though no symtab was found, since the file might have
1465 been compiled without debugging, and hence not be associated
1466 with a symtab. In order to handle this correctly, we would need
1467 to keep a list of text address ranges for undebuggable files.
1468 For now, we do nothing, since this is a fairly obscure case. */
1469 ;
1470 }
9d199712 1471
30875e1c 1472 /* FIXME, what about the minimal symbol table? */
9d199712 1473 return blewit;
30875e1c
SG
1474#else
1475 return (0);
1476#endif
9d199712
JG
1477}
1478\f
d4ea2aba
PB
1479/* Allocate and partially fill a partial symtab. It will be
1480 completely filled at the end of the symbol list.
1481
1482 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1483 is the address relative to which its symbols are (incremental) or 0
1484 (normal). */
1485
1486
1487struct partial_symtab *
a8e033f2 1488start_psymtab_common (objfile, section_offsets,
d4ea2aba
PB
1489 filename, textlow, global_syms, static_syms)
1490 struct objfile *objfile;
a8e033f2 1491 struct section_offsets *section_offsets;
d4ea2aba
PB
1492 char *filename;
1493 CORE_ADDR textlow;
1494 struct partial_symbol *global_syms;
1495 struct partial_symbol *static_syms;
1496{
30875e1c
SG
1497 struct partial_symtab *psymtab;
1498
1499 psymtab = allocate_psymtab (filename, objfile);
a8e033f2 1500 psymtab -> section_offsets = section_offsets;
30875e1c
SG
1501 psymtab -> textlow = textlow;
1502 psymtab -> texthigh = psymtab -> textlow; /* default */
1503 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1504 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1505 return (psymtab);
7d9884b9 1506}
9342ecb9
JG
1507\f
1508/* Debugging versions of functions that are usually inline macros
1509 (see symfile.h). */
1510
2e4964ad 1511#if !INLINE_ADD_PSYMBOL
9342ecb9
JG
1512
1513/* Add a symbol with a long value to a psymtab.
1514 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1515
1516void
2e4964ad
FF
1517add_psymbol_to_list (name, namelength, namespace, class, list, val, language,
1518 objfile)
9342ecb9
JG
1519 char *name;
1520 int namelength;
1521 enum namespace namespace;
1522 enum address_class class;
1523 struct psymbol_allocation_list *list;
1524 long val;
2e4964ad
FF
1525 enum language language;
1526 struct objfile *objfile;
9342ecb9 1527{
2e4964ad
FF
1528 register struct partial_symbol *psym;
1529 register char *demangled_name;
1530
1531 if (list->next >= list->list + list->size)
1532 {
1533 extend_psymbol_list (list,objfile);
1534 }
1535 psym = list->next++;
1536
1537 SYMBOL_NAME (psym) =
1538 (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
1539 memcpy (SYMBOL_NAME (psym), name, namelength);
1540 SYMBOL_NAME (psym)[namelength] = '\0';
1541 SYMBOL_VALUE (psym) = val;
1542 SYMBOL_LANGUAGE (psym) = language;
1543 PSYMBOL_NAMESPACE (psym) = namespace;
1544 PSYMBOL_CLASS (psym) = class;
7532cf10 1545 SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack);
9342ecb9
JG
1546}
1547
1548/* Add a symbol with a CORE_ADDR value to a psymtab. */
1549
1550void
2e4964ad
FF
1551add_psymbol_addr_to_list (name, namelength, namespace, class, list, val,
1552 language, objfile)
9342ecb9
JG
1553 char *name;
1554 int namelength;
1555 enum namespace namespace;
1556 enum address_class class;
1557 struct psymbol_allocation_list *list;
1558 CORE_ADDR val;
2e4964ad
FF
1559 enum language language;
1560 struct objfile *objfile;
9342ecb9 1561{
2e4964ad
FF
1562 register struct partial_symbol *psym;
1563 register char *demangled_name;
1564
1565 if (list->next >= list->list + list->size)
1566 {
1567 extend_psymbol_list (list,objfile);
1568 }
1569 psym = list->next++;
1570
1571 SYMBOL_NAME (psym) =
1572 (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1);
1573 memcpy (SYMBOL_NAME (psym), name, namelength);
1574 SYMBOL_NAME (psym)[namelength] = '\0';
1575 SYMBOL_VALUE_ADDRESS (psym) = val;
1576 SYMBOL_LANGUAGE (psym) = language;
1577 PSYMBOL_NAMESPACE (psym) = namespace;
1578 PSYMBOL_CLASS (psym) = class;
7532cf10 1579 SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack);
9342ecb9 1580}
7d9884b9 1581
2e4964ad
FF
1582#endif /* !INLINE_ADD_PSYMBOL */
1583
7d9884b9 1584\f
bd5635a1
RP
1585void
1586_initialize_symfile ()
1587{
ade40d31
RP
1588 struct cmd_list_element *c;
1589
1590 c = add_cmd ("symbol-file", class_files, symbol_file_command,
30875e1c 1591 "Load symbol table from executable file FILE.\n\
bd5635a1 1592The `file' command can also load symbol tables, as well as setting the file\n\
ade40d31
RP
1593to execute.", &cmdlist);
1594 c->completer = filename_completer;
bd5635a1 1595
ade40d31 1596 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
bd5635a1 1597 "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
ade40d31
RP
1598The second argument provides the starting address of the file's text.",
1599 &cmdlist);
1600 c->completer = filename_completer;
bd5635a1 1601
ade40d31 1602 c = add_cmd ("load", class_files, load_command,
bd5635a1 1603 "Dynamically load FILE into the running program, and record its symbols\n\
ade40d31
RP
1604for access from GDB.", &cmdlist);
1605 c->completer = filename_completer;
bd5635a1 1606
61a7292f
SG
1607 add_show_from_set
1608 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
1609 (char *)&symbol_reloading,
1610 "Set dynamic symbol table reloading multiple times in one run.",
1611 &setlist),
1612 &showlist);
1613
bd5635a1 1614}
This page took 0.241996 seconds and 4 git commands to generate.