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