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