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