1 /* GDB routines for manipulating objfiles.
3 Copyright (C) 1992-2025 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. */
30 #include "expression.h"
31 #include "parser-defs.h"
33 #include <sys/types.h>
36 #include "gdbsupport/gdb_obstack.h"
38 #include "breakpoint.h"
40 #include "dictionary.h"
43 #include "arch-utils.h"
45 #include "observable.h"
46 #include "complaints.h"
49 #include "gdbsupport/pathstuff.h"
53 /* Externally visible variables that are owned by this module.
54 See declarations in objfile.h for more info. */
56 struct objfile_pspace_info
58 objfile_pspace_info () = default;
59 ~objfile_pspace_info ();
61 struct obj_section
**sections
= nullptr;
64 /* Nonzero if object files have been added since the section map
66 int new_objfiles_available
= 0;
68 /* Nonzero if the section map MUST be updated before use. */
69 int section_map_dirty
= 0;
71 /* Nonzero if section map updates should be inhibited if possible. */
72 int inhibit_updates
= 0;
75 /* Per-program-space data key. */
76 static const registry
<program_space
>::key
<objfile_pspace_info
>
79 objfile_pspace_info::~objfile_pspace_info ()
84 /* Get the current svr4 data. If none is found yet, add it now. This
85 function always returns a valid object. */
87 static struct objfile_pspace_info
*
88 get_objfile_pspace_data (struct program_space
*pspace
)
90 struct objfile_pspace_info
*info
;
92 info
= objfiles_pspace_data
.get (pspace
);
94 info
= objfiles_pspace_data
.emplace (pspace
);
101 /* Per-BFD data key. */
103 static const registry
<bfd
>::key
<objfile_per_bfd_storage
> objfiles_bfd_data
;
105 objfile_per_bfd_storage::~objfile_per_bfd_storage ()
109 /* Create the per-BFD storage object for OBJFILE. If ABFD is not
110 NULL, and it already has a per-BFD storage object, use that.
111 Otherwise, allocate a new per-BFD storage object. */
114 set_objfile_per_bfd (struct objfile
*objfile
)
116 bfd
*abfd
= objfile
->obfd
.get ();
117 struct objfile_per_bfd_storage
*storage
= NULL
;
120 storage
= objfiles_bfd_data
.get (abfd
);
124 storage
= new objfile_per_bfd_storage (abfd
);
125 /* If the object requires gdb to do relocations, we simply fall
126 back to not sharing data across users. These cases are rare
127 enough that this seems reasonable. */
128 if (abfd
!= NULL
&& !gdb_bfd_requires_relocations (abfd
))
129 objfiles_bfd_data
.set (abfd
, storage
);
131 objfile
->per_bfd_storage
.reset (storage
);
133 /* Look up the gdbarch associated with the BFD. */
135 storage
->gdbarch
= gdbarch_from_bfd (abfd
);
138 objfile
->per_bfd
= storage
;
141 /* Set the objfile's per-BFD notion of the "main" name and
145 set_objfile_main_name (struct objfile
*objfile
,
146 const char *name
, enum language lang
)
148 if (objfile
->per_bfd
->name_of_main
== NULL
149 || strcmp (objfile
->per_bfd
->name_of_main
, name
) != 0)
150 objfile
->per_bfd
->name_of_main
151 = obstack_strdup (&objfile
->per_bfd
->storage_obstack
, name
);
152 objfile
->per_bfd
->language_of_main
= lang
;
155 /* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
156 Must not be called more than once for each BLOCK. */
159 objfile_register_static_link (struct objfile
*objfile
,
160 const struct block
*block
,
161 const struct dynamic_prop
*static_link
)
163 /* Enter the mapping and make sure it's the first mapping for this
165 bool inserted
= objfile
->static_links
.emplace (block
, static_link
).second
;
166 gdb_assert (inserted
);
169 /* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
172 const struct dynamic_prop
*
173 objfile_lookup_static_link (struct objfile
*objfile
,
174 const struct block
*block
)
176 if (auto iter
= objfile
->static_links
.find (block
);
177 iter
!= objfile
->static_links
.end ())
185 /* Build up the section table that the objfile references. The
186 objfile contains pointers to the start of the table
187 (objfile->sections) and to the first location after the end of the
188 table (objfile->sections_end). */
191 add_to_objfile_sections (struct bfd
*abfd
, struct bfd_section
*asect
,
192 struct objfile
*objfile
, int force
)
194 struct obj_section
*section
;
200 aflag
= bfd_section_flags (asect
);
201 if (!(aflag
& SEC_ALLOC
))
205 section
= &objfile
->sections_start
[gdb_bfd_section_index (abfd
, asect
)];
206 section
->objfile
= objfile
;
207 section
->the_bfd_section
= asect
;
208 section
->ovly_mapped
= 0;
211 /* Builds a section table for OBJFILE.
213 Note that the OFFSET and OVLY_MAPPED in each table entry are
214 initialized to zero. */
217 build_objfile_section_table (struct objfile
*objfile
)
219 int count
= gdb_bfd_count_sections (objfile
->obfd
.get ());
221 objfile
->sections_start
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
224 objfile
->sections_end
= (objfile
->sections_start
+ count
);
225 for (asection
*sect
: gdb_bfd_sections (objfile
->obfd
))
226 add_to_objfile_sections (objfile
->obfd
.get (), sect
, objfile
, 0);
228 /* See gdb_bfd_section_index. */
229 add_to_objfile_sections (objfile
->obfd
.get (), bfd_com_section_ptr
,
231 add_to_objfile_sections (objfile
->obfd
.get (), bfd_und_section_ptr
,
233 add_to_objfile_sections (objfile
->obfd
.get (), bfd_abs_section_ptr
,
235 add_to_objfile_sections (objfile
->obfd
.get (), bfd_ind_section_ptr
,
239 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
240 initialize the new objfile as best we can and link it into the list
241 of all known objfiles.
243 NAME should contain original non-canonicalized filename or other
244 identifier as entered by user. If there is no better source use
245 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
246 NAME content is copied into returned objfile.
248 The FLAGS word contains various bits (OBJF_*) that can be taken as
249 requests for specific operations. Other bits like OBJF_SHARED are
250 simply copied through to the new objfile flags member. */
252 objfile::objfile (gdb_bfd_ref_ptr bfd_
, program_space
*pspace
,
253 const char *name
, objfile_flags flags_
)
256 obfd (std::move (bfd_
))
258 const char *expanded_name
;
260 std::string name_holder
;
263 gdb_assert (obfd
== nullptr);
264 gdb_assert ((flags
& OBJF_NOT_FILENAME
) != 0);
265 expanded_name
= "<<anonymous objfile>>";
267 else if ((flags
& OBJF_NOT_FILENAME
) != 0
268 || is_target_filename (name
))
269 expanded_name
= name
;
272 name_holder
= gdb_abspath (name
);
273 expanded_name
= name_holder
.c_str ();
275 original_name
= obstack_strdup (&objfile_obstack
, expanded_name
);
277 /* Update the per-objfile information that comes from the bfd, ensuring
278 that any data that is reference is saved in the per-objfile data
283 mtime
= gdb_bfd_get_mtime (obfd
.get ());
285 /* Build section table. */
286 build_objfile_section_table (this);
289 set_objfile_per_bfd (this);
292 /* See objfiles.h. */
295 entry_point_address_query (program_space
*pspace
, CORE_ADDR
*entry_p
)
297 objfile
*objf
= pspace
->symfile_object_file
;
298 if (objf
== NULL
|| !objf
->per_bfd
->ei
.entry_point_p
)
301 int idx
= objf
->per_bfd
->ei
.the_bfd_section_index
;
302 *entry_p
= objf
->per_bfd
->ei
.entry_point
+ objf
->section_offsets
[idx
];
307 /* See objfiles.h. */
310 entry_point_address (program_space
*pspace
)
314 if (!entry_point_address_query (pspace
, &retval
))
315 error (_("Entry point address is not known."));
320 separate_debug_iterator
&
321 separate_debug_iterator::operator++ ()
323 gdb_assert (m_objfile
!= nullptr);
327 /* If any, return the first child. */
328 res
= m_objfile
->separate_debug_objfile
;
335 /* Common case where there is no separate debug objfile. */
336 if (m_objfile
== m_parent
)
342 /* Return the brother if any. Note that we don't iterate on brothers of
344 res
= m_objfile
->separate_debug_objfile_link
;
351 for (res
= m_objfile
->separate_debug_objfile_backlink
;
353 res
= res
->separate_debug_objfile_backlink
)
355 gdb_assert (res
!= nullptr);
356 if (res
->separate_debug_objfile_link
!= nullptr)
358 m_objfile
= res
->separate_debug_objfile_link
;
366 /* Add OBJFILE as a separate debug objfile of PARENT. */
369 add_separate_debug_objfile (struct objfile
*objfile
, struct objfile
*parent
)
371 gdb_assert (objfile
&& parent
);
373 /* Must not be already in a list. */
374 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
375 gdb_assert (objfile
->separate_debug_objfile_link
== NULL
);
376 gdb_assert (objfile
->separate_debug_objfile
== NULL
);
377 gdb_assert (parent
->separate_debug_objfile_backlink
== NULL
);
378 gdb_assert (parent
->separate_debug_objfile_link
== NULL
);
380 objfile
->separate_debug_objfile_backlink
= parent
;
381 objfile
->separate_debug_objfile_link
= parent
->separate_debug_objfile
;
382 parent
->separate_debug_objfile
= objfile
;
385 /* See objfiles.h. */
388 objfile::make (gdb_bfd_ref_ptr bfd_
, program_space
*pspace
, const char *name_
,
389 objfile_flags flags_
, objfile
*parent
)
391 objfile
*result
= new objfile (std::move (bfd_
), pspace
, name_
, flags_
);
392 if (parent
!= nullptr)
393 add_separate_debug_objfile (result
, parent
);
395 pspace
->add_objfile (std::unique_ptr
<objfile
> (result
), parent
);
397 /* Rebuild section map next time we need it. */
398 get_objfile_pspace_data (pspace
)->new_objfiles_available
= 1;
403 /* See objfiles.h. */
408 this->pspace ()->remove_objfile (this);
411 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
415 free_objfile_separate_debug (struct objfile
*objfile
)
417 struct objfile
*child
;
419 for (child
= objfile
->separate_debug_objfile
; child
;)
421 struct objfile
*next_child
= child
->separate_debug_objfile_link
;
427 /* Destroy an objfile and all the symtabs and psymtabs under it. */
431 /* First notify observers that this objfile is about to be freed. */
432 gdb::observers::free_objfile
.notify (this);
434 /* Free all separate debug objfiles. */
435 free_objfile_separate_debug (this);
437 if (separate_debug_objfile_backlink
)
439 /* We freed the separate debug file, make sure the base objfile
440 doesn't reference it. */
441 struct objfile
*child
;
443 child
= separate_debug_objfile_backlink
->separate_debug_objfile
;
447 /* THIS is the first child. */
448 separate_debug_objfile_backlink
->separate_debug_objfile
=
449 separate_debug_objfile_link
;
453 /* Find THIS in the list. */
456 if (child
->separate_debug_objfile_link
== this)
458 child
->separate_debug_objfile_link
=
459 separate_debug_objfile_link
;
462 child
= child
->separate_debug_objfile_link
;
468 /* Remove any references to this objfile in the global value
470 preserve_values (this);
472 /* It still may reference data modules have associated with the objfile and
473 the symbol file data. */
474 forget_cached_source_info ();
475 for (compunit_symtab
*cu
: compunits ())
478 breakpoint_free_objfile (this);
479 btrace_free_objfile (this);
481 /* First do any symbol file specific actions required when we are
482 finished with a particular symbol file. Note that if the objfile
483 is using reusable symbol information (via mmalloc) then each of
484 these routines is responsible for doing the correct thing, either
485 freeing things which are valid only during this particular gdb
486 execution, or leaving them to be reused during the next one. */
489 (*sf
->sym_finish
) (this);
491 /* Before the symbol table code was redone to make it easier to
492 selectively load and remove information particular to a specific
493 linkage unit, gdb used to do these things whenever the monolithic
494 symbol table was blown away. How much still needs to be done
495 is unknown, but we play it safe for now and keep each action until
496 it is shown to be no longer needed. */
498 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
499 for example), so we need to call this here. */
500 clear_pc_function_cache ();
502 /* Check to see if the current_source_symtab belongs to this objfile,
503 and if so, call clear_current_source_symtab_and_line. */
504 clear_current_source_symtab_and_line (this);
506 /* Rebuild section map next time we need it. */
507 auto info
= objfiles_pspace_data
.get (pspace ());
509 info
->section_map_dirty
= 1;
513 /* A helper function for objfile_relocate1 that relocates a single
517 relocate_one_symbol (struct symbol
*sym
, struct objfile
*objfile
,
518 const section_offsets
&delta
)
520 /* The RS6000 code from which this was taken skipped
521 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
522 But I'm leaving out that test, on the theory that
523 they can't possibly pass the tests below. */
524 if ((sym
->aclass () == LOC_LABEL
525 || sym
->aclass () == LOC_STATIC
)
526 && sym
->section_index () >= 0)
527 sym
->set_value_address (sym
->value_address ()
528 + delta
[sym
->section_index ()]);
531 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
532 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
533 Return non-zero iff any change happened. */
536 objfile_relocate1 (struct objfile
*objfile
,
537 const section_offsets
&new_offsets
)
539 section_offsets
delta (objfile
->section_offsets
.size ());
541 int something_changed
= 0;
543 for (int i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
545 delta
[i
] = new_offsets
[i
] - objfile
->section_offsets
[i
];
547 something_changed
= 1;
549 if (!something_changed
)
552 /* OK, get all the symtabs. */
553 for (compunit_symtab
*cust
: objfile
->compunits ())
555 struct blockvector
*bv
= cust
->blockvector ();
556 int block_line_section
= SECT_OFF_TEXT (objfile
);
558 if (bv
->map () != nullptr)
559 bv
->map ()->relocate (delta
[block_line_section
]);
561 for (block
*b
: bv
->blocks ())
563 b
->set_start (b
->start () + delta
[block_line_section
]);
564 b
->set_end (b
->end () + delta
[block_line_section
]);
566 for (blockrange
&r
: b
->ranges ())
568 r
.set_start (r
.start () + delta
[block_line_section
]);
569 r
.set_end (r
.end () + delta
[block_line_section
]);
572 /* We only want to iterate over the local symbols, not any
573 symbols in included symtabs. */
574 for (struct symbol
*sym
: b
->multidict_symbols ())
575 relocate_one_symbol (sym
, objfile
, delta
);
579 /* Relocate isolated symbols. */
580 for (symbol
*iter
= objfile
->template_symbols
; iter
; iter
= iter
->hash_next
)
581 relocate_one_symbol (iter
, objfile
, delta
);
583 for (int i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
584 objfile
->section_offsets
[i
] = new_offsets
[i
];
586 /* Rebuild section map next time we need it. */
587 get_objfile_pspace_data (objfile
->pspace ())->section_map_dirty
= 1;
589 /* Update the table in exec_ops, used to read memory. */
590 for (obj_section
*s
: objfile
->sections ())
592 int idx
= s
- objfile
->sections_start
;
594 exec_set_section_address (bfd_get_filename (objfile
->obfd
.get ()), idx
,
602 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
603 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
605 The number and ordering of sections does differ between the two objfiles.
606 Only their names match. Also the file offsets will differ (objfile being
607 possibly prelinked but separate_debug_objfile is probably not prelinked) but
608 the in-memory absolute address as specified by NEW_OFFSETS must match both
612 objfile_relocate (struct objfile
*objfile
,
613 const section_offsets
&new_offsets
)
617 changed
|= objfile_relocate1 (objfile
, new_offsets
);
619 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
621 if (debug_objfile
== objfile
)
624 section_addr_info objfile_addrs
625 = build_section_addr_info_from_objfile (objfile
);
627 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
628 relative ones must be already created according to debug_objfile. */
630 addr_info_make_relative (&objfile_addrs
, debug_objfile
->obfd
.get ());
632 gdb_assert (debug_objfile
->section_offsets
.size ()
633 == gdb_bfd_count_sections (debug_objfile
->obfd
.get ()));
634 section_offsets new_debug_offsets
635 (debug_objfile
->section_offsets
.size ());
636 relative_addr_info_to_section_offsets (new_debug_offsets
, objfile_addrs
);
638 changed
|= objfile_relocate1 (debug_objfile
, new_debug_offsets
);
641 /* Relocate breakpoints as necessary, after things are relocated. */
643 breakpoint_re_set ();
646 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
648 Return non-zero iff any change happened. */
651 objfile_rebase1 (struct objfile
*objfile
, CORE_ADDR slide
)
653 section_offsets
new_offsets (objfile
->section_offsets
.size (), slide
);
654 return objfile_relocate1 (objfile
, new_offsets
);
657 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
658 SEPARATE_DEBUG_OBJFILEs. */
661 objfile_rebase (struct objfile
*objfile
, CORE_ADDR slide
)
665 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
666 changed
|= objfile_rebase1 (debug_objfile
, slide
);
668 /* Relocate breakpoints as necessary, after things are relocated. */
670 breakpoint_re_set ();
673 /* See objfiles.h. */
676 objfile::has_full_symbols ()
678 return this->compunit_symtabs
!= nullptr;
681 /* See objfiles.h. */
684 objfile::has_symbols ()
686 for (::objfile
*o
: this->separate_debug_objfiles ())
687 if (o
->has_partial_symbols () || o
->has_full_symbols ())
693 /* See objfiles.h. */
696 have_partial_symbols (program_space
*pspace
)
698 for (objfile
*ofp
: pspace
->objfiles ())
699 if (ofp
->has_partial_symbols ())
705 /* See objfiles.h. */
708 have_full_symbols (program_space
*pspace
)
710 for (objfile
*ofp
: pspace
->objfiles ())
711 if (ofp
->has_full_symbols ())
718 /* See objfiles.h. */
721 objfile_purge_solibs (program_space
*pspace
)
723 for (objfile
*objf
: pspace
->objfiles_safe ())
725 /* We assume that the solib package has been purged already, or will
728 if (!(objf
->flags
& OBJF_USERLOADED
) && (objf
->flags
& OBJF_SHARED
))
733 /* See objfiles.h. */
736 have_minimal_symbols (program_space
*pspace
)
738 for (objfile
*ofp
: pspace
->objfiles ())
739 if (ofp
->per_bfd
->minimal_symbol_count
> 0)
745 /* Qsort comparison function. */
748 sort_cmp (const struct obj_section
*sect1
, const obj_section
*sect2
)
750 const CORE_ADDR sect1_addr
= sect1
->addr ();
751 const CORE_ADDR sect2_addr
= sect2
->addr ();
753 if (sect1_addr
< sect2_addr
)
755 else if (sect1_addr
> sect2_addr
)
759 /* Sections are at the same address. This could happen if
760 A) we have an objfile and a separate debuginfo.
761 B) we are confused, and have added sections without proper relocation,
762 or something like that. */
764 const struct objfile
*const objfile1
= sect1
->objfile
;
765 const struct objfile
*const objfile2
= sect2
->objfile
;
767 if (objfile1
->separate_debug_objfile
== objfile2
768 || objfile2
->separate_debug_objfile
== objfile1
)
770 /* Case A. The ordering doesn't matter: separate debuginfo files
771 will be filtered out later. */
776 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
777 triage. This section could be slow (since we iterate over all
778 objfiles in each call to sort_cmp), but this shouldn't happen
779 very often (GDB is already in a confused state; one hopes this
780 doesn't happen at all). If you discover that significant time is
781 spent in the loops below, do 'set complaints 100' and examine the
782 resulting complaints. */
783 if (objfile1
== objfile2
)
785 /* Both sections came from the same objfile. We are really
786 confused. Sort on sequence order of sections within the
787 objfile. The order of checks is important here, if we find a
788 match on SECT2 first then either SECT2 is before SECT1, or,
789 SECT2 == SECT1, in both cases we should return false. The
790 second case shouldn't occur during normal use, but std::sort
791 does check that '!(a < a)' when compiled in debug mode. */
793 for (const obj_section
*osect
: objfile1
->sections ())
796 else if (osect
== sect1
)
799 /* We should have found one of the sections before getting here. */
800 gdb_assert_not_reached ("section not found");
804 /* Sort on sequence number of the objfile in the chain. */
806 for (objfile
*objfile
: current_program_space
->objfiles ())
807 if (objfile
== objfile1
)
809 else if (objfile
== objfile2
)
812 /* We should have found one of the objfiles before getting here. */
813 gdb_assert_not_reached ("objfile not found");
818 gdb_assert_not_reached ("unexpected code path");
822 /* Select "better" obj_section to keep. We prefer the one that came from
823 the real object, rather than the one from separate debuginfo.
824 Most of the time the two sections are exactly identical, but with
825 prelinking the .rel.dyn section in the real object may have different
828 static struct obj_section
*
829 preferred_obj_section (struct obj_section
*a
, struct obj_section
*b
)
831 gdb_assert (a
->addr () == b
->addr ());
832 gdb_assert ((a
->objfile
->separate_debug_objfile
== b
->objfile
)
833 || (b
->objfile
->separate_debug_objfile
== a
->objfile
));
834 gdb_assert ((a
->objfile
->separate_debug_objfile_backlink
== b
->objfile
)
835 || (b
->objfile
->separate_debug_objfile_backlink
== a
->objfile
));
837 if (a
->objfile
->separate_debug_objfile
!= NULL
)
842 /* Return 1 if SECTION should be inserted into the section map.
843 We want to insert only non-overlay non-TLS non-empty sections. */
846 insert_section_p (const struct bfd
*abfd
,
847 const struct bfd_section
*section
)
849 const bfd_vma lma
= bfd_section_lma (section
);
851 if (overlay_debugging
&& lma
!= 0 && lma
!= bfd_section_vma (section
)
852 && (bfd_get_file_flags (abfd
) & BFD_IN_MEMORY
) == 0)
853 /* This is an overlay section. IN_MEMORY check is needed to avoid
854 discarding sections from the "system supplied DSO" (aka vdso)
855 on some Linux systems (e.g. Fedora 11). */
857 if ((bfd_section_flags (section
) & SEC_THREAD_LOCAL
) != 0)
858 /* This is a TLS section. */
860 if (bfd_section_size (section
) == 0)
862 /* This is an empty section. It has no PCs for find_pc_section (), so
863 there is no reason to insert it into the section map. */
870 /* Filter out overlapping sections where one section came from the real
871 objfile, and the other from a separate debuginfo file.
872 Return the size of table after redundant sections have been eliminated. */
875 filter_debuginfo_sections (struct obj_section
**map
, int map_size
)
879 for (i
= 0, j
= 0; i
< map_size
- 1; i
++)
881 struct obj_section
*const sect1
= map
[i
];
882 struct obj_section
*const sect2
= map
[i
+ 1];
883 const struct objfile
*const objfile1
= sect1
->objfile
;
884 const struct objfile
*const objfile2
= sect2
->objfile
;
885 const CORE_ADDR sect1_addr
= sect1
->addr ();
886 const CORE_ADDR sect2_addr
= sect2
->addr ();
888 if (sect1_addr
== sect2_addr
889 && (objfile1
->separate_debug_objfile
== objfile2
890 || objfile2
->separate_debug_objfile
== objfile1
))
892 map
[j
++] = preferred_obj_section (sect1
, sect2
);
901 gdb_assert (i
== map_size
- 1);
905 /* The map should not have shrunk to less than half the original size. */
906 gdb_assert (map_size
/ 2 <= j
);
911 /* Filter out overlapping sections, issuing a warning if any are found.
912 Overlapping sections could really be overlay sections which we didn't
913 classify as such in insert_section_p, or we could be dealing with a
917 filter_overlapping_sections (struct obj_section
**map
, int map_size
)
921 for (i
= 0, j
= 0; i
< map_size
- 1; )
926 for (k
= i
+ 1; k
< map_size
; k
++)
928 struct obj_section
*const sect1
= map
[i
];
929 struct obj_section
*const sect2
= map
[k
];
930 const CORE_ADDR sect1_addr
= sect1
->addr ();
931 const CORE_ADDR sect2_addr
= sect2
->addr ();
932 const CORE_ADDR sect1_endaddr
= sect1
->endaddr ();
934 gdb_assert (sect1_addr
<= sect2_addr
);
936 if (sect1_endaddr
<= sect2_addr
)
940 /* We have an overlap. Report it. */
942 struct objfile
*const objf1
= sect1
->objfile
;
943 struct objfile
*const objf2
= sect2
->objfile
;
945 const struct bfd_section
*const bfds1
= sect1
->the_bfd_section
;
946 const struct bfd_section
*const bfds2
= sect2
->the_bfd_section
;
948 const CORE_ADDR sect2_endaddr
= sect2
->endaddr ();
950 struct gdbarch
*const gdbarch
= objf1
->arch ();
952 complaint (_("unexpected overlap between:\n"
953 " (A) section `%s' from `%s' [%s, %s)\n"
954 " (B) section `%s' from `%s' [%s, %s).\n"
955 "Will ignore section B"),
956 bfd_section_name (bfds1
), objfile_name (objf1
),
957 paddress (gdbarch
, sect1_addr
),
958 paddress (gdbarch
, sect1_endaddr
),
959 bfd_section_name (bfds2
), objfile_name (objf2
),
960 paddress (gdbarch
, sect2_addr
),
961 paddress (gdbarch
, sect2_endaddr
));
969 gdb_assert (i
== map_size
- 1);
977 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
978 TLS, overlay and overlapping sections. */
981 update_section_map (struct program_space
*pspace
,
982 struct obj_section
***pmap
, int *pmap_size
)
984 struct objfile_pspace_info
*pspace_info
;
985 int alloc_size
, map_size
, i
;
986 struct obj_section
**map
;
988 pspace_info
= get_objfile_pspace_data (pspace
);
989 gdb_assert (pspace_info
->section_map_dirty
!= 0
990 || pspace_info
->new_objfiles_available
!= 0);
996 for (objfile
*objfile
: pspace
->objfiles ())
997 for (obj_section
*s
: objfile
->sections ())
998 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1001 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1002 if (alloc_size
== 0)
1009 map
= XNEWVEC (struct obj_section
*, alloc_size
);
1012 for (objfile
*objfile
: pspace
->objfiles ())
1013 for (obj_section
*s
: objfile
->sections ())
1014 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1017 std::sort (map
, map
+ alloc_size
, sort_cmp
);
1018 map_size
= filter_debuginfo_sections(map
, alloc_size
);
1019 map_size
= filter_overlapping_sections(map
, map_size
);
1021 if (map_size
< alloc_size
)
1022 /* Some sections were eliminated. Trim excess space. */
1023 map
= XRESIZEVEC (struct obj_section
*, map
, map_size
);
1025 gdb_assert (alloc_size
== map_size
);
1028 *pmap_size
= map_size
;
1031 /* Bsearch comparison function. */
1034 bsearch_cmp (const void *key
, const void *elt
)
1036 const CORE_ADDR pc
= *(CORE_ADDR
*) key
;
1037 const struct obj_section
*section
= *(const struct obj_section
**) elt
;
1039 if (pc
< section
->addr ())
1041 if (pc
< section
->endaddr ())
1046 /* Returns a section whose range includes PC or NULL if none found. */
1048 struct obj_section
*
1049 find_pc_section (CORE_ADDR pc
)
1051 struct objfile_pspace_info
*pspace_info
;
1052 struct obj_section
*s
, **sp
;
1054 /* Check for mapped overlay section first. */
1055 s
= find_pc_mapped_section (pc
);
1059 pspace_info
= get_objfile_pspace_data (current_program_space
);
1060 if (pspace_info
->section_map_dirty
1061 || (pspace_info
->new_objfiles_available
1062 && !pspace_info
->inhibit_updates
))
1064 update_section_map (current_program_space
,
1065 &pspace_info
->sections
,
1066 &pspace_info
->num_sections
);
1068 /* Don't need updates to section map until objfiles are added,
1069 removed or relocated. */
1070 pspace_info
->new_objfiles_available
= 0;
1071 pspace_info
->section_map_dirty
= 0;
1074 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1075 bsearch be non-NULL. */
1076 if (pspace_info
->sections
== NULL
)
1078 gdb_assert (pspace_info
->num_sections
== 0);
1082 sp
= (struct obj_section
**) bsearch (&pc
,
1083 pspace_info
->sections
,
1084 pspace_info
->num_sections
,
1085 sizeof (*pspace_info
->sections
),
1093 /* Return non-zero if PC is in a section called NAME. */
1096 pc_in_section (CORE_ADDR pc
, const char *name
)
1098 struct obj_section
*s
= find_pc_section (pc
);
1099 return (s
!= nullptr
1100 && s
->the_bfd_section
->name
!= nullptr
1101 && strcmp (s
->the_bfd_section
->name
, name
) == 0);
1104 /* See objfiles.h. */
1107 objfiles_changed (program_space
*pspace
)
1109 /* Rebuild section map next time we need it. */
1110 get_objfile_pspace_data (pspace
)->section_map_dirty
= 1;
1113 /* See comments in objfiles.h. */
1115 scoped_restore_tmpl
<int>
1116 inhibit_section_map_updates (struct program_space
*pspace
)
1118 return scoped_restore_tmpl
<int>
1119 (&get_objfile_pspace_data (pspace
)->inhibit_updates
, 1);
1122 /* See objfiles.h. */
1125 is_addr_in_objfile (CORE_ADDR addr
, const struct objfile
*objfile
)
1127 if (objfile
== NULL
)
1130 for (obj_section
*osect
: objfile
->sections ())
1132 if (section_is_overlay (osect
) && !section_is_mapped (osect
))
1135 if (osect
->contains (addr
))
1141 /* See objfiles.h. */
1144 shared_objfile_contains_address_p (struct program_space
*pspace
,
1147 for (objfile
*objfile
: pspace
->objfiles ())
1149 if ((objfile
->flags
& OBJF_SHARED
) != 0
1150 && is_addr_in_objfile (address
, objfile
))
1157 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1158 gdbarch method. It is equivalent to use the objfiles iterable,
1159 searching the objfiles in the order they are stored internally,
1160 ignoring CURRENT_OBJFILE.
1162 On most platforms, it should be close enough to doing the best
1163 we can without some knowledge specific to the architecture. */
1166 default_iterate_over_objfiles_in_search_order
1167 (gdbarch
*gdbarch
, iterate_over_objfiles_in_search_order_cb_ftype cb
,
1168 objfile
*current_objfile
)
1170 for (objfile
*objfile
: current_program_space
->objfiles ())
1175 /* See objfiles.h. */
1178 objfile_name (const struct objfile
*objfile
)
1180 if (objfile
->obfd
!= NULL
)
1181 return bfd_get_filename (objfile
->obfd
.get ());
1183 return objfile
->original_name
;
1186 /* See objfiles.h. */
1189 objfile_filename (const struct objfile
*objfile
)
1191 if (objfile
->obfd
!= NULL
)
1192 return bfd_get_filename (objfile
->obfd
.get ());
1197 /* See objfiles.h. */
1200 objfile_debug_name (const struct objfile
*objfile
)
1202 return lbasename (objfile
->original_name
);
1205 /* See objfiles.h. */
1208 objfile_flavour_name (struct objfile
*objfile
)
1210 if (objfile
->obfd
!= NULL
)
1211 return bfd_flavour_name (bfd_get_flavour (objfile
->obfd
.get ()));
1215 /* See objfiles.h. */
1218 objfile_int_type (struct objfile
*of
, int size_in_bytes
, bool unsigned_p
)
1220 struct type
*int_type
;
1222 /* Helper macro to examine the various builtin types. */
1223 #define TRY_TYPE(F) \
1224 int_type = (unsigned_p \
1225 ? builtin_type (of)->builtin_unsigned_ ## F \
1226 : builtin_type (of)->builtin_ ## F); \
1227 if (int_type != NULL && int_type->length () == size_in_bytes) \
1234 TRY_TYPE (long_long
);
1238 gdb_assert_not_reached ("unable to find suitable integer type");