1 /* Generic symbol file reading for the GNU debugger, GDB.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 Contributed by Cygnus Support, using pieces from other GDB modules.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "arch-utils.h"
37 #include "breakpoint.h"
39 #include "complaints.h"
43 #include "filenames.h" /* for DOSish file names */
44 #include "gdb-stabs.h"
45 #include "gdb_obstack.h"
46 #include "completer.h"
49 #include "readline/readline.h"
50 #include "gdb_assert.h"
54 #include "parser-defs.h"
60 #include <sys/types.h>
62 #include "gdb_string.h"
70 int (*deprecated_ui_load_progress_hook
) (const char *section
, unsigned long num
);
71 void (*deprecated_show_load_progress
) (const char *section
,
72 unsigned long section_sent
,
73 unsigned long section_size
,
74 unsigned long total_sent
,
75 unsigned long total_size
);
76 void (*deprecated_pre_add_symbol_hook
) (const char *);
77 void (*deprecated_post_add_symbol_hook
) (void);
79 static void clear_symtab_users_cleanup (void *ignore
);
81 /* Global variables owned by this file */
82 int readnow_symbol_files
; /* Read full symbols immediately */
84 /* External variables and functions referenced. */
86 extern void report_transfer_performance (unsigned long, time_t, time_t);
88 /* Functions this file defines */
91 static int simple_read_overlay_region_table (void);
92 static void simple_free_overlay_region_table (void);
95 static void load_command (char *, int);
97 static void symbol_file_add_main_1 (char *args
, int from_tty
, int flags
);
99 static void add_symbol_file_command (char *, int);
101 bfd
*symfile_bfd_open (char *);
103 int get_section_index (struct objfile
*, char *);
105 static struct sym_fns
*find_sym_fns (bfd
*);
107 static void decrement_reading_symtab (void *);
109 static void overlay_invalidate_all (void);
111 void list_overlays_command (char *, int);
113 void map_overlay_command (char *, int);
115 void unmap_overlay_command (char *, int);
117 static void overlay_auto_command (char *, int);
119 static void overlay_manual_command (char *, int);
121 static void overlay_off_command (char *, int);
123 static void overlay_load_command (char *, int);
125 static void overlay_command (char *, int);
127 static void simple_free_overlay_table (void);
129 static void read_target_long_array (CORE_ADDR
, unsigned int *, int, int,
132 static int simple_read_overlay_table (void);
134 static int simple_overlay_update_1 (struct obj_section
*);
136 static void add_filename_language (char *ext
, enum language lang
);
138 static void info_ext_lang_command (char *args
, int from_tty
);
140 static void init_filename_language_table (void);
142 static void symfile_find_segment_sections (struct objfile
*objfile
);
144 void _initialize_symfile (void);
146 /* List of all available sym_fns. On gdb startup, each object file reader
147 calls add_symtab_fns() to register information on each format it is
150 static struct sym_fns
*symtab_fns
= NULL
;
152 /* Flag for whether user will be reloading symbols multiple times.
153 Defaults to ON for VxWorks, otherwise OFF. */
155 #ifdef SYMBOL_RELOADING_DEFAULT
156 int symbol_reloading
= SYMBOL_RELOADING_DEFAULT
;
158 int symbol_reloading
= 0;
161 show_symbol_reloading (struct ui_file
*file
, int from_tty
,
162 struct cmd_list_element
*c
, const char *value
)
164 fprintf_filtered (file
, _("\
165 Dynamic symbol table reloading multiple times in one run is %s.\n"),
169 /* If non-zero, shared library symbols will be added automatically
170 when the inferior is created, new libraries are loaded, or when
171 attaching to the inferior. This is almost always what users will
172 want to have happen; but for very large programs, the startup time
173 will be excessive, and so if this is a problem, the user can clear
174 this flag and then add the shared library symbols as needed. Note
175 that there is a potential for confusion, since if the shared
176 library symbols are not loaded, commands like "info fun" will *not*
177 report all the functions that are actually present. */
179 int auto_solib_add
= 1;
181 /* For systems that support it, a threshold size in megabytes. If
182 automatically adding a new library's symbol table to those already
183 known to the debugger would cause the total shared library symbol
184 size to exceed this threshhold, then the shlib's symbols are not
185 added. The threshold is ignored if the user explicitly asks for a
186 shlib to be added, such as when using the "sharedlibrary"
189 int auto_solib_limit
;
192 /* Make a null terminated copy of the string at PTR with SIZE characters in
193 the obstack pointed to by OBSTACKP . Returns the address of the copy.
194 Note that the string at PTR does not have to be null terminated, I.E. it
195 may be part of a larger string and we are only saving a substring. */
198 obsavestring (const char *ptr
, int size
, struct obstack
*obstackp
)
200 char *p
= (char *) obstack_alloc (obstackp
, size
+ 1);
201 /* Open-coded memcpy--saves function call time. These strings are usually
202 short. FIXME: Is this really still true with a compiler that can
205 const char *p1
= ptr
;
207 const char *end
= ptr
+ size
;
215 /* Concatenate strings S1, S2 and S3; return the new string. Space is found
216 in the obstack pointed to by OBSTACKP. */
219 obconcat (struct obstack
*obstackp
, const char *s1
, const char *s2
,
222 int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
223 char *val
= (char *) obstack_alloc (obstackp
, len
);
230 /* True if we are reading a symbol table. */
232 int currently_reading_symtab
= 0;
235 decrement_reading_symtab (void *dummy
)
237 currently_reading_symtab
--;
240 /* Increment currently_reading_symtab and return a cleanup that can be
241 used to decrement it. */
243 increment_reading_symtab (void)
245 ++currently_reading_symtab
;
246 return make_cleanup (decrement_reading_symtab
, NULL
);
249 /* Remember the lowest-addressed loadable section we've seen.
250 This function is called via bfd_map_over_sections.
252 In case of equal vmas, the section with the largest size becomes the
253 lowest-addressed loadable section.
255 If the vmas and sizes are equal, the last section is considered the
256 lowest-addressed loadable section. */
259 find_lowest_section (bfd
*abfd
, asection
*sect
, void *obj
)
261 asection
**lowest
= (asection
**) obj
;
263 if (0 == (bfd_get_section_flags (abfd
, sect
) & (SEC_ALLOC
| SEC_LOAD
)))
266 *lowest
= sect
; /* First loadable section */
267 else if (bfd_section_vma (abfd
, *lowest
) > bfd_section_vma (abfd
, sect
))
268 *lowest
= sect
; /* A lower loadable section */
269 else if (bfd_section_vma (abfd
, *lowest
) == bfd_section_vma (abfd
, sect
)
270 && (bfd_section_size (abfd
, (*lowest
))
271 <= bfd_section_size (abfd
, sect
)))
275 /* Create a new section_addr_info, with room for NUM_SECTIONS. */
277 struct section_addr_info
*
278 alloc_section_addr_info (size_t num_sections
)
280 struct section_addr_info
*sap
;
283 size
= (sizeof (struct section_addr_info
)
284 + sizeof (struct other_sections
) * (num_sections
- 1));
285 sap
= (struct section_addr_info
*) xmalloc (size
);
286 memset (sap
, 0, size
);
287 sap
->num_sections
= num_sections
;
292 /* Build (allocate and populate) a section_addr_info struct from
293 an existing section table. */
295 extern struct section_addr_info
*
296 build_section_addr_info_from_section_table (const struct target_section
*start
,
297 const struct target_section
*end
)
299 struct section_addr_info
*sap
;
300 const struct target_section
*stp
;
303 sap
= alloc_section_addr_info (end
- start
);
305 for (stp
= start
, oidx
= 0; stp
!= end
; stp
++)
307 if (bfd_get_section_flags (stp
->bfd
,
308 stp
->the_bfd_section
) & (SEC_ALLOC
| SEC_LOAD
)
309 && oidx
< end
- start
)
311 sap
->other
[oidx
].addr
= stp
->addr
;
312 sap
->other
[oidx
].name
313 = xstrdup (bfd_section_name (stp
->bfd
, stp
->the_bfd_section
));
314 sap
->other
[oidx
].sectindex
= stp
->the_bfd_section
->index
;
322 /* Create a section_addr_info from section offsets in OBJFILE. */
324 struct section_addr_info
*
325 build_section_addr_info_from_objfile (const struct objfile
*objfile
)
327 struct section_addr_info
*sap
;
329 struct bfd_section
*sec
;
331 sap
= alloc_section_addr_info (objfile
->num_sections
);
332 for (i
= 0, sec
= objfile
->obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
333 if (bfd_get_section_flags (objfile
->obfd
, sec
) & (SEC_ALLOC
| SEC_LOAD
))
335 sap
->other
[i
].addr
= (bfd_get_section_vma (objfile
->obfd
, sec
)
336 + objfile
->section_offsets
->offsets
[i
]);
337 sap
->other
[i
].name
= xstrdup (bfd_get_section_name (objfile
->obfd
,
339 sap
->other
[i
].sectindex
= sec
->index
;
346 /* Free all memory allocated by build_section_addr_info_from_section_table. */
349 free_section_addr_info (struct section_addr_info
*sap
)
353 for (idx
= 0; idx
< sap
->num_sections
; idx
++)
354 if (sap
->other
[idx
].name
)
355 xfree (sap
->other
[idx
].name
);
360 /* Initialize OBJFILE's sect_index_* members. */
362 init_objfile_sect_indices (struct objfile
*objfile
)
367 sect
= bfd_get_section_by_name (objfile
->obfd
, ".text");
369 objfile
->sect_index_text
= sect
->index
;
371 sect
= bfd_get_section_by_name (objfile
->obfd
, ".data");
373 objfile
->sect_index_data
= sect
->index
;
375 sect
= bfd_get_section_by_name (objfile
->obfd
, ".bss");
377 objfile
->sect_index_bss
= sect
->index
;
379 sect
= bfd_get_section_by_name (objfile
->obfd
, ".rodata");
381 objfile
->sect_index_rodata
= sect
->index
;
383 /* This is where things get really weird... We MUST have valid
384 indices for the various sect_index_* members or gdb will abort.
385 So if for example, there is no ".text" section, we have to
386 accomodate that. First, check for a file with the standard
387 one or two segments. */
389 symfile_find_segment_sections (objfile
);
391 /* Except when explicitly adding symbol files at some address,
392 section_offsets contains nothing but zeros, so it doesn't matter
393 which slot in section_offsets the individual sect_index_* members
394 index into. So if they are all zero, it is safe to just point
395 all the currently uninitialized indices to the first slot. But
396 beware: if this is the main executable, it may be relocated
397 later, e.g. by the remote qOffsets packet, and then this will
398 be wrong! That's why we try segments first. */
400 for (i
= 0; i
< objfile
->num_sections
; i
++)
402 if (ANOFFSET (objfile
->section_offsets
, i
) != 0)
407 if (i
== objfile
->num_sections
)
409 if (objfile
->sect_index_text
== -1)
410 objfile
->sect_index_text
= 0;
411 if (objfile
->sect_index_data
== -1)
412 objfile
->sect_index_data
= 0;
413 if (objfile
->sect_index_bss
== -1)
414 objfile
->sect_index_bss
= 0;
415 if (objfile
->sect_index_rodata
== -1)
416 objfile
->sect_index_rodata
= 0;
420 /* The arguments to place_section. */
422 struct place_section_arg
424 struct section_offsets
*offsets
;
428 /* Find a unique offset to use for loadable section SECT if
429 the user did not provide an offset. */
432 place_section (bfd
*abfd
, asection
*sect
, void *obj
)
434 struct place_section_arg
*arg
= obj
;
435 CORE_ADDR
*offsets
= arg
->offsets
->offsets
, start_addr
;
437 ULONGEST align
= ((ULONGEST
) 1) << bfd_get_section_alignment (abfd
, sect
);
439 /* We are only interested in allocated sections. */
440 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
443 /* If the user specified an offset, honor it. */
444 if (offsets
[sect
->index
] != 0)
447 /* Otherwise, let's try to find a place for the section. */
448 start_addr
= (arg
->lowest
+ align
- 1) & -align
;
455 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
; cur_sec
= cur_sec
->next
)
457 int indx
= cur_sec
->index
;
458 CORE_ADDR cur_offset
;
460 /* We don't need to compare against ourself. */
464 /* We can only conflict with allocated sections. */
465 if ((bfd_get_section_flags (abfd
, cur_sec
) & SEC_ALLOC
) == 0)
468 /* If the section offset is 0, either the section has not been placed
469 yet, or it was the lowest section placed (in which case LOWEST
470 will be past its end). */
471 if (offsets
[indx
] == 0)
474 /* If this section would overlap us, then we must move up. */
475 if (start_addr
+ bfd_get_section_size (sect
) > offsets
[indx
]
476 && start_addr
< offsets
[indx
] + bfd_get_section_size (cur_sec
))
478 start_addr
= offsets
[indx
] + bfd_get_section_size (cur_sec
);
479 start_addr
= (start_addr
+ align
- 1) & -align
;
484 /* Otherwise, we appear to be OK. So far. */
489 offsets
[sect
->index
] = start_addr
;
490 arg
->lowest
= start_addr
+ bfd_get_section_size (sect
);
493 /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
494 filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
498 relative_addr_info_to_section_offsets (struct section_offsets
*section_offsets
,
500 struct section_addr_info
*addrs
)
504 memset (section_offsets
, 0, SIZEOF_N_SECTION_OFFSETS (num_sections
));
506 /* Now calculate offsets for section that were specified by the caller. */
507 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
509 struct other_sections
*osp
;
511 osp
= &addrs
->other
[i
];
515 /* Record all sections in offsets */
516 /* The section_offsets in the objfile are here filled in using
518 section_offsets
->offsets
[osp
->sectindex
] = osp
->addr
;
522 /* Relativize absolute addresses in ADDRS into offsets based on ABFD. Fill-in
523 also SECTINDEXes specific to ABFD there. This function can be used to
524 rebase ADDRS to start referencing different BFD than before. */
527 addr_info_make_relative (struct section_addr_info
*addrs
, bfd
*abfd
)
529 asection
*lower_sect
;
530 CORE_ADDR lower_offset
;
533 /* Find lowest loadable section to be used as starting point for
534 continguous sections. */
536 bfd_map_over_sections (abfd
, find_lowest_section
, &lower_sect
);
537 if (lower_sect
== NULL
)
539 warning (_("no loadable sections found in added symbol-file %s"),
540 bfd_get_filename (abfd
));
544 lower_offset
= bfd_section_vma (bfd_get_filename (abfd
), lower_sect
);
546 /* Calculate offsets for the loadable sections.
547 FIXME! Sections must be in order of increasing loadable section
548 so that contiguous sections can use the lower-offset!!!
550 Adjust offsets if the segments are not contiguous.
551 If the section is contiguous, its offset should be set to
552 the offset of the highest loadable section lower than it
553 (the loadable section directly below it in memory).
554 this_offset = lower_offset = lower_addr - lower_orig_addr */
556 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
558 const char *sect_name
= addrs
->other
[i
].name
;
559 asection
*sect
= bfd_get_section_by_name (abfd
, sect_name
);
563 /* This is the index used by BFD. */
564 addrs
->other
[i
].sectindex
= sect
->index
;
566 if (addrs
->other
[i
].addr
!= 0)
568 addrs
->other
[i
].addr
-= bfd_section_vma (abfd
, sect
);
569 lower_offset
= addrs
->other
[i
].addr
;
572 addrs
->other
[i
].addr
= lower_offset
;
576 /* This section does not exist in ABFD, which is normally
577 unexpected and we want to issue a warning.
579 However, the ELF prelinker does create a few sections which are
580 marked in the main executable as loadable (they are loaded in
581 memory from the DYNAMIC segment) and yet are not present in
582 separate debug info files. This is fine, and should not cause
583 a warning. Shared libraries contain just the section
584 ".gnu.liblist" but it is not marked as loadable there. There is
585 no other way to identify them than by their name as the sections
586 created by prelink have no special flags. */
588 if (!(strcmp (sect_name
, ".gnu.liblist") == 0
589 || strcmp (sect_name
, ".gnu.conflict") == 0
590 || strcmp (sect_name
, ".dynbss") == 0
591 || strcmp (sect_name
, ".sdynbss") == 0))
592 warning (_("section %s not found in %s"), sect_name
,
593 bfd_get_filename (abfd
));
595 addrs
->other
[i
].addr
= 0;
597 /* SECTINDEX is invalid if ADDR is zero. */
602 /* Parse the user's idea of an offset for dynamic linking, into our idea
603 of how to represent it for fast symbol reading. This is the default
604 version of the sym_fns.sym_offsets function for symbol readers that
605 don't need to do anything special. It allocates a section_offsets table
606 for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */
609 default_symfile_offsets (struct objfile
*objfile
,
610 struct section_addr_info
*addrs
)
612 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
613 objfile
->section_offsets
= (struct section_offsets
*)
614 obstack_alloc (&objfile
->objfile_obstack
,
615 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
616 relative_addr_info_to_section_offsets (objfile
->section_offsets
,
617 objfile
->num_sections
, addrs
);
619 /* For relocatable files, all loadable sections will start at zero.
620 The zero is meaningless, so try to pick arbitrary addresses such
621 that no loadable sections overlap. This algorithm is quadratic,
622 but the number of sections in a single object file is generally
624 if ((bfd_get_file_flags (objfile
->obfd
) & (EXEC_P
| DYNAMIC
)) == 0)
626 struct place_section_arg arg
;
627 bfd
*abfd
= objfile
->obfd
;
629 CORE_ADDR lowest
= 0;
631 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
; cur_sec
= cur_sec
->next
)
632 /* We do not expect this to happen; just skip this step if the
633 relocatable file has a section with an assigned VMA. */
634 if (bfd_section_vma (abfd
, cur_sec
) != 0)
639 CORE_ADDR
*offsets
= objfile
->section_offsets
->offsets
;
641 /* Pick non-overlapping offsets for sections the user did not
643 arg
.offsets
= objfile
->section_offsets
;
645 bfd_map_over_sections (objfile
->obfd
, place_section
, &arg
);
647 /* Correctly filling in the section offsets is not quite
648 enough. Relocatable files have two properties that
649 (most) shared objects do not:
651 - Their debug information will contain relocations. Some
652 shared libraries do also, but many do not, so this can not
655 - If there are multiple code sections they will be loaded
656 at different relative addresses in memory than they are
657 in the objfile, since all sections in the file will start
660 Because GDB has very limited ability to map from an
661 address in debug info to the correct code section,
662 it relies on adding SECT_OFF_TEXT to things which might be
663 code. If we clear all the section offsets, and set the
664 section VMAs instead, then symfile_relocate_debug_section
665 will return meaningful debug information pointing at the
668 GDB has too many different data structures for section
669 addresses - a bfd, objfile, and so_list all have section
670 tables, as does exec_ops. Some of these could probably
673 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
;
674 cur_sec
= cur_sec
->next
)
676 if ((bfd_get_section_flags (abfd
, cur_sec
) & SEC_ALLOC
) == 0)
679 bfd_set_section_vma (abfd
, cur_sec
, offsets
[cur_sec
->index
]);
680 exec_set_section_address (bfd_get_filename (abfd
), cur_sec
->index
,
681 offsets
[cur_sec
->index
]);
682 offsets
[cur_sec
->index
] = 0;
687 /* Remember the bfd indexes for the .text, .data, .bss and
689 init_objfile_sect_indices (objfile
);
693 /* Divide the file into segments, which are individual relocatable units.
694 This is the default version of the sym_fns.sym_segments function for
695 symbol readers that do not have an explicit representation of segments.
696 It assumes that object files do not have segments, and fully linked
697 files have a single segment. */
699 struct symfile_segment_data
*
700 default_symfile_segments (bfd
*abfd
)
704 struct symfile_segment_data
*data
;
707 /* Relocatable files contain enough information to position each
708 loadable section independently; they should not be relocated
710 if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
)) == 0)
713 /* Make sure there is at least one loadable section in the file. */
714 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
716 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
724 low
= bfd_get_section_vma (abfd
, sect
);
725 high
= low
+ bfd_get_section_size (sect
);
727 data
= XZALLOC (struct symfile_segment_data
);
728 data
->num_segments
= 1;
729 data
->segment_bases
= XCALLOC (1, CORE_ADDR
);
730 data
->segment_sizes
= XCALLOC (1, CORE_ADDR
);
732 num_sections
= bfd_count_sections (abfd
);
733 data
->segment_info
= XCALLOC (num_sections
, int);
735 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
739 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
742 vma
= bfd_get_section_vma (abfd
, sect
);
745 if (vma
+ bfd_get_section_size (sect
) > high
)
746 high
= vma
+ bfd_get_section_size (sect
);
748 data
->segment_info
[i
] = 1;
751 data
->segment_bases
[0] = low
;
752 data
->segment_sizes
[0] = high
- low
;
757 /* Process a symbol file, as either the main file or as a dynamically
760 OBJFILE is where the symbols are to be read from.
762 ADDRS is the list of section load addresses. If the user has given
763 an 'add-symbol-file' command, then this is the list of offsets and
764 addresses he or she provided as arguments to the command; or, if
765 we're handling a shared library, these are the actual addresses the
766 sections are loaded at, according to the inferior's dynamic linker
767 (as gleaned by GDB's shared library code). We convert each address
768 into an offset from the section VMA's as it appears in the object
769 file, and then call the file's sym_offsets function to convert this
770 into a format-specific offset table --- a `struct section_offsets'.
771 If ADDRS is non-zero, OFFSETS must be zero.
773 OFFSETS is a table of section offsets already in the right
774 format-specific representation. NUM_OFFSETS is the number of
775 elements present in OFFSETS->offsets. If OFFSETS is non-zero, we
776 assume this is the proper table the call to sym_offsets described
777 above would produce. Instead of calling sym_offsets, we just dump
778 it right into objfile->section_offsets. (When we're re-reading
779 symbols from an objfile, we don't have the original load address
780 list any more; all we have is the section offset table.) If
781 OFFSETS is non-zero, ADDRS must be zero.
783 ADD_FLAGS encodes verbosity level, whether this is main symbol or
784 an extra symbol file such as dynamically loaded code, and wether
785 breakpoint reset should be deferred. */
788 syms_from_objfile (struct objfile
*objfile
,
789 struct section_addr_info
*addrs
,
790 struct section_offsets
*offsets
,
794 struct section_addr_info
*local_addr
= NULL
;
795 struct cleanup
*old_chain
;
796 const int mainline
= add_flags
& SYMFILE_MAINLINE
;
798 gdb_assert (! (addrs
&& offsets
));
800 init_entry_point_info (objfile
);
801 objfile
->sf
= find_sym_fns (objfile
->obfd
);
803 if (objfile
->sf
== NULL
)
804 return; /* No symbols. */
806 /* Make sure that partially constructed symbol tables will be cleaned up
807 if an error occurs during symbol reading. */
808 old_chain
= make_cleanup_free_objfile (objfile
);
810 /* If ADDRS and OFFSETS are both NULL, put together a dummy address
811 list. We now establish the convention that an addr of zero means
812 no load address was specified. */
813 if (! addrs
&& ! offsets
)
816 = alloc_section_addr_info (bfd_count_sections (objfile
->obfd
));
817 make_cleanup (xfree
, local_addr
);
821 /* Now either addrs or offsets is non-zero. */
825 /* We will modify the main symbol table, make sure that all its users
826 will be cleaned up if an error occurs during symbol reading. */
827 make_cleanup (clear_symtab_users_cleanup
, 0 /*ignore*/);
829 /* Since no error yet, throw away the old symbol table. */
831 if (symfile_objfile
!= NULL
)
833 free_objfile (symfile_objfile
);
834 gdb_assert (symfile_objfile
== NULL
);
837 /* Currently we keep symbols from the add-symbol-file command.
838 If the user wants to get rid of them, they should do "symbol-file"
839 without arguments first. Not sure this is the best behavior
842 (*objfile
->sf
->sym_new_init
) (objfile
);
845 /* Convert addr into an offset rather than an absolute address.
846 We find the lowest address of a loaded segment in the objfile,
847 and assume that <addr> is where that got loaded.
849 We no longer warn if the lowest section is not a text segment (as
850 happens for the PA64 port. */
851 if (addrs
&& addrs
->other
[0].name
)
852 addr_info_make_relative (addrs
, objfile
->obfd
);
854 /* Initialize symbol reading routines for this objfile, allow complaints to
855 appear for this new file, and record how verbose to be, then do the
856 initial symbol reading for this file. */
858 (*objfile
->sf
->sym_init
) (objfile
);
859 clear_complaints (&symfile_complaints
, 1, add_flags
& SYMFILE_VERBOSE
);
862 (*objfile
->sf
->sym_offsets
) (objfile
, addrs
);
865 size_t size
= SIZEOF_N_SECTION_OFFSETS (num_offsets
);
867 /* Just copy in the offset table directly as given to us. */
868 objfile
->num_sections
= num_offsets
;
869 objfile
->section_offsets
870 = ((struct section_offsets
*)
871 obstack_alloc (&objfile
->objfile_obstack
, size
));
872 memcpy (objfile
->section_offsets
, offsets
, size
);
874 init_objfile_sect_indices (objfile
);
877 (*objfile
->sf
->sym_read
) (objfile
, add_flags
);
879 /* Discard cleanups as symbol reading was successful. */
881 discard_cleanups (old_chain
);
885 /* Perform required actions after either reading in the initial
886 symbols for a new objfile, or mapping in the symbols from a reusable
890 new_symfile_objfile (struct objfile
*objfile
, int add_flags
)
893 /* If this is the main symbol file we have to clean up all users of the
894 old main symbol file. Otherwise it is sufficient to fixup all the
895 breakpoints that may have been redefined by this symbol file. */
896 if (add_flags
& SYMFILE_MAINLINE
)
898 /* OK, make it the "real" symbol file. */
899 symfile_objfile
= objfile
;
901 clear_symtab_users ();
903 else if ((add_flags
& SYMFILE_DEFER_BP_RESET
) == 0)
905 breakpoint_re_set ();
908 /* We're done reading the symbol file; finish off complaints. */
909 clear_complaints (&symfile_complaints
, 0, add_flags
& SYMFILE_VERBOSE
);
912 /* Process a symbol file, as either the main file or as a dynamically
915 ABFD is a BFD already open on the file, as from symfile_bfd_open.
916 This BFD will be closed on error, and is always consumed by this function.
918 ADD_FLAGS encodes verbosity, whether this is main symbol file or
919 extra, such as dynamically loaded code, and what to do with breakpoins.
921 ADDRS, OFFSETS, and NUM_OFFSETS are as described for
922 syms_from_objfile, above.
923 ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
925 Upon success, returns a pointer to the objfile that was added.
926 Upon failure, jumps back to command level (never returns). */
928 static struct objfile
*
929 symbol_file_add_with_addrs_or_offsets (bfd
*abfd
,
931 struct section_addr_info
*addrs
,
932 struct section_offsets
*offsets
,
936 struct objfile
*objfile
;
937 struct cleanup
*my_cleanups
;
938 const char *name
= bfd_get_filename (abfd
);
939 const int from_tty
= add_flags
& SYMFILE_VERBOSE
;
941 my_cleanups
= make_cleanup_bfd_close (abfd
);
943 /* Give user a chance to burp if we'd be
944 interactively wiping out any existing symbols. */
946 if ((have_full_symbols () || have_partial_symbols ())
947 && (add_flags
& SYMFILE_MAINLINE
)
949 && !query (_("Load new symbol table from \"%s\"? "), name
))
950 error (_("Not confirmed."));
952 objfile
= allocate_objfile (abfd
, flags
);
953 discard_cleanups (my_cleanups
);
955 /* We either created a new mapped symbol table, mapped an existing
956 symbol table file which has not had initial symbol reading
957 performed, or need to read an unmapped symbol table. */
958 if (from_tty
|| info_verbose
)
960 if (deprecated_pre_add_symbol_hook
)
961 deprecated_pre_add_symbol_hook (name
);
964 printf_unfiltered (_("Reading symbols from %s..."), name
);
966 gdb_flush (gdb_stdout
);
969 syms_from_objfile (objfile
, addrs
, offsets
, num_offsets
,
972 /* We now have at least a partial symbol table. Check to see if the
973 user requested that all symbols be read on initial access via either
974 the gdb startup command line or on a per symbol file basis. Expand
975 all partial symbol tables for this objfile if so. */
977 if ((flags
& OBJF_READNOW
) || readnow_symbol_files
)
979 if (from_tty
|| info_verbose
)
981 printf_unfiltered (_("expanding to full symbols..."));
983 gdb_flush (gdb_stdout
);
987 objfile
->sf
->qf
->expand_all_symtabs (objfile
);
990 if ((from_tty
|| info_verbose
)
991 && !objfile_has_symbols (objfile
))
994 printf_unfiltered (_("(no debugging symbols found)..."));
998 if (from_tty
|| info_verbose
)
1000 if (deprecated_post_add_symbol_hook
)
1001 deprecated_post_add_symbol_hook ();
1003 printf_unfiltered (_("done.\n"));
1006 /* We print some messages regardless of whether 'from_tty ||
1007 info_verbose' is true, so make sure they go out at the right
1009 gdb_flush (gdb_stdout
);
1011 do_cleanups (my_cleanups
);
1013 if (objfile
->sf
== NULL
)
1015 observer_notify_new_objfile (objfile
);
1016 return objfile
; /* No symbols. */
1019 new_symfile_objfile (objfile
, add_flags
);
1021 observer_notify_new_objfile (objfile
);
1023 bfd_cache_close_all ();
1027 /* Add BFD as a separate debug file for OBJFILE. */
1030 symbol_file_add_separate (bfd
*bfd
, int symfile_flags
, struct objfile
*objfile
)
1032 struct objfile
*new_objfile
;
1033 struct section_addr_info
*sap
;
1034 struct cleanup
*my_cleanup
;
1036 /* Create section_addr_info. We can't directly use offsets from OBJFILE
1037 because sections of BFD may not match sections of OBJFILE and because
1038 vma may have been modified by tools such as prelink. */
1039 sap
= build_section_addr_info_from_objfile (objfile
);
1040 my_cleanup
= make_cleanup_free_section_addr_info (sap
);
1042 new_objfile
= symbol_file_add_with_addrs_or_offsets
1043 (bfd
, symfile_flags
,
1045 objfile
->flags
& (OBJF_REORDERED
| OBJF_SHARED
| OBJF_READNOW
1046 | OBJF_USERLOADED
));
1048 do_cleanups (my_cleanup
);
1050 add_separate_debug_objfile (new_objfile
, objfile
);
1053 /* Process the symbol file ABFD, as either the main file or as a
1054 dynamically loaded file.
1056 See symbol_file_add_with_addrs_or_offsets's comments for
1059 symbol_file_add_from_bfd (bfd
*abfd
, int add_flags
,
1060 struct section_addr_info
*addrs
,
1063 return symbol_file_add_with_addrs_or_offsets (abfd
, add_flags
, addrs
, 0, 0,
1068 /* Process a symbol file, as either the main file or as a dynamically
1069 loaded file. See symbol_file_add_with_addrs_or_offsets's comments
1072 symbol_file_add (char *name
, int add_flags
, struct section_addr_info
*addrs
,
1075 return symbol_file_add_from_bfd (symfile_bfd_open (name
), add_flags
, addrs
,
1080 /* Call symbol_file_add() with default values and update whatever is
1081 affected by the loading of a new main().
1082 Used when the file is supplied in the gdb command line
1083 and by some targets with special loading requirements.
1084 The auxiliary function, symbol_file_add_main_1(), has the flags
1085 argument for the switches that can only be specified in the symbol_file
1089 symbol_file_add_main (char *args
, int from_tty
)
1091 symbol_file_add_main_1 (args
, from_tty
, 0);
1095 symbol_file_add_main_1 (char *args
, int from_tty
, int flags
)
1097 const int add_flags
= SYMFILE_MAINLINE
| (from_tty
? SYMFILE_VERBOSE
: 0);
1098 symbol_file_add (args
, add_flags
, NULL
, flags
);
1100 /* Getting new symbols may change our opinion about
1101 what is frameless. */
1102 reinit_frame_cache ();
1104 set_initial_language ();
1108 symbol_file_clear (int from_tty
)
1110 if ((have_full_symbols () || have_partial_symbols ())
1113 ? !query (_("Discard symbol table from `%s'? "),
1114 symfile_objfile
->name
)
1115 : !query (_("Discard symbol table? "))))
1116 error (_("Not confirmed."));
1118 free_all_objfiles ();
1120 /* solib descriptors may have handles to objfiles. Since their
1121 storage has just been released, we'd better wipe the solib
1122 descriptors as well. */
1123 no_shared_libraries (NULL
, from_tty
);
1125 gdb_assert (symfile_objfile
== NULL
);
1127 printf_unfiltered (_("No symbol file now.\n"));
1131 get_debug_link_info (struct objfile
*objfile
, unsigned long *crc32_out
)
1134 bfd_size_type debuglink_size
;
1135 unsigned long crc32
;
1140 sect
= bfd_get_section_by_name (objfile
->obfd
, ".gnu_debuglink");
1145 debuglink_size
= bfd_section_size (objfile
->obfd
, sect
);
1147 contents
= xmalloc (debuglink_size
);
1148 bfd_get_section_contents (objfile
->obfd
, sect
, contents
,
1149 (file_ptr
)0, (bfd_size_type
)debuglink_size
);
1151 /* Crc value is stored after the filename, aligned up to 4 bytes. */
1152 crc_offset
= strlen (contents
) + 1;
1153 crc_offset
= (crc_offset
+ 3) & ~3;
1155 crc32
= bfd_get_32 (objfile
->obfd
, (bfd_byte
*) (contents
+ crc_offset
));
1162 separate_debug_file_exists (const char *name
, unsigned long crc
,
1163 struct objfile
*parent_objfile
)
1165 unsigned long file_crc
= 0;
1167 gdb_byte buffer
[8*1024];
1169 struct stat parent_stat
, abfd_stat
;
1171 /* Find a separate debug info file as if symbols would be present in
1172 PARENT_OBJFILE itself this function would not be called. .gnu_debuglink
1173 section can contain just the basename of PARENT_OBJFILE without any
1174 ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
1175 the separate debug infos with the same basename can exist. */
1177 if (strcmp (name
, parent_objfile
->name
) == 0)
1180 abfd
= bfd_open_maybe_remote (name
);
1185 /* Verify symlinks were not the cause of strcmp name difference above.
1187 Some operating systems, e.g. Windows, do not provide a meaningful
1188 st_ino; they always set it to zero. (Windows does provide a
1189 meaningful st_dev.) Do not indicate a duplicate library in that
1190 case. While there is no guarantee that a system that provides
1191 meaningful inode numbers will never set st_ino to zero, this is
1192 merely an optimization, so we do not need to worry about false
1195 if (bfd_stat (abfd
, &abfd_stat
) == 0
1196 && bfd_stat (parent_objfile
->obfd
, &parent_stat
) == 0
1197 && abfd_stat
.st_dev
== parent_stat
.st_dev
1198 && abfd_stat
.st_ino
== parent_stat
.st_ino
1199 && abfd_stat
.st_ino
!= 0)
1205 while ((count
= bfd_bread (buffer
, sizeof (buffer
), abfd
)) > 0)
1206 file_crc
= gnu_debuglink_crc32 (file_crc
, buffer
, count
);
1210 if (crc
!= file_crc
)
1212 warning (_("the debug information found in \"%s\""
1213 " does not match \"%s\" (CRC mismatch).\n"),
1214 name
, parent_objfile
->name
);
1221 char *debug_file_directory
= NULL
;
1223 show_debug_file_directory (struct ui_file
*file
, int from_tty
,
1224 struct cmd_list_element
*c
, const char *value
)
1226 fprintf_filtered (file
, _("\
1227 The directory where separate debug symbols are searched for is \"%s\".\n"),
1231 #if ! defined (DEBUG_SUBDIRECTORY)
1232 #define DEBUG_SUBDIRECTORY ".debug"
1236 find_separate_debug_file_by_debuglink (struct objfile
*objfile
)
1239 char *basename
, *name_copy
, *debugdir
;
1241 char *debugfile
= NULL
;
1242 char *canon_name
= NULL
;
1243 bfd_size_type debuglink_size
;
1244 unsigned long crc32
;
1247 basename
= get_debug_link_info (objfile
, &crc32
);
1249 if (basename
== NULL
)
1250 /* There's no separate debug info, hence there's no way we could
1251 load it => no warning. */
1252 goto cleanup_return_debugfile
;
1254 dir
= xstrdup (objfile
->name
);
1256 /* Strip off the final filename part, leaving the directory name,
1257 followed by a slash. Objfile names should always be absolute and
1258 tilde-expanded, so there should always be a slash in there
1260 for (i
= strlen(dir
) - 1; i
>= 0; i
--)
1262 if (IS_DIR_SEPARATOR (dir
[i
]))
1265 gdb_assert (i
>= 0 && IS_DIR_SEPARATOR (dir
[i
]));
1268 /* Set I to max (strlen (canon_name), strlen (dir)). */
1269 canon_name
= lrealpath (dir
);
1271 if (canon_name
&& strlen (canon_name
) > i
)
1272 i
= strlen (canon_name
);
1274 debugfile
= xmalloc (strlen (debug_file_directory
) + 1
1276 + strlen (DEBUG_SUBDIRECTORY
)
1281 /* First try in the same directory as the original file. */
1282 strcpy (debugfile
, dir
);
1283 strcat (debugfile
, basename
);
1285 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1286 goto cleanup_return_debugfile
;
1288 /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */
1289 strcpy (debugfile
, dir
);
1290 strcat (debugfile
, DEBUG_SUBDIRECTORY
);
1291 strcat (debugfile
, "/");
1292 strcat (debugfile
, basename
);
1294 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1295 goto cleanup_return_debugfile
;
1297 /* Then try in the global debugfile directories.
1299 Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1300 cause "/..." lookups. */
1302 debugdir
= debug_file_directory
;
1307 while (*debugdir
== DIRNAME_SEPARATOR
)
1310 debugdir_end
= strchr (debugdir
, DIRNAME_SEPARATOR
);
1311 if (debugdir_end
== NULL
)
1312 debugdir_end
= &debugdir
[strlen (debugdir
)];
1314 memcpy (debugfile
, debugdir
, debugdir_end
- debugdir
);
1315 debugfile
[debugdir_end
- debugdir
] = 0;
1316 strcat (debugfile
, "/");
1317 strcat (debugfile
, dir
);
1318 strcat (debugfile
, basename
);
1320 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1321 goto cleanup_return_debugfile
;
1323 /* If the file is in the sysroot, try using its base path in the
1324 global debugfile directory. */
1326 && strncmp (canon_name
, gdb_sysroot
, strlen (gdb_sysroot
)) == 0
1327 && IS_DIR_SEPARATOR (canon_name
[strlen (gdb_sysroot
)]))
1329 memcpy (debugfile
, debugdir
, debugdir_end
- debugdir
);
1330 debugfile
[debugdir_end
- debugdir
] = 0;
1331 strcat (debugfile
, canon_name
+ strlen (gdb_sysroot
));
1332 strcat (debugfile
, "/");
1333 strcat (debugfile
, basename
);
1335 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1336 goto cleanup_return_debugfile
;
1339 debugdir
= debugdir_end
;
1341 while (*debugdir
!= 0);
1346 cleanup_return_debugfile
:
1354 /* This is the symbol-file command. Read the file, analyze its
1355 symbols, and add a struct symtab to a symtab list. The syntax of
1356 the command is rather bizarre:
1358 1. The function buildargv implements various quoting conventions
1359 which are undocumented and have little or nothing in common with
1360 the way things are quoted (or not quoted) elsewhere in GDB.
1362 2. Options are used, which are not generally used in GDB (perhaps
1363 "set mapped on", "set readnow on" would be better)
1365 3. The order of options matters, which is contrary to GNU
1366 conventions (because it is confusing and inconvenient). */
1369 symbol_file_command (char *args
, int from_tty
)
1375 symbol_file_clear (from_tty
);
1379 char **argv
= gdb_buildargv (args
);
1380 int flags
= OBJF_USERLOADED
;
1381 struct cleanup
*cleanups
;
1384 cleanups
= make_cleanup_freeargv (argv
);
1385 while (*argv
!= NULL
)
1387 if (strcmp (*argv
, "-readnow") == 0)
1388 flags
|= OBJF_READNOW
;
1389 else if (**argv
== '-')
1390 error (_("unknown option `%s'"), *argv
);
1393 symbol_file_add_main_1 (*argv
, from_tty
, flags
);
1401 error (_("no symbol file name was specified"));
1403 do_cleanups (cleanups
);
1407 /* Set the initial language.
1409 FIXME: A better solution would be to record the language in the
1410 psymtab when reading partial symbols, and then use it (if known) to
1411 set the language. This would be a win for formats that encode the
1412 language in an easily discoverable place, such as DWARF. For
1413 stabs, we can jump through hoops looking for specially named
1414 symbols or try to intuit the language from the specific type of
1415 stabs we find, but we can't do that until later when we read in
1419 set_initial_language (void)
1422 enum language lang
= language_unknown
;
1424 filename
= find_main_filename ();
1425 if (filename
!= NULL
)
1426 lang
= deduce_language_from_filename (filename
);
1428 if (lang
== language_unknown
)
1430 /* Make C the default language */
1434 set_language (lang
);
1435 expected_language
= current_language
; /* Don't warn the user. */
1438 /* If NAME is a remote name open the file using remote protocol, otherwise
1439 open it normally. */
1442 bfd_open_maybe_remote (const char *name
)
1444 if (remote_filename_p (name
))
1445 return remote_bfd_open (name
, gnutarget
);
1447 return bfd_openr (name
, gnutarget
);
1451 /* Open the file specified by NAME and hand it off to BFD for
1452 preliminary analysis. Return a newly initialized bfd *, which
1453 includes a newly malloc'd` copy of NAME (tilde-expanded and made
1454 absolute). In case of trouble, error() is called. */
1457 symfile_bfd_open (char *name
)
1461 char *absolute_name
;
1463 if (remote_filename_p (name
))
1465 name
= xstrdup (name
);
1466 sym_bfd
= remote_bfd_open (name
, gnutarget
);
1469 make_cleanup (xfree
, name
);
1470 error (_("`%s': can't open to read symbols: %s."), name
,
1471 bfd_errmsg (bfd_get_error ()));
1474 if (!bfd_check_format (sym_bfd
, bfd_object
))
1476 bfd_close (sym_bfd
);
1477 make_cleanup (xfree
, name
);
1478 error (_("`%s': can't read symbols: %s."), name
,
1479 bfd_errmsg (bfd_get_error ()));
1485 name
= tilde_expand (name
); /* Returns 1st new malloc'd copy. */
1487 /* Look down path for it, allocate 2nd new malloc'd copy. */
1488 desc
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
, name
,
1489 O_RDONLY
| O_BINARY
, &absolute_name
);
1490 #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
1493 char *exename
= alloca (strlen (name
) + 5);
1494 strcat (strcpy (exename
, name
), ".exe");
1495 desc
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
, exename
,
1496 O_RDONLY
| O_BINARY
, &absolute_name
);
1501 make_cleanup (xfree
, name
);
1502 perror_with_name (name
);
1505 /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
1506 bfd. It'll be freed in free_objfile(). */
1508 name
= absolute_name
;
1510 sym_bfd
= bfd_fopen (name
, gnutarget
, FOPEN_RB
, desc
);
1514 make_cleanup (xfree
, name
);
1515 error (_("`%s': can't open to read symbols: %s."), name
,
1516 bfd_errmsg (bfd_get_error ()));
1518 bfd_set_cacheable (sym_bfd
, 1);
1520 if (!bfd_check_format (sym_bfd
, bfd_object
))
1522 /* FIXME: should be checking for errors from bfd_close (for one
1523 thing, on error it does not free all the storage associated
1525 bfd_close (sym_bfd
); /* This also closes desc. */
1526 make_cleanup (xfree
, name
);
1527 error (_("`%s': can't read symbols: %s."), name
,
1528 bfd_errmsg (bfd_get_error ()));
1531 /* bfd_usrdata exists for applications and libbfd must not touch it. */
1532 gdb_assert (bfd_usrdata (sym_bfd
) == NULL
);
1537 /* Return the section index for SECTION_NAME on OBJFILE. Return -1 if
1538 the section was not found. */
1541 get_section_index (struct objfile
*objfile
, char *section_name
)
1543 asection
*sect
= bfd_get_section_by_name (objfile
->obfd
, section_name
);
1551 /* Link SF into the global symtab_fns list. Called on startup by the
1552 _initialize routine in each object file format reader, to register
1553 information about each format the the reader is prepared to
1557 add_symtab_fns (struct sym_fns
*sf
)
1559 sf
->next
= symtab_fns
;
1563 /* Initialize OBJFILE to read symbols from its associated BFD. It
1564 either returns or calls error(). The result is an initialized
1565 struct sym_fns in the objfile structure, that contains cached
1566 information about the symbol file. */
1568 static struct sym_fns
*
1569 find_sym_fns (bfd
*abfd
)
1572 enum bfd_flavour our_flavour
= bfd_get_flavour (abfd
);
1574 if (our_flavour
== bfd_target_srec_flavour
1575 || our_flavour
== bfd_target_ihex_flavour
1576 || our_flavour
== bfd_target_tekhex_flavour
)
1577 return NULL
; /* No symbols. */
1579 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
->next
)
1580 if (our_flavour
== sf
->sym_flavour
)
1583 error (_("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown."),
1584 bfd_get_target (abfd
));
1588 /* This function runs the load command of our current target. */
1591 load_command (char *arg
, int from_tty
)
1593 /* The user might be reloading because the binary has changed. Take
1594 this opportunity to check. */
1595 reopen_exec_file ();
1603 parg
= arg
= get_exec_file (1);
1605 /* Count how many \ " ' tab space there are in the name. */
1606 while ((parg
= strpbrk (parg
, "\\\"'\t ")))
1614 /* We need to quote this string so buildargv can pull it apart. */
1615 char *temp
= xmalloc (strlen (arg
) + count
+ 1 );
1619 make_cleanup (xfree
, temp
);
1622 while ((parg
= strpbrk (parg
, "\\\"'\t ")))
1624 strncpy (ptemp
, prev
, parg
- prev
);
1625 ptemp
+= parg
- prev
;
1629 strcpy (ptemp
, prev
);
1635 target_load (arg
, from_tty
);
1637 /* After re-loading the executable, we don't really know which
1638 overlays are mapped any more. */
1639 overlay_cache_invalid
= 1;
1642 /* This version of "load" should be usable for any target. Currently
1643 it is just used for remote targets, not inftarg.c or core files,
1644 on the theory that only in that case is it useful.
1646 Avoiding xmodem and the like seems like a win (a) because we don't have
1647 to worry about finding it, and (b) On VMS, fork() is very slow and so
1648 we don't want to run a subprocess. On the other hand, I'm not sure how
1649 performance compares. */
1651 static int validate_download
= 0;
1653 /* Callback service function for generic_load (bfd_map_over_sections). */
1656 add_section_size_callback (bfd
*abfd
, asection
*asec
, void *data
)
1658 bfd_size_type
*sum
= data
;
1660 *sum
+= bfd_get_section_size (asec
);
1663 /* Opaque data for load_section_callback. */
1664 struct load_section_data
{
1665 unsigned long load_offset
;
1666 struct load_progress_data
*progress_data
;
1667 VEC(memory_write_request_s
) *requests
;
1670 /* Opaque data for load_progress. */
1671 struct load_progress_data
{
1672 /* Cumulative data. */
1673 unsigned long write_count
;
1674 unsigned long data_count
;
1675 bfd_size_type total_size
;
1678 /* Opaque data for load_progress for a single section. */
1679 struct load_progress_section_data
{
1680 struct load_progress_data
*cumulative
;
1682 /* Per-section data. */
1683 const char *section_name
;
1684 ULONGEST section_sent
;
1685 ULONGEST section_size
;
1690 /* Target write callback routine for progress reporting. */
1693 load_progress (ULONGEST bytes
, void *untyped_arg
)
1695 struct load_progress_section_data
*args
= untyped_arg
;
1696 struct load_progress_data
*totals
;
1699 /* Writing padding data. No easy way to get at the cumulative
1700 stats, so just ignore this. */
1703 totals
= args
->cumulative
;
1705 if (bytes
== 0 && args
->section_sent
== 0)
1707 /* The write is just starting. Let the user know we've started
1709 ui_out_message (uiout
, 0, "Loading section %s, size %s lma %s\n",
1710 args
->section_name
, hex_string (args
->section_size
),
1711 paddress (target_gdbarch
, args
->lma
));
1715 if (validate_download
)
1717 /* Broken memories and broken monitors manifest themselves here
1718 when bring new computers to life. This doubles already slow
1720 /* NOTE: cagney/1999-10-18: A more efficient implementation
1721 might add a verify_memory() method to the target vector and
1722 then use that. remote.c could implement that method using
1723 the ``qCRC'' packet. */
1724 gdb_byte
*check
= xmalloc (bytes
);
1725 struct cleanup
*verify_cleanups
= make_cleanup (xfree
, check
);
1727 if (target_read_memory (args
->lma
, check
, bytes
) != 0)
1728 error (_("Download verify read failed at %s"),
1729 paddress (target_gdbarch
, args
->lma
));
1730 if (memcmp (args
->buffer
, check
, bytes
) != 0)
1731 error (_("Download verify compare failed at %s"),
1732 paddress (target_gdbarch
, args
->lma
));
1733 do_cleanups (verify_cleanups
);
1735 totals
->data_count
+= bytes
;
1737 args
->buffer
+= bytes
;
1738 totals
->write_count
+= 1;
1739 args
->section_sent
+= bytes
;
1741 || (deprecated_ui_load_progress_hook
!= NULL
1742 && deprecated_ui_load_progress_hook (args
->section_name
,
1743 args
->section_sent
)))
1744 error (_("Canceled the download"));
1746 if (deprecated_show_load_progress
!= NULL
)
1747 deprecated_show_load_progress (args
->section_name
,
1751 totals
->total_size
);
1754 /* Callback service function for generic_load (bfd_map_over_sections). */
1757 load_section_callback (bfd
*abfd
, asection
*asec
, void *data
)
1759 struct memory_write_request
*new_request
;
1760 struct load_section_data
*args
= data
;
1761 struct load_progress_section_data
*section_data
;
1762 bfd_size_type size
= bfd_get_section_size (asec
);
1764 const char *sect_name
= bfd_get_section_name (abfd
, asec
);
1766 if ((bfd_get_section_flags (abfd
, asec
) & SEC_LOAD
) == 0)
1772 new_request
= VEC_safe_push (memory_write_request_s
,
1773 args
->requests
, NULL
);
1774 memset (new_request
, 0, sizeof (struct memory_write_request
));
1775 section_data
= xcalloc (1, sizeof (struct load_progress_section_data
));
1776 new_request
->begin
= bfd_section_lma (abfd
, asec
) + args
->load_offset
;
1777 new_request
->end
= new_request
->begin
+ size
; /* FIXME Should size be in instead? */
1778 new_request
->data
= xmalloc (size
);
1779 new_request
->baton
= section_data
;
1781 buffer
= new_request
->data
;
1783 section_data
->cumulative
= args
->progress_data
;
1784 section_data
->section_name
= sect_name
;
1785 section_data
->section_size
= size
;
1786 section_data
->lma
= new_request
->begin
;
1787 section_data
->buffer
= buffer
;
1789 bfd_get_section_contents (abfd
, asec
, buffer
, 0, size
);
1792 /* Clean up an entire memory request vector, including load
1793 data and progress records. */
1796 clear_memory_write_data (void *arg
)
1798 VEC(memory_write_request_s
) **vec_p
= arg
;
1799 VEC(memory_write_request_s
) *vec
= *vec_p
;
1801 struct memory_write_request
*mr
;
1803 for (i
= 0; VEC_iterate (memory_write_request_s
, vec
, i
, mr
); ++i
)
1808 VEC_free (memory_write_request_s
, vec
);
1812 generic_load (char *args
, int from_tty
)
1815 struct timeval start_time
, end_time
;
1817 struct cleanup
*old_cleanups
= make_cleanup (null_cleanup
, 0);
1818 struct load_section_data cbdata
;
1819 struct load_progress_data total_progress
;
1824 memset (&cbdata
, 0, sizeof (cbdata
));
1825 memset (&total_progress
, 0, sizeof (total_progress
));
1826 cbdata
.progress_data
= &total_progress
;
1828 make_cleanup (clear_memory_write_data
, &cbdata
.requests
);
1831 error_no_arg (_("file to load"));
1833 argv
= gdb_buildargv (args
);
1834 make_cleanup_freeargv (argv
);
1836 filename
= tilde_expand (argv
[0]);
1837 make_cleanup (xfree
, filename
);
1839 if (argv
[1] != NULL
)
1843 cbdata
.load_offset
= strtoul (argv
[1], &endptr
, 0);
1845 /* If the last word was not a valid number then
1846 treat it as a file name with spaces in. */
1847 if (argv
[1] == endptr
)
1848 error (_("Invalid download offset:%s."), argv
[1]);
1850 if (argv
[2] != NULL
)
1851 error (_("Too many parameters."));
1854 /* Open the file for loading. */
1855 loadfile_bfd
= bfd_openr (filename
, gnutarget
);
1856 if (loadfile_bfd
== NULL
)
1858 perror_with_name (filename
);
1862 /* FIXME: should be checking for errors from bfd_close (for one thing,
1863 on error it does not free all the storage associated with the
1865 make_cleanup_bfd_close (loadfile_bfd
);
1867 if (!bfd_check_format (loadfile_bfd
, bfd_object
))
1869 error (_("\"%s\" is not an object file: %s"), filename
,
1870 bfd_errmsg (bfd_get_error ()));
1873 bfd_map_over_sections (loadfile_bfd
, add_section_size_callback
,
1874 (void *) &total_progress
.total_size
);
1876 bfd_map_over_sections (loadfile_bfd
, load_section_callback
, &cbdata
);
1878 gettimeofday (&start_time
, NULL
);
1880 if (target_write_memory_blocks (cbdata
.requests
, flash_discard
,
1881 load_progress
) != 0)
1882 error (_("Load failed"));
1884 gettimeofday (&end_time
, NULL
);
1886 entry
= bfd_get_start_address (loadfile_bfd
);
1887 ui_out_text (uiout
, "Start address ");
1888 ui_out_field_fmt (uiout
, "address", "%s", paddress (target_gdbarch
, entry
));
1889 ui_out_text (uiout
, ", load size ");
1890 ui_out_field_fmt (uiout
, "load-size", "%lu", total_progress
.data_count
);
1891 ui_out_text (uiout
, "\n");
1892 /* We were doing this in remote-mips.c, I suspect it is right
1893 for other targets too. */
1894 regcache_write_pc (get_current_regcache (), entry
);
1896 /* Reset breakpoints, now that we have changed the load image. For
1897 instance, breakpoints may have been set (or reset, by
1898 post_create_inferior) while connected to the target but before we
1899 loaded the program. In that case, the prologue analyzer could
1900 have read instructions from the target to find the right
1901 breakpoint locations. Loading has changed the contents of that
1904 breakpoint_re_set ();
1906 /* FIXME: are we supposed to call symbol_file_add or not? According
1907 to a comment from remote-mips.c (where a call to symbol_file_add
1908 was commented out), making the call confuses GDB if more than one
1909 file is loaded in. Some targets do (e.g., remote-vx.c) but
1910 others don't (or didn't - perhaps they have all been deleted). */
1912 print_transfer_performance (gdb_stdout
, total_progress
.data_count
,
1913 total_progress
.write_count
,
1914 &start_time
, &end_time
);
1916 do_cleanups (old_cleanups
);
1919 /* Report how fast the transfer went. */
1921 /* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being
1922 replaced by print_transfer_performance (with a very different
1923 function signature). */
1926 report_transfer_performance (unsigned long data_count
, time_t start_time
,
1929 struct timeval start
, end
;
1931 start
.tv_sec
= start_time
;
1933 end
.tv_sec
= end_time
;
1936 print_transfer_performance (gdb_stdout
, data_count
, 0, &start
, &end
);
1940 print_transfer_performance (struct ui_file
*stream
,
1941 unsigned long data_count
,
1942 unsigned long write_count
,
1943 const struct timeval
*start_time
,
1944 const struct timeval
*end_time
)
1946 ULONGEST time_count
;
1948 /* Compute the elapsed time in milliseconds, as a tradeoff between
1949 accuracy and overflow. */
1950 time_count
= (end_time
->tv_sec
- start_time
->tv_sec
) * 1000;
1951 time_count
+= (end_time
->tv_usec
- start_time
->tv_usec
) / 1000;
1953 ui_out_text (uiout
, "Transfer rate: ");
1956 unsigned long rate
= ((ULONGEST
) data_count
* 1000) / time_count
;
1958 if (ui_out_is_mi_like_p (uiout
))
1960 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
* 8);
1961 ui_out_text (uiout
, " bits/sec");
1963 else if (rate
< 1024)
1965 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
);
1966 ui_out_text (uiout
, " bytes/sec");
1970 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
/ 1024);
1971 ui_out_text (uiout
, " KB/sec");
1976 ui_out_field_fmt (uiout
, "transferred-bits", "%lu", (data_count
* 8));
1977 ui_out_text (uiout
, " bits in <1 sec");
1979 if (write_count
> 0)
1981 ui_out_text (uiout
, ", ");
1982 ui_out_field_fmt (uiout
, "write-rate", "%lu", data_count
/ write_count
);
1983 ui_out_text (uiout
, " bytes/write");
1985 ui_out_text (uiout
, ".\n");
1988 /* This function allows the addition of incrementally linked object files.
1989 It does not modify any state in the target, only in the debugger. */
1990 /* Note: ezannoni 2000-04-13 This function/command used to have a
1991 special case syntax for the rombug target (Rombug is the boot
1992 monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
1993 rombug case, the user doesn't need to supply a text address,
1994 instead a call to target_link() (in target.c) would supply the
1995 value to use. We are now discontinuing this type of ad hoc syntax. */
1998 add_symbol_file_command (char *args
, int from_tty
)
2000 struct gdbarch
*gdbarch
= get_current_arch ();
2001 char *filename
= NULL
;
2002 int flags
= OBJF_USERLOADED
;
2004 int expecting_option
= 0;
2005 int section_index
= 0;
2009 int expecting_sec_name
= 0;
2010 int expecting_sec_addr
= 0;
2019 struct section_addr_info
*section_addrs
;
2020 struct sect_opt
*sect_opts
= NULL
;
2021 size_t num_sect_opts
= 0;
2022 struct cleanup
*my_cleanups
= make_cleanup (null_cleanup
, NULL
);
2025 sect_opts
= (struct sect_opt
*) xmalloc (num_sect_opts
2026 * sizeof (struct sect_opt
));
2031 error (_("add-symbol-file takes a file name and an address"));
2033 argv
= gdb_buildargv (args
);
2034 make_cleanup_freeargv (argv
);
2036 for (arg
= argv
[0], argcnt
= 0; arg
!= NULL
; arg
= argv
[++argcnt
])
2038 /* Process the argument. */
2041 /* The first argument is the file name. */
2042 filename
= tilde_expand (arg
);
2043 make_cleanup (xfree
, filename
);
2048 /* The second argument is always the text address at which
2049 to load the program. */
2050 sect_opts
[section_index
].name
= ".text";
2051 sect_opts
[section_index
].value
= arg
;
2052 if (++section_index
>= num_sect_opts
)
2055 sect_opts
= ((struct sect_opt
*)
2056 xrealloc (sect_opts
,
2058 * sizeof (struct sect_opt
)));
2063 /* It's an option (starting with '-') or it's an argument
2068 if (strcmp (arg
, "-readnow") == 0)
2069 flags
|= OBJF_READNOW
;
2070 else if (strcmp (arg
, "-s") == 0)
2072 expecting_sec_name
= 1;
2073 expecting_sec_addr
= 1;
2078 if (expecting_sec_name
)
2080 sect_opts
[section_index
].name
= arg
;
2081 expecting_sec_name
= 0;
2084 if (expecting_sec_addr
)
2086 sect_opts
[section_index
].value
= arg
;
2087 expecting_sec_addr
= 0;
2088 if (++section_index
>= num_sect_opts
)
2091 sect_opts
= ((struct sect_opt
*)
2092 xrealloc (sect_opts
,
2094 * sizeof (struct sect_opt
)));
2098 error (_("USAGE: add-symbol-file <filename> <textaddress> [-mapped] [-readnow] [-s <secname> <addr>]*"));
2103 /* This command takes at least two arguments. The first one is a
2104 filename, and the second is the address where this file has been
2105 loaded. Abort now if this address hasn't been provided by the
2107 if (section_index
< 1)
2108 error (_("The address where %s has been loaded is missing"), filename
);
2110 /* Print the prompt for the query below. And save the arguments into
2111 a sect_addr_info structure to be passed around to other
2112 functions. We have to split this up into separate print
2113 statements because hex_string returns a local static
2116 printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename
);
2117 section_addrs
= alloc_section_addr_info (section_index
);
2118 make_cleanup (xfree
, section_addrs
);
2119 for (i
= 0; i
< section_index
; i
++)
2122 char *val
= sect_opts
[i
].value
;
2123 char *sec
= sect_opts
[i
].name
;
2125 addr
= parse_and_eval_address (val
);
2127 /* Here we store the section offsets in the order they were
2128 entered on the command line. */
2129 section_addrs
->other
[sec_num
].name
= sec
;
2130 section_addrs
->other
[sec_num
].addr
= addr
;
2131 printf_unfiltered ("\t%s_addr = %s\n", sec
,
2132 paddress (gdbarch
, addr
));
2135 /* The object's sections are initialized when a
2136 call is made to build_objfile_section_table (objfile).
2137 This happens in reread_symbols.
2138 At this point, we don't know what file type this is,
2139 so we can't determine what section names are valid. */
2142 if (from_tty
&& (!query ("%s", "")))
2143 error (_("Not confirmed."));
2145 symbol_file_add (filename
, from_tty
? SYMFILE_VERBOSE
: 0,
2146 section_addrs
, flags
);
2148 /* Getting new symbols may change our opinion about what is
2150 reinit_frame_cache ();
2151 do_cleanups (my_cleanups
);
2155 /* Re-read symbols if a symbol-file has changed. */
2157 reread_symbols (void)
2159 struct objfile
*objfile
;
2162 struct stat new_statbuf
;
2165 /* With the addition of shared libraries, this should be modified,
2166 the load time should be saved in the partial symbol tables, since
2167 different tables may come from different source files. FIXME.
2168 This routine should then walk down each partial symbol table
2169 and see if the symbol table that it originates from has been changed */
2171 for (objfile
= object_files
; objfile
; objfile
= objfile
->next
)
2173 /* solib-sunos.c creates one objfile with obfd. */
2174 if (objfile
->obfd
== NULL
)
2177 /* Separate debug objfiles are handled in the main objfile. */
2178 if (objfile
->separate_debug_objfile_backlink
)
2181 #ifdef DEPRECATED_IBM6000_TARGET
2182 /* If this object is from a shared library, then you should
2183 stat on the library name, not member name. */
2185 if (objfile
->obfd
->my_archive
)
2186 res
= stat (objfile
->obfd
->my_archive
->filename
, &new_statbuf
);
2189 res
= stat (objfile
->name
, &new_statbuf
);
2192 /* FIXME, should use print_sys_errmsg but it's not filtered. */
2193 printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
2197 new_modtime
= new_statbuf
.st_mtime
;
2198 if (new_modtime
!= objfile
->mtime
)
2200 struct cleanup
*old_cleanups
;
2201 struct section_offsets
*offsets
;
2203 char *obfd_filename
;
2205 printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
2208 /* There are various functions like symbol_file_add,
2209 symfile_bfd_open, syms_from_objfile, etc., which might
2210 appear to do what we want. But they have various other
2211 effects which we *don't* want. So we just do stuff
2212 ourselves. We don't worry about mapped files (for one thing,
2213 any mapped file will be out of date). */
2215 /* If we get an error, blow away this objfile (not sure if
2216 that is the correct response for things like shared
2218 old_cleanups
= make_cleanup_free_objfile (objfile
);
2219 /* We need to do this whenever any symbols go away. */
2220 make_cleanup (clear_symtab_users_cleanup
, 0 /*ignore*/);
2222 if (exec_bfd
!= NULL
&& strcmp (bfd_get_filename (objfile
->obfd
),
2223 bfd_get_filename (exec_bfd
)) == 0)
2225 /* Reload EXEC_BFD without asking anything. */
2227 exec_file_attach (bfd_get_filename (objfile
->obfd
), 0);
2230 /* Clean up any state BFD has sitting around. We don't need
2231 to close the descriptor but BFD lacks a way of closing the
2232 BFD without closing the descriptor. */
2233 obfd_filename
= bfd_get_filename (objfile
->obfd
);
2234 if (!bfd_close (objfile
->obfd
))
2235 error (_("Can't close BFD for %s: %s"), objfile
->name
,
2236 bfd_errmsg (bfd_get_error ()));
2237 objfile
->obfd
= bfd_open_maybe_remote (obfd_filename
);
2238 if (objfile
->obfd
== NULL
)
2239 error (_("Can't open %s to read symbols."), objfile
->name
);
2241 objfile
->obfd
= gdb_bfd_ref (objfile
->obfd
);
2242 /* bfd_openr sets cacheable to true, which is what we want. */
2243 if (!bfd_check_format (objfile
->obfd
, bfd_object
))
2244 error (_("Can't read symbols from %s: %s."), objfile
->name
,
2245 bfd_errmsg (bfd_get_error ()));
2247 /* Save the offsets, we will nuke them with the rest of the
2249 num_offsets
= objfile
->num_sections
;
2250 offsets
= ((struct section_offsets
*)
2251 alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets
)));
2252 memcpy (offsets
, objfile
->section_offsets
,
2253 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2255 /* Remove any references to this objfile in the global
2257 preserve_values (objfile
);
2259 /* Nuke all the state that we will re-read. Much of the following
2260 code which sets things to NULL really is necessary to tell
2261 other parts of GDB that there is nothing currently there.
2263 Try to keep the freeing order compatible with free_objfile. */
2265 if (objfile
->sf
!= NULL
)
2267 (*objfile
->sf
->sym_finish
) (objfile
);
2270 clear_objfile_data (objfile
);
2272 /* Free the separate debug objfiles. It will be
2273 automatically recreated by sym_read. */
2274 free_objfile_separate_debug (objfile
);
2276 /* FIXME: Do we have to free a whole linked list, or is this
2278 if (objfile
->global_psymbols
.list
)
2279 xfree (objfile
->global_psymbols
.list
);
2280 memset (&objfile
->global_psymbols
, 0,
2281 sizeof (objfile
->global_psymbols
));
2282 if (objfile
->static_psymbols
.list
)
2283 xfree (objfile
->static_psymbols
.list
);
2284 memset (&objfile
->static_psymbols
, 0,
2285 sizeof (objfile
->static_psymbols
));
2287 /* Free the obstacks for non-reusable objfiles */
2288 bcache_xfree (objfile
->psymbol_cache
);
2289 objfile
->psymbol_cache
= bcache_xmalloc ();
2290 bcache_xfree (objfile
->macro_cache
);
2291 objfile
->macro_cache
= bcache_xmalloc ();
2292 bcache_xfree (objfile
->filename_cache
);
2293 objfile
->filename_cache
= bcache_xmalloc ();
2294 if (objfile
->demangled_names_hash
!= NULL
)
2296 htab_delete (objfile
->demangled_names_hash
);
2297 objfile
->demangled_names_hash
= NULL
;
2299 obstack_free (&objfile
->objfile_obstack
, 0);
2300 objfile
->sections
= NULL
;
2301 objfile
->symtabs
= NULL
;
2302 objfile
->psymtabs
= NULL
;
2303 objfile
->psymtabs_addrmap
= NULL
;
2304 objfile
->free_psymtabs
= NULL
;
2305 objfile
->cp_namespace_symtab
= NULL
;
2306 objfile
->msymbols
= NULL
;
2307 objfile
->deprecated_sym_private
= NULL
;
2308 objfile
->minimal_symbol_count
= 0;
2309 memset (&objfile
->msymbol_hash
, 0,
2310 sizeof (objfile
->msymbol_hash
));
2311 memset (&objfile
->msymbol_demangled_hash
, 0,
2312 sizeof (objfile
->msymbol_demangled_hash
));
2314 objfile
->psymbol_cache
= bcache_xmalloc ();
2315 objfile
->macro_cache
= bcache_xmalloc ();
2316 objfile
->filename_cache
= bcache_xmalloc ();
2317 /* obstack_init also initializes the obstack so it is
2318 empty. We could use obstack_specify_allocation but
2319 gdb_obstack.h specifies the alloc/dealloc
2321 obstack_init (&objfile
->objfile_obstack
);
2322 if (build_objfile_section_table (objfile
))
2324 error (_("Can't find the file sections in `%s': %s"),
2325 objfile
->name
, bfd_errmsg (bfd_get_error ()));
2327 terminate_minimal_symbol_table (objfile
);
2329 /* We use the same section offsets as from last time. I'm not
2330 sure whether that is always correct for shared libraries. */
2331 objfile
->section_offsets
= (struct section_offsets
*)
2332 obstack_alloc (&objfile
->objfile_obstack
,
2333 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2334 memcpy (objfile
->section_offsets
, offsets
,
2335 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2336 objfile
->num_sections
= num_offsets
;
2338 /* What the hell is sym_new_init for, anyway? The concept of
2339 distinguishing between the main file and additional files
2340 in this way seems rather dubious. */
2341 if (objfile
== symfile_objfile
)
2343 (*objfile
->sf
->sym_new_init
) (objfile
);
2346 (*objfile
->sf
->sym_init
) (objfile
);
2347 clear_complaints (&symfile_complaints
, 1, 1);
2348 /* Do not set flags as this is safe and we don't want to be
2350 (*objfile
->sf
->sym_read
) (objfile
, 0);
2351 if (!objfile_has_symbols (objfile
))
2354 printf_unfiltered (_("(no debugging symbols found)\n"));
2358 /* We're done reading the symbol file; finish off complaints. */
2359 clear_complaints (&symfile_complaints
, 0, 1);
2361 /* Getting new symbols may change our opinion about what is
2364 reinit_frame_cache ();
2366 /* Discard cleanups as symbol reading was successful. */
2367 discard_cleanups (old_cleanups
);
2369 /* If the mtime has changed between the time we set new_modtime
2370 and now, we *want* this to be out of date, so don't call stat
2372 objfile
->mtime
= new_modtime
;
2374 init_entry_point_info (objfile
);
2380 /* Notify objfiles that we've modified objfile sections. */
2381 objfiles_changed ();
2383 clear_symtab_users ();
2384 /* At least one objfile has changed, so we can consider that
2385 the executable we're debugging has changed too. */
2386 observer_notify_executable_changed ();
2399 static filename_language
*filename_language_table
;
2400 static int fl_table_size
, fl_table_next
;
2403 add_filename_language (char *ext
, enum language lang
)
2405 if (fl_table_next
>= fl_table_size
)
2407 fl_table_size
+= 10;
2408 filename_language_table
=
2409 xrealloc (filename_language_table
,
2410 fl_table_size
* sizeof (*filename_language_table
));
2413 filename_language_table
[fl_table_next
].ext
= xstrdup (ext
);
2414 filename_language_table
[fl_table_next
].lang
= lang
;
2418 static char *ext_args
;
2420 show_ext_args (struct ui_file
*file
, int from_tty
,
2421 struct cmd_list_element
*c
, const char *value
)
2423 fprintf_filtered (file
, _("\
2424 Mapping between filename extension and source language is \"%s\".\n"),
2429 set_ext_lang_command (char *args
, int from_tty
, struct cmd_list_element
*e
)
2432 char *cp
= ext_args
;
2435 /* First arg is filename extension, starting with '.' */
2437 error (_("'%s': Filename extension must begin with '.'"), ext_args
);
2439 /* Find end of first arg. */
2440 while (*cp
&& !isspace (*cp
))
2444 error (_("'%s': two arguments required -- filename extension and language"),
2447 /* Null-terminate first arg */
2450 /* Find beginning of second arg, which should be a source language. */
2451 while (*cp
&& isspace (*cp
))
2455 error (_("'%s': two arguments required -- filename extension and language"),
2458 /* Lookup the language from among those we know. */
2459 lang
= language_enum (cp
);
2461 /* Now lookup the filename extension: do we already know it? */
2462 for (i
= 0; i
< fl_table_next
; i
++)
2463 if (0 == strcmp (ext_args
, filename_language_table
[i
].ext
))
2466 if (i
>= fl_table_next
)
2468 /* new file extension */
2469 add_filename_language (ext_args
, lang
);
2473 /* redefining a previously known filename extension */
2476 /* query ("Really make files of type %s '%s'?", */
2477 /* ext_args, language_str (lang)); */
2479 xfree (filename_language_table
[i
].ext
);
2480 filename_language_table
[i
].ext
= xstrdup (ext_args
);
2481 filename_language_table
[i
].lang
= lang
;
2486 info_ext_lang_command (char *args
, int from_tty
)
2490 printf_filtered (_("Filename extensions and the languages they represent:"));
2491 printf_filtered ("\n\n");
2492 for (i
= 0; i
< fl_table_next
; i
++)
2493 printf_filtered ("\t%s\t- %s\n",
2494 filename_language_table
[i
].ext
,
2495 language_str (filename_language_table
[i
].lang
));
2499 init_filename_language_table (void)
2501 if (fl_table_size
== 0) /* protect against repetition */
2505 filename_language_table
=
2506 xmalloc (fl_table_size
* sizeof (*filename_language_table
));
2507 add_filename_language (".c", language_c
);
2508 add_filename_language (".C", language_cplus
);
2509 add_filename_language (".cc", language_cplus
);
2510 add_filename_language (".cp", language_cplus
);
2511 add_filename_language (".cpp", language_cplus
);
2512 add_filename_language (".cxx", language_cplus
);
2513 add_filename_language (".c++", language_cplus
);
2514 add_filename_language (".java", language_java
);
2515 add_filename_language (".class", language_java
);
2516 add_filename_language (".m", language_objc
);
2517 add_filename_language (".f", language_fortran
);
2518 add_filename_language (".F", language_fortran
);
2519 add_filename_language (".s", language_asm
);
2520 add_filename_language (".sx", language_asm
);
2521 add_filename_language (".S", language_asm
);
2522 add_filename_language (".pas", language_pascal
);
2523 add_filename_language (".p", language_pascal
);
2524 add_filename_language (".pp", language_pascal
);
2525 add_filename_language (".adb", language_ada
);
2526 add_filename_language (".ads", language_ada
);
2527 add_filename_language (".a", language_ada
);
2528 add_filename_language (".ada", language_ada
);
2533 deduce_language_from_filename (char *filename
)
2538 if (filename
!= NULL
)
2539 if ((cp
= strrchr (filename
, '.')) != NULL
)
2540 for (i
= 0; i
< fl_table_next
; i
++)
2541 if (strcmp (cp
, filename_language_table
[i
].ext
) == 0)
2542 return filename_language_table
[i
].lang
;
2544 return language_unknown
;
2549 Allocate and partly initialize a new symbol table. Return a pointer
2550 to it. error() if no space.
2552 Caller must set these fields:
2561 allocate_symtab (char *filename
, struct objfile
*objfile
)
2563 struct symtab
*symtab
;
2565 symtab
= (struct symtab
*)
2566 obstack_alloc (&objfile
->objfile_obstack
, sizeof (struct symtab
));
2567 memset (symtab
, 0, sizeof (*symtab
));
2568 symtab
->filename
= (char *) bcache (filename
, strlen (filename
) + 1,
2569 objfile
->filename_cache
);
2570 symtab
->fullname
= NULL
;
2571 symtab
->language
= deduce_language_from_filename (filename
);
2572 symtab
->debugformat
= "unknown";
2574 /* Hook it to the objfile it comes from */
2576 symtab
->objfile
= objfile
;
2577 symtab
->next
= objfile
->symtabs
;
2578 objfile
->symtabs
= symtab
;
2584 /* Reset all data structures in gdb which may contain references to symbol
2588 clear_symtab_users (void)
2590 /* Someday, we should do better than this, by only blowing away
2591 the things that really need to be blown. */
2593 /* Clear the "current" symtab first, because it is no longer valid.
2594 breakpoint_re_set may try to access the current symtab. */
2595 clear_current_source_symtab_and_line ();
2598 breakpoint_re_set ();
2599 set_default_breakpoint (0, NULL
, 0, 0, 0);
2600 clear_pc_function_cache ();
2601 observer_notify_new_objfile (NULL
);
2603 /* Clear globals which might have pointed into a removed objfile.
2604 FIXME: It's not clear which of these are supposed to persist
2605 between expressions and which ought to be reset each time. */
2606 expression_context_block
= NULL
;
2607 innermost_block
= NULL
;
2609 /* Varobj may refer to old symbols, perform a cleanup. */
2610 varobj_invalidate ();
2615 clear_symtab_users_cleanup (void *ignore
)
2617 clear_symtab_users ();
2621 The following code implements an abstraction for debugging overlay sections.
2623 The target model is as follows:
2624 1) The gnu linker will permit multiple sections to be mapped into the
2625 same VMA, each with its own unique LMA (or load address).
2626 2) It is assumed that some runtime mechanism exists for mapping the
2627 sections, one by one, from the load address into the VMA address.
2628 3) This code provides a mechanism for gdb to keep track of which
2629 sections should be considered to be mapped from the VMA to the LMA.
2630 This information is used for symbol lookup, and memory read/write.
2631 For instance, if a section has been mapped then its contents
2632 should be read from the VMA, otherwise from the LMA.
2634 Two levels of debugger support for overlays are available. One is
2635 "manual", in which the debugger relies on the user to tell it which
2636 overlays are currently mapped. This level of support is
2637 implemented entirely in the core debugger, and the information about
2638 whether a section is mapped is kept in the objfile->obj_section table.
2640 The second level of support is "automatic", and is only available if
2641 the target-specific code provides functionality to read the target's
2642 overlay mapping table, and translate its contents for the debugger
2643 (by updating the mapped state information in the obj_section tables).
2645 The interface is as follows:
2647 overlay map <name> -- tell gdb to consider this section mapped
2648 overlay unmap <name> -- tell gdb to consider this section unmapped
2649 overlay list -- list the sections that GDB thinks are mapped
2650 overlay read-target -- get the target's state of what's mapped
2651 overlay off/manual/auto -- set overlay debugging state
2652 Functional interface:
2653 find_pc_mapped_section(pc): if the pc is in the range of a mapped
2654 section, return that section.
2655 find_pc_overlay(pc): find any overlay section that contains
2656 the pc, either in its VMA or its LMA
2657 section_is_mapped(sect): true if overlay is marked as mapped
2658 section_is_overlay(sect): true if section's VMA != LMA
2659 pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
2660 pc_in_unmapped_range(...): true if pc belongs to section's LMA
2661 sections_overlap(sec1, sec2): true if mapped sec1 and sec2 ranges overlap
2662 overlay_mapped_address(...): map an address from section's LMA to VMA
2663 overlay_unmapped_address(...): map an address from section's VMA to LMA
2664 symbol_overlayed_address(...): Return a "current" address for symbol:
2665 either in VMA or LMA depending on whether
2666 the symbol's section is currently mapped
2669 /* Overlay debugging state: */
2671 enum overlay_debugging_state overlay_debugging
= ovly_off
;
2672 int overlay_cache_invalid
= 0; /* True if need to refresh mapped state */
2674 /* Function: section_is_overlay (SECTION)
2675 Returns true if SECTION has VMA not equal to LMA, ie.
2676 SECTION is loaded at an address different from where it will "run". */
2679 section_is_overlay (struct obj_section
*section
)
2681 if (overlay_debugging
&& section
)
2683 bfd
*abfd
= section
->objfile
->obfd
;
2684 asection
*bfd_section
= section
->the_bfd_section
;
2686 if (bfd_section_lma (abfd
, bfd_section
) != 0
2687 && bfd_section_lma (abfd
, bfd_section
)
2688 != bfd_section_vma (abfd
, bfd_section
))
2695 /* Function: overlay_invalidate_all (void)
2696 Invalidate the mapped state of all overlay sections (mark it as stale). */
2699 overlay_invalidate_all (void)
2701 struct objfile
*objfile
;
2702 struct obj_section
*sect
;
2704 ALL_OBJSECTIONS (objfile
, sect
)
2705 if (section_is_overlay (sect
))
2706 sect
->ovly_mapped
= -1;
2709 /* Function: section_is_mapped (SECTION)
2710 Returns true if section is an overlay, and is currently mapped.
2712 Access to the ovly_mapped flag is restricted to this function, so
2713 that we can do automatic update. If the global flag
2714 OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
2715 overlay_invalidate_all. If the mapped state of the particular
2716 section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
2719 section_is_mapped (struct obj_section
*osect
)
2721 struct gdbarch
*gdbarch
;
2723 if (osect
== 0 || !section_is_overlay (osect
))
2726 switch (overlay_debugging
)
2730 return 0; /* overlay debugging off */
2731 case ovly_auto
: /* overlay debugging automatic */
2732 /* Unles there is a gdbarch_overlay_update function,
2733 there's really nothing useful to do here (can't really go auto) */
2734 gdbarch
= get_objfile_arch (osect
->objfile
);
2735 if (gdbarch_overlay_update_p (gdbarch
))
2737 if (overlay_cache_invalid
)
2739 overlay_invalidate_all ();
2740 overlay_cache_invalid
= 0;
2742 if (osect
->ovly_mapped
== -1)
2743 gdbarch_overlay_update (gdbarch
, osect
);
2745 /* fall thru to manual case */
2746 case ovly_on
: /* overlay debugging manual */
2747 return osect
->ovly_mapped
== 1;
2751 /* Function: pc_in_unmapped_range
2752 If PC falls into the lma range of SECTION, return true, else false. */
2755 pc_in_unmapped_range (CORE_ADDR pc
, struct obj_section
*section
)
2757 if (section_is_overlay (section
))
2759 bfd
*abfd
= section
->objfile
->obfd
;
2760 asection
*bfd_section
= section
->the_bfd_section
;
2762 /* We assume the LMA is relocated by the same offset as the VMA. */
2763 bfd_vma size
= bfd_get_section_size (bfd_section
);
2764 CORE_ADDR offset
= obj_section_offset (section
);
2766 if (bfd_get_section_lma (abfd
, bfd_section
) + offset
<= pc
2767 && pc
< bfd_get_section_lma (abfd
, bfd_section
) + offset
+ size
)
2774 /* Function: pc_in_mapped_range
2775 If PC falls into the vma range of SECTION, return true, else false. */
2778 pc_in_mapped_range (CORE_ADDR pc
, struct obj_section
*section
)
2780 if (section_is_overlay (section
))
2782 if (obj_section_addr (section
) <= pc
2783 && pc
< obj_section_endaddr (section
))
2791 /* Return true if the mapped ranges of sections A and B overlap, false
2794 sections_overlap (struct obj_section
*a
, struct obj_section
*b
)
2796 CORE_ADDR a_start
= obj_section_addr (a
);
2797 CORE_ADDR a_end
= obj_section_endaddr (a
);
2798 CORE_ADDR b_start
= obj_section_addr (b
);
2799 CORE_ADDR b_end
= obj_section_endaddr (b
);
2801 return (a_start
< b_end
&& b_start
< a_end
);
2804 /* Function: overlay_unmapped_address (PC, SECTION)
2805 Returns the address corresponding to PC in the unmapped (load) range.
2806 May be the same as PC. */
2809 overlay_unmapped_address (CORE_ADDR pc
, struct obj_section
*section
)
2811 if (section_is_overlay (section
) && pc_in_mapped_range (pc
, section
))
2813 bfd
*abfd
= section
->objfile
->obfd
;
2814 asection
*bfd_section
= section
->the_bfd_section
;
2816 return pc
+ bfd_section_lma (abfd
, bfd_section
)
2817 - bfd_section_vma (abfd
, bfd_section
);
2823 /* Function: overlay_mapped_address (PC, SECTION)
2824 Returns the address corresponding to PC in the mapped (runtime) range.
2825 May be the same as PC. */
2828 overlay_mapped_address (CORE_ADDR pc
, struct obj_section
*section
)
2830 if (section_is_overlay (section
) && pc_in_unmapped_range (pc
, section
))
2832 bfd
*abfd
= section
->objfile
->obfd
;
2833 asection
*bfd_section
= section
->the_bfd_section
;
2835 return pc
+ bfd_section_vma (abfd
, bfd_section
)
2836 - bfd_section_lma (abfd
, bfd_section
);
2843 /* Function: symbol_overlayed_address
2844 Return one of two addresses (relative to the VMA or to the LMA),
2845 depending on whether the section is mapped or not. */
2848 symbol_overlayed_address (CORE_ADDR address
, struct obj_section
*section
)
2850 if (overlay_debugging
)
2852 /* If the symbol has no section, just return its regular address. */
2855 /* If the symbol's section is not an overlay, just return its address */
2856 if (!section_is_overlay (section
))
2858 /* If the symbol's section is mapped, just return its address */
2859 if (section_is_mapped (section
))
2862 * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
2863 * then return its LOADED address rather than its vma address!!
2865 return overlay_unmapped_address (address
, section
);
2870 /* Function: find_pc_overlay (PC)
2871 Return the best-match overlay section for PC:
2872 If PC matches a mapped overlay section's VMA, return that section.
2873 Else if PC matches an unmapped section's VMA, return that section.
2874 Else if PC matches an unmapped section's LMA, return that section. */
2876 struct obj_section
*
2877 find_pc_overlay (CORE_ADDR pc
)
2879 struct objfile
*objfile
;
2880 struct obj_section
*osect
, *best_match
= NULL
;
2882 if (overlay_debugging
)
2883 ALL_OBJSECTIONS (objfile
, osect
)
2884 if (section_is_overlay (osect
))
2886 if (pc_in_mapped_range (pc
, osect
))
2888 if (section_is_mapped (osect
))
2893 else if (pc_in_unmapped_range (pc
, osect
))
2899 /* Function: find_pc_mapped_section (PC)
2900 If PC falls into the VMA address range of an overlay section that is
2901 currently marked as MAPPED, return that section. Else return NULL. */
2903 struct obj_section
*
2904 find_pc_mapped_section (CORE_ADDR pc
)
2906 struct objfile
*objfile
;
2907 struct obj_section
*osect
;
2909 if (overlay_debugging
)
2910 ALL_OBJSECTIONS (objfile
, osect
)
2911 if (pc_in_mapped_range (pc
, osect
) && section_is_mapped (osect
))
2917 /* Function: list_overlays_command
2918 Print a list of mapped sections and their PC ranges */
2921 list_overlays_command (char *args
, int from_tty
)
2924 struct objfile
*objfile
;
2925 struct obj_section
*osect
;
2927 if (overlay_debugging
)
2928 ALL_OBJSECTIONS (objfile
, osect
)
2929 if (section_is_mapped (osect
))
2931 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2936 vma
= bfd_section_vma (objfile
->obfd
, osect
->the_bfd_section
);
2937 lma
= bfd_section_lma (objfile
->obfd
, osect
->the_bfd_section
);
2938 size
= bfd_get_section_size (osect
->the_bfd_section
);
2939 name
= bfd_section_name (objfile
->obfd
, osect
->the_bfd_section
);
2941 printf_filtered ("Section %s, loaded at ", name
);
2942 fputs_filtered (paddress (gdbarch
, lma
), gdb_stdout
);
2943 puts_filtered (" - ");
2944 fputs_filtered (paddress (gdbarch
, lma
+ size
), gdb_stdout
);
2945 printf_filtered (", mapped at ");
2946 fputs_filtered (paddress (gdbarch
, vma
), gdb_stdout
);
2947 puts_filtered (" - ");
2948 fputs_filtered (paddress (gdbarch
, vma
+ size
), gdb_stdout
);
2949 puts_filtered ("\n");
2954 printf_filtered (_("No sections are mapped.\n"));
2957 /* Function: map_overlay_command
2958 Mark the named section as mapped (ie. residing at its VMA address). */
2961 map_overlay_command (char *args
, int from_tty
)
2963 struct objfile
*objfile
, *objfile2
;
2964 struct obj_section
*sec
, *sec2
;
2966 if (!overlay_debugging
)
2968 Overlay debugging not enabled. Use either the 'overlay auto' or\n\
2969 the 'overlay manual' command."));
2971 if (args
== 0 || *args
== 0)
2972 error (_("Argument required: name of an overlay section"));
2974 /* First, find a section matching the user supplied argument */
2975 ALL_OBJSECTIONS (objfile
, sec
)
2976 if (!strcmp (bfd_section_name (objfile
->obfd
, sec
->the_bfd_section
), args
))
2978 /* Now, check to see if the section is an overlay. */
2979 if (!section_is_overlay (sec
))
2980 continue; /* not an overlay section */
2982 /* Mark the overlay as "mapped" */
2983 sec
->ovly_mapped
= 1;
2985 /* Next, make a pass and unmap any sections that are
2986 overlapped by this new section: */
2987 ALL_OBJSECTIONS (objfile2
, sec2
)
2988 if (sec2
->ovly_mapped
&& sec
!= sec2
&& sections_overlap (sec
, sec2
))
2991 printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
2992 bfd_section_name (objfile
->obfd
,
2993 sec2
->the_bfd_section
));
2994 sec2
->ovly_mapped
= 0; /* sec2 overlaps sec: unmap sec2 */
2998 error (_("No overlay section called %s"), args
);
3001 /* Function: unmap_overlay_command
3002 Mark the overlay section as unmapped
3003 (ie. resident in its LMA address range, rather than the VMA range). */
3006 unmap_overlay_command (char *args
, int from_tty
)
3008 struct objfile
*objfile
;
3009 struct obj_section
*sec
;
3011 if (!overlay_debugging
)
3013 Overlay debugging not enabled. Use either the 'overlay auto' or\n\
3014 the 'overlay manual' command."));
3016 if (args
== 0 || *args
== 0)
3017 error (_("Argument required: name of an overlay section"));
3019 /* First, find a section matching the user supplied argument */
3020 ALL_OBJSECTIONS (objfile
, sec
)
3021 if (!strcmp (bfd_section_name (objfile
->obfd
, sec
->the_bfd_section
), args
))
3023 if (!sec
->ovly_mapped
)
3024 error (_("Section %s is not mapped"), args
);
3025 sec
->ovly_mapped
= 0;
3028 error (_("No overlay section called %s"), args
);
3031 /* Function: overlay_auto_command
3032 A utility command to turn on overlay debugging.
3033 Possibly this should be done via a set/show command. */
3036 overlay_auto_command (char *args
, int from_tty
)
3038 overlay_debugging
= ovly_auto
;
3039 enable_overlay_breakpoints ();
3041 printf_unfiltered (_("Automatic overlay debugging enabled."));
3044 /* Function: overlay_manual_command
3045 A utility command to turn on overlay debugging.
3046 Possibly this should be done via a set/show command. */
3049 overlay_manual_command (char *args
, int from_tty
)
3051 overlay_debugging
= ovly_on
;
3052 disable_overlay_breakpoints ();
3054 printf_unfiltered (_("Overlay debugging enabled."));
3057 /* Function: overlay_off_command
3058 A utility command to turn on overlay debugging.
3059 Possibly this should be done via a set/show command. */
3062 overlay_off_command (char *args
, int from_tty
)
3064 overlay_debugging
= ovly_off
;
3065 disable_overlay_breakpoints ();
3067 printf_unfiltered (_("Overlay debugging disabled."));
3071 overlay_load_command (char *args
, int from_tty
)
3073 struct gdbarch
*gdbarch
= get_current_arch ();
3075 if (gdbarch_overlay_update_p (gdbarch
))
3076 gdbarch_overlay_update (gdbarch
, NULL
);
3078 error (_("This target does not know how to read its overlay state."));
3081 /* Function: overlay_command
3082 A place-holder for a mis-typed command */
3084 /* Command list chain containing all defined "overlay" subcommands. */
3085 struct cmd_list_element
*overlaylist
;
3088 overlay_command (char *args
, int from_tty
)
3091 ("\"overlay\" must be followed by the name of an overlay command.\n");
3092 help_list (overlaylist
, "overlay ", -1, gdb_stdout
);
3096 /* Target Overlays for the "Simplest" overlay manager:
3098 This is GDB's default target overlay layer. It works with the
3099 minimal overlay manager supplied as an example by Cygnus. The
3100 entry point is via a function pointer "gdbarch_overlay_update",
3101 so targets that use a different runtime overlay manager can
3102 substitute their own overlay_update function and take over the
3105 The overlay_update function pokes around in the target's data structures
3106 to see what overlays are mapped, and updates GDB's overlay mapping with
3109 In this simple implementation, the target data structures are as follows:
3110 unsigned _novlys; /# number of overlay sections #/
3111 unsigned _ovly_table[_novlys][4] = {
3112 {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/
3113 {..., ..., ..., ...},
3115 unsigned _novly_regions; /# number of overlay regions #/
3116 unsigned _ovly_region_table[_novly_regions][3] = {
3117 {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
3120 These functions will attempt to update GDB's mappedness state in the
3121 symbol section table, based on the target's mappedness state.
3123 To do this, we keep a cached copy of the target's _ovly_table, and
3124 attempt to detect when the cached copy is invalidated. The main
3125 entry point is "simple_overlay_update(SECT), which looks up SECT in
3126 the cached table and re-reads only the entry for that section from
3127 the target (whenever possible).
3130 /* Cached, dynamically allocated copies of the target data structures: */
3131 static unsigned (*cache_ovly_table
)[4] = 0;
3133 static unsigned (*cache_ovly_region_table
)[3] = 0;
3135 static unsigned cache_novlys
= 0;
3137 static unsigned cache_novly_regions
= 0;
3139 static CORE_ADDR cache_ovly_table_base
= 0;
3141 static CORE_ADDR cache_ovly_region_table_base
= 0;
3145 VMA
, SIZE
, LMA
, MAPPED
3148 /* Throw away the cached copy of _ovly_table */
3150 simple_free_overlay_table (void)
3152 if (cache_ovly_table
)
3153 xfree (cache_ovly_table
);
3155 cache_ovly_table
= NULL
;
3156 cache_ovly_table_base
= 0;
3160 /* Throw away the cached copy of _ovly_region_table */
3162 simple_free_overlay_region_table (void)
3164 if (cache_ovly_region_table
)
3165 xfree (cache_ovly_region_table
);
3166 cache_novly_regions
= 0;
3167 cache_ovly_region_table
= NULL
;
3168 cache_ovly_region_table_base
= 0;
3172 /* Read an array of ints of size SIZE from the target into a local buffer.
3173 Convert to host order. int LEN is number of ints */
3175 read_target_long_array (CORE_ADDR memaddr
, unsigned int *myaddr
,
3176 int len
, int size
, enum bfd_endian byte_order
)
3178 /* FIXME (alloca): Not safe if array is very large. */
3179 gdb_byte
*buf
= alloca (len
* size
);
3182 read_memory (memaddr
, buf
, len
* size
);
3183 for (i
= 0; i
< len
; i
++)
3184 myaddr
[i
] = extract_unsigned_integer (size
* i
+ buf
, size
, byte_order
);
3187 /* Find and grab a copy of the target _ovly_table
3188 (and _novlys, which is needed for the table's size) */
3190 simple_read_overlay_table (void)
3192 struct minimal_symbol
*novlys_msym
, *ovly_table_msym
;
3193 struct gdbarch
*gdbarch
;
3195 enum bfd_endian byte_order
;
3197 simple_free_overlay_table ();
3198 novlys_msym
= lookup_minimal_symbol ("_novlys", NULL
, NULL
);
3201 error (_("Error reading inferior's overlay table: "
3202 "couldn't find `_novlys' variable\n"
3203 "in inferior. Use `overlay manual' mode."));
3207 ovly_table_msym
= lookup_minimal_symbol ("_ovly_table", NULL
, NULL
);
3208 if (! ovly_table_msym
)
3210 error (_("Error reading inferior's overlay table: couldn't find "
3211 "`_ovly_table' array\n"
3212 "in inferior. Use `overlay manual' mode."));
3216 gdbarch
= get_objfile_arch (msymbol_objfile (ovly_table_msym
));
3217 word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3218 byte_order
= gdbarch_byte_order (gdbarch
);
3220 cache_novlys
= read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym
),
3223 = (void *) xmalloc (cache_novlys
* sizeof (*cache_ovly_table
));
3224 cache_ovly_table_base
= SYMBOL_VALUE_ADDRESS (ovly_table_msym
);
3225 read_target_long_array (cache_ovly_table_base
,
3226 (unsigned int *) cache_ovly_table
,
3227 cache_novlys
* 4, word_size
, byte_order
);
3229 return 1; /* SUCCESS */
3233 /* Find and grab a copy of the target _ovly_region_table
3234 (and _novly_regions, which is needed for the table's size) */
3236 simple_read_overlay_region_table (void)
3238 struct minimal_symbol
*msym
;
3239 struct gdbarch
*gdbarch
;
3241 enum bfd_endian byte_order
;
3243 simple_free_overlay_region_table ();
3244 msym
= lookup_minimal_symbol ("_novly_regions", NULL
, NULL
);
3246 return 0; /* failure */
3248 gdbarch
= get_objfile_arch (msymbol_objfile (msym
));
3249 word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3250 byte_order
= gdbarch_byte_order (gdbarch
);
3252 cache_novly_regions
= read_memory_integer (SYMBOL_VALUE_ADDRESS (msym
),
3255 cache_ovly_region_table
= (void *) xmalloc (cache_novly_regions
* 12);
3256 if (cache_ovly_region_table
!= NULL
)
3258 msym
= lookup_minimal_symbol ("_ovly_region_table", NULL
, NULL
);
3261 cache_ovly_region_table_base
= SYMBOL_VALUE_ADDRESS (msym
);
3262 read_target_long_array (cache_ovly_region_table_base
,
3263 (unsigned int *) cache_ovly_region_table
,
3264 cache_novly_regions
* 3,
3265 word_size
, byte_order
);
3268 return 0; /* failure */
3271 return 0; /* failure */
3272 return 1; /* SUCCESS */
3276 /* Function: simple_overlay_update_1
3277 A helper function for simple_overlay_update. Assuming a cached copy
3278 of _ovly_table exists, look through it to find an entry whose vma,
3279 lma and size match those of OSECT. Re-read the entry and make sure
3280 it still matches OSECT (else the table may no longer be valid).
3281 Set OSECT's mapped state to match the entry. Return: 1 for
3282 success, 0 for failure. */
3285 simple_overlay_update_1 (struct obj_section
*osect
)
3288 bfd
*obfd
= osect
->objfile
->obfd
;
3289 asection
*bsect
= osect
->the_bfd_section
;
3290 struct gdbarch
*gdbarch
= get_objfile_arch (osect
->objfile
);
3291 int word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3292 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3294 size
= bfd_get_section_size (osect
->the_bfd_section
);
3295 for (i
= 0; i
< cache_novlys
; i
++)
3296 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3297 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3298 /* && cache_ovly_table[i][SIZE] == size */ )
3300 read_target_long_array (cache_ovly_table_base
+ i
* word_size
,
3301 (unsigned int *) cache_ovly_table
[i
],
3302 4, word_size
, byte_order
);
3303 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3304 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3305 /* && cache_ovly_table[i][SIZE] == size */ )
3307 osect
->ovly_mapped
= cache_ovly_table
[i
][MAPPED
];
3310 else /* Warning! Warning! Target's ovly table has changed! */
3316 /* Function: simple_overlay_update
3317 If OSECT is NULL, then update all sections' mapped state
3318 (after re-reading the entire target _ovly_table).
3319 If OSECT is non-NULL, then try to find a matching entry in the
3320 cached ovly_table and update only OSECT's mapped state.
3321 If a cached entry can't be found or the cache isn't valid, then
3322 re-read the entire cache, and go ahead and update all sections. */
3325 simple_overlay_update (struct obj_section
*osect
)
3327 struct objfile
*objfile
;
3329 /* Were we given an osect to look up? NULL means do all of them. */
3331 /* Have we got a cached copy of the target's overlay table? */
3332 if (cache_ovly_table
!= NULL
)
3333 /* Does its cached location match what's currently in the symtab? */
3334 if (cache_ovly_table_base
==
3335 SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", NULL
, NULL
)))
3336 /* Then go ahead and try to look up this single section in the cache */
3337 if (simple_overlay_update_1 (osect
))
3338 /* Found it! We're done. */
3341 /* Cached table no good: need to read the entire table anew.
3342 Or else we want all the sections, in which case it's actually
3343 more efficient to read the whole table in one block anyway. */
3345 if (! simple_read_overlay_table ())
3348 /* Now may as well update all sections, even if only one was requested. */
3349 ALL_OBJSECTIONS (objfile
, osect
)
3350 if (section_is_overlay (osect
))
3353 bfd
*obfd
= osect
->objfile
->obfd
;
3354 asection
*bsect
= osect
->the_bfd_section
;
3356 size
= bfd_get_section_size (bsect
);
3357 for (i
= 0; i
< cache_novlys
; i
++)
3358 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3359 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3360 /* && cache_ovly_table[i][SIZE] == size */ )
3361 { /* obj_section matches i'th entry in ovly_table */
3362 osect
->ovly_mapped
= cache_ovly_table
[i
][MAPPED
];
3363 break; /* finished with inner for loop: break out */
3368 /* Set the output sections and output offsets for section SECTP in
3369 ABFD. The relocation code in BFD will read these offsets, so we
3370 need to be sure they're initialized. We map each section to itself,
3371 with no offset; this means that SECTP->vma will be honored. */
3374 symfile_dummy_outputs (bfd
*abfd
, asection
*sectp
, void *dummy
)
3376 sectp
->output_section
= sectp
;
3377 sectp
->output_offset
= 0;
3380 /* Default implementation for sym_relocate. */
3384 default_symfile_relocate (struct objfile
*objfile
, asection
*sectp
,
3387 bfd
*abfd
= objfile
->obfd
;
3389 /* We're only interested in sections with relocation
3391 if ((sectp
->flags
& SEC_RELOC
) == 0)
3394 /* We will handle section offsets properly elsewhere, so relocate as if
3395 all sections begin at 0. */
3396 bfd_map_over_sections (abfd
, symfile_dummy_outputs
, NULL
);
3398 return bfd_simple_get_relocated_section_contents (abfd
, sectp
, buf
, NULL
);
3401 /* Relocate the contents of a debug section SECTP in ABFD. The
3402 contents are stored in BUF if it is non-NULL, or returned in a
3403 malloc'd buffer otherwise.
3405 For some platforms and debug info formats, shared libraries contain
3406 relocations against the debug sections (particularly for DWARF-2;
3407 one affected platform is PowerPC GNU/Linux, although it depends on
3408 the version of the linker in use). Also, ELF object files naturally
3409 have unresolved relocations for their debug sections. We need to apply
3410 the relocations in order to get the locations of symbols correct.
3411 Another example that may require relocation processing, is the
3412 DWARF-2 .eh_frame section in .o files, although it isn't strictly a
3416 symfile_relocate_debug_section (struct objfile
*objfile
,
3417 asection
*sectp
, bfd_byte
*buf
)
3419 gdb_assert (objfile
->sf
->sym_relocate
);
3421 return (*objfile
->sf
->sym_relocate
) (objfile
, sectp
, buf
);
3424 struct symfile_segment_data
*
3425 get_symfile_segment_data (bfd
*abfd
)
3427 struct sym_fns
*sf
= find_sym_fns (abfd
);
3432 return sf
->sym_segments (abfd
);
3436 free_symfile_segment_data (struct symfile_segment_data
*data
)
3438 xfree (data
->segment_bases
);
3439 xfree (data
->segment_sizes
);
3440 xfree (data
->segment_info
);
3446 - DATA, containing segment addresses from the object file ABFD, and
3447 the mapping from ABFD's sections onto the segments that own them,
3449 - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
3450 segment addresses reported by the target,
3451 store the appropriate offsets for each section in OFFSETS.
3453 If there are fewer entries in SEGMENT_BASES than there are segments
3454 in DATA, then apply SEGMENT_BASES' last entry to all the segments.
3456 If there are more entries, then ignore the extra. The target may
3457 not be able to distinguish between an empty data segment and a
3458 missing data segment; a missing text segment is less plausible. */
3460 symfile_map_offsets_to_segments (bfd
*abfd
, struct symfile_segment_data
*data
,
3461 struct section_offsets
*offsets
,
3462 int num_segment_bases
,
3463 const CORE_ADDR
*segment_bases
)
3468 /* It doesn't make sense to call this function unless you have some
3469 segment base addresses. */
3470 gdb_assert (num_segment_bases
> 0);
3472 /* If we do not have segment mappings for the object file, we
3473 can not relocate it by segments. */
3474 gdb_assert (data
!= NULL
);
3475 gdb_assert (data
->num_segments
> 0);
3477 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
3479 int which
= data
->segment_info
[i
];
3481 gdb_assert (0 <= which
&& which
<= data
->num_segments
);
3483 /* Don't bother computing offsets for sections that aren't
3484 loaded as part of any segment. */
3488 /* Use the last SEGMENT_BASES entry as the address of any extra
3489 segments mentioned in DATA->segment_info. */
3490 if (which
> num_segment_bases
)
3491 which
= num_segment_bases
;
3493 offsets
->offsets
[i
] = (segment_bases
[which
- 1]
3494 - data
->segment_bases
[which
- 1]);
3501 symfile_find_segment_sections (struct objfile
*objfile
)
3503 bfd
*abfd
= objfile
->obfd
;
3506 struct symfile_segment_data
*data
;
3508 data
= get_symfile_segment_data (objfile
->obfd
);
3512 if (data
->num_segments
!= 1 && data
->num_segments
!= 2)
3514 free_symfile_segment_data (data
);
3518 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
3521 int which
= data
->segment_info
[i
];
3525 if (objfile
->sect_index_text
== -1)
3526 objfile
->sect_index_text
= sect
->index
;
3528 if (objfile
->sect_index_rodata
== -1)
3529 objfile
->sect_index_rodata
= sect
->index
;
3531 else if (which
== 2)
3533 if (objfile
->sect_index_data
== -1)
3534 objfile
->sect_index_data
= sect
->index
;
3536 if (objfile
->sect_index_bss
== -1)
3537 objfile
->sect_index_bss
= sect
->index
;
3541 free_symfile_segment_data (data
);
3545 _initialize_symfile (void)
3547 struct cmd_list_element
*c
;
3549 c
= add_cmd ("symbol-file", class_files
, symbol_file_command
, _("\
3550 Load symbol table from executable file FILE.\n\
3551 The `file' command can also load symbol tables, as well as setting the file\n\
3552 to execute."), &cmdlist
);
3553 set_cmd_completer (c
, filename_completer
);
3555 c
= add_cmd ("add-symbol-file", class_files
, add_symbol_file_command
, _("\
3556 Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
3557 Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]\n\
3558 ADDR is the starting address of the file's text.\n\
3559 The optional arguments are section-name section-address pairs and\n\
3560 should be specified if the data and bss segments are not contiguous\n\
3561 with the text. SECT is a section name to be loaded at SECT_ADDR."),
3563 set_cmd_completer (c
, filename_completer
);
3565 c
= add_cmd ("load", class_files
, load_command
, _("\
3566 Dynamically load FILE into the running program, and record its symbols\n\
3567 for access from GDB.\n\
3568 A load OFFSET may also be given."), &cmdlist
);
3569 set_cmd_completer (c
, filename_completer
);
3571 add_setshow_boolean_cmd ("symbol-reloading", class_support
,
3572 &symbol_reloading
, _("\
3573 Set dynamic symbol table reloading multiple times in one run."), _("\
3574 Show dynamic symbol table reloading multiple times in one run."), NULL
,
3576 show_symbol_reloading
,
3577 &setlist
, &showlist
);
3579 add_prefix_cmd ("overlay", class_support
, overlay_command
,
3580 _("Commands for debugging overlays."), &overlaylist
,
3581 "overlay ", 0, &cmdlist
);
3583 add_com_alias ("ovly", "overlay", class_alias
, 1);
3584 add_com_alias ("ov", "overlay", class_alias
, 1);
3586 add_cmd ("map-overlay", class_support
, map_overlay_command
,
3587 _("Assert that an overlay section is mapped."), &overlaylist
);
3589 add_cmd ("unmap-overlay", class_support
, unmap_overlay_command
,
3590 _("Assert that an overlay section is unmapped."), &overlaylist
);
3592 add_cmd ("list-overlays", class_support
, list_overlays_command
,
3593 _("List mappings of overlay sections."), &overlaylist
);
3595 add_cmd ("manual", class_support
, overlay_manual_command
,
3596 _("Enable overlay debugging."), &overlaylist
);
3597 add_cmd ("off", class_support
, overlay_off_command
,
3598 _("Disable overlay debugging."), &overlaylist
);
3599 add_cmd ("auto", class_support
, overlay_auto_command
,
3600 _("Enable automatic overlay debugging."), &overlaylist
);
3601 add_cmd ("load-target", class_support
, overlay_load_command
,
3602 _("Read the overlay mapping state from the target."), &overlaylist
);
3604 /* Filename extension to source language lookup table: */
3605 init_filename_language_table ();
3606 add_setshow_string_noescape_cmd ("extension-language", class_files
,
3608 Set mapping between filename extension and source language."), _("\
3609 Show mapping between filename extension and source language."), _("\
3610 Usage: set extension-language .foo bar"),
3611 set_ext_lang_command
,
3613 &setlist
, &showlist
);
3615 add_info ("extensions", info_ext_lang_command
,
3616 _("All filename extensions associated with a source language."));
3618 add_setshow_optional_filename_cmd ("debug-file-directory", class_support
,
3619 &debug_file_directory
, _("\
3620 Set the directories where separate debug symbols are searched for."), _("\
3621 Show the directories where separate debug symbols are searched for."), _("\
3622 Separate debug symbols are first searched for in the same\n\
3623 directory as the binary, then in the `" DEBUG_SUBDIRECTORY
"' subdirectory,\n\
3624 and lastly at the path of the directory of the binary with\n\
3625 each global debug-file-directory component prepended."),
3627 show_debug_file_directory
,
3628 &setlist
, &showlist
);