Home | History | Annotate | Line # | Download | only in gdb
      1 /* Definitions for symbol file management in GDB.
      2 
      3    Copyright (C) 1992-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef GDB_OBJFILES_H
     21 #define GDB_OBJFILES_H
     22 
     23 #include "gdbsupport/gdb_obstack.h"
     24 #include "objfile-flags.h"
     25 #include "symfile.h"
     26 #include "progspace.h"
     27 #include "registry.h"
     28 #include "gdb_bfd.h"
     29 #include <bitset>
     30 #include "bcache.h"
     31 #include "gdbarch.h"
     32 #include "jit.h"
     33 #include "quick-symbol.h"
     34 #include <forward_list>
     35 #include "gdbsupport/unordered_map.h"
     36 
     37 struct htab;
     38 struct objfile_data;
     39 struct partial_symbol;
     40 
     41 /* This structure maintains information on a per-objfile basis about the
     42    "entry point" of the objfile, and the scope within which the entry point
     43    exists.  It is possible that gdb will see more than one objfile that is
     44    executable, each with its own entry point.
     45 
     46    For example, for dynamically linked executables in SVR4, the dynamic linker
     47    code is contained within the shared C library, which is actually executable
     48    and is run by the kernel first when an exec is done of a user executable
     49    that is dynamically linked.  The dynamic linker within the shared C library
     50    then maps in the various program segments in the user executable and jumps
     51    to the user executable's recorded entry point, as if the call had been made
     52    directly by the kernel.
     53 
     54    The traditional gdb method of using this info was to use the
     55    recorded entry point to set the entry-file's lowpc and highpc from
     56    the debugging information, where these values are the starting
     57    address (inclusive) and ending address (exclusive) of the
     58    instruction space in the executable which correspond to the
     59    "startup file", i.e. crt0.o in most cases.  This file is assumed to
     60    be a startup file and frames with pc's inside it are treated as
     61    nonexistent.  Setting these variables is necessary so that
     62    backtraces do not fly off the bottom of the stack.
     63 
     64    NOTE: cagney/2003-09-09: It turns out that this "traditional"
     65    method doesn't work.  Corinna writes: ``It turns out that the call
     66    to test for "inside entry file" destroys a meaningful backtrace
     67    under some conditions.  E.g. the backtrace tests in the asm-source
     68    testcase are broken for some targets.  In this test the functions
     69    are all implemented as part of one file and the testcase is not
     70    necessarily linked with a start file (depending on the target).
     71    What happens is, that the first frame is printed normally and
     72    following frames are treated as being inside the entry file then.
     73    This way, only the #0 frame is printed in the backtrace output.''
     74    Ref "frame.c" "NOTE: vinschen/2003-04-01".
     75 
     76    Gdb also supports an alternate method to avoid running off the bottom
     77    of the stack.
     78 
     79    There are two frames that are "special", the frame for the function
     80    containing the process entry point, since it has no predecessor frame,
     81    and the frame for the function containing the user code entry point
     82    (the main() function), since all the predecessor frames are for the
     83    process startup code.  Since we have no guarantee that the linked
     84    in startup modules have any debugging information that gdb can use,
     85    we need to avoid following frame pointers back into frames that might
     86    have been built in the startup code, as we might get hopelessly
     87    confused.  However, we almost always have debugging information
     88    available for main().
     89 
     90    These variables are used to save the range of PC values which are
     91    valid within the main() function and within the function containing
     92    the process entry point.  If we always consider the frame for
     93    main() as the outermost frame when debugging user code, and the
     94    frame for the process entry point function as the outermost frame
     95    when debugging startup code, then all we have to do is have
     96    DEPRECATED_FRAME_CHAIN_VALID return false whenever a frame's
     97    current PC is within the range specified by these variables.  In
     98    essence, we set "ceilings" in the frame chain beyond which we will
     99    not proceed when following the frame chain back up the stack.
    100 
    101    A nice side effect is that we can still debug startup code without
    102    running off the end of the frame chain, assuming that we have usable
    103    debugging information in the startup modules, and if we choose to not
    104    use the block at main, or can't find it for some reason, everything
    105    still works as before.  And if we have no startup code debugging
    106    information but we do have usable information for main(), backtraces
    107    from user code don't go wandering off into the startup code.  */
    108 
    109 struct entry_info
    110 {
    111   /* The unrelocated value we should use for this objfile entry point.  */
    112   CORE_ADDR entry_point;
    113 
    114   /* The index of the section in which the entry point appears.  */
    115   int the_bfd_section_index;
    116 
    117   /* Set to 1 iff ENTRY_POINT contains a valid value.  */
    118   unsigned entry_point_p : 1;
    119 
    120   /* Set to 1 iff this object was initialized.  */
    121   unsigned initialized : 1;
    122 };
    123 
    124 #define SECT_OFF_DATA(objfile) \
    125      ((objfile->sect_index_data == -1) \
    126       ? (internal_error (_("sect_index_data not initialized")), -1)	\
    127       : objfile->sect_index_data)
    128 
    129 #define SECT_OFF_RODATA(objfile) \
    130      ((objfile->sect_index_rodata == -1) \
    131       ? (internal_error (_("sect_index_rodata not initialized")), -1)	\
    132       : objfile->sect_index_rodata)
    133 
    134 #define SECT_OFF_TEXT(objfile) \
    135      ((objfile->sect_index_text == -1) \
    136       ? (internal_error (_("sect_index_text not initialized")), -1)	\
    137       : objfile->sect_index_text)
    138 
    139 /* Sometimes the .bss section is missing from the objfile, so we don't
    140    want to die here.  Let the users of SECT_OFF_BSS deal with an
    141    uninitialized section index.  */
    142 #define SECT_OFF_BSS(objfile) (objfile)->sect_index_bss
    143 
    144 /* The "objstats" structure provides a place for gdb to record some
    145    interesting information about its internal state at runtime, on a
    146    per objfile basis, such as information about the number of symbols
    147    read, size of string table (if any), etc.  */
    148 
    149 struct objstats
    150 {
    151   /* Number of full symbols read.  */
    152   int n_syms = 0;
    153 
    154   /* Number of ".stabs" read (if applicable).  */
    155   int n_stabs = 0;
    156 
    157   /* Number of types.  */
    158   int n_types = 0;
    159 
    160   /* Size of stringtable, (if applicable).  */
    161   int sz_strtab = 0;
    162 };
    163 
    164 #define OBJSTAT(objfile, expr) (objfile -> stats.expr)
    165 #define OBJSTATS struct objstats stats
    166 extern void print_objfile_statistics (void);
    167 
    168 /* Number of entries in the minimal symbol hash table.  */
    169 #define MINIMAL_SYMBOL_HASH_SIZE 2039
    170 
    171 /* An iterator for minimal symbols.  */
    172 
    173 struct minimal_symbol_iterator
    174 {
    175   typedef minimal_symbol_iterator self_type;
    176   typedef struct minimal_symbol *value_type;
    177   typedef struct minimal_symbol *&reference;
    178   typedef struct minimal_symbol **pointer;
    179   typedef std::forward_iterator_tag iterator_category;
    180   typedef int difference_type;
    181 
    182   explicit minimal_symbol_iterator (struct minimal_symbol *msym)
    183     : m_msym (msym)
    184   {
    185   }
    186 
    187   value_type operator* () const
    188   {
    189     return m_msym;
    190   }
    191 
    192   bool operator== (const self_type &other) const
    193   {
    194     return m_msym == other.m_msym;
    195   }
    196 
    197   bool operator!= (const self_type &other) const
    198   {
    199     return m_msym != other.m_msym;
    200   }
    201 
    202   self_type &operator++ ()
    203   {
    204     ++m_msym;
    205     return *this;
    206   }
    207 
    208 private:
    209   struct minimal_symbol *m_msym;
    210 };
    211 
    212 /* Some objfile data is hung off the BFD.  This enables sharing of the
    213    data across all objfiles using the BFD.  The data is stored in an
    214    instance of this structure, and associated with the BFD using the
    215    registry system.  */
    216 
    217 struct objfile_per_bfd_storage
    218 {
    219   objfile_per_bfd_storage (bfd *bfd)
    220     : minsyms_read (false), m_bfd (bfd)
    221   {}
    222 
    223   ~objfile_per_bfd_storage ();
    224 
    225   /* Intern STRING in this object's string cache and return the unique copy.
    226      The copy has the same lifetime as this object.
    227 
    228      STRING must be null-terminated.  */
    229 
    230   const char *intern (const char *str)
    231   {
    232     return string_cache.insert (str, strlen (str) + 1);
    233   }
    234 
    235   /* Same as the above, but for an std::string.  */
    236 
    237   const char *intern (const std::string &str)
    238   {
    239     return string_cache.insert (str.c_str (), str.size () + 1);
    240   }
    241 
    242   /* Get the BFD this object is associated to.  */
    243 
    244   bfd *get_bfd () const
    245   {
    246     return m_bfd;
    247   }
    248 
    249   /* The storage has an obstack of its own.  */
    250 
    251   auto_obstack storage_obstack;
    252 
    253   /* String cache.  */
    254 
    255   gdb::bcache string_cache;
    256 
    257   /* The gdbarch associated with the BFD.  Note that this gdbarch is
    258      determined solely from BFD information, without looking at target
    259      information.  The gdbarch determined from a running target may
    260      differ from this e.g. with respect to register types and names.  */
    261 
    262   struct gdbarch *gdbarch = NULL;
    263 
    264   /* Hash table for mapping symbol names to demangled names.  Each
    265      entry in the hash table is a demangled_name_entry struct, storing the
    266      language and two consecutive strings, both null-terminated; the first one
    267      is a mangled or linkage name, and the second is the demangled name or just
    268      a zero byte if the name doesn't demangle.  */
    269 
    270   htab_up demangled_names_hash;
    271 
    272   /* The per-objfile information about the entry point, the scope (file/func)
    273      containing the entry point, and the scope of the user's main() func.  */
    274 
    275   entry_info ei {};
    276 
    277   /* The name and language of any "main" found in this objfile.  The
    278      name can be NULL, which means that the information was not
    279      recorded.  */
    280 
    281   const char *name_of_main = NULL;
    282   enum language language_of_main = language_unknown;
    283 
    284   /* Each file contains a pointer to an array of minimal symbols for all
    285      global symbols that are defined within the file.  The array is
    286      terminated by a "null symbol", one that has a NULL pointer for the
    287      name and a zero value for the address.  This makes it easy to walk
    288      through the array when passed a pointer to somewhere in the middle
    289      of it.  There is also a count of the number of symbols, which does
    290      not include the terminating null symbol.  */
    291 
    292   gdb::unique_xmalloc_ptr<minimal_symbol> msymbols;
    293   int minimal_symbol_count = 0;
    294 
    295   /* The number of minimal symbols read, before any minimal symbol
    296      de-duplication is applied.  Note in particular that this has only
    297      a passing relationship with the actual size of the table above;
    298      use minimal_symbol_count if you need the true size.  */
    299 
    300   int n_minsyms = 0;
    301 
    302   /* This is true if minimal symbols have already been read.  Symbol
    303      readers can use this to bypass minimal symbol reading.  Also, the
    304      minimal symbol table management code in minsyms.c uses this to
    305      suppress new minimal symbols.  You might think that MSYMBOLS or
    306      MINIMAL_SYMBOL_COUNT could be used for this, but it is possible
    307      for multiple readers to install minimal symbols into a given
    308      per-BFD.  */
    309 
    310   bool minsyms_read : 1;
    311 
    312   /* This is a hash table used to index the minimal symbols by (mangled)
    313      name.  */
    314 
    315   minimal_symbol *msymbol_hash[MINIMAL_SYMBOL_HASH_SIZE] {};
    316 
    317   /* This hash table is used to index the minimal symbols by their
    318      demangled names.  Uses a language-specific hash function via
    319      search_name_hash.  */
    320 
    321   minimal_symbol *msymbol_demangled_hash[MINIMAL_SYMBOL_HASH_SIZE] {};
    322 
    323   /* All the different languages of symbols found in the demangled
    324      hash table.  */
    325   std::bitset<nr_languages> demangled_hash_languages;
    326 
    327 private:
    328   /* The BFD this object is associated to.  */
    329 
    330   bfd *m_bfd;
    331 };
    332 
    333 /* An iterator that first returns a parent objfile, and then each
    334    separate debug objfile.  */
    335 
    336 class separate_debug_iterator
    337 {
    338 public:
    339 
    340   explicit separate_debug_iterator (struct objfile *objfile)
    341     : m_objfile (objfile),
    342       m_parent (objfile)
    343   {
    344   }
    345 
    346   bool operator!= (const separate_debug_iterator &other)
    347   {
    348     return m_objfile != other.m_objfile;
    349   }
    350 
    351   separate_debug_iterator &operator++ ();
    352 
    353   struct objfile *operator* ()
    354   {
    355     return m_objfile;
    356   }
    357 
    358 private:
    359 
    360   struct objfile *m_objfile;
    361   struct objfile *m_parent;
    362 };
    363 
    364 /* A range adapter wrapping separate_debug_iterator.  */
    365 
    366 typedef iterator_range<separate_debug_iterator> separate_debug_range;
    367 
    368 /* Sections in an objfile.  The section offsets are stored in the
    369    OBJFILE.  */
    370 
    371 struct obj_section
    372 {
    373   /* Relocation offset applied to the section.  */
    374   CORE_ADDR offset () const;
    375 
    376   /* Set the relocation offset applied to the section.  */
    377   void set_offset (CORE_ADDR offset);
    378 
    379   /* The memory address of the section (vma + offset).  */
    380   CORE_ADDR addr () const
    381   {
    382     return bfd_section_vma (this->the_bfd_section) + this->offset ();
    383   }
    384 
    385   /* The one-passed-the-end memory address of the section
    386      (vma + size + offset).  */
    387   CORE_ADDR endaddr () const
    388   {
    389     return this->addr () + bfd_section_size (this->the_bfd_section);
    390   }
    391 
    392   /* True if ADDR is in this obj_section, false otherwise.  */
    393   bool contains (CORE_ADDR addr) const
    394   {
    395     return addr >= this->addr () && addr < endaddr ();
    396   }
    397 
    398   /* BFD section pointer */
    399   struct bfd_section *the_bfd_section;
    400 
    401   /* Objfile this section is part of.  */
    402   struct objfile *objfile;
    403 
    404   /* True if this "overlay section" is mapped into an "overlay region".  */
    405   int ovly_mapped;
    406 };
    407 
    408 /* Master structure for keeping track of each file from which
    409    gdb reads symbols.  There are several ways these get allocated: 1.
    410    The main symbol file, symfile_objfile, set by the symbol-file command,
    411    2.  Additional symbol files added by the add-symbol-file command,
    412    3.  Shared library objfiles, added by ADD_SOLIB,  4.  symbol files
    413    for modules that were loaded when GDB attached to a remote system
    414    (see remote-vx.c).
    415 
    416    GDB typically reads symbols twice -- first an initial scan which just
    417    reads "partial symbols"; these are partial information for the
    418    static/global symbols in a symbol file.  When later looking up
    419    symbols, lookup_symbol is used to check if we only have a partial
    420    symbol and if so, read and expand the full compunit.  */
    421 
    422 struct objfile : intrusive_list_node<objfile>
    423 {
    424 private:
    425 
    426   /* The only way to create an objfile is to call objfile::make.  */
    427   objfile (gdb_bfd_ref_ptr, program_space *pspace, const char *,
    428 	   objfile_flags);
    429 
    430 public:
    431 
    432   /* Normally you should not call delete.  Instead, call 'unlink' to
    433      remove it from the program space's list.  In some cases, you may
    434      need to hold a reference to an objfile that is independent of its
    435      existence on the program space's list; for this case, the
    436      destructor must be public so that unique_ptr can reference
    437      it.  */
    438   ~objfile ();
    439 
    440   /* Create an objfile.  */
    441   static objfile *make (gdb_bfd_ref_ptr bfd_, program_space *pspace,
    442 			const char *name_, objfile_flags flags_,
    443 			objfile *parent = nullptr);
    444 
    445   /* Remove this objfile from its program space's objfile list, and frees
    446      it.  */
    447   void unlink ();
    448 
    449   DISABLE_COPY_AND_ASSIGN (objfile);
    450 
    451   /* Return the program space associated with this objfile.  */
    452   program_space *pspace () { return m_pspace; }
    453 
    454   /* A range adapter that makes it possible to iterate over all
    455      compunits in one objfile.  */
    456 
    457   compunit_symtab_range compunits ()
    458   {
    459     return compunit_symtab_range (compunit_symtabs);
    460   }
    461 
    462   /* A range adapter that makes it possible to iterate over all
    463      minimal symbols of an objfile.  */
    464 
    465   typedef iterator_range<minimal_symbol_iterator> msymbols_range;
    466 
    467   /* Return a range adapter for iterating over all minimal
    468      symbols.  */
    469 
    470   msymbols_range msymbols ()
    471   {
    472     auto start = minimal_symbol_iterator (per_bfd->msymbols.get ());
    473     auto end = minimal_symbol_iterator (per_bfd->msymbols.get ()
    474 					+ per_bfd->minimal_symbol_count);
    475     return msymbols_range (start, end);
    476   }
    477 
    478   /* Return a range adapter for iterating over all the separate debug
    479      objfiles of this objfile.  */
    480 
    481   separate_debug_range separate_debug_objfiles ()
    482   {
    483     auto start = separate_debug_iterator (this);
    484     auto end = separate_debug_iterator (nullptr);
    485     return separate_debug_range (start, end);
    486   }
    487 
    488   CORE_ADDR text_section_offset () const
    489   {
    490     return section_offsets[SECT_OFF_TEXT (this)];
    491   }
    492 
    493   CORE_ADDR data_section_offset () const
    494   {
    495     return section_offsets[SECT_OFF_DATA (this)];
    496   }
    497 
    498   /* Intern STRING and return the unique copy.  The copy has the same
    499      lifetime as the per-BFD object.  */
    500   const char *intern (const char *str)
    501   {
    502     return per_bfd->intern (str);
    503   }
    504 
    505   /* Intern STRING and return the unique copy.  The copy has the same
    506      lifetime as the per-BFD object.  */
    507   const char *intern (const std::string &str)
    508   {
    509     return per_bfd->intern (str);
    510   }
    511 
    512   /* Retrieve the gdbarch associated with this objfile.  */
    513   struct gdbarch *arch () const
    514   {
    515     return per_bfd->gdbarch;
    516   }
    517 
    518   /* Return true if OBJFILE has partial symbols.  */
    519 
    520   bool has_partial_symbols ();
    521 
    522   /* Look for a separate debug symbol file for this objfile, make use of
    523      build-id, debug-link, and debuginfod as necessary.  If a suitable
    524      separate debug symbol file is found then it is loaded using a call to
    525      symbol_file_add_separate (SYMFILE_FLAGS is passed through unmodified
    526      to this call) and this function returns true.  If no suitable separate
    527      debug symbol file is found and loaded then this function returns
    528      false.  */
    529 
    530   bool find_and_add_separate_symbol_file (symfile_add_flags symfile_flags);
    531 
    532   /* Return true if this objfile has any unexpanded symbols.  A return
    533      value of false indicates either, that this objfile has all its
    534      symbols fully expanded (i.e. fully read in), or that this objfile has
    535      no symbols at all (i.e. no debug information).  */
    536   bool has_unexpanded_symtabs ();
    537 
    538   /* See quick_symbol_functions.  */
    539   struct symtab *find_last_source_symtab ();
    540 
    541   /* See quick_symbol_functions.  */
    542   void forget_cached_source_info ();
    543 
    544   /* Expand and iterate over each "partial" symbol table in OBJFILE
    545      where the source file is named NAME.
    546 
    547      If NAME is not absolute, a match after a '/' in the symbol table's
    548      file name will also work, REAL_PATH is NULL then.  If NAME is
    549      absolute then REAL_PATH is non-NULL absolute file name as resolved
    550      via gdb_realpath from NAME.
    551 
    552      If a match is found, the "partial" symbol table is expanded.
    553      Then, this calls iterate_over_some_symtabs (or equivalent) over
    554      all newly-created symbol tables, passing CALLBACK to it.
    555      The result of this call is returned.  */
    556   bool map_symtabs_matching_filename
    557     (const char *name, const char *real_path,
    558      gdb::function_view<bool (symtab *)> callback);
    559 
    560   /* Check to see if the symbol is defined in a "partial" symbol table
    561      of this objfile.  BLOCK_INDEX should be either GLOBAL_BLOCK or
    562      STATIC_BLOCK, depending on whether we want to search global
    563      symbols or static symbols.  NAME is the name of the symbol to
    564      look for.  DOMAIN indicates what sort of symbol to search for.
    565 
    566      Returns the newly-expanded compunit in which the symbol is
    567      defined, or NULL if no such symbol table exists.  If OBJFILE
    568      contains !TYPE_OPAQUE symbol prefer its compunit.  If it contains
    569      only TYPE_OPAQUE symbol(s), return at least that compunit.  */
    570   struct compunit_symtab *lookup_symbol (block_enum kind,
    571 					 const lookup_name_info &name,
    572 					 domain_search_flags domain);
    573 
    574   /* See quick_symbol_functions.  */
    575   void print_stats (bool print_bcache);
    576 
    577   /* See quick_symbol_functions.  */
    578   void dump ();
    579 
    580   /* Find all the symbols in OBJFILE named FUNC_NAME, and ensure that
    581      the corresponding symbol tables are loaded.  */
    582   void expand_symtabs_for_function (const char *func_name);
    583 
    584   /* See quick_symbol_functions.  */
    585   void expand_all_symtabs ();
    586 
    587   /* Read all symbol tables associated with OBJFILE which have
    588      symtab_to_fullname equal to FULLNAME.
    589      This is for the purposes of examining code only, e.g., expand_line_sal.
    590      The routine may ignore debug info that is known to not be useful with
    591      code, e.g., DW_TAG_type_unit for dwarf debug info.  */
    592   void expand_symtabs_with_fullname (const char *fullname);
    593 
    594   /* See quick_symbol_functions.  */
    595   bool expand_symtabs_matching
    596     (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
    597      const lookup_name_info *lookup_name,
    598      gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
    599      gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    600      block_search_flags search_flags,
    601      domain_search_flags domain,
    602      gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher
    603        = nullptr);
    604 
    605   /* See quick_symbol_functions.  */
    606   struct compunit_symtab *
    607   find_pc_sect_compunit_symtab (bound_minimal_symbol msymbol, CORE_ADDR pc,
    608 				struct obj_section *section,
    609 				int warn_if_readin);
    610 
    611   /* See quick_symbol_functions.  */
    612   void map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
    613 			     bool need_fullname);
    614 
    615   /* See quick_symbol_functions.  */
    616   void compute_main_name ();
    617 
    618   /* See quick_symbol_functions.  */
    619   struct compunit_symtab *find_compunit_symtab_by_address (CORE_ADDR address);
    620 
    621   /* See quick_symbol_functions.  */
    622   enum language lookup_global_symbol_language (const char *name,
    623 					       domain_search_flags domain,
    624 					       bool *symbol_found_p);
    625 
    626   /* Return the relocation offset applied to SECTION.  */
    627   CORE_ADDR section_offset (bfd_section *section) const
    628   {
    629     /* The section's owner can be nullptr if it is one of the _bfd_std_section
    630        section.  */
    631     gdb_assert (section->owner == nullptr || section->owner == this->obfd);
    632 
    633     int idx = gdb_bfd_section_index (this->obfd.get (), section);
    634 
    635     /* Guarantee that the section offsets were initialized.  */
    636     gdb_assert (this->section_offsets.size () > idx);
    637     return this->section_offsets[idx];
    638   }
    639 
    640   /* Set the relocation offset applied to SECTION.  */
    641   void set_section_offset (bfd_section *section, CORE_ADDR offset)
    642   {
    643     /* The section's owner can be nullptr if it is one of the _bfd_std_section
    644        section.  */
    645     gdb_assert (section->owner == nullptr || section->owner == this->obfd);
    646 
    647     int idx = gdb_bfd_section_index (this->obfd.get (), section);
    648 
    649     /* Guarantee that the section offsets were initialized.  */
    650     gdb_assert (this->section_offsets.capacity () > idx);
    651     this->section_offsets[idx] = offset;
    652   }
    653 
    654   class section_iterator
    655   {
    656   public:
    657     section_iterator (const section_iterator &) = default;
    658     section_iterator (section_iterator &&) = default;
    659     section_iterator &operator= (const section_iterator &) = default;
    660     section_iterator &operator= (section_iterator &&) = default;
    661 
    662     typedef section_iterator self_type;
    663     typedef obj_section *value_type;
    664 
    665     value_type operator* ()
    666     { return m_iter; }
    667 
    668     section_iterator &operator++ ()
    669     {
    670       ++m_iter;
    671       skip_null ();
    672       return *this;
    673     }
    674 
    675     bool operator== (const section_iterator &other) const
    676     { return m_iter == other.m_iter && m_end == other.m_end; }
    677 
    678     bool operator!= (const section_iterator &other) const
    679     { return !(*this == other); }
    680 
    681   private:
    682 
    683     friend class objfile;
    684 
    685     section_iterator (obj_section *iter, obj_section *end)
    686       : m_iter (iter),
    687 	m_end (end)
    688     {
    689       skip_null ();
    690     }
    691 
    692     void skip_null ()
    693     {
    694       while (m_iter < m_end && m_iter->the_bfd_section == nullptr)
    695 	++m_iter;
    696     }
    697 
    698     value_type m_iter;
    699     value_type m_end;
    700   };
    701 
    702   iterator_range<section_iterator> sections ()
    703   {
    704     return (iterator_range<section_iterator>
    705 	    (section_iterator (sections_start, sections_end),
    706 	     section_iterator (sections_end, sections_end)));
    707   }
    708 
    709   iterator_range<section_iterator> sections () const
    710   {
    711     return (iterator_range<section_iterator>
    712 	    (section_iterator (sections_start, sections_end),
    713 	     section_iterator (sections_end, sections_end)));
    714   }
    715 
    716 public:
    717 
    718   /* The object file's original name as specified by the user,
    719      made absolute, and tilde-expanded.  However, it is not canonicalized
    720      (i.e., it has not been passed through gdb_realpath).
    721      This pointer is never NULL.  This does not have to be freed; it is
    722      guaranteed to have a lifetime at least as long as the objfile.  */
    723 
    724   const char *original_name = nullptr;
    725 
    726   CORE_ADDR addr_low = 0;
    727 
    728   /* Some flag bits for this objfile.  */
    729 
    730   objfile_flags flags;
    731 
    732 private:
    733   /* The program space associated with this objfile.  */
    734 
    735   program_space *m_pspace;
    736 
    737 public:
    738   /* List of compunits.
    739      These are used to do symbol lookups and file/line-number lookups.  */
    740 
    741   struct compunit_symtab *compunit_symtabs = nullptr;
    742 
    743   /* The object file's BFD.  Can be null if the objfile contains only
    744      minimal symbols (e.g. the run time common symbols for SunOS4) or
    745      if the objfile is a dynamic objfile (e.g. created by JIT reader
    746      API).  */
    747 
    748   gdb_bfd_ref_ptr obfd;
    749 
    750   /* The per-BFD data.  */
    751 
    752   struct objfile_per_bfd_storage *per_bfd = nullptr;
    753 
    754   /* In some cases, the per_bfd object is owned by this objfile and
    755      not by the BFD itself.  In this situation, this holds the owning
    756      pointer.  */
    757 
    758   std::unique_ptr<objfile_per_bfd_storage> per_bfd_storage;
    759 
    760   /* The modification timestamp of the object file, as of the last time
    761      we read its symbols.  */
    762 
    763   long mtime = 0;
    764 
    765   /* Obstack to hold objects that should be freed when we load a new symbol
    766      table from this object file.  */
    767 
    768   auto_obstack objfile_obstack;
    769 
    770   /* Structure which keeps track of functions that manipulate objfile's
    771      of the same type as this objfile.  I.e. the function to read partial
    772      symbols for example.  Note that this structure is in statically
    773      allocated memory, and is shared by all objfiles that use the
    774      object module reader of this type.  */
    775 
    776   const struct sym_fns *sf = nullptr;
    777 
    778   /* The "quick" (aka partial) symbol functions for this symbol
    779      reader.  */
    780   std::forward_list<quick_symbol_functions_up> qf;
    781 
    782   /* Per objfile data-pointers required by other GDB modules.  */
    783 
    784   registry<objfile> registry_fields;
    785 
    786   /* Set of relocation offsets to apply to each section.
    787      The table is indexed by the_bfd_section->index, thus it is generally
    788      as large as the number of sections in the binary.
    789 
    790      These offsets indicate that all symbols (including partial and
    791      minimal symbols) which have been read have been relocated by this
    792      much.  Symbols which are yet to be read need to be relocated by it.  */
    793 
    794   ::section_offsets section_offsets;
    795 
    796   /* Indexes in the section_offsets array.  These are initialized by the
    797      *_symfile_offsets() family of functions (som_symfile_offsets,
    798      xcoff_symfile_offsets, default_symfile_offsets).  In theory they
    799      should correspond to the section indexes used by bfd for the
    800      current objfile.  The exception to this for the time being is the
    801      SOM version.
    802 
    803      These are initialized to -1 so that we can later detect if they
    804      are used w/o being properly assigned to.  */
    805 
    806   int sect_index_text = -1;
    807   int sect_index_data = -1;
    808   int sect_index_bss = -1;
    809   int sect_index_rodata = -1;
    810 
    811   /* These pointers are used to locate the section table, which among
    812      other things, is used to map pc addresses into sections.
    813      SECTIONS_START points to the first entry in the table, and
    814      SECTIONS_END points to the first location past the last entry in
    815      the table.  The table is stored on the objfile_obstack.  The
    816      sections are indexed by the BFD section index; but the structure
    817      data is only valid for certain sections (e.g. non-empty,
    818      SEC_ALLOC).  */
    819 
    820   struct obj_section *sections_start = nullptr;
    821   struct obj_section *sections_end = nullptr;
    822 
    823   /* GDB allows to have debug symbols in separate object files.  This is
    824      used by .gnu_debuglink, ELF build id note and Mach-O OSO.
    825      Although this is a tree structure, GDB only support one level
    826      (ie a separate debug for a separate debug is not supported).  Note that
    827      separate debug object are in the main chain and therefore will be
    828      visited by objfiles & co iterators.  Separate debug objfile always
    829      has a non-nul separate_debug_objfile_backlink.  */
    830 
    831   /* Link to the first separate debug object, if any.  */
    832 
    833   struct objfile *separate_debug_objfile = nullptr;
    834 
    835   /* If this is a separate debug object, this is used as a link to the
    836      actual executable objfile.  */
    837 
    838   struct objfile *separate_debug_objfile_backlink = nullptr;
    839 
    840   /* If this is a separate debug object, this is a link to the next one
    841      for the same executable objfile.  */
    842 
    843   struct objfile *separate_debug_objfile_link = nullptr;
    844 
    845   /* Place to stash various statistics about this objfile.  */
    846 
    847   OBJSTATS;
    848 
    849   /* A linked list of symbols created when reading template types or
    850      function templates.  These symbols are not stored in any symbol
    851      table, so we have to keep them here to relocate them
    852      properly.  */
    853 
    854   struct symbol *template_symbols = nullptr;
    855 
    856   /* Associate a static link (struct dynamic_prop *) to all blocks (struct
    857      block *) that have one.
    858 
    859      In the context of nested functions (available in Pascal, Ada and GNU C,
    860      for instance), a static link (as in DWARF's DW_AT_static_link attribute)
    861      for a function is a way to get the frame corresponding to the enclosing
    862      function.
    863 
    864      Very few blocks have a static link, so it's more memory efficient to
    865      store these here rather than in struct block.  Static links must be
    866      allocated on the objfile's obstack.  */
    867   gdb::unordered_map<const block *, const dynamic_prop *>
    868     static_links;
    869 
    870   /* JIT-related data for this objfile, if the objfile is a JITer;
    871      that is, it produces JITed objfiles.  */
    872   std::unique_ptr<jiter_objfile_data> jiter_data = nullptr;
    873 
    874   /* JIT-related data for this objfile, if the objfile is JITed;
    875      that is, it was produced by a JITer.  */
    876   std::unique_ptr<jited_objfile_data> jited_data = nullptr;
    877 
    878   /* A flag that is set to true if the JIT interface symbols are not
    879      found in this objfile, so that we can skip the symbol lookup the
    880      next time.  If an objfile does not have the symbols, it will
    881      never have them.  */
    882   bool skip_jit_symbol_lookup = false;
    883 
    884   /* Flag which indicates, when true, that the object format
    885      potentially supports copy relocations.  ABIs for some
    886      architectures that use ELF have a copy relocation in which the
    887      initialization for a global variable defined in a shared object
    888      will be copied to memory allocated to the main program during
    889      dynamic linking.  Therefore this flag will be set for ELF
    890      objfiles.  Other object formats that use the same copy relocation
    891      mechanism as ELF should set this flag too.  This flag is used in
    892      conjunction with the minimal_symbol::maybe_copied method.  */
    893   bool object_format_has_copy_relocs = false;
    894 };
    895 
    896 /* A deleter for objfile.  */
    897 
    898 struct objfile_unlinker
    899 {
    900   void operator() (objfile *ptr) const
    901   {
    902     ptr->unlink ();
    903   }
    904 };
    905 
    906 /* A unique pointer that holds an objfile.  */
    907 
    908 typedef std::unique_ptr<objfile, objfile_unlinker> scoped_objfile_unlinker;
    909 
    910 /* Relocation offset applied to the section.  */
    911 inline CORE_ADDR
    912 obj_section::offset () const
    913 {
    914   return this->objfile->section_offset (this->the_bfd_section);
    915 }
    916 
    917 /* Set the relocation offset applied to the section.  */
    918 inline void
    919 obj_section::set_offset (CORE_ADDR offset)
    920 {
    921   this->objfile->set_section_offset (this->the_bfd_section, offset);
    922 }
    923 
    924 /* Declarations for functions defined in objfiles.c */
    925 
    926 /* If there is a valid and known entry point in PSPACE, fill *ENTRY_P with it
    927    and return non-zero.  */
    928 
    929 extern int entry_point_address_query (program_space *pspace,
    930 				      CORE_ADDR *entry_p);
    931 
    932 /* Get the entry point address in PSPACE.  Call error if it is not known.  */
    933 
    934 extern CORE_ADDR entry_point_address (program_space *pspace);
    935 
    936 extern void build_objfile_section_table (struct objfile *);
    937 
    938 extern void free_objfile_separate_debug (struct objfile *);
    939 
    940 extern void objfile_relocate (struct objfile *, const section_offsets &);
    941 extern void objfile_rebase (struct objfile *, CORE_ADDR);
    942 
    943 /* Return true if OBJFILE has full symbols.  */
    944 
    945 extern bool objfile_has_full_symbols (objfile *objfile);
    946 
    947 /* Return true if OBJFILE has full or partial symbols, either directly
    948    or through a separate debug file.  */
    949 
    950 extern bool objfile_has_symbols (objfile *objfile);
    951 
    952 /* Return true if any objfile of PSPACE has partial symbols.  */
    953 
    954 extern bool have_partial_symbols (program_space *pspace);
    955 
    956 /* Return true if any objfile of PSPACE has full symbols.  */
    957 
    958 extern bool have_full_symbols (program_space *pspace);
    959 
    960 extern void objfile_set_sym_fns (struct objfile *objfile,
    961 				 const struct sym_fns *sf);
    962 
    963 /* Set section_map_dirty for PSPACE so the section map will be rebuilt next time
    964    it is used.  */
    965 
    966 extern void objfiles_changed (program_space *pspace);
    967 
    968 /* Return true if ADDR maps into one of the sections of OBJFILE and false
    969    otherwise.  */
    970 
    971 extern bool is_addr_in_objfile (CORE_ADDR addr, const struct objfile *objfile);
    972 
    973 /* Return true if ADDRESS maps into one of the sections of a
    974    OBJF_SHARED objfile of PSPACE and false otherwise.  */
    975 
    976 extern bool shared_objfile_contains_address_p (struct program_space *pspace,
    977 					       CORE_ADDR address);
    978 
    979 /* This operation deletes all objfile entries in PSPACE that represent solibs
    980    that weren't explicitly loaded by the user, via e.g., the add-symbol-file
    981    command.  */
    982 
    983 extern void objfile_purge_solibs (program_space *pspace);
    984 
    985 /* Functions for dealing with the minimal symbol table, really a misc
    986    address<->symbol mapping for things we don't have debug symbols for.  */
    987 
    988 /* Return true if any objfile of PSPACE has minimal symbols.  */
    989 
    990 extern bool have_minimal_symbols (program_space *pspace);
    991 
    992 extern struct obj_section *find_pc_section (CORE_ADDR pc);
    993 
    994 /* Return true if PC is in a section called NAME.  */
    995 extern bool pc_in_section (CORE_ADDR, const char *);
    996 
    997 /* Return non-zero if PC is in a SVR4-style procedure linkage table
    998    section.  */
    999 
   1000 static inline int
   1001 in_plt_section (CORE_ADDR pc)
   1002 {
   1003   return (pc_in_section (pc, ".plt")
   1004 	  || pc_in_section (pc, ".plt.sec"));
   1005 }
   1006 
   1007 /* In normal use, the section map will be rebuilt by find_pc_section
   1008    if objfiles have been added, removed or relocated since it was last
   1009    called.  Calling inhibit_section_map_updates will inhibit this
   1010    behavior until the returned scoped_restore object is destroyed.  If
   1011    you call inhibit_section_map_updates you must ensure that every
   1012    call to find_pc_section in the inhibited region relates to a
   1013    section that is already in the section map and has not since been
   1014    removed or relocated.  */
   1015 extern scoped_restore_tmpl<int> inhibit_section_map_updates
   1016     (struct program_space *pspace);
   1017 
   1018 extern void default_iterate_over_objfiles_in_search_order
   1019   (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
   1020    objfile *current_objfile);
   1021 
   1022 /* Reset the per-BFD storage area on OBJ.  */
   1023 
   1024 void set_objfile_per_bfd (struct objfile *obj);
   1025 
   1026 /* Return canonical name for OBJFILE.
   1027    This is the real file name if the file has been opened.
   1028    Otherwise it is the original name supplied by the user.  */
   1029 
   1030 const char *objfile_name (const struct objfile *objfile);
   1031 
   1032 /* Return the (real) file name of OBJFILE if the file has been opened,
   1033    otherwise return NULL.  */
   1034 
   1035 const char *objfile_filename (const struct objfile *objfile);
   1036 
   1037 /* Return the name to print for OBJFILE in debugging messages.  */
   1038 
   1039 extern const char *objfile_debug_name (const struct objfile *objfile);
   1040 
   1041 /* Return the name of the file format of OBJFILE if the file has been opened,
   1042    otherwise return NULL.  */
   1043 
   1044 const char *objfile_flavour_name (struct objfile *objfile);
   1045 
   1046 /* Set the objfile's notion of the "main" name and language.  */
   1047 
   1048 extern void set_objfile_main_name (struct objfile *objfile,
   1049 				   const char *name, enum language lang);
   1050 
   1051 /* Find an integer type SIZE_IN_BYTES bytes in size from OF and return it.
   1052    UNSIGNED_P controls if the integer is unsigned or not.  */
   1053 extern struct type *objfile_int_type (struct objfile *of, int size_in_bytes,
   1054 				      bool unsigned_p);
   1055 
   1056 extern void objfile_register_static_link
   1057   (struct objfile *objfile,
   1058    const struct block *block,
   1059    const struct dynamic_prop *static_link);
   1060 
   1061 extern const struct dynamic_prop *objfile_lookup_static_link
   1062   (struct objfile *objfile, const struct block *block);
   1063 
   1064 #endif /* GDB_OBJFILES_H */
   1065