Fix seg fault when displaying linker error message
[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. */
74d6ac44
JM
659 if (pre_add_symbol_hook)
660 pre_add_symbol_hook (name);
fadce6f6
KS
661 if (from_tty || info_verbose)
662 {
663 printf_filtered ("Reading symbols from %s...", name);
664 wrap_here ("");
665 gdb_flush (gdb_stdout);
318bf84f 666 }
318bf84f 667 syms_from_objfile (objfile, addr, mainline, from_tty);
80d68b1d
FF
668 }
669
670 /* We now have at least a partial symbol table. Check to see if the
671 user requested that all symbols be read on initial access via either
672 the gdb startup command line or on a per symbol file basis. Expand
673 all partial symbol tables for this objfile if so. */
b0246b3b 674
bf349b77 675 if (readnow || readnow_symbol_files)
80d68b1d 676 {
318bf84f
FF
677 if (from_tty || info_verbose)
678 {
80d68b1d
FF
679 printf_filtered ("expanding to full symbols...");
680 wrap_here ("");
199b2450 681 gdb_flush (gdb_stdout);
318bf84f 682 }
80d68b1d
FF
683
684 for (psymtab = objfile -> psymtabs;
685 psymtab != NULL;
686 psymtab = psymtab -> next)
687 {
4ed3a9ea 688 psymtab_to_symtab (psymtab);
80d68b1d
FF
689 }
690 }
691
fadce6f6
KS
692 if (post_add_symbol_hook)
693 post_add_symbol_hook ();
80d68b1d
FF
694 if (from_tty || info_verbose)
695 {
fadce6f6
KS
696 printf_filtered ("done.\n");
697 gdb_flush (gdb_stdout);
bd5635a1 698 }
80d68b1d 699
ade40d31 700 new_symfile_objfile (objfile, mainline, from_tty);
ade40d31 701
012be3ce
DP
702 target_new_objfile (objfile);
703
30875e1c 704 return (objfile);
bd5635a1
RP
705}
706
2e6784a8
SG
707/* This is the symbol-file command. Read the file, analyze its
708 symbols, and add a struct symtab to a symtab list. The syntax of
709 the command is rather bizarre--(1) buildargv implements various
710 quoting conventions which are undocumented and have little or
711 nothing in common with the way things are quoted (or not quoted)
712 elsewhere in GDB, (2) options are used, which are not generally
713 used in GDB (perhaps "set mapped on", "set readnow on" would be
714 better), (3) the order of options matters, which is contrary to GNU
715 conventions (because it is confusing and inconvenient). */
bd5635a1
RP
716
717void
30875e1c
SG
718symbol_file_command (args, from_tty)
719 char *args;
bd5635a1
RP
720 int from_tty;
721{
30875e1c 722 char **argv;
b0246b3b 723 char *name = NULL;
25200748 724 CORE_ADDR text_relocation = 0; /* text_relocation */
30875e1c 725 struct cleanup *cleanups;
318bf84f 726 int mapped = 0;
30875e1c 727 int readnow = 0;
bd5635a1
RP
728
729 dont_repeat ();
730
30875e1c 731 if (args == NULL)
bd5635a1 732 {
cba0d141
JG
733 if ((have_full_symbols () || have_partial_symbols ())
734 && from_tty
735 && !query ("Discard symbol table from `%s'? ",
736 symfile_objfile -> name))
737 error ("Not confirmed.");
738 free_all_objfiles ();
30875e1c 739 symfile_objfile = NULL;
9342ecb9
JG
740 if (from_tty)
741 {
199b2450 742 printf_unfiltered ("No symbol file now.\n");
9342ecb9 743 }
bd5635a1 744 }
30875e1c
SG
745 else
746 {
747 if ((argv = buildargv (args)) == NULL)
748 {
318bf84f 749 nomem (0);
30875e1c
SG
750 }
751 cleanups = make_cleanup (freeargv, (char *) argv);
b0246b3b 752 while (*argv != NULL)
30875e1c 753 {
2e4964ad 754 if (STREQ (*argv, "-mapped"))
30875e1c 755 {
318bf84f 756 mapped = 1;
30875e1c 757 }
2e4964ad 758 else if (STREQ (*argv, "-readnow"))
30875e1c
SG
759 {
760 readnow = 1;
761 }
b0246b3b
FF
762 else if (**argv == '-')
763 {
764 error ("unknown option `%s'", *argv);
765 }
766 else
767 {
d9389f37
KH
768 char *p;
769
770 name = *argv;
771
772 /* this is for rombug remote only, to get the text relocation by
773 using link command */
774 p = strrchr(name, '/');
775 if (p != NULL) p++;
776 else p = name;
777
778 target_link(p, &text_relocation);
779
780 if (text_relocation == (CORE_ADDR)0)
781 return;
782 else if (text_relocation == (CORE_ADDR)-1)
d5412302
JK
783 symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped,
784 readnow);
d9389f37 785 else
d5412302
JK
786 symbol_file_add (name, from_tty, (CORE_ADDR)text_relocation,
787 0, mapped, readnow);
76212295
PS
788
789 /* Getting new symbols may change our opinion about what is
790 frameless. */
791 reinit_frame_cache ();
792
d9389f37 793 set_initial_language ();
b0246b3b
FF
794 }
795 argv++;
30875e1c 796 }
2403f49b 797
b0246b3b
FF
798 if (name == NULL)
799 {
800 error ("no symbol file name was specified");
801 }
30875e1c
SG
802 do_cleanups (cleanups);
803 }
bd5635a1
RP
804}
805
e58de8a2
FF
806/* Set the initial language.
807
808 A better solution would be to record the language in the psymtab when reading
809 partial symbols, and then use it (if known) to set the language. This would
810 be a win for formats that encode the language in an easily discoverable place,
811 such as DWARF. For stabs, we can jump through hoops looking for specially
812 named symbols or try to intuit the language from the specific type of stabs
813 we find, but we can't do that until later when we read in full symbols.
814 FIXME. */
815
816static void
817set_initial_language ()
818{
819 struct partial_symtab *pst;
820 enum language lang = language_unknown;
821
822 pst = find_main_psymtab ();
823 if (pst != NULL)
824 {
825 if (pst -> filename != NULL)
826 {
827 lang = deduce_language_from_filename (pst -> filename);
828 }
829 if (lang == language_unknown)
830 {
831 /* Make C the default language */
832 lang = language_c;
833 }
834 set_language (lang);
835 expected_language = current_language; /* Don't warn the user */
836 }
837}
838
b0246b3b
FF
839/* Open file specified by NAME and hand it off to BFD for preliminary
840 analysis. Result is a newly initialized bfd *, which includes a newly
841 malloc'd` copy of NAME (tilde-expanded and made absolute).
7d9884b9 842 In case of trouble, error() is called. */
bd5635a1 843
b0246b3b
FF
844static bfd *
845symfile_bfd_open (name)
bd5635a1
RP
846 char *name;
847{
848 bfd *sym_bfd;
849 int desc;
850 char *absolute_name;
851
7d9884b9 852 name = tilde_expand (name); /* Returns 1st new malloc'd copy */
bd5635a1 853
7d9884b9 854 /* Look down path for it, allocate 2nd new malloc'd copy. */
2093fe68 855 desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
40b647e9 856#if defined(__GO32__) || defined(_WIN32)
3f17fed8
GN
857 if (desc < 0)
858 {
859 char *exename = alloca (strlen (name) + 5);
860 strcat (strcpy (exename, name), ".exe");
861 desc = openp (getenv ("PATH"), 1, exename, O_RDONLY | O_BINARY,
862 0, &absolute_name);
863 }
864#endif
b0246b3b
FF
865 if (desc < 0)
866 {
867 make_cleanup (free, name);
868 perror_with_name (name);
869 }
7d9884b9 870 free (name); /* Free 1st new malloc'd copy */
30875e1c 871 name = absolute_name; /* Keep 2nd malloc'd copy in bfd */
346168a2 872 /* It'll be freed in free_objfile(). */
bd5635a1 873
ade40d31 874 sym_bfd = bfd_fdopenr (name, gnutarget, desc);
bd5635a1
RP
875 if (!sym_bfd)
876 {
877 close (desc);
7d9884b9 878 make_cleanup (free, name);
b0246b3b 879 error ("\"%s\": can't open to read symbols: %s.", name,
c4a081e1 880 bfd_errmsg (bfd_get_error ()));
bd5635a1 881 }
e58de8a2 882 sym_bfd->cacheable = true;
bd5635a1 883
b0246b3b
FF
884 if (!bfd_check_format (sym_bfd, bfd_object))
885 {
1a494973
C
886 /* FIXME: should be checking for errors from bfd_close (for one thing,
887 on error it does not free all the storage associated with the
888 bfd). */
b0246b3b
FF
889 bfd_close (sym_bfd); /* This also closes desc */
890 make_cleanup (free, name);
891 error ("\"%s\": can't read symbols: %s.", name,
c4a081e1 892 bfd_errmsg (bfd_get_error ()));
b0246b3b 893 }
7d9884b9 894
b0246b3b 895 return (sym_bfd);
7d9884b9
JG
896}
897
80d68b1d
FF
898/* Link a new symtab_fns into the global symtab_fns list. Called on gdb
899 startup by the _initialize routine in each object file format reader,
900 to register information about each format the the reader is prepared
901 to handle. */
bd5635a1
RP
902
903void
904add_symtab_fns (sf)
905 struct sym_fns *sf;
906{
907 sf->next = symtab_fns;
908 symtab_fns = sf;
909}
910
911
912/* Initialize to read symbols from the symbol file sym_bfd. It either
80d68b1d
FF
913 returns or calls error(). The result is an initialized struct sym_fns
914 in the objfile structure, that contains cached information about the
915 symbol file. */
bd5635a1 916
80d68b1d
FF
917static void
918find_sym_fns (objfile)
7d9884b9 919 struct objfile *objfile;
bd5635a1 920{
ac88ca20 921 struct sym_fns *sf;
0eed42de 922 enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd);
c4a081e1 923 char *our_target = bfd_get_target (objfile -> obfd);
0eed42de 924
1750a5ef
SC
925 /* Special kludge for RS/6000 and PowerMac. See xcoffread.c. */
926 if (STREQ (our_target, "aixcoff-rs6000") ||
927 STREQ (our_target, "xcoff-powermac"))
0eed42de 928 our_flavour = (enum bfd_flavour)-1;
bd5635a1 929
c4a081e1
DM
930 /* Special kludge for apollo. See dstread.c. */
931 if (STREQN (our_target, "apollo", 6))
932 our_flavour = (enum bfd_flavour)-2;
933
80d68b1d 934 for (sf = symtab_fns; sf != NULL; sf = sf -> next)
bd5635a1 935 {
0eed42de 936 if (our_flavour == sf -> sym_flavour)
bd5635a1 937 {
80d68b1d
FF
938 objfile -> sf = sf;
939 return;
bd5635a1
RP
940 }
941 }
c9bd6710 942 error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.",
b0246b3b 943 bfd_get_target (objfile -> obfd));
bd5635a1
RP
944}
945\f
946/* This function runs the load command of our current target. */
947
30875e1c 948static void
bd5635a1
RP
949load_command (arg, from_tty)
950 char *arg;
951 int from_tty;
952{
f3806e3b
PS
953 if (arg == NULL)
954 arg = get_exec_file (1);
bd5635a1
RP
955 target_load (arg, from_tty);
956}
957
ade40d31
RP
958/* This version of "load" should be usable for any target. Currently
959 it is just used for remote targets, not inftarg.c or core files,
960 on the theory that only in that case is it useful.
961
962 Avoiding xmodem and the like seems like a win (a) because we don't have
963 to worry about finding it, and (b) On VMS, fork() is very slow and so
964 we don't want to run a subprocess. On the other hand, I'm not sure how
965 performance compares. */
74d6ac44
JM
966#define GENERIC_LOAD_CHUNK 256
967#define VALIDATE_DOWNLOAD 0
ade40d31
RP
968void
969generic_load (filename, from_tty)
970 char *filename;
971 int from_tty;
972{
973 struct cleanup *old_cleanups;
974 asection *s;
c4a081e1 975 bfd *loadfile_bfd;
b52cac6b 976 time_t start_time, end_time; /* Start and end times of download */
67c1413d 977 unsigned long data_count = 0; /* Number of bytes transferred to memory */
012be3ce
DP
978 int n;
979 unsigned long load_offset = 0; /* offset to add to vma for each section */
74d6ac44
JM
980 char buf[GENERIC_LOAD_CHUNK+8];
981#if VALIDATE_DOWNLOAD
982 char verify_buffer[GENERIC_LOAD_CHUNK+8] ;
983#endif
012be3ce
DP
984
985 /* enable user to specify address for downloading as 2nd arg to load */
b607efe7 986 n = sscanf(filename, "%s 0x%lx", buf, &load_offset);
012be3ce
DP
987 if (n > 1 )
988 filename = buf;
989 else
990 load_offset = 0;
c4a081e1 991
c4a081e1 992 loadfile_bfd = bfd_openr (filename, gnutarget);
ade40d31
RP
993 if (loadfile_bfd == NULL)
994 {
995 perror_with_name (filename);
996 return;
997 }
1a494973
C
998 /* FIXME: should be checking for errors from bfd_close (for one thing,
999 on error it does not free all the storage associated with the
1000 bfd). */
ade40d31
RP
1001 old_cleanups = make_cleanup (bfd_close, loadfile_bfd);
1002
1003 if (!bfd_check_format (loadfile_bfd, bfd_object))
1004 {
1005 error ("\"%s\" is not an object file: %s", filename,
c4a081e1 1006 bfd_errmsg (bfd_get_error ()));
ade40d31
RP
1007 }
1008
b52cac6b
FF
1009 start_time = time (NULL);
1010
ade40d31
RP
1011 for (s = loadfile_bfd->sections; s; s = s->next)
1012 {
1013 if (s->flags & SEC_LOAD)
77636dea
FF
1014 {
1015 bfd_size_type size;
1016
1017 size = bfd_get_section_size_before_reloc (s);
1018 if (size > 0)
1019 {
1020 char *buffer;
1021 struct cleanup *old_chain;
1022 bfd_vma lma;
74d6ac44 1023 unsigned long l = size ;
77636dea
FF
1024 int err;
1025 char *sect;
1026 unsigned long sent;
1027 unsigned long len;
1028
74d6ac44 1029 l = l > GENERIC_LOAD_CHUNK ? GENERIC_LOAD_CHUNK : l ;
77636dea
FF
1030
1031 buffer = xmalloc (size);
1032 old_chain = make_cleanup (free, buffer);
1033
1034 lma = s->lma;
1035 lma += load_offset;
1036
1037 /* Is this really necessary? I guess it gives the user something
1038 to look at during a long download. */
1039 printf_filtered ("Loading section %s, size 0x%lx lma ",
1040 bfd_get_section_name (loadfile_bfd, s),
1041 (unsigned long) size);
1042 print_address_numeric (lma, 1, gdb_stdout);
1043 printf_filtered ("\n");
1044
1045 bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);
1046
74d6ac44
JM
1047 sect = (char *) bfd_get_section_name (loadfile_bfd, s);
1048 sent = 0;
77636dea
FF
1049 do
1050 {
1051 len = (size - sent) < l ? (size - sent) : l;
1052 sent += len;
1053 err = target_write_memory (lma, buffer, len);
1054 if (ui_load_progress_hook)
1055 if (ui_load_progress_hook (sect, sent))
1056 error ("Canceled the download");
74d6ac44
JM
1057#if VALIDATE_DOWNLOAD
1058 /* Broken memories and broken monitors manifest themselves
1059 here when bring new computers to life.
1060 This doubles already slow downloads.
1061 */
1062 if (err) break ;
1063 {
1064 target_read_memory(lma,verify_buffer,len) ;
1065 if (0 != bcmp(buffer,verify_buffer,len))
1066 error("Download verify failed at %08x",
1067 (unsigned long)lma) ;
1068 }
1069
1070#endif
1071 data_count += len ;
77636dea
FF
1072 lma += len;
1073 buffer += len;
74d6ac44 1074 } /* od */
77636dea
FF
1075 while (err == 0 && sent < size);
1076
1077 if (err != 0)
1078 error ("Memory access error while loading section %s.",
1079 bfd_get_section_name (loadfile_bfd, s));
1080
1081 do_cleanups (old_chain);
1082 }
1083 }
ade40d31
RP
1084 }
1085
b52cac6b 1086 end_time = time (NULL);
74d6ac44
JM
1087 {
1088 unsigned long entry ;
1089 entry = bfd_get_start_address(loadfile_bfd) ;
1090 printf_filtered ("Start address 0x%lx , load size %d\n", entry,data_count);
1091 /* We were doing this in remote-mips.c, I suspect it is right
1092 for other targets too. */
1093 write_pc (entry);
1094 }
ade40d31
RP
1095
1096 /* FIXME: are we supposed to call symbol_file_add or not? According to
1097 a comment from remote-mips.c (where a call to symbol_file_add was
1098 commented out), making the call confuses GDB if more than one file is
1099 loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c
1100 does. */
1101
72158e71 1102 report_transfer_performance (data_count, start_time, end_time);
b52cac6b 1103
ade40d31
RP
1104 do_cleanups (old_cleanups);
1105}
1106
72158e71
SS
1107/* Report how fast the transfer went. */
1108
1109void
1110report_transfer_performance (data_count, start_time, end_time)
1111unsigned long data_count;
1112time_t start_time, end_time;
1113{
1114 printf_filtered ("Transfer rate: ");
1115 if (end_time != start_time)
1116 printf_filtered ("%d bits/sec",
1117 (data_count * 8) / (end_time - start_time));
1118 else
1119 printf_filtered ("%d bits in <1 sec", (data_count * 8));
1120 printf_filtered (".\n");
1121}
1122
61a7292f
SG
1123/* This function allows the addition of incrementally linked object files.
1124 It does not modify any state in the target, only in the debugger. */
bd5635a1 1125
e1ce8aa5 1126/* ARGSUSED */
30875e1c 1127static void
b0246b3b
FF
1128add_symbol_file_command (args, from_tty)
1129 char *args;
bd5635a1
RP
1130 int from_tty;
1131{
b0246b3b 1132 char *name = NULL;
bd5635a1 1133 CORE_ADDR text_addr;
b0246b3b 1134 char *arg;
ac88ca20
JG
1135 int readnow = 0;
1136 int mapped = 0;
bd5635a1 1137
b0246b3b 1138 dont_repeat ();
61a7292f 1139
b0246b3b
FF
1140 if (args == NULL)
1141 {
1142 error ("add-symbol-file takes a file name and an address");
1143 }
bd5635a1 1144
b0246b3b 1145 /* Make a copy of the string that we can safely write into. */
bd5635a1 1146
b0246b3b
FF
1147 args = strdup (args);
1148 make_cleanup (free, args);
1149
1150 /* Pick off any -option args and the file name. */
1151
1152 while ((*args != '\000') && (name == NULL))
1153 {
1154 while (isspace (*args)) {args++;}
1155 arg = args;
1156 while ((*args != '\000') && !isspace (*args)) {args++;}
1157 if (*args != '\000')
1158 {
1159 *args++ = '\000';
1160 }
1161 if (*arg != '-')
1162 {
1163 name = arg;
1164 }
2e4964ad 1165 else if (STREQ (arg, "-mapped"))
b0246b3b
FF
1166 {
1167 mapped = 1;
1168 }
2e4964ad 1169 else if (STREQ (arg, "-readnow"))
b0246b3b
FF
1170 {
1171 readnow = 1;
1172 }
1173 else
1174 {
1175 error ("unknown option `%s'", arg);
1176 }
1177 }
bd5635a1 1178
b0246b3b
FF
1179 /* After picking off any options and the file name, args should be
1180 left pointing at the remainder of the command line, which should
1181 be the address expression to evaluate. */
bd5635a1 1182
1340861c 1183 if (name == NULL)
b0246b3b 1184 {
1340861c 1185 error ("add-symbol-file takes a file name");
b0246b3b
FF
1186 }
1187 name = tilde_expand (name);
1188 make_cleanup (free, name);
bd5635a1 1189
1340861c
KH
1190 if (*args != '\000')
1191 {
1192 text_addr = parse_and_eval_address (args);
1193 }
1194 else
1195 {
1196 target_link(name, &text_addr);
1197 if (text_addr == (CORE_ADDR)-1)
1198 error("Don't know how to get text start location for this file");
1199 }
bd5635a1 1200
c4a081e1 1201 /* FIXME-32x64: Assumes text_addr fits in a long. */
d8ce1326 1202 if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n",
4d57c599 1203 name, local_hex_string ((unsigned long)text_addr)))
bd5635a1
RP
1204 error ("Not confirmed.");
1205
4ed3a9ea 1206 symbol_file_add (name, 0, text_addr, 0, mapped, readnow);
76212295
PS
1207
1208 /* Getting new symbols may change our opinion about what is
1209 frameless. */
1210 reinit_frame_cache ();
bd5635a1
RP
1211}
1212\f
f3806e3b
PS
1213static void
1214add_shared_symbol_files_command (args, from_tty)
1215 char *args;
1216 int from_tty;
1217{
1218#ifdef ADD_SHARED_SYMBOL_FILES
1219 ADD_SHARED_SYMBOL_FILES (args, from_tty);
1220#else
1221 error ("This command is not available in this configuration of GDB.");
1222#endif
1223}
1224\f
7d9884b9 1225/* Re-read symbols if a symbol-file has changed. */
bd5635a1
RP
1226void
1227reread_symbols ()
1228{
7d9884b9
JG
1229 struct objfile *objfile;
1230 long new_modtime;
1231 int reread_one = 0;
cba0d141
JG
1232 struct stat new_statbuf;
1233 int res;
bd5635a1
RP
1234
1235 /* With the addition of shared libraries, this should be modified,
1236 the load time should be saved in the partial symbol tables, since
1237 different tables may come from different source files. FIXME.
1238 This routine should then walk down each partial symbol table
30875e1c 1239 and see if the symbol table that it originates from has been changed */
bd5635a1 1240
7d9884b9
JG
1241 for (objfile = object_files; objfile; objfile = objfile->next) {
1242 if (objfile->obfd) {
1eeba686 1243#ifdef IBM6000_TARGET
318bf84f
FF
1244 /* If this object is from a shared library, then you should
1245 stat on the library name, not member name. */
1246
1247 if (objfile->obfd->my_archive)
1248 res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
1249 else
1250#endif
cba0d141
JG
1251 res = stat (objfile->name, &new_statbuf);
1252 if (res != 0) {
1253 /* FIXME, should use print_sys_errmsg but it's not filtered. */
1254 printf_filtered ("`%s' has disappeared; keeping its symbols.\n",
1255 objfile->name);
1256 continue;
1257 }
1258 new_modtime = new_statbuf.st_mtime;
4d57c599
JK
1259 if (new_modtime != objfile->mtime)
1260 {
1261 struct cleanup *old_cleanups;
1262 struct section_offsets *offsets;
1263 int num_offsets;
1264 int section_offsets_size;
76212295 1265 char *obfd_filename;
4d57c599
JK
1266
1267 printf_filtered ("`%s' has changed; re-reading symbols.\n",
1268 objfile->name);
1269
1270 /* There are various functions like symbol_file_add,
1271 symfile_bfd_open, syms_from_objfile, etc., which might
1272 appear to do what we want. But they have various other
1273 effects which we *don't* want. So we just do stuff
1274 ourselves. We don't worry about mapped files (for one thing,
1275 any mapped file will be out of date). */
1276
1277 /* If we get an error, blow away this objfile (not sure if
1278 that is the correct response for things like shared
1279 libraries). */
1280 old_cleanups = make_cleanup (free_objfile, objfile);
1281 /* We need to do this whenever any symbols go away. */
1282 make_cleanup (clear_symtab_users, 0);
1283
1284 /* Clean up any state BFD has sitting around. We don't need
1285 to close the descriptor but BFD lacks a way of closing the
1286 BFD without closing the descriptor. */
76212295 1287 obfd_filename = bfd_get_filename (objfile->obfd);
4d57c599 1288 if (!bfd_close (objfile->obfd))
1a494973
C
1289 error ("Can't close BFD for %s: %s", objfile->name,
1290 bfd_errmsg (bfd_get_error ()));
76212295 1291 objfile->obfd = bfd_openr (obfd_filename, gnutarget);
4d57c599
JK
1292 if (objfile->obfd == NULL)
1293 error ("Can't open %s to read symbols.", objfile->name);
1294 /* bfd_openr sets cacheable to true, which is what we want. */
1295 if (!bfd_check_format (objfile->obfd, bfd_object))
1296 error ("Can't read symbols from %s: %s.", objfile->name,
c4a081e1 1297 bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1298
1299 /* Save the offsets, we will nuke them with the rest of the
1300 psymbol_obstack. */
1301 num_offsets = objfile->num_sections;
1302 section_offsets_size =
1303 sizeof (struct section_offsets)
1304 + sizeof (objfile->section_offsets->offsets) * num_offsets;
1305 offsets = (struct section_offsets *) alloca (section_offsets_size);
1306 memcpy (offsets, objfile->section_offsets, section_offsets_size);
1307
1308 /* Nuke all the state that we will re-read. Much of the following
1309 code which sets things to NULL really is necessary to tell
1310 other parts of GDB that there is nothing currently there. */
1311
1312 /* FIXME: Do we have to free a whole linked list, or is this
1313 enough? */
1314 if (objfile->global_psymbols.list)
1315 mfree (objfile->md, objfile->global_psymbols.list);
b52cac6b
FF
1316 memset (&objfile -> global_psymbols, 0,
1317 sizeof (objfile -> global_psymbols));
4d57c599
JK
1318 if (objfile->static_psymbols.list)
1319 mfree (objfile->md, objfile->static_psymbols.list);
b52cac6b
FF
1320 memset (&objfile -> static_psymbols, 0,
1321 sizeof (objfile -> static_psymbols));
4d57c599
JK
1322
1323 /* Free the obstacks for non-reusable objfiles */
2ad5709f 1324 obstack_free (&objfile -> psymbol_cache.cache, 0);
b52cac6b
FF
1325 memset (&objfile -> psymbol_cache, 0,
1326 sizeof (objfile -> psymbol_cache));
4d57c599
JK
1327 obstack_free (&objfile -> psymbol_obstack, 0);
1328 obstack_free (&objfile -> symbol_obstack, 0);
1329 obstack_free (&objfile -> type_obstack, 0);
1330 objfile->sections = NULL;
1331 objfile->symtabs = NULL;
1332 objfile->psymtabs = NULL;
1333 objfile->free_psymtabs = NULL;
1334 objfile->msymbols = NULL;
1335 objfile->minimal_symbol_count= 0;
1336 objfile->fundamental_types = NULL;
1337 if (objfile -> sf != NULL)
1338 {
1339 (*objfile -> sf -> sym_finish) (objfile);
1340 }
1341
1342 /* We never make this a mapped file. */
1343 objfile -> md = NULL;
1344 /* obstack_specify_allocation also initializes the obstack so
1345 it is empty. */
2ad5709f
FF
1346 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
1347 xmalloc, free);
4d57c599
JK
1348 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0,
1349 xmalloc, free);
1350 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0,
1351 xmalloc, free);
1352 obstack_specify_allocation (&objfile -> type_obstack, 0, 0,
1353 xmalloc, free);
1354 if (build_objfile_section_table (objfile))
1355 {
1356 error ("Can't find the file sections in `%s': %s",
c4a081e1 1357 objfile -> name, bfd_errmsg (bfd_get_error ()));
4d57c599
JK
1358 }
1359
1360 /* We use the same section offsets as from last time. I'm not
1361 sure whether that is always correct for shared libraries. */
1362 objfile->section_offsets = (struct section_offsets *)
1363 obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size);
1364 memcpy (objfile->section_offsets, offsets, section_offsets_size);
1365 objfile->num_sections = num_offsets;
1366
1367 /* What the hell is sym_new_init for, anyway? The concept of
1368 distinguishing between the main file and additional files
1369 in this way seems rather dubious. */
1370 if (objfile == symfile_objfile)
1371 (*objfile->sf->sym_new_init) (objfile);
1372
1373 (*objfile->sf->sym_init) (objfile);
1374 clear_complaints (1, 1);
1375 /* The "mainline" parameter is a hideous hack; I think leaving it
1376 zero is OK since dbxread.c also does what it needs to do if
1377 objfile->global_psymbols.size is 0. */
1378 (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0);
f3806e3b
PS
1379 if (!have_partial_symbols () && !have_full_symbols ())
1380 {
1381 wrap_here ("");
1382 printf_filtered ("(no debugging symbols found)\n");
1383 wrap_here ("");
1384 }
4d57c599
JK
1385 objfile -> flags |= OBJF_SYMS;
1386
1387 /* We're done reading the symbol file; finish off complaints. */
1388 clear_complaints (0, 1);
1389
1390 /* Getting new symbols may change our opinion about what is
1391 frameless. */
1392
1393 reinit_frame_cache ();
1394
1395 /* Discard cleanups as symbol reading was successful. */
1396 discard_cleanups (old_cleanups);
1397
1398 /* If the mtime has changed between the time we set new_modtime
1399 and now, we *want* this to be out of date, so don't call stat
1400 again now. */
1401 objfile->mtime = new_modtime;
1402 reread_one = 1;
dee7a11c
FF
1403
1404 /* Call this after reading in a new symbol table to give target
1405 dependant code a crack at the new symbols. For instance, this
1406 could be used to update the values of target-specific symbols GDB
1407 needs to keep track of (such as _sigtramp, or whatever). */
1408
1409 TARGET_SYMFILE_POSTREAD (objfile);
4d57c599 1410 }
bd5635a1 1411 }
7d9884b9
JG
1412 }
1413
1414 if (reread_one)
4d57c599 1415 clear_symtab_users ();
bd5635a1 1416}
bd5635a1 1417
bd5635a1 1418\f
7d9884b9
JG
1419enum language
1420deduce_language_from_filename (filename)
1421 char *filename;
1422{
2093fe68 1423 char *c;
7d9884b9 1424
2093fe68
RP
1425 if (0 == filename)
1426 ; /* Get default */
1427 else if (0 == (c = strrchr (filename, '.')))
1428 ; /* Get default. */
f3806e3b 1429 else if (STREQ (c, ".c"))
2093fe68 1430 return language_c;
f3806e3b 1431 else if (STREQ (c, ".cc") || STREQ (c, ".C") || STREQ (c, ".cxx")
1a494973 1432 || STREQ (c, ".cpp") || STREQ (c, ".cp") || STREQ (c, ".c++"))
2093fe68 1433 return language_cplus;
fadce6f6 1434 /* start-sanitize-java */
ffa157d8 1435 else if (STREQ (c, ".java") || STREQ (c, ".class"))
40b647e9 1436 return language_java;
fadce6f6 1437 /* end-sanitize-java */
f3806e3b 1438 else if (STREQ (c, ".ch") || STREQ (c, ".c186") || STREQ (c, ".c286"))
2093fe68 1439 return language_chill;
76212295
PS
1440 else if (STREQ (c, ".f") || STREQ (c, ".F"))
1441 return language_fortran;
f3806e3b
PS
1442 else if (STREQ (c, ".mod"))
1443 return language_m2;
1444 else if (STREQ (c, ".s") || STREQ (c, ".S"))
1445 return language_asm;
7d9884b9
JG
1446
1447 return language_unknown; /* default */
1448}
1449\f
d8ce1326
JG
1450/* allocate_symtab:
1451
1452 Allocate and partly initialize a new symbol table. Return a pointer
1453 to it. error() if no space.
1454
1455 Caller must set these fields:
1456 LINETABLE(symtab)
1457 symtab->blockvector
d8ce1326
JG
1458 symtab->dirname
1459 symtab->free_code
1460 symtab->free_ptr
1461 initialize any EXTRA_SYMTAB_INFO
1462 possibly free_named_symtabs (symtab->filename);
d8ce1326
JG
1463 */
1464
1465struct symtab *
30875e1c
SG
1466allocate_symtab (filename, objfile)
1467 char *filename;
1468 struct objfile *objfile;
d8ce1326
JG
1469{
1470 register struct symtab *symtab;
d8ce1326 1471
30875e1c
SG
1472 symtab = (struct symtab *)
1473 obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab));
4ed3a9ea 1474 memset (symtab, 0, sizeof (*symtab));
30875e1c
SG
1475 symtab -> filename = obsavestring (filename, strlen (filename),
1476 &objfile -> symbol_obstack);
1477 symtab -> fullname = NULL;
1478 symtab -> language = deduce_language_from_filename (filename);
77636dea
FF
1479 symtab -> debugformat = obsavestring ("unknown", 7,
1480 &objfile -> symbol_obstack);
d8ce1326 1481
7d9884b9 1482 /* Hook it to the objfile it comes from */
30875e1c
SG
1483
1484 symtab -> objfile = objfile;
1485 symtab -> next = objfile -> symtabs;
1486 objfile -> symtabs = symtab;
7d9884b9
JG
1487
1488#ifdef INIT_EXTRA_SYMTAB_INFO
30875e1c 1489 INIT_EXTRA_SYMTAB_INFO (symtab);
7d9884b9 1490#endif
d8ce1326 1491
30875e1c 1492 return (symtab);
d8ce1326 1493}
30875e1c
SG
1494
1495struct partial_symtab *
1496allocate_psymtab (filename, objfile)
1497 char *filename;
1498 struct objfile *objfile;
1499{
1500 struct partial_symtab *psymtab;
1501
cba0d141
JG
1502 if (objfile -> free_psymtabs)
1503 {
1504 psymtab = objfile -> free_psymtabs;
1505 objfile -> free_psymtabs = psymtab -> next;
1506 }
1507 else
1508 psymtab = (struct partial_symtab *)
1509 obstack_alloc (&objfile -> psymbol_obstack,
1510 sizeof (struct partial_symtab));
1511
4ed3a9ea 1512 memset (psymtab, 0, sizeof (struct partial_symtab));
30875e1c
SG
1513 psymtab -> filename = obsavestring (filename, strlen (filename),
1514 &objfile -> psymbol_obstack);
1515 psymtab -> symtab = NULL;
1516
77636dea
FF
1517 /* Prepend it to the psymtab list for the objfile it belongs to.
1518 Psymtabs are searched in most recent inserted -> least recent
1519 inserted order. */
30875e1c
SG
1520
1521 psymtab -> objfile = objfile;
1522 psymtab -> next = objfile -> psymtabs;
1523 objfile -> psymtabs = psymtab;
77636dea
FF
1524#if 0
1525 {
1526 struct partial_symtab **prev_pst;
1527 psymtab -> objfile = objfile;
1528 psymtab -> next = NULL;
1529 prev_pst = &(objfile -> psymtabs);
1530 while ((*prev_pst) != NULL)
1531 prev_pst = &((*prev_pst) -> next);
1532 (*prev_pst) = psymtab;
1533 }
1534#endif
30875e1c
SG
1535
1536 return (psymtab);
1537}
1538
77636dea
FF
1539void
1540discard_psymtab (pst)
1541 struct partial_symtab *pst;
1542{
1543 struct partial_symtab **prev_pst;
1544
1545 /* From dbxread.c:
1546 Empty psymtabs happen as a result of header files which don't
1547 have any symbols in them. There can be a lot of them. But this
1548 check is wrong, in that a psymtab with N_SLINE entries but
1549 nothing else is not empty, but we don't realize that. Fixing
1550 that without slowing things down might be tricky. */
1551
1552 /* First, snip it out of the psymtab chain */
1553
1554 prev_pst = &(pst->objfile->psymtabs);
1555 while ((*prev_pst) != pst)
1556 prev_pst = &((*prev_pst)->next);
1557 (*prev_pst) = pst->next;
1558
1559 /* Next, put it on a free list for recycling */
1560
1561 pst->next = pst->objfile->free_psymtabs;
1562 pst->objfile->free_psymtabs = pst;
1563}
1564
d8ce1326 1565\f
ade40d31 1566/* Reset all data structures in gdb which may contain references to symbol
40b647e9 1567 table data. */
ade40d31
RP
1568
1569void
1570clear_symtab_users ()
1571{
1572 /* Someday, we should do better than this, by only blowing away
1573 the things that really need to be blown. */
1574 clear_value_history ();
1575 clear_displays ();
1576 clear_internalvars ();
1577 breakpoint_re_set ();
1578 set_default_breakpoint (0, 0, 0, 0);
1579 current_source_symtab = 0;
1580 current_source_line = 0;
4d57c599 1581 clear_pc_function_cache ();
012be3ce 1582 target_new_objfile (NULL);
ade40d31
RP
1583}
1584
9d199712
JG
1585/* clear_symtab_users_once:
1586
1587 This function is run after symbol reading, or from a cleanup.
1588 If an old symbol table was obsoleted, the old symbol table
1589 has been blown away, but the other GDB data structures that may
1590 reference it have not yet been cleared or re-directed. (The old
1591 symtab was zapped, and the cleanup queued, in free_named_symtab()
1592 below.)
1593
1594 This function can be queued N times as a cleanup, or called
1595 directly; it will do all the work the first time, and then will be a
1596 no-op until the next time it is queued. This works by bumping a
1597 counter at queueing time. Much later when the cleanup is run, or at
1598 the end of symbol processing (in case the cleanup is discarded), if
1599 the queued count is greater than the "done-count", we do the work
1600 and set the done-count to the queued count. If the queued count is
1601 less than or equal to the done-count, we just ignore the call. This
1602 is needed because reading a single .o file will often replace many
1603 symtabs (one per .h file, for example), and we don't want to reset
1604 the breakpoints N times in the user's face.
1605
1606 The reason we both queue a cleanup, and call it directly after symbol
1607 reading, is because the cleanup protects us in case of errors, but is
1608 discarded if symbol reading is successful. */
1609
ade40d31 1610#if 0
996ccb30
JK
1611/* FIXME: As free_named_symtabs is currently a big noop this function
1612 is no longer needed. */
ade40d31
RP
1613static void
1614clear_symtab_users_once PARAMS ((void));
1615
9d199712
JG
1616static int clear_symtab_users_queued;
1617static int clear_symtab_users_done;
1618
ade40d31 1619static void
9d199712
JG
1620clear_symtab_users_once ()
1621{
1622 /* Enforce once-per-`do_cleanups'-semantics */
1623 if (clear_symtab_users_queued <= clear_symtab_users_done)
1624 return;
1625 clear_symtab_users_done = clear_symtab_users_queued;
1626
ade40d31 1627 clear_symtab_users ();
9d199712 1628}
ade40d31 1629#endif
9d199712
JG
1630
1631/* Delete the specified psymtab, and any others that reference it. */
1632
e1ce8aa5 1633static void
9d199712
JG
1634cashier_psymtab (pst)
1635 struct partial_symtab *pst;
1636{
46c28185 1637 struct partial_symtab *ps, *pprev = NULL;
9d199712
JG
1638 int i;
1639
1640 /* Find its previous psymtab in the chain */
30875e1c 1641 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1642 if (ps == pst)
1643 break;
1644 pprev = ps;
1645 }
1646
1647 if (ps) {
1648 /* Unhook it from the chain. */
30875e1c
SG
1649 if (ps == pst->objfile->psymtabs)
1650 pst->objfile->psymtabs = ps->next;
9d199712
JG
1651 else
1652 pprev->next = ps->next;
1653
1654 /* FIXME, we can't conveniently deallocate the entries in the
1655 partial_symbol lists (global_psymbols/static_psymbols) that
1656 this psymtab points to. These just take up space until all
1657 the psymtabs are reclaimed. Ditto the dependencies list and
1658 filename, which are all in the psymbol_obstack. */
1659
1660 /* We need to cashier any psymtab that has this one as a dependency... */
1661again:
30875e1c 1662 for (ps = pst->objfile->psymtabs; ps; ps = ps->next) {
9d199712
JG
1663 for (i = 0; i < ps->number_of_dependencies; i++) {
1664 if (ps->dependencies[i] == pst) {
1665 cashier_psymtab (ps);
1666 goto again; /* Must restart, chain has been munged. */
1667 }
1668 }
1669 }
1670 }
1671}
1672
1673/* If a symtab or psymtab for filename NAME is found, free it along
1674 with any dependent breakpoints, displays, etc.
1675 Used when loading new versions of object modules with the "add-file"
1676 command. This is only called on the top-level symtab or psymtab's name;
1677 it is not called for subsidiary files such as .h files.
1678
1679 Return value is 1 if we blew away the environment, 0 if not.
30875e1c 1680 FIXME. The return valu appears to never be used.
9d199712
JG
1681
1682 FIXME. I think this is not the best way to do this. We should
1683 work on being gentler to the environment while still cleaning up
1684 all stray pointers into the freed symtab. */
1685
1686int
1687free_named_symtabs (name)
1688 char *name;
1689{
30875e1c
SG
1690#if 0
1691 /* FIXME: With the new method of each objfile having it's own
1692 psymtab list, this function needs serious rethinking. In particular,
1693 why was it ever necessary to toss psymtabs with specific compilation
1694 unit filenames, as opposed to all psymtabs from a particular symbol
ac88ca20
JG
1695 file? -- fnf
1696 Well, the answer is that some systems permit reloading of particular
1697 compilation units. We want to blow away any old info about these
1698 compilation units, regardless of which objfiles they arrived in. --gnu. */
1699
1700 register struct symtab *s;
1701 register struct symtab *prev;
1702 register struct partial_symtab *ps;
1703 struct blockvector *bv;
1704 int blewit = 0;
30875e1c 1705
61a7292f
SG
1706 /* We only wack things if the symbol-reload switch is set. */
1707 if (!symbol_reloading)
1708 return 0;
1709
d11c44f1
JG
1710 /* Some symbol formats have trouble providing file names... */
1711 if (name == 0 || *name == '\0')
1712 return 0;
1713
9d199712
JG
1714 /* Look for a psymtab with the specified name. */
1715
1716again2:
1717 for (ps = partial_symtab_list; ps; ps = ps->next) {
2e4964ad 1718 if (STREQ (name, ps->filename)) {
9d199712
JG
1719 cashier_psymtab (ps); /* Blow it away...and its little dog, too. */
1720 goto again2; /* Must restart, chain has been munged */
1721 }
1722 }
1723
1724 /* Look for a symtab with the specified name. */
1725
1726 for (s = symtab_list; s; s = s->next)
1727 {
2e4964ad 1728 if (STREQ (name, s->filename))
9d199712
JG
1729 break;
1730 prev = s;
1731 }
1732
1733 if (s)
1734 {
1735 if (s == symtab_list)
1736 symtab_list = s->next;
1737 else
1738 prev->next = s->next;
1739
1740 /* For now, queue a delete for all breakpoints, displays, etc., whether
1741 or not they depend on the symtab being freed. This should be
1742 changed so that only those data structures affected are deleted. */
1743
1744 /* But don't delete anything if the symtab is empty.
1745 This test is necessary due to a bug in "dbxread.c" that
1746 causes empty symtabs to be created for N_SO symbols that
1747 contain the pathname of the object file. (This problem
1748 has been fixed in GDB 3.9x). */
1749
c9bd6710
JG
1750 bv = BLOCKVECTOR (s);
1751 if (BLOCKVECTOR_NBLOCKS (bv) > 2
9d199712
JG
1752 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
1753 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
1754 {
1755 complain (&oldsyms_complaint, name);
1756
1757 clear_symtab_users_queued++;
1758 make_cleanup (clear_symtab_users_once, 0);
1759 blewit = 1;
1760 } else {
1761 complain (&empty_symtab_complaint, name);
1762 }
1763
1764 free_symtab (s);
1765 }
1766 else
d8ce1326
JG
1767 {
1768 /* It is still possible that some breakpoints will be affected
1769 even though no symtab was found, since the file might have
1770 been compiled without debugging, and hence not be associated
1771 with a symtab. In order to handle this correctly, we would need
1772 to keep a list of text address ranges for undebuggable files.
1773 For now, we do nothing, since this is a fairly obscure case. */
1774 ;
1775 }
9d199712 1776
30875e1c 1777 /* FIXME, what about the minimal symbol table? */
9d199712 1778 return blewit;
30875e1c
SG
1779#else
1780 return (0);
1781#endif
9d199712
JG
1782}
1783\f
d4ea2aba
PB
1784/* Allocate and partially fill a partial symtab. It will be
1785 completely filled at the end of the symbol list.
1786
1787 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1788 is the address relative to which its symbols are (incremental) or 0
1789 (normal). */
1790
1791
1792struct partial_symtab *
a8e033f2 1793start_psymtab_common (objfile, section_offsets,
d4ea2aba
PB
1794 filename, textlow, global_syms, static_syms)
1795 struct objfile *objfile;
a8e033f2 1796 struct section_offsets *section_offsets;
d4ea2aba
PB
1797 char *filename;
1798 CORE_ADDR textlow;
2ad5709f
FF
1799 struct partial_symbol **global_syms;
1800 struct partial_symbol **static_syms;
d4ea2aba 1801{
30875e1c
SG
1802 struct partial_symtab *psymtab;
1803
1804 psymtab = allocate_psymtab (filename, objfile);
a8e033f2 1805 psymtab -> section_offsets = section_offsets;
30875e1c
SG
1806 psymtab -> textlow = textlow;
1807 psymtab -> texthigh = psymtab -> textlow; /* default */
1808 psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list;
1809 psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list;
1810 return (psymtab);
7d9884b9 1811}
9342ecb9 1812\f
9342ecb9
JG
1813/* Add a symbol with a long value to a psymtab.
1814 Since one arg is a struct, we pass in a ptr and deref it (sigh). */
1815
1816void
012be3ce
DP
1817add_psymbol_to_list (name, namelength, namespace, class, list, val, coreaddr,
1818 language, objfile)
9342ecb9
JG
1819 char *name;
1820 int namelength;
1750a5ef 1821 namespace_enum namespace;
9342ecb9
JG
1822 enum address_class class;
1823 struct psymbol_allocation_list *list;
012be3ce
DP
1824 long val; /* Value as a long */
1825 CORE_ADDR coreaddr; /* Value as a CORE_ADDR */
2e4964ad
FF
1826 enum language language;
1827 struct objfile *objfile;
9342ecb9 1828{
2e4964ad 1829 register struct partial_symbol *psym;
2ad5709f 1830 char *buf = alloca (namelength + 1);
012be3ce
DP
1831 /* psymbol is static so that there will be no uninitialized gaps in the
1832 structure which might contain random data, causing cache misses in
1833 bcache. */
1834 static struct partial_symbol psymbol;
2ad5709f
FF
1835
1836 /* Create local copy of the partial symbol */
1837 memcpy (buf, name, namelength);
1838 buf[namelength] = '\0';
1839 SYMBOL_NAME (&psymbol) = bcache (buf, namelength + 1, &objfile->psymbol_cache);
012be3ce
DP
1840 /* val and coreaddr are mutually exclusive, one of them *will* be zero */
1841 if (val != 0)
2e4964ad 1842 {
012be3ce
DP
1843 SYMBOL_VALUE (&psymbol) = val;
1844 }
1845 else
1846 {
1847 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
2e4964ad 1848 }
2ad5709f
FF
1849 SYMBOL_SECTION (&psymbol) = 0;
1850 SYMBOL_LANGUAGE (&psymbol) = language;
1851 PSYMBOL_NAMESPACE (&psymbol) = namespace;
1852 PSYMBOL_CLASS (&psymbol) = class;
1853 SYMBOL_INIT_LANGUAGE_SPECIFIC (&psymbol, language);
1854
1855 /* Stash the partial symbol away in the cache */
1856 psym = bcache (&psymbol, sizeof (struct partial_symbol), &objfile->psymbol_cache);
1857
1858 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
2e4964ad
FF
1859 if (list->next >= list->list + list->size)
1860 {
2ad5709f 1861 extend_psymbol_list (list, objfile);
2e4964ad 1862 }
2ad5709f 1863 *list->next++ = psym;
f1d39876 1864 OBJSTAT (objfile, n_psyms++);
9342ecb9 1865}
7d9884b9 1866
1a494973
C
1867/* Initialize storage for partial symbols. */
1868
1869void
1870init_psymbol_list (objfile, total_symbols)
1871 struct objfile *objfile;
1872 int total_symbols;
1873{
1874 /* Free any previously allocated psymbol lists. */
1875
1876 if (objfile -> global_psymbols.list)
1877 {
1878 mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
1879 }
1880 if (objfile -> static_psymbols.list)
1881 {
1882 mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
1883 }
1884
1885 /* Current best guess is that approximately a twentieth
1886 of the total symbols (in a debugging file) are global or static
1887 oriented symbols */
1888
1889 objfile -> global_psymbols.size = total_symbols / 10;
1890 objfile -> static_psymbols.size = total_symbols / 10;
77636dea
FF
1891
1892 if (objfile -> global_psymbols.size > 0)
1893 {
1894 objfile -> global_psymbols.next =
1895 objfile -> global_psymbols.list = (struct partial_symbol **)
1896 xmmalloc (objfile -> md, (objfile -> global_psymbols.size
1897 * sizeof (struct partial_symbol *)));
1898 }
1899 if (objfile -> static_psymbols.size > 0)
1900 {
1901 objfile -> static_psymbols.next =
1902 objfile -> static_psymbols.list = (struct partial_symbol **)
1903 xmmalloc (objfile -> md, (objfile -> static_psymbols.size
1904 * sizeof (struct partial_symbol *)));
1905 }
1a494973 1906}
40b647e9
FF
1907
1908/* OVERLAYS:
1909 The following code implements an abstraction for debugging overlay sections.
1910
1911 The target model is as follows:
1912 1) The gnu linker will permit multiple sections to be mapped into the
1913 same VMA, each with its own unique LMA (or load address).
1914 2) It is assumed that some runtime mechanism exists for mapping the
1915 sections, one by one, from the load address into the VMA address.
1916 3) This code provides a mechanism for gdb to keep track of which
1917 sections should be considered to be mapped from the VMA to the LMA.
1918 This information is used for symbol lookup, and memory read/write.
1919 For instance, if a section has been mapped then its contents
1920 should be read from the VMA, otherwise from the LMA.
1921
1922 Two levels of debugger support for overlays are available. One is
1923 "manual", in which the debugger relies on the user to tell it which
1924 overlays are currently mapped. This level of support is
1925 implemented entirely in the core debugger, and the information about
1926 whether a section is mapped is kept in the objfile->obj_section table.
1927
1928 The second level of support is "automatic", and is only available if
1929 the target-specific code provides functionality to read the target's
1930 overlay mapping table, and translate its contents for the debugger
1931 (by updating the mapped state information in the obj_section tables).
1932
1933 The interface is as follows:
1934 User commands:
1935 overlay map <name> -- tell gdb to consider this section mapped
1936 overlay unmap <name> -- tell gdb to consider this section unmapped
1937 overlay list -- list the sections that GDB thinks are mapped
1938 overlay read-target -- get the target's state of what's mapped
1939 overlay off/manual/auto -- set overlay debugging state
1940 Functional interface:
1941 find_pc_mapped_section(pc): if the pc is in the range of a mapped
1942 section, return that section.
1943 find_pc_overlay(pc): find any overlay section that contains
1944 the pc, either in its VMA or its LMA
1945 overlay_is_mapped(sect): true if overlay is marked as mapped
1946 section_is_overlay(sect): true if section's VMA != LMA
1947 pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
1948 pc_in_unmapped_range(...): true if pc belongs to section's LMA
1949 overlay_mapped_address(...): map an address from section's LMA to VMA
1950 overlay_unmapped_address(...): map an address from section's VMA to LMA
1951 symbol_overlayed_address(...): Return a "current" address for symbol:
1952 either in VMA or LMA depending on whether
1953 the symbol's section is currently mapped
1954 */
1955
1956/* Overlay debugging state: */
1957
1958int overlay_debugging = 0; /* 0 == off, 1 == manual, -1 == auto */
1959int overlay_cache_invalid = 0; /* True if need to refresh mapped state */
1960
1961/* Target vector for refreshing overlay mapped state */
1962static void simple_overlay_update PARAMS ((struct obj_section *));
1963void (*target_overlay_update) PARAMS ((struct obj_section *))
1964 = simple_overlay_update;
1965
1966/* Function: section_is_overlay (SECTION)
1967 Returns true if SECTION has VMA not equal to LMA, ie.
1968 SECTION is loaded at an address different from where it will "run". */
1969
1970int
1971section_is_overlay (section)
1972 asection *section;
1973{
1974 if (overlay_debugging)
1975 if (section && section->lma != 0 &&
1976 section->vma != section->lma)
1977 return 1;
1978
1979 return 0;
1980}
1981
1982/* Function: overlay_invalidate_all (void)
1983 Invalidate the mapped state of all overlay sections (mark it as stale). */
1984
1985static void
1986overlay_invalidate_all ()
1987{
1988 struct objfile *objfile;
1989 struct obj_section *sect;
1990
1991 ALL_OBJSECTIONS (objfile, sect)
1992 if (section_is_overlay (sect->the_bfd_section))
1993 sect->ovly_mapped = -1;
1994}
1995
1996/* Function: overlay_is_mapped (SECTION)
1997 Returns true if section is an overlay, and is currently mapped.
1998 Private: public access is thru function section_is_mapped.
1999
2000 Access to the ovly_mapped flag is restricted to this function, so
2001 that we can do automatic update. If the global flag
2002 OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
2003 overlay_invalidate_all. If the mapped state of the particular
2004 section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
2005
2006static int
2007overlay_is_mapped (osect)
2008 struct obj_section *osect;
2009{
2010 if (osect == 0 || !section_is_overlay (osect->the_bfd_section))
2011 return 0;
2012
2013 switch (overlay_debugging)
2014 {
2015 default:
2016 case 0: return 0; /* overlay debugging off */
2017 case -1: /* overlay debugging automatic */
2018 /* Unles there is a target_overlay_update function,
2019 there's really nothing useful to do here (can't really go auto) */
2020 if (target_overlay_update)
2021 {
2022 if (overlay_cache_invalid)
2023 {
2024 overlay_invalidate_all ();
2025 overlay_cache_invalid = 0;
2026 }
2027 if (osect->ovly_mapped == -1)
2028 (*target_overlay_update) (osect);
2029 }
2030 /* fall thru to manual case */
2031 case 1: /* overlay debugging manual */
2032 return osect->ovly_mapped == 1;
2033 }
2034}
2035
2036/* Function: section_is_mapped
2037 Returns true if section is an overlay, and is currently mapped. */
2038
2039int
2040section_is_mapped (section)
2041 asection *section;
2042{
2043 struct objfile *objfile;
2044 struct obj_section *osect;
2045
2046 if (overlay_debugging)
2047 if (section && section_is_overlay (section))
2048 ALL_OBJSECTIONS (objfile, osect)
2049 if (osect->the_bfd_section == section)
2050 return overlay_is_mapped (osect);
2051
2052 return 0;
2053}
2054
2055/* Function: pc_in_unmapped_range
2056 If PC falls into the lma range of SECTION, return true, else false. */
2057
2058CORE_ADDR
2059pc_in_unmapped_range (pc, section)
2060 CORE_ADDR pc;
2061 asection *section;
2062{
2063 int size;
2064
2065 if (overlay_debugging)
2066 if (section && section_is_overlay (section))
2067 {
2068 size = bfd_get_section_size_before_reloc (section);
2069 if (section->lma <= pc && pc < section->lma + size)
2070 return 1;
2071 }
2072 return 0;
2073}
2074
2075/* Function: pc_in_mapped_range
2076 If PC falls into the vma range of SECTION, return true, else false. */
2077
2078CORE_ADDR
2079pc_in_mapped_range (pc, section)
2080 CORE_ADDR pc;
2081 asection *section;
2082{
2083 int size;
2084
2085 if (overlay_debugging)
2086 if (section && section_is_overlay (section))
2087 {
2088 size = bfd_get_section_size_before_reloc (section);
2089 if (section->vma <= pc && pc < section->vma + size)
2090 return 1;
2091 }
2092 return 0;
2093}
2094
2095/* Function: overlay_unmapped_address (PC, SECTION)
2096 Returns the address corresponding to PC in the unmapped (load) range.
2097 May be the same as PC. */
2098
2099CORE_ADDR
2100overlay_unmapped_address (pc, section)
2101 CORE_ADDR pc;
2102 asection *section;
2103{
2104 if (overlay_debugging)
2105 if (section && section_is_overlay (section) &&
2106 pc_in_mapped_range (pc, section))
2107 return pc + section->lma - section->vma;
2108
2109 return pc;
2110}
2111
2112/* Function: overlay_mapped_address (PC, SECTION)
2113 Returns the address corresponding to PC in the mapped (runtime) range.
2114 May be the same as PC. */
2115
2116CORE_ADDR
2117overlay_mapped_address (pc, section)
2118 CORE_ADDR pc;
2119 asection *section;
2120{
2121 if (overlay_debugging)
2122 if (section && section_is_overlay (section) &&
2123 pc_in_unmapped_range (pc, section))
2124 return pc + section->vma - section->lma;
2125
2126 return pc;
2127}
2128
2129
2130/* Function: symbol_overlayed_address
2131 Return one of two addresses (relative to the VMA or to the LMA),
2132 depending on whether the section is mapped or not. */
2133
2134CORE_ADDR
2135symbol_overlayed_address (address, section)
2136 CORE_ADDR address;
2137 asection *section;
2138{
2139 if (overlay_debugging)
2140 {
2141 /* If the symbol has no section, just return its regular address. */
2142 if (section == 0)
2143 return address;
2144 /* If the symbol's section is not an overlay, just return its address */
2145 if (!section_is_overlay (section))
2146 return address;
2147 /* If the symbol's section is mapped, just return its address */
2148 if (section_is_mapped (section))
2149 return address;
2150 /*
2151 * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
2152 * then return its LOADED address rather than its vma address!!
2153 */
2154 return overlay_unmapped_address (address, section);
2155 }
2156 return address;
2157}
2158
2159/* Function: find_pc_overlay (PC)
2160 Return the best-match overlay section for PC:
2161 If PC matches a mapped overlay section's VMA, return that section.
2162 Else if PC matches an unmapped section's VMA, return that section.
2163 Else if PC matches an unmapped section's LMA, return that section. */
2164
2165asection *
2166find_pc_overlay (pc)
2167 CORE_ADDR pc;
2168{
2169 struct objfile *objfile;
2170 struct obj_section *osect, *best_match = NULL;
2171
2172 if (overlay_debugging)
2173 ALL_OBJSECTIONS (objfile, osect)
2174 if (section_is_overlay (osect->the_bfd_section))
2175 {
2176 if (pc_in_mapped_range (pc, osect->the_bfd_section))
2177 {
2178 if (overlay_is_mapped (osect))
2179 return osect->the_bfd_section;
2180 else
2181 best_match = osect;
2182 }
2183 else if (pc_in_unmapped_range (pc, osect->the_bfd_section))
2184 best_match = osect;
2185 }
2186 return best_match ? best_match->the_bfd_section : NULL;
2187}
2188
2189/* Function: find_pc_mapped_section (PC)
2190 If PC falls into the VMA address range of an overlay section that is
2191 currently marked as MAPPED, return that section. Else return NULL. */
2192
2193asection *
2194find_pc_mapped_section (pc)
2195 CORE_ADDR pc;
2196{
2197 struct objfile *objfile;
2198 struct obj_section *osect;
2199
2200 if (overlay_debugging)
2201 ALL_OBJSECTIONS (objfile, osect)
2202 if (pc_in_mapped_range (pc, osect->the_bfd_section) &&
2203 overlay_is_mapped (osect))
2204 return osect->the_bfd_section;
2205
2206 return NULL;
2207}
2208
2209/* Function: list_overlays_command
2210 Print a list of mapped sections and their PC ranges */
2211
2212void
2213list_overlays_command (args, from_tty)
2214 char *args;
2215 int from_tty;
2216{
2217 int nmapped = 0;
2218 struct objfile *objfile;
2219 struct obj_section *osect;
2220
2221 if (overlay_debugging)
2222 ALL_OBJSECTIONS (objfile, osect)
2223 if (overlay_is_mapped (osect))
2224 {
2225 const char *name;
2226 bfd_vma lma, vma;
2227 int size;
2228
2229 vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
2230 lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
2231 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2232 name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
2233 printf_filtered ("Section %s, loaded at %08x - %08x, ",
2234 name, lma, lma + size);
2235 printf_filtered ("mapped at %08x - %08x\n",
2236 vma, vma + size);
2237 nmapped ++;
2238 }
2239 if (nmapped == 0)
2240 printf_filtered ("No sections are mapped.\n");
2241}
2242
2243/* Function: map_overlay_command
2244 Mark the named section as mapped (ie. residing at its VMA address). */
2245
2246void
2247map_overlay_command (args, from_tty)
2248 char *args;
2249 int from_tty;
2250{
2251 struct objfile *objfile, *objfile2;
2252 struct obj_section *sec, *sec2;
2253 asection *bfdsec;
2254
2255 if (!overlay_debugging)
2256 error ("Overlay debugging not enabled. Use the 'OVERLAY ON' command.");
2257
2258 if (args == 0 || *args == 0)
2259 error ("Argument required: name of an overlay section");
2260
2261 /* First, find a section matching the user supplied argument */
2262 ALL_OBJSECTIONS (objfile, sec)
2263 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
2264 {
2265 /* Now, check to see if the section is an overlay. */
2266 bfdsec = sec->the_bfd_section;
2267 if (!section_is_overlay (bfdsec))
2268 continue; /* not an overlay section */
2269
2270 /* Mark the overlay as "mapped" */
2271 sec->ovly_mapped = 1;
2272
2273 /* Next, make a pass and unmap any sections that are
2274 overlapped by this new section: */
2275 ALL_OBJSECTIONS (objfile2, sec2)
2276 if (sec2->ovly_mapped &&
2277 sec != sec2 &&
2278 sec->the_bfd_section != sec2->the_bfd_section &&
2279 (pc_in_mapped_range (sec2->addr, sec->the_bfd_section) ||
2280 pc_in_mapped_range (sec2->endaddr, sec->the_bfd_section)))
2281 {
2282 if (info_verbose)
2283 printf_filtered ("Note: section %s unmapped by overlap\n",
2284 bfd_section_name (objfile->obfd,
2285 sec2->the_bfd_section));
2286 sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2 */
2287 }
2288 return;
2289 }
2290 error ("No overlay section called %s", args);
2291}
2292
2293/* Function: unmap_overlay_command
2294 Mark the overlay section as unmapped
2295 (ie. resident in its LMA address range, rather than the VMA range). */
2296
2297void
2298unmap_overlay_command (args, from_tty)
2299 char *args;
2300 int from_tty;
2301{
2302 struct objfile *objfile;
2303 struct obj_section *sec;
2304
2305 if (!overlay_debugging)
2306 error ("Overlay debugging not enabled. Use the 'OVERLAY ON' command.");
2307
2308 if (args == 0 || *args == 0)
2309 error ("Argument required: name of an overlay section");
2310
2311 /* First, find a section matching the user supplied argument */
2312 ALL_OBJSECTIONS (objfile, sec)
2313 if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
2314 {
2315 if (!sec->ovly_mapped)
2316 error ("Section %s is not mapped", args);
2317 sec->ovly_mapped = 0;
2318 return;
2319 }
2320 error ("No overlay section called %s", args);
2321}
2322
2323/* Function: overlay_auto_command
2324 A utility command to turn on overlay debugging.
2325 Possibly this should be done via a set/show command. */
2326
2327static void
2328overlay_auto_command (args, from_tty)
fadce6f6
KS
2329 char *args;
2330 int from_tty;
40b647e9
FF
2331{
2332 overlay_debugging = -1;
2333 if (info_verbose)
2334 printf_filtered ("Automatic overlay debugging enabled.");
2335}
2336
2337/* Function: overlay_manual_command
2338 A utility command to turn on overlay debugging.
2339 Possibly this should be done via a set/show command. */
2340
2341static void
2342overlay_manual_command (args, from_tty)
fadce6f6
KS
2343 char *args;
2344 int from_tty;
40b647e9
FF
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)
fadce6f6
KS
2357 char *args;
2358 int from_tty;
40b647e9
FF
2359{
2360 overlay_debugging = 0;
2361 if (info_verbose)
2362 printf_filtered ("Overlay debugging disabled.");
2363}
2364
2365static void
2366overlay_load_command (args, from_tty)
fadce6f6
KS
2367 char *args;
2368 int from_tty;
40b647e9
FF
2369{
2370 if (target_overlay_update)
2371 (*target_overlay_update) (NULL);
2372 else
2373 error ("This target does not know how to read its overlay state.");
2374}
2375
2376/* Function: overlay_command
2377 A place-holder for a mis-typed command */
2378
2379/* Command list chain containing all defined "overlay" subcommands. */
2380struct cmd_list_element *overlaylist;
2381
2382static void
2383overlay_command (args, from_tty)
2384 char *args;
2385 int from_tty;
2386{
2387 printf_unfiltered
2388 ("\"overlay\" must be followed by the name of an overlay command.\n");
2389 help_list (overlaylist, "overlay ", -1, gdb_stdout);
2390}
2391
2392
2393/* Target Overlays for the "Simplest" overlay manager:
2394
2395 This is GDB's default target overlay layer. It works with the
2396 minimal overlay manager supplied as an example by Cygnus. The
2397 entry point is via a function pointer "target_overlay_update",
2398 so targets that use a different runtime overlay manager can
2399 substitute their own overlay_update function and take over the
2400 function pointer.
2401
2402 The overlay_update function pokes around in the target's data structures
2403 to see what overlays are mapped, and updates GDB's overlay mapping with
2404 this information.
2405
2406 In this simple implementation, the target data structures are as follows:
2407 unsigned _novlys; /# number of overlay sections #/
2408 unsigned _ovly_table[_novlys][4] = {
2409 {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/
2410 {..., ..., ..., ...},
2411 }
2412 unsigned _novly_regions; /# number of overlay regions #/
2413 unsigned _ovly_region_table[_novly_regions][3] = {
2414 {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
2415 {..., ..., ...},
2416 }
2417 These functions will attempt to update GDB's mappedness state in the
2418 symbol section table, based on the target's mappedness state.
2419
2420 To do this, we keep a cached copy of the target's _ovly_table, and
2421 attempt to detect when the cached copy is invalidated. The main
2422 entry point is "simple_overlay_update(SECT), which looks up SECT in
2423 the cached table and re-reads only the entry for that section from
2424 the target (whenever possible).
2425 */
2426
2427/* Cached, dynamically allocated copies of the target data structures: */
2428static unsigned (*cache_ovly_table)[4] = 0;
2429#if 0
2430static unsigned (*cache_ovly_region_table)[3] = 0;
2431#endif
2432static unsigned cache_novlys = 0;
2433#if 0
2434static unsigned cache_novly_regions = 0;
2435#endif
2436static CORE_ADDR cache_ovly_table_base = 0;
2437#if 0
2438static CORE_ADDR cache_ovly_region_table_base = 0;
2439#endif
2440enum ovly_index { VMA, SIZE, LMA, MAPPED};
74d6ac44 2441#define TARGET_LONG_BYTES (TARGET_LONG_BIT / TARGET_CHAR_BIT)
40b647e9
FF
2442
2443/* Throw away the cached copy of _ovly_table */
2444static void
2445simple_free_overlay_table ()
2446{
2447 if (cache_ovly_table)
2448 free(cache_ovly_table);
2449 cache_novlys = 0;
2450 cache_ovly_table = NULL;
2451 cache_ovly_table_base = 0;
2452}
2453
2454#if 0
2455/* Throw away the cached copy of _ovly_region_table */
2456static void
2457simple_free_overlay_region_table ()
2458{
2459 if (cache_ovly_region_table)
2460 free(cache_ovly_region_table);
2461 cache_novly_regions = 0;
2462 cache_ovly_region_table = NULL;
2463 cache_ovly_region_table_base = 0;
2464}
2465#endif
2466
2467/* Read an array of ints from the target into a local buffer.
2468 Convert to host order. int LEN is number of ints */
2469static void
74d6ac44 2470read_target_long_array (memaddr, myaddr, len)
40b647e9
FF
2471 CORE_ADDR memaddr;
2472 unsigned int *myaddr;
2473 int len;
2474{
74d6ac44 2475 char *buf = alloca (len * TARGET_LONG_BYTES);
40b647e9
FF
2476 int i;
2477
74d6ac44 2478 read_memory (memaddr, buf, len * TARGET_LONG_BYTES);
40b647e9 2479 for (i = 0; i < len; i++)
74d6ac44
JM
2480 myaddr[i] = extract_unsigned_integer (TARGET_LONG_BYTES * i + buf,
2481 TARGET_LONG_BYTES);
40b647e9
FF
2482}
2483
2484/* Find and grab a copy of the target _ovly_table
2485 (and _novlys, which is needed for the table's size) */
2486static int
2487simple_read_overlay_table ()
2488{
2489 struct minimal_symbol *msym;
2490
2491 simple_free_overlay_table ();
2492 msym = lookup_minimal_symbol ("_novlys", 0, 0);
2493 if (msym != NULL)
2494 cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), 4);
2495 else
2496 return 0; /* failure */
2497 cache_ovly_table = (void *) xmalloc (cache_novlys * sizeof(*cache_ovly_table));
2498 if (cache_ovly_table != NULL)
2499 {
2500 msym = lookup_minimal_symbol ("_ovly_table", 0, 0);
2501 if (msym != NULL)
2502 {
2503 cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (msym);
74d6ac44
JM
2504 read_target_long_array (cache_ovly_table_base,
2505 (int *) cache_ovly_table,
2506 cache_novlys * 4);
40b647e9
FF
2507 }
2508 else
2509 return 0; /* failure */
2510 }
2511 else
2512 return 0; /* failure */
2513 return 1; /* SUCCESS */
2514}
2515
2516#if 0
2517/* Find and grab a copy of the target _ovly_region_table
2518 (and _novly_regions, which is needed for the table's size) */
2519static int
2520simple_read_overlay_region_table ()
2521{
2522 struct minimal_symbol *msym;
2523
2524 simple_free_overlay_region_table ();
2525 msym = lookup_minimal_symbol ("_novly_regions", 0, 0);
2526 if (msym != NULL)
2527 cache_novly_regions = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), 4);
2528 else
2529 return 0; /* failure */
2530 cache_ovly_region_table = (void *) xmalloc (cache_novly_regions * 12);
2531 if (cache_ovly_region_table != NULL)
2532 {
2533 msym = lookup_minimal_symbol ("_ovly_region_table", 0, 0);
2534 if (msym != NULL)
2535 {
2536 cache_ovly_region_table_base = SYMBOL_VALUE_ADDRESS (msym);
74d6ac44
JM
2537 read_target_long_array (cache_ovly_region_table_base,
2538 (int *) cache_ovly_region_table,
2539 cache_novly_regions * 3);
40b647e9
FF
2540 }
2541 else
2542 return 0; /* failure */
2543 }
2544 else
2545 return 0; /* failure */
2546 return 1; /* SUCCESS */
2547}
2548#endif
2549
2550/* Function: simple_overlay_update_1
2551 A helper function for simple_overlay_update. Assuming a cached copy
2552 of _ovly_table exists, look through it to find an entry whose vma,
2553 lma and size match those of OSECT. Re-read the entry and make sure
2554 it still matches OSECT (else the table may no longer be valid).
2555 Set OSECT's mapped state to match the entry. Return: 1 for
2556 success, 0 for failure. */
2557
2558static int
2559simple_overlay_update_1 (osect)
2560 struct obj_section *osect;
2561{
2562 int i, size;
2563
2564 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2565 for (i = 0; i < cache_novlys; i++)
2566 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2567 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2568 cache_ovly_table[i][SIZE] == size */)
40b647e9 2569 {
74d6ac44
JM
2570 read_target_long_array (cache_ovly_table_base + i * TARGET_LONG_BYTES,
2571 (int *) cache_ovly_table[i], 4);
40b647e9 2572 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2573 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2574 cache_ovly_table[i][SIZE] == size */)
40b647e9
FF
2575 {
2576 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
2577 return 1;
2578 }
2579 else /* Warning! Warning! Target's ovly table has changed! */
2580 return 0;
2581 }
2582 return 0;
2583}
2584
2585/* Function: simple_overlay_update
2586 If OSECT is NULL, then update all sections' mapped state
2587 (after re-reading the entire target _ovly_table).
2588 If OSECT is non-NULL, then try to find a matching entry in the
2589 cached ovly_table and update only OSECT's mapped state.
2590 If a cached entry can't be found or the cache isn't valid, then
2591 re-read the entire cache, and go ahead and update all sections. */
2592
2593static void
2594simple_overlay_update (osect)
2595 struct obj_section *osect;
2596{
2597 struct objfile *objfile;
2598
2599 /* Were we given an osect to look up? NULL means do all of them. */
2600 if (osect)
2601 /* Have we got a cached copy of the target's overlay table? */
2602 if (cache_ovly_table != NULL)
2603 /* Does its cached location match what's currently in the symtab? */
2604 if (cache_ovly_table_base ==
2605 SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", 0, 0)))
2606 /* Then go ahead and try to look up this single section in the cache */
2607 if (simple_overlay_update_1 (osect))
2608 /* Found it! We're done. */
2609 return;
2610
2611 /* Cached table no good: need to read the entire table anew.
2612 Or else we want all the sections, in which case it's actually
2613 more efficient to read the whole table in one block anyway. */
2614
2615 if (simple_read_overlay_table () == 0) /* read failed? No table? */
2616 {
2617 warning ("Failed to read the target overlay mapping table.");
2618 return;
2619 }
2620 /* Now may as well update all sections, even if only one was requested. */
2621 ALL_OBJSECTIONS (objfile, osect)
2622 if (section_is_overlay (osect->the_bfd_section))
2623 {
2624 int i, size;
2625
2626 size = bfd_get_section_size_before_reloc (osect->the_bfd_section);
2627 for (i = 0; i < cache_novlys; i++)
2628 if (cache_ovly_table[i][VMA] == osect->the_bfd_section->vma &&
77636dea
FF
2629 cache_ovly_table[i][LMA] == osect->the_bfd_section->lma /* &&
2630 cache_ovly_table[i][SIZE] == size */)
40b647e9
FF
2631 { /* obj_section matches i'th entry in ovly_table */
2632 osect->ovly_mapped = cache_ovly_table[i][MAPPED];
2633 break; /* finished with inner for loop: break out */
2634 }
2635 }
2636}
2637
2638
bd5635a1
RP
2639void
2640_initialize_symfile ()
2641{
ade40d31
RP
2642 struct cmd_list_element *c;
2643
2644 c = add_cmd ("symbol-file", class_files, symbol_file_command,
30875e1c 2645 "Load symbol table from executable file FILE.\n\
bd5635a1 2646The `file' command can also load symbol tables, as well as setting the file\n\
ade40d31
RP
2647to execute.", &cmdlist);
2648 c->completer = filename_completer;
bd5635a1 2649
ade40d31 2650 c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
f3806e3b
PS
2651 "Usage: add-symbol-file FILE ADDR\n\
2652Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\
2653ADDR is the starting address of the file's text.",
ade40d31
RP
2654 &cmdlist);
2655 c->completer = filename_completer;
bd5635a1 2656
f3806e3b
PS
2657 c = add_cmd ("add-shared-symbol-files", class_files,
2658 add_shared_symbol_files_command,
2659 "Load the symbols from shared objects in the dynamic linker's link map.",
2660 &cmdlist);
2661 c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
2662 &cmdlist);
2663
ade40d31 2664 c = add_cmd ("load", class_files, load_command,
bd5635a1 2665 "Dynamically load FILE into the running program, and record its symbols\n\
ade40d31
RP
2666for access from GDB.", &cmdlist);
2667 c->completer = filename_completer;
bd5635a1 2668
61a7292f
SG
2669 add_show_from_set
2670 (add_set_cmd ("symbol-reloading", class_support, var_boolean,
2671 (char *)&symbol_reloading,
2672 "Set dynamic symbol table reloading multiple times in one run.",
2673 &setlist),
2674 &showlist);
2675
40b647e9
FF
2676 add_prefix_cmd ("overlay", class_support, overlay_command,
2677 "Commands for debugging overlays.", &overlaylist,
2678 "overlay ", 0, &cmdlist);
2679
2680 add_com_alias ("ovly", "overlay", class_alias, 1);
2681 add_com_alias ("ov", "overlay", class_alias, 1);
2682
2683 add_cmd ("map-overlay", class_support, map_overlay_command,
2684 "Assert that an overlay section is mapped.", &overlaylist);
2685
2686 add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
2687 "Assert that an overlay section is unmapped.", &overlaylist);
2688
2689 add_cmd ("list-overlays", class_support, list_overlays_command,
2690 "List mappings of overlay sections.", &overlaylist);
2691
2692 add_cmd ("manual", class_support, overlay_manual_command,
2693 "Enable overlay debugging.", &overlaylist);
2694 add_cmd ("off", class_support, overlay_off_command,
2695 "Disable overlay debugging.", &overlaylist);
2696 add_cmd ("auto", class_support, overlay_auto_command,
2697 "Enable automatic overlay debugging.", &overlaylist);
2698 add_cmd ("load-target", class_support, overlay_load_command,
2699 "Read the overlay mapping state from the target.", &overlaylist);
bd5635a1 2700}
This page took 0.508026 seconds and 4 git commands to generate.