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 ABFD. */
324 static struct section_addr_info
*
325 build_section_addr_info_from_bfd (bfd
*abfd
)
327 struct section_addr_info
*sap
;
329 struct bfd_section
*sec
;
331 sap
= alloc_section_addr_info (bfd_count_sections (abfd
));
332 for (i
= 0, sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
333 if (bfd_get_section_flags (abfd
, sec
) & (SEC_ALLOC
| SEC_LOAD
))
335 sap
->other
[i
].addr
= bfd_get_section_vma (abfd
, sec
);
336 sap
->other
[i
].name
= xstrdup (bfd_get_section_name (abfd
, sec
));
337 sap
->other
[i
].sectindex
= sec
->index
;
343 /* Create a section_addr_info from section offsets in OBJFILE. */
345 struct section_addr_info
*
346 build_section_addr_info_from_objfile (const struct objfile
*objfile
)
348 struct section_addr_info
*sap
;
351 /* Before reread_symbols gets rewritten it is not safe to call:
352 gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
354 sap
= build_section_addr_info_from_bfd (objfile
->obfd
);
355 for (i
= 0; i
< sap
->num_sections
&& sap
->other
[i
].name
; i
++)
357 int sectindex
= sap
->other
[i
].sectindex
;
359 sap
->other
[i
].addr
+= objfile
->section_offsets
->offsets
[sectindex
];
364 /* Free all memory allocated by build_section_addr_info_from_section_table. */
367 free_section_addr_info (struct section_addr_info
*sap
)
371 for (idx
= 0; idx
< sap
->num_sections
; idx
++)
372 if (sap
->other
[idx
].name
)
373 xfree (sap
->other
[idx
].name
);
378 /* Initialize OBJFILE's sect_index_* members. */
380 init_objfile_sect_indices (struct objfile
*objfile
)
385 sect
= bfd_get_section_by_name (objfile
->obfd
, ".text");
387 objfile
->sect_index_text
= sect
->index
;
389 sect
= bfd_get_section_by_name (objfile
->obfd
, ".data");
391 objfile
->sect_index_data
= sect
->index
;
393 sect
= bfd_get_section_by_name (objfile
->obfd
, ".bss");
395 objfile
->sect_index_bss
= sect
->index
;
397 sect
= bfd_get_section_by_name (objfile
->obfd
, ".rodata");
399 objfile
->sect_index_rodata
= sect
->index
;
401 /* This is where things get really weird... We MUST have valid
402 indices for the various sect_index_* members or gdb will abort.
403 So if for example, there is no ".text" section, we have to
404 accomodate that. First, check for a file with the standard
405 one or two segments. */
407 symfile_find_segment_sections (objfile
);
409 /* Except when explicitly adding symbol files at some address,
410 section_offsets contains nothing but zeros, so it doesn't matter
411 which slot in section_offsets the individual sect_index_* members
412 index into. So if they are all zero, it is safe to just point
413 all the currently uninitialized indices to the first slot. But
414 beware: if this is the main executable, it may be relocated
415 later, e.g. by the remote qOffsets packet, and then this will
416 be wrong! That's why we try segments first. */
418 for (i
= 0; i
< objfile
->num_sections
; i
++)
420 if (ANOFFSET (objfile
->section_offsets
, i
) != 0)
425 if (i
== objfile
->num_sections
)
427 if (objfile
->sect_index_text
== -1)
428 objfile
->sect_index_text
= 0;
429 if (objfile
->sect_index_data
== -1)
430 objfile
->sect_index_data
= 0;
431 if (objfile
->sect_index_bss
== -1)
432 objfile
->sect_index_bss
= 0;
433 if (objfile
->sect_index_rodata
== -1)
434 objfile
->sect_index_rodata
= 0;
438 /* The arguments to place_section. */
440 struct place_section_arg
442 struct section_offsets
*offsets
;
446 /* Find a unique offset to use for loadable section SECT if
447 the user did not provide an offset. */
450 place_section (bfd
*abfd
, asection
*sect
, void *obj
)
452 struct place_section_arg
*arg
= obj
;
453 CORE_ADDR
*offsets
= arg
->offsets
->offsets
, start_addr
;
455 ULONGEST align
= ((ULONGEST
) 1) << bfd_get_section_alignment (abfd
, sect
);
457 /* We are only interested in allocated sections. */
458 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
461 /* If the user specified an offset, honor it. */
462 if (offsets
[sect
->index
] != 0)
465 /* Otherwise, let's try to find a place for the section. */
466 start_addr
= (arg
->lowest
+ align
- 1) & -align
;
473 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
; cur_sec
= cur_sec
->next
)
475 int indx
= cur_sec
->index
;
477 /* We don't need to compare against ourself. */
481 /* We can only conflict with allocated sections. */
482 if ((bfd_get_section_flags (abfd
, cur_sec
) & SEC_ALLOC
) == 0)
485 /* If the section offset is 0, either the section has not been placed
486 yet, or it was the lowest section placed (in which case LOWEST
487 will be past its end). */
488 if (offsets
[indx
] == 0)
491 /* If this section would overlap us, then we must move up. */
492 if (start_addr
+ bfd_get_section_size (sect
) > offsets
[indx
]
493 && start_addr
< offsets
[indx
] + bfd_get_section_size (cur_sec
))
495 start_addr
= offsets
[indx
] + bfd_get_section_size (cur_sec
);
496 start_addr
= (start_addr
+ align
- 1) & -align
;
501 /* Otherwise, we appear to be OK. So far. */
506 offsets
[sect
->index
] = start_addr
;
507 arg
->lowest
= start_addr
+ bfd_get_section_size (sect
);
510 /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
511 filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
515 relative_addr_info_to_section_offsets (struct section_offsets
*section_offsets
,
517 struct section_addr_info
*addrs
)
521 memset (section_offsets
, 0, SIZEOF_N_SECTION_OFFSETS (num_sections
));
523 /* Now calculate offsets for section that were specified by the caller. */
524 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
526 struct other_sections
*osp
;
528 osp
= &addrs
->other
[i
];
532 /* Record all sections in offsets */
533 /* The section_offsets in the objfile are here filled in using
535 section_offsets
->offsets
[osp
->sectindex
] = osp
->addr
;
539 /* qsort comparator for addrs_section_sort. Sort entries in ascending order by
540 their (name, sectindex) pair. sectindex makes the sort by name stable. */
543 addrs_section_compar (const void *ap
, const void *bp
)
545 const struct other_sections
*a
= *((struct other_sections
**) ap
);
546 const struct other_sections
*b
= *((struct other_sections
**) bp
);
547 int retval
, a_idx
, b_idx
;
549 retval
= strcmp (a
->name
, b
->name
);
553 /* SECTINDEX is undefined iff ADDR is zero. */
554 a_idx
= a
->addr
== 0 ? 0 : a
->sectindex
;
555 b_idx
= b
->addr
== 0 ? 0 : b
->sectindex
;
556 return a_idx
- b_idx
;
559 /* Provide sorted array of pointers to sections of ADDRS. The array is
560 terminated by NULL. Caller is responsible to call xfree for it. */
562 static struct other_sections
**
563 addrs_section_sort (struct section_addr_info
*addrs
)
565 struct other_sections
**array
;
568 /* `+ 1' for the NULL terminator. */
569 array
= xmalloc (sizeof (*array
) * (addrs
->num_sections
+ 1));
570 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
571 array
[i
] = &addrs
->other
[i
];
574 qsort (array
, i
, sizeof (*array
), addrs_section_compar
);
579 /* Relativize absolute addresses in ADDRS into offsets based on ABFD. Fill-in
580 also SECTINDEXes specific to ABFD there. This function can be used to
581 rebase ADDRS to start referencing different BFD than before. */
584 addr_info_make_relative (struct section_addr_info
*addrs
, bfd
*abfd
)
586 asection
*lower_sect
;
587 CORE_ADDR lower_offset
;
589 struct cleanup
*my_cleanup
;
590 struct section_addr_info
*abfd_addrs
;
591 struct other_sections
**addrs_sorted
, **abfd_addrs_sorted
;
592 struct other_sections
**addrs_to_abfd_addrs
;
594 /* Find lowest loadable section to be used as starting point for
595 continguous sections. */
597 bfd_map_over_sections (abfd
, find_lowest_section
, &lower_sect
);
598 if (lower_sect
== NULL
)
600 warning (_("no loadable sections found in added symbol-file %s"),
601 bfd_get_filename (abfd
));
605 lower_offset
= bfd_section_vma (bfd_get_filename (abfd
), lower_sect
);
607 /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
608 in ABFD. Section names are not unique - there can be multiple sections of
609 the same name. Also the sections of the same name do not have to be
610 adjacent to each other. Some sections may be present only in one of the
611 files. Even sections present in both files do not have to be in the same
614 Use stable sort by name for the sections in both files. Then linearly
615 scan both lists matching as most of the entries as possible. */
617 addrs_sorted
= addrs_section_sort (addrs
);
618 my_cleanup
= make_cleanup (xfree
, addrs_sorted
);
620 abfd_addrs
= build_section_addr_info_from_bfd (abfd
);
621 make_cleanup_free_section_addr_info (abfd_addrs
);
622 abfd_addrs_sorted
= addrs_section_sort (abfd_addrs
);
623 make_cleanup (xfree
, abfd_addrs_sorted
);
625 /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and ABFD_ADDRS_SORTED. */
627 addrs_to_abfd_addrs
= xzalloc (sizeof (*addrs_to_abfd_addrs
)
628 * addrs
->num_sections
);
629 make_cleanup (xfree
, addrs_to_abfd_addrs
);
631 while (*addrs_sorted
)
633 const char *sect_name
= (*addrs_sorted
)->name
;
635 while (*abfd_addrs_sorted
636 && strcmp ((*abfd_addrs_sorted
)->name
, sect_name
) < 0)
639 if (*abfd_addrs_sorted
640 && strcmp ((*abfd_addrs_sorted
)->name
, sect_name
) == 0)
644 /* Make the found item directly addressable from ADDRS. */
645 index_in_addrs
= *addrs_sorted
- addrs
->other
;
646 gdb_assert (addrs_to_abfd_addrs
[index_in_addrs
] == NULL
);
647 addrs_to_abfd_addrs
[index_in_addrs
] = *abfd_addrs_sorted
;
649 /* Never use the same ABFD entry twice. */
656 /* Calculate offsets for the loadable sections.
657 FIXME! Sections must be in order of increasing loadable section
658 so that contiguous sections can use the lower-offset!!!
660 Adjust offsets if the segments are not contiguous.
661 If the section is contiguous, its offset should be set to
662 the offset of the highest loadable section lower than it
663 (the loadable section directly below it in memory).
664 this_offset = lower_offset = lower_addr - lower_orig_addr */
666 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
668 const char *sect_name
= addrs
->other
[i
].name
;
669 struct other_sections
*sect
= addrs_to_abfd_addrs
[i
];
673 /* This is the index used by BFD. */
674 addrs
->other
[i
].sectindex
= sect
->sectindex
;
676 if (addrs
->other
[i
].addr
!= 0)
678 addrs
->other
[i
].addr
-= sect
->addr
;
679 lower_offset
= addrs
->other
[i
].addr
;
682 addrs
->other
[i
].addr
= lower_offset
;
686 /* This section does not exist in ABFD, which is normally
687 unexpected and we want to issue a warning.
689 However, the ELF prelinker does create a few sections which are
690 marked in the main executable as loadable (they are loaded in
691 memory from the DYNAMIC segment) and yet are not present in
692 separate debug info files. This is fine, and should not cause
693 a warning. Shared libraries contain just the section
694 ".gnu.liblist" but it is not marked as loadable there. There is
695 no other way to identify them than by their name as the sections
696 created by prelink have no special flags. */
698 if (!(strcmp (sect_name
, ".gnu.liblist") == 0
699 || strcmp (sect_name
, ".gnu.conflict") == 0
700 || strcmp (sect_name
, ".dynbss") == 0
701 || strcmp (sect_name
, ".sdynbss") == 0))
702 warning (_("section %s not found in %s"), sect_name
,
703 bfd_get_filename (abfd
));
705 addrs
->other
[i
].addr
= 0;
707 /* SECTINDEX is invalid if ADDR is zero. */
711 do_cleanups (my_cleanup
);
714 /* Parse the user's idea of an offset for dynamic linking, into our idea
715 of how to represent it for fast symbol reading. This is the default
716 version of the sym_fns.sym_offsets function for symbol readers that
717 don't need to do anything special. It allocates a section_offsets table
718 for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */
721 default_symfile_offsets (struct objfile
*objfile
,
722 struct section_addr_info
*addrs
)
724 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
725 objfile
->section_offsets
= (struct section_offsets
*)
726 obstack_alloc (&objfile
->objfile_obstack
,
727 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
728 relative_addr_info_to_section_offsets (objfile
->section_offsets
,
729 objfile
->num_sections
, addrs
);
731 /* For relocatable files, all loadable sections will start at zero.
732 The zero is meaningless, so try to pick arbitrary addresses such
733 that no loadable sections overlap. This algorithm is quadratic,
734 but the number of sections in a single object file is generally
736 if ((bfd_get_file_flags (objfile
->obfd
) & (EXEC_P
| DYNAMIC
)) == 0)
738 struct place_section_arg arg
;
739 bfd
*abfd
= objfile
->obfd
;
742 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
; cur_sec
= cur_sec
->next
)
743 /* We do not expect this to happen; just skip this step if the
744 relocatable file has a section with an assigned VMA. */
745 if (bfd_section_vma (abfd
, cur_sec
) != 0)
750 CORE_ADDR
*offsets
= objfile
->section_offsets
->offsets
;
752 /* Pick non-overlapping offsets for sections the user did not
754 arg
.offsets
= objfile
->section_offsets
;
756 bfd_map_over_sections (objfile
->obfd
, place_section
, &arg
);
758 /* Correctly filling in the section offsets is not quite
759 enough. Relocatable files have two properties that
760 (most) shared objects do not:
762 - Their debug information will contain relocations. Some
763 shared libraries do also, but many do not, so this can not
766 - If there are multiple code sections they will be loaded
767 at different relative addresses in memory than they are
768 in the objfile, since all sections in the file will start
771 Because GDB has very limited ability to map from an
772 address in debug info to the correct code section,
773 it relies on adding SECT_OFF_TEXT to things which might be
774 code. If we clear all the section offsets, and set the
775 section VMAs instead, then symfile_relocate_debug_section
776 will return meaningful debug information pointing at the
779 GDB has too many different data structures for section
780 addresses - a bfd, objfile, and so_list all have section
781 tables, as does exec_ops. Some of these could probably
784 for (cur_sec
= abfd
->sections
; cur_sec
!= NULL
;
785 cur_sec
= cur_sec
->next
)
787 if ((bfd_get_section_flags (abfd
, cur_sec
) & SEC_ALLOC
) == 0)
790 bfd_set_section_vma (abfd
, cur_sec
, offsets
[cur_sec
->index
]);
791 exec_set_section_address (bfd_get_filename (abfd
), cur_sec
->index
,
792 offsets
[cur_sec
->index
]);
793 offsets
[cur_sec
->index
] = 0;
798 /* Remember the bfd indexes for the .text, .data, .bss and
800 init_objfile_sect_indices (objfile
);
804 /* Divide the file into segments, which are individual relocatable units.
805 This is the default version of the sym_fns.sym_segments function for
806 symbol readers that do not have an explicit representation of segments.
807 It assumes that object files do not have segments, and fully linked
808 files have a single segment. */
810 struct symfile_segment_data
*
811 default_symfile_segments (bfd
*abfd
)
815 struct symfile_segment_data
*data
;
818 /* Relocatable files contain enough information to position each
819 loadable section independently; they should not be relocated
821 if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
)) == 0)
824 /* Make sure there is at least one loadable section in the file. */
825 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
827 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
835 low
= bfd_get_section_vma (abfd
, sect
);
836 high
= low
+ bfd_get_section_size (sect
);
838 data
= XZALLOC (struct symfile_segment_data
);
839 data
->num_segments
= 1;
840 data
->segment_bases
= XCALLOC (1, CORE_ADDR
);
841 data
->segment_sizes
= XCALLOC (1, CORE_ADDR
);
843 num_sections
= bfd_count_sections (abfd
);
844 data
->segment_info
= XCALLOC (num_sections
, int);
846 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
850 if ((bfd_get_section_flags (abfd
, sect
) & SEC_ALLOC
) == 0)
853 vma
= bfd_get_section_vma (abfd
, sect
);
856 if (vma
+ bfd_get_section_size (sect
) > high
)
857 high
= vma
+ bfd_get_section_size (sect
);
859 data
->segment_info
[i
] = 1;
862 data
->segment_bases
[0] = low
;
863 data
->segment_sizes
[0] = high
- low
;
868 /* Process a symbol file, as either the main file or as a dynamically
871 OBJFILE is where the symbols are to be read from.
873 ADDRS is the list of section load addresses. If the user has given
874 an 'add-symbol-file' command, then this is the list of offsets and
875 addresses he or she provided as arguments to the command; or, if
876 we're handling a shared library, these are the actual addresses the
877 sections are loaded at, according to the inferior's dynamic linker
878 (as gleaned by GDB's shared library code). We convert each address
879 into an offset from the section VMA's as it appears in the object
880 file, and then call the file's sym_offsets function to convert this
881 into a format-specific offset table --- a `struct section_offsets'.
882 If ADDRS is non-zero, OFFSETS must be zero.
884 OFFSETS is a table of section offsets already in the right
885 format-specific representation. NUM_OFFSETS is the number of
886 elements present in OFFSETS->offsets. If OFFSETS is non-zero, we
887 assume this is the proper table the call to sym_offsets described
888 above would produce. Instead of calling sym_offsets, we just dump
889 it right into objfile->section_offsets. (When we're re-reading
890 symbols from an objfile, we don't have the original load address
891 list any more; all we have is the section offset table.) If
892 OFFSETS is non-zero, ADDRS must be zero.
894 ADD_FLAGS encodes verbosity level, whether this is main symbol or
895 an extra symbol file such as dynamically loaded code, and wether
896 breakpoint reset should be deferred. */
899 syms_from_objfile (struct objfile
*objfile
,
900 struct section_addr_info
*addrs
,
901 struct section_offsets
*offsets
,
905 struct section_addr_info
*local_addr
= NULL
;
906 struct cleanup
*old_chain
;
907 const int mainline
= add_flags
& SYMFILE_MAINLINE
;
909 gdb_assert (! (addrs
&& offsets
));
911 init_entry_point_info (objfile
);
912 objfile
->sf
= find_sym_fns (objfile
->obfd
);
914 if (objfile
->sf
== NULL
)
915 return; /* No symbols. */
917 /* Make sure that partially constructed symbol tables will be cleaned up
918 if an error occurs during symbol reading. */
919 old_chain
= make_cleanup_free_objfile (objfile
);
921 /* If ADDRS and OFFSETS are both NULL, put together a dummy address
922 list. We now establish the convention that an addr of zero means
923 no load address was specified. */
924 if (! addrs
&& ! offsets
)
927 = alloc_section_addr_info (bfd_count_sections (objfile
->obfd
));
928 make_cleanup (xfree
, local_addr
);
932 /* Now either addrs or offsets is non-zero. */
936 /* We will modify the main symbol table, make sure that all its users
937 will be cleaned up if an error occurs during symbol reading. */
938 make_cleanup (clear_symtab_users_cleanup
, 0 /*ignore*/);
940 /* Since no error yet, throw away the old symbol table. */
942 if (symfile_objfile
!= NULL
)
944 free_objfile (symfile_objfile
);
945 gdb_assert (symfile_objfile
== NULL
);
948 /* Currently we keep symbols from the add-symbol-file command.
949 If the user wants to get rid of them, they should do "symbol-file"
950 without arguments first. Not sure this is the best behavior
953 (*objfile
->sf
->sym_new_init
) (objfile
);
956 /* Convert addr into an offset rather than an absolute address.
957 We find the lowest address of a loaded segment in the objfile,
958 and assume that <addr> is where that got loaded.
960 We no longer warn if the lowest section is not a text segment (as
961 happens for the PA64 port. */
962 if (addrs
&& addrs
->other
[0].name
)
963 addr_info_make_relative (addrs
, objfile
->obfd
);
965 /* Initialize symbol reading routines for this objfile, allow complaints to
966 appear for this new file, and record how verbose to be, then do the
967 initial symbol reading for this file. */
969 (*objfile
->sf
->sym_init
) (objfile
);
970 clear_complaints (&symfile_complaints
, 1, add_flags
& SYMFILE_VERBOSE
);
973 (*objfile
->sf
->sym_offsets
) (objfile
, addrs
);
976 size_t size
= SIZEOF_N_SECTION_OFFSETS (num_offsets
);
978 /* Just copy in the offset table directly as given to us. */
979 objfile
->num_sections
= num_offsets
;
980 objfile
->section_offsets
981 = ((struct section_offsets
*)
982 obstack_alloc (&objfile
->objfile_obstack
, size
));
983 memcpy (objfile
->section_offsets
, offsets
, size
);
985 init_objfile_sect_indices (objfile
);
988 (*objfile
->sf
->sym_read
) (objfile
, add_flags
);
990 /* Discard cleanups as symbol reading was successful. */
992 discard_cleanups (old_chain
);
996 /* Perform required actions after either reading in the initial
997 symbols for a new objfile, or mapping in the symbols from a reusable
1001 new_symfile_objfile (struct objfile
*objfile
, int add_flags
)
1004 /* If this is the main symbol file we have to clean up all users of the
1005 old main symbol file. Otherwise it is sufficient to fixup all the
1006 breakpoints that may have been redefined by this symbol file. */
1007 if (add_flags
& SYMFILE_MAINLINE
)
1009 /* OK, make it the "real" symbol file. */
1010 symfile_objfile
= objfile
;
1012 clear_symtab_users ();
1014 else if ((add_flags
& SYMFILE_DEFER_BP_RESET
) == 0)
1016 breakpoint_re_set ();
1019 /* We're done reading the symbol file; finish off complaints. */
1020 clear_complaints (&symfile_complaints
, 0, add_flags
& SYMFILE_VERBOSE
);
1023 /* Process a symbol file, as either the main file or as a dynamically
1026 ABFD is a BFD already open on the file, as from symfile_bfd_open.
1027 This BFD will be closed on error, and is always consumed by this function.
1029 ADD_FLAGS encodes verbosity, whether this is main symbol file or
1030 extra, such as dynamically loaded code, and what to do with breakpoins.
1032 ADDRS, OFFSETS, and NUM_OFFSETS are as described for
1033 syms_from_objfile, above.
1034 ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
1036 Upon success, returns a pointer to the objfile that was added.
1037 Upon failure, jumps back to command level (never returns). */
1039 static struct objfile
*
1040 symbol_file_add_with_addrs_or_offsets (bfd
*abfd
,
1042 struct section_addr_info
*addrs
,
1043 struct section_offsets
*offsets
,
1047 struct objfile
*objfile
;
1048 struct cleanup
*my_cleanups
;
1049 const char *name
= bfd_get_filename (abfd
);
1050 const int from_tty
= add_flags
& SYMFILE_VERBOSE
;
1052 my_cleanups
= make_cleanup_bfd_close (abfd
);
1054 /* Give user a chance to burp if we'd be
1055 interactively wiping out any existing symbols. */
1057 if ((have_full_symbols () || have_partial_symbols ())
1058 && (add_flags
& SYMFILE_MAINLINE
)
1060 && !query (_("Load new symbol table from \"%s\"? "), name
))
1061 error (_("Not confirmed."));
1063 objfile
= allocate_objfile (abfd
, flags
);
1064 discard_cleanups (my_cleanups
);
1066 /* We either created a new mapped symbol table, mapped an existing
1067 symbol table file which has not had initial symbol reading
1068 performed, or need to read an unmapped symbol table. */
1069 if (from_tty
|| info_verbose
)
1071 if (deprecated_pre_add_symbol_hook
)
1072 deprecated_pre_add_symbol_hook (name
);
1075 printf_unfiltered (_("Reading symbols from %s..."), name
);
1077 gdb_flush (gdb_stdout
);
1080 syms_from_objfile (objfile
, addrs
, offsets
, num_offsets
,
1083 /* We now have at least a partial symbol table. Check to see if the
1084 user requested that all symbols be read on initial access via either
1085 the gdb startup command line or on a per symbol file basis. Expand
1086 all partial symbol tables for this objfile if so. */
1088 if ((flags
& OBJF_READNOW
) || readnow_symbol_files
)
1090 if (from_tty
|| info_verbose
)
1092 printf_unfiltered (_("expanding to full symbols..."));
1094 gdb_flush (gdb_stdout
);
1098 objfile
->sf
->qf
->expand_all_symtabs (objfile
);
1101 if ((from_tty
|| info_verbose
)
1102 && !objfile_has_symbols (objfile
))
1105 printf_unfiltered (_("(no debugging symbols found)..."));
1109 if (from_tty
|| info_verbose
)
1111 if (deprecated_post_add_symbol_hook
)
1112 deprecated_post_add_symbol_hook ();
1114 printf_unfiltered (_("done.\n"));
1117 /* We print some messages regardless of whether 'from_tty ||
1118 info_verbose' is true, so make sure they go out at the right
1120 gdb_flush (gdb_stdout
);
1122 do_cleanups (my_cleanups
);
1124 if (objfile
->sf
== NULL
)
1126 observer_notify_new_objfile (objfile
);
1127 return objfile
; /* No symbols. */
1130 new_symfile_objfile (objfile
, add_flags
);
1132 observer_notify_new_objfile (objfile
);
1134 bfd_cache_close_all ();
1138 /* Add BFD as a separate debug file for OBJFILE. */
1141 symbol_file_add_separate (bfd
*bfd
, int symfile_flags
, struct objfile
*objfile
)
1143 struct objfile
*new_objfile
;
1144 struct section_addr_info
*sap
;
1145 struct cleanup
*my_cleanup
;
1147 /* Create section_addr_info. We can't directly use offsets from OBJFILE
1148 because sections of BFD may not match sections of OBJFILE and because
1149 vma may have been modified by tools such as prelink. */
1150 sap
= build_section_addr_info_from_objfile (objfile
);
1151 my_cleanup
= make_cleanup_free_section_addr_info (sap
);
1153 new_objfile
= symbol_file_add_with_addrs_or_offsets
1154 (bfd
, symfile_flags
,
1156 objfile
->flags
& (OBJF_REORDERED
| OBJF_SHARED
| OBJF_READNOW
1157 | OBJF_USERLOADED
));
1159 do_cleanups (my_cleanup
);
1161 add_separate_debug_objfile (new_objfile
, objfile
);
1164 /* Process the symbol file ABFD, as either the main file or as a
1165 dynamically loaded file.
1167 See symbol_file_add_with_addrs_or_offsets's comments for
1170 symbol_file_add_from_bfd (bfd
*abfd
, int add_flags
,
1171 struct section_addr_info
*addrs
,
1174 return symbol_file_add_with_addrs_or_offsets (abfd
, add_flags
, addrs
, 0, 0,
1179 /* Process a symbol file, as either the main file or as a dynamically
1180 loaded file. See symbol_file_add_with_addrs_or_offsets's comments
1183 symbol_file_add (char *name
, int add_flags
, struct section_addr_info
*addrs
,
1186 return symbol_file_add_from_bfd (symfile_bfd_open (name
), add_flags
, addrs
,
1191 /* Call symbol_file_add() with default values and update whatever is
1192 affected by the loading of a new main().
1193 Used when the file is supplied in the gdb command line
1194 and by some targets with special loading requirements.
1195 The auxiliary function, symbol_file_add_main_1(), has the flags
1196 argument for the switches that can only be specified in the symbol_file
1200 symbol_file_add_main (char *args
, int from_tty
)
1202 symbol_file_add_main_1 (args
, from_tty
, 0);
1206 symbol_file_add_main_1 (char *args
, int from_tty
, int flags
)
1208 const int add_flags
= SYMFILE_MAINLINE
| (from_tty
? SYMFILE_VERBOSE
: 0);
1209 symbol_file_add (args
, add_flags
, NULL
, flags
);
1211 /* Getting new symbols may change our opinion about
1212 what is frameless. */
1213 reinit_frame_cache ();
1215 set_initial_language ();
1219 symbol_file_clear (int from_tty
)
1221 if ((have_full_symbols () || have_partial_symbols ())
1224 ? !query (_("Discard symbol table from `%s'? "),
1225 symfile_objfile
->name
)
1226 : !query (_("Discard symbol table? "))))
1227 error (_("Not confirmed."));
1229 /* solib descriptors may have handles to objfiles. Wipe them before their
1230 objfiles get stale by free_all_objfiles. */
1231 no_shared_libraries (NULL
, from_tty
);
1233 free_all_objfiles ();
1235 gdb_assert (symfile_objfile
== NULL
);
1237 printf_unfiltered (_("No symbol file now.\n"));
1241 get_debug_link_info (struct objfile
*objfile
, unsigned long *crc32_out
)
1244 bfd_size_type debuglink_size
;
1245 unsigned long crc32
;
1249 sect
= bfd_get_section_by_name (objfile
->obfd
, ".gnu_debuglink");
1254 debuglink_size
= bfd_section_size (objfile
->obfd
, sect
);
1256 contents
= xmalloc (debuglink_size
);
1257 bfd_get_section_contents (objfile
->obfd
, sect
, contents
,
1258 (file_ptr
)0, (bfd_size_type
)debuglink_size
);
1260 /* Crc value is stored after the filename, aligned up to 4 bytes. */
1261 crc_offset
= strlen (contents
) + 1;
1262 crc_offset
= (crc_offset
+ 3) & ~3;
1264 crc32
= bfd_get_32 (objfile
->obfd
, (bfd_byte
*) (contents
+ crc_offset
));
1271 separate_debug_file_exists (const char *name
, unsigned long crc
,
1272 struct objfile
*parent_objfile
)
1274 unsigned long file_crc
= 0;
1276 gdb_byte buffer
[8*1024];
1278 struct stat parent_stat
, abfd_stat
;
1280 /* Find a separate debug info file as if symbols would be present in
1281 PARENT_OBJFILE itself this function would not be called. .gnu_debuglink
1282 section can contain just the basename of PARENT_OBJFILE without any
1283 ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
1284 the separate debug infos with the same basename can exist. */
1286 if (strcmp (name
, parent_objfile
->name
) == 0)
1289 abfd
= bfd_open_maybe_remote (name
);
1294 /* Verify symlinks were not the cause of strcmp name difference above.
1296 Some operating systems, e.g. Windows, do not provide a meaningful
1297 st_ino; they always set it to zero. (Windows does provide a
1298 meaningful st_dev.) Do not indicate a duplicate library in that
1299 case. While there is no guarantee that a system that provides
1300 meaningful inode numbers will never set st_ino to zero, this is
1301 merely an optimization, so we do not need to worry about false
1304 if (bfd_stat (abfd
, &abfd_stat
) == 0
1305 && bfd_stat (parent_objfile
->obfd
, &parent_stat
) == 0
1306 && abfd_stat
.st_dev
== parent_stat
.st_dev
1307 && abfd_stat
.st_ino
== parent_stat
.st_ino
1308 && abfd_stat
.st_ino
!= 0)
1314 while ((count
= bfd_bread (buffer
, sizeof (buffer
), abfd
)) > 0)
1315 file_crc
= gnu_debuglink_crc32 (file_crc
, buffer
, count
);
1319 if (crc
!= file_crc
)
1321 warning (_("the debug information found in \"%s\""
1322 " does not match \"%s\" (CRC mismatch).\n"),
1323 name
, parent_objfile
->name
);
1330 char *debug_file_directory
= NULL
;
1332 show_debug_file_directory (struct ui_file
*file
, int from_tty
,
1333 struct cmd_list_element
*c
, const char *value
)
1335 fprintf_filtered (file
, _("\
1336 The directory where separate debug symbols are searched for is \"%s\".\n"),
1340 #if ! defined (DEBUG_SUBDIRECTORY)
1341 #define DEBUG_SUBDIRECTORY ".debug"
1345 find_separate_debug_file_by_debuglink (struct objfile
*objfile
)
1347 char *basename
, *debugdir
;
1349 char *debugfile
= NULL
;
1350 char *canon_name
= NULL
;
1351 unsigned long crc32
;
1354 basename
= get_debug_link_info (objfile
, &crc32
);
1356 if (basename
== NULL
)
1357 /* There's no separate debug info, hence there's no way we could
1358 load it => no warning. */
1359 goto cleanup_return_debugfile
;
1361 dir
= xstrdup (objfile
->name
);
1363 /* Strip off the final filename part, leaving the directory name,
1364 followed by a slash. Objfile names should always be absolute and
1365 tilde-expanded, so there should always be a slash in there
1367 for (i
= strlen(dir
) - 1; i
>= 0; i
--)
1369 if (IS_DIR_SEPARATOR (dir
[i
]))
1372 gdb_assert (i
>= 0 && IS_DIR_SEPARATOR (dir
[i
]));
1375 /* Set I to max (strlen (canon_name), strlen (dir)). */
1376 canon_name
= lrealpath (dir
);
1378 if (canon_name
&& strlen (canon_name
) > i
)
1379 i
= strlen (canon_name
);
1381 debugfile
= xmalloc (strlen (debug_file_directory
) + 1
1383 + strlen (DEBUG_SUBDIRECTORY
)
1388 /* First try in the same directory as the original file. */
1389 strcpy (debugfile
, dir
);
1390 strcat (debugfile
, basename
);
1392 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1393 goto cleanup_return_debugfile
;
1395 /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */
1396 strcpy (debugfile
, dir
);
1397 strcat (debugfile
, DEBUG_SUBDIRECTORY
);
1398 strcat (debugfile
, "/");
1399 strcat (debugfile
, basename
);
1401 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1402 goto cleanup_return_debugfile
;
1404 /* Then try in the global debugfile directories.
1406 Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1407 cause "/..." lookups. */
1409 debugdir
= debug_file_directory
;
1414 while (*debugdir
== DIRNAME_SEPARATOR
)
1417 debugdir_end
= strchr (debugdir
, DIRNAME_SEPARATOR
);
1418 if (debugdir_end
== NULL
)
1419 debugdir_end
= &debugdir
[strlen (debugdir
)];
1421 memcpy (debugfile
, debugdir
, debugdir_end
- debugdir
);
1422 debugfile
[debugdir_end
- debugdir
] = 0;
1423 strcat (debugfile
, "/");
1424 strcat (debugfile
, dir
);
1425 strcat (debugfile
, basename
);
1427 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1428 goto cleanup_return_debugfile
;
1430 /* If the file is in the sysroot, try using its base path in the
1431 global debugfile directory. */
1433 && strncmp (canon_name
, gdb_sysroot
, strlen (gdb_sysroot
)) == 0
1434 && IS_DIR_SEPARATOR (canon_name
[strlen (gdb_sysroot
)]))
1436 memcpy (debugfile
, debugdir
, debugdir_end
- debugdir
);
1437 debugfile
[debugdir_end
- debugdir
] = 0;
1438 strcat (debugfile
, canon_name
+ strlen (gdb_sysroot
));
1439 strcat (debugfile
, "/");
1440 strcat (debugfile
, basename
);
1442 if (separate_debug_file_exists (debugfile
, crc32
, objfile
))
1443 goto cleanup_return_debugfile
;
1446 debugdir
= debugdir_end
;
1448 while (*debugdir
!= 0);
1453 cleanup_return_debugfile
:
1461 /* This is the symbol-file command. Read the file, analyze its
1462 symbols, and add a struct symtab to a symtab list. The syntax of
1463 the command is rather bizarre:
1465 1. The function buildargv implements various quoting conventions
1466 which are undocumented and have little or nothing in common with
1467 the way things are quoted (or not quoted) elsewhere in GDB.
1469 2. Options are used, which are not generally used in GDB (perhaps
1470 "set mapped on", "set readnow on" would be better)
1472 3. The order of options matters, which is contrary to GNU
1473 conventions (because it is confusing and inconvenient). */
1476 symbol_file_command (char *args
, int from_tty
)
1482 symbol_file_clear (from_tty
);
1486 char **argv
= gdb_buildargv (args
);
1487 int flags
= OBJF_USERLOADED
;
1488 struct cleanup
*cleanups
;
1491 cleanups
= make_cleanup_freeargv (argv
);
1492 while (*argv
!= NULL
)
1494 if (strcmp (*argv
, "-readnow") == 0)
1495 flags
|= OBJF_READNOW
;
1496 else if (**argv
== '-')
1497 error (_("unknown option `%s'"), *argv
);
1500 symbol_file_add_main_1 (*argv
, from_tty
, flags
);
1508 error (_("no symbol file name was specified"));
1510 do_cleanups (cleanups
);
1514 /* Set the initial language.
1516 FIXME: A better solution would be to record the language in the
1517 psymtab when reading partial symbols, and then use it (if known) to
1518 set the language. This would be a win for formats that encode the
1519 language in an easily discoverable place, such as DWARF. For
1520 stabs, we can jump through hoops looking for specially named
1521 symbols or try to intuit the language from the specific type of
1522 stabs we find, but we can't do that until later when we read in
1526 set_initial_language (void)
1529 enum language lang
= language_unknown
;
1531 filename
= find_main_filename ();
1532 if (filename
!= NULL
)
1533 lang
= deduce_language_from_filename (filename
);
1535 if (lang
== language_unknown
)
1537 /* Make C the default language */
1541 set_language (lang
);
1542 expected_language
= current_language
; /* Don't warn the user. */
1545 /* If NAME is a remote name open the file using remote protocol, otherwise
1546 open it normally. */
1549 bfd_open_maybe_remote (const char *name
)
1551 if (remote_filename_p (name
))
1552 return remote_bfd_open (name
, gnutarget
);
1554 return bfd_openr (name
, gnutarget
);
1558 /* Open the file specified by NAME and hand it off to BFD for
1559 preliminary analysis. Return a newly initialized bfd *, which
1560 includes a newly malloc'd` copy of NAME (tilde-expanded and made
1561 absolute). In case of trouble, error() is called. */
1564 symfile_bfd_open (char *name
)
1568 char *absolute_name
;
1570 if (remote_filename_p (name
))
1572 name
= xstrdup (name
);
1573 sym_bfd
= remote_bfd_open (name
, gnutarget
);
1576 make_cleanup (xfree
, name
);
1577 error (_("`%s': can't open to read symbols: %s."), name
,
1578 bfd_errmsg (bfd_get_error ()));
1581 if (!bfd_check_format (sym_bfd
, bfd_object
))
1583 bfd_close (sym_bfd
);
1584 make_cleanup (xfree
, name
);
1585 error (_("`%s': can't read symbols: %s."), name
,
1586 bfd_errmsg (bfd_get_error ()));
1592 name
= tilde_expand (name
); /* Returns 1st new malloc'd copy. */
1594 /* Look down path for it, allocate 2nd new malloc'd copy. */
1595 desc
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
, name
,
1596 O_RDONLY
| O_BINARY
, &absolute_name
);
1597 #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
1600 char *exename
= alloca (strlen (name
) + 5);
1601 strcat (strcpy (exename
, name
), ".exe");
1602 desc
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
, exename
,
1603 O_RDONLY
| O_BINARY
, &absolute_name
);
1608 make_cleanup (xfree
, name
);
1609 perror_with_name (name
);
1612 /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
1613 bfd. It'll be freed in free_objfile(). */
1615 name
= absolute_name
;
1617 sym_bfd
= bfd_fopen (name
, gnutarget
, FOPEN_RB
, desc
);
1621 make_cleanup (xfree
, name
);
1622 error (_("`%s': can't open to read symbols: %s."), name
,
1623 bfd_errmsg (bfd_get_error ()));
1625 bfd_set_cacheable (sym_bfd
, 1);
1627 if (!bfd_check_format (sym_bfd
, bfd_object
))
1629 /* FIXME: should be checking for errors from bfd_close (for one
1630 thing, on error it does not free all the storage associated
1632 bfd_close (sym_bfd
); /* This also closes desc. */
1633 make_cleanup (xfree
, name
);
1634 error (_("`%s': can't read symbols: %s."), name
,
1635 bfd_errmsg (bfd_get_error ()));
1638 /* bfd_usrdata exists for applications and libbfd must not touch it. */
1639 gdb_assert (bfd_usrdata (sym_bfd
) == NULL
);
1644 /* Return the section index for SECTION_NAME on OBJFILE. Return -1 if
1645 the section was not found. */
1648 get_section_index (struct objfile
*objfile
, char *section_name
)
1650 asection
*sect
= bfd_get_section_by_name (objfile
->obfd
, section_name
);
1658 /* Link SF into the global symtab_fns list. Called on startup by the
1659 _initialize routine in each object file format reader, to register
1660 information about each format the the reader is prepared to
1664 add_symtab_fns (struct sym_fns
*sf
)
1666 sf
->next
= symtab_fns
;
1670 /* Initialize OBJFILE to read symbols from its associated BFD. It
1671 either returns or calls error(). The result is an initialized
1672 struct sym_fns in the objfile structure, that contains cached
1673 information about the symbol file. */
1675 static struct sym_fns
*
1676 find_sym_fns (bfd
*abfd
)
1679 enum bfd_flavour our_flavour
= bfd_get_flavour (abfd
);
1681 if (our_flavour
== bfd_target_srec_flavour
1682 || our_flavour
== bfd_target_ihex_flavour
1683 || our_flavour
== bfd_target_tekhex_flavour
)
1684 return NULL
; /* No symbols. */
1686 for (sf
= symtab_fns
; sf
!= NULL
; sf
= sf
->next
)
1687 if (our_flavour
== sf
->sym_flavour
)
1690 error (_("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown."),
1691 bfd_get_target (abfd
));
1695 /* This function runs the load command of our current target. */
1698 load_command (char *arg
, int from_tty
)
1700 /* The user might be reloading because the binary has changed. Take
1701 this opportunity to check. */
1702 reopen_exec_file ();
1710 parg
= arg
= get_exec_file (1);
1712 /* Count how many \ " ' tab space there are in the name. */
1713 while ((parg
= strpbrk (parg
, "\\\"'\t ")))
1721 /* We need to quote this string so buildargv can pull it apart. */
1722 char *temp
= xmalloc (strlen (arg
) + count
+ 1 );
1726 make_cleanup (xfree
, temp
);
1729 while ((parg
= strpbrk (parg
, "\\\"'\t ")))
1731 strncpy (ptemp
, prev
, parg
- prev
);
1732 ptemp
+= parg
- prev
;
1736 strcpy (ptemp
, prev
);
1742 target_load (arg
, from_tty
);
1744 /* After re-loading the executable, we don't really know which
1745 overlays are mapped any more. */
1746 overlay_cache_invalid
= 1;
1749 /* This version of "load" should be usable for any target. Currently
1750 it is just used for remote targets, not inftarg.c or core files,
1751 on the theory that only in that case is it useful.
1753 Avoiding xmodem and the like seems like a win (a) because we don't have
1754 to worry about finding it, and (b) On VMS, fork() is very slow and so
1755 we don't want to run a subprocess. On the other hand, I'm not sure how
1756 performance compares. */
1758 static int validate_download
= 0;
1760 /* Callback service function for generic_load (bfd_map_over_sections). */
1763 add_section_size_callback (bfd
*abfd
, asection
*asec
, void *data
)
1765 bfd_size_type
*sum
= data
;
1767 *sum
+= bfd_get_section_size (asec
);
1770 /* Opaque data for load_section_callback. */
1771 struct load_section_data
{
1772 unsigned long load_offset
;
1773 struct load_progress_data
*progress_data
;
1774 VEC(memory_write_request_s
) *requests
;
1777 /* Opaque data for load_progress. */
1778 struct load_progress_data
{
1779 /* Cumulative data. */
1780 unsigned long write_count
;
1781 unsigned long data_count
;
1782 bfd_size_type total_size
;
1785 /* Opaque data for load_progress for a single section. */
1786 struct load_progress_section_data
{
1787 struct load_progress_data
*cumulative
;
1789 /* Per-section data. */
1790 const char *section_name
;
1791 ULONGEST section_sent
;
1792 ULONGEST section_size
;
1797 /* Target write callback routine for progress reporting. */
1800 load_progress (ULONGEST bytes
, void *untyped_arg
)
1802 struct load_progress_section_data
*args
= untyped_arg
;
1803 struct load_progress_data
*totals
;
1806 /* Writing padding data. No easy way to get at the cumulative
1807 stats, so just ignore this. */
1810 totals
= args
->cumulative
;
1812 if (bytes
== 0 && args
->section_sent
== 0)
1814 /* The write is just starting. Let the user know we've started
1816 ui_out_message (uiout
, 0, "Loading section %s, size %s lma %s\n",
1817 args
->section_name
, hex_string (args
->section_size
),
1818 paddress (target_gdbarch
, args
->lma
));
1822 if (validate_download
)
1824 /* Broken memories and broken monitors manifest themselves here
1825 when bring new computers to life. This doubles already slow
1827 /* NOTE: cagney/1999-10-18: A more efficient implementation
1828 might add a verify_memory() method to the target vector and
1829 then use that. remote.c could implement that method using
1830 the ``qCRC'' packet. */
1831 gdb_byte
*check
= xmalloc (bytes
);
1832 struct cleanup
*verify_cleanups
= make_cleanup (xfree
, check
);
1834 if (target_read_memory (args
->lma
, check
, bytes
) != 0)
1835 error (_("Download verify read failed at %s"),
1836 paddress (target_gdbarch
, args
->lma
));
1837 if (memcmp (args
->buffer
, check
, bytes
) != 0)
1838 error (_("Download verify compare failed at %s"),
1839 paddress (target_gdbarch
, args
->lma
));
1840 do_cleanups (verify_cleanups
);
1842 totals
->data_count
+= bytes
;
1844 args
->buffer
+= bytes
;
1845 totals
->write_count
+= 1;
1846 args
->section_sent
+= bytes
;
1848 || (deprecated_ui_load_progress_hook
!= NULL
1849 && deprecated_ui_load_progress_hook (args
->section_name
,
1850 args
->section_sent
)))
1851 error (_("Canceled the download"));
1853 if (deprecated_show_load_progress
!= NULL
)
1854 deprecated_show_load_progress (args
->section_name
,
1858 totals
->total_size
);
1861 /* Callback service function for generic_load (bfd_map_over_sections). */
1864 load_section_callback (bfd
*abfd
, asection
*asec
, void *data
)
1866 struct memory_write_request
*new_request
;
1867 struct load_section_data
*args
= data
;
1868 struct load_progress_section_data
*section_data
;
1869 bfd_size_type size
= bfd_get_section_size (asec
);
1871 const char *sect_name
= bfd_get_section_name (abfd
, asec
);
1873 if ((bfd_get_section_flags (abfd
, asec
) & SEC_LOAD
) == 0)
1879 new_request
= VEC_safe_push (memory_write_request_s
,
1880 args
->requests
, NULL
);
1881 memset (new_request
, 0, sizeof (struct memory_write_request
));
1882 section_data
= xcalloc (1, sizeof (struct load_progress_section_data
));
1883 new_request
->begin
= bfd_section_lma (abfd
, asec
) + args
->load_offset
;
1884 new_request
->end
= new_request
->begin
+ size
; /* FIXME Should size be in instead? */
1885 new_request
->data
= xmalloc (size
);
1886 new_request
->baton
= section_data
;
1888 buffer
= new_request
->data
;
1890 section_data
->cumulative
= args
->progress_data
;
1891 section_data
->section_name
= sect_name
;
1892 section_data
->section_size
= size
;
1893 section_data
->lma
= new_request
->begin
;
1894 section_data
->buffer
= buffer
;
1896 bfd_get_section_contents (abfd
, asec
, buffer
, 0, size
);
1899 /* Clean up an entire memory request vector, including load
1900 data and progress records. */
1903 clear_memory_write_data (void *arg
)
1905 VEC(memory_write_request_s
) **vec_p
= arg
;
1906 VEC(memory_write_request_s
) *vec
= *vec_p
;
1908 struct memory_write_request
*mr
;
1910 for (i
= 0; VEC_iterate (memory_write_request_s
, vec
, i
, mr
); ++i
)
1915 VEC_free (memory_write_request_s
, vec
);
1919 generic_load (char *args
, int from_tty
)
1922 struct timeval start_time
, end_time
;
1924 struct cleanup
*old_cleanups
= make_cleanup (null_cleanup
, 0);
1925 struct load_section_data cbdata
;
1926 struct load_progress_data total_progress
;
1931 memset (&cbdata
, 0, sizeof (cbdata
));
1932 memset (&total_progress
, 0, sizeof (total_progress
));
1933 cbdata
.progress_data
= &total_progress
;
1935 make_cleanup (clear_memory_write_data
, &cbdata
.requests
);
1938 error_no_arg (_("file to load"));
1940 argv
= gdb_buildargv (args
);
1941 make_cleanup_freeargv (argv
);
1943 filename
= tilde_expand (argv
[0]);
1944 make_cleanup (xfree
, filename
);
1946 if (argv
[1] != NULL
)
1950 cbdata
.load_offset
= strtoul (argv
[1], &endptr
, 0);
1952 /* If the last word was not a valid number then
1953 treat it as a file name with spaces in. */
1954 if (argv
[1] == endptr
)
1955 error (_("Invalid download offset:%s."), argv
[1]);
1957 if (argv
[2] != NULL
)
1958 error (_("Too many parameters."));
1961 /* Open the file for loading. */
1962 loadfile_bfd
= bfd_openr (filename
, gnutarget
);
1963 if (loadfile_bfd
== NULL
)
1965 perror_with_name (filename
);
1969 /* FIXME: should be checking for errors from bfd_close (for one thing,
1970 on error it does not free all the storage associated with the
1972 make_cleanup_bfd_close (loadfile_bfd
);
1974 if (!bfd_check_format (loadfile_bfd
, bfd_object
))
1976 error (_("\"%s\" is not an object file: %s"), filename
,
1977 bfd_errmsg (bfd_get_error ()));
1980 bfd_map_over_sections (loadfile_bfd
, add_section_size_callback
,
1981 (void *) &total_progress
.total_size
);
1983 bfd_map_over_sections (loadfile_bfd
, load_section_callback
, &cbdata
);
1985 gettimeofday (&start_time
, NULL
);
1987 if (target_write_memory_blocks (cbdata
.requests
, flash_discard
,
1988 load_progress
) != 0)
1989 error (_("Load failed"));
1991 gettimeofday (&end_time
, NULL
);
1993 entry
= bfd_get_start_address (loadfile_bfd
);
1994 ui_out_text (uiout
, "Start address ");
1995 ui_out_field_fmt (uiout
, "address", "%s", paddress (target_gdbarch
, entry
));
1996 ui_out_text (uiout
, ", load size ");
1997 ui_out_field_fmt (uiout
, "load-size", "%lu", total_progress
.data_count
);
1998 ui_out_text (uiout
, "\n");
1999 /* We were doing this in remote-mips.c, I suspect it is right
2000 for other targets too. */
2001 regcache_write_pc (get_current_regcache (), entry
);
2003 /* Reset breakpoints, now that we have changed the load image. For
2004 instance, breakpoints may have been set (or reset, by
2005 post_create_inferior) while connected to the target but before we
2006 loaded the program. In that case, the prologue analyzer could
2007 have read instructions from the target to find the right
2008 breakpoint locations. Loading has changed the contents of that
2011 breakpoint_re_set ();
2013 /* FIXME: are we supposed to call symbol_file_add or not? According
2014 to a comment from remote-mips.c (where a call to symbol_file_add
2015 was commented out), making the call confuses GDB if more than one
2016 file is loaded in. Some targets do (e.g., remote-vx.c) but
2017 others don't (or didn't - perhaps they have all been deleted). */
2019 print_transfer_performance (gdb_stdout
, total_progress
.data_count
,
2020 total_progress
.write_count
,
2021 &start_time
, &end_time
);
2023 do_cleanups (old_cleanups
);
2026 /* Report how fast the transfer went. */
2028 /* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being
2029 replaced by print_transfer_performance (with a very different
2030 function signature). */
2033 report_transfer_performance (unsigned long data_count
, time_t start_time
,
2036 struct timeval start
, end
;
2038 start
.tv_sec
= start_time
;
2040 end
.tv_sec
= end_time
;
2043 print_transfer_performance (gdb_stdout
, data_count
, 0, &start
, &end
);
2047 print_transfer_performance (struct ui_file
*stream
,
2048 unsigned long data_count
,
2049 unsigned long write_count
,
2050 const struct timeval
*start_time
,
2051 const struct timeval
*end_time
)
2053 ULONGEST time_count
;
2055 /* Compute the elapsed time in milliseconds, as a tradeoff between
2056 accuracy and overflow. */
2057 time_count
= (end_time
->tv_sec
- start_time
->tv_sec
) * 1000;
2058 time_count
+= (end_time
->tv_usec
- start_time
->tv_usec
) / 1000;
2060 ui_out_text (uiout
, "Transfer rate: ");
2063 unsigned long rate
= ((ULONGEST
) data_count
* 1000) / time_count
;
2065 if (ui_out_is_mi_like_p (uiout
))
2067 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
* 8);
2068 ui_out_text (uiout
, " bits/sec");
2070 else if (rate
< 1024)
2072 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
);
2073 ui_out_text (uiout
, " bytes/sec");
2077 ui_out_field_fmt (uiout
, "transfer-rate", "%lu", rate
/ 1024);
2078 ui_out_text (uiout
, " KB/sec");
2083 ui_out_field_fmt (uiout
, "transferred-bits", "%lu", (data_count
* 8));
2084 ui_out_text (uiout
, " bits in <1 sec");
2086 if (write_count
> 0)
2088 ui_out_text (uiout
, ", ");
2089 ui_out_field_fmt (uiout
, "write-rate", "%lu", data_count
/ write_count
);
2090 ui_out_text (uiout
, " bytes/write");
2092 ui_out_text (uiout
, ".\n");
2095 /* This function allows the addition of incrementally linked object files.
2096 It does not modify any state in the target, only in the debugger. */
2097 /* Note: ezannoni 2000-04-13 This function/command used to have a
2098 special case syntax for the rombug target (Rombug is the boot
2099 monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
2100 rombug case, the user doesn't need to supply a text address,
2101 instead a call to target_link() (in target.c) would supply the
2102 value to use. We are now discontinuing this type of ad hoc syntax. */
2105 add_symbol_file_command (char *args
, int from_tty
)
2107 struct gdbarch
*gdbarch
= get_current_arch ();
2108 char *filename
= NULL
;
2109 int flags
= OBJF_USERLOADED
;
2111 int section_index
= 0;
2115 int expecting_sec_name
= 0;
2116 int expecting_sec_addr
= 0;
2125 struct section_addr_info
*section_addrs
;
2126 struct sect_opt
*sect_opts
= NULL
;
2127 size_t num_sect_opts
= 0;
2128 struct cleanup
*my_cleanups
= make_cleanup (null_cleanup
, NULL
);
2131 sect_opts
= (struct sect_opt
*) xmalloc (num_sect_opts
2132 * sizeof (struct sect_opt
));
2137 error (_("add-symbol-file takes a file name and an address"));
2139 argv
= gdb_buildargv (args
);
2140 make_cleanup_freeargv (argv
);
2142 for (arg
= argv
[0], argcnt
= 0; arg
!= NULL
; arg
= argv
[++argcnt
])
2144 /* Process the argument. */
2147 /* The first argument is the file name. */
2148 filename
= tilde_expand (arg
);
2149 make_cleanup (xfree
, filename
);
2154 /* The second argument is always the text address at which
2155 to load the program. */
2156 sect_opts
[section_index
].name
= ".text";
2157 sect_opts
[section_index
].value
= arg
;
2158 if (++section_index
>= num_sect_opts
)
2161 sect_opts
= ((struct sect_opt
*)
2162 xrealloc (sect_opts
,
2164 * sizeof (struct sect_opt
)));
2169 /* It's an option (starting with '-') or it's an argument
2174 if (strcmp (arg
, "-readnow") == 0)
2175 flags
|= OBJF_READNOW
;
2176 else if (strcmp (arg
, "-s") == 0)
2178 expecting_sec_name
= 1;
2179 expecting_sec_addr
= 1;
2184 if (expecting_sec_name
)
2186 sect_opts
[section_index
].name
= arg
;
2187 expecting_sec_name
= 0;
2190 if (expecting_sec_addr
)
2192 sect_opts
[section_index
].value
= arg
;
2193 expecting_sec_addr
= 0;
2194 if (++section_index
>= num_sect_opts
)
2197 sect_opts
= ((struct sect_opt
*)
2198 xrealloc (sect_opts
,
2200 * sizeof (struct sect_opt
)));
2204 error (_("USAGE: add-symbol-file <filename> <textaddress> [-mapped] [-readnow] [-s <secname> <addr>]*"));
2209 /* This command takes at least two arguments. The first one is a
2210 filename, and the second is the address where this file has been
2211 loaded. Abort now if this address hasn't been provided by the
2213 if (section_index
< 1)
2214 error (_("The address where %s has been loaded is missing"), filename
);
2216 /* Print the prompt for the query below. And save the arguments into
2217 a sect_addr_info structure to be passed around to other
2218 functions. We have to split this up into separate print
2219 statements because hex_string returns a local static
2222 printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename
);
2223 section_addrs
= alloc_section_addr_info (section_index
);
2224 make_cleanup (xfree
, section_addrs
);
2225 for (i
= 0; i
< section_index
; i
++)
2228 char *val
= sect_opts
[i
].value
;
2229 char *sec
= sect_opts
[i
].name
;
2231 addr
= parse_and_eval_address (val
);
2233 /* Here we store the section offsets in the order they were
2234 entered on the command line. */
2235 section_addrs
->other
[sec_num
].name
= sec
;
2236 section_addrs
->other
[sec_num
].addr
= addr
;
2237 printf_unfiltered ("\t%s_addr = %s\n", sec
,
2238 paddress (gdbarch
, addr
));
2241 /* The object's sections are initialized when a
2242 call is made to build_objfile_section_table (objfile).
2243 This happens in reread_symbols.
2244 At this point, we don't know what file type this is,
2245 so we can't determine what section names are valid. */
2248 if (from_tty
&& (!query ("%s", "")))
2249 error (_("Not confirmed."));
2251 symbol_file_add (filename
, from_tty
? SYMFILE_VERBOSE
: 0,
2252 section_addrs
, flags
);
2254 /* Getting new symbols may change our opinion about what is
2256 reinit_frame_cache ();
2257 do_cleanups (my_cleanups
);
2261 /* Re-read symbols if a symbol-file has changed. */
2263 reread_symbols (void)
2265 struct objfile
*objfile
;
2268 struct stat new_statbuf
;
2271 /* With the addition of shared libraries, this should be modified,
2272 the load time should be saved in the partial symbol tables, since
2273 different tables may come from different source files. FIXME.
2274 This routine should then walk down each partial symbol table
2275 and see if the symbol table that it originates from has been changed */
2277 for (objfile
= object_files
; objfile
; objfile
= objfile
->next
)
2279 /* solib-sunos.c creates one objfile with obfd. */
2280 if (objfile
->obfd
== NULL
)
2283 /* Separate debug objfiles are handled in the main objfile. */
2284 if (objfile
->separate_debug_objfile_backlink
)
2287 /* If this object is from an archive (what you usually create with
2288 `ar', often called a `static library' on most systems, though
2289 a `shared library' on AIX is also an archive), then you should
2290 stat on the archive name, not member name. */
2291 if (objfile
->obfd
->my_archive
)
2292 res
= stat (objfile
->obfd
->my_archive
->filename
, &new_statbuf
);
2294 res
= stat (objfile
->name
, &new_statbuf
);
2297 /* FIXME, should use print_sys_errmsg but it's not filtered. */
2298 printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
2302 new_modtime
= new_statbuf
.st_mtime
;
2303 if (new_modtime
!= objfile
->mtime
)
2305 struct cleanup
*old_cleanups
;
2306 struct section_offsets
*offsets
;
2308 char *obfd_filename
;
2310 printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
2313 /* There are various functions like symbol_file_add,
2314 symfile_bfd_open, syms_from_objfile, etc., which might
2315 appear to do what we want. But they have various other
2316 effects which we *don't* want. So we just do stuff
2317 ourselves. We don't worry about mapped files (for one thing,
2318 any mapped file will be out of date). */
2320 /* If we get an error, blow away this objfile (not sure if
2321 that is the correct response for things like shared
2323 old_cleanups
= make_cleanup_free_objfile (objfile
);
2324 /* We need to do this whenever any symbols go away. */
2325 make_cleanup (clear_symtab_users_cleanup
, 0 /*ignore*/);
2327 if (exec_bfd
!= NULL
&& strcmp (bfd_get_filename (objfile
->obfd
),
2328 bfd_get_filename (exec_bfd
)) == 0)
2330 /* Reload EXEC_BFD without asking anything. */
2332 exec_file_attach (bfd_get_filename (objfile
->obfd
), 0);
2335 /* Clean up any state BFD has sitting around. We don't need
2336 to close the descriptor but BFD lacks a way of closing the
2337 BFD without closing the descriptor. */
2338 obfd_filename
= bfd_get_filename (objfile
->obfd
);
2339 if (!bfd_close (objfile
->obfd
))
2340 error (_("Can't close BFD for %s: %s"), objfile
->name
,
2341 bfd_errmsg (bfd_get_error ()));
2342 objfile
->obfd
= bfd_open_maybe_remote (obfd_filename
);
2343 if (objfile
->obfd
== NULL
)
2344 error (_("Can't open %s to read symbols."), objfile
->name
);
2346 objfile
->obfd
= gdb_bfd_ref (objfile
->obfd
);
2347 /* bfd_openr sets cacheable to true, which is what we want. */
2348 if (!bfd_check_format (objfile
->obfd
, bfd_object
))
2349 error (_("Can't read symbols from %s: %s."), objfile
->name
,
2350 bfd_errmsg (bfd_get_error ()));
2352 /* Save the offsets, we will nuke them with the rest of the
2354 num_offsets
= objfile
->num_sections
;
2355 offsets
= ((struct section_offsets
*)
2356 alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets
)));
2357 memcpy (offsets
, objfile
->section_offsets
,
2358 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2360 /* Remove any references to this objfile in the global
2362 preserve_values (objfile
);
2364 /* Nuke all the state that we will re-read. Much of the following
2365 code which sets things to NULL really is necessary to tell
2366 other parts of GDB that there is nothing currently there.
2368 Try to keep the freeing order compatible with free_objfile. */
2370 if (objfile
->sf
!= NULL
)
2372 (*objfile
->sf
->sym_finish
) (objfile
);
2375 clear_objfile_data (objfile
);
2377 /* Free the separate debug objfiles. It will be
2378 automatically recreated by sym_read. */
2379 free_objfile_separate_debug (objfile
);
2381 /* FIXME: Do we have to free a whole linked list, or is this
2383 if (objfile
->global_psymbols
.list
)
2384 xfree (objfile
->global_psymbols
.list
);
2385 memset (&objfile
->global_psymbols
, 0,
2386 sizeof (objfile
->global_psymbols
));
2387 if (objfile
->static_psymbols
.list
)
2388 xfree (objfile
->static_psymbols
.list
);
2389 memset (&objfile
->static_psymbols
, 0,
2390 sizeof (objfile
->static_psymbols
));
2392 /* Free the obstacks for non-reusable objfiles */
2393 bcache_xfree (objfile
->psymbol_cache
);
2394 objfile
->psymbol_cache
= bcache_xmalloc ();
2395 bcache_xfree (objfile
->macro_cache
);
2396 objfile
->macro_cache
= bcache_xmalloc ();
2397 bcache_xfree (objfile
->filename_cache
);
2398 objfile
->filename_cache
= bcache_xmalloc ();
2399 if (objfile
->demangled_names_hash
!= NULL
)
2401 htab_delete (objfile
->demangled_names_hash
);
2402 objfile
->demangled_names_hash
= NULL
;
2404 obstack_free (&objfile
->objfile_obstack
, 0);
2405 objfile
->sections
= NULL
;
2406 objfile
->symtabs
= NULL
;
2407 objfile
->psymtabs
= NULL
;
2408 objfile
->psymtabs_addrmap
= NULL
;
2409 objfile
->free_psymtabs
= NULL
;
2410 objfile
->cp_namespace_symtab
= NULL
;
2411 objfile
->msymbols
= NULL
;
2412 objfile
->deprecated_sym_private
= NULL
;
2413 objfile
->minimal_symbol_count
= 0;
2414 memset (&objfile
->msymbol_hash
, 0,
2415 sizeof (objfile
->msymbol_hash
));
2416 memset (&objfile
->msymbol_demangled_hash
, 0,
2417 sizeof (objfile
->msymbol_demangled_hash
));
2419 objfile
->psymbol_cache
= bcache_xmalloc ();
2420 objfile
->macro_cache
= bcache_xmalloc ();
2421 objfile
->filename_cache
= bcache_xmalloc ();
2422 /* obstack_init also initializes the obstack so it is
2423 empty. We could use obstack_specify_allocation but
2424 gdb_obstack.h specifies the alloc/dealloc
2426 obstack_init (&objfile
->objfile_obstack
);
2427 if (build_objfile_section_table (objfile
))
2429 error (_("Can't find the file sections in `%s': %s"),
2430 objfile
->name
, bfd_errmsg (bfd_get_error ()));
2432 terminate_minimal_symbol_table (objfile
);
2434 /* We use the same section offsets as from last time. I'm not
2435 sure whether that is always correct for shared libraries. */
2436 objfile
->section_offsets
= (struct section_offsets
*)
2437 obstack_alloc (&objfile
->objfile_obstack
,
2438 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2439 memcpy (objfile
->section_offsets
, offsets
,
2440 SIZEOF_N_SECTION_OFFSETS (num_offsets
));
2441 objfile
->num_sections
= num_offsets
;
2443 /* What the hell is sym_new_init for, anyway? The concept of
2444 distinguishing between the main file and additional files
2445 in this way seems rather dubious. */
2446 if (objfile
== symfile_objfile
)
2448 (*objfile
->sf
->sym_new_init
) (objfile
);
2451 (*objfile
->sf
->sym_init
) (objfile
);
2452 clear_complaints (&symfile_complaints
, 1, 1);
2453 /* Do not set flags as this is safe and we don't want to be
2455 (*objfile
->sf
->sym_read
) (objfile
, 0);
2456 if (!objfile_has_symbols (objfile
))
2459 printf_unfiltered (_("(no debugging symbols found)\n"));
2463 /* We're done reading the symbol file; finish off complaints. */
2464 clear_complaints (&symfile_complaints
, 0, 1);
2466 /* Getting new symbols may change our opinion about what is
2469 reinit_frame_cache ();
2471 /* Discard cleanups as symbol reading was successful. */
2472 discard_cleanups (old_cleanups
);
2474 /* If the mtime has changed between the time we set new_modtime
2475 and now, we *want* this to be out of date, so don't call stat
2477 objfile
->mtime
= new_modtime
;
2479 init_entry_point_info (objfile
);
2485 /* Notify objfiles that we've modified objfile sections. */
2486 objfiles_changed ();
2488 clear_symtab_users ();
2489 /* At least one objfile has changed, so we can consider that
2490 the executable we're debugging has changed too. */
2491 observer_notify_executable_changed ();
2504 static filename_language
*filename_language_table
;
2505 static int fl_table_size
, fl_table_next
;
2508 add_filename_language (char *ext
, enum language lang
)
2510 if (fl_table_next
>= fl_table_size
)
2512 fl_table_size
+= 10;
2513 filename_language_table
=
2514 xrealloc (filename_language_table
,
2515 fl_table_size
* sizeof (*filename_language_table
));
2518 filename_language_table
[fl_table_next
].ext
= xstrdup (ext
);
2519 filename_language_table
[fl_table_next
].lang
= lang
;
2523 static char *ext_args
;
2525 show_ext_args (struct ui_file
*file
, int from_tty
,
2526 struct cmd_list_element
*c
, const char *value
)
2528 fprintf_filtered (file
, _("\
2529 Mapping between filename extension and source language is \"%s\".\n"),
2534 set_ext_lang_command (char *args
, int from_tty
, struct cmd_list_element
*e
)
2537 char *cp
= ext_args
;
2540 /* First arg is filename extension, starting with '.' */
2542 error (_("'%s': Filename extension must begin with '.'"), ext_args
);
2544 /* Find end of first arg. */
2545 while (*cp
&& !isspace (*cp
))
2549 error (_("'%s': two arguments required -- filename extension and language"),
2552 /* Null-terminate first arg */
2555 /* Find beginning of second arg, which should be a source language. */
2556 while (*cp
&& isspace (*cp
))
2560 error (_("'%s': two arguments required -- filename extension and language"),
2563 /* Lookup the language from among those we know. */
2564 lang
= language_enum (cp
);
2566 /* Now lookup the filename extension: do we already know it? */
2567 for (i
= 0; i
< fl_table_next
; i
++)
2568 if (0 == strcmp (ext_args
, filename_language_table
[i
].ext
))
2571 if (i
>= fl_table_next
)
2573 /* new file extension */
2574 add_filename_language (ext_args
, lang
);
2578 /* redefining a previously known filename extension */
2581 /* query ("Really make files of type %s '%s'?", */
2582 /* ext_args, language_str (lang)); */
2584 xfree (filename_language_table
[i
].ext
);
2585 filename_language_table
[i
].ext
= xstrdup (ext_args
);
2586 filename_language_table
[i
].lang
= lang
;
2591 info_ext_lang_command (char *args
, int from_tty
)
2595 printf_filtered (_("Filename extensions and the languages they represent:"));
2596 printf_filtered ("\n\n");
2597 for (i
= 0; i
< fl_table_next
; i
++)
2598 printf_filtered ("\t%s\t- %s\n",
2599 filename_language_table
[i
].ext
,
2600 language_str (filename_language_table
[i
].lang
));
2604 init_filename_language_table (void)
2606 if (fl_table_size
== 0) /* protect against repetition */
2610 filename_language_table
=
2611 xmalloc (fl_table_size
* sizeof (*filename_language_table
));
2612 add_filename_language (".c", language_c
);
2613 add_filename_language (".d", language_d
);
2614 add_filename_language (".C", language_cplus
);
2615 add_filename_language (".cc", language_cplus
);
2616 add_filename_language (".cp", language_cplus
);
2617 add_filename_language (".cpp", language_cplus
);
2618 add_filename_language (".cxx", language_cplus
);
2619 add_filename_language (".c++", language_cplus
);
2620 add_filename_language (".java", language_java
);
2621 add_filename_language (".class", language_java
);
2622 add_filename_language (".m", language_objc
);
2623 add_filename_language (".f", language_fortran
);
2624 add_filename_language (".F", language_fortran
);
2625 add_filename_language (".s", language_asm
);
2626 add_filename_language (".sx", language_asm
);
2627 add_filename_language (".S", language_asm
);
2628 add_filename_language (".pas", language_pascal
);
2629 add_filename_language (".p", language_pascal
);
2630 add_filename_language (".pp", language_pascal
);
2631 add_filename_language (".adb", language_ada
);
2632 add_filename_language (".ads", language_ada
);
2633 add_filename_language (".a", language_ada
);
2634 add_filename_language (".ada", language_ada
);
2635 add_filename_language (".dg", language_ada
);
2640 deduce_language_from_filename (char *filename
)
2645 if (filename
!= NULL
)
2646 if ((cp
= strrchr (filename
, '.')) != NULL
)
2647 for (i
= 0; i
< fl_table_next
; i
++)
2648 if (strcmp (cp
, filename_language_table
[i
].ext
) == 0)
2649 return filename_language_table
[i
].lang
;
2651 return language_unknown
;
2656 Allocate and partly initialize a new symbol table. Return a pointer
2657 to it. error() if no space.
2659 Caller must set these fields:
2668 allocate_symtab (char *filename
, struct objfile
*objfile
)
2670 struct symtab
*symtab
;
2672 symtab
= (struct symtab
*)
2673 obstack_alloc (&objfile
->objfile_obstack
, sizeof (struct symtab
));
2674 memset (symtab
, 0, sizeof (*symtab
));
2675 symtab
->filename
= (char *) bcache (filename
, strlen (filename
) + 1,
2676 objfile
->filename_cache
);
2677 symtab
->fullname
= NULL
;
2678 symtab
->language
= deduce_language_from_filename (filename
);
2679 symtab
->debugformat
= "unknown";
2681 /* Hook it to the objfile it comes from */
2683 symtab
->objfile
= objfile
;
2684 symtab
->next
= objfile
->symtabs
;
2685 objfile
->symtabs
= symtab
;
2691 /* Reset all data structures in gdb which may contain references to symbol
2695 clear_symtab_users (void)
2697 /* Someday, we should do better than this, by only blowing away
2698 the things that really need to be blown. */
2700 /* Clear the "current" symtab first, because it is no longer valid.
2701 breakpoint_re_set may try to access the current symtab. */
2702 clear_current_source_symtab_and_line ();
2705 breakpoint_re_set ();
2706 set_default_breakpoint (0, NULL
, 0, 0, 0);
2707 clear_pc_function_cache ();
2708 observer_notify_new_objfile (NULL
);
2710 /* Clear globals which might have pointed into a removed objfile.
2711 FIXME: It's not clear which of these are supposed to persist
2712 between expressions and which ought to be reset each time. */
2713 expression_context_block
= NULL
;
2714 innermost_block
= NULL
;
2716 /* Varobj may refer to old symbols, perform a cleanup. */
2717 varobj_invalidate ();
2722 clear_symtab_users_cleanup (void *ignore
)
2724 clear_symtab_users ();
2728 The following code implements an abstraction for debugging overlay sections.
2730 The target model is as follows:
2731 1) The gnu linker will permit multiple sections to be mapped into the
2732 same VMA, each with its own unique LMA (or load address).
2733 2) It is assumed that some runtime mechanism exists for mapping the
2734 sections, one by one, from the load address into the VMA address.
2735 3) This code provides a mechanism for gdb to keep track of which
2736 sections should be considered to be mapped from the VMA to the LMA.
2737 This information is used for symbol lookup, and memory read/write.
2738 For instance, if a section has been mapped then its contents
2739 should be read from the VMA, otherwise from the LMA.
2741 Two levels of debugger support for overlays are available. One is
2742 "manual", in which the debugger relies on the user to tell it which
2743 overlays are currently mapped. This level of support is
2744 implemented entirely in the core debugger, and the information about
2745 whether a section is mapped is kept in the objfile->obj_section table.
2747 The second level of support is "automatic", and is only available if
2748 the target-specific code provides functionality to read the target's
2749 overlay mapping table, and translate its contents for the debugger
2750 (by updating the mapped state information in the obj_section tables).
2752 The interface is as follows:
2754 overlay map <name> -- tell gdb to consider this section mapped
2755 overlay unmap <name> -- tell gdb to consider this section unmapped
2756 overlay list -- list the sections that GDB thinks are mapped
2757 overlay read-target -- get the target's state of what's mapped
2758 overlay off/manual/auto -- set overlay debugging state
2759 Functional interface:
2760 find_pc_mapped_section(pc): if the pc is in the range of a mapped
2761 section, return that section.
2762 find_pc_overlay(pc): find any overlay section that contains
2763 the pc, either in its VMA or its LMA
2764 section_is_mapped(sect): true if overlay is marked as mapped
2765 section_is_overlay(sect): true if section's VMA != LMA
2766 pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA
2767 pc_in_unmapped_range(...): true if pc belongs to section's LMA
2768 sections_overlap(sec1, sec2): true if mapped sec1 and sec2 ranges overlap
2769 overlay_mapped_address(...): map an address from section's LMA to VMA
2770 overlay_unmapped_address(...): map an address from section's VMA to LMA
2771 symbol_overlayed_address(...): Return a "current" address for symbol:
2772 either in VMA or LMA depending on whether
2773 the symbol's section is currently mapped
2776 /* Overlay debugging state: */
2778 enum overlay_debugging_state overlay_debugging
= ovly_off
;
2779 int overlay_cache_invalid
= 0; /* True if need to refresh mapped state */
2781 /* Function: section_is_overlay (SECTION)
2782 Returns true if SECTION has VMA not equal to LMA, ie.
2783 SECTION is loaded at an address different from where it will "run". */
2786 section_is_overlay (struct obj_section
*section
)
2788 if (overlay_debugging
&& section
)
2790 bfd
*abfd
= section
->objfile
->obfd
;
2791 asection
*bfd_section
= section
->the_bfd_section
;
2793 if (bfd_section_lma (abfd
, bfd_section
) != 0
2794 && bfd_section_lma (abfd
, bfd_section
)
2795 != bfd_section_vma (abfd
, bfd_section
))
2802 /* Function: overlay_invalidate_all (void)
2803 Invalidate the mapped state of all overlay sections (mark it as stale). */
2806 overlay_invalidate_all (void)
2808 struct objfile
*objfile
;
2809 struct obj_section
*sect
;
2811 ALL_OBJSECTIONS (objfile
, sect
)
2812 if (section_is_overlay (sect
))
2813 sect
->ovly_mapped
= -1;
2816 /* Function: section_is_mapped (SECTION)
2817 Returns true if section is an overlay, and is currently mapped.
2819 Access to the ovly_mapped flag is restricted to this function, so
2820 that we can do automatic update. If the global flag
2821 OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
2822 overlay_invalidate_all. If the mapped state of the particular
2823 section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */
2826 section_is_mapped (struct obj_section
*osect
)
2828 struct gdbarch
*gdbarch
;
2830 if (osect
== 0 || !section_is_overlay (osect
))
2833 switch (overlay_debugging
)
2837 return 0; /* overlay debugging off */
2838 case ovly_auto
: /* overlay debugging automatic */
2839 /* Unles there is a gdbarch_overlay_update function,
2840 there's really nothing useful to do here (can't really go auto) */
2841 gdbarch
= get_objfile_arch (osect
->objfile
);
2842 if (gdbarch_overlay_update_p (gdbarch
))
2844 if (overlay_cache_invalid
)
2846 overlay_invalidate_all ();
2847 overlay_cache_invalid
= 0;
2849 if (osect
->ovly_mapped
== -1)
2850 gdbarch_overlay_update (gdbarch
, osect
);
2852 /* fall thru to manual case */
2853 case ovly_on
: /* overlay debugging manual */
2854 return osect
->ovly_mapped
== 1;
2858 /* Function: pc_in_unmapped_range
2859 If PC falls into the lma range of SECTION, return true, else false. */
2862 pc_in_unmapped_range (CORE_ADDR pc
, struct obj_section
*section
)
2864 if (section_is_overlay (section
))
2866 bfd
*abfd
= section
->objfile
->obfd
;
2867 asection
*bfd_section
= section
->the_bfd_section
;
2869 /* We assume the LMA is relocated by the same offset as the VMA. */
2870 bfd_vma size
= bfd_get_section_size (bfd_section
);
2871 CORE_ADDR offset
= obj_section_offset (section
);
2873 if (bfd_get_section_lma (abfd
, bfd_section
) + offset
<= pc
2874 && pc
< bfd_get_section_lma (abfd
, bfd_section
) + offset
+ size
)
2881 /* Function: pc_in_mapped_range
2882 If PC falls into the vma range of SECTION, return true, else false. */
2885 pc_in_mapped_range (CORE_ADDR pc
, struct obj_section
*section
)
2887 if (section_is_overlay (section
))
2889 if (obj_section_addr (section
) <= pc
2890 && pc
< obj_section_endaddr (section
))
2898 /* Return true if the mapped ranges of sections A and B overlap, false
2901 sections_overlap (struct obj_section
*a
, struct obj_section
*b
)
2903 CORE_ADDR a_start
= obj_section_addr (a
);
2904 CORE_ADDR a_end
= obj_section_endaddr (a
);
2905 CORE_ADDR b_start
= obj_section_addr (b
);
2906 CORE_ADDR b_end
= obj_section_endaddr (b
);
2908 return (a_start
< b_end
&& b_start
< a_end
);
2911 /* Function: overlay_unmapped_address (PC, SECTION)
2912 Returns the address corresponding to PC in the unmapped (load) range.
2913 May be the same as PC. */
2916 overlay_unmapped_address (CORE_ADDR pc
, struct obj_section
*section
)
2918 if (section_is_overlay (section
) && pc_in_mapped_range (pc
, section
))
2920 bfd
*abfd
= section
->objfile
->obfd
;
2921 asection
*bfd_section
= section
->the_bfd_section
;
2923 return pc
+ bfd_section_lma (abfd
, bfd_section
)
2924 - bfd_section_vma (abfd
, bfd_section
);
2930 /* Function: overlay_mapped_address (PC, SECTION)
2931 Returns the address corresponding to PC in the mapped (runtime) range.
2932 May be the same as PC. */
2935 overlay_mapped_address (CORE_ADDR pc
, struct obj_section
*section
)
2937 if (section_is_overlay (section
) && pc_in_unmapped_range (pc
, section
))
2939 bfd
*abfd
= section
->objfile
->obfd
;
2940 asection
*bfd_section
= section
->the_bfd_section
;
2942 return pc
+ bfd_section_vma (abfd
, bfd_section
)
2943 - bfd_section_lma (abfd
, bfd_section
);
2950 /* Function: symbol_overlayed_address
2951 Return one of two addresses (relative to the VMA or to the LMA),
2952 depending on whether the section is mapped or not. */
2955 symbol_overlayed_address (CORE_ADDR address
, struct obj_section
*section
)
2957 if (overlay_debugging
)
2959 /* If the symbol has no section, just return its regular address. */
2962 /* If the symbol's section is not an overlay, just return its address */
2963 if (!section_is_overlay (section
))
2965 /* If the symbol's section is mapped, just return its address */
2966 if (section_is_mapped (section
))
2969 * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
2970 * then return its LOADED address rather than its vma address!!
2972 return overlay_unmapped_address (address
, section
);
2977 /* Function: find_pc_overlay (PC)
2978 Return the best-match overlay section for PC:
2979 If PC matches a mapped overlay section's VMA, return that section.
2980 Else if PC matches an unmapped section's VMA, return that section.
2981 Else if PC matches an unmapped section's LMA, return that section. */
2983 struct obj_section
*
2984 find_pc_overlay (CORE_ADDR pc
)
2986 struct objfile
*objfile
;
2987 struct obj_section
*osect
, *best_match
= NULL
;
2989 if (overlay_debugging
)
2990 ALL_OBJSECTIONS (objfile
, osect
)
2991 if (section_is_overlay (osect
))
2993 if (pc_in_mapped_range (pc
, osect
))
2995 if (section_is_mapped (osect
))
3000 else if (pc_in_unmapped_range (pc
, osect
))
3006 /* Function: find_pc_mapped_section (PC)
3007 If PC falls into the VMA address range of an overlay section that is
3008 currently marked as MAPPED, return that section. Else return NULL. */
3010 struct obj_section
*
3011 find_pc_mapped_section (CORE_ADDR pc
)
3013 struct objfile
*objfile
;
3014 struct obj_section
*osect
;
3016 if (overlay_debugging
)
3017 ALL_OBJSECTIONS (objfile
, osect
)
3018 if (pc_in_mapped_range (pc
, osect
) && section_is_mapped (osect
))
3024 /* Function: list_overlays_command
3025 Print a list of mapped sections and their PC ranges */
3028 list_overlays_command (char *args
, int from_tty
)
3031 struct objfile
*objfile
;
3032 struct obj_section
*osect
;
3034 if (overlay_debugging
)
3035 ALL_OBJSECTIONS (objfile
, osect
)
3036 if (section_is_mapped (osect
))
3038 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3043 vma
= bfd_section_vma (objfile
->obfd
, osect
->the_bfd_section
);
3044 lma
= bfd_section_lma (objfile
->obfd
, osect
->the_bfd_section
);
3045 size
= bfd_get_section_size (osect
->the_bfd_section
);
3046 name
= bfd_section_name (objfile
->obfd
, osect
->the_bfd_section
);
3048 printf_filtered ("Section %s, loaded at ", name
);
3049 fputs_filtered (paddress (gdbarch
, lma
), gdb_stdout
);
3050 puts_filtered (" - ");
3051 fputs_filtered (paddress (gdbarch
, lma
+ size
), gdb_stdout
);
3052 printf_filtered (", mapped at ");
3053 fputs_filtered (paddress (gdbarch
, vma
), gdb_stdout
);
3054 puts_filtered (" - ");
3055 fputs_filtered (paddress (gdbarch
, vma
+ size
), gdb_stdout
);
3056 puts_filtered ("\n");
3061 printf_filtered (_("No sections are mapped.\n"));
3064 /* Function: map_overlay_command
3065 Mark the named section as mapped (ie. residing at its VMA address). */
3068 map_overlay_command (char *args
, int from_tty
)
3070 struct objfile
*objfile
, *objfile2
;
3071 struct obj_section
*sec
, *sec2
;
3073 if (!overlay_debugging
)
3075 Overlay debugging not enabled. Use either the 'overlay auto' or\n\
3076 the 'overlay manual' command."));
3078 if (args
== 0 || *args
== 0)
3079 error (_("Argument required: name of an overlay section"));
3081 /* First, find a section matching the user supplied argument */
3082 ALL_OBJSECTIONS (objfile
, sec
)
3083 if (!strcmp (bfd_section_name (objfile
->obfd
, sec
->the_bfd_section
), args
))
3085 /* Now, check to see if the section is an overlay. */
3086 if (!section_is_overlay (sec
))
3087 continue; /* not an overlay section */
3089 /* Mark the overlay as "mapped" */
3090 sec
->ovly_mapped
= 1;
3092 /* Next, make a pass and unmap any sections that are
3093 overlapped by this new section: */
3094 ALL_OBJSECTIONS (objfile2
, sec2
)
3095 if (sec2
->ovly_mapped
&& sec
!= sec2
&& sections_overlap (sec
, sec2
))
3098 printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
3099 bfd_section_name (objfile
->obfd
,
3100 sec2
->the_bfd_section
));
3101 sec2
->ovly_mapped
= 0; /* sec2 overlaps sec: unmap sec2 */
3105 error (_("No overlay section called %s"), args
);
3108 /* Function: unmap_overlay_command
3109 Mark the overlay section as unmapped
3110 (ie. resident in its LMA address range, rather than the VMA range). */
3113 unmap_overlay_command (char *args
, int from_tty
)
3115 struct objfile
*objfile
;
3116 struct obj_section
*sec
;
3118 if (!overlay_debugging
)
3120 Overlay debugging not enabled. Use either the 'overlay auto' or\n\
3121 the 'overlay manual' command."));
3123 if (args
== 0 || *args
== 0)
3124 error (_("Argument required: name of an overlay section"));
3126 /* First, find a section matching the user supplied argument */
3127 ALL_OBJSECTIONS (objfile
, sec
)
3128 if (!strcmp (bfd_section_name (objfile
->obfd
, sec
->the_bfd_section
), args
))
3130 if (!sec
->ovly_mapped
)
3131 error (_("Section %s is not mapped"), args
);
3132 sec
->ovly_mapped
= 0;
3135 error (_("No overlay section called %s"), args
);
3138 /* Function: overlay_auto_command
3139 A utility command to turn on overlay debugging.
3140 Possibly this should be done via a set/show command. */
3143 overlay_auto_command (char *args
, int from_tty
)
3145 overlay_debugging
= ovly_auto
;
3146 enable_overlay_breakpoints ();
3148 printf_unfiltered (_("Automatic overlay debugging enabled."));
3151 /* Function: overlay_manual_command
3152 A utility command to turn on overlay debugging.
3153 Possibly this should be done via a set/show command. */
3156 overlay_manual_command (char *args
, int from_tty
)
3158 overlay_debugging
= ovly_on
;
3159 disable_overlay_breakpoints ();
3161 printf_unfiltered (_("Overlay debugging enabled."));
3164 /* Function: overlay_off_command
3165 A utility command to turn on overlay debugging.
3166 Possibly this should be done via a set/show command. */
3169 overlay_off_command (char *args
, int from_tty
)
3171 overlay_debugging
= ovly_off
;
3172 disable_overlay_breakpoints ();
3174 printf_unfiltered (_("Overlay debugging disabled."));
3178 overlay_load_command (char *args
, int from_tty
)
3180 struct gdbarch
*gdbarch
= get_current_arch ();
3182 if (gdbarch_overlay_update_p (gdbarch
))
3183 gdbarch_overlay_update (gdbarch
, NULL
);
3185 error (_("This target does not know how to read its overlay state."));
3188 /* Function: overlay_command
3189 A place-holder for a mis-typed command */
3191 /* Command list chain containing all defined "overlay" subcommands. */
3192 struct cmd_list_element
*overlaylist
;
3195 overlay_command (char *args
, int from_tty
)
3198 ("\"overlay\" must be followed by the name of an overlay command.\n");
3199 help_list (overlaylist
, "overlay ", -1, gdb_stdout
);
3203 /* Target Overlays for the "Simplest" overlay manager:
3205 This is GDB's default target overlay layer. It works with the
3206 minimal overlay manager supplied as an example by Cygnus. The
3207 entry point is via a function pointer "gdbarch_overlay_update",
3208 so targets that use a different runtime overlay manager can
3209 substitute their own overlay_update function and take over the
3212 The overlay_update function pokes around in the target's data structures
3213 to see what overlays are mapped, and updates GDB's overlay mapping with
3216 In this simple implementation, the target data structures are as follows:
3217 unsigned _novlys; /# number of overlay sections #/
3218 unsigned _ovly_table[_novlys][4] = {
3219 {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/
3220 {..., ..., ..., ...},
3222 unsigned _novly_regions; /# number of overlay regions #/
3223 unsigned _ovly_region_table[_novly_regions][3] = {
3224 {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/
3227 These functions will attempt to update GDB's mappedness state in the
3228 symbol section table, based on the target's mappedness state.
3230 To do this, we keep a cached copy of the target's _ovly_table, and
3231 attempt to detect when the cached copy is invalidated. The main
3232 entry point is "simple_overlay_update(SECT), which looks up SECT in
3233 the cached table and re-reads only the entry for that section from
3234 the target (whenever possible).
3237 /* Cached, dynamically allocated copies of the target data structures: */
3238 static unsigned (*cache_ovly_table
)[4] = 0;
3240 static unsigned (*cache_ovly_region_table
)[3] = 0;
3242 static unsigned cache_novlys
= 0;
3244 static unsigned cache_novly_regions
= 0;
3246 static CORE_ADDR cache_ovly_table_base
= 0;
3248 static CORE_ADDR cache_ovly_region_table_base
= 0;
3252 VMA
, SIZE
, LMA
, MAPPED
3255 /* Throw away the cached copy of _ovly_table */
3257 simple_free_overlay_table (void)
3259 if (cache_ovly_table
)
3260 xfree (cache_ovly_table
);
3262 cache_ovly_table
= NULL
;
3263 cache_ovly_table_base
= 0;
3267 /* Throw away the cached copy of _ovly_region_table */
3269 simple_free_overlay_region_table (void)
3271 if (cache_ovly_region_table
)
3272 xfree (cache_ovly_region_table
);
3273 cache_novly_regions
= 0;
3274 cache_ovly_region_table
= NULL
;
3275 cache_ovly_region_table_base
= 0;
3279 /* Read an array of ints of size SIZE from the target into a local buffer.
3280 Convert to host order. int LEN is number of ints */
3282 read_target_long_array (CORE_ADDR memaddr
, unsigned int *myaddr
,
3283 int len
, int size
, enum bfd_endian byte_order
)
3285 /* FIXME (alloca): Not safe if array is very large. */
3286 gdb_byte
*buf
= alloca (len
* size
);
3289 read_memory (memaddr
, buf
, len
* size
);
3290 for (i
= 0; i
< len
; i
++)
3291 myaddr
[i
] = extract_unsigned_integer (size
* i
+ buf
, size
, byte_order
);
3294 /* Find and grab a copy of the target _ovly_table
3295 (and _novlys, which is needed for the table's size) */
3297 simple_read_overlay_table (void)
3299 struct minimal_symbol
*novlys_msym
, *ovly_table_msym
;
3300 struct gdbarch
*gdbarch
;
3302 enum bfd_endian byte_order
;
3304 simple_free_overlay_table ();
3305 novlys_msym
= lookup_minimal_symbol ("_novlys", NULL
, NULL
);
3308 error (_("Error reading inferior's overlay table: "
3309 "couldn't find `_novlys' variable\n"
3310 "in inferior. Use `overlay manual' mode."));
3314 ovly_table_msym
= lookup_minimal_symbol ("_ovly_table", NULL
, NULL
);
3315 if (! ovly_table_msym
)
3317 error (_("Error reading inferior's overlay table: couldn't find "
3318 "`_ovly_table' array\n"
3319 "in inferior. Use `overlay manual' mode."));
3323 gdbarch
= get_objfile_arch (msymbol_objfile (ovly_table_msym
));
3324 word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3325 byte_order
= gdbarch_byte_order (gdbarch
);
3327 cache_novlys
= read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym
),
3330 = (void *) xmalloc (cache_novlys
* sizeof (*cache_ovly_table
));
3331 cache_ovly_table_base
= SYMBOL_VALUE_ADDRESS (ovly_table_msym
);
3332 read_target_long_array (cache_ovly_table_base
,
3333 (unsigned int *) cache_ovly_table
,
3334 cache_novlys
* 4, word_size
, byte_order
);
3336 return 1; /* SUCCESS */
3340 /* Find and grab a copy of the target _ovly_region_table
3341 (and _novly_regions, which is needed for the table's size) */
3343 simple_read_overlay_region_table (void)
3345 struct minimal_symbol
*msym
;
3346 struct gdbarch
*gdbarch
;
3348 enum bfd_endian byte_order
;
3350 simple_free_overlay_region_table ();
3351 msym
= lookup_minimal_symbol ("_novly_regions", NULL
, NULL
);
3353 return 0; /* failure */
3355 gdbarch
= get_objfile_arch (msymbol_objfile (msym
));
3356 word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3357 byte_order
= gdbarch_byte_order (gdbarch
);
3359 cache_novly_regions
= read_memory_integer (SYMBOL_VALUE_ADDRESS (msym
),
3362 cache_ovly_region_table
= (void *) xmalloc (cache_novly_regions
* 12);
3363 if (cache_ovly_region_table
!= NULL
)
3365 msym
= lookup_minimal_symbol ("_ovly_region_table", NULL
, NULL
);
3368 cache_ovly_region_table_base
= SYMBOL_VALUE_ADDRESS (msym
);
3369 read_target_long_array (cache_ovly_region_table_base
,
3370 (unsigned int *) cache_ovly_region_table
,
3371 cache_novly_regions
* 3,
3372 word_size
, byte_order
);
3375 return 0; /* failure */
3378 return 0; /* failure */
3379 return 1; /* SUCCESS */
3383 /* Function: simple_overlay_update_1
3384 A helper function for simple_overlay_update. Assuming a cached copy
3385 of _ovly_table exists, look through it to find an entry whose vma,
3386 lma and size match those of OSECT. Re-read the entry and make sure
3387 it still matches OSECT (else the table may no longer be valid).
3388 Set OSECT's mapped state to match the entry. Return: 1 for
3389 success, 0 for failure. */
3392 simple_overlay_update_1 (struct obj_section
*osect
)
3395 bfd
*obfd
= osect
->objfile
->obfd
;
3396 asection
*bsect
= osect
->the_bfd_section
;
3397 struct gdbarch
*gdbarch
= get_objfile_arch (osect
->objfile
);
3398 int word_size
= gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
3399 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3401 size
= bfd_get_section_size (osect
->the_bfd_section
);
3402 for (i
= 0; i
< cache_novlys
; i
++)
3403 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3404 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3405 /* && cache_ovly_table[i][SIZE] == size */ )
3407 read_target_long_array (cache_ovly_table_base
+ i
* word_size
,
3408 (unsigned int *) cache_ovly_table
[i
],
3409 4, word_size
, byte_order
);
3410 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3411 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3412 /* && cache_ovly_table[i][SIZE] == size */ )
3414 osect
->ovly_mapped
= cache_ovly_table
[i
][MAPPED
];
3417 else /* Warning! Warning! Target's ovly table has changed! */
3423 /* Function: simple_overlay_update
3424 If OSECT is NULL, then update all sections' mapped state
3425 (after re-reading the entire target _ovly_table).
3426 If OSECT is non-NULL, then try to find a matching entry in the
3427 cached ovly_table and update only OSECT's mapped state.
3428 If a cached entry can't be found or the cache isn't valid, then
3429 re-read the entire cache, and go ahead and update all sections. */
3432 simple_overlay_update (struct obj_section
*osect
)
3434 struct objfile
*objfile
;
3436 /* Were we given an osect to look up? NULL means do all of them. */
3438 /* Have we got a cached copy of the target's overlay table? */
3439 if (cache_ovly_table
!= NULL
)
3440 /* Does its cached location match what's currently in the symtab? */
3441 if (cache_ovly_table_base
==
3442 SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", NULL
, NULL
)))
3443 /* Then go ahead and try to look up this single section in the cache */
3444 if (simple_overlay_update_1 (osect
))
3445 /* Found it! We're done. */
3448 /* Cached table no good: need to read the entire table anew.
3449 Or else we want all the sections, in which case it's actually
3450 more efficient to read the whole table in one block anyway. */
3452 if (! simple_read_overlay_table ())
3455 /* Now may as well update all sections, even if only one was requested. */
3456 ALL_OBJSECTIONS (objfile
, osect
)
3457 if (section_is_overlay (osect
))
3460 bfd
*obfd
= osect
->objfile
->obfd
;
3461 asection
*bsect
= osect
->the_bfd_section
;
3463 size
= bfd_get_section_size (bsect
);
3464 for (i
= 0; i
< cache_novlys
; i
++)
3465 if (cache_ovly_table
[i
][VMA
] == bfd_section_vma (obfd
, bsect
)
3466 && cache_ovly_table
[i
][LMA
] == bfd_section_lma (obfd
, bsect
)
3467 /* && cache_ovly_table[i][SIZE] == size */ )
3468 { /* obj_section matches i'th entry in ovly_table */
3469 osect
->ovly_mapped
= cache_ovly_table
[i
][MAPPED
];
3470 break; /* finished with inner for loop: break out */
3475 /* Set the output sections and output offsets for section SECTP in
3476 ABFD. The relocation code in BFD will read these offsets, so we
3477 need to be sure they're initialized. We map each section to itself,
3478 with no offset; this means that SECTP->vma will be honored. */
3481 symfile_dummy_outputs (bfd
*abfd
, asection
*sectp
, void *dummy
)
3483 sectp
->output_section
= sectp
;
3484 sectp
->output_offset
= 0;
3487 /* Default implementation for sym_relocate. */
3491 default_symfile_relocate (struct objfile
*objfile
, asection
*sectp
,
3494 bfd
*abfd
= objfile
->obfd
;
3496 /* We're only interested in sections with relocation
3498 if ((sectp
->flags
& SEC_RELOC
) == 0)
3501 /* We will handle section offsets properly elsewhere, so relocate as if
3502 all sections begin at 0. */
3503 bfd_map_over_sections (abfd
, symfile_dummy_outputs
, NULL
);
3505 return bfd_simple_get_relocated_section_contents (abfd
, sectp
, buf
, NULL
);
3508 /* Relocate the contents of a debug section SECTP in ABFD. The
3509 contents are stored in BUF if it is non-NULL, or returned in a
3510 malloc'd buffer otherwise.
3512 For some platforms and debug info formats, shared libraries contain
3513 relocations against the debug sections (particularly for DWARF-2;
3514 one affected platform is PowerPC GNU/Linux, although it depends on
3515 the version of the linker in use). Also, ELF object files naturally
3516 have unresolved relocations for their debug sections. We need to apply
3517 the relocations in order to get the locations of symbols correct.
3518 Another example that may require relocation processing, is the
3519 DWARF-2 .eh_frame section in .o files, although it isn't strictly a
3523 symfile_relocate_debug_section (struct objfile
*objfile
,
3524 asection
*sectp
, bfd_byte
*buf
)
3526 gdb_assert (objfile
->sf
->sym_relocate
);
3528 return (*objfile
->sf
->sym_relocate
) (objfile
, sectp
, buf
);
3531 struct symfile_segment_data
*
3532 get_symfile_segment_data (bfd
*abfd
)
3534 struct sym_fns
*sf
= find_sym_fns (abfd
);
3539 return sf
->sym_segments (abfd
);
3543 free_symfile_segment_data (struct symfile_segment_data
*data
)
3545 xfree (data
->segment_bases
);
3546 xfree (data
->segment_sizes
);
3547 xfree (data
->segment_info
);
3553 - DATA, containing segment addresses from the object file ABFD, and
3554 the mapping from ABFD's sections onto the segments that own them,
3556 - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
3557 segment addresses reported by the target,
3558 store the appropriate offsets for each section in OFFSETS.
3560 If there are fewer entries in SEGMENT_BASES than there are segments
3561 in DATA, then apply SEGMENT_BASES' last entry to all the segments.
3563 If there are more entries, then ignore the extra. The target may
3564 not be able to distinguish between an empty data segment and a
3565 missing data segment; a missing text segment is less plausible. */
3567 symfile_map_offsets_to_segments (bfd
*abfd
, struct symfile_segment_data
*data
,
3568 struct section_offsets
*offsets
,
3569 int num_segment_bases
,
3570 const CORE_ADDR
*segment_bases
)
3575 /* It doesn't make sense to call this function unless you have some
3576 segment base addresses. */
3577 gdb_assert (num_segment_bases
> 0);
3579 /* If we do not have segment mappings for the object file, we
3580 can not relocate it by segments. */
3581 gdb_assert (data
!= NULL
);
3582 gdb_assert (data
->num_segments
> 0);
3584 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
3586 int which
= data
->segment_info
[i
];
3588 gdb_assert (0 <= which
&& which
<= data
->num_segments
);
3590 /* Don't bother computing offsets for sections that aren't
3591 loaded as part of any segment. */
3595 /* Use the last SEGMENT_BASES entry as the address of any extra
3596 segments mentioned in DATA->segment_info. */
3597 if (which
> num_segment_bases
)
3598 which
= num_segment_bases
;
3600 offsets
->offsets
[i
] = (segment_bases
[which
- 1]
3601 - data
->segment_bases
[which
- 1]);
3608 symfile_find_segment_sections (struct objfile
*objfile
)
3610 bfd
*abfd
= objfile
->obfd
;
3613 struct symfile_segment_data
*data
;
3615 data
= get_symfile_segment_data (objfile
->obfd
);
3619 if (data
->num_segments
!= 1 && data
->num_segments
!= 2)
3621 free_symfile_segment_data (data
);
3625 for (i
= 0, sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
3627 int which
= data
->segment_info
[i
];
3631 if (objfile
->sect_index_text
== -1)
3632 objfile
->sect_index_text
= sect
->index
;
3634 if (objfile
->sect_index_rodata
== -1)
3635 objfile
->sect_index_rodata
= sect
->index
;
3637 else if (which
== 2)
3639 if (objfile
->sect_index_data
== -1)
3640 objfile
->sect_index_data
= sect
->index
;
3642 if (objfile
->sect_index_bss
== -1)
3643 objfile
->sect_index_bss
= sect
->index
;
3647 free_symfile_segment_data (data
);
3651 _initialize_symfile (void)
3653 struct cmd_list_element
*c
;
3655 c
= add_cmd ("symbol-file", class_files
, symbol_file_command
, _("\
3656 Load symbol table from executable file FILE.\n\
3657 The `file' command can also load symbol tables, as well as setting the file\n\
3658 to execute."), &cmdlist
);
3659 set_cmd_completer (c
, filename_completer
);
3661 c
= add_cmd ("add-symbol-file", class_files
, add_symbol_file_command
, _("\
3662 Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
3663 Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]\n\
3664 ADDR is the starting address of the file's text.\n\
3665 The optional arguments are section-name section-address pairs and\n\
3666 should be specified if the data and bss segments are not contiguous\n\
3667 with the text. SECT is a section name to be loaded at SECT_ADDR."),
3669 set_cmd_completer (c
, filename_completer
);
3671 c
= add_cmd ("load", class_files
, load_command
, _("\
3672 Dynamically load FILE into the running program, and record its symbols\n\
3673 for access from GDB.\n\
3674 A load OFFSET may also be given."), &cmdlist
);
3675 set_cmd_completer (c
, filename_completer
);
3677 add_setshow_boolean_cmd ("symbol-reloading", class_support
,
3678 &symbol_reloading
, _("\
3679 Set dynamic symbol table reloading multiple times in one run."), _("\
3680 Show dynamic symbol table reloading multiple times in one run."), NULL
,
3682 show_symbol_reloading
,
3683 &setlist
, &showlist
);
3685 add_prefix_cmd ("overlay", class_support
, overlay_command
,
3686 _("Commands for debugging overlays."), &overlaylist
,
3687 "overlay ", 0, &cmdlist
);
3689 add_com_alias ("ovly", "overlay", class_alias
, 1);
3690 add_com_alias ("ov", "overlay", class_alias
, 1);
3692 add_cmd ("map-overlay", class_support
, map_overlay_command
,
3693 _("Assert that an overlay section is mapped."), &overlaylist
);
3695 add_cmd ("unmap-overlay", class_support
, unmap_overlay_command
,
3696 _("Assert that an overlay section is unmapped."), &overlaylist
);
3698 add_cmd ("list-overlays", class_support
, list_overlays_command
,
3699 _("List mappings of overlay sections."), &overlaylist
);
3701 add_cmd ("manual", class_support
, overlay_manual_command
,
3702 _("Enable overlay debugging."), &overlaylist
);
3703 add_cmd ("off", class_support
, overlay_off_command
,
3704 _("Disable overlay debugging."), &overlaylist
);
3705 add_cmd ("auto", class_support
, overlay_auto_command
,
3706 _("Enable automatic overlay debugging."), &overlaylist
);
3707 add_cmd ("load-target", class_support
, overlay_load_command
,
3708 _("Read the overlay mapping state from the target."), &overlaylist
);
3710 /* Filename extension to source language lookup table: */
3711 init_filename_language_table ();
3712 add_setshow_string_noescape_cmd ("extension-language", class_files
,
3714 Set mapping between filename extension and source language."), _("\
3715 Show mapping between filename extension and source language."), _("\
3716 Usage: set extension-language .foo bar"),
3717 set_ext_lang_command
,
3719 &setlist
, &showlist
);
3721 add_info ("extensions", info_ext_lang_command
,
3722 _("All filename extensions associated with a source language."));
3724 add_setshow_optional_filename_cmd ("debug-file-directory", class_support
,
3725 &debug_file_directory
, _("\
3726 Set the directories where separate debug symbols are searched for."), _("\
3727 Show the directories where separate debug symbols are searched for."), _("\
3728 Separate debug symbols are first searched for in the same\n\
3729 directory as the binary, then in the `" DEBUG_SUBDIRECTORY
"' subdirectory,\n\
3730 and lastly at the path of the directory of the binary with\n\
3731 each global debug-file-directory component prepended."),
3733 show_debug_file_directory
,
3734 &setlist
, &showlist
);