arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / objfiles.c
blob555195dc61f9f88aa456ae473d3102a8a0f6b4a4
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. */
25 #include "bfd.h"
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "target.h"
30 #include "expression.h"
31 #include "parser-defs.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include "gdbsupport/gdb_obstack.h"
37 #include "hashtab.h"
39 #include "breakpoint.h"
40 #include "block.h"
41 #include "dictionary.h"
42 #include "source.h"
43 #include "addrmap.h"
44 #include "arch-utils.h"
45 #include "exec.h"
46 #include "observable.h"
47 #include "complaints.h"
48 #include "gdb_bfd.h"
49 #include "btrace.h"
50 #include "gdbsupport/pathstuff.h"
52 #include <algorithm>
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;
63 int num_sections = 0;
65 /* Nonzero if object files have been added since the section map
66 was last updated. */
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>
78 objfiles_pspace_data;
80 objfile_pspace_info::~objfile_pspace_info ()
82 xfree (sections);
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);
94 if (info == NULL)
95 info = objfiles_pspace_data.emplace (pspace);
97 return info;
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. */
114 void
115 set_objfile_per_bfd (struct objfile *objfile)
117 bfd *abfd = objfile->obfd.get ();
118 struct objfile_per_bfd_storage *storage = NULL;
120 if (abfd != NULL)
121 storage = objfiles_bfd_data.get (abfd);
123 if (storage == NULL)
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);
131 else
132 objfile->per_bfd_storage.reset (storage);
134 /* Look up the gdbarch associated with the BFD. */
135 if (abfd != NULL)
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
143 language. */
145 void
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 /* Helper structure to map blocks to static link properties in hash tables. */
158 struct static_link_htab_entry
160 const struct block *block;
161 const struct dynamic_prop *static_link;
164 /* Return a hash code for struct static_link_htab_entry *P. */
166 static hashval_t
167 static_link_htab_entry_hash (const void *p)
169 const struct static_link_htab_entry *e
170 = (const struct static_link_htab_entry *) p;
172 return htab_hash_pointer (e->block);
175 /* Return whether P1 an P2 (pointers to struct static_link_htab_entry) are
176 mappings for the same block. */
178 static int
179 static_link_htab_entry_eq (const void *p1, const void *p2)
181 const struct static_link_htab_entry *e1
182 = (const struct static_link_htab_entry *) p1;
183 const struct static_link_htab_entry *e2
184 = (const struct static_link_htab_entry *) p2;
186 return e1->block == e2->block;
189 /* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
190 Must not be called more than once for each BLOCK. */
192 void
193 objfile_register_static_link (struct objfile *objfile,
194 const struct block *block,
195 const struct dynamic_prop *static_link)
197 void **slot;
198 struct static_link_htab_entry lookup_entry;
199 struct static_link_htab_entry *entry;
201 if (objfile->static_links == NULL)
202 objfile->static_links.reset (htab_create_alloc
203 (1, &static_link_htab_entry_hash, static_link_htab_entry_eq, NULL,
204 xcalloc, xfree));
206 /* Create a slot for the mapping, make sure it's the first mapping for this
207 block and then create the mapping itself. */
208 lookup_entry.block = block;
209 slot = htab_find_slot (objfile->static_links.get (), &lookup_entry, INSERT);
210 gdb_assert (*slot == NULL);
212 entry = XOBNEW (&objfile->objfile_obstack, static_link_htab_entry);
213 entry->block = block;
214 entry->static_link = static_link;
215 *slot = (void *) entry;
218 /* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
219 none was found. */
221 const struct dynamic_prop *
222 objfile_lookup_static_link (struct objfile *objfile,
223 const struct block *block)
225 struct static_link_htab_entry *entry;
226 struct static_link_htab_entry lookup_entry;
228 if (objfile->static_links == NULL)
229 return NULL;
230 lookup_entry.block = block;
231 entry = ((struct static_link_htab_entry *)
232 htab_find (objfile->static_links.get (), &lookup_entry));
233 if (entry == NULL)
234 return NULL;
236 gdb_assert (entry->block == block);
237 return entry->static_link;
242 /* Build up the section table that the objfile references. The
243 objfile contains pointers to the start of the table
244 (objfile->sections) and to the first location after the end of the
245 table (objfile->sections_end). */
247 static void
248 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
249 struct objfile *objfile, int force)
251 struct obj_section *section;
253 if (!force)
255 flagword aflag;
257 aflag = bfd_section_flags (asect);
258 if (!(aflag & SEC_ALLOC))
259 return;
262 section = &objfile->sections_start[gdb_bfd_section_index (abfd, asect)];
263 section->objfile = objfile;
264 section->the_bfd_section = asect;
265 section->ovly_mapped = 0;
268 /* Builds a section table for OBJFILE.
270 Note that the OFFSET and OVLY_MAPPED in each table entry are
271 initialized to zero. */
273 void
274 build_objfile_section_table (struct objfile *objfile)
276 int count = gdb_bfd_count_sections (objfile->obfd.get ());
278 objfile->sections_start = OBSTACK_CALLOC (&objfile->objfile_obstack,
279 count,
280 struct obj_section);
281 objfile->sections_end = (objfile->sections_start + count);
282 for (asection *sect : gdb_bfd_sections (objfile->obfd))
283 add_to_objfile_sections (objfile->obfd.get (), sect, objfile, 0);
285 /* See gdb_bfd_section_index. */
286 add_to_objfile_sections (objfile->obfd.get (), bfd_com_section_ptr,
287 objfile, 1);
288 add_to_objfile_sections (objfile->obfd.get (), bfd_und_section_ptr,
289 objfile, 1);
290 add_to_objfile_sections (objfile->obfd.get (), bfd_abs_section_ptr,
291 objfile, 1);
292 add_to_objfile_sections (objfile->obfd.get (), bfd_ind_section_ptr,
293 objfile, 1);
296 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
297 initialize the new objfile as best we can and link it into the list
298 of all known objfiles.
300 NAME should contain original non-canonicalized filename or other
301 identifier as entered by user. If there is no better source use
302 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
303 NAME content is copied into returned objfile.
305 The FLAGS word contains various bits (OBJF_*) that can be taken as
306 requests for specific operations. Other bits like OBJF_SHARED are
307 simply copied through to the new objfile flags member. */
309 objfile::objfile (gdb_bfd_ref_ptr bfd_, program_space *pspace,
310 const char *name, objfile_flags flags_)
311 : flags (flags_),
312 m_pspace (pspace),
313 obfd (std::move (bfd_))
315 const char *expanded_name;
317 std::string name_holder;
318 if (name == NULL)
320 gdb_assert (obfd == nullptr);
321 gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
322 expanded_name = "<<anonymous objfile>>";
324 else if ((flags & OBJF_NOT_FILENAME) != 0
325 || is_target_filename (name))
326 expanded_name = name;
327 else
329 name_holder = gdb_abspath (name);
330 expanded_name = name_holder.c_str ();
332 original_name = obstack_strdup (&objfile_obstack, expanded_name);
334 /* Update the per-objfile information that comes from the bfd, ensuring
335 that any data that is reference is saved in the per-objfile data
336 region. */
338 if (obfd != nullptr)
340 mtime = bfd_get_mtime (obfd.get ());
342 /* Build section table. */
343 build_objfile_section_table (this);
346 set_objfile_per_bfd (this);
349 /* See objfiles.h. */
352 entry_point_address_query (program_space *pspace, CORE_ADDR *entry_p)
354 objfile *objf = pspace->symfile_object_file;
355 if (objf == NULL || !objf->per_bfd->ei.entry_point_p)
356 return 0;
358 int idx = objf->per_bfd->ei.the_bfd_section_index;
359 *entry_p = objf->per_bfd->ei.entry_point + objf->section_offsets[idx];
361 return 1;
364 /* See objfiles.h. */
366 CORE_ADDR
367 entry_point_address (program_space *pspace)
369 CORE_ADDR retval;
371 if (!entry_point_address_query (pspace, &retval))
372 error (_("Entry point address is not known."));
374 return retval;
377 separate_debug_iterator &
378 separate_debug_iterator::operator++ ()
380 gdb_assert (m_objfile != nullptr);
382 struct objfile *res;
384 /* If any, return the first child. */
385 res = m_objfile->separate_debug_objfile;
386 if (res != nullptr)
388 m_objfile = res;
389 return *this;
392 /* Common case where there is no separate debug objfile. */
393 if (m_objfile == m_parent)
395 m_objfile = nullptr;
396 return *this;
399 /* Return the brother if any. Note that we don't iterate on brothers of
400 the parents. */
401 res = m_objfile->separate_debug_objfile_link;
402 if (res != nullptr)
404 m_objfile = res;
405 return *this;
408 for (res = m_objfile->separate_debug_objfile_backlink;
409 res != m_parent;
410 res = res->separate_debug_objfile_backlink)
412 gdb_assert (res != nullptr);
413 if (res->separate_debug_objfile_link != nullptr)
415 m_objfile = res->separate_debug_objfile_link;
416 return *this;
419 m_objfile = nullptr;
420 return *this;
423 /* Add OBJFILE as a separate debug objfile of PARENT. */
425 static void
426 add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
428 gdb_assert (objfile && parent);
430 /* Must not be already in a list. */
431 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
432 gdb_assert (objfile->separate_debug_objfile_link == NULL);
433 gdb_assert (objfile->separate_debug_objfile == NULL);
434 gdb_assert (parent->separate_debug_objfile_backlink == NULL);
435 gdb_assert (parent->separate_debug_objfile_link == NULL);
437 objfile->separate_debug_objfile_backlink = parent;
438 objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
439 parent->separate_debug_objfile = objfile;
442 /* See objfiles.h. */
444 objfile *
445 objfile::make (gdb_bfd_ref_ptr bfd_, program_space *pspace, const char *name_,
446 objfile_flags flags_, objfile *parent)
448 objfile *result
449 = new objfile (std::move (bfd_), current_program_space, name_, flags_);
450 if (parent != nullptr)
451 add_separate_debug_objfile (result, parent);
453 current_program_space->add_objfile (std::unique_ptr<objfile> (result),
454 parent);
456 /* Rebuild section map next time we need it. */
457 get_objfile_pspace_data (current_program_space)->new_objfiles_available = 1;
459 return result;
462 /* See objfiles.h. */
464 void
465 objfile::unlink ()
467 this->pspace ()->remove_objfile (this);
470 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
471 itself. */
473 void
474 free_objfile_separate_debug (struct objfile *objfile)
476 struct objfile *child;
478 for (child = objfile->separate_debug_objfile; child;)
480 struct objfile *next_child = child->separate_debug_objfile_link;
481 child->unlink ();
482 child = next_child;
486 /* Destroy an objfile and all the symtabs and psymtabs under it. */
488 objfile::~objfile ()
490 /* First notify observers that this objfile is about to be freed. */
491 gdb::observers::free_objfile.notify (this);
493 /* Free all separate debug objfiles. */
494 free_objfile_separate_debug (this);
496 if (separate_debug_objfile_backlink)
498 /* We freed the separate debug file, make sure the base objfile
499 doesn't reference it. */
500 struct objfile *child;
502 child = separate_debug_objfile_backlink->separate_debug_objfile;
504 if (child == this)
506 /* THIS is the first child. */
507 separate_debug_objfile_backlink->separate_debug_objfile =
508 separate_debug_objfile_link;
510 else
512 /* Find THIS in the list. */
513 while (1)
515 if (child->separate_debug_objfile_link == this)
517 child->separate_debug_objfile_link =
518 separate_debug_objfile_link;
519 break;
521 child = child->separate_debug_objfile_link;
522 gdb_assert (child);
527 /* Remove any references to this objfile in the global value
528 lists. */
529 preserve_values (this);
531 /* It still may reference data modules have associated with the objfile and
532 the symbol file data. */
533 forget_cached_source_info ();
534 for (compunit_symtab *cu : compunits ())
535 cu->finalize ();
537 breakpoint_free_objfile (this);
538 btrace_free_objfile (this);
540 /* First do any symbol file specific actions required when we are
541 finished with a particular symbol file. Note that if the objfile
542 is using reusable symbol information (via mmalloc) then each of
543 these routines is responsible for doing the correct thing, either
544 freeing things which are valid only during this particular gdb
545 execution, or leaving them to be reused during the next one. */
547 if (sf != NULL)
548 (*sf->sym_finish) (this);
550 /* Before the symbol table code was redone to make it easier to
551 selectively load and remove information particular to a specific
552 linkage unit, gdb used to do these things whenever the monolithic
553 symbol table was blown away. How much still needs to be done
554 is unknown, but we play it safe for now and keep each action until
555 it is shown to be no longer needed. */
557 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
558 for example), so we need to call this here. */
559 clear_pc_function_cache ();
561 /* Check to see if the current_source_symtab belongs to this objfile,
562 and if so, call clear_current_source_symtab_and_line. */
563 clear_current_source_symtab_and_line (this);
565 /* Rebuild section map next time we need it. */
566 auto info = objfiles_pspace_data.get (pspace ());
567 if (info != nullptr)
568 info->section_map_dirty = 1;
572 /* A helper function for objfile_relocate1 that relocates a single
573 symbol. */
575 static void
576 relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
577 const section_offsets &delta)
579 /* The RS6000 code from which this was taken skipped
580 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
581 But I'm leaving out that test, on the theory that
582 they can't possibly pass the tests below. */
583 if ((sym->aclass () == LOC_LABEL
584 || sym->aclass () == LOC_STATIC)
585 && sym->section_index () >= 0)
586 sym->set_value_address (sym->value_address ()
587 + delta[sym->section_index ()]);
590 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
591 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
592 Return non-zero iff any change happened. */
594 static int
595 objfile_relocate1 (struct objfile *objfile,
596 const section_offsets &new_offsets)
598 section_offsets delta (objfile->section_offsets.size ());
600 int something_changed = 0;
602 for (int i = 0; i < objfile->section_offsets.size (); ++i)
604 delta[i] = new_offsets[i] - objfile->section_offsets[i];
605 if (delta[i] != 0)
606 something_changed = 1;
608 if (!something_changed)
609 return 0;
611 /* OK, get all the symtabs. */
612 for (compunit_symtab *cust : objfile->compunits ())
614 struct blockvector *bv = cust->blockvector ();
615 int block_line_section = SECT_OFF_TEXT (objfile);
617 if (bv->map () != nullptr)
618 bv->map ()->relocate (delta[block_line_section]);
620 for (block *b : bv->blocks ())
622 b->set_start (b->start () + delta[block_line_section]);
623 b->set_end (b->end () + delta[block_line_section]);
625 for (blockrange &r : b->ranges ())
627 r.set_start (r.start () + delta[block_line_section]);
628 r.set_end (r.end () + delta[block_line_section]);
631 /* We only want to iterate over the local symbols, not any
632 symbols in included symtabs. */
633 for (struct symbol *sym : b->multidict_symbols ())
634 relocate_one_symbol (sym, objfile, delta);
638 /* Relocate isolated symbols. */
639 for (symbol *iter = objfile->template_symbols; iter; iter = iter->hash_next)
640 relocate_one_symbol (iter, objfile, delta);
642 for (int i = 0; i < objfile->section_offsets.size (); ++i)
643 objfile->section_offsets[i] = new_offsets[i];
645 /* Rebuild section map next time we need it. */
646 get_objfile_pspace_data (objfile->pspace ())->section_map_dirty = 1;
648 /* Update the table in exec_ops, used to read memory. */
649 for (obj_section *s : objfile->sections ())
651 int idx = s - objfile->sections_start;
653 exec_set_section_address (bfd_get_filename (objfile->obfd.get ()), idx,
654 s->addr ());
657 /* Data changed. */
658 return 1;
661 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
662 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
664 The number and ordering of sections does differ between the two objfiles.
665 Only their names match. Also the file offsets will differ (objfile being
666 possibly prelinked but separate_debug_objfile is probably not prelinked) but
667 the in-memory absolute address as specified by NEW_OFFSETS must match both
668 files. */
670 void
671 objfile_relocate (struct objfile *objfile,
672 const section_offsets &new_offsets)
674 int changed = 0;
676 changed |= objfile_relocate1 (objfile, new_offsets);
678 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
680 if (debug_objfile == objfile)
681 continue;
683 section_addr_info objfile_addrs
684 = build_section_addr_info_from_objfile (objfile);
686 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
687 relative ones must be already created according to debug_objfile. */
689 addr_info_make_relative (&objfile_addrs, debug_objfile->obfd.get ());
691 gdb_assert (debug_objfile->section_offsets.size ()
692 == gdb_bfd_count_sections (debug_objfile->obfd.get ()));
693 section_offsets new_debug_offsets
694 (debug_objfile->section_offsets.size ());
695 relative_addr_info_to_section_offsets (new_debug_offsets, objfile_addrs);
697 changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
700 /* Relocate breakpoints as necessary, after things are relocated. */
701 if (changed)
702 breakpoint_re_set ();
705 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
706 not touched here.
707 Return non-zero iff any change happened. */
709 static int
710 objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
712 section_offsets new_offsets (objfile->section_offsets.size (), slide);
713 return objfile_relocate1 (objfile, new_offsets);
716 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
717 SEPARATE_DEBUG_OBJFILEs. */
719 void
720 objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
722 int changed = 0;
724 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
725 changed |= objfile_rebase1 (debug_objfile, slide);
727 /* Relocate breakpoints as necessary, after things are relocated. */
728 if (changed)
729 breakpoint_re_set ();
732 /* See objfiles.h. */
734 bool
735 objfile_has_full_symbols (objfile *objfile)
737 return objfile->compunit_symtabs != nullptr;
740 /* See objfiles.h. */
742 bool
743 objfile_has_symbols (objfile *objfile)
745 for (::objfile *o : objfile->separate_debug_objfiles ())
746 if (o->has_partial_symbols () || objfile_has_full_symbols (o))
747 return true;
749 return false;
752 /* See objfiles.h. */
754 bool
755 have_partial_symbols (program_space *pspace)
757 for (objfile *ofp : pspace->objfiles ())
758 if (ofp->has_partial_symbols ())
759 return true;
761 return false;
764 /* See objfiles.h. */
766 bool
767 have_full_symbols (program_space *pspace)
769 for (objfile *ofp : pspace->objfiles ())
770 if (objfile_has_full_symbols (ofp))
771 return true;
773 return false;
777 /* See objfiles.h. */
779 void
780 objfile_purge_solibs (program_space *pspace)
782 for (objfile *objf : pspace->objfiles_safe ())
784 /* We assume that the solib package has been purged already, or will
785 be soon. */
787 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
788 objf->unlink ();
792 /* See objfiles.h. */
794 bool
795 have_minimal_symbols (program_space *pspace)
797 for (objfile *ofp : pspace->objfiles ())
798 if (ofp->per_bfd->minimal_symbol_count > 0)
799 return true;
801 return false;
804 /* Qsort comparison function. */
806 static bool
807 sort_cmp (const struct obj_section *sect1, const obj_section *sect2)
809 const CORE_ADDR sect1_addr = sect1->addr ();
810 const CORE_ADDR sect2_addr = sect2->addr ();
812 if (sect1_addr < sect2_addr)
813 return true;
814 else if (sect1_addr > sect2_addr)
815 return false;
816 else
818 /* Sections are at the same address. This could happen if
819 A) we have an objfile and a separate debuginfo.
820 B) we are confused, and have added sections without proper relocation,
821 or something like that. */
823 const struct objfile *const objfile1 = sect1->objfile;
824 const struct objfile *const objfile2 = sect2->objfile;
826 if (objfile1->separate_debug_objfile == objfile2
827 || objfile2->separate_debug_objfile == objfile1)
829 /* Case A. The ordering doesn't matter: separate debuginfo files
830 will be filtered out later. */
832 return false;
835 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
836 triage. This section could be slow (since we iterate over all
837 objfiles in each call to sort_cmp), but this shouldn't happen
838 very often (GDB is already in a confused state; one hopes this
839 doesn't happen at all). If you discover that significant time is
840 spent in the loops below, do 'set complaints 100' and examine the
841 resulting complaints. */
842 if (objfile1 == objfile2)
844 /* Both sections came from the same objfile. We are really
845 confused. Sort on sequence order of sections within the
846 objfile. The order of checks is important here, if we find a
847 match on SECT2 first then either SECT2 is before SECT1, or,
848 SECT2 == SECT1, in both cases we should return false. The
849 second case shouldn't occur during normal use, but std::sort
850 does check that '!(a < a)' when compiled in debug mode. */
852 for (const obj_section *osect : objfile1->sections ())
853 if (osect == sect2)
854 return false;
855 else if (osect == sect1)
856 return true;
858 /* We should have found one of the sections before getting here. */
859 gdb_assert_not_reached ("section not found");
861 else
863 /* Sort on sequence number of the objfile in the chain. */
865 for (objfile *objfile : current_program_space->objfiles ())
866 if (objfile == objfile1)
867 return true;
868 else if (objfile == objfile2)
869 return false;
871 /* We should have found one of the objfiles before getting here. */
872 gdb_assert_not_reached ("objfile not found");
876 /* Unreachable. */
877 gdb_assert_not_reached ("unexpected code path");
878 return false;
881 /* Select "better" obj_section to keep. We prefer the one that came from
882 the real object, rather than the one from separate debuginfo.
883 Most of the time the two sections are exactly identical, but with
884 prelinking the .rel.dyn section in the real object may have different
885 size. */
887 static struct obj_section *
888 preferred_obj_section (struct obj_section *a, struct obj_section *b)
890 gdb_assert (a->addr () == b->addr ());
891 gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
892 || (b->objfile->separate_debug_objfile == a->objfile));
893 gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
894 || (b->objfile->separate_debug_objfile_backlink == a->objfile));
896 if (a->objfile->separate_debug_objfile != NULL)
897 return a;
898 return b;
901 /* Return 1 if SECTION should be inserted into the section map.
902 We want to insert only non-overlay non-TLS non-empty sections. */
904 static int
905 insert_section_p (const struct bfd *abfd,
906 const struct bfd_section *section)
908 const bfd_vma lma = bfd_section_lma (section);
910 if (overlay_debugging && lma != 0 && lma != bfd_section_vma (section)
911 && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
912 /* This is an overlay section. IN_MEMORY check is needed to avoid
913 discarding sections from the "system supplied DSO" (aka vdso)
914 on some Linux systems (e.g. Fedora 11). */
915 return 0;
916 if ((bfd_section_flags (section) & SEC_THREAD_LOCAL) != 0)
917 /* This is a TLS section. */
918 return 0;
919 if (bfd_section_size (section) == 0)
921 /* This is an empty section. It has no PCs for find_pc_section (), so
922 there is no reason to insert it into the section map. */
923 return 0;
926 return 1;
929 /* Filter out overlapping sections where one section came from the real
930 objfile, and the other from a separate debuginfo file.
931 Return the size of table after redundant sections have been eliminated. */
933 static int
934 filter_debuginfo_sections (struct obj_section **map, int map_size)
936 int i, j;
938 for (i = 0, j = 0; i < map_size - 1; i++)
940 struct obj_section *const sect1 = map[i];
941 struct obj_section *const sect2 = map[i + 1];
942 const struct objfile *const objfile1 = sect1->objfile;
943 const struct objfile *const objfile2 = sect2->objfile;
944 const CORE_ADDR sect1_addr = sect1->addr ();
945 const CORE_ADDR sect2_addr = sect2->addr ();
947 if (sect1_addr == sect2_addr
948 && (objfile1->separate_debug_objfile == objfile2
949 || objfile2->separate_debug_objfile == objfile1))
951 map[j++] = preferred_obj_section (sect1, sect2);
952 ++i;
954 else
955 map[j++] = sect1;
958 if (i < map_size)
960 gdb_assert (i == map_size - 1);
961 map[j++] = map[i];
964 /* The map should not have shrunk to less than half the original size. */
965 gdb_assert (map_size / 2 <= j);
967 return j;
970 /* Filter out overlapping sections, issuing a warning if any are found.
971 Overlapping sections could really be overlay sections which we didn't
972 classify as such in insert_section_p, or we could be dealing with a
973 corrupt binary. */
975 static int
976 filter_overlapping_sections (struct obj_section **map, int map_size)
978 int i, j;
980 for (i = 0, j = 0; i < map_size - 1; )
982 int k;
984 map[j++] = map[i];
985 for (k = i + 1; k < map_size; k++)
987 struct obj_section *const sect1 = map[i];
988 struct obj_section *const sect2 = map[k];
989 const CORE_ADDR sect1_addr = sect1->addr ();
990 const CORE_ADDR sect2_addr = sect2->addr ();
991 const CORE_ADDR sect1_endaddr = sect1->endaddr ();
993 gdb_assert (sect1_addr <= sect2_addr);
995 if (sect1_endaddr <= sect2_addr)
996 break;
997 else
999 /* We have an overlap. Report it. */
1001 struct objfile *const objf1 = sect1->objfile;
1002 struct objfile *const objf2 = sect2->objfile;
1004 const struct bfd_section *const bfds1 = sect1->the_bfd_section;
1005 const struct bfd_section *const bfds2 = sect2->the_bfd_section;
1007 const CORE_ADDR sect2_endaddr = sect2->endaddr ();
1009 struct gdbarch *const gdbarch = objf1->arch ();
1011 complaint (_("unexpected overlap between:\n"
1012 " (A) section `%s' from `%s' [%s, %s)\n"
1013 " (B) section `%s' from `%s' [%s, %s).\n"
1014 "Will ignore section B"),
1015 bfd_section_name (bfds1), objfile_name (objf1),
1016 paddress (gdbarch, sect1_addr),
1017 paddress (gdbarch, sect1_endaddr),
1018 bfd_section_name (bfds2), objfile_name (objf2),
1019 paddress (gdbarch, sect2_addr),
1020 paddress (gdbarch, sect2_endaddr));
1023 i = k;
1026 if (i < map_size)
1028 gdb_assert (i == map_size - 1);
1029 map[j++] = map[i];
1032 return j;
1036 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
1037 TLS, overlay and overlapping sections. */
1039 static void
1040 update_section_map (struct program_space *pspace,
1041 struct obj_section ***pmap, int *pmap_size)
1043 struct objfile_pspace_info *pspace_info;
1044 int alloc_size, map_size, i;
1045 struct obj_section **map;
1047 pspace_info = get_objfile_pspace_data (pspace);
1048 gdb_assert (pspace_info->section_map_dirty != 0
1049 || pspace_info->new_objfiles_available != 0);
1051 map = *pmap;
1052 xfree (map);
1054 alloc_size = 0;
1055 for (objfile *objfile : pspace->objfiles ())
1056 for (obj_section *s : objfile->sections ())
1057 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
1058 alloc_size += 1;
1060 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1061 if (alloc_size == 0)
1063 *pmap = NULL;
1064 *pmap_size = 0;
1065 return;
1068 map = XNEWVEC (struct obj_section *, alloc_size);
1070 i = 0;
1071 for (objfile *objfile : pspace->objfiles ())
1072 for (obj_section *s : objfile->sections ())
1073 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
1074 map[i++] = s;
1076 std::sort (map, map + alloc_size, sort_cmp);
1077 map_size = filter_debuginfo_sections(map, alloc_size);
1078 map_size = filter_overlapping_sections(map, map_size);
1080 if (map_size < alloc_size)
1081 /* Some sections were eliminated. Trim excess space. */
1082 map = XRESIZEVEC (struct obj_section *, map, map_size);
1083 else
1084 gdb_assert (alloc_size == map_size);
1086 *pmap = map;
1087 *pmap_size = map_size;
1090 /* Bsearch comparison function. */
1092 static int
1093 bsearch_cmp (const void *key, const void *elt)
1095 const CORE_ADDR pc = *(CORE_ADDR *) key;
1096 const struct obj_section *section = *(const struct obj_section **) elt;
1098 if (pc < section->addr ())
1099 return -1;
1100 if (pc < section->endaddr ())
1101 return 0;
1102 return 1;
1105 /* Returns a section whose range includes PC or NULL if none found. */
1107 struct obj_section *
1108 find_pc_section (CORE_ADDR pc)
1110 struct objfile_pspace_info *pspace_info;
1111 struct obj_section *s, **sp;
1113 /* Check for mapped overlay section first. */
1114 s = find_pc_mapped_section (pc);
1115 if (s)
1116 return s;
1118 pspace_info = get_objfile_pspace_data (current_program_space);
1119 if (pspace_info->section_map_dirty
1120 || (pspace_info->new_objfiles_available
1121 && !pspace_info->inhibit_updates))
1123 update_section_map (current_program_space,
1124 &pspace_info->sections,
1125 &pspace_info->num_sections);
1127 /* Don't need updates to section map until objfiles are added,
1128 removed or relocated. */
1129 pspace_info->new_objfiles_available = 0;
1130 pspace_info->section_map_dirty = 0;
1133 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1134 bsearch be non-NULL. */
1135 if (pspace_info->sections == NULL)
1137 gdb_assert (pspace_info->num_sections == 0);
1138 return NULL;
1141 sp = (struct obj_section **) bsearch (&pc,
1142 pspace_info->sections,
1143 pspace_info->num_sections,
1144 sizeof (*pspace_info->sections),
1145 bsearch_cmp);
1146 if (sp != NULL)
1147 return *sp;
1148 return NULL;
1152 /* Return non-zero if PC is in a section called NAME. */
1154 bool
1155 pc_in_section (CORE_ADDR pc, const char *name)
1157 struct obj_section *s = find_pc_section (pc);
1158 return (s != nullptr
1159 && s->the_bfd_section->name != nullptr
1160 && strcmp (s->the_bfd_section->name, name) == 0);
1163 /* See objfiles.h. */
1165 void
1166 objfiles_changed (program_space *pspace)
1168 /* Rebuild section map next time we need it. */
1169 get_objfile_pspace_data (pspace)->section_map_dirty = 1;
1172 /* See comments in objfiles.h. */
1174 scoped_restore_tmpl<int>
1175 inhibit_section_map_updates (struct program_space *pspace)
1177 return scoped_restore_tmpl<int>
1178 (&get_objfile_pspace_data (pspace)->inhibit_updates, 1);
1181 /* See objfiles.h. */
1183 bool
1184 is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
1186 if (objfile == NULL)
1187 return false;
1189 for (obj_section *osect : objfile->sections ())
1191 if (section_is_overlay (osect) && !section_is_mapped (osect))
1192 continue;
1194 if (osect->contains (addr))
1195 return true;
1197 return false;
1200 /* See objfiles.h. */
1202 bool
1203 shared_objfile_contains_address_p (struct program_space *pspace,
1204 CORE_ADDR address)
1206 for (objfile *objfile : pspace->objfiles ())
1208 if ((objfile->flags & OBJF_SHARED) != 0
1209 && is_addr_in_objfile (address, objfile))
1210 return true;
1213 return false;
1216 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1217 gdbarch method. It is equivalent to use the objfiles iterable,
1218 searching the objfiles in the order they are stored internally,
1219 ignoring CURRENT_OBJFILE.
1221 On most platforms, it should be close enough to doing the best
1222 we can without some knowledge specific to the architecture. */
1224 void
1225 default_iterate_over_objfiles_in_search_order
1226 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
1227 objfile *current_objfile)
1229 for (objfile *objfile : current_program_space->objfiles ())
1230 if (cb (objfile))
1231 return;
1234 /* See objfiles.h. */
1236 const char *
1237 objfile_name (const struct objfile *objfile)
1239 if (objfile->obfd != NULL)
1240 return bfd_get_filename (objfile->obfd.get ());
1242 return objfile->original_name;
1245 /* See objfiles.h. */
1247 const char *
1248 objfile_filename (const struct objfile *objfile)
1250 if (objfile->obfd != NULL)
1251 return bfd_get_filename (objfile->obfd.get ());
1253 return NULL;
1256 /* See objfiles.h. */
1258 const char *
1259 objfile_debug_name (const struct objfile *objfile)
1261 return lbasename (objfile->original_name);
1264 /* See objfiles.h. */
1266 const char *
1267 objfile_flavour_name (struct objfile *objfile)
1269 if (objfile->obfd != NULL)
1270 return bfd_flavour_name (bfd_get_flavour (objfile->obfd.get ()));
1271 return NULL;
1274 /* See objfiles.h. */
1276 struct type *
1277 objfile_int_type (struct objfile *of, int size_in_bytes, bool unsigned_p)
1279 struct type *int_type;
1281 /* Helper macro to examine the various builtin types. */
1282 #define TRY_TYPE(F) \
1283 int_type = (unsigned_p \
1284 ? builtin_type (of)->builtin_unsigned_ ## F \
1285 : builtin_type (of)->builtin_ ## F); \
1286 if (int_type != NULL && int_type->length () == size_in_bytes) \
1287 return int_type
1289 TRY_TYPE (char);
1290 TRY_TYPE (short);
1291 TRY_TYPE (int);
1292 TRY_TYPE (long);
1293 TRY_TYPE (long_long);
1295 #undef TRY_TYPE
1297 gdb_assert_not_reached ("unable to find suitable integer type");