1 /* GDB routines for manipulating objfiles.
3 Copyright (C) 1992-2024 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"
39 #include "breakpoint.h"
41 #include "dictionary.h"
44 #include "arch-utils.h"
46 #include "observable.h"
47 #include "complaints.h"
50 #include "gdbsupport/pathstuff.h"
54 /* Externally visible variables that are owned by this module.
55 See declarations in objfile.h for more info. */
57 struct objfile_pspace_info
59 objfile_pspace_info () = default;
60 ~objfile_pspace_info ();
62 struct obj_section
**sections
= nullptr;
65 /* Nonzero if object files have been added since the section map
67 int new_objfiles_available
= 0;
69 /* Nonzero if the section map MUST be updated before use. */
70 int section_map_dirty
= 0;
72 /* Nonzero if section map updates should be inhibited if possible. */
73 int inhibit_updates
= 0;
76 /* Per-program-space data key. */
77 static const registry
<program_space
>::key
<objfile_pspace_info
>
80 objfile_pspace_info::~objfile_pspace_info ()
85 /* Get the current svr4 data. If none is found yet, add it now. This
86 function always returns a valid object. */
88 static struct objfile_pspace_info
*
89 get_objfile_pspace_data (struct program_space
*pspace
)
91 struct objfile_pspace_info
*info
;
93 info
= objfiles_pspace_data
.get (pspace
);
95 info
= objfiles_pspace_data
.emplace (pspace
);
102 /* Per-BFD data key. */
104 static const registry
<bfd
>::key
<objfile_per_bfd_storage
> objfiles_bfd_data
;
106 objfile_per_bfd_storage::~objfile_per_bfd_storage ()
110 /* Create the per-BFD storage object for OBJFILE. If ABFD is not
111 NULL, and it already has a per-BFD storage object, use that.
112 Otherwise, allocate a new per-BFD storage object. */
115 set_objfile_per_bfd (struct objfile
*objfile
)
117 bfd
*abfd
= objfile
->obfd
.get ();
118 struct objfile_per_bfd_storage
*storage
= NULL
;
121 storage
= objfiles_bfd_data
.get (abfd
);
125 storage
= new objfile_per_bfd_storage (abfd
);
126 /* If the object requires gdb to do relocations, we simply fall
127 back to not sharing data across users. These cases are rare
128 enough that this seems reasonable. */
129 if (abfd
!= NULL
&& !gdb_bfd_requires_relocations (abfd
))
130 objfiles_bfd_data
.set (abfd
, storage
);
132 objfile
->per_bfd_storage
.reset (storage
);
134 /* Look up the gdbarch associated with the BFD. */
136 storage
->gdbarch
= gdbarch_from_bfd (abfd
);
139 objfile
->per_bfd
= storage
;
142 /* Set the objfile's per-BFD notion of the "main" name and
146 set_objfile_main_name (struct objfile
*objfile
,
147 const char *name
, enum language lang
)
149 if (objfile
->per_bfd
->name_of_main
== NULL
150 || strcmp (objfile
->per_bfd
->name_of_main
, name
) != 0)
151 objfile
->per_bfd
->name_of_main
152 = obstack_strdup (&objfile
->per_bfd
->storage_obstack
, name
);
153 objfile
->per_bfd
->language_of_main
= lang
;
156 /* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
157 Must not be called more than once for each BLOCK. */
160 objfile_register_static_link (struct objfile
*objfile
,
161 const struct block
*block
,
162 const struct dynamic_prop
*static_link
)
164 /* Enter the mapping and make sure it's the first mapping for this
166 bool inserted
= objfile
->static_links
.emplace (block
, static_link
).second
;
167 gdb_assert (inserted
);
170 /* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
173 const struct dynamic_prop
*
174 objfile_lookup_static_link (struct objfile
*objfile
,
175 const struct block
*block
)
177 if (auto iter
= objfile
->static_links
.find (block
);
178 iter
!= objfile
->static_links
.end ())
186 /* Build up the section table that the objfile references. The
187 objfile contains pointers to the start of the table
188 (objfile->sections) and to the first location after the end of the
189 table (objfile->sections_end). */
192 add_to_objfile_sections (struct bfd
*abfd
, struct bfd_section
*asect
,
193 struct objfile
*objfile
, int force
)
195 struct obj_section
*section
;
201 aflag
= bfd_section_flags (asect
);
202 if (!(aflag
& SEC_ALLOC
))
206 section
= &objfile
->sections_start
[gdb_bfd_section_index (abfd
, asect
)];
207 section
->objfile
= objfile
;
208 section
->the_bfd_section
= asect
;
209 section
->ovly_mapped
= 0;
212 /* Builds a section table for OBJFILE.
214 Note that the OFFSET and OVLY_MAPPED in each table entry are
215 initialized to zero. */
218 build_objfile_section_table (struct objfile
*objfile
)
220 int count
= gdb_bfd_count_sections (objfile
->obfd
.get ());
222 objfile
->sections_start
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
225 objfile
->sections_end
= (objfile
->sections_start
+ count
);
226 for (asection
*sect
: gdb_bfd_sections (objfile
->obfd
))
227 add_to_objfile_sections (objfile
->obfd
.get (), sect
, objfile
, 0);
229 /* See gdb_bfd_section_index. */
230 add_to_objfile_sections (objfile
->obfd
.get (), bfd_com_section_ptr
,
232 add_to_objfile_sections (objfile
->obfd
.get (), bfd_und_section_ptr
,
234 add_to_objfile_sections (objfile
->obfd
.get (), bfd_abs_section_ptr
,
236 add_to_objfile_sections (objfile
->obfd
.get (), bfd_ind_section_ptr
,
240 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
241 initialize the new objfile as best we can and link it into the list
242 of all known objfiles.
244 NAME should contain original non-canonicalized filename or other
245 identifier as entered by user. If there is no better source use
246 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
247 NAME content is copied into returned objfile.
249 The FLAGS word contains various bits (OBJF_*) that can be taken as
250 requests for specific operations. Other bits like OBJF_SHARED are
251 simply copied through to the new objfile flags member. */
253 objfile::objfile (gdb_bfd_ref_ptr bfd_
, program_space
*pspace
,
254 const char *name
, objfile_flags flags_
)
257 obfd (std::move (bfd_
))
259 const char *expanded_name
;
261 std::string name_holder
;
264 gdb_assert (obfd
== nullptr);
265 gdb_assert ((flags
& OBJF_NOT_FILENAME
) != 0);
266 expanded_name
= "<<anonymous objfile>>";
268 else if ((flags
& OBJF_NOT_FILENAME
) != 0
269 || is_target_filename (name
))
270 expanded_name
= name
;
273 name_holder
= gdb_abspath (name
);
274 expanded_name
= name_holder
.c_str ();
276 original_name
= obstack_strdup (&objfile_obstack
, expanded_name
);
278 /* Update the per-objfile information that comes from the bfd, ensuring
279 that any data that is reference is saved in the per-objfile data
284 mtime
= gdb_bfd_get_mtime (obfd
.get ());
286 /* Build section table. */
287 build_objfile_section_table (this);
290 set_objfile_per_bfd (this);
293 /* See objfiles.h. */
296 entry_point_address_query (program_space
*pspace
, CORE_ADDR
*entry_p
)
298 objfile
*objf
= pspace
->symfile_object_file
;
299 if (objf
== NULL
|| !objf
->per_bfd
->ei
.entry_point_p
)
302 int idx
= objf
->per_bfd
->ei
.the_bfd_section_index
;
303 *entry_p
= objf
->per_bfd
->ei
.entry_point
+ objf
->section_offsets
[idx
];
308 /* See objfiles.h. */
311 entry_point_address (program_space
*pspace
)
315 if (!entry_point_address_query (pspace
, &retval
))
316 error (_("Entry point address is not known."));
321 separate_debug_iterator
&
322 separate_debug_iterator::operator++ ()
324 gdb_assert (m_objfile
!= nullptr);
328 /* If any, return the first child. */
329 res
= m_objfile
->separate_debug_objfile
;
336 /* Common case where there is no separate debug objfile. */
337 if (m_objfile
== m_parent
)
343 /* Return the brother if any. Note that we don't iterate on brothers of
345 res
= m_objfile
->separate_debug_objfile_link
;
352 for (res
= m_objfile
->separate_debug_objfile_backlink
;
354 res
= res
->separate_debug_objfile_backlink
)
356 gdb_assert (res
!= nullptr);
357 if (res
->separate_debug_objfile_link
!= nullptr)
359 m_objfile
= res
->separate_debug_objfile_link
;
367 /* Add OBJFILE as a separate debug objfile of PARENT. */
370 add_separate_debug_objfile (struct objfile
*objfile
, struct objfile
*parent
)
372 gdb_assert (objfile
&& parent
);
374 /* Must not be already in a list. */
375 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
376 gdb_assert (objfile
->separate_debug_objfile_link
== NULL
);
377 gdb_assert (objfile
->separate_debug_objfile
== NULL
);
378 gdb_assert (parent
->separate_debug_objfile_backlink
== NULL
);
379 gdb_assert (parent
->separate_debug_objfile_link
== NULL
);
381 objfile
->separate_debug_objfile_backlink
= parent
;
382 objfile
->separate_debug_objfile_link
= parent
->separate_debug_objfile
;
383 parent
->separate_debug_objfile
= objfile
;
386 /* See objfiles.h. */
389 objfile::make (gdb_bfd_ref_ptr bfd_
, program_space
*pspace
, const char *name_
,
390 objfile_flags flags_
, objfile
*parent
)
392 objfile
*result
= new objfile (std::move (bfd_
), pspace
, name_
, flags_
);
393 if (parent
!= nullptr)
394 add_separate_debug_objfile (result
, parent
);
396 pspace
->add_objfile (std::unique_ptr
<objfile
> (result
), parent
);
398 /* Rebuild section map next time we need it. */
399 get_objfile_pspace_data (pspace
)->new_objfiles_available
= 1;
404 /* See objfiles.h. */
409 this->pspace ()->remove_objfile (this);
412 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
416 free_objfile_separate_debug (struct objfile
*objfile
)
418 struct objfile
*child
;
420 for (child
= objfile
->separate_debug_objfile
; child
;)
422 struct objfile
*next_child
= child
->separate_debug_objfile_link
;
428 /* Destroy an objfile and all the symtabs and psymtabs under it. */
432 /* First notify observers that this objfile is about to be freed. */
433 gdb::observers::free_objfile
.notify (this);
435 /* Free all separate debug objfiles. */
436 free_objfile_separate_debug (this);
438 if (separate_debug_objfile_backlink
)
440 /* We freed the separate debug file, make sure the base objfile
441 doesn't reference it. */
442 struct objfile
*child
;
444 child
= separate_debug_objfile_backlink
->separate_debug_objfile
;
448 /* THIS is the first child. */
449 separate_debug_objfile_backlink
->separate_debug_objfile
=
450 separate_debug_objfile_link
;
454 /* Find THIS in the list. */
457 if (child
->separate_debug_objfile_link
== this)
459 child
->separate_debug_objfile_link
=
460 separate_debug_objfile_link
;
463 child
= child
->separate_debug_objfile_link
;
469 /* Remove any references to this objfile in the global value
471 preserve_values (this);
473 /* It still may reference data modules have associated with the objfile and
474 the symbol file data. */
475 forget_cached_source_info ();
476 for (compunit_symtab
*cu
: compunits ())
479 breakpoint_free_objfile (this);
480 btrace_free_objfile (this);
482 /* First do any symbol file specific actions required when we are
483 finished with a particular symbol file. Note that if the objfile
484 is using reusable symbol information (via mmalloc) then each of
485 these routines is responsible for doing the correct thing, either
486 freeing things which are valid only during this particular gdb
487 execution, or leaving them to be reused during the next one. */
490 (*sf
->sym_finish
) (this);
492 /* Before the symbol table code was redone to make it easier to
493 selectively load and remove information particular to a specific
494 linkage unit, gdb used to do these things whenever the monolithic
495 symbol table was blown away. How much still needs to be done
496 is unknown, but we play it safe for now and keep each action until
497 it is shown to be no longer needed. */
499 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
500 for example), so we need to call this here. */
501 clear_pc_function_cache ();
503 /* Check to see if the current_source_symtab belongs to this objfile,
504 and if so, call clear_current_source_symtab_and_line. */
505 clear_current_source_symtab_and_line (this);
507 /* Rebuild section map next time we need it. */
508 auto info
= objfiles_pspace_data
.get (pspace ());
510 info
->section_map_dirty
= 1;
514 /* A helper function for objfile_relocate1 that relocates a single
518 relocate_one_symbol (struct symbol
*sym
, struct objfile
*objfile
,
519 const section_offsets
&delta
)
521 /* The RS6000 code from which this was taken skipped
522 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
523 But I'm leaving out that test, on the theory that
524 they can't possibly pass the tests below. */
525 if ((sym
->aclass () == LOC_LABEL
526 || sym
->aclass () == LOC_STATIC
)
527 && sym
->section_index () >= 0)
528 sym
->set_value_address (sym
->value_address ()
529 + delta
[sym
->section_index ()]);
532 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
533 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
534 Return non-zero iff any change happened. */
537 objfile_relocate1 (struct objfile
*objfile
,
538 const section_offsets
&new_offsets
)
540 section_offsets
delta (objfile
->section_offsets
.size ());
542 int something_changed
= 0;
544 for (int i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
546 delta
[i
] = new_offsets
[i
] - objfile
->section_offsets
[i
];
548 something_changed
= 1;
550 if (!something_changed
)
553 /* OK, get all the symtabs. */
554 for (compunit_symtab
*cust
: objfile
->compunits ())
556 struct blockvector
*bv
= cust
->blockvector ();
557 int block_line_section
= SECT_OFF_TEXT (objfile
);
559 if (bv
->map () != nullptr)
560 bv
->map ()->relocate (delta
[block_line_section
]);
562 for (block
*b
: bv
->blocks ())
564 b
->set_start (b
->start () + delta
[block_line_section
]);
565 b
->set_end (b
->end () + delta
[block_line_section
]);
567 for (blockrange
&r
: b
->ranges ())
569 r
.set_start (r
.start () + delta
[block_line_section
]);
570 r
.set_end (r
.end () + delta
[block_line_section
]);
573 /* We only want to iterate over the local symbols, not any
574 symbols in included symtabs. */
575 for (struct symbol
*sym
: b
->multidict_symbols ())
576 relocate_one_symbol (sym
, objfile
, delta
);
580 /* Relocate isolated symbols. */
581 for (symbol
*iter
= objfile
->template_symbols
; iter
; iter
= iter
->hash_next
)
582 relocate_one_symbol (iter
, objfile
, delta
);
584 for (int i
= 0; i
< objfile
->section_offsets
.size (); ++i
)
585 objfile
->section_offsets
[i
] = new_offsets
[i
];
587 /* Rebuild section map next time we need it. */
588 get_objfile_pspace_data (objfile
->pspace ())->section_map_dirty
= 1;
590 /* Update the table in exec_ops, used to read memory. */
591 for (obj_section
*s
: objfile
->sections ())
593 int idx
= s
- objfile
->sections_start
;
595 exec_set_section_address (bfd_get_filename (objfile
->obfd
.get ()), idx
,
603 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
604 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
606 The number and ordering of sections does differ between the two objfiles.
607 Only their names match. Also the file offsets will differ (objfile being
608 possibly prelinked but separate_debug_objfile is probably not prelinked) but
609 the in-memory absolute address as specified by NEW_OFFSETS must match both
613 objfile_relocate (struct objfile
*objfile
,
614 const section_offsets
&new_offsets
)
618 changed
|= objfile_relocate1 (objfile
, new_offsets
);
620 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
622 if (debug_objfile
== objfile
)
625 section_addr_info objfile_addrs
626 = build_section_addr_info_from_objfile (objfile
);
628 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
629 relative ones must be already created according to debug_objfile. */
631 addr_info_make_relative (&objfile_addrs
, debug_objfile
->obfd
.get ());
633 gdb_assert (debug_objfile
->section_offsets
.size ()
634 == gdb_bfd_count_sections (debug_objfile
->obfd
.get ()));
635 section_offsets new_debug_offsets
636 (debug_objfile
->section_offsets
.size ());
637 relative_addr_info_to_section_offsets (new_debug_offsets
, objfile_addrs
);
639 changed
|= objfile_relocate1 (debug_objfile
, new_debug_offsets
);
642 /* Relocate breakpoints as necessary, after things are relocated. */
644 breakpoint_re_set ();
647 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
649 Return non-zero iff any change happened. */
652 objfile_rebase1 (struct objfile
*objfile
, CORE_ADDR slide
)
654 section_offsets
new_offsets (objfile
->section_offsets
.size (), slide
);
655 return objfile_relocate1 (objfile
, new_offsets
);
658 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
659 SEPARATE_DEBUG_OBJFILEs. */
662 objfile_rebase (struct objfile
*objfile
, CORE_ADDR slide
)
666 for (::objfile
*debug_objfile
: objfile
->separate_debug_objfiles ())
667 changed
|= objfile_rebase1 (debug_objfile
, slide
);
669 /* Relocate breakpoints as necessary, after things are relocated. */
671 breakpoint_re_set ();
674 /* See objfiles.h. */
677 objfile_has_full_symbols (objfile
*objfile
)
679 return objfile
->compunit_symtabs
!= nullptr;
682 /* See objfiles.h. */
685 objfile_has_symbols (objfile
*objfile
)
687 for (::objfile
*o
: objfile
->separate_debug_objfiles ())
688 if (o
->has_partial_symbols () || objfile_has_full_symbols (o
))
694 /* See objfiles.h. */
697 have_partial_symbols (program_space
*pspace
)
699 for (objfile
*ofp
: pspace
->objfiles ())
700 if (ofp
->has_partial_symbols ())
706 /* See objfiles.h. */
709 have_full_symbols (program_space
*pspace
)
711 for (objfile
*ofp
: pspace
->objfiles ())
712 if (objfile_has_full_symbols (ofp
))
719 /* See objfiles.h. */
722 objfile_purge_solibs (program_space
*pspace
)
724 for (objfile
*objf
: pspace
->objfiles_safe ())
726 /* We assume that the solib package has been purged already, or will
729 if (!(objf
->flags
& OBJF_USERLOADED
) && (objf
->flags
& OBJF_SHARED
))
734 /* See objfiles.h. */
737 have_minimal_symbols (program_space
*pspace
)
739 for (objfile
*ofp
: pspace
->objfiles ())
740 if (ofp
->per_bfd
->minimal_symbol_count
> 0)
746 /* Qsort comparison function. */
749 sort_cmp (const struct obj_section
*sect1
, const obj_section
*sect2
)
751 const CORE_ADDR sect1_addr
= sect1
->addr ();
752 const CORE_ADDR sect2_addr
= sect2
->addr ();
754 if (sect1_addr
< sect2_addr
)
756 else if (sect1_addr
> sect2_addr
)
760 /* Sections are at the same address. This could happen if
761 A) we have an objfile and a separate debuginfo.
762 B) we are confused, and have added sections without proper relocation,
763 or something like that. */
765 const struct objfile
*const objfile1
= sect1
->objfile
;
766 const struct objfile
*const objfile2
= sect2
->objfile
;
768 if (objfile1
->separate_debug_objfile
== objfile2
769 || objfile2
->separate_debug_objfile
== objfile1
)
771 /* Case A. The ordering doesn't matter: separate debuginfo files
772 will be filtered out later. */
777 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
778 triage. This section could be slow (since we iterate over all
779 objfiles in each call to sort_cmp), but this shouldn't happen
780 very often (GDB is already in a confused state; one hopes this
781 doesn't happen at all). If you discover that significant time is
782 spent in the loops below, do 'set complaints 100' and examine the
783 resulting complaints. */
784 if (objfile1
== objfile2
)
786 /* Both sections came from the same objfile. We are really
787 confused. Sort on sequence order of sections within the
788 objfile. The order of checks is important here, if we find a
789 match on SECT2 first then either SECT2 is before SECT1, or,
790 SECT2 == SECT1, in both cases we should return false. The
791 second case shouldn't occur during normal use, but std::sort
792 does check that '!(a < a)' when compiled in debug mode. */
794 for (const obj_section
*osect
: objfile1
->sections ())
797 else if (osect
== sect1
)
800 /* We should have found one of the sections before getting here. */
801 gdb_assert_not_reached ("section not found");
805 /* Sort on sequence number of the objfile in the chain. */
807 for (objfile
*objfile
: current_program_space
->objfiles ())
808 if (objfile
== objfile1
)
810 else if (objfile
== objfile2
)
813 /* We should have found one of the objfiles before getting here. */
814 gdb_assert_not_reached ("objfile not found");
819 gdb_assert_not_reached ("unexpected code path");
823 /* Select "better" obj_section to keep. We prefer the one that came from
824 the real object, rather than the one from separate debuginfo.
825 Most of the time the two sections are exactly identical, but with
826 prelinking the .rel.dyn section in the real object may have different
829 static struct obj_section
*
830 preferred_obj_section (struct obj_section
*a
, struct obj_section
*b
)
832 gdb_assert (a
->addr () == b
->addr ());
833 gdb_assert ((a
->objfile
->separate_debug_objfile
== b
->objfile
)
834 || (b
->objfile
->separate_debug_objfile
== a
->objfile
));
835 gdb_assert ((a
->objfile
->separate_debug_objfile_backlink
== b
->objfile
)
836 || (b
->objfile
->separate_debug_objfile_backlink
== a
->objfile
));
838 if (a
->objfile
->separate_debug_objfile
!= NULL
)
843 /* Return 1 if SECTION should be inserted into the section map.
844 We want to insert only non-overlay non-TLS non-empty sections. */
847 insert_section_p (const struct bfd
*abfd
,
848 const struct bfd_section
*section
)
850 const bfd_vma lma
= bfd_section_lma (section
);
852 if (overlay_debugging
&& lma
!= 0 && lma
!= bfd_section_vma (section
)
853 && (bfd_get_file_flags (abfd
) & BFD_IN_MEMORY
) == 0)
854 /* This is an overlay section. IN_MEMORY check is needed to avoid
855 discarding sections from the "system supplied DSO" (aka vdso)
856 on some Linux systems (e.g. Fedora 11). */
858 if ((bfd_section_flags (section
) & SEC_THREAD_LOCAL
) != 0)
859 /* This is a TLS section. */
861 if (bfd_section_size (section
) == 0)
863 /* This is an empty section. It has no PCs for find_pc_section (), so
864 there is no reason to insert it into the section map. */
871 /* Filter out overlapping sections where one section came from the real
872 objfile, and the other from a separate debuginfo file.
873 Return the size of table after redundant sections have been eliminated. */
876 filter_debuginfo_sections (struct obj_section
**map
, int map_size
)
880 for (i
= 0, j
= 0; i
< map_size
- 1; i
++)
882 struct obj_section
*const sect1
= map
[i
];
883 struct obj_section
*const sect2
= map
[i
+ 1];
884 const struct objfile
*const objfile1
= sect1
->objfile
;
885 const struct objfile
*const objfile2
= sect2
->objfile
;
886 const CORE_ADDR sect1_addr
= sect1
->addr ();
887 const CORE_ADDR sect2_addr
= sect2
->addr ();
889 if (sect1_addr
== sect2_addr
890 && (objfile1
->separate_debug_objfile
== objfile2
891 || objfile2
->separate_debug_objfile
== objfile1
))
893 map
[j
++] = preferred_obj_section (sect1
, sect2
);
902 gdb_assert (i
== map_size
- 1);
906 /* The map should not have shrunk to less than half the original size. */
907 gdb_assert (map_size
/ 2 <= j
);
912 /* Filter out overlapping sections, issuing a warning if any are found.
913 Overlapping sections could really be overlay sections which we didn't
914 classify as such in insert_section_p, or we could be dealing with a
918 filter_overlapping_sections (struct obj_section
**map
, int map_size
)
922 for (i
= 0, j
= 0; i
< map_size
- 1; )
927 for (k
= i
+ 1; k
< map_size
; k
++)
929 struct obj_section
*const sect1
= map
[i
];
930 struct obj_section
*const sect2
= map
[k
];
931 const CORE_ADDR sect1_addr
= sect1
->addr ();
932 const CORE_ADDR sect2_addr
= sect2
->addr ();
933 const CORE_ADDR sect1_endaddr
= sect1
->endaddr ();
935 gdb_assert (sect1_addr
<= sect2_addr
);
937 if (sect1_endaddr
<= sect2_addr
)
941 /* We have an overlap. Report it. */
943 struct objfile
*const objf1
= sect1
->objfile
;
944 struct objfile
*const objf2
= sect2
->objfile
;
946 const struct bfd_section
*const bfds1
= sect1
->the_bfd_section
;
947 const struct bfd_section
*const bfds2
= sect2
->the_bfd_section
;
949 const CORE_ADDR sect2_endaddr
= sect2
->endaddr ();
951 struct gdbarch
*const gdbarch
= objf1
->arch ();
953 complaint (_("unexpected overlap between:\n"
954 " (A) section `%s' from `%s' [%s, %s)\n"
955 " (B) section `%s' from `%s' [%s, %s).\n"
956 "Will ignore section B"),
957 bfd_section_name (bfds1
), objfile_name (objf1
),
958 paddress (gdbarch
, sect1_addr
),
959 paddress (gdbarch
, sect1_endaddr
),
960 bfd_section_name (bfds2
), objfile_name (objf2
),
961 paddress (gdbarch
, sect2_addr
),
962 paddress (gdbarch
, sect2_endaddr
));
970 gdb_assert (i
== map_size
- 1);
978 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
979 TLS, overlay and overlapping sections. */
982 update_section_map (struct program_space
*pspace
,
983 struct obj_section
***pmap
, int *pmap_size
)
985 struct objfile_pspace_info
*pspace_info
;
986 int alloc_size
, map_size
, i
;
987 struct obj_section
**map
;
989 pspace_info
= get_objfile_pspace_data (pspace
);
990 gdb_assert (pspace_info
->section_map_dirty
!= 0
991 || pspace_info
->new_objfiles_available
!= 0);
997 for (objfile
*objfile
: pspace
->objfiles ())
998 for (obj_section
*s
: objfile
->sections ())
999 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1002 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1003 if (alloc_size
== 0)
1010 map
= XNEWVEC (struct obj_section
*, alloc_size
);
1013 for (objfile
*objfile
: pspace
->objfiles ())
1014 for (obj_section
*s
: objfile
->sections ())
1015 if (insert_section_p (objfile
->obfd
.get (), s
->the_bfd_section
))
1018 std::sort (map
, map
+ alloc_size
, sort_cmp
);
1019 map_size
= filter_debuginfo_sections(map
, alloc_size
);
1020 map_size
= filter_overlapping_sections(map
, map_size
);
1022 if (map_size
< alloc_size
)
1023 /* Some sections were eliminated. Trim excess space. */
1024 map
= XRESIZEVEC (struct obj_section
*, map
, map_size
);
1026 gdb_assert (alloc_size
== map_size
);
1029 *pmap_size
= map_size
;
1032 /* Bsearch comparison function. */
1035 bsearch_cmp (const void *key
, const void *elt
)
1037 const CORE_ADDR pc
= *(CORE_ADDR
*) key
;
1038 const struct obj_section
*section
= *(const struct obj_section
**) elt
;
1040 if (pc
< section
->addr ())
1042 if (pc
< section
->endaddr ())
1047 /* Returns a section whose range includes PC or NULL if none found. */
1049 struct obj_section
*
1050 find_pc_section (CORE_ADDR pc
)
1052 struct objfile_pspace_info
*pspace_info
;
1053 struct obj_section
*s
, **sp
;
1055 /* Check for mapped overlay section first. */
1056 s
= find_pc_mapped_section (pc
);
1060 pspace_info
= get_objfile_pspace_data (current_program_space
);
1061 if (pspace_info
->section_map_dirty
1062 || (pspace_info
->new_objfiles_available
1063 && !pspace_info
->inhibit_updates
))
1065 update_section_map (current_program_space
,
1066 &pspace_info
->sections
,
1067 &pspace_info
->num_sections
);
1069 /* Don't need updates to section map until objfiles are added,
1070 removed or relocated. */
1071 pspace_info
->new_objfiles_available
= 0;
1072 pspace_info
->section_map_dirty
= 0;
1075 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1076 bsearch be non-NULL. */
1077 if (pspace_info
->sections
== NULL
)
1079 gdb_assert (pspace_info
->num_sections
== 0);
1083 sp
= (struct obj_section
**) bsearch (&pc
,
1084 pspace_info
->sections
,
1085 pspace_info
->num_sections
,
1086 sizeof (*pspace_info
->sections
),
1094 /* Return non-zero if PC is in a section called NAME. */
1097 pc_in_section (CORE_ADDR pc
, const char *name
)
1099 struct obj_section
*s
= find_pc_section (pc
);
1100 return (s
!= nullptr
1101 && s
->the_bfd_section
->name
!= nullptr
1102 && strcmp (s
->the_bfd_section
->name
, name
) == 0);
1105 /* See objfiles.h. */
1108 objfiles_changed (program_space
*pspace
)
1110 /* Rebuild section map next time we need it. */
1111 get_objfile_pspace_data (pspace
)->section_map_dirty
= 1;
1114 /* See comments in objfiles.h. */
1116 scoped_restore_tmpl
<int>
1117 inhibit_section_map_updates (struct program_space
*pspace
)
1119 return scoped_restore_tmpl
<int>
1120 (&get_objfile_pspace_data (pspace
)->inhibit_updates
, 1);
1123 /* See objfiles.h. */
1126 is_addr_in_objfile (CORE_ADDR addr
, const struct objfile
*objfile
)
1128 if (objfile
== NULL
)
1131 for (obj_section
*osect
: objfile
->sections ())
1133 if (section_is_overlay (osect
) && !section_is_mapped (osect
))
1136 if (osect
->contains (addr
))
1142 /* See objfiles.h. */
1145 shared_objfile_contains_address_p (struct program_space
*pspace
,
1148 for (objfile
*objfile
: pspace
->objfiles ())
1150 if ((objfile
->flags
& OBJF_SHARED
) != 0
1151 && is_addr_in_objfile (address
, objfile
))
1158 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1159 gdbarch method. It is equivalent to use the objfiles iterable,
1160 searching the objfiles in the order they are stored internally,
1161 ignoring CURRENT_OBJFILE.
1163 On most platforms, it should be close enough to doing the best
1164 we can without some knowledge specific to the architecture. */
1167 default_iterate_over_objfiles_in_search_order
1168 (gdbarch
*gdbarch
, iterate_over_objfiles_in_search_order_cb_ftype cb
,
1169 objfile
*current_objfile
)
1171 for (objfile
*objfile
: current_program_space
->objfiles ())
1176 /* See objfiles.h. */
1179 objfile_name (const struct objfile
*objfile
)
1181 if (objfile
->obfd
!= NULL
)
1182 return bfd_get_filename (objfile
->obfd
.get ());
1184 return objfile
->original_name
;
1187 /* See objfiles.h. */
1190 objfile_filename (const struct objfile
*objfile
)
1192 if (objfile
->obfd
!= NULL
)
1193 return bfd_get_filename (objfile
->obfd
.get ());
1198 /* See objfiles.h. */
1201 objfile_debug_name (const struct objfile
*objfile
)
1203 return lbasename (objfile
->original_name
);
1206 /* See objfiles.h. */
1209 objfile_flavour_name (struct objfile
*objfile
)
1211 if (objfile
->obfd
!= NULL
)
1212 return bfd_flavour_name (bfd_get_flavour (objfile
->obfd
.get ()));
1216 /* See objfiles.h. */
1219 objfile_int_type (struct objfile
*of
, int size_in_bytes
, bool unsigned_p
)
1221 struct type
*int_type
;
1223 /* Helper macro to examine the various builtin types. */
1224 #define TRY_TYPE(F) \
1225 int_type = (unsigned_p \
1226 ? builtin_type (of)->builtin_unsigned_ ## F \
1227 : builtin_type (of)->builtin_ ## F); \
1228 if (int_type != NULL && int_type->length () == size_in_bytes) \
1235 TRY_TYPE (long_long
);
1239 gdb_assert_not_reached ("unable to find suitable integer type");