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