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