1 /* GDB routines for manipulating objfiles.
3 Copyright (C) 1992-2023 Free Software Foundation, Inc.
5 Contributed by Cygnus Support, using pieces from other GDB modules.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This file contains support routines for creating, manipulating, and
23 destroying objfile structures. */
26 #include "bfd.h" /* Binary File Description */
30 #include "gdb-stabs.h"
33 #include "expression.h"
34 #include "parser-defs.h"
36 #include <sys/types.h>
39 #include "gdbsupport/gdb_obstack.h"
42 #include "breakpoint.h"
44 #include "dictionary.h"
47 #include "arch-utils.h"
49 #include "observable.h"
50 #include "complaints.h"
55 #include "gdbsupport/pathstuff.h"
60 /* Externally visible variables that are owned by this module.
61 See declarations in objfile.h for more info. */
63 struct objfile_pspace_info
65 objfile_pspace_info () = default;
66 ~objfile_pspace_info ();
68 struct obj_section
**sections
= nullptr;
71 /* Nonzero if object files have been added since the section map
73 int new_objfiles_available
= 0;
75 /* Nonzero if the section map MUST be updated before use. */
76 int section_map_dirty
= 0;
78 /* Nonzero if section map updates should be inhibited if possible. */
79 int inhibit_updates
= 0;
82 /* Per-program-space data key. */
83 static const registry
<program_space
>::key
<objfile_pspace_info
>
86 objfile_pspace_info::~objfile_pspace_info ()
91 /* Get the current svr4 data. If none is found yet, add it now. This
92 function always returns a valid object. */
94 static struct objfile_pspace_info
*
95 get_objfile_pspace_data (struct program_space
*pspace
)
97 struct objfile_pspace_info
*info
;
99 info
= objfiles_pspace_data
.get (pspace
);
101 info
= objfiles_pspace_data
.emplace (pspace
);
108 /* Per-BFD data key. */
110 static const registry
<bfd
>::key
<objfile_per_bfd_storage
> objfiles_bfd_data
;
112 objfile_per_bfd_storage::~objfile_per_bfd_storage ()
116 /* Create the per-BFD storage object for OBJFILE. If ABFD is not
117 NULL, and it already has a per-BFD storage object, use that.
118 Otherwise, allocate a new per-BFD storage object. */
121 set_objfile_per_bfd (struct objfile
*objfile
)
123 bfd
*abfd
= objfile
->obfd
.get ();
124 struct objfile_per_bfd_storage
*storage
= NULL
;
127 storage
= objfiles_bfd_data
.get (abfd
);
131 storage
= new objfile_per_bfd_storage (abfd
);
132 /* If the object requires gdb to do relocations, we simply fall
133 back to not sharing data across users. These cases are rare
134 enough that this seems reasonable. */
135 if (abfd
!= NULL
&& !gdb_bfd_requires_relocations (abfd
))
136 objfiles_bfd_data
.set (abfd
, storage
);
138 objfile
->per_bfd_storage
.reset (storage
);
140 /* Look up the gdbarch associated with the BFD. */
142 storage
->gdbarch
= gdbarch_from_bfd (abfd
);
145 objfile
->per_bfd
= storage
;
148 /* Set the objfile's per-BFD notion of the "main" name and
152 set_objfile_main_name (struct objfile
*objfile
,
153 const char *name
, enum language lang
)
155 if (objfile
->per_bfd
->name_of_main
== NULL
156 || strcmp (objfile
->per_bfd
->name_of_main
, name
) != 0)
157 objfile
->per_bfd
->name_of_main
158 = obstack_strdup (&objfile
->per_bfd
->storage_obstack
, name
);
159 objfile
->per_bfd
->language_of_main
= lang
;
162 /* Helper structure to map blocks to static link properties in hash tables. */
164 struct static_link_htab_entry
166 const struct block
*block
;
167 const struct dynamic_prop
*static_link
;
170 /* Return a hash code for struct static_link_htab_entry *P. */
173 static_link_htab_entry_hash (const void *p
)
175 const struct static_link_htab_entry
*e
176 = (const struct static_link_htab_entry
*) p
;
178 return htab_hash_pointer (e
->block
);
181 /* Return whether P1 an P2 (pointers to struct static_link_htab_entry) are
182 mappings for the same block. */
185 static_link_htab_entry_eq (const void *p1
, const void *p2
)
187 const struct static_link_htab_entry
*e1
188 = (const struct static_link_htab_entry
*) p1
;
189 const struct static_link_htab_entry
*e2
190 = (const struct static_link_htab_entry
*) p2
;
192 return e1
->block
== e2
->block
;
195 /* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
196 Must not be called more than once for each BLOCK. */
199 objfile_register_static_link (struct objfile
*objfile
,
200 const struct block
*block
,
201 const struct dynamic_prop
*static_link
)
204 struct static_link_htab_entry lookup_entry
;
205 struct static_link_htab_entry
*entry
;
207 if (objfile
->static_links
== NULL
)
208 objfile
->static_links
.reset (htab_create_alloc
209 (1, &static_link_htab_entry_hash
, static_link_htab_entry_eq
, NULL
,
212 /* Create a slot for the mapping, make sure it's the first mapping for this
213 block and then create the mapping itself. */
214 lookup_entry
.block
= block
;
215 slot
= htab_find_slot (objfile
->static_links
.get (), &lookup_entry
, INSERT
);
216 gdb_assert (*slot
== NULL
);
218 entry
= XOBNEW (&objfile
->objfile_obstack
, static_link_htab_entry
);
219 entry
->block
= block
;
220 entry
->static_link
= static_link
;
221 *slot
= (void *) entry
;
224 /* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
227 const struct dynamic_prop
*
228 objfile_lookup_static_link (struct objfile
*objfile
,
229 const struct block
*block
)
231 struct static_link_htab_entry
*entry
;
232 struct static_link_htab_entry lookup_entry
;
234 if (objfile
->static_links
== NULL
)
236 lookup_entry
.block
= block
;
237 entry
= ((struct static_link_htab_entry
*)
238 htab_find (objfile
->static_links
.get (), &lookup_entry
));
242 gdb_assert (entry
->block
== block
);
243 return entry
->static_link
;
248 /* Build up the section table that the objfile references. The
249 objfile contains pointers to the start of the table
250 (objfile->sections) and to the first location after the end of the
251 table (objfile->sections_end). */
254 add_to_objfile_sections (struct bfd
*abfd
, struct bfd_section
*asect
,
255 struct objfile
*objfile
, int force
)
257 struct obj_section
*section
;
263 aflag
= bfd_section_flags (asect
);
264 if (!(aflag
& SEC_ALLOC
))
268 section
= &objfile
->sections
[gdb_bfd_section_index (abfd
, asect
)];
269 section
->objfile
= objfile
;
270 section
->the_bfd_section
= asect
;
271 section
->ovly_mapped
= 0;
274 /* Builds a section table for OBJFILE.
276 Note that the OFFSET and OVLY_MAPPED in each table entry are
277 initialized to zero. */
280 build_objfile_section_table (struct objfile
*objfile
)
282 int count
= gdb_bfd_count_sections (objfile
->obfd
.get ());
284 objfile
->sections
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
287 objfile
->sections_end
= (objfile
->sections
+ count
);
288 for (asection
*sect
: gdb_bfd_sections (objfile
->obfd
))
289 add_to_objfile_sections (objfile
->obfd
.get (), sect
, objfile
, 0);
291 /* See gdb_bfd_section_index. */
292 add_to_objfile_sections (objfile
->obfd
.get (), bfd_com_section_ptr
,
294 add_to_objfile_sections (objfile
->obfd
.get (), bfd_und_section_ptr
,
296 add_to_objfile_sections (objfile
->obfd
.get (), bfd_abs_section_ptr
,
298 add_to_objfile_sections (objfile
->obfd
.get (), bfd_ind_section_ptr
,
302 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
303 initialize the new objfile as best we can and link it into the list
304 of all known objfiles.
306 NAME should contain original non-canonicalized filename or other
307 identifier as entered by user. If there is no better source use
308 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
309 NAME content is copied into returned objfile.
311 The FLAGS word contains various bits (OBJF_*) that can be taken as
312 requests for specific operations. Other bits like OBJF_SHARED are
313 simply copied through to the new objfile flags member. */
315 objfile::objfile (gdb_bfd_ref_ptr bfd_
, const char *name
, objfile_flags flags_
)
317 pspace (current_program_space
),
318 obfd (std::move (bfd_
))
320 const char *expanded_name
;
322 std::string name_holder
;
325 gdb_assert (obfd
== nullptr);
326 gdb_assert ((flags
& OBJF_NOT_FILENAME
) != 0);
327 expanded_name
= "<<anonymous objfile>>";
329 else if ((flags
& OBJF_NOT_FILENAME
) != 0
330 || is_target_filename (name
))
331 expanded_name
= name
;
334 name_holder
= gdb_abspath (name
);
335 expanded_name
= name_holder
.c_str ();
337 original_name
= obstack_strdup (&objfile_obstack
, expanded_name
);
339 /* Update the per-objfile information that comes from the bfd, ensuring
340 that any data that is reference is saved in the per-objfile data
345 mtime
= bfd_get_mtime (obfd
.get ());
347 /* Build section table. */
348 build_objfile_section_table (this);
351 set_objfile_per_bfd (this);
354 /* If there is a valid and known entry point, function fills *ENTRY_P with it
355 and returns non-zero; otherwise it returns zero. */
358 entry_point_address_query (CORE_ADDR
*entry_p
)
360 objfile
*objf
= current_program_space
->symfile_object_file
;
361 if (objf
== NULL
|| !objf
->per_bfd
->ei
.entry_point_p
)
364 int idx
= objf
->per_bfd
->ei
.the_bfd_section_index
;
365 *entry_p
= objf
->per_bfd
->ei
.entry_point
+ objf
->section_offsets
[idx
];
370 /* Get current entry point address. Call error if it is not known. */
373 entry_point_address (void)
377 if (!entry_point_address_query (&retval
))
378 error (_("Entry point address is not known."));
383 separate_debug_iterator
&
384 separate_debug_iterator::operator++ ()
386 gdb_assert (m_objfile
!= nullptr);
390 /* If any, return the first child. */
391 res
= m_objfile
->separate_debug_objfile
;
398 /* Common case where there is no separate debug objfile. */
399 if (m_objfile
== m_parent
)
405 /* Return the brother if any. Note that we don't iterate on brothers of
407 res
= m_objfile
->separate_debug_objfile_link
;
414 for (res
= m_objfile
->separate_debug_objfile_backlink
;
416 res
= res
->separate_debug_objfile_backlink
)
418 gdb_assert (res
!= nullptr);
419 if (res
->separate_debug_objfile_link
!= nullptr)
421 m_objfile
= res
->separate_debug_objfile_link
;
429 /* Add OBJFILE as a separate debug objfile of PARENT. */
432 add_separate_debug_objfile (struct objfile
*objfile
, struct objfile
*parent
)
434 gdb_assert (objfile
&& parent
);
436 /* Must not be already in a list. */
437 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
438 gdb_assert (objfile
->separate_debug_objfile_link
== NULL
);
439 gdb_assert (objfile
->separate_debug_objfile
== NULL
);
440 gdb_assert (parent
->separate_debug_objfile_backlink
== NULL
);
441 gdb_assert (parent
->separate_debug_objfile_link
== NULL
);
443 objfile
->separate_debug_objfile_backlink
= parent
;
444 objfile
->separate_debug_objfile_link
= parent
->separate_debug_objfile
;
445 parent
->separate_debug_objfile
= objfile
;
448 /* See objfiles.h. */
451 objfile::make (gdb_bfd_ref_ptr bfd_
, const char *name_
, objfile_flags flags_
,
454 objfile
*result
= new objfile (std::move (bfd_
), name_
, flags_
);
455 if (parent
!= nullptr)
456 add_separate_debug_objfile (result
, parent
);
458 current_program_space
->add_objfile (std::unique_ptr
<objfile
> (result
),
461 /* Rebuild section map next time we need it. */
462 get_objfile_pspace_data (current_program_space
)->new_objfiles_available
= 1;
467 /* See objfiles.h. */
472 current_program_space
->remove_objfile (this);
475 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
479 free_objfile_separate_debug (struct objfile
*objfile
)
481 struct objfile
*child
;
483 for (child
= objfile
->separate_debug_objfile
; child
;)
485 struct objfile
*next_child
= child
->separate_debug_objfile_link
;
491 /* Destroy an objfile and all the symtabs and psymtabs under it. */
495 /* First notify observers that this objfile is about to be freed. */
496 gdb::observers::free_objfile
.notify (this);
498 /* Free all separate debug objfiles. */
499 free_objfile_separate_debug (this);
501 if (separate_debug_objfile_backlink
)
503 /* We freed the separate debug file, make sure the base objfile
504 doesn't reference it. */
505 struct objfile
*child
;
507 child
= separate_debug_objfile_backlink
->separate_debug_objfile
;
511 /* THIS is the first child. */
512 separate_debug_objfile_backlink
->separate_debug_objfile
=
513 separate_debug_objfile_link
;
517 /* Find THIS in the list. */
520 if (child
->separate_debug_objfile_link
== this)
522 child
->separate_debug_objfile_link
=
523 separate_debug_objfile_link
;
526 child
= child
->separate_debug_objfile_link
;
532 /* Remove any references to this objfile in the global value
534 preserve_values (this);
536 /* It still may reference data modules have associated with the objfile and
537 the symbol file data. */
538 forget_cached_source_info_for_objfile (this);
540 breakpoint_free_objfile (this);
541 btrace_free_objfile (this);
543 /* First do any symbol file specific actions required when we are
544 finished with a particular symbol file. Note that if the objfile
545 is using reusable symbol information (via mmalloc) then each of
546 these routines is responsible for doing the correct thing, either
547 freeing things which are valid only during this particular gdb
548 execution, or leaving them to be reused during the next one. */
551 (*sf
->sym_finish
) (this);
553 /* Before the symbol table code was redone to make it easier to
554 selectively load and remove information particular to a specific
555 linkage unit, gdb used to do these things whenever the monolithic
556 symbol table was blown away. How much still needs to be done
557 is unknown, but we play it safe for now and keep each action until
558 it is shown to be no longer needed. */
560 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
561 for example), so we need to call this here. */
562 clear_pc_function_cache ();
564 /* Check to see if the current_source_symtab belongs to this objfile,
565 and if so, call clear_current_source_symtab_and_line. */
568 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
570 if (cursal
.symtab
&& cursal
.symtab
->compunit ()->objfile () == this)
571 clear_current_source_symtab_and_line ();
574 /* Rebuild section map next time we need it. */
575 get_objfile_pspace_data (pspace
)->section_map_dirty
= 1;
579 /* A helper function for objfile_relocate1 that relocates a single
583 relocate_one_symbol (struct symbol
*sym
, struct objfile
*objfile
,
584 const section_offsets
&delta
)
586 fixup_symbol_section (sym
, objfile
);
588 /* The RS6000 code from which this was taken skipped
589 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
590 But I'm leaving out that test, on the theory that
591 they can't possibly pass the tests below. */
592 if ((sym
->aclass () == LOC_LABEL
593 || sym
->aclass () == LOC_STATIC
)
594 && sym
->section_index () >= 0)
595 sym
->set_value_address (sym
->value_address ()
596 + delta
[sym
->section_index ()]);
599 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
600 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
601 Return non-zero iff any change happened. */
604 objfile_relocate1 (struct objfile
*objfile
,
605 const section_offsets
&new_offsets
)
607 section_offsets
delta (objfile
->section_offsets
.size ());
609 int something_changed
= 0;
611 for (int i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
613 delta
[i
] = new_offsets
[i
] - objfile
->section_offsets
[i
];
615 something_changed
= 1;
617 if (!something_changed
)
620 /* OK, get all the symtabs. */
622 for (compunit_symtab
*cust
: objfile
->compunits ())
624 for (symtab
*s
: cust
->filetabs ())
628 /* First the line table. */
632 for (int i
= 0; i
< l
->nitems
; ++i
)
633 l
->item
[i
].pc
+= delta
[cust
->block_line_section ()];
638 for (compunit_symtab
*cust
: objfile
->compunits ())
640 struct blockvector
*bv
= cust
->blockvector ();
641 int block_line_section
= cust
->block_line_section ();
643 if (bv
->map () != nullptr)
644 bv
->map ()->relocate (delta
[block_line_section
]);
646 for (block
*b
: bv
->blocks ())
649 struct mdict_iterator miter
;
651 b
->set_start (b
->start () + delta
[block_line_section
]);
652 b
->set_end (b
->end () + delta
[block_line_section
]);
654 for (blockrange
&r
: b
->ranges ())
656 r
.set_start (r
.start () + delta
[block_line_section
]);
657 r
.set_end (r
.end () + delta
[block_line_section
]);
660 /* We only want to iterate over the local symbols, not any
661 symbols in included symtabs. */
662 ALL_DICT_SYMBOLS (b
->multidict (), miter
, sym
)
664 relocate_one_symbol (sym
, objfile
, delta
);
670 /* Relocate isolated symbols. */
674 for (iter
= objfile
->template_symbols
; iter
; iter
= iter
->hash_next
)
675 relocate_one_symbol (iter
, objfile
, delta
);
681 for (i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
682 objfile
->section_offsets
[i
] = new_offsets
[i
];
685 /* Rebuild section map next time we need it. */
686 get_objfile_pspace_data (objfile
->pspace
)->section_map_dirty
= 1;
688 /* Update the table in exec_ops, used to read memory. */
689 struct obj_section
*s
;
690 ALL_OBJFILE_OSECTIONS (objfile
, s
)
692 int idx
= s
- objfile
->sections
;
694 exec_set_section_address (bfd_get_filename (objfile
->obfd
.get ()), idx
,
702 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
703 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
705 The number and ordering of sections does differ between the two objfiles.
706 Only their names match. Also the file offsets will differ (objfile being
707 possibly prelinked but separate_debug_objfile is probably not prelinked) but
708 the in-memory absolute address as specified by NEW_OFFSETS must match both
712 objfile_relocate (struct objfile
*objfile
,
713 const section_offsets
&new_offsets
)
717 changed
|= objfile_relocate1 (objfile
, new_offsets
);
719 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
721 if (debug_objfile
== objfile
)
724 section_addr_info objfile_addrs
725 = build_section_addr_info_from_objfile (objfile
);
727 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
728 relative ones must be already created according to debug_objfile. */
730 addr_info_make_relative (&objfile_addrs
, debug_objfile
->obfd
.get ());
732 gdb_assert (debug_objfile
->section_offsets
.size ()
733 == gdb_bfd_count_sections (debug_objfile
->obfd
.get ()));
734 section_offsets new_debug_offsets
735 (debug_objfile
->section_offsets
.size ());
736 relative_addr_info_to_section_offsets (new_debug_offsets
, objfile_addrs
);
738 changed
|= objfile_relocate1 (debug_objfile
, new_debug_offsets
);
741 /* Relocate breakpoints as necessary, after things are relocated. */
743 breakpoint_re_set ();
746 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
748 Return non-zero iff any change happened. */
751 objfile_rebase1 (struct objfile
*objfile
, CORE_ADDR slide
)
753 section_offsets
new_offsets (objfile
->section_offsets
.size (), slide
);
754 return objfile_relocate1 (objfile
, new_offsets
);
757 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
758 SEPARATE_DEBUG_OBJFILEs. */
761 objfile_rebase (struct objfile
*objfile
, CORE_ADDR slide
)
765 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
766 changed
|= objfile_rebase1 (debug_objfile
, slide
);
768 /* Relocate breakpoints as necessary, after things are relocated. */
770 breakpoint_re_set ();
773 /* Return non-zero if OBJFILE has full symbols. */
776 objfile_has_full_symbols (struct objfile
*objfile
)
778 return objfile
->compunit_symtabs
!= NULL
;
781 /* Return non-zero if OBJFILE has full or partial symbols, either directly
782 or through a separate debug file. */
785 objfile_has_symbols (struct objfile
*objfile
)
787 for (::objfile
*o
: objfile
->separate_debug_objfiles ())
788 if (o
->has_partial_symbols () || objfile_has_full_symbols (o
))
794 /* Many places in gdb want to test just to see if we have any partial
795 symbols available. This function returns zero if none are currently
796 available, nonzero otherwise. */
799 have_partial_symbols (void)
801 for (objfile
*ofp
: current_program_space
->objfiles ())
803 if (ofp
->has_partial_symbols ())
809 /* Many places in gdb want to test just to see if we have any full
810 symbols available. This function returns zero if none are currently
811 available, nonzero otherwise. */
814 have_full_symbols (void)
816 for (objfile
*ofp
: current_program_space
->objfiles ())
818 if (objfile_has_full_symbols (ofp
))
825 /* This operations deletes all objfile entries that represent solibs that
826 weren't explicitly loaded by the user, via e.g., the add-symbol-file
830 objfile_purge_solibs (void)
832 for (objfile
*objf
: current_program_space
->objfiles_safe ())
834 /* We assume that the solib package has been purged already, or will
837 if (!(objf
->flags
& OBJF_USERLOADED
) && (objf
->flags
& OBJF_SHARED
))
843 /* Many places in gdb want to test just to see if we have any minimal
844 symbols available. This function returns zero if none are currently
845 available, nonzero otherwise. */
848 have_minimal_symbols (void)
850 for (objfile
*ofp
: current_program_space
->objfiles ())
852 if (ofp
->per_bfd
->minimal_symbol_count
> 0)
860 /* Qsort comparison function. */
863 sort_cmp (const struct obj_section
*sect1
, const obj_section
*sect2
)
865 const CORE_ADDR sect1_addr
= sect1
->addr ();
866 const CORE_ADDR sect2_addr
= sect2
->addr ();
868 if (sect1_addr
< sect2_addr
)
870 else if (sect1_addr
> sect2_addr
)
874 /* Sections are at the same address. This could happen if
875 A) we have an objfile and a separate debuginfo.
876 B) we are confused, and have added sections without proper relocation,
877 or something like that. */
879 const struct objfile
*const objfile1
= sect1
->objfile
;
880 const struct objfile
*const objfile2
= sect2
->objfile
;
882 if (objfile1
->separate_debug_objfile
== objfile2
883 || objfile2
->separate_debug_objfile
== objfile1
)
885 /* Case A. The ordering doesn't matter: separate debuginfo files
886 will be filtered out later. */
891 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
892 triage. This section could be slow (since we iterate over all
893 objfiles in each call to sort_cmp), but this shouldn't happen
894 very often (GDB is already in a confused state; one hopes this
895 doesn't happen at all). If you discover that significant time is
896 spent in the loops below, do 'set complaints 100' and examine the
897 resulting complaints. */
898 if (objfile1
== objfile2
)
900 /* Both sections came from the same objfile. We are really
901 confused. Sort on sequence order of sections within the
902 objfile. The order of checks is important here, if we find a
903 match on SECT2 first then either SECT2 is before SECT1, or,
904 SECT2 == SECT1, in both cases we should return false. The
905 second case shouldn't occur during normal use, but std::sort
906 does check that '!(a < a)' when compiled in debug mode. */
908 const struct obj_section
*osect
;
910 ALL_OBJFILE_OSECTIONS (objfile1
, osect
)
913 else if (osect
== sect1
)
916 /* We should have found one of the sections before getting here. */
917 gdb_assert_not_reached ("section not found");
921 /* Sort on sequence number of the objfile in the chain. */
923 for (objfile
*objfile
: current_program_space
->objfiles ())
924 if (objfile
== objfile1
)
926 else if (objfile
== objfile2
)
929 /* We should have found one of the objfiles before getting here. */
930 gdb_assert_not_reached ("objfile not found");
935 gdb_assert_not_reached ("unexpected code path");
939 /* Select "better" obj_section to keep. We prefer the one that came from
940 the real object, rather than the one from separate debuginfo.
941 Most of the time the two sections are exactly identical, but with
942 prelinking the .rel.dyn section in the real object may have different
945 static struct obj_section
*
946 preferred_obj_section (struct obj_section
*a
, struct obj_section
*b
)
948 gdb_assert (a
->addr () == b
->addr ());
949 gdb_assert ((a
->objfile
->separate_debug_objfile
== b
->objfile
)
950 || (b
->objfile
->separate_debug_objfile
== a
->objfile
));
951 gdb_assert ((a
->objfile
->separate_debug_objfile_backlink
== b
->objfile
)
952 || (b
->objfile
->separate_debug_objfile_backlink
== a
->objfile
));
954 if (a
->objfile
->separate_debug_objfile
!= NULL
)
959 /* Return 1 if SECTION should be inserted into the section map.
960 We want to insert only non-overlay non-TLS non-empty sections. */
963 insert_section_p (const struct bfd
*abfd
,
964 const struct bfd_section
*section
)
966 const bfd_vma lma
= bfd_section_lma (section
);
968 if (overlay_debugging
&& lma
!= 0 && lma
!= bfd_section_vma (section
)
969 && (bfd_get_file_flags (abfd
) & BFD_IN_MEMORY
) == 0)
970 /* This is an overlay section. IN_MEMORY check is needed to avoid
971 discarding sections from the "system supplied DSO" (aka vdso)
972 on some Linux systems (e.g. Fedora 11). */
974 if ((bfd_section_flags (section
) & SEC_THREAD_LOCAL
) != 0)
975 /* This is a TLS section. */
977 if (bfd_section_size (section
) == 0)
979 /* This is an empty section. It has no PCs for find_pc_section (), so
980 there is no reason to insert it into the section map. */
987 /* Filter out overlapping sections where one section came from the real
988 objfile, and the other from a separate debuginfo file.
989 Return the size of table after redundant sections have been eliminated. */
992 filter_debuginfo_sections (struct obj_section
**map
, int map_size
)
996 for (i
= 0, j
= 0; i
< map_size
- 1; i
++)
998 struct obj_section
*const sect1
= map
[i
];
999 struct obj_section
*const sect2
= map
[i
+ 1];
1000 const struct objfile
*const objfile1
= sect1
->objfile
;
1001 const struct objfile
*const objfile2
= sect2
->objfile
;
1002 const CORE_ADDR sect1_addr
= sect1
->addr ();
1003 const CORE_ADDR sect2_addr
= sect2
->addr ();
1005 if (sect1_addr
== sect2_addr
1006 && (objfile1
->separate_debug_objfile
== objfile2
1007 || objfile2
->separate_debug_objfile
== objfile1
))
1009 map
[j
++] = preferred_obj_section (sect1
, sect2
);
1018 gdb_assert (i
== map_size
- 1);
1022 /* The map should not have shrunk to less than half the original size. */
1023 gdb_assert (map_size
/ 2 <= j
);
1028 /* Filter out overlapping sections, issuing a warning if any are found.
1029 Overlapping sections could really be overlay sections which we didn't
1030 classify as such in insert_section_p, or we could be dealing with a
1034 filter_overlapping_sections (struct obj_section
**map
, int map_size
)
1038 for (i
= 0, j
= 0; i
< map_size
- 1; )
1043 for (k
= i
+ 1; k
< map_size
; k
++)
1045 struct obj_section
*const sect1
= map
[i
];
1046 struct obj_section
*const sect2
= map
[k
];
1047 const CORE_ADDR sect1_addr
= sect1
->addr ();
1048 const CORE_ADDR sect2_addr
= sect2
->addr ();
1049 const CORE_ADDR sect1_endaddr
= sect1
->endaddr ();
1051 gdb_assert (sect1_addr
<= sect2_addr
);
1053 if (sect1_endaddr
<= sect2_addr
)
1057 /* We have an overlap. Report it. */
1059 struct objfile
*const objf1
= sect1
->objfile
;
1060 struct objfile
*const objf2
= sect2
->objfile
;
1062 const struct bfd_section
*const bfds1
= sect1
->the_bfd_section
;
1063 const struct bfd_section
*const bfds2
= sect2
->the_bfd_section
;
1065 const CORE_ADDR sect2_endaddr
= sect2
->endaddr ();
1067 struct gdbarch
*const gdbarch
= objf1
->arch ();
1069 complaint (_("unexpected overlap between:\n"
1070 " (A) section `%s' from `%s' [%s, %s)\n"
1071 " (B) section `%s' from `%s' [%s, %s).\n"
1072 "Will ignore section B"),
1073 bfd_section_name (bfds1
), objfile_name (objf1
),
1074 paddress (gdbarch
, sect1_addr
),
1075 paddress (gdbarch
, sect1_endaddr
),
1076 bfd_section_name (bfds2
), objfile_name (objf2
),
1077 paddress (gdbarch
, sect2_addr
),
1078 paddress (gdbarch
, sect2_endaddr
));
1086 gdb_assert (i
== map_size
- 1);
1094 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
1095 TLS, overlay and overlapping sections. */
1098 update_section_map (struct program_space
*pspace
,
1099 struct obj_section
***pmap
, int *pmap_size
)
1101 struct objfile_pspace_info
*pspace_info
;
1102 int alloc_size
, map_size
, i
;
1103 struct obj_section
*s
, **map
;
1105 pspace_info
= get_objfile_pspace_data (pspace
);
1106 gdb_assert (pspace_info
->section_map_dirty
!= 0
1107 || pspace_info
->new_objfiles_available
!= 0);
1113 for (objfile
*objfile
: pspace
->objfiles ())
1114 ALL_OBJFILE_OSECTIONS (objfile
, s
)
1115 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1118 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1119 if (alloc_size
== 0)
1126 map
= XNEWVEC (struct obj_section
*, alloc_size
);
1129 for (objfile
*objfile
: pspace
->objfiles ())
1130 ALL_OBJFILE_OSECTIONS (objfile
, s
)
1131 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1134 std::sort (map
, map
+ alloc_size
, sort_cmp
);
1135 map_size
= filter_debuginfo_sections(map
, alloc_size
);
1136 map_size
= filter_overlapping_sections(map
, map_size
);
1138 if (map_size
< alloc_size
)
1139 /* Some sections were eliminated. Trim excess space. */
1140 map
= XRESIZEVEC (struct obj_section
*, map
, map_size
);
1142 gdb_assert (alloc_size
== map_size
);
1145 *pmap_size
= map_size
;
1148 /* Bsearch comparison function. */
1151 bsearch_cmp (const void *key
, const void *elt
)
1153 const CORE_ADDR pc
= *(CORE_ADDR
*) key
;
1154 const struct obj_section
*section
= *(const struct obj_section
**) elt
;
1156 if (pc
< section
->addr ())
1158 if (pc
< section
->endaddr ())
1163 /* Returns a section whose range includes PC or NULL if none found. */
1165 struct obj_section
*
1166 find_pc_section (CORE_ADDR pc
)
1168 struct objfile_pspace_info
*pspace_info
;
1169 struct obj_section
*s
, **sp
;
1171 /* Check for mapped overlay section first. */
1172 s
= find_pc_mapped_section (pc
);
1176 pspace_info
= get_objfile_pspace_data (current_program_space
);
1177 if (pspace_info
->section_map_dirty
1178 || (pspace_info
->new_objfiles_available
1179 && !pspace_info
->inhibit_updates
))
1181 update_section_map (current_program_space
,
1182 &pspace_info
->sections
,
1183 &pspace_info
->num_sections
);
1185 /* Don't need updates to section map until objfiles are added,
1186 removed or relocated. */
1187 pspace_info
->new_objfiles_available
= 0;
1188 pspace_info
->section_map_dirty
= 0;
1191 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1192 bsearch be non-NULL. */
1193 if (pspace_info
->sections
== NULL
)
1195 gdb_assert (pspace_info
->num_sections
== 0);
1199 sp
= (struct obj_section
**) bsearch (&pc
,
1200 pspace_info
->sections
,
1201 pspace_info
->num_sections
,
1202 sizeof (*pspace_info
->sections
),
1210 /* Return non-zero if PC is in a section called NAME. */
1213 pc_in_section (CORE_ADDR pc
, const char *name
)
1215 struct obj_section
*s
;
1218 s
= find_pc_section (pc
);
1221 && s
->the_bfd_section
->name
!= NULL
1222 && strcmp (s
->the_bfd_section
->name
, name
) == 0);
1227 /* Set section_map_dirty so section map will be rebuilt next time it
1228 is used. Called by reread_symbols. */
1231 objfiles_changed (void)
1233 /* Rebuild section map next time we need it. */
1234 get_objfile_pspace_data (current_program_space
)->section_map_dirty
= 1;
1237 /* See comments in objfiles.h. */
1239 scoped_restore_tmpl
<int>
1240 inhibit_section_map_updates (struct program_space
*pspace
)
1242 return scoped_restore_tmpl
<int>
1243 (&get_objfile_pspace_data (pspace
)->inhibit_updates
, 1);
1246 /* See objfiles.h. */
1249 is_addr_in_objfile (CORE_ADDR addr
, const struct objfile
*objfile
)
1251 struct obj_section
*osect
;
1253 if (objfile
== NULL
)
1256 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
1258 if (section_is_overlay (osect
) && !section_is_mapped (osect
))
1261 if (osect
->addr () <= addr
&& addr
< osect
->endaddr ())
1267 /* See objfiles.h. */
1270 shared_objfile_contains_address_p (struct program_space
*pspace
,
1273 for (objfile
*objfile
: pspace
->objfiles ())
1275 if ((objfile
->flags
& OBJF_SHARED
) != 0
1276 && is_addr_in_objfile (address
, objfile
))
1283 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1284 gdbarch method. It is equivalent to use the objfiles iterable,
1285 searching the objfiles in the order they are stored internally,
1286 ignoring CURRENT_OBJFILE.
1288 On most platforms, it should be close enough to doing the best
1289 we can without some knowledge specific to the architecture. */
1292 default_iterate_over_objfiles_in_search_order
1293 (gdbarch
*gdbarch
, iterate_over_objfiles_in_search_order_cb_ftype cb
,
1294 objfile
*current_objfile
)
1296 for (objfile
*objfile
: current_program_space
->objfiles ())
1301 /* See objfiles.h. */
1304 objfile_name (const struct objfile
*objfile
)
1306 if (objfile
->obfd
!= NULL
)
1307 return bfd_get_filename (objfile
->obfd
.get ());
1309 return objfile
->original_name
;
1312 /* See objfiles.h. */
1315 objfile_filename (const struct objfile
*objfile
)
1317 if (objfile
->obfd
!= NULL
)
1318 return bfd_get_filename (objfile
->obfd
.get ());
1323 /* See objfiles.h. */
1326 objfile_debug_name (const struct objfile
*objfile
)
1328 return lbasename (objfile
->original_name
);
1331 /* See objfiles.h. */
1334 objfile_flavour_name (struct objfile
*objfile
)
1336 if (objfile
->obfd
!= NULL
)
1337 return bfd_flavour_name (bfd_get_flavour (objfile
->obfd
.get ()));
1341 /* See objfiles.h. */
1344 objfile_int_type (struct objfile
*of
, int size_in_bytes
, bool unsigned_p
)
1346 struct type
*int_type
;
1348 /* Helper macro to examine the various builtin types. */
1349 #define TRY_TYPE(F) \
1350 int_type = (unsigned_p \
1351 ? objfile_type (of)->builtin_unsigned_ ## F \
1352 : objfile_type (of)->builtin_ ## F); \
1353 if (int_type != NULL && int_type->length () == size_in_bytes) \
1360 TRY_TYPE (long_long
);
1364 gdb_assert_not_reached ("unable to find suitable integer type");