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