Remove path name from test case
[binutils-gdb.git] / gdb / psymtab.h
1 /* Public 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 PSYMTAB_H
21 #define PSYMTAB_H
22
23 #include "objfiles.h"
24 #include "gdbsupport/gdb_string_view.h"
25 #include "gdbsupport/gdb_obstack.h"
26 #include "symfile.h"
27 #include "gdbsupport/next-iterator.h"
28 #include "bcache.h"
29
30 /* Specialization of bcache to store partial symbols. */
31
32 struct psymbol_bcache : public gdb::bcache
33 {
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;
39
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;
44 };
45
46 /* An instance of this class manages the partial symbol tables and
47 partial symbols for a given objfile.
48
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.
58
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. */
63
64 class psymtab_storage
65 {
66 public:
67 psymtab_storage () = default;
68 ~psymtab_storage ();
69
70 DISABLE_COPY_AND_ASSIGN (psymtab_storage);
71
72 /* Discard all partial symbol tables starting with "psymtabs" and
73 proceeding until "to" has been discarded. */
74
75 void discard_psymtabs_to (struct partial_symtab *to)
76 {
77 while (psymtabs != to)
78 discard_psymtab (psymtabs);
79 }
80
81 /* Discard the partial symbol table. */
82
83 void discard_psymtab (struct partial_symtab *pst);
84
85 /* Return the obstack that is used for storage by this object. */
86
87 struct obstack *obstack ()
88 {
89 if (!m_obstack.has_value ())
90 m_obstack.emplace ();
91 return &*m_obstack;
92 }
93
94 /* Allocate storage for the "dependencies" field of a psymtab.
95 NUMBER says how many dependencies there are. */
96
97 struct partial_symtab **allocate_dependencies (int number)
98 {
99 return OBSTACK_CALLOC (obstack (), number, struct partial_symtab *);
100 }
101
102 /* Install a psymtab on the psymtab list. This transfers ownership
103 of PST to this object. */
104
105 void install_psymtab (partial_symtab *pst);
106
107 using partial_symtab_range = next_range<partial_symtab>;
108
109 /* A range adapter that makes it possible to iterate over all
110 psymtabs in one objfile. */
111
112 partial_symtab_range range ()
113 {
114 return partial_symtab_range (psymtabs);
115 }
116
117
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). */
121
122 struct partial_symtab *psymtabs = nullptr;
123
124 /* A byte cache where we can stash arbitrary "chunks" of bytes that
125 will not change. */
126
127 psymbol_bcache psymbol_cache;
128
129 private:
130
131 /* The obstack where allocations are made. This is lazily allocated
132 so that we don't waste memory when there are no psymtabs. */
133
134 gdb::optional<auto_obstack> m_obstack;
135 };
136
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. */
143
144 /* This structure is space critical. See space comments at the top of
145 symtab.h. */
146
147 struct partial_symbol
148 {
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
152 {
153 return ginfo.obj_section (objfile);
154 }
155
156 /* Return the unrelocated address of this partial symbol. */
157 unrelocated_addr unrelocated_address () const
158 {
159 return ginfo.unrelocated_address ();
160 }
161
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
165 {
166 return (CORE_ADDR (ginfo.unrelocated_address ())
167 + objfile->section_offsets[ginfo.section_index ()]);
168 }
169
170 /* Set the address of this partial symbol. The address must be
171 unrelocated. */
172 void set_unrelocated_address (unrelocated_addr addr)
173 {
174 ginfo.set_unrelocated_address (addr);
175 }
176
177 /* Note that partial_symbol does not derive from general_symbol_info
178 due to the bcache. See add_psymbol_to_bcache. */
179
180 struct general_symbol_info ginfo;
181
182 /* Name space code. */
183
184 ENUM_BITFIELD(domain_enum) domain : SYMBOL_DOMAIN_BITS;
185
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. */
189
190 ENUM_BITFIELD(address_class) aclass : SYMBOL_ACLASS_BITS;
191 };
192
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. */
196
197 enum psymtab_search_status
198 {
199 PST_NOT_SEARCHED,
200 PST_SEARCHED_AND_FOUND,
201 PST_SEARCHED_AND_NOT_FOUND
202 };
203
204 /* Specify whether a partial psymbol should be allocated on the global
205 list or the static list. */
206
207 enum class psymbol_placement
208 {
209 STATIC,
210 GLOBAL
211 };
212
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.
218
219 Even after the source file has been read into a symtab, the
220 partial_symtab remains around. */
221
222 struct partial_symtab
223 {
224 /* Allocate a new partial symbol table.
225
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. */
230
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);
235
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. */
239
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);
245
246 virtual ~partial_symtab ()
247 {
248 }
249
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.
259
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;
263
264 /* Expand the full symbol table for this partial symbol table. Typically
265 calls expand_dependencies. */
266 virtual void expand_psymtab (struct objfile *) = 0;
267
268 /* Ensure that all the dependencies are read in. Calls
269 expand_psymtab for each non-shared dependency. */
270 void expand_dependencies (struct objfile *);
271
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;
275
276 /* Return a pointer to the compunit allocated for this source file
277 in the context of this objfile.
278
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;
283
284 /* Return the unrelocated low text address of this
285 partial_symtab. */
286 unrelocated_addr unrelocated_text_low () const
287 {
288 return m_text_low;
289 }
290
291 /* Return the unrelocated_addr high text address of this
292 partial_symtab. */
293 unrelocated_addr unrelocated_text_high () const
294 {
295 return m_text_high;
296 }
297
298 /* Return the relocated low text address of this partial_symtab. */
299 CORE_ADDR text_low (struct objfile *objfile) const
300 {
301 return CORE_ADDR (m_text_low) + objfile->text_section_offset ();
302 }
303
304 /* Return the relocated high text address of this partial_symtab. */
305 CORE_ADDR text_high (struct objfile *objfile) const
306 {
307 return CORE_ADDR (m_text_high) + objfile->text_section_offset ();
308 }
309
310 /* Set the low text address of this partial_symtab. */
311 void set_text_low (unrelocated_addr addr)
312 {
313 m_text_low = addr;
314 text_low_valid = 1;
315 }
316
317 /* Set the high text address of this partial_symtab. */
318 void set_text_high (unrelocated_addr addr)
319 {
320 m_text_high = addr;
321 text_high_valid = 1;
322 }
323
324 /* Return true if this symtab is empty -- meaning that it contains
325 no symbols. It may still have dependencies. */
326 bool empty () const
327 {
328 return global_psymbols.empty () && static_psymbols.empty ();
329 }
330
331 /* Add a symbol to this partial symbol table of OBJFILE.
332
333 If COPY_NAME is true, make a copy of NAME, otherwise use the passed
334 reference.
335
336 THECLASS is the type of symbol.
337
338 SECTION is the index of the section of OBJFILE in which the symbol is found.
339
340 WHERE determines whether the symbol goes in the list of static or global
341 partial symbols.
342
343 COREADDR is the address of the symbol. For partial symbols that don't have
344 an address, zero is passed.
345
346 LANGUAGE is the language from which the symbol originates. This will
347 influence, amongst other things, how the symbol name is demangled. */
348
349 void add_psymbol (gdb::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);
358
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. */
362
363 void add_psymbol (const partial_symbol &psym,
364 psymbol_placement where,
365 psymtab_storage *partial_symtabs,
366 struct objfile *objfile);
367
368
369 /* Indicate that this partial symtab is complete. */
370
371 void end ();
372
373 /* Chain of all existing partial symtabs. */
374
375 struct partial_symtab *next = nullptr;
376
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. */
380
381 const char *filename = nullptr;
382
383 /* Full path of the source file. NULL if not known. */
384
385 char *fullname = nullptr;
386
387 /* Directory in which it was compiled, or NULL if we don't know. */
388
389 const char *dirname = nullptr;
390
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. */
397
398 unrelocated_addr m_text_low {};
399 unrelocated_addr m_text_high {};
400
401 /* If NULL, this is an ordinary partial symbol table.
402
403 If non-NULL, this holds a single includer of this partial symbol
404 table, and this partial symbol table is a shared one.
405
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.
409
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.
413
414 A shared psymtabs can be referred to by other shared ones.
415
416 The psymtabs that refer to a shared psymtab will list the shared
417 psymtab in their 'dependencies' array.
418
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.
422
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. */
427
428 struct partial_symtab *user = nullptr;
429
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. */
438
439 struct partial_symtab **dependencies = nullptr;
440
441 int number_of_dependencies = 0;
442
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. */
446
447 std::vector<partial_symbol *> global_psymbols;
448
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). */
455
456 std::vector<partial_symbol *> static_psymbols;
457
458 /* True if the name of this partial symtab is not a source file name. */
459
460 bool anonymous = false;
461
462 /* A flag that is temporarily used when searching psymtabs. */
463
464 ENUM_BITFIELD (psymtab_search_status) searched_flag : 2;
465
466 /* Validity of the m_text_low and m_text_high fields. */
467
468 unsigned int text_low_valid : 1;
469 unsigned int text_high_valid : 1;
470 };
471
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
476 {
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)
481 {
482 }
483
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)
489 {
490 }
491
492 bool readin_p (struct objfile *) const override
493 {
494 return readin;
495 }
496
497 struct compunit_symtab *get_compunit_symtab (struct objfile *) const override
498 {
499 return compunit_symtab;
500 }
501
502 /* True if the symtab corresponding to this psymtab has been
503 readin. */
504
505 bool readin = false;
506
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. */
509
510 struct compunit_symtab *compunit_symtab = nullptr;
511 };
512
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. */
516
517 struct legacy_psymtab : public standard_psymtab
518 {
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)
523 {
524 }
525
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)
531 {
532 }
533
534 void read_symtab (struct objfile *objf) override
535 {
536 if (legacy_read_symtab)
537 (*legacy_read_symtab) (this, objf);
538 }
539
540 void expand_psymtab (struct objfile *objf) override
541 {
542 (*legacy_expand_psymtab) (this, objf);
543 }
544
545 /* Pointer to function which will read in the symtab corresponding to
546 this psymtab. */
547
548 void (*legacy_read_symtab) (legacy_psymtab *, struct objfile *) = nullptr;
549
550 /* Pointer to function which will actually expand this psymtab into
551 a full symtab. */
552
553 void (*legacy_expand_psymtab) (legacy_psymtab *, struct objfile *) = nullptr;
554
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. */
559
560 void *read_symtab_private = nullptr;
561 };
562
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
567 {
568 public:
569
570 psymtab_discarder (psymtab_storage *partial_symtabs)
571 : m_partial_symtabs (partial_symtabs),
572 m_psymtab (partial_symtabs->psymtabs)
573 {
574 }
575
576 ~psymtab_discarder ()
577 {
578 if (m_partial_symtabs != nullptr)
579 m_partial_symtabs->discard_psymtabs_to (m_psymtab);
580 }
581
582 /* Keep any partial symbol tables that were built. */
583 void keep ()
584 {
585 m_partial_symtabs = nullptr;
586 }
587
588 private:
589
590 /* The partial symbol storage object. */
591 psymtab_storage *m_partial_symtabs;
592 /* How far back to free. */
593 struct partial_symtab *m_psymtab;
594 };
595
596 /* An implementation of quick_symbol_functions, specialized for
597 partial symbols. */
598 struct psymbol_functions : public quick_symbol_functions
599 {
600 explicit psymbol_functions (const std::shared_ptr<psymtab_storage> &storage)
601 : m_partial_symtabs (storage)
602 {
603 }
604
605 psymbol_functions ()
606 : m_partial_symtabs (new psymtab_storage)
607 {
608 }
609
610 bool has_symbols (struct objfile *objfile) override;
611
612 bool has_unexpanded_symtabs (struct objfile *objfile) override;
613
614 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
615
616 void forget_cached_source_info (struct objfile *objfile) override;
617
618 enum language lookup_global_symbol_language (struct objfile *objfile,
619 const char *name,
620 domain_enum domain,
621 bool *symbol_found_p) override;
622
623 void print_stats (struct objfile *objfile, bool print_bcache) override;
624
625 void dump (struct objfile *objfile) override;
626
627 void expand_all_symtabs (struct objfile *objfile) override;
628
629 void expand_matching_symbols
630 (struct objfile *,
631 const lookup_name_info &lookup_name,
632 domain_enum domain,
633 int global,
634 symbol_compare_ftype *ordered_compare) override;
635
636 bool expand_symtabs_matching
637 (struct objfile *objfile,
638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
639 const lookup_name_info *lookup_name,
640 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
641 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
642 block_search_flags search_flags,
643 domain_enum domain,
644 enum search_domain kind) override;
645
646 struct compunit_symtab *find_pc_sect_compunit_symtab
647 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
648 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
649
650 struct compunit_symtab *find_compunit_symtab_by_address
651 (struct objfile *objfile, CORE_ADDR address) override
652 {
653 return nullptr;
654 }
655
656 void map_symbol_filenames (struct objfile *objfile,
657 gdb::function_view<symbol_filename_ftype> fun,
658 bool need_fullname) override;
659
660 /* Return a range adapter for the psymtabs. */
661 psymtab_storage::partial_symtab_range partial_symbols
662 (struct objfile *objfile);
663
664 /* Return the partial symbol storage associated with this
665 object. */
666 const std::shared_ptr<psymtab_storage> &get_partial_symtabs () const
667 {
668 return m_partial_symtabs;
669 }
670
671 /* Replace the partial symbol table storage in this object with
672 SYMS. */
673 void set_partial_symtabs (const std::shared_ptr<psymtab_storage> &syms)
674 {
675 m_partial_symtabs = syms;
676 }
677
678 /* Find which partial symtab contains PC and SECTION. Return NULL if
679 none. We return the psymtab that contains a symbol whose address
680 exactly matches PC, or, if we cannot find an exact match, the
681 psymtab that contains a symbol whose address is closest to PC. */
682
683 struct partial_symtab *find_pc_sect_psymtab
684 (struct objfile *objfile,
685 CORE_ADDR pc,
686 struct obj_section *section,
687 struct bound_minimal_symbol msymbol);
688
689 private:
690
691 /* Count the number of partial symbols in *THIS. */
692 int count_psyms ();
693
694 /* Storage for the partial symbols. */
695 std::shared_ptr<psymtab_storage> m_partial_symtabs;
696 };
697
698 #endif /* PSYMTAB_H */