Automatic date update in version.in
[binutils-gdb.git] / gdb / psymtab.h
blob557a7cf4c087d9745c5636afdf0eb0f09d2c61e1
1 /* Public partial symbol table definitions.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef PSYMTAB_H
21 #define PSYMTAB_H
23 #include "objfiles.h"
24 #include <string_view>
25 #include "gdbsupport/gdb_obstack.h"
26 #include "symfile.h"
27 #include "gdbsupport/next-iterator.h"
28 #include "bcache.h"
30 /* Specialization of bcache to store partial symbols. */
32 struct psymbol_bcache : public gdb::bcache
34 /* Calculate a hash code for the given partial symbol. The hash is
35 calculated using the symbol's value, language, domain, class
36 and name. These are the values which are set by
37 add_psymbol_to_bcache. */
38 unsigned long hash (const void *addr, int length) override;
40 /* Returns true if the symbol LEFT equals the symbol RIGHT.
41 For the comparison this function uses a symbols value,
42 language, domain, class and name. */
43 int compare (const void *left, const void *right, int length) override;
46 /* An instance of this class manages the partial symbol tables and
47 partial symbols for a given objfile.
49 The core psymtab functions -- those in psymtab.c -- arrange for
50 nearly all psymtab- and psymbol-related allocations to happen
51 either in the psymtab_storage object (either on its obstack or in
52 other memory managed by this class), or on the per-BFD object. The
53 only link from the psymtab storage object back to the objfile (or
54 objfile_obstack) that is made by the core psymtab code is the
55 compunit_symtab member in the standard_psymtab -- and a given
56 symbol reader can avoid this by implementing its own subclasses of
57 partial_symtab.
59 However, it is up to each symbol reader to maintain this invariant
60 in other ways, if it wants to reuse psymtabs across multiple
61 objfiles. The main issue here is ensuring that read_symtab_private
62 does not point into objfile_obstack. */
64 class psymtab_storage
66 public:
67 psymtab_storage () = default;
68 ~psymtab_storage ();
70 DISABLE_COPY_AND_ASSIGN (psymtab_storage);
72 /* Discard all partial symbol tables starting with "psymtabs" and
73 proceeding until "to" has been discarded. */
75 void discard_psymtabs_to (struct partial_symtab *to)
77 while (psymtabs != to)
78 discard_psymtab (psymtabs);
81 /* Discard the partial symbol table. */
83 void discard_psymtab (struct partial_symtab *pst);
85 /* Return the obstack that is used for storage by this object. */
87 struct obstack *obstack ()
89 if (!m_obstack.has_value ())
90 m_obstack.emplace ();
91 return &*m_obstack;
94 /* Allocate storage for the "dependencies" field of a psymtab.
95 NUMBER says how many dependencies there are. */
97 struct partial_symtab **allocate_dependencies (int number)
99 return OBSTACK_CALLOC (obstack (), number, struct partial_symtab *);
102 /* Install a psymtab on the psymtab list. This transfers ownership
103 of PST to this object. */
105 void install_psymtab (partial_symtab *pst);
107 using partial_symtab_range = next_range<partial_symtab>;
109 /* A range adapter that makes it possible to iterate over all
110 psymtabs in one objfile. */
112 partial_symtab_range range ()
114 return partial_symtab_range (psymtabs);
118 /* Each objfile points to a linked list of partial symtabs derived from
119 this file, one partial symtab structure for each compilation unit
120 (source file). */
122 struct partial_symtab *psymtabs = nullptr;
124 /* A byte cache where we can stash arbitrary "chunks" of bytes that
125 will not change. */
127 psymbol_bcache psymbol_cache;
129 private:
131 /* The obstack where allocations are made. This is lazily allocated
132 so that we don't waste memory when there are no psymtabs. */
134 std::optional<auto_obstack> m_obstack;
137 /* A partial_symbol records the name, domain, and address class of
138 symbols whose types we have not parsed yet. For functions, it also
139 contains their memory address, so we can find them from a PC value.
140 Each partial_symbol sits in a partial_symtab, all of which are chained
141 on a partial symtab list and which points to the corresponding
142 normal symtab once the partial_symtab has been referenced. */
144 /* This structure is space critical. See space comments at the top of
145 symtab.h. */
147 struct partial_symbol
149 /* Return the section for this partial symbol, or nullptr if no
150 section has been set. */
151 struct obj_section *obj_section (struct objfile *objfile) const
153 return ginfo.obj_section (objfile);
156 /* Return the unrelocated address of this partial symbol. */
157 unrelocated_addr unrelocated_address () const
159 return ginfo.unrelocated_address ();
162 /* Return the address of this partial symbol, relocated according to
163 the offsets provided in OBJFILE. */
164 CORE_ADDR address (const struct objfile *objfile) const
166 return (CORE_ADDR (ginfo.unrelocated_address ())
167 + objfile->section_offsets[ginfo.section_index ()]);
170 /* Set the address of this partial symbol. The address must be
171 unrelocated. */
172 void set_unrelocated_address (unrelocated_addr addr)
174 ginfo.set_unrelocated_address (addr);
177 /* Note that partial_symbol does not derive from general_symbol_info
178 due to the bcache. See add_psymbol_to_bcache. */
180 struct general_symbol_info ginfo;
182 /* Name space code. */
184 ENUM_BITFIELD(domain_enum) domain : SYMBOL_DOMAIN_BITS;
186 /* Address class (for info_symbols). Note that we don't allow
187 synthetic "aclass" values here at present, simply because there's
188 no need. */
190 ENUM_BITFIELD(address_class) aclass : SYMBOL_ACLASS_BITS;
193 /* A convenience enum to give names to some constants used when
194 searching psymtabs. This is internal to psymtab and should not be
195 used elsewhere. */
197 enum psymtab_search_status
199 PST_NOT_SEARCHED,
200 PST_SEARCHED_AND_FOUND,
201 PST_SEARCHED_AND_NOT_FOUND
204 /* Specify whether a partial psymbol should be allocated on the global
205 list or the static list. */
207 enum class psymbol_placement
209 STATIC,
210 GLOBAL
213 /* Each source file that has not been fully read in is represented by
214 a partial_symtab. This contains the information on where in the
215 executable the debugging symbols for a specific file are, and a
216 list of names of global symbols which are located in this file.
217 They are all chained on partial symtab lists.
219 Even after the source file has been read into a symtab, the
220 partial_symtab remains around. */
222 struct partial_symtab
224 /* Allocate a new partial symbol table.
226 FILENAME (which must be non-NULL) is the filename of this partial
227 symbol table; it is copied into the appropriate storage. The
228 partial symtab will also be installed using
229 psymtab_storage::install. */
231 partial_symtab (const char *filename,
232 psymtab_storage *partial_symtabs,
233 objfile_per_bfd_storage *objfile_per_bfd)
234 ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
236 /* Like the above, but also sets the initial text low and text high
237 from the ADDR argument, and sets the global- and
238 static-offsets. */
240 partial_symtab (const char *filename,
241 psymtab_storage *partial_symtabs,
242 objfile_per_bfd_storage *objfile_per_bfd,
243 unrelocated_addr addr)
244 ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
246 virtual ~partial_symtab ()
250 /* Psymtab expansion is done in two steps:
251 - a call to read_symtab
252 - while that call is in progress, calls to expand_psymtab can be made,
253 both for this psymtab, and its dependencies.
254 This makes a distinction between a toplevel psymtab (for which both
255 read_symtab and expand_psymtab will be called) and a non-toplevel
256 psymtab (for which only expand_psymtab will be called). The
257 distinction can be used f.i. to do things before and after all
258 dependencies of a top-level psymtab have been expanded.
260 Read the full symbol table corresponding to this partial symbol
261 table. Typically calls expand_psymtab. */
262 virtual void read_symtab (struct objfile *) = 0;
264 /* Expand the full symbol table for this partial symbol table. Typically
265 calls expand_dependencies. */
266 virtual void expand_psymtab (struct objfile *) = 0;
268 /* Ensure that all the dependencies are read in. Calls
269 expand_psymtab for each non-shared dependency. */
270 void expand_dependencies (struct objfile *);
272 /* Return true if the symtab corresponding to this psymtab has been
273 read in in the context of this objfile. */
274 virtual bool readin_p (struct objfile *) const = 0;
276 /* Return a pointer to the compunit allocated for this source file
277 in the context of this objfile.
279 Return nullptr if the compunit was not read in or if there was no
280 symtab. */
281 virtual struct compunit_symtab *get_compunit_symtab
282 (struct objfile *) const = 0;
284 /* Return the unrelocated low text address of this
285 partial_symtab. */
286 unrelocated_addr unrelocated_text_low () const
288 return m_text_low;
291 /* Return the unrelocated_addr high text address of this
292 partial_symtab. */
293 unrelocated_addr unrelocated_text_high () const
295 return m_text_high;
298 /* Return the relocated low text address of this partial_symtab. */
299 CORE_ADDR text_low (struct objfile *objfile) const
301 return CORE_ADDR (m_text_low) + objfile->text_section_offset ();
304 /* Return the relocated high text address of this partial_symtab. */
305 CORE_ADDR text_high (struct objfile *objfile) const
307 return CORE_ADDR (m_text_high) + objfile->text_section_offset ();
310 /* Set the low text address of this partial_symtab. */
311 void set_text_low (unrelocated_addr addr)
313 m_text_low = addr;
314 text_low_valid = 1;
317 /* Set the high text address of this partial_symtab. */
318 void set_text_high (unrelocated_addr addr)
320 m_text_high = addr;
321 text_high_valid = 1;
324 /* Return true if this symtab is empty -- meaning that it contains
325 no symbols. It may still have dependencies. */
326 bool empty () const
328 return global_psymbols.empty () && static_psymbols.empty ();
331 /* Add a symbol to this partial symbol table of OBJFILE.
333 If COPY_NAME is true, make a copy of NAME, otherwise use the passed
334 reference.
336 THECLASS is the type of symbol.
338 SECTION is the index of the section of OBJFILE in which the symbol is found.
340 WHERE determines whether the symbol goes in the list of static or global
341 partial symbols.
343 COREADDR is the address of the symbol. For partial symbols that don't have
344 an address, zero is passed.
346 LANGUAGE is the language from which the symbol originates. This will
347 influence, amongst other things, how the symbol name is demangled. */
349 void add_psymbol (std::string_view name,
350 bool copy_name, domain_enum domain,
351 enum address_class theclass,
352 short section,
353 psymbol_placement where,
354 unrelocated_addr coreaddr,
355 enum language language,
356 psymtab_storage *partial_symtabs,
357 struct objfile *objfile);
359 /* Add a symbol to this partial symbol table of OBJFILE. The psymbol
360 must be fully constructed, and the names must be set and intern'd
361 as appropriate. */
363 void add_psymbol (const partial_symbol &psym,
364 psymbol_placement where,
365 psymtab_storage *partial_symtabs,
366 struct objfile *objfile);
369 /* Indicate that this partial symtab is complete. */
371 void end ();
373 /* Chain of all existing partial symtabs. */
375 struct partial_symtab *next = nullptr;
377 /* Name of the source file which this partial_symtab defines,
378 or if the psymtab is anonymous then a descriptive name for
379 debugging purposes, or "". It must not be NULL. */
381 const char *filename = nullptr;
383 /* Full path of the source file. NULL if not known. */
385 char *fullname = nullptr;
387 /* Directory in which it was compiled, or NULL if we don't know. */
389 const char *dirname = nullptr;
391 /* Range of text addresses covered by this file; texthigh is the
392 beginning of the next section. Do not refer directly to these
393 fields. Instead, use the accessors. The validity of these
394 fields is determined by the text_low_valid and text_high_valid
395 fields; these are located later in this structure for better
396 packing. */
398 unrelocated_addr m_text_low {};
399 unrelocated_addr m_text_high {};
401 /* If NULL, this is an ordinary partial symbol table.
403 If non-NULL, this holds a single includer of this partial symbol
404 table, and this partial symbol table is a shared one.
406 A shared psymtab is one that is referenced by multiple other
407 psymtabs, and which conceptually has its contents directly
408 included in those.
410 Shared psymtabs have special semantics. When a search finds a
411 symbol in a shared table, we instead return one of the non-shared
412 tables that include this one.
414 A shared psymtabs can be referred to by other shared ones.
416 The psymtabs that refer to a shared psymtab will list the shared
417 psymtab in their 'dependencies' array.
419 In DWARF terms, a shared psymtab is a DW_TAG_partial_unit; but
420 of course using a name based on that would be too confusing, so
421 "shared" was chosen instead.
423 Only a single user is needed because, when expanding a shared
424 psymtab, we only need to expand its "canonical" non-shared user.
425 The choice of which one should be canonical is left to the
426 debuginfo reader; it can be arbitrary. */
428 struct partial_symtab *user = nullptr;
430 /* Array of pointers to all of the partial_symtab's which this one
431 depends on. Since this array can only be set to previous or
432 the current (?) psymtab, this dependency tree is guaranteed not
433 to have any loops. "depends on" means that symbols must be read
434 for the dependencies before being read for this psymtab; this is
435 for type references in stabs, where if foo.c includes foo.h, declarations
436 in foo.h may use type numbers defined in foo.c. For other debugging
437 formats there may be no need to use dependencies. */
439 struct partial_symtab **dependencies = nullptr;
441 int number_of_dependencies = 0;
443 /* Global symbol list. This list will be sorted after readin to
444 improve access. Binary search will be the usual method of
445 finding a symbol within it. */
447 std::vector<partial_symbol *> global_psymbols;
449 /* Static symbol list. This list will *not* be sorted after readin;
450 to find a symbol in it, exhaustive search must be used. This is
451 reasonable because searches through this list will eventually
452 lead to either the read in of a files symbols for real (assumed
453 to take a *lot* of time; check) or an error (and we don't care
454 how long errors take). */
456 std::vector<partial_symbol *> static_psymbols;
458 /* True if the name of this partial symtab is not a source file name. */
460 bool anonymous = false;
462 /* A flag that is temporarily used when searching psymtabs. */
464 ENUM_BITFIELD (psymtab_search_status) searched_flag : 2;
466 /* Validity of the m_text_low and m_text_high fields. */
468 unsigned int text_low_valid : 1;
469 unsigned int text_high_valid : 1;
472 /* A partial symtab that tracks the "readin" and "compunit_symtab"
473 information in the ordinary way -- by storing it directly in this
474 object. */
475 struct standard_psymtab : public partial_symtab
477 standard_psymtab (const char *filename,
478 psymtab_storage *partial_symtabs,
479 objfile_per_bfd_storage *objfile_per_bfd)
480 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
484 standard_psymtab (const char *filename,
485 psymtab_storage *partial_symtabs,
486 objfile_per_bfd_storage *objfile_per_bfd,
487 unrelocated_addr addr)
488 : partial_symtab (filename, partial_symtabs, objfile_per_bfd, addr)
492 bool readin_p (struct objfile *) const override
494 return readin;
497 struct compunit_symtab *get_compunit_symtab (struct objfile *) const override
499 return compunit_symtab;
502 /* True if the symtab corresponding to this psymtab has been
503 readin. */
505 bool readin = false;
507 /* Pointer to compunit eventually allocated for this source file, 0 if
508 !readin or if we haven't looked for the symtab after it was readin. */
510 struct compunit_symtab *compunit_symtab = nullptr;
513 /* A partial_symtab that works in the historical db way. This should
514 not be used in new code, but exists to transition the somewhat
515 unmaintained "legacy" debug formats. */
517 struct legacy_psymtab : public standard_psymtab
519 legacy_psymtab (const char *filename,
520 psymtab_storage *partial_symtabs,
521 objfile_per_bfd_storage *objfile_per_bfd)
522 : standard_psymtab (filename, partial_symtabs, objfile_per_bfd)
526 legacy_psymtab (const char *filename,
527 psymtab_storage *partial_symtabs,
528 objfile_per_bfd_storage *objfile_per_bfd,
529 unrelocated_addr addr)
530 : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
534 void read_symtab (struct objfile *objf) override
536 if (legacy_read_symtab)
537 (*legacy_read_symtab) (this, objf);
540 void expand_psymtab (struct objfile *objf) override
542 (*legacy_expand_psymtab) (this, objf);
545 /* Pointer to function which will read in the symtab corresponding to
546 this psymtab. */
548 void (*legacy_read_symtab) (legacy_psymtab *, struct objfile *) = nullptr;
550 /* Pointer to function which will actually expand this psymtab into
551 a full symtab. */
553 void (*legacy_expand_psymtab) (legacy_psymtab *, struct objfile *) = nullptr;
555 /* Information that lets read_symtab() locate the part of the symbol table
556 that this psymtab corresponds to. This information is private to the
557 format-dependent symbol reading routines. For further detail examine
558 the various symbol reading modules. */
560 void *read_symtab_private = nullptr;
563 /* Used when recording partial symbol tables. On destruction,
564 discards any partial symbol tables that have been built. However,
565 the tables can be kept by calling the "keep" method. */
566 class psymtab_discarder
568 public:
570 psymtab_discarder (psymtab_storage *partial_symtabs)
571 : m_partial_symtabs (partial_symtabs),
572 m_psymtab (partial_symtabs->psymtabs)
576 ~psymtab_discarder ()
578 if (m_partial_symtabs != nullptr)
579 m_partial_symtabs->discard_psymtabs_to (m_psymtab);
582 /* Keep any partial symbol tables that were built. */
583 void keep ()
585 m_partial_symtabs = nullptr;
588 private:
590 /* The partial symbol storage object. */
591 psymtab_storage *m_partial_symtabs;
592 /* How far back to free. */
593 struct partial_symtab *m_psymtab;
596 /* An implementation of quick_symbol_functions, specialized for
597 partial symbols. */
598 struct psymbol_functions : public quick_symbol_functions
600 explicit psymbol_functions (const std::shared_ptr<psymtab_storage> &storage)
601 : m_partial_symtabs (storage)
605 psymbol_functions ()
606 : m_partial_symtabs (new psymtab_storage)
610 bool has_symbols (struct objfile *objfile) override;
612 bool has_unexpanded_symtabs (struct objfile *objfile) override;
614 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
616 void forget_cached_source_info (struct objfile *objfile) override;
618 enum language lookup_global_symbol_language (struct objfile *objfile,
619 const char *name,
620 domain_search_flags domain,
621 bool *symbol_found_p) override;
623 void print_stats (struct objfile *objfile, bool print_bcache) override;
625 void dump (struct objfile *objfile) override;
627 void expand_all_symtabs (struct objfile *objfile) override;
629 bool expand_symtabs_matching
630 (struct objfile *objfile,
631 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
632 const lookup_name_info *lookup_name,
633 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
634 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
635 block_search_flags search_flags,
636 domain_search_flags kind) override;
638 struct compunit_symtab *find_pc_sect_compunit_symtab
639 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
640 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
642 struct compunit_symtab *find_compunit_symtab_by_address
643 (struct objfile *objfile, CORE_ADDR address) override
645 return nullptr;
648 void map_symbol_filenames (struct objfile *objfile,
649 gdb::function_view<symbol_filename_ftype> fun,
650 bool need_fullname) override;
652 /* Return a range adapter for the psymtabs. */
653 psymtab_storage::partial_symtab_range partial_symbols
654 (struct objfile *objfile);
656 /* Return the partial symbol storage associated with this
657 object. */
658 const std::shared_ptr<psymtab_storage> &get_partial_symtabs () const
660 return m_partial_symtabs;
663 /* Replace the partial symbol table storage in this object with
664 SYMS. */
665 void set_partial_symtabs (const std::shared_ptr<psymtab_storage> &syms)
667 m_partial_symtabs = syms;
670 /* Find which partial symtab contains PC and SECTION. Return NULL if
671 none. We return the psymtab that contains a symbol whose address
672 exactly matches PC, or, if we cannot find an exact match, the
673 psymtab that contains a symbol whose address is closest to PC. */
675 struct partial_symtab *find_pc_sect_psymtab
676 (struct objfile *objfile,
677 CORE_ADDR pc,
678 struct obj_section *section,
679 struct bound_minimal_symbol msymbol);
681 private:
683 /* Count the number of partial symbols in *THIS. */
684 int count_psyms ();
686 /* Storage for the partial symbols. */
687 std::shared_ptr<psymtab_storage> m_partial_symtabs;
690 #endif /* PSYMTAB_H */