1 /* Partial symbol tables.
3 Copyright (C) 2009-2020 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/>. */
25 #include "filenames.h"
31 #include "readline/tilde.h"
32 #include "gdb_regex.h"
33 #include "dictionary.h"
35 #include "cp-support.h"
40 static struct partial_symbol
*lookup_partial_symbol (struct objfile
*,
41 struct partial_symtab
*,
45 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
47 static struct partial_symbol
*find_pc_sect_psymbol (struct objfile
*,
48 struct partial_symtab
*,
50 struct obj_section
*);
52 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
53 struct partial_symtab
*pst
);
57 static unsigned long psymbol_hash (const void *addr
, int length
);
58 static int psymbol_compare (const void *addr1
, const void *addr2
, int length
);
60 psymtab_storage::psymtab_storage ()
61 : psymbol_cache (psymbol_hash
, psymbol_compare
)
65 psymtab_storage::~psymtab_storage ()
71 struct partial_symtab
*
72 psymtab_storage::allocate_psymtab ()
74 struct partial_symtab
*psymtab
;
76 if (free_psymtabs
!= nullptr)
78 psymtab
= free_psymtabs
;
79 free_psymtabs
= psymtab
->next
;
82 psymtab
= XOBNEW (obstack (), struct partial_symtab
);
84 memset (psymtab
, 0, sizeof (struct partial_symtab
));
86 psymtab
->next
= psymtabs
;
96 psymtab_storage::partial_symtab_range
97 require_partial_symbols (struct objfile
*objfile
, bool verbose
)
99 if ((objfile
->flags
& OBJF_PSYMTABS_READ
) == 0)
101 objfile
->flags
|= OBJF_PSYMTABS_READ
;
103 if (objfile
->sf
->sym_read_psymbols
)
106 printf_filtered (_("Reading symbols from %s...\n"),
107 objfile_name (objfile
));
108 (*objfile
->sf
->sym_read_psymbols
) (objfile
);
110 /* Partial symbols list are not expected to changed after this
112 objfile
->partial_symtabs
->global_psymbols
.shrink_to_fit ();
113 objfile
->partial_symtabs
->static_psymbols
.shrink_to_fit ();
115 if (verbose
&& !objfile_has_symbols (objfile
))
116 printf_filtered (_("(No debugging symbols found in %s)\n"),
117 objfile_name (objfile
));
121 return objfile
->psymtabs ();
124 /* Helper function for psym_map_symtabs_matching_filename that
125 expands the symtabs and calls the iterator. */
128 partial_map_expand_apply (struct objfile
*objfile
,
130 const char *real_path
,
131 struct partial_symtab
*pst
,
132 gdb::function_view
<bool (symtab
*)> callback
)
134 struct compunit_symtab
*last_made
= objfile
->compunit_symtabs
;
136 /* Shared psymtabs should never be seen here. Instead they should
137 be handled properly by the caller. */
138 gdb_assert (pst
->user
== NULL
);
140 /* Don't visit already-expanded psymtabs. */
144 /* This may expand more than one symtab, and we want to iterate over
146 psymtab_to_symtab (objfile
, pst
);
148 return iterate_over_some_symtabs (name
, real_path
, objfile
->compunit_symtabs
,
149 last_made
, callback
);
152 /* Psymtab version of map_symtabs_matching_filename. See its definition in
153 the definition of quick_symbol_functions in symfile.h. */
156 psym_map_symtabs_matching_filename
157 (struct objfile
*objfile
,
159 const char *real_path
,
160 gdb::function_view
<bool (symtab
*)> callback
)
162 const char *name_basename
= lbasename (name
);
164 for (partial_symtab
*pst
: require_partial_symbols (objfile
, true))
166 /* We can skip shared psymtabs here, because any file name will be
167 attached to the unshared psymtab. */
168 if (pst
->user
!= NULL
)
171 /* Anonymous psymtabs don't have a file name. */
175 if (compare_filenames_for_search (pst
->filename
, name
))
177 if (partial_map_expand_apply (objfile
, name
, real_path
,
183 /* Before we invoke realpath, which can get expensive when many
184 files are involved, do a quick comparison of the basenames. */
185 if (! basenames_may_differ
186 && FILENAME_CMP (name_basename
, lbasename (pst
->filename
)) != 0)
189 if (compare_filenames_for_search (psymtab_to_fullname (pst
), name
))
191 if (partial_map_expand_apply (objfile
, name
, real_path
,
197 /* If the user gave us an absolute path, try to find the file in
198 this symtab and use its absolute path. */
199 if (real_path
!= NULL
)
201 gdb_assert (IS_ABSOLUTE_PATH (real_path
));
202 gdb_assert (IS_ABSOLUTE_PATH (name
));
203 if (filename_cmp (psymtab_to_fullname (pst
), real_path
) == 0)
205 if (partial_map_expand_apply (objfile
, name
, real_path
,
216 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
217 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
219 static struct partial_symtab
*
220 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
221 CORE_ADDR pc
, struct obj_section
*section
,
222 struct partial_symtab
*pst
,
223 struct bound_minimal_symbol msymbol
)
225 struct partial_symtab
*tpst
;
226 struct partial_symtab
*best_pst
= pst
;
227 CORE_ADDR best_addr
= pst
->text_low (objfile
);
229 gdb_assert (!pst
->psymtabs_addrmap_supported
);
231 /* An objfile that has its functions reordered might have
232 many partial symbol tables containing the PC, but
233 we want the partial symbol table that contains the
234 function containing the PC. */
235 if (!(objfile
->flags
& OBJF_REORDERED
)
236 && section
== NULL
) /* Can't validate section this way. */
239 if (msymbol
.minsym
== NULL
)
242 /* The code range of partial symtabs sometimes overlap, so, in
243 the loop below, we need to check all partial symtabs and
244 find the one that fits better for the given PC address. We
245 select the partial symtab that contains a symbol whose
246 address is closest to the PC address. By closest we mean
247 that find_pc_sect_symbol returns the symbol with address
248 that is closest and still less than the given PC. */
249 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
251 if (pc
>= tpst
->text_low (objfile
) && pc
< tpst
->text_high (objfile
))
253 struct partial_symbol
*p
;
256 /* NOTE: This assumes that every psymbol has a
257 corresponding msymbol, which is not necessarily
258 true; the debug info might be much richer than the
259 object's symbol table. */
260 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
262 && (p
->address (objfile
) == BMSYMBOL_VALUE_ADDRESS (msymbol
)))
265 /* Also accept the textlow value of a psymtab as a
266 "symbol", to provide some support for partial
267 symbol tables with line information but no debug
268 symbols (e.g. those produced by an assembler). */
270 this_addr
= p
->address (objfile
);
272 this_addr
= tpst
->text_low (objfile
);
274 /* Check whether it is closer than our current
275 BEST_ADDR. Since this symbol address is
276 necessarily lower or equal to PC, the symbol closer
277 to PC is the symbol which address is the highest.
278 This way we return the psymtab which contains such
279 best match symbol. This can help in cases where the
280 symbol information/debuginfo is not complete, like
281 for instance on IRIX6 with gcc, where no debug info
282 is emitted for statics. (See also the nodebug.exp
284 if (this_addr
> best_addr
)
286 best_addr
= this_addr
;
294 /* Find which partial symtab contains PC and SECTION. Return NULL if
295 none. We return the psymtab that contains a symbol whose address
296 exactly matches PC, or, if we cannot find an exact match, the
297 psymtab that contains a symbol whose address is closest to PC. */
299 static struct partial_symtab
*
300 find_pc_sect_psymtab (struct objfile
*objfile
, CORE_ADDR pc
,
301 struct obj_section
*section
,
302 struct bound_minimal_symbol msymbol
)
304 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better
305 granularity than the later used TEXTLOW/TEXTHIGH one. However, we need
306 to take care as the PSYMTABS_ADDRMAP can hold things other than partial
307 symtabs in some cases.
309 This function should only be called for objfiles that are using partial
310 symtabs, not for objfiles that are using indexes (.gdb_index or
311 .debug_names), however 'maintenance print psymbols' calls this function
312 directly for all objfiles. If we assume that PSYMTABS_ADDRMAP contains
313 partial symtabs then we will end up returning a pointer to an object
314 that is not a partial_symtab, which doesn't end well. */
316 if (objfile
->partial_symtabs
->psymtabs
!= NULL
317 && objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
319 CORE_ADDR baseaddr
= objfile
->section_offsets
[SECT_OFF_TEXT (objfile
)];
321 struct partial_symtab
*pst
322 = ((struct partial_symtab
*)
323 addrmap_find (objfile
->partial_symtabs
->psymtabs_addrmap
,
327 /* FIXME: addrmaps currently do not handle overlayed sections,
328 so fall back to the non-addrmap case if we're debugging
329 overlays and the addrmap returned the wrong section. */
330 if (overlay_debugging
&& msymbol
.minsym
!= NULL
&& section
!= NULL
)
332 struct partial_symbol
*p
;
334 /* NOTE: This assumes that every psymbol has a
335 corresponding msymbol, which is not necessarily
336 true; the debug info might be much richer than the
337 object's symbol table. */
338 p
= find_pc_sect_psymbol (objfile
, pst
, pc
, section
);
340 || (p
->address (objfile
)
341 != BMSYMBOL_VALUE_ADDRESS (msymbol
)))
345 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
346 PSYMTABS_ADDRMAP we used has already the best 1-byte
347 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
348 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
357 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
358 which still have no corresponding full SYMTABs read. But it is not
359 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
362 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
363 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
364 debug info type in single OBJFILE. */
366 for (partial_symtab
*pst
: require_partial_symbols (objfile
, true))
367 if (!pst
->psymtabs_addrmap_supported
368 && pc
>= pst
->text_low (objfile
) && pc
< pst
->text_high (objfile
))
370 struct partial_symtab
*best_pst
;
372 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
374 if (best_pst
!= NULL
)
381 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
382 the definition of quick_symbol_functions in symfile.h. */
384 static struct compunit_symtab
*
385 psym_find_pc_sect_compunit_symtab (struct objfile
*objfile
,
386 struct bound_minimal_symbol msymbol
,
388 struct obj_section
*section
,
391 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
, section
,
395 if (warn_if_readin
&& ps
->readin
)
396 /* Might want to error() here (in case symtab is corrupt and
397 will cause a core dump), but maybe we can successfully
398 continue, so let's not. */
400 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
401 paddress (get_objfile_arch (objfile
), pc
));
402 psymtab_to_symtab (objfile
, ps
);
403 return ps
->compunit_symtab
;
408 /* Find which partial symbol within a psymtab matches PC and SECTION.
409 Return NULL if none. */
411 static struct partial_symbol
*
412 find_pc_sect_psymbol (struct objfile
*objfile
,
413 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
414 struct obj_section
*section
)
416 struct partial_symbol
*best
= NULL
;
418 const CORE_ADDR textlow
= psymtab
->text_low (objfile
);
420 gdb_assert (psymtab
!= NULL
);
422 /* Cope with programs that start at address 0. */
423 best_pc
= (textlow
!= 0) ? textlow
- 1 : 0;
425 /* Search the global symbols as well as the static symbols, so that
426 find_pc_partial_function doesn't use a minimal symbol and thus
427 cache a bad endaddr. */
428 for (int i
= 0; i
< psymtab
->n_global_syms
; i
++)
431 = objfile
->partial_symtabs
->global_psymbols
[psymtab
->globals_offset
434 if (p
->domain
== VAR_DOMAIN
435 && p
->aclass
== LOC_BLOCK
436 && pc
>= p
->address (objfile
)
437 && (p
->address (objfile
) > best_pc
438 || (psymtab
->text_low (objfile
) == 0
439 && best_pc
== 0 && p
->address (objfile
) == 0)))
441 if (section
!= NULL
) /* Match on a specific section. */
443 if (!matching_obj_sections (p
->obj_section (objfile
),
447 best_pc
= p
->address (objfile
);
452 for (int i
= 0; i
< psymtab
->n_static_syms
; i
++)
455 = objfile
->partial_symtabs
->static_psymbols
[psymtab
->statics_offset
458 if (p
->domain
== VAR_DOMAIN
459 && p
->aclass
== LOC_BLOCK
460 && pc
>= p
->address (objfile
)
461 && (p
->address (objfile
) > best_pc
462 || (psymtab
->text_low (objfile
) == 0
463 && best_pc
== 0 && p
->address (objfile
) == 0)))
465 if (section
!= NULL
) /* Match on a specific section. */
467 if (!matching_obj_sections (p
->obj_section (objfile
),
471 best_pc
= p
->address (objfile
);
479 /* Psymtab version of lookup_symbol. See its definition in
480 the definition of quick_symbol_functions in symfile.h. */
482 static struct compunit_symtab
*
483 psym_lookup_symbol (struct objfile
*objfile
,
484 block_enum block_index
, const char *name
,
485 const domain_enum domain
)
487 const int psymtab_index
= (block_index
== GLOBAL_BLOCK
? 1 : 0);
488 struct compunit_symtab
*stab_best
= NULL
;
490 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
492 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
494 if (!ps
->readin
&& lookup_partial_symbol (objfile
, ps
, name
,
495 psymtab_index
, domain
))
497 struct symbol
*sym
, *with_opaque
= NULL
;
498 struct compunit_symtab
*stab
= psymtab_to_symtab (objfile
, ps
);
499 /* Note: While psymtab_to_symtab can return NULL if the
500 partial symtab is empty, we can assume it won't here
501 because lookup_partial_symbol succeeded. */
502 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
503 const struct block
*block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
505 sym
= block_find_symbol (block
, name
, domain
,
506 block_find_non_opaque_type_preferred
,
509 /* Some caution must be observed with overloaded functions
510 and methods, since the index will not contain any overload
511 information (but NAME might contain it). */
514 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
516 if (with_opaque
!= NULL
517 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
520 /* Keep looking through other psymtabs. */
527 /* Returns true if PSYM matches LOOKUP_NAME. */
530 psymbol_name_matches (partial_symbol
*psym
,
531 const lookup_name_info
&lookup_name
)
533 const language_defn
*lang
= language_def (psym
->ginfo
.language ());
534 symbol_name_matcher_ftype
*name_match
535 = get_symbol_name_matcher (lang
, lookup_name
);
536 return name_match (psym
->ginfo
.search_name (), lookup_name
, NULL
);
539 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
540 the global block of PST if GLOBAL, and otherwise the static block.
541 MATCH is the comparison operation that returns true iff MATCH (s,
542 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
543 non-null, the symbols in the block are assumed to be ordered
544 according to it (allowing binary search). It must be compatible
545 with MATCH. Returns the symbol, if found, and otherwise NULL. */
547 static struct partial_symbol
*
548 match_partial_symbol (struct objfile
*objfile
,
549 struct partial_symtab
*pst
, int global
,
550 const lookup_name_info
&name
, domain_enum domain
,
551 symbol_compare_ftype
*ordered_compare
)
553 struct partial_symbol
**start
, **psym
;
554 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
555 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
556 int do_linear_search
= 1;
562 &objfile
->partial_symtabs
->global_psymbols
[pst
->globals_offset
] :
563 &objfile
->partial_symtabs
->static_psymbols
[pst
->statics_offset
]);
565 if (global
&& ordered_compare
) /* Can use a binary search. */
567 do_linear_search
= 0;
569 /* Binary search. This search is guaranteed to end with center
570 pointing at the earliest partial symbol whose name might be
571 correct. At that point *all* partial symbols with an
572 appropriate name will be checked against the correct
576 top
= start
+ length
- 1;
580 center
= bottom
+ (top
- bottom
) / 2;
581 gdb_assert (center
< top
);
583 enum language lang
= (*center
)->ginfo
.language ();
585 = name
.language_lookup_name (lang
).c_str ();
587 if (ordered_compare ((*center
)->ginfo
.search_name (),
593 gdb_assert (top
== bottom
);
595 while (top
<= real_top
596 && psymbol_name_matches (*top
, name
))
598 if (symbol_matches_domain ((*top
)->ginfo
.language (),
599 (*top
)->domain
, domain
))
605 /* Can't use a binary search or else we found during the binary search that
606 we should also do a linear search. */
608 if (do_linear_search
)
610 for (psym
= start
; psym
< start
+ length
; psym
++)
612 if (symbol_matches_domain ((*psym
)->ginfo
.language (),
613 (*psym
)->domain
, domain
)
614 && psymbol_name_matches (*psym
, name
))
622 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
623 not contain any method/function instance information (since this would
624 force reading type information while reading psymtabs). Therefore,
625 if NAME contains overload information, it must be stripped before searching
628 static gdb::unique_xmalloc_ptr
<char>
629 psymtab_search_name (const char *name
)
631 switch (current_language
->la_language
)
635 if (strchr (name
, '('))
637 gdb::unique_xmalloc_ptr
<char> ret
= cp_remove_params (name
);
649 return make_unique_xstrdup (name
);
652 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
653 Check the global symbols if GLOBAL, the static symbols if not. */
655 static struct partial_symbol
*
656 lookup_partial_symbol (struct objfile
*objfile
,
657 struct partial_symtab
*pst
, const char *name
,
658 int global
, domain_enum domain
)
660 struct partial_symbol
**start
, **psym
;
661 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
662 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
663 int do_linear_search
= 1;
668 gdb::unique_xmalloc_ptr
<char> search_name
= psymtab_search_name (name
);
670 lookup_name_info
lookup_name (search_name
.get (), symbol_name_match_type::FULL
);
673 &objfile
->partial_symtabs
->global_psymbols
[pst
->globals_offset
] :
674 &objfile
->partial_symtabs
->static_psymbols
[pst
->statics_offset
]);
676 if (global
) /* This means we can use a binary search. */
678 do_linear_search
= 0;
680 /* Binary search. This search is guaranteed to end with center
681 pointing at the earliest partial symbol whose name might be
682 correct. At that point *all* partial symbols with an
683 appropriate name will be checked against the correct
687 top
= start
+ length
- 1;
691 center
= bottom
+ (top
- bottom
) / 2;
693 internal_error (__FILE__
, __LINE__
,
694 _("failed internal consistency check"));
695 if (strcmp_iw_ordered ((*center
)->ginfo
.search_name (),
696 search_name
.get ()) >= 0)
705 if (!(top
== bottom
))
706 internal_error (__FILE__
, __LINE__
,
707 _("failed internal consistency check"));
709 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
710 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
711 while (top
>= start
&& symbol_matches_search_name (&(*top
)->ginfo
,
715 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
718 while (top
<= real_top
&& symbol_matches_search_name (&(*top
)->ginfo
,
721 if (symbol_matches_domain ((*top
)->ginfo
.language (),
722 (*top
)->domain
, domain
))
728 /* Can't use a binary search or else we found during the binary search that
729 we should also do a linear search. */
731 if (do_linear_search
)
733 for (psym
= start
; psym
< start
+ length
; psym
++)
735 if (symbol_matches_domain ((*psym
)->ginfo
.language (),
736 (*psym
)->domain
, domain
)
737 && symbol_matches_search_name (&(*psym
)->ginfo
, lookup_name
))
745 /* Get the symbol table that corresponds to a partial_symtab.
746 This is fast after the first time you do it.
747 The result will be NULL if the primary symtab has no symbols,
748 which can happen. Otherwise the result is the primary symtab
749 that contains PST. */
751 static struct compunit_symtab
*
752 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
754 /* If it is a shared psymtab, find an unshared psymtab that includes
755 it. Any such psymtab will do. */
756 while (pst
->user
!= NULL
)
759 /* If it's been looked up before, return it. */
760 if (pst
->compunit_symtab
)
761 return pst
->compunit_symtab
;
763 /* If it has not yet been read in, read it. */
766 scoped_restore decrementer
= increment_reading_symtab ();
768 (*pst
->read_symtab
) (pst
, objfile
);
771 return pst
->compunit_symtab
;
774 /* Psymtab version of find_last_source_symtab. See its definition in
775 the definition of quick_symbol_functions in symfile.h. */
777 static struct symtab
*
778 psym_find_last_source_symtab (struct objfile
*ofp
)
780 struct partial_symtab
*cs_pst
= NULL
;
782 for (partial_symtab
*ps
: require_partial_symbols (ofp
, true))
784 const char *name
= ps
->filename
;
785 int len
= strlen (name
);
787 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
788 || strcmp (name
, "<<C++-namespaces>>") == 0)))
796 internal_error (__FILE__
, __LINE__
,
797 _("select_source_symtab: "
798 "readin pst found and no symtabs."));
802 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
806 return compunit_primary_filetab (cust
);
812 /* Psymtab version of forget_cached_source_info. See its definition in
813 the definition of quick_symbol_functions in symfile.h. */
816 psym_forget_cached_source_info (struct objfile
*objfile
)
818 for (partial_symtab
*pst
: require_partial_symbols (objfile
, true))
820 if (pst
->fullname
!= NULL
)
822 xfree (pst
->fullname
);
823 pst
->fullname
= NULL
;
829 print_partial_symbols (struct gdbarch
*gdbarch
, struct objfile
*objfile
,
830 struct partial_symbol
**p
, int count
, const char *what
,
831 struct ui_file
*outfile
)
833 fprintf_filtered (outfile
, " %s partial symbols:\n", what
);
837 fprintf_filtered (outfile
, " `%s'", (*p
)->ginfo
.linkage_name ());
838 if ((*p
)->ginfo
.demangled_name () != NULL
)
840 fprintf_filtered (outfile
, " `%s'",
841 (*p
)->ginfo
.demangled_name ());
843 fputs_filtered (", ", outfile
);
844 switch ((*p
)->domain
)
847 fputs_filtered ("undefined domain, ", outfile
);
850 /* This is the usual thing -- don't print it. */
853 fputs_filtered ("struct domain, ", outfile
);
856 fputs_filtered ("module domain, ", outfile
);
859 fputs_filtered ("label domain, ", outfile
);
861 case COMMON_BLOCK_DOMAIN
:
862 fputs_filtered ("common block domain, ", outfile
);
865 fputs_filtered ("<invalid domain>, ", outfile
);
868 switch ((*p
)->aclass
)
871 fputs_filtered ("undefined", outfile
);
874 fputs_filtered ("constant int", outfile
);
877 fputs_filtered ("static", outfile
);
880 fputs_filtered ("register", outfile
);
883 fputs_filtered ("pass by value", outfile
);
886 fputs_filtered ("pass by reference", outfile
);
888 case LOC_REGPARM_ADDR
:
889 fputs_filtered ("register address parameter", outfile
);
892 fputs_filtered ("stack parameter", outfile
);
895 fputs_filtered ("type", outfile
);
898 fputs_filtered ("label", outfile
);
901 fputs_filtered ("function", outfile
);
903 case LOC_CONST_BYTES
:
904 fputs_filtered ("constant bytes", outfile
);
907 fputs_filtered ("unresolved", outfile
);
909 case LOC_OPTIMIZED_OUT
:
910 fputs_filtered ("optimized out", outfile
);
913 fputs_filtered ("computed at runtime", outfile
);
916 fputs_filtered ("<invalid location>", outfile
);
919 fputs_filtered (", ", outfile
);
920 fputs_filtered (paddress (gdbarch
, (*p
)->unrelocated_address ()), outfile
);
921 fprintf_filtered (outfile
, "\n");
927 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
928 struct ui_file
*outfile
)
930 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
933 if (psymtab
->anonymous
)
935 fprintf_filtered (outfile
, "\nAnonymous partial symtab (%s) ",
940 fprintf_filtered (outfile
, "\nPartial symtab for source file %s ",
943 fprintf_filtered (outfile
, "(object ");
944 gdb_print_host_address (psymtab
, outfile
);
945 fprintf_filtered (outfile
, ")\n\n");
946 fprintf_filtered (outfile
, " Read from object file %s (",
947 objfile_name (objfile
));
948 gdb_print_host_address (objfile
, outfile
);
949 fprintf_filtered (outfile
, ")\n");
953 fprintf_filtered (outfile
,
954 " Full symtab was read (at ");
955 gdb_print_host_address (psymtab
->compunit_symtab
, outfile
);
956 fprintf_filtered (outfile
, " by function at ");
957 gdb_print_host_address (psymtab
->read_symtab
, outfile
);
958 fprintf_filtered (outfile
, ")\n");
961 fprintf_filtered (outfile
, " Symbols cover text addresses ");
962 fputs_filtered (paddress (gdbarch
, psymtab
->text_low (objfile
)), outfile
);
963 fprintf_filtered (outfile
, "-");
964 fputs_filtered (paddress (gdbarch
, psymtab
->text_high (objfile
)), outfile
);
965 fprintf_filtered (outfile
, "\n");
966 fprintf_filtered (outfile
, " Address map supported - %s.\n",
967 psymtab
->psymtabs_addrmap_supported
? "yes" : "no");
968 fprintf_filtered (outfile
, " Depends on %d other partial symtabs.\n",
969 psymtab
->number_of_dependencies
);
970 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
972 fprintf_filtered (outfile
, " %d ", i
);
973 gdb_print_host_address (psymtab
->dependencies
[i
], outfile
);
974 fprintf_filtered (outfile
, " %s\n",
975 psymtab
->dependencies
[i
]->filename
);
977 if (psymtab
->user
!= NULL
)
979 fprintf_filtered (outfile
, " Shared partial symtab with user ");
980 gdb_print_host_address (psymtab
->user
, outfile
);
981 fprintf_filtered (outfile
, "\n");
983 if (psymtab
->n_global_syms
> 0)
985 print_partial_symbols
987 &objfile
->partial_symtabs
->global_psymbols
[psymtab
->globals_offset
],
988 psymtab
->n_global_syms
, "Global", outfile
);
990 if (psymtab
->n_static_syms
> 0)
992 print_partial_symbols
994 &objfile
->partial_symtabs
->static_psymbols
[psymtab
->statics_offset
],
995 psymtab
->n_static_syms
, "Static", outfile
);
997 fprintf_filtered (outfile
, "\n");
1000 /* Psymtab version of print_stats. See its definition in
1001 the definition of quick_symbol_functions in symfile.h. */
1004 psym_print_stats (struct objfile
*objfile
)
1009 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1011 if (ps
->readin
== 0)
1014 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i
);
1017 /* Psymtab version of dump. See its definition in
1018 the definition of quick_symbol_functions in symfile.h. */
1021 psym_dump (struct objfile
*objfile
)
1023 struct partial_symtab
*psymtab
;
1025 if (objfile
->partial_symtabs
->psymtabs
)
1027 printf_filtered ("Psymtabs:\n");
1028 for (psymtab
= objfile
->partial_symtabs
->psymtabs
;
1030 psymtab
= psymtab
->next
)
1032 printf_filtered ("%s at ",
1034 gdb_print_host_address (psymtab
, gdb_stdout
);
1035 printf_filtered (", ");
1038 printf_filtered ("\n\n");
1042 /* Psymtab version of expand_symtabs_for_function. See its definition in
1043 the definition of quick_symbol_functions in symfile.h. */
1046 psym_expand_symtabs_for_function (struct objfile
*objfile
,
1047 const char *func_name
)
1049 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1054 if ((lookup_partial_symbol (objfile
, ps
, func_name
, 1, VAR_DOMAIN
)
1056 || (lookup_partial_symbol (objfile
, ps
, func_name
, 0, VAR_DOMAIN
)
1058 psymtab_to_symtab (objfile
, ps
);
1062 /* Psymtab version of expand_all_symtabs. See its definition in
1063 the definition of quick_symbol_functions in symfile.h. */
1066 psym_expand_all_symtabs (struct objfile
*objfile
)
1068 for (partial_symtab
*psymtab
: require_partial_symbols (objfile
, true))
1069 psymtab_to_symtab (objfile
, psymtab
);
1072 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1073 the definition of quick_symbol_functions in symfile.h. */
1076 psym_expand_symtabs_with_fullname (struct objfile
*objfile
,
1077 const char *fullname
)
1079 for (partial_symtab
*p
: require_partial_symbols (objfile
, true))
1081 /* Anonymous psymtabs don't have a name of a source file. */
1085 /* psymtab_to_fullname tries to open the file which is slow.
1086 Don't call it if we know the basenames don't match. */
1087 if ((basenames_may_differ
1088 || filename_cmp (lbasename (fullname
), lbasename (p
->filename
)) == 0)
1089 && filename_cmp (fullname
, psymtab_to_fullname (p
)) == 0)
1090 psymtab_to_symtab (objfile
, p
);
1094 /* Psymtab version of map_symbol_filenames. See its definition in
1095 the definition of quick_symbol_functions in symfile.h. */
1098 psym_map_symbol_filenames (struct objfile
*objfile
,
1099 symbol_filename_ftype
*fun
, void *data
,
1102 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1104 const char *fullname
;
1109 /* We can skip shared psymtabs here, because any file name will be
1110 attached to the unshared psymtab. */
1111 if (ps
->user
!= NULL
)
1114 /* Anonymous psymtabs don't have a file name. */
1120 fullname
= psymtab_to_fullname (ps
);
1123 (*fun
) (ps
->filename
, fullname
, data
);
1127 /* Finds the fullname that a partial_symtab represents.
1129 If this functions finds the fullname, it will save it in ps->fullname
1130 and it will also return the value.
1132 If this function fails to find the file that this partial_symtab represents,
1133 NULL will be returned and ps->fullname will be set to NULL. */
1136 psymtab_to_fullname (struct partial_symtab
*ps
)
1138 gdb_assert (!ps
->anonymous
);
1140 /* Use cached copy if we have it.
1141 We rely on forget_cached_source_info being called appropriately
1142 to handle cases like the file being moved. */
1143 if (ps
->fullname
== NULL
)
1145 gdb::unique_xmalloc_ptr
<char> fullname
;
1146 scoped_fd fd
= find_and_open_source (ps
->filename
, ps
->dirname
,
1148 ps
->fullname
= fullname
.release ();
1152 /* rewrite_source_path would be applied by find_and_open_source, we
1153 should report the pathname where GDB tried to find the file. */
1155 if (ps
->dirname
== NULL
|| IS_ABSOLUTE_PATH (ps
->filename
))
1156 fullname
.reset (xstrdup (ps
->filename
));
1158 fullname
.reset (concat (ps
->dirname
, SLASH_STRING
,
1159 ps
->filename
, (char *) NULL
));
1161 ps
->fullname
= rewrite_source_path (fullname
.get ()).release ();
1162 if (ps
->fullname
== NULL
)
1163 ps
->fullname
= fullname
.release ();
1167 return ps
->fullname
;
1170 /* Psymtab version of map_matching_symbols. See its definition in
1171 the definition of quick_symbol_functions in symfile.h. */
1174 psym_map_matching_symbols
1175 (struct objfile
*objfile
,
1176 const lookup_name_info
&name
, domain_enum domain
,
1178 gdb::function_view
<symbol_found_callback_ftype
> callback
,
1179 symbol_compare_ftype
*ordered_compare
)
1181 const int block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
1183 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1187 || match_partial_symbol (objfile
, ps
, global
, name
, domain
,
1190 struct compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
1191 const struct block
*block
;
1195 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), block_kind
);
1196 if (!iterate_over_symbols_terminated (block
, name
,
1203 /* A helper for psym_expand_symtabs_matching that handles searching
1204 included psymtabs. This returns true if a symbol is found, and
1205 false otherwise. It also updates the 'searched_flag' on the
1206 various psymtabs that it searches. */
1209 recursively_search_psymtabs
1210 (struct partial_symtab
*ps
,
1211 struct objfile
*objfile
,
1212 enum search_domain domain
,
1213 const lookup_name_info
&lookup_name
,
1214 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
1217 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
1220 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
1221 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
1223 /* Recurse into shared psymtabs first, because they may have already
1224 been searched, and this could save some time. */
1225 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
1229 /* Skip non-shared dependencies, these are handled elsewhere. */
1230 if (ps
->dependencies
[i
]->user
== NULL
)
1233 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
1234 objfile
, domain
, lookup_name
,
1238 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
1243 partial_symbol
**gbound
1244 = (objfile
->partial_symtabs
->global_psymbols
.data ()
1245 + ps
->globals_offset
+ ps
->n_global_syms
);
1246 partial_symbol
**sbound
1247 = (objfile
->partial_symtabs
->static_psymbols
.data ()
1248 + ps
->statics_offset
+ ps
->n_static_syms
);
1249 partial_symbol
**bound
= gbound
;
1251 /* Go through all of the symbols stored in a partial
1252 symtab in one loop. */
1253 partial_symbol
**psym
= (objfile
->partial_symtabs
->global_psymbols
.data ()
1254 + ps
->globals_offset
);
1259 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
1261 psym
= (objfile
->partial_symtabs
->static_psymbols
.data ()
1262 + ps
->statics_offset
);
1273 if ((domain
== ALL_DOMAIN
1274 || (domain
== MODULES_DOMAIN
1275 && (*psym
)->domain
== MODULE_DOMAIN
)
1276 || (domain
== VARIABLES_DOMAIN
1277 && (*psym
)->aclass
!= LOC_TYPEDEF
1278 && (*psym
)->aclass
!= LOC_BLOCK
)
1279 || (domain
== FUNCTIONS_DOMAIN
1280 && (*psym
)->aclass
== LOC_BLOCK
)
1281 || (domain
== TYPES_DOMAIN
1282 && (*psym
)->aclass
== LOC_TYPEDEF
))
1283 && psymbol_name_matches (*psym
, lookup_name
)
1284 && (sym_matcher
== NULL
1285 || sym_matcher ((*psym
)->ginfo
.search_name ())))
1287 /* Found a match, so notify our caller. */
1288 result
= PST_SEARCHED_AND_FOUND
;
1295 ps
->searched_flag
= result
;
1296 return result
== PST_SEARCHED_AND_FOUND
;
1299 /* Psymtab version of expand_symtabs_matching. See its definition in
1300 the definition of quick_symbol_functions in symfile.h. */
1303 psym_expand_symtabs_matching
1304 (struct objfile
*objfile
,
1305 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1306 const lookup_name_info
&lookup_name_in
,
1307 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1308 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1309 enum search_domain domain
)
1311 lookup_name_info lookup_name
= lookup_name_in
.make_ignore_params ();
1313 /* Clear the search flags. */
1314 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1315 ps
->searched_flag
= PST_NOT_SEARCHED
;
1317 for (partial_symtab
*ps
: objfile
->psymtabs ())
1324 /* We skip shared psymtabs because file-matching doesn't apply
1325 to them; but we search them later in the loop. */
1326 if (ps
->user
!= NULL
)
1336 match
= file_matcher (ps
->filename
, false);
1339 /* Before we invoke realpath, which can get expensive when many
1340 files are involved, do a quick comparison of the basenames. */
1341 if (basenames_may_differ
1342 || file_matcher (lbasename (ps
->filename
), true))
1343 match
= file_matcher (psymtab_to_fullname (ps
), false);
1349 if (recursively_search_psymtabs (ps
, objfile
, domain
,
1350 lookup_name
, symbol_matcher
))
1352 struct compunit_symtab
*symtab
=
1353 psymtab_to_symtab (objfile
, ps
);
1355 if (expansion_notify
!= NULL
)
1356 expansion_notify (symtab
);
1361 /* Psymtab version of has_symbols. See its definition in
1362 the definition of quick_symbol_functions in symfile.h. */
1365 psym_has_symbols (struct objfile
*objfile
)
1367 return objfile
->partial_symtabs
->psymtabs
!= NULL
;
1370 /* Helper function for psym_find_compunit_symtab_by_address that fills
1371 in psymbol_map for a given range of psymbols. */
1374 psym_fill_psymbol_map (struct objfile
*objfile
,
1375 struct partial_symtab
*psymtab
,
1376 std::set
<CORE_ADDR
> *seen_addrs
,
1377 const std::vector
<partial_symbol
*> &symbols
,
1381 for (int i
= 0; i
< length
; ++i
)
1383 struct partial_symbol
*psym
= symbols
[start
+ i
];
1385 if (psym
->aclass
== LOC_STATIC
)
1387 CORE_ADDR addr
= psym
->address (objfile
);
1388 if (seen_addrs
->find (addr
) == seen_addrs
->end ())
1390 seen_addrs
->insert (addr
);
1391 objfile
->psymbol_map
.emplace_back (addr
, psymtab
);
1397 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1400 static compunit_symtab
*
1401 psym_find_compunit_symtab_by_address (struct objfile
*objfile
,
1404 if (objfile
->psymbol_map
.empty ())
1406 std::set
<CORE_ADDR
> seen_addrs
;
1408 for (partial_symtab
*pst
: require_partial_symbols (objfile
, true))
1410 psym_fill_psymbol_map (objfile
, pst
,
1412 objfile
->partial_symtabs
->global_psymbols
,
1413 pst
->globals_offset
,
1414 pst
->n_global_syms
);
1415 psym_fill_psymbol_map (objfile
, pst
,
1417 objfile
->partial_symtabs
->static_psymbols
,
1418 pst
->statics_offset
,
1419 pst
->n_static_syms
);
1422 objfile
->psymbol_map
.shrink_to_fit ();
1424 std::sort (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (),
1425 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1426 const std::pair
<CORE_ADDR
, partial_symtab
*> &b
)
1428 return a
.first
< b
.first
;
1432 auto iter
= std::lower_bound
1433 (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (), address
,
1434 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1440 if (iter
== objfile
->psymbol_map
.end () || iter
->first
!= address
)
1443 return psymtab_to_symtab (objfile
, iter
->second
);
1446 const struct quick_symbol_functions psym_functions
=
1449 psym_find_last_source_symtab
,
1450 psym_forget_cached_source_info
,
1451 psym_map_symtabs_matching_filename
,
1455 psym_expand_symtabs_for_function
,
1456 psym_expand_all_symtabs
,
1457 psym_expand_symtabs_with_fullname
,
1458 psym_map_matching_symbols
,
1459 psym_expand_symtabs_matching
,
1460 psym_find_pc_sect_compunit_symtab
,
1461 psym_find_compunit_symtab_by_address
,
1462 psym_map_symbol_filenames
1468 sort_pst_symbols (struct objfile
*objfile
, struct partial_symtab
*pst
)
1470 /* Sort the global list; don't sort the static list. */
1471 auto begin
= objfile
->partial_symtabs
->global_psymbols
.begin ();
1472 std::advance (begin
, pst
->globals_offset
);
1474 /* The psymbols for this partial_symtab are currently at the end of the
1476 auto end
= objfile
->partial_symtabs
->global_psymbols
.end ();
1478 std::sort (begin
, end
, [] (partial_symbol
*s1
, partial_symbol
*s2
)
1480 return strcmp_iw_ordered (s1
->ginfo
.search_name (),
1481 s2
->ginfo
.search_name ()) < 0;
1485 /* Allocate and partially fill a partial symtab. It will be
1486 completely filled at the end of the symbol list.
1488 FILENAME is the name of the symbol-file we are reading from. */
1490 struct partial_symtab
*
1491 start_psymtab_common (struct objfile
*objfile
,
1492 const char *filename
,
1495 struct partial_symtab
*psymtab
;
1497 psymtab
= allocate_psymtab (filename
, objfile
);
1498 psymtab
->set_text_low (textlow
);
1499 psymtab
->set_text_high (psymtab
->raw_text_low ()); /* default */
1500 psymtab
->globals_offset
= objfile
->partial_symtabs
->global_psymbols
.size ();
1501 psymtab
->statics_offset
= objfile
->partial_symtabs
->static_psymbols
.size ();
1505 /* Perform "finishing up" operations of a partial symtab. */
1508 end_psymtab_common (struct objfile
*objfile
, struct partial_symtab
*pst
)
1510 pst
->n_global_syms
= (objfile
->partial_symtabs
->global_psymbols
.size ()
1511 - pst
->globals_offset
);
1512 pst
->n_static_syms
= (objfile
->partial_symtabs
->static_psymbols
.size ()
1513 - pst
->statics_offset
);
1515 sort_pst_symbols (objfile
, pst
);
1518 /* Calculate a hash code for the given partial symbol. The hash is
1519 calculated using the symbol's value, language, domain, class
1520 and name. These are the values which are set by
1521 add_psymbol_to_bcache. */
1523 static unsigned long
1524 psymbol_hash (const void *addr
, int length
)
1526 unsigned long h
= 0;
1527 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1528 unsigned int lang
= psymbol
->ginfo
.language ();
1529 unsigned int domain
= psymbol
->domain
;
1530 unsigned int theclass
= psymbol
->aclass
;
1532 h
= fast_hash (&psymbol
->ginfo
.value
, sizeof (psymbol
->ginfo
.value
), h
);
1533 h
= fast_hash (&lang
, sizeof (unsigned int), h
);
1534 h
= fast_hash (&domain
, sizeof (unsigned int), h
);
1535 h
= fast_hash (&theclass
, sizeof (unsigned int), h
);
1536 /* Note that psymbol names are interned via compute_and_set_names, so
1537 there's no need to hash the contents of the name here. */
1538 h
= fast_hash (&psymbol
->ginfo
.m_name
, sizeof (psymbol
->ginfo
.m_name
), h
);
1543 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1544 For the comparison this function uses a symbols value,
1545 language, domain, class and name. */
1548 psymbol_compare (const void *addr1
, const void *addr2
, int length
)
1550 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1551 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1553 return (memcmp (&sym1
->ginfo
.value
, &sym2
->ginfo
.value
,
1554 sizeof (sym1
->ginfo
.value
)) == 0
1555 && sym1
->ginfo
.language () == sym2
->ginfo
.language ()
1556 && sym1
->domain
== sym2
->domain
1557 && sym1
->aclass
== sym2
->aclass
1558 /* Note that psymbol names are interned via
1559 compute_and_set_names, so there's no need to compare the
1560 contents of the name here. */
1561 && sym1
->ginfo
.linkage_name () == sym2
->ginfo
.linkage_name ());
1564 /* Helper function, initialises partial symbol structure and stashes
1565 it into objfile's bcache. Note that our caching mechanism will
1566 use all fields of struct partial_symbol to determine hash value of the
1567 structure. In other words, having two symbols with the same name but
1568 different domain (or address) is possible and correct. */
1570 static struct partial_symbol
*
1571 add_psymbol_to_bcache (gdb::string_view name
, bool copy_name
,
1573 enum address_class theclass
,
1576 enum language language
, struct objfile
*objfile
,
1579 struct partial_symbol psymbol
;
1580 memset (&psymbol
, 0, sizeof (psymbol
));
1582 psymbol
.set_unrelocated_address (coreaddr
);
1583 psymbol
.ginfo
.section
= section
;
1584 psymbol
.domain
= domain
;
1585 psymbol
.aclass
= theclass
;
1586 psymbol
.ginfo
.set_language (language
, objfile
->partial_symtabs
->obstack ());
1587 psymbol
.ginfo
.compute_and_set_names (name
, copy_name
, objfile
->per_bfd
);
1589 /* Stash the partial symbol away in the cache. */
1590 return ((struct partial_symbol
*)
1591 objfile
->partial_symtabs
->psymbol_cache
.insert
1592 (&psymbol
, sizeof (struct partial_symbol
), added
));
1595 /* Helper function, adds partial symbol to the given partial symbol list. */
1598 append_psymbol_to_list (std::vector
<partial_symbol
*> *list
,
1599 struct partial_symbol
*psym
,
1600 struct objfile
*objfile
)
1602 list
->push_back (psym
);
1603 OBJSTAT (objfile
, n_psyms
++);
1606 /* See psympriv.h. */
1609 add_psymbol_to_list (gdb::string_view name
, bool copy_name
,
1611 enum address_class theclass
,
1613 psymbol_placement where
,
1615 enum language language
, struct objfile
*objfile
)
1617 struct partial_symbol
*psym
;
1621 /* Stash the partial symbol away in the cache. */
1622 psym
= add_psymbol_to_bcache (name
, copy_name
, domain
, theclass
,
1623 section
, coreaddr
, language
, objfile
, &added
);
1625 /* Do not duplicate global partial symbols. */
1626 if (where
== psymbol_placement::GLOBAL
&& !added
)
1629 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1630 std::vector
<partial_symbol
*> *list
1631 = (where
== psymbol_placement::STATIC
1632 ? &objfile
->partial_symtabs
->static_psymbols
1633 : &objfile
->partial_symtabs
->global_psymbols
);
1634 append_psymbol_to_list (list
, psym
, objfile
);
1637 /* See psympriv.h. */
1640 init_psymbol_list (struct objfile
*objfile
, int total_symbols
)
1642 if (objfile
->partial_symtabs
->global_psymbols
.capacity () == 0
1643 && objfile
->partial_symtabs
->static_psymbols
.capacity () == 0)
1645 /* Current best guess is that approximately a twentieth of the
1646 total symbols (in a debugging file) are global or static
1647 oriented symbols, then multiply that by slop factor of
1649 objfile
->partial_symtabs
->global_psymbols
.reserve (total_symbols
/ 10);
1650 objfile
->partial_symtabs
->static_psymbols
.reserve (total_symbols
/ 10);
1654 /* See psympriv.h. */
1656 struct partial_symtab
*
1657 allocate_psymtab (const char *filename
, struct objfile
*objfile
)
1659 struct partial_symtab
*psymtab
1660 = objfile
->partial_symtabs
->allocate_psymtab ();
1663 = ((const char *) objfile
->per_bfd
->filename_cache
.insert
1664 (filename
, strlen (filename
) + 1));
1665 psymtab
->compunit_symtab
= NULL
;
1667 if (symtab_create_debug
)
1669 /* Be a bit clever with debugging messages, and don't print objfile
1670 every time, only when it changes. */
1671 static char *last_objfile_name
= NULL
;
1673 if (last_objfile_name
== NULL
1674 || strcmp (last_objfile_name
, objfile_name (objfile
)) != 0)
1676 xfree (last_objfile_name
);
1677 last_objfile_name
= xstrdup (objfile_name (objfile
));
1678 fprintf_filtered (gdb_stdlog
,
1679 "Creating one or more psymtabs for objfile %s ...\n",
1682 fprintf_filtered (gdb_stdlog
,
1683 "Created psymtab %s for module %s.\n",
1684 host_address_to_string (psymtab
), filename
);
1691 psymtab_storage::discard_psymtab (struct partial_symtab
*pst
)
1693 struct partial_symtab
**prev_pst
;
1696 Empty psymtabs happen as a result of header files which don't
1697 have any symbols in them. There can be a lot of them. But this
1698 check is wrong, in that a psymtab with N_SLINE entries but
1699 nothing else is not empty, but we don't realize that. Fixing
1700 that without slowing things down might be tricky. */
1702 /* First, snip it out of the psymtab chain. */
1704 prev_pst
= &psymtabs
;
1705 while ((*prev_pst
) != pst
)
1706 prev_pst
= &((*prev_pst
)->next
);
1707 (*prev_pst
) = pst
->next
;
1709 /* Next, put it on a free list for recycling. */
1711 pst
->next
= free_psymtabs
;
1712 free_psymtabs
= pst
;
1717 /* We need to pass a couple of items to the addrmap_foreach function,
1720 struct dump_psymtab_addrmap_data
1722 struct objfile
*objfile
;
1723 struct partial_symtab
*psymtab
;
1724 struct ui_file
*outfile
;
1726 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1727 If so, we want to print the next one as well (since the next addrmap
1728 entry defines the end of the range). */
1729 int previous_matched
;
1732 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1735 dump_psymtab_addrmap_1 (void *datap
, CORE_ADDR start_addr
, void *obj
)
1737 struct dump_psymtab_addrmap_data
*data
1738 = (struct dump_psymtab_addrmap_data
*) datap
;
1739 struct gdbarch
*gdbarch
= get_objfile_arch (data
->objfile
);
1740 struct partial_symtab
*addrmap_psymtab
= (struct partial_symtab
*) obj
;
1741 const char *psymtab_address_or_end
= NULL
;
1745 if (data
->psymtab
== NULL
1746 || data
->psymtab
== addrmap_psymtab
)
1747 psymtab_address_or_end
= host_address_to_string (addrmap_psymtab
);
1748 else if (data
->previous_matched
)
1749 psymtab_address_or_end
= "<ends here>";
1751 if (data
->psymtab
== NULL
1752 || data
->psymtab
== addrmap_psymtab
1753 || data
->previous_matched
)
1755 fprintf_filtered (data
->outfile
, " %s%s %s\n",
1756 data
->psymtab
!= NULL
? " " : "",
1757 paddress (gdbarch
, start_addr
),
1758 psymtab_address_or_end
);
1761 data
->previous_matched
= (data
->psymtab
== NULL
1762 || data
->psymtab
== addrmap_psymtab
);
1767 /* Helper function for maintenance_print_psymbols to print the addrmap
1768 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1771 dump_psymtab_addrmap (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
1772 struct ui_file
*outfile
)
1774 struct dump_psymtab_addrmap_data addrmap_dump_data
;
1776 if ((psymtab
== NULL
1777 || psymtab
->psymtabs_addrmap_supported
)
1778 && objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
1780 addrmap_dump_data
.objfile
= objfile
;
1781 addrmap_dump_data
.psymtab
= psymtab
;
1782 addrmap_dump_data
.outfile
= outfile
;
1783 addrmap_dump_data
.previous_matched
= 0;
1784 fprintf_filtered (outfile
, "%sddress map:\n",
1785 psymtab
== NULL
? "Entire a" : " A");
1786 addrmap_foreach (objfile
->partial_symtabs
->psymtabs_addrmap
,
1787 dump_psymtab_addrmap_1
, &addrmap_dump_data
);
1792 maintenance_print_psymbols (const char *args
, int from_tty
)
1794 struct ui_file
*outfile
= gdb_stdout
;
1795 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1796 int i
, outfile_idx
, found
;
1798 struct obj_section
*section
= NULL
;
1802 gdb_argv
argv (args
);
1804 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1806 if (strcmp (argv
[i
], "-pc") == 0)
1808 if (argv
[i
+ 1] == NULL
)
1809 error (_("Missing pc value"));
1810 address_arg
= argv
[++i
];
1812 else if (strcmp (argv
[i
], "-source") == 0)
1814 if (argv
[i
+ 1] == NULL
)
1815 error (_("Missing source file"));
1816 source_arg
= argv
[++i
];
1818 else if (strcmp (argv
[i
], "-objfile") == 0)
1820 if (argv
[i
+ 1] == NULL
)
1821 error (_("Missing objfile name"));
1822 objfile_arg
= argv
[++i
];
1824 else if (strcmp (argv
[i
], "--") == 0)
1826 /* End of options. */
1830 else if (argv
[i
][0] == '-')
1832 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1833 error (_("Unknown option: %s"), argv
[i
]);
1840 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1841 error (_("Must specify at most one of -pc and -source"));
1843 stdio_file arg_outfile
;
1845 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
1847 if (argv
[outfile_idx
+ 1] != NULL
)
1848 error (_("Junk at end of command"));
1849 gdb::unique_xmalloc_ptr
<char> outfile_name
1850 (tilde_expand (argv
[outfile_idx
]));
1851 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
1852 perror_with_name (outfile_name
.get ());
1853 outfile
= &arg_outfile
;
1856 if (address_arg
!= NULL
)
1858 pc
= parse_and_eval_address (address_arg
);
1859 /* If we fail to find a section, that's ok, try the lookup anyway. */
1860 section
= find_pc_section (pc
);
1864 for (objfile
*objfile
: current_program_space
->objfiles ())
1866 int printed_objfile_header
= 0;
1867 int print_for_objfile
= 1;
1870 if (objfile_arg
!= NULL
)
1872 = compare_filenames_for_search (objfile_name (objfile
),
1874 if (!print_for_objfile
)
1877 if (address_arg
!= NULL
)
1879 struct bound_minimal_symbol msymbol
= { NULL
, NULL
};
1881 /* We don't assume each pc has a unique objfile (this is for
1883 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
,
1887 if (!printed_objfile_header
)
1889 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1890 objfile_name (objfile
));
1891 printed_objfile_header
= 1;
1893 dump_psymtab (objfile
, ps
, outfile
);
1894 dump_psymtab_addrmap (objfile
, ps
, outfile
);
1900 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
1902 int print_for_source
= 0;
1905 if (source_arg
!= NULL
)
1908 = compare_filenames_for_search (ps
->filename
, source_arg
);
1911 if (source_arg
== NULL
1912 || print_for_source
)
1914 if (!printed_objfile_header
)
1916 outfile
->printf ("\nPartial symtabs for objfile %s\n",
1917 objfile_name (objfile
));
1918 printed_objfile_header
= 1;
1920 dump_psymtab (objfile
, ps
, outfile
);
1921 dump_psymtab_addrmap (objfile
, ps
, outfile
);
1926 /* If we're printing all the objfile's symbols dump the full addrmap. */
1928 if (address_arg
== NULL
1929 && source_arg
== NULL
1930 && objfile
->partial_symtabs
->psymtabs_addrmap
!= NULL
)
1932 outfile
->puts ("\n");
1933 dump_psymtab_addrmap (objfile
, NULL
, outfile
);
1939 if (address_arg
!= NULL
)
1940 error (_("No partial symtab for address: %s"), address_arg
);
1941 if (source_arg
!= NULL
)
1942 error (_("No partial symtab for source file: %s"), source_arg
);
1946 /* List all the partial symbol tables whose names match REGEXP (optional). */
1949 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
1951 struct program_space
*pspace
;
1956 ALL_PSPACES (pspace
)
1957 for (objfile
*objfile
: pspace
->objfiles ())
1959 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1961 /* We don't want to print anything for this objfile until we
1962 actually find a symtab whose name matches. */
1963 int printed_objfile_start
= 0;
1965 for (partial_symtab
*psymtab
: require_partial_symbols (objfile
, true))
1970 || re_exec (psymtab
->filename
))
1972 if (! printed_objfile_start
)
1974 printf_filtered ("{ objfile %s ", objfile_name (objfile
));
1976 printf_filtered ("((struct objfile *) %s)\n",
1977 host_address_to_string (objfile
));
1978 printed_objfile_start
= 1;
1981 printf_filtered (" { psymtab %s ", psymtab
->filename
);
1983 printf_filtered ("((struct partial_symtab *) %s)\n",
1984 host_address_to_string (psymtab
));
1986 printf_filtered (" readin %s\n",
1987 psymtab
->readin
? "yes" : "no");
1988 printf_filtered (" fullname %s\n",
1990 ? psymtab
->fullname
: "(null)");
1991 printf_filtered (" text addresses ");
1992 fputs_filtered (paddress (gdbarch
,
1993 psymtab
->text_low (objfile
)),
1995 printf_filtered (" -- ");
1996 fputs_filtered (paddress (gdbarch
,
1997 psymtab
->text_high (objfile
)),
1999 printf_filtered ("\n");
2000 printf_filtered (" psymtabs_addrmap_supported %s\n",
2001 (psymtab
->psymtabs_addrmap_supported
2003 printf_filtered (" globals ");
2004 if (psymtab
->n_global_syms
)
2006 auto p
= &(objfile
->partial_symtabs
2007 ->global_psymbols
[psymtab
->globals_offset
]);
2010 ("(* (struct partial_symbol **) %s @ %d)\n",
2011 host_address_to_string (p
),
2012 psymtab
->n_global_syms
);
2015 printf_filtered ("(none)\n");
2016 printf_filtered (" statics ");
2017 if (psymtab
->n_static_syms
)
2019 auto p
= &(objfile
->partial_symtabs
2020 ->static_psymbols
[psymtab
->statics_offset
]);
2023 ("(* (struct partial_symbol **) %s @ %d)\n",
2024 host_address_to_string (p
),
2025 psymtab
->n_static_syms
);
2028 printf_filtered ("(none)\n");
2029 printf_filtered (" dependencies ");
2030 if (psymtab
->number_of_dependencies
)
2034 printf_filtered ("{\n");
2035 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
2037 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
2039 /* Note the string concatenation there --- no
2041 printf_filtered (" psymtab %s "
2042 "((struct partial_symtab *) %s)\n",
2044 host_address_to_string (dep
));
2046 printf_filtered (" }\n");
2049 printf_filtered ("(none)\n");
2050 printf_filtered (" }\n");
2054 if (printed_objfile_start
)
2055 printf_filtered ("}\n");
2059 /* Check consistency of currently expanded psymtabs vs symtabs. */
2062 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
2065 struct compunit_symtab
*cust
= NULL
;
2066 const struct blockvector
*bv
;
2067 const struct block
*b
;
2070 for (objfile
*objfile
: current_program_space
->objfiles ())
2071 for (partial_symtab
*ps
: require_partial_symbols (objfile
, true))
2073 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2075 /* We don't call psymtab_to_symtab here because that may cause symtab
2076 expansion. When debugging a problem it helps if checkers leave
2077 things unchanged. */
2078 cust
= ps
->compunit_symtab
;
2080 /* First do some checks that don't require the associated symtab. */
2081 if (ps
->text_high (objfile
) < ps
->text_low (objfile
))
2083 printf_filtered ("Psymtab ");
2084 puts_filtered (ps
->filename
);
2085 printf_filtered (" covers bad range ");
2086 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2088 printf_filtered (" - ");
2089 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2091 printf_filtered ("\n");
2095 /* Now do checks requiring the associated symtab. */
2098 bv
= COMPUNIT_BLOCKVECTOR (cust
);
2099 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
2100 partial_symbol
**psym
2101 = &objfile
->partial_symtabs
->static_psymbols
[ps
->statics_offset
];
2102 length
= ps
->n_static_syms
;
2105 sym
= block_lookup_symbol (b
, (*psym
)->ginfo
.search_name (),
2106 symbol_name_match_type::SEARCH_NAME
,
2110 printf_filtered ("Static symbol `");
2111 puts_filtered ((*psym
)->ginfo
.linkage_name ());
2112 printf_filtered ("' only found in ");
2113 puts_filtered (ps
->filename
);
2114 printf_filtered (" psymtab\n");
2118 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
2119 psym
= &objfile
->partial_symtabs
->global_psymbols
[ps
->globals_offset
];
2120 length
= ps
->n_global_syms
;
2123 sym
= block_lookup_symbol (b
, (*psym
)->ginfo
.search_name (),
2124 symbol_name_match_type::SEARCH_NAME
,
2128 printf_filtered ("Global symbol `");
2129 puts_filtered ((*psym
)->ginfo
.linkage_name ());
2130 printf_filtered ("' only found in ");
2131 puts_filtered (ps
->filename
);
2132 printf_filtered (" psymtab\n");
2136 if (ps
->raw_text_high () != 0
2137 && (ps
->text_low (objfile
) < BLOCK_START (b
)
2138 || ps
->text_high (objfile
) > BLOCK_END (b
)))
2140 printf_filtered ("Psymtab ");
2141 puts_filtered (ps
->filename
);
2142 printf_filtered (" covers ");
2143 fputs_filtered (paddress (gdbarch
, ps
->text_low (objfile
)),
2145 printf_filtered (" - ");
2146 fputs_filtered (paddress (gdbarch
, ps
->text_high (objfile
)),
2148 printf_filtered (" but symtab covers only ");
2149 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), gdb_stdout
);
2150 printf_filtered (" - ");
2151 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), gdb_stdout
);
2152 printf_filtered ("\n");
2158 _initialize_psymtab (void)
2160 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
2161 Print dump of current partial symbol definitions.\n\
2162 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2163 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2164 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2165 or the terminal if OUTFILE is unspecified.\n\
2166 If ADDRESS is provided, dump only the file for that address.\n\
2167 If SOURCE is provided, dump only that file's symbols.\n\
2168 If OBJFILE is provided, dump only that file's minimal symbols."),
2169 &maintenanceprintlist
);
2171 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
2172 List the partial symbol tables for all object files.\n\
2173 This does not include information about individual partial symbols,\n\
2174 just the symbol table structures themselves."),
2175 &maintenanceinfolist
);
2177 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
2179 Check consistency of currently expanded psymtabs versus symtabs."),