]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/objfiles.c
d25d1a02cea1d5c7b200e078d98924e993e9d50e
[thirdparty/binutils-gdb.git] / gdb / objfiles.c
1 /* GDB routines for manipulating objfiles.
2
3 Copyright (C) 1992-2025 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 /* This file contains support routines for creating, manipulating, and
23 destroying objfile structures. */
24
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"
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include "gdbsupport/gdb_obstack.h"
37
38 #include "breakpoint.h"
39 #include "block.h"
40 #include "dictionary.h"
41 #include "source.h"
42 #include "addrmap.h"
43 #include "arch-utils.h"
44 #include "exec.h"
45 #include "observable.h"
46 #include "complaints.h"
47 #include "gdb_bfd.h"
48 #include "btrace.h"
49 #include "gdbsupport/pathstuff.h"
50
51 #include <algorithm>
52
53 /* Externally visible variables that are owned by this module.
54 See declarations in objfile.h for more info. */
55
56 struct objfile_pspace_info
57 {
58 objfile_pspace_info () = default;
59 ~objfile_pspace_info ();
60
61 struct obj_section **sections = nullptr;
62 int num_sections = 0;
63
64 /* Nonzero if object files have been added since the section map
65 was last updated. */
66 int new_objfiles_available = 0;
67
68 /* Nonzero if the section map MUST be updated before use. */
69 int section_map_dirty = 0;
70
71 /* Nonzero if section map updates should be inhibited if possible. */
72 int inhibit_updates = 0;
73 };
74
75 /* Per-program-space data key. */
76 static const registry<program_space>::key<objfile_pspace_info>
77 objfiles_pspace_data;
78
79 objfile_pspace_info::~objfile_pspace_info ()
80 {
81 xfree (sections);
82 }
83
84 /* Get the current svr4 data. If none is found yet, add it now. This
85 function always returns a valid object. */
86
87 static struct objfile_pspace_info *
88 get_objfile_pspace_data (struct program_space *pspace)
89 {
90 struct objfile_pspace_info *info;
91
92 info = objfiles_pspace_data.get (pspace);
93 if (info == NULL)
94 info = objfiles_pspace_data.emplace (pspace);
95
96 return info;
97 }
98
99 \f
100
101 /* Per-BFD data key. */
102
103 static const registry<bfd>::key<objfile_per_bfd_storage> objfiles_bfd_data;
104
105 objfile_per_bfd_storage::~objfile_per_bfd_storage ()
106 {
107 }
108
109 /* Create the per-BFD storage object for OBJFILE. If ABFD is not
110 NULL, and it already has a per-BFD storage object, use that.
111 Otherwise, allocate a new per-BFD storage object. */
112
113 void
114 set_objfile_per_bfd (struct objfile *objfile)
115 {
116 bfd *abfd = objfile->obfd.get ();
117 struct objfile_per_bfd_storage *storage = NULL;
118
119 if (abfd != NULL)
120 storage = objfiles_bfd_data.get (abfd);
121
122 if (storage == NULL)
123 {
124 storage = new objfile_per_bfd_storage (abfd);
125 /* If the object requires gdb to do relocations, we simply fall
126 back to not sharing data across users. These cases are rare
127 enough that this seems reasonable. */
128 if (abfd != NULL && !gdb_bfd_requires_relocations (abfd))
129 objfiles_bfd_data.set (abfd, storage);
130 else
131 objfile->per_bfd_storage.reset (storage);
132
133 /* Look up the gdbarch associated with the BFD. */
134 if (abfd != NULL)
135 storage->gdbarch = gdbarch_from_bfd (abfd);
136 }
137
138 objfile->per_bfd = storage;
139 }
140
141 /* Set the objfile's per-BFD notion of the "main" name and
142 language. */
143
144 void
145 set_objfile_main_name (struct objfile *objfile,
146 const char *name, enum language lang)
147 {
148 if (objfile->per_bfd->name_of_main == NULL
149 || strcmp (objfile->per_bfd->name_of_main, name) != 0)
150 objfile->per_bfd->name_of_main
151 = obstack_strdup (&objfile->per_bfd->storage_obstack, name);
152 objfile->per_bfd->language_of_main = lang;
153 }
154
155 /* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
156 Must not be called more than once for each BLOCK. */
157
158 void
159 objfile_register_static_link (struct objfile *objfile,
160 const struct block *block,
161 const struct dynamic_prop *static_link)
162 {
163 /* Enter the mapping and make sure it's the first mapping for this
164 block. */
165 bool inserted = objfile->static_links.emplace (block, static_link).second;
166 gdb_assert (inserted);
167 }
168
169 /* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
170 none was found. */
171
172 const struct dynamic_prop *
173 objfile_lookup_static_link (struct objfile *objfile,
174 const struct block *block)
175 {
176 if (auto iter = objfile->static_links.find (block);
177 iter != objfile->static_links.end ())
178 return iter->second;
179
180 return nullptr;
181 }
182
183 \f
184
185 /* Build up the section table that the objfile references. The
186 objfile contains pointers to the start of the table
187 (objfile->sections) and to the first location after the end of the
188 table (objfile->sections_end). */
189
190 static void
191 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
192 struct objfile *objfile, int force)
193 {
194 struct obj_section *section;
195
196 if (!force)
197 {
198 flagword aflag;
199
200 aflag = bfd_section_flags (asect);
201 if (!(aflag & SEC_ALLOC))
202 return;
203 }
204
205 section = &objfile->sections_start[gdb_bfd_section_index (abfd, asect)];
206 section->objfile = objfile;
207 section->the_bfd_section = asect;
208 section->ovly_mapped = 0;
209 }
210
211 /* Builds a section table for OBJFILE.
212
213 Note that the OFFSET and OVLY_MAPPED in each table entry are
214 initialized to zero. */
215
216 void
217 build_objfile_section_table (struct objfile *objfile)
218 {
219 int count = gdb_bfd_count_sections (objfile->obfd.get ());
220
221 objfile->sections_start = OBSTACK_CALLOC (&objfile->objfile_obstack,
222 count,
223 struct obj_section);
224 objfile->sections_end = (objfile->sections_start + count);
225 for (asection *sect : gdb_bfd_sections (objfile->obfd))
226 add_to_objfile_sections (objfile->obfd.get (), sect, objfile, 0);
227
228 /* See gdb_bfd_section_index. */
229 add_to_objfile_sections (objfile->obfd.get (), bfd_com_section_ptr,
230 objfile, 1);
231 add_to_objfile_sections (objfile->obfd.get (), bfd_und_section_ptr,
232 objfile, 1);
233 add_to_objfile_sections (objfile->obfd.get (), bfd_abs_section_ptr,
234 objfile, 1);
235 add_to_objfile_sections (objfile->obfd.get (), bfd_ind_section_ptr,
236 objfile, 1);
237 }
238
239 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
240 initialize the new objfile as best we can and link it into the list
241 of all known objfiles.
242
243 NAME should contain original non-canonicalized filename or other
244 identifier as entered by user. If there is no better source use
245 bfd_get_filename (ABFD). NAME may be NULL only if ABFD is NULL.
246 NAME content is copied into returned objfile.
247
248 The FLAGS word contains various bits (OBJF_*) that can be taken as
249 requests for specific operations. Other bits like OBJF_SHARED are
250 simply copied through to the new objfile flags member. */
251
252 objfile::objfile (gdb_bfd_ref_ptr bfd_, program_space *pspace,
253 const char *name, objfile_flags flags_)
254 : flags (flags_),
255 m_pspace (pspace),
256 obfd (std::move (bfd_))
257 {
258 const char *expanded_name;
259
260 std::string name_holder;
261 if (name == NULL)
262 {
263 gdb_assert (obfd == nullptr);
264 gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
265 expanded_name = "<<anonymous objfile>>";
266 }
267 else if ((flags & OBJF_NOT_FILENAME) != 0
268 || is_target_filename (name))
269 expanded_name = name;
270 else
271 {
272 name_holder = gdb_abspath (name);
273 expanded_name = name_holder.c_str ();
274 }
275 original_name = obstack_strdup (&objfile_obstack, expanded_name);
276
277 /* Update the per-objfile information that comes from the bfd, ensuring
278 that any data that is reference is saved in the per-objfile data
279 region. */
280
281 if (obfd != nullptr)
282 {
283 mtime = gdb_bfd_get_mtime (obfd.get ());
284
285 /* Build section table. */
286 build_objfile_section_table (this);
287 }
288
289 set_objfile_per_bfd (this);
290 }
291
292 /* See objfiles.h. */
293
294 int
295 entry_point_address_query (program_space *pspace, CORE_ADDR *entry_p)
296 {
297 objfile *objf = pspace->symfile_object_file;
298 if (objf == NULL || !objf->per_bfd->ei.entry_point_p)
299 return 0;
300
301 int idx = objf->per_bfd->ei.the_bfd_section_index;
302 *entry_p = objf->per_bfd->ei.entry_point + objf->section_offsets[idx];
303
304 return 1;
305 }
306
307 /* See objfiles.h. */
308
309 CORE_ADDR
310 entry_point_address (program_space *pspace)
311 {
312 CORE_ADDR retval;
313
314 if (!entry_point_address_query (pspace, &retval))
315 error (_("Entry point address is not known."));
316
317 return retval;
318 }
319
320 separate_debug_iterator &
321 separate_debug_iterator::operator++ ()
322 {
323 gdb_assert (m_objfile != nullptr);
324
325 struct objfile *res;
326
327 /* If any, return the first child. */
328 res = m_objfile->separate_debug_objfile;
329 if (res != nullptr)
330 {
331 m_objfile = res;
332 return *this;
333 }
334
335 /* Common case where there is no separate debug objfile. */
336 if (m_objfile == m_parent)
337 {
338 m_objfile = nullptr;
339 return *this;
340 }
341
342 /* Return the brother if any. Note that we don't iterate on brothers of
343 the parents. */
344 res = m_objfile->separate_debug_objfile_link;
345 if (res != nullptr)
346 {
347 m_objfile = res;
348 return *this;
349 }
350
351 for (res = m_objfile->separate_debug_objfile_backlink;
352 res != m_parent;
353 res = res->separate_debug_objfile_backlink)
354 {
355 gdb_assert (res != nullptr);
356 if (res->separate_debug_objfile_link != nullptr)
357 {
358 m_objfile = res->separate_debug_objfile_link;
359 return *this;
360 }
361 }
362 m_objfile = nullptr;
363 return *this;
364 }
365
366 /* Add OBJFILE as a separate debug objfile of PARENT. */
367
368 static void
369 add_separate_debug_objfile (struct objfile *objfile, struct objfile *parent)
370 {
371 gdb_assert (objfile && parent);
372
373 /* Must not be already in a list. */
374 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
375 gdb_assert (objfile->separate_debug_objfile_link == NULL);
376 gdb_assert (objfile->separate_debug_objfile == NULL);
377 gdb_assert (parent->separate_debug_objfile_backlink == NULL);
378 gdb_assert (parent->separate_debug_objfile_link == NULL);
379
380 objfile->separate_debug_objfile_backlink = parent;
381 objfile->separate_debug_objfile_link = parent->separate_debug_objfile;
382 parent->separate_debug_objfile = objfile;
383 }
384
385 /* See objfiles.h. */
386
387 objfile *
388 objfile::make (gdb_bfd_ref_ptr bfd_, program_space *pspace, const char *name_,
389 objfile_flags flags_, objfile *parent)
390 {
391 objfile *result = new objfile (std::move (bfd_), pspace, name_, flags_);
392 if (parent != nullptr)
393 add_separate_debug_objfile (result, parent);
394
395 pspace->add_objfile (std::unique_ptr<objfile> (result), parent);
396
397 /* Rebuild section map next time we need it. */
398 get_objfile_pspace_data (pspace)->new_objfiles_available = 1;
399
400 return result;
401 }
402
403 /* See objfiles.h. */
404
405 void
406 objfile::unlink ()
407 {
408 this->pspace ()->remove_objfile (this);
409 }
410
411 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
412 itself. */
413
414 void
415 free_objfile_separate_debug (struct objfile *objfile)
416 {
417 struct objfile *child;
418
419 for (child = objfile->separate_debug_objfile; child;)
420 {
421 struct objfile *next_child = child->separate_debug_objfile_link;
422 child->unlink ();
423 child = next_child;
424 }
425 }
426
427 /* Destroy an objfile and all the symtabs and psymtabs under it. */
428
429 objfile::~objfile ()
430 {
431 /* First notify observers that this objfile is about to be freed. */
432 gdb::observers::free_objfile.notify (this);
433
434 /* Free all separate debug objfiles. */
435 free_objfile_separate_debug (this);
436
437 if (separate_debug_objfile_backlink)
438 {
439 /* We freed the separate debug file, make sure the base objfile
440 doesn't reference it. */
441 struct objfile *child;
442
443 child = separate_debug_objfile_backlink->separate_debug_objfile;
444
445 if (child == this)
446 {
447 /* THIS is the first child. */
448 separate_debug_objfile_backlink->separate_debug_objfile =
449 separate_debug_objfile_link;
450 }
451 else
452 {
453 /* Find THIS in the list. */
454 while (1)
455 {
456 if (child->separate_debug_objfile_link == this)
457 {
458 child->separate_debug_objfile_link =
459 separate_debug_objfile_link;
460 break;
461 }
462 child = child->separate_debug_objfile_link;
463 gdb_assert (child);
464 }
465 }
466 }
467
468 /* Remove any references to this objfile in the global value
469 lists. */
470 preserve_values (this);
471
472 /* It still may reference data modules have associated with the objfile and
473 the symbol file data. */
474 forget_cached_source_info ();
475 for (compunit_symtab *cu : compunits ())
476 cu->finalize ();
477
478 breakpoint_free_objfile (this);
479 btrace_free_objfile (this);
480
481 /* First do any symbol file specific actions required when we are
482 finished with a particular symbol file. Note that if the objfile
483 is using reusable symbol information (via mmalloc) then each of
484 these routines is responsible for doing the correct thing, either
485 freeing things which are valid only during this particular gdb
486 execution, or leaving them to be reused during the next one. */
487
488 if (sf != NULL)
489 (*sf->sym_finish) (this);
490
491 /* Before the symbol table code was redone to make it easier to
492 selectively load and remove information particular to a specific
493 linkage unit, gdb used to do these things whenever the monolithic
494 symbol table was blown away. How much still needs to be done
495 is unknown, but we play it safe for now and keep each action until
496 it is shown to be no longer needed. */
497
498 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
499 for example), so we need to call this here. */
500 clear_pc_function_cache ();
501
502 /* Check to see if the current_source_symtab belongs to this objfile,
503 and if so, call clear_current_source_symtab_and_line. */
504 clear_current_source_symtab_and_line (this);
505
506 /* Rebuild section map next time we need it. */
507 auto info = objfiles_pspace_data.get (pspace ());
508 if (info != nullptr)
509 info->section_map_dirty = 1;
510 }
511
512 \f
513 /* A helper function for objfile_relocate1 that relocates a single
514 symbol. */
515
516 static void
517 relocate_one_symbol (struct symbol *sym, struct objfile *objfile,
518 const section_offsets &delta)
519 {
520 /* The RS6000 code from which this was taken skipped
521 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
522 But I'm leaving out that test, on the theory that
523 they can't possibly pass the tests below. */
524 if ((sym->aclass () == LOC_LABEL
525 || sym->aclass () == LOC_STATIC)
526 && sym->section_index () >= 0)
527 sym->set_value_address (sym->value_address ()
528 + delta[sym->section_index ()]);
529 }
530
531 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
532 entries in new_offsets. SEPARATE_DEBUG_OBJFILE is not touched here.
533 Return non-zero iff any change happened. */
534
535 static int
536 objfile_relocate1 (struct objfile *objfile,
537 const section_offsets &new_offsets)
538 {
539 section_offsets delta (objfile->section_offsets.size ());
540
541 int something_changed = 0;
542
543 for (int i = 0; i < objfile->section_offsets.size (); ++i)
544 {
545 delta[i] = new_offsets[i] - objfile->section_offsets[i];
546 if (delta[i] != 0)
547 something_changed = 1;
548 }
549 if (!something_changed)
550 return 0;
551
552 /* OK, get all the symtabs. */
553 for (compunit_symtab *cust : objfile->compunits ())
554 {
555 struct blockvector *bv = cust->blockvector ();
556 int block_line_section = SECT_OFF_TEXT (objfile);
557
558 if (bv->map () != nullptr)
559 bv->map ()->relocate (delta[block_line_section]);
560
561 for (block *b : bv->blocks ())
562 {
563 b->set_start (b->start () + delta[block_line_section]);
564 b->set_end (b->end () + delta[block_line_section]);
565
566 for (blockrange &r : b->ranges ())
567 {
568 r.set_start (r.start () + delta[block_line_section]);
569 r.set_end (r.end () + delta[block_line_section]);
570 }
571
572 /* We only want to iterate over the local symbols, not any
573 symbols in included symtabs. */
574 for (struct symbol *sym : b->multidict_symbols ())
575 relocate_one_symbol (sym, objfile, delta);
576 }
577 }
578
579 /* Relocate isolated symbols. */
580 for (symbol *iter = objfile->template_symbols; iter; iter = iter->hash_next)
581 relocate_one_symbol (iter, objfile, delta);
582
583 for (int i = 0; i < objfile->section_offsets.size (); ++i)
584 objfile->section_offsets[i] = new_offsets[i];
585
586 /* Rebuild section map next time we need it. */
587 get_objfile_pspace_data (objfile->pspace ())->section_map_dirty = 1;
588
589 /* Update the table in exec_ops, used to read memory. */
590 for (obj_section *s : objfile->sections ())
591 {
592 int idx = s - objfile->sections_start;
593
594 exec_set_section_address (bfd_get_filename (objfile->obfd.get ()), idx,
595 s->addr ());
596 }
597
598 /* Data changed. */
599 return 1;
600 }
601
602 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
603 entries in new_offsets. Process also OBJFILE's SEPARATE_DEBUG_OBJFILEs.
604
605 The number and ordering of sections does differ between the two objfiles.
606 Only their names match. Also the file offsets will differ (objfile being
607 possibly prelinked but separate_debug_objfile is probably not prelinked) but
608 the in-memory absolute address as specified by NEW_OFFSETS must match both
609 files. */
610
611 void
612 objfile_relocate (struct objfile *objfile,
613 const section_offsets &new_offsets)
614 {
615 int changed = 0;
616
617 changed |= objfile_relocate1 (objfile, new_offsets);
618
619 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
620 {
621 if (debug_objfile == objfile)
622 continue;
623
624 section_addr_info objfile_addrs
625 = build_section_addr_info_from_objfile (objfile);
626
627 /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
628 relative ones must be already created according to debug_objfile. */
629
630 addr_info_make_relative (&objfile_addrs, debug_objfile->obfd.get ());
631
632 gdb_assert (debug_objfile->section_offsets.size ()
633 == gdb_bfd_count_sections (debug_objfile->obfd.get ()));
634 section_offsets new_debug_offsets
635 (debug_objfile->section_offsets.size ());
636 relative_addr_info_to_section_offsets (new_debug_offsets, objfile_addrs);
637
638 changed |= objfile_relocate1 (debug_objfile, new_debug_offsets);
639 }
640
641 /* Relocate breakpoints as necessary, after things are relocated. */
642 if (changed)
643 breakpoint_re_set ();
644 }
645
646 /* Rebase (add to the offsets) OBJFILE by SLIDE. SEPARATE_DEBUG_OBJFILE is
647 not touched here.
648 Return non-zero iff any change happened. */
649
650 static int
651 objfile_rebase1 (struct objfile *objfile, CORE_ADDR slide)
652 {
653 section_offsets new_offsets (objfile->section_offsets.size (), slide);
654 return objfile_relocate1 (objfile, new_offsets);
655 }
656
657 /* Rebase (add to the offsets) OBJFILE by SLIDE. Process also OBJFILE's
658 SEPARATE_DEBUG_OBJFILEs. */
659
660 void
661 objfile_rebase (struct objfile *objfile, CORE_ADDR slide)
662 {
663 int changed = 0;
664
665 for (::objfile *debug_objfile : objfile->separate_debug_objfiles ())
666 changed |= objfile_rebase1 (debug_objfile, slide);
667
668 /* Relocate breakpoints as necessary, after things are relocated. */
669 if (changed)
670 breakpoint_re_set ();
671 }
672
673 /* See objfiles.h. */
674
675 bool
676 objfile::has_full_symbols ()
677 {
678 return this->compunit_symtabs != nullptr;
679 }
680
681 /* See objfiles.h. */
682
683 bool
684 objfile::has_symbols ()
685 {
686 for (::objfile *o : this->separate_debug_objfiles ())
687 if (o->has_partial_symbols () || o->has_full_symbols ())
688 return true;
689
690 return false;
691 }
692
693 /* See objfiles.h. */
694
695 bool
696 have_partial_symbols (program_space *pspace)
697 {
698 for (objfile *ofp : pspace->objfiles ())
699 if (ofp->has_partial_symbols ())
700 return true;
701
702 return false;
703 }
704
705 /* See objfiles.h. */
706
707 bool
708 have_full_symbols (program_space *pspace)
709 {
710 for (objfile *ofp : pspace->objfiles ())
711 if (ofp->has_full_symbols ())
712 return true;
713
714 return false;
715 }
716
717
718 /* See objfiles.h. */
719
720 void
721 objfile_purge_solibs (program_space *pspace)
722 {
723 for (objfile *objf : pspace->objfiles_safe ())
724 {
725 /* We assume that the solib package has been purged already, or will
726 be soon. */
727
728 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
729 objf->unlink ();
730 }
731 }
732
733 /* See objfiles.h. */
734
735 bool
736 have_minimal_symbols (program_space *pspace)
737 {
738 for (objfile *ofp : pspace->objfiles ())
739 if (ofp->per_bfd->minimal_symbol_count > 0)
740 return true;
741
742 return false;
743 }
744
745 /* Qsort comparison function. */
746
747 static bool
748 sort_cmp (const struct obj_section *sect1, const obj_section *sect2)
749 {
750 const CORE_ADDR sect1_addr = sect1->addr ();
751 const CORE_ADDR sect2_addr = sect2->addr ();
752
753 if (sect1_addr < sect2_addr)
754 return true;
755 else if (sect1_addr > sect2_addr)
756 return false;
757 else
758 {
759 /* Sections are at the same address. This could happen if
760 A) we have an objfile and a separate debuginfo.
761 B) we are confused, and have added sections without proper relocation,
762 or something like that. */
763
764 const struct objfile *const objfile1 = sect1->objfile;
765 const struct objfile *const objfile2 = sect2->objfile;
766
767 if (objfile1->separate_debug_objfile == objfile2
768 || objfile2->separate_debug_objfile == objfile1)
769 {
770 /* Case A. The ordering doesn't matter: separate debuginfo files
771 will be filtered out later. */
772
773 return false;
774 }
775
776 /* Case B. Maintain stable sort order, so bugs in GDB are easier to
777 triage. This section could be slow (since we iterate over all
778 objfiles in each call to sort_cmp), but this shouldn't happen
779 very often (GDB is already in a confused state; one hopes this
780 doesn't happen at all). If you discover that significant time is
781 spent in the loops below, do 'set complaints 100' and examine the
782 resulting complaints. */
783 if (objfile1 == objfile2)
784 {
785 /* Both sections came from the same objfile. We are really
786 confused. Sort on sequence order of sections within the
787 objfile. The order of checks is important here, if we find a
788 match on SECT2 first then either SECT2 is before SECT1, or,
789 SECT2 == SECT1, in both cases we should return false. The
790 second case shouldn't occur during normal use, but std::sort
791 does check that '!(a < a)' when compiled in debug mode. */
792
793 for (const obj_section *osect : objfile1->sections ())
794 if (osect == sect2)
795 return false;
796 else if (osect == sect1)
797 return true;
798
799 /* We should have found one of the sections before getting here. */
800 gdb_assert_not_reached ("section not found");
801 }
802 else
803 {
804 /* Sort on sequence number of the objfile in the chain. */
805
806 for (objfile *objfile : current_program_space->objfiles ())
807 if (objfile == objfile1)
808 return true;
809 else if (objfile == objfile2)
810 return false;
811
812 /* We should have found one of the objfiles before getting here. */
813 gdb_assert_not_reached ("objfile not found");
814 }
815 }
816
817 /* Unreachable. */
818 gdb_assert_not_reached ("unexpected code path");
819 return false;
820 }
821
822 /* Select "better" obj_section to keep. We prefer the one that came from
823 the real object, rather than the one from separate debuginfo.
824 Most of the time the two sections are exactly identical, but with
825 prelinking the .rel.dyn section in the real object may have different
826 size. */
827
828 static struct obj_section *
829 preferred_obj_section (struct obj_section *a, struct obj_section *b)
830 {
831 gdb_assert (a->addr () == b->addr ());
832 gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
833 || (b->objfile->separate_debug_objfile == a->objfile));
834 gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
835 || (b->objfile->separate_debug_objfile_backlink == a->objfile));
836
837 if (a->objfile->separate_debug_objfile != NULL)
838 return a;
839 return b;
840 }
841
842 /* Return 1 if SECTION should be inserted into the section map.
843 We want to insert only non-overlay non-TLS non-empty sections. */
844
845 static int
846 insert_section_p (const struct bfd *abfd,
847 const struct bfd_section *section)
848 {
849 const bfd_vma lma = bfd_section_lma (section);
850
851 if (overlay_debugging && lma != 0 && lma != bfd_section_vma (section)
852 && (bfd_get_file_flags (abfd) & BFD_IN_MEMORY) == 0)
853 /* This is an overlay section. IN_MEMORY check is needed to avoid
854 discarding sections from the "system supplied DSO" (aka vdso)
855 on some Linux systems (e.g. Fedora 11). */
856 return 0;
857 if ((bfd_section_flags (section) & SEC_THREAD_LOCAL) != 0)
858 /* This is a TLS section. */
859 return 0;
860 if (bfd_section_size (section) == 0)
861 {
862 /* This is an empty section. It has no PCs for find_pc_section (), so
863 there is no reason to insert it into the section map. */
864 return 0;
865 }
866
867 return 1;
868 }
869
870 /* Filter out overlapping sections where one section came from the real
871 objfile, and the other from a separate debuginfo file.
872 Return the size of table after redundant sections have been eliminated. */
873
874 static int
875 filter_debuginfo_sections (struct obj_section **map, int map_size)
876 {
877 int i, j;
878
879 for (i = 0, j = 0; i < map_size - 1; i++)
880 {
881 struct obj_section *const sect1 = map[i];
882 struct obj_section *const sect2 = map[i + 1];
883 const struct objfile *const objfile1 = sect1->objfile;
884 const struct objfile *const objfile2 = sect2->objfile;
885 const CORE_ADDR sect1_addr = sect1->addr ();
886 const CORE_ADDR sect2_addr = sect2->addr ();
887
888 if (sect1_addr == sect2_addr
889 && (objfile1->separate_debug_objfile == objfile2
890 || objfile2->separate_debug_objfile == objfile1))
891 {
892 map[j++] = preferred_obj_section (sect1, sect2);
893 ++i;
894 }
895 else
896 map[j++] = sect1;
897 }
898
899 if (i < map_size)
900 {
901 gdb_assert (i == map_size - 1);
902 map[j++] = map[i];
903 }
904
905 /* The map should not have shrunk to less than half the original size. */
906 gdb_assert (map_size / 2 <= j);
907
908 return j;
909 }
910
911 /* Filter out overlapping sections, issuing a warning if any are found.
912 Overlapping sections could really be overlay sections which we didn't
913 classify as such in insert_section_p, or we could be dealing with a
914 corrupt binary. */
915
916 static int
917 filter_overlapping_sections (struct obj_section **map, int map_size)
918 {
919 int i, j;
920
921 for (i = 0, j = 0; i < map_size - 1; )
922 {
923 int k;
924
925 map[j++] = map[i];
926 for (k = i + 1; k < map_size; k++)
927 {
928 struct obj_section *const sect1 = map[i];
929 struct obj_section *const sect2 = map[k];
930 const CORE_ADDR sect1_addr = sect1->addr ();
931 const CORE_ADDR sect2_addr = sect2->addr ();
932 const CORE_ADDR sect1_endaddr = sect1->endaddr ();
933
934 gdb_assert (sect1_addr <= sect2_addr);
935
936 if (sect1_endaddr <= sect2_addr)
937 break;
938 else
939 {
940 /* We have an overlap. Report it. */
941
942 struct objfile *const objf1 = sect1->objfile;
943 struct objfile *const objf2 = sect2->objfile;
944
945 const struct bfd_section *const bfds1 = sect1->the_bfd_section;
946 const struct bfd_section *const bfds2 = sect2->the_bfd_section;
947
948 const CORE_ADDR sect2_endaddr = sect2->endaddr ();
949
950 struct gdbarch *const gdbarch = objf1->arch ();
951
952 complaint (_("unexpected overlap between:\n"
953 " (A) section `%s' from `%s' [%s, %s)\n"
954 " (B) section `%s' from `%s' [%s, %s).\n"
955 "Will ignore section B"),
956 bfd_section_name (bfds1), objfile_name (objf1),
957 paddress (gdbarch, sect1_addr),
958 paddress (gdbarch, sect1_endaddr),
959 bfd_section_name (bfds2), objfile_name (objf2),
960 paddress (gdbarch, sect2_addr),
961 paddress (gdbarch, sect2_endaddr));
962 }
963 }
964 i = k;
965 }
966
967 if (i < map_size)
968 {
969 gdb_assert (i == map_size - 1);
970 map[j++] = map[i];
971 }
972
973 return j;
974 }
975
976
977 /* Update PMAP, PMAP_SIZE with sections from all objfiles, excluding any
978 TLS, overlay and overlapping sections. */
979
980 static void
981 update_section_map (struct program_space *pspace,
982 struct obj_section ***pmap, int *pmap_size)
983 {
984 struct objfile_pspace_info *pspace_info;
985 int alloc_size, map_size, i;
986 struct obj_section **map;
987
988 pspace_info = get_objfile_pspace_data (pspace);
989 gdb_assert (pspace_info->section_map_dirty != 0
990 || pspace_info->new_objfiles_available != 0);
991
992 map = *pmap;
993 xfree (map);
994
995 alloc_size = 0;
996 for (objfile *objfile : pspace->objfiles ())
997 for (obj_section *s : objfile->sections ())
998 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
999 alloc_size += 1;
1000
1001 /* This happens on detach/attach (e.g. in gdb.base/attach.exp). */
1002 if (alloc_size == 0)
1003 {
1004 *pmap = NULL;
1005 *pmap_size = 0;
1006 return;
1007 }
1008
1009 map = XNEWVEC (struct obj_section *, alloc_size);
1010
1011 i = 0;
1012 for (objfile *objfile : pspace->objfiles ())
1013 for (obj_section *s : objfile->sections ())
1014 if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
1015 map[i++] = s;
1016
1017 std::sort (map, map + alloc_size, sort_cmp);
1018 map_size = filter_debuginfo_sections(map, alloc_size);
1019 map_size = filter_overlapping_sections(map, map_size);
1020
1021 if (map_size < alloc_size)
1022 /* Some sections were eliminated. Trim excess space. */
1023 map = XRESIZEVEC (struct obj_section *, map, map_size);
1024 else
1025 gdb_assert (alloc_size == map_size);
1026
1027 *pmap = map;
1028 *pmap_size = map_size;
1029 }
1030
1031 /* Bsearch comparison function. */
1032
1033 static int
1034 bsearch_cmp (const void *key, const void *elt)
1035 {
1036 const CORE_ADDR pc = *(CORE_ADDR *) key;
1037 const struct obj_section *section = *(const struct obj_section **) elt;
1038
1039 if (pc < section->addr ())
1040 return -1;
1041 if (pc < section->endaddr ())
1042 return 0;
1043 return 1;
1044 }
1045
1046 /* Returns a section whose range includes PC or NULL if none found. */
1047
1048 struct obj_section *
1049 find_pc_section (CORE_ADDR pc)
1050 {
1051 struct objfile_pspace_info *pspace_info;
1052 struct obj_section *s, **sp;
1053
1054 /* Check for mapped overlay section first. */
1055 s = find_pc_mapped_section (pc);
1056 if (s)
1057 return s;
1058
1059 pspace_info = get_objfile_pspace_data (current_program_space);
1060 if (pspace_info->section_map_dirty
1061 || (pspace_info->new_objfiles_available
1062 && !pspace_info->inhibit_updates))
1063 {
1064 update_section_map (current_program_space,
1065 &pspace_info->sections,
1066 &pspace_info->num_sections);
1067
1068 /* Don't need updates to section map until objfiles are added,
1069 removed or relocated. */
1070 pspace_info->new_objfiles_available = 0;
1071 pspace_info->section_map_dirty = 0;
1072 }
1073
1074 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1075 bsearch be non-NULL. */
1076 if (pspace_info->sections == NULL)
1077 {
1078 gdb_assert (pspace_info->num_sections == 0);
1079 return NULL;
1080 }
1081
1082 sp = (struct obj_section **) bsearch (&pc,
1083 pspace_info->sections,
1084 pspace_info->num_sections,
1085 sizeof (*pspace_info->sections),
1086 bsearch_cmp);
1087 if (sp != NULL)
1088 return *sp;
1089 return NULL;
1090 }
1091
1092
1093 /* Return non-zero if PC is in a section called NAME. */
1094
1095 bool
1096 pc_in_section (CORE_ADDR pc, const char *name)
1097 {
1098 struct obj_section *s = find_pc_section (pc);
1099 return (s != nullptr
1100 && s->the_bfd_section->name != nullptr
1101 && strcmp (s->the_bfd_section->name, name) == 0);
1102 }
1103
1104 /* See objfiles.h. */
1105
1106 void
1107 objfiles_changed (program_space *pspace)
1108 {
1109 /* Rebuild section map next time we need it. */
1110 get_objfile_pspace_data (pspace)->section_map_dirty = 1;
1111 }
1112
1113 /* See comments in objfiles.h. */
1114
1115 scoped_restore_tmpl<int>
1116 inhibit_section_map_updates (struct program_space *pspace)
1117 {
1118 return scoped_restore_tmpl<int>
1119 (&get_objfile_pspace_data (pspace)->inhibit_updates, 1);
1120 }
1121
1122 /* See objfiles.h. */
1123
1124 bool
1125 is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile)
1126 {
1127 if (objfile == NULL)
1128 return false;
1129
1130 for (obj_section *osect : objfile->sections ())
1131 {
1132 if (section_is_overlay (osect) && !section_is_mapped (osect))
1133 continue;
1134
1135 if (osect->contains (addr))
1136 return true;
1137 }
1138 return false;
1139 }
1140
1141 /* See objfiles.h. */
1142
1143 bool
1144 shared_objfile_contains_address_p (struct program_space *pspace,
1145 CORE_ADDR address)
1146 {
1147 for (objfile *objfile : pspace->objfiles ())
1148 {
1149 if ((objfile->flags & OBJF_SHARED) != 0
1150 && is_addr_in_objfile (address, objfile))
1151 return true;
1152 }
1153
1154 return false;
1155 }
1156
1157 /* The default implementation for the "iterate_over_objfiles_in_search_order"
1158 gdbarch method. It is equivalent to use the objfiles iterable,
1159 searching the objfiles in the order they are stored internally,
1160 ignoring CURRENT_OBJFILE.
1161
1162 On most platforms, it should be close enough to doing the best
1163 we can without some knowledge specific to the architecture. */
1164
1165 void
1166 default_iterate_over_objfiles_in_search_order
1167 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
1168 objfile *current_objfile)
1169 {
1170 for (objfile *objfile : current_program_space->objfiles ())
1171 if (cb (objfile))
1172 return;
1173 }
1174
1175 /* See objfiles.h. */
1176
1177 const char *
1178 objfile_name (const struct objfile *objfile)
1179 {
1180 if (objfile->obfd != NULL)
1181 return bfd_get_filename (objfile->obfd.get ());
1182
1183 return objfile->original_name;
1184 }
1185
1186 /* See objfiles.h. */
1187
1188 const char *
1189 objfile_filename (const struct objfile *objfile)
1190 {
1191 if (objfile->obfd != NULL)
1192 return bfd_get_filename (objfile->obfd.get ());
1193
1194 return NULL;
1195 }
1196
1197 /* See objfiles.h. */
1198
1199 const char *
1200 objfile_debug_name (const struct objfile *objfile)
1201 {
1202 return lbasename (objfile->original_name);
1203 }
1204
1205 /* See objfiles.h. */
1206
1207 const char *
1208 objfile_flavour_name (struct objfile *objfile)
1209 {
1210 if (objfile->obfd != NULL)
1211 return bfd_flavour_name (bfd_get_flavour (objfile->obfd.get ()));
1212 return NULL;
1213 }
1214
1215 /* See objfiles.h. */
1216
1217 struct type *
1218 objfile_int_type (struct objfile *of, int size_in_bytes, bool unsigned_p)
1219 {
1220 struct type *int_type;
1221
1222 /* Helper macro to examine the various builtin types. */
1223 #define TRY_TYPE(F) \
1224 int_type = (unsigned_p \
1225 ? builtin_type (of)->builtin_unsigned_ ## F \
1226 : builtin_type (of)->builtin_ ## F); \
1227 if (int_type != NULL && int_type->length () == size_in_bytes) \
1228 return int_type
1229
1230 TRY_TYPE (char);
1231 TRY_TYPE (short);
1232 TRY_TYPE (int);
1233 TRY_TYPE (long);
1234 TRY_TYPE (long_long);
1235
1236 #undef TRY_TYPE
1237
1238 gdb_assert_not_reached ("unable to find suitable integer type");
1239 }