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