xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/psymtab.c (revision b5c47949a45ac972130c38cf13dfd8afb1f09285)
1 /* Partial symbol tables.
2 
3    Copyright (C) 2009-2019 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 "bcache.h"
30 #include "ui-out.h"
31 #include "command.h"
32 #include "readline/readline.h"
33 #include "gdb_regex.h"
34 #include "dictionary.h"
35 #include "language.h"
36 #include "cp-support.h"
37 #include "gdbcmd.h"
38 #include <algorithm>
39 #include <set>
40 
41 struct psymbol_bcache
42 {
43   struct bcache *bcache;
44 };
45 
46 static struct partial_symbol *match_partial_symbol (struct objfile *,
47 						    struct partial_symtab *,
48 						    int,
49 						    const char *, domain_enum,
50 						    symbol_name_match_type,
51 						    symbol_compare_ftype *);
52 
53 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
54 						     struct partial_symtab *,
55 						     const char *, int,
56 						     domain_enum);
57 
58 static const char *psymtab_to_fullname (struct partial_symtab *ps);
59 
60 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
61 						    struct partial_symtab *,
62 						    CORE_ADDR,
63 						    struct obj_section *);
64 
65 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
66 						  struct partial_symtab *pst);
67 
68 
69 
70 psymtab_storage::psymtab_storage ()
71   : psymbol_cache (psymbol_bcache_init ())
72 {
73 }
74 
75 psymtab_storage::~psymtab_storage ()
76 {
77   psymbol_bcache_free (psymbol_cache);
78 }
79 
80 /* See psymtab.h.  */
81 
82 struct partial_symtab *
83 psymtab_storage::allocate_psymtab ()
84 {
85   struct partial_symtab *psymtab;
86 
87   if (free_psymtabs != nullptr)
88     {
89       psymtab = free_psymtabs;
90       free_psymtabs = psymtab->next;
91     }
92   else
93     psymtab = XOBNEW (obstack (), struct partial_symtab);
94 
95   memset (psymtab, 0, sizeof (struct partial_symtab));
96 
97   psymtab->next = psymtabs;
98   psymtabs = psymtab;
99 
100   return psymtab;
101 }
102 
103 
104 
105 /* See psymtab.h.  */
106 
107 psymtab_storage::partial_symtab_range
108 require_partial_symbols (struct objfile *objfile, int verbose)
109 {
110   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
111     {
112       objfile->flags |= OBJF_PSYMTABS_READ;
113 
114       if (objfile->sf->sym_read_psymbols)
115 	{
116 	  if (verbose)
117 	    printf_filtered (_("Reading symbols from %s...\n"),
118 			     objfile_name (objfile));
119 	  (*objfile->sf->sym_read_psymbols) (objfile);
120 
121 	  /* Partial symbols list are not expected to changed after this
122 	     point.  */
123 	  objfile->partial_symtabs->global_psymbols.shrink_to_fit ();
124 	  objfile->partial_symtabs->static_psymbols.shrink_to_fit ();
125 
126 	  if (verbose && !objfile_has_symbols (objfile))
127 	    printf_filtered (_("(No debugging symbols found in %s)\n"),
128 			     objfile_name (objfile));
129 	}
130     }
131 
132   return objfile->psymtabs ();
133 }
134 
135 /* Helper function for psym_map_symtabs_matching_filename that
136    expands the symtabs and calls the iterator.  */
137 
138 static bool
139 partial_map_expand_apply (struct objfile *objfile,
140 			  const char *name,
141 			  const char *real_path,
142 			  struct partial_symtab *pst,
143 			  gdb::function_view<bool (symtab *)> callback)
144 {
145   struct compunit_symtab *last_made = objfile->compunit_symtabs;
146 
147   /* Shared psymtabs should never be seen here.  Instead they should
148      be handled properly by the caller.  */
149   gdb_assert (pst->user == NULL);
150 
151   /* Don't visit already-expanded psymtabs.  */
152   if (pst->readin)
153     return 0;
154 
155   /* This may expand more than one symtab, and we want to iterate over
156      all of them.  */
157   psymtab_to_symtab (objfile, pst);
158 
159   return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
160 				    last_made, callback);
161 }
162 
163 /*  Psymtab version of map_symtabs_matching_filename.  See its definition in
164     the definition of quick_symbol_functions in symfile.h.  */
165 
166 static bool
167 psym_map_symtabs_matching_filename
168   (struct objfile *objfile,
169    const char *name,
170    const char *real_path,
171    gdb::function_view<bool (symtab *)> callback)
172 {
173   const char *name_basename = lbasename (name);
174 
175   for (partial_symtab *pst : require_partial_symbols (objfile, 1))
176     {
177       /* We can skip shared psymtabs here, because any file name will be
178 	 attached to the unshared psymtab.  */
179       if (pst->user != NULL)
180 	continue;
181 
182       /* Anonymous psymtabs don't have a file name.  */
183       if (pst->anonymous)
184 	continue;
185 
186       if (compare_filenames_for_search (pst->filename, name))
187 	{
188 	  if (partial_map_expand_apply (objfile, name, real_path,
189 					pst, callback))
190 	    return true;
191 	  continue;
192 	}
193 
194       /* Before we invoke realpath, which can get expensive when many
195 	 files are involved, do a quick comparison of the basenames.  */
196       if (! basenames_may_differ
197 	  && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
198 	continue;
199 
200       if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
201 	{
202 	  if (partial_map_expand_apply (objfile, name, real_path,
203 					pst, callback))
204 	    return true;
205 	  continue;
206 	}
207 
208       /* If the user gave us an absolute path, try to find the file in
209 	 this symtab and use its absolute path.  */
210       if (real_path != NULL)
211 	{
212 	  gdb_assert (IS_ABSOLUTE_PATH (real_path));
213 	  gdb_assert (IS_ABSOLUTE_PATH (name));
214 	  if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
215 	    {
216 	      if (partial_map_expand_apply (objfile, name, real_path,
217 					    pst, callback))
218 		return true;
219 	      continue;
220 	    }
221 	}
222     }
223 
224   return false;
225 }
226 
227 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
228    We may find a different psymtab than PST.  See FIND_PC_SECT_PSYMTAB.  */
229 
230 static struct partial_symtab *
231 find_pc_sect_psymtab_closer (struct objfile *objfile,
232 			     CORE_ADDR pc, struct obj_section *section,
233 			     struct partial_symtab *pst,
234 			     struct bound_minimal_symbol msymbol)
235 {
236   struct partial_symtab *tpst;
237   struct partial_symtab *best_pst = pst;
238   CORE_ADDR best_addr = pst->text_low (objfile);
239 
240   gdb_assert (!pst->psymtabs_addrmap_supported);
241 
242   /* An objfile that has its functions reordered might have
243      many partial symbol tables containing the PC, but
244      we want the partial symbol table that contains the
245      function containing the PC.  */
246   if (!(objfile->flags & OBJF_REORDERED)
247       && section == NULL)  /* Can't validate section this way.  */
248     return pst;
249 
250   if (msymbol.minsym == NULL)
251     return pst;
252 
253   /* The code range of partial symtabs sometimes overlap, so, in
254      the loop below, we need to check all partial symtabs and
255      find the one that fits better for the given PC address.  We
256      select the partial symtab that contains a symbol whose
257      address is closest to the PC address.  By closest we mean
258      that find_pc_sect_symbol returns the symbol with address
259      that is closest and still less than the given PC.  */
260   for (tpst = pst; tpst != NULL; tpst = tpst->next)
261     {
262       if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
263 	{
264 	  struct partial_symbol *p;
265 	  CORE_ADDR this_addr;
266 
267 	  /* NOTE: This assumes that every psymbol has a
268 	     corresponding msymbol, which is not necessarily
269 	     true; the debug info might be much richer than the
270 	     object's symbol table.  */
271 	  p = find_pc_sect_psymbol (objfile, tpst, pc, section);
272 	  if (p != NULL
273 	      && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
274 	    return tpst;
275 
276 	  /* Also accept the textlow value of a psymtab as a
277 	     "symbol", to provide some support for partial
278 	     symbol tables with line information but no debug
279 	     symbols (e.g. those produced by an assembler).  */
280 	  if (p != NULL)
281 	    this_addr = p->address (objfile);
282 	  else
283 	    this_addr = tpst->text_low (objfile);
284 
285 	  /* Check whether it is closer than our current
286 	     BEST_ADDR.  Since this symbol address is
287 	     necessarily lower or equal to PC, the symbol closer
288 	     to PC is the symbol which address is the highest.
289 	     This way we return the psymtab which contains such
290 	     best match symbol.  This can help in cases where the
291 	     symbol information/debuginfo is not complete, like
292 	     for instance on IRIX6 with gcc, where no debug info
293 	     is emitted for statics.  (See also the nodebug.exp
294 	     testcase.)  */
295 	  if (this_addr > best_addr)
296 	    {
297 	      best_addr = this_addr;
298 	      best_pst = tpst;
299 	    }
300 	}
301     }
302   return best_pst;
303 }
304 
305 /* Find which partial symtab contains PC and SECTION.  Return NULL if
306    none.  We return the psymtab that contains a symbol whose address
307    exactly matches PC, or, if we cannot find an exact match, the
308    psymtab that contains a symbol whose address is closest to PC.  */
309 
310 static struct partial_symtab *
311 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
312 		      struct obj_section *section,
313 		      struct bound_minimal_symbol msymbol)
314 {
315   CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
316 				 SECT_OFF_TEXT (objfile));
317 
318   /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
319      than the later used TEXTLOW/TEXTHIGH one.  */
320 
321   if (objfile->partial_symtabs->psymtabs_addrmap != NULL)
322     {
323       struct partial_symtab *pst
324 	= ((struct partial_symtab *)
325 	   addrmap_find (objfile->partial_symtabs->psymtabs_addrmap,
326 			 pc - baseaddr));
327       if (pst != NULL)
328 	{
329 	  /* FIXME: addrmaps currently do not handle overlayed sections,
330 	     so fall back to the non-addrmap case if we're debugging
331 	     overlays and the addrmap returned the wrong section.  */
332 	  if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
333 	    {
334 	      struct partial_symbol *p;
335 
336 	      /* NOTE: This assumes that every psymbol has a
337 		 corresponding msymbol, which is not necessarily
338 		 true; the debug info might be much richer than the
339 		 object's symbol table.  */
340 	      p = find_pc_sect_psymbol (objfile, pst, pc, section);
341 	      if (p == NULL
342 		  || (p->address (objfile)
343 		      != BMSYMBOL_VALUE_ADDRESS (msymbol)))
344 		goto next;
345 	    }
346 
347 	  /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
348 	     PSYMTABS_ADDRMAP we used has already the best 1-byte
349 	     granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
350 	     a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
351 	     overlap.  */
352 
353 	  return pst;
354 	}
355     }
356 
357  next:
358 
359   /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
360      which still have no corresponding full SYMTABs read.  But it is not
361      present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
362      so far.  */
363 
364   /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
365      its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
366      debug info type in single OBJFILE.  */
367 
368   for (partial_symtab *pst : require_partial_symbols (objfile, 1))
369     if (!pst->psymtabs_addrmap_supported
370 	&& pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
371       {
372 	struct partial_symtab *best_pst;
373 
374 	best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
375 						msymbol);
376 	if (best_pst != NULL)
377 	  return best_pst;
378       }
379 
380   return NULL;
381 }
382 
383 /* Psymtab version of find_pc_sect_compunit_symtab.  See its definition in
384    the definition of quick_symbol_functions in symfile.h.  */
385 
386 static struct compunit_symtab *
387 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
388 				   struct bound_minimal_symbol msymbol,
389 				   CORE_ADDR pc,
390 				   struct obj_section *section,
391 				   int warn_if_readin)
392 {
393   struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
394 						    msymbol);
395   if (ps != NULL)
396     {
397       if (warn_if_readin && ps->readin)
398 	/* Might want to error() here (in case symtab is corrupt and
399 	   will cause a core dump), but maybe we can successfully
400 	   continue, so let's not.  */
401 	warning (_("\
402 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
403 		 paddress (get_objfile_arch (objfile), pc));
404       psymtab_to_symtab (objfile, ps);
405       return ps->compunit_symtab;
406     }
407   return NULL;
408 }
409 
410 /* Find which partial symbol within a psymtab matches PC and SECTION.
411    Return NULL if none.  */
412 
413 static struct partial_symbol *
414 find_pc_sect_psymbol (struct objfile *objfile,
415 		      struct partial_symtab *psymtab, CORE_ADDR pc,
416 		      struct obj_section *section)
417 {
418   struct partial_symbol *best = NULL;
419   CORE_ADDR best_pc;
420   const CORE_ADDR textlow = psymtab->text_low (objfile);
421 
422   gdb_assert (psymtab != NULL);
423 
424   /* Cope with programs that start at address 0.  */
425   best_pc = (textlow != 0) ? textlow - 1 : 0;
426 
427   /* Search the global symbols as well as the static symbols, so that
428      find_pc_partial_function doesn't use a minimal symbol and thus
429      cache a bad endaddr.  */
430   for (int i = 0; i < psymtab->n_global_syms; i++)
431     {
432       partial_symbol *p
433 	= objfile->partial_symtabs->global_psymbols[psymtab->globals_offset
434 						    + i];
435 
436       if (p->domain == VAR_DOMAIN
437 	  && p->aclass == LOC_BLOCK
438 	  && pc >= p->address (objfile)
439 	  && (p->address (objfile) > best_pc
440 	      || (psymtab->text_low (objfile) == 0
441 		  && best_pc == 0 && p->address (objfile) == 0)))
442 	{
443 	  if (section != NULL)  /* Match on a specific section.  */
444 	    {
445 	      if (!matching_obj_sections (p->obj_section (objfile),
446 					  section))
447 		continue;
448 	    }
449 	  best_pc = p->address (objfile);
450 	  best = p;
451 	}
452     }
453 
454   for (int i = 0; i < psymtab->n_static_syms; i++)
455     {
456       partial_symbol *p
457 	= objfile->partial_symtabs->static_psymbols[psymtab->statics_offset
458 						    + i];
459 
460       if (p->domain == VAR_DOMAIN
461 	  && p->aclass == LOC_BLOCK
462 	  && pc >= p->address (objfile)
463 	  && (p->address (objfile) > best_pc
464 	      || (psymtab->text_low (objfile) == 0
465 		  && best_pc == 0 && p->address (objfile) == 0)))
466 	{
467 	  if (section != NULL)  /* Match on a specific section.  */
468 	    {
469 	      if (!matching_obj_sections (p->obj_section (objfile),
470 					  section))
471 		continue;
472 	    }
473 	  best_pc = p->address (objfile);
474 	  best = p;
475 	}
476     }
477 
478   return best;
479 }
480 
481 /* Psymtab version of lookup_symbol.  See its definition in
482    the definition of quick_symbol_functions in symfile.h.  */
483 
484 static struct compunit_symtab *
485 psym_lookup_symbol (struct objfile *objfile,
486 		    int block_index, const char *name,
487 		    const domain_enum domain)
488 {
489   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
490   struct compunit_symtab *stab_best = NULL;
491 
492   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
493 
494   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
495     {
496       if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
497 						psymtab_index, domain))
498 	{
499 	  struct symbol *sym, *with_opaque = NULL;
500 	  struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
501 	  /* Note: While psymtab_to_symtab can return NULL if the
502 	     partial symtab is empty, we can assume it won't here
503 	     because lookup_partial_symbol succeeded.  */
504 	  const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
505 	  struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
506 
507 	  sym = block_find_symbol (block, name, domain,
508 				   block_find_non_opaque_type_preferred,
509 				   &with_opaque);
510 
511 	  /* Some caution must be observed with overloaded functions
512 	     and methods, since the index will not contain any overload
513 	     information (but NAME might contain it).  */
514 
515 	  if (sym != NULL
516 	      && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
517 	    return stab;
518 	  if (with_opaque != NULL
519 	      && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
520 	    stab_best = stab;
521 
522 	  /* Keep looking through other psymtabs.  */
523 	}
524     }
525 
526   return stab_best;
527 }
528 
529 /* Returns true if PSYM matches LOOKUP_NAME.  */
530 
531 static bool
532 psymbol_name_matches (partial_symbol *psym,
533 		      const lookup_name_info &lookup_name)
534 {
535   const language_defn *lang = language_def (psym->ginfo.language);
536   symbol_name_matcher_ftype *name_match
537     = get_symbol_name_matcher (lang, lookup_name);
538   return name_match (symbol_search_name (&psym->ginfo), lookup_name, NULL);
539 }
540 
541 /* Look in PST for a symbol in DOMAIN whose name matches NAME.  Search
542    the global block of PST if GLOBAL, and otherwise the static block.
543    MATCH is the comparison operation that returns true iff MATCH (s,
544    NAME), where s is a SYMBOL_SEARCH_NAME.  If ORDERED_COMPARE is
545    non-null, the symbols in the block are assumed to be ordered
546    according to it (allowing binary search).  It must be compatible
547    with MATCH.  Returns the symbol, if found, and otherwise NULL.  */
548 
549 static struct partial_symbol *
550 match_partial_symbol (struct objfile *objfile,
551 		      struct partial_symtab *pst, int global,
552 		      const char *name, domain_enum domain,
553 		      symbol_name_match_type match_type,
554 		      symbol_compare_ftype *ordered_compare)
555 {
556   struct partial_symbol **start, **psym;
557   struct partial_symbol **top, **real_top, **bottom, **center;
558   int length = (global ? pst->n_global_syms : pst->n_static_syms);
559   int do_linear_search = 1;
560 
561   if (length == 0)
562     return NULL;
563 
564   lookup_name_info lookup_name (name, match_type);
565 
566   start = (global ?
567 	   &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
568 	   &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
569 
570   if (global && ordered_compare)  /* Can use a binary search.  */
571     {
572       do_linear_search = 0;
573 
574       /* Binary search.  This search is guaranteed to end with center
575          pointing at the earliest partial symbol whose name might be
576          correct.  At that point *all* partial symbols with an
577          appropriate name will be checked against the correct
578          domain.  */
579 
580       bottom = start;
581       top = start + length - 1;
582       real_top = top;
583       while (top > bottom)
584 	{
585 	  center = bottom + (top - bottom) / 2;
586 	  gdb_assert (center < top);
587 
588 	  enum language lang = (*center)->ginfo.language;
589 	  const char *lang_ln
590 	    = lookup_name.language_lookup_name (lang).c_str ();
591 
592 	  if (ordered_compare (symbol_search_name (&(*center)->ginfo),
593 			       lang_ln) >= 0)
594 	    top = center;
595 	  else
596 	    bottom = center + 1;
597 	}
598       gdb_assert (top == bottom);
599 
600       while (top <= real_top
601 	     && psymbol_name_matches (*top, lookup_name))
602 	{
603 	  if (symbol_matches_domain ((*top)->ginfo.language,
604 				     (*top)->domain, domain))
605 	    return *top;
606 	  top++;
607 	}
608     }
609 
610   /* Can't use a binary search or else we found during the binary search that
611      we should also do a linear search.  */
612 
613   if (do_linear_search)
614     {
615       for (psym = start; psym < start + length; psym++)
616 	{
617 	  if (symbol_matches_domain ((*psym)->ginfo.language,
618 				     (*psym)->domain, domain)
619 	      && psymbol_name_matches (*psym, lookup_name))
620 	    return *psym;
621 	}
622     }
623 
624   return NULL;
625 }
626 
627 /* Returns the name used to search psymtabs.  Unlike symtabs, psymtabs do
628    not contain any method/function instance information (since this would
629    force reading type information while reading psymtabs).  Therefore,
630    if NAME contains overload information, it must be stripped before searching
631    psymtabs.  */
632 
633 static gdb::unique_xmalloc_ptr<char>
634 psymtab_search_name (const char *name)
635 {
636   switch (current_language->la_language)
637     {
638     case language_cplus:
639       {
640 	if (strchr (name, '('))
641 	  {
642 	    gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
643 
644 	    if (ret)
645 	      return ret;
646 	  }
647       }
648       break;
649 
650     default:
651       break;
652     }
653 
654   return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
655 }
656 
657 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
658    Check the global symbols if GLOBAL, the static symbols if not.  */
659 
660 static struct partial_symbol *
661 lookup_partial_symbol (struct objfile *objfile,
662 		       struct partial_symtab *pst, const char *name,
663 		       int global, domain_enum domain)
664 {
665   struct partial_symbol **start, **psym;
666   struct partial_symbol **top, **real_top, **bottom, **center;
667   int length = (global ? pst->n_global_syms : pst->n_static_syms);
668   int do_linear_search = 1;
669 
670   if (length == 0)
671     return NULL;
672 
673   gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
674 
675   lookup_name_info lookup_name (search_name.get (), symbol_name_match_type::FULL);
676 
677   start = (global ?
678 	   &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
679 	   &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
680 
681   if (global)			/* This means we can use a binary search.  */
682     {
683       do_linear_search = 0;
684 
685       /* Binary search.  This search is guaranteed to end with center
686          pointing at the earliest partial symbol whose name might be
687          correct.  At that point *all* partial symbols with an
688          appropriate name will be checked against the correct
689          domain.  */
690 
691       bottom = start;
692       top = start + length - 1;
693       real_top = top;
694       while (top > bottom)
695 	{
696 	  center = bottom + (top - bottom) / 2;
697 	  if (!(center < top))
698 	    internal_error (__FILE__, __LINE__,
699 			    _("failed internal consistency check"));
700 	  if (strcmp_iw_ordered (symbol_search_name (&(*center)->ginfo),
701 				 search_name.get ()) >= 0)
702 	    {
703 	      top = center;
704 	    }
705 	  else
706 	    {
707 	      bottom = center + 1;
708 	    }
709 	}
710       if (!(top == bottom))
711 	internal_error (__FILE__, __LINE__,
712 			_("failed internal consistency check"));
713 
714       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
715 	 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
716       while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
717 							 lookup_name))
718 	top--;
719 
720       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
721       top++;
722 
723       while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
724 							    lookup_name))
725 	{
726 	  if (symbol_matches_domain ((*top)->ginfo.language,
727 				     (*top)->domain, domain))
728 	    return *top;
729 	  top++;
730 	}
731     }
732 
733   /* Can't use a binary search or else we found during the binary search that
734      we should also do a linear search.  */
735 
736   if (do_linear_search)
737     {
738       for (psym = start; psym < start + length; psym++)
739 	{
740 	  if (symbol_matches_domain ((*psym)->ginfo.language,
741 				     (*psym)->domain, domain)
742 	      && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
743 	    return *psym;
744 	}
745     }
746 
747   return NULL;
748 }
749 
750 /* Get the symbol table that corresponds to a partial_symtab.
751    This is fast after the first time you do it.
752    The result will be NULL if the primary symtab has no symbols,
753    which can happen.  Otherwise the result is the primary symtab
754    that contains PST.  */
755 
756 static struct compunit_symtab *
757 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
758 {
759   /* If it is a shared psymtab, find an unshared psymtab that includes
760      it.  Any such psymtab will do.  */
761   while (pst->user != NULL)
762     pst = pst->user;
763 
764   /* If it's been looked up before, return it.  */
765   if (pst->compunit_symtab)
766     return pst->compunit_symtab;
767 
768   /* If it has not yet been read in, read it.  */
769   if (!pst->readin)
770     {
771       scoped_restore decrementer = increment_reading_symtab ();
772 
773       (*pst->read_symtab) (pst, objfile);
774     }
775 
776   return pst->compunit_symtab;
777 }
778 
779 /* Psymtab version of find_last_source_symtab.  See its definition in
780    the definition of quick_symbol_functions in symfile.h.  */
781 
782 static struct symtab *
783 psym_find_last_source_symtab (struct objfile *ofp)
784 {
785   struct partial_symtab *cs_pst = NULL;
786 
787   for (partial_symtab *ps : require_partial_symbols (ofp, 1))
788     {
789       const char *name = ps->filename;
790       int len = strlen (name);
791 
792       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
793 			|| strcmp (name, "<<C++-namespaces>>") == 0)))
794 	cs_pst = ps;
795     }
796 
797   if (cs_pst)
798     {
799       if (cs_pst->readin)
800 	{
801 	  internal_error (__FILE__, __LINE__,
802 			  _("select_source_symtab: "
803 			  "readin pst found and no symtabs."));
804 	}
805       else
806 	{
807 	  struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
808 
809 	  if (cust == NULL)
810 	    return NULL;
811 	  return compunit_primary_filetab (cust);
812 	}
813     }
814   return NULL;
815 }
816 
817 /* Psymtab version of forget_cached_source_info.  See its definition in
818    the definition of quick_symbol_functions in symfile.h.  */
819 
820 static void
821 psym_forget_cached_source_info (struct objfile *objfile)
822 {
823   for (partial_symtab *pst : require_partial_symbols (objfile, 1))
824     {
825       if (pst->fullname != NULL)
826 	{
827 	  xfree (pst->fullname);
828 	  pst->fullname = NULL;
829 	}
830     }
831 }
832 
833 static void
834 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
835 		       struct partial_symbol **p, int count, const char *what,
836 		       struct ui_file *outfile)
837 {
838   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
839   while (count-- > 0)
840     {
841       QUIT;
842       fprintf_filtered (outfile, "    `%s'", (*p)->ginfo.name);
843       if (symbol_demangled_name (&(*p)->ginfo) != NULL)
844 	{
845 	  fprintf_filtered (outfile, "  `%s'",
846 			    symbol_demangled_name (&(*p)->ginfo));
847 	}
848       fputs_filtered (", ", outfile);
849       switch ((*p)->domain)
850 	{
851 	case UNDEF_DOMAIN:
852 	  fputs_filtered ("undefined domain, ", outfile);
853 	  break;
854 	case VAR_DOMAIN:
855 	  /* This is the usual thing -- don't print it.  */
856 	  break;
857 	case STRUCT_DOMAIN:
858 	  fputs_filtered ("struct domain, ", outfile);
859 	  break;
860 	case LABEL_DOMAIN:
861 	  fputs_filtered ("label domain, ", outfile);
862 	  break;
863 	default:
864 	  fputs_filtered ("<invalid domain>, ", outfile);
865 	  break;
866 	}
867       switch ((*p)->aclass)
868 	{
869 	case LOC_UNDEF:
870 	  fputs_filtered ("undefined", outfile);
871 	  break;
872 	case LOC_CONST:
873 	  fputs_filtered ("constant int", outfile);
874 	  break;
875 	case LOC_STATIC:
876 	  fputs_filtered ("static", outfile);
877 	  break;
878 	case LOC_REGISTER:
879 	  fputs_filtered ("register", outfile);
880 	  break;
881 	case LOC_ARG:
882 	  fputs_filtered ("pass by value", outfile);
883 	  break;
884 	case LOC_REF_ARG:
885 	  fputs_filtered ("pass by reference", outfile);
886 	  break;
887 	case LOC_REGPARM_ADDR:
888 	  fputs_filtered ("register address parameter", outfile);
889 	  break;
890 	case LOC_LOCAL:
891 	  fputs_filtered ("stack parameter", outfile);
892 	  break;
893 	case LOC_TYPEDEF:
894 	  fputs_filtered ("type", outfile);
895 	  break;
896 	case LOC_LABEL:
897 	  fputs_filtered ("label", outfile);
898 	  break;
899 	case LOC_BLOCK:
900 	  fputs_filtered ("function", outfile);
901 	  break;
902 	case LOC_CONST_BYTES:
903 	  fputs_filtered ("constant bytes", outfile);
904 	  break;
905 	case LOC_UNRESOLVED:
906 	  fputs_filtered ("unresolved", outfile);
907 	  break;
908 	case LOC_OPTIMIZED_OUT:
909 	  fputs_filtered ("optimized out", outfile);
910 	  break;
911 	case LOC_COMPUTED:
912 	  fputs_filtered ("computed at runtime", outfile);
913 	  break;
914 	default:
915 	  fputs_filtered ("<invalid location>", outfile);
916 	  break;
917 	}
918       fputs_filtered (", ", outfile);
919       fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
920       fprintf_filtered (outfile, "\n");
921       p++;
922     }
923 }
924 
925 static void
926 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
927 	      struct ui_file *outfile)
928 {
929   struct gdbarch *gdbarch = get_objfile_arch (objfile);
930   int i;
931 
932   if (psymtab->anonymous)
933     {
934       fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
935 			psymtab->filename);
936     }
937   else
938     {
939       fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
940 			psymtab->filename);
941     }
942   fprintf_filtered (outfile, "(object ");
943   gdb_print_host_address (psymtab, outfile);
944   fprintf_filtered (outfile, ")\n\n");
945   fprintf_filtered (outfile, "  Read from object file %s (",
946 		    objfile_name (objfile));
947   gdb_print_host_address (objfile, outfile);
948   fprintf_filtered (outfile, ")\n");
949 
950   if (psymtab->readin)
951     {
952       fprintf_filtered (outfile,
953 			"  Full symtab was read (at ");
954       gdb_print_host_address (psymtab->compunit_symtab, outfile);
955       fprintf_filtered (outfile, " by function at ");
956       gdb_print_host_address (psymtab->read_symtab, outfile);
957       fprintf_filtered (outfile, ")\n");
958     }
959 
960   fprintf_filtered (outfile, "  Symbols cover text addresses ");
961   fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
962   fprintf_filtered (outfile, "-");
963   fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
964   fprintf_filtered (outfile, "\n");
965   fprintf_filtered (outfile, "  Address map supported - %s.\n",
966 		    psymtab->psymtabs_addrmap_supported ? "yes" : "no");
967   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
968 		    psymtab->number_of_dependencies);
969   for (i = 0; i < psymtab->number_of_dependencies; i++)
970     {
971       fprintf_filtered (outfile, "    %d ", i);
972       gdb_print_host_address (psymtab->dependencies[i], outfile);
973       fprintf_filtered (outfile, " %s\n",
974 			psymtab->dependencies[i]->filename);
975     }
976   if (psymtab->user != NULL)
977     {
978       fprintf_filtered (outfile, "  Shared partial symtab with user ");
979       gdb_print_host_address (psymtab->user, outfile);
980       fprintf_filtered (outfile, "\n");
981     }
982   if (psymtab->n_global_syms > 0)
983     {
984       print_partial_symbols
985 	(gdbarch, objfile,
986 	 &objfile->partial_symtabs->global_psymbols[psymtab->globals_offset],
987 	 psymtab->n_global_syms, "Global", outfile);
988     }
989   if (psymtab->n_static_syms > 0)
990     {
991       print_partial_symbols
992 	(gdbarch, objfile,
993 	 &objfile->partial_symtabs->static_psymbols[psymtab->statics_offset],
994 	 psymtab->n_static_syms, "Static", outfile);
995     }
996   fprintf_filtered (outfile, "\n");
997 }
998 
999 /* Psymtab version of print_stats.  See its definition in
1000    the definition of quick_symbol_functions in symfile.h.  */
1001 
1002 static void
1003 psym_print_stats (struct objfile *objfile)
1004 {
1005   int i;
1006 
1007   i = 0;
1008   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1009     {
1010       if (ps->readin == 0)
1011 	i++;
1012     }
1013   printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
1014 }
1015 
1016 /* Psymtab version of dump.  See its definition in
1017    the definition of quick_symbol_functions in symfile.h.  */
1018 
1019 static void
1020 psym_dump (struct objfile *objfile)
1021 {
1022   struct partial_symtab *psymtab;
1023 
1024   if (objfile->partial_symtabs->psymtabs)
1025     {
1026       printf_filtered ("Psymtabs:\n");
1027       for (psymtab = objfile->partial_symtabs->psymtabs;
1028 	   psymtab != NULL;
1029 	   psymtab = psymtab->next)
1030 	{
1031 	  printf_filtered ("%s at ",
1032 			   psymtab->filename);
1033 	  gdb_print_host_address (psymtab, gdb_stdout);
1034 	  printf_filtered (", ");
1035 	  wrap_here ("  ");
1036 	}
1037       printf_filtered ("\n\n");
1038     }
1039 }
1040 
1041 /* Psymtab version of expand_symtabs_for_function.  See its definition in
1042    the definition of quick_symbol_functions in symfile.h.  */
1043 
1044 static void
1045 psym_expand_symtabs_for_function (struct objfile *objfile,
1046 				  const char *func_name)
1047 {
1048   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1049     {
1050       if (ps->readin)
1051 	continue;
1052 
1053       if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1054 	   != NULL)
1055 	  || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1056 	      != NULL))
1057 	psymtab_to_symtab (objfile, ps);
1058     }
1059 }
1060 
1061 /* Psymtab version of expand_all_symtabs.  See its definition in
1062    the definition of quick_symbol_functions in symfile.h.  */
1063 
1064 static void
1065 psym_expand_all_symtabs (struct objfile *objfile)
1066 {
1067   for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
1068     psymtab_to_symtab (objfile, psymtab);
1069 }
1070 
1071 /* Psymtab version of expand_symtabs_with_fullname.  See its definition in
1072    the definition of quick_symbol_functions in symfile.h.  */
1073 
1074 static void
1075 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1076 				   const char *fullname)
1077 {
1078   for (partial_symtab *p : require_partial_symbols (objfile, 1))
1079     {
1080       /* Anonymous psymtabs don't have a name of a source file.  */
1081       if (p->anonymous)
1082 	continue;
1083 
1084       /* psymtab_to_fullname tries to open the file which is slow.
1085 	 Don't call it if we know the basenames don't match.  */
1086       if ((basenames_may_differ
1087 	   || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1088 	  && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1089 	psymtab_to_symtab (objfile, p);
1090     }
1091 }
1092 
1093 /* Psymtab version of map_symbol_filenames.  See its definition in
1094    the definition of quick_symbol_functions in symfile.h.  */
1095 
1096 static void
1097 psym_map_symbol_filenames (struct objfile *objfile,
1098 			   symbol_filename_ftype *fun, void *data,
1099 			   int need_fullname)
1100 {
1101   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1102     {
1103       const char *fullname;
1104 
1105       if (ps->readin)
1106 	continue;
1107 
1108       /* We can skip shared psymtabs here, because any file name will be
1109 	 attached to the unshared psymtab.  */
1110       if (ps->user != NULL)
1111 	continue;
1112 
1113       /* Anonymous psymtabs don't have a file name.  */
1114       if (ps->anonymous)
1115 	continue;
1116 
1117       QUIT;
1118       if (need_fullname)
1119 	fullname = psymtab_to_fullname (ps);
1120       else
1121 	fullname = NULL;
1122       (*fun) (ps->filename, fullname, data);
1123     }
1124 }
1125 
1126 /* Finds the fullname that a partial_symtab represents.
1127 
1128    If this functions finds the fullname, it will save it in ps->fullname
1129    and it will also return the value.
1130 
1131    If this function fails to find the file that this partial_symtab represents,
1132    NULL will be returned and ps->fullname will be set to NULL.  */
1133 
1134 static const char *
1135 psymtab_to_fullname (struct partial_symtab *ps)
1136 {
1137   gdb_assert (!ps->anonymous);
1138 
1139   /* Use cached copy if we have it.
1140      We rely on forget_cached_source_info being called appropriately
1141      to handle cases like the file being moved.  */
1142   if (ps->fullname == NULL)
1143     {
1144       gdb::unique_xmalloc_ptr<char> fullname;
1145       scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
1146 					   &fullname);
1147       ps->fullname = fullname.release ();
1148 
1149       if (fd.get () < 0)
1150 	{
1151 	  /* rewrite_source_path would be applied by find_and_open_source, we
1152 	     should report the pathname where GDB tried to find the file.  */
1153 
1154 	  if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1155 	    fullname.reset (xstrdup (ps->filename));
1156 	  else
1157 	    fullname.reset (concat (ps->dirname, SLASH_STRING,
1158 				    ps->filename, (char *) NULL));
1159 
1160 	  ps->fullname = rewrite_source_path (fullname.get ()).release ();
1161 	  if (ps->fullname == NULL)
1162 	    ps->fullname = fullname.release ();
1163 	}
1164     }
1165 
1166   return ps->fullname;
1167 }
1168 
1169 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1170    according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1171    BLOCK is assumed to come from OBJFILE.  Returns 1 iff CALLBACK
1172    ever returns non-zero, and otherwise returns 0.  */
1173 
1174 static int
1175 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1176 	   struct block *block,
1177 	   int (*callback) (struct block *, struct symbol *, void *),
1178 	   void *data, symbol_name_match_type match)
1179 {
1180   struct block_iterator iter;
1181   struct symbol *sym;
1182 
1183   lookup_name_info lookup_name (name, match);
1184 
1185   for (sym = block_iter_match_first (block, lookup_name, &iter);
1186        sym != NULL;
1187        sym = block_iter_match_next (lookup_name, &iter))
1188     {
1189       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1190 				 SYMBOL_DOMAIN (sym), domain))
1191 	{
1192 	  if (callback (block, sym, data))
1193 	    return 1;
1194 	}
1195     }
1196 
1197   return 0;
1198 }
1199 
1200 /* Psymtab version of map_matching_symbols.  See its definition in
1201    the definition of quick_symbol_functions in symfile.h.  */
1202 
1203 static void
1204 psym_map_matching_symbols (struct objfile *objfile,
1205 			   const char *name, domain_enum domain,
1206 			   int global,
1207 			   int (*callback) (struct block *,
1208 					    struct symbol *, void *),
1209 			   void *data,
1210 			   symbol_name_match_type match,
1211 			   symbol_compare_ftype *ordered_compare)
1212 {
1213   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1214 
1215   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1216     {
1217       QUIT;
1218       if (ps->readin
1219 	  || match_partial_symbol (objfile, ps, global, name, domain, match,
1220 				   ordered_compare))
1221 	{
1222 	  struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1223 	  struct block *block;
1224 
1225 	  if (cust == NULL)
1226 	    continue;
1227 	  block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1228 	  if (map_block (name, domain, objfile, block,
1229 			 callback, data, match))
1230 	    return;
1231 	  if (callback (block, NULL, data))
1232 	    return;
1233 	}
1234     }
1235 }
1236 
1237 /* A helper for psym_expand_symtabs_matching that handles searching
1238    included psymtabs.  This returns true if a symbol is found, and
1239    false otherwise.  It also updates the 'searched_flag' on the
1240    various psymtabs that it searches.  */
1241 
1242 static bool
1243 recursively_search_psymtabs
1244   (struct partial_symtab *ps,
1245    struct objfile *objfile,
1246    enum search_domain domain,
1247    const lookup_name_info &lookup_name,
1248    gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1249 {
1250   int keep_going = 1;
1251   enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1252   int i;
1253 
1254   if (ps->searched_flag != PST_NOT_SEARCHED)
1255     return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1256 
1257   /* Recurse into shared psymtabs first, because they may have already
1258      been searched, and this could save some time.  */
1259   for (i = 0; i < ps->number_of_dependencies; ++i)
1260     {
1261       int r;
1262 
1263       /* Skip non-shared dependencies, these are handled elsewhere.  */
1264       if (ps->dependencies[i]->user == NULL)
1265 	continue;
1266 
1267       r = recursively_search_psymtabs (ps->dependencies[i],
1268 				       objfile, domain, lookup_name,
1269 				       sym_matcher);
1270       if (r != 0)
1271 	{
1272 	  ps->searched_flag = PST_SEARCHED_AND_FOUND;
1273 	  return true;
1274 	}
1275     }
1276 
1277   partial_symbol **gbound
1278     = (objfile->partial_symtabs->global_psymbols.data ()
1279        + ps->globals_offset + ps->n_global_syms);
1280   partial_symbol **sbound
1281     = (objfile->partial_symtabs->static_psymbols.data ()
1282        + ps->statics_offset + ps->n_static_syms);
1283   partial_symbol **bound = gbound;
1284 
1285   /* Go through all of the symbols stored in a partial
1286      symtab in one loop.  */
1287   partial_symbol **psym = (objfile->partial_symtabs->global_psymbols.data ()
1288 			   + ps->globals_offset);
1289   while (keep_going)
1290     {
1291       if (psym >= bound)
1292 	{
1293 	  if (bound == gbound && ps->n_static_syms != 0)
1294 	    {
1295 	      psym = (objfile->partial_symtabs->static_psymbols.data ()
1296 		      + ps->statics_offset);
1297 	      bound = sbound;
1298 	    }
1299 	  else
1300 	    keep_going = 0;
1301 	  continue;
1302 	}
1303       else
1304 	{
1305 	  QUIT;
1306 
1307 	  if ((domain == ALL_DOMAIN
1308 	       || (domain == VARIABLES_DOMAIN
1309 		   && (*psym)->aclass != LOC_TYPEDEF
1310 		   && (*psym)->aclass != LOC_BLOCK)
1311 	       || (domain == FUNCTIONS_DOMAIN
1312 		   && (*psym)->aclass == LOC_BLOCK)
1313 	       || (domain == TYPES_DOMAIN
1314 		   && (*psym)->aclass == LOC_TYPEDEF))
1315 	      && psymbol_name_matches (*psym, lookup_name)
1316 	      && (sym_matcher == NULL
1317 		  || sym_matcher (symbol_search_name (&(*psym)->ginfo))))
1318 	    {
1319 	      /* Found a match, so notify our caller.  */
1320 	      result = PST_SEARCHED_AND_FOUND;
1321 	      keep_going = 0;
1322 	    }
1323 	}
1324       psym++;
1325     }
1326 
1327   ps->searched_flag = result;
1328   return result == PST_SEARCHED_AND_FOUND;
1329 }
1330 
1331 /* Psymtab version of expand_symtabs_matching.  See its definition in
1332    the definition of quick_symbol_functions in symfile.h.  */
1333 
1334 static void
1335 psym_expand_symtabs_matching
1336   (struct objfile *objfile,
1337    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1338    const lookup_name_info &lookup_name_in,
1339    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1340    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1341    enum search_domain domain)
1342 {
1343   lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1344 
1345   /* Clear the search flags.  */
1346   for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1347     ps->searched_flag = PST_NOT_SEARCHED;
1348 
1349   for (partial_symtab *ps : objfile->psymtabs ())
1350     {
1351       QUIT;
1352 
1353       if (ps->readin)
1354 	continue;
1355 
1356       /* We skip shared psymtabs because file-matching doesn't apply
1357 	 to them; but we search them later in the loop.  */
1358       if (ps->user != NULL)
1359 	continue;
1360 
1361       if (file_matcher)
1362 	{
1363 	  bool match;
1364 
1365 	  if (ps->anonymous)
1366 	    continue;
1367 
1368 	  match = file_matcher (ps->filename, false);
1369 	  if (!match)
1370 	    {
1371 	      /* Before we invoke realpath, which can get expensive when many
1372 		 files are involved, do a quick comparison of the basenames.  */
1373 	      if (basenames_may_differ
1374 		  || file_matcher (lbasename (ps->filename), true))
1375 		match = file_matcher (psymtab_to_fullname (ps), false);
1376 	    }
1377 	  if (!match)
1378 	    continue;
1379 	}
1380 
1381       if (recursively_search_psymtabs (ps, objfile, domain,
1382 				       lookup_name, symbol_matcher))
1383 	{
1384 	  struct compunit_symtab *symtab =
1385 	    psymtab_to_symtab (objfile, ps);
1386 
1387 	  if (expansion_notify != NULL)
1388 	    expansion_notify (symtab);
1389 	}
1390     }
1391 }
1392 
1393 /* Psymtab version of has_symbols.  See its definition in
1394    the definition of quick_symbol_functions in symfile.h.  */
1395 
1396 static int
1397 psym_has_symbols (struct objfile *objfile)
1398 {
1399   return objfile->partial_symtabs->psymtabs != NULL;
1400 }
1401 
1402 /* Helper function for psym_find_compunit_symtab_by_address that fills
1403    in psymbol_map for a given range of psymbols.  */
1404 
1405 static void
1406 psym_fill_psymbol_map (struct objfile *objfile,
1407 		       struct partial_symtab *psymtab,
1408 		       std::set<CORE_ADDR> *seen_addrs,
1409 		       const std::vector<partial_symbol *> &symbols,
1410 		       int start,
1411 		       int length)
1412 {
1413   for (int i = 0; i < length; ++i)
1414     {
1415       struct partial_symbol *psym = symbols[start + i];
1416 
1417       if (psym->aclass == LOC_STATIC)
1418 	{
1419 	  CORE_ADDR addr = psym->address (objfile);
1420 	  if (seen_addrs->find (addr) == seen_addrs->end ())
1421 	    {
1422 	      seen_addrs->insert (addr);
1423 	      objfile->psymbol_map.emplace_back (addr, psymtab);
1424 	    }
1425 	}
1426     }
1427 }
1428 
1429 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1430    symfile.h.  */
1431 
1432 static compunit_symtab *
1433 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1434 				      CORE_ADDR address)
1435 {
1436   if (objfile->psymbol_map.empty ())
1437     {
1438       std::set<CORE_ADDR> seen_addrs;
1439 
1440       for (partial_symtab *pst : require_partial_symbols (objfile, 1))
1441 	{
1442 	  psym_fill_psymbol_map (objfile, pst,
1443 				 &seen_addrs,
1444 				 objfile->partial_symtabs->global_psymbols,
1445 				 pst->globals_offset,
1446 				 pst->n_global_syms);
1447 	  psym_fill_psymbol_map (objfile, pst,
1448 				 &seen_addrs,
1449 				 objfile->partial_symtabs->static_psymbols,
1450 				 pst->statics_offset,
1451 				 pst->n_static_syms);
1452 	}
1453 
1454       objfile->psymbol_map.shrink_to_fit ();
1455 
1456       std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1457 		 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1458 		     const std::pair<CORE_ADDR, partial_symtab *> &b)
1459 		 {
1460 		   return a.first < b.first;
1461 		 });
1462     }
1463 
1464   auto iter = std::lower_bound
1465     (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1466      [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1467 	 CORE_ADDR b)
1468      {
1469        return a.first < b;
1470      });
1471 
1472   if (iter == objfile->psymbol_map.end () || iter->first != address)
1473     return NULL;
1474 
1475   return psymtab_to_symtab (objfile, iter->second);
1476 }
1477 
1478 const struct quick_symbol_functions psym_functions =
1479 {
1480   psym_has_symbols,
1481   psym_find_last_source_symtab,
1482   psym_forget_cached_source_info,
1483   psym_map_symtabs_matching_filename,
1484   psym_lookup_symbol,
1485   psym_print_stats,
1486   psym_dump,
1487   psym_expand_symtabs_for_function,
1488   psym_expand_all_symtabs,
1489   psym_expand_symtabs_with_fullname,
1490   psym_map_matching_symbols,
1491   psym_expand_symtabs_matching,
1492   psym_find_pc_sect_compunit_symtab,
1493   psym_find_compunit_symtab_by_address,
1494   psym_map_symbol_filenames
1495 };
1496 
1497 
1498 
1499 static void
1500 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1501 {
1502   /* Sort the global list; don't sort the static list.  */
1503   auto begin = objfile->partial_symtabs->global_psymbols.begin ();
1504   std::advance (begin, pst->globals_offset);
1505 
1506   /* The psymbols for this partial_symtab are currently at the end of the
1507      vector.  */
1508   auto end = objfile->partial_symtabs->global_psymbols.end ();
1509 
1510   std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1511     {
1512       return strcmp_iw_ordered (symbol_search_name (&s1->ginfo),
1513 				symbol_search_name (&s2->ginfo)) < 0;
1514     });
1515 }
1516 
1517 /* Allocate and partially fill a partial symtab.  It will be
1518    completely filled at the end of the symbol list.
1519 
1520    FILENAME is the name of the symbol-file we are reading from.  */
1521 
1522 struct partial_symtab *
1523 start_psymtab_common (struct objfile *objfile,
1524 		      const char *filename,
1525 		      CORE_ADDR textlow)
1526 {
1527   struct partial_symtab *psymtab;
1528 
1529   psymtab = allocate_psymtab (filename, objfile);
1530   psymtab->set_text_low (textlow);
1531   psymtab->set_text_high (psymtab->raw_text_low ()); /* default */
1532   psymtab->globals_offset = objfile->partial_symtabs->global_psymbols.size ();
1533   psymtab->statics_offset = objfile->partial_symtabs->static_psymbols.size ();
1534   return psymtab;
1535 }
1536 
1537 /* Perform "finishing up" operations of a partial symtab.  */
1538 
1539 void
1540 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1541 {
1542   pst->n_global_syms = (objfile->partial_symtabs->global_psymbols.size ()
1543 			- pst->globals_offset);
1544   pst->n_static_syms = (objfile->partial_symtabs->static_psymbols.size ()
1545 			- pst->statics_offset);
1546 
1547   sort_pst_symbols (objfile, pst);
1548 }
1549 
1550 /* Calculate a hash code for the given partial symbol.  The hash is
1551    calculated using the symbol's value, language, domain, class
1552    and name.  These are the values which are set by
1553    add_psymbol_to_bcache.  */
1554 
1555 static unsigned long
1556 psymbol_hash (const void *addr, int length)
1557 {
1558   unsigned long h = 0;
1559   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1560   unsigned int lang = psymbol->ginfo.language;
1561   unsigned int domain = psymbol->domain;
1562   unsigned int theclass = psymbol->aclass;
1563 
1564   h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1565   h = hash_continue (&lang, sizeof (unsigned int), h);
1566   h = hash_continue (&domain, sizeof (unsigned int), h);
1567   h = hash_continue (&theclass, sizeof (unsigned int), h);
1568   /* Note that psymbol names are interned via symbol_set_names, so
1569      there's no need to hash the contents of the name here.  */
1570   h = hash_continue (&psymbol->ginfo.name,
1571 		     sizeof (psymbol->ginfo.name), h);
1572 
1573   return h;
1574 }
1575 
1576 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1577    For the comparison this function uses a symbols value,
1578    language, domain, class and name.  */
1579 
1580 static int
1581 psymbol_compare (const void *addr1, const void *addr2, int length)
1582 {
1583   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1584   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1585 
1586   return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1587                   sizeof (sym1->ginfo.value)) == 0
1588 	  && sym1->ginfo.language == sym2->ginfo.language
1589           && sym1->domain == sym2->domain
1590           && sym1->aclass == sym2->aclass
1591 	  /* Note that psymbol names are interned via
1592 	     symbol_set_names, so there's no need to compare the
1593 	     contents of the name here.  */
1594           && sym1->ginfo.name == sym2->ginfo.name);
1595 }
1596 
1597 /* Initialize a partial symbol bcache.  */
1598 
1599 struct psymbol_bcache *
1600 psymbol_bcache_init (void)
1601 {
1602   struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1603 
1604   bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1605   return bcache;
1606 }
1607 
1608 /* Free a partial symbol bcache.  */
1609 
1610 void
1611 psymbol_bcache_free (struct psymbol_bcache *bcache)
1612 {
1613   if (bcache == NULL)
1614     return;
1615 
1616   bcache_xfree (bcache->bcache);
1617   xfree (bcache);
1618 }
1619 
1620 /* Return the internal bcache of the psymbol_bcache BCACHE.  */
1621 
1622 struct bcache *
1623 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1624 {
1625   return bcache->bcache;
1626 }
1627 
1628 /* Find a copy of the SYM in BCACHE.  If BCACHE has never seen this
1629    symbol before, add a copy to BCACHE.  In either case, return a pointer
1630    to BCACHE's copy of the symbol.  If optional ADDED is not NULL, return
1631    1 in case of new entry or 0 if returning an old entry.  */
1632 
1633 static struct partial_symbol *
1634 psymbol_bcache_full (struct partial_symbol *sym,
1635                      struct psymbol_bcache *bcache,
1636                      int *added)
1637 {
1638   return ((struct partial_symbol *)
1639 	  bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1640 		       added));
1641 }
1642 
1643 /* Helper function, initialises partial symbol structure and stashes
1644    it into objfile's bcache.  Note that our caching mechanism will
1645    use all fields of struct partial_symbol to determine hash value of the
1646    structure.  In other words, having two symbols with the same name but
1647    different domain (or address) is possible and correct.  */
1648 
1649 static struct partial_symbol *
1650 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1651 		       domain_enum domain,
1652 		       enum address_class theclass,
1653 		       short section,
1654 		       CORE_ADDR coreaddr,
1655 		       enum language language, struct objfile *objfile,
1656 		       int *added)
1657 {
1658   struct partial_symbol psymbol;
1659   memset (&psymbol, 0, sizeof (psymbol));
1660 
1661   psymbol.set_unrelocated_address (coreaddr);
1662   psymbol.ginfo.section = section;
1663   psymbol.domain = domain;
1664   psymbol.aclass = theclass;
1665   symbol_set_language (&psymbol.ginfo, language,
1666 		       objfile->partial_symtabs->obstack ());
1667   symbol_set_names (&psymbol.ginfo, name, namelength, copy_name,
1668 		    objfile->per_bfd);
1669 
1670   /* Stash the partial symbol away in the cache.  */
1671   return psymbol_bcache_full (&psymbol,
1672 			      objfile->partial_symtabs->psymbol_cache,
1673 			      added);
1674 }
1675 
1676 /* Helper function, adds partial symbol to the given partial symbol list.  */
1677 
1678 static void
1679 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1680 			struct partial_symbol *psym,
1681 			struct objfile *objfile)
1682 {
1683   list->push_back (psym);
1684   OBJSTAT (objfile, n_psyms++);
1685 }
1686 
1687 /* Add a symbol with a long value to a psymtab.
1688    Since one arg is a struct, we pass in a ptr and deref it (sigh).
1689    The only value we need to store for psyms is an address.
1690    For all other psyms pass zero for COREADDR.
1691    Return the partial symbol that has been added.  */
1692 
1693 void
1694 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1695 		     domain_enum domain,
1696 		     enum address_class theclass,
1697 		     short section,
1698 		     psymbol_placement where,
1699 		     CORE_ADDR coreaddr,
1700 		     enum language language, struct objfile *objfile)
1701 {
1702   struct partial_symbol *psym;
1703 
1704   int added;
1705 
1706   /* Stash the partial symbol away in the cache.  */
1707   psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1708 				section, coreaddr, language, objfile, &added);
1709 
1710   /* Do not duplicate global partial symbols.  */
1711   if (where == psymbol_placement::GLOBAL && !added)
1712     return;
1713 
1714   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
1715   std::vector<partial_symbol *> *list
1716     = (where == psymbol_placement::STATIC
1717        ? &objfile->partial_symtabs->static_psymbols
1718        : &objfile->partial_symtabs->global_psymbols);
1719   append_psymbol_to_list (list, psym, objfile);
1720 }
1721 
1722 /* See psympriv.h.  */
1723 
1724 void
1725 init_psymbol_list (struct objfile *objfile, int total_symbols)
1726 {
1727   if (objfile->partial_symtabs->global_psymbols.capacity () == 0
1728       && objfile->partial_symtabs->static_psymbols.capacity () == 0)
1729     {
1730       /* Current best guess is that approximately a twentieth of the
1731 	 total symbols (in a debugging file) are global or static
1732 	 oriented symbols, then multiply that by slop factor of
1733 	 two.  */
1734       objfile->partial_symtabs->global_psymbols.reserve (total_symbols / 10);
1735       objfile->partial_symtabs->static_psymbols.reserve (total_symbols / 10);
1736     }
1737 }
1738 
1739 /* See psympriv.h.  */
1740 
1741 struct partial_symtab *
1742 allocate_psymtab (const char *filename, struct objfile *objfile)
1743 {
1744   struct partial_symtab *psymtab
1745     = objfile->partial_symtabs->allocate_psymtab ();
1746 
1747   psymtab->filename
1748     = (const char *) bcache (filename, strlen (filename) + 1,
1749 			     objfile->per_bfd->filename_cache);
1750   psymtab->compunit_symtab = NULL;
1751 
1752   if (symtab_create_debug)
1753     {
1754       /* Be a bit clever with debugging messages, and don't print objfile
1755 	 every time, only when it changes.  */
1756       static char *last_objfile_name = NULL;
1757 
1758       if (last_objfile_name == NULL
1759 	  || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1760 	{
1761 	  xfree (last_objfile_name);
1762 	  last_objfile_name = xstrdup (objfile_name (objfile));
1763 	  fprintf_filtered (gdb_stdlog,
1764 			    "Creating one or more psymtabs for objfile %s ...\n",
1765 			    last_objfile_name);
1766 	}
1767       fprintf_filtered (gdb_stdlog,
1768 			"Created psymtab %s for module %s.\n",
1769 			host_address_to_string (psymtab), filename);
1770     }
1771 
1772   return psymtab;
1773 }
1774 
1775 void
1776 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1777 {
1778   struct partial_symtab **prev_pst;
1779 
1780   /* From dbxread.c:
1781      Empty psymtabs happen as a result of header files which don't
1782      have any symbols in them.  There can be a lot of them.  But this
1783      check is wrong, in that a psymtab with N_SLINE entries but
1784      nothing else is not empty, but we don't realize that.  Fixing
1785      that without slowing things down might be tricky.  */
1786 
1787   /* First, snip it out of the psymtab chain.  */
1788 
1789   prev_pst = &psymtabs;
1790   while ((*prev_pst) != pst)
1791     prev_pst = &((*prev_pst)->next);
1792   (*prev_pst) = pst->next;
1793 
1794   /* Next, put it on a free list for recycling.  */
1795 
1796   pst->next = free_psymtabs;
1797   free_psymtabs = pst;
1798 }
1799 
1800 
1801 
1802 /* We need to pass a couple of items to the addrmap_foreach function,
1803    so use a struct.  */
1804 
1805 struct dump_psymtab_addrmap_data
1806 {
1807   struct objfile *objfile;
1808   struct partial_symtab *psymtab;
1809   struct ui_file *outfile;
1810 
1811   /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1812      If so, we want to print the next one as well (since the next addrmap
1813      entry defines the end of the range).  */
1814   int previous_matched;
1815 };
1816 
1817 /* Helper function for dump_psymtab_addrmap to print an addrmap entry.  */
1818 
1819 static int
1820 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1821 {
1822   struct dump_psymtab_addrmap_data *data
1823     = (struct dump_psymtab_addrmap_data *) datap;
1824   struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1825   struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1826   const char *psymtab_address_or_end = NULL;
1827 
1828   QUIT;
1829 
1830   if (data->psymtab == NULL
1831       || data->psymtab == addrmap_psymtab)
1832     psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1833   else if (data->previous_matched)
1834     psymtab_address_or_end = "<ends here>";
1835 
1836   if (data->psymtab == NULL
1837       || data->psymtab == addrmap_psymtab
1838       || data->previous_matched)
1839     {
1840       fprintf_filtered (data->outfile, "  %s%s %s\n",
1841 			data->psymtab != NULL ? "  " : "",
1842 			paddress (gdbarch, start_addr),
1843 			psymtab_address_or_end);
1844     }
1845 
1846   data->previous_matched = (data->psymtab == NULL
1847 			    || data->psymtab == addrmap_psymtab);
1848 
1849   return 0;
1850 }
1851 
1852 /* Helper function for maintenance_print_psymbols to print the addrmap
1853    of PSYMTAB.  If PSYMTAB is NULL print the entire addrmap.  */
1854 
1855 static void
1856 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1857 		      struct ui_file *outfile)
1858 {
1859   struct dump_psymtab_addrmap_data addrmap_dump_data;
1860 
1861   if ((psymtab == NULL
1862        || psymtab->psymtabs_addrmap_supported)
1863       && objfile->partial_symtabs->psymtabs_addrmap != NULL)
1864     {
1865       addrmap_dump_data.objfile = objfile;
1866       addrmap_dump_data.psymtab = psymtab;
1867       addrmap_dump_data.outfile = outfile;
1868       addrmap_dump_data.previous_matched = 0;
1869       fprintf_filtered (outfile, "%sddress map:\n",
1870 			psymtab == NULL ? "Entire a" : "  A");
1871       addrmap_foreach (objfile->partial_symtabs->psymtabs_addrmap,
1872 		       dump_psymtab_addrmap_1, &addrmap_dump_data);
1873     }
1874 }
1875 
1876 static void
1877 maintenance_print_psymbols (const char *args, int from_tty)
1878 {
1879   struct ui_file *outfile = gdb_stdout;
1880   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1881   int i, outfile_idx, found;
1882   CORE_ADDR pc = 0;
1883   struct obj_section *section = NULL;
1884 
1885   dont_repeat ();
1886 
1887   gdb_argv argv (args);
1888 
1889   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1890     {
1891       if (strcmp (argv[i], "-pc") == 0)
1892 	{
1893 	  if (argv[i + 1] == NULL)
1894 	    error (_("Missing pc value"));
1895 	  address_arg = argv[++i];
1896 	}
1897       else if (strcmp (argv[i], "-source") == 0)
1898 	{
1899 	  if (argv[i + 1] == NULL)
1900 	    error (_("Missing source file"));
1901 	  source_arg = argv[++i];
1902 	}
1903       else if (strcmp (argv[i], "-objfile") == 0)
1904 	{
1905 	  if (argv[i + 1] == NULL)
1906 	    error (_("Missing objfile name"));
1907 	  objfile_arg = argv[++i];
1908 	}
1909       else if (strcmp (argv[i], "--") == 0)
1910 	{
1911 	  /* End of options.  */
1912 	  ++i;
1913 	  break;
1914 	}
1915       else if (argv[i][0] == '-')
1916 	{
1917 	  /* Future proofing: Don't allow OUTFILE to begin with "-".  */
1918 	  error (_("Unknown option: %s"), argv[i]);
1919 	}
1920       else
1921 	break;
1922     }
1923   outfile_idx = i;
1924 
1925   if (address_arg != NULL && source_arg != NULL)
1926     error (_("Must specify at most one of -pc and -source"));
1927 
1928   stdio_file arg_outfile;
1929 
1930   if (argv != NULL && argv[outfile_idx] != NULL)
1931     {
1932       if (argv[outfile_idx + 1] != NULL)
1933 	error (_("Junk at end of command"));
1934       gdb::unique_xmalloc_ptr<char> outfile_name
1935 	(tilde_expand (argv[outfile_idx]));
1936       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1937 	perror_with_name (outfile_name.get ());
1938       outfile = &arg_outfile;
1939     }
1940 
1941   if (address_arg != NULL)
1942     {
1943       pc = parse_and_eval_address (address_arg);
1944       /* If we fail to find a section, that's ok, try the lookup anyway.  */
1945       section = find_pc_section (pc);
1946     }
1947 
1948   found = 0;
1949   for (objfile *objfile : current_program_space->objfiles ())
1950     {
1951       int printed_objfile_header = 0;
1952       int print_for_objfile = 1;
1953 
1954       QUIT;
1955       if (objfile_arg != NULL)
1956 	print_for_objfile
1957 	  = compare_filenames_for_search (objfile_name (objfile),
1958 					  objfile_arg);
1959       if (!print_for_objfile)
1960 	continue;
1961 
1962       if (address_arg != NULL)
1963 	{
1964 	  struct bound_minimal_symbol msymbol = { NULL, NULL };
1965 
1966 	  /* We don't assume each pc has a unique objfile (this is for
1967 	     debugging).  */
1968 	  struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
1969 							    section, msymbol);
1970 	  if (ps != NULL)
1971 	    {
1972 	      if (!printed_objfile_header)
1973 		{
1974 		  outfile->printf ("\nPartial symtabs for objfile %s\n",
1975 				  objfile_name (objfile));
1976 		  printed_objfile_header = 1;
1977 		}
1978 	      dump_psymtab (objfile, ps, outfile);
1979 	      dump_psymtab_addrmap (objfile, ps, outfile);
1980 	      found = 1;
1981 	    }
1982 	}
1983       else
1984 	{
1985 	  for (partial_symtab *ps : require_partial_symbols (objfile, 1))
1986 	    {
1987 	      int print_for_source = 0;
1988 
1989 	      QUIT;
1990 	      if (source_arg != NULL)
1991 		{
1992 		  print_for_source
1993 		    = compare_filenames_for_search (ps->filename, source_arg);
1994 		  found = 1;
1995 		}
1996 	      if (source_arg == NULL
1997 		  || print_for_source)
1998 		{
1999 		  if (!printed_objfile_header)
2000 		    {
2001 		      outfile->printf ("\nPartial symtabs for objfile %s\n",
2002 				       objfile_name (objfile));
2003 		      printed_objfile_header = 1;
2004 		    }
2005 		  dump_psymtab (objfile, ps, outfile);
2006 		  dump_psymtab_addrmap (objfile, ps, outfile);
2007 		}
2008 	    }
2009 	}
2010 
2011       /* If we're printing all the objfile's symbols dump the full addrmap.  */
2012 
2013       if (address_arg == NULL
2014 	  && source_arg == NULL
2015 	  && objfile->partial_symtabs->psymtabs_addrmap != NULL)
2016 	{
2017 	  outfile->puts ("\n");
2018 	  dump_psymtab_addrmap (objfile, NULL, outfile);
2019 	}
2020     }
2021 
2022   if (!found)
2023     {
2024       if (address_arg != NULL)
2025 	error (_("No partial symtab for address: %s"), address_arg);
2026       if (source_arg != NULL)
2027 	error (_("No partial symtab for source file: %s"), source_arg);
2028     }
2029 }
2030 
2031 /* List all the partial symbol tables whose names match REGEXP (optional).  */
2032 
2033 static void
2034 maintenance_info_psymtabs (const char *regexp, int from_tty)
2035 {
2036   struct program_space *pspace;
2037 
2038   if (regexp)
2039     re_comp (regexp);
2040 
2041   ALL_PSPACES (pspace)
2042     for (objfile *objfile : pspace->objfiles ())
2043       {
2044 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
2045 
2046 	/* We don't want to print anything for this objfile until we
2047 	   actually find a symtab whose name matches.  */
2048 	int printed_objfile_start = 0;
2049 
2050 	for (partial_symtab *psymtab : require_partial_symbols (objfile, 1))
2051 	  {
2052 	    QUIT;
2053 
2054 	    if (! regexp
2055 		|| re_exec (psymtab->filename))
2056 	      {
2057 		if (! printed_objfile_start)
2058 		  {
2059 		    printf_filtered ("{ objfile %s ", objfile_name (objfile));
2060 		    wrap_here ("  ");
2061 		    printf_filtered ("((struct objfile *) %s)\n",
2062 				     host_address_to_string (objfile));
2063 		    printed_objfile_start = 1;
2064 		  }
2065 
2066 		printf_filtered ("  { psymtab %s ", psymtab->filename);
2067 		wrap_here ("    ");
2068 		printf_filtered ("((struct partial_symtab *) %s)\n",
2069 				 host_address_to_string (psymtab));
2070 
2071 		printf_filtered ("    readin %s\n",
2072 				 psymtab->readin ? "yes" : "no");
2073 		printf_filtered ("    fullname %s\n",
2074 				 psymtab->fullname
2075 				 ? psymtab->fullname : "(null)");
2076 		printf_filtered ("    text addresses ");
2077 		fputs_filtered (paddress (gdbarch,
2078 					  psymtab->text_low (objfile)),
2079 				gdb_stdout);
2080 		printf_filtered (" -- ");
2081 		fputs_filtered (paddress (gdbarch,
2082 					  psymtab->text_high (objfile)),
2083 				gdb_stdout);
2084 		printf_filtered ("\n");
2085 		printf_filtered ("    psymtabs_addrmap_supported %s\n",
2086 				 (psymtab->psymtabs_addrmap_supported
2087 				  ? "yes" : "no"));
2088 		printf_filtered ("    globals ");
2089 		if (psymtab->n_global_syms)
2090 		  {
2091 		    auto p = &(objfile->partial_symtabs
2092 			       ->global_psymbols[psymtab->globals_offset]);
2093 
2094 		    printf_filtered
2095 		      ("(* (struct partial_symbol **) %s @ %d)\n",
2096 		       host_address_to_string (p),
2097 		       psymtab->n_global_syms);
2098 		  }
2099 		else
2100 		  printf_filtered ("(none)\n");
2101 		printf_filtered ("    statics ");
2102 		if (psymtab->n_static_syms)
2103 		  {
2104 		    auto p = &(objfile->partial_symtabs
2105 			       ->static_psymbols[psymtab->statics_offset]);
2106 
2107 		    printf_filtered
2108 		      ("(* (struct partial_symbol **) %s @ %d)\n",
2109 		       host_address_to_string (p),
2110 		       psymtab->n_static_syms);
2111 		  }
2112 		else
2113 		  printf_filtered ("(none)\n");
2114 		printf_filtered ("    dependencies ");
2115 		if (psymtab->number_of_dependencies)
2116 		  {
2117 		    int i;
2118 
2119 		    printf_filtered ("{\n");
2120 		    for (i = 0; i < psymtab->number_of_dependencies; i++)
2121 		      {
2122 			struct partial_symtab *dep = psymtab->dependencies[i];
2123 
2124 			/* Note the string concatenation there --- no
2125 			   comma.  */
2126 			printf_filtered ("      psymtab %s "
2127 					 "((struct partial_symtab *) %s)\n",
2128 					 dep->filename,
2129 					 host_address_to_string (dep));
2130 		      }
2131 		    printf_filtered ("    }\n");
2132 		  }
2133 		else
2134 		  printf_filtered ("(none)\n");
2135 		printf_filtered ("  }\n");
2136 	      }
2137 	  }
2138 
2139 	if (printed_objfile_start)
2140 	  printf_filtered ("}\n");
2141       }
2142 }
2143 
2144 /* Check consistency of currently expanded psymtabs vs symtabs.  */
2145 
2146 static void
2147 maintenance_check_psymtabs (const char *ignore, int from_tty)
2148 {
2149   struct symbol *sym;
2150   struct compunit_symtab *cust = NULL;
2151   const struct blockvector *bv;
2152   struct block *b;
2153   int length;
2154 
2155   for (objfile *objfile : current_program_space->objfiles ())
2156     for (partial_symtab *ps : require_partial_symbols (objfile, 1))
2157       {
2158 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
2159 
2160 	/* We don't call psymtab_to_symtab here because that may cause symtab
2161 	   expansion.  When debugging a problem it helps if checkers leave
2162 	   things unchanged.  */
2163 	cust = ps->compunit_symtab;
2164 
2165 	/* First do some checks that don't require the associated symtab.  */
2166 	if (ps->text_high (objfile) < ps->text_low (objfile))
2167 	  {
2168 	    printf_filtered ("Psymtab ");
2169 	    puts_filtered (ps->filename);
2170 	    printf_filtered (" covers bad range ");
2171 	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2172 			    gdb_stdout);
2173 	    printf_filtered (" - ");
2174 	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2175 			    gdb_stdout);
2176 	    printf_filtered ("\n");
2177 	    continue;
2178 	  }
2179 
2180 	/* Now do checks requiring the associated symtab.  */
2181 	if (cust == NULL)
2182 	  continue;
2183 	bv = COMPUNIT_BLOCKVECTOR (cust);
2184 	b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2185 	partial_symbol **psym
2186 	  = &objfile->partial_symtabs->static_psymbols[ps->statics_offset];
2187 	length = ps->n_static_syms;
2188 	while (length--)
2189 	  {
2190 	    sym = block_lookup_symbol (b, symbol_search_name (&(*psym)->ginfo),
2191 				       symbol_name_match_type::SEARCH_NAME,
2192 				       (*psym)->domain);
2193 	    if (!sym)
2194 	      {
2195 		printf_filtered ("Static symbol `");
2196 		puts_filtered ((*psym)->ginfo.name);
2197 		printf_filtered ("' only found in ");
2198 		puts_filtered (ps->filename);
2199 		printf_filtered (" psymtab\n");
2200 	      }
2201 	    psym++;
2202 	  }
2203 	b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2204 	psym = &objfile->partial_symtabs->global_psymbols[ps->globals_offset];
2205 	length = ps->n_global_syms;
2206 	while (length--)
2207 	  {
2208 	    sym = block_lookup_symbol (b, symbol_search_name (&(*psym)->ginfo),
2209 				       symbol_name_match_type::SEARCH_NAME,
2210 				       (*psym)->domain);
2211 	    if (!sym)
2212 	      {
2213 		printf_filtered ("Global symbol `");
2214 		puts_filtered ((*psym)->ginfo.name);
2215 		printf_filtered ("' only found in ");
2216 		puts_filtered (ps->filename);
2217 		printf_filtered (" psymtab\n");
2218 	      }
2219 	    psym++;
2220 	  }
2221 	if (ps->raw_text_high () != 0
2222 	    && (ps->text_low (objfile) < BLOCK_START (b)
2223 		|| ps->text_high (objfile) > BLOCK_END (b)))
2224 	  {
2225 	    printf_filtered ("Psymtab ");
2226 	    puts_filtered (ps->filename);
2227 	    printf_filtered (" covers ");
2228 	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2229 			    gdb_stdout);
2230 	    printf_filtered (" - ");
2231 	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2232 			    gdb_stdout);
2233 	    printf_filtered (" but symtab covers only ");
2234 	    fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2235 	    printf_filtered (" - ");
2236 	    fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2237 	    printf_filtered ("\n");
2238 	  }
2239       }
2240 }
2241 
2242 void
2243 _initialize_psymtab (void)
2244 {
2245   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2246 Print dump of current partial symbol definitions.\n\
2247 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2248        mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2249 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2250 or the terminal if OUTFILE is unspecified.\n\
2251 If ADDRESS is provided, dump only the file for that address.\n\
2252 If SOURCE is provided, dump only that file's symbols.\n\
2253 If OBJFILE is provided, dump only that file's minimal symbols."),
2254 	   &maintenanceprintlist);
2255 
2256   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2257 List the partial symbol tables for all object files.\n\
2258 This does not include information about individual partial symbols,\n\
2259 just the symbol table structures themselves."),
2260 	   &maintenanceinfolist);
2261 
2262   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2263 	   _("\
2264 Check consistency of currently expanded psymtabs versus symtabs."),
2265 	   &maintenancelist);
2266 }
2267