testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / objfiles.c
blob0e076fe36be2ef1c4074871ce639075cff0db05d
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. */
565 symtab_and_line cursal
566 = get_current_source_symtab_and_line (this->pspace ());
568 if (cursal.symtab && cursal.symtab->compunit ()->objfile () == this)
569 clear_current_source_symtab_and_line (this->pspace ());
572 /* Rebuild section map next time we need it. */
573 get_objfile_pspace_data (m_pspace)->section_map_dirty = 1;
577 /* A helper function for objfile_relocate1 that relocates a single
578 symbol. */
580 static void
581 relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
582 const section_offsets &delta)
584 /* The RS6000 code from which this was taken skipped
585 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
586 But I'm leaving out that test, on the theory that
587 they can't possibly pass the tests below. */
588 if ((sym->aclass () == LOC_LABEL
589 || sym->aclass () == LOC_STATIC)
590 && sym->section_index () >= 0)
591 sym->set_value_address (sym->value_address ()
592 + delta[sym->section_index ()]);
595 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
596 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
597 Return non-zero iff any change happened. */
599 static int
600 objfile_relocate1 (struct objfile *objfile,
601 const section_offsets &new_offsets)
603 section_offsets delta (objfile->section_offsets.size ());
605 int something_changed = 0;
607 for (int i = 0; i < objfile->section_offsets.size (); ++i)
609 delta[i] = new_offsets[i] - objfile->section_offsets[i];
610 if (delta[i] != 0)
611 something_changed = 1;
613 if (!something_changed)
614 return 0;
616 /* OK, get all the symtabs. */
617 for (compunit_symtab *cust : objfile->compunits ())
619 struct blockvector *bv = cust->blockvector ();
620 int block_line_section = SECT_OFF_TEXT (objfile);
622 if (bv->map () != nullptr)
623 bv->map ()->relocate (delta[block_line_section]);
625 for (block *b : bv->blocks ())
627 b->set_start (b->start () + delta[block_line_section]);
628 b->set_end (b->end () + delta[block_line_section]);
630 for (blockrange &r : b->ranges ())
632 r.set_start (r.start () + delta[block_line_section]);
633 r.set_end (r.end () + delta[block_line_section]);
636 /* We only want to iterate over the local symbols, not any
637 symbols in included symtabs. */
638 for (struct symbol *sym : b->multidict_symbols ())
639 relocate_one_symbol (sym, objfile, delta);
643 /* Relocate isolated symbols. */
644 for (symbol *iter = objfile->template_symbols; iter; iter = iter->hash_next)
645 relocate_one_symbol (iter, objfile, delta);
647 for (int i = 0; i < objfile->section_offsets.size (); ++i)
648 objfile->section_offsets[i] = new_offsets[i];
650 /* Rebuild section map next time we need it. */
651 get_objfile_pspace_data (objfile->pspace ())->section_map_dirty = 1;
653 /* Update the table in exec_ops, used to read memory. */
654 for (obj_section *s : objfile->sections ())
656 int idx = s - objfile->sections_start;
658 exec_set_section_address (bfd_get_filename (objfile->obfd.get ()), idx,
659 s->addr ());
662 /* Data changed. */
663 return 1;
666 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
667 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
669 The number and ordering of sections does differ between the two objfiles.
670 Only their names match. Also the file offsets will differ (objfile being
671 possibly prelinked but separate_debug_objfile is probably not prelinked) but
672 the in-memory absolute address as specified by NEW_OFFSETS must match both
673 files. */
675 void
676 objfile_relocate (struct objfile *objfile,
677 const section_offsets &new_offsets)
679 int changed = 0;
681 changed |= objfile_relocate1 (objfile, new_offsets);
683 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
685 if (debug_objfile == objfile)
686 continue;
688 section_addr_info objfile_addrs
689 = build_section_addr_info_from_objfile (objfile);
691 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
692 relative ones must be already created according to debug_objfile. */
694 addr_info_make_relative (&objfile_addrs, debug_objfile->obfd.get ());
696 gdb_assert (debug_objfile->section_offsets.size ()
697 == gdb_bfd_count_sections (debug_objfile->obfd.get ()));
698 section_offsets new_debug_offsets
699 (debug_objfile->section_offsets.size ());
700 relative_addr_info_to_section_offsets (new_debug_offsets, objfile_addrs);
702 changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
705 /* Relocate breakpoints as necessary, after things are relocated. */
706 if (changed)
707 breakpoint_re_set ();
710 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
711 not touched here.
712 Return non-zero iff any change happened. */
714 static int
715 objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
717 section_offsets new_offsets (objfile->section_offsets.size (), slide);
718 return objfile_relocate1 (objfile, new_offsets);
721 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
722 SEPARATE_DEBUG_OBJFILEs. */
724 void
725 objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
727 int changed = 0;
729 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
730 changed |= objfile_rebase1 (debug_objfile, slide);
732 /* Relocate breakpoints as necessary, after things are relocated. */
733 if (changed)
734 breakpoint_re_set ();
737 /* See objfiles.h. */
739 bool
740 objfile_has_full_symbols (objfile *objfile)
742 return objfile->compunit_symtabs != nullptr;
745 /* See objfiles.h. */
747 bool
748 objfile_has_symbols (objfile *objfile)
750 for (::objfile *o : objfile->separate_debug_objfiles ())
751 if (o->has_partial_symbols () || objfile_has_full_symbols (o))
752 return true;
754 return false;
757 /* See objfiles.h. */
759 bool
760 have_partial_symbols (program_space *pspace)
762 for (objfile *ofp : pspace->objfiles ())
763 if (ofp->has_partial_symbols ())
764 return true;
766 return false;
769 /* See objfiles.h. */
771 bool
772 have_full_symbols (program_space *pspace)
774 for (objfile *ofp : pspace->objfiles ())
775 if (objfile_has_full_symbols (ofp))
776 return true;
778 return false;
782 /* See objfiles.h. */
784 void
785 objfile_purge_solibs (program_space *pspace)
787 for (objfile *objf : pspace->objfiles_safe ())
789 /* We assume that the solib package has been purged already, or will
790 be soon. */
792 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
793 objf->unlink ();
797 /* See objfiles.h. */
799 bool
800 have_minimal_symbols (program_space *pspace)
802 for (objfile *ofp : pspace->objfiles ())
803 if (ofp->per_bfd->minimal_symbol_count > 0)
804 return true;
806 return false;
809 /* Qsort comparison function. */
811 static bool
812 sort_cmp (const struct obj_section *sect1, const obj_section *sect2)
814 const CORE_ADDR sect1_addr = sect1->addr ();
815 const CORE_ADDR sect2_addr = sect2->addr ();
817 if (sect1_addr < sect2_addr)
818 return true;
819 else if (sect1_addr > sect2_addr)
820 return false;
821 else
823 /* Sections are at the same address. This could happen if
824 A) we have an objfile and a separate debuginfo.
825 B) we are confused, and have added sections without proper relocation,
826 or something like that. */
828 const struct objfile *const objfile1 = sect1->objfile;
829 const struct objfile *const objfile2 = sect2->objfile;
831 if (objfile1->separate_debug_objfile == objfile2
832 || objfile2->separate_debug_objfile == objfile1)
834 /* Case A. The ordering doesn't matter: separate debuginfo files
835 will be filtered out later. */
837 return false;
840 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
841 triage. This section could be slow (since we iterate over all
842 objfiles in each call to sort_cmp), but this shouldn't happen
843 very often (GDB is already in a confused state; one hopes this
844 doesn't happen at all). If you discover that significant time is
845 spent in the loops below, do 'set complaints 100' and examine the
846 resulting complaints. */
847 if (objfile1 == objfile2)
849 /* Both sections came from the same objfile. We are really
850 confused. Sort on sequence order of sections within the
851 objfile. The order of checks is important here, if we find a
852 match on SECT2 first then either SECT2 is before SECT1, or,
853 SECT2 == SECT1, in both cases we should return false. The
854 second case shouldn't occur during normal use, but std::sort
855 does check that '!(a < a)' when compiled in debug mode. */
857 for (const obj_section *osect : objfile1->sections ())
858 if (osect == sect2)
859 return false;
860 else if (osect == sect1)
861 return true;
863 /* We should have found one of the sections before getting here. */
864 gdb_assert_not_reached ("section not found");
866 else
868 /* Sort on sequence number of the objfile in the chain. */
870 for (objfile *objfile : current_program_space->objfiles ())
871 if (objfile == objfile1)
872 return true;
873 else if (objfile == objfile2)
874 return false;
876 /* We should have found one of the objfiles before getting here. */
877 gdb_assert_not_reached ("objfile not found");
881 /* Unreachable. */
882 gdb_assert_not_reached ("unexpected code path");
883 return false;
886 /* Select "better" obj_section to keep. We prefer the one that came from
887 the real object, rather than the one from separate debuginfo.
888 Most of the time the two sections are exactly identical, but with
889 prelinking the .rel.dyn section in the real object may have different
890 size. */
892 static struct obj_section *
893 preferred_obj_section (struct obj_section *a, struct obj_section *b)
895 gdb_assert (a->addr () == b->addr ());
896 gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
897 || (b->objfile->separate_debug_objfile == a->objfile));
898 gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
899 || (b->objfile->separate_debug_objfile_backlink == a->objfile));
901 if (a->objfile->separate_debug_objfile != NULL)
902 return a;
903 return b;
906 /* Return 1 if SECTION should be inserted into the section map.
907 We want to insert only non-overlay non-TLS non-empty sections. */
909 static int
910 insert_section_p (const struct bfd *abfd,
911 const struct bfd_section *section)
913 const bfd_vma lma = bfd_section_lma (section);
915 if (overlay_debugging && lma != 0 && lma != bfd_section_vma (section)
916 && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
917 /* This is an overlay section. IN_MEMORY check is needed to avoid
918 discarding sections from the "system supplied DSO" (aka vdso)
919 on some Linux systems (e.g. Fedora 11). */
920 return 0;
921 if ((bfd_section_flags (section) & SEC_THREAD_LOCAL) != 0)
922 /* This is a TLS section. */
923 return 0;
924 if (bfd_section_size (section) == 0)
926 /* This is an empty section. It has no PCs for find_pc_section (), so
927 there is no reason to insert it into the section map. */
928 return 0;
931 return 1;
934 /* Filter out overlapping sections where one section came from the real
935 objfile, and the other from a separate debuginfo file.
936 Return the size of table after redundant sections have been eliminated. */
938 static int
939 filter_debuginfo_sections (struct obj_section **map, int map_size)
941 int i, j;
943 for (i = 0, j = 0; i < map_size - 1; i++)
945 struct obj_section *const sect1 = map[i];
946 struct obj_section *const sect2 = map[i + 1];
947 const struct objfile *const objfile1 = sect1->objfile;
948 const struct objfile *const objfile2 = sect2->objfile;
949 const CORE_ADDR sect1_addr = sect1->addr ();
950 const CORE_ADDR sect2_addr = sect2->addr ();
952 if (sect1_addr == sect2_addr
953 && (objfile1->separate_debug_objfile == objfile2
954 || objfile2->separate_debug_objfile == objfile1))
956 map[j++] = preferred_obj_section (sect1, sect2);
957 ++i;
959 else
960 map[j++] = sect1;
963 if (i < map_size)
965 gdb_assert (i == map_size - 1);
966 map[j++] = map[i];
969 /* The map should not have shrunk to less than half the original size. */
970 gdb_assert (map_size / 2 <= j);
972 return j;
975 /* Filter out overlapping sections, issuing a warning if any are found.
976 Overlapping sections could really be overlay sections which we didn't
977 classify as such in insert_section_p, or we could be dealing with a
978 corrupt binary. */
980 static int
981 filter_overlapping_sections (struct obj_section **map, int map_size)
983 int i, j;
985 for (i = 0, j = 0; i < map_size - 1; )
987 int k;
989 map[j++] = map[i];
990 for (k = i + 1; k < map_size; k++)
992 struct obj_section *const sect1 = map[i];
993 struct obj_section *const sect2 = map[k];
994 const CORE_ADDR sect1_addr = sect1->addr ();
995 const CORE_ADDR sect2_addr = sect2->addr ();
996 const CORE_ADDR sect1_endaddr = sect1->endaddr ();
998 gdb_assert (sect1_addr <= sect2_addr);
1000 if (sect1_endaddr <= sect2_addr)
1001 break;
1002 else
1004 /* We have an overlap. Report it. */
1006 struct objfile *const objf1 = sect1->objfile;
1007 struct objfile *const objf2 = sect2->objfile;
1009 const struct bfd_section *const bfds1 = sect1->the_bfd_section;
1010 const struct bfd_section *const bfds2 = sect2->the_bfd_section;
1012 const CORE_ADDR sect2_endaddr = sect2->endaddr ();
1014 struct gdbarch *const gdbarch = objf1->arch ();
1016 complaint (_("unexpected overlap between:\n"
1017 " (A) section `%s' from `%s' [%s, %s)\n"
1018 " (B) section `%s' from `%s' [%s, %s).\n"
1019 "Will ignore section B"),
1020 bfd_section_name (bfds1), objfile_name (objf1),
1021 paddress (gdbarch, sect1_addr),
1022 paddress (gdbarch, sect1_endaddr),
1023 bfd_section_name (bfds2), objfile_name (objf2),
1024 paddress (gdbarch, sect2_addr),
1025 paddress (gdbarch, sect2_endaddr));
1028 i = k;
1031 if (i < map_size)
1033 gdb_assert (i == map_size - 1);
1034 map[j++] = map[i];
1037 return j;
1041 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
1042 TLS, overlay and overlapping sections. */
1044 static void
1045 update_section_map (struct program_space *pspace,
1046 struct obj_section ***pmap, int *pmap_size)
1048 struct objfile_pspace_info *pspace_info;
1049 int alloc_size, map_size, i;
1050 struct obj_section **map;
1052 pspace_info = get_objfile_pspace_data (pspace);
1053 gdb_assert (pspace_info->section_map_dirty != 0
1054 || pspace_info->new_objfiles_available != 0);
1056 map = *pmap;
1057 xfree (map);
1059 alloc_size = 0;
1060 for (objfile *objfile : pspace->objfiles ())
1061 for (obj_section *s : objfile->sections ())
1062 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
1063 alloc_size += 1;
1065 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1066 if (alloc_size == 0)
1068 *pmap = NULL;
1069 *pmap_size = 0;
1070 return;
1073 map = XNEWVEC (struct obj_section *, alloc_size);
1075 i = 0;
1076 for (objfile *objfile : pspace->objfiles ())
1077 for (obj_section *s : objfile->sections ())
1078 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
1079 map[i++] = s;
1081 std::sort (map, map + alloc_size, sort_cmp);
1082 map_size = filter_debuginfo_sections(map, alloc_size);
1083 map_size = filter_overlapping_sections(map, map_size);
1085 if (map_size < alloc_size)
1086 /* Some sections were eliminated. Trim excess space. */
1087 map = XRESIZEVEC (struct obj_section *, map, map_size);
1088 else
1089 gdb_assert (alloc_size == map_size);
1091 *pmap = map;
1092 *pmap_size = map_size;
1095 /* Bsearch comparison function. */
1097 static int
1098 bsearch_cmp (const void *key, const void *elt)
1100 const CORE_ADDR pc = *(CORE_ADDR *) key;
1101 const struct obj_section *section = *(const struct obj_section **) elt;
1103 if (pc < section->addr ())
1104 return -1;
1105 if (pc < section->endaddr ())
1106 return 0;
1107 return 1;
1110 /* Returns a section whose range includes PC or NULL if none found. */
1112 struct obj_section *
1113 find_pc_section (CORE_ADDR pc)
1115 struct objfile_pspace_info *pspace_info;
1116 struct obj_section *s, **sp;
1118 /* Check for mapped overlay section first. */
1119 s = find_pc_mapped_section (pc);
1120 if (s)
1121 return s;
1123 pspace_info = get_objfile_pspace_data (current_program_space);
1124 if (pspace_info->section_map_dirty
1125 || (pspace_info->new_objfiles_available
1126 && !pspace_info->inhibit_updates))
1128 update_section_map (current_program_space,
1129 &pspace_info->sections,
1130 &pspace_info->num_sections);
1132 /* Don't need updates to section map until objfiles are added,
1133 removed or relocated. */
1134 pspace_info->new_objfiles_available = 0;
1135 pspace_info->section_map_dirty = 0;
1138 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1139 bsearch be non-NULL. */
1140 if (pspace_info->sections == NULL)
1142 gdb_assert (pspace_info->num_sections == 0);
1143 return NULL;
1146 sp = (struct obj_section **) bsearch (&pc,
1147 pspace_info->sections,
1148 pspace_info->num_sections,
1149 sizeof (*pspace_info->sections),
1150 bsearch_cmp);
1151 if (sp != NULL)
1152 return *sp;
1153 return NULL;
1157 /* Return non-zero if PC is in a section called NAME. */
1159 bool
1160 pc_in_section (CORE_ADDR pc, const char *name)
1162 struct obj_section *s = find_pc_section (pc);
1163 return (s != nullptr
1164 && s->the_bfd_section->name != nullptr
1165 && strcmp (s->the_bfd_section->name, name) == 0);
1168 /* See objfiles.h. */
1170 void
1171 objfiles_changed (program_space *pspace)
1173 /* Rebuild section map next time we need it. */
1174 get_objfile_pspace_data (pspace)->section_map_dirty = 1;
1177 /* See comments in objfiles.h. */
1179 scoped_restore_tmpl<int>
1180 inhibit_section_map_updates (struct program_space *pspace)
1182 return scoped_restore_tmpl<int>
1183 (&get_objfile_pspace_data (pspace)->inhibit_updates, 1);
1186 /* See objfiles.h. */
1188 bool
1189 is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
1191 if (objfile == NULL)
1192 return false;
1194 for (obj_section *osect : objfile->sections ())
1196 if (section_is_overlay (osect) && !section_is_mapped (osect))
1197 continue;
1199 if (osect->contains (addr))
1200 return true;
1202 return false;
1205 /* See objfiles.h. */
1207 bool
1208 shared_objfile_contains_address_p (struct program_space *pspace,
1209 CORE_ADDR address)
1211 for (objfile *objfile : pspace->objfiles ())
1213 if ((objfile->flags & OBJF_SHARED) != 0
1214 && is_addr_in_objfile (address, objfile))
1215 return true;
1218 return false;
1221 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1222 gdbarch method. It is equivalent to use the objfiles iterable,
1223 searching the objfiles in the order they are stored internally,
1224 ignoring CURRENT_OBJFILE.
1226 On most platforms, it should be close enough to doing the best
1227 we can without some knowledge specific to the architecture. */
1229 void
1230 default_iterate_over_objfiles_in_search_order
1231 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
1232 objfile *current_objfile)
1234 for (objfile *objfile : current_program_space->objfiles ())
1235 if (cb (objfile))
1236 return;
1239 /* See objfiles.h. */
1241 const char *
1242 objfile_name (const struct objfile *objfile)
1244 if (objfile->obfd != NULL)
1245 return bfd_get_filename (objfile->obfd.get ());
1247 return objfile->original_name;
1250 /* See objfiles.h. */
1252 const char *
1253 objfile_filename (const struct objfile *objfile)
1255 if (objfile->obfd != NULL)
1256 return bfd_get_filename (objfile->obfd.get ());
1258 return NULL;
1261 /* See objfiles.h. */
1263 const char *
1264 objfile_debug_name (const struct objfile *objfile)
1266 return lbasename (objfile->original_name);
1269 /* See objfiles.h. */
1271 const char *
1272 objfile_flavour_name (struct objfile *objfile)
1274 if (objfile->obfd != NULL)
1275 return bfd_flavour_name (bfd_get_flavour (objfile->obfd.get ()));
1276 return NULL;
1279 /* See objfiles.h. */
1281 struct type *
1282 objfile_int_type (struct objfile *of, int size_in_bytes, bool unsigned_p)
1284 struct type *int_type;
1286 /* Helper macro to examine the various builtin types. */
1287 #define TRY_TYPE(F) \
1288 int_type = (unsigned_p \
1289 ? builtin_type (of)->builtin_unsigned_ ## F \
1290 : builtin_type (of)->builtin_ ## F); \
1291 if (int_type != NULL && int_type->length () == size_in_bytes) \
1292 return int_type
1294 TRY_TYPE (char);
1295 TRY_TYPE (short);
1296 TRY_TYPE (int);
1297 TRY_TYPE (long);
1298 TRY_TYPE (long_long);
1300 #undef TRY_TYPE
1302 gdb_assert_not_reached ("unable to find suitable integer type");