[gdb/tdep] Fix reverse execution of LDR(immediate) T4
[binutils-gdb.git] / gdb / psymtab.c
blobcb7a397afb1dba1636b32b3e3cd492d2387f37cb
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 "defs.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 "addrmap.h"
28 #include "gdbtypes.h"
29 #include "ui-out.h"
30 #include "command.h"
31 #include "readline/tilde.h"
32 #include "gdbsupport/gdb_regex.h"
33 #include "dictionary.h"
34 #include "language.h"
35 #include "cp-support.h"
36 #include "gdbcmd.h"
37 #include <algorithm>
38 #include <set>
39 #include "gdbsupport/buildargv.h"
41 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
42 struct partial_symtab *,
43 const lookup_name_info &,
44 int,
45 domain_search_flags);
47 static const char *psymtab_to_fullname (struct partial_symtab *ps);
49 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
50 struct partial_symtab *,
51 CORE_ADDR,
52 struct obj_section *);
54 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
55 struct partial_symtab *pst);
57 psymtab_storage::~psymtab_storage ()
59 partial_symtab *iter = psymtabs;
60 while (iter != nullptr)
62 partial_symtab *next = iter->next;
63 delete iter;
64 iter = next;
68 /* See psymtab.h. */
70 void
71 psymtab_storage::install_psymtab (partial_symtab *pst)
73 pst->next = psymtabs;
74 psymtabs = pst;
79 /* See psymtab.h. */
81 psymtab_storage::partial_symtab_range
82 psymbol_functions::partial_symbols (struct objfile *objfile)
84 return m_partial_symtabs->range ();
87 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
88 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
90 static struct partial_symtab *
91 find_pc_sect_psymtab_closer (struct objfile *objfile,
92 CORE_ADDR pc, struct obj_section *section,
93 struct partial_symtab *pst,
94 struct bound_minimal_symbol msymbol)
96 struct partial_symtab *tpst;
97 struct partial_symtab *best_pst = pst;
98 CORE_ADDR best_addr = pst->text_low (objfile);
100 /* An objfile that has its functions reordered might have
101 many partial symbol tables containing the PC, but
102 we want the partial symbol table that contains the
103 function containing the PC. */
104 if (section == nullptr)
105 return pst;
107 if (msymbol.minsym == NULL)
108 return pst;
110 /* The code range of partial symtabs sometimes overlap, so, in
111 the loop below, we need to check all partial symtabs and
112 find the one that fits better for the given PC address. We
113 select the partial symtab that contains a symbol whose
114 address is closest to the PC address. By closest we mean
115 that find_pc_sect_symbol returns the symbol with address
116 that is closest and still less than the given PC. */
117 for (tpst = pst; tpst != NULL; tpst = tpst->next)
119 if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
121 struct partial_symbol *p;
122 CORE_ADDR this_addr;
124 /* NOTE: This assumes that every psymbol has a
125 corresponding msymbol, which is not necessarily
126 true; the debug info might be much richer than the
127 object's symbol table. */
128 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
129 if (p != NULL
130 && (p->address (objfile) == msymbol.value_address ()))
131 return tpst;
133 /* Also accept the textlow value of a psymtab as a
134 "symbol", to provide some support for partial
135 symbol tables with line information but no debug
136 symbols (e.g. those produced by an assembler). */
137 if (p != NULL)
138 this_addr = p->address (objfile);
139 else
140 this_addr = tpst->text_low (objfile);
142 /* Check whether it is closer than our current
143 BEST_ADDR. Since this symbol address is
144 necessarily lower or equal to PC, the symbol closer
145 to PC is the symbol which address is the highest.
146 This way we return the psymtab which contains such
147 best match symbol. This can help in cases where the
148 symbol information/debuginfo is not complete, like
149 for instance on IRIX6 with gcc, where no debug info
150 is emitted for statics. (See also the nodebug.exp
151 testcase.) */
152 if (this_addr > best_addr)
154 best_addr = this_addr;
155 best_pst = tpst;
159 return best_pst;
162 /* See psymtab.h. */
164 struct partial_symtab *
165 psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
166 CORE_ADDR pc,
167 struct obj_section *section,
168 struct bound_minimal_symbol msymbol)
170 for (partial_symtab *pst : partial_symbols (objfile))
171 if (pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
173 struct partial_symtab *best_pst;
175 best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
176 msymbol);
177 if (best_pst != NULL)
178 return best_pst;
181 return NULL;
184 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
185 the definition of quick_symbol_functions in symfile.h. */
187 struct compunit_symtab *
188 psymbol_functions::find_pc_sect_compunit_symtab
189 (struct objfile *objfile,
190 struct bound_minimal_symbol msymbol,
191 CORE_ADDR pc,
192 struct obj_section *section,
193 int warn_if_readin)
195 struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
196 pc, section,
197 msymbol);
198 if (ps != NULL)
200 if (warn_if_readin && ps->readin_p (objfile))
201 /* Might want to error() here (in case symtab is corrupt and
202 will cause a core dump), but maybe we can successfully
203 continue, so let's not. */
204 warning (_("\
205 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
206 paddress (objfile->arch (), pc));
207 psymtab_to_symtab (objfile, ps);
208 return ps->get_compunit_symtab (objfile);
210 return NULL;
213 /* Find which partial symbol within a psymtab matches PC and SECTION.
214 Return NULL if none. */
216 static struct partial_symbol *
217 find_pc_sect_psymbol (struct objfile *objfile,
218 struct partial_symtab *psymtab, CORE_ADDR pc,
219 struct obj_section *section)
221 struct partial_symbol *best = NULL;
222 CORE_ADDR best_pc;
223 const CORE_ADDR textlow = psymtab->text_low (objfile);
225 gdb_assert (psymtab != NULL);
227 /* Cope with programs that start at address 0. */
228 best_pc = (textlow != 0) ? textlow - 1 : 0;
230 /* Search the global symbols as well as the static symbols, so that
231 find_pc_partial_function doesn't use a minimal symbol and thus
232 cache a bad endaddr. */
233 for (partial_symbol *p : psymtab->global_psymbols)
235 if (p->domain == VAR_DOMAIN
236 && p->aclass == LOC_BLOCK
237 && pc >= p->address (objfile)
238 && (p->address (objfile) > best_pc
239 || (psymtab->text_low (objfile) == 0
240 && best_pc == 0 && p->address (objfile) == 0)))
242 if (section != NULL) /* Match on a specific section. */
244 if (!matching_obj_sections (p->obj_section (objfile),
245 section))
246 continue;
248 best_pc = p->address (objfile);
249 best = p;
253 for (partial_symbol *p : psymtab->static_psymbols)
255 if (p->domain == VAR_DOMAIN
256 && p->aclass == LOC_BLOCK
257 && pc >= p->address (objfile)
258 && (p->address (objfile) > best_pc
259 || (psymtab->text_low (objfile) == 0
260 && best_pc == 0 && p->address (objfile) == 0)))
262 if (section != NULL) /* Match on a specific section. */
264 if (!matching_obj_sections (p->obj_section (objfile),
265 section))
266 continue;
268 best_pc = p->address (objfile);
269 best = p;
273 return best;
276 /* Psymtab version of lookup_global_symbol_language. See its definition in
277 the definition of quick_symbol_functions in symfile.h. */
279 enum language
280 psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
281 const char *name,
282 domain_search_flags domain,
283 bool *symbol_found_p)
285 *symbol_found_p = false;
286 if (objfile->sf == NULL)
287 return language_unknown;
289 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
291 for (partial_symtab *ps : partial_symbols (objfile))
293 struct partial_symbol *psym;
294 if (ps->readin_p (objfile))
295 continue;
297 psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
298 if (psym)
300 *symbol_found_p = true;
301 return psym->ginfo.language ();
305 return language_unknown;
308 /* Returns true if PSYM matches LOOKUP_NAME. */
310 static bool
311 psymbol_name_matches (partial_symbol *psym,
312 const lookup_name_info &lookup_name)
314 const language_defn *lang = language_def (psym->ginfo.language ());
315 symbol_name_matcher_ftype *name_match
316 = lang->get_symbol_name_matcher (lookup_name);
317 return name_match (psym->ginfo.search_name (), lookup_name, NULL);
320 /* Look, in partial_symtab PST, for symbol whose natural name is
321 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
322 symbols if not. */
324 static struct partial_symbol *
325 lookup_partial_symbol (struct objfile *objfile,
326 struct partial_symtab *pst,
327 const lookup_name_info &lookup_name,
328 int global, domain_search_flags domain)
330 struct partial_symbol **start, **psym;
331 struct partial_symbol **top, **real_top, **bottom, **center;
332 int length = (global
333 ? pst->global_psymbols.size ()
334 : pst->static_psymbols.size ());
335 int do_linear_search = 1;
337 if (length == 0)
338 return NULL;
340 start = (global ?
341 &pst->global_psymbols[0] :
342 &pst->static_psymbols[0]);
344 if (global) /* This means we can use a binary search. */
346 do_linear_search = 0;
348 /* Binary search. This search is guaranteed to end with center
349 pointing at the earliest partial symbol whose name might be
350 correct. At that point *all* partial symbols with an
351 appropriate name will be checked against the correct
352 domain. */
354 bottom = start;
355 top = start + length - 1;
356 real_top = top;
357 while (top > bottom)
359 center = bottom + (top - bottom) / 2;
361 gdb_assert (center < top);
363 if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
364 lookup_name.c_str ()) >= 0)
366 top = center;
368 else
370 bottom = center + 1;
374 gdb_assert (top == bottom);
376 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
377 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
378 while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
379 lookup_name))
380 top--;
382 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
383 top++;
385 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
386 lookup_name))
388 if (search_flags_matches (domain, (*top)->domain))
389 return *top;
390 top++;
394 /* Can't use a binary search or else we found during the binary search that
395 we should also do a linear search. */
397 if (do_linear_search)
399 for (psym = start; psym < start + length; psym++)
401 if (search_flags_matches (domain, (*psym)->domain)
402 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
403 return *psym;
407 return NULL;
410 /* Get the symbol table that corresponds to a partial_symtab.
411 This is fast after the first time you do it.
412 The result will be NULL if the primary symtab has no symbols,
413 which can happen. Otherwise the result is the primary symtab
414 that contains PST. */
416 static struct compunit_symtab *
417 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
419 /* If it is a shared psymtab, find an unshared psymtab that includes
420 it. Any such psymtab will do. */
421 while (pst->user != NULL)
422 pst = pst->user;
424 /* If it's been looked up before, return it. */
425 if (pst->get_compunit_symtab (objfile))
426 return pst->get_compunit_symtab (objfile);
428 /* If it has not yet been read in, read it. */
429 if (!pst->readin_p (objfile))
431 scoped_restore decrementer = increment_reading_symtab ();
433 if (info_verbose)
435 gdb_printf (_("Reading in symbols for %s...\n"),
436 pst->filename);
437 gdb_flush (gdb_stdout);
440 pst->read_symtab (objfile);
443 return pst->get_compunit_symtab (objfile);
446 /* Psymtab version of find_last_source_symtab. See its definition in
447 the definition of quick_symbol_functions in symfile.h. */
449 struct symtab *
450 psymbol_functions::find_last_source_symtab (struct objfile *ofp)
452 struct partial_symtab *cs_pst = NULL;
454 for (partial_symtab *ps : partial_symbols (ofp))
456 const char *name = ps->filename;
457 int len = strlen (name);
459 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
460 || strcmp (name, "<<C++-namespaces>>") == 0)))
461 cs_pst = ps;
464 if (cs_pst)
466 if (cs_pst->readin_p (ofp))
468 internal_error (_("select_source_symtab: "
469 "readin pst found and no symtabs."));
471 else
473 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
475 if (cust == NULL)
476 return NULL;
477 return cust->primary_filetab ();
480 return NULL;
483 /* Psymtab version of forget_cached_source_info. See its definition in
484 the definition of quick_symbol_functions in symfile.h. */
486 void
487 psymbol_functions::forget_cached_source_info (struct objfile *objfile)
489 for (partial_symtab *pst : partial_symbols (objfile))
491 if (pst->fullname != NULL)
493 xfree (pst->fullname);
494 pst->fullname = NULL;
499 static void
500 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
501 const std::vector<partial_symbol *> &symbols,
502 const char *what, struct ui_file *outfile)
504 gdb_printf (outfile, " %s partial symbols:\n", what);
505 for (partial_symbol *p : symbols)
507 QUIT;
508 gdb_printf (outfile, " `%s'", p->ginfo.linkage_name ());
509 if (p->ginfo.demangled_name () != NULL)
511 gdb_printf (outfile, " `%s'",
512 p->ginfo.demangled_name ());
514 gdb_puts (", ", outfile);
515 switch (p->domain)
517 case UNDEF_DOMAIN:
518 gdb_puts ("undefined domain, ", outfile);
519 break;
520 case VAR_DOMAIN:
521 /* This is the usual thing -- don't print it. */
522 break;
523 case STRUCT_DOMAIN:
524 gdb_puts ("struct domain, ", outfile);
525 break;
526 case MODULE_DOMAIN:
527 gdb_puts ("module domain, ", outfile);
528 break;
529 case LABEL_DOMAIN:
530 gdb_puts ("label domain, ", outfile);
531 break;
532 case COMMON_BLOCK_DOMAIN:
533 gdb_puts ("common block domain, ", outfile);
534 break;
535 default:
536 gdb_puts ("<invalid domain>, ", outfile);
537 break;
539 switch (p->aclass)
541 case LOC_UNDEF:
542 gdb_puts ("undefined", outfile);
543 break;
544 case LOC_CONST:
545 gdb_puts ("constant int", outfile);
546 break;
547 case LOC_STATIC:
548 gdb_puts ("static", outfile);
549 break;
550 case LOC_REGISTER:
551 gdb_puts ("register", outfile);
552 break;
553 case LOC_ARG:
554 gdb_puts ("pass by value", outfile);
555 break;
556 case LOC_REF_ARG:
557 gdb_puts ("pass by reference", outfile);
558 break;
559 case LOC_REGPARM_ADDR:
560 gdb_puts ("register address parameter", outfile);
561 break;
562 case LOC_LOCAL:
563 gdb_puts ("stack parameter", outfile);
564 break;
565 case LOC_TYPEDEF:
566 gdb_puts ("type", outfile);
567 break;
568 case LOC_LABEL:
569 gdb_puts ("label", outfile);
570 break;
571 case LOC_BLOCK:
572 gdb_puts ("function", outfile);
573 break;
574 case LOC_CONST_BYTES:
575 gdb_puts ("constant bytes", outfile);
576 break;
577 case LOC_UNRESOLVED:
578 gdb_puts ("unresolved", outfile);
579 break;
580 case LOC_OPTIMIZED_OUT:
581 gdb_puts ("optimized out", outfile);
582 break;
583 case LOC_COMPUTED:
584 gdb_puts ("computed at runtime", outfile);
585 break;
586 default:
587 gdb_puts ("<invalid location>", outfile);
588 break;
590 gdb_puts (", ", outfile);
591 gdb_puts (paddress (gdbarch, CORE_ADDR (p->unrelocated_address ())),
592 outfile);
593 gdb_printf (outfile, "\n");
597 static void
598 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
599 struct ui_file *outfile)
601 struct gdbarch *gdbarch = objfile->arch ();
602 int i;
604 if (psymtab->anonymous)
606 gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
607 psymtab->filename);
609 else
611 gdb_printf (outfile, "\nPartial symtab for source file %s ",
612 psymtab->filename);
614 gdb_printf (outfile, "(object %s)\n\n",
615 host_address_to_string (psymtab));
616 gdb_printf (outfile, " Read from object file %s (%s)\n",
617 objfile_name (objfile),
618 host_address_to_string (objfile));
620 if (psymtab->readin_p (objfile))
621 gdb_printf
622 (outfile,
623 " Full symtab was read (at %s)\n",
624 host_address_to_string (psymtab->get_compunit_symtab (objfile)));
626 gdb_printf (outfile, " Symbols cover text addresses ");
627 gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
628 gdb_printf (outfile, "-");
629 gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
630 gdb_printf (outfile, "\n");
631 gdb_printf (outfile, " Depends on %d other partial symtabs.\n",
632 psymtab->number_of_dependencies);
633 for (i = 0; i < psymtab->number_of_dependencies; i++)
634 gdb_printf (outfile, " %d %s\n", i,
635 host_address_to_string (psymtab->dependencies[i]));
636 if (psymtab->user != NULL)
637 gdb_printf (outfile, " Shared partial symtab with user %s\n",
638 host_address_to_string (psymtab->user));
639 if (!psymtab->global_psymbols.empty ())
641 print_partial_symbols
642 (gdbarch, objfile, psymtab->global_psymbols,
643 "Global", outfile);
645 if (!psymtab->static_psymbols.empty ())
647 print_partial_symbols
648 (gdbarch, objfile, psymtab->static_psymbols,
649 "Static", outfile);
651 gdb_printf (outfile, "\n");
654 /* Count the number of partial symbols in OBJFILE. */
657 psymbol_functions::count_psyms ()
659 int count = 0;
660 for (partial_symtab *pst : m_partial_symtabs->range ())
662 count += pst->global_psymbols.size ();
663 count += pst->static_psymbols.size ();
665 return count;
668 /* Psymtab version of print_stats. See its definition in
669 the definition of quick_symbol_functions in symfile.h. */
671 void
672 psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
674 int i;
676 if (!print_bcache)
678 int n_psyms = count_psyms ();
679 if (n_psyms > 0)
680 gdb_printf (_(" Number of \"partial\" symbols read: %d\n"),
681 n_psyms);
683 i = 0;
684 for (partial_symtab *ps : partial_symbols (objfile))
686 if (!ps->readin_p (objfile))
687 i++;
689 gdb_printf (_(" Number of psym tables (not yet expanded): %d\n"),
691 gdb_printf (_(" Total memory used for psymbol cache: %d\n"),
692 m_partial_symtabs->psymbol_cache.memory_used ());
694 else
696 gdb_printf (_("Psymbol byte cache statistics:\n"));
697 m_partial_symtabs->psymbol_cache.print_statistics
698 ("partial symbol cache");
702 /* Psymtab version of dump. See its definition in
703 the definition of quick_symbol_functions in symfile.h. */
705 void
706 psymbol_functions::dump (struct objfile *objfile)
708 struct partial_symtab *psymtab;
710 if (m_partial_symtabs->psymtabs)
712 gdb_printf ("Psymtabs:\n");
713 for (psymtab = m_partial_symtabs->psymtabs;
714 psymtab != NULL;
715 psymtab = psymtab->next)
716 gdb_printf ("%s at %s\n",
717 psymtab->filename,
718 host_address_to_string (psymtab));
719 gdb_printf ("\n\n");
723 /* Psymtab version of expand_all_symtabs. See its definition in
724 the definition of quick_symbol_functions in symfile.h. */
726 void
727 psymbol_functions::expand_all_symtabs (struct objfile *objfile)
729 for (partial_symtab *psymtab : partial_symbols (objfile))
730 psymtab_to_symtab (objfile, psymtab);
733 /* Psymtab version of map_symbol_filenames. See its definition in
734 the definition of quick_symbol_functions in symfile.h. */
736 void
737 psymbol_functions::map_symbol_filenames
738 (struct objfile *objfile,
739 gdb::function_view<symbol_filename_ftype> fun,
740 bool need_fullname)
742 for (partial_symtab *ps : partial_symbols (objfile))
744 const char *fullname;
746 if (ps->readin_p (objfile))
747 continue;
749 /* We can skip shared psymtabs here, because any file name will be
750 attached to the unshared psymtab. */
751 if (ps->user != NULL)
752 continue;
754 /* Anonymous psymtabs don't have a file name. */
755 if (ps->anonymous)
756 continue;
758 QUIT;
759 if (need_fullname)
760 fullname = psymtab_to_fullname (ps);
761 else
762 fullname = NULL;
763 fun (ps->filename, fullname);
767 /* Finds the fullname that a partial_symtab represents.
769 If this functions finds the fullname, it will save it in ps->fullname
770 and it will also return the value.
772 If this function fails to find the file that this partial_symtab represents,
773 NULL will be returned and ps->fullname will be set to NULL. */
775 static const char *
776 psymtab_to_fullname (struct partial_symtab *ps)
778 gdb_assert (!ps->anonymous);
780 /* Use cached copy if we have it.
781 We rely on forget_cached_source_info being called appropriately
782 to handle cases like the file being moved. */
783 if (ps->fullname == NULL)
785 gdb::unique_xmalloc_ptr<char> fullname
786 = find_source_or_rewrite (ps->filename, ps->dirname);
787 ps->fullname = fullname.release ();
790 return ps->fullname;
793 /* A helper for psym_expand_symtabs_matching that handles searching
794 included psymtabs. This returns true if a symbol is found, and
795 false otherwise. It also updates the 'searched_flag' on the
796 various psymtabs that it searches. */
798 static bool
799 recursively_search_psymtabs
800 (struct partial_symtab *ps,
801 struct objfile *objfile,
802 block_search_flags search_flags,
803 domain_search_flags domain,
804 const lookup_name_info &lookup_name,
805 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
807 int keep_going = 1;
808 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
809 int i;
811 if (ps->searched_flag != PST_NOT_SEARCHED)
812 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
814 /* Recurse into shared psymtabs first, because they may have already
815 been searched, and this could save some time. */
816 for (i = 0; i < ps->number_of_dependencies; ++i)
818 int r;
820 /* Skip non-shared dependencies, these are handled elsewhere. */
821 if (ps->dependencies[i]->user == NULL)
822 continue;
824 r = recursively_search_psymtabs (ps->dependencies[i],
825 objfile, search_flags, domain,
826 lookup_name, sym_matcher);
827 if (r != 0)
829 ps->searched_flag = PST_SEARCHED_AND_FOUND;
830 return true;
834 partial_symbol **gbound = (ps->global_psymbols.data ()
835 + ps->global_psymbols.size ());
836 partial_symbol **sbound = (ps->static_psymbols.data ()
837 + ps->static_psymbols.size ());
838 partial_symbol **bound = gbound;
840 /* Go through all of the symbols stored in a partial
841 symtab in one loop. */
842 partial_symbol **psym = ps->global_psymbols.data ();
844 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
846 if (ps->static_psymbols.empty ())
847 keep_going = 0;
848 else
850 psym = ps->static_psymbols.data ();
851 bound = sbound;
855 while (keep_going)
857 if (psym >= bound)
859 if (bound == gbound && !ps->static_psymbols.empty ()
860 && (search_flags & SEARCH_STATIC_BLOCK) != 0)
862 psym = ps->static_psymbols.data ();
863 bound = sbound;
865 else
866 keep_going = 0;
867 continue;
869 else
871 QUIT;
873 if (search_flags_matches (domain, (*psym)->domain)
874 && psymbol_name_matches (*psym, lookup_name)
875 && (sym_matcher == NULL
876 || sym_matcher ((*psym)->ginfo.search_name ())))
878 /* Found a match, so notify our caller. */
879 result = PST_SEARCHED_AND_FOUND;
880 keep_going = 0;
883 psym++;
886 ps->searched_flag = result;
887 return result == PST_SEARCHED_AND_FOUND;
890 /* Psymtab version of expand_symtabs_matching. See its definition in
891 the definition of quick_symbol_functions in symfile.h. */
893 bool
894 psymbol_functions::expand_symtabs_matching
895 (struct objfile *objfile,
896 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
897 const lookup_name_info *lookup_name,
898 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
899 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
900 block_search_flags search_flags,
901 domain_search_flags domain)
903 /* Clear the search flags. */
904 for (partial_symtab *ps : partial_symbols (objfile))
905 ps->searched_flag = PST_NOT_SEARCHED;
907 std::optional<lookup_name_info> psym_lookup_name;
908 if (lookup_name != nullptr)
909 psym_lookup_name = lookup_name->make_ignore_params ();
911 /* This invariant is documented in quick-functions.h. */
912 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
914 for (partial_symtab *ps : m_partial_symtabs->range ())
916 QUIT;
918 if (ps->readin_p (objfile))
919 continue;
921 if (file_matcher)
923 bool match;
925 if (ps->anonymous)
926 continue;
928 match = file_matcher (ps->filename, false);
929 if (!match)
931 /* Before we invoke realpath, which can get expensive when many
932 files are involved, do a quick comparison of the basenames. */
933 if (basenames_may_differ
934 || file_matcher (lbasename (ps->filename), true))
935 match = file_matcher (psymtab_to_fullname (ps), false);
937 if (!match)
938 continue;
941 if (lookup_name == nullptr
942 || recursively_search_psymtabs (ps, objfile, search_flags,
943 domain, *psym_lookup_name,
944 symbol_matcher))
946 compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
948 if (cust != nullptr && expansion_notify != nullptr)
949 if (!expansion_notify (cust))
950 return false;
954 return true;
957 /* Psymtab version of has_symbols. See its definition in
958 the definition of quick_symbol_functions in symfile.h. */
960 bool
961 psymbol_functions::has_symbols (struct objfile *objfile)
963 return m_partial_symtabs->psymtabs != NULL;
966 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
968 bool
969 psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
971 for (partial_symtab *psymtab : partial_symbols (objfile))
973 /* Is this already expanded? */
974 if (psymtab->readin_p (objfile))
975 continue;
977 /* It has not yet been expanded. */
978 return true;
981 return false;
986 /* Partially fill a partial symtab. It will be completely filled at
987 the end of the symbol list. */
989 partial_symtab::partial_symtab (const char *filename,
990 psymtab_storage *partial_symtabs,
991 objfile_per_bfd_storage *objfile_per_bfd,
992 unrelocated_addr textlow)
993 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
995 set_text_low (textlow);
996 set_text_high (unrelocated_text_low ()); /* default */
999 /* Perform "finishing up" operations of a partial symtab. */
1001 void
1002 partial_symtab::end ()
1004 global_psymbols.shrink_to_fit ();
1005 static_psymbols.shrink_to_fit ();
1007 /* Sort the global list; don't sort the static list. */
1008 std::sort (global_psymbols.begin (),
1009 global_psymbols.end (),
1010 [] (partial_symbol *s1, partial_symbol *s2)
1012 return strcmp_iw_ordered (s1->ginfo.search_name (),
1013 s2->ginfo.search_name ()) < 0;
1017 /* See psymtab.h. */
1019 unsigned long
1020 psymbol_bcache::hash (const void *addr, int length)
1022 unsigned long h = 0;
1023 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1024 unsigned int lang = psymbol->ginfo.language ();
1025 unsigned int domain = psymbol->domain;
1026 unsigned int theclass = psymbol->aclass;
1028 h = fast_hash (&psymbol->ginfo.m_value, sizeof (psymbol->ginfo.m_value), h);
1029 h = fast_hash (&lang, sizeof (unsigned int), h);
1030 h = fast_hash (&domain, sizeof (unsigned int), h);
1031 h = fast_hash (&theclass, sizeof (unsigned int), h);
1032 /* Note that psymbol names are interned via compute_and_set_names, so
1033 there's no need to hash the contents of the name here. */
1034 h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1036 return h;
1039 /* See psymtab.h. */
1042 psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
1044 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1045 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1047 return (memcmp (&sym1->ginfo.m_value, &sym2->ginfo.m_value,
1048 sizeof (sym1->ginfo.m_value)) == 0
1049 && sym1->ginfo.language () == sym2->ginfo.language ()
1050 && sym1->domain == sym2->domain
1051 && sym1->aclass == sym2->aclass
1052 /* Note that psymbol names are interned via
1053 compute_and_set_names, so there's no need to compare the
1054 contents of the name here. */
1055 && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1058 /* See psymtab.h. */
1060 void
1061 partial_symtab::add_psymbol (const partial_symbol &psymbol,
1062 psymbol_placement where,
1063 psymtab_storage *partial_symtabs,
1064 struct objfile *objfile)
1066 bool added;
1068 /* Stash the partial symbol away in the cache. */
1069 partial_symbol *psym
1070 = ((struct partial_symbol *)
1071 partial_symtabs->psymbol_cache.insert
1072 (&psymbol, sizeof (struct partial_symbol), &added));
1074 /* Do not duplicate global partial symbols. */
1075 if (where == psymbol_placement::GLOBAL && !added)
1076 return;
1078 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1079 std::vector<partial_symbol *> &list
1080 = (where == psymbol_placement::STATIC
1081 ? static_psymbols
1082 : global_psymbols);
1083 list.push_back (psym);
1086 /* See psymtab.h. */
1088 void
1089 partial_symtab::add_psymbol (std::string_view name, bool copy_name,
1090 domain_enum domain,
1091 enum address_class theclass,
1092 short section,
1093 psymbol_placement where,
1094 unrelocated_addr coreaddr,
1095 enum language language,
1096 psymtab_storage *partial_symtabs,
1097 struct objfile *objfile)
1099 struct partial_symbol psymbol;
1100 memset (&psymbol, 0, sizeof (psymbol));
1102 psymbol.set_unrelocated_address (coreaddr);
1103 psymbol.ginfo.set_section_index (section);
1104 psymbol.domain = domain;
1105 psymbol.aclass = theclass;
1106 psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
1107 psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1109 add_psymbol (psymbol, where, partial_symtabs, objfile);
1112 /* See psymtab.h. */
1114 partial_symtab::partial_symtab (const char *filename_,
1115 psymtab_storage *partial_symtabs,
1116 objfile_per_bfd_storage *objfile_per_bfd)
1117 : searched_flag (PST_NOT_SEARCHED),
1118 text_low_valid (0),
1119 text_high_valid (0)
1121 partial_symtabs->install_psymtab (this);
1123 filename = objfile_per_bfd->intern (filename_);
1125 if (symtab_create_debug >= 1)
1127 /* Be a bit clever with debugging messages, and don't print objfile
1128 every time, only when it changes. */
1129 static std::string last_bfd_name;
1130 const char *this_bfd_name
1131 = bfd_get_filename (objfile_per_bfd->get_bfd ());
1133 if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
1135 last_bfd_name = this_bfd_name;
1137 symtab_create_debug_printf ("creating one or more psymtabs for %s",
1138 this_bfd_name);
1141 symtab_create_debug_printf ("created psymtab %s for module %s",
1142 host_address_to_string (this), filename);
1146 /* See psymtab.h. */
1148 void
1149 partial_symtab::expand_dependencies (struct objfile *objfile)
1151 for (int i = 0; i < number_of_dependencies; ++i)
1153 if (!dependencies[i]->readin_p (objfile)
1154 && dependencies[i]->user == NULL)
1156 /* Inform about additional files to be read in. */
1157 if (info_verbose)
1159 gdb_puts (" ");
1160 gdb_stdout->wrap_here (0);
1161 gdb_puts ("and ");
1162 gdb_stdout->wrap_here (0);
1163 gdb_printf ("%s...", dependencies[i]->filename);
1164 gdb_flush (gdb_stdout);
1166 dependencies[i]->expand_psymtab (objfile);
1172 void
1173 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1175 struct partial_symtab **prev_pst;
1177 /* From dbxread.c:
1178 Empty psymtabs happen as a result of header files which don't
1179 have any symbols in them. There can be a lot of them. But this
1180 check is wrong, in that a psymtab with N_SLINE entries but
1181 nothing else is not empty, but we don't realize that. Fixing
1182 that without slowing things down might be tricky. */
1184 /* First, snip it out of the psymtab chain. */
1186 prev_pst = &psymtabs;
1187 while ((*prev_pst) != pst)
1188 prev_pst = &((*prev_pst)->next);
1189 (*prev_pst) = pst->next;
1190 delete pst;
1195 static void
1196 maintenance_print_psymbols (const char *args, int from_tty)
1198 struct ui_file *outfile = gdb_stdout;
1199 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1200 int i, outfile_idx, found;
1201 CORE_ADDR pc = 0;
1202 struct obj_section *section = NULL;
1204 dont_repeat ();
1206 gdb_argv argv (args);
1208 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1210 if (strcmp (argv[i], "-pc") == 0)
1212 if (argv[i + 1] == NULL)
1213 error (_("Missing pc value"));
1214 address_arg = argv[++i];
1216 else if (strcmp (argv[i], "-source") == 0)
1218 if (argv[i + 1] == NULL)
1219 error (_("Missing source file"));
1220 source_arg = argv[++i];
1222 else if (strcmp (argv[i], "-objfile") == 0)
1224 if (argv[i + 1] == NULL)
1225 error (_("Missing objfile name"));
1226 objfile_arg = argv[++i];
1228 else if (strcmp (argv[i], "--") == 0)
1230 /* End of options. */
1231 ++i;
1232 break;
1234 else if (argv[i][0] == '-')
1236 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1237 error (_("Unknown option: %s"), argv[i]);
1239 else
1240 break;
1242 outfile_idx = i;
1244 if (address_arg != NULL && source_arg != NULL)
1245 error (_("Must specify at most one of -pc and -source"));
1247 stdio_file arg_outfile;
1249 if (argv != NULL && argv[outfile_idx] != NULL)
1251 if (argv[outfile_idx + 1] != NULL)
1252 error (_("Junk at end of command"));
1253 gdb::unique_xmalloc_ptr<char> outfile_name
1254 (tilde_expand (argv[outfile_idx]));
1255 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1256 perror_with_name (outfile_name.get ());
1257 outfile = &arg_outfile;
1260 if (address_arg != NULL)
1262 pc = parse_and_eval_address (address_arg);
1263 /* If we fail to find a section, that's ok, try the lookup anyway. */
1264 section = find_pc_section (pc);
1267 found = 0;
1268 for (objfile *objfile : current_program_space->objfiles ())
1270 int printed_objfile_header = 0;
1271 int print_for_objfile = 1;
1273 QUIT;
1274 if (objfile_arg != NULL)
1275 print_for_objfile
1276 = compare_filenames_for_search (objfile_name (objfile),
1277 objfile_arg);
1278 if (!print_for_objfile)
1279 continue;
1281 for (const auto &iter : objfile->qf)
1283 psymbol_functions *psf
1284 = dynamic_cast<psymbol_functions *> (iter.get ());
1285 if (psf == nullptr)
1286 continue;
1288 if (address_arg != NULL)
1290 struct bound_minimal_symbol msymbol;
1292 /* We don't assume each pc has a unique objfile (this is for
1293 debugging). */
1294 struct partial_symtab *ps
1295 = psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
1296 if (ps != NULL)
1298 if (!printed_objfile_header)
1300 outfile->printf ("\nPartial symtabs for objfile %s\n",
1301 objfile_name (objfile));
1302 printed_objfile_header = 1;
1304 dump_psymtab (objfile, ps, outfile);
1305 found = 1;
1308 else
1310 for (partial_symtab *ps : psf->partial_symbols (objfile))
1312 int print_for_source = 0;
1314 QUIT;
1315 if (source_arg != NULL)
1317 print_for_source
1318 = compare_filenames_for_search (ps->filename, source_arg);
1319 found = 1;
1321 if (source_arg == NULL
1322 || print_for_source)
1324 if (!printed_objfile_header)
1326 outfile->printf ("\nPartial symtabs for objfile %s\n",
1327 objfile_name (objfile));
1328 printed_objfile_header = 1;
1330 dump_psymtab (objfile, ps, outfile);
1337 if (!found)
1339 if (address_arg != NULL)
1340 error (_("No partial symtab for address: %s"), address_arg);
1341 if (source_arg != NULL)
1342 error (_("No partial symtab for source file: %s"), source_arg);
1346 /* List all the partial symbol tables whose names match REGEXP (optional). */
1348 static void
1349 maintenance_info_psymtabs (const char *regexp, int from_tty)
1351 if (regexp)
1352 re_comp (regexp);
1354 for (struct program_space *pspace : program_spaces)
1355 for (objfile *objfile : pspace->objfiles ())
1357 struct gdbarch *gdbarch = objfile->arch ();
1359 /* We don't want to print anything for this objfile until we
1360 actually find a symtab whose name matches. */
1361 int printed_objfile_start = 0;
1363 for (const auto &iter : objfile->qf)
1365 psymbol_functions *psf
1366 = dynamic_cast<psymbol_functions *> (iter.get ());
1367 if (psf == nullptr)
1368 continue;
1369 for (partial_symtab *psymtab : psf->partial_symbols (objfile))
1371 QUIT;
1373 if (! regexp
1374 || re_exec (psymtab->filename))
1376 if (! printed_objfile_start)
1378 gdb_printf ("{ objfile %s ", objfile_name (objfile));
1379 gdb_stdout->wrap_here (2);
1380 gdb_printf ("((struct objfile *) %s)\n",
1381 host_address_to_string (objfile));
1382 printed_objfile_start = 1;
1385 gdb_printf (" { psymtab %s ", psymtab->filename);
1386 gdb_stdout->wrap_here (4);
1387 gdb_printf ("((struct partial_symtab *) %s)\n",
1388 host_address_to_string (psymtab));
1390 gdb_printf (" readin %s\n",
1391 psymtab->readin_p (objfile) ? "yes" : "no");
1392 gdb_printf (" fullname %s\n",
1393 psymtab->fullname
1394 ? psymtab->fullname : "(null)");
1395 gdb_printf (" text addresses ");
1396 gdb_puts (paddress (gdbarch,
1397 psymtab->text_low (objfile)));
1398 gdb_printf (" -- ");
1399 gdb_puts (paddress (gdbarch,
1400 psymtab->text_high (objfile)));
1401 gdb_printf ("\n");
1402 gdb_printf (" globals ");
1403 if (!psymtab->global_psymbols.empty ())
1404 gdb_printf
1405 ("(* (struct partial_symbol **) %s @ %d)\n",
1406 host_address_to_string (psymtab->global_psymbols.data ()),
1407 (int) psymtab->global_psymbols.size ());
1408 else
1409 gdb_printf ("(none)\n");
1410 gdb_printf (" statics ");
1411 if (!psymtab->static_psymbols.empty ())
1412 gdb_printf
1413 ("(* (struct partial_symbol **) %s @ %d)\n",
1414 host_address_to_string (psymtab->static_psymbols.data ()),
1415 (int) psymtab->static_psymbols.size ());
1416 else
1417 gdb_printf ("(none)\n");
1418 if (psymtab->user)
1419 gdb_printf (" user %s "
1420 "((struct partial_symtab *) %s)\n",
1421 psymtab->user->filename,
1422 host_address_to_string (psymtab->user));
1423 gdb_printf (" dependencies ");
1424 if (psymtab->number_of_dependencies)
1426 int i;
1428 gdb_printf ("{\n");
1429 for (i = 0; i < psymtab->number_of_dependencies; i++)
1431 struct partial_symtab *dep = psymtab->dependencies[i];
1433 /* Note the string concatenation there --- no
1434 comma. */
1435 gdb_printf (" psymtab %s "
1436 "((struct partial_symtab *) %s)\n",
1437 dep->filename,
1438 host_address_to_string (dep));
1440 gdb_printf (" }\n");
1442 else
1443 gdb_printf ("(none)\n");
1444 gdb_printf (" }\n");
1449 if (printed_objfile_start)
1450 gdb_printf ("}\n");
1454 /* Check consistency of currently expanded psymtabs vs symtabs. */
1456 static void
1457 maintenance_check_psymtabs (const char *ignore, int from_tty)
1459 struct symbol *sym;
1460 struct compunit_symtab *cust = NULL;
1461 const struct blockvector *bv;
1462 const struct block *b;
1464 for (objfile *objfile : current_program_space->objfiles ())
1466 for (const auto &iter : objfile->qf)
1468 psymbol_functions *psf
1469 = dynamic_cast<psymbol_functions *> (iter.get ());
1470 if (psf == nullptr)
1471 continue;
1473 for (partial_symtab *ps : psf->partial_symbols (objfile))
1475 struct gdbarch *gdbarch = objfile->arch ();
1477 /* We don't call psymtab_to_symtab here because that may cause symtab
1478 expansion. When debugging a problem it helps if checkers leave
1479 things unchanged. */
1480 cust = ps->get_compunit_symtab (objfile);
1482 /* First do some checks that don't require the associated symtab. */
1483 if (ps->text_high (objfile) < ps->text_low (objfile))
1485 gdb_printf ("Psymtab ");
1486 gdb_puts (ps->filename);
1487 gdb_printf (" covers bad range ");
1488 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1489 gdb_printf (" - ");
1490 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1491 gdb_printf ("\n");
1492 continue;
1495 /* Now do checks requiring the associated symtab. */
1496 if (cust == NULL)
1497 continue;
1498 bv = cust->blockvector ();
1499 b = bv->static_block ();
1500 for (partial_symbol *psym : ps->static_psymbols)
1502 /* Skip symbols for inlined functions without address. These may
1503 or may not have a match in the full symtab. */
1504 if (psym->aclass == LOC_BLOCK
1505 && psym->ginfo.value_address () == 0)
1506 continue;
1508 sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1509 symbol_name_match_type::SEARCH_NAME,
1510 to_search_flags (psym->domain));
1511 if (!sym)
1513 gdb_printf ("Static symbol `");
1514 gdb_puts (psym->ginfo.linkage_name ());
1515 gdb_printf ("' only found in ");
1516 gdb_puts (ps->filename);
1517 gdb_printf (" psymtab\n");
1520 b = bv->global_block ();
1521 for (partial_symbol *psym : ps->global_psymbols)
1523 sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1524 symbol_name_match_type::SEARCH_NAME,
1525 to_search_flags (psym->domain));
1526 if (!sym)
1528 gdb_printf ("Global symbol `");
1529 gdb_puts (psym->ginfo.linkage_name ());
1530 gdb_printf ("' only found in ");
1531 gdb_puts (ps->filename);
1532 gdb_printf (" psymtab\n");
1535 if (ps->unrelocated_text_high () != unrelocated_addr (0)
1536 && (ps->text_low (objfile) < b->start ()
1537 || ps->text_high (objfile) > b->end ()))
1539 gdb_printf ("Psymtab ");
1540 gdb_puts (ps->filename);
1541 gdb_printf (" covers ");
1542 gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1543 gdb_printf (" - ");
1544 gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1545 gdb_printf (" but symtab covers only ");
1546 gdb_puts (paddress (gdbarch, b->start ()));
1547 gdb_printf (" - ");
1548 gdb_puts (paddress (gdbarch, b->end ()));
1549 gdb_printf ("\n");
1556 void _initialize_psymtab ();
1557 void
1558 _initialize_psymtab ()
1560 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
1561 Print dump of current partial symbol definitions.\n\
1562 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1563 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1564 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1565 or the terminal if OUTFILE is unspecified.\n\
1566 If ADDRESS is provided, dump only the symbols for the file with code at that address.\n\
1567 If SOURCE is provided, dump only that file's symbols.\n\
1568 If OBJFILE is provided, dump only that object file's symbols."),
1569 &maintenanceprintlist);
1571 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
1572 List the partial symbol tables for all object files.\n\
1573 This does not include information about individual partial symbols,\n\
1574 just the symbol table structures themselves."),
1575 &maintenanceinfolist);
1577 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
1578 _("\
1579 Check consistency of currently expanded psymtabs versus symtabs."),
1580 &maintenancelist);