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