arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / psymtab.c
blobf8c83d9b17f082f4a22ac3467765b149a8b3e864
1 /* Partial symbol tables.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "event-top.h"
21 #include "symtab.h"
22 #include "objfiles.h"
23 #include "psymtab.h"
24 #include "block.h"
25 #include "filenames.h"
26 #include "source.h"
27 #include "gdbtypes.h"
28 #include "ui-out.h"
29 #include "command.h"
30 #include "readline/tilde.h"
31 #include "gdbsupport/gdb_regex.h"
32 #include "dictionary.h"
33 #include "language.h"
34 #include "cp-support.h"
35 #include "cli/cli-cmds.h"
36 #include <algorithm>
37 #include <set>
38 #include "gdbsupport/buildargv.h"
40 static const struct partial_symbol *lookup_partial_symbol
41 (struct objfile *, struct partial_symtab *, const lookup_name_info &,
42 int, domain_search_flags);
44 static const char *psymtab_to_fullname (struct partial_symtab *ps);
46 static const struct partial_symbol *find_pc_sect_psymbol
47 (struct objfile *, struct partial_symtab *, CORE_ADDR,
48 struct obj_section *);
50 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
51 struct partial_symtab *pst);
53 psymtab_storage::~psymtab_storage ()
55 partial_symtab *iter = psymtabs;
56 while (iter != nullptr)
58 partial_symtab *next = iter->next;
59 delete iter;
60 iter = next;
64 /* See psymtab.h. */
66 void
67 psymtab_storage::install_psymtab (partial_symtab *pst)
69 pst->next = psymtabs;
70 psymtabs = pst;
75 /* See psymtab.h. */
77 psymtab_storage::partial_symtab_range
78 psymbol_functions::partial_symbols (struct objfile *objfile)
80 return m_partial_symtabs->range ();
83 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
84 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
86 static struct partial_symtab *
87 find_pc_sect_psymtab_closer (struct objfile *objfile,
88 CORE_ADDR pc, struct obj_section *section,
89 struct partial_symtab *pst,
90 bound_minimal_symbol msymbol)
92 struct partial_symtab *tpst;
93 struct partial_symtab *best_pst = pst;
94 CORE_ADDR best_addr = pst->text_low (objfile);
96 /* An objfile that has its functions reordered might have
97 many partial symbol tables containing the PC, but
98 we want the partial symbol table that contains the
99 function containing the PC. */
100 if (section == nullptr)
101 return pst;
103 if (msymbol.minsym == NULL)
104 return pst;
106 /* The code range of partial symtabs sometimes overlap, so, in
107 the loop below, we need to check all partial symtabs and
108 find the one that fits better for the given PC address. We
109 select the partial symtab that contains a symbol whose
110 address is closest to the PC address. By closest we mean
111 that find_pc_sect_symbol returns the symbol with address
112 that is closest and still less than the given PC. */
113 for (tpst = pst; tpst != NULL; tpst = tpst->next)
115 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
117 const struct partial_symbol *p;
118 CORE_ADDR this_addr;
120 /* NOTE: This assumes that every psymbol has a
121 corresponding msymbol, which is not necessarily
122 true; the debug info might be much richer than the
123 object's symbol table. */
124 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
125 if (p != NULL
126 && (p->address (objfile) == msymbol.value_address ()))
127 return tpst;
129 /* Also accept the textlow value of a psymtab as a
130 "symbol", to provide some support for partial
131 symbol tables with line information but no debug
132 symbols (e.g. those produced by an assembler). */
133 if (p != NULL)
134 this_addr = p->address (objfile);
135 else
136 this_addr = tpst->text_low (objfile);
138 /* Check whether it is closer than our current
139 BEST_ADDR. Since this symbol address is
140 necessarily lower or equal to PC, the symbol closer
141 to PC is the symbol which address is the highest.
142 This way we return the psymtab which contains such
143 best match symbol. This can help in cases where the
144 symbol information/debuginfo is not complete, like
145 for instance on IRIX6 with gcc, where no debug info
146 is emitted for statics. (See also the nodebug.exp
147 testcase.) */
148 if (this_addr > best_addr)
150 best_addr = this_addr;
151 best_pst = tpst;
155 return best_pst;
158 /* See psymtab.h. */
160 struct partial_symtab *
161 psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
162 CORE_ADDR pc,
163 struct obj_section *section,
164 bound_minimal_symbol msymbol)
166 for (partial_symtab *pst : partial_symbols (objfile))
167 if (pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
169 struct partial_symtab *best_pst;
171 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
172 msymbol);
173 if (best_pst != NULL)
174 return best_pst;
177 return NULL;
180 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
181 the definition of quick_symbol_functions in symfile.h. */
183 struct compunit_symtab *
184 psymbol_functions::find_pc_sect_compunit_symtab (struct objfile *objfile,
185 bound_minimal_symbol msymbol,
186 CORE_ADDR pc,
187 struct obj_section *section,
188 int warn_if_readin)
190 struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
191 pc, section,
192 msymbol);
193 if (ps != NULL)
195 if (warn_if_readin && ps->readin_p (objfile))
196 /* Might want to error() here (in case symtab is corrupt and
197 will cause a core dump), but maybe we can successfully
198 continue, so let's not. */
199 warning (_("\
200 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
201 paddress (objfile->arch (), pc));
202 psymtab_to_symtab (objfile, ps);
203 return ps->get_compunit_symtab (objfile);
205 return NULL;
208 /* Find which partial symbol within a psymtab matches PC and SECTION.
209 Return NULL if none. */
211 static const struct partial_symbol *
212 find_pc_sect_psymbol (struct objfile *objfile,
213 struct partial_symtab *psymtab, CORE_ADDR pc,
214 struct obj_section *section)
216 const struct partial_symbol *best = NULL;
217 CORE_ADDR best_pc;
218 const CORE_ADDR textlow = psymtab->text_low (objfile);
220 gdb_assert (psymtab != NULL);
222 /* Cope with programs that start at address 0. */
223 best_pc = (textlow != 0) ? textlow - 1 : 0;
225 /* Search the global symbols as well as the static symbols, so that
226 find_pc_partial_function doesn't use a minimal symbol and thus
227 cache a bad endaddr. */
228 for (const partial_symbol *p : psymtab->global_psymbols)
230 if (p->domain == VAR_DOMAIN
231 && p->aclass == LOC_BLOCK
232 && pc >= p->address (objfile)
233 && (p->address (objfile) > best_pc
234 || (psymtab->text_low (objfile) == 0
235 && best_pc == 0 && p->address (objfile) == 0)))
237 if (section != NULL) /* Match on a specific section. */
239 if (!matching_obj_sections (p->obj_section (objfile),
240 section))
241 continue;
243 best_pc = p->address (objfile);
244 best = p;
248 for (const partial_symbol *p : psymtab->static_psymbols)
250 if (p->domain == VAR_DOMAIN
251 && p->aclass == LOC_BLOCK
252 && pc >= p->address (objfile)
253 && (p->address (objfile) > best_pc
254 || (psymtab->text_low (objfile) == 0
255 && best_pc == 0 && p->address (objfile) == 0)))
257 if (section != NULL) /* Match on a specific section. */
259 if (!matching_obj_sections (p->obj_section (objfile),
260 section))
261 continue;
263 best_pc = p->address (objfile);
264 best = p;
268 return best;
271 /* Psymtab version of lookup_global_symbol_language. See its definition in
272 the definition of quick_symbol_functions in symfile.h. */
274 enum language
275 psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
276 const char *name,
277 domain_search_flags domain,
278 bool *symbol_found_p)
280 *symbol_found_p = false;
281 if (objfile->sf == NULL)
282 return language_unknown;
284 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
286 for (partial_symtab *ps : partial_symbols (objfile))
288 const struct partial_symbol *psym;
289 if (ps->readin_p (objfile))
290 continue;
292 psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
293 if (psym)
295 *symbol_found_p = true;
296 return psym->ginfo.language ();
300 return language_unknown;
303 /* Returns true if PSYM matches LOOKUP_NAME. */
305 static bool
306 psymbol_name_matches (const partial_symbol *psym,
307 const lookup_name_info &lookup_name)
309 const language_defn *lang = language_def (psym->ginfo.language ());
310 symbol_name_matcher_ftype *name_match
311 = lang->get_symbol_name_matcher (lookup_name);
312 return name_match (psym->ginfo.search_name (), lookup_name, NULL);
315 /* Look, in partial_symtab PST, for symbol whose natural name is
316 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
317 symbols if not. */
319 static const struct partial_symbol *
320 lookup_partial_symbol (struct objfile *objfile,
321 struct partial_symtab *pst,
322 const lookup_name_info &lookup_name,
323 int global, domain_search_flags domain)
325 const struct partial_symbol **start, **psym;
326 const struct partial_symbol **top, **real_top, **bottom, **center;
327 int length = (global
328 ? pst->global_psymbols.size ()
329 : pst->static_psymbols.size ());
330 int do_linear_search = 1;
332 if (length == 0)
333 return NULL;
335 start = (global ?
336 &pst->global_psymbols[0] :
337 &pst->static_psymbols[0]);
339 if (global) /* This means we can use a binary search. */
341 do_linear_search = 0;
343 /* Binary search. This search is guaranteed to end with center
344 pointing at the earliest partial symbol whose name might be
345 correct. At that point *all* partial symbols with an
346 appropriate name will be checked against the correct
347 domain. */
349 bottom = start;
350 top = start + length - 1;
351 real_top = top;
352 while (top > bottom)
354 center = bottom + (top - bottom) / 2;
356 gdb_assert (center < top);
358 if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
359 lookup_name.c_str ()) >= 0)
361 top = center;
363 else
365 bottom = center + 1;
369 gdb_assert (top == bottom);
371 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
372 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
373 while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
374 lookup_name))
375 top--;
377 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
378 top++;
380 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
381 lookup_name))
383 if (search_flags_matches (domain, (*top)->domain))
384 return *top;
385 top++;
389 /* Can't use a binary search or else we found during the binary search that
390 we should also do a linear search. */
392 if (do_linear_search)
394 for (psym = start; psym < start + length; psym++)
396 if (search_flags_matches (domain, (*psym)->domain)
397 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
398 return *psym;
402 return NULL;
405 /* Get the symbol table that corresponds to a partial_symtab.
406 This is fast after the first time you do it.
407 The result will be NULL if the primary symtab has no symbols,
408 which can happen. Otherwise the result is the primary symtab
409 that contains PST. */
411 static struct compunit_symtab *
412 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
414 /* If it is a shared psymtab, find an unshared psymtab that includes
415 it. Any such psymtab will do. */
416 while (pst->user != NULL)
417 pst = pst->user;
419 /* If it's been looked up before, return it. */
420 if (pst->get_compunit_symtab (objfile))
421 return pst->get_compunit_symtab (objfile);
423 /* If it has not yet been read in, read it. */
424 if (!pst->readin_p (objfile))
426 scoped_restore decrementer = increment_reading_symtab ();
428 if (info_verbose)
430 gdb_printf (_("Reading in symbols for %s...\n"),
431 pst->filename);
432 gdb_flush (gdb_stdout);
435 pst->read_symtab (objfile);
438 return pst->get_compunit_symtab (objfile);
441 /* Psymtab version of find_last_source_symtab. See its definition in
442 the definition of quick_symbol_functions in symfile.h. */
444 struct symtab *
445 psymbol_functions::find_last_source_symtab (struct objfile *ofp)
447 struct partial_symtab *cs_pst = NULL;
449 for (partial_symtab *ps : partial_symbols (ofp))
451 const char *name = ps->filename;
452 int len = strlen (name);
454 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
455 || strcmp (name, "<<C++-namespaces>>") == 0)))
456 cs_pst = ps;
459 if (cs_pst)
461 if (cs_pst->readin_p (ofp))
463 internal_error (_("select_source_symtab: "
464 "readin pst found and no symtabs."));
466 else
468 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
470 if (cust == NULL)
471 return NULL;
472 return cust->primary_filetab ();
475 return NULL;
478 /* Psymtab version of forget_cached_source_info. See its definition in
479 the definition of quick_symbol_functions in symfile.h. */
481 void
482 psymbol_functions::forget_cached_source_info (struct objfile *objfile)
484 for (partial_symtab *pst : partial_symbols (objfile))
486 if (pst->fullname != NULL)
488 xfree (pst->fullname);
489 pst->fullname = NULL;
494 static void
495 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
496 const std::vector<const partial_symbol *> &symbols,
497 const char *what, struct ui_file *outfile)
499 gdb_printf (outfile, " %s partial symbols:\n", what);
500 for (const partial_symbol *p : symbols)
502 QUIT;
503 gdb_printf (outfile, " `%s'", p->ginfo.linkage_name ());
504 if (p->ginfo.demangled_name () != NULL)
506 gdb_printf (outfile, " `%s'",
507 p->ginfo.demangled_name ());
509 gdb_puts (", ", outfile);
510 switch (p->domain)
512 case UNDEF_DOMAIN:
513 gdb_puts ("undefined domain, ", outfile);
514 break;
515 case VAR_DOMAIN:
516 /* This is the usual thing -- don't print it. */
517 break;
518 case STRUCT_DOMAIN:
519 gdb_puts ("struct domain, ", outfile);
520 break;
521 case MODULE_DOMAIN:
522 gdb_puts ("module domain, ", outfile);
523 break;
524 case LABEL_DOMAIN:
525 gdb_puts ("label domain, ", outfile);
526 break;
527 case COMMON_BLOCK_DOMAIN:
528 gdb_puts ("common block domain, ", outfile);
529 break;
530 default:
531 gdb_puts ("<invalid domain>, ", outfile);
532 break;
534 switch (p->aclass)
536 case LOC_UNDEF:
537 gdb_puts ("undefined", outfile);
538 break;
539 case LOC_CONST:
540 gdb_puts ("constant int", outfile);
541 break;
542 case LOC_STATIC:
543 gdb_puts ("static", outfile);
544 break;
545 case LOC_REGISTER:
546 gdb_puts ("register", outfile);
547 break;
548 case LOC_ARG:
549 gdb_puts ("pass by value", outfile);
550 break;
551 case LOC_REF_ARG:
552 gdb_puts ("pass by reference", outfile);
553 break;
554 case LOC_REGPARM_ADDR:
555 gdb_puts ("register address parameter", outfile);
556 break;
557 case LOC_LOCAL:
558 gdb_puts ("stack parameter", outfile);
559 break;
560 case LOC_TYPEDEF:
561 gdb_puts ("type", outfile);
562 break;
563 case LOC_LABEL:
564 gdb_puts ("label", outfile);
565 break;
566 case LOC_BLOCK:
567 gdb_puts ("function", outfile);
568 break;
569 case LOC_CONST_BYTES:
570 gdb_puts ("constant bytes", outfile);
571 break;
572 case LOC_UNRESOLVED:
573 gdb_puts ("unresolved", outfile);
574 break;
575 case LOC_OPTIMIZED_OUT:
576 gdb_puts ("optimized out", outfile);
577 break;
578 case LOC_COMPUTED:
579 gdb_puts ("computed at runtime", outfile);
580 break;
581 default:
582 gdb_puts ("<invalid location>", outfile);
583 break;
585 gdb_puts (", ", outfile);
586 gdb_puts (paddress (gdbarch, CORE_ADDR (p->unrelocated_address ())),
587 outfile);
588 gdb_printf (outfile, "\n");
592 static void
593 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
594 struct ui_file *outfile)
596 struct gdbarch *gdbarch = objfile->arch ();
597 int i;
599 if (psymtab->anonymous)
601 gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
602 psymtab->filename);
604 else
606 gdb_printf (outfile, "\nPartial symtab for source file %s ",
607 psymtab->filename);
609 gdb_printf (outfile, "(object %s)\n\n",
610 host_address_to_string (psymtab));
611 gdb_printf (outfile, " Read from object file %s (%s)\n",
612 objfile_name (objfile),
613 host_address_to_string (objfile));
615 if (psymtab->readin_p (objfile))
616 gdb_printf
617 (outfile,
618 " Full symtab was read (at %s)\n",
619 host_address_to_string (psymtab->get_compunit_symtab (objfile)));
621 gdb_printf (outfile, " Symbols cover text addresses ");
622 gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
623 gdb_printf (outfile, "-");
624 gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
625 gdb_printf (outfile, "\n");
626 gdb_printf (outfile, " Depends on %d other partial symtabs.\n",
627 psymtab->number_of_dependencies);
628 for (i = 0; i < psymtab->number_of_dependencies; i++)
629 gdb_printf (outfile, " %d %s\n", i,
630 host_address_to_string (psymtab->dependencies[i]));
631 if (psymtab->user != NULL)
632 gdb_printf (outfile, " Shared partial symtab with user %s\n",
633 host_address_to_string (psymtab->user));
634 if (!psymtab->global_psymbols.empty ())
636 print_partial_symbols
637 (gdbarch, objfile, psymtab->global_psymbols,
638 "Global", outfile);
640 if (!psymtab->static_psymbols.empty ())
642 print_partial_symbols
643 (gdbarch, objfile, psymtab->static_psymbols,
644 "Static", outfile);
646 gdb_printf (outfile, "\n");
649 /* Count the number of partial symbols in OBJFILE. */
652 psymbol_functions::count_psyms ()
654 int count = 0;
655 for (partial_symtab *pst : m_partial_symtabs->range ())
657 count += pst->global_psymbols.size ();
658 count += pst->static_psymbols.size ();
660 return count;
663 /* Psymtab version of print_stats. See its definition in
664 the definition of quick_symbol_functions in symfile.h. */
666 void
667 psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
669 int i;
671 if (!print_bcache)
673 int n_psyms = count_psyms ();
674 if (n_psyms > 0)
675 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
676 n_psyms);
678 i = 0;
679 for (partial_symtab *ps : partial_symbols (objfile))
681 if (!ps->readin_p (objfile))
682 i++;
684 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
686 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
687 m_partial_symtabs->psymbol_cache.memory_used ());
689 else
691 gdb_printf (_("Psymbol byte cache statistics:\n"));
692 m_partial_symtabs->psymbol_cache.print_statistics
693 ("partial symbol cache");
697 /* Psymtab version of dump. See its definition in
698 the definition of quick_symbol_functions in symfile.h. */
700 void
701 psymbol_functions::dump (struct objfile *objfile)
703 struct partial_symtab *psymtab;
705 if (m_partial_symtabs->psymtabs)
707 gdb_printf ("Psymtabs:\n");
708 for (psymtab = m_partial_symtabs->psymtabs;
709 psymtab != NULL;
710 psymtab = psymtab->next)
711 gdb_printf ("%s at %s\n",
712 psymtab->filename,
713 host_address_to_string (psymtab));
714 gdb_printf ("\n\n");
718 /* Psymtab version of expand_all_symtabs. See its definition in
719 the definition of quick_symbol_functions in symfile.h. */
721 void
722 psymbol_functions::expand_all_symtabs (struct objfile *objfile)
724 for (partial_symtab *psymtab : partial_symbols (objfile))
725 psymtab_to_symtab (objfile, psymtab);
728 /* Psymtab version of map_symbol_filenames. See its definition in
729 the definition of quick_symbol_functions in symfile.h. */
731 void
732 psymbol_functions::map_symbol_filenames
733 (struct objfile *objfile,
734 gdb::function_view<symbol_filename_ftype> fun,
735 bool need_fullname)
737 for (partial_symtab *ps : partial_symbols (objfile))
739 const char *fullname;
741 if (ps->readin_p (objfile))
742 continue;
744 /* We can skip shared psymtabs here, because any file name will be
745 attached to the unshared psymtab. */
746 if (ps->user != NULL)
747 continue;
749 /* Anonymous psymtabs don't have a file name. */
750 if (ps->anonymous)
751 continue;
753 QUIT;
754 if (need_fullname)
755 fullname = psymtab_to_fullname (ps);
756 else
757 fullname = NULL;
758 fun (ps->filename, fullname);
762 /* Finds the fullname that a partial_symtab represents.
764 If this functions finds the fullname, it will save it in ps->fullname
765 and it will also return the value.
767 If this function fails to find the file that this partial_symtab represents,
768 NULL will be returned and ps->fullname will be set to NULL. */
770 static const char *
771 psymtab_to_fullname (struct partial_symtab *ps)
773 gdb_assert (!ps->anonymous);
775 /* Use cached copy if we have it.
776 We rely on forget_cached_source_info being called appropriately
777 to handle cases like the file being moved. */
778 if (ps->fullname == NULL)
780 gdb::unique_xmalloc_ptr<char> fullname
781 = find_source_or_rewrite (ps->filename, ps->dirname);
782 ps->fullname = fullname.release ();
785 return ps->fullname;
788 /* A helper for psym_expand_symtabs_matching that handles searching
789 included psymtabs. This returns true if a symbol is found, and
790 false otherwise. It also updates the 'searched_flag' on the
791 various psymtabs that it searches. */
793 static bool
794 recursively_search_psymtabs
795 (struct partial_symtab *ps,
796 struct objfile *objfile,
797 block_search_flags search_flags,
798 domain_search_flags domain,
799 const lookup_name_info &lookup_name,
800 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
802 int keep_going = 1;
803 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
804 int i;
806 if (ps->searched_flag != PST_NOT_SEARCHED)
807 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
809 /* Recurse into shared psymtabs first, because they may have already
810 been searched, and this could save some time. */
811 for (i = 0; i < ps->number_of_dependencies; ++i)
813 int r;
815 /* Skip non-shared dependencies, these are handled elsewhere. */
816 if (ps->dependencies[i]->user == NULL)
817 continue;
819 r = recursively_search_psymtabs (ps->dependencies[i],
820 objfile, search_flags, domain,
821 lookup_name, sym_matcher);
822 if (r != 0)
824 ps->searched_flag = PST_SEARCHED_AND_FOUND;
825 return true;
829 const partial_symbol **gbound = (ps->global_psymbols.data ()
830 + ps->global_psymbols.size ());
831 const partial_symbol **sbound = (ps->static_psymbols.data ()
832 + ps->static_psymbols.size ());
833 const partial_symbol **bound = gbound;
835 /* Go through all of the symbols stored in a partial
836 symtab in one loop. */
837 const partial_symbol **psym = ps->global_psymbols.data ();
839 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
841 if (ps->static_psymbols.empty ())
842 keep_going = 0;
843 else
845 psym = ps->static_psymbols.data ();
846 bound = sbound;
850 while (keep_going)
852 if (psym >= bound)
854 if (bound == gbound && !ps->static_psymbols.empty ()
855 && (search_flags & SEARCH_STATIC_BLOCK) != 0)
857 psym = ps->static_psymbols.data ();
858 bound = sbound;
860 else
861 keep_going = 0;
862 continue;
864 else
866 QUIT;
868 if (search_flags_matches (domain, (*psym)->domain)
869 && psymbol_name_matches (*psym, lookup_name)
870 && (sym_matcher == NULL
871 || sym_matcher ((*psym)->ginfo.search_name ())))
873 /* Found a match, so notify our caller. */
874 result = PST_SEARCHED_AND_FOUND;
875 keep_going = 0;
878 psym++;
881 ps->searched_flag = result;
882 return result == PST_SEARCHED_AND_FOUND;
885 /* Psymtab version of expand_symtabs_matching. See its definition in
886 the definition of quick_symbol_functions in symfile.h. */
888 bool
889 psymbol_functions::expand_symtabs_matching
890 (struct objfile *objfile,
891 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
892 const lookup_name_info *lookup_name,
893 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
894 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
895 block_search_flags search_flags,
896 domain_search_flags domain,
897 gdb::function_view<expand_symtabs_lang_matcher_ftype>
898 lang_matcher ATTRIBUTE_UNUSED)
900 /* Clear the search flags. */
901 for (partial_symtab *ps : partial_symbols (objfile))
902 ps->searched_flag = PST_NOT_SEARCHED;
904 std::optional<lookup_name_info> psym_lookup_name;
905 if (lookup_name != nullptr)
906 psym_lookup_name = lookup_name->make_ignore_params ();
908 /* This invariant is documented in quick-functions.h. */
909 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
911 for (partial_symtab *ps : m_partial_symtabs->range ())
913 QUIT;
915 if (ps->readin_p (objfile))
916 continue;
918 if (file_matcher)
920 bool match;
922 if (ps->anonymous)
923 continue;
925 match = file_matcher (ps->filename, false);
926 if (!match)
928 /* Before we invoke realpath, which can get expensive when many
929 files are involved, do a quick comparison of the basenames. */
930 if (basenames_may_differ
931 || file_matcher (lbasename (ps->filename), true))
932 match = file_matcher (psymtab_to_fullname (ps), false);
934 if (!match)
935 continue;
938 if (lookup_name == nullptr
939 || recursively_search_psymtabs (ps, objfile, search_flags,
940 domain, *psym_lookup_name,
941 symbol_matcher))
943 compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
945 if (cust != nullptr && expansion_notify != nullptr)
946 if (!expansion_notify (cust))
947 return false;
951 return true;
954 /* Psymtab version of has_symbols. See its definition in
955 the definition of quick_symbol_functions in symfile.h. */
957 bool
958 psymbol_functions::has_symbols (struct objfile *objfile)
960 return m_partial_symtabs->psymtabs != NULL;
963 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
965 bool
966 psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
968 for (partial_symtab *psymtab : partial_symbols (objfile))
970 /* Is this already expanded? */
971 if (psymtab->readin_p (objfile))
972 continue;
974 /* It has not yet been expanded. */
975 return true;
978 return false;
983 /* Partially fill a partial symtab. It will be completely filled at
984 the end of the symbol list. */
986 partial_symtab::partial_symtab (const char *filename,
987 psymtab_storage *partial_symtabs,
988 objfile_per_bfd_storage *objfile_per_bfd,
989 unrelocated_addr textlow)
990 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
992 set_text_low (textlow);
993 set_text_high (unrelocated_text_low ()); /* default */
996 /* Perform "finishing up" operations of a partial symtab. */
998 void
999 partial_symtab::end ()
1001 global_psymbols.shrink_to_fit ();
1002 static_psymbols.shrink_to_fit ();
1004 /* Sort the global list; don't sort the static list. */
1005 std::sort (global_psymbols.begin (),
1006 global_psymbols.end (),
1007 [] (const partial_symbol *s1, const partial_symbol *s2)
1009 return strcmp_iw_ordered (s1->ginfo.search_name (),
1010 s2->ginfo.search_name ()) < 0;
1014 /* See psymtab.h. */
1016 unsigned long
1017 psymbol_bcache::hash (const void *addr, int length)
1019 unsigned long h = 0;
1020 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1021 unsigned int lang = psymbol->ginfo.language ();
1022 unsigned int domain = psymbol->domain;
1023 unsigned int theclass = psymbol->aclass;
1025 h = fast_hash (&psymbol->ginfo.m_value, sizeof (psymbol->ginfo.m_value), h);
1026 h = fast_hash (&lang, sizeof (unsigned int), h);
1027 h = fast_hash (&domain, sizeof (unsigned int), h);
1028 h = fast_hash (&theclass, sizeof (unsigned int), h);
1029 /* Note that psymbol names are interned via compute_and_set_names, so
1030 there's no need to hash the contents of the name here. */
1031 h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1033 return h;
1036 /* See psymtab.h. */
1039 psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
1041 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1042 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1044 return (memcmp (&sym1->ginfo.m_value, &sym2->ginfo.m_value,
1045 sizeof (sym1->ginfo.m_value)) == 0
1046 && sym1->ginfo.language () == sym2->ginfo.language ()
1047 && sym1->domain == sym2->domain
1048 && sym1->aclass == sym2->aclass
1049 /* Note that psymbol names are interned via
1050 compute_and_set_names, so there's no need to compare the
1051 contents of the name here. */
1052 && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1055 /* See psymtab.h. */
1057 void
1058 partial_symtab::add_psymbol (const partial_symbol &psymbol,
1059 psymbol_placement where,
1060 psymtab_storage *partial_symtabs,
1061 struct objfile *objfile)
1063 bool added;
1065 /* Stash the partial symbol away in the cache. */
1066 const partial_symbol *psym = partial_symtabs->psymbol_cache.insert (psymbol,
1067 &added);
1069 /* Do not duplicate global partial symbols. */
1070 if (where == psymbol_placement::GLOBAL && !added)
1071 return;
1073 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1074 std::vector<const partial_symbol *> &list
1075 = (where == psymbol_placement::STATIC
1076 ? static_psymbols
1077 : global_psymbols);
1078 list.push_back (psym);
1081 /* See psymtab.h. */
1083 void
1084 partial_symtab::add_psymbol (std::string_view name, bool copy_name,
1085 domain_enum domain,
1086 enum address_class theclass,
1087 int section,
1088 psymbol_placement where,
1089 unrelocated_addr coreaddr,
1090 enum language language,
1091 psymtab_storage *partial_symtabs,
1092 struct objfile *objfile)
1094 struct partial_symbol psymbol;
1095 memset (&psymbol, 0, sizeof (psymbol));
1097 psymbol.set_unrelocated_address (coreaddr);
1098 psymbol.ginfo.set_section_index (section);
1099 psymbol.domain = domain;
1100 psymbol.aclass = theclass;
1101 psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
1102 psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1104 add_psymbol (psymbol, where, partial_symtabs, objfile);
1107 /* See psymtab.h. */
1109 partial_symtab::partial_symtab (const char *filename_,
1110 psymtab_storage *partial_symtabs,
1111 objfile_per_bfd_storage *objfile_per_bfd)
1112 : searched_flag (PST_NOT_SEARCHED),
1113 text_low_valid (0),
1114 text_high_valid (0)
1116 partial_symtabs->install_psymtab (this);
1118 filename = objfile_per_bfd->intern (filename_);
1120 if (symtab_create_debug >= 1)
1122 /* Be a bit clever with debugging messages, and don't print objfile
1123 every time, only when it changes. */
1124 static std::string last_bfd_name;
1125 const char *this_bfd_name
1126 = bfd_get_filename (objfile_per_bfd->get_bfd ());
1128 if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
1130 last_bfd_name = this_bfd_name;
1132 symtab_create_debug_printf ("creating one or more psymtabs for %s",
1133 this_bfd_name);
1136 symtab_create_debug_printf ("created psymtab %s for module %s",
1137 host_address_to_string (this), filename);
1141 /* See psymtab.h. */
1143 void
1144 partial_symtab::expand_dependencies (struct objfile *objfile)
1146 for (int i = 0; i < number_of_dependencies; ++i)
1148 if (!dependencies[i]->readin_p (objfile)
1149 && dependencies[i]->user == NULL)
1151 /* Inform about additional files to be read in. */
1152 if (info_verbose)
1154 gdb_puts (" ");
1155 gdb_stdout->wrap_here (0);
1156 gdb_puts ("and ");
1157 gdb_stdout->wrap_here (0);
1158 gdb_printf ("%s...", dependencies[i]->filename);
1159 gdb_flush (gdb_stdout);
1161 dependencies[i]->expand_psymtab (objfile);
1167 void
1168 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1170 struct partial_symtab **prev_pst;
1172 /* From dbxread.c:
1173 Empty psymtabs happen as a result of header files which don't
1174 have any symbols in them. There can be a lot of them. But this
1175 check is wrong, in that a psymtab with N_SLINE entries but
1176 nothing else is not empty, but we don't realize that. Fixing
1177 that without slowing things down might be tricky. */
1179 /* First, snip it out of the psymtab chain. */
1181 prev_pst = &psymtabs;
1182 while ((*prev_pst) != pst)
1183 prev_pst = &((*prev_pst)->next);
1184 (*prev_pst) = pst->next;
1185 delete pst;
1190 static void
1191 maintenance_print_psymbols (const char *args, int from_tty)
1193 struct ui_file *outfile = gdb_stdout;
1194 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1195 int i, outfile_idx, found;
1196 CORE_ADDR pc = 0;
1197 struct obj_section *section = NULL;
1199 dont_repeat ();
1201 gdb_argv argv (args);
1203 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1205 if (strcmp (argv[i], "-pc") == 0)
1207 if (argv[i + 1] == NULL)
1208 error (_("Missing pc value"));
1209 address_arg = argv[++i];
1211 else if (strcmp (argv[i], "-source") == 0)
1213 if (argv[i + 1] == NULL)
1214 error (_("Missing source file"));
1215 source_arg = argv[++i];
1217 else if (strcmp (argv[i], "-objfile") == 0)
1219 if (argv[i + 1] == NULL)
1220 error (_("Missing objfile name"));
1221 objfile_arg = argv[++i];
1223 else if (strcmp (argv[i], "--") == 0)
1225 /* End of options. */
1226 ++i;
1227 break;
1229 else if (argv[i][0] == '-')
1231 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1232 error (_("Unknown option: %s"), argv[i]);
1234 else
1235 break;
1237 outfile_idx = i;
1239 if (address_arg != NULL && source_arg != NULL)
1240 error (_("Must specify at most one of -pc and -source"));
1242 stdio_file arg_outfile;
1244 if (argv != NULL && argv[outfile_idx] != NULL)
1246 if (argv[outfile_idx + 1] != NULL)
1247 error (_("Junk at end of command"));
1248 gdb::unique_xmalloc_ptr<char> outfile_name
1249 (tilde_expand (argv[outfile_idx]));
1250 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1251 perror_with_name (outfile_name.get ());
1252 outfile = &arg_outfile;
1255 if (address_arg != NULL)
1257 pc = parse_and_eval_address (address_arg);
1258 /* If we fail to find a section, that's ok, try the lookup anyway. */
1259 section = find_pc_section (pc);
1262 found = 0;
1263 for (objfile *objfile : current_program_space->objfiles ())
1265 int printed_objfile_header = 0;
1266 int print_for_objfile = 1;
1268 QUIT;
1269 if (objfile_arg != NULL)
1270 print_for_objfile
1271 = compare_filenames_for_search (objfile_name (objfile),
1272 objfile_arg);
1273 if (!print_for_objfile)
1274 continue;
1276 for (const auto &iter : objfile->qf)
1278 psymbol_functions *psf
1279 = dynamic_cast<psymbol_functions *> (iter.get ());
1280 if (psf == nullptr)
1281 continue;
1283 if (address_arg != NULL)
1285 bound_minimal_symbol msymbol;
1287 /* We don't assume each pc has a unique objfile (this is for
1288 debugging). */
1289 struct partial_symtab *ps
1290 = psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
1291 if (ps != NULL)
1293 if (!printed_objfile_header)
1295 outfile->printf ("\nPartial symtabs for objfile %s\n",
1296 objfile_name (objfile));
1297 printed_objfile_header = 1;
1299 dump_psymtab (objfile, ps, outfile);
1300 found = 1;
1303 else
1305 for (partial_symtab *ps : psf->partial_symbols (objfile))
1307 int print_for_source = 0;
1309 QUIT;
1310 if (source_arg != NULL)
1312 print_for_source
1313 = compare_filenames_for_search (ps->filename, source_arg);
1314 found = 1;
1316 if (source_arg == NULL
1317 || print_for_source)
1319 if (!printed_objfile_header)
1321 outfile->printf ("\nPartial symtabs for objfile %s\n",
1322 objfile_name (objfile));
1323 printed_objfile_header = 1;
1325 dump_psymtab (objfile, ps, outfile);
1332 if (!found)
1334 if (address_arg != NULL)
1335 error (_("No partial symtab for address: %s"), address_arg);
1336 if (source_arg != NULL)
1337 error (_("No partial symtab for source file: %s"), source_arg);
1341 /* List all the partial symbol tables whose names match REGEXP (optional). */
1343 static void
1344 maintenance_info_psymtabs (const char *regexp, int from_tty)
1346 if (regexp)
1347 re_comp (regexp);
1349 for (struct program_space *pspace : program_spaces)
1350 for (objfile *objfile : pspace->objfiles ())
1352 struct gdbarch *gdbarch = objfile->arch ();
1354 /* We don't want to print anything for this objfile until we
1355 actually find a symtab whose name matches. */
1356 int printed_objfile_start = 0;
1358 for (const auto &iter : objfile->qf)
1360 psymbol_functions *psf
1361 = dynamic_cast<psymbol_functions *> (iter.get ());
1362 if (psf == nullptr)
1363 continue;
1364 for (partial_symtab *psymtab : psf->partial_symbols (objfile))
1366 QUIT;
1368 if (! regexp
1369 || re_exec (psymtab->filename))
1371 if (! printed_objfile_start)
1373 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1374 gdb_stdout->wrap_here (2);
1375 gdb_printf ("((struct objfile *) %s)\n",
1376 host_address_to_string (objfile));
1377 printed_objfile_start = 1;
1380 gdb_printf (" { psymtab %s ", psymtab->filename);
1381 gdb_stdout->wrap_here (4);
1382 gdb_printf ("((struct partial_symtab *) %s)\n",
1383 host_address_to_string (psymtab));
1385 gdb_printf (" readin %s\n",
1386 psymtab->readin_p (objfile) ? "yes" : "no");
1387 gdb_printf (" fullname %s\n",
1388 psymtab->fullname
1389 ? psymtab->fullname : "(null)");
1390 gdb_printf (" text addresses ");
1391 gdb_puts (paddress (gdbarch,
1392 psymtab->text_low (objfile)));
1393 gdb_printf (" -- ");
1394 gdb_puts (paddress (gdbarch,
1395 psymtab->text_high (objfile)));
1396 gdb_printf ("\n");
1397 gdb_printf (" globals ");
1398 if (!psymtab->global_psymbols.empty ())
1399 gdb_printf
1400 ("(* (struct partial_symbol **) %s @ %d)\n",
1401 host_address_to_string (psymtab->global_psymbols.data ()),
1402 (int) psymtab->global_psymbols.size ());
1403 else
1404 gdb_printf ("(none)\n");
1405 gdb_printf (" statics ");
1406 if (!psymtab->static_psymbols.empty ())
1407 gdb_printf
1408 ("(* (struct partial_symbol **) %s @ %d)\n",
1409 host_address_to_string (psymtab->static_psymbols.data ()),
1410 (int) psymtab->static_psymbols.size ());
1411 else
1412 gdb_printf ("(none)\n");
1413 if (psymtab->user)
1414 gdb_printf (" user %s "
1415 "((struct partial_symtab *) %s)\n",
1416 psymtab->user->filename,
1417 host_address_to_string (psymtab->user));
1418 gdb_printf (" dependencies ");
1419 if (psymtab->number_of_dependencies)
1421 int i;
1423 gdb_printf ("{\n");
1424 for (i = 0; i < psymtab->number_of_dependencies; i++)
1426 struct partial_symtab *dep = psymtab->dependencies[i];
1428 /* Note the string concatenation there --- no
1429 comma. */
1430 gdb_printf (" psymtab %s "
1431 "((struct partial_symtab *) %s)\n",
1432 dep->filename,
1433 host_address_to_string (dep));
1435 gdb_printf (" }\n");
1437 else
1438 gdb_printf ("(none)\n");
1439 gdb_printf (" }\n");
1444 if (printed_objfile_start)
1445 gdb_printf ("}\n");
1449 /* Check consistency of currently expanded psymtabs vs symtabs. */
1451 static void
1452 maintenance_check_psymtabs (const char *ignore, int from_tty)
1454 struct symbol *sym;
1455 struct compunit_symtab *cust = NULL;
1456 const struct blockvector *bv;
1457 const struct block *b;
1459 for (objfile *objfile : current_program_space->objfiles ())
1461 for (const auto &iter : objfile->qf)
1463 psymbol_functions *psf
1464 = dynamic_cast<psymbol_functions *> (iter.get ());
1465 if (psf == nullptr)
1466 continue;
1468 for (partial_symtab *ps : psf->partial_symbols (objfile))
1470 struct gdbarch *gdbarch = objfile->arch ();
1472 /* We don't call psymtab_to_symtab here because that may cause symtab
1473 expansion. When debugging a problem it helps if checkers leave
1474 things unchanged. */
1475 cust = ps->get_compunit_symtab (objfile);
1477 /* First do some checks that don't require the associated symtab. */
1478 if (ps->text_high (objfile) < ps->text_low (objfile))
1480 gdb_printf ("Psymtab ");
1481 gdb_puts (ps->filename);
1482 gdb_printf (" covers bad range ");
1483 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1484 gdb_printf (" - ");
1485 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1486 gdb_printf ("\n");
1487 continue;
1490 /* Now do checks requiring the associated symtab. */
1491 if (cust == NULL)
1492 continue;
1493 bv = cust->blockvector ();
1494 b = bv->static_block ();
1495 for (const partial_symbol *psym : ps->static_psymbols)
1497 /* Skip symbols for inlined functions without address. These may
1498 or may not have a match in the full symtab. */
1499 if (psym->aclass == LOC_BLOCK
1500 && psym->ginfo.value_address () == 0)
1501 continue;
1503 lookup_name_info lookup_name
1504 (psym->ginfo.search_name (), symbol_name_match_type::SEARCH_NAME);
1505 sym = block_lookup_symbol (b, lookup_name,
1506 to_search_flags (psym->domain));
1507 if (!sym)
1509 gdb_printf ("Static symbol `");
1510 gdb_puts (psym->ginfo.linkage_name ());
1511 gdb_printf ("' only found in ");
1512 gdb_puts (ps->filename);
1513 gdb_printf (" psymtab\n");
1516 b = bv->global_block ();
1517 for (const partial_symbol *psym : ps->global_psymbols)
1519 lookup_name_info lookup_name
1520 (psym->ginfo.search_name (), symbol_name_match_type::SEARCH_NAME);
1521 sym = block_lookup_symbol (b, lookup_name,
1522 to_search_flags (psym->domain));
1523 if (!sym)
1525 gdb_printf ("Global symbol `");
1526 gdb_puts (psym->ginfo.linkage_name ());
1527 gdb_printf ("' only found in ");
1528 gdb_puts (ps->filename);
1529 gdb_printf (" psymtab\n");
1532 if (ps->unrelocated_text_high () != unrelocated_addr (0)
1533 && (ps->text_low (objfile) < b->start ()
1534 || ps->text_high (objfile) > b->end ()))
1536 gdb_printf ("Psymtab ");
1537 gdb_puts (ps->filename);
1538 gdb_printf (" covers ");
1539 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1540 gdb_printf (" - ");
1541 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1542 gdb_printf (" but symtab covers only ");
1543 gdb_puts (paddress (gdbarch, b->start ()));
1544 gdb_printf (" - ");
1545 gdb_puts (paddress (gdbarch, b->end ()));
1546 gdb_printf ("\n");
1553 void _initialize_psymtab ();
1554 void
1555 _initialize_psymtab ()
1557 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
1558 Print dump of current partial symbol definitions.\n\
1559 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1560 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1561 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1562 or the terminal if OUTFILE is unspecified.\n\
1563 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1564 If SOURCE is provided, dump only that file's symbols.\n\
1565 If OBJFILE is provided, dump only that object file's symbols."),
1566 &maintenanceprintlist);
1568 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
1569 List the partial symbol tables for all object files.\n\
1570 This does not include information about individual partial symbols,\n\
1571 just the symbol table structures themselves."),
1572 &maintenanceinfolist);
1574 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
1575 _("\
1576 Check consistency of currently expanded psymtabs versus symtabs."),
1577 &maintenancelist);