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