Thu Apr 23 12:27:43 1998 Philippe De Muyter <phdm@macqel.be>
[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 */
e74acce4 37#include "gdb-stabs.h"
2dd30c72 38#include "obstack.h"
bd5635a1 39
e74acce4 40#include <assert.h>
bd5635a1
RP
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>
b52cac6b 46#include <time.h>
1a494973
C
47#ifdef HAVE_UNISTD_H
48#include <unistd.h>
49#endif
bd5635a1 50
2093fe68
RP
51#ifndef O_BINARY
52#define O_BINARY 0
53#endif
54
77636dea 55int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
74d6ac44
JM
56void (*pre_add_symbol_hook) PARAMS ((char *));
57void (*post_add_symbol_hook) PARAMS ((void));
77636dea 58
30875e1c 59/* Global variables owned by this file */
80d68b1d 60int readnow_symbol_files; /* Read full symbols immediately */
d47d5315 61
51b80b00
FF
62struct complaint oldsyms_complaint = {
63 "Replacing old symbols for `%s'", 0, 0
64};
65
66struct complaint empty_symtab_complaint = {
67 "Empty symbol table found for `%s'", 0, 0
68};
69
30875e1c 70/* External variables and functions referenced. */
bd5635a1 71
30875e1c 72extern int info_verbose;
bd5635a1 73
72158e71
SS
74extern void report_transfer_performance PARAMS ((unsigned long,
75 time_t, time_t));
76
bd5635a1 77/* Functions this file defines */
7d9884b9 78
40b647e9
FF
79#if 0
80static int simple_read_overlay_region_table PARAMS ((void));
81static void simple_free_overlay_region_table PARAMS ((void));
82#endif
83
72158e71 84static void set_initial_language PARAMS ((void));
e58de8a2 85
72158e71 86static void load_command PARAMS ((char *, int));
30875e1c 87
72158e71 88static void add_symbol_file_command PARAMS ((char *, int));
30875e1c 89
72158e71 90static void add_shared_symbol_files_command PARAMS ((char *, int));
f3806e3b 91
72158e71 92static void cashier_psymtab PARAMS ((struct partial_symtab *));
bd5635a1 93
72158e71 94static int compare_psymbols PARAMS ((const void *, const void *));
bd5635a1 95
72158e71 96static int compare_symbols PARAMS ((const void *, const void *));
30875e1c 97
72158e71 98static bfd *symfile_bfd_open PARAMS ((char *));
30875e1c 99
72158e71 100static void find_sym_fns PARAMS ((struct objfile *));
30875e1c 101
b607efe7
FF
102static void decrement_reading_symtab PARAMS ((void *));
103
80d68b1d
FF
104/* List of all available sym_fns. On gdb startup, each object file reader
105 calls add_symtab_fns() to register information on each format it is
106 prepared to read. */
bd5635a1 107
80d68b1d 108static struct sym_fns *symtab_fns = NULL;
bd5635a1 109
61a7292f
SG
110/* Flag for whether user will be reloading symbols multiple times.
111 Defaults to ON for VxWorks, otherwise OFF. */
112
113#ifdef SYMBOL_RELOADING_DEFAULT
114int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
115#else
116int symbol_reloading = 0;
117#endif
118
dee7a11c 119/* If true, then shared library symbols will be added automatically
f1d39876
FF
120 when the inferior is created, new libraries are loaded, or when
121 attaching to the inferior. This is almost always what users
dee7a11c
FF
122 will want to have happen; but for very large programs, the startup
123 time will be excessive, and so if this is a problem, the user can
124 clear this flag and then add the shared library symbols as needed.
125 Note that there is a potential for confusion, since if the shared
126 library symbols are not loaded, commands like "info fun" will *not*
127 report all the functions that are actually present. */
128
f1d39876 129int auto_solib_add = 1;
dee7a11c 130
bd5635a1 131\f
ade40d31 132/* Since this function is called from within qsort, in an ANSI environment
30875e1c
SG
133 it must conform to the prototype for qsort, which specifies that the
134 comparison function takes two "void *" pointers. */
bd5635a1
RP
135
136static int
30875e1c
SG
137compare_symbols (s1p, s2p)
138 const PTR s1p;
139 const PTR s2p;
bd5635a1 140{
30875e1c 141 register struct symbol **s1, **s2;
bd5635a1 142
30875e1c
SG
143 s1 = (struct symbol **) s1p;
144 s2 = (struct symbol **) s2p;
145
ade40d31 146 return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)));
bd5635a1
RP
147}
148
30875e1c
SG
149/*
150
151LOCAL FUNCTION
152
153 compare_psymbols -- compare two partial symbols by name
154
155DESCRIPTION
156
2ad5709f
FF
157 Given pointers to pointers to two partial symbol table entries,
158 compare them by name and return -N, 0, or +N (ala strcmp).
159 Typically used by sorting routines like qsort().
30875e1c
SG
160
161NOTES
162
163 Does direct compare of first two characters before punting
164 and passing to strcmp for longer compares. Note that the
165 original version had a bug whereby two null strings or two
166 identically named one character strings would return the
167 comparison of memory following the null byte.
168
169 */
170
171static int
172compare_psymbols (s1p, s2p)
173 const PTR s1p;
174 const PTR s2p;
175{
2ad5709f
FF
176 register char *st1 = SYMBOL_NAME (*(struct partial_symbol **) s1p);
177 register char *st2 = SYMBOL_NAME (*(struct partial_symbol **) s2p);
30875e1c
SG
178
179 if ((st1[0] - st2[0]) || !st1[0])
180 {
181 return (st1[0] - st2[0]);
182 }
183 else if ((st1[1] - st2[1]) || !st1[1])
184 {
185 return (st1[1] - st2[1]);
186 }
187 else
188 {
2e4964ad 189 return (STRCMP (st1 + 2, st2 + 2));
30875e1c
SG
190 }
191}
192
193void
194sort_pst_symbols (pst)
195 struct partial_symtab *pst;
196{
197 /* Sort the global list; don't sort the static list */
198
199 qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset,
2ad5709f 200 pst -> n_global_syms, sizeof (struct partial_symbol *),
30875e1c
SG
201 compare_psymbols);
202}
203
bd5635a1
RP
204/* Call sort_block_syms to sort alphabetically the symbols of one block. */
205
206void
207sort_block_syms (b)
208 register struct block *b;
209{
210 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
211 sizeof (struct symbol *), compare_symbols);
212}
213
214/* Call sort_symtab_syms to sort alphabetically
215 the symbols of each block of one symtab. */
216
217void
218sort_symtab_syms (s)
219 register struct symtab *s;
220{
c9bd6710
JG
221 register struct blockvector *bv;
222 int nbl;
bd5635a1
RP
223 int i;
224 register struct block *b;
225
c9bd6710
JG
226 if (s == 0)
227 return;
228 bv = BLOCKVECTOR (s);
229 nbl = BLOCKVECTOR_NBLOCKS (bv);
bd5635a1
RP
230 for (i = 0; i < nbl; i++)
231 {
232 b = BLOCKVECTOR_BLOCK (bv, i);
233 if (BLOCK_SHOULD_SORT (b))
234 sort_block_syms (b);
235 }
236}
237
ace4b8d7
FF
238/* Make a null terminated copy of the string at PTR with SIZE characters in
239 the obstack pointed to by OBSTACKP . Returns the address of the copy.
240 Note that the string at PTR does not have to be null terminated, I.E. it
241 may be part of a larger string and we are only saving a substring. */
bd5635a1
RP
242
243char *
30875e1c 244obsavestring (ptr, size, obstackp)
bd5635a1
RP
245 char *ptr;
246 int size;
30875e1c 247 struct obstack *obstackp;
bd5635a1 248{
30875e1c 249 register char *p = (char *) obstack_alloc (obstackp, size + 1);
ace4b8d7
FF
250 /* Open-coded memcpy--saves function call time. These strings are usually
251 short. FIXME: Is this really still true with a compiler that can
252 inline memcpy? */
bd5635a1
RP
253 {
254 register char *p1 = ptr;
255 register char *p2 = p;
256 char *end = ptr + size;
257 while (p1 != end)
258 *p2++ = *p1++;
259 }
260 p[size] = 0;
261 return p;
262}
263
ace4b8d7
FF
264/* Concatenate strings S1, S2 and S3; return the new string. Space is found
265 in the obstack pointed to by OBSTACKP. */
bd5635a1
RP
266
267char *
30875e1c
SG
268obconcat (obstackp, s1, s2, s3)
269 struct obstack *obstackp;
270 const char *s1, *s2, *s3;
bd5635a1
RP
271{
272 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
30875e1c 273 register char *val = (char *) obstack_alloc (obstackp, len);
bd5635a1
RP
274 strcpy (val, s1);
275 strcat (val, s2);
276 strcat (val, s3);
277 return val;
278}
bd5635a1 279
7ef89313
PB
280/* True if we are nested inside psymtab_to_symtab. */
281
282int currently_reading_symtab = 0;
283
b52cac6b 284static void
7ef89313
PB
285decrement_reading_symtab (dummy)
286 void *dummy;
287{
288 currently_reading_symtab--;
289}
290
bd5635a1
RP
291/* Get the symbol table that corresponds to a partial_symtab.
292 This is fast after the first time you do it. In fact, there
293 is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
294 case inline. */
295
296struct symtab *
297psymtab_to_symtab (pst)
298 register struct partial_symtab *pst;
299{
bd5635a1
RP
300 /* If it's been looked up before, return it. */
301 if (pst->symtab)
302 return pst->symtab;
303
304 /* If it has not yet been read in, read it. */
305 if (!pst->readin)
306 {
7ef89313
PB
307 struct cleanup *back_to = make_cleanup (decrement_reading_symtab, NULL);
308 currently_reading_symtab++;
bd5635a1 309 (*pst->read_symtab) (pst);
7ef89313 310 do_cleanups (back_to);
bd5635a1
RP
311 }
312
61a7292f 313 return pst->symtab;
bd5635a1
RP
314}
315
bf349b77
FF
316/* Initialize entry point information for this objfile. */
317
318void
319init_entry_point_info (objfile)
320 struct objfile *objfile;
321{
322 /* Save startup file's range of PC addresses to help blockframe.c
323 decide where the bottom of the stack is. */
324
325 if (bfd_get_file_flags (objfile -> obfd) & EXEC_P)
326 {
327 /* Executable file -- record its entry point so we'll recognize
328 the startup file because it contains the entry point. */
329 objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd);
330 }
331 else
332 {
333 /* Examination of non-executable.o files. Short-circuit this stuff. */
f3806e3b 334 objfile -> ei.entry_point = INVALID_ENTRY_POINT;
bf349b77 335 }
dee7a11c
FF
336 objfile -> ei.entry_file_lowpc = INVALID_ENTRY_LOWPC;
337 objfile -> ei.entry_file_highpc = INVALID_ENTRY_HIGHPC;
338 objfile -> ei.entry_func_lowpc = INVALID_ENTRY_LOWPC;
339 objfile -> ei.entry_func_highpc = INVALID_ENTRY_HIGHPC;
340 objfile -> ei.main_func_lowpc = INVALID_ENTRY_LOWPC;
341 objfile -> ei.main_func_highpc = INVALID_ENTRY_HIGHPC;
bf349b77
FF
342}
343
4d57c599
JK
344/* Get current entry point address. */
345
346CORE_ADDR
347entry_point_address()
348{
349 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
350}
351
a8e033f2 352/* Remember the lowest-addressed loadable section we've seen.
1a494973
C
353 This function is called via bfd_map_over_sections.
354
355 In case of equal vmas, the section with the largest size becomes the
356 lowest-addressed loadable section.
357
358 If the vmas and sizes are equal, the last section is considered the
359 lowest-addressed loadable section. */
a8e033f2 360
464c6c5f 361void
a8e033f2
SG
362find_lowest_section (abfd, sect, obj)
363 bfd *abfd;
364 asection *sect;
365 PTR obj;
366{
367 asection **lowest = (asection **)obj;
368
369 if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
370 return;
371 if (!*lowest)
372 *lowest = sect; /* First loadable section */
1a494973 373 else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
a8e033f2 374 *lowest = sect; /* A lower loadable section */
1a494973
C
375 else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
376 && (bfd_section_size (abfd, (*lowest))
377 <= bfd_section_size (abfd, sect)))
378 *lowest = sect;
a8e033f2 379}
a8e033f2 380
e74acce4
MA
381/* Parse the user's idea of an offset for dynamic linking, into our idea
382 of how to represent it for fast symbol reading. This is the default
383 version of the sym_fns.sym_offsets function for symbol readers that
384 don't need to do anything special. It allocates a section_offsets table
385 for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */
386
387struct section_offsets *
388default_symfile_offsets (objfile, addr)
389 struct objfile *objfile;
390 CORE_ADDR addr;
391{
392 struct section_offsets *section_offsets;
393 int i;
394
395 objfile->num_sections = SECT_OFF_MAX;
396 section_offsets = (struct section_offsets *)
397 obstack_alloc (&objfile -> psymbol_obstack, SIZEOF_SECTION_OFFSETS);
398
399 for (i = 0; i < SECT_OFF_MAX; i++)
400 ANOFFSET (section_offsets, i) = addr;
401
402 return section_offsets;
403}
404
405
bd5635a1
RP
406/* Process a symbol file, as either the main file or as a dynamically
407 loaded file.
408
b3fdaf3d
JK
409 NAME is the file name (which will be tilde-expanded and made
410 absolute herein) (but we don't free or modify NAME itself).
411 FROM_TTY says how verbose to be. MAINLINE specifies whether this
412 is the main symbol file, or whether it's an extra symbol file such
413 as dynamically loaded code. If !mainline, ADDR is the address
4369a140
JG
414 where the text segment was loaded. If VERBO, the caller has printed
415 a verbose message about the symbol reading (and complaints can be
416 more terse about it). */
bd5635a1
RP
417
418void
4369a140 419syms_from_objfile (objfile, addr, mainline, verbo)
7d9884b9 420 struct objfile *objfile;
bd5635a1
RP
421 CORE_ADDR addr;
422 int mainline;
4369a140 423 int verbo;
bd5635a1 424{
a8e033f2
SG
425 struct section_offsets *section_offsets;
426 asection *lowest_sect;
ade40d31 427 struct cleanup *old_chain;
bd5635a1 428
bf349b77 429 init_entry_point_info (objfile);
80d68b1d 430 find_sym_fns (objfile);
bd5635a1 431
ade40d31
RP
432 /* Make sure that partially constructed symbol tables will be cleaned up
433 if an error occurs during symbol reading. */
434 old_chain = make_cleanup (free_objfile, objfile);
435
bd5635a1
RP
436 if (mainline)
437 {
ade40d31
RP
438 /* We will modify the main symbol table, make sure that all its users
439 will be cleaned up if an error occurs during symbol reading. */
440 make_cleanup (clear_symtab_users, 0);
441
bd5635a1
RP
442 /* Since no error yet, throw away the old symbol table. */
443
80d68b1d
FF
444 if (symfile_objfile != NULL)
445 {
446 free_objfile (symfile_objfile);
447 symfile_objfile = NULL;
448 }
bd5635a1 449
f6c4bf1a
JK
450 /* Currently we keep symbols from the add-symbol-file command.
451 If the user wants to get rid of them, they should do "symbol-file"
452 without arguments first. Not sure this is the best behavior
453 (PR 2207). */
454
80d68b1d 455 (*objfile -> sf -> sym_new_init) (objfile);
a8e033f2 456 }
bd5635a1 457
a8e033f2
SG
458 /* Convert addr into an offset rather than an absolute address.
459 We find the lowest address of a loaded segment in the objfile,
460 and assume that <addr> is where that got loaded. Due to historical
1a494973 461 precedent, we warn if that doesn't happen to be a text segment. */
80d68b1d 462
a8e033f2
SG
463 if (mainline)
464 {
465 addr = 0; /* No offset from objfile addresses. */
466 }
467 else
468 {
469 lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text");
1a494973
C
470 if (lowest_sect == NULL)
471 bfd_map_over_sections (objfile->obfd, find_lowest_section,
472 (PTR) &lowest_sect);
a8e033f2 473
1a494973 474 if (lowest_sect == NULL)
a8e033f2
SG
475 warning ("no loadable sections found in added symbol-file %s",
476 objfile->name);
1a494973
C
477 else if ((bfd_get_section_flags (objfile->obfd, lowest_sect) & SEC_CODE)
478 == 0)
c4a081e1 479 /* FIXME-32x64--assumes bfd_vma fits in long. */
4d57c599 480 warning ("Lowest section in %s is %s at 0x%lx",
a8e033f2
SG
481 objfile->name,
482 bfd_section_name (objfile->obfd, lowest_sect),
4d57c599 483 (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect));
a8e033f2
SG
484
485 if (lowest_sect)
486 addr -= bfd_section_vma (objfile->obfd, lowest_sect);
bd5635a1
RP
487 }
488
80d68b1d
FF
489 /* Initialize symbol reading routines for this objfile, allow complaints to
490 appear for this new file, and record how verbose to be, then do the
491 initial symbol reading for this file. */
4369a140 492
80d68b1d
FF
493 (*objfile -> sf -> sym_init) (objfile);
494 clear_complaints (1, verbo);
2093fe68 495
a8e033f2 496 section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr);
2093fe68
RP
497 objfile->section_offsets = section_offsets;
498
4365c36c
JK
499#ifndef IBM6000_TARGET
500 /* This is a SVR4/SunOS specific hack, I think. In any event, it
501 screws RS/6000. sym_offsets should be doing this sort of thing,
502 because it knows the mapping between bfd sections and
503 section_offsets. */
5aefc1ca
FF
504 /* This is a hack. As far as I can tell, section offsets are not
505 target dependent. They are all set to addr with a couple of
506 exceptions. The exceptions are sysvr4 shared libraries, whose
507 offsets are kept in solib structures anyway and rs6000 xcoff
508 which handles shared libraries in a completely unique way.
509
510 Section offsets are built similarly, except that they are built
511 by adding addr in all cases because there is no clear mapping
512 from section_offsets into actual sections. Note that solib.c
513 has a different algorythm for finding section offsets.
514
515 These should probably all be collapsed into some target
516 independent form of shared library support. FIXME. */
517
518 if (addr)
519 {
520 struct obj_section *s;
521
522 for (s = objfile->sections; s < objfile->sections_end; ++s)
523 {
524 s->addr -= s->offset;
525 s->addr += addr;
526 s->endaddr -= s->offset;
527 s->endaddr += addr;
528 s->offset += addr;
529 }
530 }
4365c36c 531#endif /* not IBM6000_TARGET */
5aefc1ca 532
a8e033f2 533 (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline);
bd5635a1 534
f3806e3b
PS
535 if (!have_partial_symbols () && !have_full_symbols ())
536 {
537 wrap_here ("");
538 printf_filtered ("(no debugging symbols found)...");
539 wrap_here ("");
540 }
541
4d57c599
JK
542 /* Don't allow char * to have a typename (else would get caddr_t).
543 Ditto void *. FIXME: Check whether this is now done by all the
544 symbol readers themselves (many of them now do), and if so remove
545 it from here. */
bd5635a1
RP
546
547 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
548 TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
549
9342ecb9
JG
550 /* Mark the objfile has having had initial symbol read attempted. Note
551 that this does not mean we found any symbols... */
552
553 objfile -> flags |= OBJF_SYMS;
ade40d31
RP
554
555 /* Discard cleanups as symbol reading was successful. */
556
557 discard_cleanups (old_chain);
dee7a11c
FF
558
559/* Call this after reading in a new symbol table to give target dependant code
560 a crack at the new symbols. For instance, this could be used to update the
561 values of target-specific symbols GDB needs to keep track of (such as
562 _sigtramp, or whatever). */
563
564 TARGET_SYMFILE_POSTREAD (objfile);
9342ecb9
JG
565}
566
ade40d31 567/* Perform required actions after either reading in the initial
9342ecb9
JG
568 symbols for a new objfile, or mapping in the symbols from a reusable
569 objfile. */
570
571void
572new_symfile_objfile (objfile, mainline, verbo)
573 struct objfile *objfile;
574 int mainline;
575 int verbo;
576{
ade40d31
RP
577
578 /* If this is the main symbol file we have to clean up all users of the
579 old main symbol file. Otherwise it is sufficient to fixup all the
580 breakpoints that may have been redefined by this symbol file. */
bd5635a1
RP
581 if (mainline)
582 {
583 /* OK, make it the "real" symbol file. */
7d9884b9 584 symfile_objfile = objfile;
bd5635a1 585
ade40d31
RP
586 clear_symtab_users ();
587 }
588 else
589 {
590 breakpoint_re_set ();
591 }
4369a140
JG
592
593 /* We're done reading the symbol file; finish off complaints. */
80d68b1d 594 clear_complaints (0, verbo);
30875e1c 595}
d47d5315
JG
596
597/* Process a symbol file, as either the main file or as a dynamically
598 loaded file.
599
600 NAME is the file name (which will be tilde-expanded and made
601 absolute herein) (but we don't free or modify NAME itself).
602 FROM_TTY says how verbose to be. MAINLINE specifies whether this
603 is the main symbol file, or whether it's an extra symbol file such
604 as dynamically loaded code. If !mainline, ADDR is the address
30875e1c 605 where the text segment was loaded.
d47d5315 606
30875e1c
SG
607 Upon success, returns a pointer to the objfile that was added.
608 Upon failure, jumps back to command level (never returns). */
609
610struct objfile *
b0246b3b 611symbol_file_add (name, from_tty, addr, mainline, mapped, readnow)
d47d5315
JG
612 char *name;
613 int from_tty;
614 CORE_ADDR addr;
615 int mainline;
318bf84f 616 int mapped;
b0246b3b 617 int readnow;
d47d5315 618{
7d9884b9 619 struct objfile *objfile;
b0246b3b 620 struct partial_symtab *psymtab;
80d68b1d 621 bfd *abfd;
d47d5315 622
2093fe68
RP
623 /* Open a bfd for the file, and give user a chance to burp if we'd be
624 interactively wiping out any existing symbols. */
80d68b1d
FF
625
626 abfd = symfile_bfd_open (name);
d47d5315 627
80d68b1d
FF
628 if ((have_full_symbols () || have_partial_symbols ())
629 && mainline
630 && from_tty
631 && !query ("Load new symbol table from \"%s\"? ", name))
632 error ("Not confirmed.");
a8e033f2 633
80d68b1d
FF
634 objfile = allocate_objfile (abfd, mapped);
635
318bf84f
FF
636 /* If the objfile uses a mapped symbol file, and we have a psymtab for
637 it, then skip reading any symbols at this time. */
d47d5315 638
bf349b77 639 if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS))
d47d5315 640 {
80d68b1d 641 /* We mapped in an existing symbol table file that already has had
bf349b77
FF
642 initial symbol reading performed, so we can skip that part. Notify
643 the user that instead of reading the symbols, they have been mapped.
644 */
318bf84f
FF
645 if (from_tty || info_verbose)
646 {
80d68b1d
FF
647 printf_filtered ("Mapped symbols for %s...", name);
648 wrap_here ("");
199b2450 649 gdb_flush (gdb_stdout);
318bf84f 650 }
9342ecb9
JG
651 init_entry_point_info (objfile);
652 find_sym_fns (objfile);
d47d5315 653 }
318bf84f 654 else
bd5635a1 655 {
80d68b1d 656 /* We either created a new mapped symbol table, mapped an existing
bf349b77
FF
657 symbol table file which has not had initial symbol reading
658 performed, or need to read an unmapped symbol table. */
318bf84f
FF
659 if (from_tty || info_verbose)
660 {
74d6ac44
JM
661 if (pre_add_symbol_hook)
662 pre_add_symbol_hook (name);
663 else
664 {
665 printf_filtered ("Reading symbols from %s...", name);
666 wrap_here ("");
667 gdb_flush (gdb_stdout);
668 }
318bf84f 669 }
318bf84f 670 syms_from_objfile (objfile, addr, mainline, from_tty);
80d68b1d
FF
671 }
672
673 /* We now have at least a partial symbol table. Check to see if the
674 user requested that all symbols be read on initial access via either
675 the gdb startup command line or on a per symbol file basis. Expand
676 all partial symbol tables for this objfile if so. */
b0246b3b 677
bf349b77 678 if (readnow || readnow_symbol_files)
80d68b1d 679 {
318bf84f
FF
680 if (from_tty || info_verbose)
681 {
80d68b1d
FF
682 printf_filtered ("expanding to full symbols...");
683 wrap_here ("");
199b2450 684 gdb_flush (gdb_stdout);
318bf84f 685 }
80d68b1d
FF
686
687 for (psymtab = objfile -> psymtabs;
688 psymtab != NULL;
689 psymtab = psymtab -> next)
690 {
4ed3a9ea 691 psymtab_to_symtab (psymtab);
80d68b1d
FF
692 }
693 }
694
695 if (from_tty || info_verbose)
696 {
74d6ac44
JM
697 if (post_add_symbol_hook)
698 post_add_symbol_hook ();
699 else
700 {
701 printf_filtered ("done.\n");
702 gdb_flush (gdb_stdout);
703 }
bd5635a1 704 }
80d68b1d 705
ade40d31 706 new_symfile_objfile (objfile, mainline, from_tty);
ade40d31 707
012be3ce
DP
708 target_new_objfile (objfile);
709
30875e1c 710 return (objfile);
bd5635a1
RP
711}
712
2e6784a8
SG
713/* This is the symbol-file command. Read the file, analyze its
714 symbols, and add a struct symtab to a symtab list. The syntax of
715 the command is rather bizarre--(1) buildargv implements various
716 quoting conventions which are undocumented and have little or
717 nothing in common with the way things are quoted (or not quoted)
718 elsewhere in GDB, (2) options are used, which are not generally
719 used in GDB (perhaps "set mapped on", "set readnow on" would be
720 better), (3) the order of options matters, which is contrary to GNU
721 conventions (because it is confusing and inconvenient). */
bd5635a1
RP
722
723void
30875e1c
SG
724symbol_file_command (args, from_tty)
725 char *args;
bd5635a1
RP
726 int from_tty;
727{
30875e1c 728 char **argv;
b0246b3b 729 char *name = NULL;
25200748 730 CORE_ADDR text_relocation = 0; /* text_relocation */
30875e1c 731 struct cleanup *cleanups;
318bf84f 732 int mapped = 0;
30875e1c 733 int readnow = 0;
bd5635a1
RP
734
735 dont_repeat ();
736
30875e1c 737 if (args == NULL)
bd5635a1 738 {
cba0d141
JG
739 if ((have_full_symbols () || have_partial_symbols ())
740 && from_tty
741 && !query ("Discard symbol table from `%s'? ",
742 symfile_objfile -> name))
743 error ("Not confirmed.");
744 free_all_objfiles ();
30875e1c 745 symfile_objfile = NULL;
9342ecb9
JG
746 if (from_tty)
747 {
199b2450 748 printf_unfiltered ("No symbol file now.\n");
9342ecb9 749 }
bd5635a1 750 }
30875e1c
SG
751 else
752 {
753 if ((argv = buildargv (args)) == NULL)
754 {
318bf84f 755 nomem (0);
30875e1c
SG
756 }
757 cleanups = make_cleanup (freeargv, (char *) argv);
b0246b3b 758 while (*argv != NULL)
30875e1c 759 {
2e4964ad 760 if (STREQ (*argv, "-mapped"))
30875e1c 761 {
318bf84f 762 mapped = 1;
30875e1c 763 }
2e4964ad 764 else if (STREQ (*argv, "-readnow"))
30875e1c
SG
765 {
766 readnow = 1;
767 }
b0246b3b
FF
768 else if (**argv == '-')
769 {
770 error ("unknown option `%s'", *argv);
771 }
772 else
773 {
d9389f37
KH
774 char *p;
775
776 name = *argv;
777
778 /* this is for rombug remote only, to get the text relocation by
779 using link command */
780 p = strrchr(name, '/');
781 if (p != NULL) p++;
782 else p = name;
783
784 target_link(p, &text_relocation);
785
786 if (text_relocation == (CORE_ADDR)0)
787 return;
788 else if (text_relocation == (CORE_ADDR)-1)
d5412302
JK
789 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
790 readnow);
d9389f37 791 else
d5412302
JK
792 symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
793 0, mapped, readnow);
76212295
PS
794
795 /* Getting new symbols may change our opinion about what is
796 frameless. */
797 reinit_frame_cache ();
798
d9389f37 799 set_initial_language ();
b0246b3b
FF
800 }
801 argv++;
30875e1c 802 }
2403f49b 803
b0246b3b
FF
804 if (name == NULL)
805 {
806 error ("no symbol file name was specified");
807 }
30875e1c
SG
808 do_cleanups (cleanups);
809 }
bd5635a1
RP
810}
811
e58de8a2
FF
812/* Set the initial language.
813
814 A better solution would be to record the language in the psymtab when reading
815 partial symbols, and then use it (if known) to set the language. This would
816 be a win for formats that encode the language in an easily discoverable place,
817 such as DWARF. For stabs, we can jump through hoops looking for specially
818 named symbols or try to intuit the language from the specific type of stabs
819 we find, but we can't do that until later when we read in full symbols.
820 FIXME. */
821
822static void
823set_initial_language ()
824{
825 struct partial_symtab *pst;
826 enum language lang = language_unknown;
827
828 pst = find_main_psymtab ();
829 if (pst != NULL)
830 {
831 if (pst -> filename != NULL)
832 {
833 lang = deduce_language_from_filename (pst -> filename);
834 }
835 if (lang == language_unknown)
836 {
837 /* Make C the default language */
838 lang = language_c;
839 }
840 set_language (lang);
841 expected_language = current_language; /* Don't warn the user */
842 }
843}
844
b0246b3b
FF
845/* Open file specified by NAME and hand it off to BFD for preliminary
846 analysis. Result is a newly initialized bfd *, which includes a newly
847 malloc'd` copy of NAME (tilde-expanded and made absolute).
7d9884b9 848 In case of trouble, error() is called. */
bd5635a1 849
b0246b3b
FF
850static bfd *
851symfile_bfd_open (name)
bd5635a1
RP
852 char *name;
853{
854 bfd *sym_bfd;
855 int desc;
856 char *absolute_name;
857
7d9884b9 858 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 859
7d9884b9 860 /* Look down path for it, allocate 2nd new malloc'd copy. */
2093fe68 861 desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
40b647e9 862#if defined(__GO32__) || defined(_WIN32)
3f17fed8
GN
863 if (desc < 0)
864 {
865 char *exename = alloca (strlen (name) + 5);
866 strcat (strcpy (exename, name), ".exe");
867 desc = openp (getenv ("PATH"), 1, exename, O_RDONLY | O_BINARY,
868 0, &absolute_name);
869 }
870#endif
b0246b3b
FF
871 if (desc < 0)
872 {
873 make_cleanup (free, name);
874 perror_with_name (name);
875 }
7d9884b9 876 free (name); /* Free 1st new malloc'd copy */
30875e1c 877 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
346168a2 878 /* It'll be freed in free_objfile(). */
bd5635a1 879
ade40d31 880 sym_bfd = bfd_fdopenr (name, gnutarget, desc);
bd5635a1
RP
881 if (!sym_bfd)
882 {
883 close (desc);
7d9884b9 884 make_cleanup (free, name);
b0246b3b 885 error ("\"%s\": can't open to read symbols: %s.", name,
c4a081e1 886 bfd_errmsg (bfd_get_error ()));
bd5635a1 887 }
e58de8a2 888 sym_bfd->cacheable = true;
bd5635a1 889
b0246b3b
FF
890 if (!bfd_check_format (sym_bfd, bfd_object))
891 {
1a494973
C
892 /* FIXME: should be checking for errors from bfd_close (for one thing,
893 on error it does not free all the storage associated with the
894 bfd). */
b0246b3b
FF
895 bfd_close (sym_bfd); /* This also closes desc */
896 make_cleanup (free, name);
897 error ("\"%s\": can't read symbols: %s.", name,
c4a081e1 898 bfd_errmsg (bfd_get_error ()));
b0246b3b 899 }
7d9884b9 900
b0246b3b 901 return (sym_bfd);
7d9884b9
JG
902}
903
80d68b1d
FF
904/* Link a new symtab_fns into the global symtab_fns list. Called on gdb
905 startup by the _initialize routine in each object file format reader,
906 to register information about each format the the reader is prepared
907 to handle. */
bd5635a1
RP
908
909void
910add_symtab_fns (sf)
911 struct sym_fns *sf;
912{
913 sf->next = symtab_fns;
914 symtab_fns = sf;
915}
916
917
918/* Initialize to read symbols from the symbol file sym_bfd. It either
80d68b1d
FF
919 returns or calls error(). The result is an initialized struct sym_fns
920 in the objfile structure, that contains cached information about the
921 symbol file. */
bd5635a1 922
80d68b1d
FF
923static void
924find_sym_fns (objfile)
7d9884b9 925 struct objfile *objfile;
bd5635a1 926{
ac88ca20 927 struct sym_fns *sf;
0eed42de 928 enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
c4a081e1 929 char *our_target = bfd_get_target (objfile -> obfd);
0eed42de 930
1750a5ef
SC
931 /* Special kludge for RS/6000 and PowerMac. See xcoffread.c. */
932 if (STREQ (our_target, "aixcoff-rs6000") ||
933 STREQ (our_target, "xcoff-powermac"))
0eed42de 934 our_flavour = (enum bfd_flavour)-1;
bd5635a1 935
c4a081e1
DM
936 /* Special kludge for apollo. See dstread.c. */
937 if (STREQN (our_target, "apollo", 6))
938 our_flavour = (enum bfd_flavour)-2;
939
80d68b1d 940 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
bd5635a1 941 {
0eed42de 942 if (our_flavour == sf -> sym_flavour)
bd5635a1 943 {
80d68b1d
FF
944 objfile -> sf = sf;
945 return;
bd5635a1
RP
946 }
947 }
c9bd6710 948 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
b0246b3b 949 bfd_get_target (objfile -> obfd));
bd5635a1
RP
950}
951\f
952/* This function runs the load command of our current target. */
953
30875e1c 954static void
bd5635a1
RP
955load_command (arg, from_tty)
956 char *arg;
957 int from_tty;
958{
f3806e3b
PS
959 if (arg == NULL)
960 arg = get_exec_file (1);
bd5635a1
RP
961 target_load (arg, from_tty);
962}
963
ade40d31
RP
964/* This version of "load" should be usable for any target. Currently
965 it is just used for remote targets, not inftarg.c or core files,
966 on the theory that only in that case is it useful.
967
968 Avoiding xmodem and the like seems like a win (a) because we don't have
969 to worry about finding it, and (b) On VMS, fork() is very slow and so
970 we don't want to run a subprocess. On the other hand, I'm not sure how
971 performance compares. */
74d6ac44
JM
972#define GENERIC_LOAD_CHUNK 256
973#define VALIDATE_DOWNLOAD 0
ade40d31
RP
974void
975generic_load (filename, from_tty)
976 char *filename;
977 int from_tty;
978{
979 struct cleanup *old_cleanups;
980 asection *s;
c4a081e1 981 bfd *loadfile_bfd;
b52cac6b 982 time_t start_time, end_time; /* Start and end times of download */
67c1413d 983 unsigned long data_count = 0; /* Number of bytes transferred to memory */
012be3ce
DP
984 int n;
985 unsigned long load_offset = 0; /* offset to add to vma for each section */
74d6ac44
JM
986 char buf[GENERIC_LOAD_CHUNK+8];
987#if VALIDATE_DOWNLOAD
988 char verify_buffer[GENERIC_LOAD_CHUNK+8] ;
989#endif
012be3ce
DP
990
991 /* enable user to specify address for downloading as 2nd arg to load */
b607efe7 992 n = sscanf(filename, "%s 0x%lx", buf, &load_offset);
012be3ce
DP
993 if (n > 1 )
994 filename = buf;
995 else
996 load_offset = 0;
c4a081e1 997
c4a081e1 998 loadfile_bfd = bfd_openr (filename, gnutarget);
ade40d31
RP
999 if (loadfile_bfd == NULL)
1000 {
1001 perror_with_name (filename);
1002 return;
1003 }
1a494973
C
1004 /* FIXME: should be checking for errors from bfd_close (for one thing,
1005 on error it does not free all the storage associated with the
1006 bfd). */
ade40d31
RP
1007 old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
1008
1009 if (!bfd_check_format (loadfile_bfd, bfd_object))
1010 {
1011 error ("\"%s\" is not an object file: %s", filename,
c4a081e1 1012 bfd_errmsg (bfd_get_error ()));
ade40d31
RP
1013 }
1014
b52cac6b
FF
1015 start_time = time (NULL);
1016
ade40d31
RP
1017 for (s = loadfile_bfd->sections; s; s = s->next)
1018 {
1019 if (s->flags & SEC_LOAD)
77636dea
FF
1020 {
1021 bfd_size_type size;
1022
1023 size = bfd_get_section_size_before_reloc (s);
1024 if (size > 0)
1025 {
1026 char *buffer;
1027 struct cleanup *old_chain;
1028 bfd_vma lma;
74d6ac44 1029 unsigned long l = size ;
77636dea
FF
1030 int err;
1031 char *sect;
1032 unsigned long sent;
1033 unsigned long len;
1034
74d6ac44 1035 l = l > GENERIC_LOAD_CHUNK ? GENERIC_LOAD_CHUNK : l ;
77636dea
FF
1036
1037 buffer = xmalloc (size);
1038 old_chain = make_cleanup (free, buffer);
1039
1040 lma = s->lma;
1041 lma += load_offset;
1042
1043 /* Is this really necessary? I guess it gives the user something
1044 to look at during a long download. */
1045 printf_filtered ("Loading section %s, size 0x%lx lma ",
1046 bfd_get_section_name (loadfile_bfd, s),
1047 (unsigned long) size);
1048 print_address_numeric (lma, 1, gdb_stdout);
1049 printf_filtered ("\n");
1050
1051 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
1052
74d6ac44
JM
1053 sect = (char *) bfd_get_section_name (loadfile_bfd, s);
1054 sent = 0;
77636dea
FF
1055 do
1056 {
1057 len = (size - sent) < l ? (size - sent) : l;
1058 sent += len;
1059 err = target_write_memory (lma, buffer, len);
1060 if (ui_load_progress_hook)
1061 if (ui_load_progress_hook (sect, sent))
1062 error ("Canceled the download");
74d6ac44
JM
1063#if VALIDATE_DOWNLOAD
1064 /* Broken memories and broken monitors manifest themselves
1065 here when bring new computers to life.
1066 This doubles already slow downloads.
1067 */
1068 if (err) break ;
1069 {
1070 target_read_memory(lma,verify_buffer,len) ;
1071 if (0 != bcmp(buffer,verify_buffer,len))
1072 error("Download verify failed at %08x",
1073 (unsigned long)lma) ;
1074 }
1075
1076#endif
1077 data_count += len ;
77636dea
FF
1078 lma += len;
1079 buffer += len;
74d6ac44 1080 } /* od */
77636dea
FF
1081 while (err == 0 && sent < size);
1082
1083 if (err != 0)
1084 error ("Memory access error while loading section %s.",
1085 bfd_get_section_name (loadfile_bfd, s));
1086
1087 do_cleanups (old_chain);
1088 }
1089 }
ade40d31
RP
1090 }
1091
b52cac6b 1092 end_time = time (NULL);
74d6ac44
JM
1093 {
1094 unsigned long entry ;
1095 entry = bfd_get_start_address(loadfile_bfd) ;
1096 printf_filtered ("Start address 0x%lx , load size %d\n", entry,data_count);
1097 /* We were doing this in remote-mips.c, I suspect it is right
1098 for other targets too. */
1099 write_pc (entry);
1100 }
ade40d31
RP
1101
1102 /* FIXME: are we supposed to call symbol_file_add or not? According to
1103 a comment from remote-mips.c (where a call to symbol_file_add was
1104 commented out), making the call confuses GDB if more than one file is
1105 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
1106 does. */
1107
72158e71 1108 report_transfer_performance (data_count, start_time, end_time);
b52cac6b 1109
ade40d31
RP
1110 do_cleanups (old_cleanups);
1111}
1112
72158e71
SS
1113/* Report how fast the transfer went. */
1114
1115void
1116report_transfer_performance (data_count, start_time, end_time)
1117unsigned long data_count;
1118time_t start_time, end_time;
1119{
1120 printf_filtered ("Transfer rate: ");
1121 if (end_time != start_time)
1122 printf_filtered ("%d bits/sec",
1123 (data_count * 8) / (end_time - start_time));
1124 else
1125 printf_filtered ("%d bits in <1 sec", (data_count * 8));
1126 printf_filtered (".\n");
1127}
1128
61a7292f
SG
1129/* This function allows the addition of incrementally linked object files.
1130 It does not modify any state in the target, only in the debugger. */
bd5635a1 1131
e1ce8aa5 1132/* ARGSUSED */
30875e1c 1133static void
b0246b3b
FF
1134add_symbol_file_command (args, from_tty)
1135 char *args;
bd5635a1
RP
1136 int from_tty;
1137{
b0246b3b 1138 char *name = NULL;
bd5635a1 1139 CORE_ADDR text_addr;
b0246b3b 1140 char *arg;
ac88ca20
JG
1141 int readnow = 0;
1142 int mapped = 0;
bd5635a1 1143
b0246b3b 1144 dont_repeat ();
61a7292f 1145
b0246b3b
FF
1146 if (args == NULL)
1147 {
1148 error ("add-symbol-file takes a file name and an address");
1149 }
bd5635a1 1150
b0246b3b 1151 /* Make a copy of the string that we can safely write into. */
bd5635a1 1152
b0246b3b
FF
1153 args = strdup (args);
1154 make_cleanup (free, args);
1155
1156 /* Pick off any -option args and the file name. */
1157
1158 while ((*args != '\000') && (name == NULL))
1159 {
1160 while (isspace (*args)) {args++;}
1161 arg = args;
1162 while ((*args != '\000') && !isspace (*args)) {args++;}
1163 if (*args != '\000')
1164 {
1165 *args++ = '\000';
1166 }
1167 if (*arg != '-')
1168 {
1169 name = arg;
1170 }
2e4964ad 1171 else if (STREQ (arg, "-mapped"))
b0246b3b
FF
1172 {
1173 mapped = 1;
1174 }
2e4964ad 1175 else if (STREQ (arg, "-readnow"))
b0246b3b
FF
1176 {
1177 readnow = 1;
1178 }
1179 else
1180 {
1181 error ("unknown option `%s'", arg);
1182 }
1183 }
bd5635a1 1184
b0246b3b
FF
1185 /* After picking off any options and the file name, args should be
1186 left pointing at the remainder of the command line, which should
1187 be the address expression to evaluate. */
bd5635a1 1188
1340861c 1189 if (name == NULL)
b0246b3b 1190 {
1340861c 1191 error ("add-symbol-file takes a file name");
b0246b3b
FF
1192 }
1193 name = tilde_expand (name);
1194 make_cleanup (free, name);
bd5635a1 1195
1340861c
KH
1196 if (*args != '\000')
1197 {
1198 text_addr = parse_and_eval_address (args);
1199 }
1200 else
1201 {
1202 target_link(name, &text_addr);
1203 if (text_addr == (CORE_ADDR)-1)
1204 error("Don't know how to get text start location for this file");
1205 }
bd5635a1 1206
c4a081e1 1207 /* FIXME-32x64: Assumes text_addr fits in a long. */
d8ce1326 1208 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
4d57c599 1209 name, local_hex_string ((unsigned long)text_addr)))
bd5635a1
RP
1210 error ("Not confirmed.");
1211
4ed3a9ea 1212 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
76212295
PS
1213
1214 /* Getting new symbols may change our opinion about what is
1215 frameless. */
1216 reinit_frame_cache ();
bd5635a1
RP
1217}
1218\f
f3806e3b
PS
1219static void
1220add_shared_symbol_files_command (args, from_tty)
1221 char *args;
1222 int from_tty;
1223{
1224#ifdef ADD_SHARED_SYMBOL_FILES
1225 ADD_SHARED_SYMBOL_FILES (args, from_tty);
1226#else
1227 error ("This command is not available in this configuration of GDB.");
1228#endif
1229}
1230\f
7d9884b9 1231/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
1232void
1233reread_symbols ()
1234{
7d9884b9
JG
1235 struct objfile *objfile;
1236 long new_modtime;
1237 int reread_one = 0;
cba0d141
JG
1238 struct stat new_statbuf;
1239 int res;
bd5635a1
RP
1240
1241 /* With the addition of shared libraries, this should be modified,
1242 the load time should be saved in the partial symbol tables, since
1243 different tables may come from different source files. FIXME.
1244 This routine should then walk down each partial symbol table
30875e1c 1245 and see if the symbol table that it originates from has been changed */
bd5635a1 1246
7d9884b9
JG
1247 for (objfile = object_files; objfile; objfile = objfile->next) {
1248 if (objfile->obfd) {
1eeba686 1249#ifdef IBM6000_TARGET
318bf84f
FF
1250 /* If this object is from a shared library, then you should
1251 stat on the library name, not member name. */
1252
1253 if (objfile->obfd->my_archive)
1254 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1255 else
1256#endif
cba0d141
JG
1257 res = stat (objfile->name, &new_statbuf);
1258 if (res != 0) {
1259 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1260 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1261 objfile->name);
1262 continue;
1263 }
1264 new_modtime = new_statbuf.st_mtime;
4d57c599
JK
1265 if (new_modtime != objfile->mtime)
1266 {
1267 struct cleanup *old_cleanups;
1268 struct section_offsets *offsets;
1269 int num_offsets;
1270 int section_offsets_size;
76212295 1271 char *obfd_filename;
4d57c599
JK
1272
1273 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1274 objfile->name);
1275
1276 /* There are various functions like symbol_file_add,
1277 symfile_bfd_open, syms_from_objfile, etc., which might
1278 appear to do what we want. But they have various other
1279 effects which we *don't* want. So we just do stuff
1280 ourselves. We don't worry about mapped files (for one thing,
1281 any mapped file will be out of date). */
1282
1283 /* If we get an error, blow away this objfile (not sure if
1284 that is the correct response for things like shared
1285 libraries). */
1286 old_cleanups = make_cleanup (free_objfile, objfile);
1287 /* We need to do this whenever any symbols go away. */
1288 make_cleanup (clear_symtab_users, 0);
1289
1290 /* Clean up any state BFD has sitting around. We don't need
1291 to close the descriptor but BFD lacks a way of closing the
1292 BFD without closing the descriptor. */
76212295 1293 obfd_filename = bfd_get_filename (objfile->obfd);
4d57c599 1294 if (!bfd_close (objfile->obfd))
1a494973
C
1295 error ("Can't close BFD for %s: %s", objfile->name,
1296 bfd_errmsg (bfd_get_error ()));
76212295 1297 objfile->obfd = bfd_openr (obfd_filename, gnutarget);
4d57c599
JK
1298 if (objfile->obfd == NULL)
1299 error ("Can't open %s to read symbols.", objfile->name);
1300 /* bfd_openr sets cacheable to true, which is what we want. */
1301 if (!bfd_check_format (objfile->obfd, bfd_object))
1302 error ("Can't read symbols from %s: %s.", objfile->name,
c4a081e1 1303 bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1304
1305 /* Save the offsets, we will nuke them with the rest of the
1306 psymbol_obstack. */
1307 num_offsets = objfile->num_sections;
1308 section_offsets_size =
1309 sizeof (struct section_offsets)
1310 + sizeof (objfile->section_offsets->offsets) * num_offsets;
1311 offsets = (struct section_offsets *) alloca (section_offsets_size);
1312 memcpy (offsets, objfile->section_offsets, section_offsets_size);
1313
1314 /* Nuke all the state that we will re-read. Much of the following
1315 code which sets things to NULL really is necessary to tell
1316 other parts of GDB that there is nothing currently there. */
1317
1318 /* FIXME: Do we have to free a whole linked list, or is this
1319 enough? */
1320 if (objfile->global_psymbols.list)
1321 mfree (objfile->md, objfile->global_psymbols.list);
b52cac6b
FF
1322 memset (&objfile -> global_psymbols, 0,
1323 sizeof (objfile -> global_psymbols));
4d57c599
JK
1324 if (objfile->static_psymbols.list)
1325 mfree (objfile->md, objfile->static_psymbols.list);
b52cac6b
FF
1326 memset (&objfile -> static_psymbols, 0,
1327 sizeof (objfile -> static_psymbols));
4d57c599
JK
1328
1329 /* Free the obstacks for non-reusable objfiles */
2ad5709f 1330 obstack_free (&objfile -> psymbol_cache.cache, 0);
b52cac6b
FF
1331 memset (&objfile -> psymbol_cache, 0,
1332 sizeof (objfile -> psymbol_cache));
4d57c599
JK
1333 obstack_free (&objfile -> psymbol_obstack, 0);
1334 obstack_free (&objfile -> symbol_obstack, 0);
1335 obstack_free (&objfile -> type_obstack, 0);
1336 objfile->sections = NULL;
1337 objfile->symtabs = NULL;
1338 objfile->psymtabs = NULL;
1339 objfile->free_psymtabs = NULL;
1340 objfile->msymbols = NULL;
1341 objfile->minimal_symbol_count= 0;
1342 objfile->fundamental_types = NULL;
1343 if (objfile -> sf != NULL)
1344 {
1345 (*objfile -> sf -> sym_finish) (objfile);
1346 }
1347
1348 /* We never make this a mapped file. */
1349 objfile -> md = NULL;
1350 /* obstack_specify_allocation also initializes the obstack so
1351 it is empty. */
2ad5709f
FF
1352 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
1353 xmalloc, free);
4d57c599
JK
1354 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1355 xmalloc, free);
1356 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1357 xmalloc, free);
1358 obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1359 xmalloc, free);
1360 if (build_objfile_section_table (objfile))
1361 {
1362 error ("Can't find the file sections in `%s': %s",
c4a081e1 1363 objfile -> name, bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1364 }
1365
1366 /* We use the same section offsets as from last time. I'm not
1367 sure whether that is always correct for shared libraries. */
1368 objfile->section_offsets = (struct section_offsets *)
1369 obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1370 memcpy (objfile->section_offsets, offsets, section_offsets_size);
1371 objfile->num_sections = num_offsets;
1372
1373 /* What the hell is sym_new_init for, anyway? The concept of
1374 distinguishing between the main file and additional files
1375 in this way seems rather dubious. */
1376 if (objfile == symfile_objfile)
1377 (*objfile->sf->sym_new_init) (objfile);
1378
1379 (*objfile->sf->sym_init) (objfile);
1380 clear_complaints (1, 1);
1381 /* The "mainline" parameter is a hideous hack; I think leaving it
1382 zero is OK since dbxread.c also does what it needs to do if
1383 objfile->global_psymbols.size is 0. */
1384 (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
f3806e3b
PS
1385 if (!have_partial_symbols () && !have_full_symbols ())
1386 {
1387 wrap_here ("");
1388 printf_filtered ("(no debugging symbols found)\n");
1389 wrap_here ("");
1390 }
4d57c599
JK
1391 objfile -> flags |= OBJF_SYMS;
1392
1393 /* We're done reading the symbol file; finish off complaints. */
1394 clear_complaints (0, 1);
1395
1396 /* Getting new symbols may change our opinion about what is
1397 frameless. */
1398
1399 reinit_frame_cache ();
1400
1401 /* Discard cleanups as symbol reading was successful. */
1402 discard_cleanups (old_cleanups);
1403
1404 /* If the mtime has changed between the time we set new_modtime
1405 and now, we *want* this to be out of date, so don't call stat
1406 again now. */
1407 objfile->mtime = new_modtime;
1408 reread_one = 1;
dee7a11c
FF
1409
1410 /* Call this after reading in a new symbol table to give target
1411 dependant code a crack at the new symbols. For instance, this
1412 could be used to update the values of target-specific symbols GDB
1413 needs to keep track of (such as _sigtramp, or whatever). */
1414
1415 TARGET_SYMFILE_POSTREAD (objfile);
4d57c599 1416 }
bd5635a1 1417 }
7d9884b9
JG
1418 }
1419
1420 if (reread_one)
4d57c599 1421 clear_symtab_users ();
bd5635a1 1422}
bd5635a1 1423
bd5635a1 1424\f
7d9884b9
JG
1425enum language
1426deduce_language_from_filename (filename)
1427 char *filename;
1428{
2093fe68 1429 char *c;
7d9884b9 1430
2093fe68
RP
1431 if (0 == filename)
1432 ; /* Get default */
1433 else if (0 == (c = strrchr (filename, '.')))
1434 ; /* Get default. */
f3806e3b 1435 else if (STREQ (c, ".c"))
2093fe68 1436 return language_c;
f3806e3b 1437 else if (STREQ (c, ".cc") || STREQ (c, ".C") || STREQ (c, ".cxx")
1a494973 1438 || STREQ (c, ".cpp") || STREQ (c, ".cp") || STREQ (c, ".c++"))
2093fe68 1439 return language_cplus;
40b647e9
FF
1440 else if (STREQ (c, ".java"))
1441 return language_java;
f3806e3b 1442 else if (STREQ (c, ".ch") || STREQ (c, ".c186") || STREQ (c, ".c286"))
2093fe68 1443 return language_chill;
76212295
PS
1444 else if (STREQ (c, ".f") || STREQ (c, ".F"))
1445 return language_fortran;
f3806e3b
PS
1446 else if (STREQ (c, ".mod"))
1447 return language_m2;
1448 else if (STREQ (c, ".s") || STREQ (c, ".S"))
1449 return language_asm;
7d9884b9
JG
1450
1451 return language_unknown; /* default */
1452}
1453\f
d8ce1326
JG
1454/* allocate_symtab:
1455
1456 Allocate and partly initialize a new symbol table. Return a pointer
1457 to it. error() if no space.
1458
1459 Caller must set these fields:
1460 LINETABLE(symtab)
1461 symtab->blockvector
d8ce1326
JG
1462 symtab->dirname
1463 symtab->free_code
1464 symtab->free_ptr
1465 initialize any EXTRA_SYMTAB_INFO
1466 possibly free_named_symtabs (symtab->filename);
d8ce1326
JG
1467 */
1468
1469struct symtab *
30875e1c
SG
1470allocate_symtab (filename, objfile)
1471 char *filename;
1472 struct objfile *objfile;
d8ce1326
JG
1473{
1474 register struct symtab *symtab;
d8ce1326 1475
30875e1c
SG
1476 symtab = (struct symtab *)
1477 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
4ed3a9ea 1478 memset (symtab, 0, sizeof (*symtab));
30875e1c
SG
1479 symtab -> filename = obsavestring (filename, strlen (filename),
1480 &objfile -> symbol_obstack);
1481 symtab -> fullname = NULL;
1482 symtab -> language = deduce_language_from_filename (filename);
77636dea
FF
1483 symtab -> debugformat = obsavestring ("unknown", 7,
1484 &objfile -> symbol_obstack);
d8ce1326 1485
7d9884b9 1486 /* Hook it to the objfile it comes from */
30875e1c
SG
1487
1488 symtab -> objfile = objfile;
1489 symtab -> next = objfile -> symtabs;
1490 objfile -> symtabs = symtab;
7d9884b9
JG
1491
1492#ifdef INIT_EXTRA_SYMTAB_INFO
30875e1c 1493 INIT_EXTRA_SYMTAB_INFO (symtab);
7d9884b9 1494#endif
d8ce1326 1495
30875e1c 1496 return (symtab);
d8ce1326 1497}
30875e1c
SG
1498
1499struct partial_symtab *
1500allocate_psymtab (filename, objfile)
1501 char *filename;
1502 struct objfile *objfile;
1503{
1504 struct partial_symtab *psymtab;
1505
cba0d141
JG
1506 if (objfile -> free_psymtabs)
1507 {
1508 psymtab = objfile -> free_psymtabs;
1509 objfile -> free_psymtabs = psymtab -> next;
1510 }
1511 else
1512 psymtab = (struct partial_symtab *)
1513 obstack_alloc (&objfile -> psymbol_obstack,
1514 sizeof (struct partial_symtab));
1515
4ed3a9ea 1516 memset (psymtab, 0, sizeof (struct partial_symtab));
30875e1c
SG
1517 psymtab -> filename = obsavestring (filename, strlen (filename),
1518 &objfile -> psymbol_obstack);
1519 psymtab -> symtab = NULL;
1520
77636dea
FF
1521 /* Prepend it to the psymtab list for the objfile it belongs to.
1522 Psymtabs are searched in most recent inserted -> least recent
1523 inserted order. */
30875e1c
SG
1524
1525 psymtab -> objfile = objfile;
1526 psymtab -> next = objfile -> psymtabs;
1527 objfile -> psymtabs = psymtab;
77636dea
FF
1528#if 0
1529 {
1530 struct partial_symtab **prev_pst;
1531 psymtab -> objfile = objfile;
1532 psymtab -> next = NULL;
1533 prev_pst = &(objfile -> psymtabs);
1534 while ((*prev_pst) != NULL)
1535 prev_pst = &((*prev_pst) -> next);
1536 (*prev_pst) = psymtab;
1537 }
1538#endif
30875e1c
SG
1539
1540 return (psymtab);
1541}
1542
77636dea
FF
1543void
1544discard_psymtab (pst)
1545 struct partial_symtab *pst;
1546{
1547 struct partial_symtab **prev_pst;
1548
1549 /* From dbxread.c:
1550 Empty psymtabs happen as a result of header files which don't
1551 have any symbols in them. There can be a lot of them. But this
1552 check is wrong, in that a psymtab with N_SLINE entries but
1553 nothing else is not empty, but we don't realize that. Fixing
1554 that without slowing things down might be tricky. */
1555
1556 /* First, snip it out of the psymtab chain */
1557
1558 prev_pst = &(pst->objfile->psymtabs);
1559 while ((*prev_pst) != pst)
1560 prev_pst = &((*prev_pst)->next);
1561 (*prev_pst) = pst->next;
1562
1563 /* Next, put it on a free list for recycling */
1564
1565 pst->next = pst->objfile->free_psymtabs;
1566 pst->objfile->free_psymtabs = pst;
1567}
1568
d8ce1326 1569\f
ade40d31 1570/* Reset all data structures in gdb which may contain references to symbol
40b647e9 1571 table data. */
ade40d31
RP
1572
1573void
1574clear_symtab_users ()
1575{
1576 /* Someday, we should do better than this, by only blowing away
1577 the things that really need to be blown. */
1578 clear_value_history ();
1579 clear_displays ();
1580 clear_internalvars ();
1581 breakpoint_re_set ();
1582 set_default_breakpoint (0, 0, 0, 0);
1583 current_source_symtab = 0;
1584 current_source_line = 0;
4d57c599 1585 clear_pc_function_cache ();
012be3ce 1586 target_new_objfile (NULL);
ade40d31
RP
1587}
1588
9d199712
JG
1589/* clear_symtab_users_once:
1590
1591 This function is run after symbol reading, or from a cleanup.
1592 If an old symbol table was obsoleted, the old symbol table
1593 has been blown away, but the other GDB data structures that may
1594 reference it have not yet been cleared or re-directed. (The old
1595 symtab was zapped, and the cleanup queued, in free_named_symtab()
1596 below.)
1597
1598 This function can be queued N times as a cleanup, or called
1599 directly; it will do all the work the first time, and then will be a
1600 no-op until the next time it is queued. This works by bumping a
1601 counter at queueing time. Much later when the cleanup is run, or at
1602 the end of symbol processing (in case the cleanup is discarded), if
1603 the queued count is greater than the "done-count", we do the work
1604 and set the done-count to the queued count. If the queued count is
1605 less than or equal to the done-count, we just ignore the call. This
1606 is needed because reading a single .o file will often replace many
1607 symtabs (one per .h file, for example), and we don't want to reset
1608 the breakpoints N times in the user's face.
1609
1610 The reason we both queue a cleanup, and call it directly after symbol
1611 reading, is because the cleanup protects us in case of errors, but is
1612 discarded if symbol reading is successful. */
1613
ade40d31 1614#if 0
996ccb30
JK
1615/* FIXME: As free_named_symtabs is currently a big noop this function
1616 is no longer needed. */
ade40d31
RP
1617static void
1618clear_symtab_users_once PARAMS ((void));
1619
9d199712
JG
1620static int clear_symtab_users_queued;
1621static int clear_symtab_users_done;
1622
ade40d31 1623static void
9d199712
JG
1624clear_symtab_users_once ()
1625{
1626 /* Enforce once-per-`do_cleanups'-semantics */
1627 if (clear_symtab_users_queued <= clear_symtab_users_done)
1628 return;
1629 clear_symtab_users_done = clear_symtab_users_queued;
1630
ade40d31 1631 clear_symtab_users ();
9d199712 1632}
ade40d31 1633#endif
9d199712
JG
1634
1635/* Delete the specified psymtab, and any others that reference it. */
1636
e1ce8aa5 1637static void
9d199712
JG
1638cashier_psymtab (pst)
1639 struct partial_symtab *pst;
1640{
46c28185 1641 struct partial_symtab *ps, *pprev = NULL;
9d199712
JG
1642 int i;
1643
1644 /* Find its previous psymtab in the chain */
30875e1c 1645 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1646 if (ps == pst)
1647 break;
1648 pprev = ps;
1649 }
1650
1651 if (ps) {
1652 /* Unhook it from the chain. */
30875e1c
SG
1653 if (ps == pst->objfile->psymtabs)
1654 pst->objfile->psymtabs = ps->next;
9d199712
JG
1655 else
1656 pprev->next = ps->next;
1657
1658 /* FIXME, we can't conveniently deallocate the entries in the
1659 partial_symbol lists (global_psymbols/static_psymbols) that
1660 this psymtab points to. These just take up space until all
1661 the psymtabs are reclaimed. Ditto the dependencies list and
1662 filename, which are all in the psymbol_obstack. */
1663
1664 /* We need to cashier any psymtab that has this one as a dependency... */
1665again:
30875e1c 1666 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1667 for (i = 0; i < ps->number_of_dependencies; i++) {
1668 if (ps->dependencies[i] == pst) {
1669 cashier_psymtab (ps);
1670 goto again; /* Must restart, chain has been munged. */
1671 }
1672 }
1673 }
1674 }
1675}
1676
1677/* If a symtab or psymtab for filename NAME is found, free it along
1678 with any dependent breakpoints, displays, etc.
1679 Used when loading new versions of object modules with the "add-file"
1680 command. This is only called on the top-level symtab or psymtab's name;
1681 it is not called for subsidiary files such as .h files.
1682
1683 Return value is 1 if we blew away the environment, 0 if not.
30875e1c 1684 FIXME. The return valu appears to never be used.
9d199712
JG
1685
1686 FIXME. I think this is not the best way to do this. We should
1687 work on being gentler to the environment while still cleaning up
1688 all stray pointers into the freed symtab. */
1689
1690int
1691free_named_symtabs (name)
1692 char *name;
1693{
30875e1c
SG
1694#if 0
1695 /* FIXME: With the new method of each objfile having it's own
1696 psymtab list, this function needs serious rethinking. In particular,
1697 why was it ever necessary to toss psymtabs with specific compilation
1698 unit filenames, as opposed to all psymtabs from a particular symbol
ac88ca20
JG
1699 file? -- fnf
1700 Well, the answer is that some systems permit reloading of particular
1701 compilation units. We want to blow away any old info about these
1702 compilation units, regardless of which objfiles they arrived in. --gnu. */
1703
1704 register struct symtab *s;
1705 register struct symtab *prev;
1706 register struct partial_symtab *ps;
1707 struct blockvector *bv;
1708 int blewit = 0;
30875e1c 1709
61a7292f
SG
1710 /* We only wack things if the symbol-reload switch is set. */
1711 if (!symbol_reloading)
1712 return 0;
1713
d11c44f1
JG
1714 /* Some symbol formats have trouble providing file names... */
1715 if (name == 0 || *name == '\0')
1716 return 0;
1717
9d199712
JG
1718 /* Look for a psymtab with the specified name. */
1719
1720again2:
1721 for (ps = partial_symtab_list; ps; ps = ps->next) {
2e4964ad 1722 if (STREQ (name, ps->filename)) {
9d199712
JG
1723 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1724 goto again2; /* Must restart, chain has been munged */
1725 }
1726 }
1727
1728 /* Look for a symtab with the specified name. */
1729
1730 for (s = symtab_list; s; s = s->next)
1731 {
2e4964ad 1732 if (STREQ (name, s->filename))
9d199712
JG
1733 break;
1734 prev = s;
1735 }
1736
1737 if (s)
1738 {
1739 if (s == symtab_list)
1740 symtab_list = s->next;
1741 else
1742 prev->next = s->next;
1743
1744 /* For now, queue a delete for all breakpoints, displays, etc., whether
1745 or not they depend on the symtab being freed. This should be
1746 changed so that only those data structures affected are deleted. */
1747
1748 /* But don't delete anything if the symtab is empty.
1749 This test is necessary due to a bug in "dbxread.c" that
1750 causes empty symtabs to be created for N_SO symbols that
1751 contain the pathname of the object file. (This problem
1752 has been fixed in GDB 3.9x). */
1753
c9bd6710
JG
1754 bv = BLOCKVECTOR (s);
1755 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1756 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1757 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1758 {
1759 complain (&oldsyms_complaint, name);
1760
1761 clear_symtab_users_queued++;
1762 make_cleanup (clear_symtab_users_once, 0);
1763 blewit = 1;
1764 } else {
1765 complain (&empty_symtab_complaint, name);
1766 }
1767
1768 free_symtab (s);
1769 }
1770 else
d8ce1326
JG
1771 {
1772 /* It is still possible that some breakpoints will be affected
1773 even though no symtab was found, since the file might have
1774 been compiled without debugging, and hence not be associated
1775 with a symtab. In order to handle this correctly, we would need
1776 to keep a list of text address ranges for undebuggable files.
1777 For now, we do nothing, since this is a fairly obscure case. */
1778 ;
1779 }
9d199712 1780
30875e1c 1781 /* FIXME, what about the minimal symbol table? */
9d199712 1782 return blewit;
30875e1c
SG
1783#else
1784 return (0);
1785#endif
9d199712
JG
1786}
1787\f
d4ea2aba
PB
1788/* Allocate and partially fill a partial symtab. It will be
1789 completely filled at the end of the symbol list.
1790
1791 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1792 is the address relative to which its symbols are (incremental) or 0
1793 (normal). */
1794
1795
1796struct partial_symtab *
a8e033f2 1797start_psymtab_common (objfile, section_offsets,
d4ea2aba
PB
1798 filename, textlow, global_syms, static_syms)
1799 struct objfile *objfile;
a8e033f2 1800 struct section_offsets *section_offsets;
d4ea2aba
PB
1801 char *filename;
1802 CORE_ADDR textlow;
2ad5709f
FF
1803 struct partial_symbol **global_syms;
1804 struct partial_symbol **static_syms;
d4ea2aba 1805{
30875e1c
SG
1806 struct partial_symtab *psymtab;
1807
1808 psymtab = allocate_psymtab (filename, objfile);
a8e033f2 1809 psymtab -> section_offsets = section_offsets;
30875e1c
SG
1810 psymtab -> textlow = textlow;
1811 psymtab -> texthigh = psymtab -> textlow; /* default */
1812 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1813 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1814 return (psymtab);
7d9884b9 1815}
9342ecb9 1816\f
9342ecb9
JG
1817/* Add a symbol with a long value to a psymtab.
1818 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1819
1820void
012be3ce
DP
1821add_psymbol_to_list (name, namelength, namespace, class, list, val, coreaddr,
1822 language, objfile)
9342ecb9
JG
1823 char *name;
1824 int namelength;
1750a5ef 1825 namespace_enum namespace;
9342ecb9
JG
1826 enum address_class class;
1827 struct psymbol_allocation_list *list;
012be3ce
DP
1828 long val; /* Value as a long */
1829 CORE_ADDR coreaddr; /* Value as a CORE_ADDR */
2e4964ad
FF
1830 enum language language;
1831 struct objfile *objfile;
9342ecb9 1832{
2e4964ad 1833 register struct partial_symbol *psym;
2ad5709f 1834 char *buf = alloca (namelength + 1);
012be3ce
DP
1835 /* psymbol is static so that there will be no uninitialized gaps in the
1836 structure which might contain random data, causing cache misses in
1837 bcache. */
1838 static struct partial_symbol psymbol;
2ad5709f
FF
1839
1840 /* Create local copy of the partial symbol */
1841 memcpy (buf, name, namelength);
1842 buf[namelength] = '\0';
1843 SYMBOL_NAME (&psymbol) = bcache (buf, namelength + 1, &objfile->psymbol_cache);
012be3ce
DP
1844 /* val and coreaddr are mutually exclusive, one of them *will* be zero */
1845 if (val != 0)
2e4964ad 1846 {
012be3ce
DP
1847 SYMBOL_VALUE (&psymbol) = val;
1848 }
1849 else
1850 {
1851 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
2e4964ad 1852 }
2ad5709f
FF
1853 SYMBOL_SECTION (&psymbol) = 0;
1854 SYMBOL_LANGUAGE (&psymbol) = language;
1855 PSYMBOL_NAMESPACE (&psymbol) = namespace;
1856 PSYMBOL_CLASS (&psymbol) = class;
1857 SYMBOL_INIT_LANGUAGE_SPECIFIC (&psymbol, language);
1858
1859 /* Stash the partial symbol away in the cache */
1860 psym = bcache (&psymbol, sizeof (struct partial_symbol), &objfile->psymbol_cache);
1861
1862 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
2e4964ad
FF
1863 if (list->next >= list->list + list->size)
1864 {
2ad5709f 1865 extend_psymbol_list (list, objfile);
2e4964ad 1866 }
2ad5709f 1867 *list->next++ = psym;
f1d39876 1868 OBJSTAT (objfile, n_psyms++);
9342ecb9 1869}
7d9884b9 1870
1a494973
C
1871/* Initialize storage for partial symbols. */
1872
1873void
1874init_psymbol_list (objfile, total_symbols)
1875 struct objfile *objfile;
1876 int total_symbols;
1877{
1878 /* Free any previously allocated psymbol lists. */
1879
1880 if (objfile -> global_psymbols.list)
1881 {
1882 mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
1883 }
1884 if (objfile -> static_psymbols.list)
1885 {
1886 mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
1887 }
1888
1889 /* Current best guess is that approximately a twentieth
1890 of the total symbols (in a debugging file) are global or static
1891 oriented symbols */
1892
1893 objfile -> global_psymbols.size = total_symbols / 10;
1894 objfile -> static_psymbols.size = total_symbols / 10;
77636dea
FF
1895
1896 if (objfile -> global_psymbols.size > 0)
1897 {
1898 objfile -> global_psymbols.next =
1899 objfile -> global_psymbols.list = (struct partial_symbol **)
1900 xmmalloc (objfile -> md, (objfile -> global_psymbols.size
1901 * sizeof (struct partial_symbol *)));
1902 }
1903 if (objfile -> static_psymbols.size > 0)
1904 {
1905 objfile -> static_psymbols.next =
1906 objfile -> static_psymbols.list = (struct partial_symbol **)
1907 xmmalloc (objfile -> md, (objfile -> static_psymbols.size
1908 * sizeof (struct partial_symbol *)));
1909 }
1a494973 1910}
40b647e9
FF
1911
1912/* OVERLAYS:
1913 The following code implements an abstraction for debugging overlay sections.
1914
1915 The target model is as follows:
1916 1) The gnu linker will permit multiple sections to be mapped into the
1917 same VMA, each with its own unique LMA (or load address).
1918 2) It is assumed that some runtime mechanism exists for mapping the
1919 sections, one by one, from the load address into the VMA address.
1920 3) This code provides a mechanism for gdb to keep track of which
1921 sections should be considered to be mapped from the VMA to the LMA.
1922 This information is used for symbol lookup, and memory read/write.
1923 For instance, if a section has been mapped then its contents
1924 should be read from the VMA, otherwise from the LMA.
1925
1926 Two levels of debugger support for overlays are available. One is
1927 "manual", in which the debugger relies on the user to tell it which
1928 overlays are currently mapped. This level of support is
1929 implemented entirely in the core debugger, and the information about
1930 whether a section is mapped is kept in the objfile->obj_section table.
1931
1932 The second level of support is "automatic", and is only available if
1933 the target-specific code provides functionality to read the target's
1934 overlay mapping table, and translate its contents for the debugger
1935 (by updating the mapped state information in the obj_section tables).
1936
1937 The interface is as follows:
1938 User commands:
1939 overlay map <name> -- tell gdb to consider this section mapped
1940 overlay unmap <name> -- tell gdb to consider this section unmapped
1941 overlay list -- list the sections that GDB thinks are mapped
1942 overlay read-target -- get the target's state of what's mapped
1943 overlay off/manual/auto -- set overlay debugging state
1944 Functional interface:
1945 find_pc_mapped_section(pc): if the pc is in the range of a mapped
1946 section, return that section.
1947 find_pc_overlay(pc): find any overlay section that contains
1948 the pc, either in its VMA or its LMA
1949 overlay_is_mapped(sect): true if overlay is marked as mapped
1950 section_is_overlay(sect): true if section's VMA != LMA
1951 pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
1952 pc_in_unmapped_range(...): true if pc belongs to section's LMA
1953 overlay_mapped_address(...): map an address from section's LMA to VMA
1954 overlay_unmapped_address(...): map an address from section's VMA to LMA
1955 symbol_overlayed_address(...): Return a "current" address for symbol:
1956 either in VMA or LMA depending on whether
1957 the symbol's section is currently mapped
1958 */
1959
1960/* Overlay debugging state: */
1961
1962int overlay_debugging = 0; /* 0 == off, 1 == manual, -1 == auto */
1963int overlay_cache_invalid = 0; /* True if need to refresh mapped state */
1964
1965/* Target vector for refreshing overlay mapped state */
1966static void simple_overlay_update PARAMS ((struct obj_section *));
1967void (*target_overlay_update) PARAMS ((struct obj_section *))
1968 = simple_overlay_update;
1969
1970/* Function: section_is_overlay (SECTION)
1971 Returns true if SECTION has VMA not equal to LMA, ie.
1972 SECTION is loaded at an address different from where it will "run". */
1973
1974int
1975section_is_overlay (section)
1976 asection *section;
1977{
1978 if (overlay_debugging)
1979 if (section && section->lma != 0 &&
1980 section->vma != section->lma)
1981 return 1;
1982
1983 return 0;
1984}
1985
1986/* Function: overlay_invalidate_all (void)
1987 Invalidate the mapped state of all overlay sections (mark it as stale). */
1988
1989static void
1990overlay_invalidate_all ()
1991{
1992 struct objfile *objfile;
1993 struct obj_section *sect;
1994
1995 ALL_OBJSECTIONS (objfile, sect)
1996 if (section_is_overlay (sect->the_bfd_section))
1997 sect->ovly_mapped = -1;
1998}
1999
2000/* Function: overlay_is_mapped (SECTION)
2001 Returns true if section is an overlay, and is currently mapped.
2002 Private: public access is thru function section_is_mapped.
2003
2004 Access to the ovly_mapped flag is restricted to this function, so
2005 that we can do automatic update. If the global flag
2006 OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
2007 overlay_invalidate_all. If the mapped state of the particular
2008 section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
2009
2010static int
2011overlay_is_mapped (osect)
2012 struct obj_section *osect;
2013{
2014 if (osect == 0 || !section_is_overlay (osect->the_bfd_section))
2015 return 0;
2016
2017 switch (overlay_debugging)
2018 {
2019 default:
2020 case 0: return 0; /* overlay debugging off */
2021 case -1: /* overlay debugging automatic */
2022 /* Unles there is a target_overlay_update function,
2023 there's really nothing useful to do here (can't really go auto) */
2024 if (target_overlay_update)
2025 {
2026 if (overlay_cache_invalid)
2027 {
2028 overlay_invalidate_all ();
2029 overlay_cache_invalid = 0;
2030 }
2031 if (osect->ovly_mapped == -1)
2032 (*target_overlay_update) (osect);
2033 }
2034 /* fall thru to manual case */
2035 case 1: /* overlay debugging manual */
2036 return osect->ovly_mapped == 1;
2037 }
2038}
2039
2040/* Function: section_is_mapped
2041 Returns true if section is an overlay, and is currently mapped. */
2042
2043int
2044section_is_mapped (section)
2045 asection *section;
2046{
2047 struct objfile *objfile;
2048 struct obj_section *osect;
2049
2050 if (overlay_debugging)
2051 if (section && section_is_overlay (section))
2052 ALL_OBJSECTIONS (objfile, osect)
2053 if (osect->the_bfd_section == section)
2054 return overlay_is_mapped (osect);
2055
2056 return 0;
2057}
2058
2059/* Function: pc_in_unmapped_range
2060 If PC falls into the lma range of SECTION, return true, else false. */
2061
2062CORE_ADDR
2063pc_in_unmapped_range (pc, section)
2064 CORE_ADDR pc;
2065 asection *section;
2066{
2067 int size;
2068
2069 if (overlay_debugging)
2070 if (section && section_is_overlay (section))
2071 {
2072 size = bfd_get_section_size_before_reloc (section);
2073 if (section->lma <= pc && pc < section->lma + size)
2074 return 1;
2075 }
2076 return 0;
2077}
2078
2079/* Function: pc_in_mapped_range
2080 If PC falls into the vma range of SECTION, return true, else false. */
2081
2082CORE_ADDR
2083pc_in_mapped_range (pc, section)
2084 CORE_ADDR pc;
2085 asection *section;
2086{
2087 int size;
2088
2089 if (overlay_debugging)
2090 if (section && section_is_overlay (section))
2091 {
2092 size = bfd_get_section_size_before_reloc (section);
2093 if (section->vma <= pc && pc < section->vma + size)
2094 return 1;
2095 }
2096 return 0;
2097}
2098
2099/* Function: overlay_unmapped_address (PC, SECTION)
2100 Returns the address corresponding to PC in the unmapped (load) range.
2101 May be the same as PC. */
2102
2103CORE_ADDR
2104overlay_unmapped_address (pc, section)
2105 CORE_ADDR pc;
2106 asection *section;
2107{
2108 if (overlay_debugging)
2109 if (section && section_is_overlay (section) &&
2110 pc_in_mapped_range (pc, section))
2111 return pc + section->lma - section->vma;
2112
2113 return pc;
2114}
2115
2116/* Function: overlay_mapped_address (PC, SECTION)
2117 Returns the address corresponding to PC in the mapped (runtime) range.
2118 May be the same as PC. */
2119
2120CORE_ADDR
2121overlay_mapped_address (pc, section)
2122 CORE_ADDR pc;
2123 asection *section;
2124{
2125 if (overlay_debugging)
2126 if (section && section_is_overlay (section) &&
2127 pc_in_unmapped_range (pc, section))
2128 return pc + section->vma - section->lma;
2129
2130 return pc;
2131}
2132
2133
2134/* Function: symbol_overlayed_address
2135 Return one of two addresses (relative to the VMA or to the LMA),
2136 depending on whether the section is mapped or not. */
2137
2138CORE_ADDR
2139symbol_overlayed_address (address, section)
2140 CORE_ADDR address;
2141 asection *section;
2142{
2143 if (overlay_debugging)
2144 {
2145 /* If the symbol has no section, just return its regular address. */
2146 if (section == 0)
2147 return address;
2148 /* If the symbol's section is not an overlay, just return its address */
2149 if (!section_is_overlay (section))
2150 return address;
2151 /* If the symbol's section is mapped, just return its address */
2152 if (section_is_mapped (section))
2153 return address;
2154 /*
2155 * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
2156 * then return its LOADED address rather than its vma address!!
2157 */
2158 return overlay_unmapped_address (address, section);
2159 }
2160 return address;
2161}
2162
2163/* Function: find_pc_overlay (PC)
2164 Return the best-match overlay section for PC:
2165 If PC matches a mapped overlay section's VMA, return that section.
2166 Else if PC matches an unmapped section's VMA, return that section.
2167 Else if PC matches an unmapped section's LMA, return that section. */
2168
2169asection *
2170find_pc_overlay (pc)
2171 CORE_ADDR pc;
2172{
2173 struct objfile *objfile;
2174 struct obj_section *osect, *best_match = NULL;
2175
2176 if (overlay_debugging)
2177 ALL_OBJSECTIONS (objfile, osect)
2178 if (section_is_overlay (osect->the_bfd_section))
2179 {
2180 if (pc_in_mapped_range (pc, osect->the_bfd_section))
2181 {
2182 if (overlay_is_mapped (osect))
2183 return osect->the_bfd_section;
2184 else
2185 best_match = osect;
2186 }
2187 else if (pc_in_unmapped_range (pc, osect->the_bfd_section))
2188 best_match = osect;
2189 }
2190 return best_match ? best_match->the_bfd_section : NULL;
2191}
2192
2193/* Function: find_pc_mapped_section (PC)
2194 If PC falls into the VMA address range of an overlay section that is
2195 currently marked as MAPPED, return that section. Else return NULL. */
2196
2197asection *
2198find_pc_mapped_section (pc)
2199 CORE_ADDR pc;
2200{
2201 struct objfile *objfile;
2202 struct obj_section *osect;
2203
2204 if (overlay_debugging)
2205 ALL_OBJSECTIONS (objfile, osect)
2206 if (pc_in_mapped_range (pc, osect->the_bfd_section) &&
2207 overlay_is_mapped (osect))
2208 return osect->the_bfd_section;
2209
2210 return NULL;
2211}
2212
2213/* Function: list_overlays_command
2214 Print a list of mapped sections and their PC ranges */
2215
2216void
2217list_overlays_command (args, from_tty)
2218 char *args;
2219 int from_tty;
2220{
2221 int nmapped = 0;
2222 struct objfile *objfile;
2223 struct obj_section *osect;
2224
2225 if (overlay_debugging)
2226 ALL_OBJSECTIONS (objfile, osect)
2227 if (overlay_is_mapped (osect))
2228 {
2229 const char *name;
2230 bfd_vma lma, vma;
2231 int size;
2232
2233 vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
2234 lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
2235 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2236 name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
2237 printf_filtered ("Section %s, loaded at %08x - %08x, ",
2238 name, lma, lma + size);
2239 printf_filtered ("mapped at %08x - %08x\n",
2240 vma, vma + size);
2241 nmapped ++;
2242 }
2243 if (nmapped == 0)
2244 printf_filtered ("No sections are mapped.\n");
2245}
2246
2247/* Function: map_overlay_command
2248 Mark the named section as mapped (ie. residing at its VMA address). */
2249
2250void
2251map_overlay_command (args, from_tty)
2252 char *args;
2253 int from_tty;
2254{
2255 struct objfile *objfile, *objfile2;
2256 struct obj_section *sec, *sec2;
2257 asection *bfdsec;
2258
2259 if (!overlay_debugging)
2260 error ("Overlay debugging not enabled. Use the 'OVERLAY ON' command.");
2261
2262 if (args == 0 || *args == 0)
2263 error ("Argument required: name of an overlay section");
2264
2265 /* First, find a section matching the user supplied argument */
2266 ALL_OBJSECTIONS (objfile, sec)
2267 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
2268 {
2269 /* Now, check to see if the section is an overlay. */
2270 bfdsec = sec->the_bfd_section;
2271 if (!section_is_overlay (bfdsec))
2272 continue; /* not an overlay section */
2273
2274 /* Mark the overlay as "mapped" */
2275 sec->ovly_mapped = 1;
2276
2277 /* Next, make a pass and unmap any sections that are
2278 overlapped by this new section: */
2279 ALL_OBJSECTIONS (objfile2, sec2)
2280 if (sec2->ovly_mapped &&
2281 sec != sec2 &&
2282 sec->the_bfd_section != sec2->the_bfd_section &&
2283 (pc_in_mapped_range (sec2->addr, sec->the_bfd_section) ||
2284 pc_in_mapped_range (sec2->endaddr, sec->the_bfd_section)))
2285 {
2286 if (info_verbose)
2287 printf_filtered ("Note: section %s unmapped by overlap\n",
2288 bfd_section_name (objfile->obfd,
2289 sec2->the_bfd_section));
2290 sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2 */
2291 }
2292 return;
2293 }
2294 error ("No overlay section called %s", args);
2295}
2296
2297/* Function: unmap_overlay_command
2298 Mark the overlay section as unmapped
2299 (ie. resident in its LMA address range, rather than the VMA range). */
2300
2301void
2302unmap_overlay_command (args, from_tty)
2303 char *args;
2304 int from_tty;
2305{
2306 struct objfile *objfile;
2307 struct obj_section *sec;
2308
2309 if (!overlay_debugging)
2310 error ("Overlay debugging not enabled. Use the 'OVERLAY ON' command.");
2311
2312 if (args == 0 || *args == 0)
2313 error ("Argument required: name of an overlay section");
2314
2315 /* First, find a section matching the user supplied argument */
2316 ALL_OBJSECTIONS (objfile, sec)
2317 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
2318 {
2319 if (!sec->ovly_mapped)
2320 error ("Section %s is not mapped", args);
2321 sec->ovly_mapped = 0;
2322 return;
2323 }
2324 error ("No overlay section called %s", args);
2325}
2326
2327/* Function: overlay_auto_command
2328 A utility command to turn on overlay debugging.
2329 Possibly this should be done via a set/show command. */
2330
2331static void
2332overlay_auto_command (args, from_tty)
2333{
2334 overlay_debugging = -1;
2335 if (info_verbose)
2336 printf_filtered ("Automatic overlay debugging enabled.");
2337}
2338
2339/* Function: overlay_manual_command
2340 A utility command to turn on overlay debugging.
2341 Possibly this should be done via a set/show command. */
2342
2343static void
2344overlay_manual_command (args, from_tty)
2345{
2346 overlay_debugging = 1;
2347 if (info_verbose)
2348 printf_filtered ("Overlay debugging enabled.");
2349}
2350
2351/* Function: overlay_off_command
2352 A utility command to turn on overlay debugging.
2353 Possibly this should be done via a set/show command. */
2354
2355static void
2356overlay_off_command (args, from_tty)
2357{
2358 overlay_debugging = 0;
2359 if (info_verbose)
2360 printf_filtered ("Overlay debugging disabled.");
2361}
2362
2363static void
2364overlay_load_command (args, from_tty)
2365{
2366 if (target_overlay_update)
2367 (*target_overlay_update) (NULL);
2368 else
2369 error ("This target does not know how to read its overlay state.");
2370}
2371
2372/* Function: overlay_command
2373 A place-holder for a mis-typed command */
2374
2375/* Command list chain containing all defined "overlay" subcommands. */
2376struct cmd_list_element *overlaylist;
2377
2378static void
2379overlay_command (args, from_tty)
2380 char *args;
2381 int from_tty;
2382{
2383 printf_unfiltered
2384 ("\"overlay\" must be followed by the name of an overlay command.\n");
2385 help_list (overlaylist, "overlay ", -1, gdb_stdout);
2386}
2387
2388
2389/* Target Overlays for the "Simplest" overlay manager:
2390
2391 This is GDB's default target overlay layer. It works with the
2392 minimal overlay manager supplied as an example by Cygnus. The
2393 entry point is via a function pointer "target_overlay_update",
2394 so targets that use a different runtime overlay manager can
2395 substitute their own overlay_update function and take over the
2396 function pointer.
2397
2398 The overlay_update function pokes around in the target's data structures
2399 to see what overlays are mapped, and updates GDB's overlay mapping with
2400 this information.
2401
2402 In this simple implementation, the target data structures are as follows:
2403 unsigned _novlys; /# number of overlay sections #/
2404 unsigned _ovly_table[_novlys][4] = {
2405 {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/
2406 {..., ..., ..., ...},
2407 }
2408 unsigned _novly_regions; /# number of overlay regions #/
2409 unsigned _ovly_region_table[_novly_regions][3] = {
2410 {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
2411 {..., ..., ...},
2412 }
2413 These functions will attempt to update GDB's mappedness state in the
2414 symbol section table, based on the target's mappedness state.
2415
2416 To do this, we keep a cached copy of the target's _ovly_table, and
2417 attempt to detect when the cached copy is invalidated. The main
2418 entry point is "simple_overlay_update(SECT), which looks up SECT in
2419 the cached table and re-reads only the entry for that section from
2420 the target (whenever possible).
2421 */
2422
2423/* Cached, dynamically allocated copies of the target data structures: */
2424static unsigned (*cache_ovly_table)[4] = 0;
2425#if 0
2426static unsigned (*cache_ovly_region_table)[3] = 0;
2427#endif
2428static unsigned cache_novlys = 0;
2429#if 0
2430static unsigned cache_novly_regions = 0;
2431#endif
2432static CORE_ADDR cache_ovly_table_base = 0;
2433#if 0
2434static CORE_ADDR cache_ovly_region_table_base = 0;
2435#endif
2436enum ovly_index { VMA, SIZE, LMA, MAPPED};
74d6ac44 2437#define TARGET_LONG_BYTES (TARGET_LONG_BIT / TARGET_CHAR_BIT)
40b647e9
FF
2438
2439/* Throw away the cached copy of _ovly_table */
2440static void
2441simple_free_overlay_table ()
2442{
2443 if (cache_ovly_table)
2444 free(cache_ovly_table);
2445 cache_novlys = 0;
2446 cache_ovly_table = NULL;
2447 cache_ovly_table_base = 0;
2448}
2449
2450#if 0
2451/* Throw away the cached copy of _ovly_region_table */
2452static void
2453simple_free_overlay_region_table ()
2454{
2455 if (cache_ovly_region_table)
2456 free(cache_ovly_region_table);
2457 cache_novly_regions = 0;
2458 cache_ovly_region_table = NULL;
2459 cache_ovly_region_table_base = 0;
2460}
2461#endif
2462
2463/* Read an array of ints from the target into a local buffer.
2464 Convert to host order. int LEN is number of ints */
2465static void
74d6ac44 2466read_target_long_array (memaddr, myaddr, len)
40b647e9
FF
2467 CORE_ADDR memaddr;
2468 unsigned int *myaddr;
2469 int len;
2470{
74d6ac44 2471 char *buf = alloca (len * TARGET_LONG_BYTES);
40b647e9
FF
2472 int i;
2473
74d6ac44 2474 read_memory (memaddr, buf, len * TARGET_LONG_BYTES);
40b647e9 2475 for (i = 0; i < len; i++)
74d6ac44
JM
2476 myaddr[i] = extract_unsigned_integer (TARGET_LONG_BYTES * i + buf,
2477 TARGET_LONG_BYTES);
40b647e9
FF
2478}
2479
2480/* Find and grab a copy of the target _ovly_table
2481 (and _novlys, which is needed for the table's size) */
2482static int
2483simple_read_overlay_table ()
2484{
2485 struct minimal_symbol *msym;
2486
2487 simple_free_overlay_table ();
2488 msym = lookup_minimal_symbol ("_novlys", 0, 0);
2489 if (msym != NULL)
2490 cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), 4);
2491 else
2492 return 0; /* failure */
2493 cache_ovly_table = (void *) xmalloc (cache_novlys * sizeof(*cache_ovly_table));
2494 if (cache_ovly_table != NULL)
2495 {
2496 msym = lookup_minimal_symbol ("_ovly_table", 0, 0);
2497 if (msym != NULL)
2498 {
2499 cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (msym);
74d6ac44
JM
2500 read_target_long_array (cache_ovly_table_base,
2501 (int *) cache_ovly_table,
2502 cache_novlys * 4);
40b647e9
FF
2503 }
2504 else
2505 return 0; /* failure */
2506 }
2507 else
2508 return 0; /* failure */
2509 return 1; /* SUCCESS */
2510}
2511
2512#if 0
2513/* Find and grab a copy of the target _ovly_region_table
2514 (and _novly_regions, which is needed for the table's size) */
2515static int
2516simple_read_overlay_region_table ()
2517{
2518 struct minimal_symbol *msym;
2519
2520 simple_free_overlay_region_table ();
2521 msym = lookup_minimal_symbol ("_novly_regions", 0, 0);
2522 if (msym != NULL)
2523 cache_novly_regions = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), 4);
2524 else
2525 return 0; /* failure */
2526 cache_ovly_region_table = (void *) xmalloc (cache_novly_regions * 12);
2527 if (cache_ovly_region_table != NULL)
2528 {
2529 msym = lookup_minimal_symbol ("_ovly_region_table", 0, 0);
2530 if (msym != NULL)
2531 {
2532 cache_ovly_region_table_base = SYMBOL_VALUE_ADDRESS (msym);
74d6ac44
JM
2533 read_target_long_array (cache_ovly_region_table_base,
2534 (int *) cache_ovly_region_table,
2535 cache_novly_regions * 3);
40b647e9
FF
2536 }
2537 else
2538 return 0; /* failure */
2539 }
2540 else
2541 return 0; /* failure */
2542 return 1; /* SUCCESS */
2543}
2544#endif
2545
2546/* Function: simple_overlay_update_1
2547 A helper function for simple_overlay_update. Assuming a cached copy
2548 of _ovly_table exists, look through it to find an entry whose vma,
2549 lma and size match those of OSECT. Re-read the entry and make sure
2550 it still matches OSECT (else the table may no longer be valid).
2551 Set OSECT's mapped state to match the entry. Return: 1 for
2552 success, 0 for failure. */
2553
2554static int
2555simple_overlay_update_1 (osect)
2556 struct obj_section *osect;
2557{
2558 int i, size;
2559
2560 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2561 for (i = 0; i < cache_novlys; i++)
2562 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2563 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2564 cache_ovly_table[i][SIZE] == size */)
40b647e9 2565 {
74d6ac44
JM
2566 read_target_long_array (cache_ovly_table_base + i * TARGET_LONG_BYTES,
2567 (int *) cache_ovly_table[i], 4);
40b647e9 2568 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2569 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2570 cache_ovly_table[i][SIZE] == size */)
40b647e9
FF
2571 {
2572 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
2573 return 1;
2574 }
2575 else /* Warning! Warning! Target's ovly table has changed! */
2576 return 0;
2577 }
2578 return 0;
2579}
2580
2581/* Function: simple_overlay_update
2582 If OSECT is NULL, then update all sections' mapped state
2583 (after re-reading the entire target _ovly_table).
2584 If OSECT is non-NULL, then try to find a matching entry in the
2585 cached ovly_table and update only OSECT's mapped state.
2586 If a cached entry can't be found or the cache isn't valid, then
2587 re-read the entire cache, and go ahead and update all sections. */
2588
2589static void
2590simple_overlay_update (osect)
2591 struct obj_section *osect;
2592{
2593 struct objfile *objfile;
2594
2595 /* Were we given an osect to look up? NULL means do all of them. */
2596 if (osect)
2597 /* Have we got a cached copy of the target's overlay table? */
2598 if (cache_ovly_table != NULL)
2599 /* Does its cached location match what's currently in the symtab? */
2600 if (cache_ovly_table_base ==
2601 SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", 0, 0)))
2602 /* Then go ahead and try to look up this single section in the cache */
2603 if (simple_overlay_update_1 (osect))
2604 /* Found it! We're done. */
2605 return;
2606
2607 /* Cached table no good: need to read the entire table anew.
2608 Or else we want all the sections, in which case it's actually
2609 more efficient to read the whole table in one block anyway. */
2610
2611 if (simple_read_overlay_table () == 0) /* read failed? No table? */
2612 {
2613 warning ("Failed to read the target overlay mapping table.");
2614 return;
2615 }
2616 /* Now may as well update all sections, even if only one was requested. */
2617 ALL_OBJSECTIONS (objfile, osect)
2618 if (section_is_overlay (osect->the_bfd_section))
2619 {
2620 int i, size;
2621
2622 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2623 for (i = 0; i < cache_novlys; i++)
2624 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2625 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2626 cache_ovly_table[i][SIZE] == size */)
40b647e9
FF
2627 { /* obj_section matches i'th entry in ovly_table */
2628 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
2629 break; /* finished with inner for loop: break out */
2630 }
2631 }
2632}
2633
2634
bd5635a1
RP
2635void
2636_initialize_symfile ()
2637{
ade40d31
RP
2638 struct cmd_list_element *c;
2639
2640 c = add_cmd ("symbol-file", class_files, symbol_file_command,
30875e1c 2641 "Load symbol table from executable file FILE.\n\
bd5635a1 2642The `file' command can also load symbol tables, as well as setting the file\n\
ade40d31
RP
2643to execute.", &cmdlist);
2644 c->completer = filename_completer;
bd5635a1 2645
ade40d31 2646 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
f3806e3b
PS
2647 "Usage: add-symbol-file FILE ADDR\n\
2648Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
2649ADDR is the starting address of the file's text.",
ade40d31
RP
2650 &cmdlist);
2651 c->completer = filename_completer;
bd5635a1 2652
f3806e3b
PS
2653 c = add_cmd ("add-shared-symbol-files", class_files,
2654 add_shared_symbol_files_command,
2655 "Load the symbols from shared objects in the dynamic linker's link map.",
2656 &cmdlist);
2657 c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
2658 &cmdlist);
2659
ade40d31 2660 c = add_cmd ("load", class_files, load_command,
bd5635a1 2661 "Dynamically load FILE into the running program, and record its symbols\n\
ade40d31
RP
2662for access from GDB.", &cmdlist);
2663 c->completer = filename_completer;
bd5635a1 2664
61a7292f
SG
2665 add_show_from_set
2666 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
2667 (char *)&symbol_reloading,
2668 "Set dynamic symbol table reloading multiple times in one run.",
2669 &setlist),
2670 &showlist);
2671
40b647e9
FF
2672 add_prefix_cmd ("overlay", class_support, overlay_command,
2673 "Commands for debugging overlays.", &overlaylist,
2674 "overlay ", 0, &cmdlist);
2675
2676 add_com_alias ("ovly", "overlay", class_alias, 1);
2677 add_com_alias ("ov", "overlay", class_alias, 1);
2678
2679 add_cmd ("map-overlay", class_support, map_overlay_command,
2680 "Assert that an overlay section is mapped.", &overlaylist);
2681
2682 add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
2683 "Assert that an overlay section is unmapped.", &overlaylist);
2684
2685 add_cmd ("list-overlays", class_support, list_overlays_command,
2686 "List mappings of overlay sections.", &overlaylist);
2687
2688 add_cmd ("manual", class_support, overlay_manual_command,
2689 "Enable overlay debugging.", &overlaylist);
2690 add_cmd ("off", class_support, overlay_off_command,
2691 "Disable overlay debugging.", &overlaylist);
2692 add_cmd ("auto", class_support, overlay_auto_command,
2693 "Enable automatic overlay debugging.", &overlaylist);
2694 add_cmd ("load-target", class_support, overlay_load_command,
2695 "Read the overlay mapping state from the target.", &overlaylist);
bd5635a1 2696}
This page took 0.497838 seconds and 4 git commands to generate.