xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/psympriv.h (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Private partial symbol table definitions.
2 
3    Copyright (C) 2009-2023 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 PSYMPRIV_H
21 #define PSYMPRIV_H
22 
23 #include "psymtab.h"
24 #include "objfiles.h"
25 #include "gdbsupport/gdb_string_view.h"
26 
27 /* A partial_symbol records the name, domain, and address class of
28    symbols whose types we have not parsed yet.  For functions, it also
29    contains their memory address, so we can find them from a PC value.
30    Each partial_symbol sits in a partial_symtab, all of which are chained
31    on a  partial symtab list and which points to the corresponding
32    normal symtab once the partial_symtab has been referenced.  */
33 
34 /* This structure is space critical.  See space comments at the top of
35    symtab.h.  */
36 
37 struct partial_symbol
38 {
39   /* Return the section for this partial symbol, or nullptr if no
40      section has been set.  */
41   struct obj_section *obj_section (struct objfile *objfile) const
42   {
43     return ginfo.obj_section (objfile);
44   }
45 
46   /* Return the unrelocated address of this partial symbol.  */
47   CORE_ADDR unrelocated_address () const
48   {
49     return ginfo.value_address ();
50   }
51 
52   /* Return the address of this partial symbol, relocated according to
53      the offsets provided in OBJFILE.  */
54   CORE_ADDR address (const struct objfile *objfile) const
55   {
56     return (ginfo.value_address ()
57 	    + objfile->section_offsets[ginfo.section_index ()]);
58   }
59 
60   /* Set the address of this partial symbol.  The address must be
61      unrelocated.  */
62   void set_unrelocated_address (CORE_ADDR addr)
63   {
64     ginfo.set_value_address (addr);
65   }
66 
67   /* Note that partial_symbol does not derive from general_symbol_info
68      due to the bcache.  See add_psymbol_to_bcache.  */
69 
70   struct general_symbol_info ginfo;
71 
72   /* Name space code.  */
73 
74   ENUM_BITFIELD(domain_enum) domain : SYMBOL_DOMAIN_BITS;
75 
76   /* Address class (for info_symbols).  Note that we don't allow
77      synthetic "aclass" values here at present, simply because there's
78      no need.  */
79 
80   ENUM_BITFIELD(address_class) aclass : SYMBOL_ACLASS_BITS;
81 };
82 
83 /* A convenience enum to give names to some constants used when
84    searching psymtabs.  This is internal to psymtab and should not be
85    used elsewhere.  */
86 
87 enum psymtab_search_status
88   {
89     PST_NOT_SEARCHED,
90     PST_SEARCHED_AND_FOUND,
91     PST_SEARCHED_AND_NOT_FOUND
92   };
93 
94 /* Specify whether a partial psymbol should be allocated on the global
95    list or the static list.  */
96 
97 enum class psymbol_placement
98 {
99   STATIC,
100   GLOBAL
101 };
102 
103 /* Each source file that has not been fully read in is represented by
104    a partial_symtab.  This contains the information on where in the
105    executable the debugging symbols for a specific file are, and a
106    list of names of global symbols which are located in this file.
107    They are all chained on partial symtab lists.
108 
109    Even after the source file has been read into a symtab, the
110    partial_symtab remains around.  */
111 
112 struct partial_symtab
113 {
114   /* Allocate a new partial symbol table.
115 
116      FILENAME (which must be non-NULL) is the filename of this partial
117      symbol table; it is copied into the appropriate storage.  The
118      partial symtab will also be installed using
119      psymtab_storage::install.  */
120 
121   partial_symtab (const char *filename,
122 		  psymtab_storage *partial_symtabs,
123 		  objfile_per_bfd_storage *objfile_per_bfd)
124     ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
125 
126   /* Like the above, but also sets the initial text low and text high
127      from the ADDR argument, and sets the global- and
128      static-offsets.  */
129 
130   partial_symtab (const char *filename,
131 		  psymtab_storage *partial_symtabs,
132 		  objfile_per_bfd_storage *objfile_per_bfd,
133 		  CORE_ADDR addr)
134     ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (3);
135 
136   virtual ~partial_symtab ()
137   {
138   }
139 
140   /* Psymtab expansion is done in two steps:
141      - a call to read_symtab
142      - while that call is in progress, calls to expand_psymtab can be made,
143        both for this psymtab, and its dependencies.
144      This makes a distinction between a toplevel psymtab (for which both
145      read_symtab and expand_psymtab will be called) and a non-toplevel
146      psymtab (for which only expand_psymtab will be called). The
147      distinction can be used f.i. to do things before and after all
148      dependencies of a top-level psymtab have been expanded.
149 
150      Read the full symbol table corresponding to this partial symbol
151      table.  Typically calls expand_psymtab.  */
152   virtual void read_symtab (struct objfile *) = 0;
153 
154   /* Expand the full symbol table for this partial symbol table.  Typically
155      calls expand_dependencies.  */
156   virtual void expand_psymtab (struct objfile *) = 0;
157 
158   /* Ensure that all the dependencies are read in.  Calls
159      expand_psymtab for each non-shared dependency.  */
160   void expand_dependencies (struct objfile *);
161 
162   /* Return true if the symtab corresponding to this psymtab has been
163      read in in the context of this objfile.  */
164   virtual bool readin_p (struct objfile *) const = 0;
165 
166   /* Return a pointer to the compunit allocated for this source file
167      in the context of this objfile.
168 
169      Return nullptr if the compunit was not read in or if there was no
170      symtab.  */
171   virtual struct compunit_symtab *get_compunit_symtab
172     (struct objfile *) const = 0;
173 
174   /* Return the raw low text address of this partial_symtab.  */
175   CORE_ADDR raw_text_low () const
176   {
177     return m_text_low;
178   }
179 
180   /* Return the raw high text address of this partial_symtab.  */
181   CORE_ADDR raw_text_high () const
182   {
183     return m_text_high;
184   }
185 
186   /* Return the relocated low text address of this partial_symtab.  */
187   CORE_ADDR text_low (struct objfile *objfile) const
188   {
189     return m_text_low + objfile->text_section_offset ();
190   }
191 
192   /* Return the relocated high text address of this partial_symtab.  */
193   CORE_ADDR text_high (struct objfile *objfile) const
194   {
195     return m_text_high + objfile->text_section_offset ();
196   }
197 
198   /* Set the low text address of this partial_symtab.  */
199   void set_text_low (CORE_ADDR addr)
200   {
201     m_text_low = addr;
202     text_low_valid = 1;
203   }
204 
205   /* Set the hight text address of this partial_symtab.  */
206   void set_text_high (CORE_ADDR addr)
207   {
208     m_text_high = addr;
209     text_high_valid = 1;
210   }
211 
212   /* Return true if this symtab is empty -- meaning that it contains
213      no symbols.  It may still have dependencies.  */
214   bool empty () const
215   {
216     return global_psymbols.empty () && static_psymbols.empty ();
217   }
218 
219   /* Add a symbol to this partial symbol table of OBJFILE.
220 
221      If COPY_NAME is true, make a copy of NAME, otherwise use the passed
222      reference.
223 
224      THECLASS is the type of symbol.
225 
226      SECTION is the index of the section of OBJFILE in which the symbol is found.
227 
228      WHERE determines whether the symbol goes in the list of static or global
229      partial symbols.
230 
231      COREADDR is the address of the symbol.  For partial symbols that don't have
232      an address, zero is passed.
233 
234      LANGUAGE is the language from which the symbol originates.  This will
235      influence, amongst other things, how the symbol name is demangled. */
236 
237   void add_psymbol (gdb::string_view name,
238 		    bool copy_name, domain_enum domain,
239 		    enum address_class theclass,
240 		    short section,
241 		    psymbol_placement where,
242 		    CORE_ADDR coreaddr,
243 		    enum language language,
244 		    psymtab_storage *partial_symtabs,
245 		    struct objfile *objfile);
246 
247   /* Add a symbol to this partial symbol table of OBJFILE.  The psymbol
248      must be fully constructed, and the names must be set and intern'd
249      as appropriate.  */
250 
251   void add_psymbol (const partial_symbol &psym,
252 		    psymbol_placement where,
253 		    psymtab_storage *partial_symtabs,
254 		    struct objfile *objfile);
255 
256 
257   /* Indicate that this partial symtab is complete.  */
258 
259   void end ();
260 
261   /* Chain of all existing partial symtabs.  */
262 
263   struct partial_symtab *next = nullptr;
264 
265   /* Name of the source file which this partial_symtab defines,
266      or if the psymtab is anonymous then a descriptive name for
267      debugging purposes, or "".  It must not be NULL.  */
268 
269   const char *filename = nullptr;
270 
271   /* Full path of the source file.  NULL if not known.  */
272 
273   char *fullname = nullptr;
274 
275   /* Directory in which it was compiled, or NULL if we don't know.  */
276 
277   const char *dirname = nullptr;
278 
279   /* Range of text addresses covered by this file; texthigh is the
280      beginning of the next section.  Do not refer directly to these
281      fields.  Instead, use the accessors.  The validity of these
282      fields is determined by the text_low_valid and text_high_valid
283      fields; these are located later in this structure for better
284      packing.  */
285 
286   CORE_ADDR m_text_low = 0;
287   CORE_ADDR m_text_high = 0;
288 
289   /* If NULL, this is an ordinary partial symbol table.
290 
291      If non-NULL, this holds a single includer of this partial symbol
292      table, and this partial symbol table is a shared one.
293 
294      A shared psymtab is one that is referenced by multiple other
295      psymtabs, and which conceptually has its contents directly
296      included in those.
297 
298      Shared psymtabs have special semantics.  When a search finds a
299      symbol in a shared table, we instead return one of the non-shared
300      tables that include this one.
301 
302      A shared psymtabs can be referred to by other shared ones.
303 
304      The psymtabs that refer to a shared psymtab will list the shared
305      psymtab in their 'dependencies' array.
306 
307      In DWARF terms, a shared psymtab is a DW_TAG_partial_unit; but
308      of course using a name based on that would be too confusing, so
309      "shared" was chosen instead.
310 
311      Only a single user is needed because, when expanding a shared
312      psymtab, we only need to expand its "canonical" non-shared user.
313      The choice of which one should be canonical is left to the
314      debuginfo reader; it can be arbitrary.  */
315 
316   struct partial_symtab *user = nullptr;
317 
318   /* Array of pointers to all of the partial_symtab's which this one
319      depends on.  Since this array can only be set to previous or
320      the current (?) psymtab, this dependency tree is guaranteed not
321      to have any loops.  "depends on" means that symbols must be read
322      for the dependencies before being read for this psymtab; this is
323      for type references in stabs, where if foo.c includes foo.h, declarations
324      in foo.h may use type numbers defined in foo.c.  For other debugging
325      formats there may be no need to use dependencies.  */
326 
327   struct partial_symtab **dependencies = nullptr;
328 
329   int number_of_dependencies = 0;
330 
331   /* Global symbol list.  This list will be sorted after readin to
332      improve access.  Binary search will be the usual method of
333      finding a symbol within it.  */
334 
335   std::vector<partial_symbol *> global_psymbols;
336 
337   /* Static symbol list.  This list will *not* be sorted after readin;
338      to find a symbol in it, exhaustive search must be used.  This is
339      reasonable because searches through this list will eventually
340      lead to either the read in of a files symbols for real (assumed
341      to take a *lot* of time; check) or an error (and we don't care
342      how long errors take).  */
343 
344   std::vector<partial_symbol *> static_psymbols;
345 
346   /* True if the name of this partial symtab is not a source file name.  */
347 
348   bool anonymous = false;
349 
350   /* A flag that is temporarily used when searching psymtabs.  */
351 
352   ENUM_BITFIELD (psymtab_search_status) searched_flag : 2;
353 
354   /* Validity of the m_text_low and m_text_high fields.  */
355 
356   unsigned int text_low_valid : 1;
357   unsigned int text_high_valid : 1;
358 };
359 
360 /* A partial symtab that tracks the "readin" and "compunit_symtab"
361    information in the ordinary way -- by storing it directly in this
362    object.  */
363 struct standard_psymtab : public partial_symtab
364 {
365   standard_psymtab (const char *filename,
366 		    psymtab_storage *partial_symtabs,
367 		    objfile_per_bfd_storage *objfile_per_bfd)
368     : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
369   {
370   }
371 
372   standard_psymtab (const char *filename,
373 		    psymtab_storage *partial_symtabs,
374 		    objfile_per_bfd_storage *objfile_per_bfd,
375 		    CORE_ADDR addr)
376     : partial_symtab (filename, partial_symtabs, objfile_per_bfd, addr)
377   {
378   }
379 
380   bool readin_p (struct objfile *) const override
381   {
382     return readin;
383   }
384 
385   struct compunit_symtab *get_compunit_symtab (struct objfile *) const override
386   {
387     return compunit_symtab;
388   }
389 
390   /* True if the symtab corresponding to this psymtab has been
391      readin.  */
392 
393   bool readin = false;
394 
395   /* Pointer to compunit eventually allocated for this source file, 0 if
396      !readin or if we haven't looked for the symtab after it was readin.  */
397 
398   struct compunit_symtab *compunit_symtab = nullptr;
399 };
400 
401 /* A partial_symtab that works in the historical db way.  This should
402    not be used in new code, but exists to transition the somewhat
403    unmaintained "legacy" debug formats.  */
404 
405 struct legacy_psymtab : public standard_psymtab
406 {
407   legacy_psymtab (const char *filename,
408 		  psymtab_storage *partial_symtabs,
409 		  objfile_per_bfd_storage *objfile_per_bfd)
410     : standard_psymtab (filename, partial_symtabs, objfile_per_bfd)
411   {
412   }
413 
414   legacy_psymtab (const char *filename,
415 		  psymtab_storage *partial_symtabs,
416 		  objfile_per_bfd_storage *objfile_per_bfd,
417 		  CORE_ADDR addr)
418     : standard_psymtab (filename, partial_symtabs, objfile_per_bfd, addr)
419   {
420   }
421 
422   void read_symtab (struct objfile *objf) override
423   {
424     if (legacy_read_symtab)
425       (*legacy_read_symtab) (this, objf);
426   }
427 
428   void expand_psymtab (struct objfile *objf) override
429   {
430     (*legacy_expand_psymtab) (this, objf);
431   }
432 
433   /* Pointer to function which will read in the symtab corresponding to
434      this psymtab.  */
435 
436   void (*legacy_read_symtab) (legacy_psymtab *, struct objfile *) = nullptr;
437 
438   /* Pointer to function which will actually expand this psymtab into
439      a full symtab.  */
440 
441   void (*legacy_expand_psymtab) (legacy_psymtab *, struct objfile *) = nullptr;
442 
443   /* Information that lets read_symtab() locate the part of the symbol table
444      that this psymtab corresponds to.  This information is private to the
445      format-dependent symbol reading routines.  For further detail examine
446      the various symbol reading modules.  */
447 
448   void *read_symtab_private = nullptr;
449 };
450 
451 /* Used when recording partial symbol tables.  On destruction,
452    discards any partial symbol tables that have been built.  However,
453    the tables can be kept by calling the "keep" method.  */
454 class psymtab_discarder
455 {
456  public:
457 
458   psymtab_discarder (psymtab_storage *partial_symtabs)
459     : m_partial_symtabs (partial_symtabs),
460       m_psymtab (partial_symtabs->psymtabs)
461   {
462   }
463 
464   ~psymtab_discarder ()
465   {
466     if (m_partial_symtabs != nullptr)
467       m_partial_symtabs->discard_psymtabs_to (m_psymtab);
468   }
469 
470   /* Keep any partial symbol tables that were built.  */
471   void keep ()
472   {
473     m_partial_symtabs = nullptr;
474   }
475 
476  private:
477 
478   /* The partial symbol storage object.  */
479   psymtab_storage *m_partial_symtabs;
480   /* How far back to free.  */
481   struct partial_symtab *m_psymtab;
482 };
483 
484 /* An implementation of quick_symbol_functions, specialized for
485    partial symbols.  */
486 struct psymbol_functions : public quick_symbol_functions
487 {
488   explicit psymbol_functions (const std::shared_ptr<psymtab_storage> &storage)
489     : m_partial_symtabs (storage)
490   {
491   }
492 
493   psymbol_functions ()
494     : m_partial_symtabs (new psymtab_storage)
495   {
496   }
497 
498   bool has_symbols (struct objfile *objfile) override;
499 
500   bool has_unexpanded_symtabs (struct objfile *objfile) override;
501 
502   struct symtab *find_last_source_symtab (struct objfile *objfile) override;
503 
504   void forget_cached_source_info (struct objfile *objfile) override;
505 
506   enum language lookup_global_symbol_language (struct objfile *objfile,
507 					       const char *name,
508 					       domain_enum domain,
509 					       bool *symbol_found_p) override;
510 
511   void print_stats (struct objfile *objfile, bool print_bcache) override;
512 
513   void dump (struct objfile *objfile) override;
514 
515   void expand_all_symtabs (struct objfile *objfile) override;
516 
517   void expand_matching_symbols
518     (struct objfile *,
519      const lookup_name_info &lookup_name,
520      domain_enum domain,
521      int global,
522      symbol_compare_ftype *ordered_compare) override;
523 
524   bool expand_symtabs_matching
525     (struct objfile *objfile,
526      gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
527      const lookup_name_info *lookup_name,
528      gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
529      gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
530      block_search_flags search_flags,
531      domain_enum domain,
532      enum search_domain kind) override;
533 
534   struct compunit_symtab *find_pc_sect_compunit_symtab
535     (struct objfile *objfile, struct bound_minimal_symbol msymbol,
536      CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
537 
538   struct compunit_symtab *find_compunit_symtab_by_address
539     (struct objfile *objfile, CORE_ADDR address) override;
540 
541   void map_symbol_filenames (struct objfile *objfile,
542 			     gdb::function_view<symbol_filename_ftype> fun,
543 			     bool need_fullname) override;
544 
545   void relocated () override
546   {
547     m_psymbol_map.clear ();
548   }
549 
550   /* Return a range adapter for the psymtabs.  */
551   psymtab_storage::partial_symtab_range partial_symbols
552        (struct objfile *objfile);
553 
554   /* Return the partial symbol storage associated with this
555      object.  */
556   const std::shared_ptr<psymtab_storage> &get_partial_symtabs () const
557   {
558     return m_partial_symtabs;
559   }
560 
561   /* Replace the partial symbol table storage in this object with
562      SYMS.  */
563   void set_partial_symtabs (const std::shared_ptr<psymtab_storage> &syms)
564   {
565     m_partial_symtabs = syms;
566   }
567 
568   /* Find which partial symtab contains PC and SECTION.  Return NULL if
569      none.  We return the psymtab that contains a symbol whose address
570      exactly matches PC, or, if we cannot find an exact match, the
571      psymtab that contains a symbol whose address is closest to PC.  */
572 
573   struct partial_symtab *find_pc_sect_psymtab
574        (struct objfile *objfile,
575 	CORE_ADDR pc,
576 	struct obj_section *section,
577 	struct bound_minimal_symbol msymbol);
578 
579 private:
580 
581   /* Count the number of partial symbols in *THIS.  */
582   int count_psyms ();
583 
584   void fill_psymbol_map (struct objfile *objfile,
585 			 struct partial_symtab *psymtab,
586 			 std::set<CORE_ADDR> *seen_addrs,
587 			 const std::vector<partial_symbol *> &symbols);
588 
589   /* Storage for the partial symbols.  */
590   std::shared_ptr<psymtab_storage> m_partial_symtabs;
591 
592   /* Map symbol addresses to the partial symtab that defines the
593      object at that address.  */
594 
595   std::vector<std::pair<CORE_ADDR, partial_symtab *>> m_psymbol_map;
596 };
597 
598 #endif /* PSYMPRIV_H */
599