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