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