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