xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/psymtab.c (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Partial symbol tables.
2 
3    Copyright (C) 2009-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "symtab.h"
22 #include "objfiles.h"
23 #include "psympriv.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"
40 
41 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
42 						     struct partial_symtab *,
43 						     const lookup_name_info &,
44 						     int,
45 						     domain_enum);
46 
47 static const char *psymtab_to_fullname (struct partial_symtab *ps);
48 
49 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
50 						    struct partial_symtab *,
51 						    CORE_ADDR,
52 						    struct obj_section *);
53 
54 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
55 						  struct partial_symtab *pst);
56 
57 psymtab_storage::~psymtab_storage ()
58 {
59   partial_symtab *iter = psymtabs;
60   while (iter != nullptr)
61     {
62       partial_symtab *next = iter->next;
63       delete iter;
64       iter = next;
65     }
66 }
67 
68 /* See psymtab.h.  */
69 
70 void
71 psymtab_storage::install_psymtab (partial_symtab *pst)
72 {
73   pst->next = psymtabs;
74   psymtabs = pst;
75 }
76 
77 
78 
79 /* See psympriv.h.  */
80 
81 psymtab_storage::partial_symtab_range
82 psymbol_functions::partial_symbols (struct objfile *objfile)
83 {
84   gdb_assert ((objfile->flags & OBJF_PSYMTABS_READ) != 0);
85   return m_partial_symtabs->range ();
86 }
87 
88 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
89    We may find a different psymtab than PST.  See FIND_PC_SECT_PSYMTAB.  */
90 
91 static struct partial_symtab *
92 find_pc_sect_psymtab_closer (struct objfile *objfile,
93 			     CORE_ADDR pc, struct obj_section *section,
94 			     struct partial_symtab *pst,
95 			     struct bound_minimal_symbol msymbol)
96 {
97   struct partial_symtab *tpst;
98   struct partial_symtab *best_pst = pst;
99   CORE_ADDR best_addr = pst->text_low (objfile);
100 
101   /* An objfile that has its functions reordered might have
102      many partial symbol tables containing the PC, but
103      we want the partial symbol table that contains the
104      function containing the PC.  */
105   if (!(objfile->flags & OBJF_REORDERED)
106       && section == NULL)  /* Can't validate section this way.  */
107     return pst;
108 
109   if (msymbol.minsym == NULL)
110     return pst;
111 
112   /* The code range of partial symtabs sometimes overlap, so, in
113      the loop below, we need to check all partial symtabs and
114      find the one that fits better for the given PC address.  We
115      select the partial symtab that contains a symbol whose
116      address is closest to the PC address.  By closest we mean
117      that find_pc_sect_symbol returns the symbol with address
118      that is closest and still less than the given PC.  */
119   for (tpst = pst; tpst != NULL; tpst = tpst->next)
120     {
121       if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
122 	{
123 	  struct partial_symbol *p;
124 	  CORE_ADDR this_addr;
125 
126 	  /* NOTE: This assumes that every psymbol has a
127 	     corresponding msymbol, which is not necessarily
128 	     true; the debug info might be much richer than the
129 	     object's symbol table.  */
130 	  p = find_pc_sect_psymbol (objfile, tpst, pc, section);
131 	  if (p != NULL
132 	      && (p->address (objfile) == msymbol.value_address ()))
133 	    return tpst;
134 
135 	  /* Also accept the textlow value of a psymtab as a
136 	     "symbol", to provide some support for partial
137 	     symbol tables with line information but no debug
138 	     symbols (e.g. those produced by an assembler).  */
139 	  if (p != NULL)
140 	    this_addr = p->address (objfile);
141 	  else
142 	    this_addr = tpst->text_low (objfile);
143 
144 	  /* Check whether it is closer than our current
145 	     BEST_ADDR.  Since this symbol address is
146 	     necessarily lower or equal to PC, the symbol closer
147 	     to PC is the symbol which address is the highest.
148 	     This way we return the psymtab which contains such
149 	     best match symbol.  This can help in cases where the
150 	     symbol information/debuginfo is not complete, like
151 	     for instance on IRIX6 with gcc, where no debug info
152 	     is emitted for statics.  (See also the nodebug.exp
153 	     testcase.)  */
154 	  if (this_addr > best_addr)
155 	    {
156 	      best_addr = this_addr;
157 	      best_pst = tpst;
158 	    }
159 	}
160     }
161   return best_pst;
162 }
163 
164 /* See psympriv.h.  */
165 
166 struct partial_symtab *
167 psymbol_functions::find_pc_sect_psymtab (struct objfile *objfile,
168 					 CORE_ADDR pc,
169 					 struct obj_section *section,
170 					 struct bound_minimal_symbol msymbol)
171 {
172   for (partial_symtab *pst : partial_symbols (objfile))
173     if (pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
174       {
175 	struct partial_symtab *best_pst;
176 
177 	best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
178 						msymbol);
179 	if (best_pst != NULL)
180 	  return best_pst;
181       }
182 
183   return NULL;
184 }
185 
186 /* Psymtab version of find_pc_sect_compunit_symtab.  See its definition in
187    the definition of quick_symbol_functions in symfile.h.  */
188 
189 struct compunit_symtab *
190 psymbol_functions::find_pc_sect_compunit_symtab
191      (struct objfile *objfile,
192       struct bound_minimal_symbol msymbol,
193       CORE_ADDR pc,
194       struct obj_section *section,
195       int warn_if_readin)
196 {
197   struct partial_symtab *ps = find_pc_sect_psymtab (objfile,
198 						    pc, section,
199 						    msymbol);
200   if (ps != NULL)
201     {
202       if (warn_if_readin && ps->readin_p (objfile))
203 	/* Might want to error() here (in case symtab is corrupt and
204 	   will cause a core dump), but maybe we can successfully
205 	   continue, so let's not.  */
206 	warning (_("\
207 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
208 		 paddress (objfile->arch (), pc));
209       psymtab_to_symtab (objfile, ps);
210       return ps->get_compunit_symtab (objfile);
211     }
212   return NULL;
213 }
214 
215 /* Find which partial symbol within a psymtab matches PC and SECTION.
216    Return NULL if none.  */
217 
218 static struct partial_symbol *
219 find_pc_sect_psymbol (struct objfile *objfile,
220 		      struct partial_symtab *psymtab, CORE_ADDR pc,
221 		      struct obj_section *section)
222 {
223   struct partial_symbol *best = NULL;
224   CORE_ADDR best_pc;
225   const CORE_ADDR textlow = psymtab->text_low (objfile);
226 
227   gdb_assert (psymtab != NULL);
228 
229   /* Cope with programs that start at address 0.  */
230   best_pc = (textlow != 0) ? textlow - 1 : 0;
231 
232   /* Search the global symbols as well as the static symbols, so that
233      find_pc_partial_function doesn't use a minimal symbol and thus
234      cache a bad endaddr.  */
235   for (partial_symbol *p : psymtab->global_psymbols)
236     {
237       if (p->domain == VAR_DOMAIN
238 	  && p->aclass == LOC_BLOCK
239 	  && pc >= p->address (objfile)
240 	  && (p->address (objfile) > best_pc
241 	      || (psymtab->text_low (objfile) == 0
242 		  && best_pc == 0 && p->address (objfile) == 0)))
243 	{
244 	  if (section != NULL)  /* Match on a specific section.  */
245 	    {
246 	      if (!matching_obj_sections (p->obj_section (objfile),
247 					  section))
248 		continue;
249 	    }
250 	  best_pc = p->address (objfile);
251 	  best = p;
252 	}
253     }
254 
255   for (partial_symbol *p : psymtab->static_psymbols)
256     {
257       if (p->domain == VAR_DOMAIN
258 	  && p->aclass == LOC_BLOCK
259 	  && pc >= p->address (objfile)
260 	  && (p->address (objfile) > best_pc
261 	      || (psymtab->text_low (objfile) == 0
262 		  && best_pc == 0 && p->address (objfile) == 0)))
263 	{
264 	  if (section != NULL)  /* Match on a specific section.  */
265 	    {
266 	      if (!matching_obj_sections (p->obj_section (objfile),
267 					  section))
268 		continue;
269 	    }
270 	  best_pc = p->address (objfile);
271 	  best = p;
272 	}
273     }
274 
275   return best;
276 }
277 
278 /* Psymtab version of lookup_global_symbol_language.  See its definition in
279    the definition of quick_symbol_functions in symfile.h.  */
280 
281 enum language
282 psymbol_functions::lookup_global_symbol_language (struct objfile *objfile,
283 						  const char *name,
284 						  domain_enum domain,
285 						  bool *symbol_found_p)
286 {
287   *symbol_found_p = false;
288   if (objfile->sf == NULL)
289     return language_unknown;
290 
291   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
292 
293   for (partial_symtab *ps : partial_symbols (objfile))
294     {
295       struct partial_symbol *psym;
296       if (ps->readin_p (objfile))
297 	continue;
298 
299       psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
300       if (psym)
301 	{
302 	  *symbol_found_p = true;
303 	  return psym->ginfo.language ();
304 	}
305     }
306 
307   return language_unknown;
308 }
309 
310 /* Returns true if PSYM matches LOOKUP_NAME.  */
311 
312 static bool
313 psymbol_name_matches (partial_symbol *psym,
314 		      const lookup_name_info &lookup_name)
315 {
316   const language_defn *lang = language_def (psym->ginfo.language ());
317   symbol_name_matcher_ftype *name_match
318     = lang->get_symbol_name_matcher (lookup_name);
319   return name_match (psym->ginfo.search_name (), lookup_name, NULL);
320 }
321 
322 /* Look in PST for a symbol in DOMAIN whose name matches NAME.  Search
323    the global block of PST if GLOBAL, and otherwise the static block.
324    MATCH is the comparison operation that returns true iff MATCH (s,
325    NAME), where s is a SYMBOL_SEARCH_NAME.  If ORDERED_COMPARE is
326    non-null, the symbols in the block are assumed to be ordered
327    according to it (allowing binary search).  It must be compatible
328    with MATCH.  Returns the symbol, if found, and otherwise NULL.  */
329 
330 static struct partial_symbol *
331 match_partial_symbol (struct objfile *objfile,
332 		      struct partial_symtab *pst, int global,
333 		      const lookup_name_info &name, domain_enum domain,
334 		      symbol_compare_ftype *ordered_compare)
335 {
336   struct partial_symbol **start, **psym;
337   struct partial_symbol **top, **real_top, **bottom, **center;
338   int length = (global
339 		? pst->global_psymbols.size ()
340 		: pst->static_psymbols.size ());
341   int do_linear_search = 1;
342 
343   if (length == 0)
344     return NULL;
345 
346   start = (global ?
347 	   &pst->global_psymbols[0] :
348 	   &pst->static_psymbols[0]);
349 
350   if (global && ordered_compare)  /* Can use a binary search.  */
351     {
352       do_linear_search = 0;
353 
354       /* Binary search.  This search is guaranteed to end with center
355 	 pointing at the earliest partial symbol whose name might be
356 	 correct.  At that point *all* partial symbols with an
357 	 appropriate name will be checked against the correct
358 	 domain.  */
359 
360       bottom = start;
361       top = start + length - 1;
362       real_top = top;
363       while (top > bottom)
364 	{
365 	  center = bottom + (top - bottom) / 2;
366 	  gdb_assert (center < top);
367 
368 	  enum language lang = (*center)->ginfo.language ();
369 	  const char *lang_ln = name.language_lookup_name (lang);
370 
371 	  if (ordered_compare ((*center)->ginfo.search_name (),
372 			       lang_ln) >= 0)
373 	    top = center;
374 	  else
375 	    bottom = center + 1;
376 	}
377       gdb_assert (top == bottom);
378 
379       while (top <= real_top
380 	     && psymbol_name_matches (*top, name))
381 	{
382 	  if (symbol_matches_domain ((*top)->ginfo.language (),
383 				     (*top)->domain, domain))
384 	    return *top;
385 	  top++;
386 	}
387     }
388 
389   /* Can't use a binary search or else we found during the binary search that
390      we should also do a linear search.  */
391 
392   if (do_linear_search)
393     {
394       for (psym = start; psym < start + length; psym++)
395 	{
396 	  if (symbol_matches_domain ((*psym)->ginfo.language (),
397 				     (*psym)->domain, domain)
398 	      && psymbol_name_matches (*psym, name))
399 	    return *psym;
400 	}
401     }
402 
403   return NULL;
404 }
405 
406 /* Look, in partial_symtab PST, for symbol whose natural name is
407    LOOKUP_NAME.  Check the global symbols if GLOBAL, the static
408    symbols if not.  */
409 
410 static struct partial_symbol *
411 lookup_partial_symbol (struct objfile *objfile,
412 		       struct partial_symtab *pst,
413 		       const lookup_name_info &lookup_name,
414 		       int global, domain_enum domain)
415 {
416   struct partial_symbol **start, **psym;
417   struct partial_symbol **top, **real_top, **bottom, **center;
418   int length = (global
419 		? pst->global_psymbols.size ()
420 		: pst->static_psymbols.size ());
421   int do_linear_search = 1;
422 
423   if (length == 0)
424     return NULL;
425 
426   start = (global ?
427 	   &pst->global_psymbols[0] :
428 	   &pst->static_psymbols[0]);
429 
430   if (global)			/* This means we can use a binary search.  */
431     {
432       do_linear_search = 0;
433 
434       /* Binary search.  This search is guaranteed to end with center
435 	 pointing at the earliest partial symbol whose name might be
436 	 correct.  At that point *all* partial symbols with an
437 	 appropriate name will be checked against the correct
438 	 domain.  */
439 
440       bottom = start;
441       top = start + length - 1;
442       real_top = top;
443       while (top > bottom)
444 	{
445 	  center = bottom + (top - bottom) / 2;
446 
447 	  gdb_assert (center < top);
448 
449 	  if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
450 				 lookup_name.c_str ()) >= 0)
451 	    {
452 	      top = center;
453 	    }
454 	  else
455 	    {
456 	      bottom = center + 1;
457 	    }
458 	}
459 
460       gdb_assert (top == bottom);
461 
462       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
463 	 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
464       while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
465 							 lookup_name))
466 	top--;
467 
468       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
469       top++;
470 
471       while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
472 							    lookup_name))
473 	{
474 	  if (symbol_matches_domain ((*top)->ginfo.language (),
475 				     (*top)->domain, domain))
476 	    return *top;
477 	  top++;
478 	}
479     }
480 
481   /* Can't use a binary search or else we found during the binary search that
482      we should also do a linear search.  */
483 
484   if (do_linear_search)
485     {
486       for (psym = start; psym < start + length; psym++)
487 	{
488 	  if (symbol_matches_domain ((*psym)->ginfo.language (),
489 				     (*psym)->domain, domain)
490 	      && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
491 	    return *psym;
492 	}
493     }
494 
495   return NULL;
496 }
497 
498 /* Get the symbol table that corresponds to a partial_symtab.
499    This is fast after the first time you do it.
500    The result will be NULL if the primary symtab has no symbols,
501    which can happen.  Otherwise the result is the primary symtab
502    that contains PST.  */
503 
504 static struct compunit_symtab *
505 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
506 {
507   /* If it is a shared psymtab, find an unshared psymtab that includes
508      it.  Any such psymtab will do.  */
509   while (pst->user != NULL)
510     pst = pst->user;
511 
512   /* If it's been looked up before, return it.  */
513   if (pst->get_compunit_symtab (objfile))
514     return pst->get_compunit_symtab (objfile);
515 
516   /* If it has not yet been read in, read it.  */
517   if (!pst->readin_p (objfile))
518     {
519       scoped_restore decrementer = increment_reading_symtab ();
520 
521       if (info_verbose)
522 	{
523 	  gdb_printf (_("Reading in symbols for %s...\n"),
524 		      pst->filename);
525 	  gdb_flush (gdb_stdout);
526 	}
527 
528       pst->read_symtab (objfile);
529     }
530 
531   return pst->get_compunit_symtab (objfile);
532 }
533 
534 /* Psymtab version of find_last_source_symtab.  See its definition in
535    the definition of quick_symbol_functions in symfile.h.  */
536 
537 struct symtab *
538 psymbol_functions::find_last_source_symtab (struct objfile *ofp)
539 {
540   struct partial_symtab *cs_pst = NULL;
541 
542   for (partial_symtab *ps : partial_symbols (ofp))
543     {
544       const char *name = ps->filename;
545       int len = strlen (name);
546 
547       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
548 			|| strcmp (name, "<<C++-namespaces>>") == 0)))
549 	cs_pst = ps;
550     }
551 
552   if (cs_pst)
553     {
554       if (cs_pst->readin_p (ofp))
555 	{
556 	  internal_error (_("select_source_symtab: "
557 			  "readin pst found and no symtabs."));
558 	}
559       else
560 	{
561 	  struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
562 
563 	  if (cust == NULL)
564 	    return NULL;
565 	  return cust->primary_filetab ();
566 	}
567     }
568   return NULL;
569 }
570 
571 /* Psymtab version of forget_cached_source_info.  See its definition in
572    the definition of quick_symbol_functions in symfile.h.  */
573 
574 void
575 psymbol_functions::forget_cached_source_info (struct objfile *objfile)
576 {
577   for (partial_symtab *pst : partial_symbols (objfile))
578     {
579       if (pst->fullname != NULL)
580 	{
581 	  xfree (pst->fullname);
582 	  pst->fullname = NULL;
583 	}
584     }
585 }
586 
587 static void
588 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
589 		       const std::vector<partial_symbol *> &symbols,
590 		       const char *what, struct ui_file *outfile)
591 {
592   gdb_printf (outfile, "  %s partial symbols:\n", what);
593   for (partial_symbol *p : symbols)
594     {
595       QUIT;
596       gdb_printf (outfile, "    `%s'", p->ginfo.linkage_name ());
597       if (p->ginfo.demangled_name () != NULL)
598 	{
599 	  gdb_printf (outfile, "  `%s'",
600 		      p->ginfo.demangled_name ());
601 	}
602       gdb_puts (", ", outfile);
603       switch (p->domain)
604 	{
605 	case UNDEF_DOMAIN:
606 	  gdb_puts ("undefined domain, ", outfile);
607 	  break;
608 	case VAR_DOMAIN:
609 	  /* This is the usual thing -- don't print it.  */
610 	  break;
611 	case STRUCT_DOMAIN:
612 	  gdb_puts ("struct domain, ", outfile);
613 	  break;
614 	case MODULE_DOMAIN:
615 	  gdb_puts ("module domain, ", outfile);
616 	  break;
617 	case LABEL_DOMAIN:
618 	  gdb_puts ("label domain, ", outfile);
619 	  break;
620 	case COMMON_BLOCK_DOMAIN:
621 	  gdb_puts ("common block domain, ", outfile);
622 	  break;
623 	default:
624 	  gdb_puts ("<invalid domain>, ", outfile);
625 	  break;
626 	}
627       switch (p->aclass)
628 	{
629 	case LOC_UNDEF:
630 	  gdb_puts ("undefined", outfile);
631 	  break;
632 	case LOC_CONST:
633 	  gdb_puts ("constant int", outfile);
634 	  break;
635 	case LOC_STATIC:
636 	  gdb_puts ("static", outfile);
637 	  break;
638 	case LOC_REGISTER:
639 	  gdb_puts ("register", outfile);
640 	  break;
641 	case LOC_ARG:
642 	  gdb_puts ("pass by value", outfile);
643 	  break;
644 	case LOC_REF_ARG:
645 	  gdb_puts ("pass by reference", outfile);
646 	  break;
647 	case LOC_REGPARM_ADDR:
648 	  gdb_puts ("register address parameter", outfile);
649 	  break;
650 	case LOC_LOCAL:
651 	  gdb_puts ("stack parameter", outfile);
652 	  break;
653 	case LOC_TYPEDEF:
654 	  gdb_puts ("type", outfile);
655 	  break;
656 	case LOC_LABEL:
657 	  gdb_puts ("label", outfile);
658 	  break;
659 	case LOC_BLOCK:
660 	  gdb_puts ("function", outfile);
661 	  break;
662 	case LOC_CONST_BYTES:
663 	  gdb_puts ("constant bytes", outfile);
664 	  break;
665 	case LOC_UNRESOLVED:
666 	  gdb_puts ("unresolved", outfile);
667 	  break;
668 	case LOC_OPTIMIZED_OUT:
669 	  gdb_puts ("optimized out", outfile);
670 	  break;
671 	case LOC_COMPUTED:
672 	  gdb_puts ("computed at runtime", outfile);
673 	  break;
674 	default:
675 	  gdb_puts ("<invalid location>", outfile);
676 	  break;
677 	}
678       gdb_puts (", ", outfile);
679       gdb_puts (paddress (gdbarch, p->unrelocated_address ()), outfile);
680       gdb_printf (outfile, "\n");
681     }
682 }
683 
684 static void
685 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
686 	      struct ui_file *outfile)
687 {
688   struct gdbarch *gdbarch = objfile->arch ();
689   int i;
690 
691   if (psymtab->anonymous)
692     {
693       gdb_printf (outfile, "\nAnonymous partial symtab (%s) ",
694 		  psymtab->filename);
695     }
696   else
697     {
698       gdb_printf (outfile, "\nPartial symtab for source file %s ",
699 		  psymtab->filename);
700     }
701   gdb_printf (outfile, "(object %s)\n\n",
702 	      host_address_to_string (psymtab));
703   gdb_printf (outfile, "  Read from object file %s (%s)\n",
704 	      objfile_name (objfile),
705 	      host_address_to_string (objfile));
706 
707   if (psymtab->readin_p (objfile))
708     gdb_printf
709       (outfile,
710        "  Full symtab was read (at %s)\n",
711        host_address_to_string (psymtab->get_compunit_symtab (objfile)));
712 
713   gdb_printf (outfile, "  Symbols cover text addresses ");
714   gdb_puts (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
715   gdb_printf (outfile, "-");
716   gdb_puts (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
717   gdb_printf (outfile, "\n");
718   gdb_printf (outfile, "  Depends on %d other partial symtabs.\n",
719 	      psymtab->number_of_dependencies);
720   for (i = 0; i < psymtab->number_of_dependencies; i++)
721     gdb_printf (outfile, "    %d %s\n", i,
722 		host_address_to_string (psymtab->dependencies[i]));
723   if (psymtab->user != NULL)
724     gdb_printf (outfile, "  Shared partial symtab with user %s\n",
725 		host_address_to_string (psymtab->user));
726   if (!psymtab->global_psymbols.empty ())
727     {
728       print_partial_symbols
729 	(gdbarch, objfile, psymtab->global_psymbols,
730 	 "Global", outfile);
731     }
732   if (!psymtab->static_psymbols.empty ())
733     {
734       print_partial_symbols
735 	(gdbarch, objfile, psymtab->static_psymbols,
736 	 "Static", outfile);
737     }
738   gdb_printf (outfile, "\n");
739 }
740 
741 /* Count the number of partial symbols in OBJFILE.  */
742 
743 int
744 psymbol_functions::count_psyms ()
745 {
746   int count = 0;
747   for (partial_symtab *pst : m_partial_symtabs->range ())
748     {
749       count += pst->global_psymbols.size ();
750       count += pst->static_psymbols.size ();
751     }
752   return count;
753 }
754 
755 /* Psymtab version of print_stats.  See its definition in
756    the definition of quick_symbol_functions in symfile.h.  */
757 
758 void
759 psymbol_functions::print_stats (struct objfile *objfile, bool print_bcache)
760 {
761   int i;
762 
763   if (!print_bcache)
764     {
765       int n_psyms = count_psyms ();
766       if (n_psyms > 0)
767 	gdb_printf (_("  Number of \"partial\" symbols read: %d\n"),
768 		    n_psyms);
769 
770       i = 0;
771       for (partial_symtab *ps : partial_symbols (objfile))
772 	{
773 	  if (!ps->readin_p (objfile))
774 	    i++;
775 	}
776       gdb_printf (_("  Number of psym tables (not yet expanded): %d\n"),
777 		  i);
778       gdb_printf (_("  Total memory used for psymbol cache: %d\n"),
779 		  m_partial_symtabs->psymbol_cache.memory_used ());
780     }
781   else
782     {
783       gdb_printf (_("Psymbol byte cache statistics:\n"));
784       m_partial_symtabs->psymbol_cache.print_statistics
785 	("partial symbol cache");
786     }
787 }
788 
789 /* Psymtab version of dump.  See its definition in
790    the definition of quick_symbol_functions in symfile.h.  */
791 
792 void
793 psymbol_functions::dump (struct objfile *objfile)
794 {
795   struct partial_symtab *psymtab;
796 
797   if (m_partial_symtabs->psymtabs)
798     {
799       gdb_printf ("Psymtabs:\n");
800       for (psymtab = m_partial_symtabs->psymtabs;
801 	   psymtab != NULL;
802 	   psymtab = psymtab->next)
803 	gdb_printf ("%s at %s\n",
804 		    psymtab->filename,
805 		    host_address_to_string (psymtab));
806       gdb_printf ("\n\n");
807     }
808 }
809 
810 /* Psymtab version of expand_all_symtabs.  See its definition in
811    the definition of quick_symbol_functions in symfile.h.  */
812 
813 void
814 psymbol_functions::expand_all_symtabs (struct objfile *objfile)
815 {
816   for (partial_symtab *psymtab : partial_symbols (objfile))
817     psymtab_to_symtab (objfile, psymtab);
818 }
819 
820 /* Psymtab version of map_symbol_filenames.  See its definition in
821    the definition of quick_symbol_functions in symfile.h.  */
822 
823 void
824 psymbol_functions::map_symbol_filenames
825      (struct objfile *objfile,
826       gdb::function_view<symbol_filename_ftype> fun,
827       bool need_fullname)
828 {
829   for (partial_symtab *ps : partial_symbols (objfile))
830     {
831       const char *fullname;
832 
833       if (ps->readin_p (objfile))
834 	continue;
835 
836       /* We can skip shared psymtabs here, because any file name will be
837 	 attached to the unshared psymtab.  */
838       if (ps->user != NULL)
839 	continue;
840 
841       /* Anonymous psymtabs don't have a file name.  */
842       if (ps->anonymous)
843 	continue;
844 
845       QUIT;
846       if (need_fullname)
847 	fullname = psymtab_to_fullname (ps);
848       else
849 	fullname = NULL;
850       fun (ps->filename, fullname);
851     }
852 }
853 
854 /* Finds the fullname that a partial_symtab represents.
855 
856    If this functions finds the fullname, it will save it in ps->fullname
857    and it will also return the value.
858 
859    If this function fails to find the file that this partial_symtab represents,
860    NULL will be returned and ps->fullname will be set to NULL.  */
861 
862 static const char *
863 psymtab_to_fullname (struct partial_symtab *ps)
864 {
865   gdb_assert (!ps->anonymous);
866 
867   /* Use cached copy if we have it.
868      We rely on forget_cached_source_info being called appropriately
869      to handle cases like the file being moved.  */
870   if (ps->fullname == NULL)
871     {
872       gdb::unique_xmalloc_ptr<char> fullname
873 	= find_source_or_rewrite (ps->filename, ps->dirname);
874       ps->fullname = fullname.release ();
875     }
876 
877   return ps->fullname;
878 }
879 
880 /* Psymtab version of expand_matching_symbols.  See its definition in
881    the definition of quick_symbol_functions in symfile.h.  */
882 
883 void
884 psymbol_functions::expand_matching_symbols
885   (struct objfile *objfile,
886    const lookup_name_info &name, domain_enum domain,
887    int global,
888    symbol_compare_ftype *ordered_compare)
889 {
890   for (partial_symtab *ps : partial_symbols (objfile))
891     {
892       QUIT;
893       if (!ps->readin_p (objfile)
894 	  && match_partial_symbol (objfile, ps, global, name, domain,
895 				   ordered_compare))
896 	psymtab_to_symtab (objfile, ps);
897     }
898 }
899 
900 /* A helper for psym_expand_symtabs_matching that handles searching
901    included psymtabs.  This returns true if a symbol is found, and
902    false otherwise.  It also updates the 'searched_flag' on the
903    various psymtabs that it searches.  */
904 
905 static bool
906 recursively_search_psymtabs
907   (struct partial_symtab *ps,
908    struct objfile *objfile,
909    block_search_flags search_flags,
910    domain_enum domain,
911    enum search_domain search,
912    const lookup_name_info &lookup_name,
913    gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
914 {
915   int keep_going = 1;
916   enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
917   int i;
918 
919   if (ps->searched_flag != PST_NOT_SEARCHED)
920     return ps->searched_flag == PST_SEARCHED_AND_FOUND;
921 
922   /* Recurse into shared psymtabs first, because they may have already
923      been searched, and this could save some time.  */
924   for (i = 0; i < ps->number_of_dependencies; ++i)
925     {
926       int r;
927 
928       /* Skip non-shared dependencies, these are handled elsewhere.  */
929       if (ps->dependencies[i]->user == NULL)
930 	continue;
931 
932       r = recursively_search_psymtabs (ps->dependencies[i],
933 				       objfile, search_flags, domain, search,
934 				       lookup_name, sym_matcher);
935       if (r != 0)
936 	{
937 	  ps->searched_flag = PST_SEARCHED_AND_FOUND;
938 	  return true;
939 	}
940     }
941 
942   partial_symbol **gbound = (ps->global_psymbols.data ()
943 			     + ps->global_psymbols.size ());
944   partial_symbol **sbound = (ps->static_psymbols.data ()
945 			     + ps->static_psymbols.size ());
946   partial_symbol **bound = gbound;
947 
948   /* Go through all of the symbols stored in a partial
949      symtab in one loop.  */
950   partial_symbol **psym = ps->global_psymbols.data ();
951 
952   if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
953     {
954       if (ps->static_psymbols.empty ())
955 	keep_going = 0;
956       else
957 	{
958 	  psym = ps->static_psymbols.data ();
959 	  bound = sbound;
960 	}
961     }
962 
963   while (keep_going)
964     {
965       if (psym >= bound)
966 	{
967 	  if (bound == gbound && !ps->static_psymbols.empty ()
968 	      && (search_flags & SEARCH_STATIC_BLOCK) != 0)
969 	    {
970 	      psym = ps->static_psymbols.data ();
971 	      bound = sbound;
972 	    }
973 	  else
974 	    keep_going = 0;
975 	  continue;
976 	}
977       else
978 	{
979 	  QUIT;
980 
981 	  if ((domain == UNDEF_DOMAIN
982 	       || symbol_matches_domain ((*psym)->ginfo.language (),
983 					 (*psym)->domain, domain))
984 	      && (search == ALL_DOMAIN
985 		  || (search == MODULES_DOMAIN
986 		      && (*psym)->domain == MODULE_DOMAIN)
987 		  || (search == VARIABLES_DOMAIN
988 		      && (*psym)->aclass != LOC_TYPEDEF
989 		      && (*psym)->aclass != LOC_BLOCK)
990 		  || (search == FUNCTIONS_DOMAIN
991 		      && (*psym)->aclass == LOC_BLOCK)
992 		  || (search == TYPES_DOMAIN
993 		      && (*psym)->aclass == LOC_TYPEDEF))
994 	      && psymbol_name_matches (*psym, lookup_name)
995 	      && (sym_matcher == NULL
996 		  || sym_matcher ((*psym)->ginfo.search_name ())))
997 	    {
998 	      /* Found a match, so notify our caller.  */
999 	      result = PST_SEARCHED_AND_FOUND;
1000 	      keep_going = 0;
1001 	    }
1002 	}
1003       psym++;
1004     }
1005 
1006   ps->searched_flag = result;
1007   return result == PST_SEARCHED_AND_FOUND;
1008 }
1009 
1010 /* Psymtab version of expand_symtabs_matching.  See its definition in
1011    the definition of quick_symbol_functions in symfile.h.  */
1012 
1013 bool
1014 psymbol_functions::expand_symtabs_matching
1015   (struct objfile *objfile,
1016    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1017    const lookup_name_info *lookup_name,
1018    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1019    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1020    block_search_flags search_flags,
1021    domain_enum domain,
1022    enum search_domain search)
1023 {
1024   /* Clear the search flags.  */
1025   for (partial_symtab *ps : partial_symbols (objfile))
1026     ps->searched_flag = PST_NOT_SEARCHED;
1027 
1028   gdb::optional<lookup_name_info> psym_lookup_name;
1029   if (lookup_name != nullptr)
1030     psym_lookup_name = lookup_name->make_ignore_params ();
1031 
1032   /* This invariant is documented in quick-functions.h.  */
1033   gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
1034 
1035   for (partial_symtab *ps : m_partial_symtabs->range ())
1036     {
1037       QUIT;
1038 
1039       if (ps->readin_p (objfile))
1040 	continue;
1041 
1042       if (file_matcher)
1043 	{
1044 	  bool match;
1045 
1046 	  if (ps->anonymous)
1047 	    continue;
1048 
1049 	  match = file_matcher (ps->filename, false);
1050 	  if (!match)
1051 	    {
1052 	      /* Before we invoke realpath, which can get expensive when many
1053 		 files are involved, do a quick comparison of the basenames.  */
1054 	      if (basenames_may_differ
1055 		  || file_matcher (lbasename (ps->filename), true))
1056 		match = file_matcher (psymtab_to_fullname (ps), false);
1057 	    }
1058 	  if (!match)
1059 	    continue;
1060 	}
1061 
1062       if (lookup_name == nullptr
1063 	  || recursively_search_psymtabs (ps, objfile, search_flags,
1064 					  domain, search,
1065 					  *psym_lookup_name,
1066 					  symbol_matcher))
1067 	{
1068 	  compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1069 
1070 	  if (cust != nullptr && expansion_notify != nullptr)
1071 	    if (!expansion_notify (cust))
1072 	      return false;
1073 	}
1074     }
1075 
1076   return true;
1077 }
1078 
1079 /* Psymtab version of has_symbols.  See its definition in
1080    the definition of quick_symbol_functions in symfile.h.  */
1081 
1082 bool
1083 psymbol_functions::has_symbols (struct objfile *objfile)
1084 {
1085   return m_partial_symtabs->psymtabs != NULL;
1086 }
1087 
1088 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h.  */
1089 
1090 bool
1091 psymbol_functions::has_unexpanded_symtabs (struct objfile *objfile)
1092 {
1093   for (partial_symtab *psymtab : partial_symbols (objfile))
1094     {
1095       /* Is this already expanded?  */
1096       if (psymtab->readin_p (objfile))
1097 	continue;
1098 
1099       /* It has not yet been expanded.  */
1100       return true;
1101     }
1102 
1103   return false;
1104 }
1105 
1106 /* Helper function for psym_find_compunit_symtab_by_address that fills
1107    in m_psymbol_map for a given range of psymbols.  */
1108 
1109 void
1110 psymbol_functions::fill_psymbol_map
1111      (struct objfile *objfile,
1112       struct partial_symtab *psymtab,
1113       std::set<CORE_ADDR> *seen_addrs,
1114       const std::vector<partial_symbol *> &symbols)
1115 {
1116   for (partial_symbol *psym : symbols)
1117     {
1118       if (psym->aclass == LOC_STATIC)
1119 	{
1120 	  CORE_ADDR addr = psym->address (objfile);
1121 	  if (seen_addrs->find (addr) == seen_addrs->end ())
1122 	    {
1123 	      seen_addrs->insert (addr);
1124 	      m_psymbol_map.emplace_back (addr, psymtab);
1125 	    }
1126 	}
1127     }
1128 }
1129 
1130 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1131    symfile.h.  */
1132 
1133 compunit_symtab *
1134 psymbol_functions::find_compunit_symtab_by_address (struct objfile *objfile,
1135 						    CORE_ADDR address)
1136 {
1137   if (m_psymbol_map.empty ())
1138     {
1139       std::set<CORE_ADDR> seen_addrs;
1140 
1141       for (partial_symtab *pst : partial_symbols (objfile))
1142 	{
1143 	  fill_psymbol_map (objfile, pst,
1144 			    &seen_addrs,
1145 			    pst->global_psymbols);
1146 	  fill_psymbol_map (objfile, pst,
1147 			    &seen_addrs,
1148 			    pst->static_psymbols);
1149 	}
1150 
1151       m_psymbol_map.shrink_to_fit ();
1152 
1153       std::sort (m_psymbol_map.begin (), m_psymbol_map.end (),
1154 		 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1155 		     const std::pair<CORE_ADDR, partial_symtab *> &b)
1156 		 {
1157 		   return a.first < b.first;
1158 		 });
1159     }
1160 
1161   auto iter = std::lower_bound
1162     (m_psymbol_map.begin (), m_psymbol_map.end (), address,
1163      [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1164 	 CORE_ADDR b)
1165      {
1166        return a.first < b;
1167      });
1168 
1169   if (iter == m_psymbol_map.end () || iter->first != address)
1170     return NULL;
1171 
1172   return psymtab_to_symtab (objfile, iter->second);
1173 }
1174 
1175 
1176 
1177 /* Partially fill a partial symtab.  It will be completely filled at
1178    the end of the symbol list.  */
1179 
1180 partial_symtab::partial_symtab (const char *filename,
1181 				psymtab_storage *partial_symtabs,
1182 				objfile_per_bfd_storage *objfile_per_bfd,
1183 				CORE_ADDR textlow)
1184   : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
1185 {
1186   set_text_low (textlow);
1187   set_text_high (raw_text_low ()); /* default */
1188 }
1189 
1190 /* Perform "finishing up" operations of a partial symtab.  */
1191 
1192 void
1193 partial_symtab::end ()
1194 {
1195   global_psymbols.shrink_to_fit ();
1196   static_psymbols.shrink_to_fit ();
1197 
1198   /* Sort the global list; don't sort the static list.  */
1199   std::sort (global_psymbols.begin (),
1200 	     global_psymbols.end (),
1201 	     [] (partial_symbol *s1, partial_symbol *s2)
1202     {
1203       return strcmp_iw_ordered (s1->ginfo.search_name (),
1204 				s2->ginfo.search_name ()) < 0;
1205     });
1206 }
1207 
1208 /* See psymtab.h.  */
1209 
1210 unsigned long
1211 psymbol_bcache::hash (const void *addr, int length)
1212 {
1213   unsigned long h = 0;
1214   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1215   unsigned int lang = psymbol->ginfo.language ();
1216   unsigned int domain = psymbol->domain;
1217   unsigned int theclass = psymbol->aclass;
1218 
1219   h = fast_hash (&psymbol->ginfo.m_value, sizeof (psymbol->ginfo.m_value), h);
1220   h = fast_hash (&lang, sizeof (unsigned int), h);
1221   h = fast_hash (&domain, sizeof (unsigned int), h);
1222   h = fast_hash (&theclass, sizeof (unsigned int), h);
1223   /* Note that psymbol names are interned via compute_and_set_names, so
1224      there's no need to hash the contents of the name here.  */
1225   h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1226 
1227   return h;
1228 }
1229 
1230 /* See psymtab.h.  */
1231 
1232 int
1233 psymbol_bcache::compare (const void *addr1, const void *addr2, int length)
1234 {
1235   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1236   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1237 
1238   return (memcmp (&sym1->ginfo.m_value, &sym2->ginfo.m_value,
1239 		  sizeof (sym1->ginfo.m_value)) == 0
1240 	  && sym1->ginfo.language () == sym2->ginfo.language ()
1241 	  && sym1->domain == sym2->domain
1242 	  && sym1->aclass == sym2->aclass
1243 	  /* Note that psymbol names are interned via
1244 	     compute_and_set_names, so there's no need to compare the
1245 	     contents of the name here.  */
1246 	  && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1247 }
1248 
1249 /* See psympriv.h.  */
1250 
1251 void
1252 partial_symtab::add_psymbol (const partial_symbol &psymbol,
1253 			     psymbol_placement where,
1254 			     psymtab_storage *partial_symtabs,
1255 			     struct objfile *objfile)
1256 {
1257   bool added;
1258 
1259   /* Stash the partial symbol away in the cache.  */
1260   partial_symbol *psym
1261     = ((struct partial_symbol *)
1262        partial_symtabs->psymbol_cache.insert
1263        (&psymbol, sizeof (struct partial_symbol), &added));
1264 
1265   /* Do not duplicate global partial symbols.  */
1266   if (where == psymbol_placement::GLOBAL && !added)
1267     return;
1268 
1269   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
1270   std::vector<partial_symbol *> &list
1271     = (where == psymbol_placement::STATIC
1272        ? static_psymbols
1273        : global_psymbols);
1274   list.push_back (psym);
1275 }
1276 
1277 /* See psympriv.h.  */
1278 
1279 void
1280 partial_symtab::add_psymbol (gdb::string_view name, bool copy_name,
1281 			     domain_enum domain,
1282 			     enum address_class theclass,
1283 			     short section,
1284 			     psymbol_placement where,
1285 			     CORE_ADDR coreaddr,
1286 			     enum language language,
1287 			     psymtab_storage *partial_symtabs,
1288 			     struct objfile *objfile)
1289 {
1290   struct partial_symbol psymbol;
1291   memset (&psymbol, 0, sizeof (psymbol));
1292 
1293   psymbol.set_unrelocated_address (coreaddr);
1294   psymbol.ginfo.set_section_index (section);
1295   psymbol.domain = domain;
1296   psymbol.aclass = theclass;
1297   psymbol.ginfo.set_language (language, partial_symtabs->obstack ());
1298   psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1299 
1300   add_psymbol (psymbol, where, partial_symtabs, objfile);
1301 }
1302 
1303 /* See psympriv.h.  */
1304 
1305 partial_symtab::partial_symtab (const char *filename_,
1306 				psymtab_storage *partial_symtabs,
1307 				objfile_per_bfd_storage *objfile_per_bfd)
1308   : searched_flag (PST_NOT_SEARCHED),
1309     text_low_valid (0),
1310     text_high_valid (0)
1311 {
1312   partial_symtabs->install_psymtab (this);
1313 
1314   filename = objfile_per_bfd->intern (filename_);
1315 
1316   if (symtab_create_debug >= 1)
1317     {
1318       /* Be a bit clever with debugging messages, and don't print objfile
1319 	 every time, only when it changes.  */
1320       static std::string last_bfd_name;
1321       const char *this_bfd_name
1322 	= bfd_get_filename (objfile_per_bfd->get_bfd ());
1323 
1324       if (last_bfd_name.empty () || last_bfd_name != this_bfd_name)
1325 	{
1326 	  last_bfd_name = this_bfd_name;
1327 
1328 	  symtab_create_debug_printf ("creating one or more psymtabs for %s",
1329 				      this_bfd_name);
1330 	}
1331 
1332       symtab_create_debug_printf ("created psymtab %s for module %s",
1333 				  host_address_to_string (this), filename);
1334     }
1335 }
1336 
1337 /* See psympriv.h.  */
1338 
1339 void
1340 partial_symtab::expand_dependencies (struct objfile *objfile)
1341 {
1342   for (int i = 0; i < number_of_dependencies; ++i)
1343     {
1344       if (!dependencies[i]->readin_p (objfile)
1345 	  && dependencies[i]->user == NULL)
1346 	{
1347 	  /* Inform about additional files to be read in.  */
1348 	  if (info_verbose)
1349 	    {
1350 	      gdb_puts (" ");
1351 	      gdb_stdout->wrap_here (0);
1352 	      gdb_puts ("and ");
1353 	      gdb_stdout->wrap_here (0);
1354 	      gdb_printf ("%s...", dependencies[i]->filename);
1355 	      gdb_flush (gdb_stdout);
1356 	    }
1357 	  dependencies[i]->expand_psymtab (objfile);
1358 	}
1359     }
1360 }
1361 
1362 
1363 void
1364 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1365 {
1366   struct partial_symtab **prev_pst;
1367 
1368   /* From dbxread.c:
1369      Empty psymtabs happen as a result of header files which don't
1370      have any symbols in them.  There can be a lot of them.  But this
1371      check is wrong, in that a psymtab with N_SLINE entries but
1372      nothing else is not empty, but we don't realize that.  Fixing
1373      that without slowing things down might be tricky.  */
1374 
1375   /* First, snip it out of the psymtab chain.  */
1376 
1377   prev_pst = &psymtabs;
1378   while ((*prev_pst) != pst)
1379     prev_pst = &((*prev_pst)->next);
1380   (*prev_pst) = pst->next;
1381   delete pst;
1382 }
1383 
1384 
1385 
1386 static void
1387 maintenance_print_psymbols (const char *args, int from_tty)
1388 {
1389   struct ui_file *outfile = gdb_stdout;
1390   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1391   int i, outfile_idx, found;
1392   CORE_ADDR pc = 0;
1393   struct obj_section *section = NULL;
1394 
1395   dont_repeat ();
1396 
1397   gdb_argv argv (args);
1398 
1399   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1400     {
1401       if (strcmp (argv[i], "-pc") == 0)
1402 	{
1403 	  if (argv[i + 1] == NULL)
1404 	    error (_("Missing pc value"));
1405 	  address_arg = argv[++i];
1406 	}
1407       else if (strcmp (argv[i], "-source") == 0)
1408 	{
1409 	  if (argv[i + 1] == NULL)
1410 	    error (_("Missing source file"));
1411 	  source_arg = argv[++i];
1412 	}
1413       else if (strcmp (argv[i], "-objfile") == 0)
1414 	{
1415 	  if (argv[i + 1] == NULL)
1416 	    error (_("Missing objfile name"));
1417 	  objfile_arg = argv[++i];
1418 	}
1419       else if (strcmp (argv[i], "--") == 0)
1420 	{
1421 	  /* End of options.  */
1422 	  ++i;
1423 	  break;
1424 	}
1425       else if (argv[i][0] == '-')
1426 	{
1427 	  /* Future proofing: Don't allow OUTFILE to begin with "-".  */
1428 	  error (_("Unknown option: %s"), argv[i]);
1429 	}
1430       else
1431 	break;
1432     }
1433   outfile_idx = i;
1434 
1435   if (address_arg != NULL && source_arg != NULL)
1436     error (_("Must specify at most one of -pc and -source"));
1437 
1438   stdio_file arg_outfile;
1439 
1440   if (argv != NULL && argv[outfile_idx] != NULL)
1441     {
1442       if (argv[outfile_idx + 1] != NULL)
1443 	error (_("Junk at end of command"));
1444       gdb::unique_xmalloc_ptr<char> outfile_name
1445 	(tilde_expand (argv[outfile_idx]));
1446       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1447 	perror_with_name (outfile_name.get ());
1448       outfile = &arg_outfile;
1449     }
1450 
1451   if (address_arg != NULL)
1452     {
1453       pc = parse_and_eval_address (address_arg);
1454       /* If we fail to find a section, that's ok, try the lookup anyway.  */
1455       section = find_pc_section (pc);
1456     }
1457 
1458   found = 0;
1459   for (objfile *objfile : current_program_space->objfiles ())
1460     {
1461       int printed_objfile_header = 0;
1462       int print_for_objfile = 1;
1463 
1464       QUIT;
1465       if (objfile_arg != NULL)
1466 	print_for_objfile
1467 	  = compare_filenames_for_search (objfile_name (objfile),
1468 					  objfile_arg);
1469       if (!print_for_objfile)
1470 	continue;
1471 
1472       for (const auto &iter : objfile->qf)
1473 	{
1474 	  psymbol_functions *psf
1475 	    = dynamic_cast<psymbol_functions *> (iter.get ());
1476 	  if (psf == nullptr)
1477 	    continue;
1478 
1479 	  if (address_arg != NULL)
1480 	    {
1481 	      struct bound_minimal_symbol msymbol;
1482 
1483 	      /* We don't assume each pc has a unique objfile (this is for
1484 		 debugging).  */
1485 	      struct partial_symtab *ps
1486 		= psf->find_pc_sect_psymtab (objfile, pc, section, msymbol);
1487 	      if (ps != NULL)
1488 		{
1489 		  if (!printed_objfile_header)
1490 		    {
1491 		      outfile->printf ("\nPartial symtabs for objfile %s\n",
1492 				       objfile_name (objfile));
1493 		      printed_objfile_header = 1;
1494 		    }
1495 		  dump_psymtab (objfile, ps, outfile);
1496 		  found = 1;
1497 		}
1498 	    }
1499 	  else
1500 	    {
1501 	      for (partial_symtab *ps : psf->partial_symbols (objfile))
1502 		{
1503 		  int print_for_source = 0;
1504 
1505 		  QUIT;
1506 		  if (source_arg != NULL)
1507 		    {
1508 		      print_for_source
1509 			= compare_filenames_for_search (ps->filename, source_arg);
1510 		      found = 1;
1511 		    }
1512 		  if (source_arg == NULL
1513 		      || print_for_source)
1514 		    {
1515 		      if (!printed_objfile_header)
1516 			{
1517 			  outfile->printf ("\nPartial symtabs for objfile %s\n",
1518 					   objfile_name (objfile));
1519 			  printed_objfile_header = 1;
1520 			}
1521 		      dump_psymtab (objfile, ps, outfile);
1522 		    }
1523 		}
1524 	    }
1525 	}
1526     }
1527 
1528   if (!found)
1529     {
1530       if (address_arg != NULL)
1531 	error (_("No partial symtab for address: %s"), address_arg);
1532       if (source_arg != NULL)
1533 	error (_("No partial symtab for source file: %s"), source_arg);
1534     }
1535 }
1536 
1537 /* List all the partial symbol tables whose names match REGEXP (optional).  */
1538 
1539 static void
1540 maintenance_info_psymtabs (const char *regexp, int from_tty)
1541 {
1542   if (regexp)
1543     re_comp (regexp);
1544 
1545   for (struct program_space *pspace : program_spaces)
1546     for (objfile *objfile : pspace->objfiles ())
1547       {
1548 	struct gdbarch *gdbarch = objfile->arch ();
1549 
1550 	/* We don't want to print anything for this objfile until we
1551 	   actually find a symtab whose name matches.  */
1552 	int printed_objfile_start = 0;
1553 
1554 	for (const auto &iter : objfile->qf)
1555 	  {
1556 	    psymbol_functions *psf
1557 	      = dynamic_cast<psymbol_functions *> (iter.get ());
1558 	    if (psf == nullptr)
1559 	      continue;
1560 	    for (partial_symtab *psymtab : psf->partial_symbols (objfile))
1561 	      {
1562 		QUIT;
1563 
1564 		if (! regexp
1565 		    || re_exec (psymtab->filename))
1566 		  {
1567 		    if (! printed_objfile_start)
1568 		      {
1569 			gdb_printf ("{ objfile %s ", objfile_name (objfile));
1570 			gdb_stdout->wrap_here (2);
1571 			gdb_printf ("((struct objfile *) %s)\n",
1572 				    host_address_to_string (objfile));
1573 			printed_objfile_start = 1;
1574 		      }
1575 
1576 		    gdb_printf ("  { psymtab %s ", psymtab->filename);
1577 		    gdb_stdout->wrap_here (4);
1578 		    gdb_printf ("((struct partial_symtab *) %s)\n",
1579 				host_address_to_string (psymtab));
1580 
1581 		    gdb_printf ("    readin %s\n",
1582 				psymtab->readin_p (objfile) ? "yes" : "no");
1583 		    gdb_printf ("    fullname %s\n",
1584 				psymtab->fullname
1585 				? psymtab->fullname : "(null)");
1586 		    gdb_printf ("    text addresses ");
1587 		    gdb_puts (paddress (gdbarch,
1588 					psymtab->text_low (objfile)));
1589 		    gdb_printf (" -- ");
1590 		    gdb_puts (paddress (gdbarch,
1591 					psymtab->text_high (objfile)));
1592 		    gdb_printf ("\n");
1593 		    gdb_printf ("    globals ");
1594 		    if (!psymtab->global_psymbols.empty ())
1595 		      gdb_printf
1596 			("(* (struct partial_symbol **) %s @ %d)\n",
1597 			 host_address_to_string (psymtab->global_psymbols.data ()),
1598 			 (int) psymtab->global_psymbols.size ());
1599 		    else
1600 		      gdb_printf ("(none)\n");
1601 		    gdb_printf ("    statics ");
1602 		    if (!psymtab->static_psymbols.empty ())
1603 		      gdb_printf
1604 			("(* (struct partial_symbol **) %s @ %d)\n",
1605 			 host_address_to_string (psymtab->static_psymbols.data ()),
1606 			 (int) psymtab->static_psymbols.size ());
1607 		    else
1608 		      gdb_printf ("(none)\n");
1609 		    if (psymtab->user)
1610 		      gdb_printf ("    user %s "
1611 				  "((struct partial_symtab *) %s)\n",
1612 				  psymtab->user->filename,
1613 				  host_address_to_string (psymtab->user));
1614 		    gdb_printf ("    dependencies ");
1615 		    if (psymtab->number_of_dependencies)
1616 		      {
1617 			int i;
1618 
1619 			gdb_printf ("{\n");
1620 			for (i = 0; i < psymtab->number_of_dependencies; i++)
1621 			  {
1622 			    struct partial_symtab *dep = psymtab->dependencies[i];
1623 
1624 			    /* Note the string concatenation there --- no
1625 			       comma.  */
1626 			    gdb_printf ("      psymtab %s "
1627 					"((struct partial_symtab *) %s)\n",
1628 					dep->filename,
1629 					host_address_to_string (dep));
1630 			  }
1631 			gdb_printf ("    }\n");
1632 		      }
1633 		    else
1634 		      gdb_printf ("(none)\n");
1635 		    gdb_printf ("  }\n");
1636 		  }
1637 	      }
1638 	  }
1639 
1640 	if (printed_objfile_start)
1641 	  gdb_printf ("}\n");
1642       }
1643 }
1644 
1645 /* Check consistency of currently expanded psymtabs vs symtabs.  */
1646 
1647 static void
1648 maintenance_check_psymtabs (const char *ignore, int from_tty)
1649 {
1650   struct symbol *sym;
1651   struct compunit_symtab *cust = NULL;
1652   const struct blockvector *bv;
1653   const struct block *b;
1654 
1655   for (objfile *objfile : current_program_space->objfiles ())
1656     {
1657       for (const auto &iter : objfile->qf)
1658 	{
1659 	  psymbol_functions *psf
1660 	    = dynamic_cast<psymbol_functions *> (iter.get ());
1661 	  if (psf == nullptr)
1662 	    continue;
1663 
1664 	  for (partial_symtab *ps : psf->partial_symbols (objfile))
1665 	    {
1666 	      struct gdbarch *gdbarch = objfile->arch ();
1667 
1668 	      /* We don't call psymtab_to_symtab here because that may cause symtab
1669 		 expansion.  When debugging a problem it helps if checkers leave
1670 		 things unchanged.  */
1671 	      cust = ps->get_compunit_symtab (objfile);
1672 
1673 	      /* First do some checks that don't require the associated symtab.  */
1674 	      if (ps->text_high (objfile) < ps->text_low (objfile))
1675 		{
1676 		  gdb_printf ("Psymtab ");
1677 		  gdb_puts (ps->filename);
1678 		  gdb_printf (" covers bad range ");
1679 		  gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1680 		  gdb_printf (" - ");
1681 		  gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1682 		  gdb_printf ("\n");
1683 		  continue;
1684 		}
1685 
1686 	      /* Now do checks requiring the associated symtab.  */
1687 	      if (cust == NULL)
1688 		continue;
1689 	      bv = cust->blockvector ();
1690 	      b = bv->static_block ();
1691 	      for (partial_symbol *psym : ps->static_psymbols)
1692 		{
1693 		  /* Skip symbols for inlined functions without address.  These may
1694 		     or may not have a match in the full symtab.  */
1695 		  if (psym->aclass == LOC_BLOCK
1696 		      && psym->ginfo.value_address () == 0)
1697 		    continue;
1698 
1699 		  sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1700 					     symbol_name_match_type::SEARCH_NAME,
1701 					     psym->domain);
1702 		  if (!sym)
1703 		    {
1704 		      gdb_printf ("Static symbol `");
1705 		      gdb_puts (psym->ginfo.linkage_name ());
1706 		      gdb_printf ("' only found in ");
1707 		      gdb_puts (ps->filename);
1708 		      gdb_printf (" psymtab\n");
1709 		    }
1710 		}
1711 	      b = bv->global_block ();
1712 	      for (partial_symbol *psym : ps->global_psymbols)
1713 		{
1714 		  sym = block_lookup_symbol (b, psym->ginfo.search_name (),
1715 					     symbol_name_match_type::SEARCH_NAME,
1716 					     psym->domain);
1717 		  if (!sym)
1718 		    {
1719 		      gdb_printf ("Global symbol `");
1720 		      gdb_puts (psym->ginfo.linkage_name ());
1721 		      gdb_printf ("' only found in ");
1722 		      gdb_puts (ps->filename);
1723 		      gdb_printf (" psymtab\n");
1724 		    }
1725 		}
1726 	      if (ps->raw_text_high () != 0
1727 		  && (ps->text_low (objfile) < b->start ()
1728 		      || ps->text_high (objfile) > b->end ()))
1729 		{
1730 		  gdb_printf ("Psymtab ");
1731 		  gdb_puts (ps->filename);
1732 		  gdb_printf (" covers ");
1733 		  gdb_puts (paddress (gdbarch, ps->text_low (objfile)));
1734 		  gdb_printf (" - ");
1735 		  gdb_puts (paddress (gdbarch, ps->text_high (objfile)));
1736 		  gdb_printf (" but symtab covers only ");
1737 		  gdb_puts (paddress (gdbarch, b->start ()));
1738 		  gdb_printf (" - ");
1739 		  gdb_puts (paddress (gdbarch, b->end ()));
1740 		  gdb_printf ("\n");
1741 		}
1742 	    }
1743 	}
1744     }
1745 }
1746 
1747 void _initialize_psymtab ();
1748 void
1749 _initialize_psymtab ()
1750 {
1751   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
1752 Print dump of current partial symbol definitions.\n\
1753 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
1754        mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1755 Entries in the partial symbol table are dumped to file OUTFILE,\n\
1756 or the terminal if OUTFILE is unspecified.\n\
1757 If ADDRESS is provided, dump only the file for that address.\n\
1758 If SOURCE is provided, dump only that file's symbols.\n\
1759 If OBJFILE is provided, dump only that file's minimal symbols."),
1760 	   &maintenanceprintlist);
1761 
1762   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
1763 List the partial symbol tables for all object files.\n\
1764 This does not include information about individual partial symbols,\n\
1765 just the symbol table structures themselves."),
1766 	   &maintenanceinfolist);
1767 
1768   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
1769 	   _("\
1770 Check consistency of currently expanded psymtabs versus symtabs."),
1771 	   &maintenancelist);
1772 }
1773