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